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,
57 const char *text_interfaces[] = {
64 const char *text_calls[] = {
79 char logline[LOGLINES][512];
80 unsigned int logcur = 0;
87 void init_curses(void)
90 initscr(); cbreak(); noecho();
92 nodelay(stdscr, TRUE);
93 if (COLOR_PAIRS>=8 && COLORS>=8)
107 void cleanup_curses(void)
112 void color(int color)
114 if (COLOR_PAIRS>=8 && COLORS>=8)
115 attrset(COLOR_PAIR(color));
119 * permanently show current state using ncurses
121 int debug_port(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
128 SPRINT(buffer,"%s(%d)", m[i].u.p.name,m[i].u.p.serial);
132 switch (m[i].u.p.state)
134 case ADMIN_STATE_IDLE:
138 case ADMIN_STATE_IN_SETUP:
140 addstr("'in << setup'");
142 case ADMIN_STATE_OUT_SETUP:
144 addstr("'out >> setup'");
146 case ADMIN_STATE_IN_OVERLAP:
148 addstr("'in << overlap'");
150 case ADMIN_STATE_OUT_OVERLAP:
152 addstr("'out >> overlap'");
154 case ADMIN_STATE_IN_PROCEEDING:
156 addstr("'in << proc'");
158 case ADMIN_STATE_OUT_PROCEEDING:
160 addstr("'out >> proc'");
162 case ADMIN_STATE_IN_ALERTING:
164 addstr("'in << alert'");
166 case ADMIN_STATE_OUT_ALERTING:
168 addstr("'out >> alert'");
170 case ADMIN_STATE_CONNECT:
174 case ADMIN_STATE_IN_DISCONNECT:
176 addstr("'in << disc'");
178 case ADMIN_STATE_OUT_DISCONNECT:
180 addstr("'out >> disc'");
184 addstr("'--NONE--'");
190 addstr(" bchannel=");
192 SPRINT(buffer,"%d", m[i].u.p.isdn_chan);
194 if (m[i].u.p.isdn_ces >= 0)
199 SPRINT(buffer, "%d", m[i].u.p.isdn_ces);
202 if (m[i].u.p.isdn_hold)
211 int debug_epoint(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
213 unsigned int epoint = m[i].u.e.serial;
220 SPRINT(buffer,"EPOINT(%d)", epoint);
224 switch (m[i].u.e.state)
226 case ADMIN_STATE_IDLE:
230 case ADMIN_STATE_IN_SETUP:
232 addstr("'in << setup'");
234 case ADMIN_STATE_OUT_SETUP:
236 addstr("'out >> setup'");
238 case ADMIN_STATE_IN_OVERLAP:
240 addstr("'in << overlap'");
242 case ADMIN_STATE_OUT_OVERLAP:
244 addstr("'out >> overlap'");
246 case ADMIN_STATE_IN_PROCEEDING:
248 addstr("'in << proc'");
250 case ADMIN_STATE_OUT_PROCEEDING:
252 addstr("'out >> proc'");
254 case ADMIN_STATE_IN_ALERTING:
256 addstr("'in << alert'");
258 case ADMIN_STATE_OUT_ALERTING:
260 addstr("'out >> alert'");
262 case ADMIN_STATE_CONNECT:
266 case ADMIN_STATE_IN_DISCONNECT:
268 addstr("'in << disc'");
270 case ADMIN_STATE_OUT_DISCONNECT:
272 addstr("'out >> disc'");
276 addstr("'--NONE--'");
278 if (m[i].u.e.terminal[0])
281 addstr(" terminal=");
283 addstr(m[i].u.e.terminal);
286 SPRINT(buffer, " %s", m[i].u.e.callerid);
291 addstr(m[i].u.e.dialing);
292 if (m[i].u.e.action[0])
297 addstr(m[i].u.e.action);
302 addstr(" park="); /* 9 digits */
304 UCPY(buffer, "\""); /* 9 digits */
306 jj = m[i].u.e.park_len;
309 c = m[i].u.e.park_callid[j];
310 if (c >= 32 && c < 127 && c != '[')
314 UPRINT(buffer+strlen(buffer), "[%02x]", c);
322 switch(m[i].u.e.rx_state)
324 case NOTIFY_STATE_SUSPEND:
325 addstr(" in=suspend");
327 case NOTIFY_STATE_HOLD:
330 case NOTIFY_STATE_CONFERENCE:
331 addstr(" in=conference");
334 switch(m[i].u.e.tx_state)
336 case NOTIFY_STATE_SUSPEND:
337 addstr(" out=suspend");
339 case NOTIFY_STATE_HOLD:
342 case NOTIFY_STATE_CONFERENCE:
343 addstr(" out=conference");
351 if (m[i].u.e.crypt) /* crypt on */
361 /* loop all related ports */
363 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins+msg->u.s.epoints;
364 jj = j + msg->u.s.ports;
367 if (m[j].u.p.epoint == epoint)
370 move(++line>1?line:1, 1);
375 move(line>1?line:1, 5);
378 move(line>1?line:1, 8);
379 if (line+2 >= LINES) break;
380 line = debug_port(msg, m, line, j, vline);
381 if (line+2 >= LINES) break;
388 move(ltee>1?line:1, 5);
394 int debug_join(struct admin_message *msg, struct admin_message *m, int line, int i)
396 unsigned int join = m[i].u.j.serial;
401 SPRINT(buffer,"JOIN(%d)", join);
403 if (m[i].u.j.partyline)
406 addstr(" partyline=");
408 SPRINT(buffer, "%d\n", m[i].u.j.partyline);
411 if (m[i].u.j.remote[0])
416 SPRINT(buffer, "%s\n", m[i].u.j.remote);
419 /* find number of epoints */
420 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins;
421 jj = j + msg->u.s.epoints;
425 if (m[j].u.e.join == join)
429 /* loop all related endpoints */
430 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins;
431 jj = j + msg->u.s.epoints;
434 if (m[j].u.e.join == join)
437 move(++line>1?line:1, 1);
443 move(line>1?line:1, 4);
444 if (line+2 >= LINES) break;
445 line = debug_epoint(msg, m, line, j, i?1:0);
446 if (line+2 >= LINES) break;
454 const char *admin_state(int sock, char *argv[])
456 struct admin_message msg,
460 int line, offset = 0, hoffset = 0;
469 char enter_string[128] = "", ch;
471 struct timeval select_tv;
473 /* flush logfile name */
480 /* send reload command */
481 memset(&msg, 0, sizeof(msg));
482 msg.message = ADMIN_REQUEST_STATE;
483 // printf("sizeof=%d\n",sizeof(msg));fflush(stdout);
484 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
487 return("Broken pipe while sending command.");
490 /* receive response */
491 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
494 return("Broken pipe while receiving response.");
497 if (msg.message != ADMIN_RESPONSE_STATE)
500 return("Response not valid. Expecting state response.");
502 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
503 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
508 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off)
512 // fprintf(stderr, "got=%d expected=%d\n", i, num*sizeof(struct admin_message));
514 return("Broken pipe while receiving state infos.");
516 if (len < num*(int)sizeof(struct admin_message))
525 // fprintf("getting =%d interfaces\n", msg.u.s.interfaces);
526 while(i < msg.u.s.interfaces)
528 // fprintf(stderr, "j=%d message=%d\n", j, m[j].message);
529 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE)
533 return("Response not valid. Expecting interface information.");
539 while(i < msg.u.s.remotes)
541 if (m[j].message != ADMIN_RESPONSE_S_REMOTE)
545 return("Response not valid. Expecting remote application information.");
551 while(i < msg.u.s.joins)
553 if (m[j].message != ADMIN_RESPONSE_S_JOIN)
557 return("Response not valid. Expecting join information.");
563 while(i < msg.u.s.epoints)
565 if (m[j].message != ADMIN_RESPONSE_S_EPOINT)
569 return("Response not valid. Expecting endpoint information.");
575 while(i < msg.u.s.ports)
577 if (m[j].message != ADMIN_RESPONSE_S_PORT)
581 return("Response not valid. Expecting port information.");
586 // now j is the number of message blocks
594 if (!!strcmp(logfile, msg.u.s.logfile))
596 SCPY(logfile, msg.u.s.logfile);
604 logline[i][1] = '\0';
608 logfh = open(logfile, O_RDONLY|O_NONBLOCK);
611 /* seek at the end -8000 chars */
612 lseek(logfh, -8000, SEEK_END);
613 /* if not at the beginning, read until endofline */
614 logline[logcur % LOGLINES][0] = '\0';
615 l = read(logfh, logline[logcur % LOGLINES], sizeof(logline[logcur % LOGLINES])-1);
618 /* read first line and skip junk */
619 logline[logcur % LOGLINES][l] = '\0';
620 if ((p = strchr(logline[logcur % LOGLINES],'\n')))
623 SCPY(logline[logcur % LOGLINES], p+1);
624 SCPY(logline[(logcur-1) % LOGLINES], "...");
636 ll = strlen(logline[logcur % LOGLINES]);
637 l = read(logfh, logline[logcur % LOGLINES]+ll, sizeof(logline[logcur % LOGLINES])-ll-1);
640 logline[logcur % LOGLINES][ll+l] = '\0';
642 /* put data to lines */
643 while ((p = strchr(logline[logcur % LOGLINES],'\n')))
647 SCPY(logline[logcur % LOGLINES], p+1);
649 /* if line is full without return, go next line */
650 if (strlen(logline[logcur % LOGLINES]) == sizeof(logline[logcur % LOGLINES])-1)
653 logline[logcur % LOGLINES][0] = '\0';
658 /* display interfaces */
659 if (show_interfaces > 0)
663 ii = i + msg.u.s.interfaces;
666 /* show interface summary */
667 move(++line>1?line:1, 0);
669 if (m[i].u.i.block >= 2)
671 if (m[i].u.i.portnum < 0)
672 SPRINT(buffer, "%s (port ?: %s)%s", m[i].u.i.interface_name, m[i].u.i.portname, (m[i].u.i.extension)?" extension":"");
674 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":"");
677 addstr(" not loaded");
680 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);
682 if (m[i].u.i.ptp || !m[i].u.i.ntmode)
684 color((m[i].u.i.l2link)?green:red);
685 addstr((m[i].u.i.l2link)?" L2 UP":" L2 down");
687 color((m[i].u.i.l1link > 0)?green:blue);
688 if (m[i].u.i.l1link < 0)
689 addstr(" L1 unknown");
691 addstr((m[i].u.i.l1link)?" L1 ACTIVE":" L1 inactive");
707 if (m[i].u.i.slip_tx || m[i].u.i.slip_rx)
710 SPRINT(buffer, " SLIP(tx:%d rx:%d)", m[i].u.i.slip_tx, m[i].u.i.slip_rx);
718 if (line+2 >= LINES) goto end;
720 if (show_interfaces > 1)
724 jj = m[i].u.i.channels;
727 /* show all channels */
728 if (show_interfaces>2 || m[i].u.i.busy[j]>0)
731 /* show left side / right side */
732 if ((k & 1) && (COLS > 70))
734 move(line>1?line:1,4+((COLS-4)/2));
737 move(++line>1?line:1, 1);
744 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
746 SPRINT(buffer,"B%2d: ", j+1);
747 if (m[i].u.i.mode[j] == B_MODE_HDLC)
748 SCAT(buffer,"HDLC ");
750 switch(m[i].u.i.busy[j])
753 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block)
763 case B_STATE_ACTIVATING:
771 case B_STATE_DEACTIVATING:
775 case B_STATE_EXPORTING:
783 case B_STATE_IMPORTING:
788 if (m[i].u.i.port[j])
790 /* search for port */
791 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
792 ll = l+msg.u.s.ports;
795 if (m[l].u.p.serial == m[i].u.i.port[j])
797 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
808 move(line>1?line:1, 1);
819 move(line>1?line:1, 1);
822 if (line+2 >= LINES) goto end;
823 /* show summary if no channels were shown */
824 if (show_interfaces<2 && ltee==0)
827 move(++line>1?line:1, 1);
830 if (m[i].u.i.l2link && m[i].u.i.block==0)
833 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
837 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
841 if (line+2 >= LINES) goto end;
847 i = msg.u.s.interfaces;
848 ii = i + msg.u.s.remotes;
851 /* show remote summary */
852 move(++line>1?line:1, 0);
854 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
860 if (line+2 >= LINES) goto end;
862 /* display calls (brief) */
866 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
867 ii = i+msg.u.s.epoints;
870 /* for each endpoint... */
873 move(++line>1?line:1, 0);
875 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
878 if (m[i].u.e.terminal[0])
882 addstr(m[i].u.e.terminal);
886 SPRINT(buffer, " %s", m[i].u.e.callerid);
891 SPRINT(buffer, "%s", m[i].u.e.dialing);
893 if (m[i].u.e.action[0])
898 addstr(m[i].u.e.action);
900 if (line+2 >= LINES) goto end;
905 j = msg.u.s.interfaces+msg.u.s.remotes;
906 jj = j+msg.u.s.joins;
909 /* for each call... */
910 move(++line>1?line:1, 0);
912 SPRINT(buffer, "(%d):", m[j].u.j.serial);
914 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
915 ii = i+msg.u.s.epoints;
918 /* for each endpoint... */
919 if (m[i].u.e.join == m[j].u.j.serial)
922 SPRINT(buffer, " (%d)", m[i].u.e.serial);
925 if (m[i].u.e.terminal[0])
929 addstr(m[i].u.e.terminal);
933 SPRINT(buffer, "-%s", m[i].u.e.callerid);
938 SPRINT(buffer, "%s", m[i].u.e.dialing);
944 if (line+2 >= LINES) goto end;
949 if (line+2 >= LINES) goto end;
951 /* display calls (structurd) */
954 /* show all ports with no epoint */
956 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
957 ii = i+msg.u.s.ports;
960 if (!m[i].u.p.epoint)
962 move(++line>1?line:1, 8);
963 if (line+2 >= LINES) goto end;
964 line = debug_port(&msg, m, line, i, 0);
965 if (line+2 >= LINES) goto end;
972 if (line+2 >= LINES) goto end;
974 /* show all epoints with no call */
976 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
977 ii = i+msg.u.s.epoints;
982 move(++line>1?line:1, 4);
983 if (line+2 >= LINES) goto end;
984 line = debug_epoint(&msg, m, line, i, 0);
985 if (line+2 >= LINES) goto end;
992 if (line+2 >= LINES) goto end;
996 i = msg.u.s.interfaces+msg.u.s.remotes;
997 ii = i+msg.u.s.joins;
1000 move(++line>1?line:1, 0);
1001 if (line+2 >= LINES) goto end;
1002 line = debug_join(&msg, m, line, i);
1003 if (line+2 >= LINES) goto end;
1009 if (line+2 >= LINES) goto end;
1018 move(line++>1?line-1:1, 0);
1020 hline(ACS_HLINE, COLS);
1023 l = logcur-(LINES-line-2);
1025 if (ll-l >= LOGLINES)
1029 move(line++>1?line-1:1, 0);
1030 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
1031 SCPY(buffer, logline[l % LOGLINES] + hoffset);
1034 if (COLS < (int)strlen(buffer))
1036 buffer[COLS-1] = '\0';
1051 /* display name/time */
1053 // hline(' ', COLS);
1056 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
1057 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
1062 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1063 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1064 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1067 /* displeay head line */
1070 hline(ACS_HLINE, COLS);
1074 SPRINT(buffer, "Offset +%d", offset);
1081 SPRINT(buffer, "H-Offset +%d", hoffset);
1088 hline(ACS_HLINE, COLS);
1093 SPRINT(buffer, "-> %s", enter_string);
1097 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1103 if (lastlines!=LINES || lastcols!=COLS)
1112 /* user input in enter mode */
1120 if (!enter_string[0])
1123 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1124 if (!!strncmp(enter_string, "interface", 10) &&
1125 !!strncmp(enter_string, "route", 6) &&
1126 !!strncmp(enter_string, "release ", 8) &&
1127 !!strncmp(enter_string, "block ", 6) &&
1128 !!strncmp(enter_string, "unblock ", 8) &&
1129 !!strncmp(enter_string, "unload ", 7))
1131 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1132 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1133 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1134 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1135 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1136 SPRINT(logline[logcur++ % LOGLINES], "unblock <port> (unblock port for further calls, load if not loaded)");
1137 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1140 /* applend output to log window */
1141 SPRINT(buffer, "%s %s", argv[0], enter_string);
1142 fp = popen(buffer, "r");
1145 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1146 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1150 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1153 logline[logcur % LOGLINES][0] = '\0';
1154 enter_string[0] = '\0';
1157 if (ch>=32 && ch<=126)
1159 SCCAT(enter_string, ch);
1165 if (ch==8 || ch==127)
1167 if (enter_string[0])
1168 enter_string[strlen(enter_string)-1] = '\0';
1179 FD_ZERO(&select_rfds);
1180 FD_SET(0, &select_rfds);
1181 select_tv.tv_sec = 0;
1182 select_tv.tv_usec = 250000;
1183 select(1, &select_rfds, NULL, NULL, &select_tv);
1188 /* user input in normal mode */
1191 case 12: /* refresh */
1202 case 'i': /* toggle interface */
1204 if (show_interfaces > 3) show_interfaces = 0;
1207 case 'c': /* toggle calls */
1209 if (show_calls > 2) show_calls = 0;
1212 case 'l': /* toggle log */
1214 if (show_log > 1) show_log = 0;
1217 case '+': /* scroll down */
1221 case '-': /* scroll up */
1226 case '*': /* scroll right */
1230 case '/': /* scroll left */
1235 case 10: /* entermode */
1240 FD_ZERO(&select_rfds);
1241 FD_SET(0, &select_rfds);
1242 select_tv.tv_sec = 0;
1243 select_tv.tv_usec = 250000;
1244 select(1, &select_rfds, NULL, NULL, &select_tv);
1249 /* check for logfh */
1254 /* cleanup curses and exit */
1262 * Send command and show error message.
1264 const char *admin_cmd(int sock, int mode, char *extension, char *number)
1266 static struct admin_message msg;
1268 /* send reload command */
1269 memset(&msg, 0, sizeof(msg));
1272 case MODE_INTERFACE:
1273 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1276 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1279 msg.message = ADMIN_REQUEST_CMD_DIAL;
1280 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1283 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1284 SCPY(msg.u.x.message, number);
1287 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1288 msg.u.x.portnum = atoi(number);
1292 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1293 msg.u.x.portnum = atoi(number);
1297 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1298 msg.u.x.portnum = atoi(number);
1303 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1304 return("Broken pipe while sending command.");
1306 /* receive response */
1307 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1308 return("Broken pipe while receiving response.");
1311 case MODE_INTERFACE:
1312 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1313 return("Response not valid.");
1316 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1317 return("Response not valid.");
1320 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1321 return("Response not valid.");
1324 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1325 return("Response not valid.");
1330 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1331 return("Response not valid.");
1335 /* process response */
1338 return(msg.u.x.message);
1340 printf("Command successfull.\n");
1348 #define GET_NOW() { \
1349 gettimeofday(&now_tv, &now_tz); \
1350 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1352 const char *admin_testcall(int sock, int argc, char *argv[])
1354 static struct admin_message msg;
1356 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1358 double timer = 0, now_d;
1359 unsigned int on = 1;
1360 struct timeval now_tv;
1361 struct timezone now_tz;
1363 printf("pid=%d\n", getpid()); fflush(stdout);
1367 if (!strcmp(argv[ar], "--setup-timeout"))
1371 return("Missing setup timeout value.\n");
1372 stimeout = atoi(argv[ar]);
1375 if (!strcmp(argv[ar], "--proceeding-timeout"))
1379 return("Missing proceeding timeout value.\n");
1380 ptimeout = atoi(argv[ar]);
1383 if (!strcmp(argv[ar], "--alerting-timeout"))
1387 return("Missing alerting timeout value.\n");
1388 atimeout = atoi(argv[ar]);
1391 if (!strcmp(argv[ar], "--connect-timeout"))
1395 return("Missing connect timeout value.\n");
1396 ctimeout = atoi(argv[ar]);
1404 /* send reload command */
1405 memset(&msg, 0, sizeof(msg));
1406 msg.message = ADMIN_CALL_SETUP;
1407 msg.u.call.present = 1;
1411 SCPY(msg.u.call.interface, argv[ar]);
1416 SCPY(msg.u.call.callerid, argv[ar]);
1421 SCPY(msg.u.call.dialing, argv[ar]);
1426 if (argv[ar][0] == 'r')
1427 msg.u.call.present = 0;
1430 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1431 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1432 msg.u.call.bc_info1 = 0;
1434 msg.u.call.exthlc = 0;
1436 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1438 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1441 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1442 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1446 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1447 if (msg.u.call.bc_info1 < 0)
1448 msg.u.call.bc_info1 = 0;
1450 msg.u.call.bc_info1 |= 0x80;
1454 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1455 if (msg.u.call.hlc < 0)
1458 msg.u.call.hlc |= 0x80;
1462 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1463 if (msg.u.call.exthlc < 0)
1464 msg.u.call.exthlc = 0;
1466 msg.u.call.exthlc |= 0x80;
1470 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1471 return("Broken pipe while sending command.");
1473 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1474 return("Failed to set socket into non-blocking IO.");
1479 timer = now_d + (double)stimeout;
1482 /* receive response */
1484 l = read(sock, &msg, sizeof(msg));
1487 if (errno == EWOULDBLOCK)
1494 printf("Timeout\n"); fflush(stdout);
1501 return("Broken pipe while receiving response.");
1503 if (l != sizeof(msg))
1504 return("Response has unexpected message size.");
1507 case ADMIN_CALL_SETUP_ACK:
1508 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1511 case ADMIN_CALL_PROCEEDING:
1512 printf("PROCEEDING\n"); fflush(stdout);
1516 timer = now_d + (double)ptimeout;
1520 case ADMIN_CALL_ALERTING:
1521 printf("ALERTING\n"); fflush(stdout);
1525 timer = now_d + (double)atimeout;
1529 case ADMIN_CALL_CONNECT:
1530 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1534 timer = now_d + (double)ctimeout;
1538 case ADMIN_CALL_NOTIFY:
1539 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1542 case ADMIN_CALL_DISCONNECT:
1543 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);
1546 case ADMIN_CALL_RELEASE:
1547 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);
1551 return("Response not valid.");
1554 printf("Call released.\n"); fflush(stdout);
1562 const char *admin_trace(int sock, int argc, char *argv[])
1564 static struct admin_message msg;
1568 if (argc > 2) if (!strcasecmp(argv[2], "help"))
1570 printf("Trace Help\n----------\n");
1571 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1572 printf("By default a complete trace is shown in detailed format.\n");
1573 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1574 printf("Use filter values to select specific trace messages.\n");
1575 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1576 printf("Filters:\n");
1577 printf(" category=<mask bits>\n");
1578 printf(" 0x01 = CH: channel object trace\n");
1579 printf(" 0x02 = EP: endpoint object trace\n");
1580 printf(" port=<mISDN port> select only given port for trace\n");
1581 printf(" interface=<interface name> select only given interface for trace\n");
1582 printf(" caller=<caller id> select only given caller id for trace\n");
1583 printf(" dialing=<number> select only given dialed number for trace\n");
1587 /* init trace request */
1588 memset(&msg, 0, sizeof(msg));
1589 msg.message = ADMIN_TRACE_REQUEST;
1590 msg.u.trace_req.detail = 3;
1591 msg.u.trace_req.port = -1;
1597 if (!strcasecmp(argv[i], "brief"))
1598 msg.u.trace_req.detail = 1;
1599 else if (!strcasecmp(argv[i], "short"))
1600 msg.u.trace_req.detail = 2;
1601 else if (!strncasecmp(argv[i], "category=", 9))
1602 msg.u.trace_req.category = atoi(argv[i]+9);
1603 else if (!strncasecmp(argv[i], "port=", 5))
1604 msg.u.trace_req.port = atoi(argv[i]+5);
1605 else if (!strncasecmp(argv[i], "interface=", 10))
1606 SCPY(msg.u.trace_req.interface, argv[i]+10);
1607 else if (!strncasecmp(argv[i], "caller=", 7))
1608 SCPY(msg.u.trace_req.caller, argv[i]+7);
1609 else if (!strncasecmp(argv[i], "dialing=", 8))
1610 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1611 else return("Invalid trace option, try 'trace help'.");
1616 /* send trace request */
1617 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1618 return("Broken pipe while sending trace request.");
1620 /* receive response */
1622 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1623 return("Broken pipe while receiving response.");
1625 if (msg.message != ADMIN_TRACE_RESPONSE)
1626 return("Response not valid.");
1628 printf("%s", msg.u.trace_rsp.text);
1636 int main(int argc, char *argv[])
1640 struct sockaddr_un sock_address;
1649 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1650 printf("state - View current states using graphical console output.\n");
1651 printf("interface - Tell LCR to reload \"interface.conf\".\n");
1652 printf("route - Tell LCR to reload \"route.conf\".\n");
1653 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1654 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1655 printf("block <port> - Block given port.\n");
1656 printf("unblock <port> - Unblock given port.\n");
1657 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1658 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1659 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1660 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1661 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1662 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1663 printf(" -> Use 'trace help' to see filter description.\n");
1669 if (!(strcasecmp(argv[1],"state")))
1673 if (!(strcasecmp(argv[1],"interface")))
1675 mode = MODE_INTERFACE;
1677 if (!(strcasecmp(argv[1],"route")))
1681 if (!(strcasecmp(argv[1],"dial")))
1687 if (!(strcasecmp(argv[1],"release")))
1691 mode = MODE_RELEASE;
1693 if (!(strcasecmp(argv[1],"unblock")))
1697 mode = MODE_UNBLOCK;
1699 if (!(strcasecmp(argv[1],"block")))
1705 if (!(strcasecmp(argv[1],"unload")))
1711 if (!(strcasecmp(argv[1],"testcall")))
1715 mode = MODE_TESTCALL;
1717 if (!(strcasecmp(argv[1],"trace")))
1725 if (read_options() == 0) {
1731 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1733 fprintf(stderr, "Failed to create socket.\n");
1736 memset(&sock_address, 0, sizeof(sock_address));
1737 SPRINT(sock_address.sun_path, SOCKET_NAME, options.lock);
1738 sock_address.sun_family = PF_UNIX;
1739 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1742 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1750 ret = admin_state(sock, argv);
1753 case MODE_INTERFACE:
1755 ret = admin_cmd(sock, mode, NULL, NULL);
1759 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1766 ret = admin_cmd(sock, mode, NULL, argv[2]);
1770 ret = admin_testcall(sock, argc, argv);
1774 ret = admin_trace(sock, argc, argv);
1779 /* now we say good bye */
1782 // if (!strncasecmp(ret, "Broken Pipe", 11))
1784 printf("%s\n", ret);