Mon Jun 27 16:50:46 2011

Asterisk developer's documentation


app_dahdiras.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Execute an ISDN RAS
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  * 
00025  * \ingroup applications
00026  */
00027 
00028 /*** MODULEINFO
00029    <depend>dahdi</depend>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 199479 $")
00035 
00036 #include <sys/ioctl.h>
00037 #include <sys/wait.h>
00038 #ifdef __linux__
00039 #include <sys/signal.h>
00040 #else
00041 #include <signal.h>
00042 #endif /* __linux__ */
00043 
00044 #include <fcntl.h>
00045 
00046 #include <dahdi/user.h>
00047 
00048 #include "asterisk/lock.h"
00049 #include "asterisk/file.h"
00050 #include "asterisk/channel.h"
00051 #include "asterisk/pbx.h"
00052 #include "asterisk/module.h"
00053 #include "asterisk/app.h"
00054 
00055 /*** DOCUMENTATION
00056    <application name="DAHDIRAS" language="en_US">
00057       <synopsis>
00058          Executes DAHDI ISDN RAS application.
00059       </synopsis>
00060       <syntax>
00061          <parameter name="args" required="true">
00062             <para>A list of parameters to pass to the pppd daemon,
00063             separated by <literal>,</literal> characters.</para>
00064          </parameter>
00065       </syntax>
00066       <description>
00067          <para>Executes a RAS server using pppd on the given channel.
00068          The channel must be a clear channel (i.e. PRI source) and a DAHDI
00069          channel to be able to use this function (No modem emulation is included).</para>
00070          <para>Your pppd must be patched to be DAHDI aware.</para>
00071       </description>
00072    </application>
00073 
00074  ***/
00075 
00076 static const char app[] = "DAHDIRAS";
00077 
00078 #define PPP_MAX_ARGS 32
00079 #define PPP_EXEC  "/usr/sbin/pppd"
00080 
00081 static pid_t spawn_ras(struct ast_channel *chan, char *args)
00082 {
00083    pid_t pid;
00084    char *c;
00085 
00086    char *argv[PPP_MAX_ARGS];
00087    int argc = 0;
00088    char *stringp=NULL;
00089 
00090    /* Start by forking */
00091    pid = ast_safe_fork(1);
00092    if (pid) {
00093       return pid;
00094    }
00095 
00096    /* Execute RAS on File handles */
00097    dup2(chan->fds[0], STDIN_FILENO);
00098 
00099    /* Drop high priority */
00100    if (ast_opt_high_priority)
00101       ast_set_priority(0);
00102 
00103    /* Close other file descriptors */
00104    ast_close_fds_above_n(STDERR_FILENO);
00105 
00106    /* Reset all arguments */
00107    memset(argv, 0, sizeof(argv));
00108 
00109    /* First argument is executable, followed by standard
00110       arguments for DAHDI PPP */
00111    argv[argc++] = PPP_EXEC;
00112    argv[argc++] = "nodetach";
00113 
00114    /* And all the other arguments */
00115    stringp=args;
00116    c = strsep(&stringp, ",");
00117    while(c && strlen(c) && (argc < (PPP_MAX_ARGS - 4))) {
00118       argv[argc++] = c;
00119       c = strsep(&stringp, ",");
00120    }
00121 
00122    argv[argc++] = "plugin";
00123    argv[argc++] = "dahdi.so";
00124    argv[argc++] = "stdin";
00125 
00126    /* Finally launch PPP */
00127    execv(PPP_EXEC, argv);
00128    fprintf(stderr, "Failed to exec PPPD!\n");
00129    exit(1);
00130 }
00131 
00132 static void run_ras(struct ast_channel *chan, char *args)
00133 {
00134    pid_t pid;
00135    int status;
00136    int res;
00137    int signalled = 0;
00138    struct dahdi_bufferinfo savebi;
00139    int x;
00140    
00141    res = ioctl(chan->fds[0], DAHDI_GET_BUFINFO, &savebi);
00142    if(res) {
00143       ast_log(LOG_WARNING, "Unable to check buffer policy on channel %s\n", chan->name);
00144       return;
00145    }
00146 
00147    pid = spawn_ras(chan, args);
00148    if (pid < 0) {
00149       ast_log(LOG_WARNING, "Failed to spawn RAS\n");
00150    } else {
00151       for (;;) {
00152          res = wait4(pid, &status, WNOHANG, NULL);
00153          if (!res) {
00154             /* Check for hangup */
00155             if (ast_check_hangup(chan) && !signalled) {
00156                ast_debug(1, "Channel '%s' hungup.  Signalling RAS at %d to die...\n", chan->name, pid);
00157                kill(pid, SIGTERM);
00158                signalled=1;
00159             }
00160             /* Try again */
00161             sleep(1);
00162             continue;
00163          }
00164          if (res < 0) {
00165             ast_log(LOG_WARNING, "wait4 returned %d: %s\n", res, strerror(errno));
00166          }
00167          if (WIFEXITED(status)) {
00168             ast_verb(3, "RAS on %s terminated with status %d\n", chan->name, WEXITSTATUS(status));
00169          } else if (WIFSIGNALED(status)) {
00170             ast_verb(3, "RAS on %s terminated with signal %d\n", 
00171                 chan->name, WTERMSIG(status));
00172          } else {
00173             ast_verb(3, "RAS on %s terminated weirdly.\n", chan->name);
00174          }
00175          /* Throw back into audio mode */
00176          x = 1;
00177          ioctl(chan->fds[0], DAHDI_AUDIOMODE, &x);
00178 
00179          /* Restore saved values */
00180          res = ioctl(chan->fds[0], DAHDI_SET_BUFINFO, &savebi);
00181          if (res < 0) {
00182             ast_log(LOG_WARNING, "Unable to set buffer policy on channel %s\n", chan->name);
00183          }
00184          break;
00185       }
00186    }
00187    ast_safe_fork_cleanup();
00188 }
00189 
00190 static int dahdiras_exec(struct ast_channel *chan, const char *data)
00191 {
00192    int res=-1;
00193    char *args;
00194    struct dahdi_params dahdip;
00195 
00196    if (!data) 
00197       data = "";
00198 
00199    args = ast_strdupa(data);
00200    
00201    /* Answer the channel if it's not up */
00202    if (chan->_state != AST_STATE_UP)
00203       ast_answer(chan);
00204    if (strcasecmp(chan->tech->type, "DAHDI")) {
00205       /* If it's not a DAHDI channel, we're done.  Wait a couple of
00206          seconds and then hangup... */
00207       ast_verb(2, "Channel %s is not a DAHDI channel\n", chan->name);
00208       sleep(2);
00209    } else {
00210       memset(&dahdip, 0, sizeof(dahdip));
00211       if (ioctl(chan->fds[0], DAHDI_GET_PARAMS, &dahdip)) {
00212          ast_log(LOG_WARNING, "Unable to get DAHDI parameters\n");
00213       } else if (dahdip.sigtype != DAHDI_SIG_CLEAR) {
00214          ast_verb(2, "Channel %s is not a clear channel\n", chan->name);
00215       } else {
00216          /* Everything should be okay.  Run PPP. */
00217          ast_verb(3, "Starting RAS on %s\n", chan->name);
00218          /* Execute RAS */
00219          run_ras(chan, args);
00220       }
00221    }
00222    return res;
00223 }
00224 
00225 static int unload_module(void) 
00226 {
00227    return ast_unregister_application(app);
00228 }
00229 
00230 static int load_module(void)
00231 {
00232    return ((ast_register_application_xml(app, dahdiras_exec)) ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS);
00233 }
00234 
00235 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "DAHDI ISDN Remote Access Server");
00236 

Generated on Mon Jun 27 16:50:46 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7