| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 	<depend>zaptel</depend> | 
					
						
							|  |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2003-02-04 15:48:42 +00:00
										 |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <fcntl.h>
 | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Need some zaptel help here */ | 
					
						
							| 
									
										
										
										
											2004-06-04 15:44:14 +00:00
										 |  |  | #ifdef __linux__
 | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | #include <linux/zaptel.h>
 | 
					
						
							| 
									
										
										
										
											2004-06-04 15:44:14 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | #include <zaptel.h>
 | 
					
						
							|  |  |  | #endif /* __linux__ */
 | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-06-06 22:39:32 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "asterisk/lock.h"
 | 
					
						
							|  |  |  | #include "asterisk/file.h"
 | 
					
						
							|  |  |  | #include "asterisk/logger.h"
 | 
					
						
							|  |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/pbx.h"
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | #include "asterisk/options.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | static char *tdesc = "Zap RAS Application"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *app = "ZapRAS"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *synopsis = "Executes Zaptel ISDN RAS application"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *descrip = | 
					
						
							|  |  |  | "  ZapRAS(args): Executes a RAS server using pppd on the given channel.\n" | 
					
						
							|  |  |  | "The channel must be a clear channel (i.e. PRI source) and a Zaptel\n" | 
					
						
							| 
									
										
										
										
											2004-02-27 06:00:35 +00:00
										 |  |  | "channel to be able to use this function (No modem emulation is included).\n" | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | "Your pppd must be patched to be zaptel aware. Arguments should be\n" | 
					
						
							| 
									
										
										
										
											2005-11-07 22:01:22 +00:00
										 |  |  | "separated by | characters.\n"; | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | LOCAL_USER_DECL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #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; | 
					
						
							|  |  |  | 	int x;	 | 
					
						
							|  |  |  | 	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 */ | 
					
						
							|  |  |  | 	pid = fork(); | 
					
						
							|  |  |  | 	if (pid) | 
					
						
							|  |  |  | 		return pid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* 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 */ | 
					
						
							|  |  |  | 	for (x=STDERR_FILENO + 1;x<1024;x++)  | 
					
						
							|  |  |  | 		close(x); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Restore original signal handlers */ | 
					
						
							|  |  |  | 	for (x=0;x<NSIG;x++) | 
					
						
							|  |  |  | 		signal(x, SIG_DFL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Reset all arguments */ | 
					
						
							|  |  |  | 	memset(argv, 0, sizeof(argv)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* First argument is executable, followed by standard
 | 
					
						
							|  |  |  | 	   arguments for zaptel PPP */ | 
					
						
							|  |  |  | 	argv[argc++] = PPP_EXEC; | 
					
						
							|  |  |  | 	argv[argc++] = "nodetach"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* And all the other arguments */ | 
					
						
							| 
									
										
										
										
											2003-02-04 15:48:42 +00:00
										 |  |  | 	stringp=args; | 
					
						
							|  |  |  | 	c = strsep(&stringp, "|"); | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 	while(c && strlen(c) && (argc < (PPP_MAX_ARGS - 4))) { | 
					
						
							|  |  |  | 		argv[argc++] = c; | 
					
						
							| 
									
										
										
										
											2003-02-04 15:48:42 +00:00
										 |  |  | 		c = strsep(&stringp, "|"); | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	argv[argc++] = "plugin"; | 
					
						
							|  |  |  | 	argv[argc++] = "zaptel.so"; | 
					
						
							|  |  |  | 	argv[argc++] = "stdin"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if 0
 | 
					
						
							|  |  |  | 	for (x=0;x<argc;x++) { | 
					
						
							|  |  |  | 		fprintf(stderr, "Arg %d: %s\n", x, argv[x]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* 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; | 
					
						
							| 
									
										
										
										
											2005-07-06 01:41:54 +00:00
										 |  |  | 	struct zt_bufferinfo savebi; | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 	int x; | 
					
						
							| 
									
										
										
										
											2005-07-06 01:41:54 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	res = ioctl(chan->fds[0], ZT_GET_BUFINFO, &savebi); | 
					
						
							|  |  |  | 	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 */ | 
					
						
							| 
									
										
										
										
											2002-07-12 09:03:50 +00:00
										 |  |  | 				if (chan->_softhangup && !signalled) { | 
					
						
							| 
									
										
										
										
											2002-05-13 22:29:39 +00:00
										 |  |  | 					ast_log(LOG_DEBUG, "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)); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (option_verbose > 2) { | 
					
						
							|  |  |  | 				if (WIFEXITED(status)) { | 
					
						
							|  |  |  | 					ast_verbose(VERBOSE_PREFIX_3 "RAS on %s terminated with status %d\n", chan->name, WEXITSTATUS(status)); | 
					
						
							|  |  |  | 				} else if (WIFSIGNALED(status)) { | 
					
						
							|  |  |  | 					ast_verbose(VERBOSE_PREFIX_3 "RAS on %s terminated with signal %d\n",  | 
					
						
							|  |  |  | 						 chan->name, WTERMSIG(status)); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					ast_verbose(VERBOSE_PREFIX_3 "RAS on %s terminated weirdly.\n", chan->name); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			/* Throw back into audio mode */ | 
					
						
							| 
									
										
										
										
											2002-05-13 22:29:39 +00:00
										 |  |  | 			x = 1; | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 			ioctl(chan->fds[0], ZT_AUDIOMODE, &x); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-07-06 01:41:54 +00:00
										 |  |  | 			/* Restore saved values */ | 
					
						
							|  |  |  | 			res = ioctl(chan->fds[0], ZT_SET_BUFINFO, &savebi); | 
					
						
							|  |  |  | 			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; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int zapras_exec(struct ast_channel *chan, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res=-1; | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 	char *args; | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 	struct localuser *u; | 
					
						
							|  |  |  | 	ZT_PARAMS ztp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!data)  | 
					
						
							|  |  |  | 		data = ""; | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 	LOCAL_USER_ADD(u); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2006-02-01 23:05:28 +00:00
										 |  |  | 	if (strcasecmp(chan->tech->type, "Zap")) { | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 		/* If it's not a zap channel, we're done.  Wait a couple of
 | 
					
						
							|  |  |  | 		   seconds and then hangup... */ | 
					
						
							|  |  |  | 		if (option_verbose > 1) | 
					
						
							|  |  |  | 			ast_verbose(VERBOSE_PREFIX_2 "Channel %s is not a Zap channel\n", chan->name); | 
					
						
							|  |  |  | 		sleep(2); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		memset(&ztp, 0, sizeof(ztp)); | 
					
						
							|  |  |  | 		if (ioctl(chan->fds[0], ZT_GET_PARAMS, &ztp)) { | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "Unable to get zaptel parameters\n"); | 
					
						
							|  |  |  | 		} else if (ztp.sigtype != ZT_SIG_CLEAR) { | 
					
						
							|  |  |  | 			if (option_verbose > 1) | 
					
						
							|  |  |  | 				ast_verbose(VERBOSE_PREFIX_2 "Channel %s is not a clear channel\n", chan->name); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			/* Everything should be okay.  Run PPP. */ | 
					
						
							|  |  |  | 			if (option_verbose > 2) | 
					
						
							|  |  |  | 				ast_verbose(VERBOSE_PREFIX_3 "Starting RAS on %s\n", chan->name); | 
					
						
							|  |  |  | 			/* Execute RAS */ | 
					
						
							|  |  |  | 			run_ras(chan, args); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	LOCAL_USER_REMOVE(u); | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-14 14:08:19 +00:00
										 |  |  | static int unload_module(void *mod)  | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-10-18 22:52:21 +00:00
										 |  |  | 	int res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res = ast_unregister_application(app); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | 	STANDARD_HANGUP_LOCALUSERS; | 
					
						
							| 
									
										
										
										
											2005-10-18 22:52:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return res; | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-14 14:08:19 +00:00
										 |  |  | static int load_module(void *mod) | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return ast_register_application(app, zapras_exec, synopsis, descrip); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-14 14:08:19 +00:00
										 |  |  | static const char *description(void) | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return tdesc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-14 14:08:19 +00:00
										 |  |  | static const char *key(void) | 
					
						
							| 
									
										
										
										
											2002-03-08 23:48:42 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return ASTERISK_GPL_KEY; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2006-04-14 14:08:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | STD_MOD1; | 
					
						
							| 
									
										
										
										
											2006-04-24 17:11:45 +00:00
										 |  |  | 
 |