1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
13 #ifdef PACKAGE_VERSION
14 #undef PACKAGE_VERSION
20 struct timeval now_tv;
21 struct timezone now_tz;
24 gettimeofday(&now_tv, &now_tz); \
25 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
28 FILE *debug_fp = NULL;
32 struct lcr_fdset lcr_fdset[FD_SETSIZE];
35 pthread_mutex_t mutexd; // debug output mutex
36 pthread_mutex_t mutext; // trace output mutex
37 pthread_mutex_t mutexe; // error output mutex
38 //pthread_mutex_t mutex_lcr; // lcr process mutex
53 int debug_newline = 1;
56 void debug(const char *file, const char *function, int line, const char *prefix, char *buffer)
61 /* if we have a new debug count, we add a mark */
62 if (last_debug != debug_count) {
63 last_debug = debug_count;
65 now_tm = localtime(&now);
67 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);
69 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);
74 printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
76 printf("(in %s/%s() line %d): %s", file, function, line, buffer);
84 fprintf(debug_fp, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
86 fprintf(debug_fp, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
93 if (buffer[strlen(buffer)-1] == '\n')
98 void _printdebug(const char *file, const char *function, int line, unsigned int mask, const char *fmt, ...)
103 if (!(options.deb & mask))
105 pthread_mutex_lock(&mutexd);
108 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
109 buffer[sizeof(buffer)-1]=0;
112 debug(file, function, line, "DEBUG", buffer);
114 pthread_mutex_unlock(&mutexd);
117 void _printerror(const char *file, const char *function, int line, const char *fmt, ...)
122 pthread_mutex_lock(&mutexe);
125 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
126 buffer[sizeof(buffer)-1]=0;
130 debug(file, function, line, "ERROR", buffer);
131 else { /* only if we do not debug */
133 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
135 fprintf(stderr, "ERROR %s", buffer);
138 pthread_mutex_unlock(&mutexe);
142 void sighandler(int sigset)
144 struct sched_param schedp;
146 if (sigset == SIGHUP)
148 if (sigset == SIGPIPE)
150 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
151 PDEBUG(DEBUG_LOG, "Signal received: %d\n", sigset);
153 signal(SIGINT,SIG_DFL);
154 signal(SIGHUP,SIG_DFL);
155 signal(SIGTERM,SIG_DFL);
156 signal(SIGPIPE,SIG_DFL);
159 /* set scheduler & priority */
160 if (options.schedule > 1) {
161 memset(&schedp, 0, sizeof(schedp));
162 schedp.sched_priority = 0;
163 sched_setscheduler(0, SCHED_OTHER, &schedp);
172 int main(int argc, char *argv[])
175 double now_d, last_d;
179 int lockfd = -1; /* file lock */
180 struct lcr_msg *message;
182 struct sched_param schedp;
183 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
184 created_lock = 0, created_signal = 0, created_message = 0;
186 int created_misdn = 0;
188 char tracetext[256], lock[128];
189 char options_error[256];
193 memset(lcr_fdset, 0, sizeof(lcr_fdset));
196 /* lock LCR process */
197 // pthread_mutex_lock(&mutex_lcr);
200 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
206 printf("Usage: lcr (query | start | fork | rules | route)\n");
207 printf("query = Show available isdn ports.\n");
208 printf("start = Run lcr normally, abort with CTRL+C.\n");
209 printf("fork = Do daemon fork and run as background process.\n");
210 printf("interface = Get help of available interface syntax.\n");
211 printf("rules = Get help of available routing rule syntax.\n");
212 printf("rules [action] = Get individual help for given action.\n");
213 // printf("route = Show current routing as it is parsed.\n");
227 vootp_loglevel(VOOTP_LOGL_INFO);
231 if (pthread_mutex_init(&mutexd, NULL)) {
232 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
236 // if (pthread_mutex_init(&mutext, NULL)) {
237 // fprintf(stderr, "cannot create 'trace' mutex\n");
240 // created_mutext = 1;
241 if (pthread_mutex_init(&mutexe, NULL)) {
242 fprintf(stderr, "cannot create 'PERROR' mutex\n");
248 if (!(strcasecmp(argv[1],"interface"))) {
255 if (!(strcasecmp(argv[1],"rules"))) {
264 /* query available isdn ports */
265 if (!(strcasecmp(argv[1],"query"))) {
266 int __attribute__((__unused__)) rc;
267 fprintf(stderr, "-> Using 'misdn_info'\n");
268 rc = system("misdn_info");
274 if (read_options(options_error) == 0) {
275 PERROR("%s", options_error);
281 if (mISDN_initialize() < 0)
286 /* read ruleset(s) */
287 if (!(ruleset_first = ruleset_parse()))
290 /* set pointer to main ruleset */
291 ruleset_main = getrulesetbyname("main");
293 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
294 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
299 /* query available isdn ports */
300 if (!(strcasecmp(argv[1],"route"))) {
301 ruleset_debug(ruleset_first);
307 /* do fork in special cases */
308 if (!(strcasecmp(argv[1],"fork"))) {
316 fprintf(stderr, "Cannot fork!\n");
329 fprintf(stderr, "Cannot fork!\n");
333 printf("LCR: Starting daemon.\n");
339 SPRINT(lock, "%s/lcr.pid", options.lock);
340 pidfile = fopen(lock,"w");
342 fprintf(pidfile, "%d\n", getpid());
345 fprintf(stderr, "Failed to create PID file: %s\n", lock);
348 if (!!strcasecmp(argv[1],"start")) {
352 /* create lock and lock! */
353 SPRINT(lock, "%s/lcr.lock", options.lock);
354 if ((lockfd = open(lock, O_CREAT | O_WRONLY, S_IWUSR)) < 0) {
355 fprintf(stderr, "Cannot create lock file: %s\n", lock);
356 fprintf(stderr, "Check options.conf to change to path with permissions for you.\n");
359 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
360 if (errno == EWOULDBLOCK)
361 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
362 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
367 /* initialize admin socket */
369 fprintf(stderr, "Unable to initialize admin socket.\n");
373 /* generate alaw / ulaw tables */
374 generate_tables(options.law);
377 /* init SIP globals */
382 /* init ss5 sine tables */
387 /* load tones (if requested) */
388 if (fetch_tones() == 0) {
389 fprintf(stderr, "Unable to fetch tones into memory.\n");
393 #if defined WITH_GSM_BS || defined WITH_GSM_MS
396 fprintf(stderr, "GSM initialization failed.\n");
402 fprintf(stderr, "GSM BS initialization failed.\n");
408 fprintf(stderr, "GSM MS initialization failed.\n");
413 /* read interfaces and open ports */
414 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 */
426 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
434 fprintf(stderr, "Warning: Not enough memory to lock paging.\n");
437 fprintf(stderr, "Warning: No permission to lock paging.\n");
440 fprintf(stderr, "Warning: 'Bad address' while locking paging.\n");
443 fprintf(stderr, "Warning: Unknown error %d while locking paging.\n", errno);
447 /* set real time scheduler & priority */
448 if (options.schedule > 1) {
449 memset(&schedp, 0, sizeof(schedp));
450 schedp.sched_priority = options.schedule;
451 ret = sched_setscheduler(0, SCHED_RR, &schedp);
453 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
458 /* signal handlers */
459 signal(SIGINT,sighandler);
460 signal(SIGHUP,sighandler);
461 signal(SIGTERM,sighandler);
462 signal(SIGPIPE,sighandler);
470 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
471 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
472 printf("%s\n", tracetext);
482 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 /* must be processed after all queues, so they are empty */
489 if (select_main(1, NULL, NULL, NULL))
496 if (options.polling || any_sip_interface) {
498 if (options.polling) {
500 if (!select_main(1, NULL, NULL, NULL)) {
502 /* FIXME: check if work was done */
508 select_main(0, NULL, NULL, NULL);
511 SPRINT(tracetext, "%s terminated", NAME);
512 printf("%s\n", tracetext);
513 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
515 add_trace((char *)"signal", NULL, "%d", quit);
517 add_trace((char *)"errno", NULL, "%d", quit);
524 /* set scheduler & priority
526 if (options.schedule > 1) {
527 memset(&schedp, 0, sizeof(schedp));
528 schedp.sched_priority = options.schedule;
529 sched_setscheduler(0, SCHED_OTHER, &schedp);
532 if (created_signal) {
533 signal(SIGINT,SIG_DFL);
534 signal(SIGHUP,SIG_DFL);
535 signal(SIGTERM,SIG_DFL);
536 signal(SIGPIPE,SIG_DFL);
539 /* destroy objects */
544 while(epoint_first) {
553 /* close isdn ports */
554 mISDNport_close_all();
557 /* free interfaces */
559 free_interfaces(interface_first);
560 interface_first = NULL;
565 while ((message = message_get())) {
567 message_free(message);
570 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
581 /* free admin socket */
586 flock(lockfd, LOCK_UN);
597 ruleset_free(ruleset_first);
598 ruleset_first = NULL;
602 if (pthread_mutex_destroy(&mutexe))
603 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
604 // if (created_mutext)
605 // if (pthread_mutex_destroy(&mutext))
606 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
608 if (pthread_mutex_destroy(&mutexd))
609 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
612 /* deinitialize mISDN */
614 mISDN_deinitialize();
624 #if defined WITH_GSM_BS || defined WITH_GSM_MS
629 /* cleanup SIP globals */
633 /* display memory leak */
634 #define MEMCHECK(a, b) \
636 SPRINT(tracetext, a, NAME); \
637 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext); \
638 if (ret) add_trace("blocks", NULL, "%d", b); \
640 printf("\n******************************\n\007"); \
641 printf("\nERROR: %d %s\n", b, a); \
642 printf("\n******************************\n"); \
646 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
647 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
648 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
649 MEMCHECK("memory block(s) left (message.c)",mmemuse)
650 MEMCHECK("memory block(s) left (route.c)",rmemuse)
651 MEMCHECK("memory block(s) left (args)",amemuse)
652 MEMCHECK("class(es) left",classuse)
653 MEMCHECK("file descriptor(s) left",fduse)
654 MEMCHECK("file handler(s) left",fhuse)
656 /* unlock LCR process */
657 // pthread_mutex_unlock(&mutex_lcr);