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); \
43 //#define DEBUG_DURATION
48 pthread_mutex_t mutexd; // debug output mutex
49 //pthread_mutex_t mutext; // trace output mutex
50 pthread_mutex_t mutexe; // error output mutex
63 char *debug_prefix = 0;
66 int debug_newline = 1;
69 void debug_usleep(int msec, char *file, int line, int hour, int min, int sec)
74 void debug(const char *function, int line, char *prefix, char *buffer)
76 /* if we have a new debug count, we add a mark */
77 if (last_debug != debug_count)
79 last_debug = debug_count;
81 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);
82 if (options.deb&DEBUG_LOG && global_debug)
83 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);
89 printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
91 printf("(in %s() line %d): %s", function, line, buffer);
96 if (options.deb&DEBUG_LOG && global_debug)
101 dprint(DBGM_MAN, 0, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
103 dprint(DBGM_MAN, 0, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
109 if (buffer[strlen(buffer)-1] == '\n')
114 void _printdebug(const char *function, int line, unsigned long mask, const char *fmt, ...)
119 if (!(options.deb & mask))
121 pthread_mutex_lock(&mutexd);
124 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
125 buffer[sizeof(buffer)-1]=0;
128 debug(function, line, debug_prefix, buffer);
130 pthread_mutex_unlock(&mutexd);
133 void _printerror(const char *function, int line, const char *fmt, ...)
138 pthread_mutex_lock(&mutexe);
141 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
142 buffer[sizeof(buffer)-1]=0;
146 debug(function, line, "ERROR", buffer);
147 else /* only if we do not debug */
150 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
152 fprintf(stderr, "ERROR %s", buffer);
155 pthread_mutex_unlock(&mutexe);
159 void sighandler(int sigset)
161 struct sched_param schedp;
163 if (sigset == SIGHUP)
165 if (sigset == SIGPIPE)
170 /* set scheduler & priority */
171 if (options.schedule > 1)
173 memset(&schedp, 0, sizeof(schedp));
174 schedp.sched_priority = 0;
175 sched_setscheduler(0, SCHED_OTHER, &schedp);
177 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
178 PERROR("Signal received: %d\n", sigset);
186 int main(int argc, char *argv[])
189 int lockfd = -1; /* file lock */
190 struct message *message;
192 class Endpoint *epoint;
196 char prefix_string[64];
197 struct sched_param schedp;
198 char *debug_prefix = "alloc";
199 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
200 created_lock = 0, created_signal = 0, created_debug = 0;
201 #ifdef DEBUG_DURATION
202 time_t durationupdate;
203 double idle_duration, isdn_duration, port_duration, epoint_duration, join_duration, message_duration, admin_duration;
206 int idletime = 0, idlecheck = 0;
213 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
220 printf("Usage: lcr (query | start | fork | rules | route)\n");
221 printf("query = Show available isdn ports.\n");
222 printf("start = Run lcr normally, abort with CTRL+C.\n");
223 printf("fork = Do daemon fork and run as background process.\n");
224 printf("interface = Get help of available interface syntax.\n");
225 printf("rules = Get help of available routing rule syntax.\n");
226 printf("rules [action] = Get individual help for given action.\n");
227 // printf("route = Show current routing as it is parsed.\n");
236 /* check for root (real or effective) */
237 if (getuid() && geteuid())
239 fprintf(stderr, "Please run %s as super-user.\n", NAME);
244 if (pthread_mutex_init(&mutexd, NULL))
246 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
250 // if (pthread_mutex_init(&mutext, NULL))
252 // fprintf(stderr, "cannot create 'trace' mutex\n");
255 // created_mutext = 1;
256 if (pthread_mutex_init(&mutexe, NULL))
258 fprintf(stderr, "cannot create 'PERROR' mutex\n");
264 if (!(strcasecmp(argv[1],"interface")))
272 if (!(strcasecmp(argv[1],"rules")))
282 /* query available isdn ports */
283 if (!(strcasecmp(argv[1],"query")))
291 if (read_options() == 0)
295 if (mISDN_initialize() < 0)
299 /* read ruleset(s) */
300 if (!(ruleset_first = ruleset_parse()))
303 /* set pointer to main ruleset */
304 ruleset_main = getrulesetbyname("main");
307 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
308 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
313 /* query available isdn ports */
314 if (!(strcasecmp(argv[1],"route")))
316 ruleset_debug(ruleset_first);
322 /* do fork in special cases */
323 if (!(strcasecmp(argv[1],"fork")))
332 fprintf(stderr, "Cannot fork!\n");
347 fprintf(stderr, "Cannot fork!\n");
352 printf("LCR: Starting daemon.\n");
358 if (!!strcasecmp(argv[1],"start"))
363 /* create lock and lock! */
364 if ((lockfd = open("/var/run/lcr.lock", O_CREAT, 0)) < 0)
366 fprintf(stderr, "Cannot create lock file: /var/run/lcr.lock\n");
369 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0)
371 if (errno == EWOULDBLOCK)
372 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
373 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
378 /* initialize admin socket */
381 fprintf(stderr, "Unable to initialize admin socket.\n");
385 /* generate alaw / ulaw tables */
386 generate_tables(options.law);
388 /* load tones (if requested) */
389 if (fetch_tones() == 0)
391 fprintf(stderr, "Unable to fetch tones into memory.\n");
395 /* read interfaces and open ports */
396 if (!read_interfaces())
398 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
399 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
403 interface_first = interface_newlist;
404 interface_newlist = NULL;
406 /* locking memory paging */
410 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
420 fprintf(stderr, "Not enough memory to lock paging, exitting...\n");
423 fprintf(stderr, "No permission to lock paging, exitting...\n");
426 fprintf(stderr, "'Bad address' while locking paging, exitting...\n");
429 fprintf(stderr, "Unknown error %d while locking paging, exitting...\n", errno);
434 /* set real time scheduler & priority */
435 if (options.schedule > 1)
437 memset(&schedp, 0, sizeof(schedp));
438 schedp.sched_priority = options.schedule;
439 ret = sched_setscheduler(0, SCHED_RR, &schedp);
442 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
447 /* signal handlers */
448 signal(SIGINT,sighandler);
449 signal(SIGHUP,sighandler);
450 signal(SIGTERM,sighandler);
451 signal(SIGPIPE,sighandler);
455 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
456 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
457 printf("%s\n", tracetext);
460 #ifdef DEBUG_DURATION
462 durationupdate = now;
463 idle_duration = isdn_duration = port_duration = epoint_duration = call_duration = message_duration = admin_duration = 0;
468 /* all loops must be counted from the beginning since nodes might get freed during handler */
471 #warning debugging usleep crash
472 debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
474 /* handle mISDN messages from kernel */
475 debug_prefix = "ISDN";
478 #warning debugging usleep crash
479 debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
481 #ifdef DEBUG_DURATION
483 isdn_duration += (now_d - start_d);
488 /* loop through all port ports and call their handler */
493 debug_prefix = port->p_name;
495 ret = port->handler();
498 if (ret < 0) /* port has been destroyed */
502 #ifdef DEBUG_DURATION
504 port_duration += (now_d - start_d);
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;
523 #ifdef DEBUG_DURATION
525 epoint_duration += (now_d - start_d);
529 /* loop through all joins and call their handler */
534 debug_prefix = "join";
536 ret = join->handler();
539 if (ret < 0) /* join has been destroyed */
543 #ifdef DEBUG_DURATION
545 join_duration += (now_d - start_d);
551 /* process any message */
553 debug_prefix = "message";
554 while ((message = message_get()))
557 switch(message->flow)
560 debug_prefix = "msg port->epoint";
561 epoint = find_epoint_id(message->id_to);
566 epoint->ep_app->ea_message_port(message->id_from, message->type, &message->param);
569 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);
573 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);
578 debug_prefix = "msg epoint->join";
579 join = find_join_id(message->id_to);
582 join->message_epoint(message->id_from, message->type, &message->param);
585 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);
590 debug_prefix = "msg join->epoint";
591 epoint = find_epoint_id(message->id_to);
596 epoint->ep_app->ea_message_join(message->id_from, message->type, &message->param);
599 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);
603 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);
608 debug_prefix = "msg epoint->port";
609 port = find_port_id(message->id_to);
612 port->message_epoint(message->id_from, message->type, &message->param);
616 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);
621 PERROR("Message flow %d unknown.\n", message->flow);
623 message_free(message);
625 debug_prefix = "message";
627 #ifdef DEBUG_DURATION
629 message_duration += (now_d - start_d);
637 #ifdef DEBUG_DURATION
639 admin_duration += (now_d - start_d);
645 /* check for child to exit (eliminate zombies) */
646 if (waitpid(-1, NULL, WNOHANG) > 0)
648 PDEBUG(DEBUG_EPOINT, "a child process (created by endpoint) has exitted.\n");
652 #warning debugging usleep crash
653 debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
655 /* do idle checking */
656 if (idlecheck != now)
658 PDEBUG(DEBUG_IDLETIME, "Idle time : %d%%\n", idletime/10000);
662 #ifdef DEBUG_DURATION
664 idle_duration += (now_d - start_d);
666 if (durationupdate != now)
668 durationupdate = now;
669 printf("Idle:%3d ISDN:%3d Port:%3d Epoint:%3d Join:%3d Message:%3d Admin:%3d\n",
670 (int)(idle_duration*100),
671 (int)(isdn_duration*100),
672 (int)(port_duration*100),
673 (int)(epoint_duration*100),
674 (int)(join_duration*100),
675 (int)(message_duration*100),
676 (int)(admin_duration*100));
677 idle_duration = isdn_duration = port_duration = epoint_duration = join_duration = message_duration = admin_duration = 0;
683 /* did we do nothing? so we wait to give time to other processes */
686 debug_usleep(4000, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
690 SPRINT(tracetext, "%s terminated", NAME);
691 printf("%s\n", tracetext);
692 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
694 add_trace("error", NULL, "%d", ret);
702 /* set scheduler & priority
704 if (options.schedule > 1)
706 memset(&schedp, 0, sizeof(schedp));
707 schedp.sched_priority = 0;
708 sched_setscheduler(0, SCHED_OTHER, &schedp);
713 signal(SIGINT,SIG_DFL);
714 signal(SIGHUP,SIG_DFL);
715 signal(SIGTERM,SIG_DFL);
716 signal(SIGPIPE,SIG_DFL);
719 /* destroy objects */
720 debug_prefix = "free";
736 /* free interfaces */
738 free_interfaces(interface_first);
739 interface_first = NULL;
741 /* close isdn ports */
742 mISDNport_close_all();
747 while ((message = message_get()))
750 message_free(message);
754 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
761 /* free admin socket */
766 flock(lockfd, LOCK_UN);
772 ruleset_free(ruleset_first);
773 ruleset_first = NULL;
777 if (pthread_mutex_destroy(&mutexe))
778 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
779 // if (created_mutext)
780 // if (pthread_mutex_destroy(&mutext))
781 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
783 if (pthread_mutex_destroy(&mutexd))
784 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
786 /* deinitialize mISDN */
787 mISDN_deinitialize();
790 /* display memory leak */
791 #define MEMCHECK(a, b) \
794 SPRINT(tracetext, a, NAME); \
795 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext); \
796 if (ret) add_trace("blocks", NULL, "%d", b); \
798 printf("\n******************************\n\007"); \
799 printf("\nERROR: %d %s\n", b, a); \
800 printf("\n******************************\n"); \
804 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
805 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
806 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
807 MEMCHECK("memory block(s) left (message.c)",mmemuse)
808 MEMCHECK("memory block(s) left (route.c)",rmemuse)
809 MEMCHECK("memory block(s) left (args)",amemuse)
810 MEMCHECK("class(es) left",classuse)
811 MEMCHECK("file descriptor(s) left",fduse)
812 MEMCHECK("file handler(s) left",fhuse)
820 /* special debug function to detect buffer overflow
822 int budetect_stop = 0;
823 void budetect(const char *file, int line, char *function)
827 /* modify this function to detect race-bugs */
828 #warning DID YOU MODIFY THIS FUNCTION TO DETECT THE BUFFER OVERFLOW BUG?
830 class PmISDN *pmisdn;
831 struct mISDNport *mISDNport = mISDNport_first;
837 ii = mISDNport->b_num;
840 if (mISDNport->b_port[i])
845 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_ISDN)
847 pmisdn = (class PmISDN *)port;
848 if (pmisdn->p_isdn_crypt_listen)
850 PERROR_RUNTIME("************************************************\n");
851 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
852 PERROR_RUNTIME("** p_isdn_crypt_listen = %d\n", pmisdn->p_isdn_crypt_listen);
853 PERROR_RUNTIME("************************************************\n");
857 if (port == mISDNport->b_port[i])
862 PERROR_RUNTIME("************************************************\n");
863 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
864 PERROR_RUNTIME("** b_port not in list.\n");
865 PERROR_RUNTIME("************************************************\n");
872 mISDNport = mISDNport->next;