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