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