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", 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-mode":"TE-mode", (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)
699 addstr(" L2 unknown");
701 addstr((m[i].u.i.l2link)?" L2 UP":" L2 down");
703 color((m[i].u.i.l1link > 0)?green:blue);
704 if (m[i].u.i.l1link < 0)
705 addstr(" L1 unknown");
707 addstr((m[i].u.i.l1link)?" L1 ACTIVE":" L1 inactive");
723 if (m[i].u.i.slip_tx || m[i].u.i.slip_rx)
726 SPRINT(buffer, " SLIP(tx:%d rx:%d)", m[i].u.i.slip_tx, m[i].u.i.slip_rx);
734 if (line+2 >= LINES) goto end;
736 if (show_interfaces > 1)
740 jj = m[i].u.i.channels;
743 /* show all channels */
744 if (show_interfaces>2 || m[i].u.i.busy[j]>0)
747 /* show left side / right side */
748 if ((k & 1) && (COLS > 70))
750 move(line>1?line:1,4+((COLS-4)/2));
753 move(++line>1?line:1, 1);
760 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
762 SPRINT(buffer,"B%2d: ", j+1);
763 if (m[i].u.i.mode[j] == B_MODE_HDLC)
764 SCAT(buffer,"HDLC ");
766 switch(m[i].u.i.busy[j])
769 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block)
779 case B_STATE_ACTIVATING:
787 case B_STATE_DEACTIVATING:
791 case B_STATE_EXPORTING:
799 case B_STATE_IMPORTING:
804 if (m[i].u.i.port[j])
806 /* search for port */
807 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
808 ll = l+msg.u.s.ports;
811 if (m[l].u.p.serial == m[i].u.i.port[j])
813 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
824 move(line>1?line:1, 1);
835 move(line>1?line:1, 1);
838 if (line+2 >= LINES) goto end;
839 /* show summary if no channels were shown */
840 if (show_interfaces<2 && ltee==0)
843 move(++line>1?line:1, 1);
846 if (m[i].u.i.l2link && m[i].u.i.block==0)
849 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
853 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
857 if (line+2 >= LINES) goto end;
863 i = msg.u.s.interfaces;
864 ii = i + msg.u.s.remotes;
867 /* show remote summary */
868 move(++line>1?line:1, 0);
870 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
876 if (line+2 >= LINES) goto end;
878 /* display calls (brief) */
882 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
883 ii = i+msg.u.s.epoints;
886 /* for each endpoint... */
889 move(++line>1?line:1, 0);
891 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
894 if (m[i].u.e.terminal[0])
898 addstr(m[i].u.e.terminal);
902 SPRINT(buffer, " %s", m[i].u.e.callerid);
907 SPRINT(buffer, "%s", m[i].u.e.dialing);
909 if (m[i].u.e.action[0])
914 addstr(m[i].u.e.action);
916 if (line+2 >= LINES) goto end;
921 j = msg.u.s.interfaces+msg.u.s.remotes;
922 jj = j+msg.u.s.joins;
925 /* for each call... */
926 move(++line>1?line:1, 0);
928 SPRINT(buffer, "(%d):", m[j].u.j.serial);
930 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
931 ii = i+msg.u.s.epoints;
934 /* for each endpoint... */
935 if (m[i].u.e.join == m[j].u.j.serial)
938 SPRINT(buffer, " (%d)", m[i].u.e.serial);
941 if (m[i].u.e.terminal[0])
945 addstr(m[i].u.e.terminal);
949 SPRINT(buffer, "-%s", m[i].u.e.callerid);
954 SPRINT(buffer, "%s", m[i].u.e.dialing);
960 if (line+2 >= LINES) goto end;
965 if (line+2 >= LINES) goto end;
967 /* display calls (structurd) */
970 /* show all ports with no epoint */
972 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
973 ii = i+msg.u.s.ports;
976 if (!m[i].u.p.epoint)
978 move(++line>1?line:1, 8);
979 if (line+2 >= LINES) goto end;
980 line = debug_port(&msg, m, line, i, 0);
981 if (line+2 >= LINES) goto end;
988 if (line+2 >= LINES) goto end;
990 /* show all epoints with no call */
992 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
993 ii = i+msg.u.s.epoints;
998 move(++line>1?line:1, 4);
999 if (line+2 >= LINES) goto end;
1000 line = debug_epoint(&msg, m, line, i, 0);
1001 if (line+2 >= LINES) goto end;
1008 if (line+2 >= LINES) goto end;
1010 /* show all joins */
1012 i = msg.u.s.interfaces+msg.u.s.remotes;
1013 ii = i+msg.u.s.joins;
1016 move(++line>1?line:1, 0);
1017 if (line+2 >= LINES) goto end;
1018 line = debug_join(&msg, m, line, i);
1019 if (line+2 >= LINES) goto end;
1025 if (line+2 >= LINES) goto end;
1034 move(line++>1?line-1:1, 0);
1036 hline(ACS_HLINE, COLS);
1039 l = logcur-(LINES-line-2);
1041 if (ll-l >= LOGLINES)
1045 move(line++>1?line-1:1, 0);
1046 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
1047 SCPY(buffer, logline[l % LOGLINES] + hoffset);
1050 if (COLS < (int)strlen(buffer))
1052 buffer[COLS-1] = '\0';
1067 /* display name/time */
1069 // hline(' ', COLS);
1072 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
1073 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
1078 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1079 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1080 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1083 /* displeay head line */
1086 hline(ACS_HLINE, COLS);
1090 SPRINT(buffer, "Offset +%d", offset);
1097 SPRINT(buffer, "H-Offset +%d", hoffset);
1104 hline(ACS_HLINE, COLS);
1109 SPRINT(buffer, "-> %s", enter_string);
1113 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1119 if (lastlines!=LINES || lastcols!=COLS)
1128 /* user input in enter mode */
1136 if (!enter_string[0])
1139 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1140 if (!!strncmp(enter_string, "interface", 10) &&
1141 !!strncmp(enter_string, "route", 6) &&
1142 !!strncmp(enter_string, "release ", 8) &&
1143 !!strncmp(enter_string, "block ", 6) &&
1144 !!strncmp(enter_string, "unblock ", 8) &&
1145 !!strncmp(enter_string, "load ", 5) &&
1146 !!strncmp(enter_string, "unload ", 7))
1148 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1149 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1150 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1151 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1152 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1153 SPRINT(logline[logcur++ % LOGLINES], "unblock/load <port> (unblock port for further calls, load if not loaded)");
1154 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1157 /* applend output to log window */
1158 SPRINT(buffer, "%s %s", argv[0], enter_string);
1159 fp = popen(buffer, "r");
1162 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1163 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1167 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1170 logline[logcur % LOGLINES][0] = '\0';
1171 enter_string[0] = '\0';
1174 if (ch>=32 && ch<=126)
1176 SCCAT(enter_string, ch);
1182 if (ch==8 || ch==127)
1184 if (enter_string[0])
1185 enter_string[strlen(enter_string)-1] = '\0';
1196 FD_ZERO(&select_rfds);
1197 FD_SET(0, &select_rfds);
1198 select_tv.tv_sec = 0;
1199 select_tv.tv_usec = 250000;
1200 select(1, &select_rfds, NULL, NULL, &select_tv);
1205 /* user input in normal mode */
1208 case 12: /* refresh */
1219 case 'i': /* toggle interface */
1221 if (show_interfaces > 3) show_interfaces = 0;
1224 case 'c': /* toggle calls */
1226 if (show_calls > 2) show_calls = 0;
1229 case 'l': /* toggle log */
1231 if (show_log > 1) show_log = 0;
1234 case '+': /* scroll down */
1238 case '-': /* scroll up */
1243 case '*': /* scroll right */
1247 case '/': /* scroll left */
1252 case 10: /* entermode */
1257 FD_ZERO(&select_rfds);
1258 FD_SET(0, &select_rfds);
1259 select_tv.tv_sec = 0;
1260 select_tv.tv_usec = 250000;
1261 select(1, &select_rfds, NULL, NULL, &select_tv);
1266 /* check for logfh */
1271 /* cleanup curses and exit */
1277 const char *admin_portinfo(int sock, int argc, char *argv[])
1279 struct admin_message msg,
1286 /* send state request command */
1287 memset(&msg, 0, sizeof(msg));
1288 msg.message = ADMIN_REQUEST_STATE;
1289 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1292 return("Broken pipe while sending command.");
1295 /* receive response */
1296 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1299 return("Broken pipe while receiving response.");
1302 if (msg.message != ADMIN_RESPONSE_STATE)
1305 return("Response not valid. Expecting state response.");
1307 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
1308 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
1313 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off)
1318 return("Broken pipe while receiving state infos.");
1320 if (len < num*(int)sizeof(struct admin_message))
1329 while(i < msg.u.s.interfaces)
1331 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE)
1335 return("Response not valid. Expecting interface information.");
1341 while(i < msg.u.s.remotes)
1343 if (m[j].message != ADMIN_RESPONSE_S_REMOTE)
1347 return("Response not valid. Expecting remote application information.");
1353 while(i < msg.u.s.joins)
1355 if (m[j].message != ADMIN_RESPONSE_S_JOIN)
1359 return("Response not valid. Expecting join information.");
1365 while(i < msg.u.s.epoints)
1367 if (m[j].message != ADMIN_RESPONSE_S_EPOINT)
1371 return("Response not valid. Expecting endpoint information.");
1377 while(i < msg.u.s.ports)
1379 if (m[j].message != ADMIN_RESPONSE_S_PORT)
1383 return("Response not valid. Expecting port information.");
1388 // now j is the number of message blocks
1390 /* output interfaces */
1392 ii = i + msg.u.s.interfaces;
1397 if (!!strcmp(argv[2], m[i].u.i.interface_name))
1403 printf("%s:\n", m[i].u.i.interface_name);
1404 if (m[i].u.i.portnum < 0)
1405 printf("\t port = unknown\n");
1407 printf("\t port = %d \"%s\"\n",m[i].u.i.portnum, m[i].u.i.portname);
1408 printf("\t extension = %s\n", (m[i].u.i.extension)?"yes":"no");
1409 if (m[i].u.i.block >= 2)
1411 printf("\t status = not loaded\n");
1415 printf("\t status = blocked\n");
1417 printf("\t status = unblocked\n");
1418 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":"");
1419 if (m[i].u.i.l1link < 0)
1420 printf("\t l1 link = unknown\n");
1422 printf("\t l1 link = %s\n", (m[i].u.i.l1link)?"up":"down");
1423 if (m[i].u.i.ptp || !m[i].u.i.ntmode)
1425 if (m[i].u.i.l2link < 0)
1426 printf("\t l2 link = unknown\n");
1428 printf("\t l2 link = %s\n", (m[i].u.i.l2link)?"up":"down");
1430 printf("\t usage = %d\n", m[i].u.i.use);
1443 * Send command and show error message.
1445 const char *admin_cmd(int sock, int mode, char *extension, char *number)
1447 static struct admin_message msg;
1449 /* send reload command */
1450 memset(&msg, 0, sizeof(msg));
1453 case MODE_INTERFACE:
1454 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1457 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1460 msg.message = ADMIN_REQUEST_CMD_DIAL;
1461 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1464 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1465 SCPY(msg.u.x.message, number);
1468 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1469 msg.u.x.portnum = atoi(number);
1473 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1474 msg.u.x.portnum = atoi(number);
1478 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1479 msg.u.x.portnum = atoi(number);
1484 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1485 return("Broken pipe while sending command.");
1487 /* receive response */
1488 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1489 return("Broken pipe while receiving response.");
1492 case MODE_INTERFACE:
1493 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1494 return("Response not valid.");
1497 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1498 return("Response not valid.");
1501 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1502 return("Response not valid.");
1505 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1506 return("Response not valid.");
1511 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1512 return("Response not valid.");
1516 /* process response */
1519 return(msg.u.x.message);
1521 printf("Command successfull.\n");
1529 #define GET_NOW() { \
1530 gettimeofday(&now_tv, &now_tz); \
1531 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1533 const char *admin_testcall(int sock, int argc, char *argv[])
1535 static struct admin_message msg;
1537 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1539 double timer = 0, now_d;
1540 unsigned int on = 1;
1541 struct timeval now_tv;
1542 struct timezone now_tz;
1544 printf("pid=%d\n", getpid()); fflush(stdout);
1548 if (!strcmp(argv[ar], "--setup-timeout"))
1552 return("Missing setup timeout value.\n");
1553 stimeout = atoi(argv[ar]);
1556 if (!strcmp(argv[ar], "--proceeding-timeout"))
1560 return("Missing proceeding timeout value.\n");
1561 ptimeout = atoi(argv[ar]);
1564 if (!strcmp(argv[ar], "--alerting-timeout"))
1568 return("Missing alerting timeout value.\n");
1569 atimeout = atoi(argv[ar]);
1572 if (!strcmp(argv[ar], "--connect-timeout"))
1576 return("Missing connect timeout value.\n");
1577 ctimeout = atoi(argv[ar]);
1585 /* send reload command */
1586 memset(&msg, 0, sizeof(msg));
1587 msg.message = ADMIN_CALL_SETUP;
1588 msg.u.call.present = 1;
1592 SCPY(msg.u.call.interface, argv[ar]);
1597 SCPY(msg.u.call.callerid, argv[ar]);
1602 SCPY(msg.u.call.dialing, argv[ar]);
1607 if (argv[ar][0] == 'r')
1608 msg.u.call.present = 0;
1611 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1612 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1613 msg.u.call.bc_info1 = 0;
1615 msg.u.call.exthlc = 0;
1617 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1619 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1622 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1623 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1627 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1628 if (msg.u.call.bc_info1 < 0)
1629 msg.u.call.bc_info1 = 0;
1631 msg.u.call.bc_info1 |= 0x80;
1635 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1636 if (msg.u.call.hlc < 0)
1639 msg.u.call.hlc |= 0x80;
1643 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1644 if (msg.u.call.exthlc < 0)
1645 msg.u.call.exthlc = 0;
1647 msg.u.call.exthlc |= 0x80;
1651 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1652 return("Broken pipe while sending command.");
1654 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1655 return("Failed to set socket into non-blocking IO.");
1660 timer = now_d + (double)stimeout;
1663 /* receive response */
1665 l = read(sock, &msg, sizeof(msg));
1668 if (errno == EWOULDBLOCK)
1675 printf("Timeout\n"); fflush(stdout);
1682 return("Broken pipe while receiving response.");
1684 if (l != sizeof(msg))
1685 return("Response has unexpected message size.");
1688 case ADMIN_CALL_SETUP_ACK:
1689 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1692 case ADMIN_CALL_PROCEEDING:
1693 printf("PROCEEDING\n"); fflush(stdout);
1697 timer = now_d + (double)ptimeout;
1701 case ADMIN_CALL_ALERTING:
1702 printf("ALERTING\n"); fflush(stdout);
1706 timer = now_d + (double)atimeout;
1710 case ADMIN_CALL_CONNECT:
1711 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1715 timer = now_d + (double)ctimeout;
1719 case ADMIN_CALL_NOTIFY:
1720 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1723 case ADMIN_CALL_DISCONNECT:
1724 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);
1727 case ADMIN_CALL_RELEASE:
1728 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);
1732 return("Response not valid.");
1735 printf("Call released.\n"); fflush(stdout);
1743 const char *admin_trace(int sock, int argc, char *argv[])
1745 static struct admin_message msg;
1749 if (argc > 2) if (!strcasecmp(argv[2], "help"))
1751 printf("Trace Help\n----------\n");
1752 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1753 printf("By default a complete trace is shown in detailed format.\n");
1754 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1755 printf("Use filter values to select specific trace messages.\n");
1756 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1757 printf("Filters:\n");
1758 printf(" category=<mask bits>\n");
1759 printf(" 0x01 = CH: channel object trace\n");
1760 printf(" 0x02 = EP: endpoint object trace\n");
1761 printf(" port=<mISDN port> select only given port for trace\n");
1762 printf(" interface=<interface name> select only given interface for trace\n");
1763 printf(" caller=<caller id> select only given caller id for trace\n");
1764 printf(" dialing=<number> select only given dialed number for trace\n");
1768 /* init trace request */
1769 memset(&msg, 0, sizeof(msg));
1770 msg.message = ADMIN_TRACE_REQUEST;
1771 msg.u.trace_req.detail = 3;
1772 msg.u.trace_req.port = -1;
1778 if (!strcasecmp(argv[i], "brief"))
1779 msg.u.trace_req.detail = 1;
1780 else if (!strcasecmp(argv[i], "short"))
1781 msg.u.trace_req.detail = 2;
1782 else if (!strncasecmp(argv[i], "category=", 9))
1783 msg.u.trace_req.category = atoi(argv[i]+9);
1784 else if (!strncasecmp(argv[i], "port=", 5))
1785 msg.u.trace_req.port = atoi(argv[i]+5);
1786 else if (!strncasecmp(argv[i], "interface=", 10))
1787 SCPY(msg.u.trace_req.interface, argv[i]+10);
1788 else if (!strncasecmp(argv[i], "caller=", 7))
1789 SCPY(msg.u.trace_req.caller, argv[i]+7);
1790 else if (!strncasecmp(argv[i], "dialing=", 8))
1791 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1792 else return("Invalid trace option, try 'trace help'.");
1797 /* send trace request */
1798 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1799 return("Broken pipe while sending trace request.");
1801 /* receive response */
1803 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1804 return("Broken pipe while receiving response.");
1806 if (msg.message != ADMIN_TRACE_RESPONSE)
1807 return("Response not valid.");
1809 printf("%s", msg.u.trace_rsp.text);
1817 int main(int argc, char *argv[])
1821 struct sockaddr_un sock_address;
1830 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1831 printf("state - View current states using graphical console output.\n");
1832 printf("portinfo - Get info of current ports.\n");
1833 printf("interface [<portname>] - Tell LCR to reload \"interface.conf\".\n");
1834 printf("route - Tell LCR to reload \"route.conf\".\n");
1835 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1836 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1837 printf("block <port> - Block given port.\n");
1838 printf("unblock/load <port> - Unblock given port.\n");
1839 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1840 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1841 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1842 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1843 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1844 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1845 printf(" -> Use 'trace help' to see filter description.\n");
1851 if (!(strcasecmp(argv[1],"state")))
1855 if (!(strcasecmp(argv[1],"portinfo")))
1857 mode = MODE_PORTINFO;
1859 if (!(strcasecmp(argv[1],"interface")))
1861 mode = MODE_INTERFACE;
1863 if (!(strcasecmp(argv[1],"route")))
1867 if (!(strcasecmp(argv[1],"dial")))
1873 if (!(strcasecmp(argv[1],"release")))
1877 mode = MODE_RELEASE;
1879 if (!(strcasecmp(argv[1],"unblock"))
1880 || !(strcasecmp(argv[1],"load")))
1884 mode = MODE_UNBLOCK;
1886 if (!(strcasecmp(argv[1],"block")))
1892 if (!(strcasecmp(argv[1],"unload")))
1898 if (!(strcasecmp(argv[1],"testcall")))
1902 mode = MODE_TESTCALL;
1904 if (!(strcasecmp(argv[1],"trace")))
1912 if (read_options() == 0) {
1918 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1920 fprintf(stderr, "Failed to create socket.\n");
1923 memset(&sock_address, 0, sizeof(sock_address));
1924 SPRINT(sock_address.sun_path, SOCKET_NAME, options.lock);
1925 sock_address.sun_family = PF_UNIX;
1926 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1929 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1937 ret = admin_state(sock, argv);
1941 ret = admin_portinfo(sock, argc, argv);
1944 case MODE_INTERFACE:
1946 ret = admin_cmd(sock, mode, NULL, NULL);
1950 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1957 ret = admin_cmd(sock, mode, NULL, argv[2]);
1961 ret = admin_testcall(sock, argc, argv);
1965 ret = admin_trace(sock, argc, argv);
1970 /* now we say good bye */
1973 // if (!strncasecmp(ret, "Broken Pipe", 11))
1975 printf("%s\n", ret);