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