rebuffer option for chan_lcr (160 bytes per frame)
[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 > 0)?green:blue);
683                                 if (m[i].u.i.l1link < 0)
684                                         addstr("  L1 unknown");
685                                 else
686                                         addstr((m[i].u.i.l1link)?"  L1 ACTIVE":"  L1 inactive");
687                                 if (m[i].u.i.los)
688                                 {
689                                         color(red);
690                                         addstr(" LOS");
691                                 }
692                                 if (m[i].u.i.ais)
693                                 {
694                                         color(red);
695                                         addstr(" AIS");
696                                 }
697                                 if (m[i].u.i.rdi)
698                                 {
699                                         color(red);
700                                         addstr(" RDI");
701                                 }
702                                 if (m[i].u.i.slip_tx || m[i].u.i.slip_rx)
703                                 {
704                                         color(red);
705                                         SPRINT(buffer, " SLIP(tx:%d rx:%d)", m[i].u.i.slip_tx, m[i].u.i.slip_rx);
706                                         addstr(buffer);
707                                 }
708                                 if (m[i].u.i.block)
709                                 {
710                                         color(red);
711                                         addstr("  blocked");
712                                 }
713                                 if (line+2 >= LINES) goto end;
714                                 /* show channels */
715                                 if (show_interfaces > 1)
716                                 {
717                                         ltee = 0;
718                                         j = k =0;
719                                         jj = m[i].u.i.channels;
720                                         while(j < jj)
721                                         {
722                                                 /* show all channels */
723                                                 if (show_interfaces>2 || m[i].u.i.busy[j]>0)
724                                                 {
725                                                         color(cyan);
726                                                         /* show left side / right side */
727                                                         if ((k & 1) && (COLS > 70))
728                                                         {
729                                                                 move(line>1?line:1,4+((COLS-4)/2));
730                                                         } else
731                                                         {
732                                                                 move(++line>1?line:1, 1);
733                                                                 LTEE
734                                                                 ltee = 1;
735                                                         }
736                                                         k++;
737                                                         color(white);
738                                                         if (m[i].u.i.pri)
739                                                                 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
740                                                         else
741                                                                 SPRINT(buffer,"B%2d: ", j+1);
742                                                         addstr(buffer);
743                                                         switch(m[i].u.i.busy[j])
744                                                         {
745                                                                 case B_STATE_IDLE:
746                                                                 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block)
747                                                                 {
748                                                                         color(red);
749                                                                         addstr("blocked ");
750                                                                 } else
751                                                                 {
752                                                                         color(blue);
753                                                                         addstr("idle    ");
754                                                                 }
755                                                                 break;
756                                                                 case B_STATE_ACTIVATING:
757                                                                 color(yellow);
758                                                                 addstr("act'ing ");
759                                                                 break;
760                                                                 case B_STATE_ACTIVE:
761                                                                 color(green);
762                                                                 addstr("busy    ");
763                                                                 break;
764                                                                 case B_STATE_DEACTIVATING:
765                                                                 color(yellow);
766                                                                 addstr("dact'ing");
767                                                                 break;
768                                                                 case B_STATE_EXPORTING:
769                                                                 color(yellow);
770                                                                 addstr("exp'ing ");
771                                                                 break;
772                                                                 case B_STATE_REMOTE:
773                                                                 color(green);
774                                                                 addstr("remote  ");
775                                                                 break;
776                                                                 case B_STATE_IMPORTING:
777                                                                 color(yellow);
778                                                                 addstr("imp'ing ");
779                                                                 break;
780                                                         }
781                                                         if (m[i].u.i.port[j])
782                                                         {
783                                                                 /* search for port */
784                                                                 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
785                                                                 ll = l+msg.u.s.ports;
786                                                                 while(l < ll)
787                                                                 {
788                                                                         if (m[l].u.p.serial == m[i].u.i.port[j])
789                                                                         {
790                                                                                 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
791                                                                                 addstr(buffer);
792                                                                         }
793                                                                         l++;
794                                                                 }
795                                                         }
796                                                         if (line+2 >= LINES)
797                                                         {
798                                                                 if (ltee)
799                                                                 {
800                                                                         color(cyan);
801                                                                         move(line>1?line:1, 1);
802                                                                         LLCORNER
803                                                                 }
804                                                                 goto end;
805                                                         }
806                                                 }
807                                                 j++;
808                                         }
809                                         if (ltee)
810                                         {
811                                                 color(cyan);
812                                                 move(line>1?line:1, 1);
813                                                 LLCORNER
814                                         }
815                                         if (line+2 >= LINES) goto end;
816                                         /* show summary if no channels were shown */
817                                         if (show_interfaces<2 && ltee==0)
818                                         {
819                                                 color(cyan);
820                                                 move(++line>1?line:1, 1);
821                                                 LLCORNER
822                                                         
823                                                 if (m[i].u.i.l2link && m[i].u.i.block==0)
824                                                 {
825                                                         color(green);
826                                                         SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
827                                                 } else
828                                                 {
829                                                         color(red);
830                                                         SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
831                                                 }
832                                                 addstr(buffer);
833                                         }
834                                         if (line+2 >= LINES) goto end;
835                                 }
836                         }
837                         i++;
838                         anything = 1;
839                 }
840                 i = msg.u.s.interfaces;
841                 ii = i + msg.u.s.remotes;
842                 while(i < ii)
843                 {
844                         /* show remote summary */
845                         move(++line>1?line:1, 0);
846                         color(white);
847                         SPRINT(buffer, "Remote: %s", m[i].u.r.name);
848                         addstr(buffer);
849                         i++;
850                 }
851                 if (anything)
852                         line++;
853                 if (line+2 >= LINES) goto end;
854         }               
855         /* display calls (brief) */
856         if (show_calls == 1)
857         {
858                 anything = 0;
859                 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
860                 ii = i+msg.u.s.epoints;
861                 while(i < ii)
862                 {
863                         /* for each endpoint... */
864                         if (!m[i].u.e.join)
865                         {
866                                 move(++line>1?line:1, 0);
867                                 color(white);
868                                 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
869                                 addstr(buffer);
870                                 color(cyan);
871                                 if (m[i].u.e.terminal[0])
872                                 {
873                                         addstr("intern=");
874                                         color(green);
875                                         addstr(m[i].u.e.terminal);
876                                 } else
877                                         addstr("extern");
878                                 color(white);
879                                 SPRINT(buffer, " %s", m[i].u.e.callerid);
880                                 addstr(buffer);
881                                 color(cyan);
882                                 addstr("->");
883                                 color(white);
884                                 SPRINT(buffer, "%s", m[i].u.e.dialing);
885                                 addstr(buffer);
886                                 if (m[i].u.e.action[0])
887                                 {
888                                         color(cyan);
889                                         addstr(" action=");
890                                         color(yellow);
891                                         addstr(m[i].u.e.action);
892                                 }
893                                 if (line+2 >= LINES) goto end;
894                         }
895                         i++;
896                         anything = 1;
897                 }
898                 j = msg.u.s.interfaces+msg.u.s.remotes;
899                 jj = j+msg.u.s.joins;
900                 while(j < jj)
901                 {
902                         /* for each call... */
903                         move(++line>1?line:1, 0);
904                         color(white);
905                         SPRINT(buffer, "(%d):", m[j].u.j.serial);
906                         addstr(buffer);
907                         i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
908                         ii = i+msg.u.s.epoints;
909                         while(i < ii)
910                         {
911                                 /* for each endpoint... */
912                                 if (m[i].u.e.join == m[j].u.j.serial)
913                                 {
914                                         color(white);
915                                         SPRINT(buffer, " (%d)", m[i].u.e.serial);
916                                         addstr(buffer);
917                                         color(cyan);
918                                         if (m[i].u.e.terminal[0])
919                                         {
920                                                 addstr("int=");
921                                                 color(green);
922                                                 addstr(m[i].u.e.terminal);
923                                         } else
924                                                 addstr("ext");
925                                         color(white);
926                                         SPRINT(buffer, "-%s", m[i].u.e.callerid);
927                                         addstr(buffer);
928                                         color(cyan);
929                                         addstr(">");
930                                         color(white);
931                                         SPRINT(buffer, "%s", m[i].u.e.dialing);
932                                         addstr(buffer);
933                                 }
934                                 i++;
935                                 anything = 1;
936                         }
937                         if (line+2 >= LINES) goto end;
938                         j++;
939                 }
940                 if (anything)
941                         line++;
942                 if (line+2 >= LINES) goto end;
943         }
944         /* display calls (structurd) */
945         if (show_calls == 2)
946         {
947                 /* show all ports with no epoint */
948                 anything = 0;
949                 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
950                 ii = i+msg.u.s.ports;
951                 while(i < ii)
952                 {
953                         if (!m[i].u.p.epoint)
954                         {
955                                 move(++line>1?line:1, 8);
956                                 if (line+2 >= LINES) goto end;
957                                 line = debug_port(&msg, m, line, i, 0);
958                                 if (line+2 >= LINES) goto end;
959                                 anything = 1;
960                         }
961                         i++;
962                 }
963                 if (anything)
964                         line++;
965                 if (line+2 >= LINES) goto end;
966
967                 /* show all epoints with no call */
968                 anything = 0;
969                 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
970                 ii = i+msg.u.s.epoints;
971                 while(i < ii)
972                 {
973                         if (!m[i].u.e.join)
974                         {
975                                 move(++line>1?line:1, 4);
976                                 if (line+2 >= LINES) goto end;
977                                 line = debug_epoint(&msg, m, line, i, 0);
978                                 if (line+2 >= LINES) goto end;
979                                 anything = 1;
980                         }
981                         i++;
982                 }
983                 if (anything)
984                         line++;
985                 if (line+2 >= LINES) goto end;
986
987                 /* show all joins */
988                 anything = 0;
989                 i = msg.u.s.interfaces+msg.u.s.remotes;
990                 ii = i+msg.u.s.joins;
991                 while(i < ii)
992                 {
993                         move(++line>1?line:1, 0);
994                         if (line+2 >= LINES) goto end;
995                         line = debug_join(&msg, m, line, i);
996                         if (line+2 >= LINES) goto end;
997                         i++;
998                         anything = 1;
999                 }
1000                 if (anything)
1001                         line++;
1002                 if (line+2 >= LINES) goto end;
1003
1004         }
1005
1006         /* show log */
1007         if (show_log)
1008         {
1009                 if (line+2 < LINES)
1010                 {
1011                         move(line++>1?line-1:1, 0);
1012                         color(blue);
1013                         hline(ACS_HLINE, COLS);
1014                         color(white);
1015                         
1016                         l = logcur-(LINES-line-2);
1017                         ll = logcur;
1018                         if (ll-l >= LOGLINES)
1019                                 l = ll-LOGLINES+1;
1020                         while(l!=ll)
1021                         {
1022                                 move(line++>1?line-1:1, 0);
1023                                 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
1024                                         SCPY(buffer, logline[l % LOGLINES] + hoffset);
1025                                 else
1026                                         buffer[0] = '\0';
1027                                 if (COLS < (int)strlen(buffer))
1028                                 {
1029                                         buffer[COLS-1] = '\0';
1030                                         addstr(buffer);
1031                                         color(mangenta);
1032                                         addch('*');
1033                                         color(white);
1034                                 } else
1035                                         addstr(buffer);
1036                                 l++;
1037                         }
1038                 }
1039         }
1040
1041         end:
1042         /* free memory */
1043         FREE(m, 0);
1044         /* display name/time */
1045 //      move(0, 0);
1046 //      hline(' ', COLS);
1047         move(0, 0);
1048         color(cyan);
1049         msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
1050         SPRINT(buffer, "LCR %s", msg.u.s.version_string);
1051         addstr(buffer);
1052         if (COLS>50)
1053         {
1054                 move(0, COLS-19);
1055                 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1056                         msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1057                         msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1058                 addstr(buffer);
1059         }
1060         /* displeay head line */
1061         move(1, 0);
1062         color(blue);
1063         hline(ACS_HLINE, COLS);
1064         if (offset)
1065         {
1066                 move(1, 1);
1067                 SPRINT(buffer, "Offset +%d", offset);
1068                 color(red);
1069                 addstr(buffer);
1070         }
1071         if (hoffset)
1072         {
1073                 move(1, 13);
1074                 SPRINT(buffer, "H-Offset +%d", hoffset);
1075                 color(red);
1076                 addstr(buffer);
1077         }
1078         /* display end */
1079         move(LINES-2, 0);
1080         color(blue);
1081         hline(ACS_HLINE, COLS);
1082         move(LINES-1, 0);
1083         if (enter)
1084         {
1085                 color(white);
1086                 SPRINT(buffer, "-> %s", enter_string);
1087         } else
1088         {
1089                 color(cyan);
1090                 SPRINT(buffer, "i=interfaces '%s'  c=calls '%s'  l=log  q=quit  +-*/=scroll  enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1091         }
1092         addstr(buffer);
1093         refresh();
1094
1095         /* resize */
1096         if (lastlines!=LINES || lastcols!=COLS)
1097         {
1098                 cleanup_curses();
1099                 init_curses();
1100                 goto again;
1101         }
1102
1103         if (enter)
1104         {
1105                 /* user input in enter mode */
1106                 ch = getch();
1107                 enter_again:
1108                 if (ch == 10)
1109                 {
1110                         FILE *fp;
1111
1112                         enter = 0;
1113                         if (!enter_string[0])
1114                                 goto again;
1115
1116                         SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1117                         if (!!strncmp(enter_string, "interface", 10) &&
1118                             !!strncmp(enter_string, "route", 6) &&
1119                             !!strncmp(enter_string, "release ", 8) &&
1120                             !!strncmp(enter_string, "block ", 6) &&
1121                             !!strncmp(enter_string, "unblock ", 8) &&
1122                             !!strncmp(enter_string, "unload ", 7))
1123                         {
1124                                 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1125                                 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1126                                 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1127                                 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1128                                 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1129                                 SPRINT(logline[logcur++ % LOGLINES], "unblock <port> (unblock port for further calls, load if not loaded)");
1130                                 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1131                         } else
1132                         {
1133                                 /* applend output to log window */
1134                                 SPRINT(buffer, "%s %s", argv[0], enter_string);
1135                                 fp = popen(buffer, "r");
1136                                 if (fp)
1137                                 {
1138                                         while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1139                                                 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1140                                         pclose(fp);
1141                                 } else
1142                                 {
1143                                         SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1144                                 }
1145                         }
1146                         logline[logcur % LOGLINES][0] = '\0';
1147                         enter_string[0] = '\0';
1148                         goto again;
1149                 }
1150                 if (ch>=32 && ch<=126)
1151                 {
1152                         SCCAT(enter_string, ch);
1153                         ch = getch();
1154                         if (ch > 0)
1155                                 goto enter_again;
1156                         goto again;
1157                 } else
1158                 if (ch==8 || ch==127)
1159                 {
1160                         if (enter_string[0])
1161                                 enter_string[strlen(enter_string)-1] = '\0';
1162                         ch = getch();
1163                         if (ch > 0)
1164                                 goto enter_again;
1165                         goto again;
1166                 } else
1167                 if (ch != 3)
1168                 {
1169                         ch = getch();
1170                         if (ch > 0)
1171                                 goto enter_again;
1172                         FD_ZERO(&select_rfds);
1173                         FD_SET(0, &select_rfds);
1174                         select_tv.tv_sec = 0;
1175                         select_tv.tv_usec = 250000;
1176                         select(1, &select_rfds, NULL, NULL, &select_tv);
1177                         goto again;
1178                 }
1179         } else
1180         {
1181                 /* user input in normal mode */
1182                 switch(getch())
1183                 {
1184                         case 12: /* refresh */
1185                         cleanup_curses();
1186                         init_curses();
1187                         goto again;
1188                         break;
1189
1190                         case 3: /* abort */
1191                         case 'q':
1192                         case 'Q':
1193                         break;
1194
1195                         case 'i': /* toggle interface */
1196                         show_interfaces++;
1197                         if (show_interfaces > 3) show_interfaces = 0;
1198                         goto again;
1199
1200                         case 'c': /* toggle calls */
1201                         show_calls++;
1202                         if (show_calls > 2) show_calls = 0;
1203                         goto again;
1204
1205                         case 'l': /* toggle log */
1206                         show_log++;
1207                         if (show_log > 1) show_log = 0;
1208                         goto again;
1209
1210                         case '+': /* scroll down */
1211                         offset++;
1212                         goto again;
1213                         
1214                         case '-': /* scroll up */
1215                         if (offset)
1216                                 offset--;
1217                         goto again;
1218
1219                         case '*': /* scroll right */
1220                         hoffset += 2;
1221                         goto again;
1222                         
1223                         case '/': /* scroll left */
1224                         if (hoffset)
1225                                 hoffset -= 2;
1226                         goto again;
1227
1228                         case 10: /* entermode */
1229                         enter = 1;
1230                         goto again;
1231
1232                         default:
1233                         FD_ZERO(&select_rfds);
1234                         FD_SET(0, &select_rfds);
1235                         select_tv.tv_sec = 0;
1236                         select_tv.tv_usec = 250000;
1237                         select(1, &select_rfds, NULL, NULL, &select_tv);
1238                         goto again;
1239                 }
1240         }
1241
1242         /* check for logfh */
1243         if (logfh >= 0)
1244                 close(logfh);
1245         logfh = -1;
1246
1247         /* cleanup curses and exit */
1248         cleanup_curses();
1249
1250         return(NULL);
1251 }
1252
1253
1254 /*
1255  * Send command and show error message.
1256  */
1257 char *admin_cmd(int sock, int mode, char *extension, char *number)
1258 {
1259         static struct admin_message msg;
1260
1261         /* send reload command */
1262         memset(&msg, 0, sizeof(msg));
1263         switch(mode)
1264         {
1265                 case MODE_INTERFACE:
1266                 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1267                 break;
1268                 case MODE_ROUTE:
1269                 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1270                 break;
1271                 case MODE_DIAL:
1272                 msg.message = ADMIN_REQUEST_CMD_DIAL;
1273                 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1274                 break;
1275                 case MODE_RELEASE:
1276                 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1277                 SCPY(msg.u.x.message, number);
1278                 break;
1279                 case MODE_UNBLOCK:
1280                 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1281                 msg.u.x.portnum = atoi(number);
1282                 msg.u.x.block = 0;
1283                 break;
1284                 case MODE_BLOCK:
1285                 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1286                 msg.u.x.portnum = atoi(number);
1287                 msg.u.x.block = 1;
1288                 break;
1289                 case MODE_UNLOAD:
1290                 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1291                 msg.u.x.portnum = atoi(number);
1292                 msg.u.x.block = 2;
1293                 break;
1294         }
1295
1296         if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1297                 return("Broken pipe while sending command.");
1298
1299         /* receive response */
1300         if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1301                 return("Broken pipe while receiving response.");
1302         switch(mode)
1303         {
1304                 case MODE_INTERFACE:
1305                 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1306                         return("Response not valid.");
1307                 break;
1308                 case MODE_ROUTE:
1309                 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1310                         return("Response not valid.");
1311                 break;
1312                 case MODE_DIAL:
1313                 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1314                         return("Response not valid.");
1315                 break;
1316                 case MODE_RELEASE:
1317                 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1318                         return("Response not valid.");
1319                 break;
1320                 case MODE_UNBLOCK:
1321                 case MODE_BLOCK:
1322                 case MODE_UNLOAD:
1323                 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1324                         return("Response not valid.");
1325                 break;
1326         }
1327
1328         /* process response */
1329         if (msg.u.x.error)
1330         {
1331                 return(msg.u.x.message);
1332         }
1333         printf("Command successfull.\n");
1334         return(NULL);
1335 }
1336
1337
1338 /*
1339  * makes a testcall
1340  */
1341 #define GET_NOW() { \
1342         gettimeofday(&now_tv, &now_tz); \
1343         now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1344         }
1345 char *admin_testcall(int sock, int argc, char *argv[])
1346 {
1347         static struct admin_message msg;
1348         int ar = 2;
1349         int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1350         int l;
1351         double timer = 0, now_d;
1352         unsigned int on = 1;
1353         struct timeval now_tv;
1354         struct timezone now_tz;
1355
1356         printf("pid=%d\n", getpid()); fflush(stdout);
1357
1358         while (argc > ar)
1359         {
1360                 if (!strcmp(argv[ar], "--setup-timeout"))
1361                 {
1362                         ar++;
1363                         if (argc == ar)
1364                                 return("Missing setup timeout value.\n");
1365                         stimeout = atoi(argv[ar]);
1366                         ar++;
1367                 } else
1368                 if (!strcmp(argv[ar], "--proceeding-timeout"))
1369                 {
1370                         ar++;
1371                         if (argc == ar)
1372                                 return("Missing proceeding timeout value.\n");
1373                         ptimeout = atoi(argv[ar]);
1374                         ar++;
1375                 } else
1376                 if (!strcmp(argv[ar], "--alerting-timeout"))
1377                 {
1378                         ar++;
1379                         if (argc == ar)
1380                                 return("Missing alerting timeout value.\n");
1381                         atimeout = atoi(argv[ar]);
1382                         ar++;
1383                 } else
1384                 if (!strcmp(argv[ar], "--connect-timeout"))
1385                 {
1386                         ar++;
1387                         if (argc == ar)
1388                                 return("Missing connect timeout value.\n");
1389                         ctimeout = atoi(argv[ar]);
1390                         ar++;
1391                 } else
1392                 {
1393                         break;
1394                 }
1395         }
1396
1397         /* send reload command */
1398         memset(&msg, 0, sizeof(msg));
1399         msg.message = ADMIN_CALL_SETUP;
1400         msg.u.call.present = 1;
1401
1402         if (argc > ar)
1403         {
1404                 SCPY(msg.u.call.interface, argv[ar]);
1405         }
1406         ar++;
1407         if (argc > ar)
1408         {
1409                 SCPY(msg.u.call.callerid, argv[ar]);
1410         }
1411         ar++;
1412         if (argc > ar)
1413         {
1414                 SCPY(msg.u.call.dialing, argv[ar]);
1415         }
1416         ar++;
1417         if (argc > ar)
1418         {
1419                 if (argv[ar][0] == 'r')
1420                         msg.u.call.present = 0;
1421         }
1422         ar++;
1423         msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1424         msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1425         msg.u.call.bc_info1 = 0;
1426         msg.u.call.hlc = 0;
1427         msg.u.call.exthlc = 0;
1428         if (argc > ar)
1429                 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1430         else
1431                 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1432         ar++;
1433         if (argc > ar) {
1434                 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1435                 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1436         }
1437         ar++;
1438         if (argc > ar) {
1439                 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1440                 if (msg.u.call.bc_info1 < 0)
1441                         msg.u.call.bc_info1 = 0;
1442                 else
1443                         msg.u.call.bc_info1 |= 0x80;
1444         }
1445         ar++;
1446         if (argc > ar) {
1447                 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1448                 if (msg.u.call.hlc < 0)
1449                         msg.u.call.hlc = 0;
1450                 else
1451                         msg.u.call.hlc |= 0x80;
1452         }
1453         ar++;
1454         if (argc > ar) {
1455                 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1456                 if (msg.u.call.exthlc < 0)
1457                         msg.u.call.exthlc = 0;
1458                 else
1459                         msg.u.call.exthlc |= 0x80;
1460         }
1461         ar++;
1462
1463         if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1464                 return("Broken pipe while sending command.");
1465
1466         if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1467                 return("Failed to set socket into non-blocking IO.");
1468
1469         if (stimeout)
1470         {
1471                 GET_NOW();
1472                 timer = now_d + (double)stimeout;
1473         }
1474         
1475         /* receive response */
1476 next:
1477         l = read(sock, &msg, sizeof(msg));
1478         if (l < 0)
1479         {
1480                 if (errno == EWOULDBLOCK)
1481                 {
1482                         if (timer)
1483                         {
1484                                 GET_NOW();
1485                                 if (timer <= now_d)
1486                                 {
1487                                         printf("Timeout\n"); fflush(stdout);
1488                                         return(NULL);
1489                                 }
1490                         }
1491                         usleep(30000);
1492                         goto next;
1493                 }
1494                 return("Broken pipe while receiving response.");
1495         }
1496         if (l != sizeof(msg))
1497                 return("Response has unexpected message size.");
1498         switch(msg.message)
1499         {
1500                 case ADMIN_CALL_SETUP_ACK:
1501                 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1502                 goto next;
1503
1504                 case ADMIN_CALL_PROCEEDING:
1505                 printf("PROCEEDING\n"); fflush(stdout);
1506                 if (ptimeout)
1507                 {
1508                         GET_NOW();
1509                         timer = now_d + (double)ptimeout;
1510                 }
1511                 goto next;
1512
1513                 case ADMIN_CALL_ALERTING:
1514                 printf("ALERTING\n"); fflush(stdout);
1515                 if (atimeout)
1516                 {
1517                         GET_NOW();
1518                         timer = now_d + (double)atimeout;
1519                 }
1520                 goto next;
1521
1522                 case ADMIN_CALL_CONNECT:
1523                 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1524                 if (ctimeout)
1525                 {
1526                         GET_NOW();
1527                         timer = now_d + (double)ctimeout;
1528                 }
1529                 goto next;
1530
1531                 case ADMIN_CALL_NOTIFY:
1532                 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1533                 goto next;
1534
1535                 case ADMIN_CALL_DISCONNECT:
1536                 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);
1537                 break;
1538
1539                 case ADMIN_CALL_RELEASE:
1540                 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);
1541                 break;
1542
1543                 default:
1544                 return("Response not valid.");
1545         }
1546
1547         printf("Call released.\n"); fflush(stdout);
1548         return(NULL);
1549 }
1550
1551
1552 /*
1553  * makes a trace
1554  */
1555 char *admin_trace(int sock, int argc, char *argv[])
1556 {
1557         static struct admin_message msg;
1558         int i;
1559
1560         /* show help */
1561         if (argc > 2) if (!strcasecmp(argv[2], "help"))
1562         {
1563                 printf("Trace Help\n----------\n");
1564                 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1565                 printf("By default a complete trace is shown in detailed format.\n");
1566                 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1567                 printf("Use filter values to select specific trace messages.\n");
1568                 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1569                 printf("Filters:\n");
1570                 printf(" category=<mask bits>\n");
1571                 printf("  0x01 = CH: channel object trace\n");
1572                 printf("  0x02 = EP: endpoint object trace\n");
1573                 printf(" port=<mISDN port>  select only given port for trace\n");
1574                 printf(" interface=<interface name>  select only given interface for trace\n");
1575                 printf(" caller=<caller id>  select only given caller id for trace\n");
1576                 printf(" dialing=<number>  select only given dialed number for trace\n");
1577                 return(NULL);
1578         }
1579
1580         /* init trace request */        
1581         memset(&msg, 0, sizeof(msg));
1582         msg.message = ADMIN_TRACE_REQUEST;
1583         msg.u.trace_req.detail = 3;
1584
1585         /* parse args */
1586         i = 2;
1587         while(i < argc)
1588         {
1589                 if (!strcasecmp(argv[i], "brief"))
1590                         msg.u.trace_req.detail = 1;
1591                 else if (!strcasecmp(argv[i], "short"))
1592                         msg.u.trace_req.detail = 2;
1593                 else if (!strncasecmp(argv[i], "category=", 9))
1594                         msg.u.trace_req.category = atoi(argv[i]+9);
1595                 else if (!strncasecmp(argv[i], "port=", 5))
1596                         msg.u.trace_req.port = atoi(argv[i]+5);
1597                 else if (!strncasecmp(argv[i], "interface=", 10))
1598                         SCPY(msg.u.trace_req.interface, argv[i]+10);
1599                 else if (!strncasecmp(argv[i], "caller=", 7))
1600                         SCPY(msg.u.trace_req.caller, argv[i]+7);
1601                 else if (!strncasecmp(argv[i], "dialing=", 8))
1602                         SCPY(msg.u.trace_req.dialing, argv[i]+8);
1603                 else return("Invalid trace option, try 'trace help'.");
1604
1605                 i++;
1606         }
1607
1608         /* send trace request */
1609         if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1610                 return("Broken pipe while sending trace request.");
1611
1612         /* receive response */
1613 next:
1614         if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1615                 return("Broken pipe while receiving response.");
1616
1617         if (msg.message != ADMIN_TRACE_RESPONSE)
1618                 return("Response not valid.");
1619
1620         printf("%s", msg.u.trace_rsp.text);
1621         goto next;
1622 }
1623
1624
1625 /*
1626  * main function
1627  */
1628 int main(int argc, char *argv[])
1629 {
1630         int mode;
1631         char *socket_name = SOCKET_NAME;
1632         int sock, conn;
1633         struct sockaddr_un sock_address;
1634         char *ret;
1635
1636
1637         /* show options */
1638         if (argc <= 1)
1639         {
1640                 usage:
1641                 printf("\n");
1642                 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1643                 printf("state - View current states using graphical console output.\n");
1644                 printf("interface - Tell LCR to reload \"interface.conf\".\n");
1645                 printf("route - Tell LCR to reload \"route.conf\".\n");
1646                 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1647                 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1648                 printf("block <port> - Block given port.\n");
1649                 printf("unblock <port> - Unblock given port.\n");
1650                 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1651                 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1652                 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1653                 printf("              --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1654                 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1655                 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1656                 printf(" -> Use 'trace help' to see filter description.\n");
1657                 printf("\n");
1658                 return(0);
1659         }
1660
1661         /* check mode */
1662         if (!(strcasecmp(argv[1],"state")))
1663         {
1664                 mode = MODE_STATE;
1665         } else
1666         if (!(strcasecmp(argv[1],"interface")))
1667         {
1668                 mode = MODE_INTERFACE;
1669         } else
1670         if (!(strcasecmp(argv[1],"route")))
1671         {
1672                 mode = MODE_ROUTE;
1673         } else
1674         if (!(strcasecmp(argv[1],"dial")))
1675         {
1676                 if (argc <= 3)
1677                         goto usage;
1678                 mode = MODE_DIAL;
1679         } else
1680         if (!(strcasecmp(argv[1],"release")))
1681         {
1682                 if (argc <= 2)
1683                         goto usage;
1684                 mode = MODE_RELEASE;
1685         } else
1686         if (!(strcasecmp(argv[1],"unblock")))
1687         {
1688                 if (argc <= 2)
1689                         goto usage;
1690                 mode = MODE_UNBLOCK;
1691         } else
1692         if (!(strcasecmp(argv[1],"block")))
1693         {
1694                 if (argc <= 2)
1695                         goto usage;
1696                 mode = MODE_BLOCK;
1697         } else
1698         if (!(strcasecmp(argv[1],"unload")))
1699         {
1700                 if (argc <= 2)
1701                         goto usage;
1702                 mode = MODE_UNLOAD;
1703         } else
1704         if (!(strcasecmp(argv[1],"testcall")))
1705         {
1706                 if (argc <= 4)
1707                         goto usage;
1708                 mode = MODE_TESTCALL;
1709         } else
1710         if (!(strcasecmp(argv[1],"trace")))
1711         {
1712                 mode = MODE_TRACE;
1713         } else
1714         {
1715                 goto usage;
1716         }
1717
1718 //pipeagain:
1719         /* open socket */
1720         if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1721         {
1722                 fprintf(stderr, "Failed to create socket.\n");
1723                 exit(EXIT_FAILURE);
1724         }
1725         memset(&sock_address, 0, sizeof(sock_address));
1726         sock_address.sun_family = PF_UNIX;
1727         UCPY(sock_address.sun_path, socket_name);
1728         if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1729         {
1730                 close(sock);
1731                 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1732                 exit(EXIT_FAILURE);
1733         }
1734
1735         /* process mode */
1736         switch(mode)
1737         {
1738                 case MODE_STATE:
1739                 ret = admin_state(sock, argv);
1740                 break;
1741         
1742                 case MODE_INTERFACE:
1743                 case MODE_ROUTE:
1744                 ret = admin_cmd(sock, mode, NULL, NULL);
1745                 break;
1746
1747                 case MODE_DIAL:
1748                 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1749                 break;
1750
1751                 case MODE_RELEASE:
1752                 case MODE_UNBLOCK:
1753                 case MODE_BLOCK:
1754                 case MODE_UNLOAD:
1755                 ret = admin_cmd(sock, mode, NULL, argv[2]);
1756                 break;
1757
1758                 case MODE_TESTCALL:
1759                 ret = admin_testcall(sock, argc, argv);
1760                 break;
1761
1762                 case MODE_TRACE:
1763                 ret = admin_trace(sock, argc, argv);
1764                 break;
1765         }
1766
1767         close(sock);
1768         /* now we say good bye */
1769         if (ret)
1770         {
1771 //              if (!strncasecmp(ret, "Broken Pipe", 11))
1772 //                      goto pipeagain;
1773                 printf("%s\n", ret);
1774                 exit(EXIT_FAILURE);
1775         }
1776 }
1777
1778
1779
1780
1781