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)
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'");
185 addstr("'--NONE--'");
191 addstr(" bchannel=");
193 SPRINT(buffer,"%d", m[i].u.p.isdn_chan);
195 if (m[i].u.p.isdn_ces >= 0)
200 SPRINT(buffer, "%d", m[i].u.p.isdn_ces);
203 if (m[i].u.p.isdn_hold)
212 int debug_epoint(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
214 unsigned int epoint = m[i].u.e.serial;
221 SPRINT(buffer,"EPOINT(%d)", epoint);
225 switch (m[i].u.e.state)
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])
282 addstr(" terminal=");
284 addstr(m[i].u.e.terminal);
287 SPRINT(buffer, " %s", m[i].u.e.callerid);
292 addstr(m[i].u.e.dialing);
293 if (m[i].u.e.action[0])
298 addstr(m[i].u.e.action);
303 addstr(" park="); /* 9 digits */
305 UCPY(buffer, "\""); /* 9 digits */
307 jj = m[i].u.e.park_len;
310 c = m[i].u.e.park_callid[j];
311 if (c >= 32 && c < 127 && c != '[')
315 UPRINT(buffer+strlen(buffer), "[%02x]", c);
323 switch(m[i].u.e.rx_state)
325 case NOTIFY_STATE_SUSPEND:
326 addstr(" in=suspend");
328 case NOTIFY_STATE_HOLD:
331 case NOTIFY_STATE_CONFERENCE:
332 addstr(" in=conference");
335 switch(m[i].u.e.tx_state)
337 case NOTIFY_STATE_SUSPEND:
338 addstr(" out=suspend");
340 case NOTIFY_STATE_HOLD:
343 case NOTIFY_STATE_CONFERENCE:
344 addstr(" out=conference");
352 if (m[i].u.e.crypt) /* crypt on */
362 /* loop all related ports */
364 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins+msg->u.s.epoints;
365 jj = j + msg->u.s.ports;
368 if (m[j].u.p.epoint == epoint)
371 move(++line>1?line:1, 1);
376 move(line>1?line:1, 5);
379 move(line>1?line:1, 8);
380 if (line+2 >= LINES) break;
381 line = debug_port(msg, m, line, j, vline);
382 if (line+2 >= LINES) break;
389 move(ltee>1?line:1, 5);
395 int debug_join(struct admin_message *msg, struct admin_message *m, int line, int i)
397 unsigned int join = m[i].u.j.serial;
402 SPRINT(buffer,"JOIN(%d)", join);
404 if (m[i].u.j.partyline)
407 addstr(" partyline=");
409 SPRINT(buffer, "%d\n", m[i].u.j.partyline);
412 if (m[i].u.j.remote[0])
417 SPRINT(buffer, "%s\n", m[i].u.j.remote);
420 /* find number of epoints */
421 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins;
422 jj = j + msg->u.s.epoints;
426 if (m[j].u.e.join == join)
430 /* loop all related endpoints */
431 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins;
432 jj = j + msg->u.s.epoints;
435 if (m[j].u.e.join == join)
438 move(++line>1?line:1, 1);
444 move(line>1?line:1, 4);
445 if (line+2 >= LINES) break;
446 line = debug_epoint(msg, m, line, j, i?1:0);
447 if (line+2 >= LINES) break;
455 const char *admin_state(int sock, char *argv[])
457 struct admin_message msg,
461 int line, offset = 0, hoffset = 0;
470 char enter_string[128] = "", ch;
472 struct timeval select_tv;
474 /* flush logfile name */
481 /* send reload command */
482 memset(&msg, 0, sizeof(msg));
483 msg.message = ADMIN_REQUEST_STATE;
484 // printf("sizeof=%d\n",sizeof(msg));fflush(stdout);
485 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
488 return("Broken pipe while sending command.");
491 /* receive response */
492 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
495 return("Broken pipe while receiving response.");
498 if (msg.message != ADMIN_RESPONSE_STATE)
501 return("Response not valid. Expecting state response.");
503 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
504 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
509 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off)
513 // fprintf(stderr, "got=%d expected=%d\n", i, num*sizeof(struct admin_message));
515 return("Broken pipe while receiving state infos.");
517 if (len < num*(int)sizeof(struct admin_message))
526 // fprintf("getting =%d interfaces\n", msg.u.s.interfaces);
527 while(i < msg.u.s.interfaces)
529 // fprintf(stderr, "j=%d message=%d\n", j, m[j].message);
530 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE)
534 return("Response not valid. Expecting interface information.");
540 while(i < msg.u.s.remotes)
542 if (m[j].message != ADMIN_RESPONSE_S_REMOTE)
546 return("Response not valid. Expecting remote application information.");
552 while(i < msg.u.s.joins)
554 if (m[j].message != ADMIN_RESPONSE_S_JOIN)
558 return("Response not valid. Expecting join information.");
564 while(i < msg.u.s.epoints)
566 if (m[j].message != ADMIN_RESPONSE_S_EPOINT)
570 return("Response not valid. Expecting endpoint information.");
576 while(i < msg.u.s.ports)
578 if (m[j].message != ADMIN_RESPONSE_S_PORT)
582 return("Response not valid. Expecting port information.");
587 // now j is the number of message blocks
595 if (!!strcmp(logfile, msg.u.s.logfile))
597 SCPY(logfile, msg.u.s.logfile);
605 logline[i][1] = '\0';
609 logfh = open(logfile, O_RDONLY|O_NONBLOCK);
612 /* seek at the end -8000 chars */
613 lseek(logfh, -8000, SEEK_END);
614 /* if not at the beginning, read until endofline */
615 logline[logcur % LOGLINES][0] = '\0';
616 l = read(logfh, logline[logcur % LOGLINES], sizeof(logline[logcur % LOGLINES])-1);
619 /* read first line and skip junk */
620 logline[logcur % LOGLINES][l] = '\0';
621 if ((p = strchr(logline[logcur % LOGLINES],'\n')))
624 SCPY(logline[logcur % LOGLINES], p+1);
625 SCPY(logline[(logcur-1) % LOGLINES], "...");
637 ll = strlen(logline[logcur % LOGLINES]);
638 l = read(logfh, logline[logcur % LOGLINES]+ll, sizeof(logline[logcur % LOGLINES])-ll-1);
641 logline[logcur % LOGLINES][ll+l] = '\0';
643 /* put data to lines */
644 while ((p = strchr(logline[logcur % LOGLINES],'\n')))
648 SCPY(logline[logcur % LOGLINES], p+1);
650 /* if line is full without return, go next line */
651 if (strlen(logline[logcur % LOGLINES]) == sizeof(logline[logcur % LOGLINES])-1)
654 logline[logcur % LOGLINES][0] = '\0';
659 /* display interfaces */
660 if (show_interfaces > 0)
664 ii = i + msg.u.s.interfaces;
667 /* show interface summary */
668 move(++line>1?line:1, 0);
670 if (m[i].u.i.block >= 2)
672 if (m[i].u.i.portnum < 0)
673 SPRINT(buffer, "%s (port ?: %s)%s", m[i].u.i.interface_name, m[i].u.i.portname, (m[i].u.i.extension)?" extension":"");
675 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":"");
678 addstr(" not loaded");
681 SPRINT(buffer, "%s (port %d: %s) %s %s%s use:%d", m[i].u.i.interface_name, m[i].u.i.portnum, m[i].u.i.portname, (m[i].u.i.ntmode)?"NT-mode":"TE-mode", (m[i].u.i.ptp)?"ptp ":"ptmp", (m[i].u.i.extension)?" extension":"", m[i].u.i.use);
683 if (m[i].u.i.ptp || !m[i].u.i.ntmode)
685 color((m[i].u.i.l2link > 0)?green:red);
686 if (m[i].u.i.l2link < 0)
687 addstr(" L2 unknown");
689 addstr((m[i].u.i.l2link)?" L2 UP":" L2 down");
691 color((m[i].u.i.l1link > 0)?green:blue);
692 if (m[i].u.i.l1link < 0)
693 addstr(" L1 unknown");
695 addstr((m[i].u.i.l1link)?" L1 ACTIVE":" L1 inactive");
711 if (m[i].u.i.slip_tx || m[i].u.i.slip_rx)
714 SPRINT(buffer, " SLIP(tx:%d rx:%d)", m[i].u.i.slip_tx, m[i].u.i.slip_rx);
722 if (line+2 >= LINES) goto end;
724 if (show_interfaces > 1)
728 jj = m[i].u.i.channels;
731 /* show all channels */
732 if (show_interfaces>2 || m[i].u.i.busy[j]>0)
735 /* show left side / right side */
736 if ((k & 1) && (COLS > 70))
738 move(line>1?line:1,4+((COLS-4)/2));
741 move(++line>1?line:1, 1);
748 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
750 SPRINT(buffer,"B%2d: ", j+1);
751 if (m[i].u.i.mode[j] == B_MODE_HDLC)
752 SCAT(buffer,"HDLC ");
754 switch(m[i].u.i.busy[j])
757 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block)
767 case B_STATE_ACTIVATING:
775 case B_STATE_DEACTIVATING:
779 case B_STATE_EXPORTING:
787 case B_STATE_IMPORTING:
792 if (m[i].u.i.port[j])
794 /* search for port */
795 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
796 ll = l+msg.u.s.ports;
799 if (m[l].u.p.serial == m[i].u.i.port[j])
801 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
812 move(line>1?line:1, 1);
823 move(line>1?line:1, 1);
826 if (line+2 >= LINES) goto end;
827 /* show summary if no channels were shown */
828 if (show_interfaces<2 && ltee==0)
831 move(++line>1?line:1, 1);
834 if (m[i].u.i.l2link && m[i].u.i.block==0)
837 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
841 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
845 if (line+2 >= LINES) goto end;
851 i = msg.u.s.interfaces;
852 ii = i + msg.u.s.remotes;
855 /* show remote summary */
856 move(++line>1?line:1, 0);
858 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
864 if (line+2 >= LINES) goto end;
866 /* display calls (brief) */
870 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
871 ii = i+msg.u.s.epoints;
874 /* for each endpoint... */
877 move(++line>1?line:1, 0);
879 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
882 if (m[i].u.e.terminal[0])
886 addstr(m[i].u.e.terminal);
890 SPRINT(buffer, " %s", m[i].u.e.callerid);
895 SPRINT(buffer, "%s", m[i].u.e.dialing);
897 if (m[i].u.e.action[0])
902 addstr(m[i].u.e.action);
904 if (line+2 >= LINES) goto end;
909 j = msg.u.s.interfaces+msg.u.s.remotes;
910 jj = j+msg.u.s.joins;
913 /* for each call... */
914 move(++line>1?line:1, 0);
916 SPRINT(buffer, "(%d):", m[j].u.j.serial);
918 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
919 ii = i+msg.u.s.epoints;
922 /* for each endpoint... */
923 if (m[i].u.e.join == m[j].u.j.serial)
926 SPRINT(buffer, " (%d)", m[i].u.e.serial);
929 if (m[i].u.e.terminal[0])
933 addstr(m[i].u.e.terminal);
937 SPRINT(buffer, "-%s", m[i].u.e.callerid);
942 SPRINT(buffer, "%s", m[i].u.e.dialing);
948 if (line+2 >= LINES) goto end;
953 if (line+2 >= LINES) goto end;
955 /* display calls (structurd) */
958 /* show all ports with no epoint */
960 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
961 ii = i+msg.u.s.ports;
964 if (!m[i].u.p.epoint)
966 move(++line>1?line:1, 8);
967 if (line+2 >= LINES) goto end;
968 line = debug_port(&msg, m, line, i, 0);
969 if (line+2 >= LINES) goto end;
976 if (line+2 >= LINES) goto end;
978 /* show all epoints with no call */
980 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
981 ii = i+msg.u.s.epoints;
986 move(++line>1?line:1, 4);
987 if (line+2 >= LINES) goto end;
988 line = debug_epoint(&msg, m, line, i, 0);
989 if (line+2 >= LINES) goto end;
996 if (line+2 >= LINES) goto end;
1000 i = msg.u.s.interfaces+msg.u.s.remotes;
1001 ii = i+msg.u.s.joins;
1004 move(++line>1?line:1, 0);
1005 if (line+2 >= LINES) goto end;
1006 line = debug_join(&msg, m, line, i);
1007 if (line+2 >= LINES) goto end;
1013 if (line+2 >= LINES) goto end;
1022 move(line++>1?line-1:1, 0);
1024 hline(ACS_HLINE, COLS);
1027 l = logcur-(LINES-line-2);
1029 if (ll-l >= LOGLINES)
1033 move(line++>1?line-1:1, 0);
1034 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
1035 SCPY(buffer, logline[l % LOGLINES] + hoffset);
1038 if (COLS < (int)strlen(buffer))
1040 buffer[COLS-1] = '\0';
1055 /* display name/time */
1057 // hline(' ', COLS);
1060 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
1061 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
1066 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1067 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1068 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1071 /* displeay head line */
1074 hline(ACS_HLINE, COLS);
1078 SPRINT(buffer, "Offset +%d", offset);
1085 SPRINT(buffer, "H-Offset +%d", hoffset);
1092 hline(ACS_HLINE, COLS);
1097 SPRINT(buffer, "-> %s", enter_string);
1101 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1107 if (lastlines!=LINES || lastcols!=COLS)
1116 /* user input in enter mode */
1124 if (!enter_string[0])
1127 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1128 if (!!strncmp(enter_string, "interface", 10) &&
1129 !!strncmp(enter_string, "route", 6) &&
1130 !!strncmp(enter_string, "release ", 8) &&
1131 !!strncmp(enter_string, "block ", 6) &&
1132 !!strncmp(enter_string, "unblock ", 8) &&
1133 !!strncmp(enter_string, "load ", 5) &&
1134 !!strncmp(enter_string, "unload ", 7))
1136 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1137 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1138 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1139 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1140 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1141 SPRINT(logline[logcur++ % LOGLINES], "unblock/load <port> (unblock port for further calls, load if not loaded)");
1142 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1145 /* applend output to log window */
1146 SPRINT(buffer, "%s %s", argv[0], enter_string);
1147 fp = popen(buffer, "r");
1150 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1151 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1155 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1158 logline[logcur % LOGLINES][0] = '\0';
1159 enter_string[0] = '\0';
1162 if (ch>=32 && ch<=126)
1164 SCCAT(enter_string, ch);
1170 if (ch==8 || ch==127)
1172 if (enter_string[0])
1173 enter_string[strlen(enter_string)-1] = '\0';
1184 FD_ZERO(&select_rfds);
1185 FD_SET(0, &select_rfds);
1186 select_tv.tv_sec = 0;
1187 select_tv.tv_usec = 250000;
1188 select(1, &select_rfds, NULL, NULL, &select_tv);
1193 /* user input in normal mode */
1196 case 12: /* refresh */
1207 case 'i': /* toggle interface */
1209 if (show_interfaces > 3) show_interfaces = 0;
1212 case 'c': /* toggle calls */
1214 if (show_calls > 2) show_calls = 0;
1217 case 'l': /* toggle log */
1219 if (show_log > 1) show_log = 0;
1222 case '+': /* scroll down */
1226 case '-': /* scroll up */
1231 case '*': /* scroll right */
1235 case '/': /* scroll left */
1240 case 10: /* entermode */
1245 FD_ZERO(&select_rfds);
1246 FD_SET(0, &select_rfds);
1247 select_tv.tv_sec = 0;
1248 select_tv.tv_usec = 250000;
1249 select(1, &select_rfds, NULL, NULL, &select_tv);
1254 /* check for logfh */
1259 /* cleanup curses and exit */
1265 const char *admin_portinfo(int sock, int argc, char *argv[])
1267 struct admin_message msg,
1274 /* send state request command */
1275 memset(&msg, 0, sizeof(msg));
1276 msg.message = ADMIN_REQUEST_STATE;
1277 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1280 return("Broken pipe while sending command.");
1283 /* receive response */
1284 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1287 return("Broken pipe while receiving response.");
1290 if (msg.message != ADMIN_RESPONSE_STATE)
1293 return("Response not valid. Expecting state response.");
1295 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
1296 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
1301 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off)
1306 return("Broken pipe while receiving state infos.");
1308 if (len < num*(int)sizeof(struct admin_message))
1317 while(i < msg.u.s.interfaces)
1319 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE)
1323 return("Response not valid. Expecting interface information.");
1329 while(i < msg.u.s.remotes)
1331 if (m[j].message != ADMIN_RESPONSE_S_REMOTE)
1335 return("Response not valid. Expecting remote application information.");
1341 while(i < msg.u.s.joins)
1343 if (m[j].message != ADMIN_RESPONSE_S_JOIN)
1347 return("Response not valid. Expecting join information.");
1353 while(i < msg.u.s.epoints)
1355 if (m[j].message != ADMIN_RESPONSE_S_EPOINT)
1359 return("Response not valid. Expecting endpoint information.");
1365 while(i < msg.u.s.ports)
1367 if (m[j].message != ADMIN_RESPONSE_S_PORT)
1371 return("Response not valid. Expecting port information.");
1376 // now j is the number of message blocks
1378 /* output interfaces */
1380 ii = i + msg.u.s.interfaces;
1385 if (!!strcmp(argv[2], m[i].u.i.interface_name))
1391 printf("%s:\n", m[i].u.i.interface_name);
1392 if (m[i].u.i.portnum < 0)
1393 printf("\t port = unknown\n");
1395 printf("\t port = %d \"%s\"\n",m[i].u.i.portnum, m[i].u.i.portname);
1396 printf("\t extension = %s\n", (m[i].u.i.extension)?" yes":"no");
1397 if (m[i].u.i.block >= 2)
1399 printf("\t status = not loaded\n");
1403 printf("\t status = blocked\n");
1405 printf("\t status = unblocked\n");
1406 printf("\t mode = %s %s\n", (m[i].u.i.ntmode)?"NT-mode":"TE-mode", (m[i].u.i.ptp)?"ptp ":"ptmp");
1407 if (m[i].u.i.l1link < 0)
1408 printf("\t l1 link = unknown\n");
1410 printf("\t l1 link = %s\n", (m[i].u.i.l1link)?"up":"down");
1411 if (m[i].u.i.ptp || !m[i].u.i.ntmode)
1413 if (m[i].u.i.l2link < 0)
1414 printf("\t l2 link = unknown\n");
1416 printf("\t l2 link = %s\n", (m[i].u.i.l2link)?"up":"down");
1418 printf("\t usage = %d\n", m[i].u.i.use);
1431 * Send command and show error message.
1433 const char *admin_cmd(int sock, int mode, char *extension, char *number)
1435 static struct admin_message msg;
1437 /* send reload command */
1438 memset(&msg, 0, sizeof(msg));
1441 case MODE_INTERFACE:
1442 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1445 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1448 msg.message = ADMIN_REQUEST_CMD_DIAL;
1449 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1452 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1453 SCPY(msg.u.x.message, number);
1456 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1457 msg.u.x.portnum = atoi(number);
1461 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1462 msg.u.x.portnum = atoi(number);
1466 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1467 msg.u.x.portnum = atoi(number);
1472 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1473 return("Broken pipe while sending command.");
1475 /* receive response */
1476 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1477 return("Broken pipe while receiving response.");
1480 case MODE_INTERFACE:
1481 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1482 return("Response not valid.");
1485 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1486 return("Response not valid.");
1489 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1490 return("Response not valid.");
1493 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1494 return("Response not valid.");
1499 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1500 return("Response not valid.");
1504 /* process response */
1507 return(msg.u.x.message);
1509 printf("Command successfull.\n");
1517 #define GET_NOW() { \
1518 gettimeofday(&now_tv, &now_tz); \
1519 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1521 const char *admin_testcall(int sock, int argc, char *argv[])
1523 static struct admin_message msg;
1525 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1527 double timer = 0, now_d;
1528 unsigned int on = 1;
1529 struct timeval now_tv;
1530 struct timezone now_tz;
1532 printf("pid=%d\n", getpid()); fflush(stdout);
1536 if (!strcmp(argv[ar], "--setup-timeout"))
1540 return("Missing setup timeout value.\n");
1541 stimeout = atoi(argv[ar]);
1544 if (!strcmp(argv[ar], "--proceeding-timeout"))
1548 return("Missing proceeding timeout value.\n");
1549 ptimeout = atoi(argv[ar]);
1552 if (!strcmp(argv[ar], "--alerting-timeout"))
1556 return("Missing alerting timeout value.\n");
1557 atimeout = atoi(argv[ar]);
1560 if (!strcmp(argv[ar], "--connect-timeout"))
1564 return("Missing connect timeout value.\n");
1565 ctimeout = atoi(argv[ar]);
1573 /* send reload command */
1574 memset(&msg, 0, sizeof(msg));
1575 msg.message = ADMIN_CALL_SETUP;
1576 msg.u.call.present = 1;
1580 SCPY(msg.u.call.interface, argv[ar]);
1585 SCPY(msg.u.call.callerid, argv[ar]);
1590 SCPY(msg.u.call.dialing, argv[ar]);
1595 if (argv[ar][0] == 'r')
1596 msg.u.call.present = 0;
1599 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1600 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1601 msg.u.call.bc_info1 = 0;
1603 msg.u.call.exthlc = 0;
1605 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1607 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1610 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1611 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1615 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1616 if (msg.u.call.bc_info1 < 0)
1617 msg.u.call.bc_info1 = 0;
1619 msg.u.call.bc_info1 |= 0x80;
1623 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1624 if (msg.u.call.hlc < 0)
1627 msg.u.call.hlc |= 0x80;
1631 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1632 if (msg.u.call.exthlc < 0)
1633 msg.u.call.exthlc = 0;
1635 msg.u.call.exthlc |= 0x80;
1639 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1640 return("Broken pipe while sending command.");
1642 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1643 return("Failed to set socket into non-blocking IO.");
1648 timer = now_d + (double)stimeout;
1651 /* receive response */
1653 l = read(sock, &msg, sizeof(msg));
1656 if (errno == EWOULDBLOCK)
1663 printf("Timeout\n"); fflush(stdout);
1670 return("Broken pipe while receiving response.");
1672 if (l != sizeof(msg))
1673 return("Response has unexpected message size.");
1676 case ADMIN_CALL_SETUP_ACK:
1677 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1680 case ADMIN_CALL_PROCEEDING:
1681 printf("PROCEEDING\n"); fflush(stdout);
1685 timer = now_d + (double)ptimeout;
1689 case ADMIN_CALL_ALERTING:
1690 printf("ALERTING\n"); fflush(stdout);
1694 timer = now_d + (double)atimeout;
1698 case ADMIN_CALL_CONNECT:
1699 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1703 timer = now_d + (double)ctimeout;
1707 case ADMIN_CALL_NOTIFY:
1708 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1711 case ADMIN_CALL_DISCONNECT:
1712 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);
1715 case ADMIN_CALL_RELEASE:
1716 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);
1720 return("Response not valid.");
1723 printf("Call released.\n"); fflush(stdout);
1731 const char *admin_trace(int sock, int argc, char *argv[])
1733 static struct admin_message msg;
1737 if (argc > 2) if (!strcasecmp(argv[2], "help"))
1739 printf("Trace Help\n----------\n");
1740 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1741 printf("By default a complete trace is shown in detailed format.\n");
1742 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1743 printf("Use filter values to select specific trace messages.\n");
1744 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1745 printf("Filters:\n");
1746 printf(" category=<mask bits>\n");
1747 printf(" 0x01 = CH: channel object trace\n");
1748 printf(" 0x02 = EP: endpoint object trace\n");
1749 printf(" port=<mISDN port> select only given port for trace\n");
1750 printf(" interface=<interface name> select only given interface for trace\n");
1751 printf(" caller=<caller id> select only given caller id for trace\n");
1752 printf(" dialing=<number> select only given dialed number for trace\n");
1756 /* init trace request */
1757 memset(&msg, 0, sizeof(msg));
1758 msg.message = ADMIN_TRACE_REQUEST;
1759 msg.u.trace_req.detail = 3;
1760 msg.u.trace_req.port = -1;
1766 if (!strcasecmp(argv[i], "brief"))
1767 msg.u.trace_req.detail = 1;
1768 else if (!strcasecmp(argv[i], "short"))
1769 msg.u.trace_req.detail = 2;
1770 else if (!strncasecmp(argv[i], "category=", 9))
1771 msg.u.trace_req.category = atoi(argv[i]+9);
1772 else if (!strncasecmp(argv[i], "port=", 5))
1773 msg.u.trace_req.port = atoi(argv[i]+5);
1774 else if (!strncasecmp(argv[i], "interface=", 10))
1775 SCPY(msg.u.trace_req.interface, argv[i]+10);
1776 else if (!strncasecmp(argv[i], "caller=", 7))
1777 SCPY(msg.u.trace_req.caller, argv[i]+7);
1778 else if (!strncasecmp(argv[i], "dialing=", 8))
1779 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1780 else return("Invalid trace option, try 'trace help'.");
1785 /* send trace request */
1786 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1787 return("Broken pipe while sending trace request.");
1789 /* receive response */
1791 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1792 return("Broken pipe while receiving response.");
1794 if (msg.message != ADMIN_TRACE_RESPONSE)
1795 return("Response not valid.");
1797 printf("%s", msg.u.trace_rsp.text);
1805 int main(int argc, char *argv[])
1809 struct sockaddr_un sock_address;
1818 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1819 printf("state - View current states using graphical console output.\n");
1820 printf("portinfo - Get info of current ports.\n");
1821 printf("interface [<portname>] - Tell LCR to reload \"interface.conf\".\n");
1822 printf("route - Tell LCR to reload \"route.conf\".\n");
1823 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1824 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1825 printf("block <port> - Block given port.\n");
1826 printf("unblock/load <port> - Unblock given port.\n");
1827 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1828 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1829 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1830 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1831 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1832 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1833 printf(" -> Use 'trace help' to see filter description.\n");
1839 if (!(strcasecmp(argv[1],"state")))
1843 if (!(strcasecmp(argv[1],"portinfo")))
1845 mode = MODE_PORTINFO;
1847 if (!(strcasecmp(argv[1],"interface")))
1849 mode = MODE_INTERFACE;
1851 if (!(strcasecmp(argv[1],"route")))
1855 if (!(strcasecmp(argv[1],"dial")))
1861 if (!(strcasecmp(argv[1],"release")))
1865 mode = MODE_RELEASE;
1867 if (!(strcasecmp(argv[1],"unblock"))
1868 || !(strcasecmp(argv[1],"load")))
1872 mode = MODE_UNBLOCK;
1874 if (!(strcasecmp(argv[1],"block")))
1880 if (!(strcasecmp(argv[1],"unload")))
1886 if (!(strcasecmp(argv[1],"testcall")))
1890 mode = MODE_TESTCALL;
1892 if (!(strcasecmp(argv[1],"trace")))
1900 if (read_options() == 0) {
1906 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1908 fprintf(stderr, "Failed to create socket.\n");
1911 memset(&sock_address, 0, sizeof(sock_address));
1912 SPRINT(sock_address.sun_path, SOCKET_NAME, options.lock);
1913 sock_address.sun_family = PF_UNIX;
1914 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1917 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1925 ret = admin_state(sock, argv);
1929 ret = admin_portinfo(sock, argc, argv);
1932 case MODE_INTERFACE:
1934 ret = admin_cmd(sock, mode, NULL, NULL);
1938 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1945 ret = admin_cmd(sock, mode, NULL, argv[2]);
1949 ret = admin_testcall(sock, argc, argv);
1953 ret = admin_trace(sock, argc, argv);
1958 /* now we say good bye */
1961 // if (!strncasecmp(ret, "Broken Pipe", 11))
1963 printf("%s\n", ret);