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>
30 #include "extension.h"
32 #include "lcrsocket.h"
35 #define LTEE {addch(ACS_LTEE);addch(ACS_HLINE);addch(ACS_HLINE);}
36 #define LLCORNER {addch(ACS_LLCORNER);addch(ACS_HLINE);addch(ACS_HLINE);}
37 #define VLINE {addch(ACS_VLINE);addstr(" ");}
38 #define EMPTY {addstr(" ");}
39 //char rotator[] = {'-', '\\', '|', '/'};
40 int lastlines, lastcols;
41 int show_interfaces = 2,
59 const char *text_interfaces[] = {
66 const char *text_calls[] = {
81 char logline[LOGLINES][512];
82 unsigned int logcur = 0;
89 void init_curses(void)
92 initscr(); cbreak(); noecho();
94 nodelay(stdscr, TRUE);
95 if (COLOR_PAIRS>=8 && COLORS>=8)
109 void cleanup_curses(void)
114 void color(int color)
116 if (COLOR_PAIRS>=8 && COLORS>=8)
117 attrset(COLOR_PAIR(color));
121 * permanently show current state using ncurses
123 int debug_port(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
130 SPRINT(buffer,"%s(%d)", m[i].u.p.name,m[i].u.p.serial);
134 switch (m[i].u.p.state) {
135 case ADMIN_STATE_IDLE:
139 case ADMIN_STATE_IN_SETUP:
141 addstr("'in << setup'");
143 case ADMIN_STATE_OUT_SETUP:
145 addstr("'out >> setup'");
147 case ADMIN_STATE_IN_OVERLAP:
149 addstr("'in << overlap'");
151 case ADMIN_STATE_OUT_OVERLAP:
153 addstr("'out >> overlap'");
155 case ADMIN_STATE_IN_PROCEEDING:
157 addstr("'in << proc'");
159 case ADMIN_STATE_OUT_PROCEEDING:
161 addstr("'out >> proc'");
163 case ADMIN_STATE_IN_ALERTING:
165 addstr("'in << alert'");
167 case ADMIN_STATE_OUT_ALERTING:
169 addstr("'out >> alert'");
171 case ADMIN_STATE_CONNECT:
175 case ADMIN_STATE_IN_DISCONNECT:
177 addstr("'in << disc'");
179 case ADMIN_STATE_OUT_DISCONNECT:
181 addstr("'out >> disc'");
183 case ADMIN_STATE_RELEASE:
189 addstr("'--NONE--'");
194 addstr(" bchannel=");
196 SPRINT(buffer,"%d", m[i].u.p.isdn_chan);
198 if (m[i].u.p.isdn_ces >= 0) {
202 SPRINT(buffer, "%d", m[i].u.p.isdn_ces);
205 if (m[i].u.p.isdn_hold) {
213 int debug_epoint(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
215 unsigned int epoint = m[i].u.e.serial;
222 SPRINT(buffer,"EPOINT(%d)", epoint);
226 switch (m[i].u.e.state) {
227 case ADMIN_STATE_IDLE:
231 case ADMIN_STATE_IN_SETUP:
233 addstr("'in << setup'");
235 case ADMIN_STATE_OUT_SETUP:
237 addstr("'out >> setup'");
239 case ADMIN_STATE_IN_OVERLAP:
241 addstr("'in << overlap'");
243 case ADMIN_STATE_OUT_OVERLAP:
245 addstr("'out >> overlap'");
247 case ADMIN_STATE_IN_PROCEEDING:
249 addstr("'in << proc'");
251 case ADMIN_STATE_OUT_PROCEEDING:
253 addstr("'out >> proc'");
255 case ADMIN_STATE_IN_ALERTING:
257 addstr("'in << alert'");
259 case ADMIN_STATE_OUT_ALERTING:
261 addstr("'out >> alert'");
263 case ADMIN_STATE_CONNECT:
267 case ADMIN_STATE_IN_DISCONNECT:
269 addstr("'in << disc'");
271 case ADMIN_STATE_OUT_DISCONNECT:
273 addstr("'out >> disc'");
277 addstr("'--NONE--'");
279 if (m[i].u.e.terminal[0]) {
281 addstr(" terminal=");
283 addstr(m[i].u.e.terminal);
286 SPRINT(buffer, " %s", m[i].u.e.callerid);
291 addstr(m[i].u.e.dialing);
292 if (m[i].u.e.action[0]) {
296 addstr(m[i].u.e.action);
300 addstr(" park="); /* 9 digits */
302 UCPY(buffer, "\""); /* 9 digits */
304 jj = m[i].u.e.park_len;
306 c = m[i].u.e.park_callid[j];
307 if (c >= 32 && c < 127 && c != '[') {
310 UPRINT(buffer+strlen(buffer), "[%02x]", c);
317 switch(m[i].u.e.rx_state) {
318 case NOTIFY_STATE_SUSPEND:
319 addstr(" in=suspend");
321 case NOTIFY_STATE_HOLD:
324 case NOTIFY_STATE_CONFERENCE:
325 addstr(" in=conference");
328 switch(m[i].u.e.tx_state) {
329 case NOTIFY_STATE_SUSPEND:
330 addstr(" out=suspend");
332 case NOTIFY_STATE_HOLD:
335 case NOTIFY_STATE_CONFERENCE:
336 addstr(" out=conference");
340 if (m[i].u.e.crypt) {
343 if (m[i].u.e.crypt) { /* crypt on */
351 /* loop all related ports */
353 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins+msg->u.s.epoints;
354 jj = j + msg->u.s.ports;
356 if (m[j].u.p.epoint == epoint) {
358 move(++line>1?line:1, 1);
363 move(line>1?line:1, 5);
366 move(line>1?line:1, 8);
367 if (line+2 >= LINES) break;
368 line = debug_port(msg, m, line, j, vline);
369 if (line+2 >= LINES) break;
375 move(ltee>1?line:1, 5);
381 int debug_join(struct admin_message *msg, struct admin_message *m, int line, int i)
383 unsigned int join = m[i].u.j.serial;
388 SPRINT(buffer,"JOIN(%d)", join);
390 if (m[i].u.j.threepty) {
394 SPRINT(buffer, "%d\n", m[i].u.j.threepty);
397 if (m[i].u.j.partyline) {
399 addstr(" partyline=");
401 SPRINT(buffer, "%d\n", m[i].u.j.partyline);
404 if (m[i].u.j.remote[0]) {
408 SPRINT(buffer, "%s\n", m[i].u.j.remote);
411 /* find number of epoints */
412 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins;
413 jj = j + msg->u.s.epoints;
416 if (m[j].u.e.join == join)
420 /* loop all related endpoints */
421 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins;
422 jj = j + msg->u.s.epoints;
424 if (m[j].u.e.join == join) {
426 move(++line>1?line:1, 1);
432 move(line>1?line:1, 4);
433 if (line+2 >= LINES) break;
434 line = debug_epoint(msg, m, line, j, i?1:0);
435 if (line+2 >= LINES) break;
443 const char *admin_state(int sock, char *argv[])
445 struct admin_message msg,
449 int line, offset = 0, hoffset = 0;
458 char enter_string[128] = "", ch;
460 struct timeval select_tv;
462 /* flush logfile name */
469 /* send reload command */
470 memset(&msg, 0, sizeof(msg));
471 msg.message = ADMIN_REQUEST_STATE;
472 // printf("sizeof=%d\n",sizeof(msg));fflush(stdout);
473 if (write(sock, &msg, sizeof(msg)) != sizeof(msg)) {
475 return("Broken pipe while sending command.");
478 /* receive response */
479 if (read(sock, &msg, sizeof(msg)) != sizeof(msg)) {
481 return("Broken pipe while receiving response.");
484 if (msg.message != ADMIN_RESPONSE_STATE) {
486 return("Response not valid. Expecting state response.");
488 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
489 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
493 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off) {
496 // fprintf(stderr, "got=%d expected=%d\n", i, num*sizeof(struct admin_message));
498 return("Broken pipe while receiving state infos.");
500 if (len < num*(int)sizeof(struct admin_message)) {
508 // fprintf("getting =%d interfaces\n", msg.u.s.interfaces);
509 while(i < msg.u.s.interfaces) {
510 // fprintf(stderr, "j=%d message=%d\n", j, m[j].message);
511 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE) {
514 return("Response not valid. Expecting interface information.");
520 while(i < msg.u.s.remotes) {
521 if (m[j].message != ADMIN_RESPONSE_S_REMOTE) {
524 return("Response not valid. Expecting remote application information.");
530 while(i < msg.u.s.joins) {
531 if (m[j].message != ADMIN_RESPONSE_S_JOIN) {
534 return("Response not valid. Expecting join information.");
540 while(i < msg.u.s.epoints) {
541 if (m[j].message != ADMIN_RESPONSE_S_EPOINT) {
544 return("Response not valid. Expecting endpoint information.");
550 while(i < msg.u.s.ports) {
551 if (m[j].message != ADMIN_RESPONSE_S_PORT) {
554 return("Response not valid. Expecting port information.");
559 // now j is the number of message blocks
567 if (!!strcmp(logfile, msg.u.s.logfile)) {
568 SCPY(logfile, msg.u.s.logfile);
575 logline[i][1] = '\0';
579 logfh = open(logfile, O_RDONLY|O_NONBLOCK);
581 /* seek at the end -8000 chars */
582 lseek(logfh, -8000, SEEK_END);
583 /* if not at the beginning, read until endofline */
584 logline[logcur % LOGLINES][0] = '\0';
585 l = read(logfh, logline[logcur % LOGLINES], sizeof(logline[logcur % LOGLINES])-1);
587 /* read first line and skip junk */
588 logline[logcur % LOGLINES][l] = '\0';
589 if ((p = strchr(logline[logcur % LOGLINES],'\n'))) {
591 SCPY(logline[logcur % LOGLINES], p+1);
592 SCPY(logline[(logcur-1) % LOGLINES], "...");
602 ll = strlen(logline[logcur % LOGLINES]);
603 l = read(logfh, logline[logcur % LOGLINES]+ll, sizeof(logline[logcur % LOGLINES])-ll-1);
606 logline[logcur % LOGLINES][ll+l] = '\0';
608 /* put data to lines */
609 while ((p = strchr(logline[logcur % LOGLINES],'\n'))) {
612 SCPY(logline[logcur % LOGLINES], p+1);
614 /* if line is full without return, go next line */
615 if (strlen(logline[logcur % LOGLINES]) == sizeof(logline[logcur % LOGLINES])-1) {
617 logline[logcur % LOGLINES][0] = '\0';
622 /* display interfaces */
623 if (show_interfaces > 0) {
626 ii = i + msg.u.s.interfaces;
628 /* show interface summary */
629 move(++line>1?line:1, 0);
631 if (m[i].u.i.portnum == -100) {
632 SPRINT(buffer, "%s %s", m[i].u.i.interface_name, (m[i].u.i.extension)?" exten":"");
634 } else if (m[i].u.i.block >= 2) {
635 if (m[i].u.i.portnum < 0)
636 SPRINT(buffer, "%s (port ?: %s)%s", m[i].u.i.interface_name, m[i].u.i.portname, (m[i].u.i.extension)?" exten":"");
638 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":"");
641 addstr(" not loaded");
643 SPRINT(buffer, "%s", m[i].u.i.interface_name);
646 SPRINT(buffer, "(port %d: %s)", m[i].u.i.portnum, m[i].u.i.portname);
649 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":"");
655 SPRINT(buffer, " use:%d", m[i].u.i.use);
657 if (m[i].u.i.ptp || !m[i].u.i.ntmode) {
658 color((m[i].u.i.l2link > 0)?green:red);
659 if (m[i].u.i.l2link < 0)
662 addstr((m[i].u.i.l2link)?" L2 UP":" L2 down");
668 if (m[i].u.i.l2mask[j>>3] & (1 << (j&7))) {
669 SPRINT(buffer, "%s%d", k?",":" TEI(", j);
678 color((m[i].u.i.l1link > 0)?green:blue);
679 if (m[i].u.i.l1link < 0)
682 addstr((m[i].u.i.l1link)?" L1 UP":" L1 down");
695 if (m[i].u.i.slip_tx || m[i].u.i.slip_rx) {
697 SPRINT(buffer, " SLIP(tx:%d rx:%d)", m[i].u.i.slip_tx, m[i].u.i.slip_rx);
700 if (m[i].u.i.block) {
704 if (line+2 >= LINES) goto end;
706 if (show_interfaces > 1) {
709 jj = m[i].u.i.channels;
711 /* show all channels */
712 if (show_interfaces>2 || m[i].u.i.busy[j]>0) {
714 /* show left side / right side */
715 if ((k & 1) && (COLS > 70)) {
716 move(line>1?line:1,4+((COLS-4)/2));
718 move(++line>1?line:1, 1);
725 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
727 SPRINT(buffer,"B%2d: ", j+1);
728 if (m[i].u.i.mode[j] == B_MODE_HDLC)
729 SCAT(buffer,"HDLC ");
731 switch(m[i].u.i.busy[j]) {
733 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block) {
741 case B_STATE_ACTIVATING:
749 case B_STATE_DEACTIVATING:
754 if (m[i].u.i.port[j]) {
755 /* search for port */
756 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
757 ll = l+msg.u.s.ports;
759 if (m[l].u.p.serial == m[i].u.i.port[j]) {
760 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
766 if (line+2 >= LINES) {
769 move(line>1?line:1, 1);
779 move(line>1?line:1, 1);
782 if (line+2 >= LINES) goto end;
783 /* show summary if no channels were shown */
784 if (show_interfaces<2 && ltee==0) {
786 move(++line>1?line:1, 1);
789 if (m[i].u.i.l2link && m[i].u.i.block==0) {
791 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
794 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
798 if (line+2 >= LINES) goto end;
804 i = msg.u.s.interfaces;
805 ii = i + msg.u.s.remotes;
807 /* show remote summary */
808 move(++line>1?line:1, 0);
810 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
816 if (line+2 >= LINES) goto end;
818 /* display calls (brief) */
819 if (show_calls == 1) {
821 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
822 ii = i+msg.u.s.epoints;
824 /* for each endpoint... */
825 if (!m[i].u.e.join) {
826 move(++line>1?line:1, 0);
828 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
831 if (m[i].u.e.terminal[0]) {
834 addstr(m[i].u.e.terminal);
838 SPRINT(buffer, " %s", m[i].u.e.callerid);
843 SPRINT(buffer, "%s", m[i].u.e.dialing);
845 if (m[i].u.e.action[0]) {
849 addstr(m[i].u.e.action);
851 if (line+2 >= LINES) goto end;
856 j = msg.u.s.interfaces+msg.u.s.remotes;
857 jj = j+msg.u.s.joins;
859 /* for each call... */
860 move(++line>1?line:1, 0);
862 SPRINT(buffer, "(%d):", m[j].u.j.serial);
864 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
865 ii = i+msg.u.s.epoints;
867 /* for each endpoint... */
868 if (m[i].u.e.join == m[j].u.j.serial) {
870 SPRINT(buffer, " (%d)", m[i].u.e.serial);
873 if (m[i].u.e.terminal[0]) {
876 addstr(m[i].u.e.terminal);
880 SPRINT(buffer, "-%s", m[i].u.e.callerid);
885 SPRINT(buffer, "%s", m[i].u.e.dialing);
891 if (line+2 >= LINES) goto end;
896 if (line+2 >= LINES) goto end;
898 /* display calls (structurd) */
899 if (show_calls == 2) {
900 /* show all ports with no epoint */
902 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
903 ii = i+msg.u.s.ports;
905 if (!m[i].u.p.epoint) {
906 move(++line>1?line:1, 8);
907 if (line+2 >= LINES) goto end;
908 line = debug_port(&msg, m, line, i, 0);
909 if (line+2 >= LINES) goto end;
916 if (line+2 >= LINES) goto end;
918 /* show all epoints with no call */
920 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
921 ii = i+msg.u.s.epoints;
923 if (!m[i].u.e.join) {
924 move(++line>1?line:1, 4);
925 if (line+2 >= LINES) goto end;
926 line = debug_epoint(&msg, m, line, i, 0);
927 if (line+2 >= LINES) goto end;
934 if (line+2 >= LINES) goto end;
938 i = msg.u.s.interfaces+msg.u.s.remotes;
939 ii = i+msg.u.s.joins;
941 move(++line>1?line:1, 0);
942 if (line+2 >= LINES) goto end;
943 line = debug_join(&msg, m, line, i);
944 if (line+2 >= LINES) goto end;
950 if (line+2 >= LINES) goto end;
956 if (line+2 < LINES) {
957 move(line++>1?line-1:1, 0);
959 hline(ACS_HLINE, COLS);
962 l = logcur-(LINES-line-2);
964 if (ll-l >= LOGLINES)
967 move(line++>1?line-1:1, 0);
968 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
969 SCPY(buffer, logline[l % LOGLINES] + hoffset);
972 if (COLS < (int)strlen(buffer)) {
973 buffer[COLS-1] = '\0';
988 /* display name/time */
993 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
994 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
998 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
999 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1000 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1003 /* displeay head line */
1006 hline(ACS_HLINE, COLS);
1009 SPRINT(buffer, "Offset +%d", offset);
1015 SPRINT(buffer, "H-Offset +%d", hoffset);
1022 hline(ACS_HLINE, COLS);
1026 SPRINT(buffer, "-> %s", enter_string);
1029 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1035 if (lastlines!=LINES || lastcols!=COLS) {
1042 /* user input in enter mode */
1049 if (!enter_string[0])
1052 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1053 if (!!strncmp(enter_string, "interface", 10) &&
1054 !!strncmp(enter_string, "route", 6) &&
1055 !!strncmp(enter_string, "release ", 8) &&
1056 !!strncmp(enter_string, "block ", 6) &&
1057 !!strncmp(enter_string, "unblock ", 8) &&
1058 !!strncmp(enter_string, "load ", 5) &&
1059 !!strncmp(enter_string, "unload ", 7)) {
1060 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1061 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1062 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1063 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1064 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1065 SPRINT(logline[logcur++ % LOGLINES], "unblock/load <port> (unblock port for further calls, load if not loaded)");
1066 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1068 /* applend output to log window */
1069 SPRINT(buffer, "%s %s", argv[0], enter_string);
1070 fp = popen(buffer, "r");
1072 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1073 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1076 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1079 logline[logcur % LOGLINES][0] = '\0';
1080 enter_string[0] = '\0';
1083 if (ch>=32 && ch<=126) {
1084 SCCAT(enter_string, ch);
1090 if (ch==8 || ch==127) {
1091 if (enter_string[0])
1092 enter_string[strlen(enter_string)-1] = '\0';
1102 FD_ZERO(&select_rfds);
1103 FD_SET(0, &select_rfds);
1104 select_tv.tv_sec = 0;
1105 select_tv.tv_usec = 250000;
1106 select(1, &select_rfds, NULL, NULL, &select_tv);
1110 /* user input in normal mode */
1112 case 12: /* refresh */
1123 case 'i': /* toggle interface */
1125 if (show_interfaces > 3) show_interfaces = 0;
1128 case 'c': /* toggle calls */
1130 if (show_calls > 2) show_calls = 0;
1133 case 'l': /* toggle log */
1135 if (show_log > 1) show_log = 0;
1138 case '+': /* scroll down */
1142 case '-': /* scroll up */
1147 case '*': /* scroll right */
1151 case '/': /* scroll left */
1156 case 10: /* entermode */
1161 FD_ZERO(&select_rfds);
1162 FD_SET(0, &select_rfds);
1163 select_tv.tv_sec = 0;
1164 select_tv.tv_usec = 250000;
1165 select(1, &select_rfds, NULL, NULL, &select_tv);
1170 /* check for logfh */
1175 /* cleanup curses and exit */
1181 const char *admin_portinfo(int sock, int argc, char *argv[])
1183 struct admin_message msg,
1190 /* send state request command */
1191 memset(&msg, 0, sizeof(msg));
1192 msg.message = ADMIN_REQUEST_STATE;
1193 if (write(sock, &msg, sizeof(msg)) != sizeof(msg)) {
1195 return("Broken pipe while sending command.");
1198 /* receive response */
1199 if (read(sock, &msg, sizeof(msg)) != sizeof(msg)) {
1201 return("Broken pipe while receiving response.");
1204 if (msg.message != ADMIN_RESPONSE_STATE) {
1206 return("Response not valid. Expecting state response.");
1208 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
1209 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
1213 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off) {
1217 return("Broken pipe while receiving state infos.");
1219 if (len < num*(int)sizeof(struct admin_message)) {
1227 while(i < msg.u.s.interfaces) {
1228 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE) {
1231 return("Response not valid. Expecting interface information.");
1237 while(i < msg.u.s.remotes) {
1238 if (m[j].message != ADMIN_RESPONSE_S_REMOTE) {
1241 return("Response not valid. Expecting remote application information.");
1247 while(i < msg.u.s.joins) {
1248 if (m[j].message != ADMIN_RESPONSE_S_JOIN) {
1251 return("Response not valid. Expecting join information.");
1257 while(i < msg.u.s.epoints) {
1258 if (m[j].message != ADMIN_RESPONSE_S_EPOINT) {
1261 return("Response not valid. Expecting endpoint information.");
1267 while(i < msg.u.s.ports) {
1268 if (m[j].message != ADMIN_RESPONSE_S_PORT) {
1271 return("Response not valid. Expecting port information.");
1276 // now j is the number of message blocks
1278 /* output interfaces */
1280 ii = i + msg.u.s.interfaces;
1283 if (!!strcmp(argv[2], m[i].u.i.interface_name)) {
1288 printf("%s:\n", m[i].u.i.interface_name);
1289 if (m[i].u.i.portnum < 0)
1290 printf("\t port = unknown\n");
1292 printf("\t port = %d \"%s\"\n",m[i].u.i.portnum, m[i].u.i.portname);
1293 printf("\t extension = %s\n", (m[i].u.i.extension)?"yes":"no");
1294 if (m[i].u.i.block >= 2) {
1295 printf("\t status = not loaded\n");
1298 printf("\t status = blocked\n");
1300 printf("\t status = unblocked\n");
1301 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":"");
1302 printf("\t out-channel = %s\n", m[i].u.i.out_channel);
1303 printf("\t in-channel = %s\n", m[i].u.i.in_channel);
1304 if (m[i].u.i.l1link < 0)
1305 printf("\t l1 link = unknown\n");
1307 printf("\t l1 link = %s\n", (m[i].u.i.l1link)?"up":"down");
1308 if (m[i].u.i.ptp || !m[i].u.i.ntmode) {
1309 if (m[i].u.i.l2link < 0)
1310 printf("\t l2 link = unknown\n");
1312 printf("\t l2 link = %s\n", (m[i].u.i.l2link)?"up":"down");
1314 printf("\t usage = %d\n", m[i].u.i.use);
1327 * Send command and show error message.
1329 const char *admin_cmd(int sock, int mode, char *extension, char *number)
1331 static struct admin_message msg;
1333 /* send reload command */
1334 memset(&msg, 0, sizeof(msg));
1336 case MODE_INTERFACE:
1337 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1340 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1343 msg.message = ADMIN_REQUEST_CMD_DIAL;
1344 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1347 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1348 SCPY(msg.u.x.message, number);
1351 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1352 msg.u.x.portnum = atoi(number);
1356 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1357 msg.u.x.portnum = atoi(number);
1361 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1362 msg.u.x.portnum = atoi(number);
1367 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1368 return("Broken pipe while sending command.");
1370 /* receive response */
1371 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1372 return("Broken pipe while receiving response.");
1374 case MODE_INTERFACE:
1375 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1376 return("Response not valid.");
1379 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1380 return("Response not valid.");
1383 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1384 return("Response not valid.");
1387 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1388 return("Response not valid.");
1393 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1394 return("Response not valid.");
1398 /* process response */
1399 if (msg.u.x.error) {
1400 return(msg.u.x.message);
1402 printf("Command successfull.\n");
1410 #define GET_NOW() { \
1411 gettimeofday(&now_tv, &now_tz); \
1412 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1414 const char *admin_testcall(int sock, int argc, char *argv[])
1416 static struct admin_message msg;
1418 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1420 double timer = 0, now_d;
1421 unsigned int on = 1;
1422 struct timeval now_tv;
1423 struct timezone now_tz;
1425 printf("pid=%d\n", getpid()); fflush(stdout);
1428 if (!strcmp(argv[ar], "--setup-timeout")) {
1431 return("Missing setup timeout value.\n");
1432 stimeout = atoi(argv[ar]);
1435 if (!strcmp(argv[ar], "--proceeding-timeout")) {
1438 return("Missing proceeding timeout value.\n");
1439 ptimeout = atoi(argv[ar]);
1442 if (!strcmp(argv[ar], "--alerting-timeout")) {
1445 return("Missing alerting timeout value.\n");
1446 atimeout = atoi(argv[ar]);
1449 if (!strcmp(argv[ar], "--connect-timeout")) {
1452 return("Missing connect timeout value.\n");
1453 ctimeout = atoi(argv[ar]);
1460 /* send reload command */
1461 memset(&msg, 0, sizeof(msg));
1462 msg.message = ADMIN_CALL_SETUP;
1463 msg.u.call.present = 1;
1466 SCPY(msg.u.call.interface, argv[ar]);
1470 SCPY(msg.u.call.callerid, argv[ar]);
1474 SCPY(msg.u.call.dialing, argv[ar]);
1478 if (argv[ar][0] == 'r')
1479 msg.u.call.present = 0;
1482 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1483 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1484 msg.u.call.bc_info1 = 0;
1486 msg.u.call.exthlc = 0;
1488 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1490 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1493 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1494 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1498 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1499 if (msg.u.call.bc_info1 < 0)
1500 msg.u.call.bc_info1 = 0;
1502 msg.u.call.bc_info1 |= 0x80;
1506 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1507 if (msg.u.call.hlc < 0)
1510 msg.u.call.hlc |= 0x80;
1514 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1515 if (msg.u.call.exthlc < 0)
1516 msg.u.call.exthlc = 0;
1518 msg.u.call.exthlc |= 0x80;
1522 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1523 return("Broken pipe while sending command.");
1525 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1526 return("Failed to set socket into non-blocking IO.");
1530 timer = now_d + (double)stimeout;
1533 /* receive response */
1535 l = read(sock, &msg, sizeof(msg));
1537 if (errno == EWOULDBLOCK) {
1540 if (timer <= now_d) {
1541 printf("Timeout\n"); fflush(stdout);
1548 return("Broken pipe while receiving response.");
1550 if (l != sizeof(msg))
1551 return("Response has unexpected message size.");
1552 switch(msg.message) {
1553 case ADMIN_CALL_SETUP_ACK:
1554 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1557 case ADMIN_CALL_PROCEEDING:
1558 printf("PROCEEDING\n"); fflush(stdout);
1561 timer = now_d + (double)ptimeout;
1565 case ADMIN_CALL_ALERTING:
1566 printf("ALERTING\n"); fflush(stdout);
1569 timer = now_d + (double)atimeout;
1573 case ADMIN_CALL_CONNECT:
1574 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1577 timer = now_d + (double)ctimeout;
1581 case ADMIN_CALL_NOTIFY:
1582 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify_progress, msg.u.call.callerid); fflush(stdout);
1585 case ADMIN_CALL_PROGRESS:
1586 printf("PROGRESS\n progress=%d\n location=%d\n", msg.u.call.notify_progress, msg.u.call.location); fflush(stdout);
1589 case ADMIN_CALL_DISCONNECT:
1590 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);
1593 case ADMIN_CALL_RELEASE:
1594 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);
1598 return("Response not valid.");
1601 printf("Call released.\n"); fflush(stdout);
1609 const char *admin_trace(int sock, int argc, char *argv[])
1611 static struct admin_message msg;
1615 if (argc > 2) if (!strcasecmp(argv[2], "help")) {
1616 printf("Trace Help\n----------\n");
1617 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1618 printf("By default a complete trace is shown in detailed format.\n");
1619 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1620 printf("Use filter values to select specific trace messages.\n");
1621 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1622 printf("Filters:\n");
1623 printf(" category=<mask bits>\n");
1624 printf(" 0x01 = CH: channel object trace\n");
1625 printf(" 0x02 = EP: endpoint object trace\n");
1626 printf(" port=<mISDN port> select only given port for trace\n");
1627 printf(" interface=<interface name> select only given interface for trace\n");
1628 printf(" caller=<caller id> select only given caller id for trace\n");
1629 printf(" dialing=<number> select only given dialed number for trace\n");
1633 /* init trace request */
1634 memset(&msg, 0, sizeof(msg));
1635 msg.message = ADMIN_TRACE_REQUEST;
1636 msg.u.trace_req.detail = 3;
1637 msg.u.trace_req.port = -1;
1642 if (!strcasecmp(argv[i], "brief"))
1643 msg.u.trace_req.detail = 1;
1644 else if (!strcasecmp(argv[i], "short"))
1645 msg.u.trace_req.detail = 2;
1646 else if (!strncasecmp(argv[i], "category=", 9))
1647 msg.u.trace_req.category = atoi(argv[i]+9);
1648 else if (!strncasecmp(argv[i], "port=", 5))
1649 msg.u.trace_req.port = atoi(argv[i]+5);
1650 else if (!strncasecmp(argv[i], "interface=", 10))
1651 SCPY(msg.u.trace_req.interface, argv[i]+10);
1652 else if (!strncasecmp(argv[i], "caller=", 7))
1653 SCPY(msg.u.trace_req.caller, argv[i]+7);
1654 else if (!strncasecmp(argv[i], "dialing=", 8))
1655 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1656 else return("Invalid trace option, try 'trace help'.");
1661 /* send trace request */
1662 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1663 return("Broken pipe while sending trace request.");
1665 /* receive response */
1667 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1668 return("Broken pipe while receiving response.");
1670 if (msg.message != ADMIN_TRACE_RESPONSE)
1671 return("Response not valid.");
1673 printf("%s", msg.u.trace_rsp.text);
1681 int main(int argc, char *argv[])
1685 struct sockaddr_un sock_address;
1686 const char *ret = "invalid mode";
1687 char options_error[256];
1693 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1694 printf("state - View current states using graphical console output.\n");
1695 printf("portinfo - Get info of current ports.\n");
1696 printf("interface [<portname>] - Tell LCR to reload \"interface.conf\".\n");
1697 printf("route - Tell LCR to reload \"route.conf\".\n");
1698 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1699 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1700 printf("block <port> - Block given port.\n");
1701 printf("unblock/load <port> - Unblock given port.\n");
1702 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1703 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1704 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1705 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1706 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1707 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1708 printf(" -> Use 'trace help' to see filter description.\n");
1714 if (!(strcasecmp(argv[1],"state"))) {
1717 if (!(strcasecmp(argv[1],"portinfo"))) {
1718 mode = MODE_PORTINFO;
1720 if (!(strcasecmp(argv[1],"interface"))) {
1721 mode = MODE_INTERFACE;
1723 if (!(strcasecmp(argv[1],"route"))) {
1726 if (!(strcasecmp(argv[1],"dial"))) {
1731 if (!(strcasecmp(argv[1],"release"))) {
1734 mode = MODE_RELEASE;
1736 if (!(strcasecmp(argv[1],"unblock"))
1737 || !(strcasecmp(argv[1],"load"))) {
1740 mode = MODE_UNBLOCK;
1742 if (!(strcasecmp(argv[1],"block"))) {
1747 if (!(strcasecmp(argv[1],"unload"))) {
1752 if (!(strcasecmp(argv[1],"testcall"))) {
1755 mode = MODE_TESTCALL;
1757 if (!(strcasecmp(argv[1],"trace"))) {
1763 if (read_options(options_error) == 0) {
1769 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1770 fprintf(stderr, "Failed to create socket.\n");
1773 memset(&sock_address, 0, sizeof(sock_address));
1774 SPRINT(sock_address.sun_path, SOCKET_NAME, options.lock);
1775 sock_address.sun_family = PF_UNIX;
1776 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1778 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1785 ret = admin_state(sock, argv);
1789 ret = admin_portinfo(sock, argc, argv);
1792 case MODE_INTERFACE:
1794 ret = admin_cmd(sock, mode, NULL, NULL);
1798 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1805 ret = admin_cmd(sock, mode, NULL, argv[2]);
1809 ret = admin_testcall(sock, argc, argv);
1813 ret = admin_trace(sock, argc, argv);
1818 /* now we say good bye */
1820 // if (!strncasecmp(ret, "Broken Pipe", 11))
1822 printf("%s\n", ret);