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
43 //pthread_mutex_t mutex_lcr; // lcr process mutex
56 char *debug_prefix = 0;
59 int debug_newline = 1;
62 void debug_usleep(int msec, char *file, int line, int hour, int min, int sec)
67 void debug(const char *function, int line, char *prefix, char *buffer)
69 /* if we have a new debug count, we add a mark */
70 if (last_debug != debug_count)
72 last_debug = debug_count;
74 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 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);
79 if (options.deb&DEBUG_LOG && global_debug)
80 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);
87 printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
89 printf("(in %s() line %d): %s", function, line, buffer);
97 if (options.deb&DEBUG_LOG && global_debug)
104 fprintf(debug_fp, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
106 dprint(DBGM_MAN, 0, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
110 fprintf(debug_fp, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
112 dprint(DBGM_MAN, 0, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
119 if (buffer[strlen(buffer)-1] == '\n')
124 void _printdebug(const char *function, int line, unsigned long mask, const char *fmt, ...)
129 if (!(options.deb & mask))
131 pthread_mutex_lock(&mutexd);
134 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
135 buffer[sizeof(buffer)-1]=0;
138 debug(function, line, debug_prefix, buffer);
140 pthread_mutex_unlock(&mutexd);
143 void _printerror(const char *function, int line, const char *fmt, ...)
148 pthread_mutex_lock(&mutexe);
151 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
152 buffer[sizeof(buffer)-1]=0;
156 debug(function, line, "ERROR", buffer);
157 else /* only if we do not debug */
160 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
162 fprintf(stderr, "ERROR %s", buffer);
165 pthread_mutex_unlock(&mutexe);
169 void sighandler(int sigset)
171 struct sched_param schedp;
173 if (sigset == SIGHUP)
175 if (sigset == SIGPIPE)
180 /* set scheduler & priority */
181 if (options.schedule > 1)
183 memset(&schedp, 0, sizeof(schedp));
184 schedp.sched_priority = 0;
185 sched_setscheduler(0, SCHED_OTHER, &schedp);
187 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
188 PERROR("Signal received: %d\n", sigset);
196 int main(int argc, char *argv[])
199 int lockfd = -1; /* file lock */
200 struct lcr_msg *message;
202 class Endpoint *epoint;
206 char prefix_string[64];
207 struct sched_param schedp;
208 char *debug_prefix = "alloc";
209 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
210 created_lock = 0, created_signal = 0, created_debug = 0,
212 int idletime = 0, idlecheck = 0;
217 memset(lcr_fdset, 0, sizeof(lcr_fdset));
220 /* lock LCR process */
221 // pthread_mutex_lock(&mutex_lcr);
227 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
234 printf("Usage: lcr (query | start | fork | rules | route)\n");
235 printf("query = Show available isdn ports.\n");
236 printf("start = Run lcr normally, abort with CTRL+C.\n");
237 printf("fork = Do daemon fork and run as background process.\n");
238 printf("interface = Get help of available interface syntax.\n");
239 printf("rules = Get help of available routing rule syntax.\n");
240 printf("rules [action] = Get individual help for given action.\n");
241 // printf("route = Show current routing as it is parsed.\n");
250 /* check for root (real or effective) */
251 if (getuid() && geteuid())
253 fprintf(stderr, "Please run %s as super-user.\n", NAME);
258 if (pthread_mutex_init(&mutexd, NULL))
260 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
264 // if (pthread_mutex_init(&mutext, NULL))
266 // fprintf(stderr, "cannot create 'trace' mutex\n");
269 // created_mutext = 1;
270 if (pthread_mutex_init(&mutexe, NULL))
272 fprintf(stderr, "cannot create 'PERROR' mutex\n");
278 if (!(strcasecmp(argv[1],"interface")))
286 if (!(strcasecmp(argv[1],"rules")))
296 /* query available isdn ports */
297 if (!(strcasecmp(argv[1],"query")))
305 if (read_options() == 0)
309 if (mISDN_initialize() < 0)
314 /* read ruleset(s) */
315 if (!(ruleset_first = ruleset_parse()))
318 /* set pointer to main ruleset */
319 ruleset_main = getrulesetbyname("main");
322 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
323 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
328 /* query available isdn ports */
329 if (!(strcasecmp(argv[1],"route")))
331 ruleset_debug(ruleset_first);
337 /* do fork in special cases */
338 if (!(strcasecmp(argv[1],"fork")))
347 fprintf(stderr, "Cannot fork!\n");
362 fprintf(stderr, "Cannot fork!\n");
367 printf("LCR: Starting daemon.\n");
373 if (!!strcasecmp(argv[1],"start"))
378 /* create lock and lock! */
379 if ((lockfd = open("/var/run/lcr.lock", O_CREAT, 0)) < 0)
381 fprintf(stderr, "Cannot create lock file: /var/run/lcr.lock\n");
384 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0)
386 if (errno == EWOULDBLOCK)
387 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
388 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
393 /* initialize admin socket */
396 fprintf(stderr, "Unable to initialize admin socket.\n");
400 /* generate alaw / ulaw tables */
401 generate_tables(options.law);
403 /* load tones (if requested) */
404 if (fetch_tones() == 0)
406 fprintf(stderr, "Unable to fetch tones into memory.\n");
410 /* read interfaces and open ports */
411 if (!read_interfaces())
413 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
414 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
418 interface_first = interface_newlist;
419 interface_newlist = NULL;
421 /* locking memory paging */
425 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
435 fprintf(stderr, "Not enough memory to lock paging, exitting...\n");
438 fprintf(stderr, "No permission to lock paging, exitting...\n");
441 fprintf(stderr, "'Bad address' while locking paging, exitting...\n");
444 fprintf(stderr, "Unknown error %d while locking paging, exitting...\n", errno);
449 /* set real time scheduler & priority */
450 if (options.schedule > 1)
452 memset(&schedp, 0, sizeof(schedp));
453 schedp.sched_priority = options.schedule;
454 ret = sched_setscheduler(0, SCHED_RR, &schedp);
457 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
462 /* signal handlers */
463 signal(SIGINT,sighandler);
464 signal(SIGHUP,sighandler);
465 signal(SIGTERM,sighandler);
466 signal(SIGPIPE,sighandler);
470 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
471 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
472 printf("%s\n", tracetext);
481 if (now_d-last_d > 1.0)
483 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
485 /* all loops must be counted from the beginning since nodes might get freed during handler */
488 //#warning debugging usleep crash
489 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
491 /* handle mISDN messages from kernel */
492 debug_prefix = "ISDN";
495 //#warning debugging usleep crash
496 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
500 /* loop through all port ports and call their handler */
505 debug_prefix = port->p_name;
507 ret = port->handler();
510 if (ret < 0) /* port has been destroyed */
515 /* loop through all epoint and call their handler */
517 epoint = epoint_first;
520 debug_prefix = prefix_string;
521 SPRINT(prefix_string, "ep%ld", epoint->ep_serial);
523 ret = epoint->handler();
526 if (ret < 0) /* epoint has been destroyed */
528 epoint = epoint->next;
531 /* loop through all joins and call their handler */
536 debug_prefix = "join";
538 ret = join->handler();
541 if (ret < 0) /* join has been destroyed */
548 /* process any message */
550 debug_prefix = "message";
551 while ((message = message_get()))
554 switch(message->flow)
557 debug_prefix = "msg port->epoint";
558 epoint = find_epoint_id(message->id_to);
563 epoint->ep_app->ea_message_port(message->id_from, message->type, &message->param);
566 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);
570 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);
575 debug_prefix = "msg epoint->join";
576 join = find_join_id(message->id_to);
579 join->message_epoint(message->id_from, message->type, &message->param);
582 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);
587 debug_prefix = "msg join->epoint";
588 epoint = find_epoint_id(message->id_to);
593 epoint->ep_app->ea_message_join(message->id_from, message->type, &message->param);
596 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);
600 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);
605 debug_prefix = "msg epoint->port";
606 port = find_port_id(message->id_to);
609 port->message_epoint(message->id_from, message->type, &message->param);
613 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);
618 PERROR("Message flow %d unknown.\n", message->flow);
620 message_free(message);
622 debug_prefix = "message";
632 /* check for child to exit (eliminate zombies) */
633 if (waitpid(-1, NULL, WNOHANG) > 0)
635 PDEBUG(DEBUG_EPOINT, "a child process (created by endpoint) has exitted.\n");
639 //#warning debugging usleep crash
640 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
642 /* do idle checking */
643 if (idlecheck != now)
645 PDEBUG(DEBUG_IDLETIME, "Idle time : %d%%\n", idletime/10000);
650 /* did we do nothing? so we wait to give time to other processes */
653 // pthread_mutex_unlock(&mutex_lcr); // unlock LCR
654 debug_usleep(4000, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
655 // pthread_mutex_lock(&mutex_lcr); // lock LCR
659 SPRINT(tracetext, "%s terminated", NAME);
660 printf("%s\n", tracetext);
661 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
663 add_trace("error", NULL, "%d", ret);
671 /* set scheduler & priority
673 if (options.schedule > 1)
675 memset(&schedp, 0, sizeof(schedp));
676 schedp.sched_priority = options.schedule;
677 sched_setscheduler(0, SCHED_OTHER, &schedp);
682 signal(SIGINT,SIG_DFL);
683 signal(SIGHUP,SIG_DFL);
684 signal(SIGTERM,SIG_DFL);
685 signal(SIGPIPE,SIG_DFL);
688 /* destroy objects */
689 debug_prefix = "free";
705 /* free interfaces */
707 free_interfaces(interface_first);
708 interface_first = NULL;
710 /* close isdn ports */
711 mISDNport_close_all();
716 while ((message = message_get()))
719 message_free(message);
723 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
730 /* free admin socket */
735 flock(lockfd, LOCK_UN);
741 ruleset_free(ruleset_first);
742 ruleset_first = NULL;
746 if (pthread_mutex_destroy(&mutexe))
747 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
748 // if (created_mutext)
749 // if (pthread_mutex_destroy(&mutext))
750 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
752 if (pthread_mutex_destroy(&mutexd))
753 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
755 /* deinitialize mISDN */
757 mISDN_deinitialize();
759 /* display memory leak */
760 #define MEMCHECK(a, b) \
763 SPRINT(tracetext, a, NAME); \
764 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext); \
765 if (ret) add_trace("blocks", NULL, "%d", b); \
767 printf("\n******************************\n\007"); \
768 printf("\nERROR: %d %s\n", b, a); \
769 printf("\n******************************\n"); \
773 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
774 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
775 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
776 MEMCHECK("memory block(s) left (message.c)",mmemuse)
777 MEMCHECK("memory block(s) left (route.c)",rmemuse)
778 MEMCHECK("memory block(s) left (args)",amemuse)
779 MEMCHECK("class(es) left",classuse)
780 MEMCHECK("file descriptor(s) left",fduse)
781 MEMCHECK("file handler(s) left",fhuse)
783 /* unlock LCR process */
784 // pthread_mutex_unlock(&mutex_lcr);
792 /* special debug function to detect buffer overflow
794 int budetect_stop = 0;
795 void budetect(const char *file, int line, char *function)
799 /* modify this function to detect race-bugs */
800 #warning DID YOU MODIFY THIS FUNCTION TO DETECT THE BUFFER OVERFLOW BUG?
802 class PmISDN *pmisdn;
803 struct mISDNport *mISDNport = mISDNport_first;
809 ii = mISDNport->b_num;
812 if (mISDNport->b_port[i])
817 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_ISDN)
819 pmisdn = (class PmISDN *)port;
820 if (pmisdn->p_isdn_crypt_listen)
822 PERROR_RUNTIME("************************************************\n");
823 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
824 PERROR_RUNTIME("** p_isdn_crypt_listen = %d\n", pmisdn->p_isdn_crypt_listen);
825 PERROR_RUNTIME("************************************************\n");
829 if (port == mISDNport->b_port[i])
834 PERROR_RUNTIME("************************************************\n");
835 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
836 PERROR_RUNTIME("** b_port not in list.\n");
837 PERROR_RUNTIME("************************************************\n");
844 mISDNport = mISDNport->next;