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