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