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);
742 if (m[i].u.i.mode[j] == B_MODE_HDLC)
743 SCAT(buffer,"HDLC ");
745 switch(m[i].u.i.busy[j])
748 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block)
758 case B_STATE_ACTIVATING:
766 case B_STATE_DEACTIVATING:
770 case B_STATE_EXPORTING:
778 case B_STATE_IMPORTING:
783 if (m[i].u.i.port[j])
785 /* search for port */
786 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
787 ll = l+msg.u.s.ports;
790 if (m[l].u.p.serial == m[i].u.i.port[j])
792 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
803 move(line>1?line:1, 1);
814 move(line>1?line:1, 1);
817 if (line+2 >= LINES) goto end;
818 /* show summary if no channels were shown */
819 if (show_interfaces<2 && ltee==0)
822 move(++line>1?line:1, 1);
825 if (m[i].u.i.l2link && m[i].u.i.block==0)
828 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
832 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
836 if (line+2 >= LINES) goto end;
842 i = msg.u.s.interfaces;
843 ii = i + msg.u.s.remotes;
846 /* show remote summary */
847 move(++line>1?line:1, 0);
849 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
855 if (line+2 >= LINES) goto end;
857 /* display calls (brief) */
861 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
862 ii = i+msg.u.s.epoints;
865 /* for each endpoint... */
868 move(++line>1?line:1, 0);
870 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
873 if (m[i].u.e.terminal[0])
877 addstr(m[i].u.e.terminal);
881 SPRINT(buffer, " %s", m[i].u.e.callerid);
886 SPRINT(buffer, "%s", m[i].u.e.dialing);
888 if (m[i].u.e.action[0])
893 addstr(m[i].u.e.action);
895 if (line+2 >= LINES) goto end;
900 j = msg.u.s.interfaces+msg.u.s.remotes;
901 jj = j+msg.u.s.joins;
904 /* for each call... */
905 move(++line>1?line:1, 0);
907 SPRINT(buffer, "(%d):", m[j].u.j.serial);
909 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
910 ii = i+msg.u.s.epoints;
913 /* for each endpoint... */
914 if (m[i].u.e.join == m[j].u.j.serial)
917 SPRINT(buffer, " (%d)", m[i].u.e.serial);
920 if (m[i].u.e.terminal[0])
924 addstr(m[i].u.e.terminal);
928 SPRINT(buffer, "-%s", m[i].u.e.callerid);
933 SPRINT(buffer, "%s", m[i].u.e.dialing);
939 if (line+2 >= LINES) goto end;
944 if (line+2 >= LINES) goto end;
946 /* display calls (structurd) */
949 /* show all ports with no epoint */
951 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
952 ii = i+msg.u.s.ports;
955 if (!m[i].u.p.epoint)
957 move(++line>1?line:1, 8);
958 if (line+2 >= LINES) goto end;
959 line = debug_port(&msg, m, line, i, 0);
960 if (line+2 >= LINES) goto end;
967 if (line+2 >= LINES) goto end;
969 /* show all epoints with no call */
971 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
972 ii = i+msg.u.s.epoints;
977 move(++line>1?line:1, 4);
978 if (line+2 >= LINES) goto end;
979 line = debug_epoint(&msg, m, line, i, 0);
980 if (line+2 >= LINES) goto end;
987 if (line+2 >= LINES) goto end;
991 i = msg.u.s.interfaces+msg.u.s.remotes;
992 ii = i+msg.u.s.joins;
995 move(++line>1?line:1, 0);
996 if (line+2 >= LINES) goto end;
997 line = debug_join(&msg, m, line, i);
998 if (line+2 >= LINES) goto end;
1004 if (line+2 >= LINES) goto end;
1013 move(line++>1?line-1:1, 0);
1015 hline(ACS_HLINE, COLS);
1018 l = logcur-(LINES-line-2);
1020 if (ll-l >= LOGLINES)
1024 move(line++>1?line-1:1, 0);
1025 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
1026 SCPY(buffer, logline[l % LOGLINES] + hoffset);
1029 if (COLS < (int)strlen(buffer))
1031 buffer[COLS-1] = '\0';
1046 /* display name/time */
1048 // hline(' ', COLS);
1051 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
1052 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
1057 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1058 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1059 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1062 /* displeay head line */
1065 hline(ACS_HLINE, COLS);
1069 SPRINT(buffer, "Offset +%d", offset);
1076 SPRINT(buffer, "H-Offset +%d", hoffset);
1083 hline(ACS_HLINE, COLS);
1088 SPRINT(buffer, "-> %s", enter_string);
1092 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1098 if (lastlines!=LINES || lastcols!=COLS)
1107 /* user input in enter mode */
1115 if (!enter_string[0])
1118 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1119 if (!!strncmp(enter_string, "interface", 10) &&
1120 !!strncmp(enter_string, "route", 6) &&
1121 !!strncmp(enter_string, "release ", 8) &&
1122 !!strncmp(enter_string, "block ", 6) &&
1123 !!strncmp(enter_string, "unblock ", 8) &&
1124 !!strncmp(enter_string, "unload ", 7))
1126 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1127 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1128 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1129 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1130 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1131 SPRINT(logline[logcur++ % LOGLINES], "unblock <port> (unblock port for further calls, load if not loaded)");
1132 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1135 /* applend output to log window */
1136 SPRINT(buffer, "%s %s", argv[0], enter_string);
1137 fp = popen(buffer, "r");
1140 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1141 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1145 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1148 logline[logcur % LOGLINES][0] = '\0';
1149 enter_string[0] = '\0';
1152 if (ch>=32 && ch<=126)
1154 SCCAT(enter_string, ch);
1160 if (ch==8 || ch==127)
1162 if (enter_string[0])
1163 enter_string[strlen(enter_string)-1] = '\0';
1174 FD_ZERO(&select_rfds);
1175 FD_SET(0, &select_rfds);
1176 select_tv.tv_sec = 0;
1177 select_tv.tv_usec = 250000;
1178 select(1, &select_rfds, NULL, NULL, &select_tv);
1183 /* user input in normal mode */
1186 case 12: /* refresh */
1197 case 'i': /* toggle interface */
1199 if (show_interfaces > 3) show_interfaces = 0;
1202 case 'c': /* toggle calls */
1204 if (show_calls > 2) show_calls = 0;
1207 case 'l': /* toggle log */
1209 if (show_log > 1) show_log = 0;
1212 case '+': /* scroll down */
1216 case '-': /* scroll up */
1221 case '*': /* scroll right */
1225 case '/': /* scroll left */
1230 case 10: /* entermode */
1235 FD_ZERO(&select_rfds);
1236 FD_SET(0, &select_rfds);
1237 select_tv.tv_sec = 0;
1238 select_tv.tv_usec = 250000;
1239 select(1, &select_rfds, NULL, NULL, &select_tv);
1244 /* check for logfh */
1249 /* cleanup curses and exit */
1257 * Send command and show error message.
1259 char *admin_cmd(int sock, int mode, char *extension, char *number)
1261 static struct admin_message msg;
1263 /* send reload command */
1264 memset(&msg, 0, sizeof(msg));
1267 case MODE_INTERFACE:
1268 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1271 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1274 msg.message = ADMIN_REQUEST_CMD_DIAL;
1275 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1278 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1279 SCPY(msg.u.x.message, 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);
1292 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1293 msg.u.x.portnum = atoi(number);
1298 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1299 return("Broken pipe while sending command.");
1301 /* receive response */
1302 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1303 return("Broken pipe while receiving response.");
1306 case MODE_INTERFACE:
1307 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1308 return("Response not valid.");
1311 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1312 return("Response not valid.");
1315 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1316 return("Response not valid.");
1319 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1320 return("Response not valid.");
1325 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1326 return("Response not valid.");
1330 /* process response */
1333 return(msg.u.x.message);
1335 printf("Command successfull.\n");
1343 #define GET_NOW() { \
1344 gettimeofday(&now_tv, &now_tz); \
1345 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1347 char *admin_testcall(int sock, int argc, char *argv[])
1349 static struct admin_message msg;
1351 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1353 double timer = 0, now_d;
1354 unsigned int on = 1;
1355 struct timeval now_tv;
1356 struct timezone now_tz;
1358 printf("pid=%d\n", getpid()); fflush(stdout);
1362 if (!strcmp(argv[ar], "--setup-timeout"))
1366 return("Missing setup timeout value.\n");
1367 stimeout = atoi(argv[ar]);
1370 if (!strcmp(argv[ar], "--proceeding-timeout"))
1374 return("Missing proceeding timeout value.\n");
1375 ptimeout = atoi(argv[ar]);
1378 if (!strcmp(argv[ar], "--alerting-timeout"))
1382 return("Missing alerting timeout value.\n");
1383 atimeout = atoi(argv[ar]);
1386 if (!strcmp(argv[ar], "--connect-timeout"))
1390 return("Missing connect timeout value.\n");
1391 ctimeout = atoi(argv[ar]);
1399 /* send reload command */
1400 memset(&msg, 0, sizeof(msg));
1401 msg.message = ADMIN_CALL_SETUP;
1402 msg.u.call.present = 1;
1406 SCPY(msg.u.call.interface, argv[ar]);
1411 SCPY(msg.u.call.callerid, argv[ar]);
1416 SCPY(msg.u.call.dialing, argv[ar]);
1421 if (argv[ar][0] == 'r')
1422 msg.u.call.present = 0;
1425 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1426 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1427 msg.u.call.bc_info1 = 0;
1429 msg.u.call.exthlc = 0;
1431 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1433 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1436 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1437 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1441 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1442 if (msg.u.call.bc_info1 < 0)
1443 msg.u.call.bc_info1 = 0;
1445 msg.u.call.bc_info1 |= 0x80;
1449 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1450 if (msg.u.call.hlc < 0)
1453 msg.u.call.hlc |= 0x80;
1457 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1458 if (msg.u.call.exthlc < 0)
1459 msg.u.call.exthlc = 0;
1461 msg.u.call.exthlc |= 0x80;
1465 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1466 return("Broken pipe while sending command.");
1468 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1469 return("Failed to set socket into non-blocking IO.");
1474 timer = now_d + (double)stimeout;
1477 /* receive response */
1479 l = read(sock, &msg, sizeof(msg));
1482 if (errno == EWOULDBLOCK)
1489 printf("Timeout\n"); fflush(stdout);
1496 return("Broken pipe while receiving response.");
1498 if (l != sizeof(msg))
1499 return("Response has unexpected message size.");
1502 case ADMIN_CALL_SETUP_ACK:
1503 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1506 case ADMIN_CALL_PROCEEDING:
1507 printf("PROCEEDING\n"); fflush(stdout);
1511 timer = now_d + (double)ptimeout;
1515 case ADMIN_CALL_ALERTING:
1516 printf("ALERTING\n"); fflush(stdout);
1520 timer = now_d + (double)atimeout;
1524 case ADMIN_CALL_CONNECT:
1525 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1529 timer = now_d + (double)ctimeout;
1533 case ADMIN_CALL_NOTIFY:
1534 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify, msg.u.call.callerid); fflush(stdout);
1537 case ADMIN_CALL_DISCONNECT:
1538 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);
1541 case ADMIN_CALL_RELEASE:
1542 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);
1546 return("Response not valid.");
1549 printf("Call released.\n"); fflush(stdout);
1557 char *admin_trace(int sock, int argc, char *argv[])
1559 static struct admin_message msg;
1563 if (argc > 2) if (!strcasecmp(argv[2], "help"))
1565 printf("Trace Help\n----------\n");
1566 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1567 printf("By default a complete trace is shown in detailed format.\n");
1568 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1569 printf("Use filter values to select specific trace messages.\n");
1570 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1571 printf("Filters:\n");
1572 printf(" category=<mask bits>\n");
1573 printf(" 0x01 = CH: channel object trace\n");
1574 printf(" 0x02 = EP: endpoint object trace\n");
1575 printf(" port=<mISDN port> select only given port for trace\n");
1576 printf(" interface=<interface name> select only given interface for trace\n");
1577 printf(" caller=<caller id> select only given caller id for trace\n");
1578 printf(" dialing=<number> select only given dialed number for trace\n");
1582 /* init trace request */
1583 memset(&msg, 0, sizeof(msg));
1584 msg.message = ADMIN_TRACE_REQUEST;
1585 msg.u.trace_req.detail = 3;
1591 if (!strcasecmp(argv[i], "brief"))
1592 msg.u.trace_req.detail = 1;
1593 else if (!strcasecmp(argv[i], "short"))
1594 msg.u.trace_req.detail = 2;
1595 else if (!strncasecmp(argv[i], "category=", 9))
1596 msg.u.trace_req.category = atoi(argv[i]+9);
1597 else if (!strncasecmp(argv[i], "port=", 5))
1598 msg.u.trace_req.port = atoi(argv[i]+5);
1599 else if (!strncasecmp(argv[i], "interface=", 10))
1600 SCPY(msg.u.trace_req.interface, argv[i]+10);
1601 else if (!strncasecmp(argv[i], "caller=", 7))
1602 SCPY(msg.u.trace_req.caller, argv[i]+7);
1603 else if (!strncasecmp(argv[i], "dialing=", 8))
1604 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1605 else return("Invalid trace option, try 'trace help'.");
1610 /* send trace request */
1611 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1612 return("Broken pipe while sending trace request.");
1614 /* receive response */
1616 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1617 return("Broken pipe while receiving response.");
1619 if (msg.message != ADMIN_TRACE_RESPONSE)
1620 return("Response not valid.");
1622 printf("%s", msg.u.trace_rsp.text);
1630 int main(int argc, char *argv[])
1633 char *socket_name = SOCKET_NAME;
1635 struct sockaddr_un sock_address;
1644 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1645 printf("state - View current states using graphical console output.\n");
1646 printf("interface - Tell LCR to reload \"interface.conf\".\n");
1647 printf("route - Tell LCR to reload \"route.conf\".\n");
1648 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1649 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1650 printf("block <port> - Block given port.\n");
1651 printf("unblock <port> - Unblock given port.\n");
1652 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1653 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1654 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1655 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1656 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1657 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1658 printf(" -> Use 'trace help' to see filter description.\n");
1664 if (!(strcasecmp(argv[1],"state")))
1668 if (!(strcasecmp(argv[1],"interface")))
1670 mode = MODE_INTERFACE;
1672 if (!(strcasecmp(argv[1],"route")))
1676 if (!(strcasecmp(argv[1],"dial")))
1682 if (!(strcasecmp(argv[1],"release")))
1686 mode = MODE_RELEASE;
1688 if (!(strcasecmp(argv[1],"unblock")))
1692 mode = MODE_UNBLOCK;
1694 if (!(strcasecmp(argv[1],"block")))
1700 if (!(strcasecmp(argv[1],"unload")))
1706 if (!(strcasecmp(argv[1],"testcall")))
1710 mode = MODE_TESTCALL;
1712 if (!(strcasecmp(argv[1],"trace")))
1722 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1724 fprintf(stderr, "Failed to create socket.\n");
1727 memset(&sock_address, 0, sizeof(sock_address));
1728 sock_address.sun_family = PF_UNIX;
1729 UCPY(sock_address.sun_path, socket_name);
1730 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1733 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1741 ret = admin_state(sock, argv);
1744 case MODE_INTERFACE:
1746 ret = admin_cmd(sock, mode, NULL, NULL);
1750 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1757 ret = admin_cmd(sock, mode, NULL, argv[2]);
1761 ret = admin_testcall(sock, argc, argv);
1765 ret = admin_trace(sock, argc, argv);
1770 /* now we say good bye */
1773 // if (!strncasecmp(ret, "Broken Pipe", 11))
1775 printf("%s\n", ret);