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");
689 if (line+2 >= LINES) goto end;
691 if (show_interfaces > 1)
695 jj = m[i].u.i.channels;
698 /* show all channels */
699 if (show_interfaces>2 || m[i].u.i.busy[j]>0)
702 /* show left side / right side */
703 if ((k & 1) && (COLS > 70))
705 move(line>1?line:1,4+((COLS-4)/2));
708 move(++line>1?line:1, 1);
715 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
717 SPRINT(buffer,"B%2d: ", j+1);
719 switch(m[i].u.i.busy[j])
722 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block)
732 case B_STATE_ACTIVATING:
740 case B_STATE_DEACTIVATING:
744 case B_STATE_EXPORTING:
752 case B_STATE_IMPORTING:
757 if (m[i].u.i.port[j])
759 /* search for port */
760 l = msg.u.s.interfaces+msg.u.s.joins+msg.u.s.epoints;
761 ll = l+msg.u.s.ports;
764 if (m[l].u.p.serial == m[i].u.i.port[j])
766 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
777 move(line>1?line:1, 1);
788 move(line>1?line:1, 1);
791 if (line+2 >= LINES) goto end;
792 /* show summary if no channels were shown */
793 if (show_interfaces<2 && ltee==0)
796 move(++line>1?line:1, 1);
799 if (m[i].u.i.l2link && m[i].u.i.block==0)
802 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
806 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
810 if (line+2 >= LINES) goto end;
817 ii = i + msg.u.s.remotes;
820 /* show remote summary */
821 move(++line>1?line:1, 0);
823 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
829 if (line+2 >= LINES) goto end;
831 /* display calls (brief) */
835 i = msg.u.s.interfaces+msg.u.s.joins;
836 ii = i+msg.u.s.epoints;
839 /* for each endpoint... */
842 move(++line>1?line:1, 0);
844 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
847 if (m[i].u.e.terminal[0])
851 addstr(m[i].u.e.terminal);
855 SPRINT(buffer, " %s", m[i].u.e.callerid);
860 SPRINT(buffer, "%s", m[i].u.e.dialing);
862 if (m[i].u.e.action[0])
867 addstr(m[i].u.e.action);
869 if (line+2 >= LINES) goto end;
874 j = msg.u.s.interfaces;
875 jj = j+msg.u.s.joins;
878 /* for each call... */
879 move(++line>1?line:1, 0);
881 SPRINT(buffer, "(%d):", m[j].u.j.serial);
883 i = msg.u.s.interfaces+msg.u.s.joins;
884 ii = i+msg.u.s.epoints;
887 /* for each endpoint... */
888 if (m[i].u.e.join == m[j].u.j.serial)
891 SPRINT(buffer, " (%d)", m[i].u.e.serial);
894 if (m[i].u.e.terminal[0])
898 addstr(m[i].u.e.terminal);
902 SPRINT(buffer, "-%s", m[i].u.e.callerid);
907 SPRINT(buffer, "%s", m[i].u.e.dialing);
913 if (line+2 >= LINES) goto end;
918 if (line+2 >= LINES) goto end;
920 /* display calls (structurd) */
923 /* show all ports with no epoint */
925 i = msg.u.s.interfaces+msg.u.s.joins+msg.u.s.epoints;
926 ii = i+msg.u.s.ports;
929 if (!m[i].u.p.epoint)
931 move(++line>1?line:1, 8);
932 if (line+2 >= LINES) goto end;
933 line = debug_port(&msg, m, line, i, 0);
934 if (line+2 >= LINES) goto end;
941 if (line+2 >= LINES) goto end;
943 /* show all epoints with no call */
945 i = msg.u.s.interfaces+msg.u.s.joins;
946 ii = i+msg.u.s.epoints;
951 move(++line>1?line:1, 4);
952 if (line+2 >= LINES) goto end;
953 line = debug_epoint(&msg, m, line, i, 0);
954 if (line+2 >= LINES) goto end;
961 if (line+2 >= LINES) goto end;
965 i = msg.u.s.interfaces;
966 ii = i+msg.u.s.joins;
969 move(++line>1?line:1, 0);
970 if (line+2 >= LINES) goto end;
971 line = debug_join(&msg, m, line, i);
972 if (line+2 >= LINES) goto end;
978 if (line+2 >= LINES) goto end;
987 move(line++>1?line-1:1, 0);
989 hline(ACS_HLINE, COLS);
992 l = logcur-(LINES-line-2);
994 if (ll-l >= LOGLINES)
998 move(line++>1?line-1:1, 0);
999 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
1000 SCPY(buffer, logline[l % LOGLINES] + hoffset);
1003 if (COLS < (int)strlen(buffer))
1005 buffer[COLS-1] = '\0';
1020 /* display name/time */
1022 // hline(' ', COLS);
1025 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
1026 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
1031 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1032 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1033 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1036 /* displeay head line */
1039 hline(ACS_HLINE, COLS);
1043 SPRINT(buffer, "Offset +%d", offset);
1050 SPRINT(buffer, "H-Offset +%d", hoffset);
1057 hline(ACS_HLINE, COLS);
1062 SPRINT(buffer, "-> %s", enter_string);
1066 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1072 if (lastlines!=LINES || lastcols!=COLS)
1081 /* user input in enter mode */
1089 if (!enter_string[0])
1092 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1093 if (!!strncmp(enter_string, "interface", 10) &&
1094 !!strncmp(enter_string, "route", 6) &&
1095 !!strncmp(enter_string, "release ", 8) &&
1096 !!strncmp(enter_string, "block ", 6) &&
1097 !!strncmp(enter_string, "unblock ", 8) &&
1098 !!strncmp(enter_string, "unload ", 7))
1100 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1101 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1102 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1103 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1104 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1105 SPRINT(logline[logcur++ % LOGLINES], "unblock <port> (unblock port for further calls, load if not loaded)");
1106 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1109 /* applend output to log window */
1110 SPRINT(buffer, "%s %s", argv[0], enter_string);
1111 fp = popen(buffer, "r");
1114 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1115 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1119 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1122 logline[logcur % LOGLINES][0] = '\0';
1123 enter_string[0] = '\0';
1126 if (ch>=32 && ch<=126)
1128 SCCAT(enter_string, ch);
1134 if (ch==8 || ch==127)
1136 if (enter_string[0])
1137 enter_string[strlen(enter_string)-1] = '\0';
1148 FD_ZERO(&select_rfds);
1149 FD_SET(0, &select_rfds);
1150 select_tv.tv_sec = 0;
1151 select_tv.tv_usec = 250000;
1152 select(1, &select_rfds, NULL, NULL, &select_tv);
1157 /* user input in normal mode */
1160 case 12: /* refresh */
1171 case 'i': /* toggle interface */
1173 if (show_interfaces > 3) show_interfaces = 0;
1176 case 'c': /* toggle calls */
1178 if (show_calls > 2) show_calls = 0;
1181 case 'l': /* toggle log */
1183 if (show_log > 1) show_log = 0;
1186 case '+': /* scroll down */
1190 case '-': /* scroll up */
1195 case '*': /* scroll right */
1199 case '/': /* scroll left */
1204 case 10: /* entermode */
1209 FD_ZERO(&select_rfds);
1210 FD_SET(0, &select_rfds);
1211 select_tv.tv_sec = 0;
1212 select_tv.tv_usec = 250000;
1213 select(1, &select_rfds, NULL, NULL, &select_tv);
1218 /* check for logfh */
1223 /* cleanup curses and exit */
1231 * Send command and show error message.
1233 char *admin_cmd(int sock, int mode, char *extension, char *number)
1235 static struct admin_message msg;
1237 /* send reload command */
1238 memset(&msg, 0, sizeof(msg));
1241 case MODE_INTERFACE:
1242 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1245 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1248 msg.message = ADMIN_REQUEST_CMD_DIAL;
1249 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1252 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1253 SCPY(msg.u.x.message, number);
1256 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1257 msg.u.x.portnum = atoi(number);
1261 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1262 msg.u.x.portnum = atoi(number);
1266 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1267 msg.u.x.portnum = atoi(number);
1272 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1273 return("Broken pipe while sending command.");
1275 /* receive response */
1276 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1277 return("Broken pipe while receiving response.");
1280 case MODE_INTERFACE:
1281 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1282 return("Response not valid.");
1285 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1286 return("Response not valid.");
1289 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1290 return("Response not valid.");
1293 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1294 return("Response not valid.");
1299 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1300 return("Response not valid.");
1304 /* process response */
1307 return(msg.u.x.message);
1309 printf("Command successfull.\n");
1317 #define GET_NOW() { \
1318 gettimeofday(&now_tv, &now_tz); \
1319 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1321 char *admin_testcall(int sock, int argc, char *argv[])
1323 static struct admin_message msg;
1325 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1327 double timer = 0, now_d;
1328 unsigned long on = 1;
1329 struct timeval now_tv;
1330 struct timezone now_tz;
1332 printf("pid=%d\n", getpid()); fflush(stdout);
1336 if (!strcmp(argv[ar], "--setup-timeout"))
1340 return("Missing setup timeout value.\n");
1341 stimeout = atoi(argv[ar]);
1344 if (!strcmp(argv[ar], "--proceeding-timeout"))
1348 return("Missing proceeding timeout value.\n");
1349 ptimeout = atoi(argv[ar]);
1352 if (!strcmp(argv[ar], "--alerting-timeout"))
1356 return("Missing alerting timeout value.\n");
1357 atimeout = atoi(argv[ar]);
1360 if (!strcmp(argv[ar], "--connect-timeout"))
1364 return("Missing connect timeout value.\n");
1365 ctimeout = atoi(argv[ar]);
1373 /* send reload command */
1374 memset(&msg, 0, sizeof(msg));
1375 msg.message = ADMIN_CALL_SETUP;
1376 msg.u.call.present = 1;
1380 SCPY(msg.u.call.interface, argv[ar]);
1385 SCPY(msg.u.call.callerid, argv[ar]);
1390 SCPY(msg.u.call.dialing, argv[ar]);
1395 if (argv[ar][0] == 'r')
1396 msg.u.call.present = 0;
1399 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1400 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1401 msg.u.call.bc_info1 = 0;
1403 msg.u.call.exthlc = 0;
1405 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1407 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1410 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1411 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1415 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1416 if (msg.u.call.bc_info1 < 0)
1417 msg.u.call.bc_info1 = 0;
1419 msg.u.call.bc_info1 |= 0x80;
1423 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1424 if (msg.u.call.hlc < 0)
1427 msg.u.call.hlc |= 0x80;
1431 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1432 if (msg.u.call.exthlc < 0)
1433 msg.u.call.exthlc = 0;
1435 msg.u.call.exthlc |= 0x80;
1439 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1440 return("Broken pipe while sending command.");
1442 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1443 return("Failed to set socket into non-blocking IO.");
1448 timer = now_d + (double)stimeout;
1451 /* receive response */
1453 l = read(sock, &msg, sizeof(msg));
1456 if (errno == EWOULDBLOCK)
1463 printf("Timeout\n"); fflush(stdout);
1470 return("Broken pipe while receiving response.");
1472 if (l != sizeof(msg))
1473 return("Response has unexpected message size.");
1476 case ADMIN_CALL_SETUP_ACK:
1477 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1480 case ADMIN_CALL_PROCEEDING:
1481 printf("PROCEEDING\n"); fflush(stdout);
1485 timer = now_d + (double)ptimeout;
1489 case ADMIN_CALL_ALERTING:
1490 printf("ALERTING\n"); fflush(stdout);
1494 timer = now_d + (double)atimeout;
1498 case ADMIN_CALL_CONNECT:
1499 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1503 timer = now_d + (double)ctimeout;
1507 case ADMIN_CALL_NOTIFY:
1508 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1511 case ADMIN_CALL_DISCONNECT:
1512 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);
1515 case ADMIN_CALL_RELEASE:
1516 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);
1520 return("Response not valid.");
1523 printf("Call released.\n"); fflush(stdout);
1531 char *admin_trace(int sock, int argc, char *argv[])
1533 static struct admin_message msg;
1537 if (argc > 2) if (!strcasecmp(argv[2], "help"))
1539 printf("Trace Help\n----------\n");
1540 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1541 printf("By default a complete trace is shown in detailed format.\n");
1542 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1543 printf("Use filter values to select specific trace messages.\n");
1544 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1545 printf("Filters:\n");
1546 printf(" category=<mask bits>\n");
1547 printf(" 0x01 = CH: channel object trace\n");
1548 printf(" 0x02 = EP: endpoint object trace\n");
1549 printf(" port=<mISDN port> select only given port for trace\n");
1550 printf(" interface=<interface name> select only given interface for trace\n");
1551 printf(" caller=<caller id> select only given caller id for trace\n");
1552 printf(" dialing=<number> select only given dialed number for trace\n");
1556 /* init trace request */
1557 memset(&msg, 0, sizeof(msg));
1558 msg.message = ADMIN_TRACE_REQUEST;
1559 msg.u.trace_req.detail = 3;
1565 if (!strcasecmp(argv[i], "brief"))
1566 msg.u.trace_req.detail = 1;
1567 else if (!strcasecmp(argv[i], "short"))
1568 msg.u.trace_req.detail = 2;
1569 else if (!strncasecmp(argv[i], "category=", 9))
1570 msg.u.trace_req.category = atoi(argv[i]+9);
1571 else if (!strncasecmp(argv[i], "port=", 5))
1572 msg.u.trace_req.port = atoi(argv[i]+5);
1573 else if (!strncasecmp(argv[i], "interface=", 10))
1574 SCPY(msg.u.trace_req.interface, argv[i]+10);
1575 else if (!strncasecmp(argv[i], "caller=", 7))
1576 SCPY(msg.u.trace_req.caller, argv[i]+7);
1577 else if (!strncasecmp(argv[i], "dialing=", 8))
1578 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1579 else return("Invalid trace option, try 'trace help'.");
1584 /* send trace request */
1585 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1586 return("Broken pipe while sending trace request.");
1588 /* receive response */
1590 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1591 return("Broken pipe while receiving response.");
1593 if (msg.message != ADMIN_TRACE_RESPONSE)
1594 return("Response not valid.");
1596 printf("%s", msg.u.trace_rsp.text);
1604 int main(int argc, char *argv[])
1607 char *socket_name = SOCKET_NAME;
1609 struct sockaddr_un sock_address;
1618 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1619 printf("state - View current states using graphical console output.\n");
1620 printf("interface - Tell LCR to reload \"interface.conf\".\n");
1621 printf("route - Tell LCR to reload \"route.conf\".\n");
1622 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1623 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1624 printf("block <port> - Block given port.\n");
1625 printf("unblock <port> - Unblock given port.\n");
1626 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1627 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1628 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1629 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1630 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1631 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1632 printf(" -> Use 'trace help' to see filter description.\n");
1638 if (!(strcasecmp(argv[1],"state")))
1642 if (!(strcasecmp(argv[1],"interface")))
1644 mode = MODE_INTERFACE;
1646 if (!(strcasecmp(argv[1],"route")))
1650 if (!(strcasecmp(argv[1],"dial")))
1656 if (!(strcasecmp(argv[1],"release")))
1660 mode = MODE_RELEASE;
1662 if (!(strcasecmp(argv[1],"unblock")))
1666 mode = MODE_UNBLOCK;
1668 if (!(strcasecmp(argv[1],"block")))
1674 if (!(strcasecmp(argv[1],"unload")))
1680 if (!(strcasecmp(argv[1],"testcall")))
1684 mode = MODE_TESTCALL;
1686 if (!(strcasecmp(argv[1],"trace")))
1696 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1698 fprintf(stderr, "Failed to create socket.\n");
1701 memset(&sock_address, 0, sizeof(sock_address));
1702 sock_address.sun_family = PF_UNIX;
1703 UCPY(sock_address.sun_path, socket_name);
1704 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1707 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1715 ret = admin_state(sock, argv);
1718 case MODE_INTERFACE:
1720 ret = admin_cmd(sock, mode, NULL, NULL);
1724 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1731 ret = admin_cmd(sock, mode, NULL, argv[2]);
1735 ret = admin_testcall(sock, argc, argv);
1739 ret = admin_trace(sock, argc, argv);
1744 /* now we say good bye */
1747 // if (!strncasecmp(ret, "Broken Pipe", 11))
1749 printf("%s\n", ret);