fbd0015a5980a9881ed67f6e3608409a1b39ad91
[lcr.git] / lcradmin.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** Administration tool                                                       **
9 **                                                                           **
10 \*****************************************************************************/
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/types.h>
16 #include <time.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <sys/time.h>
23 #include <errno.h>
24 #include <curses.h>
25 #include "macro.h"
26 #include "join.h"
27 #include "joinpbx.h"
28 #include "extension.h"
29 #include "message.h"
30 #include "lcrsocket.h"
31 #include "cause.h"
32
33 #define LTEE {addch(ACS_LTEE);addch(ACS_HLINE);addch(ACS_HLINE);}
34 #define LLCORNER {addch(ACS_LLCORNER);addch(ACS_HLINE);addch(ACS_HLINE);}
35 #define VLINE {addch(ACS_VLINE);addstr("  ");}
36 #define EMPTY {addstr("   ");}
37 //char rotator[] = {'-', '\\', '|', '/'};
38 int     lastlines, lastcols;
39 int     show_interfaces = 2,
40         show_calls = 1,
41         show_log = 1;
42
43 enum {
44         MODE_STATE,
45         MODE_INTERFACE,
46         MODE_ROUTE,
47         MODE_DIAL,
48         MODE_RELEASE,
49         MODE_UNBLOCK,
50         MODE_BLOCK,
51         MODE_UNLOAD,
52         MODE_TESTCALL,
53         MODE_TRACE,
54 };
55
56 char *text_interfaces[] = {
57         "off",
58         "brief",
59         "active channels",
60         "all channels",
61 };
62
63 char *text_calls[] = {
64         "off",
65         "brief",
66         "structured",
67 };
68
69 char    red = 1,
70         green = 2,
71         yellow = 3,
72         blue = 4,
73         mangenta = 5,
74         cyan = 6,
75         white = 7;
76
77 #define LOGLINES 128
78 char logline[LOGLINES][512];
79 unsigned int logcur = 0;
80 int logfh = -1;
81 char logfile[128];
82
83 /*
84  * curses
85  */
86 void init_curses(void)
87 {
88         /* init curses */
89         initscr(); cbreak(); noecho();
90         start_color();
91         nodelay(stdscr, TRUE);
92         if (COLOR_PAIRS>=8 && COLORS>=8)
93         {
94                 init_pair(1,1,0);
95                 init_pair(2,2,0);
96                 init_pair(3,3,0);
97                 init_pair(4,4,0);
98                 init_pair(5,5,0);
99                 init_pair(6,6,0);
100                 init_pair(7,7,0);
101         }
102         lastlines = LINES;
103         lastcols = COLS;
104 }
105
106 void cleanup_curses(void)
107 {
108         endwin();
109 }
110
111 void color(int color)
112 {
113         if (COLOR_PAIRS>=8 && COLORS>=8)
114                 attrset(COLOR_PAIR(color));
115 }
116
117 /*
118  * permanently show current state using ncurses
119  */
120 int debug_port(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
121 {
122         char buffer[256];
123
124         color(white);
125         addstr("PORT:");
126         color(yellow);
127         SPRINT(buffer,"%s(%d)", m[i].u.p.name,m[i].u.p.serial);
128         addstr(buffer);
129         color(cyan);
130         addstr(" state=");
131         switch (m[i].u.p.state)
132         {
133                 case ADMIN_STATE_IDLE:
134                 color(red);
135                 addstr("'idle'");
136                 break;
137                 case ADMIN_STATE_IN_SETUP:
138                 color(red);
139                 addstr("'in << setup'");
140                 break;
141                 case ADMIN_STATE_OUT_SETUP:
142                 color(red);
143                 addstr("'out >> setup'");
144                 break;
145                 case ADMIN_STATE_IN_OVERLAP:
146                 color(yellow);
147                 addstr("'in << overlap'");
148                 break;
149                 case ADMIN_STATE_OUT_OVERLAP:
150                 color(yellow);
151                 addstr("'out >> overlap'");
152                 break;
153                 case ADMIN_STATE_IN_PROCEEDING:
154                 color(mangenta);
155                 addstr("'in << proc'");
156                 break;
157                 case ADMIN_STATE_OUT_PROCEEDING:
158                 color(mangenta);
159                 addstr("'out >> proc'");
160                 break;
161                 case ADMIN_STATE_IN_ALERTING:
162                 color(cyan);
163                 addstr("'in << alert'");
164                 break;
165                 case ADMIN_STATE_OUT_ALERTING:
166                 color(cyan);
167                 addstr("'out >> alert'");
168                 break;
169                 case ADMIN_STATE_CONNECT:
170                 color(white);
171                 addstr("'connect'");
172                 break;
173                 case ADMIN_STATE_IN_DISCONNECT:
174                 color(blue);
175                 addstr("'in  << disc'");
176                 break;
177                 case ADMIN_STATE_OUT_DISCONNECT:
178                 color(blue);
179                 addstr("'out >> disc'");
180                 break;
181                 default:
182                 color(blue);
183                 addstr("'--NONE--'");
184         }
185
186         if (m[i].u.p.isdn)
187         {       
188                 color(cyan);
189                 addstr(" bchannel=");
190                 color(white);
191                 SPRINT(buffer,"%d", m[i].u.p.isdn_chan);
192                 addstr(buffer);
193                 if (m[i].u.p.isdn_ces >= 0)
194                 {
195                         color(cyan);
196                         addstr(" ces=");
197                         color(yellow);
198                         SPRINT(buffer, "%d", m[i].u.p.isdn_ces);
199                         addstr(buffer);
200                 }
201                 if (m[i].u.p.isdn_hold)
202                 {
203                         color(red);
204                         addstr(" hold");
205                 }
206         }
207
208         return(line);
209 }
210 int debug_epoint(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
211 {
212         unsigned int epoint = m[i].u.e.serial;
213         char buffer[256];
214         unsigned char c;
215         int j, jj;
216         int ltee;
217
218         color(white);
219         SPRINT(buffer,"EPOINT(%d)", epoint);
220         addstr(buffer);
221         color(cyan);
222         addstr(" state=");
223         switch (m[i].u.e.state)
224         {
225                 case ADMIN_STATE_IDLE:
226                 color(red);
227                 addstr("'idle'");
228                 break;
229                 case ADMIN_STATE_IN_SETUP:
230                 color(red);
231                 addstr("'in << setup'");
232                 break;
233                 case ADMIN_STATE_OUT_SETUP:
234                 color(red);
235                 addstr("'out >> setup'");
236                 break;
237                 case ADMIN_STATE_IN_OVERLAP:
238                 color(yellow);
239                 addstr("'in << overlap'");
240                 break;
241                 case ADMIN_STATE_OUT_OVERLAP:
242                 color(yellow);
243                 addstr("'out >> overlap'");
244                 break;
245                 case ADMIN_STATE_IN_PROCEEDING:
246                 color(mangenta);
247                 addstr("'in << proc'");
248                 break;
249                 case ADMIN_STATE_OUT_PROCEEDING:
250                 color(mangenta);
251                 addstr("'out >> proc'");
252                 break;
253                 case ADMIN_STATE_IN_ALERTING:
254                 color(cyan);
255                 addstr("'in << alert'");
256                 break;
257                 case ADMIN_STATE_OUT_ALERTING:
258                 color(cyan);
259                 addstr("'out >> alert'");
260                 break;
261                 case ADMIN_STATE_CONNECT:
262                 color(white);
263                 addstr("'connect'");
264                 break;
265                 case ADMIN_STATE_IN_DISCONNECT:
266                 color(blue);
267                 addstr("'in  << disc'");
268                 break;
269                 case ADMIN_STATE_OUT_DISCONNECT:
270                 color(blue);
271                 addstr("'out >> disc'");
272                 break;
273                 default:
274                 color(blue);
275                 addstr("'--NONE--'");
276         }
277         if (m[i].u.e.terminal[0])
278         {
279                 color(cyan);
280                 addstr(" terminal=");
281                 color(green);
282                 addstr(m[i].u.e.terminal);
283         }
284         color(white);
285         SPRINT(buffer, " %s", m[i].u.e.callerid);
286         addstr(buffer);
287         color(cyan);
288         addstr("->");
289         color(white);
290         addstr(m[i].u.e.dialing);
291         if (m[i].u.e.action[0])
292         {
293                 color(cyan);
294                 addstr(" action=");
295                 color(yellow);
296                 addstr(m[i].u.e.action);
297         }
298         if (m[i].u.e.park)
299         {
300                 color(cyan);
301                 addstr(" park="); /* 9 digits */
302                 color(green);
303                 UCPY(buffer, "\""); /* 9 digits */
304                 j = 0;
305                 jj = m[i].u.e.park_len;
306                 while(j < jj)
307                 {
308                         c = m[i].u.e.park_callid[j];
309                         if (c >= 32 && c < 127 && c != '[')
310                         {
311                                 SCCAT(buffer, c);
312                         } else
313                                 UPRINT(buffer+strlen(buffer), "[%02x]", c);
314                         j++;
315                 }
316                 SCAT(buffer, "\"");
317                 addstr(buffer);
318         } else
319         {
320                 color(red);
321                 switch(m[i].u.e.rx_state)
322                 {
323                         case NOTIFY_STATE_SUSPEND:
324                         addstr(" in=suspend");
325                         break;
326                         case NOTIFY_STATE_HOLD:
327                         addstr(" in=hold");
328                         break;
329                         case NOTIFY_STATE_CONFERENCE:
330                         addstr(" in=conference");
331                         break;
332                 }
333                 switch(m[i].u.e.tx_state)
334                 {
335                         case NOTIFY_STATE_SUSPEND:
336                         addstr(" out=suspend");
337                         break;
338                         case NOTIFY_STATE_HOLD:
339                         addstr(" out=hold");
340                         break;
341                         case NOTIFY_STATE_CONFERENCE:
342                         addstr(" out=conference");
343                         break;
344                 }
345         }
346         if (m[i].u.e.crypt)
347         {
348                 color(cyan);
349                 addstr(" crypt=");
350                 if (m[i].u.e.crypt) /* crypt on */
351                 {
352                         color(green);
353                         addstr("active");
354                 } else
355                 {
356                         color(yellow);
357                         addstr("pending");
358                 }
359         }
360         /* loop all related ports */
361         ltee = 0;
362         j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins+msg->u.s.epoints;
363         jj = j + msg->u.s.ports;
364         while(j < jj)
365         {
366                 if (m[j].u.p.epoint == epoint)
367                 {
368                         color(cyan);
369                         move(++line>1?line:1, 1);
370                         if (vline)
371                                 VLINE
372                         else
373                                 EMPTY
374                         move(line>1?line:1, 5);
375                         LTEE
376                         ltee = line;
377                         move(line>1?line:1, 8);
378                         if (line+2 >= LINES) break;
379                         line = debug_port(msg, m, line, j, vline);
380                         if (line+2 >= LINES) break;
381                 }
382                 j++;
383         }
384         if (ltee)
385         {
386                 color(cyan);
387                 move(ltee>1?line:1, 5);
388                 LLCORNER
389         }
390
391         return(line);
392 }
393 int debug_join(struct admin_message *msg, struct admin_message *m, int line, int i)
394 {
395         unsigned int    join = m[i].u.j.serial;
396         char            buffer[256];
397         int             j, jj;
398
399         color(white);
400         SPRINT(buffer,"JOIN(%d)", join);
401         addstr(buffer);
402         if (m[i].u.j.partyline)
403         {
404                 color(cyan);
405                 addstr(" partyline=");
406                 color(white);
407                 SPRINT(buffer, "%d\n", m[i].u.j.partyline);
408                 addstr(buffer);
409         }
410         if (m[i].u.j.remote[0])
411         {
412                 color(cyan);
413                 addstr(" remote=");
414                 color(white);
415                 SPRINT(buffer, "%s\n", m[i].u.j.remote);
416                 addstr(buffer);
417         }
418         /* find number of epoints */
419         j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins;
420         jj = j + msg->u.s.epoints;
421         i = 0;
422         while(j < jj)
423         {
424                 if (m[j].u.e.join == join)
425                         i++;
426                 j++;
427         }
428         /* loop all related endpoints */
429         j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins;
430         jj = j + msg->u.s.epoints;
431         while(j < jj)
432         {
433                 if (m[j].u.e.join == join)
434                 {
435                         i--;
436                         move(++line>1?line:1, 1);
437                         color(cyan);
438                         if (i)
439                                 LTEE
440                         else
441                                 LLCORNER
442                         move(line>1?line:1, 4);
443                         if (line+2 >= LINES) break;
444                         line = debug_epoint(msg, m, line, j, i?1:0);
445                         if (line+2 >= LINES) break;
446                 }
447                 j++;
448         }
449
450         return(line);
451 }
452 char *admin_state(int sock, char *argv[])
453 {
454         struct admin_message    msg,
455                                 *m;
456         char                    buffer[512],
457                                 *p;
458         int                     line, offset = 0, hoffset = 0;
459         int                     i, ii, j, jj, k;
460         unsigned int            l, ll;
461         int                     num;
462         int                     len;
463         int                     off;
464         int                     ltee;
465         int                     anything;
466         int                     enter = 0;
467         char                    enter_string[128] = "", ch;
468         fd_set                  select_rfds;
469         struct timeval          select_tv;
470
471         /* flush logfile name */
472         logfile[0] = '\0';
473
474         /* init curses */
475         init_curses();
476
477         again:
478         /* send reload command */
479         memset(&msg, 0, sizeof(msg));
480         msg.message = ADMIN_REQUEST_STATE;
481 //      printf("sizeof=%d\n",sizeof(msg));fflush(stdout);
482         if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
483         {
484                 cleanup_curses();
485                 return("Broken pipe while sending command.");
486         }
487
488         /* receive response */
489         if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
490         {
491                 cleanup_curses();
492                 return("Broken pipe while receiving response.");
493         }
494
495         if (msg.message != ADMIN_RESPONSE_STATE)
496         {
497                 cleanup_curses();
498                 return("Response not valid. Expecting state response.");
499         }
500         num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
501         m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
502         off=0;
503         if (num)
504         {
505                 readagain:
506                 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off)
507                 {
508                         if (len <= 0) {
509                                 FREE(m, 0);
510         //                      fprintf(stderr, "got=%d expected=%d\n", i, num*sizeof(struct admin_message));
511                                 cleanup_curses();
512                                 return("Broken pipe while receiving state infos.");
513                         }
514                         if (len < num*(int)sizeof(struct admin_message))
515                         {
516                                 off+=len;
517                                 goto readagain;
518                         }
519                 }
520         }
521         j = 0;
522         i = 0;
523 //      fprintf("getting =%d interfaces\n", msg.u.s.interfaces);
524         while(i < msg.u.s.interfaces)
525         {
526 //              fprintf(stderr, "j=%d message=%d\n", j, m[j].message);
527                 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE)
528                 {
529                         FREE(m, 0);
530                         cleanup_curses();
531                         return("Response not valid. Expecting interface information.");
532                 }
533                 i++;
534                 j++;
535         }
536         i = 0;
537         while(i < msg.u.s.remotes)
538         {
539                 if (m[j].message != ADMIN_RESPONSE_S_REMOTE)
540                 {
541                         FREE(m, 0);
542                         cleanup_curses();
543                         return("Response not valid. Expecting remote application information.");
544                 }
545                 i++;
546                 j++;
547         }
548         i = 0;
549         while(i < msg.u.s.joins)
550         {
551                 if (m[j].message != ADMIN_RESPONSE_S_JOIN)
552                 {
553                         FREE(m, 0);
554                         cleanup_curses();
555                         return("Response not valid. Expecting join information.");
556                 }
557                 i++;
558                 j++;
559         }
560         i = 0;
561         while(i < msg.u.s.epoints)
562         {
563                 if (m[j].message != ADMIN_RESPONSE_S_EPOINT)
564                 {
565                         FREE(m, 0);
566                         cleanup_curses();
567                         return("Response not valid. Expecting endpoint information.");
568                 }
569                 i++;
570                 j++;
571         }
572         i = 0;
573         while(i < msg.u.s.ports)
574         {
575                 if (m[j].message != ADMIN_RESPONSE_S_PORT)
576                 {
577                         FREE(m, 0);
578                         cleanup_curses();
579                         return("Response not valid. Expecting port information.");
580                 }
581                 i++;
582                 j++;
583         }
584         // now j is the number of message blocks
585
586         /* display start */
587         erase();
588
589         line = 1-offset; 
590
591         /* change log */
592         if (!!strcmp(logfile, msg.u.s.logfile))
593         {
594                 SCPY(logfile, msg.u.s.logfile);
595                 if (logfh >= 0)
596                         close(logfh);
597                 i = 0;
598                 ii = LOGLINES;
599                 while(i < ii)
600                 {
601                         logline[i][0] = '~';
602                         logline[i][1] = '\0';
603                         i++;
604                 }
605                 logcur = 0;
606                 logfh = open(logfile, O_RDONLY|O_NONBLOCK);
607                 if (logfh >= 0)
608                 {
609                         /* seek at the end -8000 chars */
610                         lseek(logfh, -8000, SEEK_END);
611                         /* if not at the beginning, read until endofline */
612                         logline[logcur % LOGLINES][0] = '\0';
613                         l = read(logfh, logline[logcur % LOGLINES], sizeof(logline[logcur % LOGLINES])-1);
614                         if (l > 0)
615                         {
616                                 /* read first line and skip junk */
617                                 logline[logcur % LOGLINES][l] = '\0';
618                                 if ((p = strchr(logline[logcur % LOGLINES],'\n')))
619                                 {
620                                         logcur++;
621                                         SCPY(logline[logcur % LOGLINES], p+1);
622                                         SCPY(logline[(logcur-1) % LOGLINES], "...");
623                                 }
624                                 goto finish_line;
625                         }
626                 }
627         }
628
629         /* read log */
630         if (logfh >= 0)
631         {
632                 while(42)
633                 {
634                         ll = strlen(logline[logcur % LOGLINES]);
635                         l = read(logfh, logline[logcur % LOGLINES]+ll, sizeof(logline[logcur % LOGLINES])-ll-1);
636                         if (l<=0)
637                                 break;
638                         logline[logcur % LOGLINES][ll+l] = '\0';
639                         finish_line:
640                         /* put data to lines */
641                         while ((p = strchr(logline[logcur % LOGLINES],'\n')))
642                         {
643                                 *p = '\0';
644                                 logcur++;
645                                 SCPY(logline[logcur % LOGLINES], p+1);
646                         }
647                         /* if line is full without return, go next line */
648                         if (strlen(logline[logcur % LOGLINES]) == sizeof(logline[logcur % LOGLINES])-1)
649                         {
650                                 logcur++;
651                                 logline[logcur % LOGLINES][0] = '\0';
652                         }
653                 }
654         }
655
656         /* display interfaces */
657         if (show_interfaces > 0)
658         {
659                 anything = 0;
660                 i = 0;
661                 ii = i + msg.u.s.interfaces;
662                 while(i < ii)
663                 {
664                         /* show interface summary */
665                         move(++line>1?line:1, 0);
666                         color(white);
667                         if (m[i].u.i.block >= 2)
668                         {
669                                 SPRINT(buffer, "%s (%d)%s", m[i].u.i.interface_name, m[i].u.i.portnum, (m[i].u.i.extension)?" (extension)":"");
670                                 addstr(buffer);
671                                 color(red);
672                                 addstr("  not loaded");
673                         } else
674                         {
675                                 SPRINT(buffer, "%s (%d) %s %s%s use:%d", m[i].u.i.interface_name, m[i].u.i.portnum, (m[i].u.i.ntmode)?"NT-mode":"TE-mode", (m[i].u.i.ptp)?"ptp ":"ptmp", (m[i].u.i.extension)?" extension":"", m[i].u.i.use);
676                                 addstr(buffer);
677                                 if (m[i].u.i.ptp || !m[i].u.i.ntmode)
678                                 {
679                                         color((m[i].u.i.l2link)?green:red);
680                                         addstr((m[i].u.i.l2link)?"  L2 UP":"  L2 down");
681                                 }
682                                 color((m[i].u.i.l1link)?green:blue);
683                                 addstr((m[i].u.i.l1link)?"  L1 ACTIVE":"  L1 inactive");
684                                 if (m[i].u.i.los)
685                                 {
686                                         color(red);
687                                         addstr(" LOS");
688                                 }
689                                 if (m[i].u.i.ais)
690                                 {
691                                         color(red);
692                                         addstr(" AIS");
693                                 }
694                                 if (m[i].u.i.rdi)
695                                 {
696                                         color(red);
697                                         addstr(" RDI");
698                                 }
699                                 if (m[i].u.i.slip_tx || m[i].u.i.slip_rx)
700                                 {
701                                         color(red);
702                                         SPRINT(buffer, " SLIP(tx:%d rx:%d)", m[i].u.i.slip_tx, m[i].u.i.slip_rx);
703                                         addstr(buffer);
704                                 }
705                                 if (m[i].u.i.block)
706                                 {
707                                         color(red);
708                                         addstr("  blocked");
709                                 }
710                                 if (line+2 >= LINES) goto end;
711                                 /* show channels */
712                                 if (show_interfaces > 1)
713                                 {
714                                         ltee = 0;
715                                         j = k =0;
716                                         jj = m[i].u.i.channels;
717                                         while(j < jj)
718                                         {
719                                                 /* show all channels */
720                                                 if (show_interfaces>2 || m[i].u.i.busy[j]>0)
721                                                 {
722                                                         color(cyan);
723                                                         /* show left side / right side */
724                                                         if ((k & 1) && (COLS > 70))
725                                                         {
726                                                                 move(line>1?line:1,4+((COLS-4)/2));
727                                                         } else
728                                                         {
729                                                                 move(++line>1?line:1, 1);
730                                                                 LTEE
731                                                                 ltee = 1;
732                                                         }
733                                                         k++;
734                                                         color(white);
735                                                         if (m[i].u.i.pri)
736                                                                 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
737                                                         else
738                                                                 SPRINT(buffer,"B%2d: ", j+1);
739                                                         addstr(buffer);
740                                                         switch(m[i].u.i.busy[j])
741                                                         {
742                                                                 case B_STATE_IDLE:
743                                                                 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block)
744                                                                 {
745                                                                         color(red);
746                                                                         addstr("blocked ");
747                                                                 } else
748                                                                 {
749                                                                         color(blue);
750                                                                         addstr("idle    ");
751                                                                 }
752                                                                 break;
753                                                                 case B_STATE_ACTIVATING:
754                                                                 color(yellow);
755                                                                 addstr("act'ing ");
756                                                                 break;
757                                                                 case B_STATE_ACTIVE:
758                                                                 color(green);
759                                                                 addstr("busy    ");
760                                                                 break;
761                                                                 case B_STATE_DEACTIVATING:
762                                                                 color(yellow);
763                                                                 addstr("dact'ing");
764                                                                 break;
765                                                                 case B_STATE_EXPORTING:
766                                                                 color(yellow);
767                                                                 addstr("exp'ing ");
768                                                                 break;
769                                                                 case B_STATE_REMOTE:
770                                                                 color(green);
771                                                                 addstr("remote  ");
772                                                                 break;
773                                                                 case B_STATE_IMPORTING:
774                                                                 color(yellow);
775                                                                 addstr("imp'ing ");
776                                                                 break;
777                                                         }
778                                                         if (m[i].u.i.port[j])
779                                                         {
780                                                                 /* search for port */
781                                                                 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
782                                                                 ll = l+msg.u.s.ports;
783                                                                 while(l < ll)
784                                                                 {
785                                                                         if (m[l].u.p.serial == m[i].u.i.port[j])
786                                                                         {
787                                                                                 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
788                                                                                 addstr(buffer);
789                                                                         }
790                                                                         l++;
791                                                                 }
792                                                         }
793                                                         if (line+2 >= LINES)
794                                                         {
795                                                                 if (ltee)
796                                                                 {
797                                                                         color(cyan);
798                                                                         move(line>1?line:1, 1);
799                                                                         LLCORNER
800                                                                 }
801                                                                 goto end;
802                                                         }
803                                                 }
804                                                 j++;
805                                         }
806                                         if (ltee)
807                                         {
808                                                 color(cyan);
809                                                 move(line>1?line:1, 1);
810                                                 LLCORNER
811                                         }
812                                         if (line+2 >= LINES) goto end;
813                                         /* show summary if no channels were shown */
814                                         if (show_interfaces<2 && ltee==0)
815                                         {
816                                                 color(cyan);
817                                                 move(++line>1?line:1, 1);
818                                                 LLCORNER
819                                                         
820                                                 if (m[i].u.i.l2link && m[i].u.i.block==0)
821                                                 {
822                                                         color(green);
823                                                         SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
824                                                 } else
825                                                 {
826                                                         color(red);
827                                                         SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
828                                                 }
829                                                 addstr(buffer);
830                                         }
831                                         if (line+2 >= LINES) goto end;
832                                 }
833                         }
834                         i++;
835                         anything = 1;
836                 }
837                 i = msg.u.s.interfaces;
838                 ii = i + msg.u.s.remotes;
839                 while(i < ii)
840                 {
841                         /* show remote summary */
842                         move(++line>1?line:1, 0);
843                         color(white);
844                         SPRINT(buffer, "Remote: %s", m[i].u.r.name);
845                         addstr(buffer);
846                         i++;
847                 }
848                 if (anything)
849                         line++;
850                 if (line+2 >= LINES) goto end;
851         }               
852         /* display calls (brief) */
853         if (show_calls == 1)
854         {
855                 anything = 0;
856                 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
857                 ii = i+msg.u.s.epoints;
858                 while(i < ii)
859                 {
860                         /* for each endpoint... */
861                         if (!m[i].u.e.join)
862                         {
863                                 move(++line>1?line:1, 0);
864                                 color(white);
865                                 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
866                                 addstr(buffer);
867                                 color(cyan);
868                                 if (m[i].u.e.terminal[0])
869                                 {
870                                         addstr("intern=");
871                                         color(green);
872                                         addstr(m[i].u.e.terminal);
873                                 } else
874                                         addstr("extern");
875                                 color(white);
876                                 SPRINT(buffer, " %s", m[i].u.e.callerid);
877                                 addstr(buffer);
878                                 color(cyan);
879                                 addstr("->");
880                                 color(white);
881                                 SPRINT(buffer, "%s", m[i].u.e.dialing);
882                                 addstr(buffer);
883                                 if (m[i].u.e.action[0])
884                                 {
885                                         color(cyan);
886                                         addstr(" action=");
887                                         color(yellow);
888                                         addstr(m[i].u.e.action);
889                                 }
890                                 if (line+2 >= LINES) goto end;
891                         }
892                         i++;
893                         anything = 1;
894                 }
895                 j = msg.u.s.interfaces+msg.u.s.remotes;
896                 jj = j+msg.u.s.joins;
897                 while(j < jj)
898                 {
899                         /* for each call... */
900                         move(++line>1?line:1, 0);
901                         color(white);
902                         SPRINT(buffer, "(%d):", m[j].u.j.serial);
903                         addstr(buffer);
904                         i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
905                         ii = i+msg.u.s.epoints;
906                         while(i < ii)
907                         {
908                                 /* for each endpoint... */
909                                 if (m[i].u.e.join == m[j].u.j.serial)
910                                 {
911                                         color(white);
912                                         SPRINT(buffer, " (%d)", m[i].u.e.serial);
913                                         addstr(buffer);
914                                         color(cyan);
915                                         if (m[i].u.e.terminal[0])
916                                         {
917                                                 addstr("int=");
918                                                 color(green);
919                                                 addstr(m[i].u.e.terminal);
920                                         } else
921                                                 addstr("ext");
922                                         color(white);
923                                         SPRINT(buffer, "-%s", m[i].u.e.callerid);
924                                         addstr(buffer);
925                                         color(cyan);
926                                         addstr(">");
927                                         color(white);
928                                         SPRINT(buffer, "%s", m[i].u.e.dialing);
929                                         addstr(buffer);
930                                 }
931                                 i++;
932                                 anything = 1;
933                         }
934                         if (line+2 >= LINES) goto end;
935                         j++;
936                 }
937                 if (anything)
938                         line++;
939                 if (line+2 >= LINES) goto end;
940         }
941         /* display calls (structurd) */
942         if (show_calls == 2)
943         {
944                 /* show all ports with no epoint */
945                 anything = 0;
946                 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
947                 ii = i+msg.u.s.ports;
948                 while(i < ii)
949                 {
950                         if (!m[i].u.p.epoint)
951                         {
952                                 move(++line>1?line:1, 8);
953                                 if (line+2 >= LINES) goto end;
954                                 line = debug_port(&msg, m, line, i, 0);
955                                 if (line+2 >= LINES) goto end;
956                                 anything = 1;
957                         }
958                         i++;
959                 }
960                 if (anything)
961                         line++;
962                 if (line+2 >= LINES) goto end;
963
964                 /* show all epoints with no call */
965                 anything = 0;
966                 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
967                 ii = i+msg.u.s.epoints;
968                 while(i < ii)
969                 {
970                         if (!m[i].u.e.join)
971                         {
972                                 move(++line>1?line:1, 4);
973                                 if (line+2 >= LINES) goto end;
974                                 line = debug_epoint(&msg, m, line, i, 0);
975                                 if (line+2 >= LINES) goto end;
976                                 anything = 1;
977                         }
978                         i++;
979                 }
980                 if (anything)
981                         line++;
982                 if (line+2 >= LINES) goto end;
983
984                 /* show all joins */
985                 anything = 0;
986                 i = msg.u.s.interfaces+msg.u.s.remotes;
987                 ii = i+msg.u.s.joins;
988                 while(i < ii)
989                 {
990                         move(++line>1?line:1, 0);
991                         if (line+2 >= LINES) goto end;
992                         line = debug_join(&msg, m, line, i);
993                         if (line+2 >= LINES) goto end;
994                         i++;
995                         anything = 1;
996                 }
997                 if (anything)
998                         line++;
999                 if (line+2 >= LINES) goto end;
1000
1001         }
1002
1003         /* show log */
1004         if (show_log)
1005         {
1006                 if (line+2 < LINES)
1007                 {
1008                         move(line++>1?line-1:1, 0);
1009                         color(blue);
1010                         hline(ACS_HLINE, COLS);
1011                         color(white);
1012                         
1013                         l = logcur-(LINES-line-2);
1014                         ll = logcur;
1015                         if (ll-l >= LOGLINES)
1016                                 l = ll-LOGLINES+1;
1017                         while(l!=ll)
1018                         {
1019                                 move(line++>1?line-1:1, 0);
1020                                 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
1021                                         SCPY(buffer, logline[l % LOGLINES] + hoffset);
1022                                 else
1023                                         buffer[0] = '\0';
1024                                 if (COLS < (int)strlen(buffer))
1025                                 {
1026                                         buffer[COLS-1] = '\0';
1027                                         addstr(buffer);
1028                                         color(mangenta);
1029                                         addch('*');
1030                                         color(white);
1031                                 } else
1032                                         addstr(buffer);
1033                                 l++;
1034                         }
1035                 }
1036         }
1037
1038         end:
1039         /* free memory */
1040         FREE(m, 0);
1041         /* display name/time */
1042 //      move(0, 0);
1043 //      hline(' ', COLS);
1044         move(0, 0);
1045         color(cyan);
1046         msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
1047         SPRINT(buffer, "LCR %s", msg.u.s.version_string);
1048         addstr(buffer);
1049         if (COLS>50)
1050         {
1051                 move(0, COLS-19);
1052                 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1053                         msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1054                         msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1055                 addstr(buffer);
1056         }
1057         /* displeay head line */
1058         move(1, 0);
1059         color(blue);
1060         hline(ACS_HLINE, COLS);
1061         if (offset)
1062         {
1063                 move(1, 1);
1064                 SPRINT(buffer, "Offset +%d", offset);
1065                 color(red);
1066                 addstr(buffer);
1067         }
1068         if (hoffset)
1069         {
1070                 move(1, 13);
1071                 SPRINT(buffer, "H-Offset +%d", hoffset);
1072                 color(red);
1073                 addstr(buffer);
1074         }
1075         /* display end */
1076         move(LINES-2, 0);
1077         color(blue);
1078         hline(ACS_HLINE, COLS);
1079         move(LINES-1, 0);
1080         if (enter)
1081         {
1082                 color(white);
1083                 SPRINT(buffer, "-> %s", enter_string);
1084         } else
1085         {
1086                 color(cyan);
1087                 SPRINT(buffer, "i=interfaces '%s'  c=calls '%s'  l=log  q=quit  +-*/=scroll  enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1088         }
1089         addstr(buffer);
1090         refresh();
1091
1092         /* resize */
1093         if (lastlines!=LINES || lastcols!=COLS)
1094         {
1095                 cleanup_curses();
1096                 init_curses();
1097                 goto again;
1098         }
1099
1100         if (enter)
1101         {
1102                 /* user input in enter mode */
1103                 ch = getch();
1104                 enter_again:
1105                 if (ch == 10)
1106                 {
1107                         FILE *fp;
1108
1109                         enter = 0;
1110                         if (!enter_string[0])
1111                                 goto again;
1112
1113                         SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1114                         if (!!strncmp(enter_string, "interface", 10) &&
1115                             !!strncmp(enter_string, "route", 6) &&
1116                             !!strncmp(enter_string, "release ", 8) &&
1117                             !!strncmp(enter_string, "block ", 6) &&
1118                             !!strncmp(enter_string, "unblock ", 8) &&
1119                             !!strncmp(enter_string, "unload ", 7))
1120                         {
1121                                 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1122                                 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1123                                 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1124                                 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1125                                 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1126                                 SPRINT(logline[logcur++ % LOGLINES], "unblock <port> (unblock port for further calls, load if not loaded)");
1127                                 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1128                         } else
1129                         {
1130                                 /* applend output to log window */
1131                                 SPRINT(buffer, "%s %s", argv[0], enter_string);
1132                                 fp = popen(buffer, "r");
1133                                 if (fp)
1134                                 {
1135                                         while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1136                                                 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1137                                         pclose(fp);
1138                                 } else
1139                                 {
1140                                         SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1141                                 }
1142                         }
1143                         logline[logcur % LOGLINES][0] = '\0';
1144                         enter_string[0] = '\0';
1145                         goto again;
1146                 }
1147                 if (ch>=32 && ch<=126)
1148                 {
1149                         SCCAT(enter_string, ch);
1150                         ch = getch();
1151                         if (ch > 0)
1152                                 goto enter_again;
1153                         goto again;
1154                 } else
1155                 if (ch==8 || ch==127)
1156                 {
1157                         if (enter_string[0])
1158                                 enter_string[strlen(enter_string)-1] = '\0';
1159                         ch = getch();
1160                         if (ch > 0)
1161                                 goto enter_again;
1162                         goto again;
1163                 } else
1164                 if (ch != 3)
1165                 {
1166                         ch = getch();
1167                         if (ch > 0)
1168                                 goto enter_again;
1169                         FD_ZERO(&select_rfds);
1170                         FD_SET(0, &select_rfds);
1171                         select_tv.tv_sec = 0;
1172                         select_tv.tv_usec = 250000;
1173                         select(1, &select_rfds, NULL, NULL, &select_tv);
1174                         goto again;
1175                 }
1176         } else
1177         {
1178                 /* user input in normal mode */
1179                 switch(getch())
1180                 {
1181                         case 12: /* refresh */
1182                         cleanup_curses();
1183                         init_curses();
1184                         goto again;
1185                         break;
1186
1187                         case 3: /* abort */
1188                         case 'q':
1189                         case 'Q':
1190                         break;
1191
1192                         case 'i': /* toggle interface */
1193                         show_interfaces++;
1194                         if (show_interfaces > 3) show_interfaces = 0;
1195                         goto again;
1196
1197                         case 'c': /* toggle calls */
1198                         show_calls++;
1199                         if (show_calls > 2) show_calls = 0;
1200                         goto again;
1201
1202                         case 'l': /* toggle log */
1203                         show_log++;
1204                         if (show_log > 1) show_log = 0;
1205                         goto again;
1206
1207                         case '+': /* scroll down */
1208                         offset++;
1209                         goto again;
1210                         
1211                         case '-': /* scroll up */
1212                         if (offset)
1213                                 offset--;
1214                         goto again;
1215
1216                         case '*': /* scroll right */
1217                         hoffset += 2;
1218                         goto again;
1219                         
1220                         case '/': /* scroll left */
1221                         if (hoffset)
1222                                 hoffset -= 2;
1223                         goto again;
1224
1225                         case 10: /* entermode */
1226                         enter = 1;
1227                         goto again;
1228
1229                         default:
1230                         FD_ZERO(&select_rfds);
1231                         FD_SET(0, &select_rfds);
1232                         select_tv.tv_sec = 0;
1233                         select_tv.tv_usec = 250000;
1234                         select(1, &select_rfds, NULL, NULL, &select_tv);
1235                         goto again;
1236                 }
1237         }
1238
1239         /* check for logfh */
1240         if (logfh >= 0)
1241                 close(logfh);
1242         logfh = -1;
1243
1244         /* cleanup curses and exit */
1245         cleanup_curses();
1246
1247         return(NULL);
1248 }
1249
1250
1251 /*
1252  * Send command and show error message.
1253  */
1254 char *admin_cmd(int sock, int mode, char *extension, char *number)
1255 {
1256         static struct admin_message msg;
1257
1258         /* send reload command */
1259         memset(&msg, 0, sizeof(msg));
1260         switch(mode)
1261         {
1262                 case MODE_INTERFACE:
1263                 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1264                 break;
1265                 case MODE_ROUTE:
1266                 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1267                 break;
1268                 case MODE_DIAL:
1269                 msg.message = ADMIN_REQUEST_CMD_DIAL;
1270                 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1271                 break;
1272                 case MODE_RELEASE:
1273                 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1274                 SCPY(msg.u.x.message, number);
1275                 break;
1276                 case MODE_UNBLOCK:
1277                 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1278                 msg.u.x.portnum = atoi(number);
1279                 msg.u.x.block = 0;
1280                 break;
1281                 case MODE_BLOCK:
1282                 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1283                 msg.u.x.portnum = atoi(number);
1284                 msg.u.x.block = 1;
1285                 break;
1286                 case MODE_UNLOAD:
1287                 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1288                 msg.u.x.portnum = atoi(number);
1289                 msg.u.x.block = 2;
1290                 break;
1291         }
1292
1293         if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1294                 return("Broken pipe while sending command.");
1295
1296         /* receive response */
1297         if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1298                 return("Broken pipe while receiving response.");
1299         switch(mode)
1300         {
1301                 case MODE_INTERFACE:
1302                 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1303                         return("Response not valid.");
1304                 break;
1305                 case MODE_ROUTE:
1306                 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1307                         return("Response not valid.");
1308                 break;
1309                 case MODE_DIAL:
1310                 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1311                         return("Response not valid.");
1312                 break;
1313                 case MODE_RELEASE:
1314                 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1315                         return("Response not valid.");
1316                 break;
1317                 case MODE_UNBLOCK:
1318                 case MODE_BLOCK:
1319                 case MODE_UNLOAD:
1320                 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1321                         return("Response not valid.");
1322                 break;
1323         }
1324
1325         /* process response */
1326         if (msg.u.x.error)
1327         {
1328                 return(msg.u.x.message);
1329         }
1330         printf("Command successfull.\n");
1331         return(NULL);
1332 }
1333
1334
1335 /*
1336  * makes a testcall
1337  */
1338 #define GET_NOW() { \
1339         gettimeofday(&now_tv, &now_tz); \
1340         now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1341         }
1342 char *admin_testcall(int sock, int argc, char *argv[])
1343 {
1344         static struct admin_message msg;
1345         int ar = 2;
1346         int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1347         int l;
1348         double timer = 0, now_d;
1349         unsigned int on = 1;
1350         struct timeval now_tv;
1351         struct timezone now_tz;
1352
1353         printf("pid=%d\n", getpid()); fflush(stdout);
1354
1355         while (argc > ar)
1356         {
1357                 if (!strcmp(argv[ar], "--setup-timeout"))
1358                 {
1359                         ar++;
1360                         if (argc == ar)
1361                                 return("Missing setup timeout value.\n");
1362                         stimeout = atoi(argv[ar]);
1363                         ar++;
1364                 } else
1365                 if (!strcmp(argv[ar], "--proceeding-timeout"))
1366                 {
1367                         ar++;
1368                         if (argc == ar)
1369                                 return("Missing proceeding timeout value.\n");
1370                         ptimeout = atoi(argv[ar]);
1371                         ar++;
1372                 } else
1373                 if (!strcmp(argv[ar], "--alerting-timeout"))
1374                 {
1375                         ar++;
1376                         if (argc == ar)
1377                                 return("Missing alerting timeout value.\n");
1378                         atimeout = atoi(argv[ar]);
1379                         ar++;
1380                 } else
1381                 if (!strcmp(argv[ar], "--connect-timeout"))
1382                 {
1383                         ar++;
1384                         if (argc == ar)
1385                                 return("Missing connect timeout value.\n");
1386                         ctimeout = atoi(argv[ar]);
1387                         ar++;
1388                 } else
1389                 {
1390                         break;
1391                 }
1392         }
1393
1394         /* send reload command */
1395         memset(&msg, 0, sizeof(msg));
1396         msg.message = ADMIN_CALL_SETUP;
1397         msg.u.call.present = 1;
1398
1399         if (argc > ar)
1400         {
1401                 SCPY(msg.u.call.interface, argv[ar]);
1402         }
1403         ar++;
1404         if (argc > ar)
1405         {
1406                 SCPY(msg.u.call.callerid, argv[ar]);
1407         }
1408         ar++;
1409         if (argc > ar)
1410         {
1411                 SCPY(msg.u.call.dialing, argv[ar]);
1412         }
1413         ar++;
1414         if (argc > ar)
1415         {
1416                 if (argv[ar][0] == 'r')
1417                         msg.u.call.present = 0;
1418         }
1419         ar++;
1420         msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1421         msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1422         msg.u.call.bc_info1 = 0;
1423         msg.u.call.hlc = 0;
1424         msg.u.call.exthlc = 0;
1425         if (argc > ar)
1426                 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1427         else
1428                 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1429         ar++;
1430         if (argc > ar) {
1431                 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1432                 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1433         }
1434         ar++;
1435         if (argc > ar) {
1436                 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1437                 if (msg.u.call.bc_info1 < 0)
1438                         msg.u.call.bc_info1 = 0;
1439                 else
1440                         msg.u.call.bc_info1 |= 0x80;
1441         }
1442         ar++;
1443         if (argc > ar) {
1444                 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1445                 if (msg.u.call.hlc < 0)
1446                         msg.u.call.hlc = 0;
1447                 else
1448                         msg.u.call.hlc |= 0x80;
1449         }
1450         ar++;
1451         if (argc > ar) {
1452                 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1453                 if (msg.u.call.exthlc < 0)
1454                         msg.u.call.exthlc = 0;
1455                 else
1456                         msg.u.call.exthlc |= 0x80;
1457         }
1458         ar++;
1459
1460         if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1461                 return("Broken pipe while sending command.");
1462
1463         if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1464                 return("Failed to set socket into non-blocking IO.");
1465
1466         if (stimeout)
1467         {
1468                 GET_NOW();
1469                 timer = now_d + (double)stimeout;
1470         }
1471         
1472         /* receive response */
1473 next:
1474         l = read(sock, &msg, sizeof(msg));
1475         if (l < 0)
1476         {
1477                 if (errno == EWOULDBLOCK)
1478                 {
1479                         if (timer)
1480                         {
1481                                 GET_NOW();
1482                                 if (timer <= now_d)
1483                                 {
1484                                         printf("Timeout\n"); fflush(stdout);
1485                                         return(NULL);
1486                                 }
1487                         }
1488                         usleep(30000);
1489                         goto next;
1490                 }
1491                 return("Broken pipe while receiving response.");
1492         }
1493         if (l != sizeof(msg))
1494                 return("Response has unexpected message size.");
1495         switch(msg.message)
1496         {
1497                 case ADMIN_CALL_SETUP_ACK:
1498                 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1499                 goto next;
1500
1501                 case ADMIN_CALL_PROCEEDING:
1502                 printf("PROCEEDING\n"); fflush(stdout);
1503                 if (ptimeout)
1504                 {
1505                         GET_NOW();
1506                         timer = now_d + (double)ptimeout;
1507                 }
1508                 goto next;
1509
1510                 case ADMIN_CALL_ALERTING:
1511                 printf("ALERTING\n"); fflush(stdout);
1512                 if (atimeout)
1513                 {
1514                         GET_NOW();
1515                         timer = now_d + (double)atimeout;
1516                 }
1517                 goto next;
1518
1519                 case ADMIN_CALL_CONNECT:
1520                 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1521                 if (ctimeout)
1522                 {
1523                         GET_NOW();
1524                         timer = now_d + (double)ctimeout;
1525                 }
1526                 goto next;
1527
1528                 case ADMIN_CALL_NOTIFY:
1529                 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1530                 goto next;
1531
1532                 case ADMIN_CALL_DISCONNECT:
1533                 printf("DISCONNECT\n cause=%d %s\n location=%d %s\n", msg.u.call.cause, (msg.u.call.cause>0 && msg.u.call.cause<128)?isdn_cause[msg.u.call.cause].german:"", msg.u.call.location, (msg.u.call.location>=0 && msg.u.call.location<128)?isdn_location[msg.u.call.location].german:""); fflush(stdout);
1534                 break;
1535
1536                 case ADMIN_CALL_RELEASE:
1537                 printf("RELEASE\n cause=%d %s\n location=%d %s\n", msg.u.call.cause, (msg.u.call.cause>0 && msg.u.call.cause<128)?isdn_cause[msg.u.call.cause].german:"", msg.u.call.location, (msg.u.call.location>=0 && msg.u.call.location<128)?isdn_location[msg.u.call.location].german:""); fflush(stdout);
1538                 break;
1539
1540                 default:
1541                 return("Response not valid.");
1542         }
1543
1544         printf("Call released.\n"); fflush(stdout);
1545         return(NULL);
1546 }
1547
1548
1549 /*
1550  * makes a trace
1551  */
1552 char *admin_trace(int sock, int argc, char *argv[])
1553 {
1554         static struct admin_message msg;
1555         int i;
1556
1557         /* show help */
1558         if (argc > 2) if (!strcasecmp(argv[2], "help"))
1559         {
1560                 printf("Trace Help\n----------\n");
1561                 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1562                 printf("By default a complete trace is shown in detailed format.\n");
1563                 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1564                 printf("Use filter values to select specific trace messages.\n");
1565                 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1566                 printf("Filters:\n");
1567                 printf(" category=<mask bits>\n");
1568                 printf("  0x01 = CH: channel object trace\n");
1569                 printf("  0x02 = EP: endpoint object trace\n");
1570                 printf(" port=<mISDN port>  select only given port for trace\n");
1571                 printf(" interface=<interface name>  select only given interface for trace\n");
1572                 printf(" caller=<caller id>  select only given caller id for trace\n");
1573                 printf(" dialing=<number>  select only given dialed number for trace\n");
1574                 return(NULL);
1575         }
1576
1577         /* init trace request */        
1578         memset(&msg, 0, sizeof(msg));
1579         msg.message = ADMIN_TRACE_REQUEST;
1580         msg.u.trace_req.detail = 3;
1581
1582         /* parse args */
1583         i = 2;
1584         while(i < argc)
1585         {
1586                 if (!strcasecmp(argv[i], "brief"))
1587                         msg.u.trace_req.detail = 1;
1588                 else if (!strcasecmp(argv[i], "short"))
1589                         msg.u.trace_req.detail = 2;
1590                 else if (!strncasecmp(argv[i], "category=", 9))
1591                         msg.u.trace_req.category = atoi(argv[i]+9);
1592                 else if (!strncasecmp(argv[i], "port=", 5))
1593                         msg.u.trace_req.port = atoi(argv[i]+5);
1594                 else if (!strncasecmp(argv[i], "interface=", 10))
1595                         SCPY(msg.u.trace_req.interface, argv[i]+10);
1596                 else if (!strncasecmp(argv[i], "caller=", 7))
1597                         SCPY(msg.u.trace_req.caller, argv[i]+7);
1598                 else if (!strncasecmp(argv[i], "dialing=", 8))
1599                         SCPY(msg.u.trace_req.dialing, argv[i]+8);
1600                 else return("Invalid trace option, try 'trace help'.");
1601
1602                 i++;
1603         }
1604
1605         /* send trace request */
1606         if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1607                 return("Broken pipe while sending trace request.");
1608
1609         /* receive response */
1610 next:
1611         if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1612                 return("Broken pipe while receiving response.");
1613
1614         if (msg.message != ADMIN_TRACE_RESPONSE)
1615                 return("Response not valid.");
1616
1617         printf("%s", msg.u.trace_rsp.text);
1618         goto next;
1619 }
1620
1621
1622 /*
1623  * main function
1624  */
1625 int main(int argc, char *argv[])
1626 {
1627         int mode;
1628         char *socket_name = SOCKET_NAME;
1629         int sock, conn;
1630         struct sockaddr_un sock_address;
1631         char *ret;
1632
1633
1634         /* show options */
1635         if (argc <= 1)
1636         {
1637                 usage:
1638                 printf("\n");
1639                 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1640                 printf("state - View current states using graphical console output.\n");
1641                 printf("interface - Tell LCR to reload \"interface.conf\".\n");
1642                 printf("route - Tell LCR to reload \"route.conf\".\n");
1643                 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1644                 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1645                 printf("block <port> - Block given port.\n");
1646                 printf("unblock <port> - Unblock given port.\n");
1647                 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1648                 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1649                 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1650                 printf("              --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1651                 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1652                 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1653                 printf(" -> Use 'trace help' to see filter description.\n");
1654                 printf("\n");
1655                 return(0);
1656         }
1657
1658         /* check mode */
1659         if (!(strcasecmp(argv[1],"state")))
1660         {
1661                 mode = MODE_STATE;
1662         } else
1663         if (!(strcasecmp(argv[1],"interface")))
1664         {
1665                 mode = MODE_INTERFACE;
1666         } else
1667         if (!(strcasecmp(argv[1],"route")))
1668         {
1669                 mode = MODE_ROUTE;
1670         } else
1671         if (!(strcasecmp(argv[1],"dial")))
1672         {
1673                 if (argc <= 3)
1674                         goto usage;
1675                 mode = MODE_DIAL;
1676         } else
1677         if (!(strcasecmp(argv[1],"release")))
1678         {
1679                 if (argc <= 2)
1680                         goto usage;
1681                 mode = MODE_RELEASE;
1682         } else
1683         if (!(strcasecmp(argv[1],"unblock")))
1684         {
1685                 if (argc <= 2)
1686                         goto usage;
1687                 mode = MODE_UNBLOCK;
1688         } else
1689         if (!(strcasecmp(argv[1],"block")))
1690         {
1691                 if (argc <= 2)
1692                         goto usage;
1693                 mode = MODE_BLOCK;
1694         } else
1695         if (!(strcasecmp(argv[1],"unload")))
1696         {
1697                 if (argc <= 2)
1698                         goto usage;
1699                 mode = MODE_UNLOAD;
1700         } else
1701         if (!(strcasecmp(argv[1],"testcall")))
1702         {
1703                 if (argc <= 4)
1704                         goto usage;
1705                 mode = MODE_TESTCALL;
1706         } else
1707         if (!(strcasecmp(argv[1],"trace")))
1708         {
1709                 mode = MODE_TRACE;
1710         } else
1711         {
1712                 goto usage;
1713         }
1714
1715 //pipeagain:
1716         /* open socket */
1717         if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1718         {
1719                 fprintf(stderr, "Failed to create socket.\n");
1720                 exit(EXIT_FAILURE);
1721         }
1722         memset(&sock_address, 0, sizeof(sock_address));
1723         sock_address.sun_family = PF_UNIX;
1724         UCPY(sock_address.sun_path, socket_name);
1725         if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1726         {
1727                 close(sock);
1728                 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1729                 exit(EXIT_FAILURE);
1730         }
1731
1732         /* process mode */
1733         switch(mode)
1734         {
1735                 case MODE_STATE:
1736                 ret = admin_state(sock, argv);
1737                 break;
1738         
1739                 case MODE_INTERFACE:
1740                 case MODE_ROUTE:
1741                 ret = admin_cmd(sock, mode, NULL, NULL);
1742                 break;
1743
1744                 case MODE_DIAL:
1745                 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1746                 break;
1747
1748                 case MODE_RELEASE:
1749                 case MODE_UNBLOCK:
1750                 case MODE_BLOCK:
1751                 case MODE_UNLOAD:
1752                 ret = admin_cmd(sock, mode, NULL, argv[2]);
1753                 break;
1754
1755                 case MODE_TESTCALL:
1756                 ret = admin_testcall(sock, argc, argv);
1757                 break;
1758
1759                 case MODE_TRACE:
1760                 ret = admin_trace(sock, argc, argv);
1761                 break;
1762         }
1763
1764         close(sock);
1765         /* now we say good bye */
1766         if (ret)
1767         {
1768 //              if (!strncasecmp(ret, "Broken Pipe", 11))
1769 //                      goto pipeagain;
1770                 printf("%s\n", ret);
1771                 exit(EXIT_FAILURE);
1772         }
1773 }
1774
1775
1776
1777
1778