SIP: Fix incoming re-invite
[lcr.git] / trace.c
diff --git a/trace.c b/trace.c
index 9453bd7..0d6c114 100644 (file)
--- a/trace.c
+++ b/trace.c
 struct trace trace;
 char trace_string[MAX_TRACE_ELEMENTS * 100 + 400];
 
-static char *spaces[11] = {
-       "          ",
-       "         ",
-       "        ",
-       "       ",
-       "      ",
-       "     ",
-       "    ",
-       "   ",
-       "  ",
-       " ",
-       "",
-};
+static const char *spaces = "          ";
 
 /*
  * initializes a new trace
  * all values will be reset
  */
-void start_trace(int port, struct interface *interface, char *caller, char *dialing, int direction, int category, int serial, char *name)
+void _start_trace(const char *__file, int __line, int port, struct interface *interface, const char *caller, const char *dialing, int direction, int category, int serial, const char *name)
 {
+       struct timeval current_time;
+
        if (trace.name[0])
-               PERROR("trace already started (name=%s)\n", trace.name);
+               PERROR("trace already started (name=%s) in file %s line %d\n", trace.name, __file, __line);
        memset(&trace, 0, sizeof(struct trace));
        trace.port = port;
        if (interface)
@@ -49,8 +39,11 @@ void start_trace(int port, struct interface *interface, char *caller, char *dial
        trace.serial = serial;
        if (name) if (name[0])
                SCPY(trace.name, name);
-       trace.sec = now_tv.tv_sec;
-       trace.usec = now_tv.tv_usec;
+       if (!trace.name[0])
+               SCPY(trace.name, "<unknown>");
+       gettimeofday(&current_time, NULL);
+       trace.sec = current_time.tv_sec;
+       trace.usec = current_time.tv_usec;
 }
 
 
@@ -59,18 +52,17 @@ void start_trace(int port, struct interface *interface, char *caller, char *dial
  * if subelement is given, element will also contain a subelement
  * if multiple subelements belong to same element, name must be equal for all subelements
  */
-void add_trace(char *name, char *sub, const char *fmt, ...)
+void _add_trace(const char *__file, int __line, const char *name, const char *sub, const char *fmt, ...)
 {
        va_list args;
 
        if (!trace.name[0])
-               PERROR("trace not started\n");
+               PERROR("trace not started in file %s line %d\n", __file, __line);
        
        /* check for required name value */
        if (!name)
                goto nostring;
-       if (!name[0])
-       {
+       if (!name[0]) {
                nostring:
                PERROR("trace with name=%s gets element with no string\n", trace.name);
                return;
@@ -80,8 +72,7 @@ void add_trace(char *name, char *sub, const char *fmt, ...)
        SCPY(trace.element[trace.elements].name, name);
        if (sub) if (sub[0])
                SCPY(trace.element[trace.elements].sub, sub);
-       if (fmt) if (fmt[0])
-       {
+       if (fmt) if (fmt[0]) {
                va_start(args, fmt);
                VUNPRINT(trace.element[trace.elements].value, sizeof(trace.element[trace.elements].value)-1, fmt, args);
                va_end(args);
@@ -101,7 +92,9 @@ static char *print_trace(int detail, int port, char *interface, char *caller, ch
        char buffer[256];
        time_t ti = trace.sec;
        struct tm *tm;
+#ifdef WITH_MISDN
        struct mISDNport *mISDNport;
+#endif
        int i;
 
        trace_string[0] = '\0'; // always clear string
@@ -110,7 +103,7 @@ static char *print_trace(int detail, int port, char *interface, char *caller, ch
                return(NULL);
 
        /* filter trace */
-       if (port && trace.port)
+       if (port >= 0 && trace.port >= 0)
                if (port != trace.port) return(NULL);
        if (interface) if (interface[0] && trace.interface[0])
                if (!!strcasecmp(interface, trace.interface)) return(NULL);
@@ -122,21 +115,18 @@ static char *print_trace(int detail, int port, char *interface, char *caller, ch
                if (!(category & trace.category)) return(NULL);
 
        /* head */
-       if (detail >= 3)
-       {
+       if (detail >= 3) {
                SCAT(trace_string, "------------------------------------------------------------------------------\n");
+#ifdef WITH_MISDN
                /* "Port: 1 (BRI PTMP TE)" */
-               if (trace.port)
-               {
+               if (trace.port >= 0) {
                        mISDNport = mISDNport_first;
-                       while(mISDNport)
-                       {
+                       while(mISDNport) {
                                if (mISDNport->portnum == trace.port)
                                        break;
                                mISDNport = mISDNport->next;
                        }
-                       if (mISDNport)
-                       {
+                       if (mISDNport) {
                                SPRINT(buffer, "Port: %d (%s %s %s)", trace.port, (mISDNport->pri)?"PRI":"BRI", (mISDNport->ptp)?"PTP":"PTMP", (mISDNport->ntmode)?"NT":"TE");
                                /* copy interface, if we have a port */
                                if (mISDNport->ifport) if (mISDNport->ifport->interface)
@@ -145,18 +135,17 @@ static char *print_trace(int detail, int port, char *interface, char *caller, ch
                                SPRINT(buffer, "Port: %d (does not exist)\n", trace.port);
                        SCAT(trace_string, buffer);
                } else
+#endif
                        SCAT(trace_string, "Port: ---");
 
-               if (trace.interface[0])
-               {
+               if (trace.interface[0]) {
                        /* "  Interface: 'Ext'" */
                        SPRINT(buffer, "  Interface: '%s'", trace.interface);
                        SCAT(trace_string, buffer);
                } else
                        SCAT(trace_string, "  Interface: ---");
                        
-               if (trace.caller[0])
-               {
+               if (trace.caller[0]) {
                        /* "  Caller: '021256493'" */
                        SPRINT(buffer, "  Caller: '%s'\n", trace.caller);
                        SCAT(trace_string, buffer);
@@ -168,16 +157,14 @@ static char *print_trace(int detail, int port, char *interface, char *caller, ch
                SPRINT(buffer, "Time: %02d.%02d.%02d %02d:%02d:%02d.%03d", tm->tm_mday, tm->tm_mon+1, tm->tm_year%100, tm->tm_hour, tm->tm_min, tm->tm_sec, trace.usec/1000);
                SCAT(trace_string, buffer);
 
-               if (trace.direction)
-               {
+               if (trace.direction) {
                        /* "  Direction: out" */
                        SPRINT(buffer, "  Direction: %s", (trace.direction==DIRECTION_OUT)?"OUT":"IN");
                        SCAT(trace_string, buffer);
                } else
                        SCAT(trace_string, "  Direction: ---");
 
-               if (trace.dialing[0])
-               {
+               if (trace.dialing[0]) {
                        /* "  Dialing: '57077'" */
                        SPRINT(buffer, "  Dialing: '%s'\n", trace.dialing);
                        SCAT(trace_string, buffer);
@@ -187,16 +174,15 @@ static char *print_trace(int detail, int port, char *interface, char *caller, ch
                SCAT(trace_string, "------------------------------------------------------------------------------\n");
        }
 
-       if (detail < 3)
-       {
+       if (detail < 3) {
                tm = localtime(&ti);
                SPRINT(buffer, "%02d.%02d.%02d %02d:%02d:%02d.%03d ", tm->tm_mday, tm->tm_mon+1, tm->tm_year%100, tm->tm_hour, tm->tm_min, tm->tm_sec, trace.usec/1000);
                SCAT(trace_string, buffer);
        }
 
        /* "CH(45): CC_SETUP (net->user)" */
-       switch (trace.category)
-       {       case CATEGORY_CH:
+       switch (trace.category) {
+               case CATEGORY_CH:
                SCAT(trace_string, "CH");
                break;
 
@@ -208,18 +194,24 @@ static char *print_trace(int detail, int port, char *interface, char *caller, ch
                SCAT(trace_string, "--");
        }
        if (trace.serial)
-               SPRINT(buffer, "(%d): %s", trace.serial, trace.name[0]?trace.name:"<unknown>");
+               SPRINT(buffer, "(%lu): %s", trace.serial, trace.name[0]?trace.name:"<unknown>");
        else
                SPRINT(buffer, ": %s", trace.name[0]?trace.name:"<unknown>");
        SCAT(trace_string, buffer);
 
        /* elements */
-       switch(detail)
-       {
+       switch(detail) {
                case 1: /* brief */
+               if (trace.interface[0]) {
+                       SPRINT(buffer, "  iface %s", trace.interface);
+                       SCAT(trace_string, buffer);
+               }
+               if (trace.port >= 0) {
+                       SPRINT(buffer, "  port %d", trace.port);
+                       SCAT(trace_string, buffer);
+               }
                i = 0;
-               while(i < trace.elements)
-               {
+               while(i < trace.elements) {
                        SPRINT(buffer, "  %s", trace.element[i].name);
                        if (i) if (!strcmp(trace.element[i].name, trace.element[i-1].name))
                                buffer[0] = '\0';
@@ -243,14 +235,13 @@ static char *print_trace(int detail, int port, char *interface, char *caller, ch
                case 3: /* long */
                SCAT(trace_string, "\n");
                i = 0;
-               while(i < trace.elements)
-               {
-                       SPRINT(buffer, " %s%s", trace.element[i].name, spaces[strlen(trace.element[i].name)]);
+               while(i < trace.elements) {
+                       SPRINT(buffer, " %s%s", trace.element[i].name, &spaces[strlen(trace.element[i].name)]);
                        if (i) if (!strcmp(trace.element[i].name, trace.element[i-1].name))
                                SPRINT(buffer, "           ");
                        SCAT(trace_string, buffer);
                        if (trace.element[i].sub[0])
-                               SPRINT(buffer, " : %s%s = ", trace.element[i].sub, spaces[strlen(trace.element[i].sub)]);
+                               SPRINT(buffer, " : %s%s = ", trace.element[i].sub, &spaces[strlen(trace.element[i].sub)]);
                        else
                                SPRINT(buffer, " :              ");
                        SCAT(trace_string, buffer);
@@ -275,31 +266,28 @@ static char *print_trace(int detail, int port, char *interface, char *caller, ch
  * trace ends
  * this function will put the trace to sockets and logfile, if requested
  */
-void end_trace(void)
+void _end_trace(const char *__file, int __line)
 {
        char *string;
        FILE *fp;
        struct admin_list       *admin;
        struct admin_queue      *response, **responsep; /* response pointer */
+       int __attribute__((__unused__)) ret;
 
        if (!trace.name[0])
-               PERROR("trace not started\n");
+               PERROR("trace not started in file %s line %d\n", __file, __line);
        
-       if (options.deb || options.log[0])
-       {
-               string = print_trace(1, 0, NULL, NULL, NULL, 0);
-               if (string)
-               {
+       if (options.deb || options.log[0]) {
+               string = print_trace(1, -1, NULL, NULL, NULL, 0);
+               if (string) {
                        /* process debug */
                        if (options.deb)
-                               debug(NULL, 0, "trace", string);
+                               debug(NULL, NULL, 0, "TRACE", string);
                        /* process log */
-                       if (options.log[0])
-                       {
+                       if (options.log[0]) {
                                fp = fopen(options.log, "a");
-                               if (fp)
-                               {
-                                       fwrite(string, strlen(string), 1, fp);
+                               if (fp) {
+                                       ret = fwrite(string, strlen(string), 1, fp);
                                        fclose(fp);
                                }
                        }
@@ -307,29 +295,22 @@ void end_trace(void)
        }
 
        /* process admin */
-       admin = admin_list;
-       while(admin)
-       {
-               if (admin->trace.detail)
-               {
+       admin = admin_first;
+       while(admin) {
+               if (admin->trace.detail) {
                        string = print_trace(admin->trace.detail, admin->trace.port, admin->trace.interface, admin->trace.caller, admin->trace.dialing, admin->trace.category);
-                       if (string)
-                       {
+                       if (string) {
                                /* seek to end of response list */
                                response = admin->response;
                                responsep = &admin->response;
-                               while(response)
-                               {
+                               while(response) {
                                        responsep = &response->next;
                                        response = response->next;
                                }
 
                                /* create state response */
-                               response = (struct admin_queue *)malloc(sizeof(struct admin_queue)+sizeof(admin_message));
-                               if (!response)
-                                       return;
+                               response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
                                memuse++;
-                               memset(response, 0, sizeof(admin_queue)+sizeof(admin_message));
                                response->num = 1;
                                /* message */
                                response->am[0].message = ADMIN_TRACE_RESPONSE;
@@ -338,6 +319,7 @@ void end_trace(void)
                                /* attach to response chain */
                                *responsep = response;
                                responsep = &response->next;
+                               admin->fd.when |= LCR_FD_WRITE;
                        }
                }
                admin = admin->next;
@@ -350,4 +332,3 @@ void end_trace(void)
 
 
 
-