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);
363 /* load tones (if requested) */
364 if (fetch_tones() == 0) {
365 fprintf(stderr, "Unable to fetch tones into memory.\n");
371 if (options.gsm && gsm_init()) {
372 fprintf(stderr, "GSM initialization failed.\n");
377 fprintf(stderr, "GSM is enabled, but not compiled. Use --with-gsm while configure!\n");
382 /* read interfaces and open ports */
383 if (!read_interfaces()) {
384 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
385 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
389 interface_first = interface_newlist;
390 interface_newlist = NULL;
392 /* locking memory paging */
395 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
403 fprintf(stderr, "Warning: Not enough memory to lock paging.\n");
406 fprintf(stderr, "Warning: No permission to lock paging.\n");
409 fprintf(stderr, "Warning: 'Bad address' while locking paging.\n");
412 fprintf(stderr, "Warning: Unknown error %d while locking paging.\n", errno);
416 /* set real time scheduler & priority */
417 if (options.schedule > 1) {
418 memset(&schedp, 0, sizeof(schedp));
419 schedp.sched_priority = options.schedule;
420 ret = sched_setscheduler(0, SCHED_RR, &schedp);
422 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
427 /* signal handlers */
428 signal(SIGINT,sighandler);
429 signal(SIGHUP,sighandler);
430 signal(SIGTERM,sighandler);
431 signal(SIGPIPE,sighandler);
435 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
436 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
437 printf("%s\n", tracetext);
445 if (now_d-last_d > 1.0) {
446 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
448 /* all loops must be counted from the beginning since nodes might get freed during handler */
451 //#warning debugging usleep crash
452 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
454 /* handle mISDN messages from kernel */
455 debug_prefix = "ISDN";
458 //#warning debugging usleep crash
459 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
463 /* loop through all port ports and call their handler */
467 debug_prefix = port->p_name;
469 ret = port->handler();
472 if (ret < 0) /* port has been destroyed */
477 /* loop through all epoint and call their handler */
479 epoint = epoint_first;
481 debug_prefix = prefix_string;
482 SPRINT(prefix_string, "ep%ld", epoint->ep_serial);
484 ret = epoint->handler();
487 if (ret < 0) /* epoint has been destroyed */
489 epoint = epoint->next;
492 /* loop through all joins and call their handler */
496 debug_prefix = "join";
498 ret = join->handler();
501 if (ret < 0) /* join has been destroyed */
508 /* process any message */
510 debug_prefix = "message";
511 while ((message = message_get())) {
513 switch(message->flow) {
515 debug_prefix = "msg port->epoint";
516 epoint = find_epoint_id(message->id_to);
518 if (epoint->ep_app) {
519 epoint->ep_app->ea_message_port(message->id_from, message->type, &message->param);
521 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);
524 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);
529 debug_prefix = "msg epoint->join";
530 join = find_join_id(message->id_to);
532 join->message_epoint(message->id_from, message->type, &message->param);
534 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);
539 debug_prefix = "msg join->epoint";
540 epoint = find_epoint_id(message->id_to);
542 if (epoint->ep_app) {
543 epoint->ep_app->ea_message_join(message->id_from, message->type, &message->param);
545 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);
548 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);
553 debug_prefix = "msg epoint->port";
554 port = find_port_id(message->id_to);
556 port->message_epoint(message->id_from, message->type, &message->param);
559 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);
564 PERROR("Message flow %d unknown.\n", message->flow);
566 message_free(message);
568 debug_prefix = "message";
587 /* check for child to exit (eliminate zombies) */
588 if (waitpid(-1, NULL, WNOHANG) > 0) {
589 PDEBUG(DEBUG_EPOINT, "a child process (created by endpoint) has exitted.\n");
593 //#warning debugging usleep crash
594 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
596 /* do idle checking */
597 if (idlecheck != now) {
598 PDEBUG(DEBUG_IDLETIME, "Idle time : %d%%\n", idletime/10000);
603 /* did we do nothing? so we wait to give time to other processes */
605 // pthread_mutex_unlock(&mutex_lcr); // unlock LCR
606 debug_usleep(4000, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
607 // pthread_mutex_lock(&mutex_lcr); // lock LCR
611 SPRINT(tracetext, "%s terminated", NAME);
612 printf("%s\n", tracetext);
613 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
615 add_trace((char *)"signal", NULL, "%d", quit);
623 /* set scheduler & priority
625 if (options.schedule > 1) {
626 memset(&schedp, 0, sizeof(schedp));
627 schedp.sched_priority = options.schedule;
628 sched_setscheduler(0, SCHED_OTHER, &schedp);
631 if (created_signal) {
632 signal(SIGINT,SIG_DFL);
633 signal(SIGHUP,SIG_DFL);
634 signal(SIGTERM,SIG_DFL);
635 signal(SIGPIPE,SIG_DFL);
638 /* destroy objects */
639 debug_prefix = "free";
645 while(epoint_first) {
653 /* free interfaces */
655 free_interfaces(interface_first);
656 interface_first = NULL;
658 /* close isdn ports */
659 mISDNport_close_all();
664 while ((message = message_get())) {
666 message_free(message);
669 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
676 /* free admin socket */
681 flock(lockfd, LOCK_UN);
690 ruleset_free(ruleset_first);
691 ruleset_first = NULL;
695 if (pthread_mutex_destroy(&mutexe))
696 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
697 // if (created_mutext)
698 // if (pthread_mutex_destroy(&mutext))
699 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
701 if (pthread_mutex_destroy(&mutexd))
702 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
704 /* deinitialize mISDN */
706 mISDN_deinitialize();
714 /* display memory leak */
715 #define MEMCHECK(a, b) \
717 SPRINT(tracetext, a, NAME); \
718 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext); \
719 if (ret) add_trace("blocks", NULL, "%d", b); \
721 printf("\n******************************\n\007"); \
722 printf("\nERROR: %d %s\n", b, a); \
723 printf("\n******************************\n"); \
727 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
728 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
729 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
730 MEMCHECK("memory block(s) left (message.c)",mmemuse)
731 MEMCHECK("memory block(s) left (route.c)",rmemuse)
732 MEMCHECK("memory block(s) left (args)",amemuse)
733 MEMCHECK("class(es) left",classuse)
734 MEMCHECK("file descriptor(s) left",fduse)
735 MEMCHECK("file handler(s) left",fhuse)
737 /* unlock LCR process */
738 // pthread_mutex_unlock(&mutex_lcr);
746 /* special debug function to detect buffer overflow
748 int budetect_stop = 0;
749 void budetect(const char *file, int line, const char *function)
753 /* modify this function to detect race-bugs */
754 #warning DID YOU MODIFY THIS FUNCTION TO DETECT THE BUFFER OVERFLOW BUG?
756 class PmISDN *pmisdn;
757 struct mISDNport *mISDNport = mISDNport_first;
762 ii = mISDNport->b_num;
764 if (mISDNport->b_port[i]) {
767 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_ISDN) {
768 pmisdn = (class PmISDN *)port;
769 if (pmisdn->p_isdn_crypt_listen) {
770 PERROR_RUNTIME("************************************************\n");
771 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
772 PERROR_RUNTIME("** p_isdn_crypt_listen = %d\n", pmisdn->p_isdn_crypt_listen);
773 PERROR_RUNTIME("************************************************\n");
777 if (port == mISDNport->b_port[i])
781 PERROR_RUNTIME("************************************************\n");
782 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
783 PERROR_RUNTIME("** b_port not in list.\n");
784 PERROR_RUNTIME("************************************************\n");
791 mISDNport = mISDNport->next;