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); \
30 FILE *debug_fp = NULL;
37 struct lcr_fdset lcr_fdset[FD_SETSIZE];
40 pthread_mutex_t mutexd; // debug output mutex
41 //pthread_mutex_t mutext; // trace output mutex
42 pthread_mutex_t mutexe; // error output mutex
55 char *debug_prefix = 0;
58 int debug_newline = 1;
61 void debug_usleep(int msec, char *file, int line, int hour, int min, int sec)
66 void debug(const char *function, int line, char *prefix, char *buffer)
68 /* if we have a new debug count, we add a mark */
69 if (last_debug != debug_count)
71 last_debug = debug_count;
73 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);
76 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 if (options.deb&DEBUG_LOG && global_debug)
79 dprint(DBGM_MAN, 0, "--------------------- %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);
86 printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
88 printf("(in %s() line %d): %s", function, line, buffer);
96 if (options.deb&DEBUG_LOG && global_debug)
103 fprintf(debug_fp, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
105 dprint(DBGM_MAN, 0, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
109 fprintf(debug_fp, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
111 dprint(DBGM_MAN, 0, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
118 if (buffer[strlen(buffer)-1] == '\n')
123 void _printdebug(const char *function, int line, unsigned long mask, const char *fmt, ...)
128 if (!(options.deb & mask))
130 pthread_mutex_lock(&mutexd);
133 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
134 buffer[sizeof(buffer)-1]=0;
137 debug(function, line, debug_prefix, buffer);
139 pthread_mutex_unlock(&mutexd);
142 void _printerror(const char *function, int line, const char *fmt, ...)
147 pthread_mutex_lock(&mutexe);
150 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
151 buffer[sizeof(buffer)-1]=0;
155 debug(function, line, "ERROR", buffer);
156 else /* only if we do not debug */
159 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
161 fprintf(stderr, "ERROR %s", buffer);
164 pthread_mutex_unlock(&mutexe);
168 void sighandler(int sigset)
170 struct sched_param schedp;
172 if (sigset == SIGHUP)
174 if (sigset == SIGPIPE)
179 /* set scheduler & priority */
180 if (options.schedule > 1)
182 memset(&schedp, 0, sizeof(schedp));
183 schedp.sched_priority = 0;
184 sched_setscheduler(0, SCHED_OTHER, &schedp);
186 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
187 PERROR("Signal received: %d\n", sigset);
195 int main(int argc, char *argv[])
198 int lockfd = -1; /* file lock */
199 struct message *message;
201 class Endpoint *epoint;
205 char prefix_string[64];
206 struct sched_param schedp;
207 char *debug_prefix = "alloc";
208 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
209 created_lock = 0, created_signal = 0, created_debug = 0;
210 int idletime = 0, idlecheck = 0;
215 memset(lcr_fdset, 0, sizeof(lcr_fdset));
222 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
229 printf("Usage: lcr (query | start | fork | rules | route)\n");
230 printf("query = Show available isdn ports.\n");
231 printf("start = Run lcr normally, abort with CTRL+C.\n");
232 printf("fork = Do daemon fork and run as background process.\n");
233 printf("interface = Get help of available interface syntax.\n");
234 printf("rules = Get help of available routing rule syntax.\n");
235 printf("rules [action] = Get individual help for given action.\n");
236 // printf("route = Show current routing as it is parsed.\n");
245 /* check for root (real or effective) */
246 if (getuid() && geteuid())
248 fprintf(stderr, "Please run %s as super-user.\n", NAME);
253 if (pthread_mutex_init(&mutexd, NULL))
255 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
259 // if (pthread_mutex_init(&mutext, NULL))
261 // fprintf(stderr, "cannot create 'trace' mutex\n");
264 // created_mutext = 1;
265 if (pthread_mutex_init(&mutexe, NULL))
267 fprintf(stderr, "cannot create 'PERROR' mutex\n");
273 if (!(strcasecmp(argv[1],"interface")))
281 if (!(strcasecmp(argv[1],"rules")))
291 /* query available isdn ports */
292 if (!(strcasecmp(argv[1],"query")))
300 if (read_options() == 0)
304 if (mISDN_initialize() < 0)
308 /* read ruleset(s) */
309 if (!(ruleset_first = ruleset_parse()))
312 /* set pointer to main ruleset */
313 ruleset_main = getrulesetbyname("main");
316 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
317 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
322 /* query available isdn ports */
323 if (!(strcasecmp(argv[1],"route")))
325 ruleset_debug(ruleset_first);
331 /* do fork in special cases */
332 if (!(strcasecmp(argv[1],"fork")))
341 fprintf(stderr, "Cannot fork!\n");
356 fprintf(stderr, "Cannot fork!\n");
361 printf("LCR: Starting daemon.\n");
367 if (!!strcasecmp(argv[1],"start"))
372 /* create lock and lock! */
373 if ((lockfd = open("/var/run/lcr.lock", O_CREAT, 0)) < 0)
375 fprintf(stderr, "Cannot create lock file: /var/run/lcr.lock\n");
378 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0)
380 if (errno == EWOULDBLOCK)
381 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
382 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
387 /* initialize admin socket */
390 fprintf(stderr, "Unable to initialize admin socket.\n");
394 /* generate alaw / ulaw tables */
395 generate_tables(options.law);
397 /* load tones (if requested) */
398 if (fetch_tones() == 0)
400 fprintf(stderr, "Unable to fetch tones into memory.\n");
404 /* read interfaces and open ports */
405 if (!read_interfaces())
407 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
408 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
412 interface_first = interface_newlist;
413 interface_newlist = NULL;
415 /* locking memory paging */
419 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
429 fprintf(stderr, "Not enough memory to lock paging, exitting...\n");
432 fprintf(stderr, "No permission to lock paging, exitting...\n");
435 fprintf(stderr, "'Bad address' while locking paging, exitting...\n");
438 fprintf(stderr, "Unknown error %d while locking paging, exitting...\n", errno);
443 /* set real time scheduler & priority */
444 if (options.schedule > 1)
446 memset(&schedp, 0, sizeof(schedp));
447 schedp.sched_priority = options.schedule;
448 ret = sched_setscheduler(0, SCHED_RR, &schedp);
451 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
456 /* signal handlers */
457 signal(SIGINT,sighandler);
458 signal(SIGHUP,sighandler);
459 signal(SIGTERM,sighandler);
460 signal(SIGPIPE,sighandler);
464 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
465 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
466 printf("%s\n", tracetext);
474 if (now_d-last_d > 1.0)
476 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
478 /* all loops must be counted from the beginning since nodes might get freed during handler */
481 //#warning debugging usleep crash
482 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
484 /* handle mISDN messages from kernel */
485 debug_prefix = "ISDN";
488 //#warning debugging usleep crash
489 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
493 /* loop through all port ports and call their handler */
498 debug_prefix = port->p_name;
500 ret = port->handler();
503 if (ret < 0) /* port has been destroyed */
508 /* loop through all epoint and call their handler */
510 epoint = epoint_first;
513 debug_prefix = prefix_string;
514 SPRINT(prefix_string, "ep%ld", epoint->ep_serial);
516 ret = epoint->handler();
519 if (ret < 0) /* epoint has been destroyed */
521 epoint = epoint->next;
524 /* loop through all joins and call their handler */
529 debug_prefix = "join";
531 ret = join->handler();
534 if (ret < 0) /* join has been destroyed */
541 /* process any message */
543 debug_prefix = "message";
544 while ((message = message_get()))
547 switch(message->flow)
550 debug_prefix = "msg port->epoint";
551 epoint = find_epoint_id(message->id_to);
556 epoint->ep_app->ea_message_port(message->id_from, message->type, &message->param);
559 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);
563 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);
568 debug_prefix = "msg epoint->join";
569 join = find_join_id(message->id_to);
572 join->message_epoint(message->id_from, message->type, &message->param);
575 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);
580 debug_prefix = "msg join->epoint";
581 epoint = find_epoint_id(message->id_to);
586 epoint->ep_app->ea_message_join(message->id_from, message->type, &message->param);
589 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);
593 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);
598 debug_prefix = "msg epoint->port";
599 port = find_port_id(message->id_to);
602 port->message_epoint(message->id_from, message->type, &message->param);
606 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);
611 PERROR("Message flow %d unknown.\n", message->flow);
613 message_free(message);
615 debug_prefix = "message";
625 /* check for child to exit (eliminate zombies) */
626 if (waitpid(-1, NULL, WNOHANG) > 0)
628 PDEBUG(DEBUG_EPOINT, "a child process (created by endpoint) has exitted.\n");
632 //#warning debugging usleep crash
633 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
635 /* do idle checking */
636 if (idlecheck != now)
638 PDEBUG(DEBUG_IDLETIME, "Idle time : %d%%\n", idletime/10000);
643 /* did we do nothing? so we wait to give time to other processes */
646 debug_usleep(4000, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
650 SPRINT(tracetext, "%s terminated", NAME);
651 printf("%s\n", tracetext);
652 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
654 add_trace("error", NULL, "%d", ret);
662 /* set scheduler & priority
664 if (options.schedule > 1)
666 memset(&schedp, 0, sizeof(schedp));
667 schedp.sched_priority = options.schedule;
668 sched_setscheduler(0, SCHED_OTHER, &schedp);
673 signal(SIGINT,SIG_DFL);
674 signal(SIGHUP,SIG_DFL);
675 signal(SIGTERM,SIG_DFL);
676 signal(SIGPIPE,SIG_DFL);
679 /* destroy objects */
680 debug_prefix = "free";
696 /* free interfaces */
698 free_interfaces(interface_first);
699 interface_first = NULL;
701 /* close isdn ports */
702 mISDNport_close_all();
707 while ((message = message_get()))
710 message_free(message);
714 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
721 /* free admin socket */
726 flock(lockfd, LOCK_UN);
732 ruleset_free(ruleset_first);
733 ruleset_first = NULL;
737 if (pthread_mutex_destroy(&mutexe))
738 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
739 // if (created_mutext)
740 // if (pthread_mutex_destroy(&mutext))
741 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
743 if (pthread_mutex_destroy(&mutexd))
744 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
746 /* deinitialize mISDN */
747 mISDN_deinitialize();
749 /* display memory leak */
750 #define MEMCHECK(a, b) \
753 SPRINT(tracetext, a, NAME); \
754 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext); \
755 if (ret) add_trace("blocks", NULL, "%d", b); \
757 printf("\n******************************\n\007"); \
758 printf("\nERROR: %d %s\n", b, a); \
759 printf("\n******************************\n"); \
763 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
764 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
765 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
766 MEMCHECK("memory block(s) left (message.c)",mmemuse)
767 MEMCHECK("memory block(s) left (route.c)",rmemuse)
768 MEMCHECK("memory block(s) left (args)",amemuse)
769 MEMCHECK("class(es) left",classuse)
770 MEMCHECK("file descriptor(s) left",fduse)
771 MEMCHECK("file handler(s) left",fhuse)
779 /* special debug function to detect buffer overflow
781 int budetect_stop = 0;
782 void budetect(const char *file, int line, char *function)
786 /* modify this function to detect race-bugs */
787 #warning DID YOU MODIFY THIS FUNCTION TO DETECT THE BUFFER OVERFLOW BUG?
789 class PmISDN *pmisdn;
790 struct mISDNport *mISDNport = mISDNport_first;
796 ii = mISDNport->b_num;
799 if (mISDNport->b_port[i])
804 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_ISDN)
806 pmisdn = (class PmISDN *)port;
807 if (pmisdn->p_isdn_crypt_listen)
809 PERROR_RUNTIME("************************************************\n");
810 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
811 PERROR_RUNTIME("** p_isdn_crypt_listen = %d\n", pmisdn->p_isdn_crypt_listen);
812 PERROR_RUNTIME("************************************************\n");
816 if (port == mISDNport->b_port[i])
821 PERROR_RUNTIME("************************************************\n");
822 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
823 PERROR_RUNTIME("** b_port not in list.\n");
824 PERROR_RUNTIME("************************************************\n");
831 mISDNport = mISDNport->next;