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); \
29 FILE *debug_fp = NULL;
33 struct lcr_fdset lcr_fdset[FD_SETSIZE];
36 pthread_mutex_t mutexd; // debug output mutex
37 pthread_mutex_t mutext; // trace output mutex
38 pthread_mutex_t mutexe; // error output mutex
39 //pthread_mutex_t mutex_lcr; // lcr process mutex
52 const char *debug_prefix = NULL;
55 int debug_newline = 1;
58 void debug_usleep(int msec, const char *file, int line, int hour, int min, int sec)
63 void debug(const char *function, int line, const char *prefix, char *buffer)
65 /* if we have a new debug count, we add a mark */
66 if (last_debug != debug_count)
68 last_debug = debug_count;
70 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);
72 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);
78 printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
80 printf("(in %s() line %d): %s", function, line, buffer);
90 fprintf(debug_fp, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
92 fprintf(debug_fp, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
98 if (buffer[strlen(buffer)-1] == '\n')
103 void _printdebug(const char *function, int line, unsigned int mask, const char *fmt, ...)
108 if (!(options.deb & mask))
110 pthread_mutex_lock(&mutexd);
113 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
114 buffer[sizeof(buffer)-1]=0;
117 debug(function, line, debug_prefix, buffer);
119 pthread_mutex_unlock(&mutexd);
122 void _printerror(const char *function, int line, const char *fmt, ...)
127 pthread_mutex_lock(&mutexe);
130 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
131 buffer[sizeof(buffer)-1]=0;
135 debug(function, line, "ERROR", buffer);
136 else /* only if we do not debug */
139 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
141 fprintf(stderr, "ERROR %s", buffer);
144 pthread_mutex_unlock(&mutexe);
148 void sighandler(int sigset)
150 struct sched_param schedp;
152 if (sigset == SIGHUP)
154 if (sigset == SIGPIPE)
159 /* set scheduler & priority */
160 if (options.schedule > 1)
162 memset(&schedp, 0, sizeof(schedp));
163 schedp.sched_priority = 0;
164 sched_setscheduler(0, SCHED_OTHER, &schedp);
166 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
167 PDEBUG(DEBUG_LOG, "Signal received: %d\n", sigset);
175 int main(int argc, char *argv[])
178 int lockfd = -1; /* file lock */
179 struct lcr_msg *message;
181 class Endpoint *epoint;
185 char prefix_string[64];
186 struct sched_param schedp;
187 const char *debug_prefix = "alloc";
188 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
189 created_lock = 0, created_signal = 0, created_debug = 0,
191 int idletime = 0, idlecheck = 0;
192 char tracetext[256], lock[128];
196 memset(lcr_fdset, 0, sizeof(lcr_fdset));
199 /* lock LCR process */
200 // pthread_mutex_lock(&mutex_lcr);
206 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
213 printf("Usage: lcr (query | start | fork | rules | route)\n");
214 printf("query = Show available isdn ports.\n");
215 printf("start = Run lcr normally, abort with CTRL+C.\n");
216 printf("fork = Do daemon fork and run as background process.\n");
217 printf("interface = Get help of available interface syntax.\n");
218 printf("rules = Get help of available routing rule syntax.\n");
219 printf("rules [action] = Get individual help for given action.\n");
220 // printf("route = Show current routing as it is parsed.\n");
230 if (pthread_mutex_init(&mutexd, NULL))
232 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
236 // if (pthread_mutex_init(&mutext, NULL))
238 // fprintf(stderr, "cannot create 'trace' mutex\n");
241 // created_mutext = 1;
242 if (pthread_mutex_init(&mutexe, NULL))
244 fprintf(stderr, "cannot create 'PERROR' mutex\n");
250 if (!(strcasecmp(argv[1],"interface")))
258 if (!(strcasecmp(argv[1],"rules")))
268 /* query available isdn ports */
269 if (!(strcasecmp(argv[1],"query")))
271 fprintf(stderr, "-> Using 'misdn_info'\n");
272 system("misdn_info");
278 if (read_options() == 0)
280 PERROR("%s", options_error);
285 if (mISDN_initialize() < 0)
290 /* read ruleset(s) */
291 if (!(ruleset_first = ruleset_parse()))
294 /* set pointer to main ruleset */
295 ruleset_main = getrulesetbyname("main");
298 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
299 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
304 /* query available isdn ports */
305 if (!(strcasecmp(argv[1],"route")))
307 ruleset_debug(ruleset_first);
313 /* do fork in special cases */
314 if (!(strcasecmp(argv[1],"fork")))
324 fprintf(stderr, "Cannot fork!\n");
339 fprintf(stderr, "Cannot fork!\n");
344 printf("LCR: Starting daemon.\n");
350 pidfile = fopen("/var/run/lcr.pid","w");
353 fprintf(pidfile, "%d\n", getpid());
358 if (!!strcasecmp(argv[1],"start"))
363 /* create lock and lock! */
364 SPRINT(lock, "%s/lcr.lock", options.lock);
365 if ((lockfd = open(lock, O_CREAT | O_WRONLY, S_IWUSR)) < 0)
367 fprintf(stderr, "Cannot create lock file: %s\n", lock);
368 fprintf(stderr, "Check options.conf to change to path with permissions for you.\n");
371 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0)
373 if (errno == EWOULDBLOCK)
374 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
375 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
380 /* initialize admin socket */
383 fprintf(stderr, "Unable to initialize admin socket.\n");
387 /* generate alaw / ulaw tables */
388 generate_tables(options.law);
390 /* load tones (if requested) */
391 if (fetch_tones() == 0)
393 fprintf(stderr, "Unable to fetch tones into memory.\n");
399 if (options.gsm && gsm_init())
401 fprintf(stderr, "GSM initialization failed.\n");
407 fprintf(stderr, "GSM is enabled, but not compiled. Use --with-gsm while configure!\n");
412 /* read interfaces and open ports */
413 if (!read_interfaces())
415 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
416 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
420 interface_first = interface_newlist;
421 interface_newlist = NULL;
423 /* locking memory paging */
427 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
437 fprintf(stderr, "Warning: Not enough memory to lock paging.\n");
440 fprintf(stderr, "Warning: No permission to lock paging.\n");
443 fprintf(stderr, "Warning: 'Bad address' while locking paging.\n");
446 fprintf(stderr, "Warning: Unknown error %d while locking paging.\n", errno);
450 /* set real time scheduler & priority */
451 if (options.schedule > 1)
453 memset(&schedp, 0, sizeof(schedp));
454 schedp.sched_priority = options.schedule;
455 ret = sched_setscheduler(0, SCHED_RR, &schedp);
458 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
463 /* signal handlers */
464 signal(SIGINT,sighandler);
465 signal(SIGHUP,sighandler);
466 signal(SIGTERM,sighandler);
467 signal(SIGPIPE,sighandler);
471 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
472 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
473 printf("%s\n", tracetext);
482 if (now_d-last_d > 1.0)
484 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
486 /* all loops must be counted from the beginning since nodes might get freed during handler */
489 //#warning debugging usleep crash
490 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
492 /* handle mISDN messages from kernel */
493 debug_prefix = "ISDN";
496 //#warning debugging usleep crash
497 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
501 /* loop through all port ports and call their handler */
506 debug_prefix = port->p_name;
508 ret = port->handler();
511 if (ret < 0) /* port has been destroyed */
516 /* loop through all epoint and call their handler */
518 epoint = epoint_first;
521 debug_prefix = prefix_string;
522 SPRINT(prefix_string, "ep%ld", epoint->ep_serial);
524 ret = epoint->handler();
527 if (ret < 0) /* epoint has been destroyed */
529 epoint = epoint->next;
532 /* loop through all joins and call their handler */
537 debug_prefix = "join";
539 ret = join->handler();
542 if (ret < 0) /* join has been destroyed */
549 /* process any message */
551 debug_prefix = "message";
552 while ((message = message_get()))
555 switch(message->flow)
558 debug_prefix = "msg port->epoint";
559 epoint = find_epoint_id(message->id_to);
564 epoint->ep_app->ea_message_port(message->id_from, message->type, &message->param);
567 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);
571 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);
576 debug_prefix = "msg epoint->join";
577 join = find_join_id(message->id_to);
580 join->message_epoint(message->id_from, message->type, &message->param);
583 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);
588 debug_prefix = "msg join->epoint";
589 epoint = find_epoint_id(message->id_to);
594 epoint->ep_app->ea_message_join(message->id_from, message->type, &message->param);
597 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);
601 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);
606 debug_prefix = "msg epoint->port";
607 port = find_port_id(message->id_to);
610 port->message_epoint(message->id_from, message->type, &message->param);
614 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);
619 PERROR("Message flow %d unknown.\n", message->flow);
621 message_free(message);
623 debug_prefix = "message";
642 /* check for child to exit (eliminate zombies) */
643 if (waitpid(-1, NULL, WNOHANG) > 0)
645 PDEBUG(DEBUG_EPOINT, "a child process (created by endpoint) has exitted.\n");
649 //#warning debugging usleep crash
650 // debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
652 /* do idle checking */
653 if (idlecheck != now)
655 PDEBUG(DEBUG_IDLETIME, "Idle time : %d%%\n", idletime/10000);
660 /* did we do nothing? so we wait to give time to other processes */
663 // pthread_mutex_unlock(&mutex_lcr); // unlock LCR
664 debug_usleep(4000, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
665 // pthread_mutex_lock(&mutex_lcr); // lock LCR
669 SPRINT(tracetext, "%s terminated", NAME);
670 printf("%s\n", tracetext);
671 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
673 add_trace((char *)"signal", NULL, "%d", quit);
681 /* set scheduler & priority
683 if (options.schedule > 1)
685 memset(&schedp, 0, sizeof(schedp));
686 schedp.sched_priority = options.schedule;
687 sched_setscheduler(0, SCHED_OTHER, &schedp);
692 signal(SIGINT,SIG_DFL);
693 signal(SIGHUP,SIG_DFL);
694 signal(SIGTERM,SIG_DFL);
695 signal(SIGPIPE,SIG_DFL);
698 /* destroy objects */
699 debug_prefix = "free";
715 /* free interfaces */
717 free_interfaces(interface_first);
718 interface_first = NULL;
720 /* close isdn ports */
721 mISDNport_close_all();
726 while ((message = message_get()))
729 message_free(message);
733 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
740 /* free admin socket */
745 flock(lockfd, LOCK_UN);
755 ruleset_free(ruleset_first);
756 ruleset_first = NULL;
760 if (pthread_mutex_destroy(&mutexe))
761 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
762 // if (created_mutext)
763 // if (pthread_mutex_destroy(&mutext))
764 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
766 if (pthread_mutex_destroy(&mutexd))
767 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
769 /* deinitialize mISDN */
771 mISDN_deinitialize();
779 /* display memory leak */
780 #define MEMCHECK(a, b) \
783 SPRINT(tracetext, a, NAME); \
784 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext); \
785 if (ret) add_trace("blocks", NULL, "%d", b); \
787 printf("\n******************************\n\007"); \
788 printf("\nERROR: %d %s\n", b, a); \
789 printf("\n******************************\n"); \
793 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
794 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
795 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
796 MEMCHECK("memory block(s) left (message.c)",mmemuse)
797 MEMCHECK("memory block(s) left (route.c)",rmemuse)
798 MEMCHECK("memory block(s) left (args)",amemuse)
799 MEMCHECK("class(es) left",classuse)
800 MEMCHECK("file descriptor(s) left",fduse)
801 MEMCHECK("file handler(s) left",fhuse)
803 /* unlock LCR process */
804 // pthread_mutex_unlock(&mutex_lcr);
812 /* special debug function to detect buffer overflow
814 int budetect_stop = 0;
815 void budetect(const char *file, int line, const char *function)
819 /* modify this function to detect race-bugs */
820 #warning DID YOU MODIFY THIS FUNCTION TO DETECT THE BUFFER OVERFLOW BUG?
822 class PmISDN *pmisdn;
823 struct mISDNport *mISDNport = mISDNport_first;
829 ii = mISDNport->b_num;
832 if (mISDNport->b_port[i])
837 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_ISDN)
839 pmisdn = (class PmISDN *)port;
840 if (pmisdn->p_isdn_crypt_listen)
842 PERROR_RUNTIME("************************************************\n");
843 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
844 PERROR_RUNTIME("** p_isdn_crypt_listen = %d\n", pmisdn->p_isdn_crypt_listen);
845 PERROR_RUNTIME("************************************************\n");
849 if (port == mISDNport->b_port[i])
854 PERROR_RUNTIME("************************************************\n");
855 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
856 PERROR_RUNTIME("** b_port not in list.\n");
857 PERROR_RUNTIME("************************************************\n");
864 mISDNport = mISDNport->next;