1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
19 struct timeval now_tv;
20 struct timezone now_tz;
23 gettimeofday(&now_tv, &now_tz); \
24 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
25 now = now_tv.tv_sec; \
26 now_tm = localtime(&now); \
29 FILE *debug_fp = NULL;
33 struct lcr_fdset lcr_fdset[FD_SETSIZE];
36 pthread_mutex_t mutexd; // debug output mutex
37 pthread_mutex_t mutext; // trace output mutex
38 pthread_mutex_t mutexe; // error output mutex
39 //pthread_mutex_t mutex_lcr; // lcr process mutex
52 const char *debug_prefix = NULL;
55 int debug_newline = 1;
58 void debug_usleep(int msec, const char *file, int line, int hour, int min, int sec)
63 void debug(const char *function, int line, const char *prefix, char *buffer)
65 /* if we have a new debug count, we add a mark */
66 if (last_debug != debug_count) {
67 last_debug = debug_count;
69 printf("\033[34m--------------------- %04d.%02d.%02d %02d:%02d:%02d %06d\033[36m\n", now_tm->tm_year+1900, now_tm->tm_mon+1, now_tm->tm_mday, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec, debug_count%1000000);
71 fprintf(debug_fp, "--------------------- %04d.%02d.%02d %02d:%02d:%02d %06d\n", now_tm->tm_year+1900, now_tm->tm_mon+1, now_tm->tm_mday, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec, debug_count%1000000);
76 printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
78 printf("(in %s() line %d): %s", function, line, buffer);
86 fprintf(debug_fp, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
88 fprintf(debug_fp, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
94 if (buffer[strlen(buffer)-1] == '\n')
99 void _printdebug(const char *function, int line, unsigned int mask, const char *fmt, ...)
104 if (!(options.deb & mask))
106 pthread_mutex_lock(&mutexd);
109 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
110 buffer[sizeof(buffer)-1]=0;
113 debug(function, line, debug_prefix, buffer);
115 pthread_mutex_unlock(&mutexd);
118 void _printerror(const char *function, int line, const char *fmt, ...)
123 pthread_mutex_lock(&mutexe);
126 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
127 buffer[sizeof(buffer)-1]=0;
131 debug(function, line, "ERROR", buffer);
132 else { /* only if we do not debug */
134 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
136 fprintf(stderr, "ERROR %s", buffer);
139 pthread_mutex_unlock(&mutexe);
143 void sighandler(int sigset)
145 struct sched_param schedp;
147 if (sigset == SIGHUP)
149 if (sigset == SIGPIPE)
153 /* set scheduler & priority */
154 if (options.schedule > 1) {
155 memset(&schedp, 0, sizeof(schedp));
156 schedp.sched_priority = 0;
157 sched_setscheduler(0, SCHED_OTHER, &schedp);
159 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
160 PDEBUG(DEBUG_LOG, "Signal received: %d\n", sigset);
168 int main(int argc, char *argv[])
171 int lockfd = -1; /* file lock */
172 struct lcr_msg *message;
174 class Endpoint *epoint;
178 char prefix_string[64];
179 struct sched_param schedp;
180 const char *debug_prefix = "alloc";
181 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
182 created_lock = 0, created_signal = 0, created_debug = 0,
184 int idletime = 0, idlecheck = 0;
185 char tracetext[256], lock[128];
189 memset(lcr_fdset, 0, sizeof(lcr_fdset));
192 /* lock LCR process */
193 // pthread_mutex_lock(&mutex_lcr);
199 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
205 printf("Usage: lcr (query | start | fork | rules | route)\n");
206 printf("query = Show available isdn ports.\n");
207 printf("start = Run lcr normally, abort with CTRL+C.\n");
208 printf("fork = Do daemon fork and run as background process.\n");
209 printf("interface = Get help of available interface syntax.\n");
210 printf("rules = Get help of available routing rule syntax.\n");
211 printf("rules [action] = Get individual help for given action.\n");
212 // printf("route = Show current routing as it is parsed.\n");
222 if (pthread_mutex_init(&mutexd, NULL)) {
223 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
227 // if (pthread_mutex_init(&mutext, NULL)) {
228 // fprintf(stderr, "cannot create 'trace' mutex\n");
231 // created_mutext = 1;
232 if (pthread_mutex_init(&mutexe, NULL)) {
233 fprintf(stderr, "cannot create 'PERROR' mutex\n");
239 if (!(strcasecmp(argv[1],"interface"))) {
246 if (!(strcasecmp(argv[1],"rules"))) {
255 /* query available isdn ports */
256 if (!(strcasecmp(argv[1],"query"))) {
257 fprintf(stderr, "-> Using 'misdn_info'\n");
258 system("misdn_info");
264 if (read_options() == 0) {
265 PERROR("%s", options_error);
270 if (mISDN_initialize() < 0)
275 /* read ruleset(s) */
276 if (!(ruleset_first = ruleset_parse()))
279 /* set pointer to main ruleset */
280 ruleset_main = getrulesetbyname("main");
282 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
283 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
288 /* query available isdn ports */
289 if (!(strcasecmp(argv[1],"route"))) {
290 ruleset_debug(ruleset_first);
296 /* do fork in special cases */
297 if (!(strcasecmp(argv[1],"fork"))) {
305 fprintf(stderr, "Cannot fork!\n");
318 fprintf(stderr, "Cannot fork!\n");
322 printf("LCR: Starting daemon.\n");
328 pidfile = fopen("/var/run/lcr.pid","w");
330 fprintf(pidfile, "%d\n", getpid());
335 if (!!strcasecmp(argv[1],"start")) {
339 /* create lock and lock! */
340 SPRINT(lock, "%s/lcr.lock", options.lock);
341 if ((lockfd = open(lock, O_CREAT | O_WRONLY, S_IWUSR)) < 0) {
342 fprintf(stderr, "Cannot create lock file: %s\n", lock);
343 fprintf(stderr, "Check options.conf to change to path with permissions for you.\n");
346 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
347 if (errno == EWOULDBLOCK)
348 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
349 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
354 /* initialize admin socket */
356 fprintf(stderr, "Unable to initialize admin socket.\n");
360 /* generate alaw / ulaw tables */
361 generate_tables(options.law);
364 /* init ss5 sine tables */
369 /* load tones (if requested) */
370 if (fetch_tones() == 0) {
371 fprintf(stderr, "Unable to fetch tones into memory.\n");
377 if (options.gsm && gsm_init()) {
378 fprintf(stderr, "GSM initialization failed.\n");
383 fprintf(stderr, "GSM is enabled, but not compiled. Use --with-gsm while configure!\n");
388 /* read interfaces and open ports */
389 if (!read_interfaces()) {
390 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
391 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
395 interface_first = interface_newlist;
396 interface_newlist = NULL;
398 /* locking memory paging */
401 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
409 fprintf(stderr, "Warning: Not enough memory to lock paging.\n");
412 fprintf(stderr, "Warning: No permission to lock paging.\n");
415 fprintf(stderr, "Warning: 'Bad address' while locking paging.\n");
418 fprintf(stderr, "Warning: Unknown error %d while locking paging.\n", errno);
422 /* set real time scheduler & priority */
423 if (options.schedule > 1) {
424 memset(&schedp, 0, sizeof(schedp));
425 schedp.sched_priority = options.schedule;
426 ret = sched_setscheduler(0, SCHED_RR, &schedp);
428 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
433 /* signal handlers */
434 signal(SIGINT,sighandler);
435 signal(SIGHUP,sighandler);
436 signal(SIGTERM,sighandler);
437 signal(SIGPIPE,sighandler);
441 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
442 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
443 printf("%s\n", tracetext);
451 if (now_d-last_d > 1.0) {
452 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
454 /* all loops must be counted from the beginning since nodes might get freed during handler */
457 //#warning debugging usleep crash
458 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
460 /* handle mISDN messages from kernel */
461 debug_prefix = "ISDN";
464 //#warning debugging usleep crash
465 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
469 /* loop through all port ports and call their handler */
473 debug_prefix = port->p_name;
475 ret = port->handler();
478 if (ret < 0) /* port has been destroyed */
483 /* loop through all epoint and call their handler */
485 epoint = epoint_first;
487 debug_prefix = prefix_string;
488 SPRINT(prefix_string, "ep%ld", epoint->ep_serial);
490 ret = epoint->handler();
493 if (ret < 0) /* epoint has been destroyed */
495 epoint = epoint->next;
498 /* loop through all joins and call their handler */
502 debug_prefix = "join";
504 ret = join->handler();
507 if (ret < 0) /* join has been destroyed */
514 /* process any message */
516 debug_prefix = "message";
517 while ((message = message_get())) {
519 switch(message->flow) {
521 debug_prefix = "msg port->epoint";
522 epoint = find_epoint_id(message->id_to);
524 if (epoint->ep_app) {
525 epoint->ep_app->ea_message_port(message->id_from, message->type, &message->param);
527 PDEBUG(DEBUG_MSG, "Warning: message %s from port %d to endpoint %d. endpoint doesn't have an application.\n", messages_txt[message->type], message->id_from, message->id_to);
530 PDEBUG(DEBUG_MSG, "Warning: message %s from port %d to endpoint %d. endpoint doesn't exist anymore.\n", messages_txt[message->type], message->id_from, message->id_to);
535 debug_prefix = "msg epoint->join";
536 join = find_join_id(message->id_to);
538 join->message_epoint(message->id_from, message->type, &message->param);
540 PDEBUG(DEBUG_MSG, "Warning: message %s from endpoint %d to join %d. join doesn't exist anymore\n", messages_txt[message->type], message->id_from, message->id_to);
545 debug_prefix = "msg join->epoint";
546 epoint = find_epoint_id(message->id_to);
548 if (epoint->ep_app) {
549 epoint->ep_app->ea_message_join(message->id_from, message->type, &message->param);
551 PDEBUG(DEBUG_MSG, "Warning: message %s from join %d to endpoint %d. endpoint doesn't have an application.\n", messages_txt[message->type], message->id_from, message->id_to);
554 PDEBUG(DEBUG_MSG, "Warning: message %s from join %d to endpoint %d. endpoint doesn't exist anymore.\n", messages_txt[message->type], message->id_from, message->id_to);
559 debug_prefix = "msg epoint->port";
560 port = find_port_id(message->id_to);
562 port->message_epoint(message->id_from, message->type, &message->param);
565 PDEBUG(DEBUG_MSG, "Warning: message %s from endpoint %d to port %d. port doesn't exist anymore\n", messages_txt[message->type], message->id_from, message->id_to);
570 PERROR("Message flow %d unknown.\n", message->flow);
572 message_free(message);
574 debug_prefix = "message";
593 /* check for child to exit (eliminate zombies) */
594 if (waitpid(-1, NULL, WNOHANG) > 0) {
595 PDEBUG(DEBUG_EPOINT, "a child process (created by endpoint) has exitted.\n");
599 //#warning debugging usleep crash
600 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
602 /* do idle checking */
603 if (idlecheck != now) {
604 PDEBUG(DEBUG_IDLETIME, "Idle time : %d%%\n", idletime/10000);
609 /* did we do nothing? so we wait to give time to other processes */
611 // pthread_mutex_unlock(&mutex_lcr); // unlock LCR
612 debug_usleep(4000, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
613 // pthread_mutex_lock(&mutex_lcr); // lock LCR
617 SPRINT(tracetext, "%s terminated", NAME);
618 printf("%s\n", tracetext);
619 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
621 add_trace((char *)"signal", NULL, "%d", quit);
629 /* set scheduler & priority
631 if (options.schedule > 1) {
632 memset(&schedp, 0, sizeof(schedp));
633 schedp.sched_priority = options.schedule;
634 sched_setscheduler(0, SCHED_OTHER, &schedp);
637 if (created_signal) {
638 signal(SIGINT,SIG_DFL);
639 signal(SIGHUP,SIG_DFL);
640 signal(SIGTERM,SIG_DFL);
641 signal(SIGPIPE,SIG_DFL);
644 /* destroy objects */
645 debug_prefix = "free";
651 while(epoint_first) {
659 /* free interfaces */
661 free_interfaces(interface_first);
662 interface_first = NULL;
664 /* close isdn ports */
665 mISDNport_close_all();
670 while ((message = message_get())) {
672 message_free(message);
675 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
682 /* free admin socket */
687 flock(lockfd, LOCK_UN);
696 ruleset_free(ruleset_first);
697 ruleset_first = NULL;
701 if (pthread_mutex_destroy(&mutexe))
702 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
703 // if (created_mutext)
704 // if (pthread_mutex_destroy(&mutext))
705 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
707 if (pthread_mutex_destroy(&mutexd))
708 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
710 /* deinitialize mISDN */
712 mISDN_deinitialize();
720 /* display memory leak */
721 #define MEMCHECK(a, b) \
723 SPRINT(tracetext, a, NAME); \
724 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext); \
725 if (ret) add_trace("blocks", NULL, "%d", b); \
727 printf("\n******************************\n\007"); \
728 printf("\nERROR: %d %s\n", b, a); \
729 printf("\n******************************\n"); \
733 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
734 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
735 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
736 MEMCHECK("memory block(s) left (message.c)",mmemuse)
737 MEMCHECK("memory block(s) left (route.c)",rmemuse)
738 MEMCHECK("memory block(s) left (args)",amemuse)
739 MEMCHECK("class(es) left",classuse)
740 MEMCHECK("file descriptor(s) left",fduse)
741 MEMCHECK("file handler(s) left",fhuse)
743 /* unlock LCR process */
744 // pthread_mutex_unlock(&mutex_lcr);
752 /* special debug function to detect buffer overflow
754 int budetect_stop = 0;
755 void budetect(const char *file, int line, const char *function)
759 /* modify this function to detect race-bugs */
760 #warning DID YOU MODIFY THIS FUNCTION TO DETECT THE BUFFER OVERFLOW BUG?
762 class PmISDN *pmisdn;
763 struct mISDNport *mISDNport = mISDNport_first;
768 ii = mISDNport->b_num;
770 if (mISDNport->b_port[i]) {
773 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_ISDN) {
774 pmisdn = (class PmISDN *)port;
775 if (pmisdn->p_isdn_crypt_listen) {
776 PERROR_RUNTIME("************************************************\n");
777 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
778 PERROR_RUNTIME("** p_isdn_crypt_listen = %d\n", pmisdn->p_isdn_crypt_listen);
779 PERROR_RUNTIME("************************************************\n");
783 if (port == mISDNport->b_port[i])
787 PERROR_RUNTIME("************************************************\n");
788 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
789 PERROR_RUNTIME("** b_port not in list.\n");
790 PERROR_RUNTIME("************************************************\n");
797 mISDNport = mISDNport->next;