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 SPRINT(buffer, "%s (%d)%s", m[i].u.i.interface_name, m[i].u.i.portnum, (m[i].u.i.extension)?" (extension)":"");
674 addstr(" not loaded");
677 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);
679 if (m[i].u.i.ptp || !m[i].u.i.ntmode)
681 color((m[i].u.i.l2link)?green:red);
682 addstr((m[i].u.i.l2link)?" L2 UP":" L2 down");
684 color((m[i].u.i.l1link > 0)?green:blue);
685 if (m[i].u.i.l1link < 0)
686 addstr(" L1 unknown");
688 addstr((m[i].u.i.l1link)?" L1 ACTIVE":" L1 inactive");
704 if (m[i].u.i.slip_tx || m[i].u.i.slip_rx)
707 SPRINT(buffer, " SLIP(tx:%d rx:%d)", m[i].u.i.slip_tx, m[i].u.i.slip_rx);
715 if (line+2 >= LINES) goto end;
717 if (show_interfaces > 1)
721 jj = m[i].u.i.channels;
724 /* show all channels */
725 if (show_interfaces>2 || m[i].u.i.busy[j]>0)
728 /* show left side / right side */
729 if ((k & 1) && (COLS > 70))
731 move(line>1?line:1,4+((COLS-4)/2));
734 move(++line>1?line:1, 1);
741 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
743 SPRINT(buffer,"B%2d: ", j+1);
744 if (m[i].u.i.mode[j] == B_MODE_HDLC)
745 SCAT(buffer,"HDLC ");
747 switch(m[i].u.i.busy[j])
750 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block)
760 case B_STATE_ACTIVATING:
768 case B_STATE_DEACTIVATING:
772 case B_STATE_EXPORTING:
780 case B_STATE_IMPORTING:
785 if (m[i].u.i.port[j])
787 /* search for port */
788 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
789 ll = l+msg.u.s.ports;
792 if (m[l].u.p.serial == m[i].u.i.port[j])
794 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
805 move(line>1?line:1, 1);
816 move(line>1?line:1, 1);
819 if (line+2 >= LINES) goto end;
820 /* show summary if no channels were shown */
821 if (show_interfaces<2 && ltee==0)
824 move(++line>1?line:1, 1);
827 if (m[i].u.i.l2link && m[i].u.i.block==0)
830 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
834 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
838 if (line+2 >= LINES) goto end;
844 i = msg.u.s.interfaces;
845 ii = i + msg.u.s.remotes;
848 /* show remote summary */
849 move(++line>1?line:1, 0);
851 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
857 if (line+2 >= LINES) goto end;
859 /* display calls (brief) */
863 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
864 ii = i+msg.u.s.epoints;
867 /* for each endpoint... */
870 move(++line>1?line:1, 0);
872 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
875 if (m[i].u.e.terminal[0])
879 addstr(m[i].u.e.terminal);
883 SPRINT(buffer, " %s", m[i].u.e.callerid);
888 SPRINT(buffer, "%s", m[i].u.e.dialing);
890 if (m[i].u.e.action[0])
895 addstr(m[i].u.e.action);
897 if (line+2 >= LINES) goto end;
902 j = msg.u.s.interfaces+msg.u.s.remotes;
903 jj = j+msg.u.s.joins;
906 /* for each call... */
907 move(++line>1?line:1, 0);
909 SPRINT(buffer, "(%d):", m[j].u.j.serial);
911 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
912 ii = i+msg.u.s.epoints;
915 /* for each endpoint... */
916 if (m[i].u.e.join == m[j].u.j.serial)
919 SPRINT(buffer, " (%d)", m[i].u.e.serial);
922 if (m[i].u.e.terminal[0])
926 addstr(m[i].u.e.terminal);
930 SPRINT(buffer, "-%s", m[i].u.e.callerid);
935 SPRINT(buffer, "%s", m[i].u.e.dialing);
941 if (line+2 >= LINES) goto end;
946 if (line+2 >= LINES) goto end;
948 /* display calls (structurd) */
951 /* show all ports with no epoint */
953 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
954 ii = i+msg.u.s.ports;
957 if (!m[i].u.p.epoint)
959 move(++line>1?line:1, 8);
960 if (line+2 >= LINES) goto end;
961 line = debug_port(&msg, m, line, i, 0);
962 if (line+2 >= LINES) goto end;
969 if (line+2 >= LINES) goto end;
971 /* show all epoints with no call */
973 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
974 ii = i+msg.u.s.epoints;
979 move(++line>1?line:1, 4);
980 if (line+2 >= LINES) goto end;
981 line = debug_epoint(&msg, m, line, i, 0);
982 if (line+2 >= LINES) goto end;
989 if (line+2 >= LINES) goto end;
993 i = msg.u.s.interfaces+msg.u.s.remotes;
994 ii = i+msg.u.s.joins;
997 move(++line>1?line:1, 0);
998 if (line+2 >= LINES) goto end;
999 line = debug_join(&msg, m, line, i);
1000 if (line+2 >= LINES) goto end;
1006 if (line+2 >= LINES) goto end;
1015 move(line++>1?line-1:1, 0);
1017 hline(ACS_HLINE, COLS);
1020 l = logcur-(LINES-line-2);
1022 if (ll-l >= LOGLINES)
1026 move(line++>1?line-1:1, 0);
1027 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
1028 SCPY(buffer, logline[l % LOGLINES] + hoffset);
1031 if (COLS < (int)strlen(buffer))
1033 buffer[COLS-1] = '\0';
1048 /* display name/time */
1050 // hline(' ', COLS);
1053 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
1054 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
1059 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1060 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1061 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1064 /* displeay head line */
1067 hline(ACS_HLINE, COLS);
1071 SPRINT(buffer, "Offset +%d", offset);
1078 SPRINT(buffer, "H-Offset +%d", hoffset);
1085 hline(ACS_HLINE, COLS);
1090 SPRINT(buffer, "-> %s", enter_string);
1094 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1100 if (lastlines!=LINES || lastcols!=COLS)
1109 /* user input in enter mode */
1117 if (!enter_string[0])
1120 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1121 if (!!strncmp(enter_string, "interface", 10) &&
1122 !!strncmp(enter_string, "route", 6) &&
1123 !!strncmp(enter_string, "release ", 8) &&
1124 !!strncmp(enter_string, "block ", 6) &&
1125 !!strncmp(enter_string, "unblock ", 8) &&
1126 !!strncmp(enter_string, "unload ", 7))
1128 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1129 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1130 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1131 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1132 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1133 SPRINT(logline[logcur++ % LOGLINES], "unblock <port> (unblock port for further calls, load if not loaded)");
1134 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1137 /* applend output to log window */
1138 SPRINT(buffer, "%s %s", argv[0], enter_string);
1139 fp = popen(buffer, "r");
1142 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1143 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1147 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1150 logline[logcur % LOGLINES][0] = '\0';
1151 enter_string[0] = '\0';
1154 if (ch>=32 && ch<=126)
1156 SCCAT(enter_string, ch);
1162 if (ch==8 || ch==127)
1164 if (enter_string[0])
1165 enter_string[strlen(enter_string)-1] = '\0';
1176 FD_ZERO(&select_rfds);
1177 FD_SET(0, &select_rfds);
1178 select_tv.tv_sec = 0;
1179 select_tv.tv_usec = 250000;
1180 select(1, &select_rfds, NULL, NULL, &select_tv);
1185 /* user input in normal mode */
1188 case 12: /* refresh */
1199 case 'i': /* toggle interface */
1201 if (show_interfaces > 3) show_interfaces = 0;
1204 case 'c': /* toggle calls */
1206 if (show_calls > 2) show_calls = 0;
1209 case 'l': /* toggle log */
1211 if (show_log > 1) show_log = 0;
1214 case '+': /* scroll down */
1218 case '-': /* scroll up */
1223 case '*': /* scroll right */
1227 case '/': /* scroll left */
1232 case 10: /* entermode */
1237 FD_ZERO(&select_rfds);
1238 FD_SET(0, &select_rfds);
1239 select_tv.tv_sec = 0;
1240 select_tv.tv_usec = 250000;
1241 select(1, &select_rfds, NULL, NULL, &select_tv);
1246 /* check for logfh */
1251 /* cleanup curses and exit */
1259 * Send command and show error message.
1261 const char *admin_cmd(int sock, int mode, char *extension, char *number)
1263 static struct admin_message msg;
1265 /* send reload command */
1266 memset(&msg, 0, sizeof(msg));
1269 case MODE_INTERFACE:
1270 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1273 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1276 msg.message = ADMIN_REQUEST_CMD_DIAL;
1277 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1280 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1281 SCPY(msg.u.x.message, number);
1284 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1285 msg.u.x.portnum = atoi(number);
1289 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1290 msg.u.x.portnum = atoi(number);
1294 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1295 msg.u.x.portnum = atoi(number);
1300 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1301 return("Broken pipe while sending command.");
1303 /* receive response */
1304 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1305 return("Broken pipe while receiving response.");
1308 case MODE_INTERFACE:
1309 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1310 return("Response not valid.");
1313 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1314 return("Response not valid.");
1317 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1318 return("Response not valid.");
1321 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1322 return("Response not valid.");
1327 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1328 return("Response not valid.");
1332 /* process response */
1335 return(msg.u.x.message);
1337 printf("Command successfull.\n");
1345 #define GET_NOW() { \
1346 gettimeofday(&now_tv, &now_tz); \
1347 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1349 const char *admin_testcall(int sock, int argc, char *argv[])
1351 static struct admin_message msg;
1353 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1355 double timer = 0, now_d;
1356 unsigned int on = 1;
1357 struct timeval now_tv;
1358 struct timezone now_tz;
1360 printf("pid=%d\n", getpid()); fflush(stdout);
1364 if (!strcmp(argv[ar], "--setup-timeout"))
1368 return("Missing setup timeout value.\n");
1369 stimeout = atoi(argv[ar]);
1372 if (!strcmp(argv[ar], "--proceeding-timeout"))
1376 return("Missing proceeding timeout value.\n");
1377 ptimeout = atoi(argv[ar]);
1380 if (!strcmp(argv[ar], "--alerting-timeout"))
1384 return("Missing alerting timeout value.\n");
1385 atimeout = atoi(argv[ar]);
1388 if (!strcmp(argv[ar], "--connect-timeout"))
1392 return("Missing connect timeout value.\n");
1393 ctimeout = atoi(argv[ar]);
1401 /* send reload command */
1402 memset(&msg, 0, sizeof(msg));
1403 msg.message = ADMIN_CALL_SETUP;
1404 msg.u.call.present = 1;
1408 SCPY(msg.u.call.interface, argv[ar]);
1413 SCPY(msg.u.call.callerid, argv[ar]);
1418 SCPY(msg.u.call.dialing, argv[ar]);
1423 if (argv[ar][0] == 'r')
1424 msg.u.call.present = 0;
1427 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1428 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1429 msg.u.call.bc_info1 = 0;
1431 msg.u.call.exthlc = 0;
1433 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1435 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1438 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1439 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1443 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1444 if (msg.u.call.bc_info1 < 0)
1445 msg.u.call.bc_info1 = 0;
1447 msg.u.call.bc_info1 |= 0x80;
1451 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1452 if (msg.u.call.hlc < 0)
1455 msg.u.call.hlc |= 0x80;
1459 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1460 if (msg.u.call.exthlc < 0)
1461 msg.u.call.exthlc = 0;
1463 msg.u.call.exthlc |= 0x80;
1467 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1468 return("Broken pipe while sending command.");
1470 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1471 return("Failed to set socket into non-blocking IO.");
1476 timer = now_d + (double)stimeout;
1479 /* receive response */
1481 l = read(sock, &msg, sizeof(msg));
1484 if (errno == EWOULDBLOCK)
1491 printf("Timeout\n"); fflush(stdout);
1498 return("Broken pipe while receiving response.");
1500 if (l != sizeof(msg))
1501 return("Response has unexpected message size.");
1504 case ADMIN_CALL_SETUP_ACK:
1505 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1508 case ADMIN_CALL_PROCEEDING:
1509 printf("PROCEEDING\n"); fflush(stdout);
1513 timer = now_d + (double)ptimeout;
1517 case ADMIN_CALL_ALERTING:
1518 printf("ALERTING\n"); fflush(stdout);
1522 timer = now_d + (double)atimeout;
1526 case ADMIN_CALL_CONNECT:
1527 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1531 timer = now_d + (double)ctimeout;
1535 case ADMIN_CALL_NOTIFY:
1536 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1539 case ADMIN_CALL_DISCONNECT:
1540 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);
1543 case ADMIN_CALL_RELEASE:
1544 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);
1548 return("Response not valid.");
1551 printf("Call released.\n"); fflush(stdout);
1559 const char *admin_trace(int sock, int argc, char *argv[])
1561 static struct admin_message msg;
1565 if (argc > 2) if (!strcasecmp(argv[2], "help"))
1567 printf("Trace Help\n----------\n");
1568 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1569 printf("By default a complete trace is shown in detailed format.\n");
1570 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1571 printf("Use filter values to select specific trace messages.\n");
1572 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1573 printf("Filters:\n");
1574 printf(" category=<mask bits>\n");
1575 printf(" 0x01 = CH: channel object trace\n");
1576 printf(" 0x02 = EP: endpoint object trace\n");
1577 printf(" port=<mISDN port> select only given port for trace\n");
1578 printf(" interface=<interface name> select only given interface for trace\n");
1579 printf(" caller=<caller id> select only given caller id for trace\n");
1580 printf(" dialing=<number> select only given dialed number for trace\n");
1584 /* init trace request */
1585 memset(&msg, 0, sizeof(msg));
1586 msg.message = ADMIN_TRACE_REQUEST;
1587 msg.u.trace_req.detail = 3;
1593 if (!strcasecmp(argv[i], "brief"))
1594 msg.u.trace_req.detail = 1;
1595 else if (!strcasecmp(argv[i], "short"))
1596 msg.u.trace_req.detail = 2;
1597 else if (!strncasecmp(argv[i], "category=", 9))
1598 msg.u.trace_req.category = atoi(argv[i]+9);
1599 else if (!strncasecmp(argv[i], "port=", 5))
1600 msg.u.trace_req.port = atoi(argv[i]+5);
1601 else if (!strncasecmp(argv[i], "interface=", 10))
1602 SCPY(msg.u.trace_req.interface, argv[i]+10);
1603 else if (!strncasecmp(argv[i], "caller=", 7))
1604 SCPY(msg.u.trace_req.caller, argv[i]+7);
1605 else if (!strncasecmp(argv[i], "dialing=", 8))
1606 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1607 else return("Invalid trace option, try 'trace help'.");
1612 /* send trace request */
1613 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1614 return("Broken pipe while sending trace request.");
1616 /* receive response */
1618 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1619 return("Broken pipe while receiving response.");
1621 if (msg.message != ADMIN_TRACE_RESPONSE)
1622 return("Response not valid.");
1624 printf("%s", msg.u.trace_rsp.text);
1632 int main(int argc, char *argv[])
1636 struct sockaddr_un sock_address;
1645 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1646 printf("state - View current states using graphical console output.\n");
1647 printf("interface - Tell LCR to reload \"interface.conf\".\n");
1648 printf("route - Tell LCR to reload \"route.conf\".\n");
1649 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1650 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1651 printf("block <port> - Block given port.\n");
1652 printf("unblock <port> - Unblock given port.\n");
1653 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1654 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1655 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1656 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1657 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1658 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1659 printf(" -> Use 'trace help' to see filter description.\n");
1665 if (!(strcasecmp(argv[1],"state")))
1669 if (!(strcasecmp(argv[1],"interface")))
1671 mode = MODE_INTERFACE;
1673 if (!(strcasecmp(argv[1],"route")))
1677 if (!(strcasecmp(argv[1],"dial")))
1683 if (!(strcasecmp(argv[1],"release")))
1687 mode = MODE_RELEASE;
1689 if (!(strcasecmp(argv[1],"unblock")))
1693 mode = MODE_UNBLOCK;
1695 if (!(strcasecmp(argv[1],"block")))
1701 if (!(strcasecmp(argv[1],"unload")))
1707 if (!(strcasecmp(argv[1],"testcall")))
1711 mode = MODE_TESTCALL;
1713 if (!(strcasecmp(argv[1],"trace")))
1721 if (read_options() == 0) {
1727 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1729 fprintf(stderr, "Failed to create socket.\n");
1732 memset(&sock_address, 0, sizeof(sock_address));
1733 SPRINT(sock_address.sun_path, SOCKET_NAME, options.lock);
1734 sock_address.sun_family = PF_UNIX;
1735 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1738 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1746 ret = admin_state(sock, argv);
1749 case MODE_INTERFACE:
1751 ret = admin_cmd(sock, mode, NULL, NULL);
1755 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1762 ret = admin_cmd(sock, mode, NULL, argv[2]);
1766 ret = admin_testcall(sock, argc, argv);
1770 ret = admin_trace(sock, argc, argv);
1775 /* now we say good bye */
1778 // if (!strncasecmp(ret, "Broken Pipe", 11))
1780 printf("%s\n", ret);