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:
747 if (m[i].u.i.port[j]) {
748 /* search for port */
749 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
750 ll = l+msg.u.s.ports;
752 if (m[l].u.p.serial == m[i].u.i.port[j]) {
753 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
759 if (line+2 >= LINES) {
762 move(line>1?line:1, 1);
772 move(line>1?line:1, 1);
775 if (line+2 >= LINES) goto end;
776 /* show summary if no channels were shown */
777 if (show_interfaces<2 && ltee==0) {
779 move(++line>1?line:1, 1);
782 if (m[i].u.i.l2link && m[i].u.i.block==0) {
784 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
787 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
791 if (line+2 >= LINES) goto end;
797 i = msg.u.s.interfaces;
798 ii = i + msg.u.s.remotes;
800 /* show remote summary */
801 move(++line>1?line:1, 0);
803 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
809 if (line+2 >= LINES) goto end;
811 /* display calls (brief) */
812 if (show_calls == 1) {
814 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
815 ii = i+msg.u.s.epoints;
817 /* for each endpoint... */
818 if (!m[i].u.e.join) {
819 move(++line>1?line:1, 0);
821 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
824 if (m[i].u.e.terminal[0]) {
827 addstr(m[i].u.e.terminal);
831 SPRINT(buffer, " %s", m[i].u.e.callerid);
836 SPRINT(buffer, "%s", m[i].u.e.dialing);
838 if (m[i].u.e.action[0]) {
842 addstr(m[i].u.e.action);
844 if (line+2 >= LINES) goto end;
849 j = msg.u.s.interfaces+msg.u.s.remotes;
850 jj = j+msg.u.s.joins;
852 /* for each call... */
853 move(++line>1?line:1, 0);
855 SPRINT(buffer, "(%d):", m[j].u.j.serial);
857 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
858 ii = i+msg.u.s.epoints;
860 /* for each endpoint... */
861 if (m[i].u.e.join == m[j].u.j.serial) {
863 SPRINT(buffer, " (%d)", m[i].u.e.serial);
866 if (m[i].u.e.terminal[0]) {
869 addstr(m[i].u.e.terminal);
873 SPRINT(buffer, "-%s", m[i].u.e.callerid);
878 SPRINT(buffer, "%s", m[i].u.e.dialing);
884 if (line+2 >= LINES) goto end;
889 if (line+2 >= LINES) goto end;
891 /* display calls (structurd) */
892 if (show_calls == 2) {
893 /* show all ports with no epoint */
895 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
896 ii = i+msg.u.s.ports;
898 if (!m[i].u.p.epoint) {
899 move(++line>1?line:1, 8);
900 if (line+2 >= LINES) goto end;
901 line = debug_port(&msg, m, line, i, 0);
902 if (line+2 >= LINES) goto end;
909 if (line+2 >= LINES) goto end;
911 /* show all epoints with no call */
913 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
914 ii = i+msg.u.s.epoints;
916 if (!m[i].u.e.join) {
917 move(++line>1?line:1, 4);
918 if (line+2 >= LINES) goto end;
919 line = debug_epoint(&msg, m, line, i, 0);
920 if (line+2 >= LINES) goto end;
927 if (line+2 >= LINES) goto end;
931 i = msg.u.s.interfaces+msg.u.s.remotes;
932 ii = i+msg.u.s.joins;
934 move(++line>1?line:1, 0);
935 if (line+2 >= LINES) goto end;
936 line = debug_join(&msg, m, line, i);
937 if (line+2 >= LINES) goto end;
943 if (line+2 >= LINES) goto end;
949 if (line+2 < LINES) {
950 move(line++>1?line-1:1, 0);
952 hline(ACS_HLINE, COLS);
955 l = logcur-(LINES-line-2);
957 if (ll-l >= LOGLINES)
960 move(line++>1?line-1:1, 0);
961 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
962 SCPY(buffer, logline[l % LOGLINES] + hoffset);
965 if (COLS < (int)strlen(buffer)) {
966 buffer[COLS-1] = '\0';
981 /* display name/time */
986 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
987 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
991 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
992 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
993 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
996 /* displeay head line */
999 hline(ACS_HLINE, COLS);
1002 SPRINT(buffer, "Offset +%d", offset);
1008 SPRINT(buffer, "H-Offset +%d", hoffset);
1015 hline(ACS_HLINE, COLS);
1019 SPRINT(buffer, "-> %s", enter_string);
1022 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1028 if (lastlines!=LINES || lastcols!=COLS) {
1035 /* user input in enter mode */
1042 if (!enter_string[0])
1045 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1046 if (!!strncmp(enter_string, "interface", 10) &&
1047 !!strncmp(enter_string, "route", 6) &&
1048 !!strncmp(enter_string, "release ", 8) &&
1049 !!strncmp(enter_string, "block ", 6) &&
1050 !!strncmp(enter_string, "unblock ", 8) &&
1051 !!strncmp(enter_string, "load ", 5) &&
1052 !!strncmp(enter_string, "unload ", 7)) {
1053 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1054 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1055 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1056 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1057 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1058 SPRINT(logline[logcur++ % LOGLINES], "unblock/load <port> (unblock port for further calls, load if not loaded)");
1059 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1061 /* applend output to log window */
1062 SPRINT(buffer, "%s %s", argv[0], enter_string);
1063 fp = popen(buffer, "r");
1065 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1066 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1069 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1072 logline[logcur % LOGLINES][0] = '\0';
1073 enter_string[0] = '\0';
1076 if (ch>=32 && ch<=126) {
1077 SCCAT(enter_string, ch);
1083 if (ch==8 || ch==127) {
1084 if (enter_string[0])
1085 enter_string[strlen(enter_string)-1] = '\0';
1095 FD_ZERO(&select_rfds);
1096 FD_SET(0, &select_rfds);
1097 select_tv.tv_sec = 0;
1098 select_tv.tv_usec = 250000;
1099 select(1, &select_rfds, NULL, NULL, &select_tv);
1103 /* user input in normal mode */
1105 case 12: /* refresh */
1116 case 'i': /* toggle interface */
1118 if (show_interfaces > 3) show_interfaces = 0;
1121 case 'c': /* toggle calls */
1123 if (show_calls > 2) show_calls = 0;
1126 case 'l': /* toggle log */
1128 if (show_log > 1) show_log = 0;
1131 case '+': /* scroll down */
1135 case '-': /* scroll up */
1140 case '*': /* scroll right */
1144 case '/': /* scroll left */
1149 case 10: /* entermode */
1154 FD_ZERO(&select_rfds);
1155 FD_SET(0, &select_rfds);
1156 select_tv.tv_sec = 0;
1157 select_tv.tv_usec = 250000;
1158 select(1, &select_rfds, NULL, NULL, &select_tv);
1163 /* check for logfh */
1168 /* cleanup curses and exit */
1174 const char *admin_portinfo(int sock, int argc, char *argv[])
1176 struct admin_message msg,
1183 /* send state request command */
1184 memset(&msg, 0, sizeof(msg));
1185 msg.message = ADMIN_REQUEST_STATE;
1186 if (write(sock, &msg, sizeof(msg)) != sizeof(msg)) {
1188 return("Broken pipe while sending command.");
1191 /* receive response */
1192 if (read(sock, &msg, sizeof(msg)) != sizeof(msg)) {
1194 return("Broken pipe while receiving response.");
1197 if (msg.message != ADMIN_RESPONSE_STATE) {
1199 return("Response not valid. Expecting state response.");
1201 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
1202 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
1206 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off) {
1210 return("Broken pipe while receiving state infos.");
1212 if (len < num*(int)sizeof(struct admin_message)) {
1220 while(i < msg.u.s.interfaces) {
1221 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE) {
1224 return("Response not valid. Expecting interface information.");
1230 while(i < msg.u.s.remotes) {
1231 if (m[j].message != ADMIN_RESPONSE_S_REMOTE) {
1234 return("Response not valid. Expecting remote application information.");
1240 while(i < msg.u.s.joins) {
1241 if (m[j].message != ADMIN_RESPONSE_S_JOIN) {
1244 return("Response not valid. Expecting join information.");
1250 while(i < msg.u.s.epoints) {
1251 if (m[j].message != ADMIN_RESPONSE_S_EPOINT) {
1254 return("Response not valid. Expecting endpoint information.");
1260 while(i < msg.u.s.ports) {
1261 if (m[j].message != ADMIN_RESPONSE_S_PORT) {
1264 return("Response not valid. Expecting port information.");
1269 // now j is the number of message blocks
1271 /* output interfaces */
1273 ii = i + msg.u.s.interfaces;
1276 if (!!strcmp(argv[2], m[i].u.i.interface_name)) {
1281 printf("%s:\n", m[i].u.i.interface_name);
1282 if (m[i].u.i.portnum < 0)
1283 printf("\t port = unknown\n");
1285 printf("\t port = %d \"%s\"\n",m[i].u.i.portnum, m[i].u.i.portname);
1286 printf("\t extension = %s\n", (m[i].u.i.extension)?"yes":"no");
1287 if (m[i].u.i.block >= 2) {
1288 printf("\t status = not loaded\n");
1291 printf("\t status = blocked\n");
1293 printf("\t status = unblocked\n");
1294 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":"");
1295 printf("\t out-channel = %s\n", m[i].u.i.out_channel);
1296 printf("\t in-channel = %s\n", m[i].u.i.in_channel);
1297 if (m[i].u.i.l1link < 0)
1298 printf("\t l1 link = unknown\n");
1300 printf("\t l1 link = %s\n", (m[i].u.i.l1link)?"up":"down");
1301 if (m[i].u.i.ptp || !m[i].u.i.ntmode) {
1302 if (m[i].u.i.l2link < 0)
1303 printf("\t l2 link = unknown\n");
1305 printf("\t l2 link = %s\n", (m[i].u.i.l2link)?"up":"down");
1307 printf("\t usage = %d\n", m[i].u.i.use);
1320 * Send command and show error message.
1322 const char *admin_cmd(int sock, int mode, char *extension, char *number)
1324 static struct admin_message msg;
1326 /* send reload command */
1327 memset(&msg, 0, sizeof(msg));
1329 case MODE_INTERFACE:
1330 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1333 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1336 msg.message = ADMIN_REQUEST_CMD_DIAL;
1337 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1340 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1341 SCPY(msg.u.x.message, number);
1344 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1345 msg.u.x.portnum = atoi(number);
1349 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1350 msg.u.x.portnum = atoi(number);
1354 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1355 msg.u.x.portnum = atoi(number);
1360 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1361 return("Broken pipe while sending command.");
1363 /* receive response */
1364 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1365 return("Broken pipe while receiving response.");
1367 case MODE_INTERFACE:
1368 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1369 return("Response not valid.");
1372 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1373 return("Response not valid.");
1376 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1377 return("Response not valid.");
1380 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1381 return("Response not valid.");
1386 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1387 return("Response not valid.");
1391 /* process response */
1392 if (msg.u.x.error) {
1393 return(msg.u.x.message);
1395 printf("Command successfull.\n");
1403 #define GET_NOW() { \
1404 gettimeofday(&now_tv, &now_tz); \
1405 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1407 const char *admin_testcall(int sock, int argc, char *argv[])
1409 static struct admin_message msg;
1411 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1413 double timer = 0, now_d;
1414 unsigned int on = 1;
1415 struct timeval now_tv;
1416 struct timezone now_tz;
1418 printf("pid=%d\n", getpid()); fflush(stdout);
1421 if (!strcmp(argv[ar], "--setup-timeout")) {
1424 return("Missing setup timeout value.\n");
1425 stimeout = atoi(argv[ar]);
1428 if (!strcmp(argv[ar], "--proceeding-timeout")) {
1431 return("Missing proceeding timeout value.\n");
1432 ptimeout = atoi(argv[ar]);
1435 if (!strcmp(argv[ar], "--alerting-timeout")) {
1438 return("Missing alerting timeout value.\n");
1439 atimeout = atoi(argv[ar]);
1442 if (!strcmp(argv[ar], "--connect-timeout")) {
1445 return("Missing connect timeout value.\n");
1446 ctimeout = atoi(argv[ar]);
1453 /* send reload command */
1454 memset(&msg, 0, sizeof(msg));
1455 msg.message = ADMIN_CALL_SETUP;
1456 msg.u.call.present = 1;
1459 SCPY(msg.u.call.interface, argv[ar]);
1463 SCPY(msg.u.call.callerid, argv[ar]);
1467 SCPY(msg.u.call.dialing, argv[ar]);
1471 if (argv[ar][0] == 'r')
1472 msg.u.call.present = 0;
1475 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1476 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1477 msg.u.call.bc_info1 = 0;
1479 msg.u.call.exthlc = 0;
1481 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1483 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1486 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1487 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1491 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1492 if (msg.u.call.bc_info1 < 0)
1493 msg.u.call.bc_info1 = 0;
1495 msg.u.call.bc_info1 |= 0x80;
1499 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1500 if (msg.u.call.hlc < 0)
1503 msg.u.call.hlc |= 0x80;
1507 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1508 if (msg.u.call.exthlc < 0)
1509 msg.u.call.exthlc = 0;
1511 msg.u.call.exthlc |= 0x80;
1515 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1516 return("Broken pipe while sending command.");
1518 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1519 return("Failed to set socket into non-blocking IO.");
1523 timer = now_d + (double)stimeout;
1526 /* receive response */
1528 l = read(sock, &msg, sizeof(msg));
1530 if (errno == EWOULDBLOCK) {
1533 if (timer <= now_d) {
1534 printf("Timeout\n"); fflush(stdout);
1541 return("Broken pipe while receiving response.");
1543 if (l != sizeof(msg))
1544 return("Response has unexpected message size.");
1545 switch(msg.message) {
1546 case ADMIN_CALL_SETUP_ACK:
1547 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1550 case ADMIN_CALL_PROCEEDING:
1551 printf("PROCEEDING\n"); fflush(stdout);
1554 timer = now_d + (double)ptimeout;
1558 case ADMIN_CALL_ALERTING:
1559 printf("ALERTING\n"); fflush(stdout);
1562 timer = now_d + (double)atimeout;
1566 case ADMIN_CALL_CONNECT:
1567 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1570 timer = now_d + (double)ctimeout;
1574 case ADMIN_CALL_NOTIFY:
1575 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify_progress, msg.u.call.callerid); fflush(stdout);
1578 case ADMIN_CALL_PROGRESS:
1579 printf("PROGRESS\n progress=%d\n location=%d\n", msg.u.call.notify_progress, msg.u.call.location); fflush(stdout);
1582 case ADMIN_CALL_DISCONNECT:
1583 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);
1586 case ADMIN_CALL_RELEASE:
1587 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);
1591 return("Response not valid.");
1594 printf("Call released.\n"); fflush(stdout);
1602 const char *admin_trace(int sock, int argc, char *argv[])
1604 static struct admin_message msg;
1608 if (argc > 2) if (!strcasecmp(argv[2], "help")) {
1609 printf("Trace Help\n----------\n");
1610 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1611 printf("By default a complete trace is shown in detailed format.\n");
1612 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1613 printf("Use filter values to select specific trace messages.\n");
1614 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1615 printf("Filters:\n");
1616 printf(" category=<mask bits>\n");
1617 printf(" 0x01 = CH: channel object trace\n");
1618 printf(" 0x02 = EP: endpoint object trace\n");
1619 printf(" port=<mISDN port> select only given port for trace\n");
1620 printf(" interface=<interface name> select only given interface for trace\n");
1621 printf(" caller=<caller id> select only given caller id for trace\n");
1622 printf(" dialing=<number> select only given dialed number for trace\n");
1626 /* init trace request */
1627 memset(&msg, 0, sizeof(msg));
1628 msg.message = ADMIN_TRACE_REQUEST;
1629 msg.u.trace_req.detail = 3;
1630 msg.u.trace_req.port = -1;
1635 if (!strcasecmp(argv[i], "brief"))
1636 msg.u.trace_req.detail = 1;
1637 else if (!strcasecmp(argv[i], "short"))
1638 msg.u.trace_req.detail = 2;
1639 else if (!strncasecmp(argv[i], "category=", 9))
1640 msg.u.trace_req.category = atoi(argv[i]+9);
1641 else if (!strncasecmp(argv[i], "port=", 5))
1642 msg.u.trace_req.port = atoi(argv[i]+5);
1643 else if (!strncasecmp(argv[i], "interface=", 10))
1644 SCPY(msg.u.trace_req.interface, argv[i]+10);
1645 else if (!strncasecmp(argv[i], "caller=", 7))
1646 SCPY(msg.u.trace_req.caller, argv[i]+7);
1647 else if (!strncasecmp(argv[i], "dialing=", 8))
1648 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1649 else return("Invalid trace option, try 'trace help'.");
1654 /* send trace request */
1655 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1656 return("Broken pipe while sending trace request.");
1658 /* receive response */
1660 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1661 return("Broken pipe while receiving response.");
1663 if (msg.message != ADMIN_TRACE_RESPONSE)
1664 return("Response not valid.");
1666 printf("%s", msg.u.trace_rsp.text);
1674 int main(int argc, char *argv[])
1678 struct sockaddr_un sock_address;
1679 const char *ret = "invalid mode";
1680 char options_error[256];
1686 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1687 printf("state - View current states using graphical console output.\n");
1688 printf("portinfo - Get info of current ports.\n");
1689 printf("interface [<portname>] - Tell LCR to reload \"interface.conf\".\n");
1690 printf("route - Tell LCR to reload \"route.conf\".\n");
1691 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1692 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1693 printf("block <port> - Block given port.\n");
1694 printf("unblock/load <port> - Unblock given port.\n");
1695 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1696 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1697 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1698 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1699 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1700 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1701 printf(" -> Use 'trace help' to see filter description.\n");
1707 if (!(strcasecmp(argv[1],"state"))) {
1710 if (!(strcasecmp(argv[1],"portinfo"))) {
1711 mode = MODE_PORTINFO;
1713 if (!(strcasecmp(argv[1],"interface"))) {
1714 mode = MODE_INTERFACE;
1716 if (!(strcasecmp(argv[1],"route"))) {
1719 if (!(strcasecmp(argv[1],"dial"))) {
1724 if (!(strcasecmp(argv[1],"release"))) {
1727 mode = MODE_RELEASE;
1729 if (!(strcasecmp(argv[1],"unblock"))
1730 || !(strcasecmp(argv[1],"load"))) {
1733 mode = MODE_UNBLOCK;
1735 if (!(strcasecmp(argv[1],"block"))) {
1740 if (!(strcasecmp(argv[1],"unload"))) {
1745 if (!(strcasecmp(argv[1],"testcall"))) {
1748 mode = MODE_TESTCALL;
1750 if (!(strcasecmp(argv[1],"trace"))) {
1756 if (read_options(options_error) == 0) {
1762 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1763 fprintf(stderr, "Failed to create socket.\n");
1766 memset(&sock_address, 0, sizeof(sock_address));
1767 SPRINT(sock_address.sun_path, SOCKET_NAME, options.lock);
1768 sock_address.sun_family = PF_UNIX;
1769 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1771 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1778 ret = admin_state(sock, argv);
1782 ret = admin_portinfo(sock, argc, argv);
1785 case MODE_INTERFACE:
1787 ret = admin_cmd(sock, mode, NULL, NULL);
1791 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1798 ret = admin_cmd(sock, mode, NULL, argv[2]);
1802 ret = admin_testcall(sock, argc, argv);
1806 ret = admin_trace(sock, argc, argv);
1811 /* now we say good bye */
1813 // if (!strncasecmp(ret, "Broken Pipe", 11))
1815 printf("%s\n", ret);