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 message *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;
211 int idletime = 0, idlecheck = 0;
216 memset(lcr_fdset, 0, sizeof(lcr_fdset));
219 /* lock LCR process */
220 // pthread_mutex_lock(&mutex_lcr);
226 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
233 printf("Usage: lcr (query | start | fork | rules | route)\n");
234 printf("query = Show available isdn ports.\n");
235 printf("start = Run lcr normally, abort with CTRL+C.\n");
236 printf("fork = Do daemon fork and run as background process.\n");
237 printf("interface = Get help of available interface syntax.\n");
238 printf("rules = Get help of available routing rule syntax.\n");
239 printf("rules [action] = Get individual help for given action.\n");
240 // printf("route = Show current routing as it is parsed.\n");
249 /* check for root (real or effective) */
250 if (getuid() && geteuid())
252 fprintf(stderr, "Please run %s as super-user.\n", NAME);
257 if (pthread_mutex_init(&mutexd, NULL))
259 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
263 // if (pthread_mutex_init(&mutext, NULL))
265 // fprintf(stderr, "cannot create 'trace' mutex\n");
268 // created_mutext = 1;
269 if (pthread_mutex_init(&mutexe, NULL))
271 fprintf(stderr, "cannot create 'PERROR' mutex\n");
277 if (!(strcasecmp(argv[1],"interface")))
285 if (!(strcasecmp(argv[1],"rules")))
295 /* query available isdn ports */
296 if (!(strcasecmp(argv[1],"query")))
304 if (read_options() == 0)
308 if (mISDN_initialize() < 0)
312 /* read ruleset(s) */
313 if (!(ruleset_first = ruleset_parse()))
316 /* set pointer to main ruleset */
317 ruleset_main = getrulesetbyname("main");
320 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
321 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
326 /* query available isdn ports */
327 if (!(strcasecmp(argv[1],"route")))
329 ruleset_debug(ruleset_first);
335 /* do fork in special cases */
336 if (!(strcasecmp(argv[1],"fork")))
345 fprintf(stderr, "Cannot fork!\n");
360 fprintf(stderr, "Cannot fork!\n");
365 printf("LCR: Starting daemon.\n");
371 if (!!strcasecmp(argv[1],"start"))
376 /* create lock and lock! */
377 if ((lockfd = open("/var/run/lcr.lock", O_CREAT, 0)) < 0)
379 fprintf(stderr, "Cannot create lock file: /var/run/lcr.lock\n");
382 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0)
384 if (errno == EWOULDBLOCK)
385 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
386 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
391 /* initialize admin socket */
394 fprintf(stderr, "Unable to initialize admin socket.\n");
398 /* generate alaw / ulaw tables */
399 generate_tables(options.law);
401 /* load tones (if requested) */
402 if (fetch_tones() == 0)
404 fprintf(stderr, "Unable to fetch tones into memory.\n");
408 /* read interfaces and open ports */
409 if (!read_interfaces())
411 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
412 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
416 interface_first = interface_newlist;
417 interface_newlist = NULL;
419 /* locking memory paging */
423 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
433 fprintf(stderr, "Not enough memory to lock paging, exitting...\n");
436 fprintf(stderr, "No permission to lock paging, exitting...\n");
439 fprintf(stderr, "'Bad address' while locking paging, exitting...\n");
442 fprintf(stderr, "Unknown error %d while locking paging, exitting...\n", errno);
447 /* set real time scheduler & priority */
448 if (options.schedule > 1)
450 memset(&schedp, 0, sizeof(schedp));
451 schedp.sched_priority = options.schedule;
452 ret = sched_setscheduler(0, SCHED_RR, &schedp);
455 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
460 /* signal handlers */
461 signal(SIGINT,sighandler);
462 signal(SIGHUP,sighandler);
463 signal(SIGTERM,sighandler);
464 signal(SIGPIPE,sighandler);
468 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
469 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
470 printf("%s\n", tracetext);
479 if (now_d-last_d > 1.0)
481 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
483 /* all loops must be counted from the beginning since nodes might get freed during handler */
486 //#warning debugging usleep crash
487 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
489 /* handle mISDN messages from kernel */
490 debug_prefix = "ISDN";
493 //#warning debugging usleep crash
494 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
498 /* loop through all port ports and call their handler */
503 debug_prefix = port->p_name;
505 ret = port->handler();
508 if (ret < 0) /* port has been destroyed */
513 /* loop through all epoint and call their handler */
515 epoint = epoint_first;
518 debug_prefix = prefix_string;
519 SPRINT(prefix_string, "ep%ld", epoint->ep_serial);
521 ret = epoint->handler();
524 if (ret < 0) /* epoint has been destroyed */
526 epoint = epoint->next;
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 */
546 /* process any message */
548 debug_prefix = "message";
549 while ((message = message_get()))
552 switch(message->flow)
555 debug_prefix = "msg port->epoint";
556 epoint = find_epoint_id(message->id_to);
561 epoint->ep_app->ea_message_port(message->id_from, message->type, &message->param);
564 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);
568 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);
573 debug_prefix = "msg epoint->join";
574 join = find_join_id(message->id_to);
577 join->message_epoint(message->id_from, message->type, &message->param);
580 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);
585 debug_prefix = "msg join->epoint";
586 epoint = find_epoint_id(message->id_to);
591 epoint->ep_app->ea_message_join(message->id_from, message->type, &message->param);
594 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);
598 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);
603 debug_prefix = "msg epoint->port";
604 port = find_port_id(message->id_to);
607 port->message_epoint(message->id_from, message->type, &message->param);
611 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);
616 PERROR("Message flow %d unknown.\n", message->flow);
618 message_free(message);
620 debug_prefix = "message";
630 /* check for child to exit (eliminate zombies) */
631 if (waitpid(-1, NULL, WNOHANG) > 0)
633 PDEBUG(DEBUG_EPOINT, "a child process (created by endpoint) has exitted.\n");
637 //#warning debugging usleep crash
638 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
640 /* do idle checking */
641 if (idlecheck != now)
643 PDEBUG(DEBUG_IDLETIME, "Idle time : %d%%\n", idletime/10000);
648 /* did we do nothing? so we wait to give time to other processes */
651 // pthread_mutex_unlock(&mutex_lcr); // unlock LCR
652 debug_usleep(4000, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
653 // pthread_mutex_lock(&mutex_lcr); // lock LCR
657 SPRINT(tracetext, "%s terminated", NAME);
658 printf("%s\n", tracetext);
659 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
661 add_trace("error", NULL, "%d", ret);
669 /* set scheduler & priority
671 if (options.schedule > 1)
673 memset(&schedp, 0, sizeof(schedp));
674 schedp.sched_priority = options.schedule;
675 sched_setscheduler(0, SCHED_OTHER, &schedp);
680 signal(SIGINT,SIG_DFL);
681 signal(SIGHUP,SIG_DFL);
682 signal(SIGTERM,SIG_DFL);
683 signal(SIGPIPE,SIG_DFL);
686 /* destroy objects */
687 debug_prefix = "free";
703 /* free interfaces */
705 free_interfaces(interface_first);
706 interface_first = NULL;
708 /* close isdn ports */
709 mISDNport_close_all();
714 while ((message = message_get()))
717 message_free(message);
721 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
728 /* free admin socket */
733 flock(lockfd, LOCK_UN);
739 ruleset_free(ruleset_first);
740 ruleset_first = NULL;
744 if (pthread_mutex_destroy(&mutexe))
745 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
746 // if (created_mutext)
747 // if (pthread_mutex_destroy(&mutext))
748 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
750 if (pthread_mutex_destroy(&mutexd))
751 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
753 /* deinitialize mISDN */
754 mISDN_deinitialize();
756 /* display memory leak */
757 #define MEMCHECK(a, b) \
760 SPRINT(tracetext, a, NAME); \
761 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext); \
762 if (ret) add_trace("blocks", NULL, "%d", b); \
764 printf("\n******************************\n\007"); \
765 printf("\nERROR: %d %s\n", b, a); \
766 printf("\n******************************\n"); \
770 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
771 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
772 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
773 MEMCHECK("memory block(s) left (message.c)",mmemuse)
774 MEMCHECK("memory block(s) left (route.c)",rmemuse)
775 MEMCHECK("memory block(s) left (args)",amemuse)
776 MEMCHECK("class(es) left",classuse)
777 MEMCHECK("file descriptor(s) left",fduse)
778 MEMCHECK("file handler(s) left",fhuse)
780 /* unlock LCR process */
781 // pthread_mutex_unlock(&mutex_lcr);
789 /* special debug function to detect buffer overflow
791 int budetect_stop = 0;
792 void budetect(const char *file, int line, char *function)
796 /* modify this function to detect race-bugs */
797 #warning DID YOU MODIFY THIS FUNCTION TO DETECT THE BUFFER OVERFLOW BUG?
799 class PmISDN *pmisdn;
800 struct mISDNport *mISDNport = mISDNport_first;
806 ii = mISDNport->b_num;
809 if (mISDNport->b_port[i])
814 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_ISDN)
816 pmisdn = (class PmISDN *)port;
817 if (pmisdn->p_isdn_crypt_listen)
819 PERROR_RUNTIME("************************************************\n");
820 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
821 PERROR_RUNTIME("** p_isdn_crypt_listen = %d\n", pmisdn->p_isdn_crypt_listen);
822 PERROR_RUNTIME("************************************************\n");
826 if (port == mISDNport->b_port[i])
831 PERROR_RUNTIME("************************************************\n");
832 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
833 PERROR_RUNTIME("** b_port not in list.\n");
834 PERROR_RUNTIME("************************************************\n");
841 mISDNport = mISDNport->next;