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>
30 #include "extension.h"
32 #include "lcrsocket.h"
35 #define LTEE {addch(ACS_LTEE);addch(ACS_HLINE);addch(ACS_HLINE);}
36 #define LLCORNER {addch(ACS_LLCORNER);addch(ACS_HLINE);addch(ACS_HLINE);}
37 #define VLINE {addch(ACS_VLINE);addstr(" ");}
38 #define EMPTY {addstr(" ");}
39 //char rotator[] = {'-', '\\', '|', '/'};
40 int lastlines, lastcols;
41 int show_interfaces = 2,
59 const char *text_interfaces[] = {
66 const char *text_calls[] = {
81 char logline[LOGLINES][512];
82 unsigned int logcur = 0;
89 void init_curses(void)
92 initscr(); cbreak(); noecho();
94 nodelay(stdscr, TRUE);
95 if (COLOR_PAIRS>=8 && COLORS>=8)
109 void cleanup_curses(void)
114 void color(int color)
116 if (COLOR_PAIRS>=8 && COLORS>=8)
117 attrset(COLOR_PAIR(color));
121 * permanently show current state using ncurses
123 int debug_port(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
130 SPRINT(buffer,"%s(%d)", m[i].u.p.name,m[i].u.p.serial);
134 switch (m[i].u.p.state) {
135 case ADMIN_STATE_IDLE:
139 case ADMIN_STATE_IN_SETUP:
141 addstr("'in << setup'");
143 case ADMIN_STATE_OUT_SETUP:
145 addstr("'out >> setup'");
147 case ADMIN_STATE_IN_OVERLAP:
149 addstr("'in << overlap'");
151 case ADMIN_STATE_OUT_OVERLAP:
153 addstr("'out >> overlap'");
155 case ADMIN_STATE_IN_PROCEEDING:
157 addstr("'in << proc'");
159 case ADMIN_STATE_OUT_PROCEEDING:
161 addstr("'out >> proc'");
163 case ADMIN_STATE_IN_ALERTING:
165 addstr("'in << alert'");
167 case ADMIN_STATE_OUT_ALERTING:
169 addstr("'out >> alert'");
171 case ADMIN_STATE_CONNECT:
175 case ADMIN_STATE_IN_DISCONNECT:
177 addstr("'in << disc'");
179 case ADMIN_STATE_OUT_DISCONNECT:
181 addstr("'out >> disc'");
183 case ADMIN_STATE_RELEASE:
189 addstr("'--NONE--'");
194 addstr(" bchannel=");
196 SPRINT(buffer,"%d", m[i].u.p.isdn_chan);
198 if (m[i].u.p.isdn_ces >= 0) {
202 SPRINT(buffer, "%d", m[i].u.p.isdn_ces);
205 if (m[i].u.p.isdn_hold) {
213 int debug_epoint(struct admin_message *msg, struct admin_message *m, int line, int i, int vline)
215 unsigned int epoint = m[i].u.e.serial;
222 SPRINT(buffer,"EPOINT(%d)", epoint);
226 switch (m[i].u.e.state) {
227 case ADMIN_STATE_IDLE:
231 case ADMIN_STATE_IN_SETUP:
233 addstr("'in << setup'");
235 case ADMIN_STATE_OUT_SETUP:
237 addstr("'out >> setup'");
239 case ADMIN_STATE_IN_OVERLAP:
241 addstr("'in << overlap'");
243 case ADMIN_STATE_OUT_OVERLAP:
245 addstr("'out >> overlap'");
247 case ADMIN_STATE_IN_PROCEEDING:
249 addstr("'in << proc'");
251 case ADMIN_STATE_OUT_PROCEEDING:
253 addstr("'out >> proc'");
255 case ADMIN_STATE_IN_ALERTING:
257 addstr("'in << alert'");
259 case ADMIN_STATE_OUT_ALERTING:
261 addstr("'out >> alert'");
263 case ADMIN_STATE_CONNECT:
267 case ADMIN_STATE_IN_DISCONNECT:
269 addstr("'in << disc'");
271 case ADMIN_STATE_OUT_DISCONNECT:
273 addstr("'out >> disc'");
277 addstr("'--NONE--'");
279 if (m[i].u.e.terminal[0]) {
281 addstr(" terminal=");
283 addstr(m[i].u.e.terminal);
286 SPRINT(buffer, " %s", m[i].u.e.callerid);
291 addstr(m[i].u.e.dialing);
292 if (m[i].u.e.action[0]) {
296 addstr(m[i].u.e.action);
300 addstr(" park="); /* 9 digits */
302 UCPY(buffer, "\""); /* 9 digits */
304 jj = m[i].u.e.park_len;
306 c = m[i].u.e.park_callid[j];
307 if (c >= 32 && c < 127 && c != '[') {
310 UPRINT(buffer+strlen(buffer), "[%02x]", c);
317 switch(m[i].u.e.rx_state) {
318 case NOTIFY_STATE_SUSPEND:
319 addstr(" in=suspend");
321 case NOTIFY_STATE_HOLD:
324 case NOTIFY_STATE_CONFERENCE:
325 addstr(" in=conference");
328 switch(m[i].u.e.tx_state) {
329 case NOTIFY_STATE_SUSPEND:
330 addstr(" out=suspend");
332 case NOTIFY_STATE_HOLD:
335 case NOTIFY_STATE_CONFERENCE:
336 addstr(" out=conference");
340 if (m[i].u.e.crypt) {
343 if (m[i].u.e.crypt) { /* crypt on */
351 /* loop all related ports */
353 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins+msg->u.s.epoints;
354 jj = j + msg->u.s.ports;
356 if (m[j].u.p.epoint == epoint) {
358 move(++line>1?line:1, 1);
363 move(line>1?line:1, 5);
366 move(line>1?line:1, 8);
367 if (line+2 >= LINES) break;
368 line = debug_port(msg, m, line, j, vline);
369 if (line+2 >= LINES) break;
375 move(ltee>1?line:1, 5);
381 int debug_join(struct admin_message *msg, struct admin_message *m, int line, int i)
383 unsigned int join = m[i].u.j.serial;
388 SPRINT(buffer,"JOIN(%d)", join);
390 if (m[i].u.j.partyline) {
392 addstr(" partyline=");
394 SPRINT(buffer, "%d\n", m[i].u.j.partyline);
397 if (m[i].u.j.remote[0]) {
401 SPRINT(buffer, "%s\n", m[i].u.j.remote);
404 /* find number of epoints */
405 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins;
406 jj = j + msg->u.s.epoints;
409 if (m[j].u.e.join == join)
413 /* loop all related endpoints */
414 j = msg->u.s.interfaces+msg->u.s.remotes+msg->u.s.joins;
415 jj = j + msg->u.s.epoints;
417 if (m[j].u.e.join == join) {
419 move(++line>1?line:1, 1);
425 move(line>1?line:1, 4);
426 if (line+2 >= LINES) break;
427 line = debug_epoint(msg, m, line, j, i?1:0);
428 if (line+2 >= LINES) break;
436 const char *admin_state(int sock, char *argv[])
438 struct admin_message msg,
442 int line, offset = 0, hoffset = 0;
451 char enter_string[128] = "", ch;
453 struct timeval select_tv;
455 /* flush logfile name */
462 /* send reload command */
463 memset(&msg, 0, sizeof(msg));
464 msg.message = ADMIN_REQUEST_STATE;
465 // printf("sizeof=%d\n",sizeof(msg));fflush(stdout);
466 if (write(sock, &msg, sizeof(msg)) != sizeof(msg)) {
468 return("Broken pipe while sending command.");
471 /* receive response */
472 if (read(sock, &msg, sizeof(msg)) != sizeof(msg)) {
474 return("Broken pipe while receiving response.");
477 if (msg.message != ADMIN_RESPONSE_STATE) {
479 return("Response not valid. Expecting state response.");
481 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
482 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
486 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off) {
489 // fprintf(stderr, "got=%d expected=%d\n", i, num*sizeof(struct admin_message));
491 return("Broken pipe while receiving state infos.");
493 if (len < num*(int)sizeof(struct admin_message)) {
501 // fprintf("getting =%d interfaces\n", msg.u.s.interfaces);
502 while(i < msg.u.s.interfaces) {
503 // fprintf(stderr, "j=%d message=%d\n", j, m[j].message);
504 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE) {
507 return("Response not valid. Expecting interface information.");
513 while(i < msg.u.s.remotes) {
514 if (m[j].message != ADMIN_RESPONSE_S_REMOTE) {
517 return("Response not valid. Expecting remote application information.");
523 while(i < msg.u.s.joins) {
524 if (m[j].message != ADMIN_RESPONSE_S_JOIN) {
527 return("Response not valid. Expecting join information.");
533 while(i < msg.u.s.epoints) {
534 if (m[j].message != ADMIN_RESPONSE_S_EPOINT) {
537 return("Response not valid. Expecting endpoint information.");
543 while(i < msg.u.s.ports) {
544 if (m[j].message != ADMIN_RESPONSE_S_PORT) {
547 return("Response not valid. Expecting port information.");
552 // now j is the number of message blocks
560 if (!!strcmp(logfile, msg.u.s.logfile)) {
561 SCPY(logfile, msg.u.s.logfile);
568 logline[i][1] = '\0';
572 logfh = open(logfile, O_RDONLY|O_NONBLOCK);
574 /* seek at the end -8000 chars */
575 lseek(logfh, -8000, SEEK_END);
576 /* if not at the beginning, read until endofline */
577 logline[logcur % LOGLINES][0] = '\0';
578 l = read(logfh, logline[logcur % LOGLINES], sizeof(logline[logcur % LOGLINES])-1);
580 /* read first line and skip junk */
581 logline[logcur % LOGLINES][l] = '\0';
582 if ((p = strchr(logline[logcur % LOGLINES],'\n'))) {
584 SCPY(logline[logcur % LOGLINES], p+1);
585 SCPY(logline[(logcur-1) % LOGLINES], "...");
595 ll = strlen(logline[logcur % LOGLINES]);
596 l = read(logfh, logline[logcur % LOGLINES]+ll, sizeof(logline[logcur % LOGLINES])-ll-1);
599 logline[logcur % LOGLINES][ll+l] = '\0';
601 /* put data to lines */
602 while ((p = strchr(logline[logcur % LOGLINES],'\n'))) {
605 SCPY(logline[logcur % LOGLINES], p+1);
607 /* if line is full without return, go next line */
608 if (strlen(logline[logcur % LOGLINES]) == sizeof(logline[logcur % LOGLINES])-1) {
610 logline[logcur % LOGLINES][0] = '\0';
615 /* display interfaces */
616 if (show_interfaces > 0) {
619 ii = i + msg.u.s.interfaces;
621 /* show interface summary */
622 move(++line>1?line:1, 0);
624 if (m[i].u.i.block >= 2) {
625 if (m[i].u.i.portnum < 0)
626 SPRINT(buffer, "%s (port ?: %s)%s", m[i].u.i.interface_name, m[i].u.i.portname, (m[i].u.i.extension)?" exten":"");
628 SPRINT(buffer, "%s (port %d: %s)%s", m[i].u.i.interface_name, m[i].u.i.portnum, m[i].u.i.portname, (m[i].u.i.extension)?" extension":"");
631 addstr(" not loaded");
633 SPRINT(buffer, "%s", m[i].u.i.interface_name);
636 SPRINT(buffer, "(port %d: %s)", m[i].u.i.portnum, m[i].u.i.portname);
639 SPRINT(buffer, " %s %s%s%s%s", (m[i].u.i.ntmode)?"NT":"TE", (m[i].u.i.ptp)?"ptp":"ptmp", (m[i].u.i.l1hold)?" l1hold":"", (m[i].u.i.l2hold)?" l2hold":"", (m[i].u.i.extension)?" extension":"");
645 SPRINT(buffer, " use:%d", m[i].u.i.use);
647 if (m[i].u.i.ptp || !m[i].u.i.ntmode) {
648 color((m[i].u.i.l2link > 0)?green:red);
649 if (m[i].u.i.l2link < 0)
652 addstr((m[i].u.i.l2link)?" L2 UP":" L2 down");
658 if (m[i].u.i.l2mask[j>>3] & (1 << (j&7))) {
659 SPRINT(buffer, "%s%d", k?",":" TEI(", j);
668 color((m[i].u.i.l1link > 0)?green:blue);
669 if (m[i].u.i.l1link < 0)
672 addstr((m[i].u.i.l1link)?" L1 UP":" L1 down");
685 if (m[i].u.i.slip_tx || m[i].u.i.slip_rx) {
687 SPRINT(buffer, " SLIP(tx:%d rx:%d)", m[i].u.i.slip_tx, m[i].u.i.slip_rx);
690 if (m[i].u.i.block) {
694 if (line+2 >= LINES) goto end;
696 if (show_interfaces > 1) {
699 jj = m[i].u.i.channels;
701 /* show all channels */
702 if (show_interfaces>2 || m[i].u.i.busy[j]>0) {
704 /* show left side / right side */
705 if ((k & 1) && (COLS > 70)) {
706 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);
718 if (m[i].u.i.mode[j] == B_MODE_HDLC)
719 SCAT(buffer,"HDLC ");
721 switch(m[i].u.i.busy[j]) {
723 if ((!m[i].u.i.l2link && m[i].u.i.ptp) || m[i].u.i.block) {
731 case B_STATE_ACTIVATING:
739 case B_STATE_DEACTIVATING:
743 case B_STATE_EXPORTING:
751 case B_STATE_IMPORTING:
756 if (m[i].u.i.port[j]) {
757 /* search for port */
758 l = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
759 ll = l+msg.u.s.ports;
761 if (m[l].u.p.serial == m[i].u.i.port[j]) {
762 SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
768 if (line+2 >= LINES) {
771 move(line>1?line:1, 1);
781 move(line>1?line:1, 1);
784 if (line+2 >= LINES) goto end;
785 /* show summary if no channels were shown */
786 if (show_interfaces<2 && ltee==0) {
788 move(++line>1?line:1, 1);
791 if (m[i].u.i.l2link && m[i].u.i.block==0) {
793 SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
796 SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
800 if (line+2 >= LINES) goto end;
806 i = msg.u.s.interfaces;
807 ii = i + msg.u.s.remotes;
809 /* show remote summary */
810 move(++line>1?line:1, 0);
812 SPRINT(buffer, "Remote: %s", m[i].u.r.name);
818 if (line+2 >= LINES) goto end;
820 /* display calls (brief) */
821 if (show_calls == 1) {
823 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
824 ii = i+msg.u.s.epoints;
826 /* for each endpoint... */
827 if (!m[i].u.e.join) {
828 move(++line>1?line:1, 0);
830 SPRINT(buffer, "(%d): ", m[i].u.e.serial);
833 if (m[i].u.e.terminal[0]) {
836 addstr(m[i].u.e.terminal);
840 SPRINT(buffer, " %s", m[i].u.e.callerid);
845 SPRINT(buffer, "%s", m[i].u.e.dialing);
847 if (m[i].u.e.action[0]) {
851 addstr(m[i].u.e.action);
853 if (line+2 >= LINES) goto end;
858 j = msg.u.s.interfaces+msg.u.s.remotes;
859 jj = j+msg.u.s.joins;
861 /* for each call... */
862 move(++line>1?line:1, 0);
864 SPRINT(buffer, "(%d):", m[j].u.j.serial);
866 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
867 ii = i+msg.u.s.epoints;
869 /* for each endpoint... */
870 if (m[i].u.e.join == m[j].u.j.serial) {
872 SPRINT(buffer, " (%d)", m[i].u.e.serial);
875 if (m[i].u.e.terminal[0]) {
878 addstr(m[i].u.e.terminal);
882 SPRINT(buffer, "-%s", m[i].u.e.callerid);
887 SPRINT(buffer, "%s", m[i].u.e.dialing);
893 if (line+2 >= LINES) goto end;
898 if (line+2 >= LINES) goto end;
900 /* display calls (structurd) */
901 if (show_calls == 2) {
902 /* show all ports with no epoint */
904 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins+msg.u.s.epoints;
905 ii = i+msg.u.s.ports;
907 if (!m[i].u.p.epoint) {
908 move(++line>1?line:1, 8);
909 if (line+2 >= LINES) goto end;
910 line = debug_port(&msg, m, line, i, 0);
911 if (line+2 >= LINES) goto end;
918 if (line+2 >= LINES) goto end;
920 /* show all epoints with no call */
922 i = msg.u.s.interfaces+msg.u.s.remotes+msg.u.s.joins;
923 ii = i+msg.u.s.epoints;
925 if (!m[i].u.e.join) {
926 move(++line>1?line:1, 4);
927 if (line+2 >= LINES) goto end;
928 line = debug_epoint(&msg, m, line, i, 0);
929 if (line+2 >= LINES) goto end;
936 if (line+2 >= LINES) goto end;
940 i = msg.u.s.interfaces+msg.u.s.remotes;
941 ii = i+msg.u.s.joins;
943 move(++line>1?line:1, 0);
944 if (line+2 >= LINES) goto end;
945 line = debug_join(&msg, m, line, i);
946 if (line+2 >= LINES) goto end;
952 if (line+2 >= LINES) goto end;
958 if (line+2 < LINES) {
959 move(line++>1?line-1:1, 0);
961 hline(ACS_HLINE, COLS);
964 l = logcur-(LINES-line-2);
966 if (ll-l >= LOGLINES)
969 move(line++>1?line-1:1, 0);
970 if ((int)strlen(logline[l % LOGLINES]) > hoffset)
971 SCPY(buffer, logline[l % LOGLINES] + hoffset);
974 if (COLS < (int)strlen(buffer)) {
975 buffer[COLS-1] = '\0';
990 /* display name/time */
995 msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
996 SPRINT(buffer, "LCR %s", msg.u.s.version_string);
1000 SPRINT(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1001 msg.u.s.tm.tm_year+1900, msg.u.s.tm.tm_mon+1, msg.u.s.tm.tm_mday,
1002 msg.u.s.tm.tm_hour, msg.u.s.tm.tm_min, msg.u.s.tm.tm_sec);
1005 /* displeay head line */
1008 hline(ACS_HLINE, COLS);
1011 SPRINT(buffer, "Offset +%d", offset);
1017 SPRINT(buffer, "H-Offset +%d", hoffset);
1024 hline(ACS_HLINE, COLS);
1028 SPRINT(buffer, "-> %s", enter_string);
1031 SPRINT(buffer, "i=interfaces '%s' c=calls '%s' l=log q=quit +-*/=scroll enter", text_interfaces[show_interfaces], text_calls[show_calls]);
1037 if (lastlines!=LINES || lastcols!=COLS) {
1044 /* user input in enter mode */
1051 if (!enter_string[0])
1054 SPRINT(logline[logcur++ % LOGLINES], "> %s", enter_string);
1055 if (!!strncmp(enter_string, "interface", 10) &&
1056 !!strncmp(enter_string, "route", 6) &&
1057 !!strncmp(enter_string, "release ", 8) &&
1058 !!strncmp(enter_string, "block ", 6) &&
1059 !!strncmp(enter_string, "unblock ", 8) &&
1060 !!strncmp(enter_string, "load ", 5) &&
1061 !!strncmp(enter_string, "unload ", 7)) {
1062 SPRINT(logline[logcur++ % LOGLINES], "usage:");
1063 SPRINT(logline[logcur++ % LOGLINES], "interface (reload interface.conf)");
1064 SPRINT(logline[logcur++ % LOGLINES], "route (reload routing.conf)");
1065 SPRINT(logline[logcur++ % LOGLINES], "release <EP> (release endpoint with given ID)");
1066 SPRINT(logline[logcur++ % LOGLINES], "block <port> (block port for further calls)");
1067 SPRINT(logline[logcur++ % LOGLINES], "unblock/load <port> (unblock port for further calls, load if not loaded)");
1068 SPRINT(logline[logcur++ % LOGLINES], "unload <port> (unload mISDN stack, release call calls)");
1070 /* applend output to log window */
1071 SPRINT(buffer, "%s %s", argv[0], enter_string);
1072 fp = popen(buffer, "r");
1074 while(fgets(logline[logcur % LOGLINES], sizeof(logline[0]), fp))
1075 logline[logcur++ % LOGLINES][sizeof(logline[0])-1] = '\0';
1078 SPRINT(logline[logcur++ % LOGLINES], "failed to execute '%s'", buffer);
1081 logline[logcur % LOGLINES][0] = '\0';
1082 enter_string[0] = '\0';
1085 if (ch>=32 && ch<=126) {
1086 SCCAT(enter_string, ch);
1092 if (ch==8 || ch==127) {
1093 if (enter_string[0])
1094 enter_string[strlen(enter_string)-1] = '\0';
1104 FD_ZERO(&select_rfds);
1105 FD_SET(0, &select_rfds);
1106 select_tv.tv_sec = 0;
1107 select_tv.tv_usec = 250000;
1108 select(1, &select_rfds, NULL, NULL, &select_tv);
1112 /* user input in normal mode */
1114 case 12: /* refresh */
1125 case 'i': /* toggle interface */
1127 if (show_interfaces > 3) show_interfaces = 0;
1130 case 'c': /* toggle calls */
1132 if (show_calls > 2) show_calls = 0;
1135 case 'l': /* toggle log */
1137 if (show_log > 1) show_log = 0;
1140 case '+': /* scroll down */
1144 case '-': /* scroll up */
1149 case '*': /* scroll right */
1153 case '/': /* scroll left */
1158 case 10: /* entermode */
1163 FD_ZERO(&select_rfds);
1164 FD_SET(0, &select_rfds);
1165 select_tv.tv_sec = 0;
1166 select_tv.tv_usec = 250000;
1167 select(1, &select_rfds, NULL, NULL, &select_tv);
1172 /* check for logfh */
1177 /* cleanup curses and exit */
1183 const char *admin_portinfo(int sock, int argc, char *argv[])
1185 struct admin_message msg,
1192 /* send state request command */
1193 memset(&msg, 0, sizeof(msg));
1194 msg.message = ADMIN_REQUEST_STATE;
1195 if (write(sock, &msg, sizeof(msg)) != sizeof(msg)) {
1197 return("Broken pipe while sending command.");
1200 /* receive response */
1201 if (read(sock, &msg, sizeof(msg)) != sizeof(msg)) {
1203 return("Broken pipe while receiving response.");
1206 if (msg.message != ADMIN_RESPONSE_STATE) {
1208 return("Response not valid. Expecting state response.");
1210 num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
1211 m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
1215 if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off) {
1219 return("Broken pipe while receiving state infos.");
1221 if (len < num*(int)sizeof(struct admin_message)) {
1229 while(i < msg.u.s.interfaces) {
1230 if (m[j].message != ADMIN_RESPONSE_S_INTERFACE) {
1233 return("Response not valid. Expecting interface information.");
1239 while(i < msg.u.s.remotes) {
1240 if (m[j].message != ADMIN_RESPONSE_S_REMOTE) {
1243 return("Response not valid. Expecting remote application information.");
1249 while(i < msg.u.s.joins) {
1250 if (m[j].message != ADMIN_RESPONSE_S_JOIN) {
1253 return("Response not valid. Expecting join information.");
1259 while(i < msg.u.s.epoints) {
1260 if (m[j].message != ADMIN_RESPONSE_S_EPOINT) {
1263 return("Response not valid. Expecting endpoint information.");
1269 while(i < msg.u.s.ports) {
1270 if (m[j].message != ADMIN_RESPONSE_S_PORT) {
1273 return("Response not valid. Expecting port information.");
1278 // now j is the number of message blocks
1280 /* output interfaces */
1282 ii = i + msg.u.s.interfaces;
1285 if (!!strcmp(argv[2], m[i].u.i.interface_name)) {
1290 printf("%s:\n", m[i].u.i.interface_name);
1291 if (m[i].u.i.portnum < 0)
1292 printf("\t port = unknown\n");
1294 printf("\t port = %d \"%s\"\n",m[i].u.i.portnum, m[i].u.i.portname);
1295 printf("\t extension = %s\n", (m[i].u.i.extension)?"yes":"no");
1296 if (m[i].u.i.block >= 2) {
1297 printf("\t status = not loaded\n");
1300 printf("\t status = blocked\n");
1302 printf("\t status = unblocked\n");
1303 printf("\t mode = %s %s%s%s\n", (m[i].u.i.ntmode)?"NT-mode":"TE-mode", (m[i].u.i.ptp)?"ptp":"ptmp", (m[i].u.i.l1hold)?" l1hold":"", (m[i].u.i.l2hold)?" l2hold":"");
1304 printf("\t out-channel = %s\n", m[i].u.i.out_channel);
1305 printf("\t in-channel = %s\n", m[i].u.i.in_channel);
1306 if (m[i].u.i.l1link < 0)
1307 printf("\t l1 link = unknown\n");
1309 printf("\t l1 link = %s\n", (m[i].u.i.l1link)?"up":"down");
1310 if (m[i].u.i.ptp || !m[i].u.i.ntmode) {
1311 if (m[i].u.i.l2link < 0)
1312 printf("\t l2 link = unknown\n");
1314 printf("\t l2 link = %s\n", (m[i].u.i.l2link)?"up":"down");
1316 printf("\t usage = %d\n", m[i].u.i.use);
1329 * Send command and show error message.
1331 const char *admin_cmd(int sock, int mode, char *extension, char *number)
1333 static struct admin_message msg;
1335 /* send reload command */
1336 memset(&msg, 0, sizeof(msg));
1338 case MODE_INTERFACE:
1339 msg.message = ADMIN_REQUEST_CMD_INTERFACE;
1342 msg.message = ADMIN_REQUEST_CMD_ROUTE;
1345 msg.message = ADMIN_REQUEST_CMD_DIAL;
1346 SPRINT(msg.u.x.message, "%s:%s", extension?:"", number?:"");
1349 msg.message = ADMIN_REQUEST_CMD_RELEASE;
1350 SCPY(msg.u.x.message, number);
1353 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1354 msg.u.x.portnum = atoi(number);
1358 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1359 msg.u.x.portnum = atoi(number);
1363 msg.message = ADMIN_REQUEST_CMD_BLOCK;
1364 msg.u.x.portnum = atoi(number);
1369 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1370 return("Broken pipe while sending command.");
1372 /* receive response */
1373 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1374 return("Broken pipe while receiving response.");
1376 case MODE_INTERFACE:
1377 if (msg.message != ADMIN_RESPONSE_CMD_INTERFACE)
1378 return("Response not valid.");
1381 if (msg.message != ADMIN_RESPONSE_CMD_ROUTE)
1382 return("Response not valid.");
1385 if (msg.message != ADMIN_RESPONSE_CMD_DIAL)
1386 return("Response not valid.");
1389 if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
1390 return("Response not valid.");
1395 if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
1396 return("Response not valid.");
1400 /* process response */
1401 if (msg.u.x.error) {
1402 return(msg.u.x.message);
1404 printf("Command successfull.\n");
1412 #define GET_NOW() { \
1413 gettimeofday(&now_tv, &now_tz); \
1414 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
1416 const char *admin_testcall(int sock, int argc, char *argv[])
1418 static struct admin_message msg;
1420 int stimeout = 0, ptimeout = 0, atimeout = 0, ctimeout = 0;
1422 double timer = 0, now_d;
1423 unsigned int on = 1;
1424 struct timeval now_tv;
1425 struct timezone now_tz;
1427 printf("pid=%d\n", getpid()); fflush(stdout);
1430 if (!strcmp(argv[ar], "--setup-timeout")) {
1433 return("Missing setup timeout value.\n");
1434 stimeout = atoi(argv[ar]);
1437 if (!strcmp(argv[ar], "--proceeding-timeout")) {
1440 return("Missing proceeding timeout value.\n");
1441 ptimeout = atoi(argv[ar]);
1444 if (!strcmp(argv[ar], "--alerting-timeout")) {
1447 return("Missing alerting timeout value.\n");
1448 atimeout = atoi(argv[ar]);
1451 if (!strcmp(argv[ar], "--connect-timeout")) {
1454 return("Missing connect timeout value.\n");
1455 ctimeout = atoi(argv[ar]);
1462 /* send reload command */
1463 memset(&msg, 0, sizeof(msg));
1464 msg.message = ADMIN_CALL_SETUP;
1465 msg.u.call.present = 1;
1468 SCPY(msg.u.call.interface, argv[ar]);
1472 SCPY(msg.u.call.callerid, argv[ar]);
1476 SCPY(msg.u.call.dialing, argv[ar]);
1480 if (argv[ar][0] == 'r')
1481 msg.u.call.present = 0;
1484 msg.u.call.bc_capa = 0x00; /*INFO_BC_SPEECH*/
1485 msg.u.call.bc_mode = 0x00; /*INFO_BMODE_CIRCUIT*/
1486 msg.u.call.bc_info1 = 0;
1488 msg.u.call.exthlc = 0;
1490 msg.u.call.bc_capa = strtol(argv[ar],NULL,0);
1492 msg.u.call.bc_info1 = 3 | 0x80; /* alaw, if no capability is given at all */
1495 msg.u.call.bc_mode = strtol(argv[ar],NULL,0);
1496 if (msg.u.call.bc_mode) msg.u.call.bc_mode = 2;
1500 msg.u.call.bc_info1 = strtol(argv[ar],NULL,0);
1501 if (msg.u.call.bc_info1 < 0)
1502 msg.u.call.bc_info1 = 0;
1504 msg.u.call.bc_info1 |= 0x80;
1508 msg.u.call.hlc = strtol(argv[ar],NULL,0);
1509 if (msg.u.call.hlc < 0)
1512 msg.u.call.hlc |= 0x80;
1516 msg.u.call.exthlc = strtol(argv[ar],NULL,0);
1517 if (msg.u.call.exthlc < 0)
1518 msg.u.call.exthlc = 0;
1520 msg.u.call.exthlc |= 0x80;
1524 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1525 return("Broken pipe while sending command.");
1527 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
1528 return("Failed to set socket into non-blocking IO.");
1532 timer = now_d + (double)stimeout;
1535 /* receive response */
1537 l = read(sock, &msg, sizeof(msg));
1539 if (errno == EWOULDBLOCK) {
1542 if (timer <= now_d) {
1543 printf("Timeout\n"); fflush(stdout);
1550 return("Broken pipe while receiving response.");
1552 if (l != sizeof(msg))
1553 return("Response has unexpected message size.");
1554 switch(msg.message) {
1555 case ADMIN_CALL_SETUP_ACK:
1556 printf("SETUP ACKNOWLEDGE\n"); fflush(stdout);
1559 case ADMIN_CALL_PROCEEDING:
1560 printf("PROCEEDING\n"); fflush(stdout);
1563 timer = now_d + (double)ptimeout;
1567 case ADMIN_CALL_ALERTING:
1568 printf("ALERTING\n"); fflush(stdout);
1571 timer = now_d + (double)atimeout;
1575 case ADMIN_CALL_CONNECT:
1576 printf("CONNECT\n number=%s\n", msg.u.call.callerid); fflush(stdout);
1579 timer = now_d + (double)ctimeout;
1583 case ADMIN_CALL_NOTIFY:
1584 printf("NOTIFY\n notify=%d\n number=%s\n", msg.u.call.notify_progress, msg.u.call.callerid); fflush(stdout);
1587 case ADMIN_CALL_PROGRESS:
1588 printf("PROGRESS\n progress=%d\n location=%d\n", msg.u.call.notify_progress, msg.u.call.location); fflush(stdout);
1591 case ADMIN_CALL_DISCONNECT:
1592 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);
1595 case ADMIN_CALL_RELEASE:
1596 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);
1600 return("Response not valid.");
1603 printf("Call released.\n"); fflush(stdout);
1611 const char *admin_trace(int sock, int argc, char *argv[])
1613 static struct admin_message msg;
1617 if (argc > 2) if (!strcasecmp(argv[2], "help")) {
1618 printf("Trace Help\n----------\n");
1619 printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
1620 printf("By default a complete trace is shown in detailed format.\n");
1621 printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
1622 printf("Use filter values to select specific trace messages.\n");
1623 printf("All given filter values must match. If no filter is given, anything matches.\n\n");
1624 printf("Filters:\n");
1625 printf(" category=<mask bits>\n");
1626 printf(" 0x01 = CH: channel object trace\n");
1627 printf(" 0x02 = EP: endpoint object trace\n");
1628 printf(" port=<mISDN port> select only given port for trace\n");
1629 printf(" interface=<interface name> select only given interface for trace\n");
1630 printf(" caller=<caller id> select only given caller id for trace\n");
1631 printf(" dialing=<number> select only given dialed number for trace\n");
1635 /* init trace request */
1636 memset(&msg, 0, sizeof(msg));
1637 msg.message = ADMIN_TRACE_REQUEST;
1638 msg.u.trace_req.detail = 3;
1639 msg.u.trace_req.port = -1;
1644 if (!strcasecmp(argv[i], "brief"))
1645 msg.u.trace_req.detail = 1;
1646 else if (!strcasecmp(argv[i], "short"))
1647 msg.u.trace_req.detail = 2;
1648 else if (!strncasecmp(argv[i], "category=", 9))
1649 msg.u.trace_req.category = atoi(argv[i]+9);
1650 else if (!strncasecmp(argv[i], "port=", 5))
1651 msg.u.trace_req.port = atoi(argv[i]+5);
1652 else if (!strncasecmp(argv[i], "interface=", 10))
1653 SCPY(msg.u.trace_req.interface, argv[i]+10);
1654 else if (!strncasecmp(argv[i], "caller=", 7))
1655 SCPY(msg.u.trace_req.caller, argv[i]+7);
1656 else if (!strncasecmp(argv[i], "dialing=", 8))
1657 SCPY(msg.u.trace_req.dialing, argv[i]+8);
1658 else return("Invalid trace option, try 'trace help'.");
1663 /* send trace request */
1664 if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
1665 return("Broken pipe while sending trace request.");
1667 /* receive response */
1669 if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
1670 return("Broken pipe while receiving response.");
1672 if (msg.message != ADMIN_TRACE_RESPONSE)
1673 return("Response not valid.");
1675 printf("%s", msg.u.trace_rsp.text);
1683 int main(int argc, char *argv[])
1687 struct sockaddr_un sock_address;
1688 const char *ret = "invalid mode";
1689 char options_error[256];
1695 printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
1696 printf("state - View current states using graphical console output.\n");
1697 printf("portinfo - Get info of current ports.\n");
1698 printf("interface [<portname>] - Tell LCR to reload \"interface.conf\".\n");
1699 printf("route - Tell LCR to reload \"route.conf\".\n");
1700 printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
1701 printf("release <number> - Tell LCR to release endpoint with given number.\n");
1702 printf("block <port> - Block given port.\n");
1703 printf("unblock/load <port> - Unblock given port.\n");
1704 printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
1705 printf("testcall [options] <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
1706 printf(" -> options = --setup-timeout <seconds> --proceeding-timeout <seconds>\n");
1707 printf(" --alerting-timeout <seconds> --connect-timeout <seconds>\n");
1708 printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
1709 printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
1710 printf(" -> Use 'trace help' to see filter description.\n");
1716 if (!(strcasecmp(argv[1],"state"))) {
1719 if (!(strcasecmp(argv[1],"portinfo"))) {
1720 mode = MODE_PORTINFO;
1722 if (!(strcasecmp(argv[1],"interface"))) {
1723 mode = MODE_INTERFACE;
1725 if (!(strcasecmp(argv[1],"route"))) {
1728 if (!(strcasecmp(argv[1],"dial"))) {
1733 if (!(strcasecmp(argv[1],"release"))) {
1736 mode = MODE_RELEASE;
1738 if (!(strcasecmp(argv[1],"unblock"))
1739 || !(strcasecmp(argv[1],"load"))) {
1742 mode = MODE_UNBLOCK;
1744 if (!(strcasecmp(argv[1],"block"))) {
1749 if (!(strcasecmp(argv[1],"unload"))) {
1754 if (!(strcasecmp(argv[1],"testcall"))) {
1757 mode = MODE_TESTCALL;
1759 if (!(strcasecmp(argv[1],"trace"))) {
1765 if (read_options(options_error) == 0) {
1771 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1772 fprintf(stderr, "Failed to create socket.\n");
1775 memset(&sock_address, 0, sizeof(sock_address));
1776 SPRINT(sock_address.sun_path, SOCKET_NAME, options.lock);
1777 sock_address.sun_family = PF_UNIX;
1778 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1780 fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
1787 ret = admin_state(sock, argv);
1791 ret = admin_portinfo(sock, argc, argv);
1794 case MODE_INTERFACE:
1796 ret = admin_cmd(sock, mode, NULL, NULL);
1800 ret = admin_cmd(sock, mode, argv[2], argv[3]);
1807 ret = admin_cmd(sock, mode, NULL, argv[2]);
1811 ret = admin_testcall(sock, argc, argv);
1815 ret = admin_trace(sock, argc, argv);
1820 /* now we say good bye */
1822 // if (!strncasecmp(ret, "Broken Pipe", 11))
1824 printf("%s\n", ret);