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>dahdi</depend>
34 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
36 #include <sys/ioctl.h>
39 #include <sys/signal.h>
42 #endif /* __linux__ */
52 #include "asterisk/lock.h"
53 #include "asterisk/file.h"
54 #include "asterisk/logger.h"
55 #include "asterisk/channel.h"
56 #include "asterisk/pbx.h"
57 #include "asterisk/module.h"
58 #include "asterisk/options.h"
60 #include "asterisk/dahdi_compat.h"
62 static char *dahdi_app
= "DAHDIRAS";
63 static char *zap_app
= "ZapRAS";
65 static char *dahdi_synopsis
= "Executes DAHDI ISDN RAS application";
66 static char *zap_synopsis
= "Executes Zaptel ISDN RAS application";
68 static char *dahdi_descrip
=
69 " DAHDIRAS(args): Executes a RAS server using pppd on the given channel.\n"
70 "The channel must be a clear channel (i.e. PRI source) and a DAHDI\n"
71 "channel to be able to use this function (no modem emulation is included).\n"
72 "Your pppd must have the DAHDI plugin available. Arguments should be\n"
73 "separated by | characters.\n";
75 static char *zap_descrip
=
76 " ZapRAS(args): Executes a RAS server using pppd on the given channel.\n"
77 "The channel must be a clear channel (i.e. PRI source) and a Zaptel\n"
78 "channel to be able to use this function (no modem emulation is included).\n"
79 "Your pppd must have the Zaptel plugin available. Arguments should be\n"
80 "separated by | characters.\n";
82 #define PPP_MAX_ARGS 32
83 #define PPP_EXEC "/usr/sbin/pppd"
85 static pid_t
spawn_ras(struct ast_channel
*chan
, char *args
)
91 char *argv
[PPP_MAX_ARGS
];
94 sigset_t fullset
, oldset
;
97 pthread_sigmask(SIG_BLOCK
, &fullset
, &oldset
);
99 /* Start by forking */
102 pthread_sigmask(SIG_SETMASK
, &oldset
, NULL
);
106 /* Restore original signal handlers */
110 pthread_sigmask(SIG_UNBLOCK
, &fullset
, NULL
);
112 /* Execute RAS on File handles */
113 dup2(chan
->fds
[0], STDIN_FILENO
);
115 /* Drop high priority */
116 if (ast_opt_high_priority
)
119 /* Close other file descriptors */
120 for (x
=STDERR_FILENO
+ 1;x
<1024;x
++)
123 /* Reset all arguments */
124 memset(argv
, 0, sizeof(argv
));
126 /* First argument is executable, followed by standard
127 arguments for DAHDI PPP */
128 argv
[argc
++] = PPP_EXEC
;
129 argv
[argc
++] = "nodetach";
131 /* And all the other arguments */
133 c
= strsep(&stringp
, "|");
134 while(c
&& strlen(c
) && (argc
< (PPP_MAX_ARGS
- 4))) {
136 c
= strsep(&stringp
, "|");
139 argv
[argc
++] = "plugin";
141 argv
[argc
++] = "zaptel.so";
143 argv
[argc
++] = "dahdi.so";
145 argv
[argc
++] = "stdin";
147 /* Finally launch PPP */
148 execv(PPP_EXEC
, argv
);
149 fprintf(stderr
, "Failed to exec PPPD!\n");
153 static void run_ras(struct ast_channel
*chan
, char *args
)
159 struct dahdi_bufferinfo savebi
;
162 res
= ioctl(chan
->fds
[0], DAHDI_GET_BUFINFO
, &savebi
);
164 ast_log(LOG_WARNING
, "Unable to check buffer policy on channel %s\n", chan
->name
);
168 pid
= spawn_ras(chan
, args
);
170 ast_log(LOG_WARNING
, "Failed to spawn RAS\n");
173 res
= wait4(pid
, &status
, WNOHANG
, NULL
);
175 /* Check for hangup */
176 if (chan
->_softhangup
&& !signalled
) {
177 ast_log(LOG_DEBUG
, "Channel '%s' hungup. Signalling RAS at %d to die...\n", chan
->name
, pid
);
186 ast_log(LOG_WARNING
, "wait4 returned %d: %s\n", res
, strerror(errno
));
188 if (option_verbose
> 2) {
189 if (WIFEXITED(status
)) {
190 ast_verbose(VERBOSE_PREFIX_3
"RAS on %s terminated with status %d\n", chan
->name
, WEXITSTATUS(status
));
191 } else if (WIFSIGNALED(status
)) {
192 ast_verbose(VERBOSE_PREFIX_3
"RAS on %s terminated with signal %d\n",
193 chan
->name
, WTERMSIG(status
));
195 ast_verbose(VERBOSE_PREFIX_3
"RAS on %s terminated weirdly.\n", chan
->name
);
198 /* Throw back into audio mode */
200 ioctl(chan
->fds
[0], DAHDI_AUDIOMODE
, &x
);
202 /* Restore saved values */
203 res
= ioctl(chan
->fds
[0], DAHDI_SET_BUFINFO
, &savebi
);
205 ast_log(LOG_WARNING
, "Unable to set buffer policy on channel %s\n", chan
->name
);
212 static int exec(struct ast_channel
*chan
, void *data
)
216 struct ast_module_user
*u
;
217 struct dahdi_params ztp
;
222 u
= ast_module_user_add(chan
);
224 args
= ast_strdupa(data
);
226 /* Answer the channel if it's not up */
227 if (chan
->_state
!= AST_STATE_UP
)
229 if (strcasecmp(chan
->tech
->type
, dahdi_chan_name
)) {
230 if (option_verbose
> 1)
231 ast_verbose(VERBOSE_PREFIX_2
"Channel %s is not a %s channel\n", chan
->name
, dahdi_chan_name
);
234 memset(&ztp
, 0, sizeof(ztp
));
235 if (ioctl(chan
->fds
[0], DAHDI_GET_PARAMS
, &ztp
)) {
236 ast_log(LOG_WARNING
, "Unable to get parameters\n");
237 } else if (ztp
.sigtype
!= DAHDI_SIG_CLEAR
) {
238 if (option_verbose
> 1)
239 ast_verbose(VERBOSE_PREFIX_2
"Channel %s is not a clear channel\n", chan
->name
);
241 /* Everything should be okay. Run PPP. */
242 if (option_verbose
> 2)
243 ast_verbose(VERBOSE_PREFIX_3
"Starting RAS on %s\n", chan
->name
);
248 ast_module_user_remove(u
);
252 static int exec_warn(struct ast_channel
*chan
, void *data
)
254 ast_log(LOG_WARNING
, "Use of the command %s is deprecated, please use %s instead.\n", zap_app
, dahdi_app
);
256 return exec(chan
, data
);
259 static int unload_module(void)
263 if (*dahdi_chan_mode
== CHAN_DAHDI_PLUS_ZAP_MODE
) {
264 res
|= ast_unregister_application(dahdi_app
);
267 res
|= ast_unregister_application(zap_app
);
269 ast_module_user_hangup_all();
274 static int load_module(void)
278 if (*dahdi_chan_mode
== CHAN_DAHDI_PLUS_ZAP_MODE
) {
279 res
|= ast_register_application(dahdi_app
, exec
, dahdi_synopsis
, dahdi_descrip
);
282 res
|= ast_register_application(zap_app
, exec_warn
, zap_synopsis
, zap_descrip
);
287 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "DAHDI RAS Application");