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 > 0)?green:red);
685 if (m[i].u.i.l2link < 0)
686 addstr(" L2 unknown");
688 addstr((m[i].u.i.l2link)?" L2 UP":" L2 down");
690 color((m[i].u.i.l1link > 0)?green:blue);
691 if (m[i].u.i.l1link < 0)
692 addstr(" L1 unknown");
694 addstr((m[i].u.i.l1link)?" L1 ACTIVE":" L1 inactive");
710 if (m[i].u.i.slip_tx || m[i].u.i.slip_rx)
713 SPRINT(buffer, " SLIP(tx:%d rx:%d)", m[i].u.i.slip_tx, m[i].u.i.slip_rx);
721 if (line+2 >= LINES) goto end;
723 if (show_interfaces > 1)
727 jj = m[i].u.i.channels;
730 /* show all channels */
731 if (show_interfaces>2 || m[i].u.i.busy[j]>0)
734 /* show left side / right side */
735 if ((k & 1) && (COLS > 70))
737 move(line>1?line:1,4+((COLS-4)/2));
740 move(++line>1?line:1, 1);
747 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
749 SPRINT(buffer,"B%2d: ", j+1);
750 if (m[i].u.i.mode[j] == B_MODE_HDLC)
751 SCAT(buffer,"HDLC ");
753 switch(m[i].u.i.busy[j])
756 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block)
766 case B_STATE_ACTIVATING:
774 case B_STATE_DEACTIVATING:
778 case B_STATE_EXPORTING:
786 case B_STATE_IMPORTING:
791 if (m[i].u.i.port[j])
793 /* search for port */
794 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
795 ll = l+msg.u.s.ports;
798 if (m[l].u.p.serial == m[i].u.i.port[j])
800 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
811 move(line>1?line:1, 1);
822 move(line>1?line:1, 1);
825 if (line+2 >= LINES) goto end;
826 /* show summary if no channels were shown */
827 if (show_interfaces<2 && ltee==0)
830 move(++line>1?line:1, 1);
833 if (m[i].u.i.l2link && m[i].u.i.block==0)
836 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
840 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
844 if (line+2 >= LINES) goto end;
850 i = msg.u.s.interfaces;
851 ii = i + msg.u.s.remotes;
854 /* show remote summary */
855 move(++line>1?line:1, 0);
857 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
863 if (line+2 >= LINES) goto end;
865 /* display calls (brief) */
869 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
870 ii = i+msg.u.s.epoints;
873 /* for each endpoint... */
876 move(++line>1?line:1, 0);
878 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
881 if (m[i].u.e.terminal[0])
885 addstr(m[i].u.e.terminal);
889 SPRINT(buffer, " %s", m[i].u.e.callerid);
894 SPRINT(buffer, "%s", m[i].u.e.dialing);
896 if (m[i].u.e.action[0])
901 addstr(m[i].u.e.action);
903 if (line+2 >= LINES) goto end;
908 j = msg.u.s.interfaces+msg.u.s.remotes;
909 jj = j+msg.u.s.joins;
912 /* for each call... */
913 move(++line>1?line:1, 0);
915 SPRINT(buffer, "(%d):", m[j].u.j.serial);
917 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
918 ii = i+msg.u.s.epoints;
921 /* for each endpoint... */
922 if (m[i].u.e.join == m[j].u.j.serial)
925 SPRINT(buffer, " (%d)", m[i].u.e.serial);
928 if (m[i].u.e.terminal[0])
932 addstr(m[i].u.e.terminal);
936 SPRINT(buffer, "-%s", m[i].u.e.callerid);
941 SPRINT(buffer, "%s", m[i].u.e.dialing);
947 if (line+2 >= LINES) goto end;
952 if (line+2 >= LINES) goto end;
954 /* display calls (structurd) */
957 /* show all ports with no epoint */
959 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
960 ii = i+msg.u.s.ports;
963 if (!m[i].u.p.epoint)
965 move(++line>1?line:1, 8);
966 if (line+2 >= LINES) goto end;
967 line = debug_port(&msg, m, line, i, 0);
968 if (line+2 >= LINES) goto end;
975 if (line+2 >= LINES) goto end;
977 /* show all epoints with no call */
979 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
980 ii = i+msg.u.s.epoints;
985 move(++line>1?line:1, 4);
986 if (line+2 >= LINES) goto end;
987 line = debug_epoint(&msg, m, line, i, 0);
988 if (line+2 >= LINES) goto end;
995 if (line+2 >= LINES) goto end;
999 i = msg.u.s.interfaces+msg.u.s.remotes;
1000 ii = i+msg.u.s.joins;
1003 move(++line>1?line:1, 0);
1004 if (line+2 >= LINES) goto end;
1005 line = debug_join(&msg, m, line, i);
1006 if (line+2 >= LINES) goto end;
1012 if (line+2 >= LINES) goto end;
1021 move(line++>1?line-1:1, 0);
1023 hline(ACS_HLINE, COLS);
1026 l = logcur-(LINES-line-2);
1028 if (ll-l >= LOGLINES)
1032 move(line++>1?line-1:1, 0);
1033 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
1034 SCPY(buffer, logline[l % LOGLINES] + hoffset);
1037 if (COLS < (int)strlen(buffer))
1039 buffer[COLS-1] = '\0';
1054 /* display name/time */
1056 // hline(' ', COLS);
1059 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
1060 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
1065 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1066 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1067 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1070 /* displeay head line */
1073 hline(ACS_HLINE, COLS);
1077 SPRINT(buffer, "Offset +%d", offset);
1084 SPRINT(buffer, "H-Offset +%d", hoffset);
1091 hline(ACS_HLINE, COLS);
1096 SPRINT(buffer, "-> %s", enter_string);
1100 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1106 if (lastlines!=LINES || lastcols!=COLS)
1115 /* user input in enter mode */
1123 if (!enter_string[0])
1126 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1127 if (!!strncmp(enter_string, "interface", 10) &&
1128 !!strncmp(enter_string, "route", 6) &&
1129 !!strncmp(enter_string, "release ", 8) &&
1130 !!strncmp(enter_string, "block ", 6) &&
1131 !!strncmp(enter_string, "unblock ", 8) &&
1132 !!strncmp(enter_string, "unload ", 7))
1134 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1135 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1136 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1137 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1138 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1139 SPRINT(logline[logcur++ % LOGLINES], "unblock <port> (unblock port for further calls, load if not loaded)");
1140 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1143 /* applend output to log window */
1144 SPRINT(buffer, "%s %s", argv[0], enter_string);
1145 fp = popen(buffer, "r");
1148 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1149 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1153 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1156 logline[logcur % LOGLINES][0] = '\0';
1157 enter_string[0] = '\0';
1160 if (ch>=32 && ch<=126)
1162 SCCAT(enter_string, ch);
1168 if (ch==8 || ch==127)
1170 if (enter_string[0])
1171 enter_string[strlen(enter_string)-1] = '\0';
1182 FD_ZERO(&select_rfds);
1183 FD_SET(0, &select_rfds);
1184 select_tv.tv_sec = 0;
1185 select_tv.tv_usec = 250000;
1186 select(1, &select_rfds, NULL, NULL, &select_tv);
1191 /* user input in normal mode */
1194 case 12: /* refresh */
1205 case 'i': /* toggle interface */
1207 if (show_interfaces > 3) show_interfaces = 0;
1210 case 'c': /* toggle calls */
1212 if (show_calls > 2) show_calls = 0;
1215 case 'l': /* toggle log */
1217 if (show_log > 1) show_log = 0;
1220 case '+': /* scroll down */
1224 case '-': /* scroll up */
1229 case '*': /* scroll right */
1233 case '/': /* scroll left */
1238 case 10: /* entermode */
1243 FD_ZERO(&select_rfds);
1244 FD_SET(0, &select_rfds);
1245 select_tv.tv_sec = 0;
1246 select_tv.tv_usec = 250000;
1247 select(1, &select_rfds, NULL, NULL, &select_tv);
1252 /* check for logfh */
1257 /* cleanup curses and exit */
1265 * Send command and show error message.
1267 const char *admin_cmd(int sock, int mode, char *extension, char *number)
1269 static struct admin_message msg;
1271 /* send reload command */
1272 memset(&msg, 0, sizeof(msg));
1275 case MODE_INTERFACE:
1276 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1279 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1282 msg.message = ADMIN_REQUEST_CMD_DIAL;
1283 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1286 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1287 SCPY(msg.u.x.message, number);
1290 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1291 msg.u.x.portnum = atoi(number);
1295 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1296 msg.u.x.portnum = atoi(number);
1300 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1301 msg.u.x.portnum = atoi(number);
1306 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1307 return("Broken pipe while sending command.");
1309 /* receive response */
1310 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1311 return("Broken pipe while receiving response.");
1314 case MODE_INTERFACE:
1315 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1316 return("Response not valid.");
1319 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1320 return("Response not valid.");
1323 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1324 return("Response not valid.");
1327 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1328 return("Response not valid.");
1333 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1334 return("Response not valid.");
1338 /* process response */
1341 return(msg.u.x.message);
1343 printf("Command successfull.\n");
1351 #define GET_NOW() { \
1352 gettimeofday(&now_tv, &now_tz); \
1353 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1355 const char *admin_testcall(int sock, int argc, char *argv[])
1357 static struct admin_message msg;
1359 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1361 double timer = 0, now_d;
1362 unsigned int on = 1;
1363 struct timeval now_tv;
1364 struct timezone now_tz;
1366 printf("pid=%d\n", getpid()); fflush(stdout);
1370 if (!strcmp(argv[ar], "--setup-timeout"))
1374 return("Missing setup timeout value.\n");
1375 stimeout = atoi(argv[ar]);
1378 if (!strcmp(argv[ar], "--proceeding-timeout"))
1382 return("Missing proceeding timeout value.\n");
1383 ptimeout = atoi(argv[ar]);
1386 if (!strcmp(argv[ar], "--alerting-timeout"))
1390 return("Missing alerting timeout value.\n");
1391 atimeout = atoi(argv[ar]);
1394 if (!strcmp(argv[ar], "--connect-timeout"))
1398 return("Missing connect timeout value.\n");
1399 ctimeout = atoi(argv[ar]);
1407 /* send reload command */
1408 memset(&msg, 0, sizeof(msg));
1409 msg.message = ADMIN_CALL_SETUP;
1410 msg.u.call.present = 1;
1414 SCPY(msg.u.call.interface, argv[ar]);
1419 SCPY(msg.u.call.callerid, argv[ar]);
1424 SCPY(msg.u.call.dialing, argv[ar]);
1429 if (argv[ar][0] == 'r')
1430 msg.u.call.present = 0;
1433 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1434 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1435 msg.u.call.bc_info1 = 0;
1437 msg.u.call.exthlc = 0;
1439 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1441 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1444 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1445 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1449 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1450 if (msg.u.call.bc_info1 < 0)
1451 msg.u.call.bc_info1 = 0;
1453 msg.u.call.bc_info1 |= 0x80;
1457 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1458 if (msg.u.call.hlc < 0)
1461 msg.u.call.hlc |= 0x80;
1465 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1466 if (msg.u.call.exthlc < 0)
1467 msg.u.call.exthlc = 0;
1469 msg.u.call.exthlc |= 0x80;
1473 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1474 return("Broken pipe while sending command.");
1476 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1477 return("Failed to set socket into non-blocking IO.");
1482 timer = now_d + (double)stimeout;
1485 /* receive response */
1487 l = read(sock, &msg, sizeof(msg));
1490 if (errno == EWOULDBLOCK)
1497 printf("Timeout\n"); fflush(stdout);
1504 return("Broken pipe while receiving response.");
1506 if (l != sizeof(msg))
1507 return("Response has unexpected message size.");
1510 case ADMIN_CALL_SETUP_ACK:
1511 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1514 case ADMIN_CALL_PROCEEDING:
1515 printf("PROCEEDING\n"); fflush(stdout);
1519 timer = now_d + (double)ptimeout;
1523 case ADMIN_CALL_ALERTING:
1524 printf("ALERTING\n"); fflush(stdout);
1528 timer = now_d + (double)atimeout;
1532 case ADMIN_CALL_CONNECT:
1533 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1537 timer = now_d + (double)ctimeout;
1541 case ADMIN_CALL_NOTIFY:
1542 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1545 case ADMIN_CALL_DISCONNECT:
1546 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);
1549 case ADMIN_CALL_RELEASE:
1550 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);
1554 return("Response not valid.");
1557 printf("Call released.\n"); fflush(stdout);
1565 const char *admin_trace(int sock, int argc, char *argv[])
1567 static struct admin_message msg;
1571 if (argc > 2) if (!strcasecmp(argv[2], "help"))
1573 printf("Trace Help\n----------\n");
1574 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1575 printf("By default a complete trace is shown in detailed format.\n");
1576 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1577 printf("Use filter values to select specific trace messages.\n");
1578 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1579 printf("Filters:\n");
1580 printf(" category=<mask bits>\n");
1581 printf(" 0x01 = CH: channel object trace\n");
1582 printf(" 0x02 = EP: endpoint object trace\n");
1583 printf(" port=<mISDN port> select only given port for trace\n");
1584 printf(" interface=<interface name> select only given interface for trace\n");
1585 printf(" caller=<caller id> select only given caller id for trace\n");
1586 printf(" dialing=<number> select only given dialed number for trace\n");
1590 /* init trace request */
1591 memset(&msg, 0, sizeof(msg));
1592 msg.message = ADMIN_TRACE_REQUEST;
1593 msg.u.trace_req.detail = 3;
1594 msg.u.trace_req.port = -1;
1600 if (!strcasecmp(argv[i], "brief"))
1601 msg.u.trace_req.detail = 1;
1602 else if (!strcasecmp(argv[i], "short"))
1603 msg.u.trace_req.detail = 2;
1604 else if (!strncasecmp(argv[i], "category=", 9))
1605 msg.u.trace_req.category = atoi(argv[i]+9);
1606 else if (!strncasecmp(argv[i], "port=", 5))
1607 msg.u.trace_req.port = atoi(argv[i]+5);
1608 else if (!strncasecmp(argv[i], "interface=", 10))
1609 SCPY(msg.u.trace_req.interface, argv[i]+10);
1610 else if (!strncasecmp(argv[i], "caller=", 7))
1611 SCPY(msg.u.trace_req.caller, argv[i]+7);
1612 else if (!strncasecmp(argv[i], "dialing=", 8))
1613 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1614 else return("Invalid trace option, try 'trace help'.");
1619 /* send trace request */
1620 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1621 return("Broken pipe while sending trace request.");
1623 /* receive response */
1625 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1626 return("Broken pipe while receiving response.");
1628 if (msg.message != ADMIN_TRACE_RESPONSE)
1629 return("Response not valid.");
1631 printf("%s", msg.u.trace_rsp.text);
1639 int main(int argc, char *argv[])
1643 struct sockaddr_un sock_address;
1652 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1653 printf("state - View current states using graphical console output.\n");
1654 printf("interface - Tell LCR to reload \"interface.conf\".\n");
1655 printf("route - Tell LCR to reload \"route.conf\".\n");
1656 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1657 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1658 printf("block <port> - Block given port.\n");
1659 printf("unblock <port> - Unblock given port.\n");
1660 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1661 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1662 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1663 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1664 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1665 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1666 printf(" -> Use 'trace help' to see filter description.\n");
1672 if (!(strcasecmp(argv[1],"state")))
1676 if (!(strcasecmp(argv[1],"interface")))
1678 mode = MODE_INTERFACE;
1680 if (!(strcasecmp(argv[1],"route")))
1684 if (!(strcasecmp(argv[1],"dial")))
1690 if (!(strcasecmp(argv[1],"release")))
1694 mode = MODE_RELEASE;
1696 if (!(strcasecmp(argv[1],"unblock")))
1700 mode = MODE_UNBLOCK;
1702 if (!(strcasecmp(argv[1],"block")))
1708 if (!(strcasecmp(argv[1],"unload")))
1714 if (!(strcasecmp(argv[1],"testcall")))
1718 mode = MODE_TESTCALL;
1720 if (!(strcasecmp(argv[1],"trace")))
1728 if (read_options() == 0) {
1734 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1736 fprintf(stderr, "Failed to create socket.\n");
1739 memset(&sock_address, 0, sizeof(sock_address));
1740 SPRINT(sock_address.sun_path, SOCKET_NAME, options.lock);
1741 sock_address.sun_family = PF_UNIX;
1742 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1745 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1753 ret = admin_state(sock, argv);
1756 case MODE_INTERFACE:
1758 ret = admin_cmd(sock, mode, NULL, NULL);
1762 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1769 ret = admin_cmd(sock, mode, NULL, argv[2]);
1773 ret = admin_testcall(sock, argc, argv);
1777 ret = admin_trace(sock, argc, argv);
1782 /* now we say good bye */
1785 // if (!strncasecmp(ret, "Broken Pipe", 11))
1787 printf("%s\n", ret);