1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** Administration tool **
10 \*****************************************************************************/
15 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
29 #include "extension.h"
31 #include "lcrsocket.h"
34 #define LTEE {addch(ACS_LTEE);addch(ACS_HLINE);addch(ACS_HLINE);}
35 #define LLCORNER {addch(ACS_LLCORNER);addch(ACS_HLINE);addch(ACS_HLINE);}
36 #define VLINE {addch(ACS_VLINE);addstr(" ");}
37 #define EMPTY {addstr(" ");}
38 //char rotator[] = {'-', '\\', '|', '/'};
39 int lastlines, lastcols;
40 int show_interfaces = 2,
58 const char *text_interfaces[] = {
65 const char *text_calls[] = {
80 char logline[LOGLINES][512];
81 unsigned int logcur = 0;
88 void init_curses(void)
91 initscr(); cbreak(); noecho();
93 nodelay(stdscr, TRUE);
94 if (COLOR_PAIRS>=8 && COLORS>=8)
108 void cleanup_curses(void)
113 void color(int color)
115 if (COLOR_PAIRS>=8 && COLORS>=8)
116 attrset(COLOR_PAIR(color));
120 * permanently show current state using ncurses
122 int debug_port(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
129 SPRINT(buffer,"%s(%d)", m[i].u.p.name,m[i].u.p.serial);
133 switch (m[i].u.p.state) {
134 case ADMIN_STATE_IDLE:
138 case ADMIN_STATE_IN_SETUP:
140 addstr("'in << setup'");
142 case ADMIN_STATE_OUT_SETUP:
144 addstr("'out >> setup'");
146 case ADMIN_STATE_IN_OVERLAP:
148 addstr("'in << overlap'");
150 case ADMIN_STATE_OUT_OVERLAP:
152 addstr("'out >> overlap'");
154 case ADMIN_STATE_IN_PROCEEDING:
156 addstr("'in << proc'");
158 case ADMIN_STATE_OUT_PROCEEDING:
160 addstr("'out >> proc'");
162 case ADMIN_STATE_IN_ALERTING:
164 addstr("'in << alert'");
166 case ADMIN_STATE_OUT_ALERTING:
168 addstr("'out >> alert'");
170 case ADMIN_STATE_CONNECT:
174 case ADMIN_STATE_IN_DISCONNECT:
176 addstr("'in << disc'");
178 case ADMIN_STATE_OUT_DISCONNECT:
180 addstr("'out >> disc'");
182 case ADMIN_STATE_RELEASE:
188 addstr("'--NONE--'");
193 addstr(" bchannel=");
195 SPRINT(buffer,"%d", m[i].u.p.isdn_chan);
197 if (m[i].u.p.isdn_ces >= 0) {
201 SPRINT(buffer, "%d", m[i].u.p.isdn_ces);
204 if (m[i].u.p.isdn_hold) {
212 int debug_epoint(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
214 unsigned int epoint = m[i].u.e.serial;
221 SPRINT(buffer,"EPOINT(%d)", epoint);
225 switch (m[i].u.e.state) {
226 case ADMIN_STATE_IDLE:
230 case ADMIN_STATE_IN_SETUP:
232 addstr("'in << setup'");
234 case ADMIN_STATE_OUT_SETUP:
236 addstr("'out >> setup'");
238 case ADMIN_STATE_IN_OVERLAP:
240 addstr("'in << overlap'");
242 case ADMIN_STATE_OUT_OVERLAP:
244 addstr("'out >> overlap'");
246 case ADMIN_STATE_IN_PROCEEDING:
248 addstr("'in << proc'");
250 case ADMIN_STATE_OUT_PROCEEDING:
252 addstr("'out >> proc'");
254 case ADMIN_STATE_IN_ALERTING:
256 addstr("'in << alert'");
258 case ADMIN_STATE_OUT_ALERTING:
260 addstr("'out >> alert'");
262 case ADMIN_STATE_CONNECT:
266 case ADMIN_STATE_IN_DISCONNECT:
268 addstr("'in << disc'");
270 case ADMIN_STATE_OUT_DISCONNECT:
272 addstr("'out >> disc'");
276 addstr("'--NONE--'");
278 if (m[i].u.e.terminal[0]) {
280 addstr(" terminal=");
282 addstr(m[i].u.e.terminal);
285 SPRINT(buffer, " %s", m[i].u.e.callerid);
290 addstr(m[i].u.e.dialing);
291 if (m[i].u.e.action[0]) {
295 addstr(m[i].u.e.action);
299 addstr(" park="); /* 9 digits */
301 UCPY(buffer, "\""); /* 9 digits */
303 jj = m[i].u.e.park_len;
305 c = m[i].u.e.park_callid[j];
306 if (c >= 32 && c < 127 && c != '[') {
309 UPRINT(buffer+strlen(buffer), "[%02x]", c);
316 switch(m[i].u.e.rx_state) {
317 case NOTIFY_STATE_SUSPEND:
318 addstr(" in=suspend");
320 case NOTIFY_STATE_HOLD:
323 case NOTIFY_STATE_CONFERENCE:
324 addstr(" in=conference");
327 switch(m[i].u.e.tx_state) {
328 case NOTIFY_STATE_SUSPEND:
329 addstr(" out=suspend");
331 case NOTIFY_STATE_HOLD:
334 case NOTIFY_STATE_CONFERENCE:
335 addstr(" out=conference");
339 if (m[i].u.e.crypt) {
342 if (m[i].u.e.crypt) { /* crypt on */
350 /* loop all related ports */
352 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins+msg->u.s.epoints;
353 jj = j + msg->u.s.ports;
355 if (m[j].u.p.epoint == epoint) {
357 move(++line>1?line:1, 1);
362 move(line>1?line:1, 5);
365 move(line>1?line:1, 8);
366 if (line+2 >= LINES) break;
367 line = debug_port(msg, m, line, j, vline);
368 if (line+2 >= LINES) break;
374 move(ltee>1?line:1, 5);
380 int debug_join(struct admin_message *msg, struct admin_message *m, int line, int i)
382 unsigned int join = m[i].u.j.serial;
387 SPRINT(buffer,"JOIN(%d)", join);
389 if (m[i].u.j.partyline) {
391 addstr(" partyline=");
393 SPRINT(buffer, "%d\n", m[i].u.j.partyline);
396 if (m[i].u.j.remote[0]) {
400 SPRINT(buffer, "%s\n", m[i].u.j.remote);
403 /* find number of epoints */
404 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins;
405 jj = j + msg->u.s.epoints;
408 if (m[j].u.e.join == join)
412 /* loop all related endpoints */
413 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins;
414 jj = j + msg->u.s.epoints;
416 if (m[j].u.e.join == join) {
418 move(++line>1?line:1, 1);
424 move(line>1?line:1, 4);
425 if (line+2 >= LINES) break;
426 line = debug_epoint(msg, m, line, j, i?1:0);
427 if (line+2 >= LINES) break;
435 const char *admin_state(int sock, char *argv[])
437 struct admin_message msg,
441 int line, offset = 0, hoffset = 0;
450 char enter_string[128] = "", ch;
452 struct timeval select_tv;
454 /* flush logfile name */
461 /* send reload command */
462 memset(&msg, 0, sizeof(msg));
463 msg.message = ADMIN_REQUEST_STATE;
464 // printf("sizeof=%d\n",sizeof(msg));fflush(stdout);
465 if (write(sock, &msg, sizeof(msg)) != sizeof(msg)) {
467 return("Broken pipe while sending command.");
470 /* receive response */
471 if (read(sock, &msg, sizeof(msg)) != sizeof(msg)) {
473 return("Broken pipe while receiving response.");
476 if (msg.message != ADMIN_RESPONSE_STATE) {
478 return("Response not valid. Expecting state response.");
480 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
481 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
485 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off) {
488 // fprintf(stderr, "got=%d expected=%d\n", i, num*sizeof(struct admin_message));
490 return("Broken pipe while receiving state infos.");
492 if (len < num*(int)sizeof(struct admin_message)) {
500 // fprintf("getting =%d interfaces\n", msg.u.s.interfaces);
501 while(i < msg.u.s.interfaces) {
502 // fprintf(stderr, "j=%d message=%d\n", j, m[j].message);
503 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE) {
506 return("Response not valid. Expecting interface information.");
512 while(i < msg.u.s.remotes) {
513 if (m[j].message != ADMIN_RESPONSE_S_REMOTE) {
516 return("Response not valid. Expecting remote application information.");
522 while(i < msg.u.s.joins) {
523 if (m[j].message != ADMIN_RESPONSE_S_JOIN) {
526 return("Response not valid. Expecting join information.");
532 while(i < msg.u.s.epoints) {
533 if (m[j].message != ADMIN_RESPONSE_S_EPOINT) {
536 return("Response not valid. Expecting endpoint information.");
542 while(i < msg.u.s.ports) {
543 if (m[j].message != ADMIN_RESPONSE_S_PORT) {
546 return("Response not valid. Expecting port information.");
551 // now j is the number of message blocks
559 if (!!strcmp(logfile, msg.u.s.logfile)) {
560 SCPY(logfile, msg.u.s.logfile);
567 logline[i][1] = '\0';
571 logfh = open(logfile, O_RDONLY|O_NONBLOCK);
573 /* seek at the end -8000 chars */
574 lseek(logfh, -8000, SEEK_END);
575 /* if not at the beginning, read until endofline */
576 logline[logcur % LOGLINES][0] = '\0';
577 l = read(logfh, logline[logcur % LOGLINES], sizeof(logline[logcur % LOGLINES])-1);
579 /* read first line and skip junk */
580 logline[logcur % LOGLINES][l] = '\0';
581 if ((p = strchr(logline[logcur % LOGLINES],'\n'))) {
583 SCPY(logline[logcur % LOGLINES], p+1);
584 SCPY(logline[(logcur-1) % LOGLINES], "...");
594 ll = strlen(logline[logcur % LOGLINES]);
595 l = read(logfh, logline[logcur % LOGLINES]+ll, sizeof(logline[logcur % LOGLINES])-ll-1);
598 logline[logcur % LOGLINES][ll+l] = '\0';
600 /* put data to lines */
601 while ((p = strchr(logline[logcur % LOGLINES],'\n'))) {
604 SCPY(logline[logcur % LOGLINES], p+1);
606 /* if line is full without return, go next line */
607 if (strlen(logline[logcur % LOGLINES]) == sizeof(logline[logcur % LOGLINES])-1) {
609 logline[logcur % LOGLINES][0] = '\0';
614 /* display interfaces */
615 if (show_interfaces > 0) {
618 ii = i + msg.u.s.interfaces;
620 /* show interface summary */
621 move(++line>1?line:1, 0);
623 if (m[i].u.i.block >= 2) {
624 if (m[i].u.i.portnum < 0)
625 SPRINT(buffer, "%s (port ?: %s)%s", m[i].u.i.interface_name, m[i].u.i.portname, (m[i].u.i.extension)?" exten":"");
627 SPRINT(buffer, "%s (port %d: %s)%s", m[i].u.i.interface_name, m[i].u.i.portnum, m[i].u.i.portname, (m[i].u.i.extension)?" extension":"");
630 addstr(" not loaded");
632 SPRINT(buffer, "%s", m[i].u.i.interface_name);
635 SPRINT(buffer, "(port %d: %s)", m[i].u.i.portnum, m[i].u.i.portname);
638 SPRINT(buffer, " %s %s%s%s%s", (m[i].u.i.ntmode)?"NT":"TE", (m[i].u.i.ptp)?"ptp":"ptmp", (m[i].u.i.l1hold)?" l1hold":"", (m[i].u.i.l2hold)?" l2hold":"", (m[i].u.i.extension)?" extension":"");
644 SPRINT(buffer, " use:%d", m[i].u.i.use);
646 if (m[i].u.i.ptp || !m[i].u.i.ntmode) {
647 color((m[i].u.i.l2link > 0)?green:red);
648 if (m[i].u.i.l2link < 0)
651 addstr((m[i].u.i.l2link)?" L2 UP":" L2 down");
657 if (m[i].u.i.l2mask[j>>3] & (1 << (j&7))) {
658 SPRINT(buffer, "%s%d", k?",":" TEI(", j);
667 color((m[i].u.i.l1link > 0)?green:blue);
668 if (m[i].u.i.l1link < 0)
671 addstr((m[i].u.i.l1link)?" L1 UP":" L1 down");
684 if (m[i].u.i.slip_tx || m[i].u.i.slip_rx) {
686 SPRINT(buffer, " SLIP(tx:%d rx:%d)", m[i].u.i.slip_tx, m[i].u.i.slip_rx);
689 if (m[i].u.i.block) {
693 if (line+2 >= LINES) goto end;
695 if (show_interfaces > 1) {
698 jj = m[i].u.i.channels;
700 /* show all channels */
701 if (show_interfaces>2 || m[i].u.i.busy[j]>0) {
703 /* show left side / right side */
704 if ((k & 1) && (COLS > 70)) {
705 move(line>1?line:1,4+((COLS-4)/2));
707 move(++line>1?line:1, 1);
714 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
716 SPRINT(buffer,"B%2d: ", j+1);
717 if (m[i].u.i.mode[j] == B_MODE_HDLC)
718 SCAT(buffer,"HDLC ");
720 switch(m[i].u.i.busy[j]) {
722 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block) {
730 case B_STATE_ACTIVATING:
738 case B_STATE_DEACTIVATING:
742 case B_STATE_EXPORTING:
750 case B_STATE_IMPORTING:
755 if (m[i].u.i.port[j]) {
756 /* search for port */
757 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
758 ll = l+msg.u.s.ports;
760 if (m[l].u.p.serial == m[i].u.i.port[j]) {
761 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
767 if (line+2 >= LINES) {
770 move(line>1?line:1, 1);
780 move(line>1?line:1, 1);
783 if (line+2 >= LINES) goto end;
784 /* show summary if no channels were shown */
785 if (show_interfaces<2 && ltee==0) {
787 move(++line>1?line:1, 1);
790 if (m[i].u.i.l2link && m[i].u.i.block==0) {
792 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
795 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
799 if (line+2 >= LINES) goto end;
805 i = msg.u.s.interfaces;
806 ii = i + msg.u.s.remotes;
808 /* show remote summary */
809 move(++line>1?line:1, 0);
811 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
817 if (line+2 >= LINES) goto end;
819 /* display calls (brief) */
820 if (show_calls == 1) {
822 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
823 ii = i+msg.u.s.epoints;
825 /* for each endpoint... */
826 if (!m[i].u.e.join) {
827 move(++line>1?line:1, 0);
829 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
832 if (m[i].u.e.terminal[0]) {
835 addstr(m[i].u.e.terminal);
839 SPRINT(buffer, " %s", m[i].u.e.callerid);
844 SPRINT(buffer, "%s", m[i].u.e.dialing);
846 if (m[i].u.e.action[0]) {
850 addstr(m[i].u.e.action);
852 if (line+2 >= LINES) goto end;
857 j = msg.u.s.interfaces+msg.u.s.remotes;
858 jj = j+msg.u.s.joins;
860 /* for each call... */
861 move(++line>1?line:1, 0);
863 SPRINT(buffer, "(%d):", m[j].u.j.serial);
865 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
866 ii = i+msg.u.s.epoints;
868 /* for each endpoint... */
869 if (m[i].u.e.join == m[j].u.j.serial) {
871 SPRINT(buffer, " (%d)", m[i].u.e.serial);
874 if (m[i].u.e.terminal[0]) {
877 addstr(m[i].u.e.terminal);
881 SPRINT(buffer, "-%s", m[i].u.e.callerid);
886 SPRINT(buffer, "%s", m[i].u.e.dialing);
892 if (line+2 >= LINES) goto end;
897 if (line+2 >= LINES) goto end;
899 /* display calls (structurd) */
900 if (show_calls == 2) {
901 /* show all ports with no epoint */
903 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
904 ii = i+msg.u.s.ports;
906 if (!m[i].u.p.epoint) {
907 move(++line>1?line:1, 8);
908 if (line+2 >= LINES) goto end;
909 line = debug_port(&msg, m, line, i, 0);
910 if (line+2 >= LINES) goto end;
917 if (line+2 >= LINES) goto end;
919 /* show all epoints with no call */
921 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
922 ii = i+msg.u.s.epoints;
924 if (!m[i].u.e.join) {
925 move(++line>1?line:1, 4);
926 if (line+2 >= LINES) goto end;
927 line = debug_epoint(&msg, m, line, i, 0);
928 if (line+2 >= LINES) goto end;
935 if (line+2 >= LINES) goto end;
939 i = msg.u.s.interfaces+msg.u.s.remotes;
940 ii = i+msg.u.s.joins;
942 move(++line>1?line:1, 0);
943 if (line+2 >= LINES) goto end;
944 line = debug_join(&msg, m, line, i);
945 if (line+2 >= LINES) goto end;
951 if (line+2 >= LINES) goto end;
957 if (line+2 < LINES) {
958 move(line++>1?line-1:1, 0);
960 hline(ACS_HLINE, COLS);
963 l = logcur-(LINES-line-2);
965 if (ll-l >= LOGLINES)
968 move(line++>1?line-1:1, 0);
969 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
970 SCPY(buffer, logline[l % LOGLINES] + hoffset);
973 if (COLS < (int)strlen(buffer)) {
974 buffer[COLS-1] = '\0';
989 /* display name/time */
994 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
995 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
999 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1000 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1001 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1004 /* displeay head line */
1007 hline(ACS_HLINE, COLS);
1010 SPRINT(buffer, "Offset +%d", offset);
1016 SPRINT(buffer, "H-Offset +%d", hoffset);
1023 hline(ACS_HLINE, COLS);
1027 SPRINT(buffer, "-> %s", enter_string);
1030 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1036 if (lastlines!=LINES || lastcols!=COLS) {
1043 /* user input in enter mode */
1050 if (!enter_string[0])
1053 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1054 if (!!strncmp(enter_string, "interface", 10) &&
1055 !!strncmp(enter_string, "route", 6) &&
1056 !!strncmp(enter_string, "release ", 8) &&
1057 !!strncmp(enter_string, "block ", 6) &&
1058 !!strncmp(enter_string, "unblock ", 8) &&
1059 !!strncmp(enter_string, "load ", 5) &&
1060 !!strncmp(enter_string, "unload ", 7)) {
1061 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1062 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1063 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1064 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1065 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1066 SPRINT(logline[logcur++ % LOGLINES], "unblock/load <port> (unblock port for further calls, load if not loaded)");
1067 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1069 /* applend output to log window */
1070 SPRINT(buffer, "%s %s", argv[0], enter_string);
1071 fp = popen(buffer, "r");
1073 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1074 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1077 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1080 logline[logcur % LOGLINES][0] = '\0';
1081 enter_string[0] = '\0';
1084 if (ch>=32 && ch<=126) {
1085 SCCAT(enter_string, ch);
1091 if (ch==8 || ch==127) {
1092 if (enter_string[0])
1093 enter_string[strlen(enter_string)-1] = '\0';
1103 FD_ZERO(&select_rfds);
1104 FD_SET(0, &select_rfds);
1105 select_tv.tv_sec = 0;
1106 select_tv.tv_usec = 250000;
1107 select(1, &select_rfds, NULL, NULL, &select_tv);
1111 /* user input in normal mode */
1113 case 12: /* refresh */
1124 case 'i': /* toggle interface */
1126 if (show_interfaces > 3) show_interfaces = 0;
1129 case 'c': /* toggle calls */
1131 if (show_calls > 2) show_calls = 0;
1134 case 'l': /* toggle log */
1136 if (show_log > 1) show_log = 0;
1139 case '+': /* scroll down */
1143 case '-': /* scroll up */
1148 case '*': /* scroll right */
1152 case '/': /* scroll left */
1157 case 10: /* entermode */
1162 FD_ZERO(&select_rfds);
1163 FD_SET(0, &select_rfds);
1164 select_tv.tv_sec = 0;
1165 select_tv.tv_usec = 250000;
1166 select(1, &select_rfds, NULL, NULL, &select_tv);
1171 /* check for logfh */
1176 /* cleanup curses and exit */
1182 const char *admin_portinfo(int sock, int argc, char *argv[])
1184 struct admin_message msg,
1191 /* send state request command */
1192 memset(&msg, 0, sizeof(msg));
1193 msg.message = ADMIN_REQUEST_STATE;
1194 if (write(sock, &msg, sizeof(msg)) != sizeof(msg)) {
1196 return("Broken pipe while sending command.");
1199 /* receive response */
1200 if (read(sock, &msg, sizeof(msg)) != sizeof(msg)) {
1202 return("Broken pipe while receiving response.");
1205 if (msg.message != ADMIN_RESPONSE_STATE) {
1207 return("Response not valid. Expecting state response.");
1209 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
1210 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
1214 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off) {
1218 return("Broken pipe while receiving state infos.");
1220 if (len < num*(int)sizeof(struct admin_message)) {
1228 while(i < msg.u.s.interfaces) {
1229 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE) {
1232 return("Response not valid. Expecting interface information.");
1238 while(i < msg.u.s.remotes) {
1239 if (m[j].message != ADMIN_RESPONSE_S_REMOTE) {
1242 return("Response not valid. Expecting remote application information.");
1248 while(i < msg.u.s.joins) {
1249 if (m[j].message != ADMIN_RESPONSE_S_JOIN) {
1252 return("Response not valid. Expecting join information.");
1258 while(i < msg.u.s.epoints) {
1259 if (m[j].message != ADMIN_RESPONSE_S_EPOINT) {
1262 return("Response not valid. Expecting endpoint information.");
1268 while(i < msg.u.s.ports) {
1269 if (m[j].message != ADMIN_RESPONSE_S_PORT) {
1272 return("Response not valid. Expecting port information.");
1277 // now j is the number of message blocks
1279 /* output interfaces */
1281 ii = i + msg.u.s.interfaces;
1284 if (!!strcmp(argv[2], m[i].u.i.interface_name)) {
1289 printf("%s:\n", m[i].u.i.interface_name);
1290 if (m[i].u.i.portnum < 0)
1291 printf("\t port = unknown\n");
1293 printf("\t port = %d \"%s\"\n",m[i].u.i.portnum, m[i].u.i.portname);
1294 printf("\t extension = %s\n", (m[i].u.i.extension)?"yes":"no");
1295 if (m[i].u.i.block >= 2) {
1296 printf("\t status = not loaded\n");
1299 printf("\t status = blocked\n");
1301 printf("\t status = unblocked\n");
1302 printf("\t mode = %s %s%s%s\n", (m[i].u.i.ntmode)?"NT-mode":"TE-mode", (m[i].u.i.ptp)?"ptp":"ptmp", (m[i].u.i.l1hold)?" l1hold":"", (m[i].u.i.l2hold)?" l2hold":"");
1303 if (m[i].u.i.l1link < 0)
1304 printf("\t l1 link = unknown\n");
1306 printf("\t l1 link = %s\n", (m[i].u.i.l1link)?"up":"down");
1307 if (m[i].u.i.ptp || !m[i].u.i.ntmode) {
1308 if (m[i].u.i.l2link < 0)
1309 printf("\t l2 link = unknown\n");
1311 printf("\t l2 link = %s\n", (m[i].u.i.l2link)?"up":"down");
1313 printf("\t usage = %d\n", m[i].u.i.use);
1326 * Send command and show error message.
1328 const char *admin_cmd(int sock, int mode, char *extension, char *number)
1330 static struct admin_message msg;
1332 /* send reload command */
1333 memset(&msg, 0, sizeof(msg));
1335 case MODE_INTERFACE:
1336 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1339 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1342 msg.message = ADMIN_REQUEST_CMD_DIAL;
1343 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1346 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1347 SCPY(msg.u.x.message, number);
1350 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1351 msg.u.x.portnum = atoi(number);
1355 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1356 msg.u.x.portnum = atoi(number);
1360 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1361 msg.u.x.portnum = atoi(number);
1366 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1367 return("Broken pipe while sending command.");
1369 /* receive response */
1370 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1371 return("Broken pipe while receiving response.");
1373 case MODE_INTERFACE:
1374 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1375 return("Response not valid.");
1378 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1379 return("Response not valid.");
1382 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1383 return("Response not valid.");
1386 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1387 return("Response not valid.");
1392 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1393 return("Response not valid.");
1397 /* process response */
1398 if (msg.u.x.error) {
1399 return(msg.u.x.message);
1401 printf("Command successfull.\n");
1409 #define GET_NOW() { \
1410 gettimeofday(&now_tv, &now_tz); \
1411 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1413 const char *admin_testcall(int sock, int argc, char *argv[])
1415 static struct admin_message msg;
1417 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1419 double timer = 0, now_d;
1420 unsigned int on = 1;
1421 struct timeval now_tv;
1422 struct timezone now_tz;
1424 printf("pid=%d\n", getpid()); fflush(stdout);
1427 if (!strcmp(argv[ar], "--setup-timeout")) {
1430 return("Missing setup timeout value.\n");
1431 stimeout = atoi(argv[ar]);
1434 if (!strcmp(argv[ar], "--proceeding-timeout")) {
1437 return("Missing proceeding timeout value.\n");
1438 ptimeout = atoi(argv[ar]);
1441 if (!strcmp(argv[ar], "--alerting-timeout")) {
1444 return("Missing alerting timeout value.\n");
1445 atimeout = atoi(argv[ar]);
1448 if (!strcmp(argv[ar], "--connect-timeout")) {
1451 return("Missing connect timeout value.\n");
1452 ctimeout = atoi(argv[ar]);
1459 /* send reload command */
1460 memset(&msg, 0, sizeof(msg));
1461 msg.message = ADMIN_CALL_SETUP;
1462 msg.u.call.present = 1;
1465 SCPY(msg.u.call.interface, argv[ar]);
1469 SCPY(msg.u.call.callerid, argv[ar]);
1473 SCPY(msg.u.call.dialing, argv[ar]);
1477 if (argv[ar][0] == 'r')
1478 msg.u.call.present = 0;
1481 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1482 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1483 msg.u.call.bc_info1 = 0;
1485 msg.u.call.exthlc = 0;
1487 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1489 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1492 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1493 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1497 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1498 if (msg.u.call.bc_info1 < 0)
1499 msg.u.call.bc_info1 = 0;
1501 msg.u.call.bc_info1 |= 0x80;
1505 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1506 if (msg.u.call.hlc < 0)
1509 msg.u.call.hlc |= 0x80;
1513 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1514 if (msg.u.call.exthlc < 0)
1515 msg.u.call.exthlc = 0;
1517 msg.u.call.exthlc |= 0x80;
1521 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1522 return("Broken pipe while sending command.");
1524 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1525 return("Failed to set socket into non-blocking IO.");
1529 timer = now_d + (double)stimeout;
1532 /* receive response */
1534 l = read(sock, &msg, sizeof(msg));
1536 if (errno == EWOULDBLOCK) {
1539 if (timer <= now_d) {
1540 printf("Timeout\n"); fflush(stdout);
1547 return("Broken pipe while receiving response.");
1549 if (l != sizeof(msg))
1550 return("Response has unexpected message size.");
1551 switch(msg.message) {
1552 case ADMIN_CALL_SETUP_ACK:
1553 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1556 case ADMIN_CALL_PROCEEDING:
1557 printf("PROCEEDING\n"); fflush(stdout);
1560 timer = now_d + (double)ptimeout;
1564 case ADMIN_CALL_ALERTING:
1565 printf("ALERTING\n"); fflush(stdout);
1568 timer = now_d + (double)atimeout;
1572 case ADMIN_CALL_CONNECT:
1573 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1576 timer = now_d + (double)ctimeout;
1580 case ADMIN_CALL_NOTIFY:
1581 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1584 case ADMIN_CALL_DISCONNECT:
1585 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);
1588 case ADMIN_CALL_RELEASE:
1589 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);
1593 return("Response not valid.");
1596 printf("Call released.\n"); fflush(stdout);
1604 const char *admin_trace(int sock, int argc, char *argv[])
1606 static struct admin_message msg;
1610 if (argc > 2) if (!strcasecmp(argv[2], "help")) {
1611 printf("Trace Help\n----------\n");
1612 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1613 printf("By default a complete trace is shown in detailed format.\n");
1614 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1615 printf("Use filter values to select specific trace messages.\n");
1616 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1617 printf("Filters:\n");
1618 printf(" category=<mask bits>\n");
1619 printf(" 0x01 = CH: channel object trace\n");
1620 printf(" 0x02 = EP: endpoint object trace\n");
1621 printf(" port=<mISDN port> select only given port for trace\n");
1622 printf(" interface=<interface name> select only given interface for trace\n");
1623 printf(" caller=<caller id> select only given caller id for trace\n");
1624 printf(" dialing=<number> select only given dialed number for trace\n");
1628 /* init trace request */
1629 memset(&msg, 0, sizeof(msg));
1630 msg.message = ADMIN_TRACE_REQUEST;
1631 msg.u.trace_req.detail = 3;
1632 msg.u.trace_req.port = -1;
1637 if (!strcasecmp(argv[i], "brief"))
1638 msg.u.trace_req.detail = 1;
1639 else if (!strcasecmp(argv[i], "short"))
1640 msg.u.trace_req.detail = 2;
1641 else if (!strncasecmp(argv[i], "category=", 9))
1642 msg.u.trace_req.category = atoi(argv[i]+9);
1643 else if (!strncasecmp(argv[i], "port=", 5))
1644 msg.u.trace_req.port = atoi(argv[i]+5);
1645 else if (!strncasecmp(argv[i], "interface=", 10))
1646 SCPY(msg.u.trace_req.interface, argv[i]+10);
1647 else if (!strncasecmp(argv[i], "caller=", 7))
1648 SCPY(msg.u.trace_req.caller, argv[i]+7);
1649 else if (!strncasecmp(argv[i], "dialing=", 8))
1650 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1651 else return("Invalid trace option, try 'trace help'.");
1656 /* send trace request */
1657 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1658 return("Broken pipe while sending trace request.");
1660 /* receive response */
1662 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1663 return("Broken pipe while receiving response.");
1665 if (msg.message != ADMIN_TRACE_RESPONSE)
1666 return("Response not valid.");
1668 printf("%s", msg.u.trace_rsp.text);
1676 int main(int argc, char *argv[])
1680 struct sockaddr_un sock_address;
1681 const char *ret = "invalid mode";
1688 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1689 printf("state - View current states using graphical console output.\n");
1690 printf("portinfo - Get info of current ports.\n");
1691 printf("interface [<portname>] - Tell LCR to reload \"interface.conf\".\n");
1692 printf("route - Tell LCR to reload \"route.conf\".\n");
1693 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1694 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1695 printf("block <port> - Block given port.\n");
1696 printf("unblock/load <port> - Unblock given port.\n");
1697 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1698 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1699 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1700 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1701 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1702 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1703 printf(" -> Use 'trace help' to see filter description.\n");
1709 if (!(strcasecmp(argv[1],"state"))) {
1712 if (!(strcasecmp(argv[1],"portinfo"))) {
1713 mode = MODE_PORTINFO;
1715 if (!(strcasecmp(argv[1],"interface"))) {
1716 mode = MODE_INTERFACE;
1718 if (!(strcasecmp(argv[1],"route"))) {
1721 if (!(strcasecmp(argv[1],"dial"))) {
1726 if (!(strcasecmp(argv[1],"release"))) {
1729 mode = MODE_RELEASE;
1731 if (!(strcasecmp(argv[1],"unblock"))
1732 || !(strcasecmp(argv[1],"load"))) {
1735 mode = MODE_UNBLOCK;
1737 if (!(strcasecmp(argv[1],"block"))) {
1742 if (!(strcasecmp(argv[1],"unload"))) {
1747 if (!(strcasecmp(argv[1],"testcall"))) {
1750 mode = MODE_TESTCALL;
1752 if (!(strcasecmp(argv[1],"trace"))) {
1758 if (read_options() == 0) {
1764 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1765 fprintf(stderr, "Failed to create socket.\n");
1768 memset(&sock_address, 0, sizeof(sock_address));
1769 SPRINT(sock_address.sun_path, SOCKET_NAME, options.lock);
1770 sock_address.sun_family = PF_UNIX;
1771 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1773 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1780 ret = admin_state(sock, argv);
1784 ret = admin_portinfo(sock, argc, argv);
1787 case MODE_INTERFACE:
1789 ret = admin_cmd(sock, mode, NULL, NULL);
1793 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1800 ret = admin_cmd(sock, mode, NULL, argv[2]);
1804 ret = admin_testcall(sock, argc, argv);
1808 ret = admin_trace(sock, argc, argv);
1813 /* now we say good bye */
1815 // if (!strncasecmp(ret, "Broken Pipe", 11))
1817 printf("%s\n", ret);