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