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); \
47 pthread_mutex_t mutexd; // debug output mutex
48 //pthread_mutex_t mutext; // trace output mutex
49 pthread_mutex_t mutexe; // error output mutex
62 char *debug_prefix = 0;
65 int debug_newline = 1;
68 void debug_usleep(int msec, char *file, int line, int hour, int min, int sec)
73 void debug(const char *function, int line, char *prefix, char *buffer)
75 /* if we have a new debug count, we add a mark */
76 if (last_debug != debug_count)
78 last_debug = debug_count;
80 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);
81 if (options.deb&DEBUG_LOG && global_debug)
82 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);
88 printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
90 printf("(in %s() line %d): %s", function, line, buffer);
95 if (options.deb&DEBUG_LOG && global_debug)
100 dprint(DBGM_MAN, 0, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
102 dprint(DBGM_MAN, 0, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
108 if (buffer[strlen(buffer)-1] == '\n')
113 void _printdebug(const char *function, int line, unsigned long mask, const char *fmt, ...)
118 if (!(options.deb & mask))
120 pthread_mutex_lock(&mutexd);
123 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
124 buffer[sizeof(buffer)-1]=0;
127 debug(function, line, debug_prefix, buffer);
129 pthread_mutex_unlock(&mutexd);
132 void _printerror(const char *function, int line, const char *fmt, ...)
137 pthread_mutex_lock(&mutexe);
140 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
141 buffer[sizeof(buffer)-1]=0;
145 debug(function, line, "ERROR", buffer);
146 else /* only if we do not debug */
149 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
151 fprintf(stderr, "ERROR %s", buffer);
154 pthread_mutex_unlock(&mutexe);
158 void sighandler(int sigset)
160 struct sched_param schedp;
162 if (sigset == SIGHUP)
164 if (sigset == SIGPIPE)
169 /* set scheduler & priority */
170 if (options.schedule > 1)
172 memset(&schedp, 0, sizeof(schedp));
173 schedp.sched_priority = 0;
174 sched_setscheduler(0, SCHED_OTHER, &schedp);
176 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
177 PERROR("Signal received: %d\n", sigset);
185 int main(int argc, char *argv[])
188 int lockfd = -1; /* file lock */
189 struct message *message;
191 class Endpoint *epoint;
195 char prefix_string[64];
196 struct sched_param schedp;
197 char *debug_prefix = "alloc";
198 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
199 created_lock = 0, created_signal = 0, created_debug = 0;
200 int idletime = 0, idlecheck = 0;
207 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
214 printf("Usage: lcr (query | start | fork | rules | route)\n");
215 printf("query = Show available isdn ports.\n");
216 printf("start = Run lcr normally, abort with CTRL+C.\n");
217 printf("fork = Do daemon fork and run as background process.\n");
218 printf("interface = Get help of available interface syntax.\n");
219 printf("rules = Get help of available routing rule syntax.\n");
220 printf("rules [action] = Get individual help for given action.\n");
221 // printf("route = Show current routing as it is parsed.\n");
230 /* check for root (real or effective) */
231 if (getuid() && geteuid())
233 fprintf(stderr, "Please run %s as super-user.\n", NAME);
238 if (pthread_mutex_init(&mutexd, NULL))
240 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
244 // if (pthread_mutex_init(&mutext, NULL))
246 // fprintf(stderr, "cannot create 'trace' mutex\n");
249 // created_mutext = 1;
250 if (pthread_mutex_init(&mutexe, NULL))
252 fprintf(stderr, "cannot create 'PERROR' mutex\n");
258 if (!(strcasecmp(argv[1],"interface")))
266 if (!(strcasecmp(argv[1],"rules")))
276 /* query available isdn ports */
277 if (!(strcasecmp(argv[1],"query")))
285 if (read_options() == 0)
289 if (mISDN_initialize() < 0)
293 /* read ruleset(s) */
294 if (!(ruleset_first = ruleset_parse()))
297 /* set pointer to main ruleset */
298 ruleset_main = getrulesetbyname("main");
301 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
302 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
307 /* query available isdn ports */
308 if (!(strcasecmp(argv[1],"route")))
310 ruleset_debug(ruleset_first);
316 /* do fork in special cases */
317 if (!(strcasecmp(argv[1],"fork")))
326 fprintf(stderr, "Cannot fork!\n");
341 fprintf(stderr, "Cannot fork!\n");
346 printf("LCR: Starting daemon.\n");
352 if (!!strcasecmp(argv[1],"start"))
357 /* create lock and lock! */
358 if ((lockfd = open("/var/run/lcr.lock", O_CREAT, 0)) < 0)
360 fprintf(stderr, "Cannot create lock file: /var/run/lcr.lock\n");
363 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0)
365 if (errno == EWOULDBLOCK)
366 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
367 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
372 /* initialize admin socket */
375 fprintf(stderr, "Unable to initialize admin socket.\n");
379 /* generate alaw / ulaw tables */
380 generate_tables(options.law);
382 /* load tones (if requested) */
383 if (fetch_tones() == 0)
385 fprintf(stderr, "Unable to fetch tones into memory.\n");
389 /* read interfaces and open ports */
390 if (!read_interfaces())
392 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
393 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
397 interface_first = interface_newlist;
398 interface_newlist = NULL;
400 /* locking memory paging */
404 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
414 fprintf(stderr, "Not enough memory to lock paging, exitting...\n");
417 fprintf(stderr, "No permission to lock paging, exitting...\n");
420 fprintf(stderr, "'Bad address' while locking paging, exitting...\n");
423 fprintf(stderr, "Unknown error %d while locking paging, exitting...\n", errno);
428 /* set real time scheduler & priority */
429 if (options.schedule > 1)
431 memset(&schedp, 0, sizeof(schedp));
432 schedp.sched_priority = options.schedule;
433 ret = sched_setscheduler(0, SCHED_RR, &schedp);
436 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
441 /* signal handlers */
442 signal(SIGINT,sighandler);
443 signal(SIGHUP,sighandler);
444 signal(SIGTERM,sighandler);
445 signal(SIGPIPE,sighandler);
449 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
450 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
451 printf("%s\n", tracetext);
459 if (now_d-last_d > 1.0)
461 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
463 /* all loops must be counted from the beginning since nodes might get freed during handler */
466 #warning debugging usleep crash
467 debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
469 /* handle mISDN messages from kernel */
470 debug_prefix = "ISDN";
473 #warning debugging usleep crash
474 debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
478 /* loop through all port ports and call their handler */
483 debug_prefix = port->p_name;
485 ret = port->handler();
488 if (ret < 0) /* port has been destroyed */
493 /* loop through all epoint and call their handler */
495 epoint = epoint_first;
498 debug_prefix = prefix_string;
499 SPRINT(prefix_string, "ep%ld", epoint->ep_serial);
501 ret = epoint->handler();
504 if (ret < 0) /* epoint has been destroyed */
506 epoint = epoint->next;
509 /* loop through all joins and call their handler */
514 debug_prefix = "join";
516 ret = join->handler();
519 if (ret < 0) /* join has been destroyed */
526 /* process any message */
528 debug_prefix = "message";
529 while ((message = message_get()))
532 switch(message->flow)
535 debug_prefix = "msg port->epoint";
536 epoint = find_epoint_id(message->id_to);
541 epoint->ep_app->ea_message_port(message->id_from, message->type, &message->param);
544 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);
548 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);
553 debug_prefix = "msg epoint->join";
554 join = find_join_id(message->id_to);
557 join->message_epoint(message->id_from, message->type, &message->param);
560 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);
565 debug_prefix = "msg join->epoint";
566 epoint = find_epoint_id(message->id_to);
571 epoint->ep_app->ea_message_join(message->id_from, message->type, &message->param);
574 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);
578 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);
583 debug_prefix = "msg epoint->port";
584 port = find_port_id(message->id_to);
587 port->message_epoint(message->id_from, message->type, &message->param);
591 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);
596 PERROR("Message flow %d unknown.\n", message->flow);
598 message_free(message);
600 debug_prefix = "message";
610 /* check for child to exit (eliminate zombies) */
611 if (waitpid(-1, NULL, WNOHANG) > 0)
613 PDEBUG(DEBUG_EPOINT, "a child process (created by endpoint) has exitted.\n");
617 #warning debugging usleep crash
618 debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
620 /* do idle checking */
621 if (idlecheck != now)
623 PDEBUG(DEBUG_IDLETIME, "Idle time : %d%%\n", idletime/10000);
628 /* did we do nothing? so we wait to give time to other processes */
631 debug_usleep(4000, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
635 SPRINT(tracetext, "%s terminated", NAME);
636 printf("%s\n", tracetext);
637 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
639 add_trace("error", NULL, "%d", ret);
647 /* set scheduler & priority
649 if (options.schedule > 1)
651 memset(&schedp, 0, sizeof(schedp));
652 schedp.sched_priority = 0;
653 sched_setscheduler(0, SCHED_OTHER, &schedp);
658 signal(SIGINT,SIG_DFL);
659 signal(SIGHUP,SIG_DFL);
660 signal(SIGTERM,SIG_DFL);
661 signal(SIGPIPE,SIG_DFL);
664 /* destroy objects */
665 debug_prefix = "free";
681 /* free interfaces */
683 free_interfaces(interface_first);
684 interface_first = NULL;
686 /* close isdn ports */
687 mISDNport_close_all();
692 while ((message = message_get()))
695 message_free(message);
699 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
706 /* free admin socket */
711 flock(lockfd, LOCK_UN);
717 ruleset_free(ruleset_first);
718 ruleset_first = NULL;
722 if (pthread_mutex_destroy(&mutexe))
723 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
724 // if (created_mutext)
725 // if (pthread_mutex_destroy(&mutext))
726 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
728 if (pthread_mutex_destroy(&mutexd))
729 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
731 /* deinitialize mISDN */
732 mISDN_deinitialize();
735 /* display memory leak */
736 #define MEMCHECK(a, b) \
739 SPRINT(tracetext, a, NAME); \
740 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext); \
741 if (ret) add_trace("blocks", NULL, "%d", b); \
743 printf("\n******************************\n\007"); \
744 printf("\nERROR: %d %s\n", b, a); \
745 printf("\n******************************\n"); \
749 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
750 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
751 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
752 MEMCHECK("memory block(s) left (message.c)",mmemuse)
753 MEMCHECK("memory block(s) left (route.c)",rmemuse)
754 MEMCHECK("memory block(s) left (args)",amemuse)
755 MEMCHECK("class(es) left",classuse)
756 MEMCHECK("file descriptor(s) left",fduse)
757 MEMCHECK("file handler(s) left",fhuse)
765 /* special debug function to detect buffer overflow
767 int budetect_stop = 0;
768 void budetect(const char *file, int line, char *function)
772 /* modify this function to detect race-bugs */
773 #warning DID YOU MODIFY THIS FUNCTION TO DETECT THE BUFFER OVERFLOW BUG?
775 class PmISDN *pmisdn;
776 struct mISDNport *mISDNport = mISDNport_first;
782 ii = mISDNport->b_num;
785 if (mISDNport->b_port[i])
790 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_ISDN)
792 pmisdn = (class PmISDN *)port;
793 if (pmisdn->p_isdn_crypt_listen)
795 PERROR_RUNTIME("************************************************\n");
796 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
797 PERROR_RUNTIME("** p_isdn_crypt_listen = %d\n", pmisdn->p_isdn_crypt_listen);
798 PERROR_RUNTIME("************************************************\n");
802 if (port == mISDNport->b_port[i])
807 PERROR_RUNTIME("************************************************\n");
808 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
809 PERROR_RUNTIME("** b_port not in list.\n");
810 PERROR_RUNTIME("************************************************\n");
817 mISDNport = mISDNport->next;