backup
[lcr.git] / admin_client.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
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 <curses.h>
23 #include "save.h"
24 #include "call.h"
25 #include "callpbx.h"
26 #include "admin.h"
27 #include "cause.h"
28
29 #define LTEE {addch(ACS_LTEE);addch(ACS_HLINE);addch(ACS_HLINE);}
30 #define LLCORNER {addch(ACS_LLCORNER);addch(ACS_HLINE);addch(ACS_HLINE);}
31 #define VLINE {addch(ACS_VLINE);addstr("  ");}
32 #define EMPTY {addstr("   ");}
33 //char rotator[] = {'-', '\\', '|', '/'};
34 int     lastlines, lastcols;
35 int     show_interfaces = 2,
36         show_calls = 1,
37         show_log = 1;
38
39 enum {
40         MODE_STATE,
41         MODE_INTERFACE,
42         MODE_ROUTE,
43         MODE_DIAL,
44         MODE_RELEASE,
45         MODE_UNBLOCK,
46         MODE_BLOCK,
47         MODE_UNLOAD,
48         MODE_TESTCALL,
49         MODE_TRACE,
50 };
51
52 char *text_interfaces[] = {
53         "off",
54         "brief",
55         "active channels",
56         "all channels",
57 };
58
59 char *text_calls[] = {
60         "off",
61         "brief",
62         "structured",
63 };
64
65 char    red = 1,
66         green = 2,
67         yellow = 3,
68         blue = 4,
69         mangenta = 5,
70         cyan = 6,
71         white = 7;
72
73 #define LOGLINES 128
74 char logline[LOGLINES][256];
75 unsigned long logcur = 0;
76 int logfh = -1;
77 char logfile[128];
78
79 /*
80  * curses
81  */
82 void init_curses(void)
83 {
84         /* init curses */
85         initscr(); cbreak(); noecho();
86         start_color();
87         nodelay(stdscr, TRUE);
88         if (COLOR_PAIRS>=8 && COLORS>=8)
89         {
90                 init_pair(1,1,0);
91                 init_pair(2,2,0);
92                 init_pair(3,3,0);
93                 init_pair(4,4,0);
94                 init_pair(5,5,0);
95                 init_pair(6,6,0);
96                 init_pair(7,7,0);
97         }
98         lastlines = LINES;
99         lastcols = COLS;
100 }
101
102 void cleanup_curses(void)
103 {
104         endwin();
105 }
106
107 void color(int color)
108 {
109         if (COLOR_PAIRS>=8 && COLORS>=8)
110                 attrset(COLOR_PAIR(color));
111 }
112
113 /*
114  * permanently show current state using ncurses
115  */
116 int debug_port(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
117 {
118         char buffer[256];
119
120         color(white);
121         addstr("PORT:");
122         color(yellow);
123         SPRINT(buffer,"%s(%d)", m[i].u.p.name,m[i].u.p.serial);
124         addstr(buffer);
125         color(cyan);
126         addstr(" state=");
127         switch (m[i].u.p.state)
128         {
129                 case ADMIN_STATE_IDLE:
130                 color(red);
131                 addstr("'idle'");
132                 break;
133                 case ADMIN_STATE_IN_SETUP:
134                 color(red);
135                 addstr("'in << setup'");
136                 break;
137                 case ADMIN_STATE_OUT_SETUP:
138                 color(red);
139                 addstr("'out >> setup'");
140                 break;
141                 case ADMIN_STATE_IN_OVERLAP:
142                 color(yellow);
143                 addstr("'in << overlap'");
144                 break;
145                 case ADMIN_STATE_OUT_OVERLAP:
146                 color(yellow);
147                 addstr("'out >> overlap'");
148                 break;
149                 case ADMIN_STATE_IN_PROCEEDING:
150                 color(mangenta);
151                 addstr("'in << proc'");
152                 break;
153                 case ADMIN_STATE_OUT_PROCEEDING:
154                 color(mangenta);
155                 addstr("'out >> proc'");
156                 break;
157                 case ADMIN_STATE_IN_ALERTING:
158                 color(cyan);
159                 addstr("'in << alert'");
160                 break;
161                 case ADMIN_STATE_OUT_ALERTING:
162                 color(cyan);
163                 addstr("'out >> alert'");
164                 break;
165                 case ADMIN_STATE_CONNECT:
166                 color(white);
167                 addstr("'connect'");
168                 break;
169                 case ADMIN_STATE_IN_DISCONNECT:
170                 color(blue);
171                 addstr("'in  << disc'");
172                 break;
173                 case ADMIN_STATE_OUT_DISCONNECT:
174                 color(blue);
175                 addstr("'out >> disc'");
176                 break;
177                 default:
178                 color(blue);
179                 addstr("'--NONE--'");
180         }
181
182         if (m[i].u.p.isdn)
183         {       
184                 color(cyan);
185                 addstr(" bchannel=");
186                 color(white);
187                 SPRINT(buffer,"%d", m[i].u.p.isdn_chan);
188                 addstr(buffer);
189                 if (m[i].u.p.isdn_ces >= 0)
190                 {
191                         color(cyan);
192                         addstr(" ces=");
193                         color(yellow);
194                         SPRINT(buffer, "%d", m[i].u.p.isdn_ces);
195                         addstr(buffer);
196                 }
197                 if (m[i].u.p.isdn_hold)
198                 {
199                         color(red);
200                         addstr(" hold");
201                 }
202         }
203
204         return(line);
205 }
206 int debug_epoint(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
207 {
208         unsigned long epoint = m[i].u.e.serial;
209         char buffer[256];
210         unsigned char c;
211         int j, jj;
212         int ltee;
213
214         color(white);
215         SPRINT(buffer,"EPOINT(%d)", epoint);
216         addstr(buffer);
217         color(cyan);
218         addstr(" state=");
219         switch (m[i].u.e.state)
220         {
221                 case ADMIN_STATE_IDLE:
222                 color(red);
223                 addstr("'idle'");
224                 break;
225                 case ADMIN_STATE_IN_SETUP:
226                 color(red);
227                 addstr("'in << setup'");
228                 break;
229                 case ADMIN_STATE_OUT_SETUP:
230                 color(red);
231                 addstr("'out >> setup'");
232                 break;
233                 case ADMIN_STATE_IN_OVERLAP:
234                 color(yellow);
235                 addstr("'in << overlap'");
236                 break;
237                 case ADMIN_STATE_OUT_OVERLAP:
238                 color(yellow);
239                 addstr("'out >> overlap'");
240                 break;
241                 case ADMIN_STATE_IN_PROCEEDING:
242                 color(mangenta);
243                 addstr("'in << proc'");
244                 break;
245                 case ADMIN_STATE_OUT_PROCEEDING:
246                 color(mangenta);
247                 addstr("'out >> proc'");
248                 break;
249                 case ADMIN_STATE_IN_ALERTING:
250                 color(cyan);
251                 addstr("'in << alert'");
252                 break;
253                 case ADMIN_STATE_OUT_ALERTING:
254                 color(cyan);
255                 addstr("'out >> alert'");
256                 break;
257                 case ADMIN_STATE_CONNECT:
258                 color(white);
259                 addstr("'connect'");
260                 break;
261                 case ADMIN_STATE_IN_DISCONNECT:
262                 color(blue);
263                 addstr("'in  << disc'");
264                 break;
265                 case ADMIN_STATE_OUT_DISCONNECT:
266                 color(blue);
267                 addstr("'out >> disc'");
268                 break;
269                 default:
270                 color(blue);
271                 addstr("'--NONE--'");
272         }
273         if (m[i].u.e.terminal[0])
274         {
275                 color(cyan);
276                 addstr(" terminal=");
277                 color(green);
278                 addstr(m[i].u.e.terminal);
279         }
280         color(white);
281         SPRINT(buffer, " %s", m[i].u.e.callerid);
282         addstr(buffer);
283         color(cyan);
284         addstr("->");
285         color(white);
286         addstr(m[i].u.e.dialing);
287         if (m[i].u.e.action[0])
288         {
289                 color(cyan);
290                 addstr(" action=");
291                 color(yellow);
292                 addstr(m[i].u.e.action);
293         }
294         if (m[i].u.e.park)
295         {
296                 color(cyan);
297                 addstr(" park="); /* 9 digits */
298                 color(green);
299                 UCPY(buffer, "\""); /* 9 digits */
300                 j = 0;
301                 jj = m[i].u.e.park_len;
302                 while(j < jj)
303                 {
304                         c = m[i].u.e.park_callid[j];
305                         if (c >= 32 && c < 127 && c != '[')
306                         {
307                                 SCCAT(buffer, c);
308                         } else
309                                 UPRINT(buffer+strlen(buffer), "[%02x]", c);
310                         j++;
311                 }
312                 SCAT(buffer, "\"");
313                 addstr(buffer);
314         } else
315         {
316                 color(red);
317                 switch(m[i].u.e.rx_state)
318                 {
319                         case NOTIFY_STATE_SUSPEND:
320                         addstr(" in=suspend");
321                         break;
322                         case NOTIFY_STATE_HOLD:
323                         addstr(" in=hold");
324                         break;
325                         case NOTIFY_STATE_CONFERENCE:
326                         addstr(" in=conference");
327                         break;
328                 }
329                 switch(m[i].u.e.tx_state)
330                 {
331                         case NOTIFY_STATE_SUSPEND:
332                         addstr(" out=suspend");
333                         break;
334                         case NOTIFY_STATE_HOLD:
335                         addstr(" out=hold");
336                         break;
337                         case NOTIFY_STATE_CONFERENCE:
338                         addstr(" out=conference");
339                         break;
340                 }
341         }
342         if (m[i].u.e.crypt)
343         {
344                 color(cyan);
345                 addstr(" crypt=");
346                 if (m[i].u.e.crypt) /* crypt on */
347                 {
348                         color(green);
349                         addstr("active");
350                 } else
351                 {
352                         color(yellow);
353                         addstr("pending");
354                 }
355         }
356         /* loop all related ports */
357         ltee = 0;
358         j = msg->u.s.interfaces+msg->u.s.calls+msg->u.s.epoints;
359         jj = j + msg->u.s.ports;
360         while(j < jj)
361         {
362                 if (m[j].u.p.epoint == epoint)
363                 {
364                         color(cyan);
365                         move(++line>1?line:1, 1);
366                         if (vline)
367                                 VLINE
368                         else
369                                 EMPTY
370                         move(line>1?line:1, 5);
371                         LTEE
372                         ltee = line;
373                         move(line>1?line:1, 8);
374                         if (line+2 >= LINES) break;
375                         line = debug_port(msg, m, line, j, vline);
376                         if (line+2 >= LINES) break;
377                 }
378                 j++;
379         }
380         if (ltee)
381         {
382                 color(cyan);
383                 move(ltee>1?line:1, 5);
384                 LLCORNER
385         }
386
387         return(line);
388 }
389 int debug_call(struct admin_message *msg, struct admin_message *m, int line, int i)
390 {
391         unsigned long   call = m[i].u.c.serial;
392         char            buffer[256];
393         int             j, jj;
394
395         color(white);
396         SPRINT(buffer,"CALL(%d)", call);
397         addstr(buffer);
398         if (m[i].u.c.partyline)
399         {
400                 color(cyan);
401                 addstr(" partyline=");
402                 color(white);
403                 SPRINT(buffer, "%d\n", m[i].u.c.partyline);
404                 addstr(buffer);
405         }
406         /* find number of epoints */
407         j = msg->u.s.interfaces+msg->u.s.calls;
408         jj = j + msg->u.s.epoints;
409         i = 0;
410         while(j < jj)
411         {
412                 if (m[j].u.e.call == call)
413                         i++;
414                 j++;
415         }
416         /* loop all related endpoints */
417         j = msg->u.s.interfaces+msg->u.s.calls;
418         jj = j + msg->u.s.epoints;
419         while(j < jj)
420         {
421                 if (m[j].u.e.call == call)
422                 {
423                         i--;
424                         move(++line>1?line:1, 1);
425                         color(cyan);
426                         if (i)
427                                 LTEE
428                         else
429                                 LLCORNER
430                         move(line>1?line:1, 4);
431                         if (line+2 >= LINES) break;
432                         line = debug_epoint(msg, m, line, j, i?1:0);
433                         if (line+2 >= LINES) break;
434                 }
435                 j++;
436         }
437
438         return(line);
439 }
440 char *admin_state(int sock)
441 {
442         struct admin_message    msg,
443                                 *m;
444         char                    buffer[256],
445                                 *p;
446         int                     line, offset = 0;
447         int                     i, ii, j, jj, k;
448         unsigned long           l, ll;
449         int                     num;
450         int                     len;
451         int                     off;
452         int                     ltee;
453         int                     anything;
454
455         /* flush logfile name */
456         logfile[0] = '\0';
457
458         /* init curses */
459         init_curses();
460
461         again:
462         /* send reload command */
463         memset(&msg, 0, sizeof(msg));
464         msg.message = ADMIN_REQUEST_STATE;
465 //      printf("sizeof=%d\n",sizeof(msg));fflush(stdout);
466         if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
467         {
468                 cleanup_curses();
469                 return("Broken pipe while sending command.");
470         }
471
472         /* receive response */
473         if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
474         {
475                 cleanup_curses();
476                 return("Broken pipe while receiving response.");
477         }
478
479         if (msg.message != ADMIN_RESPONSE_STATE)
480         {
481                 cleanup_curses();
482                 return("Response not valid. Expecting state response.");
483         }
484         num = msg.u.s.interfaces + msg.u.s.calls + msg.u.s.epoints + msg.u.s.ports;
485         if (!(m = (struct admin_message *)malloc(num*sizeof(struct admin_message))))
486         {
487                 cleanup_curses();
488                 return("Not enough memory for messages.");
489         }
490         off=0;
491         if (num)
492         {
493                 readagain:
494                 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off)
495                 {
496                         if (len <= 0) {
497                                 free(m);
498         //                      fprintf(stderr, "got=%d expected=%d\n", i, num*sizeof(struct admin_message));
499                                 cleanup_curses();
500                                 return("Broken pipe while receiving state infos.");
501                         }
502                         if (len < num*(int)sizeof(struct admin_message))
503                         {
504                                 off+=len;
505                                 goto readagain;
506                         }
507                 }
508         }
509         j = 0;
510         i = 0;
511 //      fprintf("getting =%d interfaces\n", msg.u.s.interfaces);
512         while(i < msg.u.s.interfaces)
513         {
514 //              fprintf(stderr, "j=%d message=%d\n", j, m[j].message);
515                 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE)
516                 {
517                         free(m);
518                         cleanup_curses();
519                         return("Response not valid. Expecting interface information.");
520                 }
521                 i++;
522                 j++;
523         }
524         i = 0;
525         while(i < msg.u.s.calls)
526         {
527                 if (m[j].message != ADMIN_RESPONSE_S_CALL)
528                 {
529                         free(m);
530                         cleanup_curses();
531                         return("Response not valid. Expecting call information.");
532                 }
533                 i++;
534                 j++;
535         }
536         i = 0;
537         while(i < msg.u.s.epoints)
538         {
539                 if (m[j].message != ADMIN_RESPONSE_S_EPOINT)
540                 {
541                         free(m);
542                         cleanup_curses();
543                         return("Response not valid. Expecting endpoint information.");
544                 }
545                 i++;
546                 j++;
547         }
548         i = 0;
549         while(i < msg.u.s.ports)
550         {
551                 if (m[j].message != ADMIN_RESPONSE_S_PORT)
552                 {
553                         free(m);
554                         cleanup_curses();
555                         return("Response not valid. Expecting port information.");
556                 }
557                 i++;
558                 j++;
559         }
560         // now j is the number of message blocks
561
562         /* display start */
563         erase();
564
565         line = 1-offset; 
566
567         /* change log */
568         if (!!strcmp(logfile, msg.u.s.logfile))
569         {
570                 SCPY(logfile, msg.u.s.logfile);
571                 if (logfh >= 0)
572                         close(logfh);
573                 i = 0;
574                 ii = LOGLINES;
575                 while(i < ii)
576                 {
577                         logline[i][0] = '~';
578                         logline[i][1] = '\0';
579                         i++;
580                 }
581                 logcur = 0;
582                 logfh = open(logfile, O_RDONLY|O_NONBLOCK);
583                 if (logfh >= 0)
584                 {
585                         /* seek at the end -8000 chars */
586                         lseek(logfh, -8000, SEEK_END);
587                         /* if not at the beginning, read until endofline */
588                         logline[logcur % LOGLINES][0] = '\0';
589                         l = read(logfh, logline[logcur % LOGLINES], sizeof(logline[logcur % LOGLINES])-1);
590                         if (l > 0)
591                         {
592                                 /* read first line and skip junk */
593                                 logline[logcur % LOGLINES][l] = '\0';
594                                 if ((p = strchr(logline[logcur % LOGLINES],'\n')))
595                                 {
596                                         logcur++;
597                                         SCPY(logline[logcur % LOGLINES], p+1);
598                                         SCPY(logline[(logcur-1) % LOGLINES], "...");
599                                 }
600                                 goto finish_line;
601                         }
602                 }
603         }
604
605         /* read log */
606         if (logfh >= 0)
607         {
608                 while(42)
609                 {
610                         ll = strlen(logline[logcur % LOGLINES]);
611                         l = read(logfh, logline[logcur % LOGLINES]+ll, sizeof(logline[logcur % LOGLINES])-ll-1);
612                         if (l<=0)
613                                 break;
614                         logline[logcur % LOGLINES][ll+l] = '\0';
615                         finish_line:
616                         /* put data to lines */
617                         while ((p = strchr(logline[logcur % LOGLINES],'\n')))
618                         {
619                                 *p = '\0';
620                                 logcur++;
621                                 SCPY(logline[logcur % LOGLINES], p+1);
622                         }
623                         /* if line is full without return, go next line */
624                         if (strlen(logline[logcur % LOGLINES]) == sizeof(logline[logcur % LOGLINES])-1)
625                         {
626                                 logcur++;
627                                 logline[logcur % LOGLINES][0] = '\0';
628                         }
629                 }
630         }
631
632         /* display interfaces */
633         if (show_interfaces > 0)
634         {
635                 anything = 0;
636                 i = 0;
637                 ii = i + msg.u.s.interfaces;
638                 while(i < ii)
639                 {
640                         /* show interface summary */
641                         move(++line>1?line:1, 0);
642                         color(white);
643                         if (m[i].u.i.block >= 2)
644                         {
645                                 SPRINT(buffer, "%s (%d)%s", m[i].u.i.interface_name, m[i].u.i.portnum, (m[i].u.i.extension)?" (extension)":"");
646                                 addstr(buffer);
647                                 color(red);
648                                 addstr("  not loaded");
649                         } else
650                         {
651                                 SPRINT(buffer, "%s (%d) %s %s%s use:%d", m[i].u.i.interface_name, m[i].u.i.portnum, (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);
652                                 addstr(buffer);
653                                 if (m[i].u.i.ptp || !m[i].u.i.ntmode)
654                                 {
655                                         color((m[i].u.i.l2link)?green:red);
656                                         addstr((m[i].u.i.l2link)?"  L2 UP":"  L2 down");
657                                 }
658                                 color((m[i].u.i.l1link)?green:blue);
659                                 addstr((m[i].u.i.l1link)?"  L1 ACTIVE":"  L1 inactive");
660                                 if (m[i].u.i.block)
661                                 {
662                                         color(red);
663                                         addstr("  blocked");
664                                 }
665                                 if (line+2 >= LINES) goto end;
666                                 /* show channels */
667                                 if (show_interfaces > 1)
668                                 {
669                                         ltee = 0;
670                                         j = k =0;
671                                         jj = m[i].u.i.channels;
672                                         while(j < jj)
673                                         {
674                                                 /* show all channels */
675                                                 if (show_interfaces>2 || m[i].u.i.busy[j]>0)
676                                                 {
677                                                         color(cyan);
678                                                         /* show left side / right side */
679                                                         if ((k & 1) && (COLS > 70))
680                                                         {
681                                                                 move(line>1?line:1,4+((COLS-4)/2));
682                                                         } else
683                                                         {
684                                                                 move(++line>1?line:1, 1);
685                                                                 LTEE
686                                                                 ltee = 1;
687                                                         }
688                                                         k++;
689                                                         color(white);
690                                                         if (m[i].u.i.pri)
691                                                                 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
692                                                         else
693                                                                 SPRINT(buffer,"B%2d: ", j+1);
694                                                         addstr(buffer);
695                                                         if (!m[i].u.i.ptp)
696                                                                 goto ptmp;
697                                                         if (m[i].u.i.l2link && m[i].u.i.block==0)
698                                                         {
699                                                                 ptmp:
700                                                                 color((m[i].u.i.busy[j])?yellow:blue);
701                                                                 addstr((m[i].u.i.busy[j])?"busy":"idle");
702                                                         } else
703                                                         {
704                                                                 color(red);
705                                                                 addstr("blk ");
706                                                         }
707                                                         if (m[i].u.i.port[j])
708                                                         {
709                                                                 /* search for port */
710                                                                 l = msg.u.s.interfaces+msg.u.s.calls+msg.u.s.epoints;
711                                                                 ll = l+msg.u.s.ports;
712                                                                 while(l < ll)
713                                                                 {
714                                                                         if (m[l].u.p.serial == m[i].u.i.port[j])
715                                                                         {
716                                                                                 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
717                                                                                 addstr(buffer);
718                                                                         }
719                                                                         l++;
720                                                                 }
721                                                         }
722                                                         if (line+2 >= LINES)
723                                                         {
724                                                                 if (ltee)
725                                                                 {
726                                                                         color(cyan);
727                                                                         move(line>1?line:1, 1);
728                                                                         LLCORNER
729                                                                 }
730                                                                 goto end;
731                                                         }
732                                                 }
733                                                 j++;
734                                         }
735                                         if (ltee)
736                                         {
737                                                 color(cyan);
738                                                 move(line>1?line:1, 1);
739                                                 LLCORNER
740                                         }
741                                         if (line+2 >= LINES) goto end;
742                                         /* show summary if no channels were shown */
743                                         if (show_interfaces<2 && ltee==0)
744                                         {
745                                                 color(cyan);
746                                                 move(++line>1?line:1, 1);
747                                                 LLCORNER
748                                                         
749                                                 if (m[i].u.i.l2link && m[i].u.i.block==0)
750                                                 {
751                                                         color(green);
752                                                         SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
753                                                 } else
754                                                 {
755                                                         color(red);
756                                                         SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
757                                                 }
758                                                 addstr(buffer);
759                                         }
760                                         if (line+2 >= LINES) goto end;
761                                 }
762                         }
763                         i++;
764                         anything = 1;
765                 }
766                 if (anything)
767                         line++;
768                 if (line+2 >= LINES) goto end;
769         }               
770         /* display calls (brief) */
771         if (show_calls == 1)
772         {
773                 anything = 0;
774                 i = msg.u.s.interfaces+msg.u.s.calls;
775                 ii = i+msg.u.s.epoints;
776                 while(i < ii)
777                 {
778                         /* for each endpoint... */
779                         if (!m[i].u.e.call)
780                         {
781                                 move(++line>1?line:1, 0);
782                                 color(white);
783                                 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
784                                 addstr(buffer);
785                                 color(cyan);
786                                 if (m[i].u.e.terminal[0])
787                                 {
788                                         addstr("intern=");
789                                         color(green);
790                                         addstr(m[i].u.e.terminal);
791                                 } else
792                                         addstr("extern");
793                                 color(white);
794                                 SPRINT(buffer, " %s", m[i].u.e.callerid);
795                                 addstr(buffer);
796                                 color(cyan);
797                                 addstr("->");
798                                 color(white);
799                                 SPRINT(buffer, "%s", m[i].u.e.dialing);
800                                 addstr(buffer);
801                                 if (m[i].u.e.action[0])
802                                 {
803                                         color(cyan);
804                                         addstr(" action=");
805                                         color(yellow);
806                                         addstr(m[i].u.e.action);
807                                 }
808                                 if (line+2 >= LINES) goto end;
809                         }
810                         i++;
811                         anything = 1;
812                 }
813                 j = msg.u.s.interfaces;
814                 jj = j+msg.u.s.calls;
815                 while(j < jj)
816                 {
817                         /* for each call... */
818                         move(++line>1?line:1, 0);
819                         color(white);
820                         SPRINT(buffer, "(%d):", m[j].u.c.serial);
821                         addstr(buffer);
822                         i = msg.u.s.interfaces+msg.u.s.calls;
823                         ii = i+msg.u.s.epoints;
824                         while(i < ii)
825                         {
826                                 /* for each endpoint... */
827                                 if (m[i].u.e.call == m[j].u.c.serial)
828                                 {
829                                         color(white);
830                                         SPRINT(buffer, " (%d)", m[i].u.e.serial);
831                                         addstr(buffer);
832                                         color(cyan);
833                                         if (m[i].u.e.terminal[0])
834                                         {
835                                                 addstr("int=");
836                                                 color(green);
837                                                 addstr(m[i].u.e.terminal);
838                                         } else
839                                                 addstr("ext");
840                                         color(white);
841                                         SPRINT(buffer, "-%s", m[i].u.e.callerid);
842                                         addstr(buffer);
843                                         color(cyan);
844                                         addstr(">");
845                                         color(white);
846                                         SPRINT(buffer, "%s", m[i].u.e.dialing);
847                                         addstr(buffer);
848                                 }
849                                 i++;
850                                 anything = 1;
851                         }
852                         if (line+2 >= LINES) goto end;
853                         j++;
854                 }
855                 if (anything)
856                         line++;
857                 if (line+2 >= LINES) goto end;
858         }
859         /* display calls (structurd) */
860         if (show_calls == 2)
861         {
862                 /* show all ports with no epoint */
863                 anything = 0;
864                 i = msg.u.s.interfaces+msg.u.s.calls+msg.u.s.epoints;
865                 ii = i+msg.u.s.ports;
866                 while(i < ii)
867                 {
868                         if (!m[i].u.p.epoint)
869                         {
870                                 move(++line>1?line:1, 8);
871                                 if (line+2 >= LINES) goto end;
872                                 line = debug_port(&msg, m, line, i, 0);
873                                 if (line+2 >= LINES) goto end;
874                                 anything = 1;
875                         }
876                         i++;
877                 }
878                 if (anything)
879                         line++;
880                 if (line+2 >= LINES) goto end;
881
882                 /* show all epoints with no call */
883                 anything = 0;
884                 i = msg.u.s.interfaces+msg.u.s.calls;
885                 ii = i+msg.u.s.epoints;
886                 while(i < ii)
887                 {
888                         if (!m[i].u.e.call)
889                         {
890                                 move(++line>1?line:1, 4);
891                                 if (line+2 >= LINES) goto end;
892                                 line = debug_epoint(&msg, m, line, i, 0);
893                                 if (line+2 >= LINES) goto end;
894                                 anything = 1;
895                         }
896                         i++;
897                 }
898                 if (anything)
899                         line++;
900                 if (line+2 >= LINES) goto end;
901
902                 /* show all calls */
903                 anything = 0;
904                 i = msg.u.s.interfaces;
905                 ii = i+msg.u.s.calls;
906                 while(i < ii)
907                 {
908                         move(++line>1?line:1, 0);
909                         if (line+2 >= LINES) goto end;
910                         line = debug_call(&msg, m, line, i);
911                         if (line+2 >= LINES) goto end;
912                         i++;
913                         anything = 1;
914                 }
915                 if (anything)
916                         line++;
917                 if (line+2 >= LINES) goto end;
918
919         }
920
921         /* show log */
922         if (show_log)
923         {
924                 if (line+2 < LINES)
925                 {
926                         move(line++>1?line-1:1, 0);
927                         color(blue);
928                         hline(ACS_HLINE, COLS);
929                         color(white);
930                         
931                         l = logcur-(LINES-line-2);
932                         ll = logcur;
933                         if (ll-l >= LOGLINES)
934                                 l = ll-LOGLINES+1;
935                         while(l!=ll)
936                         {
937                                 move(line++>1?line-1:1, 0);
938                                 SCPY(buffer, logline[l % LOGLINES]);
939                                 if (COLS < (int)sizeof(buffer))
940                                         buffer[COLS] = '\0';
941                                 addstr(buffer);
942                                 l++;
943                         }
944                 }
945         }
946
947         end:
948         /* free memory */
949         free(m);
950         /* display name/time */
951 //      move(0, 0);
952 //      hline(' ', COLS);
953         move(0, 0);
954         color(white);
955         msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
956         SPRINT(buffer, "LCR %s", msg.u.s.version_string);
957         addstr(buffer);
958         if (COLS>50)
959         {
960                 move(0, COLS-19);
961                 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
962                         msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
963                         msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
964                 addstr(buffer);
965         }
966         /* displeay head line */
967         move(1, 0);
968         color(blue);
969         hline(ACS_HLINE, COLS);
970         if (offset)
971         {
972                 move(1, 1);
973                 SPRINT(buffer, "Offset +%d", offset);
974                 color(red);
975                 addstr(buffer);
976         }
977         /* display end */
978         move(LINES-2, 0);
979         color(white);
980         hline(ACS_HLINE, COLS);
981         move(LINES-1, 0);
982         color(white);
983         SPRINT(buffer, "i = interfaces '%s'  c = calls '%s'  l = log  q = quit  +/- = scroll", text_interfaces[show_interfaces], text_calls[show_calls]);
984         addstr(buffer);
985         refresh();
986
987         /* resize */
988         if (lastlines!=LINES || lastcols!=COLS)
989         {
990                 cleanup_curses();
991                 init_curses();
992                 goto again;
993         }
994
995         /* user input */
996         switch(getch())
997         {
998                 case 12: /* refresh */
999                 cleanup_curses();
1000                 init_curses();
1001                 goto again;
1002                 break;
1003
1004                 case 3: /* abort */
1005                 case 'q':
1006                 case 'Q':
1007                 break;
1008
1009                 case 'i': /* toggle interface */
1010                 show_interfaces++;
1011                 if (show_interfaces > 3) show_interfaces = 0;
1012                 goto again;
1013
1014                 case 'c': /* toggle calls */
1015                 show_calls++;
1016                 if (show_calls > 2) show_calls = 0;
1017                 goto again;
1018
1019                 case 'l': /* toggle log */
1020                 show_log++;
1021                 if (show_log > 1) show_log = 0;
1022                 goto again;
1023
1024                 case '+': /* scroll down */
1025                 offset++;
1026                 goto again;
1027                 
1028                 case '-': /* scroll up */
1029                 if (offset)
1030                         offset--;
1031                 goto again;
1032
1033                 default:
1034                 usleep(250000);
1035                 goto again;
1036         }
1037
1038         /* check for logfh */
1039         if (logfh >= 0)
1040                 close(logfh);
1041         logfh = -1;
1042
1043         /* cleanup curses and exit */
1044         cleanup_curses();
1045
1046         return(NULL);
1047 }
1048
1049
1050 /*
1051  * Send command and show error message.
1052  */
1053 char *admin_cmd(int sock, int mode, char *extension, char *number)
1054 {
1055         static struct admin_message msg;
1056
1057         /* send reload command */
1058         memset(&msg, 0, sizeof(msg));
1059         switch(mode)
1060         {
1061                 case MODE_INTERFACE:
1062                 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1063                 break;
1064                 case MODE_ROUTE:
1065                 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1066                 break;
1067                 case MODE_DIAL:
1068                 msg.message = ADMIN_REQUEST_CMD_DIAL;
1069                 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1070                 break;
1071                 case MODE_RELEASE:
1072                 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1073                 SCPY(msg.u.x.message, number);
1074                 break;
1075                 case MODE_UNBLOCK:
1076                 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1077                 msg.u.x.portnum = atoi(number);
1078                 msg.u.x.block = 0;
1079                 break;
1080                 case MODE_BLOCK:
1081                 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1082                 msg.u.x.portnum = atoi(number);
1083                 msg.u.x.block = 1;
1084                 break;
1085                 case MODE_UNLOAD:
1086                 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1087                 msg.u.x.portnum = atoi(number);
1088                 msg.u.x.block = 2;
1089                 break;
1090         }
1091
1092         if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1093                 return("Broken pipe while sending command.");
1094
1095         /* receive response */
1096         if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1097                 return("Broken pipe while receiving response.");
1098         switch(mode)
1099         {
1100                 case MODE_INTERFACE:
1101                 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1102                         return("Response not valid.");
1103                 break;
1104                 case MODE_ROUTE:
1105                 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1106                         return("Response not valid.");
1107                 break;
1108                 case MODE_DIAL:
1109                 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1110                         return("Response not valid.");
1111                 break;
1112                 case MODE_RELEASE:
1113                 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1114                         return("Response not valid.");
1115                 break;
1116                 case MODE_UNBLOCK:
1117                 case MODE_BLOCK:
1118                 case MODE_UNLOAD:
1119                 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1120                         return("Response not valid.");
1121                 break;
1122         }
1123
1124         /* process response */
1125         if (msg.u.x.error)
1126         {
1127                 return(msg.u.x.message);
1128         }
1129         printf("Command successfull.\n");
1130         return(NULL);
1131 }
1132
1133
1134 /*
1135  * makes a testcall
1136  */
1137 char *admin_testcall(int sock, int argc, char *argv[])
1138 {
1139         static struct admin_message msg;
1140
1141         printf("pid=%d\n", getpid()); fflush(stdout);
1142
1143         /* send reload command */
1144         memset(&msg, 0, sizeof(msg));
1145         msg.message = ADMIN_CALL_SETUP;
1146         if (argc > 2)
1147         {
1148                 SCPY(msg.u.call.interface, argv[2]);
1149         }
1150         if (argc > 3)
1151         {
1152                 SCPY(msg.u.call.callerid, argv[3]);
1153         }
1154         if (argc > 4)
1155         {
1156                 SCPY(msg.u.call.dialing, argv[4]);
1157         }
1158         if (argc > 5)
1159         {
1160                 if (argv[5][0] == 'p')
1161                         msg.u.call.present = 1;
1162         }
1163         msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1164         msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1165         msg.u.call.bc_info1 = 0;
1166         msg.u.call.hlc = 0;
1167         msg.u.call.exthlc = 0;
1168         if (argc > 6)
1169                 msg.u.call.bc_capa = strtol(argv[6],NULL,0);
1170         else
1171                 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1172         if (argc > 7) {
1173                 msg.u.call.bc_mode = strtol(argv[7],NULL,0);
1174                 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1175         }
1176         if (argc > 8) {
1177                 msg.u.call.bc_info1 = strtol(argv[8],NULL,0);
1178                 if (msg.u.call.bc_info1 < 0)
1179                         msg.u.call.bc_info1 = 0;
1180                 else
1181                         msg.u.call.bc_info1 |= 0x80;
1182         }
1183         if (argc > 9) {
1184                 msg.u.call.hlc = strtol(argv[9],NULL,0);
1185                 if (msg.u.call.hlc < 0)
1186                         msg.u.call.hlc = 0;
1187                 else
1188                         msg.u.call.hlc |= 0x80;
1189         }
1190 //              printf("hlc=%d\n",  msg.u.call.hlc);
1191         if (argc > 10) {
1192                 msg.u.call.exthlc = strtol(argv[10],NULL,0);
1193                 if (msg.u.call.exthlc < 0)
1194                         msg.u.call.exthlc = 0;
1195                 else
1196                         msg.u.call.exthlc |= 0x80;
1197         }
1198
1199         if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1200                 return("Broken pipe while sending command.");
1201
1202         /* receive response */
1203 next:
1204         if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1205                 return("Broken pipe while receiving response.");
1206         switch(msg.message)
1207         {
1208                 case ADMIN_CALL_SETUP_ACK:
1209                 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1210                 goto next;
1211
1212                 case ADMIN_CALL_PROCEEDING:
1213                 printf("PROCEEDING\n"); fflush(stdout);
1214                 goto next;
1215
1216                 case ADMIN_CALL_ALERTING:
1217                 printf("ALERTING\n"); fflush(stdout);
1218                 goto next;
1219
1220                 case ADMIN_CALL_CONNECT:
1221                 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1222                 goto next;
1223
1224                 case ADMIN_CALL_NOTIFY:
1225                 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1226                 goto next;
1227
1228                 case ADMIN_CALL_DISCONNECT:
1229                 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);
1230                 goto next;
1231
1232                 case ADMIN_CALL_RELEASE:
1233                 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);
1234                 break;
1235
1236                 default:
1237                 return("Response not valid.");
1238         }
1239         
1240         printf("Command successfull.\n");
1241         return(NULL);
1242 }
1243
1244
1245 /*
1246  * makes a trace
1247  */
1248 char *admin_trace(int sock, int argc, char *argv[])
1249 {
1250         static struct admin_message msg;
1251         int i;
1252
1253         /* show help */
1254         if (!strcasecmp(argv[2], "help"))
1255         {
1256                 printf("Trace Help\n----------\n");
1257                 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1258                 printf("By default a complete trace is shown in detailed format.\n");
1259                 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1260                 printf("Use filter values to select specific trace messages.\n");
1261                 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1262                 printf("Filters:\n");
1263                 printf(" category=<mask bits>\n");
1264                 printf("  0x01 = CH: channel object trace\n");
1265                 printf("  0x02 = EP: endpoint object trace\n");
1266                 printf(" port=<mISDN port>  select only given port for trace\n");
1267                 printf(" interface=<interface name>  select only given interface for trace\n");
1268                 printf(" caller=<caller id>  select only given caller id for trace\n");
1269                 printf(" dialing=<number>  select only given dialed number for trace\n");
1270                 return(NULL);
1271         }
1272
1273         /* init trace request */        
1274         memset(&msg, 0, sizeof(msg));
1275         msg.message = ADMIN_TRACE_REQUEST;
1276         msg.u.trace_req.detail = 3;
1277
1278         /* parse args */
1279         i = 2;
1280         while(i < argc)
1281         {
1282                 if (!strcasecmp(argv[i], "brief"))
1283                         msg.u.trace_req.detail = 1;
1284                 else if (!strcasecmp(argv[i], "short"))
1285                         msg.u.trace_req.detail = 2;
1286                 else if (!strncasecmp(argv[i], "category=", 9))
1287                         SCPY(msg.u.trace_req.category, argv[i]+9);
1288                 else if (!strncasecmp(argv[i], "port=", 5))
1289                         msg.u.trace_req.port = atoi(argv[i]+5);
1290                 else if (!strncasecmp(argv[i], "interface=", 10))
1291                         SCPY(msg.u.trace_req.interface, argv[i]+10);
1292                 else if (!strncasecmp(argv[i], "caller=", 7))
1293                         SCPY(msg.u.trace_req.caller, argv[i]+7);
1294                 else if (!strncasecmp(argv[i], "dialing=", 8))
1295                         SCPY(msg.u.trace_req.dialing, argv[i]+8);
1296                 else return("Invalid trace option, try 'trace help'.");
1297
1298                 i++;
1299         }
1300
1301         /* send trace request */
1302         if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1303                 return("Broken pipe while sending trace request.");
1304
1305         /* receive response */
1306 next:
1307         if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1308                 return("Broken pipe while receiving response.");
1309
1310         if (msg.message != ADMIN_TRACE_RESPONSE)
1311                 return("Response not valid.");
1312
1313         printf("%s", msg.u.trace_rsp.text);
1314         goto next;
1315 }
1316
1317
1318 /*
1319  * main function
1320  */
1321 int main(int argc, char *argv[])
1322 {
1323         int mode;
1324         char *socket_name = SOCKET_NAME;
1325         int sock, conn;
1326         struct sockaddr_un sock_address;
1327         char *ret;
1328
1329
1330         /* show options */
1331         if (argc <= 1)
1332         {
1333                 usage:
1334                 printf("\n");
1335                 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1336                 printf("state - View current states using graphical console output.\n");
1337                 printf("interface - Tell LCR to reload \"interface.conf\".\n");
1338                 printf("route - Tell LCR to reload \"route.conf\".\n");
1339                 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1340                 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1341                 printf("block <port> - Block given port.\n");
1342                 printf("unblock <port> - Unblock given port.\n");
1343                 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1344                 printf("testcall <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1345                 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1346                 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1347                 printf(" -> Use 'trace help' to see filter description.\n");
1348                 printf("\n");
1349                 return(0);
1350         }
1351
1352         /* check mode */
1353         if (!(strcasecmp(argv[1],"state")))
1354         {
1355                 mode = MODE_STATE;
1356         } else
1357         if (!(strcasecmp(argv[1],"interface")))
1358         {
1359                 mode = MODE_INTERFACE;
1360         } else
1361         if (!(strcasecmp(argv[1],"route")))
1362         {
1363                 mode = MODE_ROUTE;
1364         } else
1365         if (!(strcasecmp(argv[1],"dial")))
1366         {
1367                 if (argc <= 3)
1368                         goto usage;
1369                 mode = MODE_DIAL;
1370         } else
1371         if (!(strcasecmp(argv[1],"release")))
1372         {
1373                 if (argc <= 2)
1374                         goto usage;
1375                 mode = MODE_RELEASE;
1376         } else
1377         if (!(strcasecmp(argv[1],"unblock")))
1378         {
1379                 if (argc <= 2)
1380                         goto usage;
1381                 mode = MODE_UNBLOCK;
1382         } else
1383         if (!(strcasecmp(argv[1],"block")))
1384         {
1385                 if (argc <= 2)
1386                         goto usage;
1387                 mode = MODE_BLOCK;
1388         } else
1389         if (!(strcasecmp(argv[1],"unload")))
1390         {
1391                 if (argc <= 2)
1392                         goto usage;
1393                 mode = MODE_UNLOAD;
1394         } else
1395         if (!(strcasecmp(argv[1],"testcall")))
1396         {
1397                 if (argc <= 4)
1398                         goto usage;
1399                 mode = MODE_TESTCALL;
1400         } else
1401         if (!(strcasecmp(argv[1],"trace")))
1402         {
1403                 mode = MODE_TRACE;
1404         } else
1405         {
1406                 goto usage;
1407         }
1408
1409 //pipeagain:
1410         /* open socket */
1411         if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1412         {
1413                 fprintf(stderr, "Failed to create socket.\n");
1414                 exit(EXIT_FAILURE);
1415         }
1416         memset(&sock_address, 0, sizeof(sock_address));
1417         sock_address.sun_family = PF_UNIX;
1418         UCPY(sock_address.sun_path, socket_name);
1419         if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1420         {
1421                 close(sock);
1422                 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1423                 exit(EXIT_FAILURE);
1424         }
1425
1426         /* process mode */
1427         switch(mode)
1428         {
1429                 case MODE_STATE:
1430                 ret = admin_state(sock);
1431                 break;
1432         
1433                 case MODE_INTERFACE:
1434                 case MODE_ROUTE:
1435                 ret = admin_cmd(sock, mode, NULL, NULL);
1436                 break;
1437
1438                 case MODE_DIAL:
1439                 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1440                 break;
1441
1442                 case MODE_RELEASE:
1443                 case MODE_UNBLOCK:
1444                 case MODE_BLOCK:
1445                 case MODE_UNLOAD:
1446                 ret = admin_cmd(sock, mode, NULL, argv[2]);
1447                 break;
1448
1449                 case MODE_TESTCALL:
1450                 ret = admin_testcall(sock, argc, argv);
1451
1452                 case MODE_TRACE:
1453                 ret = admin_trace(sock, argc, argv);
1454         }
1455
1456         close(sock);
1457         /* now we say good bye */
1458         if (ret)
1459         {
1460 //              if (!strncasecmp(ret, "Broken Pipe", 11))
1461 //                      goto pipeagain;
1462                 printf("%s\n", ret);
1463                 exit(EXIT_FAILURE);
1464         }
1465 }
1466
1467
1468
1469
1470