Fri Jul 3 20:35:37 2009

Asterisk developer's documentation


chan_ss7.c

Go to the documentation of this file.
00001 #define CHAN_SS7_VERSION "1.0.93-beta"
00002 /* chan_ss7.c - Implementation of SS7 (MTP2, MTP3, and ISUP) for Asterisk.
00003  *
00004  * Copyright (C) 2005-2006, Sifira A/S.
00005  *
00006  * Author: Kristian Nielsen <kn@sifira.dk>,
00007  *         Anders Baekgaard <ab@sifira.dk>
00008  *         Anders Baekgaard <ab@dicea.dk>
00009  *
00010  * This file is part of chan_ss7.
00011  *
00012  * chan_ss7 is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * chan_ss7 is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with chan_ss7; if not, write to the Free Software
00024  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00025  */
00026 #include "asterisk.h"
00027 
00028 #include <errno.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 #include <stdio.h>
00032 
00033 #include <unistd.h>
00034 #include <sys/poll.h>
00035 #include <netinet/in.h>
00036 
00037 #include "asterisk/channel.h"
00038 #include "asterisk/module.h"
00039 #include "asterisk/logger.h"
00040 #include "asterisk/options.h"
00041 #include "asterisk/utils.h"
00042 #include "asterisk/sched.h"
00043 #include "asterisk/cli.h"
00044 #include "asterisk/lock.h"
00045 
00046 #include "astversion.h"
00047 #include "config.h"
00048 #include "lffifo.h"
00049 #include "utils.h"
00050 #include "mtp.h"
00051 #include "transport.h"
00052 #include "isup.h"
00053 #include "l4isup.h"
00054 #include "cluster.h"
00055 #include "mtp3io.h"
00056 
00057 #ifdef USE_ASTERISK_1_2
00058 #define AST_MODULE_LOAD_SUCCESS  0
00059 #define AST_MODULE_LOAD_DECLINE  1
00060 #define AST_MODULE_LOAD_FAILURE -1
00061 #endif
00062 
00063 /* Send fifo for sending control requests to the MTP thread.
00064    The fifo is lock-free (one thread may put and another get simultaneously),
00065    but multiple threads doing put must be serialized with this mutex. */
00066 AST_MUTEX_DEFINE_STATIC(mtp_control_mutex);
00067 static struct lffifo *mtp_control_fifo = NULL;
00068 
00069 /* This is the MTP2/MTP3 thread, which runs at high real-time priority
00070    and is careful not to wait for locks in order not to loose MTP
00071    frames. */
00072 static pthread_t mtp_thread = AST_PTHREADT_NULL;
00073 static int mtp_thread_running = 0;
00074 
00075 
00076 /* This is the monitor thread which mainly handles scheduling/timeouts. */
00077 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00078 static int monitor_running = 0;
00079 
00080 
00081 
00082 /* State for dumps. */
00083 AST_MUTEX_DEFINE_STATIC(dump_mutex);
00084 static FILE *dump_in_fh = NULL;
00085 static FILE *dump_out_fh = NULL;
00086 static int dump_do_fisu, dump_do_lssu, dump_do_msu;
00087 
00088 
00089 static const char desc[] = "SS7 Protocol Support";
00090 static const char config[] = "ss7.conf";
00091 
00092 
00093 
00094 
00095 static int cmd_version(int fd, int argc, char *argv[]);
00096 static int cmd_dump_status(int fd, int argc, char *argv[]);
00097 static int cmd_dump_stop(int fd, int argc, char *argv[]);
00098 static int cmd_dump_start(int fd, int argc, char *argv[]);
00099 static char *complete_dump_stop(const char *line, const char *word, int pos, int state);
00100 static char *complete_dump_start(const char *line, const char *word, int pos, int state);
00101 static int cmd_link_up(int fd, int argc, char *argv[]);
00102 static int cmd_link_down(int fd, int argc, char *argv[]);
00103 static int cmd_link_status(int fd, int argc, char *argv[]);
00104 static int cmd_ss7_status(int fd, int argc, char *argv[]);
00105 
00106 static struct ast_cli_entry my_clis[] = {
00107   { {"ss7", "version", NULL}, cmd_version,
00108     "Show current version of chan_ss7",
00109     "Usage: ss7 version\n",
00110     NULL
00111   },
00112 
00113   { { "ss7", "dump", "start", NULL}, cmd_dump_start,
00114     "Start MTP2 dump to a file",
00115     "Usage: ss7 dump start <file> [in|out|both] [fisu] [lssu] [msu]\n"
00116     "       Start mtp2 dump to file. Either incoming, outgoing, or both(default).\n"
00117     "       Optinally specify which of fisu, lssu, and msu should be dumped.\n"
00118     "       The output is in PCAP format (can be read by wireshark).\n",
00119     complete_dump_start
00120   },
00121 
00122   { {"ss7", "dump", "stop", NULL}, cmd_dump_stop,
00123     "Stop a running MTP2 dump",
00124     "Usage: ss7 dump stop [in|out|both]\n"
00125     "       Stop mtp2 dump started with \"ss7 start dump\". Either incoming,\n"
00126     "       outgoing, or both(default).\n",
00127     complete_dump_stop
00128   },
00129 
00130   { {"ss7", "dump", "status", NULL}, cmd_dump_status,
00131     "Stop what dumps are running",
00132     "Usage: ss7 dump status\n",
00133     NULL
00134   },
00135 
00136 #ifndef MODULETEST
00137   { {"ss7", "link", "down", NULL}, cmd_link_down,
00138     "Stop the MTP2 link(s) [logical-link-no]...",
00139     "Usage: ss7 link down [logical-link-no]\n"
00140     "       Take the link(s) down; it will be down until started explicitly with\n"
00141     "       'ss7 link up'.\n"
00142     "       Until then, it will continuously transmit LSSU 'OS' (out-of-service)\n"
00143     "       frames.\n"
00144     "       If no logical-link-no argument is given, all links are affected.\n",
00145     NULL
00146   },
00147 
00148   { {"ss7", "link", "up", NULL}, cmd_link_up,
00149     "Start the MTP2 link(s) [logical-link-no]...",
00150     "Usage: ss7 link up\n"
00151     "       Attempt to take the MTP2 link(s) up with the initial alignment procedure.\n"
00152     "       If no logical-link-no argument is given, all links are affected.\n",
00153     NULL
00154   },
00155 
00156   { {"ss7", "link", "status", NULL}, cmd_link_status,
00157     "Show status of the MTP2 links",
00158     "Usage: ss7 link status\n"
00159     "       Show the status of the MTP2 links.\n",
00160     NULL
00161   },
00162 #endif
00163 
00164   { {"ss7", "block", NULL}, cmd_block,
00165     "Set circuits in local maintenance blocked mode",
00166     "Usage: ss7 block <first> <count> [<linksetname>]\n"
00167     "       Set <count> lines into local maintenance blocked mode, starting at circuit <first>on linkset <linksetname>\n",
00168     NULL
00169   },
00170 
00171   { {"ss7", "unblock", NULL}, cmd_unblock,
00172     "Remove local maintenance blocked mode from circuits",
00173     "Usage: ss7 unblock <first> <count> [<linksetname>]\n"
00174     "       Remove <count> lines from local maintenance blocked mode, starting at circuit <first> on linkset <linksetname>.\n",
00175     NULL
00176   },
00177 
00178   { {"ss7", "linestat", NULL}, cmd_linestat,
00179     "Show line states",
00180     "Usage: ss7 linestat\n"
00181     "       Show status for all circuits.\n",
00182     NULL
00183   },
00184 
00185   { {"ss7", "show", "channels", NULL}, cmd_linestat,
00186     "Show channel states",
00187     "Usage: ss7 show channels\n"
00188     "       Show status for all channels.\n",
00189     NULL
00190   },
00191 
00192   { {"ss7", "cluster", "start", NULL}, cmd_cluster_start,
00193     "Start cluster",
00194     "Usage: ss7 cluster start\n"
00195     "       Start the cluster.\n",
00196     NULL
00197   },
00198 
00199   { {"ss7", "cluster", "stop", NULL}, cmd_cluster_stop,
00200     "Stop cluster",
00201     "Usage: ss7 cluster stop\n"
00202     "       Stop the cluster.\n",
00203     NULL
00204   },
00205 
00206   { {"ss7", "cluster", "status", NULL}, cmd_cluster_status,
00207     "Show status of the cluster",
00208     "Usage: ss7 cluster status\n"
00209     "       Show the status of the cluster.\n",
00210     NULL
00211   },
00212 
00213   { {"ss7", "reset", NULL}, cmd_reset,
00214     "Reset all circuits",
00215     "Usage: ss7 reset\n"
00216     "       Reset all circuits.\n",
00217     NULL
00218   },
00219 
00220   { { "ss7", "mtp", "data", NULL}, mtp_cmd_data,
00221     "Copy hex encoded string to MTP",
00222     "Usage: ss7 mtp data string\n"
00223     "       Copy hex encoded string to MTP",
00224     NULL,
00225   },
00226 
00227   { { "ss7", "status", NULL}, cmd_ss7_status,
00228     "Show status of ss7",
00229     "Usage: ss7 status\n"
00230     "       Show status/statistics of ss7",
00231     NULL,
00232   },
00233 
00234 #ifdef MODULETEST
00235   { {"ss7", "testfailover", NULL}, cmd_testfailover,
00236     "Test the failover mechanism",
00237     "Usage: ss7 testfailover"
00238     "       Test the failover mechanism.\n",
00239     NULL
00240   },
00241   { {"ss7", "moduletest", NULL}, cmd_moduletest,
00242     "Run a moduletest",
00243     "Usage: ss7 moduletest <no>"
00244     "       Run moduletest <no>.\n",
00245     NULL
00246   },
00247 #endif
00248 };
00249 
00250 
00251 static void dump_pcap(FILE *f, struct mtp_event *event)
00252 {
00253   unsigned int sec  = event->dump.stamp.tv_sec;
00254   unsigned int usec  = event->dump.stamp.tv_usec - (event->dump.stamp.tv_usec % 1000) +
00255     event->dump.slinkno*2 + /* encode link number in usecs */
00256     event->dump.out /* encode direction in/out */;
00257 
00258   fwrite(&sec, sizeof(sec), 1, f);
00259   fwrite(&usec, sizeof(usec), 1, f);
00260   fwrite(&event->len, sizeof(event->len), 1, f); /* number of bytes of packet in file */
00261   fwrite(&event->len, sizeof(event->len), 1, f); /* actual length of packet */
00262   fwrite(event->buf, 1, event->len, f);
00263   fflush(f);
00264 }
00265 
00266 static void init_pcap_file(FILE *f)
00267 {
00268   unsigned int magic = 0xa1b2c3d4;  /* text2pcap does this */
00269   unsigned short version_major = 2;
00270   unsigned short version_minor = 4;
00271   unsigned int thiszone = 0;
00272   unsigned int sigfigs = 0;
00273   unsigned int snaplen = 102400;
00274   unsigned int linktype = 140;
00275 
00276   fwrite(&magic, sizeof(magic), 1, f);
00277   fwrite(&version_major, sizeof(version_major), 1, f);
00278   fwrite(&version_minor, sizeof(version_minor), 1, f);
00279   fwrite(&thiszone, sizeof(thiszone), 1, f);
00280   fwrite(&sigfigs, sizeof(sigfigs), 1, f);
00281   fwrite(&snaplen, sizeof(snaplen), 1, f);
00282   fwrite(&linktype, sizeof(linktype), 1, f);
00283 }
00284 
00285 /* Queue a request to the MTP thread. */
00286 static void mtp_enqueue_control(struct mtp_req *req) {
00287   int res;
00288 
00289   ast_mutex_lock(&mtp_control_mutex);
00290   res = lffifo_put(mtp_control_fifo, (unsigned char *)req, sizeof(struct mtp_req) + req->len);
00291   ast_mutex_unlock(&mtp_control_mutex);
00292   if(res != 0) {
00293     ast_log(LOG_WARNING, "MTP control fifo full (MTP thread hanging?).\n");
00294   }
00295 }
00296 
00297 
00298 static int start_mtp_thread(void)
00299 {
00300   return start_thread(&mtp_thread, mtp_thread_main, &mtp_thread_running, 15);
00301 }
00302 
00303 static void stop_mtp_thread(void)
00304 {
00305     mtp_thread_signal_stop();
00306     stop_thread(&mtp_thread, &mtp_thread_running);
00307 }
00308 
00309 static int cmd_link_up_down(int fd, int argc, char *argv[], int updown) {
00310   static unsigned char buf[sizeof(struct mtp_req)];
00311   struct mtp_req *req = (struct mtp_req *)buf;
00312   int i;
00313 
00314   req->typ = updown;
00315   req->len = sizeof(req->link);
00316   if(argc > 3) {
00317     for (i = 3; i < argc; i++) {
00318       int link_ix = atoi(argv[i]);
00319       ast_log(LOG_DEBUG, "MTP control link %s %d\n", updown == MTP_REQ_LINK_UP ? "up" : "down", link_ix);
00320       if (link_ix >= this_host->n_schannels) {
00321    ast_log(LOG_ERROR, "Link index out of range %d, max %d.\n", link_ix, this_host->n_schannels);
00322    return RESULT_FAILURE;
00323       }
00324       req->link.link_ix = link_ix;
00325       mtp_enqueue_control(req);
00326     }
00327   }
00328   else {
00329     for (i=0; i < this_host->n_schannels; i++) {
00330       ast_log(LOG_DEBUG, "MTP control link %s %d\n", updown == MTP_REQ_LINK_UP ? "up" : "down", i);
00331       req->link.link_ix = i;
00332       mtp_enqueue_control(req);
00333     }
00334   }
00335   return RESULT_SUCCESS;
00336 }
00337 
00338 
00339 static int cmd_link_down(int fd, int argc, char *argv[]) {
00340   return cmd_link_up_down(fd, argc, argv, MTP_REQ_LINK_DOWN);
00341 }
00342 
00343 
00344 static int cmd_link_up(int fd, int argc, char *argv[]) {
00345   return cmd_link_up_down(fd, argc, argv, MTP_REQ_LINK_UP);
00346 }
00347 
00348 
00349 static int cmd_link_status(int fd, int argc, char *argv[]) {
00350   char buff[256];
00351   int i;
00352 
00353   for (i = 0; i < this_host->n_schannels; i++) {
00354     if (mtp_cmd_linkstatus(buff, i) == 0)
00355       ast_cli(fd, buff);
00356   }
00357   return RESULT_SUCCESS;
00358 }
00359 
00360 
00361 static char *complete_generic(const char *word, int state, char **options, int entries) {
00362   int which = 0;
00363   int i;
00364 
00365   for(i = 0; i < entries; i++) {
00366     if(0 == strncasecmp(word, options[i], strlen(word))) {
00367       if(++which > state) {
00368         return strdup(options[i]);
00369       }
00370     }
00371   }
00372   return NULL;
00373 }
00374 
00375 static char *dir_options[] = { "in", "out", "both", };
00376 static char *filter_options[] = { "fisu", "lssu", "msu", };
00377 
00378 static char *complete_dump_start(const char *line, const char *word, int pos, int state)
00379 {
00380   if(pos == 4) {
00381     return complete_generic(word, state, dir_options,
00382                             sizeof(dir_options)/sizeof(dir_options[0]));
00383   } else if(pos > 4) {
00384     return complete_generic(word, state, filter_options,
00385                             sizeof(filter_options)/sizeof(filter_options[0]));
00386   } else {
00387     /* We won't attempt to complete file names, that's not worth it. */
00388     return NULL;
00389   }
00390 }
00391 
00392 static char *complete_dump_stop(const char *line, const char *word, int pos, int state)
00393 {
00394   if(pos == 3) {
00395     return complete_generic(word, state, dir_options,
00396                             sizeof(dir_options)/sizeof(dir_options[0]));
00397   } else {
00398     return NULL;
00399   }
00400 }
00401 
00402 static int cmd_dump_start(int fd, int argc, char *argv[]) {
00403   int in, out;
00404   int i;
00405   int fisu,lssu,msu;
00406   FILE *fh;
00407 
00408   if(argc < 4) {
00409     return RESULT_SHOWUSAGE;
00410   }
00411 
00412   if(argc == 4) {
00413     in = 1;
00414     out = 1;
00415   } else {
00416     if(0 == strcasecmp(argv[4], "in")) {
00417       in = 1;
00418       out = 0;
00419     } else if(0 == strcasecmp(argv[4], "out")) {
00420       in = 0;
00421       out = 1;
00422     } else if(0 == strcasecmp(argv[4], "both")) {
00423       in = 1;
00424       out = 1;
00425     } else {
00426       return RESULT_SHOWUSAGE;
00427     }
00428   }
00429 
00430   ast_mutex_lock(&dump_mutex);
00431   if((in && dump_in_fh != NULL) || (out && dump_out_fh != NULL)) {
00432     ast_cli(fd, "Dump already running, must be stopped (with 'ss7 stop dump') "
00433             "before new can be started.\n");
00434     ast_mutex_unlock(&dump_mutex);
00435     return RESULT_FAILURE;
00436   }
00437 
00438   if(argc <= 5) {
00439     fisu = 0;
00440     lssu = 0;
00441     msu = 1;
00442   } else {
00443     fisu = 0;
00444     lssu = 0;
00445     msu = 0;
00446     for(i = 5; i < argc; i++) {
00447       if(0 == strcasecmp(argv[i], "fisu")) {
00448         fisu = 1;
00449       } else if(0 == strcasecmp(argv[i], "lssu")) {
00450         lssu = 1;
00451       } else if(0 == strcasecmp(argv[i], "msu")) {
00452         msu = 1;
00453       } else {
00454         ast_mutex_unlock(&dump_mutex);
00455         return RESULT_SHOWUSAGE;
00456       }
00457     }
00458   }
00459 
00460   fh = fopen(argv[3], "w");
00461   if(fh == NULL) {
00462     ast_cli(fd, "Error opening file '%s': %s.\n", argv[3], strerror(errno));
00463     ast_mutex_unlock(&dump_mutex);
00464     return RESULT_FAILURE;
00465   }
00466 
00467   if(in) {
00468     dump_in_fh = fh;
00469   }
00470   if(out) {
00471     dump_out_fh = fh;
00472   }
00473   dump_do_fisu = fisu;
00474   dump_do_lssu = lssu;
00475   dump_do_msu = msu;
00476   init_pcap_file(fh);
00477 
00478   ast_mutex_unlock(&dump_mutex);
00479   return RESULT_SUCCESS;
00480 }
00481 
00482 static int cmd_dump_stop(int fd, int argc, char *argv[]) {
00483   int in, out;
00484 
00485   if(argc == 3) {
00486     in = 1;
00487     out = 1;
00488   } else if(argc == 4) {
00489     if(0 == strcasecmp(argv[3], "in")) {
00490       in = 1;
00491       out = 0;
00492     } else if(0 == strcasecmp(argv[3], "out")) {
00493       in = 0;
00494       out = 1;
00495     } else if(0 == strcasecmp(argv[3], "both")) {
00496       in = 1;
00497       out = 1;
00498     } else {
00499       return RESULT_SHOWUSAGE;
00500     }
00501   } else {
00502     return RESULT_SHOWUSAGE;
00503   }
00504 
00505   ast_mutex_lock(&dump_mutex);
00506 
00507   if((in && !out && dump_in_fh == NULL) ||
00508      (out && !in && dump_out_fh == NULL) ||
00509      (in && out && dump_in_fh == NULL && dump_out_fh == NULL)) {
00510     ast_cli(fd, "No dump running.\n");
00511     ast_mutex_unlock(&dump_mutex);
00512     return RESULT_SUCCESS;
00513   }
00514 
00515   if(in && dump_in_fh != NULL) {
00516     if(dump_out_fh == dump_in_fh) {
00517       /* Avoid closing it twice. */
00518       dump_out_fh = NULL;
00519     }
00520     fclose(dump_in_fh);
00521     dump_in_fh = NULL;
00522   }
00523   if(out && dump_out_fh != NULL) {
00524     fclose(dump_out_fh);
00525     dump_out_fh = NULL;
00526   }
00527 
00528   ast_mutex_unlock(&dump_mutex);
00529   return RESULT_SUCCESS;
00530 }
00531 
00532 static int cmd_dump_status(int fd, int argc, char *argv[]) {
00533   ast_mutex_lock(&dump_mutex);
00534 
00535   /* ToDo: This doesn't seem to work, the output is getting lost somehow.
00536      Not sure why, but could be related to ast_carefulwrite() called in
00537      ast_cli(). */
00538   ast_cli(fd, "Yuck! what is going on here?!?\n");
00539   if(dump_in_fh != NULL) {
00540     ast_cli(fd, "Dump of incoming frames is running.\n");
00541   }
00542   if(dump_out_fh != NULL) {
00543     ast_cli(fd, "Dump of outgoing frames is running.\n");
00544   }
00545   if(dump_in_fh != NULL || dump_out_fh != NULL) {
00546     ast_cli(fd, "Filter:%s%s%s.\n",
00547             (dump_do_fisu ? " fisu" : ""),
00548             (dump_do_lssu ? " lssu" : ""),
00549             (dump_do_msu ? " msu" : ""));
00550   }
00551 
00552   ast_mutex_unlock(&dump_mutex);
00553   return RESULT_SUCCESS;
00554 }
00555 
00556 
00557 static int cmd_version(int fd, int argc, char *argv[])
00558 {
00559   ast_cli(fd, "chan_ss7 version %s\n", CHAN_SS7_VERSION);
00560 
00561   return RESULT_SUCCESS;
00562 }
00563 
00564 
00565 static int cmd_ss7_status(int fd, int argc, char *argv[])
00566 {
00567   cmd_linkset_status(fd, argc, argv);
00568   return RESULT_SUCCESS;
00569 }
00570 
00571 
00572 static void process_event(struct mtp_event* event)
00573 {
00574   FILE *dump_fh;
00575 
00576   switch(event->typ) {
00577   case MTP_EVENT_ISUP:
00578     l4isup_event(event);
00579     break;
00580   case MTP_EVENT_SCCP:
00581     break;
00582   case MTP_EVENT_REQ_REGISTER:
00583     if (event->regist.ss7_protocol == 5) {
00584       struct link* link = &links[event->regist.isup.slinkix];
00585       mtp3_register_isup(link->mtp3fd, link->linkix);
00586     }
00587     break;
00588   case MTP_EVENT_LOG:
00589     ast_log(event->log.level, event->log.file, event->log.line,
00590        event->log.function, "%s", event->buf);
00591     break;
00592 
00593   case MTP_EVENT_DUMP:
00594     ast_mutex_lock(&dump_mutex);
00595 
00596     if(event->dump.out) {
00597       dump_fh = dump_out_fh;
00598     } else {
00599       dump_fh = dump_in_fh;
00600     }
00601     if(dump_fh != NULL) {
00602       if(event->len < 3 ||
00603     ( !(event->buf[2] == 0 && !dump_do_fisu) &&
00604       !((event->buf[2] == 1 || event->buf[2] == 2) && !dump_do_lssu) &&
00605       !(event->buf[2] > 2 && !dump_do_msu)))
00606    dump_pcap(dump_fh, event);
00607     }
00608 
00609     ast_mutex_unlock(&dump_mutex);
00610     break;
00611 
00612   case MTP_EVENT_STATUS:
00613     {
00614       struct link* link = event->status.link;
00615       char* name = link ? link->name : "(peer)";
00616       switch(event->status.link_state) {
00617       case MTP_EVENT_STATUS_LINK_UP:
00618    l4isup_link_status_change(link, 1);
00619    ast_log(LOG_WARNING, "MTP is now UP on link '%s'.\n", name);
00620    break;
00621       case MTP_EVENT_STATUS_LINK_DOWN:
00622    l4isup_link_status_change(link, 0);
00623    ast_log(LOG_WARNING, "MTP is now DOWN on link '%s'.\n", name);
00624    break;
00625       case MTP_EVENT_STATUS_INSERVICE:
00626    ast_log(LOG_WARNING, "Signaling ready for linkset '%s'.\n", link->linkset->name);
00627    l4isup_inservice(link);
00628    break;
00629       default:
00630    ast_log(LOG_NOTICE, "Unknown event type STATUS (%d), "
00631       "not processed.\n", event->status.link_state);
00632       }
00633     }
00634     break;
00635 
00636   default:
00637     ast_log(LOG_NOTICE, "Unexpected mtp event type %d.\n", event->typ);
00638   }
00639 }
00640 
00641 /* Monitor thread main loop.
00642    Monitor reads events from the realtime MTP thread, and processes them at
00643    non-realtime priority. It also handles timers for ISUP etc.
00644 */
00645 static void *monitor_main(void *data) {
00646   int res, nres;
00647   struct pollfd fds[(MAX_LINKS+1)];
00648   int i, n_fds;
00649   int rebuild_fds = 1;
00650   struct lffifo *receive_fifo = mtp_get_receive_fifo();
00651 
00652   ast_verbose(VERBOSE_PREFIX_3 "Starting monitor thread, pid=%d.\n", getpid());
00653 
00654   fds[0].fd = get_receive_pipe();
00655   fds[0].events = POLLIN;
00656   while(monitor_running) {
00657     if (rebuild_fds) {
00658       if (rebuild_fds > 1)
00659    poll(fds, 0, 200); /* sleep */
00660       rebuild_fds = 0;
00661       n_fds = 1;
00662       for (i = 0; i < n_linksets; i++) {
00663    struct linkset* linkset = &linksets[i];
00664    int j;
00665    for (j = 0; j < linkset->n_links; j++) {
00666      int k;
00667      struct link* link = linkset->links[j];
00668      for (k = 0; k < this_host->n_spans; k++) {
00669        if (this_host->spans[k].link == link)
00670          break;
00671        if ((this_host->spans[k].link->linkset == link->linkset) ||
00672       (is_combined_linkset(this_host->spans[k].link->linkset, link->linkset)))
00673          break;
00674      }
00675      if (k < this_host->n_spans) {
00676        if (link->remote) {
00677          if (link->mtp3fd == -1) {
00678       link->mtp3fd = mtp3_connect_socket(link->mtp3server_host, link->mtp3server_port);
00679       if (link->mtp3fd != -1)
00680         res = mtp3_register_isup(link->mtp3fd, link->linkix);
00681       if ((link->mtp3fd == -1) || (res == -1))
00682         rebuild_fds += 2;
00683          }
00684          fds[n_fds].fd = link->mtp3fd;
00685          fds[n_fds++].events = POLLIN|POLLERR|POLLNVAL|POLLHUP;
00686        }
00687      }
00688    }
00689       }
00690     }
00691     int timeout = timers_wait();
00692 
00693     nres = poll(fds, n_fds, timeout);
00694     if(nres < 0) {
00695       if(errno == EINTR) {
00696         /* Just try again. */
00697       } else {
00698         ast_log(LOG_ERROR, "poll() failure, errno=%d: %s\n",
00699                 errno, strerror(errno));
00700       }
00701     } else if(nres > 0) {
00702       for (i = 0; (i < n_fds) && (nres > 0); i++) {
00703    unsigned char eventbuf[MTP_EVENT_MAX_SIZE];
00704    struct mtp_event *event = (struct mtp_event*) eventbuf;
00705    struct link* link = NULL;
00706    if(fds[i].revents) {
00707      int j;
00708      for (j = 0; j < n_links; j++) {
00709        if (links[j].remote && (links[j].mtp3fd == fds[i].fd)) {
00710          link = &links[j];
00711          break;
00712        }
00713      }
00714    }
00715    else
00716      continue;
00717    if(fds[i].revents & (POLLERR|POLLNVAL|POLLHUP)) {
00718      if (i == 0) { /* receivepipe */
00719        ast_log(LOG_ERROR, "poll() return bad revents for receivepipe, 0x%04x\n", fds[i].revents);
00720      }
00721      close(fds[i].fd);
00722      if (link)
00723        link->mtp3fd = -1;
00724      rebuild_fds++; rebuild_fds++; /* when > 1, use short sleep */
00725      nres--;
00726      continue;
00727    }
00728    if(!(fds[i].revents & POLLIN))
00729      continue;
00730    if (i == 0) {
00731      /* Events waiting in the receive buffer. */
00732      unsigned char dummy[512];
00733 
00734      /* Empty the pipe before pulling from fifo. This way the race
00735         condition between mtp and monitor threads may cause spurious
00736         wakeups, but not loss/delay of messages. */
00737      read(fds[i].fd, dummy, sizeof(dummy));
00738 
00739      /* Process all available events. */
00740      while((res = lffifo_get(receive_fifo, eventbuf, sizeof(eventbuf))) != 0) {
00741        if(res < 0) {
00742          ast_log(LOG_ERROR, "Yuck! oversized frame in receive fifo, bailing out.\n");
00743          return NULL;
00744        }
00745        process_event(event);
00746      }
00747    }
00748    else {
00749 #if MTP3_SOCKET == SOCK_STREAM
00750      res = read(fds[i].fd, eventbuf, sizeof(struct mtp_event));
00751      if ((res > 0) && (event->len > 0)) {
00752        int p = res;
00753        int len = event->len;
00754        if (sizeof(struct mtp_event) + event->len > MTP_EVENT_MAX_SIZE) {
00755          ast_log(LOG_NOTICE, "Got too large packet: len %d, max %d, discarded", sizeof(struct mtp_event) + event->len, MTP_EVENT_MAX_SIZE);
00756          len = 0;
00757          res = 0;
00758        }
00759        do {
00760          res = read(fds[i].fd, &eventbuf[p], len);
00761          if (res > 0) {
00762       p += res;
00763       len -= res;
00764          }
00765          else if ((res < 0) && (errno != EINTR)) {
00766       len = 0;
00767          }
00768          else {
00769       len = 0;
00770          }
00771        } while (len > 0);
00772      }
00773 #else
00774      res = read(fds[i].fd, eventbuf, sizeof(eventbuf)+MTP_MAX_PCK_SIZE);
00775 #endif
00776      if (res > 0) {
00777        if (event->typ == MTP_EVENT_ISUP) {
00778          event->isup.link = NULL;
00779          event->isup.slink = &links[event->isup.slinkix];
00780        }
00781        process_event(event);
00782      }
00783      else if (res == 0) {
00784        int j;
00785        for (j = 0; j < n_links; j++) {
00786          struct link* link = &links[j];
00787          if (link->remote && (link->mtp3fd == fds[i].fd)) {
00788       close(fds[i].fd);
00789       link->mtp3fd = -1;
00790       rebuild_fds++;
00791          }
00792        }
00793      }
00794    }
00795    nres--;
00796       }
00797     }
00798 
00799     /* We need to lock the global glock mutex around ast_sched_runq() so that
00800        we avoid a race with ss7_hangup. With the lock, invalidating the
00801        channel in ss7_hangup() and removing associated monitor_sched entries
00802        is an atomic operation, so that we avoid calling timer handlers with
00803        references to invalidated channels. */
00804     run_timers();
00805   }
00806   return NULL;
00807 }
00808 
00809 
00810 static void stop_monitor(void) {
00811   int i;
00812 
00813   if(monitor_running) {
00814     monitor_running = 0;
00815     /* Monitor wakes up every 1/2 sec, so no need to signal it explicitly. */
00816     pthread_join(monitor_thread, NULL);
00817   }
00818   for (i = 0; i < n_links; i++) {
00819     struct link* link = &links[i];
00820     if (link->remote && (link->mtp3fd > -1))
00821       close(link->mtp3fd);
00822   }
00823 }
00824 
00825 
00826 static int ss7_reload_module(void) {
00827   ast_log(LOG_NOTICE, "SS7 reload not implemented.\n");
00828   return AST_MODULE_LOAD_SUCCESS;
00829 }
00830 
00831 
00832 static int ss7_load_module(void)
00833 {
00834   if(load_config(0)) {
00835     return AST_MODULE_LOAD_FAILURE;
00836   }
00837 
00838   if (timers_init()) {
00839     ast_log(LOG_ERROR, "Unable to initialize timers.\n");
00840     return AST_MODULE_LOAD_FAILURE;
00841   }
00842   if (isup_init()) {
00843     ast_log(LOG_ERROR, "Unable to initialize ISUP.\n");
00844     return AST_MODULE_LOAD_FAILURE;
00845   }
00846 #ifdef SCCP
00847   if (sccp_init()) {
00848     ast_log(LOG_ERROR, "Unable to initialize SCCP.\n");
00849     return AST_MODULE_LOAD_FAILURE;
00850   }
00851 #endif
00852 
00853   if(mtp_init()) {
00854     ast_log(LOG_ERROR, "Unable to initialize MTP.\n");
00855     return AST_MODULE_LOAD_FAILURE;
00856   }
00857   if(start_mtp_thread()) {
00858     ast_log(LOG_ERROR, "Unable to start MTP thread.\n");
00859     return AST_MODULE_LOAD_FAILURE;
00860   }
00861   mtp_control_fifo = mtp_get_control_fifo();
00862 
00863   monitor_running = 1;          /* Otherwise there is a race, and
00864                                    monitor may exit immediately */
00865   if(ast_pthread_create(&monitor_thread, NULL, monitor_main, NULL) < 0) {
00866     ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
00867     monitor_running = 0;
00868     return AST_MODULE_LOAD_FAILURE;
00869   }
00870 
00871 
00872   ast_cli_register_multiple(my_clis, sizeof(my_clis)/ sizeof(my_clis[0]));
00873 
00874   ast_verbose(VERBOSE_PREFIX_3 "SS7 channel loaded successfully.\n");
00875   return AST_MODULE_LOAD_SUCCESS;
00876 }
00877 
00878 
00879 static int ss7_unload_module(void)
00880 {
00881   ast_cli_unregister_multiple(my_clis, sizeof(my_clis)/ sizeof(my_clis[0]));
00882 
00883 #ifdef SCCP
00884   sccp_cleanup();
00885 #endif
00886   isup_cleanup();
00887 
00888   ast_mutex_lock(&dump_mutex);
00889   if(dump_in_fh != NULL) {
00890     if(dump_in_fh == dump_out_fh) {
00891       dump_out_fh = NULL;
00892     }
00893     fclose(dump_in_fh);
00894     dump_in_fh = NULL;
00895   }
00896   if(dump_out_fh != NULL) {
00897     fclose(dump_out_fh);
00898     dump_out_fh = NULL;
00899   }
00900   ast_mutex_unlock(&dump_mutex);
00901 
00902   if(monitor_running) {
00903     stop_monitor();
00904   }
00905   stop_mtp_thread();
00906   mtp_cleanup();
00907   timers_cleanup();
00908 
00909 
00910   destroy_config();
00911   ast_verbose(VERBOSE_PREFIX_3 "SS7 channel unloaded.\n");
00912   return AST_MODULE_LOAD_SUCCESS;
00913 }
00914 
00915 
00916 #ifdef USE_ASTERISK_1_2
00917 int reload(void)
00918 {
00919   return ss7_reload_module();
00920 }
00921 int load_module(void)
00922 {
00923   return ss7_load_module();
00924 }
00925 int unload_module(void)
00926 {
00927   return ss7_unload_module();
00928 }
00929 char *description() {
00930   return (char *) desc;
00931 }
00932 
00933 char *key() {
00934   return ASTERISK_GPL_KEY;
00935 }
00936 #else
00937 #define AST_MODULE "chan_ss7"
00938 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, desc,
00939                 .load = ss7_load_module,
00940                 .unload = ss7_unload_module,
00941                 .reload = ss7_reload_module,
00942 );
00943 #endif

Generated on Fri Jul 3 20:35:37 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.9