9baad71b48e15e1c14578af505b0a33cdf2de6f1
[lcr.git] / admin_client.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 <curses.h>
23 #include "macro.h"
24 #include "join.h"
25 #include "joinpbx.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][512];
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.joins+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_join(struct admin_message *msg, struct admin_message *m, int line, int i)
392 {
393         unsigned long   join = m[i].u.j.serial;
394         char            buffer[256];
395         int             j, jj;
396
397         color(white);
398         SPRINT(buffer,"JOIN(%d)", join);
399         addstr(buffer);
400         if (m[i].u.j.partyline)
401         {
402                 color(cyan);
403                 addstr(" partyline=");
404                 color(white);
405                 SPRINT(buffer, "%d\n", m[i].u.j.partyline);
406                 addstr(buffer);
407         }
408         if (m[i].u.j.remote[0])
409         {
410                 color(cyan);
411                 addstr(" remote=");
412                 color(white);
413                 SPRINT(buffer, "%s\n", m[i].u.j.remote);
414                 addstr(buffer);
415         }
416         /* find number of epoints */
417         j = msg->u.s.interfaces+msg->u.s.joins;
418         jj = j + msg->u.s.epoints;
419         i = 0;
420         while(j < jj)
421         {
422                 if (m[j].u.e.join == join)
423                         i++;
424                 j++;
425         }
426         /* loop all related endpoints */
427         j = msg->u.s.interfaces+msg->u.s.joins;
428         jj = j + msg->u.s.epoints;
429         while(j < jj)
430         {
431                 if (m[j].u.e.join == join)
432                 {
433                         i--;
434                         move(++line>1?line:1, 1);
435                         color(cyan);
436                         if (i)
437                                 LTEE
438                         else
439                                 LLCORNER
440                         move(line>1?line:1, 4);
441                         if (line+2 >= LINES) break;
442                         line = debug_epoint(msg, m, line, j, i?1:0);
443                         if (line+2 >= LINES) break;
444                 }
445                 j++;
446         }
447
448         return(line);
449 }
450 char *admin_state(int sock, char *argv[])
451 {
452         struct admin_message    msg,
453                                 *m;
454         char                    buffer[512],
455                                 *p;
456         int                     line, offset = 0, hoffset = 0;
457         int                     i, ii, j, jj, k;
458         unsigned long           l, ll;
459         int                     num;
460         int                     len;
461         int                     off;
462         int                     ltee;
463         int                     anything;
464         int                     enter = 0;
465         char                    enter_string[128] = "", ch;
466         fd_set                  select_rfds;
467         struct timeval          select_tv;
468
469         /* flush logfile name */
470         logfile[0] = '\0';
471
472         /* init curses */
473         init_curses();
474
475         again:
476         /* send reload command */
477         memset(&msg, 0, sizeof(msg));
478         msg.message = ADMIN_REQUEST_STATE;
479 //      printf("sizeof=%d\n",sizeof(msg));fflush(stdout);
480         if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
481         {
482                 cleanup_curses();
483                 return("Broken pipe while sending command.");
484         }
485
486         /* receive response */
487         if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
488         {
489                 cleanup_curses();
490                 return("Broken pipe while receiving response.");
491         }
492
493         if (msg.message != ADMIN_RESPONSE_STATE)
494         {
495                 cleanup_curses();
496                 return("Response not valid. Expecting state response.");
497         }
498         num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
499         m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
500         off=0;
501         if (num)
502         {
503                 readagain:
504                 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off)
505                 {
506                         if (len <= 0) {
507                                 FREE(m, 0);
508         //                      fprintf(stderr, "got=%d expected=%d\n", i, num*sizeof(struct admin_message));
509                                 cleanup_curses();
510                                 return("Broken pipe while receiving state infos.");
511                         }
512                         if (len < num*(int)sizeof(struct admin_message))
513                         {
514                                 off+=len;
515                                 goto readagain;
516                         }
517                 }
518         }
519         j = 0;
520         i = 0;
521 //      fprintf("getting =%d interfaces\n", msg.u.s.interfaces);
522         while(i < msg.u.s.interfaces)
523         {
524 //              fprintf(stderr, "j=%d message=%d\n", j, m[j].message);
525                 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE)
526                 {
527                         FREE(m, 0);
528                         cleanup_curses();
529                         return("Response not valid. Expecting interface information.");
530                 }
531                 i++;
532                 j++;
533         }
534         i = 0;
535         while(i < msg.u.s.remotes)
536         {
537                 if (m[j].message != ADMIN_RESPONSE_S_REMOTE)
538                 {
539                         FREE(m, 0);
540                         cleanup_curses();
541                         return("Response not valid. Expecting remote application information.");
542                 }
543                 i++;
544                 j++;
545         }
546         i = 0;
547         while(i < msg.u.s.joins)
548         {
549                 if (m[j].message != ADMIN_RESPONSE_S_JOIN)
550                 {
551                         FREE(m, 0);
552                         cleanup_curses();
553                         return("Response not valid. Expecting join information.");
554                 }
555                 i++;
556                 j++;
557         }
558         i = 0;
559         while(i < msg.u.s.epoints)
560         {
561                 if (m[j].message != ADMIN_RESPONSE_S_EPOINT)
562                 {
563                         FREE(m, 0);
564                         cleanup_curses();
565                         return("Response not valid. Expecting endpoint information.");
566                 }
567                 i++;
568                 j++;
569         }
570         i = 0;
571         while(i < msg.u.s.ports)
572         {
573                 if (m[j].message != ADMIN_RESPONSE_S_PORT)
574                 {
575                         FREE(m, 0);
576                         cleanup_curses();
577                         return("Response not valid. Expecting port information.");
578                 }
579                 i++;
580                 j++;
581         }
582         // now j is the number of message blocks
583
584         /* display start */
585         erase();
586
587         line = 1-offset; 
588
589         /* change log */
590         if (!!strcmp(logfile, msg.u.s.logfile))
591         {
592                 SCPY(logfile, msg.u.s.logfile);
593                 if (logfh >= 0)
594                         close(logfh);
595                 i = 0;
596                 ii = LOGLINES;
597                 while(i < ii)
598                 {
599                         logline[i][0] = '~';
600                         logline[i][1] = '\0';
601                         i++;
602                 }
603                 logcur = 0;
604                 logfh = open(logfile, O_RDONLY|O_NONBLOCK);
605                 if (logfh >= 0)
606                 {
607                         /* seek at the end -8000 chars */
608                         lseek(logfh, -8000, SEEK_END);
609                         /* if not at the beginning, read until endofline */
610                         logline[logcur % LOGLINES][0] = '\0';
611                         l = read(logfh, logline[logcur % LOGLINES], sizeof(logline[logcur % LOGLINES])-1);
612                         if (l > 0)
613                         {
614                                 /* read first line and skip junk */
615                                 logline[logcur % LOGLINES][l] = '\0';
616                                 if ((p = strchr(logline[logcur % LOGLINES],'\n')))
617                                 {
618                                         logcur++;
619                                         SCPY(logline[logcur % LOGLINES], p+1);
620                                         SCPY(logline[(logcur-1) % LOGLINES], "...");
621                                 }
622                                 goto finish_line;
623                         }
624                 }
625         }
626
627         /* read log */
628         if (logfh >= 0)
629         {
630                 while(42)
631                 {
632                         ll = strlen(logline[logcur % LOGLINES]);
633                         l = read(logfh, logline[logcur % LOGLINES]+ll, sizeof(logline[logcur % LOGLINES])-ll-1);
634                         if (l<=0)
635                                 break;
636                         logline[logcur % LOGLINES][ll+l] = '\0';
637                         finish_line:
638                         /* put data to lines */
639                         while ((p = strchr(logline[logcur % LOGLINES],'\n')))
640                         {
641                                 *p = '\0';
642                                 logcur++;
643                                 SCPY(logline[logcur % LOGLINES], p+1);
644                         }
645                         /* if line is full without return, go next line */
646                         if (strlen(logline[logcur % LOGLINES]) == sizeof(logline[logcur % LOGLINES])-1)
647                         {
648                                 logcur++;
649                                 logline[logcur % LOGLINES][0] = '\0';
650                         }
651                 }
652         }
653
654         /* display interfaces */
655         if (show_interfaces > 0)
656         {
657                 anything = 0;
658                 i = 0;
659                 ii = i + msg.u.s.interfaces;
660                 while(i < ii)
661                 {
662                         /* show interface summary */
663                         move(++line>1?line:1, 0);
664                         color(white);
665                         if (m[i].u.i.block >= 2)
666                         {
667                                 SPRINT(buffer, "%s (%d)%s", m[i].u.i.interface_name, m[i].u.i.portnum, (m[i].u.i.extension)?" (extension)":"");
668                                 addstr(buffer);
669                                 color(red);
670                                 addstr("  not loaded");
671                         } else
672                         {
673                                 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);
674                                 addstr(buffer);
675                                 if (m[i].u.i.ptp || !m[i].u.i.ntmode)
676                                 {
677                                         color((m[i].u.i.l2link)?green:red);
678                                         addstr((m[i].u.i.l2link)?"  L2 UP":"  L2 down");
679                                 }
680                                 color((m[i].u.i.l1link)?green:blue);
681                                 addstr((m[i].u.i.l1link)?"  L1 ACTIVE":"  L1 inactive");
682                                 if (m[i].u.i.block)
683                                 {
684                                         color(red);
685                                         addstr("  blocked");
686                                 }
687                                 if (line+2 >= LINES) goto end;
688                                 /* show channels */
689                                 if (show_interfaces > 1)
690                                 {
691                                         ltee = 0;
692                                         j = k =0;
693                                         jj = m[i].u.i.channels;
694                                         while(j < jj)
695                                         {
696                                                 /* show all channels */
697                                                 if (show_interfaces>2 || m[i].u.i.busy[j]>0)
698                                                 {
699                                                         color(cyan);
700                                                         /* show left side / right side */
701                                                         if ((k & 1) && (COLS > 70))
702                                                         {
703                                                                 move(line>1?line:1,4+((COLS-4)/2));
704                                                         } else
705                                                         {
706                                                                 move(++line>1?line:1, 1);
707                                                                 LTEE
708                                                                 ltee = 1;
709                                                         }
710                                                         k++;
711                                                         color(white);
712                                                         if (m[i].u.i.pri)
713                                                                 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
714                                                         else
715                                                                 SPRINT(buffer,"B%2d: ", j+1);
716                                                         addstr(buffer);
717                                                         switch(m[i].u.i.busy[j])
718                                                         {
719                                                                 case B_STATE_IDLE:
720                                                                 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block)
721                                                                 {
722                                                                         color(red);
723                                                                         addstr("blocked ");
724                                                                 } else
725                                                                 {
726                                                                         color(blue);
727                                                                         addstr("idle    ");
728                                                                 }
729                                                                 break;
730                                                                 case B_STATE_ACTIVATING:
731                                                                 color(yellow);
732                                                                 addstr("act'ing ");
733                                                                 break;
734                                                                 case B_STATE_ACTIVE:
735                                                                 color(green);
736                                                                 addstr("busy    ");
737                                                                 break;
738                                                                 case B_STATE_DEACTIVATING:
739                                                                 color(yellow);
740                                                                 addstr("dact'ing");
741                                                                 break;
742                                                                 case B_STATE_EXPORTING:
743                                                                 color(yellow);
744                                                                 addstr("exp'ing ");
745                                                                 break;
746                                                                 case B_STATE_REMOTE:
747                                                                 color(green);
748                                                                 addstr("remote  ");
749                                                                 break;
750                                                                 case B_STATE_IMPORTING:
751                                                                 color(yellow);
752                                                                 addstr("imp'ing ");
753                                                                 break;
754                                                         }
755                                                         if (m[i].u.i.port[j])
756                                                         {
757                                                                 /* search for port */
758                                                                 l = msg.u.s.interfaces+msg.u.s.joins+msg.u.s.epoints;
759                                                                 ll = l+msg.u.s.ports;
760                                                                 while(l < ll)
761                                                                 {
762                                                                         if (m[l].u.p.serial == m[i].u.i.port[j])
763                                                                         {
764                                                                                 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
765                                                                                 addstr(buffer);
766                                                                         }
767                                                                         l++;
768                                                                 }
769                                                         }
770                                                         if (line+2 >= LINES)
771                                                         {
772                                                                 if (ltee)
773                                                                 {
774                                                                         color(cyan);
775                                                                         move(line>1?line:1, 1);
776                                                                         LLCORNER
777                                                                 }
778                                                                 goto end;
779                                                         }
780                                                 }
781                                                 j++;
782                                         }
783                                         if (ltee)
784                                         {
785                                                 color(cyan);
786                                                 move(line>1?line:1, 1);
787                                                 LLCORNER
788                                         }
789                                         if (line+2 >= LINES) goto end;
790                                         /* show summary if no channels were shown */
791                                         if (show_interfaces<2 && ltee==0)
792                                         {
793                                                 color(cyan);
794                                                 move(++line>1?line:1, 1);
795                                                 LLCORNER
796                                                         
797                                                 if (m[i].u.i.l2link && m[i].u.i.block==0)
798                                                 {
799                                                         color(green);
800                                                         SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
801                                                 } else
802                                                 {
803                                                         color(red);
804                                                         SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
805                                                 }
806                                                 addstr(buffer);
807                                         }
808                                         if (line+2 >= LINES) goto end;
809                                 }
810                         }
811                         i++;
812                         anything = 1;
813                 }
814                 i = 0;
815                 ii = i + msg.u.s.remotes;
816                 while(i < ii)
817                 {
818                         /* show remote summary */
819                         move(++line>1?line:1, 0);
820                         color(white);
821                         SPRINT(buffer, "Remote: %s", m[i].u.r.name);
822                         addstr(buffer);
823                         i++;
824                 }
825                 if (anything)
826                         line++;
827                 if (line+2 >= LINES) goto end;
828         }               
829         /* display calls (brief) */
830         if (show_calls == 1)
831         {
832                 anything = 0;
833                 i = msg.u.s.interfaces+msg.u.s.joins;
834                 ii = i+msg.u.s.epoints;
835                 while(i < ii)
836                 {
837                         /* for each endpoint... */
838                         if (!m[i].u.e.join)
839                         {
840                                 move(++line>1?line:1, 0);
841                                 color(white);
842                                 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
843                                 addstr(buffer);
844                                 color(cyan);
845                                 if (m[i].u.e.terminal[0])
846                                 {
847                                         addstr("intern=");
848                                         color(green);
849                                         addstr(m[i].u.e.terminal);
850                                 } else
851                                         addstr("extern");
852                                 color(white);
853                                 SPRINT(buffer, " %s", m[i].u.e.callerid);
854                                 addstr(buffer);
855                                 color(cyan);
856                                 addstr("->");
857                                 color(white);
858                                 SPRINT(buffer, "%s", m[i].u.e.dialing);
859                                 addstr(buffer);
860                                 if (m[i].u.e.action[0])
861                                 {
862                                         color(cyan);
863                                         addstr(" action=");
864                                         color(yellow);
865                                         addstr(m[i].u.e.action);
866                                 }
867                                 if (line+2 >= LINES) goto end;
868                         }
869                         i++;
870                         anything = 1;
871                 }
872                 j = msg.u.s.interfaces;
873                 jj = j+msg.u.s.joins;
874                 while(j < jj)
875                 {
876                         /* for each call... */
877                         move(++line>1?line:1, 0);
878                         color(white);
879                         SPRINT(buffer, "(%d):", m[j].u.j.serial);
880                         addstr(buffer);
881                         i = msg.u.s.interfaces+msg.u.s.joins;
882                         ii = i+msg.u.s.epoints;
883                         while(i < ii)
884                         {
885                                 /* for each endpoint... */
886                                 if (m[i].u.e.join == m[j].u.j.serial)
887                                 {
888                                         color(white);
889                                         SPRINT(buffer, " (%d)", m[i].u.e.serial);
890                                         addstr(buffer);
891                                         color(cyan);
892                                         if (m[i].u.e.terminal[0])
893                                         {
894                                                 addstr("int=");
895                                                 color(green);
896                                                 addstr(m[i].u.e.terminal);
897                                         } else
898                                                 addstr("ext");
899                                         color(white);
900                                         SPRINT(buffer, "-%s", m[i].u.e.callerid);
901                                         addstr(buffer);
902                                         color(cyan);
903                                         addstr(">");
904                                         color(white);
905                                         SPRINT(buffer, "%s", m[i].u.e.dialing);
906                                         addstr(buffer);
907                                 }
908                                 i++;
909                                 anything = 1;
910                         }
911                         if (line+2 >= LINES) goto end;
912                         j++;
913                 }
914                 if (anything)
915                         line++;
916                 if (line+2 >= LINES) goto end;
917         }
918         /* display calls (structurd) */
919         if (show_calls == 2)
920         {
921                 /* show all ports with no epoint */
922                 anything = 0;
923                 i = msg.u.s.interfaces+msg.u.s.joins+msg.u.s.epoints;
924                 ii = i+msg.u.s.ports;
925                 while(i < ii)
926                 {
927                         if (!m[i].u.p.epoint)
928                         {
929                                 move(++line>1?line:1, 8);
930                                 if (line+2 >= LINES) goto end;
931                                 line = debug_port(&msg, m, line, i, 0);
932                                 if (line+2 >= LINES) goto end;
933                                 anything = 1;
934                         }
935                         i++;
936                 }
937                 if (anything)
938                         line++;
939                 if (line+2 >= LINES) goto end;
940
941                 /* show all epoints with no call */
942                 anything = 0;
943                 i = msg.u.s.interfaces+msg.u.s.joins;
944                 ii = i+msg.u.s.epoints;
945                 while(i < ii)
946                 {
947                         if (!m[i].u.e.join)
948                         {
949                                 move(++line>1?line:1, 4);
950                                 if (line+2 >= LINES) goto end;
951                                 line = debug_epoint(&msg, m, line, i, 0);
952                                 if (line+2 >= LINES) goto end;
953                                 anything = 1;
954                         }
955                         i++;
956                 }
957                 if (anything)
958                         line++;
959                 if (line+2 >= LINES) goto end;
960
961                 /* show all joins */
962                 anything = 0;
963                 i = msg.u.s.interfaces;
964                 ii = i+msg.u.s.joins;
965                 while(i < ii)
966                 {
967                         move(++line>1?line:1, 0);
968                         if (line+2 >= LINES) goto end;
969                         line = debug_join(&msg, m, line, i);
970                         if (line+2 >= LINES) goto end;
971                         i++;
972                         anything = 1;
973                 }
974                 if (anything)
975                         line++;
976                 if (line+2 >= LINES) goto end;
977
978         }
979
980         /* show log */
981         if (show_log)
982         {
983                 if (line+2 < LINES)
984                 {
985                         move(line++>1?line-1:1, 0);
986                         color(blue);
987                         hline(ACS_HLINE, COLS);
988                         color(white);
989                         
990                         l = logcur-(LINES-line-2);
991                         ll = logcur;
992                         if (ll-l >= LOGLINES)
993                                 l = ll-LOGLINES+1;
994                         while(l!=ll)
995                         {
996                                 move(line++>1?line-1:1, 0);
997                                 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
998                                         SCPY(buffer, logline[l % LOGLINES] + hoffset);
999                                 else
1000                                         buffer[0] = '\0';
1001                                 if (COLS < (int)strlen(buffer))
1002                                 {
1003                                         buffer[COLS-1] = '\0';
1004                                         addstr(buffer);
1005                                         color(mangenta);
1006                                         addch('*');
1007                                         color(white);
1008                                 } else
1009                                         addstr(buffer);
1010                                 l++;
1011                         }
1012                 }
1013         }
1014
1015         end:
1016         /* free memory */
1017         FREE(m, 0);
1018         /* display name/time */
1019 //      move(0, 0);
1020 //      hline(' ', COLS);
1021         move(0, 0);
1022         color(cyan);
1023         msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
1024         SPRINT(buffer, "LCR %s", msg.u.s.version_string);
1025         addstr(buffer);
1026         if (COLS>50)
1027         {
1028                 move(0, COLS-19);
1029                 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1030                         msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1031                         msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1032                 addstr(buffer);
1033         }
1034         /* displeay head line */
1035         move(1, 0);
1036         color(blue);
1037         hline(ACS_HLINE, COLS);
1038         if (offset)
1039         {
1040                 move(1, 1);
1041                 SPRINT(buffer, "Offset +%d", offset);
1042                 color(red);
1043                 addstr(buffer);
1044         }
1045         if (hoffset)
1046         {
1047                 move(1, 13);
1048                 SPRINT(buffer, "H-Offset +%d", hoffset);
1049                 color(red);
1050                 addstr(buffer);
1051         }
1052         /* display end */
1053         move(LINES-2, 0);
1054         color(blue);
1055         hline(ACS_HLINE, COLS);
1056         move(LINES-1, 0);
1057         if (enter)
1058         {
1059                 color(white);
1060                 SPRINT(buffer, "-> %s", enter_string);
1061         } else
1062         {
1063                 color(cyan);
1064                 SPRINT(buffer, "i=interfaces '%s'  c=calls '%s'  l=log  q=quit  +-*/=scroll  enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1065         }
1066         addstr(buffer);
1067         refresh();
1068
1069         /* resize */
1070         if (lastlines!=LINES || lastcols!=COLS)
1071         {
1072                 cleanup_curses();
1073                 init_curses();
1074                 goto again;
1075         }
1076
1077         if (enter)
1078         {
1079                 /* user input in enter mode */
1080                 ch = getch();
1081                 enter_again:
1082                 if (ch == 10)
1083                 {
1084                         FILE *fp;
1085
1086                         enter = 0;
1087                         if (!enter_string[0])
1088                                 goto again;
1089
1090                         SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1091                         if (!!strncmp(enter_string, "interface", 10) &&
1092                             !!strncmp(enter_string, "route", 6) &&
1093                             !!strncmp(enter_string, "release ", 8) &&
1094                             !!strncmp(enter_string, "block ", 6) &&
1095                             !!strncmp(enter_string, "unblock ", 8) &&
1096                             !!strncmp(enter_string, "unload ", 7))
1097                         {
1098                                 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1099                                 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1100                                 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1101                                 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1102                                 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1103                                 SPRINT(logline[logcur++ % LOGLINES], "unblock <port> (unblock port for further calls, load if not loaded)");
1104                                 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1105                         } else
1106                         {
1107                                 /* applend output to log window */
1108                                 SPRINT(buffer, "%s %s", argv[0], enter_string);
1109                                 fp = popen(buffer, "r");
1110                                 if (fp)
1111                                 {
1112                                         while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1113                                                 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1114                                         pclose(fp);
1115                                 } else
1116                                 {
1117                                         SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1118                                 }
1119                         }
1120                         logline[logcur % LOGLINES][0] = '\0';
1121                         enter_string[0] = '\0';
1122                         goto again;
1123                 }
1124                 if (ch>=32 && ch<=126)
1125                 {
1126                         SCCAT(enter_string, ch);
1127                         ch = getch();
1128                         if (ch > 0)
1129                                 goto enter_again;
1130                         goto again;
1131                 } else
1132                 if (ch==8 || ch==127)
1133                 {
1134                         if (enter_string[0])
1135                                 enter_string[strlen(enter_string)-1] = '\0';
1136                         ch = getch();
1137                         if (ch > 0)
1138                                 goto enter_again;
1139                         goto again;
1140                 } else
1141                 if (ch != 3)
1142                 {
1143                         ch = getch();
1144                         if (ch > 0)
1145                                 goto enter_again;
1146                         FD_ZERO(&select_rfds);
1147                         FD_SET(0, &select_rfds);
1148                         select_tv.tv_sec = 0;
1149                         select_tv.tv_usec = 250000;
1150                         select(1, &select_rfds, NULL, NULL, &select_tv);
1151                         goto again;
1152                 }
1153         } else
1154         {
1155                 /* user input in normal mode */
1156                 switch(getch())
1157                 {
1158                         case 12: /* refresh */
1159                         cleanup_curses();
1160                         init_curses();
1161                         goto again;
1162                         break;
1163
1164                         case 3: /* abort */
1165                         case 'q':
1166                         case 'Q':
1167                         break;
1168
1169                         case 'i': /* toggle interface */
1170                         show_interfaces++;
1171                         if (show_interfaces > 3) show_interfaces = 0;
1172                         goto again;
1173
1174                         case 'c': /* toggle calls */
1175                         show_calls++;
1176                         if (show_calls > 2) show_calls = 0;
1177                         goto again;
1178
1179                         case 'l': /* toggle log */
1180                         show_log++;
1181                         if (show_log > 1) show_log = 0;
1182                         goto again;
1183
1184                         case '+': /* scroll down */
1185                         offset++;
1186                         goto again;
1187                         
1188                         case '-': /* scroll up */
1189                         if (offset)
1190                                 offset--;
1191                         goto again;
1192
1193                         case '*': /* scroll right */
1194                         hoffset += 2;
1195                         goto again;
1196                         
1197                         case '/': /* scroll left */
1198                         if (hoffset)
1199                                 hoffset -= 2;
1200                         goto again;
1201
1202                         case 10: /* entermode */
1203                         enter = 1;
1204                         goto again;
1205
1206                         default:
1207                         FD_ZERO(&select_rfds);
1208                         FD_SET(0, &select_rfds);
1209                         select_tv.tv_sec = 0;
1210                         select_tv.tv_usec = 250000;
1211                         select(1, &select_rfds, NULL, NULL, &select_tv);
1212                         goto again;
1213                 }
1214         }
1215
1216         /* check for logfh */
1217         if (logfh >= 0)
1218                 close(logfh);
1219         logfh = -1;
1220
1221         /* cleanup curses and exit */
1222         cleanup_curses();
1223
1224         return(NULL);
1225 }
1226
1227
1228 /*
1229  * Send command and show error message.
1230  */
1231 char *admin_cmd(int sock, int mode, char *extension, char *number)
1232 {
1233         static struct admin_message msg;
1234
1235         /* send reload command */
1236         memset(&msg, 0, sizeof(msg));
1237         switch(mode)
1238         {
1239                 case MODE_INTERFACE:
1240                 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1241                 break;
1242                 case MODE_ROUTE:
1243                 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1244                 break;
1245                 case MODE_DIAL:
1246                 msg.message = ADMIN_REQUEST_CMD_DIAL;
1247                 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1248                 break;
1249                 case MODE_RELEASE:
1250                 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1251                 SCPY(msg.u.x.message, number);
1252                 break;
1253                 case MODE_UNBLOCK:
1254                 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1255                 msg.u.x.portnum = atoi(number);
1256                 msg.u.x.block = 0;
1257                 break;
1258                 case MODE_BLOCK:
1259                 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1260                 msg.u.x.portnum = atoi(number);
1261                 msg.u.x.block = 1;
1262                 break;
1263                 case MODE_UNLOAD:
1264                 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1265                 msg.u.x.portnum = atoi(number);
1266                 msg.u.x.block = 2;
1267                 break;
1268         }
1269
1270         if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1271                 return("Broken pipe while sending command.");
1272
1273         /* receive response */
1274         if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1275                 return("Broken pipe while receiving response.");
1276         switch(mode)
1277         {
1278                 case MODE_INTERFACE:
1279                 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1280                         return("Response not valid.");
1281                 break;
1282                 case MODE_ROUTE:
1283                 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1284                         return("Response not valid.");
1285                 break;
1286                 case MODE_DIAL:
1287                 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1288                         return("Response not valid.");
1289                 break;
1290                 case MODE_RELEASE:
1291                 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1292                         return("Response not valid.");
1293                 break;
1294                 case MODE_UNBLOCK:
1295                 case MODE_BLOCK:
1296                 case MODE_UNLOAD:
1297                 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1298                         return("Response not valid.");
1299                 break;
1300         }
1301
1302         /* process response */
1303         if (msg.u.x.error)
1304         {
1305                 return(msg.u.x.message);
1306         }
1307         printf("Command successfull.\n");
1308         return(NULL);
1309 }
1310
1311
1312 /*
1313  * makes a testcall
1314  */
1315 char *admin_testcall(int sock, int argc, char *argv[])
1316 {
1317         static struct admin_message msg;
1318
1319         printf("pid=%d\n", getpid()); fflush(stdout);
1320
1321         /* send reload command */
1322         memset(&msg, 0, sizeof(msg));
1323         msg.message = ADMIN_CALL_SETUP;
1324         if (argc > 2)
1325         {
1326                 SCPY(msg.u.call.interface, argv[2]);
1327         }
1328         if (argc > 3)
1329         {
1330                 SCPY(msg.u.call.callerid, argv[3]);
1331         }
1332         if (argc > 4)
1333         {
1334                 SCPY(msg.u.call.dialing, argv[4]);
1335         }
1336         if (argc > 5)
1337         {
1338                 if (argv[5][0] == 'p')
1339                         msg.u.call.present = 1;
1340         }
1341         msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1342         msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1343         msg.u.call.bc_info1 = 0;
1344         msg.u.call.hlc = 0;
1345         msg.u.call.exthlc = 0;
1346         if (argc > 6)
1347                 msg.u.call.bc_capa = strtol(argv[6],NULL,0);
1348         else
1349                 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1350         if (argc > 7) {
1351                 msg.u.call.bc_mode = strtol(argv[7],NULL,0);
1352                 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1353         }
1354         if (argc > 8) {
1355                 msg.u.call.bc_info1 = strtol(argv[8],NULL,0);
1356                 if (msg.u.call.bc_info1 < 0)
1357                         msg.u.call.bc_info1 = 0;
1358                 else
1359                         msg.u.call.bc_info1 |= 0x80;
1360         }
1361         if (argc > 9) {
1362                 msg.u.call.hlc = strtol(argv[9],NULL,0);
1363                 if (msg.u.call.hlc < 0)
1364                         msg.u.call.hlc = 0;
1365                 else
1366                         msg.u.call.hlc |= 0x80;
1367         }
1368 //              printf("hlc=%d\n",  msg.u.call.hlc);
1369         if (argc > 10) {
1370                 msg.u.call.exthlc = strtol(argv[10],NULL,0);
1371                 if (msg.u.call.exthlc < 0)
1372                         msg.u.call.exthlc = 0;
1373                 else
1374                         msg.u.call.exthlc |= 0x80;
1375         }
1376
1377         if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1378                 return("Broken pipe while sending command.");
1379
1380         /* receive response */
1381 next:
1382         if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1383                 return("Broken pipe while receiving response.");
1384         switch(msg.message)
1385         {
1386                 case ADMIN_CALL_SETUP_ACK:
1387                 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1388                 goto next;
1389
1390                 case ADMIN_CALL_PROCEEDING:
1391                 printf("PROCEEDING\n"); fflush(stdout);
1392                 goto next;
1393
1394                 case ADMIN_CALL_ALERTING:
1395                 printf("ALERTING\n"); fflush(stdout);
1396                 goto next;
1397
1398                 case ADMIN_CALL_CONNECT:
1399                 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1400                 goto next;
1401
1402                 case ADMIN_CALL_NOTIFY:
1403                 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1404                 goto next;
1405
1406                 case ADMIN_CALL_DISCONNECT:
1407                 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);
1408                 goto next;
1409
1410                 case ADMIN_CALL_RELEASE:
1411                 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);
1412                 break;
1413
1414                 default:
1415                 return("Response not valid.");
1416         }
1417         
1418         printf("Command successfull.\n");
1419         return(NULL);
1420 }
1421
1422
1423 /*
1424  * makes a trace
1425  */
1426 char *admin_trace(int sock, int argc, char *argv[])
1427 {
1428         static struct admin_message msg;
1429         int i;
1430
1431         /* show help */
1432         if (argc > 2) if (!strcasecmp(argv[2], "help"))
1433         {
1434                 printf("Trace Help\n----------\n");
1435                 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1436                 printf("By default a complete trace is shown in detailed format.\n");
1437                 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1438                 printf("Use filter values to select specific trace messages.\n");
1439                 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1440                 printf("Filters:\n");
1441                 printf(" category=<mask bits>\n");
1442                 printf("  0x01 = CH: channel object trace\n");
1443                 printf("  0x02 = EP: endpoint object trace\n");
1444                 printf(" port=<mISDN port>  select only given port for trace\n");
1445                 printf(" interface=<interface name>  select only given interface for trace\n");
1446                 printf(" caller=<caller id>  select only given caller id for trace\n");
1447                 printf(" dialing=<number>  select only given dialed number for trace\n");
1448                 return(NULL);
1449         }
1450
1451         /* init trace request */        
1452         memset(&msg, 0, sizeof(msg));
1453         msg.message = ADMIN_TRACE_REQUEST;
1454         msg.u.trace_req.detail = 3;
1455
1456         /* parse args */
1457         i = 2;
1458         while(i < argc)
1459         {
1460                 if (!strcasecmp(argv[i], "brief"))
1461                         msg.u.trace_req.detail = 1;
1462                 else if (!strcasecmp(argv[i], "short"))
1463                         msg.u.trace_req.detail = 2;
1464                 else if (!strncasecmp(argv[i], "category=", 9))
1465                         msg.u.trace_req.category = atoi(argv[i]+9);
1466                 else if (!strncasecmp(argv[i], "port=", 5))
1467                         msg.u.trace_req.port = atoi(argv[i]+5);
1468                 else if (!strncasecmp(argv[i], "interface=", 10))
1469                         SCPY(msg.u.trace_req.interface, argv[i]+10);
1470                 else if (!strncasecmp(argv[i], "caller=", 7))
1471                         SCPY(msg.u.trace_req.caller, argv[i]+7);
1472                 else if (!strncasecmp(argv[i], "dialing=", 8))
1473                         SCPY(msg.u.trace_req.dialing, argv[i]+8);
1474                 else return("Invalid trace option, try 'trace help'.");
1475
1476                 i++;
1477         }
1478
1479         /* send trace request */
1480         if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1481                 return("Broken pipe while sending trace request.");
1482
1483         /* receive response */
1484 next:
1485         if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1486                 return("Broken pipe while receiving response.");
1487
1488         if (msg.message != ADMIN_TRACE_RESPONSE)
1489                 return("Response not valid.");
1490
1491         printf("%s", msg.u.trace_rsp.text);
1492         goto next;
1493 }
1494
1495
1496 /*
1497  * main function
1498  */
1499 int main(int argc, char *argv[])
1500 {
1501         int mode;
1502         char *socket_name = SOCKET_NAME;
1503         int sock, conn;
1504         struct sockaddr_un sock_address;
1505         char *ret;
1506
1507
1508         /* show options */
1509         if (argc <= 1)
1510         {
1511                 usage:
1512                 printf("\n");
1513                 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1514                 printf("state - View current states using graphical console output.\n");
1515                 printf("interface - Tell LCR to reload \"interface.conf\".\n");
1516                 printf("route - Tell LCR to reload \"route.conf\".\n");
1517                 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1518                 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1519                 printf("block <port> - Block given port.\n");
1520                 printf("unblock <port> - Unblock given port.\n");
1521                 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1522                 printf("testcall <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1523                 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1524                 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1525                 printf(" -> Use 'trace help' to see filter description.\n");
1526                 printf("\n");
1527                 return(0);
1528         }
1529
1530         /* check mode */
1531         if (!(strcasecmp(argv[1],"state")))
1532         {
1533                 mode = MODE_STATE;
1534         } else
1535         if (!(strcasecmp(argv[1],"interface")))
1536         {
1537                 mode = MODE_INTERFACE;
1538         } else
1539         if (!(strcasecmp(argv[1],"route")))
1540         {
1541                 mode = MODE_ROUTE;
1542         } else
1543         if (!(strcasecmp(argv[1],"dial")))
1544         {
1545                 if (argc <= 3)
1546                         goto usage;
1547                 mode = MODE_DIAL;
1548         } else
1549         if (!(strcasecmp(argv[1],"release")))
1550         {
1551                 if (argc <= 2)
1552                         goto usage;
1553                 mode = MODE_RELEASE;
1554         } else
1555         if (!(strcasecmp(argv[1],"unblock")))
1556         {
1557                 if (argc <= 2)
1558                         goto usage;
1559                 mode = MODE_UNBLOCK;
1560         } else
1561         if (!(strcasecmp(argv[1],"block")))
1562         {
1563                 if (argc <= 2)
1564                         goto usage;
1565                 mode = MODE_BLOCK;
1566         } else
1567         if (!(strcasecmp(argv[1],"unload")))
1568         {
1569                 if (argc <= 2)
1570                         goto usage;
1571                 mode = MODE_UNLOAD;
1572         } else
1573         if (!(strcasecmp(argv[1],"testcall")))
1574         {
1575                 if (argc <= 4)
1576                         goto usage;
1577                 mode = MODE_TESTCALL;
1578         } else
1579         if (!(strcasecmp(argv[1],"trace")))
1580         {
1581                 mode = MODE_TRACE;
1582         } else
1583         {
1584                 goto usage;
1585         }
1586
1587 //pipeagain:
1588         /* open socket */
1589         if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1590         {
1591                 fprintf(stderr, "Failed to create socket.\n");
1592                 exit(EXIT_FAILURE);
1593         }
1594         memset(&sock_address, 0, sizeof(sock_address));
1595         sock_address.sun_family = PF_UNIX;
1596         UCPY(sock_address.sun_path, socket_name);
1597         if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1598         {
1599                 close(sock);
1600                 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1601                 exit(EXIT_FAILURE);
1602         }
1603
1604         /* process mode */
1605         switch(mode)
1606         {
1607                 case MODE_STATE:
1608                 ret = admin_state(sock, argv);
1609                 break;
1610         
1611                 case MODE_INTERFACE:
1612                 case MODE_ROUTE:
1613                 ret = admin_cmd(sock, mode, NULL, NULL);
1614                 break;
1615
1616                 case MODE_DIAL:
1617                 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1618                 break;
1619
1620                 case MODE_RELEASE:
1621                 case MODE_UNBLOCK:
1622                 case MODE_BLOCK:
1623                 case MODE_UNLOAD:
1624                 ret = admin_cmd(sock, mode, NULL, argv[2]);
1625                 break;
1626
1627                 case MODE_TESTCALL:
1628                 ret = admin_testcall(sock, argc, argv);
1629
1630                 case MODE_TRACE:
1631                 ret = admin_trace(sock, argc, argv);
1632         }
1633
1634         close(sock);
1635         /* now we say good bye */
1636         if (ret)
1637         {
1638 //              if (!strncasecmp(ret, "Broken Pipe", 11))
1639 //                      goto pipeagain;
1640                 printf("%s\n", ret);
1641                 exit(EXIT_FAILURE);
1642         }
1643 }
1644
1645
1646
1647
1648