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)?" exten":"");
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", m[i].u.i.interface_name);
684 SPRINT(buffer, "(port %d: %s)", m[i].u.i.portnum, m[i].u.i.portname);
687 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":"");
693 SPRINT(buffer, " use:%d", m[i].u.i.use);
695 if (m[i].u.i.ptp || !m[i].u.i.ntmode)
697 color((m[i].u.i.l2link > 0)?green:red);
698 if (m[i].u.i.l2link < 0)
701 addstr((m[i].u.i.l2link)?" L2 UP":" L2 down");
709 if (m[i].u.i.l2mask[j>>3] & (1 << (j&7)))
711 SPRINT(buffer, "%s%d", k?",":" TEI(", j);
720 color((m[i].u.i.l1link > 0)?green:blue);
721 if (m[i].u.i.l1link < 0)
724 addstr((m[i].u.i.l1link)?" L1 UP":" L1 down");
740 if (m[i].u.i.slip_tx || m[i].u.i.slip_rx)
743 SPRINT(buffer, " SLIP(tx:%d rx:%d)", m[i].u.i.slip_tx, m[i].u.i.slip_rx);
751 if (line+2 >= LINES) goto end;
753 if (show_interfaces > 1)
757 jj = m[i].u.i.channels;
760 /* show all channels */
761 if (show_interfaces>2 || m[i].u.i.busy[j]>0)
764 /* show left side / right side */
765 if ((k & 1) && (COLS > 70))
767 move(line>1?line:1,4+((COLS-4)/2));
770 move(++line>1?line:1, 1);
777 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
779 SPRINT(buffer,"B%2d: ", j+1);
780 if (m[i].u.i.mode[j] == B_MODE_HDLC)
781 SCAT(buffer,"HDLC ");
783 switch(m[i].u.i.busy[j])
786 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block)
796 case B_STATE_ACTIVATING:
804 case B_STATE_DEACTIVATING:
808 case B_STATE_EXPORTING:
816 case B_STATE_IMPORTING:
821 if (m[i].u.i.port[j])
823 /* search for port */
824 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
825 ll = l+msg.u.s.ports;
828 if (m[l].u.p.serial == m[i].u.i.port[j])
830 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
841 move(line>1?line:1, 1);
852 move(line>1?line:1, 1);
855 if (line+2 >= LINES) goto end;
856 /* show summary if no channels were shown */
857 if (show_interfaces<2 && ltee==0)
860 move(++line>1?line:1, 1);
863 if (m[i].u.i.l2link && m[i].u.i.block==0)
866 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
870 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
874 if (line+2 >= LINES) goto end;
880 i = msg.u.s.interfaces;
881 ii = i + msg.u.s.remotes;
884 /* show remote summary */
885 move(++line>1?line:1, 0);
887 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
893 if (line+2 >= LINES) goto end;
895 /* display calls (brief) */
899 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
900 ii = i+msg.u.s.epoints;
903 /* for each endpoint... */
906 move(++line>1?line:1, 0);
908 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
911 if (m[i].u.e.terminal[0])
915 addstr(m[i].u.e.terminal);
919 SPRINT(buffer, " %s", m[i].u.e.callerid);
924 SPRINT(buffer, "%s", m[i].u.e.dialing);
926 if (m[i].u.e.action[0])
931 addstr(m[i].u.e.action);
933 if (line+2 >= LINES) goto end;
938 j = msg.u.s.interfaces+msg.u.s.remotes;
939 jj = j+msg.u.s.joins;
942 /* for each call... */
943 move(++line>1?line:1, 0);
945 SPRINT(buffer, "(%d):", m[j].u.j.serial);
947 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
948 ii = i+msg.u.s.epoints;
951 /* for each endpoint... */
952 if (m[i].u.e.join == m[j].u.j.serial)
955 SPRINT(buffer, " (%d)", m[i].u.e.serial);
958 if (m[i].u.e.terminal[0])
962 addstr(m[i].u.e.terminal);
966 SPRINT(buffer, "-%s", m[i].u.e.callerid);
971 SPRINT(buffer, "%s", m[i].u.e.dialing);
977 if (line+2 >= LINES) goto end;
982 if (line+2 >= LINES) goto end;
984 /* display calls (structurd) */
987 /* show all ports with no epoint */
989 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
990 ii = i+msg.u.s.ports;
993 if (!m[i].u.p.epoint)
995 move(++line>1?line:1, 8);
996 if (line+2 >= LINES) goto end;
997 line = debug_port(&msg, m, line, i, 0);
998 if (line+2 >= LINES) goto end;
1005 if (line+2 >= LINES) goto end;
1007 /* show all epoints with no call */
1009 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
1010 ii = i+msg.u.s.epoints;
1015 move(++line>1?line:1, 4);
1016 if (line+2 >= LINES) goto end;
1017 line = debug_epoint(&msg, m, line, i, 0);
1018 if (line+2 >= LINES) goto end;
1025 if (line+2 >= LINES) goto end;
1027 /* show all joins */
1029 i = msg.u.s.interfaces+msg.u.s.remotes;
1030 ii = i+msg.u.s.joins;
1033 move(++line>1?line:1, 0);
1034 if (line+2 >= LINES) goto end;
1035 line = debug_join(&msg, m, line, i);
1036 if (line+2 >= LINES) goto end;
1042 if (line+2 >= LINES) goto end;
1051 move(line++>1?line-1:1, 0);
1053 hline(ACS_HLINE, COLS);
1056 l = logcur-(LINES-line-2);
1058 if (ll-l >= LOGLINES)
1062 move(line++>1?line-1:1, 0);
1063 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
1064 SCPY(buffer, logline[l % LOGLINES] + hoffset);
1067 if (COLS < (int)strlen(buffer))
1069 buffer[COLS-1] = '\0';
1084 /* display name/time */
1086 // hline(' ', COLS);
1089 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
1090 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
1095 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1096 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1097 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1100 /* displeay head line */
1103 hline(ACS_HLINE, COLS);
1107 SPRINT(buffer, "Offset +%d", offset);
1114 SPRINT(buffer, "H-Offset +%d", hoffset);
1121 hline(ACS_HLINE, COLS);
1126 SPRINT(buffer, "-> %s", enter_string);
1130 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1136 if (lastlines!=LINES || lastcols!=COLS)
1145 /* user input in enter mode */
1153 if (!enter_string[0])
1156 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1157 if (!!strncmp(enter_string, "interface", 10) &&
1158 !!strncmp(enter_string, "route", 6) &&
1159 !!strncmp(enter_string, "release ", 8) &&
1160 !!strncmp(enter_string, "block ", 6) &&
1161 !!strncmp(enter_string, "unblock ", 8) &&
1162 !!strncmp(enter_string, "load ", 5) &&
1163 !!strncmp(enter_string, "unload ", 7))
1165 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1166 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1167 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1168 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1169 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1170 SPRINT(logline[logcur++ % LOGLINES], "unblock/load <port> (unblock port for further calls, load if not loaded)");
1171 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1174 /* applend output to log window */
1175 SPRINT(buffer, "%s %s", argv[0], enter_string);
1176 fp = popen(buffer, "r");
1179 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1180 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1184 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1187 logline[logcur % LOGLINES][0] = '\0';
1188 enter_string[0] = '\0';
1191 if (ch>=32 && ch<=126)
1193 SCCAT(enter_string, ch);
1199 if (ch==8 || ch==127)
1201 if (enter_string[0])
1202 enter_string[strlen(enter_string)-1] = '\0';
1213 FD_ZERO(&select_rfds);
1214 FD_SET(0, &select_rfds);
1215 select_tv.tv_sec = 0;
1216 select_tv.tv_usec = 250000;
1217 select(1, &select_rfds, NULL, NULL, &select_tv);
1222 /* user input in normal mode */
1225 case 12: /* refresh */
1236 case 'i': /* toggle interface */
1238 if (show_interfaces > 3) show_interfaces = 0;
1241 case 'c': /* toggle calls */
1243 if (show_calls > 2) show_calls = 0;
1246 case 'l': /* toggle log */
1248 if (show_log > 1) show_log = 0;
1251 case '+': /* scroll down */
1255 case '-': /* scroll up */
1260 case '*': /* scroll right */
1264 case '/': /* scroll left */
1269 case 10: /* entermode */
1274 FD_ZERO(&select_rfds);
1275 FD_SET(0, &select_rfds);
1276 select_tv.tv_sec = 0;
1277 select_tv.tv_usec = 250000;
1278 select(1, &select_rfds, NULL, NULL, &select_tv);
1283 /* check for logfh */
1288 /* cleanup curses and exit */
1294 const char *admin_portinfo(int sock, int argc, char *argv[])
1296 struct admin_message msg,
1303 /* send state request command */
1304 memset(&msg, 0, sizeof(msg));
1305 msg.message = ADMIN_REQUEST_STATE;
1306 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1309 return("Broken pipe while sending command.");
1312 /* receive response */
1313 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1316 return("Broken pipe while receiving response.");
1319 if (msg.message != ADMIN_RESPONSE_STATE)
1322 return("Response not valid. Expecting state response.");
1324 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
1325 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
1330 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off)
1335 return("Broken pipe while receiving state infos.");
1337 if (len < num*(int)sizeof(struct admin_message))
1346 while(i < msg.u.s.interfaces)
1348 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE)
1352 return("Response not valid. Expecting interface information.");
1358 while(i < msg.u.s.remotes)
1360 if (m[j].message != ADMIN_RESPONSE_S_REMOTE)
1364 return("Response not valid. Expecting remote application information.");
1370 while(i < msg.u.s.joins)
1372 if (m[j].message != ADMIN_RESPONSE_S_JOIN)
1376 return("Response not valid. Expecting join information.");
1382 while(i < msg.u.s.epoints)
1384 if (m[j].message != ADMIN_RESPONSE_S_EPOINT)
1388 return("Response not valid. Expecting endpoint information.");
1394 while(i < msg.u.s.ports)
1396 if (m[j].message != ADMIN_RESPONSE_S_PORT)
1400 return("Response not valid. Expecting port information.");
1405 // now j is the number of message blocks
1407 /* output interfaces */
1409 ii = i + msg.u.s.interfaces;
1414 if (!!strcmp(argv[2], m[i].u.i.interface_name))
1420 printf("%s:\n", m[i].u.i.interface_name);
1421 if (m[i].u.i.portnum < 0)
1422 printf("\t port = unknown\n");
1424 printf("\t port = %d \"%s\"\n",m[i].u.i.portnum, m[i].u.i.portname);
1425 printf("\t extension = %s\n", (m[i].u.i.extension)?"yes":"no");
1426 if (m[i].u.i.block >= 2)
1428 printf("\t status = not loaded\n");
1432 printf("\t status = blocked\n");
1434 printf("\t status = unblocked\n");
1435 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":"");
1436 if (m[i].u.i.l1link < 0)
1437 printf("\t l1 link = unknown\n");
1439 printf("\t l1 link = %s\n", (m[i].u.i.l1link)?"up":"down");
1440 if (m[i].u.i.ptp || !m[i].u.i.ntmode)
1442 if (m[i].u.i.l2link < 0)
1443 printf("\t l2 link = unknown\n");
1445 printf("\t l2 link = %s\n", (m[i].u.i.l2link)?"up":"down");
1447 printf("\t usage = %d\n", m[i].u.i.use);
1460 * Send command and show error message.
1462 const char *admin_cmd(int sock, int mode, char *extension, char *number)
1464 static struct admin_message msg;
1466 /* send reload command */
1467 memset(&msg, 0, sizeof(msg));
1470 case MODE_INTERFACE:
1471 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1474 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1477 msg.message = ADMIN_REQUEST_CMD_DIAL;
1478 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1481 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1482 SCPY(msg.u.x.message, number);
1485 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1486 msg.u.x.portnum = atoi(number);
1490 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1491 msg.u.x.portnum = atoi(number);
1495 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1496 msg.u.x.portnum = atoi(number);
1501 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1502 return("Broken pipe while sending command.");
1504 /* receive response */
1505 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1506 return("Broken pipe while receiving response.");
1509 case MODE_INTERFACE:
1510 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1511 return("Response not valid.");
1514 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1515 return("Response not valid.");
1518 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1519 return("Response not valid.");
1522 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1523 return("Response not valid.");
1528 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1529 return("Response not valid.");
1533 /* process response */
1536 return(msg.u.x.message);
1538 printf("Command successfull.\n");
1546 #define GET_NOW() { \
1547 gettimeofday(&now_tv, &now_tz); \
1548 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1550 const char *admin_testcall(int sock, int argc, char *argv[])
1552 static struct admin_message msg;
1554 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1556 double timer = 0, now_d;
1557 unsigned int on = 1;
1558 struct timeval now_tv;
1559 struct timezone now_tz;
1561 printf("pid=%d\n", getpid()); fflush(stdout);
1565 if (!strcmp(argv[ar], "--setup-timeout"))
1569 return("Missing setup timeout value.\n");
1570 stimeout = atoi(argv[ar]);
1573 if (!strcmp(argv[ar], "--proceeding-timeout"))
1577 return("Missing proceeding timeout value.\n");
1578 ptimeout = atoi(argv[ar]);
1581 if (!strcmp(argv[ar], "--alerting-timeout"))
1585 return("Missing alerting timeout value.\n");
1586 atimeout = atoi(argv[ar]);
1589 if (!strcmp(argv[ar], "--connect-timeout"))
1593 return("Missing connect timeout value.\n");
1594 ctimeout = atoi(argv[ar]);
1602 /* send reload command */
1603 memset(&msg, 0, sizeof(msg));
1604 msg.message = ADMIN_CALL_SETUP;
1605 msg.u.call.present = 1;
1609 SCPY(msg.u.call.interface, argv[ar]);
1614 SCPY(msg.u.call.callerid, argv[ar]);
1619 SCPY(msg.u.call.dialing, argv[ar]);
1624 if (argv[ar][0] == 'r')
1625 msg.u.call.present = 0;
1628 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1629 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1630 msg.u.call.bc_info1 = 0;
1632 msg.u.call.exthlc = 0;
1634 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1636 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1639 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1640 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1644 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1645 if (msg.u.call.bc_info1 < 0)
1646 msg.u.call.bc_info1 = 0;
1648 msg.u.call.bc_info1 |= 0x80;
1652 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1653 if (msg.u.call.hlc < 0)
1656 msg.u.call.hlc |= 0x80;
1660 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1661 if (msg.u.call.exthlc < 0)
1662 msg.u.call.exthlc = 0;
1664 msg.u.call.exthlc |= 0x80;
1668 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1669 return("Broken pipe while sending command.");
1671 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1672 return("Failed to set socket into non-blocking IO.");
1677 timer = now_d + (double)stimeout;
1680 /* receive response */
1682 l = read(sock, &msg, sizeof(msg));
1685 if (errno == EWOULDBLOCK)
1692 printf("Timeout\n"); fflush(stdout);
1699 return("Broken pipe while receiving response.");
1701 if (l != sizeof(msg))
1702 return("Response has unexpected message size.");
1705 case ADMIN_CALL_SETUP_ACK:
1706 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1709 case ADMIN_CALL_PROCEEDING:
1710 printf("PROCEEDING\n"); fflush(stdout);
1714 timer = now_d + (double)ptimeout;
1718 case ADMIN_CALL_ALERTING:
1719 printf("ALERTING\n"); fflush(stdout);
1723 timer = now_d + (double)atimeout;
1727 case ADMIN_CALL_CONNECT:
1728 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1732 timer = now_d + (double)ctimeout;
1736 case ADMIN_CALL_NOTIFY:
1737 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1740 case ADMIN_CALL_DISCONNECT:
1741 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);
1744 case ADMIN_CALL_RELEASE:
1745 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);
1749 return("Response not valid.");
1752 printf("Call released.\n"); fflush(stdout);
1760 const char *admin_trace(int sock, int argc, char *argv[])
1762 static struct admin_message msg;
1766 if (argc > 2) if (!strcasecmp(argv[2], "help"))
1768 printf("Trace Help\n----------\n");
1769 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1770 printf("By default a complete trace is shown in detailed format.\n");
1771 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1772 printf("Use filter values to select specific trace messages.\n");
1773 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1774 printf("Filters:\n");
1775 printf(" category=<mask bits>\n");
1776 printf(" 0x01 = CH: channel object trace\n");
1777 printf(" 0x02 = EP: endpoint object trace\n");
1778 printf(" port=<mISDN port> select only given port for trace\n");
1779 printf(" interface=<interface name> select only given interface for trace\n");
1780 printf(" caller=<caller id> select only given caller id for trace\n");
1781 printf(" dialing=<number> select only given dialed number for trace\n");
1785 /* init trace request */
1786 memset(&msg, 0, sizeof(msg));
1787 msg.message = ADMIN_TRACE_REQUEST;
1788 msg.u.trace_req.detail = 3;
1789 msg.u.trace_req.port = -1;
1795 if (!strcasecmp(argv[i], "brief"))
1796 msg.u.trace_req.detail = 1;
1797 else if (!strcasecmp(argv[i], "short"))
1798 msg.u.trace_req.detail = 2;
1799 else if (!strncasecmp(argv[i], "category=", 9))
1800 msg.u.trace_req.category = atoi(argv[i]+9);
1801 else if (!strncasecmp(argv[i], "port=", 5))
1802 msg.u.trace_req.port = atoi(argv[i]+5);
1803 else if (!strncasecmp(argv[i], "interface=", 10))
1804 SCPY(msg.u.trace_req.interface, argv[i]+10);
1805 else if (!strncasecmp(argv[i], "caller=", 7))
1806 SCPY(msg.u.trace_req.caller, argv[i]+7);
1807 else if (!strncasecmp(argv[i], "dialing=", 8))
1808 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1809 else return("Invalid trace option, try 'trace help'.");
1814 /* send trace request */
1815 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1816 return("Broken pipe while sending trace request.");
1818 /* receive response */
1820 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1821 return("Broken pipe while receiving response.");
1823 if (msg.message != ADMIN_TRACE_RESPONSE)
1824 return("Response not valid.");
1826 printf("%s", msg.u.trace_rsp.text);
1834 int main(int argc, char *argv[])
1838 struct sockaddr_un sock_address;
1839 const char *ret = "invalid mode";
1847 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1848 printf("state - View current states using graphical console output.\n");
1849 printf("portinfo - Get info of current ports.\n");
1850 printf("interface [<portname>] - Tell LCR to reload \"interface.conf\".\n");
1851 printf("route - Tell LCR to reload \"route.conf\".\n");
1852 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1853 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1854 printf("block <port> - Block given port.\n");
1855 printf("unblock/load <port> - Unblock given port.\n");
1856 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1857 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1858 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1859 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1860 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1861 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1862 printf(" -> Use 'trace help' to see filter description.\n");
1868 if (!(strcasecmp(argv[1],"state")))
1872 if (!(strcasecmp(argv[1],"portinfo")))
1874 mode = MODE_PORTINFO;
1876 if (!(strcasecmp(argv[1],"interface")))
1878 mode = MODE_INTERFACE;
1880 if (!(strcasecmp(argv[1],"route")))
1884 if (!(strcasecmp(argv[1],"dial")))
1890 if (!(strcasecmp(argv[1],"release")))
1894 mode = MODE_RELEASE;
1896 if (!(strcasecmp(argv[1],"unblock"))
1897 || !(strcasecmp(argv[1],"load")))
1901 mode = MODE_UNBLOCK;
1903 if (!(strcasecmp(argv[1],"block")))
1909 if (!(strcasecmp(argv[1],"unload")))
1915 if (!(strcasecmp(argv[1],"testcall")))
1919 mode = MODE_TESTCALL;
1921 if (!(strcasecmp(argv[1],"trace")))
1929 if (read_options() == 0) {
1935 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1937 fprintf(stderr, "Failed to create socket.\n");
1940 memset(&sock_address, 0, sizeof(sock_address));
1941 SPRINT(sock_address.sun_path, SOCKET_NAME, options.lock);
1942 sock_address.sun_family = PF_UNIX;
1943 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1946 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1954 ret = admin_state(sock, argv);
1958 ret = admin_portinfo(sock, argc, argv);
1961 case MODE_INTERFACE:
1963 ret = admin_cmd(sock, mode, NULL, NULL);
1967 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1974 ret = admin_cmd(sock, mode, NULL, argv[2]);
1978 ret = admin_testcall(sock, argc, argv);
1982 ret = admin_trace(sock, argc, argv);
1987 /* now we say good bye */
1990 // if (!strncasecmp(ret, "Broken Pipe", 11))
1992 printf("%s\n", ret);