1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** Administration tool **
10 \*****************************************************************************/
15 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
29 #include "extension.h"
31 #include "lcrsocket.h"
34 #define LTEE {addch(ACS_LTEE);addch(ACS_HLINE);addch(ACS_HLINE);}
35 #define LLCORNER {addch(ACS_LLCORNER);addch(ACS_HLINE);addch(ACS_HLINE);}
36 #define VLINE {addch(ACS_VLINE);addstr(" ");}
37 #define EMPTY {addstr(" ");}
38 //char rotator[] = {'-', '\\', '|', '/'};
39 int lastlines, lastcols;
40 int show_interfaces = 2,
58 const char *text_interfaces[] = {
65 const char *text_calls[] = {
80 char logline[LOGLINES][512];
81 unsigned int logcur = 0;
88 void init_curses(void)
91 initscr(); cbreak(); noecho();
93 nodelay(stdscr, TRUE);
94 if (COLOR_PAIRS>=8 && COLORS>=8)
108 void cleanup_curses(void)
113 void color(int color)
115 if (COLOR_PAIRS>=8 && COLORS>=8)
116 attrset(COLOR_PAIR(color));
120 * permanently show current state using ncurses
122 int debug_port(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
129 SPRINT(buffer,"%s(%d)", m[i].u.p.name,m[i].u.p.serial);
133 switch (m[i].u.p.state) {
134 case ADMIN_STATE_IDLE:
138 case ADMIN_STATE_IN_SETUP:
140 addstr("'in << setup'");
142 case ADMIN_STATE_OUT_SETUP:
144 addstr("'out >> setup'");
146 case ADMIN_STATE_IN_OVERLAP:
148 addstr("'in << overlap'");
150 case ADMIN_STATE_OUT_OVERLAP:
152 addstr("'out >> overlap'");
154 case ADMIN_STATE_IN_PROCEEDING:
156 addstr("'in << proc'");
158 case ADMIN_STATE_OUT_PROCEEDING:
160 addstr("'out >> proc'");
162 case ADMIN_STATE_IN_ALERTING:
164 addstr("'in << alert'");
166 case ADMIN_STATE_OUT_ALERTING:
168 addstr("'out >> alert'");
170 case ADMIN_STATE_CONNECT:
174 case ADMIN_STATE_IN_DISCONNECT:
176 addstr("'in << disc'");
178 case ADMIN_STATE_OUT_DISCONNECT:
180 addstr("'out >> disc'");
184 addstr("'--NONE--'");
189 addstr(" bchannel=");
191 SPRINT(buffer,"%d", m[i].u.p.isdn_chan);
193 if (m[i].u.p.isdn_ces >= 0) {
197 SPRINT(buffer, "%d", m[i].u.p.isdn_ces);
200 if (m[i].u.p.isdn_hold) {
208 int debug_epoint(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
210 unsigned int epoint = m[i].u.e.serial;
217 SPRINT(buffer,"EPOINT(%d)", epoint);
221 switch (m[i].u.e.state) {
222 case ADMIN_STATE_IDLE:
226 case ADMIN_STATE_IN_SETUP:
228 addstr("'in << setup'");
230 case ADMIN_STATE_OUT_SETUP:
232 addstr("'out >> setup'");
234 case ADMIN_STATE_IN_OVERLAP:
236 addstr("'in << overlap'");
238 case ADMIN_STATE_OUT_OVERLAP:
240 addstr("'out >> overlap'");
242 case ADMIN_STATE_IN_PROCEEDING:
244 addstr("'in << proc'");
246 case ADMIN_STATE_OUT_PROCEEDING:
248 addstr("'out >> proc'");
250 case ADMIN_STATE_IN_ALERTING:
252 addstr("'in << alert'");
254 case ADMIN_STATE_OUT_ALERTING:
256 addstr("'out >> alert'");
258 case ADMIN_STATE_CONNECT:
262 case ADMIN_STATE_IN_DISCONNECT:
264 addstr("'in << disc'");
266 case ADMIN_STATE_OUT_DISCONNECT:
268 addstr("'out >> disc'");
272 addstr("'--NONE--'");
274 if (m[i].u.e.terminal[0]) {
276 addstr(" terminal=");
278 addstr(m[i].u.e.terminal);
281 SPRINT(buffer, " %s", m[i].u.e.callerid);
286 addstr(m[i].u.e.dialing);
287 if (m[i].u.e.action[0]) {
291 addstr(m[i].u.e.action);
295 addstr(" park="); /* 9 digits */
297 UCPY(buffer, "\""); /* 9 digits */
299 jj = m[i].u.e.park_len;
301 c = m[i].u.e.park_callid[j];
302 if (c >= 32 && c < 127 && c != '[') {
305 UPRINT(buffer+strlen(buffer), "[%02x]", c);
312 switch(m[i].u.e.rx_state) {
313 case NOTIFY_STATE_SUSPEND:
314 addstr(" in=suspend");
316 case NOTIFY_STATE_HOLD:
319 case NOTIFY_STATE_CONFERENCE:
320 addstr(" in=conference");
323 switch(m[i].u.e.tx_state) {
324 case NOTIFY_STATE_SUSPEND:
325 addstr(" out=suspend");
327 case NOTIFY_STATE_HOLD:
330 case NOTIFY_STATE_CONFERENCE:
331 addstr(" out=conference");
335 if (m[i].u.e.crypt) {
338 if (m[i].u.e.crypt) { /* crypt on */
346 /* loop all related ports */
348 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins+msg->u.s.epoints;
349 jj = j + msg->u.s.ports;
351 if (m[j].u.p.epoint == epoint) {
353 move(++line>1?line:1, 1);
358 move(line>1?line:1, 5);
361 move(line>1?line:1, 8);
362 if (line+2 >= LINES) break;
363 line = debug_port(msg, m, line, j, vline);
364 if (line+2 >= LINES) break;
370 move(ltee>1?line:1, 5);
376 int debug_join(struct admin_message *msg, struct admin_message *m, int line, int i)
378 unsigned int join = m[i].u.j.serial;
383 SPRINT(buffer,"JOIN(%d)", join);
385 if (m[i].u.j.partyline) {
387 addstr(" partyline=");
389 SPRINT(buffer, "%d\n", m[i].u.j.partyline);
392 if (m[i].u.j.remote[0]) {
396 SPRINT(buffer, "%s\n", m[i].u.j.remote);
399 /* find number of epoints */
400 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins;
401 jj = j + msg->u.s.epoints;
404 if (m[j].u.e.join == join)
408 /* loop all related endpoints */
409 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins;
410 jj = j + msg->u.s.epoints;
412 if (m[j].u.e.join == join) {
414 move(++line>1?line:1, 1);
420 move(line>1?line:1, 4);
421 if (line+2 >= LINES) break;
422 line = debug_epoint(msg, m, line, j, i?1:0);
423 if (line+2 >= LINES) break;
431 const char *admin_state(int sock, char *argv[])
433 struct admin_message msg,
437 int line, offset = 0, hoffset = 0;
446 char enter_string[128] = "", ch;
448 struct timeval select_tv;
450 /* flush logfile name */
457 /* send reload command */
458 memset(&msg, 0, sizeof(msg));
459 msg.message = ADMIN_REQUEST_STATE;
460 // printf("sizeof=%d\n",sizeof(msg));fflush(stdout);
461 if (write(sock, &msg, sizeof(msg)) != sizeof(msg)) {
463 return("Broken pipe while sending command.");
466 /* receive response */
467 if (read(sock, &msg, sizeof(msg)) != sizeof(msg)) {
469 return("Broken pipe while receiving response.");
472 if (msg.message != ADMIN_RESPONSE_STATE) {
474 return("Response not valid. Expecting state response.");
476 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
477 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
481 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off) {
484 // fprintf(stderr, "got=%d expected=%d\n", i, num*sizeof(struct admin_message));
486 return("Broken pipe while receiving state infos.");
488 if (len < num*(int)sizeof(struct admin_message)) {
496 // fprintf("getting =%d interfaces\n", msg.u.s.interfaces);
497 while(i < msg.u.s.interfaces) {
498 // fprintf(stderr, "j=%d message=%d\n", j, m[j].message);
499 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE) {
502 return("Response not valid. Expecting interface information.");
508 while(i < msg.u.s.remotes) {
509 if (m[j].message != ADMIN_RESPONSE_S_REMOTE) {
512 return("Response not valid. Expecting remote application information.");
518 while(i < msg.u.s.joins) {
519 if (m[j].message != ADMIN_RESPONSE_S_JOIN) {
522 return("Response not valid. Expecting join information.");
528 while(i < msg.u.s.epoints) {
529 if (m[j].message != ADMIN_RESPONSE_S_EPOINT) {
532 return("Response not valid. Expecting endpoint information.");
538 while(i < msg.u.s.ports) {
539 if (m[j].message != ADMIN_RESPONSE_S_PORT) {
542 return("Response not valid. Expecting port information.");
547 // now j is the number of message blocks
555 if (!!strcmp(logfile, msg.u.s.logfile)) {
556 SCPY(logfile, msg.u.s.logfile);
563 logline[i][1] = '\0';
567 logfh = open(logfile, O_RDONLY|O_NONBLOCK);
569 /* seek at the end -8000 chars */
570 lseek(logfh, -8000, SEEK_END);
571 /* if not at the beginning, read until endofline */
572 logline[logcur % LOGLINES][0] = '\0';
573 l = read(logfh, logline[logcur % LOGLINES], sizeof(logline[logcur % LOGLINES])-1);
575 /* read first line and skip junk */
576 logline[logcur % LOGLINES][l] = '\0';
577 if ((p = strchr(logline[logcur % LOGLINES],'\n'))) {
579 SCPY(logline[logcur % LOGLINES], p+1);
580 SCPY(logline[(logcur-1) % LOGLINES], "...");
590 ll = strlen(logline[logcur % LOGLINES]);
591 l = read(logfh, logline[logcur % LOGLINES]+ll, sizeof(logline[logcur % LOGLINES])-ll-1);
594 logline[logcur % LOGLINES][ll+l] = '\0';
596 /* put data to lines */
597 while ((p = strchr(logline[logcur % LOGLINES],'\n'))) {
600 SCPY(logline[logcur % LOGLINES], p+1);
602 /* if line is full without return, go next line */
603 if (strlen(logline[logcur % LOGLINES]) == sizeof(logline[logcur % LOGLINES])-1) {
605 logline[logcur % LOGLINES][0] = '\0';
610 /* display interfaces */
611 if (show_interfaces > 0) {
614 ii = i + msg.u.s.interfaces;
616 /* show interface summary */
617 move(++line>1?line:1, 0);
619 if (m[i].u.i.block >= 2) {
620 if (m[i].u.i.portnum < 0)
621 SPRINT(buffer, "%s (port ?: %s)%s", m[i].u.i.interface_name, m[i].u.i.portname, (m[i].u.i.extension)?" exten":"");
623 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":"");
626 addstr(" not loaded");
628 SPRINT(buffer, "%s", m[i].u.i.interface_name);
631 SPRINT(buffer, "(port %d: %s)", m[i].u.i.portnum, m[i].u.i.portname);
634 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":"");
640 SPRINT(buffer, " use:%d", m[i].u.i.use);
642 if (m[i].u.i.ptp || !m[i].u.i.ntmode) {
643 color((m[i].u.i.l2link > 0)?green:red);
644 if (m[i].u.i.l2link < 0)
647 addstr((m[i].u.i.l2link)?" L2 UP":" L2 down");
653 if (m[i].u.i.l2mask[j>>3] & (1 << (j&7))) {
654 SPRINT(buffer, "%s%d", k?",":" TEI(", j);
663 color((m[i].u.i.l1link > 0)?green:blue);
664 if (m[i].u.i.l1link < 0)
667 addstr((m[i].u.i.l1link)?" L1 UP":" L1 down");
680 if (m[i].u.i.slip_tx || m[i].u.i.slip_rx) {
682 SPRINT(buffer, " SLIP(tx:%d rx:%d)", m[i].u.i.slip_tx, m[i].u.i.slip_rx);
685 if (m[i].u.i.block) {
689 if (line+2 >= LINES) goto end;
691 if (show_interfaces > 1) {
694 jj = m[i].u.i.channels;
696 /* show all channels */
697 if (show_interfaces>2 || m[i].u.i.busy[j]>0) {
699 /* show left side / right side */
700 if ((k & 1) && (COLS > 70)) {
701 move(line>1?line:1,4+((COLS-4)/2));
703 move(++line>1?line:1, 1);
710 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
712 SPRINT(buffer,"B%2d: ", j+1);
713 if (m[i].u.i.mode[j] == B_MODE_HDLC)
714 SCAT(buffer,"HDLC ");
716 switch(m[i].u.i.busy[j]) {
718 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block) {
726 case B_STATE_ACTIVATING:
734 case B_STATE_DEACTIVATING:
738 case B_STATE_EXPORTING:
746 case B_STATE_IMPORTING:
751 if (m[i].u.i.port[j]) {
752 /* search for port */
753 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
754 ll = l+msg.u.s.ports;
756 if (m[l].u.p.serial == m[i].u.i.port[j]) {
757 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
763 if (line+2 >= LINES) {
766 move(line>1?line:1, 1);
776 move(line>1?line:1, 1);
779 if (line+2 >= LINES) goto end;
780 /* show summary if no channels were shown */
781 if (show_interfaces<2 && ltee==0) {
783 move(++line>1?line:1, 1);
786 if (m[i].u.i.l2link && m[i].u.i.block==0) {
788 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
791 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
795 if (line+2 >= LINES) goto end;
801 i = msg.u.s.interfaces;
802 ii = i + msg.u.s.remotes;
804 /* show remote summary */
805 move(++line>1?line:1, 0);
807 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
813 if (line+2 >= LINES) goto end;
815 /* display calls (brief) */
816 if (show_calls == 1) {
818 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
819 ii = i+msg.u.s.epoints;
821 /* for each endpoint... */
822 if (!m[i].u.e.join) {
823 move(++line>1?line:1, 0);
825 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
828 if (m[i].u.e.terminal[0]) {
831 addstr(m[i].u.e.terminal);
835 SPRINT(buffer, " %s", m[i].u.e.callerid);
840 SPRINT(buffer, "%s", m[i].u.e.dialing);
842 if (m[i].u.e.action[0]) {
846 addstr(m[i].u.e.action);
848 if (line+2 >= LINES) goto end;
853 j = msg.u.s.interfaces+msg.u.s.remotes;
854 jj = j+msg.u.s.joins;
856 /* for each call... */
857 move(++line>1?line:1, 0);
859 SPRINT(buffer, "(%d):", m[j].u.j.serial);
861 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
862 ii = i+msg.u.s.epoints;
864 /* for each endpoint... */
865 if (m[i].u.e.join == m[j].u.j.serial) {
867 SPRINT(buffer, " (%d)", m[i].u.e.serial);
870 if (m[i].u.e.terminal[0]) {
873 addstr(m[i].u.e.terminal);
877 SPRINT(buffer, "-%s", m[i].u.e.callerid);
882 SPRINT(buffer, "%s", m[i].u.e.dialing);
888 if (line+2 >= LINES) goto end;
893 if (line+2 >= LINES) goto end;
895 /* display calls (structurd) */
896 if (show_calls == 2) {
897 /* show all ports with no epoint */
899 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
900 ii = i+msg.u.s.ports;
902 if (!m[i].u.p.epoint) {
903 move(++line>1?line:1, 8);
904 if (line+2 >= LINES) goto end;
905 line = debug_port(&msg, m, line, i, 0);
906 if (line+2 >= LINES) goto end;
913 if (line+2 >= LINES) goto end;
915 /* show all epoints with no call */
917 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
918 ii = i+msg.u.s.epoints;
920 if (!m[i].u.e.join) {
921 move(++line>1?line:1, 4);
922 if (line+2 >= LINES) goto end;
923 line = debug_epoint(&msg, m, line, i, 0);
924 if (line+2 >= LINES) goto end;
931 if (line+2 >= LINES) goto end;
935 i = msg.u.s.interfaces+msg.u.s.remotes;
936 ii = i+msg.u.s.joins;
938 move(++line>1?line:1, 0);
939 if (line+2 >= LINES) goto end;
940 line = debug_join(&msg, m, line, i);
941 if (line+2 >= LINES) goto end;
947 if (line+2 >= LINES) goto end;
953 if (line+2 < LINES) {
954 move(line++>1?line-1:1, 0);
956 hline(ACS_HLINE, COLS);
959 l = logcur-(LINES-line-2);
961 if (ll-l >= LOGLINES)
964 move(line++>1?line-1:1, 0);
965 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
966 SCPY(buffer, logline[l % LOGLINES] + hoffset);
969 if (COLS < (int)strlen(buffer)) {
970 buffer[COLS-1] = '\0';
985 /* display name/time */
990 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
991 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
995 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
996 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
997 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1000 /* displeay head line */
1003 hline(ACS_HLINE, COLS);
1006 SPRINT(buffer, "Offset +%d", offset);
1012 SPRINT(buffer, "H-Offset +%d", hoffset);
1019 hline(ACS_HLINE, COLS);
1023 SPRINT(buffer, "-> %s", enter_string);
1026 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1032 if (lastlines!=LINES || lastcols!=COLS) {
1039 /* user input in enter mode */
1046 if (!enter_string[0])
1049 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1050 if (!!strncmp(enter_string, "interface", 10) &&
1051 !!strncmp(enter_string, "route", 6) &&
1052 !!strncmp(enter_string, "release ", 8) &&
1053 !!strncmp(enter_string, "block ", 6) &&
1054 !!strncmp(enter_string, "unblock ", 8) &&
1055 !!strncmp(enter_string, "load ", 5) &&
1056 !!strncmp(enter_string, "unload ", 7)) {
1057 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1058 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1059 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1060 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1061 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1062 SPRINT(logline[logcur++ % LOGLINES], "unblock/load <port> (unblock port for further calls, load if not loaded)");
1063 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1065 /* applend output to log window */
1066 SPRINT(buffer, "%s %s", argv[0], enter_string);
1067 fp = popen(buffer, "r");
1069 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1070 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1073 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1076 logline[logcur % LOGLINES][0] = '\0';
1077 enter_string[0] = '\0';
1080 if (ch>=32 && ch<=126) {
1081 SCCAT(enter_string, ch);
1087 if (ch==8 || ch==127) {
1088 if (enter_string[0])
1089 enter_string[strlen(enter_string)-1] = '\0';
1099 FD_ZERO(&select_rfds);
1100 FD_SET(0, &select_rfds);
1101 select_tv.tv_sec = 0;
1102 select_tv.tv_usec = 250000;
1103 select(1, &select_rfds, NULL, NULL, &select_tv);
1107 /* user input in normal mode */
1109 case 12: /* refresh */
1120 case 'i': /* toggle interface */
1122 if (show_interfaces > 3) show_interfaces = 0;
1125 case 'c': /* toggle calls */
1127 if (show_calls > 2) show_calls = 0;
1130 case 'l': /* toggle log */
1132 if (show_log > 1) show_log = 0;
1135 case '+': /* scroll down */
1139 case '-': /* scroll up */
1144 case '*': /* scroll right */
1148 case '/': /* scroll left */
1153 case 10: /* entermode */
1158 FD_ZERO(&select_rfds);
1159 FD_SET(0, &select_rfds);
1160 select_tv.tv_sec = 0;
1161 select_tv.tv_usec = 250000;
1162 select(1, &select_rfds, NULL, NULL, &select_tv);
1167 /* check for logfh */
1172 /* cleanup curses and exit */
1178 const char *admin_portinfo(int sock, int argc, char *argv[])
1180 struct admin_message msg,
1187 /* send state request command */
1188 memset(&msg, 0, sizeof(msg));
1189 msg.message = ADMIN_REQUEST_STATE;
1190 if (write(sock, &msg, sizeof(msg)) != sizeof(msg)) {
1192 return("Broken pipe while sending command.");
1195 /* receive response */
1196 if (read(sock, &msg, sizeof(msg)) != sizeof(msg)) {
1198 return("Broken pipe while receiving response.");
1201 if (msg.message != ADMIN_RESPONSE_STATE) {
1203 return("Response not valid. Expecting state response.");
1205 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
1206 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
1210 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off) {
1214 return("Broken pipe while receiving state infos.");
1216 if (len < num*(int)sizeof(struct admin_message)) {
1224 while(i < msg.u.s.interfaces) {
1225 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE) {
1228 return("Response not valid. Expecting interface information.");
1234 while(i < msg.u.s.remotes) {
1235 if (m[j].message != ADMIN_RESPONSE_S_REMOTE) {
1238 return("Response not valid. Expecting remote application information.");
1244 while(i < msg.u.s.joins) {
1245 if (m[j].message != ADMIN_RESPONSE_S_JOIN) {
1248 return("Response not valid. Expecting join information.");
1254 while(i < msg.u.s.epoints) {
1255 if (m[j].message != ADMIN_RESPONSE_S_EPOINT) {
1258 return("Response not valid. Expecting endpoint information.");
1264 while(i < msg.u.s.ports) {
1265 if (m[j].message != ADMIN_RESPONSE_S_PORT) {
1268 return("Response not valid. Expecting port information.");
1273 // now j is the number of message blocks
1275 /* output interfaces */
1277 ii = i + msg.u.s.interfaces;
1280 if (!!strcmp(argv[2], m[i].u.i.interface_name)) {
1285 printf("%s:\n", m[i].u.i.interface_name);
1286 if (m[i].u.i.portnum < 0)
1287 printf("\t port = unknown\n");
1289 printf("\t port = %d \"%s\"\n",m[i].u.i.portnum, m[i].u.i.portname);
1290 printf("\t extension = %s\n", (m[i].u.i.extension)?"yes":"no");
1291 if (m[i].u.i.block >= 2) {
1292 printf("\t status = not loaded\n");
1295 printf("\t status = blocked\n");
1297 printf("\t status = unblocked\n");
1298 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":"");
1299 if (m[i].u.i.l1link < 0)
1300 printf("\t l1 link = unknown\n");
1302 printf("\t l1 link = %s\n", (m[i].u.i.l1link)?"up":"down");
1303 if (m[i].u.i.ptp || !m[i].u.i.ntmode) {
1304 if (m[i].u.i.l2link < 0)
1305 printf("\t l2 link = unknown\n");
1307 printf("\t l2 link = %s\n", (m[i].u.i.l2link)?"up":"down");
1309 printf("\t usage = %d\n", m[i].u.i.use);
1322 * Send command and show error message.
1324 const char *admin_cmd(int sock, int mode, char *extension, char *number)
1326 static struct admin_message msg;
1328 /* send reload command */
1329 memset(&msg, 0, sizeof(msg));
1331 case MODE_INTERFACE:
1332 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1335 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1338 msg.message = ADMIN_REQUEST_CMD_DIAL;
1339 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1342 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1343 SCPY(msg.u.x.message, number);
1346 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1347 msg.u.x.portnum = atoi(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);
1362 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1363 return("Broken pipe while sending command.");
1365 /* receive response */
1366 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1367 return("Broken pipe while receiving response.");
1369 case MODE_INTERFACE:
1370 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1371 return("Response not valid.");
1374 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1375 return("Response not valid.");
1378 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1379 return("Response not valid.");
1382 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1383 return("Response not valid.");
1388 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1389 return("Response not valid.");
1393 /* process response */
1394 if (msg.u.x.error) {
1395 return(msg.u.x.message);
1397 printf("Command successfull.\n");
1405 #define GET_NOW() { \
1406 gettimeofday(&now_tv, &now_tz); \
1407 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1409 const char *admin_testcall(int sock, int argc, char *argv[])
1411 static struct admin_message msg;
1413 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1415 double timer = 0, now_d;
1416 unsigned int on = 1;
1417 struct timeval now_tv;
1418 struct timezone now_tz;
1420 printf("pid=%d\n", getpid()); fflush(stdout);
1423 if (!strcmp(argv[ar], "--setup-timeout")) {
1426 return("Missing setup timeout value.\n");
1427 stimeout = atoi(argv[ar]);
1430 if (!strcmp(argv[ar], "--proceeding-timeout")) {
1433 return("Missing proceeding timeout value.\n");
1434 ptimeout = atoi(argv[ar]);
1437 if (!strcmp(argv[ar], "--alerting-timeout")) {
1440 return("Missing alerting timeout value.\n");
1441 atimeout = atoi(argv[ar]);
1444 if (!strcmp(argv[ar], "--connect-timeout")) {
1447 return("Missing connect timeout value.\n");
1448 ctimeout = atoi(argv[ar]);
1455 /* send reload command */
1456 memset(&msg, 0, sizeof(msg));
1457 msg.message = ADMIN_CALL_SETUP;
1458 msg.u.call.present = 1;
1461 SCPY(msg.u.call.interface, argv[ar]);
1465 SCPY(msg.u.call.callerid, argv[ar]);
1469 SCPY(msg.u.call.dialing, argv[ar]);
1473 if (argv[ar][0] == 'r')
1474 msg.u.call.present = 0;
1477 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1478 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1479 msg.u.call.bc_info1 = 0;
1481 msg.u.call.exthlc = 0;
1483 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1485 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1488 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1489 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1493 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1494 if (msg.u.call.bc_info1 < 0)
1495 msg.u.call.bc_info1 = 0;
1497 msg.u.call.bc_info1 |= 0x80;
1501 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1502 if (msg.u.call.hlc < 0)
1505 msg.u.call.hlc |= 0x80;
1509 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1510 if (msg.u.call.exthlc < 0)
1511 msg.u.call.exthlc = 0;
1513 msg.u.call.exthlc |= 0x80;
1517 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1518 return("Broken pipe while sending command.");
1520 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1521 return("Failed to set socket into non-blocking IO.");
1525 timer = now_d + (double)stimeout;
1528 /* receive response */
1530 l = read(sock, &msg, sizeof(msg));
1532 if (errno == EWOULDBLOCK) {
1535 if (timer <= now_d) {
1536 printf("Timeout\n"); fflush(stdout);
1543 return("Broken pipe while receiving response.");
1545 if (l != sizeof(msg))
1546 return("Response has unexpected message size.");
1547 switch(msg.message) {
1548 case ADMIN_CALL_SETUP_ACK:
1549 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1552 case ADMIN_CALL_PROCEEDING:
1553 printf("PROCEEDING\n"); fflush(stdout);
1556 timer = now_d + (double)ptimeout;
1560 case ADMIN_CALL_ALERTING:
1561 printf("ALERTING\n"); fflush(stdout);
1564 timer = now_d + (double)atimeout;
1568 case ADMIN_CALL_CONNECT:
1569 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1572 timer = now_d + (double)ctimeout;
1576 case ADMIN_CALL_NOTIFY:
1577 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1580 case ADMIN_CALL_DISCONNECT:
1581 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);
1584 case ADMIN_CALL_RELEASE:
1585 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);
1589 return("Response not valid.");
1592 printf("Call released.\n"); fflush(stdout);
1600 const char *admin_trace(int sock, int argc, char *argv[])
1602 static struct admin_message msg;
1606 if (argc > 2) if (!strcasecmp(argv[2], "help")) {
1607 printf("Trace Help\n----------\n");
1608 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1609 printf("By default a complete trace is shown in detailed format.\n");
1610 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1611 printf("Use filter values to select specific trace messages.\n");
1612 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1613 printf("Filters:\n");
1614 printf(" category=<mask bits>\n");
1615 printf(" 0x01 = CH: channel object trace\n");
1616 printf(" 0x02 = EP: endpoint object trace\n");
1617 printf(" port=<mISDN port> select only given port for trace\n");
1618 printf(" interface=<interface name> select only given interface for trace\n");
1619 printf(" caller=<caller id> select only given caller id for trace\n");
1620 printf(" dialing=<number> select only given dialed number for trace\n");
1624 /* init trace request */
1625 memset(&msg, 0, sizeof(msg));
1626 msg.message = ADMIN_TRACE_REQUEST;
1627 msg.u.trace_req.detail = 3;
1628 msg.u.trace_req.port = -1;
1633 if (!strcasecmp(argv[i], "brief"))
1634 msg.u.trace_req.detail = 1;
1635 else if (!strcasecmp(argv[i], "short"))
1636 msg.u.trace_req.detail = 2;
1637 else if (!strncasecmp(argv[i], "category=", 9))
1638 msg.u.trace_req.category = atoi(argv[i]+9);
1639 else if (!strncasecmp(argv[i], "port=", 5))
1640 msg.u.trace_req.port = atoi(argv[i]+5);
1641 else if (!strncasecmp(argv[i], "interface=", 10))
1642 SCPY(msg.u.trace_req.interface, argv[i]+10);
1643 else if (!strncasecmp(argv[i], "caller=", 7))
1644 SCPY(msg.u.trace_req.caller, argv[i]+7);
1645 else if (!strncasecmp(argv[i], "dialing=", 8))
1646 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1647 else return("Invalid trace option, try 'trace help'.");
1652 /* send trace request */
1653 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1654 return("Broken pipe while sending trace request.");
1656 /* receive response */
1658 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1659 return("Broken pipe while receiving response.");
1661 if (msg.message != ADMIN_TRACE_RESPONSE)
1662 return("Response not valid.");
1664 printf("%s", msg.u.trace_rsp.text);
1672 int main(int argc, char *argv[])
1676 struct sockaddr_un sock_address;
1677 const char *ret = "invalid mode";
1684 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1685 printf("state - View current states using graphical console output.\n");
1686 printf("portinfo - Get info of current ports.\n");
1687 printf("interface [<portname>] - Tell LCR to reload \"interface.conf\".\n");
1688 printf("route - Tell LCR to reload \"route.conf\".\n");
1689 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1690 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1691 printf("block <port> - Block given port.\n");
1692 printf("unblock/load <port> - Unblock given port.\n");
1693 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1694 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1695 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1696 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1697 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1698 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1699 printf(" -> Use 'trace help' to see filter description.\n");
1705 if (!(strcasecmp(argv[1],"state"))) {
1708 if (!(strcasecmp(argv[1],"portinfo"))) {
1709 mode = MODE_PORTINFO;
1711 if (!(strcasecmp(argv[1],"interface"))) {
1712 mode = MODE_INTERFACE;
1714 if (!(strcasecmp(argv[1],"route"))) {
1717 if (!(strcasecmp(argv[1],"dial"))) {
1722 if (!(strcasecmp(argv[1],"release"))) {
1725 mode = MODE_RELEASE;
1727 if (!(strcasecmp(argv[1],"unblock"))
1728 || !(strcasecmp(argv[1],"load"))) {
1731 mode = MODE_UNBLOCK;
1733 if (!(strcasecmp(argv[1],"block"))) {
1738 if (!(strcasecmp(argv[1],"unload"))) {
1743 if (!(strcasecmp(argv[1],"testcall"))) {
1746 mode = MODE_TESTCALL;
1748 if (!(strcasecmp(argv[1],"trace"))) {
1754 if (read_options() == 0) {
1760 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1761 fprintf(stderr, "Failed to create socket.\n");
1764 memset(&sock_address, 0, sizeof(sock_address));
1765 SPRINT(sock_address.sun_path, SOCKET_NAME, options.lock);
1766 sock_address.sun_family = PF_UNIX;
1767 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1769 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1776 ret = admin_state(sock, argv);
1780 ret = admin_portinfo(sock, argc, argv);
1783 case MODE_INTERFACE:
1785 ret = admin_cmd(sock, mode, NULL, NULL);
1789 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1796 ret = admin_cmd(sock, mode, NULL, argv[2]);
1800 ret = admin_testcall(sock, argc, argv);
1804 ret = admin_trace(sock, argc, argv);
1809 /* now we say good bye */
1811 // if (!strncasecmp(ret, "Broken Pipe", 11))
1813 printf("%s\n", ret);