backup
[lcr.git] / admin_client.c
index 41430f6..5ab45f6 100644 (file)
@@ -1,6 +1,6 @@
 /*****************************************************************************\
 **                                                                           **
-** PBX4Linux                                                                 **
+** LCR                                                                       **
 **                                                                           **
 **---------------------------------------------------------------------------**
 ** Copyright: Andreas Eversberg                                              **
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <curses.h>
-#include "save.h"
+#include "macro.h"
 #include "call.h"
 #include "callpbx.h"
+#include "extension.h"
+#include "message.h"
 #include "admin.h"
 #include "cause.h"
 
@@ -42,7 +44,11 @@ enum {
        MODE_ROUTE,
        MODE_DIAL,
        MODE_RELEASE,
+       MODE_UNBLOCK,
+       MODE_BLOCK,
+       MODE_UNLOAD,
        MODE_TESTCALL,
+       MODE_TRACE,
 };
 
 char *text_interfaces[] = {
@@ -458,7 +464,7 @@ char *admin_state(int sock)
        /* send reload command */
        memset(&msg, 0, sizeof(msg));
        msg.message = ADMIN_REQUEST_STATE;
-//     printf("sizeof=%d\n",sizeof(msg));
+//     printf("sizeof=%d\n",sizeof(msg));fflush(stdout);
        if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
        {
                cleanup_curses();
@@ -471,32 +477,31 @@ char *admin_state(int sock)
                cleanup_curses();
                return("Broken pipe while receiving response.");
        }
+
        if (msg.message != ADMIN_RESPONSE_STATE)
        {
                cleanup_curses();
                return("Response not valid. Expecting state response.");
        }
        num = msg.u.s.interfaces + msg.u.s.calls + msg.u.s.epoints + msg.u.s.ports;
-       if (!(m = (struct admin_message *)malloc(num*sizeof(struct admin_message))))
-       {
-               cleanup_curses();
-               return("Not enough memory for messages.");
-       }
+       m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
        off=0;
-readagain:
-       if ((len = read(sock, ((unsigned char *)(m))+off,
-                                       num*sizeof(struct admin_message)-off)) != num*sizeof(struct admin_message)-off)
+       if (num)
        {
-               if (len <= 0) {
-                       free(m);
-//                     fprintf(stderr, "got=%d expected=%d\n", i, num*sizeof(struct admin_message));
-                       cleanup_curses();
-                       return("Broken pipe while receiving state infos.");
-               }
-               if (len < num*sizeof(struct admin_message))
+               readagain:
+               if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off)
                {
-                       off+=len;
-                       goto readagain;
+                       if (len <= 0) {
+                               FREE(m, 0);
+       //                      fprintf(stderr, "got=%d expected=%d\n", i, num*sizeof(struct admin_message));
+                               cleanup_curses();
+                               return("Broken pipe while receiving state infos.");
+                       }
+                       if (len < num*(int)sizeof(struct admin_message))
+                       {
+                               off+=len;
+                               goto readagain;
+                       }
                }
        }
        j = 0;
@@ -507,7 +512,7 @@ readagain:
 //             fprintf(stderr, "j=%d message=%d\n", j, m[j].message);
                if (m[j].message != ADMIN_RESPONSE_S_INTERFACE)
                {
-                       free(m);
+                       FREE(m, 0);
                        cleanup_curses();
                        return("Response not valid. Expecting interface information.");
                }
@@ -519,7 +524,7 @@ readagain:
        {
                if (m[j].message != ADMIN_RESPONSE_S_CALL)
                {
-                       free(m);
+                       FREE(m, 0);
                        cleanup_curses();
                        return("Response not valid. Expecting call information.");
                }
@@ -531,7 +536,7 @@ readagain:
        {
                if (m[j].message != ADMIN_RESPONSE_S_EPOINT)
                {
-                       free(m);
+                       FREE(m, 0);
                        cleanup_curses();
                        return("Response not valid. Expecting endpoint information.");
                }
@@ -543,7 +548,7 @@ readagain:
        {
                if (m[j].message != ADMIN_RESPONSE_S_PORT)
                {
-                       free(m);
+                       FREE(m, 0);
                        cleanup_curses();
                        return("Response not valid. Expecting port information.");
                }
@@ -633,112 +638,125 @@ readagain:
                        /* show interface summary */
                        move(++line>1?line:1, 0);
                        color(white);
-
-                       SPRINT(buffer, "%s(%d) '%s' %s use:%d ", (m[i].u.i.ntmode)?"NT":"TE", m[i].u.i.portnum, m[i].u.i.interface_name, (m[i].u.i.ptp)?"ptp ":"ptmp", m[i].u.i.use);
-                       addstr(buffer);
-                       if (m[i].u.i.ptp || !m[i].u.i.ntmode)
+                       if (m[i].u.i.block >= 2)
                        {
-                               color((m[i].u.i.l2link)?green:red);
-                               addstr((m[i].u.i.l2link)?"  L2 UP":"  L2 down");
-                       }
-                       color((m[i].u.i.l1link)?green:blue);
-                       addstr((m[i].u.i.l1link)?"  L1 ACTIVE":"  L1 inactive");
-                       if (line+2 >= LINES) goto end;
-                       /* show channels */
-                       if (show_interfaces > 1)
+                               SPRINT(buffer, "%s (%d)%s", m[i].u.i.interface_name, m[i].u.i.portnum, (m[i].u.i.extension)?" (extension)":"");
+                               addstr(buffer);
+                               color(red);
+                               addstr("  not loaded");
+                       } else
                        {
-                               ltee = 0;
-                               j = k =0;
-                               jj = m[i].u.i.channels;
-                               while(j < jj)
+                               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);
+                               addstr(buffer);
+                               if (m[i].u.i.ptp || !m[i].u.i.ntmode)
+                               {
+                                       color((m[i].u.i.l2link)?green:red);
+                                       addstr((m[i].u.i.l2link)?"  L2 UP":"  L2 down");
+                               }
+                               color((m[i].u.i.l1link)?green:blue);
+                               addstr((m[i].u.i.l1link)?"  L1 ACTIVE":"  L1 inactive");
+                               if (m[i].u.i.block)
+                               {
+                                       color(red);
+                                       addstr("  blocked");
+                               }
+                               if (line+2 >= LINES) goto end;
+                               /* show channels */
+                               if (show_interfaces > 1)
                                {
-                                       /* show all channels */
-                                       if (show_interfaces>2 || m[i].u.i.busy[j]>0)
+                                       ltee = 0;
+                                       j = k =0;
+                                       jj = m[i].u.i.channels;
+                                       while(j < jj)
                                        {
-                                               color(cyan);
-                                               /* show left side / right side */
-                                               if ((k & 1) && (COLS > 70))
-                                               {
-                                                       move(line>1?line:1,4+((COLS-4)/2));
-                                               } else
+                                               /* show all channels */
+                                               if (show_interfaces>2 || m[i].u.i.busy[j]>0)
                                                {
-                                                       move(++line>1?line:1, 1);
-                                                       LTEE
-                                                       ltee = 1;
-                                               }
-                                               k++;
-                                               color(white);
-                                               if (m[i].u.i.pri)
-                                                       SPRINT(buffer,"S%2d: ", j+1+(j>=15));
-                                               else
-                                                       SPRINT(buffer,"B%2d: ", j+1);
-                                               addstr(buffer);
-                                               if (!m[i].u.i.ptp)
-                                                       goto ptmp;
-                                               if (m[i].u.i.l2link)
-                                               {
-                                                       ptmp:
-                                                       color((m[i].u.i.busy[j])?yellow:blue);
-                                                       addstr((m[i].u.i.busy[j])?"busy":"idle");
-                                               } else
-                                               {
-                                                       color(red);
-                                                       addstr("blk ");
-                                               }
-                                               if (m[i].u.i.port[j])
-                                               {
-                                                       /* search for port */
-                                                       l = msg.u.s.interfaces+msg.u.s.calls+msg.u.s.epoints;
-                                                       ll = l+msg.u.s.ports;
-                                                       while(l < ll)
+                                                       color(cyan);
+                                                       /* show left side / right side */
+                                                       if ((k & 1) && (COLS > 70))
+                                                       {
+                                                               move(line>1?line:1,4+((COLS-4)/2));
+                                                       } else
+                                                       {
+                                                               move(++line>1?line:1, 1);
+                                                               LTEE
+                                                               ltee = 1;
+                                                       }
+                                                       k++;
+                                                       color(white);
+                                                       if (m[i].u.i.pri)
+                                                               SPRINT(buffer,"S%2d: ", j+1+(j>=15));
+                                                       else
+                                                               SPRINT(buffer,"B%2d: ", j+1);
+                                                       addstr(buffer);
+                                                       if (!m[i].u.i.ptp)
+                                                               goto ptmp;
+                                                       if (m[i].u.i.l2link && m[i].u.i.block==0)
                                                        {
-                                                               if (m[l].u.p.serial == m[i].u.i.port[j])
+                                                               ptmp:
+                                                               color((m[i].u.i.busy[j])?yellow:blue);
+                                                               addstr((m[i].u.i.busy[j])?"busy":"idle");
+                                                       } else
+                                                       {
+                                                               color(red);
+                                                               addstr("blk ");
+                                                       }
+                                                       if (m[i].u.i.port[j])
+                                                       {
+                                                               /* search for port */
+                                                               l = msg.u.s.interfaces+msg.u.s.calls+msg.u.s.epoints;
+                                                               ll = l+msg.u.s.ports;
+                                                               while(l < ll)
                                                                {
-                                                                       SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
-                                                                       addstr(buffer);
+                                                                       if (m[l].u.p.serial == m[i].u.i.port[j])
+                                                                       {
+                                                                               SPRINT(buffer, " %s(%ld)", m[l].u.p.name, m[l].u.p.serial);
+                                                                               addstr(buffer);
+                                                                       }
+                                                                       l++;
                                                                }
-                                                               l++;
                                                        }
-                                               }
-                                               if (line+2 >= LINES)
-                                               {
-                                                       if (ltee)
+                                                       if (line+2 >= LINES)
                                                        {
-                                                               color(cyan);
-                                                               move(line>1?line:1, 1);
-                                                               LLCORNER
+                                                               if (ltee)
+                                                               {
+                                                                       color(cyan);
+                                                                       move(line>1?line:1, 1);
+                                                                       LLCORNER
+                                                               }
+                                                               goto end;
                                                        }
-                                                       goto end;
                                                }
+                                               j++;
                                        }
-                                       j++;
-                               }
-                               if (ltee)
-                               {
-                                       color(cyan);
-                                       move(line>1?line:1, 1);
-                                       LLCORNER
-                               }
-                               if (line+2 >= LINES) goto end;
-                               /* show summary if no channels were shown */
-                               if (show_interfaces<2 && ltee==0)
-                               {
-                                       color(cyan);
-                                       move(++line>1?line:1, 1);
-                                       LLCORNER
-                                               
-                                       if (m[i].u.i.l2link)
+                                       if (ltee)
                                        {
-                                               color(green);
-                                               SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
-                                       } else
+                                               color(cyan);
+                                               move(line>1?line:1, 1);
+                                               LLCORNER
+                                       }
+                                       if (line+2 >= LINES) goto end;
+                                       /* show summary if no channels were shown */
+                                       if (show_interfaces<2 && ltee==0)
                                        {
-                                               color(red);
-                                               SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
+                                               color(cyan);
+                                               move(++line>1?line:1, 1);
+                                               LLCORNER
+                                                       
+                                               if (m[i].u.i.l2link && m[i].u.i.block==0)
+                                               {
+                                                       color(green);
+                                                       SPRINT(buffer,"all %d channels free", m[i].u.i.channels);
+                                               } else
+                                               {
+                                                       color(red);
+                                                       SPRINT(buffer,"all %d channels blocked", m[i].u.i.channels);
+                                               }
+                                               addstr(buffer);
                                        }
-                                       addstr(buffer);
+                                       if (line+2 >= LINES) goto end;
                                }
-                               if (line+2 >= LINES) goto end;
                        }
                        i++;
                        anything = 1;
@@ -926,14 +944,14 @@ readagain:
 
        end:
        /* free memory */
-       free(m);
+       FREE(m, 0);
        /* display name/time */
 //     move(0, 0);
 //     hline(' ', COLS);
        move(0, 0);
        color(white);
        msg.u.s.version_string[sizeof(msg.u.s.version_string)-1] = '\0';
-       SPRINT(buffer, "PBX4Linux %s", msg.u.s.version_string);
+       SPRINT(buffer, "LCR %s", msg.u.s.version_string);
        addstr(buffer);
        if (COLS>50)
        {
@@ -1052,6 +1070,21 @@ char *admin_cmd(int sock, int mode, char *extension, char *number)
                msg.message = ADMIN_REQUEST_CMD_RELEASE;
                SCPY(msg.u.x.message, number);
                break;
+               case MODE_UNBLOCK:
+               msg.message = ADMIN_REQUEST_CMD_BLOCK;
+               msg.u.x.portnum = atoi(number);
+               msg.u.x.block = 0;
+               break;
+               case MODE_BLOCK:
+               msg.message = ADMIN_REQUEST_CMD_BLOCK;
+               msg.u.x.portnum = atoi(number);
+               msg.u.x.block = 1;
+               break;
+               case MODE_UNLOAD:
+               msg.message = ADMIN_REQUEST_CMD_BLOCK;
+               msg.u.x.portnum = atoi(number);
+               msg.u.x.block = 2;
+               break;
        }
 
        if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
@@ -1078,6 +1111,12 @@ char *admin_cmd(int sock, int mode, char *extension, char *number)
                if (msg.message != ADMIN_RESPONSE_CMD_RELEASE)
                        return("Response not valid.");
                break;
+               case MODE_UNBLOCK:
+               case MODE_BLOCK:
+               case MODE_UNLOAD:
+               if (msg.message != ADMIN_RESPONSE_CMD_BLOCK)
+                       return("Response not valid.");
+               break;
        }
 
        /* process response */
@@ -1202,6 +1241,79 @@ next:
 
 
 /*
+ * makes a trace
+ */
+char *admin_trace(int sock, int argc, char *argv[])
+{
+       static struct admin_message msg;
+       int i;
+
+       /* show help */
+       if (argc > 2) if (!strcasecmp(argv[2], "help"))
+       {
+               printf("Trace Help\n----------\n");
+               printf("%s trace [brief|short] [<filter>=<value> [...]]\n\n", argv[0]);
+               printf("By default a complete trace is shown in detailed format.\n");
+               printf("To show a more compact format, use 'brief' or 'short' keyword.\n");
+               printf("Use filter values to select specific trace messages.\n");
+               printf("All given filter values must match. If no filter is given, anything matches.\n\n");
+               printf("Filters:\n");
+               printf(" category=<mask bits>\n");
+               printf("  0x01 = CH: channel object trace\n");
+               printf("  0x02 = EP: endpoint object trace\n");
+               printf(" port=<mISDN port>  select only given port for trace\n");
+               printf(" interface=<interface name>  select only given interface for trace\n");
+               printf(" caller=<caller id>  select only given caller id for trace\n");
+               printf(" dialing=<number>  select only given dialed number for trace\n");
+               return(NULL);
+       }
+
+       /* init trace request */        
+       memset(&msg, 0, sizeof(msg));
+       msg.message = ADMIN_TRACE_REQUEST;
+       msg.u.trace_req.detail = 3;
+
+       /* parse args */
+       i = 2;
+       while(i < argc)
+       {
+               if (!strcasecmp(argv[i], "brief"))
+                       msg.u.trace_req.detail = 1;
+               else if (!strcasecmp(argv[i], "short"))
+                       msg.u.trace_req.detail = 2;
+               else if (!strncasecmp(argv[i], "category=", 9))
+                       msg.u.trace_req.category = atoi(argv[i]+9);
+               else if (!strncasecmp(argv[i], "port=", 5))
+                       msg.u.trace_req.port = atoi(argv[i]+5);
+               else if (!strncasecmp(argv[i], "interface=", 10))
+                       SCPY(msg.u.trace_req.interface, argv[i]+10);
+               else if (!strncasecmp(argv[i], "caller=", 7))
+                       SCPY(msg.u.trace_req.caller, argv[i]+7);
+               else if (!strncasecmp(argv[i], "dialing=", 8))
+                       SCPY(msg.u.trace_req.dialing, argv[i]+8);
+               else return("Invalid trace option, try 'trace help'.");
+
+               i++;
+       }
+
+       /* send trace request */
+       if (write(sock, &msg, sizeof(msg)) != sizeof(msg))
+               return("Broken pipe while sending trace request.");
+
+       /* receive response */
+next:
+       if (read(sock, &msg, sizeof(msg)) != sizeof(msg))
+               return("Broken pipe while receiving response.");
+
+       if (msg.message != ADMIN_TRACE_RESPONSE)
+               return("Response not valid.");
+
+       printf("%s", msg.u.trace_rsp.text);
+       goto next;
+}
+
+
+/*
  * main function
  */
 int main(int argc, char *argv[])
@@ -1220,12 +1332,17 @@ int main(int argc, char *argv[])
                printf("\n");
                printf("Usage: %s state | interface | route | dial ...\n", argv[0]);
                printf("state - View current states using graphical console output.\n");
-               printf("interface - Tell PBX to reload \"interface.conf\".\n");
-               printf("route - Tell PBX to reload \"route.conf\".\n");
-               printf("dial <extension> <number> - Tell PBX the next number to dial for extension.\n");
-               printf("release <number> - Tell PBX to release endpoint with given number.\n");
+               printf("interface - Tell LCR to reload \"interface.conf\".\n");
+               printf("route - Tell LCR to reload \"route.conf\".\n");
+               printf("dial <extension> <number> - Tell LCR the next number to dial for extension.\n");
+               printf("release <number> - Tell LCR to release endpoint with given number.\n");
+               printf("block <port> - Block given port.\n");
+               printf("unblock <port> - Unblock given port.\n");
+               printf("unload <port> - Unload port. To load port use 'block' or 'unblock'.\n");
                printf("testcall <interface> <callerid> <number> [present|restrict [<capability>]] - Testcall\n");
                printf(" -> capability = <bc> <mode> <codec> <hlc> <exthlc> (Values must be numbers, -1 to omit.)\n");
+               printf("trace [brief|short] [<filter> [...]] - Shows call trace. Use filter to reduce output.\n");
+               printf(" -> Use 'trace help' to see filter description.\n");
                printf("\n");
                return(0);
        }
@@ -1255,12 +1372,34 @@ int main(int argc, char *argv[])
                        goto usage;
                mode = MODE_RELEASE;
        } else
+       if (!(strcasecmp(argv[1],"unblock")))
+       {
+               if (argc <= 2)
+                       goto usage;
+               mode = MODE_UNBLOCK;
+       } else
+       if (!(strcasecmp(argv[1],"block")))
+       {
+               if (argc <= 2)
+                       goto usage;
+               mode = MODE_BLOCK;
+       } else
+       if (!(strcasecmp(argv[1],"unload")))
+       {
+               if (argc <= 2)
+                       goto usage;
+               mode = MODE_UNLOAD;
+       } else
        if (!(strcasecmp(argv[1],"testcall")))
        {
                if (argc <= 4)
                        goto usage;
                mode = MODE_TESTCALL;
        } else
+       if (!(strcasecmp(argv[1],"trace")))
+       {
+               mode = MODE_TRACE;
+       } else
        {
                goto usage;
        }
@@ -1278,7 +1417,7 @@ int main(int argc, char *argv[])
        if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
        {
                close(sock);
-               fprintf(stderr, "Failed to connect to socket \"%s\".\nIs PBX4Linux running?\n", sock_address.sun_path);
+               fprintf(stderr, "Failed to connect to socket \"%s\".\nIs LCR running?\n", sock_address.sun_path);
                exit(EXIT_FAILURE);
        }
 
@@ -1299,11 +1438,17 @@ int main(int argc, char *argv[])
                break;
 
                case MODE_RELEASE:
+               case MODE_UNBLOCK:
+               case MODE_BLOCK:
+               case MODE_UNLOAD:
                ret = admin_cmd(sock, mode, NULL, argv[2]);
                break;
 
                case MODE_TESTCALL:
                ret = admin_testcall(sock, argc, argv);
+
+               case MODE_TRACE:
+               ret = admin_trace(sock, argc, argv);
        }
 
        close(sock);