| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Copyright (C) 1999 - 2005, Digium, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  |  * \brief Execute an ISDN RAS | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * \author Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  *  | 
					
						
							| 
									
										
										
										
											2005-11-06 15:09:47 +00:00
										 |  |  |  * \ingroup applications | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-24 17:11:45 +00:00
										 |  |  | /*** MODULEINFO
 | 
					
						
							| 
									
										
										
										
											2008-06-12 17:27:55 +00:00
										 |  |  | 	<depend>dahdi</depend> | 
					
						
							| 
									
										
										
										
											2011-07-14 20:13:06 +00:00
										 |  |  | 	<support_level>extended</support_level> | 
					
						
							| 
									
										
										
										
											2006-04-24 17:11:45 +00:00
										 |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | #include <sys/ioctl.h>
 | 
					
						
							|  |  |  | #include <sys/wait.h>
 | 
					
						
							| 
									
										
										
										
											2004-06-07 19:12:14 +00:00
										 |  |  | #ifdef __linux__
 | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | #include <sys/signal.h>
 | 
					
						
							| 
									
										
										
										
											2004-06-07 19:12:14 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | #include <signal.h>
 | 
					
						
							|  |  |  | #endif /* __linux__ */
 | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-04 15:48:42 +00:00
										 |  |  | #include <fcntl.h>
 | 
					
						
							| 
									
										
										
										
											2007-01-19 16:16:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-25 23:05:28 +00:00
										 |  |  | #include <dahdi/user.h>
 | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-06-06 22:39:32 +00:00
										 |  |  | #include "asterisk/lock.h"
 | 
					
						
							|  |  |  | #include "asterisk/file.h"
 | 
					
						
							|  |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/pbx.h"
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							| 
									
										
										
										
											2008-04-16 22:57:54 +00:00
										 |  |  | #include "asterisk/app.h"
 | 
					
						
							| 
									
										
										
										
											2005-06-06 22:39:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | /*** DOCUMENTATION
 | 
					
						
							|  |  |  | 	<application name="DAHDIRAS" language="en_US"> | 
					
						
							|  |  |  | 		<synopsis> | 
					
						
							|  |  |  | 			Executes DAHDI ISDN RAS application. | 
					
						
							|  |  |  | 		</synopsis> | 
					
						
							|  |  |  | 		<syntax> | 
					
						
							|  |  |  | 			<parameter name="args" required="true"> | 
					
						
							|  |  |  | 				<para>A list of parameters to pass to the pppd daemon, | 
					
						
							| 
									
										
										
										
											2008-11-02 02:50:33 +00:00
										 |  |  | 				separated by <literal>,</literal> characters.</para> | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | 			</parameter> | 
					
						
							|  |  |  | 		</syntax> | 
					
						
							|  |  |  | 		<description> | 
					
						
							|  |  |  | 			<para>Executes a RAS server using pppd on the given channel. | 
					
						
							|  |  |  | 			The channel must be a clear channel (i.e. PRI source) and a DAHDI | 
					
						
							|  |  |  | 			channel to be able to use this function (No modem emulation is included).</para> | 
					
						
							|  |  |  | 			<para>Your pppd must be patched to be DAHDI aware.</para> | 
					
						
							|  |  |  | 		</description> | 
					
						
							|  |  |  | 	</application> | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  |  ***/ | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-07 14:55:51 +00:00
										 |  |  | static const char app[] = "DAHDIRAS"; | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define PPP_MAX_ARGS	32
 | 
					
						
							|  |  |  | #define PPP_EXEC	"/usr/sbin/pppd"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static pid_t spawn_ras(struct ast_channel *chan, char *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	pid_t pid; | 
					
						
							|  |  |  | 	char *c; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	char *argv[PPP_MAX_ARGS]; | 
					
						
							|  |  |  | 	int argc = 0; | 
					
						
							| 
									
										
										
										
											2003-02-04 15:48:42 +00:00
										 |  |  | 	char *stringp=NULL; | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Start by forking */ | 
					
						
							| 
									
										
										
										
											2008-04-16 22:57:54 +00:00
										 |  |  | 	pid = ast_safe_fork(1); | 
					
						
							| 
									
										
										
										
											2006-12-11 00:52:19 +00:00
										 |  |  | 	if (pid) { | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 		return pid; | 
					
						
							| 
									
										
										
										
											2006-12-11 00:52:19 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 	/* Execute RAS on File handles */ | 
					
						
							|  |  |  | 	dup2(chan->fds[0], STDIN_FILENO); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-01 21:48:30 +00:00
										 |  |  | 	/* Drop high priority */ | 
					
						
							|  |  |  | 	if (ast_opt_high_priority) | 
					
						
							|  |  |  | 		ast_set_priority(0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 	/* Close other file descriptors */ | 
					
						
							| 
									
										
										
										
											2008-04-16 22:57:54 +00:00
										 |  |  | 	ast_close_fds_above_n(STDERR_FILENO); | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Reset all arguments */ | 
					
						
							|  |  |  | 	memset(argv, 0, sizeof(argv)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* First argument is executable, followed by standard
 | 
					
						
							| 
									
										
										
										
											2008-06-12 17:27:55 +00:00
										 |  |  | 	   arguments for DAHDI PPP */ | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 	argv[argc++] = PPP_EXEC; | 
					
						
							|  |  |  | 	argv[argc++] = "nodetach"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* And all the other arguments */ | 
					
						
							| 
									
										
										
										
											2003-02-04 15:48:42 +00:00
										 |  |  | 	stringp=args; | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	c = strsep(&stringp, ","); | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 	while(c && strlen(c) && (argc < (PPP_MAX_ARGS - 4))) { | 
					
						
							|  |  |  | 		argv[argc++] = c; | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 		c = strsep(&stringp, ","); | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	argv[argc++] = "plugin"; | 
					
						
							| 
									
										
										
										
											2008-06-12 17:27:55 +00:00
										 |  |  | 	argv[argc++] = "dahdi.so"; | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 	argv[argc++] = "stdin"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Finally launch PPP */ | 
					
						
							|  |  |  | 	execv(PPP_EXEC, argv); | 
					
						
							|  |  |  | 	fprintf(stderr, "Failed to exec PPPD!\n"); | 
					
						
							|  |  |  | 	exit(1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void run_ras(struct ast_channel *chan, char *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	pid_t pid; | 
					
						
							|  |  |  | 	int status; | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	int signalled = 0; | 
					
						
							| 
									
										
										
										
											2008-06-12 17:27:55 +00:00
										 |  |  | 	struct dahdi_bufferinfo savebi; | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 	int x; | 
					
						
							| 
									
										
										
										
											2005-07-06 01:41:54 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2008-06-12 17:27:55 +00:00
										 |  |  | 	res = ioctl(chan->fds[0], DAHDI_GET_BUFINFO, &savebi); | 
					
						
							| 
									
										
										
										
											2005-07-06 01:41:54 +00:00
										 |  |  | 	if(res) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Unable to check buffer policy on channel %s\n", chan->name); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	pid = spawn_ras(chan, args); | 
					
						
							|  |  |  | 	if (pid < 0) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Failed to spawn RAS\n"); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		for (;;) { | 
					
						
							|  |  |  | 			res = wait4(pid, &status, WNOHANG, NULL); | 
					
						
							|  |  |  | 			if (!res) { | 
					
						
							|  |  |  | 				/* Check for hangup */ | 
					
						
							| 
									
										
										
										
											2007-08-01 15:39:54 +00:00
										 |  |  | 				if (ast_check_hangup(chan) && !signalled) { | 
					
						
							| 
									
										
										
										
											2007-06-14 19:39:12 +00:00
										 |  |  | 					ast_debug(1, "Channel '%s' hungup.  Signalling RAS at %d to die...\n", chan->name, pid); | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 					kill(pid, SIGTERM); | 
					
						
							|  |  |  | 					signalled=1; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				/* Try again */ | 
					
						
							|  |  |  | 				sleep(1); | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (res < 0) { | 
					
						
							|  |  |  | 				ast_log(LOG_WARNING, "wait4 returned %d: %s\n", res, strerror(errno)); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2008-03-12 05:46:39 +00:00
										 |  |  | 			if (WIFEXITED(status)) { | 
					
						
							| 
									
										
										
										
											2008-02-05 23:00:15 +00:00
										 |  |  | 				ast_verb(3, "RAS on %s terminated with status %d\n", chan->name, WEXITSTATUS(status)); | 
					
						
							| 
									
										
										
										
											2008-03-12 05:46:39 +00:00
										 |  |  | 			} else if (WIFSIGNALED(status)) { | 
					
						
							|  |  |  | 				ast_verb(3, "RAS on %s terminated with signal %d\n",  | 
					
						
							|  |  |  | 					 chan->name, WTERMSIG(status)); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				ast_verb(3, "RAS on %s terminated weirdly.\n", chan->name); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 			/* Throw back into audio mode */ | 
					
						
							| 
									
										
										
										
											2002-05-13 22:29:39 +00:00
										 |  |  | 			x = 1; | 
					
						
							| 
									
										
										
										
											2008-06-12 17:27:55 +00:00
										 |  |  | 			ioctl(chan->fds[0], DAHDI_AUDIOMODE, &x); | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-07-06 01:41:54 +00:00
										 |  |  | 			/* Restore saved values */ | 
					
						
							| 
									
										
										
										
											2008-06-12 17:27:55 +00:00
										 |  |  | 			res = ioctl(chan->fds[0], DAHDI_SET_BUFINFO, &savebi); | 
					
						
							| 
									
										
										
										
											2005-07-06 01:41:54 +00:00
										 |  |  | 			if (res < 0) { | 
					
						
							|  |  |  | 				ast_log(LOG_WARNING, "Unable to set buffer policy on channel %s\n", chan->name); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-04-16 22:57:54 +00:00
										 |  |  | 	ast_safe_fork_cleanup(); | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-21 21:13:09 +00:00
										 |  |  | static int dahdiras_exec(struct ast_channel *chan, const char *data) | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int res=-1; | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 	char *args; | 
					
						
							| 
									
										
										
										
											2008-07-29 22:22:13 +00:00
										 |  |  | 	struct dahdi_params dahdip; | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!data)  | 
					
						
							|  |  |  | 		data = ""; | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-10 13:22:15 +00:00
										 |  |  | 	args = ast_strdupa(data); | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 	/* Answer the channel if it's not up */ | 
					
						
							| 
									
										
										
										
											2002-07-12 09:03:50 +00:00
										 |  |  | 	if (chan->_state != AST_STATE_UP) | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 		ast_answer(chan); | 
					
						
							| 
									
										
										
										
											2008-06-12 17:27:55 +00:00
										 |  |  | 	if (strcasecmp(chan->tech->type, "DAHDI")) { | 
					
						
							|  |  |  | 		/* If it's not a DAHDI channel, we're done.  Wait a couple of
 | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 		   seconds and then hangup... */ | 
					
						
							| 
									
										
										
										
											2008-06-12 17:27:55 +00:00
										 |  |  | 		ast_verb(2, "Channel %s is not a DAHDI channel\n", chan->name); | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 		sleep(2); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2008-06-12 17:27:55 +00:00
										 |  |  | 		memset(&dahdip, 0, sizeof(dahdip)); | 
					
						
							|  |  |  | 		if (ioctl(chan->fds[0], DAHDI_GET_PARAMS, &dahdip)) { | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "Unable to get DAHDI parameters\n"); | 
					
						
							|  |  |  | 		} else if (dahdip.sigtype != DAHDI_SIG_CLEAR) { | 
					
						
							| 
									
										
										
										
											2007-07-26 15:49:18 +00:00
										 |  |  | 			ast_verb(2, "Channel %s is not a clear channel\n", chan->name); | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			/* Everything should be okay.  Run PPP. */ | 
					
						
							| 
									
										
										
										
											2007-07-26 15:49:18 +00:00
										 |  |  | 			ast_verb(3, "Starting RAS on %s\n", chan->name); | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 			/* Execute RAS */ | 
					
						
							|  |  |  | 			run_ras(chan, args); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int unload_module(void)  | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-07-16 13:35:20 +00:00
										 |  |  | 	return ast_unregister_application(app); | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | 	return ((ast_register_application_xml(app, dahdiras_exec)) ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS); | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-12 17:27:55 +00:00
										 |  |  | AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "DAHDI ISDN Remote Access Server"); | 
					
						
							| 
									
										
										
										
											2006-04-24 17:11:45 +00:00
										 |  |  | 
 |