1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
15 #include <sys/types.h>
22 #include <sys/ioctl.h>
26 #include <sys/resource.h>
34 struct timeval now_tv;
35 struct timezone now_tz;
38 gettimeofday(&now_tv, &now_tz); \
39 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
40 now = now_tv.tv_sec; \
41 now_tm = localtime(&now); \
48 struct lcr_fdset lcr_fdset[FD_SETSIZE];
51 pthread_mutex_t mutexd; // debug output mutex
52 //pthread_mutex_t mutext; // trace output mutex
53 pthread_mutex_t mutexe; // error output mutex
66 char *debug_prefix = 0;
69 int debug_newline = 1;
72 void debug_usleep(int msec, char *file, int line, int hour, int min, int sec)
77 void debug(const char *function, int line, char *prefix, char *buffer)
79 /* if we have a new debug count, we add a mark */
80 if (last_debug != debug_count)
82 last_debug = debug_count;
84 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);
85 if (options.deb&DEBUG_LOG && global_debug)
86 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);
92 printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
94 printf("(in %s() line %d): %s", function, line, buffer);
99 if (options.deb&DEBUG_LOG && global_debug)
104 dprint(DBGM_MAN, 0, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
106 dprint(DBGM_MAN, 0, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
112 if (buffer[strlen(buffer)-1] == '\n')
117 void _printdebug(const char *function, int line, unsigned long mask, const char *fmt, ...)
122 if (!(options.deb & mask))
124 pthread_mutex_lock(&mutexd);
127 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
128 buffer[sizeof(buffer)-1]=0;
131 debug(function, line, debug_prefix, buffer);
133 pthread_mutex_unlock(&mutexd);
136 void _printerror(const char *function, int line, const char *fmt, ...)
141 pthread_mutex_lock(&mutexe);
144 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
145 buffer[sizeof(buffer)-1]=0;
149 debug(function, line, "ERROR", buffer);
150 else /* only if we do not debug */
153 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
155 fprintf(stderr, "ERROR %s", buffer);
158 pthread_mutex_unlock(&mutexe);
162 void sighandler(int sigset)
164 struct sched_param schedp;
166 if (sigset == SIGHUP)
168 if (sigset == SIGPIPE)
173 /* set scheduler & priority */
174 if (options.schedule > 1)
176 memset(&schedp, 0, sizeof(schedp));
177 schedp.sched_priority = 0;
178 sched_setscheduler(0, SCHED_OTHER, &schedp);
180 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
181 PERROR("Signal received: %d\n", sigset);
189 int main(int argc, char *argv[])
192 int lockfd = -1; /* file lock */
193 struct message *message;
195 class Endpoint *epoint;
199 char prefix_string[64];
200 struct sched_param schedp;
201 char *debug_prefix = "alloc";
202 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
203 created_lock = 0, created_signal = 0, created_debug = 0;
204 int idletime = 0, idlecheck = 0;
209 memset(lcr_fdset, 0, sizeof(lcr_fdset));
216 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
223 printf("Usage: lcr (query | start | fork | rules | route)\n");
224 printf("query = Show available isdn ports.\n");
225 printf("start = Run lcr normally, abort with CTRL+C.\n");
226 printf("fork = Do daemon fork and run as background process.\n");
227 printf("interface = Get help of available interface syntax.\n");
228 printf("rules = Get help of available routing rule syntax.\n");
229 printf("rules [action] = Get individual help for given action.\n");
230 // printf("route = Show current routing as it is parsed.\n");
239 /* check for root (real or effective) */
240 if (getuid() && geteuid())
242 fprintf(stderr, "Please run %s as super-user.\n", NAME);
247 if (pthread_mutex_init(&mutexd, NULL))
249 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
253 // if (pthread_mutex_init(&mutext, NULL))
255 // fprintf(stderr, "cannot create 'trace' mutex\n");
258 // created_mutext = 1;
259 if (pthread_mutex_init(&mutexe, NULL))
261 fprintf(stderr, "cannot create 'PERROR' mutex\n");
267 if (!(strcasecmp(argv[1],"interface")))
275 if (!(strcasecmp(argv[1],"rules")))
285 /* query available isdn ports */
286 if (!(strcasecmp(argv[1],"query")))
294 if (read_options() == 0)
298 if (mISDN_initialize() < 0)
302 /* read ruleset(s) */
303 if (!(ruleset_first = ruleset_parse()))
306 /* set pointer to main ruleset */
307 ruleset_main = getrulesetbyname("main");
310 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
311 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
316 /* query available isdn ports */
317 if (!(strcasecmp(argv[1],"route")))
319 ruleset_debug(ruleset_first);
325 /* do fork in special cases */
326 if (!(strcasecmp(argv[1],"fork")))
335 fprintf(stderr, "Cannot fork!\n");
350 fprintf(stderr, "Cannot fork!\n");
355 printf("LCR: Starting daemon.\n");
361 if (!!strcasecmp(argv[1],"start"))
366 /* create lock and lock! */
367 if ((lockfd = open("/var/run/lcr.lock", O_CREAT, 0)) < 0)
369 fprintf(stderr, "Cannot create lock file: /var/run/lcr.lock\n");
372 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0)
374 if (errno == EWOULDBLOCK)
375 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
376 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
381 /* initialize admin socket */
384 fprintf(stderr, "Unable to initialize admin socket.\n");
388 /* generate alaw / ulaw tables */
389 generate_tables(options.law);
391 /* load tones (if requested) */
392 if (fetch_tones() == 0)
394 fprintf(stderr, "Unable to fetch tones into memory.\n");
398 /* read interfaces and open ports */
399 if (!read_interfaces())
401 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
402 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
406 interface_first = interface_newlist;
407 interface_newlist = NULL;
409 /* locking memory paging */
413 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
423 fprintf(stderr, "Not enough memory to lock paging, exitting...\n");
426 fprintf(stderr, "No permission to lock paging, exitting...\n");
429 fprintf(stderr, "'Bad address' while locking paging, exitting...\n");
432 fprintf(stderr, "Unknown error %d while locking paging, exitting...\n", errno);
437 /* set real time scheduler & priority */
438 if (options.schedule > 1)
440 memset(&schedp, 0, sizeof(schedp));
441 schedp.sched_priority = options.schedule;
442 ret = sched_setscheduler(0, SCHED_RR, &schedp);
445 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
450 /* signal handlers */
451 signal(SIGINT,sighandler);
452 signal(SIGHUP,sighandler);
453 signal(SIGTERM,sighandler);
454 signal(SIGPIPE,sighandler);
458 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
459 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
460 printf("%s\n", tracetext);
468 if (now_d-last_d > 1.0)
470 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
472 /* all loops must be counted from the beginning since nodes might get freed during handler */
475 //#warning debugging usleep crash
476 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
478 /* handle mISDN messages from kernel */
479 debug_prefix = "ISDN";
482 //#warning debugging usleep crash
483 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
487 /* loop through all port ports and call their handler */
492 debug_prefix = port->p_name;
494 ret = port->handler();
497 if (ret < 0) /* port has been destroyed */
502 /* loop through all epoint and call their handler */
504 epoint = epoint_first;
507 debug_prefix = prefix_string;
508 SPRINT(prefix_string, "ep%ld", epoint->ep_serial);
510 ret = epoint->handler();
513 if (ret < 0) /* epoint has been destroyed */
515 epoint = epoint->next;
518 /* loop through all joins and call their handler */
523 debug_prefix = "join";
525 ret = join->handler();
528 if (ret < 0) /* join has been destroyed */
535 /* process any message */
537 debug_prefix = "message";
538 while ((message = message_get()))
541 switch(message->flow)
544 debug_prefix = "msg port->epoint";
545 epoint = find_epoint_id(message->id_to);
550 epoint->ep_app->ea_message_port(message->id_from, message->type, &message->param);
553 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);
557 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);
562 debug_prefix = "msg epoint->join";
563 join = find_join_id(message->id_to);
566 join->message_epoint(message->id_from, message->type, &message->param);
569 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);
574 debug_prefix = "msg join->epoint";
575 epoint = find_epoint_id(message->id_to);
580 epoint->ep_app->ea_message_join(message->id_from, message->type, &message->param);
583 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);
587 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);
592 debug_prefix = "msg epoint->port";
593 port = find_port_id(message->id_to);
596 port->message_epoint(message->id_from, message->type, &message->param);
600 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);
605 PERROR("Message flow %d unknown.\n", message->flow);
607 message_free(message);
609 debug_prefix = "message";
619 /* check for child to exit (eliminate zombies) */
620 if (waitpid(-1, NULL, WNOHANG) > 0)
622 PDEBUG(DEBUG_EPOINT, "a child process (created by endpoint) has exitted.\n");
626 //#warning debugging usleep crash
627 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
629 /* do idle checking */
630 if (idlecheck != now)
632 PDEBUG(DEBUG_IDLETIME, "Idle time : %d%%\n", idletime/10000);
637 /* did we do nothing? so we wait to give time to other processes */
640 debug_usleep(4000, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
644 SPRINT(tracetext, "%s terminated", NAME);
645 printf("%s\n", tracetext);
646 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
648 add_trace("error", NULL, "%d", ret);
656 /* set scheduler & priority
658 if (options.schedule > 1)
660 memset(&schedp, 0, sizeof(schedp));
661 schedp.sched_priority = 0;
662 sched_setscheduler(0, SCHED_OTHER, &schedp);
667 signal(SIGINT,SIG_DFL);
668 signal(SIGHUP,SIG_DFL);
669 signal(SIGTERM,SIG_DFL);
670 signal(SIGPIPE,SIG_DFL);
673 /* destroy objects */
674 debug_prefix = "free";
690 /* free interfaces */
692 free_interfaces(interface_first);
693 interface_first = NULL;
695 /* close isdn ports */
696 mISDNport_close_all();
701 while ((message = message_get()))
704 message_free(message);
708 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
715 /* free admin socket */
720 flock(lockfd, LOCK_UN);
726 ruleset_free(ruleset_first);
727 ruleset_first = NULL;
731 if (pthread_mutex_destroy(&mutexe))
732 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
733 // if (created_mutext)
734 // if (pthread_mutex_destroy(&mutext))
735 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
737 if (pthread_mutex_destroy(&mutexd))
738 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
740 /* deinitialize mISDN */
741 mISDN_deinitialize();
744 /* display memory leak */
745 #define MEMCHECK(a, b) \
748 SPRINT(tracetext, a, NAME); \
749 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext); \
750 if (ret) add_trace("blocks", NULL, "%d", b); \
752 printf("\n******************************\n\007"); \
753 printf("\nERROR: %d %s\n", b, a); \
754 printf("\n******************************\n"); \
758 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
759 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
760 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
761 MEMCHECK("memory block(s) left (message.c)",mmemuse)
762 MEMCHECK("memory block(s) left (route.c)",rmemuse)
763 MEMCHECK("memory block(s) left (args)",amemuse)
764 MEMCHECK("class(es) left",classuse)
765 MEMCHECK("file descriptor(s) left",fduse)
766 MEMCHECK("file handler(s) left",fhuse)
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;