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