Minor formatting change to test a mantis change ...
[asterisk-bristuff.git] / apps / app_zapras.c
blob6e079e32dbbe1d2e7b12c92dcc4b8e595fa75a97
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
21 * \brief Execute an ISDN RAS
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
28 /*** MODULEINFO
29 <depend>zaptel</depend>
30 ***/
32 #include "asterisk.h"
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include <sys/ioctl.h>
37 #include <sys/wait.h>
38 #ifdef __linux__
39 #include <sys/signal.h>
40 #else
41 #include <signal.h>
42 #endif /* __linux__ */
44 #include <fcntl.h>
46 #include "asterisk/zapata.h"
48 #include "asterisk/lock.h"
49 #include "asterisk/file.h"
50 #include "asterisk/channel.h"
51 #include "asterisk/pbx.h"
52 #include "asterisk/module.h"
53 #include "asterisk/app.h"
55 static char *app = "ZapRAS";
57 static char *synopsis = "Executes Zaptel ISDN RAS application";
59 static char *descrip =
60 " ZapRAS(args): Executes a RAS server using pppd on the given channel.\n"
61 "The channel must be a clear channel (i.e. PRI source) and a Zaptel\n"
62 "channel to be able to use this function (No modem emulation is included).\n"
63 "Your pppd must be patched to be zaptel aware. Arguments should be\n"
64 "separated by , characters.\n";
67 #define PPP_MAX_ARGS 32
68 #define PPP_EXEC "/usr/sbin/pppd"
70 static pid_t spawn_ras(struct ast_channel *chan, char *args)
72 pid_t pid;
73 char *c;
75 char *argv[PPP_MAX_ARGS];
76 int argc = 0;
77 char *stringp=NULL;
79 /* Start by forking */
80 pid = ast_safe_fork(1);
81 if (pid) {
82 return pid;
85 /* Execute RAS on File handles */
86 dup2(chan->fds[0], STDIN_FILENO);
88 /* Drop high priority */
89 if (ast_opt_high_priority)
90 ast_set_priority(0);
92 /* Close other file descriptors */
93 ast_close_fds_above_n(STDERR_FILENO);
95 /* Reset all arguments */
96 memset(argv, 0, sizeof(argv));
98 /* First argument is executable, followed by standard
99 arguments for zaptel PPP */
100 argv[argc++] = PPP_EXEC;
101 argv[argc++] = "nodetach";
103 /* And all the other arguments */
104 stringp=args;
105 c = strsep(&stringp, ",");
106 while(c && strlen(c) && (argc < (PPP_MAX_ARGS - 4))) {
107 argv[argc++] = c;
108 c = strsep(&stringp, ",");
111 argv[argc++] = "plugin";
112 argv[argc++] = "zaptel.so";
113 argv[argc++] = "stdin";
115 /* Finally launch PPP */
116 execv(PPP_EXEC, argv);
117 fprintf(stderr, "Failed to exec PPPD!\n");
118 exit(1);
121 static void run_ras(struct ast_channel *chan, char *args)
123 pid_t pid;
124 int status;
125 int res;
126 int signalled = 0;
127 struct zt_bufferinfo savebi;
128 int x;
130 res = ioctl(chan->fds[0], ZT_GET_BUFINFO, &savebi);
131 if(res) {
132 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %s\n", chan->name);
133 return;
136 pid = spawn_ras(chan, args);
137 if (pid < 0) {
138 ast_log(LOG_WARNING, "Failed to spawn RAS\n");
139 } else {
140 for (;;) {
141 res = wait4(pid, &status, WNOHANG, NULL);
142 if (!res) {
143 /* Check for hangup */
144 if (ast_check_hangup(chan) && !signalled) {
145 ast_debug(1, "Channel '%s' hungup. Signalling RAS at %d to die...\n", chan->name, pid);
146 kill(pid, SIGTERM);
147 signalled=1;
149 /* Try again */
150 sleep(1);
151 continue;
153 if (res < 0) {
154 ast_log(LOG_WARNING, "wait4 returned %d: %s\n", res, strerror(errno));
156 if (WIFEXITED(status)) {
157 ast_verb(3, "RAS on %s terminated with status %d\n", chan->name, WEXITSTATUS(status));
158 } else if (WIFSIGNALED(status)) {
159 ast_verb(3, "RAS on %s terminated with signal %d\n",
160 chan->name, WTERMSIG(status));
161 } else {
162 ast_verb(3, "RAS on %s terminated weirdly.\n", chan->name);
164 /* Throw back into audio mode */
165 x = 1;
166 ioctl(chan->fds[0], ZT_AUDIOMODE, &x);
168 /* Restore saved values */
169 res = ioctl(chan->fds[0], ZT_SET_BUFINFO, &savebi);
170 if (res < 0) {
171 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %s\n", chan->name);
173 break;
176 ast_safe_fork_cleanup();
179 static int zapras_exec(struct ast_channel *chan, void *data)
181 int res=-1;
182 char *args;
183 ZT_PARAMS ztp;
185 if (!data)
186 data = "";
188 args = ast_strdupa(data);
190 /* Answer the channel if it's not up */
191 if (chan->_state != AST_STATE_UP)
192 ast_answer(chan);
193 if (strcasecmp(chan->tech->type, "Zap")) {
194 /* If it's not a zap channel, we're done. Wait a couple of
195 seconds and then hangup... */
196 ast_verb(2, "Channel %s is not a Zap channel\n", chan->name);
197 sleep(2);
198 } else {
199 memset(&ztp, 0, sizeof(ztp));
200 if (ioctl(chan->fds[0], ZT_GET_PARAMS, &ztp)) {
201 ast_log(LOG_WARNING, "Unable to get zaptel parameters\n");
202 } else if (ztp.sigtype != ZT_SIG_CLEAR) {
203 ast_verb(2, "Channel %s is not a clear channel\n", chan->name);
204 } else {
205 /* Everything should be okay. Run PPP. */
206 ast_verb(3, "Starting RAS on %s\n", chan->name);
207 /* Execute RAS */
208 run_ras(chan, args);
211 return res;
214 static int unload_module(void)
216 return ast_unregister_application(app);
219 static int load_module(void)
221 return ((ast_register_application(app, zapras_exec, synopsis, descrip)) ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS);
224 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Zaptel ISDN Remote Access Server");