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.
21 * \brief Execute an ISDN RAS
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
29 <depend>zaptel</depend>
34 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
36 #include <sys/ioctl.h>
39 #include <sys/signal.h>
42 #endif /* __linux__ */
51 #include <zaptel/zaptel.h>
53 #include "asterisk/lock.h"
54 #include "asterisk/file.h"
55 #include "asterisk/logger.h"
56 #include "asterisk/channel.h"
57 #include "asterisk/pbx.h"
58 #include "asterisk/module.h"
59 #include "asterisk/options.h"
61 static char *app
= "ZapRAS";
63 static char *synopsis
= "Executes Zaptel ISDN RAS application";
65 static char *descrip
=
66 " ZapRAS(args): Executes a RAS server using pppd on the given channel.\n"
67 "The channel must be a clear channel (i.e. PRI source) and a Zaptel\n"
68 "channel to be able to use this function (No modem emulation is included).\n"
69 "Your pppd must be patched to be zaptel aware. Arguments should be\n"
70 "separated by | characters.\n";
73 #define PPP_MAX_ARGS 32
74 #define PPP_EXEC "/usr/sbin/pppd"
76 static pid_t
spawn_ras(struct ast_channel
*chan
, char *args
)
82 char *argv
[PPP_MAX_ARGS
];
85 sigset_t fullset
, oldset
;
88 pthread_sigmask(SIG_BLOCK
, &fullset
, &oldset
);
90 /* Start by forking */
93 pthread_sigmask(SIG_SETMASK
, &oldset
, NULL
);
97 /* Restore original signal handlers */
101 pthread_sigmask(SIG_UNBLOCK
, &fullset
, NULL
);
103 /* Execute RAS on File handles */
104 dup2(chan
->fds
[0], STDIN_FILENO
);
106 /* Drop high priority */
107 if (ast_opt_high_priority
)
110 /* Close other file descriptors */
111 for (x
=STDERR_FILENO
+ 1;x
<1024;x
++)
114 /* Reset all arguments */
115 memset(argv
, 0, sizeof(argv
));
117 /* First argument is executable, followed by standard
118 arguments for zaptel PPP */
119 argv
[argc
++] = PPP_EXEC
;
120 argv
[argc
++] = "nodetach";
122 /* And all the other arguments */
124 c
= strsep(&stringp
, "|");
125 while(c
&& strlen(c
) && (argc
< (PPP_MAX_ARGS
- 4))) {
127 c
= strsep(&stringp
, "|");
130 argv
[argc
++] = "plugin";
131 argv
[argc
++] = "zaptel.so";
132 argv
[argc
++] = "stdin";
134 /* Finally launch PPP */
135 execv(PPP_EXEC
, argv
);
136 fprintf(stderr
, "Failed to exec PPPD!\n");
140 static void run_ras(struct ast_channel
*chan
, char *args
)
146 struct zt_bufferinfo savebi
;
149 res
= ioctl(chan
->fds
[0], ZT_GET_BUFINFO
, &savebi
);
151 ast_log(LOG_WARNING
, "Unable to check buffer policy on channel %s\n", chan
->name
);
155 pid
= spawn_ras(chan
, args
);
157 ast_log(LOG_WARNING
, "Failed to spawn RAS\n");
160 res
= wait4(pid
, &status
, WNOHANG
, NULL
);
162 /* Check for hangup */
163 if (chan
->_softhangup
&& !signalled
) {
164 ast_log(LOG_DEBUG
, "Channel '%s' hungup. Signalling RAS at %d to die...\n", chan
->name
, pid
);
173 ast_log(LOG_WARNING
, "wait4 returned %d: %s\n", res
, strerror(errno
));
175 if (option_verbose
> 2) {
176 if (WIFEXITED(status
)) {
177 ast_verbose(VERBOSE_PREFIX_3
"RAS on %s terminated with status %d\n", chan
->name
, WEXITSTATUS(status
));
178 } else if (WIFSIGNALED(status
)) {
179 ast_verbose(VERBOSE_PREFIX_3
"RAS on %s terminated with signal %d\n",
180 chan
->name
, WTERMSIG(status
));
182 ast_verbose(VERBOSE_PREFIX_3
"RAS on %s terminated weirdly.\n", chan
->name
);
185 /* Throw back into audio mode */
187 ioctl(chan
->fds
[0], ZT_AUDIOMODE
, &x
);
189 /* Restore saved values */
190 res
= ioctl(chan
->fds
[0], ZT_SET_BUFINFO
, &savebi
);
192 ast_log(LOG_WARNING
, "Unable to set buffer policy on channel %s\n", chan
->name
);
199 static int zapras_exec(struct ast_channel
*chan
, void *data
)
203 struct ast_module_user
*u
;
209 u
= ast_module_user_add(chan
);
211 args
= ast_strdupa(data
);
213 /* Answer the channel if it's not up */
214 if (chan
->_state
!= AST_STATE_UP
)
216 if (strcasecmp(chan
->tech
->type
, "Zap")) {
217 /* If it's not a zap channel, we're done. Wait a couple of
218 seconds and then hangup... */
219 if (option_verbose
> 1)
220 ast_verbose(VERBOSE_PREFIX_2
"Channel %s is not a Zap channel\n", chan
->name
);
223 memset(&ztp
, 0, sizeof(ztp
));
224 if (ioctl(chan
->fds
[0], ZT_GET_PARAMS
, &ztp
)) {
225 ast_log(LOG_WARNING
, "Unable to get zaptel parameters\n");
226 } else if (ztp
.sigtype
!= ZT_SIG_CLEAR
) {
227 if (option_verbose
> 1)
228 ast_verbose(VERBOSE_PREFIX_2
"Channel %s is not a clear channel\n", chan
->name
);
230 /* Everything should be okay. Run PPP. */
231 if (option_verbose
> 2)
232 ast_verbose(VERBOSE_PREFIX_3
"Starting RAS on %s\n", chan
->name
);
237 ast_module_user_remove(u
);
241 static int unload_module(void)
245 res
= ast_unregister_application(app
);
247 ast_module_user_hangup_all();
252 static int load_module(void)
254 return ast_register_application(app
, zapras_exec
, synopsis
, descrip
);
257 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Zap RAS Application");