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