00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "asterisk.h"
00033
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 153468 $")
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
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
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 static 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
00091 pid = ast_safe_fork(1);
00092 if (pid) {
00093 return pid;
00094 }
00095
00096
00097 dup2(chan->fds[0], STDIN_FILENO);
00098
00099
00100 if (ast_opt_high_priority)
00101 ast_set_priority(0);
00102
00103
00104 ast_close_fds_above_n(STDERR_FILENO);
00105
00106
00107 memset(argv, 0, sizeof(argv));
00108
00109
00110
00111 argv[argc++] = PPP_EXEC;
00112 argv[argc++] = "nodetach";
00113
00114
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
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
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
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
00176 x = 1;
00177 ioctl(chan->fds[0], DAHDI_AUDIOMODE, &x);
00178
00179
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, void *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
00202 if (chan->_state != AST_STATE_UP)
00203 ast_answer(chan);
00204 if (strcasecmp(chan->tech->type, "DAHDI")) {
00205
00206
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
00217 ast_verb(3, "Starting RAS on %s\n", chan->name);
00218
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