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