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 char *text_interfaces[] = {
64 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;
453 char *admin_state(int sock, char *argv[])
455 struct admin_message msg,
459 int line, offset = 0, hoffset = 0;
468 char enter_string[128] = "", ch;
470 struct timeval select_tv;
472 /* flush logfile name */
479 /* send reload command */
480 memset(&msg, 0, sizeof(msg));
481 msg.message = ADMIN_REQUEST_STATE;
482 // printf("sizeof=%d\n",sizeof(msg));fflush(stdout);
483 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
486 return("Broken pipe while sending command.");
489 /* receive response */
490 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
493 return("Broken pipe while receiving response.");
496 if (msg.message != ADMIN_RESPONSE_STATE)
499 return("Response not valid. Expecting state response.");
501 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
502 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
507 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off)
511 // fprintf(stderr, "got=%d expected=%d\n", i, num*sizeof(struct admin_message));
513 return("Broken pipe while receiving state infos.");
515 if (len < num*(int)sizeof(struct admin_message))
524 // fprintf("getting =%d interfaces\n", msg.u.s.interfaces);
525 while(i < msg.u.s.interfaces)
527 // fprintf(stderr, "j=%d message=%d\n", j, m[j].message);
528 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE)
532 return("Response not valid. Expecting interface information.");
538 while(i < msg.u.s.remotes)
540 if (m[j].message != ADMIN_RESPONSE_S_REMOTE)
544 return("Response not valid. Expecting remote application information.");
550 while(i < msg.u.s.joins)
552 if (m[j].message != ADMIN_RESPONSE_S_JOIN)
556 return("Response not valid. Expecting join information.");
562 while(i < msg.u.s.epoints)
564 if (m[j].message != ADMIN_RESPONSE_S_EPOINT)
568 return("Response not valid. Expecting endpoint information.");
574 while(i < msg.u.s.ports)
576 if (m[j].message != ADMIN_RESPONSE_S_PORT)
580 return("Response not valid. Expecting port information.");
585 // now j is the number of message blocks
593 if (!!strcmp(logfile, msg.u.s.logfile))
595 SCPY(logfile, msg.u.s.logfile);
603 logline[i][1] = '\0';
607 logfh = open(logfile, O_RDONLY|O_NONBLOCK);
610 /* seek at the end -8000 chars */
611 lseek(logfh, -8000, SEEK_END);
612 /* if not at the beginning, read until endofline */
613 logline[logcur % LOGLINES][0] = '\0';
614 l = read(logfh, logline[logcur % LOGLINES], sizeof(logline[logcur % LOGLINES])-1);
617 /* read first line and skip junk */
618 logline[logcur % LOGLINES][l] = '\0';
619 if ((p = strchr(logline[logcur % LOGLINES],'\n')))
622 SCPY(logline[logcur % LOGLINES], p+1);
623 SCPY(logline[(logcur-1) % LOGLINES], "...");
635 ll = strlen(logline[logcur % LOGLINES]);
636 l = read(logfh, logline[logcur % LOGLINES]+ll, sizeof(logline[logcur % LOGLINES])-ll-1);
639 logline[logcur % LOGLINES][ll+l] = '\0';
641 /* put data to lines */
642 while ((p = strchr(logline[logcur % LOGLINES],'\n')))
646 SCPY(logline[logcur % LOGLINES], p+1);
648 /* if line is full without return, go next line */
649 if (strlen(logline[logcur % LOGLINES]) == sizeof(logline[logcur % LOGLINES])-1)
652 logline[logcur % LOGLINES][0] = '\0';
657 /* display interfaces */
658 if (show_interfaces > 0)
662 ii = i + msg.u.s.interfaces;
665 /* show interface summary */
666 move(++line>1?line:1, 0);
668 if (m[i].u.i.block >= 2)
670 SPRINT(buffer, "%s (%d)%s", m[i].u.i.interface_name, m[i].u.i.portnum, (m[i].u.i.extension)?" (extension)":"");
673 addstr(" not loaded");
676 SPRINT(buffer, "%s (%d) %s %s%s use:%d", m[i].u.i.interface_name, m[i].u.i.portnum, (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);
678 if (m[i].u.i.ptp || !m[i].u.i.ntmode)
680 color((m[i].u.i.l2link)?green:red);
681 addstr((m[i].u.i.l2link)?" L2 UP":" L2 down");
683 color((m[i].u.i.l1link > 0)?green:blue);
684 if (m[i].u.i.l1link < 0)
685 addstr(" L1 unknown");
687 addstr((m[i].u.i.l1link)?" L1 ACTIVE":" L1 inactive");
703 if (m[i].u.i.slip_tx || m[i].u.i.slip_rx)
706 SPRINT(buffer, " SLIP(tx:%d rx:%d)", m[i].u.i.slip_tx, m[i].u.i.slip_rx);
714 if (line+2 >= LINES) goto end;
716 if (show_interfaces > 1)
720 jj = m[i].u.i.channels;
723 /* show all channels */
724 if (show_interfaces>2 || m[i].u.i.busy[j]>0)
727 /* show left side / right side */
728 if ((k & 1) && (COLS > 70))
730 move(line>1?line:1,4+((COLS-4)/2));
733 move(++line>1?line:1, 1);
740 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
742 SPRINT(buffer,"B%2d: ", j+1);
743 if (m[i].u.i.mode[j] == B_MODE_HDLC)
744 SCAT(buffer,"HDLC ");
746 switch(m[i].u.i.busy[j])
749 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block)
759 case B_STATE_ACTIVATING:
767 case B_STATE_DEACTIVATING:
771 case B_STATE_EXPORTING:
779 case B_STATE_IMPORTING:
784 if (m[i].u.i.port[j])
786 /* search for port */
787 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
788 ll = l+msg.u.s.ports;
791 if (m[l].u.p.serial == m[i].u.i.port[j])
793 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
804 move(line>1?line:1, 1);
815 move(line>1?line:1, 1);
818 if (line+2 >= LINES) goto end;
819 /* show summary if no channels were shown */
820 if (show_interfaces<2 && ltee==0)
823 move(++line>1?line:1, 1);
826 if (m[i].u.i.l2link && m[i].u.i.block==0)
829 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
833 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
837 if (line+2 >= LINES) goto end;
843 i = msg.u.s.interfaces;
844 ii = i + msg.u.s.remotes;
847 /* show remote summary */
848 move(++line>1?line:1, 0);
850 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
856 if (line+2 >= LINES) goto end;
858 /* display calls (brief) */
862 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
863 ii = i+msg.u.s.epoints;
866 /* for each endpoint... */
869 move(++line>1?line:1, 0);
871 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
874 if (m[i].u.e.terminal[0])
878 addstr(m[i].u.e.terminal);
882 SPRINT(buffer, " %s", m[i].u.e.callerid);
887 SPRINT(buffer, "%s", m[i].u.e.dialing);
889 if (m[i].u.e.action[0])
894 addstr(m[i].u.e.action);
896 if (line+2 >= LINES) goto end;
901 j = msg.u.s.interfaces+msg.u.s.remotes;
902 jj = j+msg.u.s.joins;
905 /* for each call... */
906 move(++line>1?line:1, 0);
908 SPRINT(buffer, "(%d):", m[j].u.j.serial);
910 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
911 ii = i+msg.u.s.epoints;
914 /* for each endpoint... */
915 if (m[i].u.e.join == m[j].u.j.serial)
918 SPRINT(buffer, " (%d)", m[i].u.e.serial);
921 if (m[i].u.e.terminal[0])
925 addstr(m[i].u.e.terminal);
929 SPRINT(buffer, "-%s", m[i].u.e.callerid);
934 SPRINT(buffer, "%s", m[i].u.e.dialing);
940 if (line+2 >= LINES) goto end;
945 if (line+2 >= LINES) goto end;
947 /* display calls (structurd) */
950 /* show all ports with no epoint */
952 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
953 ii = i+msg.u.s.ports;
956 if (!m[i].u.p.epoint)
958 move(++line>1?line:1, 8);
959 if (line+2 >= LINES) goto end;
960 line = debug_port(&msg, m, line, i, 0);
961 if (line+2 >= LINES) goto end;
968 if (line+2 >= LINES) goto end;
970 /* show all epoints with no call */
972 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
973 ii = i+msg.u.s.epoints;
978 move(++line>1?line:1, 4);
979 if (line+2 >= LINES) goto end;
980 line = debug_epoint(&msg, m, line, i, 0);
981 if (line+2 >= LINES) goto end;
988 if (line+2 >= LINES) goto end;
992 i = msg.u.s.interfaces+msg.u.s.remotes;
993 ii = i+msg.u.s.joins;
996 move(++line>1?line:1, 0);
997 if (line+2 >= LINES) goto end;
998 line = debug_join(&msg, m, line, i);
999 if (line+2 >= LINES) goto end;
1005 if (line+2 >= LINES) goto end;
1014 move(line++>1?line-1:1, 0);
1016 hline(ACS_HLINE, COLS);
1019 l = logcur-(LINES-line-2);
1021 if (ll-l >= LOGLINES)
1025 move(line++>1?line-1:1, 0);
1026 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
1027 SCPY(buffer, logline[l % LOGLINES] + hoffset);
1030 if (COLS < (int)strlen(buffer))
1032 buffer[COLS-1] = '\0';
1047 /* display name/time */
1049 // hline(' ', COLS);
1052 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
1053 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
1058 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1059 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1060 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1063 /* displeay head line */
1066 hline(ACS_HLINE, COLS);
1070 SPRINT(buffer, "Offset +%d", offset);
1077 SPRINT(buffer, "H-Offset +%d", hoffset);
1084 hline(ACS_HLINE, COLS);
1089 SPRINT(buffer, "-> %s", enter_string);
1093 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1099 if (lastlines!=LINES || lastcols!=COLS)
1108 /* user input in enter mode */
1116 if (!enter_string[0])
1119 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1120 if (!!strncmp(enter_string, "interface", 10) &&
1121 !!strncmp(enter_string, "route", 6) &&
1122 !!strncmp(enter_string, "release ", 8) &&
1123 !!strncmp(enter_string, "block ", 6) &&
1124 !!strncmp(enter_string, "unblock ", 8) &&
1125 !!strncmp(enter_string, "unload ", 7))
1127 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1128 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1129 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1130 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1131 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1132 SPRINT(logline[logcur++ % LOGLINES], "unblock <port> (unblock port for further calls, load if not loaded)");
1133 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1136 /* applend output to log window */
1137 SPRINT(buffer, "%s %s", argv[0], enter_string);
1138 fp = popen(buffer, "r");
1141 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1142 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1146 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1149 logline[logcur % LOGLINES][0] = '\0';
1150 enter_string[0] = '\0';
1153 if (ch>=32 && ch<=126)
1155 SCCAT(enter_string, ch);
1161 if (ch==8 || ch==127)
1163 if (enter_string[0])
1164 enter_string[strlen(enter_string)-1] = '\0';
1175 FD_ZERO(&select_rfds);
1176 FD_SET(0, &select_rfds);
1177 select_tv.tv_sec = 0;
1178 select_tv.tv_usec = 250000;
1179 select(1, &select_rfds, NULL, NULL, &select_tv);
1184 /* user input in normal mode */
1187 case 12: /* refresh */
1198 case 'i': /* toggle interface */
1200 if (show_interfaces > 3) show_interfaces = 0;
1203 case 'c': /* toggle calls */
1205 if (show_calls > 2) show_calls = 0;
1208 case 'l': /* toggle log */
1210 if (show_log > 1) show_log = 0;
1213 case '+': /* scroll down */
1217 case '-': /* scroll up */
1222 case '*': /* scroll right */
1226 case '/': /* scroll left */
1231 case 10: /* entermode */
1236 FD_ZERO(&select_rfds);
1237 FD_SET(0, &select_rfds);
1238 select_tv.tv_sec = 0;
1239 select_tv.tv_usec = 250000;
1240 select(1, &select_rfds, NULL, NULL, &select_tv);
1245 /* check for logfh */
1250 /* cleanup curses and exit */
1258 * Send command and show error message.
1260 char *admin_cmd(int sock, int mode, char *extension, char *number)
1262 static struct admin_message msg;
1264 /* send reload command */
1265 memset(&msg, 0, sizeof(msg));
1268 case MODE_INTERFACE:
1269 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1272 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1275 msg.message = ADMIN_REQUEST_CMD_DIAL;
1276 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1279 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1280 SCPY(msg.u.x.message, number);
1283 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1284 msg.u.x.portnum = atoi(number);
1288 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1289 msg.u.x.portnum = atoi(number);
1293 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1294 msg.u.x.portnum = atoi(number);
1299 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1300 return("Broken pipe while sending command.");
1302 /* receive response */
1303 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1304 return("Broken pipe while receiving response.");
1307 case MODE_INTERFACE:
1308 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1309 return("Response not valid.");
1312 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1313 return("Response not valid.");
1316 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1317 return("Response not valid.");
1320 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1321 return("Response not valid.");
1326 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1327 return("Response not valid.");
1331 /* process response */
1334 return(msg.u.x.message);
1336 printf("Command successfull.\n");
1344 #define GET_NOW() { \
1345 gettimeofday(&now_tv, &now_tz); \
1346 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1348 char *admin_testcall(int sock, int argc, char *argv[])
1350 static struct admin_message msg;
1352 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1354 double timer = 0, now_d;
1355 unsigned int on = 1;
1356 struct timeval now_tv;
1357 struct timezone now_tz;
1359 printf("pid=%d\n", getpid()); fflush(stdout);
1363 if (!strcmp(argv[ar], "--setup-timeout"))
1367 return("Missing setup timeout value.\n");
1368 stimeout = atoi(argv[ar]);
1371 if (!strcmp(argv[ar], "--proceeding-timeout"))
1375 return("Missing proceeding timeout value.\n");
1376 ptimeout = atoi(argv[ar]);
1379 if (!strcmp(argv[ar], "--alerting-timeout"))
1383 return("Missing alerting timeout value.\n");
1384 atimeout = atoi(argv[ar]);
1387 if (!strcmp(argv[ar], "--connect-timeout"))
1391 return("Missing connect timeout value.\n");
1392 ctimeout = atoi(argv[ar]);
1400 /* send reload command */
1401 memset(&msg, 0, sizeof(msg));
1402 msg.message = ADMIN_CALL_SETUP;
1403 msg.u.call.present = 1;
1407 SCPY(msg.u.call.interface, argv[ar]);
1412 SCPY(msg.u.call.callerid, argv[ar]);
1417 SCPY(msg.u.call.dialing, argv[ar]);
1422 if (argv[ar][0] == 'r')
1423 msg.u.call.present = 0;
1426 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1427 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1428 msg.u.call.bc_info1 = 0;
1430 msg.u.call.exthlc = 0;
1432 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1434 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1437 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1438 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1442 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1443 if (msg.u.call.bc_info1 < 0)
1444 msg.u.call.bc_info1 = 0;
1446 msg.u.call.bc_info1 |= 0x80;
1450 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1451 if (msg.u.call.hlc < 0)
1454 msg.u.call.hlc |= 0x80;
1458 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1459 if (msg.u.call.exthlc < 0)
1460 msg.u.call.exthlc = 0;
1462 msg.u.call.exthlc |= 0x80;
1466 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1467 return("Broken pipe while sending command.");
1469 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1470 return("Failed to set socket into non-blocking IO.");
1475 timer = now_d + (double)stimeout;
1478 /* receive response */
1480 l = read(sock, &msg, sizeof(msg));
1483 if (errno == EWOULDBLOCK)
1490 printf("Timeout\n"); fflush(stdout);
1497 return("Broken pipe while receiving response.");
1499 if (l != sizeof(msg))
1500 return("Response has unexpected message size.");
1503 case ADMIN_CALL_SETUP_ACK:
1504 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1507 case ADMIN_CALL_PROCEEDING:
1508 printf("PROCEEDING\n"); fflush(stdout);
1512 timer = now_d + (double)ptimeout;
1516 case ADMIN_CALL_ALERTING:
1517 printf("ALERTING\n"); fflush(stdout);
1521 timer = now_d + (double)atimeout;
1525 case ADMIN_CALL_CONNECT:
1526 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1530 timer = now_d + (double)ctimeout;
1534 case ADMIN_CALL_NOTIFY:
1535 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1538 case ADMIN_CALL_DISCONNECT:
1539 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);
1542 case ADMIN_CALL_RELEASE:
1543 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);
1547 return("Response not valid.");
1550 printf("Call released.\n"); fflush(stdout);
1558 char *admin_trace(int sock, int argc, char *argv[])
1560 static struct admin_message msg;
1564 if (argc > 2) if (!strcasecmp(argv[2], "help"))
1566 printf("Trace Help\n----------\n");
1567 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1568 printf("By default a complete trace is shown in detailed format.\n");
1569 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1570 printf("Use filter values to select specific trace messages.\n");
1571 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1572 printf("Filters:\n");
1573 printf(" category=<mask bits>\n");
1574 printf(" 0x01 = CH: channel object trace\n");
1575 printf(" 0x02 = EP: endpoint object trace\n");
1576 printf(" port=<mISDN port> select only given port for trace\n");
1577 printf(" interface=<interface name> select only given interface for trace\n");
1578 printf(" caller=<caller id> select only given caller id for trace\n");
1579 printf(" dialing=<number> select only given dialed number for trace\n");
1583 /* init trace request */
1584 memset(&msg, 0, sizeof(msg));
1585 msg.message = ADMIN_TRACE_REQUEST;
1586 msg.u.trace_req.detail = 3;
1592 if (!strcasecmp(argv[i], "brief"))
1593 msg.u.trace_req.detail = 1;
1594 else if (!strcasecmp(argv[i], "short"))
1595 msg.u.trace_req.detail = 2;
1596 else if (!strncasecmp(argv[i], "category=", 9))
1597 msg.u.trace_req.category = atoi(argv[i]+9);
1598 else if (!strncasecmp(argv[i], "port=", 5))
1599 msg.u.trace_req.port = atoi(argv[i]+5);
1600 else if (!strncasecmp(argv[i], "interface=", 10))
1601 SCPY(msg.u.trace_req.interface, argv[i]+10);
1602 else if (!strncasecmp(argv[i], "caller=", 7))
1603 SCPY(msg.u.trace_req.caller, argv[i]+7);
1604 else if (!strncasecmp(argv[i], "dialing=", 8))
1605 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1606 else return("Invalid trace option, try 'trace help'.");
1611 /* send trace request */
1612 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1613 return("Broken pipe while sending trace request.");
1615 /* receive response */
1617 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1618 return("Broken pipe while receiving response.");
1620 if (msg.message != ADMIN_TRACE_RESPONSE)
1621 return("Response not valid.");
1623 printf("%s", msg.u.trace_rsp.text);
1631 int main(int argc, char *argv[])
1635 struct sockaddr_un sock_address;
1644 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1645 printf("state - View current states using graphical console output.\n");
1646 printf("interface - Tell LCR to reload \"interface.conf\".\n");
1647 printf("route - Tell LCR to reload \"route.conf\".\n");
1648 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1649 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1650 printf("block <port> - Block given port.\n");
1651 printf("unblock <port> - Unblock given port.\n");
1652 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1653 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1654 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1655 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1656 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1657 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1658 printf(" -> Use 'trace help' to see filter description.\n");
1664 if (!(strcasecmp(argv[1],"state")))
1668 if (!(strcasecmp(argv[1],"interface")))
1670 mode = MODE_INTERFACE;
1672 if (!(strcasecmp(argv[1],"route")))
1676 if (!(strcasecmp(argv[1],"dial")))
1682 if (!(strcasecmp(argv[1],"release")))
1686 mode = MODE_RELEASE;
1688 if (!(strcasecmp(argv[1],"unblock")))
1692 mode = MODE_UNBLOCK;
1694 if (!(strcasecmp(argv[1],"block")))
1700 if (!(strcasecmp(argv[1],"unload")))
1706 if (!(strcasecmp(argv[1],"testcall")))
1710 mode = MODE_TESTCALL;
1712 if (!(strcasecmp(argv[1],"trace")))
1720 if (read_options() == 0) {
1726 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1728 fprintf(stderr, "Failed to create socket.\n");
1731 memset(&sock_address, 0, sizeof(sock_address));
1732 SPRINT(sock_address.sun_path, SOCKET_NAME, options.lock);
1733 sock_address.sun_family = PF_UNIX;
1734 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1737 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1745 ret = admin_state(sock, argv);
1748 case MODE_INTERFACE:
1750 ret = admin_cmd(sock, mode, NULL, NULL);
1754 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1761 ret = admin_cmd(sock, mode, NULL, argv[2]);
1765 ret = admin_testcall(sock, argc, argv);
1769 ret = admin_trace(sock, argc, argv);
1774 /* now we say good bye */
1777 // if (!strncasecmp(ret, "Broken Pipe", 11))
1779 printf("%s\n", ret);