24c0039e8cde517b0ee56a50d47fcfe48301fe2a
[lcr.git] / main.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** Main function                                                             **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13
14 MESSAGES
15
16 double now_d, last_d;
17 time_t now;
18 struct tm *now_tm;
19 struct timeval now_tv;
20 struct timezone now_tz;
21 #define GET_NOW() \
22         { \
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); \
27         }
28
29 #ifdef SOCKET_MISDN
30 FILE *debug_fp = NULL;
31 #else
32 int global_debug = 0;
33 #endif
34 int quit=0;
35
36 #if 0
37 struct lcr_fdset lcr_fdset[FD_SETSIZE];
38 #endif
39
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
44 int memuse = 0;
45 int mmemuse = 0;
46 int cmemuse = 0;
47 int ememuse = 0;
48 int pmemuse = 0;
49 int amemuse = 0;
50 int rmemuse = 0;
51 int classuse = 0;
52 int fduse = 0;
53 int fhuse = 0;
54
55 char *debug_prefix = 0;
56 int debug_count = 0;
57 int last_debug = 0;
58 int debug_newline = 1;
59 int nooutput = 0;
60
61 void debug_usleep(int msec, char *file, int line, int hour, int min, int sec)
62 {
63         usleep(msec);
64 }
65
66 void debug(const char *function, int line, char *prefix, char *buffer)
67 {
68         /* if we have a new debug count, we add a mark */
69         if (last_debug != debug_count)
70         {
71                 last_debug = debug_count;
72                 if (!nooutput)
73                         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);
74 #ifdef SOCKET_MISDN
75                 if (debug_fp)
76                         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);
77 #else
78                 if (options.deb&DEBUG_LOG && global_debug)
79                         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);
80 #endif
81         }
82
83         if (!nooutput)
84         {
85                 if (debug_newline)
86                         printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
87                 if (function)
88                         printf("(in %s() line %d): %s", function, line, buffer);
89                 else
90                         printf("%s", buffer);
91         }
92
93 #ifdef SOCKET_MISDN
94         if (debug_fp)
95 #else
96         if (options.deb&DEBUG_LOG && global_debug)
97 #endif
98         {
99                 if (debug_newline)
100                 {
101                         if (function)
102 #ifdef SOCKET_MISDN
103                                 fprintf(debug_fp, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
104 #else
105                                 dprint(DBGM_MAN, 0, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
106 #endif
107                         else
108 #ifdef SOCKET_MISDN
109                                 fprintf(debug_fp, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
110 #else
111                                 dprint(DBGM_MAN, 0, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
112 #endif
113                 }
114         }
115
116         debug_newline = 0;
117         if (buffer[0])
118                 if (buffer[strlen(buffer)-1] == '\n')
119                         debug_newline = 1;
120 }
121
122
123 void _printdebug(const char *function, int line, unsigned long mask, const char *fmt, ...)
124 {
125         char buffer[4096];
126         va_list args;
127
128         if (!(options.deb & mask))
129                 return;
130         pthread_mutex_lock(&mutexd);
131
132         va_start(args,fmt);
133         VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
134         buffer[sizeof(buffer)-1]=0;
135         va_end(args);
136
137         debug(function, line, debug_prefix, buffer);
138
139         pthread_mutex_unlock(&mutexd);
140 }
141
142 void _printerror(const char *function, int line, const char *fmt, ...)
143 {
144         char buffer[4096];
145         va_list args;
146
147         pthread_mutex_lock(&mutexe);
148
149         va_start(args,fmt);
150         VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
151         buffer[sizeof(buffer)-1]=0;
152         va_end(args);
153
154         if (options.deb)
155                 debug(function, line, "ERROR", buffer);
156         else /* only if we do not debug */
157         {
158                 if (function)
159                         fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
160                 else
161                         fprintf(stderr, "ERROR %s", buffer);
162         }
163
164         pthread_mutex_unlock(&mutexe);
165 }
166
167
168 void sighandler(int sigset)
169 {
170         struct sched_param schedp;
171
172         if (sigset == SIGHUP)
173                 return;
174         if (sigset == SIGPIPE)
175                 return;
176         if (!quit)
177         {
178                 quit=1;
179                 /* set scheduler & priority */
180                 if (options.schedule > 1)
181                 {
182                         memset(&schedp, 0, sizeof(schedp));
183                         schedp.sched_priority = 0;
184                         sched_setscheduler(0, SCHED_OTHER, &schedp);
185                 }
186                 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
187                 PERROR("Signal received: %d\n", sigset);
188         }
189 }
190
191
192 /*
193  * the main
194  */
195 int main(int argc, char *argv[])
196 {
197         int                     ret = -1;
198         int                     lockfd = -1; /* file lock */
199         struct message          *message;
200         class Port              *port;
201         class Endpoint          *epoint;
202         class Join              *join;
203         int                     i;
204         int                     all_idle;
205         char                    prefix_string[64];
206         struct sched_param      schedp;
207         char                    *debug_prefix = "alloc";
208         int                     created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
209                                 created_lock = 0, created_signal = 0, created_debug = 0;
210         int                     idletime = 0, idlecheck = 0;
211         char                    tracetext[256];
212
213 #if 0
214         /* init fdset */
215         memset(lcr_fdset, 0, sizeof(lcr_fdset));
216 #endif
217
218         /* current time */
219         GET_NOW();
220
221         /* show version */
222         printf("\n** %s  Version %s\n\n", NAME, VERSION_STRING);
223
224         /* show options */
225         if (argc <= 1)
226         {
227                 usage:
228                 printf("\n");
229                 printf("Usage: lcr (query | start | fork | rules | route)\n");
230                 printf("query     = Show available isdn ports.\n");
231                 printf("start     = Run lcr normally, abort with CTRL+C.\n");
232                 printf("fork      = Do daemon fork and run as background process.\n");
233                 printf("interface = Get help of available interface syntax.\n");
234                 printf("rules     = Get help of available routing rule syntax.\n");
235                 printf("rules [action] = Get individual help for given action.\n");
236 //              printf("route = Show current routing as it is parsed.\n");
237                 printf("\n");
238                 ret = 999;
239                 goto free;
240         }
241
242         /* init crc */
243         crc_init();
244
245         /* check for root (real or effective) */
246         if (getuid() && geteuid())
247         {
248                 fprintf(stderr, "Please run %s as super-user.\n", NAME);
249                 goto free;
250         }
251
252         /* the mutex init */
253         if (pthread_mutex_init(&mutexd, NULL))
254         {
255                 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
256                 goto free;
257         }
258         created_mutexd = 1;
259 //      if (pthread_mutex_init(&mutext, NULL))
260 //      {
261 //              fprintf(stderr, "cannot create 'trace' mutex\n");
262 //              goto free;
263 //      }
264 //      created_mutext = 1;
265         if (pthread_mutex_init(&mutexe, NULL))
266         {
267                 fprintf(stderr, "cannot create 'PERROR' mutex\n");
268                 goto free;
269         }
270         created_mutexe = 1;
271
272         /* show interface */
273         if (!(strcasecmp(argv[1],"interface")))
274         {
275                 doc_interface();
276                 ret = 0;
277                 goto free;
278         }
279
280         /* show rules */
281         if (!(strcasecmp(argv[1],"rules")))
282         {
283                 if (argc <= 2)
284                         doc_rules(NULL);
285                 else
286                         doc_rules(argv[2]);
287                 ret = 0;
288                 goto free;
289         }
290
291         /* query available isdn ports */
292         if (!(strcasecmp(argv[1],"query")))
293         {
294                 mISDN_port_info();
295                 ret = 0;
296                 goto free;
297         }
298
299         /* read options */
300         if (read_options() == 0)
301                 goto free;
302
303         /* init mISDN */
304         if (mISDN_initialize() < 0)
305                 goto free;
306         created_debug = 1;
307
308         /* read ruleset(s) */
309         if (!(ruleset_first = ruleset_parse()))
310                 goto free;
311
312         /* set pointer to main ruleset */
313         ruleset_main = getrulesetbyname("main");
314         if (!ruleset_main)
315         {
316                 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
317                 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
318                 sleep(2);
319         }
320
321 #if 0
322         /* query available isdn ports */
323         if (!(strcasecmp(argv[1],"route")))
324         {
325                 ruleset_debug(ruleset_first);
326                 ret = 0;
327                 goto free;
328         }
329 #endif
330
331         /* do fork in special cases */
332         if (!(strcasecmp(argv[1],"fork")))
333         {
334                 pid_t pid;
335
336                 /* do daemon fork */
337                 pid = fork();
338
339                 if (pid < 0)
340                 {
341                         fprintf(stderr, "Cannot fork!\n");
342                         goto free;
343                 }
344                 if (pid != 0)
345                 {
346                         exit(0);
347                 }
348                 usleep(200000);
349                 printf("\n");
350                 
351                 /* do second fork */
352                 pid = fork();
353
354                 if (pid < 0)
355                 {
356                         fprintf(stderr, "Cannot fork!\n");
357                         goto free;
358                 }
359                 if (pid != 0)
360                 {
361                         printf("LCR: Starting daemon.\n");
362                         exit(0);
363                 }
364                 nooutput = 1;
365         } else
366         /* if not start */
367         if (!!strcasecmp(argv[1],"start"))
368         {
369                 goto usage;
370         }
371
372         /* create lock and lock! */
373         if ((lockfd = open("/var/run/lcr.lock", O_CREAT, 0)) < 0)
374         {
375                 fprintf(stderr, "Cannot create lock file: /var/run/lcr.lock\n");
376                 goto free;
377         }
378         if (flock(lockfd, LOCK_EX|LOCK_NB) < 0)
379         {
380                 if (errno == EWOULDBLOCK)
381                         fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
382                 else    fprintf(stderr, "Locking process failed: errno=%d\n", errno);
383                 goto free;
384         }
385         created_lock = 1;
386
387         /* initialize admin socket */
388         if (admin_init())
389         {
390                 fprintf(stderr, "Unable to initialize admin socket.\n");
391                 goto free;
392         }
393
394         /* generate alaw / ulaw tables */
395         generate_tables(options.law);
396
397         /* load tones (if requested) */
398         if (fetch_tones() == 0)
399         {
400                 fprintf(stderr, "Unable to fetch tones into memory.\n");
401                 goto free;
402         }
403
404         /* read interfaces and open ports */
405         if (!read_interfaces())
406         {
407                 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
408                 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
409                 goto free;
410         }
411         relink_interfaces();
412         interface_first = interface_newlist;
413         interface_newlist = NULL;
414         
415         /* locking memory paging */
416         i = 0;
417         while(i < 10)
418         {
419                 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
420                         break;
421                 usleep(200000);
422                 i++;
423         }
424         if (i == 10)
425         {
426                 switch(errno)
427                 {
428                         case ENOMEM:
429                         fprintf(stderr, "Not enough memory to lock paging, exitting...\n");
430                         break;
431                         case EPERM:
432                         fprintf(stderr, "No permission to lock paging, exitting...\n");
433                         break;
434                         case EFAULT:
435                         fprintf(stderr, "'Bad address' while locking paging, exitting...\n");
436                         break;
437                         default:
438                         fprintf(stderr, "Unknown error %d while locking paging, exitting...\n", errno);
439                 }
440                 goto free;
441         }
442
443         /* set real time scheduler & priority */
444         if (options.schedule > 1)
445         {
446                 memset(&schedp, 0, sizeof(schedp));
447                 schedp.sched_priority = options.schedule;
448                 ret = sched_setscheduler(0, SCHED_RR, &schedp);
449                 if (ret < 0)
450                 {
451                         PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
452                         goto free;
453                 }
454         }
455
456         /* signal handlers */   
457         signal(SIGINT,sighandler);
458         signal(SIGHUP,sighandler);
459         signal(SIGTERM,sighandler);
460         signal(SIGPIPE,sighandler);
461         created_signal = 1;
462
463         /*** main loop ***/
464         SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
465         start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
466         printf("%s\n", tracetext);
467         end_trace();
468         GET_NOW();
469         quit = 0;
470         while(!quit)
471         {
472                 last_d = now_d;
473                 GET_NOW();
474                 if (now_d-last_d > 1.0)
475                 {
476                         PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
477                 }
478                 /* all loops must be counted from the beginning since nodes might get freed during handler */
479                 all_idle = 1;
480
481 //#warning debugging usleep crash
482 //              debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
483
484                 /* handle mISDN messages from kernel */
485                 debug_prefix = "ISDN";
486                 if (mISDN_handler())
487                         all_idle = 0;
488 //#warning debugging usleep crash
489 //              debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
490
491 BUDETECT
492
493                 /* loop through all port ports and call their handler */
494                 port_again:
495                 port = port_first;
496                 while(port)
497                 {
498                         debug_prefix = port->p_name;
499                         debug_count++;
500                         ret = port->handler();
501                         if (ret)
502                                 all_idle = 0;
503                         if (ret < 0) /* port has been destroyed */
504                                 goto port_again;
505                         port = port->next;
506                 }
507
508                 /* loop through all epoint and call their handler */
509                 epoint_again:
510                 epoint = epoint_first;
511                 while(epoint)
512                 {
513                         debug_prefix = prefix_string;
514                         SPRINT(prefix_string, "ep%ld", epoint->ep_serial);
515                         debug_count++;
516                         ret = epoint->handler();
517                         if (ret)
518                                 all_idle = 0;
519                         if (ret < 0) /* epoint has been destroyed */
520                                 goto epoint_again;
521                         epoint = epoint->next;
522                 }
523
524                 /* loop through all joins and call their handler */
525                 join_again:
526                 join = join_first;
527                 while(join)
528                 {
529                         debug_prefix = "join";
530                         debug_count++;
531                         ret = join->handler();
532                         if (ret)
533                                 all_idle = 0;
534                         if (ret < 0) /* join has been destroyed */
535                                 goto join_again;
536                         join = join->next;
537                 }
538
539                 debug_prefix = 0;
540
541                 /* process any message */
542                 debug_count++;
543                 debug_prefix = "message";
544                 while ((message = message_get()))
545                 {
546                         all_idle = 0;
547                         switch(message->flow)
548                         {
549                                 case PORT_TO_EPOINT:
550                                 debug_prefix = "msg port->epoint";
551                                 epoint = find_epoint_id(message->id_to);
552                                 if (epoint)
553                                 {
554                                         if (epoint->ep_app)
555                                         {
556                                                 epoint->ep_app->ea_message_port(message->id_from, message->type, &message->param);
557                                         } else
558                                         {
559                                                 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);
560                                         }
561                                 } else
562                                 {
563                                         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);
564                                 }
565                                 break;
566
567                                 case EPOINT_TO_JOIN:
568                                 debug_prefix = "msg epoint->join";
569                                 join = find_join_id(message->id_to);
570                                 if (join)
571                                 {
572                                         join->message_epoint(message->id_from, message->type, &message->param);
573                                 } else
574                                 {
575                                         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);
576                                 }
577                                 break;
578
579                                 case JOIN_TO_EPOINT:
580                                 debug_prefix = "msg join->epoint";
581                                 epoint = find_epoint_id(message->id_to);
582                                 if (epoint)
583                                 {
584                                         if (epoint->ep_app)
585                                         {
586                                                 epoint->ep_app->ea_message_join(message->id_from, message->type, &message->param);
587                                         } else
588                                         {
589                                                 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);
590                                         }
591                                 } else
592                                 {
593                                         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);
594                                 }
595                                 break;
596
597                                 case EPOINT_TO_PORT:
598                                 debug_prefix = "msg epoint->port";
599                                 port = find_port_id(message->id_to);
600                                 if (port)
601                                 {
602                                         port->message_epoint(message->id_from, message->type, &message->param);
603 BUDETECT
604                                 } else
605                                 {
606                                         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);
607                                 }
608                                 break;
609
610                                 default:
611                                 PERROR("Message flow %d unknown.\n", message->flow);
612                         }
613                         message_free(message);
614                         debug_count++;
615                         debug_prefix = "message";
616                 }
617 BUDETECT
618
619                 /* handle socket */
620                 if (admin_handle())
621                         all_idle = 0;
622 BUDETECT
623
624 #if 0
625                 /* check for child to exit (eliminate zombies) */
626                 if (waitpid(-1, NULL, WNOHANG) > 0)
627                 {
628                         PDEBUG(DEBUG_EPOINT, "a child process (created by endpoint) has exitted.\n");
629                         all_idle = 0;
630                 }
631 #endif
632 //#warning debugging usleep crash
633 //              debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
634
635                 /* do idle checking */
636                 if (idlecheck != now)
637                 {
638                         PDEBUG(DEBUG_IDLETIME, "Idle time : %d%%\n", idletime/10000);
639                         idletime = 0;
640                         idlecheck = now;
641                 }
642
643                 /* did we do nothing? so we wait to give time to other processes */
644                 if (all_idle)
645                 {
646                         debug_usleep(4000, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
647                         idletime += 4000;
648                 }
649         }
650         SPRINT(tracetext, "%s terminated", NAME);
651         printf("%s\n", tracetext);
652         start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
653         if (ret)
654                 add_trace("error", NULL, "%d", ret);
655         end_trace();
656         ret=0;
657
658         /* free all */
659 free:
660
661
662         /* set scheduler & priority
663          */
664         if (options.schedule > 1)
665         {
666                 memset(&schedp, 0, sizeof(schedp));
667                 schedp.sched_priority = options.schedule;
668                 sched_setscheduler(0, SCHED_OTHER, &schedp);
669         }
670         /* reset signals */
671         if (created_signal)
672         {
673                 signal(SIGINT,SIG_DFL);
674                 signal(SIGHUP,SIG_DFL);
675                 signal(SIGTERM,SIG_DFL);
676                 signal(SIGPIPE,SIG_DFL);
677         }
678
679         /* destroy objects */
680         debug_prefix = "free";
681
682         while(port_first)
683         {
684                 debug_count++;
685                 delete port_first;
686         }
687         while(epoint_first)
688         {
689                 debug_count++;
690                 delete epoint_first;
691         }
692         epoint_first = NULL;
693         debug_count++;
694         join_free();
695
696         /* free interfaces */
697         if (interface_first)
698                 free_interfaces(interface_first);
699         interface_first = NULL;
700
701         /* close isdn ports */
702         mISDNport_close_all();
703
704         /* flush messages */
705         debug_count++;
706         i = 0;
707         while ((message = message_get()))
708         {
709                 i++;
710                 message_free(message);
711         }
712         if (i)
713         {
714                 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
715         }
716
717         /* free tones */
718         if (toneset_first)
719                 free_tones();
720
721         /* free admin socket */
722         admin_cleanup();
723
724         /* close lock */
725         if (created_lock)
726                 flock(lockfd, LOCK_UN);
727         if (lockfd >= 0)
728                 close(lockfd);
729
730         /* free rulesets */
731         if (ruleset_first)
732                 ruleset_free(ruleset_first);
733         ruleset_first = NULL;
734
735         /* free mutex */
736         if (created_mutexe)
737                 if (pthread_mutex_destroy(&mutexe))
738                         fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
739 //      if (created_mutext)
740 //              if (pthread_mutex_destroy(&mutext))
741 //                      fprintf(stderr, "cannot destroy 'trace' mutex\n");
742         if (created_mutexd)
743                 if (pthread_mutex_destroy(&mutexd))
744                         fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
745
746         /* deinitialize mISDN */
747         mISDN_deinitialize();
748
749         /* display memory leak */
750 #define MEMCHECK(a, b) \
751         if (b) \
752         { \
753                 SPRINT(tracetext, a, NAME); \
754                 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext); \
755                 if (ret) add_trace("blocks", NULL, "%d", b); \
756                 end_trace(); \
757                 printf("\n******************************\n\007"); \
758                 printf("\nERROR: %d %s\n", b, a); \
759                 printf("\n******************************\n"); \
760                 ret = -1; \
761         }
762         MEMCHECK("",memuse)
763         MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
764         MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
765         MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
766         MEMCHECK("memory block(s) left (message.c)",mmemuse)
767         MEMCHECK("memory block(s) left (route.c)",rmemuse)
768         MEMCHECK("memory block(s) left (args)",amemuse)
769         MEMCHECK("class(es) left",classuse)
770         MEMCHECK("file descriptor(s) left",fduse)
771         MEMCHECK("file handler(s) left",fhuse)
772
773         /* take me out */
774         return(ret);
775 }
776
777
778 #ifdef BUDETECT_DEF
779 /* special debug function to detect buffer overflow
780  */
781 int budetect_stop = 0;
782 void budetect(const char *file, int line, char *function)
783 {
784         if (budetect_stop)
785                 return;
786         /* modify this function to detect race-bugs */
787 #warning DID YOU MODIFY THIS FUNCTION TO DETECT THE BUFFER OVERFLOW BUG?
788         class Port *port;
789         class PmISDN *pmisdn;
790         struct mISDNport *mISDNport = mISDNport_first;
791         int i, ii;
792
793         while(mISDNport)
794         {
795                 i = 0;
796                 ii = mISDNport->b_num;
797                 while(i < ii)
798                 {
799                         if (mISDNport->b_port[i])
800                         {
801                                 port = port_first;
802                                 while(port)
803                                 {
804                                         if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_ISDN)
805                                         {
806                                                 pmisdn = (class PmISDN *)port;
807                                                 if (pmisdn->p_isdn_crypt_listen)
808                                                 {
809                                                         PERROR_RUNTIME("************************************************\n");
810                                                         PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
811                                                         PERROR_RUNTIME("** p_isdn_crypt_listen = %d\n", pmisdn->p_isdn_crypt_listen);
812                                                         PERROR_RUNTIME("************************************************\n");
813                                                         budetect_stop = 1;
814                                                 }
815                                         }
816                                         if (port == mISDNport->b_port[i])
817                                                 break;
818                                         port = port->next;
819                                         if (!port)
820                                         {
821                                                 PERROR_RUNTIME("************************************************\n");
822                                                 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
823                                                 PERROR_RUNTIME("** b_port not in list.\n");
824                                                 PERROR_RUNTIME("************************************************\n");
825                                                 budetect_stop = 1;
826                                         }
827                                 }
828                         }
829                         i++;
830                 }
831                 mISDNport = mISDNport->next;
832         }
833
834 }
835 #endif
836