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