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