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 int 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 int 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.remotes+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 int 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.remotes+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.remotes+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 > 0)?green:blue);
683 if (m[i].u.i.l1link < 0)
684 addstr(" L1 unknown");
686 addstr((m[i].u.i.l1link)?" L1 ACTIVE":" L1 inactive");
702 if (m[i].u.i.slip_tx || m[i].u.i.slip_rx)
705 SPRINT(buffer, " SLIP(tx:%d rx:%d)", m[i].u.i.slip_tx, m[i].u.i.slip_rx);
713 if (line+2 >= LINES) goto end;
715 if (show_interfaces > 1)
719 jj = m[i].u.i.channels;
722 /* show all channels */
723 if (show_interfaces>2 || m[i].u.i.busy[j]>0)
726 /* show left side / right side */
727 if ((k & 1) && (COLS > 70))
729 move(line>1?line:1,4+((COLS-4)/2));
732 move(++line>1?line:1, 1);
739 SPRINT(buffer,"S%2d: ", j+1+(j>=15));
741 SPRINT(buffer,"B%2d: ", j+1);
743 switch(m[i].u.i.busy[j])
746 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block)
756 case B_STATE_ACTIVATING:
764 case B_STATE_DEACTIVATING:
768 case B_STATE_EXPORTING:
776 case B_STATE_IMPORTING:
781 if (m[i].u.i.port[j])
783 /* search for port */
784 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
785 ll = l+msg.u.s.ports;
788 if (m[l].u.p.serial == m[i].u.i.port[j])
790 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
801 move(line>1?line:1, 1);
812 move(line>1?line:1, 1);
815 if (line+2 >= LINES) goto end;
816 /* show summary if no channels were shown */
817 if (show_interfaces<2 && ltee==0)
820 move(++line>1?line:1, 1);
823 if (m[i].u.i.l2link && m[i].u.i.block==0)
826 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
830 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
834 if (line+2 >= LINES) goto end;
840 i = msg.u.s.interfaces;
841 ii = i + msg.u.s.remotes;
844 /* show remote summary */
845 move(++line>1?line:1, 0);
847 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
853 if (line+2 >= LINES) goto end;
855 /* display calls (brief) */
859 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
860 ii = i+msg.u.s.epoints;
863 /* for each endpoint... */
866 move(++line>1?line:1, 0);
868 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
871 if (m[i].u.e.terminal[0])
875 addstr(m[i].u.e.terminal);
879 SPRINT(buffer, " %s", m[i].u.e.callerid);
884 SPRINT(buffer, "%s", m[i].u.e.dialing);
886 if (m[i].u.e.action[0])
891 addstr(m[i].u.e.action);
893 if (line+2 >= LINES) goto end;
898 j = msg.u.s.interfaces+msg.u.s.remotes;
899 jj = j+msg.u.s.joins;
902 /* for each call... */
903 move(++line>1?line:1, 0);
905 SPRINT(buffer, "(%d):", m[j].u.j.serial);
907 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
908 ii = i+msg.u.s.epoints;
911 /* for each endpoint... */
912 if (m[i].u.e.join == m[j].u.j.serial)
915 SPRINT(buffer, " (%d)", m[i].u.e.serial);
918 if (m[i].u.e.terminal[0])
922 addstr(m[i].u.e.terminal);
926 SPRINT(buffer, "-%s", m[i].u.e.callerid);
931 SPRINT(buffer, "%s", m[i].u.e.dialing);
937 if (line+2 >= LINES) goto end;
942 if (line+2 >= LINES) goto end;
944 /* display calls (structurd) */
947 /* show all ports with no epoint */
949 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
950 ii = i+msg.u.s.ports;
953 if (!m[i].u.p.epoint)
955 move(++line>1?line:1, 8);
956 if (line+2 >= LINES) goto end;
957 line = debug_port(&msg, m, line, i, 0);
958 if (line+2 >= LINES) goto end;
965 if (line+2 >= LINES) goto end;
967 /* show all epoints with no call */
969 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
970 ii = i+msg.u.s.epoints;
975 move(++line>1?line:1, 4);
976 if (line+2 >= LINES) goto end;
977 line = debug_epoint(&msg, m, line, i, 0);
978 if (line+2 >= LINES) goto end;
985 if (line+2 >= LINES) goto end;
989 i = msg.u.s.interfaces+msg.u.s.remotes;
990 ii = i+msg.u.s.joins;
993 move(++line>1?line:1, 0);
994 if (line+2 >= LINES) goto end;
995 line = debug_join(&msg, m, line, i);
996 if (line+2 >= LINES) goto end;
1002 if (line+2 >= LINES) goto end;
1011 move(line++>1?line-1:1, 0);
1013 hline(ACS_HLINE, COLS);
1016 l = logcur-(LINES-line-2);
1018 if (ll-l >= LOGLINES)
1022 move(line++>1?line-1:1, 0);
1023 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
1024 SCPY(buffer, logline[l % LOGLINES] + hoffset);
1027 if (COLS < (int)strlen(buffer))
1029 buffer[COLS-1] = '\0';
1044 /* display name/time */
1046 // hline(' ', COLS);
1049 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
1050 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
1055 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1056 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1057 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1060 /* displeay head line */
1063 hline(ACS_HLINE, COLS);
1067 SPRINT(buffer, "Offset +%d", offset);
1074 SPRINT(buffer, "H-Offset +%d", hoffset);
1081 hline(ACS_HLINE, COLS);
1086 SPRINT(buffer, "-> %s", enter_string);
1090 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1096 if (lastlines!=LINES || lastcols!=COLS)
1105 /* user input in enter mode */
1113 if (!enter_string[0])
1116 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1117 if (!!strncmp(enter_string, "interface", 10) &&
1118 !!strncmp(enter_string, "route", 6) &&
1119 !!strncmp(enter_string, "release ", 8) &&
1120 !!strncmp(enter_string, "block ", 6) &&
1121 !!strncmp(enter_string, "unblock ", 8) &&
1122 !!strncmp(enter_string, "unload ", 7))
1124 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1125 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1126 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1127 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1128 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1129 SPRINT(logline[logcur++ % LOGLINES], "unblock <port> (unblock port for further calls, load if not loaded)");
1130 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1133 /* applend output to log window */
1134 SPRINT(buffer, "%s %s", argv[0], enter_string);
1135 fp = popen(buffer, "r");
1138 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1139 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1143 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1146 logline[logcur % LOGLINES][0] = '\0';
1147 enter_string[0] = '\0';
1150 if (ch>=32 && ch<=126)
1152 SCCAT(enter_string, ch);
1158 if (ch==8 || ch==127)
1160 if (enter_string[0])
1161 enter_string[strlen(enter_string)-1] = '\0';
1172 FD_ZERO(&select_rfds);
1173 FD_SET(0, &select_rfds);
1174 select_tv.tv_sec = 0;
1175 select_tv.tv_usec = 250000;
1176 select(1, &select_rfds, NULL, NULL, &select_tv);
1181 /* user input in normal mode */
1184 case 12: /* refresh */
1195 case 'i': /* toggle interface */
1197 if (show_interfaces > 3) show_interfaces = 0;
1200 case 'c': /* toggle calls */
1202 if (show_calls > 2) show_calls = 0;
1205 case 'l': /* toggle log */
1207 if (show_log > 1) show_log = 0;
1210 case '+': /* scroll down */
1214 case '-': /* scroll up */
1219 case '*': /* scroll right */
1223 case '/': /* scroll left */
1228 case 10: /* entermode */
1233 FD_ZERO(&select_rfds);
1234 FD_SET(0, &select_rfds);
1235 select_tv.tv_sec = 0;
1236 select_tv.tv_usec = 250000;
1237 select(1, &select_rfds, NULL, NULL, &select_tv);
1242 /* check for logfh */
1247 /* cleanup curses and exit */
1255 * Send command and show error message.
1257 char *admin_cmd(int sock, int mode, char *extension, char *number)
1259 static struct admin_message msg;
1261 /* send reload command */
1262 memset(&msg, 0, sizeof(msg));
1265 case MODE_INTERFACE:
1266 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1269 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1272 msg.message = ADMIN_REQUEST_CMD_DIAL;
1273 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1276 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1277 SCPY(msg.u.x.message, number);
1280 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1281 msg.u.x.portnum = atoi(number);
1285 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1286 msg.u.x.portnum = atoi(number);
1290 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1291 msg.u.x.portnum = atoi(number);
1296 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1297 return("Broken pipe while sending command.");
1299 /* receive response */
1300 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1301 return("Broken pipe while receiving response.");
1304 case MODE_INTERFACE:
1305 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1306 return("Response not valid.");
1309 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1310 return("Response not valid.");
1313 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1314 return("Response not valid.");
1317 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1318 return("Response not valid.");
1323 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1324 return("Response not valid.");
1328 /* process response */
1331 return(msg.u.x.message);
1333 printf("Command successfull.\n");
1341 #define GET_NOW() { \
1342 gettimeofday(&now_tv, &now_tz); \
1343 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1345 char *admin_testcall(int sock, int argc, char *argv[])
1347 static struct admin_message msg;
1349 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1351 double timer = 0, now_d;
1352 unsigned int on = 1;
1353 struct timeval now_tv;
1354 struct timezone now_tz;
1356 printf("pid=%d\n", getpid()); fflush(stdout);
1360 if (!strcmp(argv[ar], "--setup-timeout"))
1364 return("Missing setup timeout value.\n");
1365 stimeout = atoi(argv[ar]);
1368 if (!strcmp(argv[ar], "--proceeding-timeout"))
1372 return("Missing proceeding timeout value.\n");
1373 ptimeout = atoi(argv[ar]);
1376 if (!strcmp(argv[ar], "--alerting-timeout"))
1380 return("Missing alerting timeout value.\n");
1381 atimeout = atoi(argv[ar]);
1384 if (!strcmp(argv[ar], "--connect-timeout"))
1388 return("Missing connect timeout value.\n");
1389 ctimeout = atoi(argv[ar]);
1397 /* send reload command */
1398 memset(&msg, 0, sizeof(msg));
1399 msg.message = ADMIN_CALL_SETUP;
1400 msg.u.call.present = 1;
1404 SCPY(msg.u.call.interface, argv[ar]);
1409 SCPY(msg.u.call.callerid, argv[ar]);
1414 SCPY(msg.u.call.dialing, argv[ar]);
1419 if (argv[ar][0] == 'r')
1420 msg.u.call.present = 0;
1423 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1424 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1425 msg.u.call.bc_info1 = 0;
1427 msg.u.call.exthlc = 0;
1429 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1431 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1434 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1435 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1439 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1440 if (msg.u.call.bc_info1 < 0)
1441 msg.u.call.bc_info1 = 0;
1443 msg.u.call.bc_info1 |= 0x80;
1447 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1448 if (msg.u.call.hlc < 0)
1451 msg.u.call.hlc |= 0x80;
1455 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1456 if (msg.u.call.exthlc < 0)
1457 msg.u.call.exthlc = 0;
1459 msg.u.call.exthlc |= 0x80;
1463 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1464 return("Broken pipe while sending command.");
1466 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1467 return("Failed to set socket into non-blocking IO.");
1472 timer = now_d + (double)stimeout;
1475 /* receive response */
1477 l = read(sock, &msg, sizeof(msg));
1480 if (errno == EWOULDBLOCK)
1487 printf("Timeout\n"); fflush(stdout);
1494 return("Broken pipe while receiving response.");
1496 if (l != sizeof(msg))
1497 return("Response has unexpected message size.");
1500 case ADMIN_CALL_SETUP_ACK:
1501 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1504 case ADMIN_CALL_PROCEEDING:
1505 printf("PROCEEDING\n"); fflush(stdout);
1509 timer = now_d + (double)ptimeout;
1513 case ADMIN_CALL_ALERTING:
1514 printf("ALERTING\n"); fflush(stdout);
1518 timer = now_d + (double)atimeout;
1522 case ADMIN_CALL_CONNECT:
1523 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1527 timer = now_d + (double)ctimeout;
1531 case ADMIN_CALL_NOTIFY:
1532 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1535 case ADMIN_CALL_DISCONNECT:
1536 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);
1539 case ADMIN_CALL_RELEASE:
1540 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);
1544 return("Response not valid.");
1547 printf("Call released.\n"); fflush(stdout);
1555 char *admin_trace(int sock, int argc, char *argv[])
1557 static struct admin_message msg;
1561 if (argc > 2) if (!strcasecmp(argv[2], "help"))
1563 printf("Trace Help\n----------\n");
1564 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1565 printf("By default a complete trace is shown in detailed format.\n");
1566 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1567 printf("Use filter values to select specific trace messages.\n");
1568 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1569 printf("Filters:\n");
1570 printf(" category=<mask bits>\n");
1571 printf(" 0x01 = CH: channel object trace\n");
1572 printf(" 0x02 = EP: endpoint object trace\n");
1573 printf(" port=<mISDN port> select only given port for trace\n");
1574 printf(" interface=<interface name> select only given interface for trace\n");
1575 printf(" caller=<caller id> select only given caller id for trace\n");
1576 printf(" dialing=<number> select only given dialed number for trace\n");
1580 /* init trace request */
1581 memset(&msg, 0, sizeof(msg));
1582 msg.message = ADMIN_TRACE_REQUEST;
1583 msg.u.trace_req.detail = 3;
1589 if (!strcasecmp(argv[i], "brief"))
1590 msg.u.trace_req.detail = 1;
1591 else if (!strcasecmp(argv[i], "short"))
1592 msg.u.trace_req.detail = 2;
1593 else if (!strncasecmp(argv[i], "category=", 9))
1594 msg.u.trace_req.category = atoi(argv[i]+9);
1595 else if (!strncasecmp(argv[i], "port=", 5))
1596 msg.u.trace_req.port = atoi(argv[i]+5);
1597 else if (!strncasecmp(argv[i], "interface=", 10))
1598 SCPY(msg.u.trace_req.interface, argv[i]+10);
1599 else if (!strncasecmp(argv[i], "caller=", 7))
1600 SCPY(msg.u.trace_req.caller, argv[i]+7);
1601 else if (!strncasecmp(argv[i], "dialing=", 8))
1602 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1603 else return("Invalid trace option, try 'trace help'.");
1608 /* send trace request */
1609 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1610 return("Broken pipe while sending trace request.");
1612 /* receive response */
1614 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1615 return("Broken pipe while receiving response.");
1617 if (msg.message != ADMIN_TRACE_RESPONSE)
1618 return("Response not valid.");
1620 printf("%s", msg.u.trace_rsp.text);
1628 int main(int argc, char *argv[])
1631 char *socket_name = SOCKET_NAME;
1633 struct sockaddr_un sock_address;
1642 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1643 printf("state - View current states using graphical console output.\n");
1644 printf("interface - Tell LCR to reload \"interface.conf\".\n");
1645 printf("route - Tell LCR to reload \"route.conf\".\n");
1646 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1647 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1648 printf("block <port> - Block given port.\n");
1649 printf("unblock <port> - Unblock given port.\n");
1650 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1651 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1652 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1653 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1654 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1655 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1656 printf(" -> Use 'trace help' to see filter description.\n");
1662 if (!(strcasecmp(argv[1],"state")))
1666 if (!(strcasecmp(argv[1],"interface")))
1668 mode = MODE_INTERFACE;
1670 if (!(strcasecmp(argv[1],"route")))
1674 if (!(strcasecmp(argv[1],"dial")))
1680 if (!(strcasecmp(argv[1],"release")))
1684 mode = MODE_RELEASE;
1686 if (!(strcasecmp(argv[1],"unblock")))
1690 mode = MODE_UNBLOCK;
1692 if (!(strcasecmp(argv[1],"block")))
1698 if (!(strcasecmp(argv[1],"unload")))
1704 if (!(strcasecmp(argv[1],"testcall")))
1708 mode = MODE_TESTCALL;
1710 if (!(strcasecmp(argv[1],"trace")))
1720 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1722 fprintf(stderr, "Failed to create socket.\n");
1725 memset(&sock_address, 0, sizeof(sock_address));
1726 sock_address.sun_family = PF_UNIX;
1727 UCPY(sock_address.sun_path, socket_name);
1728 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1731 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1739 ret = admin_state(sock, argv);
1742 case MODE_INTERFACE:
1744 ret = admin_cmd(sock, mode, NULL, NULL);
1748 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1755 ret = admin_cmd(sock, mode, NULL, argv[2]);
1759 ret = admin_testcall(sock, argc, argv);
1763 ret = admin_trace(sock, argc, argv);
1768 /* now we say good bye */
1771 // if (!strncasecmp(ret, "Broken Pipe", 11))
1773 printf("%s\n", ret);