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 char *debug_prefix = 0;
55 int debug_newline = 1;
58 void debug_usleep(int msec, char *file, int line, int hour, int min, int sec)
63 void debug(const char *function, int line, char *prefix, char *buffer)
65 /* if we have a new debug count, we add a mark */
66 if (last_debug != debug_count)
68 last_debug = debug_count;
70 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);
72 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);
78 printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
80 printf("(in %s() line %d): %s", function, line, buffer);
90 fprintf(debug_fp, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
92 fprintf(debug_fp, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
98 if (buffer[strlen(buffer)-1] == '\n')
103 void _printdebug(const char *function, int line, unsigned int mask, const char *fmt, ...)
108 if (!(options.deb & mask))
110 pthread_mutex_lock(&mutexd);
113 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
114 buffer[sizeof(buffer)-1]=0;
117 debug(function, line, debug_prefix, buffer);
119 pthread_mutex_unlock(&mutexd);
122 void _printerror(const char *function, int line, const char *fmt, ...)
127 pthread_mutex_lock(&mutexe);
130 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
131 buffer[sizeof(buffer)-1]=0;
135 debug(function, line, "ERROR", buffer);
136 else /* only if we do not debug */
139 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
141 fprintf(stderr, "ERROR %s", buffer);
144 pthread_mutex_unlock(&mutexe);
148 void sighandler(int sigset)
150 struct sched_param schedp;
152 if (sigset == SIGHUP)
154 if (sigset == SIGPIPE)
159 /* set scheduler & priority */
160 if (options.schedule > 1)
162 memset(&schedp, 0, sizeof(schedp));
163 schedp.sched_priority = 0;
164 sched_setscheduler(0, SCHED_OTHER, &schedp);
166 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
167 PDEBUG(DEBUG_LOG, "Signal received: %d\n", sigset);
175 int main(int argc, char *argv[])
178 int lockfd = -1; /* file lock */
179 struct lcr_msg *message;
181 class Endpoint *epoint;
185 char prefix_string[64];
186 struct sched_param schedp;
187 char *debug_prefix = "alloc";
188 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
189 created_lock = 0, created_signal = 0, created_debug = 0,
191 int idletime = 0, idlecheck = 0;
196 memset(lcr_fdset, 0, sizeof(lcr_fdset));
199 /* lock LCR process */
200 // pthread_mutex_lock(&mutex_lcr);
206 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
213 printf("Usage: lcr (query | start | fork | rules | route)\n");
214 printf("query = Show available isdn ports.\n");
215 printf("start = Run lcr normally, abort with CTRL+C.\n");
216 printf("fork = Do daemon fork and run as background process.\n");
217 printf("interface = Get help of available interface syntax.\n");
218 printf("rules = Get help of available routing rule syntax.\n");
219 printf("rules [action] = Get individual help for given action.\n");
220 // printf("route = Show current routing as it is parsed.\n");
229 /* check for root (real or effective) */
230 if (getuid() && geteuid())
232 fprintf(stderr, "Please run %s as super-user.\n", NAME);
237 if (pthread_mutex_init(&mutexd, NULL))
239 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
243 // if (pthread_mutex_init(&mutext, NULL))
245 // fprintf(stderr, "cannot create 'trace' mutex\n");
248 // created_mutext = 1;
249 if (pthread_mutex_init(&mutexe, NULL))
251 fprintf(stderr, "cannot create 'PERROR' mutex\n");
257 if (!(strcasecmp(argv[1],"interface")))
265 if (!(strcasecmp(argv[1],"rules")))
275 /* query available isdn ports */
276 if (!(strcasecmp(argv[1],"query")))
284 if (read_options() == 0)
286 PERROR("%s", options_error);
291 if (mISDN_initialize() < 0)
296 /* read ruleset(s) */
297 if (!(ruleset_first = ruleset_parse()))
300 /* set pointer to main ruleset */
301 ruleset_main = getrulesetbyname("main");
304 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
305 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
310 /* query available isdn ports */
311 if (!(strcasecmp(argv[1],"route")))
313 ruleset_debug(ruleset_first);
319 /* do fork in special cases */
320 if (!(strcasecmp(argv[1],"fork")))
329 fprintf(stderr, "Cannot fork!\n");
344 fprintf(stderr, "Cannot fork!\n");
349 printf("LCR: Starting daemon.\n");
355 if (!!strcasecmp(argv[1],"start"))
360 /* create lock and lock! */
361 if ((lockfd = open("/var/run/lcr.lock", O_CREAT, 0)) < 0)
363 fprintf(stderr, "Cannot create lock file: /var/run/lcr.lock\n");
366 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0)
368 if (errno == EWOULDBLOCK)
369 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
370 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
375 /* initialize admin socket */
378 fprintf(stderr, "Unable to initialize admin socket.\n");
382 /* generate alaw / ulaw tables */
383 generate_tables(options.law);
385 /* load tones (if requested) */
386 if (fetch_tones() == 0)
388 fprintf(stderr, "Unable to fetch tones into memory.\n");
392 /* read interfaces and open ports */
393 if (!read_interfaces())
395 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
396 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
400 interface_first = interface_newlist;
401 interface_newlist = NULL;
403 /* locking memory paging */
407 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
417 fprintf(stderr, "Not enough memory to lock paging, exitting...\n");
420 fprintf(stderr, "No permission to lock paging, exitting...\n");
423 fprintf(stderr, "'Bad address' while locking paging, exitting...\n");
426 fprintf(stderr, "Unknown error %d while locking paging, exitting...\n", errno);
431 /* set real time scheduler & priority */
432 if (options.schedule > 1)
434 memset(&schedp, 0, sizeof(schedp));
435 schedp.sched_priority = options.schedule;
436 ret = sched_setscheduler(0, SCHED_RR, &schedp);
439 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
444 /* signal handlers */
445 signal(SIGINT,sighandler);
446 signal(SIGHUP,sighandler);
447 signal(SIGTERM,sighandler);
448 signal(SIGPIPE,sighandler);
452 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
453 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
454 printf("%s\n", tracetext);
463 if (now_d-last_d > 1.0)
465 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
467 /* all loops must be counted from the beginning since nodes might get freed during handler */
470 //#warning debugging usleep crash
471 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
473 /* handle mISDN messages from kernel */
474 debug_prefix = "ISDN";
477 //#warning debugging usleep crash
478 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
482 /* loop through all port ports and call their handler */
487 debug_prefix = port->p_name;
489 ret = port->handler();
492 if (ret < 0) /* port has been destroyed */
497 /* loop through all epoint and call their handler */
499 epoint = epoint_first;
502 debug_prefix = prefix_string;
503 SPRINT(prefix_string, "ep%ld", epoint->ep_serial);
505 ret = epoint->handler();
508 if (ret < 0) /* epoint has been destroyed */
510 epoint = epoint->next;
513 /* loop through all joins and call their handler */
518 debug_prefix = "join";
520 ret = join->handler();
523 if (ret < 0) /* join has been destroyed */
530 /* process any message */
532 debug_prefix = "message";
533 while ((message = message_get()))
536 switch(message->flow)
539 debug_prefix = "msg port->epoint";
540 epoint = find_epoint_id(message->id_to);
545 epoint->ep_app->ea_message_port(message->id_from, message->type, &message->param);
548 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);
552 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);
557 debug_prefix = "msg epoint->join";
558 join = find_join_id(message->id_to);
561 join->message_epoint(message->id_from, message->type, &message->param);
564 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);
569 debug_prefix = "msg join->epoint";
570 epoint = find_epoint_id(message->id_to);
575 epoint->ep_app->ea_message_join(message->id_from, message->type, &message->param);
578 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);
582 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);
587 debug_prefix = "msg epoint->port";
588 port = find_port_id(message->id_to);
591 port->message_epoint(message->id_from, message->type, &message->param);
595 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);
600 PERROR("Message flow %d unknown.\n", message->flow);
602 message_free(message);
604 debug_prefix = "message";
614 /* check for child to exit (eliminate zombies) */
615 if (waitpid(-1, NULL, WNOHANG) > 0)
617 PDEBUG(DEBUG_EPOINT, "a child process (created by endpoint) has exitted.\n");
621 //#warning debugging usleep crash
622 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
624 /* do idle checking */
625 if (idlecheck != now)
627 PDEBUG(DEBUG_IDLETIME, "Idle time : %d%%\n", idletime/10000);
632 /* did we do nothing? so we wait to give time to other processes */
635 // pthread_mutex_unlock(&mutex_lcr); // unlock LCR
636 debug_usleep(4000, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
637 // pthread_mutex_lock(&mutex_lcr); // lock LCR
641 SPRINT(tracetext, "%s terminated", NAME);
642 printf("%s\n", tracetext);
643 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
645 add_trace("signal", NULL, "%d", quit);
653 /* set scheduler & priority
655 if (options.schedule > 1)
657 memset(&schedp, 0, sizeof(schedp));
658 schedp.sched_priority = options.schedule;
659 sched_setscheduler(0, SCHED_OTHER, &schedp);
664 signal(SIGINT,SIG_DFL);
665 signal(SIGHUP,SIG_DFL);
666 signal(SIGTERM,SIG_DFL);
667 signal(SIGPIPE,SIG_DFL);
670 /* destroy objects */
671 debug_prefix = "free";
687 /* free interfaces */
689 free_interfaces(interface_first);
690 interface_first = NULL;
692 /* close isdn ports */
693 mISDNport_close_all();
698 while ((message = message_get()))
701 message_free(message);
705 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
712 /* free admin socket */
717 flock(lockfd, LOCK_UN);
723 ruleset_free(ruleset_first);
724 ruleset_first = NULL;
728 if (pthread_mutex_destroy(&mutexe))
729 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
730 // if (created_mutext)
731 // if (pthread_mutex_destroy(&mutext))
732 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
734 if (pthread_mutex_destroy(&mutexd))
735 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
737 /* deinitialize mISDN */
739 mISDN_deinitialize();
741 /* display memory leak */
742 #define MEMCHECK(a, b) \
745 SPRINT(tracetext, a, NAME); \
746 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext); \
747 if (ret) add_trace("blocks", NULL, "%d", b); \
749 printf("\n******************************\n\007"); \
750 printf("\nERROR: %d %s\n", b, a); \
751 printf("\n******************************\n"); \
755 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
756 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
757 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
758 MEMCHECK("memory block(s) left (message.c)",mmemuse)
759 MEMCHECK("memory block(s) left (route.c)",rmemuse)
760 MEMCHECK("memory block(s) left (args)",amemuse)
761 MEMCHECK("class(es) left",classuse)
762 MEMCHECK("file descriptor(s) left",fduse)
763 MEMCHECK("file handler(s) left",fhuse)
765 /* unlock LCR process */
766 // pthread_mutex_unlock(&mutex_lcr);
774 /* special debug function to detect buffer overflow
776 int budetect_stop = 0;
777 void budetect(const char *file, int line, char *function)
781 /* modify this function to detect race-bugs */
782 #warning DID YOU MODIFY THIS FUNCTION TO DETECT THE BUFFER OVERFLOW BUG?
784 class PmISDN *pmisdn;
785 struct mISDNport *mISDNport = mISDNport_first;
791 ii = mISDNport->b_num;
794 if (mISDNport->b_port[i])
799 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_ISDN)
801 pmisdn = (class PmISDN *)port;
802 if (pmisdn->p_isdn_crypt_listen)
804 PERROR_RUNTIME("************************************************\n");
805 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
806 PERROR_RUNTIME("** p_isdn_crypt_listen = %d\n", pmisdn->p_isdn_crypt_listen);
807 PERROR_RUNTIME("************************************************\n");
811 if (port == mISDNport->b_port[i])
816 PERROR_RUNTIME("************************************************\n");
817 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
818 PERROR_RUNTIME("** b_port not in list.\n");
819 PERROR_RUNTIME("************************************************\n");
826 mISDNport = mISDNport->next;