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