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