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.partyline) {
392 addstr(" partyline=");
394 SPRINT(buffer, "%d\n", m[i].u.j.partyline);
397 if (m[i].u.j.remote[0]) {
401 SPRINT(buffer, "%s\n", m[i].u.j.remote);
404 /* find number of epoints */
405 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins;
406 jj = j + msg->u.s.epoints;
409 if (m[j].u.e.join == join)
413 /* loop all related endpoints */
414 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins;
415 jj = j + msg->u.s.epoints;
417 if (m[j].u.e.join == join) {
419 move(++line>1?line:1, 1);
425 move(line>1?line:1, 4);
426 if (line+2 >= LINES) break;
427 line = debug_epoint(msg, m, line, j, i?1:0);
428 if (line+2 >= LINES) break;
436 const char *admin_state(int sock, char *argv[])
438 struct admin_message msg,
442 int line, offset = 0, hoffset = 0;
451 char enter_string[128] = "", ch;
453 struct timeval select_tv;
455 /* flush logfile name */
462 /* send reload command */
463 memset(&msg, 0, sizeof(msg));
464 msg.message = ADMIN_REQUEST_STATE;
465 // printf("sizeof=%d\n",sizeof(msg));fflush(stdout);
466 if (write(sock, &msg, sizeof(msg)) != sizeof(msg)) {
468 return("Broken pipe while sending command.");
471 /* receive response */
472 if (read(sock, &msg, sizeof(msg)) != sizeof(msg)) {
474 return("Broken pipe while receiving response.");
477 if (msg.message != ADMIN_RESPONSE_STATE) {
479 return("Response not valid. Expecting state response.");
481 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
482 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
486 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off) {
489 // fprintf(stderr, "got=%d expected=%d\n", i, num*sizeof(struct admin_message));
491 return("Broken pipe while receiving state infos.");
493 if (len < num*(int)sizeof(struct admin_message)) {
501 // fprintf("getting =%d interfaces\n", msg.u.s.interfaces);
502 while(i < msg.u.s.interfaces) {
503 // fprintf(stderr, "j=%d message=%d\n", j, m[j].message);
504 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE) {
507 return("Response not valid. Expecting interface information.");
513 while(i < msg.u.s.remotes) {
514 if (m[j].message != ADMIN_RESPONSE_S_REMOTE) {
517 return("Response not valid. Expecting remote application information.");
523 while(i < msg.u.s.joins) {
524 if (m[j].message != ADMIN_RESPONSE_S_JOIN) {
527 return("Response not valid. Expecting join information.");
533 while(i < msg.u.s.epoints) {
534 if (m[j].message != ADMIN_RESPONSE_S_EPOINT) {
537 return("Response not valid. Expecting endpoint information.");
543 while(i < msg.u.s.ports) {
544 if (m[j].message != ADMIN_RESPONSE_S_PORT) {
547 return("Response not valid. Expecting port information.");
552 // now j is the number of message blocks
560 if (!!strcmp(logfile, msg.u.s.logfile)) {
561 SCPY(logfile, msg.u.s.logfile);
568 logline[i][1] = '\0';
572 logfh = open(logfile, O_RDONLY|O_NONBLOCK);
574 /* seek at the end -8000 chars */
575 lseek(logfh, -8000, SEEK_END);
576 /* if not at the beginning, read until endofline */
577 logline[logcur % LOGLINES][0] = '\0';
578 l = read(logfh, logline[logcur % LOGLINES], sizeof(logline[logcur % LOGLINES])-1);
580 /* read first line and skip junk */
581 logline[logcur % LOGLINES][l] = '\0';
582 if ((p = strchr(logline[logcur % LOGLINES],'\n'))) {
584 SCPY(logline[logcur % LOGLINES], p+1);
585 SCPY(logline[(logcur-1) % LOGLINES], "...");
595 ll = strlen(logline[logcur % LOGLINES]);
596 l = read(logfh, logline[logcur % LOGLINES]+ll, sizeof(logline[logcur % LOGLINES])-ll-1);
599 logline[logcur % LOGLINES][ll+l] = '\0';
601 /* put data to lines */
602 while ((p = strchr(logline[logcur % LOGLINES],'\n'))) {
605 SCPY(logline[logcur % LOGLINES], p+1);
607 /* if line is full without return, go next line */
608 if (strlen(logline[logcur % LOGLINES]) == sizeof(logline[logcur % LOGLINES])-1) {
610 logline[logcur % LOGLINES][0] = '\0';
615 /* display interfaces */
616 if (show_interfaces > 0) {
619 ii = i + msg.u.s.interfaces;
621 /* show interface summary */
622 move(++line>1?line:1, 0);
624 if (m[i].u.i.portnum == -100) {
625 SPRINT(buffer, "%s %s", m[i].u.i.interface_name, (m[i].u.i.extension)?" exten":"");
627 } else if (m[i].u.i.block >= 2) {
628 if (m[i].u.i.portnum < 0)
629 SPRINT(buffer, "%s (port ?: %s)%s", m[i].u.i.interface_name, m[i].u.i.portname, (m[i].u.i.extension)?" exten":"");
631 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":"");
634 addstr(" not loaded");
636 SPRINT(buffer, "%s", m[i].u.i.interface_name);
639 SPRINT(buffer, "(port %d: %s)", m[i].u.i.portnum, m[i].u.i.portname);
642 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":"");
648 SPRINT(buffer, " use:%d", m[i].u.i.use);
650 if (m[i].u.i.ptp || !m[i].u.i.ntmode) {
651 color((m[i].u.i.l2link > 0)?green:red);
652 if (m[i].u.i.l2link < 0)
655 addstr((m[i].u.i.l2link)?" L2 UP":" L2 down");
661 if (m[i].u.i.l2mask[j>>3] & (1 << (j&7))) {
662 SPRINT(buffer, "%s%d", k?",":" TEI(", j);
671 color((m[i].u.i.l1link > 0)?green:blue);
672 if (m[i].u.i.l1link < 0)
675 addstr((m[i].u.i.l1link)?" L1 UP":" L1 down");
688 if (m[i].u.i.slip_tx || m[i].u.i.slip_rx) {
690 SPRINT(buffer, " SLIP(tx:%d rx:%d)", m[i].u.i.slip_tx, m[i].u.i.slip_rx);
693 if (m[i].u.i.block) {
697 if (line+2 >= LINES) goto end;
699 if (show_interfaces > 1) {
702 jj = m[i].u.i.channels;
704 /* show all channels */
705 if (show_interfaces>2 || m[i].u.i.busy[j]>0) {
707 /* show left side / right side */
708 if ((k & 1) && (COLS > 70)) {
709 move(line>1?line:1,4+((COLS-4)/2));
711 move(++line>1?line:1, 1);
718 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
720 SPRINT(buffer,"B%2d: ", j+1);
721 if (m[i].u.i.mode[j] == B_MODE_HDLC)
722 SCAT(buffer,"HDLC ");
724 switch(m[i].u.i.busy[j]) {
726 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block) {
734 case B_STATE_ACTIVATING:
742 case B_STATE_DEACTIVATING:
746 case B_STATE_EXPORTING:
754 case B_STATE_IMPORTING:
759 if (m[i].u.i.port[j]) {
760 /* search for port */
761 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
762 ll = l+msg.u.s.ports;
764 if (m[l].u.p.serial == m[i].u.i.port[j]) {
765 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
771 if (line+2 >= LINES) {
774 move(line>1?line:1, 1);
784 move(line>1?line:1, 1);
787 if (line+2 >= LINES) goto end;
788 /* show summary if no channels were shown */
789 if (show_interfaces<2 && ltee==0) {
791 move(++line>1?line:1, 1);
794 if (m[i].u.i.l2link && m[i].u.i.block==0) {
796 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
799 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
803 if (line+2 >= LINES) goto end;
809 i = msg.u.s.interfaces;
810 ii = i + msg.u.s.remotes;
812 /* show remote summary */
813 move(++line>1?line:1, 0);
815 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
821 if (line+2 >= LINES) goto end;
823 /* display calls (brief) */
824 if (show_calls == 1) {
826 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
827 ii = i+msg.u.s.epoints;
829 /* for each endpoint... */
830 if (!m[i].u.e.join) {
831 move(++line>1?line:1, 0);
833 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
836 if (m[i].u.e.terminal[0]) {
839 addstr(m[i].u.e.terminal);
843 SPRINT(buffer, " %s", m[i].u.e.callerid);
848 SPRINT(buffer, "%s", m[i].u.e.dialing);
850 if (m[i].u.e.action[0]) {
854 addstr(m[i].u.e.action);
856 if (line+2 >= LINES) goto end;
861 j = msg.u.s.interfaces+msg.u.s.remotes;
862 jj = j+msg.u.s.joins;
864 /* for each call... */
865 move(++line>1?line:1, 0);
867 SPRINT(buffer, "(%d):", m[j].u.j.serial);
869 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
870 ii = i+msg.u.s.epoints;
872 /* for each endpoint... */
873 if (m[i].u.e.join == m[j].u.j.serial) {
875 SPRINT(buffer, " (%d)", m[i].u.e.serial);
878 if (m[i].u.e.terminal[0]) {
881 addstr(m[i].u.e.terminal);
885 SPRINT(buffer, "-%s", m[i].u.e.callerid);
890 SPRINT(buffer, "%s", m[i].u.e.dialing);
896 if (line+2 >= LINES) goto end;
901 if (line+2 >= LINES) goto end;
903 /* display calls (structurd) */
904 if (show_calls == 2) {
905 /* show all ports with no epoint */
907 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
908 ii = i+msg.u.s.ports;
910 if (!m[i].u.p.epoint) {
911 move(++line>1?line:1, 8);
912 if (line+2 >= LINES) goto end;
913 line = debug_port(&msg, m, line, i, 0);
914 if (line+2 >= LINES) goto end;
921 if (line+2 >= LINES) goto end;
923 /* show all epoints with no call */
925 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
926 ii = i+msg.u.s.epoints;
928 if (!m[i].u.e.join) {
929 move(++line>1?line:1, 4);
930 if (line+2 >= LINES) goto end;
931 line = debug_epoint(&msg, m, line, i, 0);
932 if (line+2 >= LINES) goto end;
939 if (line+2 >= LINES) goto end;
943 i = msg.u.s.interfaces+msg.u.s.remotes;
944 ii = i+msg.u.s.joins;
946 move(++line>1?line:1, 0);
947 if (line+2 >= LINES) goto end;
948 line = debug_join(&msg, m, line, i);
949 if (line+2 >= LINES) goto end;
955 if (line+2 >= LINES) goto end;
961 if (line+2 < LINES) {
962 move(line++>1?line-1:1, 0);
964 hline(ACS_HLINE, COLS);
967 l = logcur-(LINES-line-2);
969 if (ll-l >= LOGLINES)
972 move(line++>1?line-1:1, 0);
973 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
974 SCPY(buffer, logline[l % LOGLINES] + hoffset);
977 if (COLS < (int)strlen(buffer)) {
978 buffer[COLS-1] = '\0';
993 /* display name/time */
998 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
999 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
1003 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1004 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1005 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1008 /* displeay head line */
1011 hline(ACS_HLINE, COLS);
1014 SPRINT(buffer, "Offset +%d", offset);
1020 SPRINT(buffer, "H-Offset +%d", hoffset);
1027 hline(ACS_HLINE, COLS);
1031 SPRINT(buffer, "-> %s", enter_string);
1034 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1040 if (lastlines!=LINES || lastcols!=COLS) {
1047 /* user input in enter mode */
1054 if (!enter_string[0])
1057 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1058 if (!!strncmp(enter_string, "interface", 10) &&
1059 !!strncmp(enter_string, "route", 6) &&
1060 !!strncmp(enter_string, "release ", 8) &&
1061 !!strncmp(enter_string, "block ", 6) &&
1062 !!strncmp(enter_string, "unblock ", 8) &&
1063 !!strncmp(enter_string, "load ", 5) &&
1064 !!strncmp(enter_string, "unload ", 7)) {
1065 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1066 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1067 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1068 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1069 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1070 SPRINT(logline[logcur++ % LOGLINES], "unblock/load <port> (unblock port for further calls, load if not loaded)");
1071 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1073 /* applend output to log window */
1074 SPRINT(buffer, "%s %s", argv[0], enter_string);
1075 fp = popen(buffer, "r");
1077 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1078 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1081 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1084 logline[logcur % LOGLINES][0] = '\0';
1085 enter_string[0] = '\0';
1088 if (ch>=32 && ch<=126) {
1089 SCCAT(enter_string, ch);
1095 if (ch==8 || ch==127) {
1096 if (enter_string[0])
1097 enter_string[strlen(enter_string)-1] = '\0';
1107 FD_ZERO(&select_rfds);
1108 FD_SET(0, &select_rfds);
1109 select_tv.tv_sec = 0;
1110 select_tv.tv_usec = 250000;
1111 select(1, &select_rfds, NULL, NULL, &select_tv);
1115 /* user input in normal mode */
1117 case 12: /* refresh */
1128 case 'i': /* toggle interface */
1130 if (show_interfaces > 3) show_interfaces = 0;
1133 case 'c': /* toggle calls */
1135 if (show_calls > 2) show_calls = 0;
1138 case 'l': /* toggle log */
1140 if (show_log > 1) show_log = 0;
1143 case '+': /* scroll down */
1147 case '-': /* scroll up */
1152 case '*': /* scroll right */
1156 case '/': /* scroll left */
1161 case 10: /* entermode */
1166 FD_ZERO(&select_rfds);
1167 FD_SET(0, &select_rfds);
1168 select_tv.tv_sec = 0;
1169 select_tv.tv_usec = 250000;
1170 select(1, &select_rfds, NULL, NULL, &select_tv);
1175 /* check for logfh */
1180 /* cleanup curses and exit */
1186 const char *admin_portinfo(int sock, int argc, char *argv[])
1188 struct admin_message msg,
1195 /* send state request command */
1196 memset(&msg, 0, sizeof(msg));
1197 msg.message = ADMIN_REQUEST_STATE;
1198 if (write(sock, &msg, sizeof(msg)) != sizeof(msg)) {
1200 return("Broken pipe while sending command.");
1203 /* receive response */
1204 if (read(sock, &msg, sizeof(msg)) != sizeof(msg)) {
1206 return("Broken pipe while receiving response.");
1209 if (msg.message != ADMIN_RESPONSE_STATE) {
1211 return("Response not valid. Expecting state response.");
1213 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
1214 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
1218 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off) {
1222 return("Broken pipe while receiving state infos.");
1224 if (len < num*(int)sizeof(struct admin_message)) {
1232 while(i < msg.u.s.interfaces) {
1233 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE) {
1236 return("Response not valid. Expecting interface information.");
1242 while(i < msg.u.s.remotes) {
1243 if (m[j].message != ADMIN_RESPONSE_S_REMOTE) {
1246 return("Response not valid. Expecting remote application information.");
1252 while(i < msg.u.s.joins) {
1253 if (m[j].message != ADMIN_RESPONSE_S_JOIN) {
1256 return("Response not valid. Expecting join information.");
1262 while(i < msg.u.s.epoints) {
1263 if (m[j].message != ADMIN_RESPONSE_S_EPOINT) {
1266 return("Response not valid. Expecting endpoint information.");
1272 while(i < msg.u.s.ports) {
1273 if (m[j].message != ADMIN_RESPONSE_S_PORT) {
1276 return("Response not valid. Expecting port information.");
1281 // now j is the number of message blocks
1283 /* output interfaces */
1285 ii = i + msg.u.s.interfaces;
1288 if (!!strcmp(argv[2], m[i].u.i.interface_name)) {
1293 printf("%s:\n", m[i].u.i.interface_name);
1294 if (m[i].u.i.portnum < 0)
1295 printf("\t port = unknown\n");
1297 printf("\t port = %d \"%s\"\n",m[i].u.i.portnum, m[i].u.i.portname);
1298 printf("\t extension = %s\n", (m[i].u.i.extension)?"yes":"no");
1299 if (m[i].u.i.block >= 2) {
1300 printf("\t status = not loaded\n");
1303 printf("\t status = blocked\n");
1305 printf("\t status = unblocked\n");
1306 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":"");
1307 printf("\t out-channel = %s\n", m[i].u.i.out_channel);
1308 printf("\t in-channel = %s\n", m[i].u.i.in_channel);
1309 if (m[i].u.i.l1link < 0)
1310 printf("\t l1 link = unknown\n");
1312 printf("\t l1 link = %s\n", (m[i].u.i.l1link)?"up":"down");
1313 if (m[i].u.i.ptp || !m[i].u.i.ntmode) {
1314 if (m[i].u.i.l2link < 0)
1315 printf("\t l2 link = unknown\n");
1317 printf("\t l2 link = %s\n", (m[i].u.i.l2link)?"up":"down");
1319 printf("\t usage = %d\n", m[i].u.i.use);
1332 * Send command and show error message.
1334 const char *admin_cmd(int sock, int mode, char *extension, char *number)
1336 static struct admin_message msg;
1338 /* send reload command */
1339 memset(&msg, 0, sizeof(msg));
1341 case MODE_INTERFACE:
1342 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1345 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1348 msg.message = ADMIN_REQUEST_CMD_DIAL;
1349 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1352 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1353 SCPY(msg.u.x.message, 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);
1366 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1367 msg.u.x.portnum = atoi(number);
1372 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1373 return("Broken pipe while sending command.");
1375 /* receive response */
1376 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1377 return("Broken pipe while receiving response.");
1379 case MODE_INTERFACE:
1380 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1381 return("Response not valid.");
1384 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1385 return("Response not valid.");
1388 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1389 return("Response not valid.");
1392 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1393 return("Response not valid.");
1398 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1399 return("Response not valid.");
1403 /* process response */
1404 if (msg.u.x.error) {
1405 return(msg.u.x.message);
1407 printf("Command successfull.\n");
1415 #define GET_NOW() { \
1416 gettimeofday(&now_tv, &now_tz); \
1417 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1419 const char *admin_testcall(int sock, int argc, char *argv[])
1421 static struct admin_message msg;
1423 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1425 double timer = 0, now_d;
1426 unsigned int on = 1;
1427 struct timeval now_tv;
1428 struct timezone now_tz;
1430 printf("pid=%d\n", getpid()); fflush(stdout);
1433 if (!strcmp(argv[ar], "--setup-timeout")) {
1436 return("Missing setup timeout value.\n");
1437 stimeout = atoi(argv[ar]);
1440 if (!strcmp(argv[ar], "--proceeding-timeout")) {
1443 return("Missing proceeding timeout value.\n");
1444 ptimeout = atoi(argv[ar]);
1447 if (!strcmp(argv[ar], "--alerting-timeout")) {
1450 return("Missing alerting timeout value.\n");
1451 atimeout = atoi(argv[ar]);
1454 if (!strcmp(argv[ar], "--connect-timeout")) {
1457 return("Missing connect timeout value.\n");
1458 ctimeout = atoi(argv[ar]);
1465 /* send reload command */
1466 memset(&msg, 0, sizeof(msg));
1467 msg.message = ADMIN_CALL_SETUP;
1468 msg.u.call.present = 1;
1471 SCPY(msg.u.call.interface, argv[ar]);
1475 SCPY(msg.u.call.callerid, argv[ar]);
1479 SCPY(msg.u.call.dialing, argv[ar]);
1483 if (argv[ar][0] == 'r')
1484 msg.u.call.present = 0;
1487 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1488 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1489 msg.u.call.bc_info1 = 0;
1491 msg.u.call.exthlc = 0;
1493 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1495 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1498 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1499 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1503 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1504 if (msg.u.call.bc_info1 < 0)
1505 msg.u.call.bc_info1 = 0;
1507 msg.u.call.bc_info1 |= 0x80;
1511 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1512 if (msg.u.call.hlc < 0)
1515 msg.u.call.hlc |= 0x80;
1519 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1520 if (msg.u.call.exthlc < 0)
1521 msg.u.call.exthlc = 0;
1523 msg.u.call.exthlc |= 0x80;
1527 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1528 return("Broken pipe while sending command.");
1530 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1531 return("Failed to set socket into non-blocking IO.");
1535 timer = now_d + (double)stimeout;
1538 /* receive response */
1540 l = read(sock, &msg, sizeof(msg));
1542 if (errno == EWOULDBLOCK) {
1545 if (timer <= now_d) {
1546 printf("Timeout\n"); fflush(stdout);
1553 return("Broken pipe while receiving response.");
1555 if (l != sizeof(msg))
1556 return("Response has unexpected message size.");
1557 switch(msg.message) {
1558 case ADMIN_CALL_SETUP_ACK:
1559 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1562 case ADMIN_CALL_PROCEEDING:
1563 printf("PROCEEDING\n"); fflush(stdout);
1566 timer = now_d + (double)ptimeout;
1570 case ADMIN_CALL_ALERTING:
1571 printf("ALERTING\n"); fflush(stdout);
1574 timer = now_d + (double)atimeout;
1578 case ADMIN_CALL_CONNECT:
1579 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1582 timer = now_d + (double)ctimeout;
1586 case ADMIN_CALL_NOTIFY:
1587 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify_progress, msg.u.call.callerid); fflush(stdout);
1590 case ADMIN_CALL_PROGRESS:
1591 printf("PROGRESS\n progress=%d\n location=%d\n", msg.u.call.notify_progress, msg.u.call.location); fflush(stdout);
1594 case ADMIN_CALL_DISCONNECT:
1595 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);
1598 case ADMIN_CALL_RELEASE:
1599 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);
1603 return("Response not valid.");
1606 printf("Call released.\n"); fflush(stdout);
1614 const char *admin_trace(int sock, int argc, char *argv[])
1616 static struct admin_message msg;
1620 if (argc > 2) if (!strcasecmp(argv[2], "help")) {
1621 printf("Trace Help\n----------\n");
1622 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1623 printf("By default a complete trace is shown in detailed format.\n");
1624 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1625 printf("Use filter values to select specific trace messages.\n");
1626 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1627 printf("Filters:\n");
1628 printf(" category=<mask bits>\n");
1629 printf(" 0x01 = CH: channel object trace\n");
1630 printf(" 0x02 = EP: endpoint object trace\n");
1631 printf(" port=<mISDN port> select only given port for trace\n");
1632 printf(" interface=<interface name> select only given interface for trace\n");
1633 printf(" caller=<caller id> select only given caller id for trace\n");
1634 printf(" dialing=<number> select only given dialed number for trace\n");
1638 /* init trace request */
1639 memset(&msg, 0, sizeof(msg));
1640 msg.message = ADMIN_TRACE_REQUEST;
1641 msg.u.trace_req.detail = 3;
1642 msg.u.trace_req.port = -1;
1647 if (!strcasecmp(argv[i], "brief"))
1648 msg.u.trace_req.detail = 1;
1649 else if (!strcasecmp(argv[i], "short"))
1650 msg.u.trace_req.detail = 2;
1651 else if (!strncasecmp(argv[i], "category=", 9))
1652 msg.u.trace_req.category = atoi(argv[i]+9);
1653 else if (!strncasecmp(argv[i], "port=", 5))
1654 msg.u.trace_req.port = atoi(argv[i]+5);
1655 else if (!strncasecmp(argv[i], "interface=", 10))
1656 SCPY(msg.u.trace_req.interface, argv[i]+10);
1657 else if (!strncasecmp(argv[i], "caller=", 7))
1658 SCPY(msg.u.trace_req.caller, argv[i]+7);
1659 else if (!strncasecmp(argv[i], "dialing=", 8))
1660 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1661 else return("Invalid trace option, try 'trace help'.");
1666 /* send trace request */
1667 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1668 return("Broken pipe while sending trace request.");
1670 /* receive response */
1672 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1673 return("Broken pipe while receiving response.");
1675 if (msg.message != ADMIN_TRACE_RESPONSE)
1676 return("Response not valid.");
1678 printf("%s", msg.u.trace_rsp.text);
1686 int main(int argc, char *argv[])
1690 struct sockaddr_un sock_address;
1691 const char *ret = "invalid mode";
1692 char options_error[256];
1698 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1699 printf("state - View current states using graphical console output.\n");
1700 printf("portinfo - Get info of current ports.\n");
1701 printf("interface [<portname>] - Tell LCR to reload \"interface.conf\".\n");
1702 printf("route - Tell LCR to reload \"route.conf\".\n");
1703 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1704 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1705 printf("block <port> - Block given port.\n");
1706 printf("unblock/load <port> - Unblock given port.\n");
1707 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1708 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1709 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1710 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1711 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1712 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1713 printf(" -> Use 'trace help' to see filter description.\n");
1719 if (!(strcasecmp(argv[1],"state"))) {
1722 if (!(strcasecmp(argv[1],"portinfo"))) {
1723 mode = MODE_PORTINFO;
1725 if (!(strcasecmp(argv[1],"interface"))) {
1726 mode = MODE_INTERFACE;
1728 if (!(strcasecmp(argv[1],"route"))) {
1731 if (!(strcasecmp(argv[1],"dial"))) {
1736 if (!(strcasecmp(argv[1],"release"))) {
1739 mode = MODE_RELEASE;
1741 if (!(strcasecmp(argv[1],"unblock"))
1742 || !(strcasecmp(argv[1],"load"))) {
1745 mode = MODE_UNBLOCK;
1747 if (!(strcasecmp(argv[1],"block"))) {
1752 if (!(strcasecmp(argv[1],"unload"))) {
1757 if (!(strcasecmp(argv[1],"testcall"))) {
1760 mode = MODE_TESTCALL;
1762 if (!(strcasecmp(argv[1],"trace"))) {
1768 if (read_options(options_error) == 0) {
1774 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1775 fprintf(stderr, "Failed to create socket.\n");
1778 memset(&sock_address, 0, sizeof(sock_address));
1779 SPRINT(sock_address.sun_path, SOCKET_NAME, options.lock);
1780 sock_address.sun_family = PF_UNIX;
1781 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1783 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1790 ret = admin_state(sock, argv);
1794 ret = admin_portinfo(sock, argc, argv);
1797 case MODE_INTERFACE:
1799 ret = admin_cmd(sock, mode, NULL, NULL);
1803 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1810 ret = admin_cmd(sock, mode, NULL, argv[2]);
1814 ret = admin_testcall(sock, argc, argv);
1818 ret = admin_trace(sock, argc, argv);
1823 /* now we say good bye */
1825 // if (!strncasecmp(ret, "Broken Pipe", 11))
1827 printf("%s\n", ret);