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(const char *function, int line, char *prefix, char *buffer)
71 /* if we have a new debug count, we add a mark */
72 if (last_debug != debug_count)
74 last_debug = debug_count;
76 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);
77 if (options.deb&DEBUG_LOG && global_debug)
78 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);
84 printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
86 printf("(in %s() line %d): %s", function, line, buffer);
91 if (options.deb&DEBUG_LOG && global_debug)
96 dprint(DBGM_MAN, 0, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
98 dprint(DBGM_MAN, 0, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
104 if (buffer[strlen(buffer)-1] == '\n')
109 void _printdebug(const char *function, int line, unsigned long mask, const char *fmt, ...)
114 if (!(options.deb & mask))
116 pthread_mutex_lock(&mutexd);
119 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
120 buffer[sizeof(buffer)-1]=0;
123 debug(function, line, debug_prefix, buffer);
125 pthread_mutex_unlock(&mutexd);
128 void _printerror(const char *function, int line, const char *fmt, ...)
133 pthread_mutex_lock(&mutexe);
136 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
137 buffer[sizeof(buffer)-1]=0;
141 debug(function, line, "ERROR", buffer);
142 else /* only if we do not debug */
145 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
147 fprintf(stderr, "ERROR %s", buffer);
150 pthread_mutex_unlock(&mutexe);
154 void sighandler(int sigset)
156 struct sched_param schedp;
158 if (sigset == SIGHUP)
160 if (sigset == SIGPIPE)
165 /* set scheduler & priority */
166 if (options.schedule > 1)
168 memset(&schedp, 0, sizeof(schedp));
169 schedp.sched_priority = 0;
170 sched_setscheduler(0, SCHED_OTHER, &schedp);
172 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
173 PERROR("Signal received: %d\n", sigset);
181 int main(int argc, char *argv[])
184 int lockfd = -1; /* file lock */
185 struct message *message;
187 class Endpoint *epoint;
191 char prefix_string[64];
192 struct sched_param schedp;
193 char *debug_prefix = "alloc";
194 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
195 created_lock = 0, created_signal = 0, created_debug = 0;
196 #ifdef DEBUG_DURATION
197 time_t durationupdate;
198 double idle_duration, isdn_duration, port_duration, epoint_duration, join_duration, message_duration, admin_duration;
201 int idletime = 0, idlecheck = 0;
208 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
215 printf("Usage: lcr (query | start | fork | rules | route)\n");
216 printf("query = Show available isdn ports.\n");
217 printf("start = Run lcr normally, abort with CTRL+C.\n");
218 printf("fork = Do daemon fork and run as background process.\n");
219 printf("interface = Get help of available interface syntax.\n");
220 printf("rules = Get help of available routing rule syntax.\n");
221 printf("rules [action] = Get individual help for given action.\n");
222 // printf("route = Show current routing as it is parsed.\n");
231 /* check for root (real or effective) */
232 if (getuid() && geteuid())
234 fprintf(stderr, "Please run %s as super-user.\n", NAME);
239 if (pthread_mutex_init(&mutexd, NULL))
241 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
245 // if (pthread_mutex_init(&mutext, NULL))
247 // fprintf(stderr, "cannot create 'trace' mutex\n");
250 // created_mutext = 1;
251 if (pthread_mutex_init(&mutexe, NULL))
253 fprintf(stderr, "cannot create 'PERROR' mutex\n");
259 if (!(strcasecmp(argv[1],"interface")))
267 if (!(strcasecmp(argv[1],"rules")))
277 /* query available isdn ports */
278 if (!(strcasecmp(argv[1],"query")))
286 if (read_options() == 0)
290 if (mISDN_initialize() < 0)
294 /* read ruleset(s) */
295 if (!(ruleset_first = ruleset_parse()))
298 /* set pointer to main ruleset */
299 ruleset_main = getrulesetbyname("main");
302 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
303 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
308 /* query available isdn ports */
309 if (!(strcasecmp(argv[1],"route")))
311 ruleset_debug(ruleset_first);
317 /* do fork in special cases */
318 if (!(strcasecmp(argv[1],"fork")))
327 fprintf(stderr, "Cannot fork!\n");
342 fprintf(stderr, "Cannot fork!\n");
347 printf("LCR: Starting daemon.\n");
353 if (!!strcasecmp(argv[1],"start"))
358 /* create lock and lock! */
359 if ((lockfd = open("/var/run/lcr.lock", O_CREAT, 0)) < 0)
361 fprintf(stderr, "Cannot create lock file: /var/run/lcr.lock\n");
364 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0)
366 if (errno == EWOULDBLOCK)
367 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
368 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
373 /* initialize admin socket */
376 fprintf(stderr, "Unable to initialize admin socket.\n");
380 /* generate alaw / ulaw tables */
381 generate_tables(options.law);
383 /* load tones (if requested) */
384 if (fetch_tones() == 0)
386 fprintf(stderr, "Unable to fetch tones into memory.\n");
390 /* read interfaces and open ports */
391 if (!read_interfaces())
393 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
394 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
398 interface_first = interface_newlist;
399 interface_newlist = NULL;
401 /* locking memory paging */
405 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
415 fprintf(stderr, "Not enough memory to lock paging, exitting...\n");
418 fprintf(stderr, "No permission to lock paging, exitting...\n");
421 fprintf(stderr, "'Bad address' while locking paging, exitting...\n");
424 fprintf(stderr, "Unknown error %d while locking paging, exitting...\n", errno);
429 /* set real time scheduler & priority */
430 if (options.schedule > 1)
432 memset(&schedp, 0, sizeof(schedp));
433 schedp.sched_priority = options.schedule;
434 ret = sched_setscheduler(0, SCHED_RR, &schedp);
437 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
442 /* signal handlers */
443 signal(SIGINT,sighandler);
444 signal(SIGHUP,sighandler);
445 signal(SIGTERM,sighandler);
446 signal(SIGPIPE,sighandler);
450 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
451 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
452 printf("%s\n", tracetext);
455 #ifdef DEBUG_DURATION
457 durationupdate = now;
458 idle_duration = isdn_duration = port_duration = epoint_duration = call_duration = message_duration = admin_duration = 0;
463 /* all loops must be counted from the beginning since nodes might get freed during handler */
466 /* handle mISDN messages from kernel */
467 debug_prefix = "ISDN";
470 #ifdef DEBUG_DURATION
472 isdn_duration += (now_d - start_d);
477 /* loop through all port ports and call their handler */
482 debug_prefix = port->p_name;
484 ret = port->handler();
487 if (ret < 0) /* port has been destroyed */
491 #ifdef DEBUG_DURATION
493 port_duration += (now_d - start_d);
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;
512 #ifdef DEBUG_DURATION
514 epoint_duration += (now_d - start_d);
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 */
532 #ifdef DEBUG_DURATION
534 join_duration += (now_d - start_d);
540 /* process any message */
542 debug_prefix = "message";
543 while ((message = message_get()))
546 switch(message->flow)
549 debug_prefix = "msg port->epoint";
550 epoint = find_epoint_id(message->id_to);
555 epoint->ep_app->ea_message_port(message->id_from, message->type, &message->param);
558 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);
562 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);
567 debug_prefix = "msg epoint->join";
568 join = find_join_id(message->id_to);
571 join->message_epoint(message->id_from, message->type, &message->param);
574 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);
579 debug_prefix = "msg join->epoint";
580 epoint = find_epoint_id(message->id_to);
585 epoint->ep_app->ea_message_join(message->id_from, message->type, &message->param);
588 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);
592 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);
597 debug_prefix = "msg epoint->port";
598 port = find_port_id(message->id_to);
601 port->message_epoint(message->id_from, message->type, &message->param);
605 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);
610 PERROR("Message flow %d unknown.\n", message->flow);
612 message_free(message);
614 debug_prefix = "message";
616 #ifdef DEBUG_DURATION
618 message_duration += (now_d - start_d);
626 #ifdef DEBUG_DURATION
628 admin_duration += (now_d - start_d);
634 /* check for child to exit (eliminate zombies) */
635 if (waitpid(-1, NULL, WNOHANG) > 0)
637 PDEBUG(DEBUG_EPOINT, "a child process (created by endpoint) has exitted.\n");
642 /* do idle checking */
643 if (idlecheck != now)
645 PDEBUG(DEBUG_IDLETIME, "Idle time : %d%%\n", idletime/10000);
649 #ifdef DEBUG_DURATION
651 idle_duration += (now_d - start_d);
653 if (durationupdate != now)
655 durationupdate = now;
656 printf("Idle:%3d ISDN:%3d Port:%3d Epoint:%3d Join:%3d Message:%3d Admin:%3d\n",
657 (int)(idle_duration*100),
658 (int)(isdn_duration*100),
659 (int)(port_duration*100),
660 (int)(epoint_duration*100),
661 (int)(join_duration*100),
662 (int)(message_duration*100),
663 (int)(admin_duration*100));
664 idle_duration = isdn_duration = port_duration = epoint_duration = join_duration = message_duration = admin_duration = 0;
670 /* did we do nothing? so we wait to give time to other processes */
673 usleep(4000); /* wait 32 samples */
677 SPRINT(tracetext, "%s terminated", NAME);
678 printf("%s\n", tracetext);
679 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
681 add_trace("error", NULL, "%d", ret);
689 /* set scheduler & priority
691 if (options.schedule > 1)
693 memset(&schedp, 0, sizeof(schedp));
694 schedp.sched_priority = 0;
695 sched_setscheduler(0, SCHED_OTHER, &schedp);
700 signal(SIGINT,SIG_DFL);
701 signal(SIGHUP,SIG_DFL);
702 signal(SIGTERM,SIG_DFL);
703 signal(SIGPIPE,SIG_DFL);
706 /* destroy objects */
707 debug_prefix = "free";
723 /* free interfaces */
725 free_interfaces(interface_first);
726 interface_first = NULL;
728 /* close isdn ports */
729 mISDNport_close_all();
734 while ((message = message_get()))
737 message_free(message);
741 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
748 /* free admin socket */
753 flock(lockfd, LOCK_UN);
759 ruleset_free(ruleset_first);
760 ruleset_first = NULL;
764 if (pthread_mutex_destroy(&mutexe))
765 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
766 // if (created_mutext)
767 // if (pthread_mutex_destroy(&mutext))
768 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
770 if (pthread_mutex_destroy(&mutexd))
771 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
773 /* deinitialize mISDN */
774 mISDN_deinitialize();
777 /* display memory leak */
778 #define MEMCHECK(a, b) \
781 SPRINT(tracetext, a, NAME); \
782 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext); \
783 if (ret) add_trace("blocks", NULL, "%d", b); \
785 printf("\n******************************\n\007"); \
786 printf("\nERROR: %d %s\n", b, a); \
787 printf("\n******************************\n"); \
791 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
792 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
793 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
794 MEMCHECK("memory block(s) left (message.c)",mmemuse)
795 MEMCHECK("memory block(s) left (route.c)",rmemuse)
796 MEMCHECK("memory block(s) left (args)",amemuse)
797 MEMCHECK("class(es) left",classuse)
798 MEMCHECK("file descriptor(s) left",fduse)
799 MEMCHECK("file handler(s) left",fhuse)
807 /* special debug function to detect buffer overflow
809 int budetect_stop = 0;
810 void budetect(const char *file, int line, char *function)
814 /* modify this function to detect race-bugs */
815 #warning DID YOU MODIFY THIS FUNCTION TO DETECT THE BUFFER OVERFLOW BUG?
817 class PmISDN *pmisdn;
818 struct mISDNport *mISDNport = mISDNport_first;
824 ii = mISDNport->b_num;
827 if (mISDNport->b_port[i])
832 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_ISDN)
834 pmisdn = (class PmISDN *)port;
835 if (pmisdn->p_isdn_crypt_listen)
837 PERROR_RUNTIME("************************************************\n");
838 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
839 PERROR_RUNTIME("** p_isdn_crypt_listen = %d\n", pmisdn->p_isdn_crypt_listen);
840 PERROR_RUNTIME("************************************************\n");
844 if (port == mISDNport->b_port[i])
849 PERROR_RUNTIME("************************************************\n");
850 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
851 PERROR_RUNTIME("** b_port not in list.\n");
852 PERROR_RUNTIME("************************************************\n");
859 mISDNport = mISDNport->next;