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>
28 #include "extension.h"
30 #include "lcrsocket.h"
33 #define LTEE {addch(ACS_LTEE);addch(ACS_HLINE);addch(ACS_HLINE);}
34 #define LLCORNER {addch(ACS_LLCORNER);addch(ACS_HLINE);addch(ACS_HLINE);}
35 #define VLINE {addch(ACS_VLINE);addstr(" ");}
36 #define EMPTY {addstr(" ");}
37 //char rotator[] = {'-', '\\', '|', '/'};
38 int lastlines, lastcols;
39 int show_interfaces = 2,
56 char *text_interfaces[] = {
63 char *text_calls[] = {
78 char logline[LOGLINES][512];
79 unsigned long logcur = 0;
86 void init_curses(void)
89 initscr(); cbreak(); noecho();
91 nodelay(stdscr, TRUE);
92 if (COLOR_PAIRS>=8 && COLORS>=8)
106 void cleanup_curses(void)
111 void color(int color)
113 if (COLOR_PAIRS>=8 && COLORS>=8)
114 attrset(COLOR_PAIR(color));
118 * permanently show current state using ncurses
120 int debug_port(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
127 SPRINT(buffer,"%s(%d)", m[i].u.p.name,m[i].u.p.serial);
131 switch (m[i].u.p.state)
133 case ADMIN_STATE_IDLE:
137 case ADMIN_STATE_IN_SETUP:
139 addstr("'in << setup'");
141 case ADMIN_STATE_OUT_SETUP:
143 addstr("'out >> setup'");
145 case ADMIN_STATE_IN_OVERLAP:
147 addstr("'in << overlap'");
149 case ADMIN_STATE_OUT_OVERLAP:
151 addstr("'out >> overlap'");
153 case ADMIN_STATE_IN_PROCEEDING:
155 addstr("'in << proc'");
157 case ADMIN_STATE_OUT_PROCEEDING:
159 addstr("'out >> proc'");
161 case ADMIN_STATE_IN_ALERTING:
163 addstr("'in << alert'");
165 case ADMIN_STATE_OUT_ALERTING:
167 addstr("'out >> alert'");
169 case ADMIN_STATE_CONNECT:
173 case ADMIN_STATE_IN_DISCONNECT:
175 addstr("'in << disc'");
177 case ADMIN_STATE_OUT_DISCONNECT:
179 addstr("'out >> disc'");
183 addstr("'--NONE--'");
189 addstr(" bchannel=");
191 SPRINT(buffer,"%d", m[i].u.p.isdn_chan);
193 if (m[i].u.p.isdn_ces >= 0)
198 SPRINT(buffer, "%d", m[i].u.p.isdn_ces);
201 if (m[i].u.p.isdn_hold)
210 int debug_epoint(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
212 unsigned long epoint = m[i].u.e.serial;
219 SPRINT(buffer,"EPOINT(%d)", epoint);
223 switch (m[i].u.e.state)
225 case ADMIN_STATE_IDLE:
229 case ADMIN_STATE_IN_SETUP:
231 addstr("'in << setup'");
233 case ADMIN_STATE_OUT_SETUP:
235 addstr("'out >> setup'");
237 case ADMIN_STATE_IN_OVERLAP:
239 addstr("'in << overlap'");
241 case ADMIN_STATE_OUT_OVERLAP:
243 addstr("'out >> overlap'");
245 case ADMIN_STATE_IN_PROCEEDING:
247 addstr("'in << proc'");
249 case ADMIN_STATE_OUT_PROCEEDING:
251 addstr("'out >> proc'");
253 case ADMIN_STATE_IN_ALERTING:
255 addstr("'in << alert'");
257 case ADMIN_STATE_OUT_ALERTING:
259 addstr("'out >> alert'");
261 case ADMIN_STATE_CONNECT:
265 case ADMIN_STATE_IN_DISCONNECT:
267 addstr("'in << disc'");
269 case ADMIN_STATE_OUT_DISCONNECT:
271 addstr("'out >> disc'");
275 addstr("'--NONE--'");
277 if (m[i].u.e.terminal[0])
280 addstr(" terminal=");
282 addstr(m[i].u.e.terminal);
285 SPRINT(buffer, " %s", m[i].u.e.callerid);
290 addstr(m[i].u.e.dialing);
291 if (m[i].u.e.action[0])
296 addstr(m[i].u.e.action);
301 addstr(" park="); /* 9 digits */
303 UCPY(buffer, "\""); /* 9 digits */
305 jj = m[i].u.e.park_len;
308 c = m[i].u.e.park_callid[j];
309 if (c >= 32 && c < 127 && c != '[')
313 UPRINT(buffer+strlen(buffer), "[%02x]", c);
321 switch(m[i].u.e.rx_state)
323 case NOTIFY_STATE_SUSPEND:
324 addstr(" in=suspend");
326 case NOTIFY_STATE_HOLD:
329 case NOTIFY_STATE_CONFERENCE:
330 addstr(" in=conference");
333 switch(m[i].u.e.tx_state)
335 case NOTIFY_STATE_SUSPEND:
336 addstr(" out=suspend");
338 case NOTIFY_STATE_HOLD:
341 case NOTIFY_STATE_CONFERENCE:
342 addstr(" out=conference");
350 if (m[i].u.e.crypt) /* crypt on */
360 /* loop all related ports */
362 j = msg->u.s.interfaces+msg->u.s.joins+msg->u.s.epoints;
363 jj = j + msg->u.s.ports;
366 if (m[j].u.p.epoint == epoint)
369 move(++line>1?line:1, 1);
374 move(line>1?line:1, 5);
377 move(line>1?line:1, 8);
378 if (line+2 >= LINES) break;
379 line = debug_port(msg, m, line, j, vline);
380 if (line+2 >= LINES) break;
387 move(ltee>1?line:1, 5);
393 int debug_join(struct admin_message *msg, struct admin_message *m, int line, int i)
395 unsigned long join = m[i].u.j.serial;
400 SPRINT(buffer,"JOIN(%d)", join);
402 if (m[i].u.j.partyline)
405 addstr(" partyline=");
407 SPRINT(buffer, "%d\n", m[i].u.j.partyline);
410 if (m[i].u.j.remote[0])
415 SPRINT(buffer, "%s\n", m[i].u.j.remote);
418 /* find number of epoints */
419 j = msg->u.s.interfaces+msg->u.s.joins;
420 jj = j + msg->u.s.epoints;
424 if (m[j].u.e.join == join)
428 /* loop all related endpoints */
429 j = msg->u.s.interfaces+msg->u.s.joins;
430 jj = j + msg->u.s.epoints;
433 if (m[j].u.e.join == join)
436 move(++line>1?line:1, 1);
442 move(line>1?line:1, 4);
443 if (line+2 >= LINES) break;
444 line = debug_epoint(msg, m, line, j, i?1:0);
445 if (line+2 >= LINES) break;
452 char *admin_state(int sock, char *argv[])
454 struct admin_message msg,
458 int line, offset = 0, hoffset = 0;
467 char enter_string[128] = "", ch;
469 struct timeval select_tv;
471 /* flush logfile name */
478 /* send reload command */
479 memset(&msg, 0, sizeof(msg));
480 msg.message = ADMIN_REQUEST_STATE;
481 // printf("sizeof=%d\n",sizeof(msg));fflush(stdout);
482 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
485 return("Broken pipe while sending command.");
488 /* receive response */
489 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
492 return("Broken pipe while receiving response.");
495 if (msg.message != ADMIN_RESPONSE_STATE)
498 return("Response not valid. Expecting state response.");
500 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
501 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
506 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off)
510 // fprintf(stderr, "got=%d expected=%d\n", i, num*sizeof(struct admin_message));
512 return("Broken pipe while receiving state infos.");
514 if (len < num*(int)sizeof(struct admin_message))
523 // fprintf("getting =%d interfaces\n", msg.u.s.interfaces);
524 while(i < msg.u.s.interfaces)
526 // fprintf(stderr, "j=%d message=%d\n", j, m[j].message);
527 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE)
531 return("Response not valid. Expecting interface information.");
537 while(i < msg.u.s.remotes)
539 if (m[j].message != ADMIN_RESPONSE_S_REMOTE)
543 return("Response not valid. Expecting remote application information.");
549 while(i < msg.u.s.joins)
551 if (m[j].message != ADMIN_RESPONSE_S_JOIN)
555 return("Response not valid. Expecting join information.");
561 while(i < msg.u.s.epoints)
563 if (m[j].message != ADMIN_RESPONSE_S_EPOINT)
567 return("Response not valid. Expecting endpoint information.");
573 while(i < msg.u.s.ports)
575 if (m[j].message != ADMIN_RESPONSE_S_PORT)
579 return("Response not valid. Expecting port information.");
584 // now j is the number of message blocks
592 if (!!strcmp(logfile, msg.u.s.logfile))
594 SCPY(logfile, msg.u.s.logfile);
602 logline[i][1] = '\0';
606 logfh = open(logfile, O_RDONLY|O_NONBLOCK);
609 /* seek at the end -8000 chars */
610 lseek(logfh, -8000, SEEK_END);
611 /* if not at the beginning, read until endofline */
612 logline[logcur % LOGLINES][0] = '\0';
613 l = read(logfh, logline[logcur % LOGLINES], sizeof(logline[logcur % LOGLINES])-1);
616 /* read first line and skip junk */
617 logline[logcur % LOGLINES][l] = '\0';
618 if ((p = strchr(logline[logcur % LOGLINES],'\n')))
621 SCPY(logline[logcur % LOGLINES], p+1);
622 SCPY(logline[(logcur-1) % LOGLINES], "...");
634 ll = strlen(logline[logcur % LOGLINES]);
635 l = read(logfh, logline[logcur % LOGLINES]+ll, sizeof(logline[logcur % LOGLINES])-ll-1);
638 logline[logcur % LOGLINES][ll+l] = '\0';
640 /* put data to lines */
641 while ((p = strchr(logline[logcur % LOGLINES],'\n')))
645 SCPY(logline[logcur % LOGLINES], p+1);
647 /* if line is full without return, go next line */
648 if (strlen(logline[logcur % LOGLINES]) == sizeof(logline[logcur % LOGLINES])-1)
651 logline[logcur % LOGLINES][0] = '\0';
656 /* display interfaces */
657 if (show_interfaces > 0)
661 ii = i + msg.u.s.interfaces;
664 /* show interface summary */
665 move(++line>1?line:1, 0);
667 if (m[i].u.i.block >= 2)
669 SPRINT(buffer, "%s (%d)%s", m[i].u.i.interface_name, m[i].u.i.portnum, (m[i].u.i.extension)?" (extension)":"");
672 addstr(" not loaded");
675 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);
677 if (m[i].u.i.ptp || !m[i].u.i.ntmode)
679 color((m[i].u.i.l2link)?green:red);
680 addstr((m[i].u.i.l2link)?" L2 UP":" L2 down");
682 color((m[i].u.i.l1link)?green:blue);
683 addstr((m[i].u.i.l1link)?" L1 ACTIVE":" L1 inactive");
699 if (m[i].u.i.slip_tx || m[i].u.i.slip_rx)
702 SPRINT(buffer, " SLIP(tx:%d rx:%d)", m[i].u.i.slip_tx, m[i].u.i.slip_rx);
710 if (line+2 >= LINES) goto end;
712 if (show_interfaces > 1)
716 jj = m[i].u.i.channels;
719 /* show all channels */
720 if (show_interfaces>2 || m[i].u.i.busy[j]>0)
723 /* show left side / right side */
724 if ((k & 1) && (COLS > 70))
726 move(line>1?line:1,4+((COLS-4)/2));
729 move(++line>1?line:1, 1);
736 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
738 SPRINT(buffer,"B%2d: ", j+1);
740 switch(m[i].u.i.busy[j])
743 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block)
753 case B_STATE_ACTIVATING:
761 case B_STATE_DEACTIVATING:
765 case B_STATE_EXPORTING:
773 case B_STATE_IMPORTING:
778 if (m[i].u.i.port[j])
780 /* search for port */
781 l = msg.u.s.interfaces+msg.u.s.joins+msg.u.s.epoints;
782 ll = l+msg.u.s.ports;
785 if (m[l].u.p.serial == m[i].u.i.port[j])
787 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
798 move(line>1?line:1, 1);
809 move(line>1?line:1, 1);
812 if (line+2 >= LINES) goto end;
813 /* show summary if no channels were shown */
814 if (show_interfaces<2 && ltee==0)
817 move(++line>1?line:1, 1);
820 if (m[i].u.i.l2link && m[i].u.i.block==0)
823 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
827 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
831 if (line+2 >= LINES) goto end;
838 ii = i + msg.u.s.remotes;
841 /* show remote summary */
842 move(++line>1?line:1, 0);
844 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
850 if (line+2 >= LINES) goto end;
852 /* display calls (brief) */
856 i = msg.u.s.interfaces+msg.u.s.joins;
857 ii = i+msg.u.s.epoints;
860 /* for each endpoint... */
863 move(++line>1?line:1, 0);
865 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
868 if (m[i].u.e.terminal[0])
872 addstr(m[i].u.e.terminal);
876 SPRINT(buffer, " %s", m[i].u.e.callerid);
881 SPRINT(buffer, "%s", m[i].u.e.dialing);
883 if (m[i].u.e.action[0])
888 addstr(m[i].u.e.action);
890 if (line+2 >= LINES) goto end;
895 j = msg.u.s.interfaces;
896 jj = j+msg.u.s.joins;
899 /* for each call... */
900 move(++line>1?line:1, 0);
902 SPRINT(buffer, "(%d):", m[j].u.j.serial);
904 i = msg.u.s.interfaces+msg.u.s.joins;
905 ii = i+msg.u.s.epoints;
908 /* for each endpoint... */
909 if (m[i].u.e.join == m[j].u.j.serial)
912 SPRINT(buffer, " (%d)", m[i].u.e.serial);
915 if (m[i].u.e.terminal[0])
919 addstr(m[i].u.e.terminal);
923 SPRINT(buffer, "-%s", m[i].u.e.callerid);
928 SPRINT(buffer, "%s", m[i].u.e.dialing);
934 if (line+2 >= LINES) goto end;
939 if (line+2 >= LINES) goto end;
941 /* display calls (structurd) */
944 /* show all ports with no epoint */
946 i = msg.u.s.interfaces+msg.u.s.joins+msg.u.s.epoints;
947 ii = i+msg.u.s.ports;
950 if (!m[i].u.p.epoint)
952 move(++line>1?line:1, 8);
953 if (line+2 >= LINES) goto end;
954 line = debug_port(&msg, m, line, i, 0);
955 if (line+2 >= LINES) goto end;
962 if (line+2 >= LINES) goto end;
964 /* show all epoints with no call */
966 i = msg.u.s.interfaces+msg.u.s.joins;
967 ii = i+msg.u.s.epoints;
972 move(++line>1?line:1, 4);
973 if (line+2 >= LINES) goto end;
974 line = debug_epoint(&msg, m, line, i, 0);
975 if (line+2 >= LINES) goto end;
982 if (line+2 >= LINES) goto end;
986 i = msg.u.s.interfaces;
987 ii = i+msg.u.s.joins;
990 move(++line>1?line:1, 0);
991 if (line+2 >= LINES) goto end;
992 line = debug_join(&msg, m, line, i);
993 if (line+2 >= LINES) goto end;
999 if (line+2 >= LINES) goto end;
1008 move(line++>1?line-1:1, 0);
1010 hline(ACS_HLINE, COLS);
1013 l = logcur-(LINES-line-2);
1015 if (ll-l >= LOGLINES)
1019 move(line++>1?line-1:1, 0);
1020 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
1021 SCPY(buffer, logline[l % LOGLINES] + hoffset);
1024 if (COLS < (int)strlen(buffer))
1026 buffer[COLS-1] = '\0';
1041 /* display name/time */
1043 // hline(' ', COLS);
1046 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
1047 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
1052 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1053 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1054 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1057 /* displeay head line */
1060 hline(ACS_HLINE, COLS);
1064 SPRINT(buffer, "Offset +%d", offset);
1071 SPRINT(buffer, "H-Offset +%d", hoffset);
1078 hline(ACS_HLINE, COLS);
1083 SPRINT(buffer, "-> %s", enter_string);
1087 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1093 if (lastlines!=LINES || lastcols!=COLS)
1102 /* user input in enter mode */
1110 if (!enter_string[0])
1113 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1114 if (!!strncmp(enter_string, "interface", 10) &&
1115 !!strncmp(enter_string, "route", 6) &&
1116 !!strncmp(enter_string, "release ", 8) &&
1117 !!strncmp(enter_string, "block ", 6) &&
1118 !!strncmp(enter_string, "unblock ", 8) &&
1119 !!strncmp(enter_string, "unload ", 7))
1121 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1122 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1123 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1124 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1125 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1126 SPRINT(logline[logcur++ % LOGLINES], "unblock <port> (unblock port for further calls, load if not loaded)");
1127 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1130 /* applend output to log window */
1131 SPRINT(buffer, "%s %s", argv[0], enter_string);
1132 fp = popen(buffer, "r");
1135 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1136 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1140 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1143 logline[logcur % LOGLINES][0] = '\0';
1144 enter_string[0] = '\0';
1147 if (ch>=32 && ch<=126)
1149 SCCAT(enter_string, ch);
1155 if (ch==8 || ch==127)
1157 if (enter_string[0])
1158 enter_string[strlen(enter_string)-1] = '\0';
1169 FD_ZERO(&select_rfds);
1170 FD_SET(0, &select_rfds);
1171 select_tv.tv_sec = 0;
1172 select_tv.tv_usec = 250000;
1173 select(1, &select_rfds, NULL, NULL, &select_tv);
1178 /* user input in normal mode */
1181 case 12: /* refresh */
1192 case 'i': /* toggle interface */
1194 if (show_interfaces > 3) show_interfaces = 0;
1197 case 'c': /* toggle calls */
1199 if (show_calls > 2) show_calls = 0;
1202 case 'l': /* toggle log */
1204 if (show_log > 1) show_log = 0;
1207 case '+': /* scroll down */
1211 case '-': /* scroll up */
1216 case '*': /* scroll right */
1220 case '/': /* scroll left */
1225 case 10: /* entermode */
1230 FD_ZERO(&select_rfds);
1231 FD_SET(0, &select_rfds);
1232 select_tv.tv_sec = 0;
1233 select_tv.tv_usec = 250000;
1234 select(1, &select_rfds, NULL, NULL, &select_tv);
1239 /* check for logfh */
1244 /* cleanup curses and exit */
1252 * Send command and show error message.
1254 char *admin_cmd(int sock, int mode, char *extension, char *number)
1256 static struct admin_message msg;
1258 /* send reload command */
1259 memset(&msg, 0, sizeof(msg));
1262 case MODE_INTERFACE:
1263 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1266 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1269 msg.message = ADMIN_REQUEST_CMD_DIAL;
1270 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1273 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1274 SCPY(msg.u.x.message, number);
1277 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1278 msg.u.x.portnum = atoi(number);
1282 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1283 msg.u.x.portnum = atoi(number);
1287 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1288 msg.u.x.portnum = atoi(number);
1293 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1294 return("Broken pipe while sending command.");
1296 /* receive response */
1297 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1298 return("Broken pipe while receiving response.");
1301 case MODE_INTERFACE:
1302 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1303 return("Response not valid.");
1306 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1307 return("Response not valid.");
1310 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1311 return("Response not valid.");
1314 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1315 return("Response not valid.");
1320 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1321 return("Response not valid.");
1325 /* process response */
1328 return(msg.u.x.message);
1330 printf("Command successfull.\n");
1338 #define GET_NOW() { \
1339 gettimeofday(&now_tv, &now_tz); \
1340 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1342 char *admin_testcall(int sock, int argc, char *argv[])
1344 static struct admin_message msg;
1346 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1348 double timer = 0, now_d;
1349 unsigned long on = 1;
1350 struct timeval now_tv;
1351 struct timezone now_tz;
1353 printf("pid=%d\n", getpid()); fflush(stdout);
1357 if (!strcmp(argv[ar], "--setup-timeout"))
1361 return("Missing setup timeout value.\n");
1362 stimeout = atoi(argv[ar]);
1365 if (!strcmp(argv[ar], "--proceeding-timeout"))
1369 return("Missing proceeding timeout value.\n");
1370 ptimeout = atoi(argv[ar]);
1373 if (!strcmp(argv[ar], "--alerting-timeout"))
1377 return("Missing alerting timeout value.\n");
1378 atimeout = atoi(argv[ar]);
1381 if (!strcmp(argv[ar], "--connect-timeout"))
1385 return("Missing connect timeout value.\n");
1386 ctimeout = atoi(argv[ar]);
1394 /* send reload command */
1395 memset(&msg, 0, sizeof(msg));
1396 msg.message = ADMIN_CALL_SETUP;
1397 msg.u.call.present = 1;
1401 SCPY(msg.u.call.interface, argv[ar]);
1406 SCPY(msg.u.call.callerid, argv[ar]);
1411 SCPY(msg.u.call.dialing, argv[ar]);
1416 if (argv[ar][0] == 'r')
1417 msg.u.call.present = 0;
1420 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1421 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1422 msg.u.call.bc_info1 = 0;
1424 msg.u.call.exthlc = 0;
1426 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1428 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1431 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1432 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1436 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1437 if (msg.u.call.bc_info1 < 0)
1438 msg.u.call.bc_info1 = 0;
1440 msg.u.call.bc_info1 |= 0x80;
1444 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1445 if (msg.u.call.hlc < 0)
1448 msg.u.call.hlc |= 0x80;
1452 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1453 if (msg.u.call.exthlc < 0)
1454 msg.u.call.exthlc = 0;
1456 msg.u.call.exthlc |= 0x80;
1460 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1461 return("Broken pipe while sending command.");
1463 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1464 return("Failed to set socket into non-blocking IO.");
1469 timer = now_d + (double)stimeout;
1472 /* receive response */
1474 l = read(sock, &msg, sizeof(msg));
1477 if (errno == EWOULDBLOCK)
1484 printf("Timeout\n"); fflush(stdout);
1491 return("Broken pipe while receiving response.");
1493 if (l != sizeof(msg))
1494 return("Response has unexpected message size.");
1497 case ADMIN_CALL_SETUP_ACK:
1498 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1501 case ADMIN_CALL_PROCEEDING:
1502 printf("PROCEEDING\n"); fflush(stdout);
1506 timer = now_d + (double)ptimeout;
1510 case ADMIN_CALL_ALERTING:
1511 printf("ALERTING\n"); fflush(stdout);
1515 timer = now_d + (double)atimeout;
1519 case ADMIN_CALL_CONNECT:
1520 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1524 timer = now_d + (double)ctimeout;
1528 case ADMIN_CALL_NOTIFY:
1529 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1532 case ADMIN_CALL_DISCONNECT:
1533 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);
1536 case ADMIN_CALL_RELEASE:
1537 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);
1541 return("Response not valid.");
1544 printf("Call released.\n"); fflush(stdout);
1552 char *admin_trace(int sock, int argc, char *argv[])
1554 static struct admin_message msg;
1558 if (argc > 2) if (!strcasecmp(argv[2], "help"))
1560 printf("Trace Help\n----------\n");
1561 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1562 printf("By default a complete trace is shown in detailed format.\n");
1563 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1564 printf("Use filter values to select specific trace messages.\n");
1565 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1566 printf("Filters:\n");
1567 printf(" category=<mask bits>\n");
1568 printf(" 0x01 = CH: channel object trace\n");
1569 printf(" 0x02 = EP: endpoint object trace\n");
1570 printf(" port=<mISDN port> select only given port for trace\n");
1571 printf(" interface=<interface name> select only given interface for trace\n");
1572 printf(" caller=<caller id> select only given caller id for trace\n");
1573 printf(" dialing=<number> select only given dialed number for trace\n");
1577 /* init trace request */
1578 memset(&msg, 0, sizeof(msg));
1579 msg.message = ADMIN_TRACE_REQUEST;
1580 msg.u.trace_req.detail = 3;
1586 if (!strcasecmp(argv[i], "brief"))
1587 msg.u.trace_req.detail = 1;
1588 else if (!strcasecmp(argv[i], "short"))
1589 msg.u.trace_req.detail = 2;
1590 else if (!strncasecmp(argv[i], "category=", 9))
1591 msg.u.trace_req.category = atoi(argv[i]+9);
1592 else if (!strncasecmp(argv[i], "port=", 5))
1593 msg.u.trace_req.port = atoi(argv[i]+5);
1594 else if (!strncasecmp(argv[i], "interface=", 10))
1595 SCPY(msg.u.trace_req.interface, argv[i]+10);
1596 else if (!strncasecmp(argv[i], "caller=", 7))
1597 SCPY(msg.u.trace_req.caller, argv[i]+7);
1598 else if (!strncasecmp(argv[i], "dialing=", 8))
1599 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1600 else return("Invalid trace option, try 'trace help'.");
1605 /* send trace request */
1606 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1607 return("Broken pipe while sending trace request.");
1609 /* receive response */
1611 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1612 return("Broken pipe while receiving response.");
1614 if (msg.message != ADMIN_TRACE_RESPONSE)
1615 return("Response not valid.");
1617 printf("%s", msg.u.trace_rsp.text);
1625 int main(int argc, char *argv[])
1628 char *socket_name = SOCKET_NAME;
1630 struct sockaddr_un sock_address;
1639 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1640 printf("state - View current states using graphical console output.\n");
1641 printf("interface - Tell LCR to reload \"interface.conf\".\n");
1642 printf("route - Tell LCR to reload \"route.conf\".\n");
1643 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1644 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1645 printf("block <port> - Block given port.\n");
1646 printf("unblock <port> - Unblock given port.\n");
1647 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1648 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1649 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1650 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1651 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1652 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1653 printf(" -> Use 'trace help' to see filter description.\n");
1659 if (!(strcasecmp(argv[1],"state")))
1663 if (!(strcasecmp(argv[1],"interface")))
1665 mode = MODE_INTERFACE;
1667 if (!(strcasecmp(argv[1],"route")))
1671 if (!(strcasecmp(argv[1],"dial")))
1677 if (!(strcasecmp(argv[1],"release")))
1681 mode = MODE_RELEASE;
1683 if (!(strcasecmp(argv[1],"unblock")))
1687 mode = MODE_UNBLOCK;
1689 if (!(strcasecmp(argv[1],"block")))
1695 if (!(strcasecmp(argv[1],"unload")))
1701 if (!(strcasecmp(argv[1],"testcall")))
1705 mode = MODE_TESTCALL;
1707 if (!(strcasecmp(argv[1],"trace")))
1717 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1719 fprintf(stderr, "Failed to create socket.\n");
1722 memset(&sock_address, 0, sizeof(sock_address));
1723 sock_address.sun_family = PF_UNIX;
1724 UCPY(sock_address.sun_path, socket_name);
1725 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1728 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1736 ret = admin_state(sock, argv);
1739 case MODE_INTERFACE:
1741 ret = admin_cmd(sock, mode, NULL, NULL);
1745 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1752 ret = admin_cmd(sock, mode, NULL, argv[2]);
1756 ret = admin_testcall(sock, argc, argv);
1760 ret = admin_trace(sock, argc, argv);
1765 /* now we say good bye */
1768 // if (!strncasecmp(ret, "Broken Pipe", 11))
1770 printf("%s\n", ret);