fix a bug in computing line numbers
[asterisk/asterisk.git] / pbx / dundi-parser.c
old mode 100755 (executable)
new mode 100644 (file)
index 45e186c..9b9d9d2
@@ -1,30 +1,49 @@
 /*
- * Distributed Universal Number Discovery (DUNDi)
+ * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 2004, Digium Inc.
+ * Copyright (C) 1999 - 2005, Digium, Inc.
  *
- * Written by Mark Spencer <markster@digium.com>
+ * Mark Spencer <markster@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Distributed Universal Number Discovery (DUNDi)
  *
- * This program is Free Software distributed under the terms of
- * of the GNU General Public License.
  */
 
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <string.h>
 #include <netinet/in.h>
-#include <asterisk/frame.h>
-#include <asterisk/utils.h>
 #include <arpa/inet.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdio.h>
-#include <asterisk/dundi.h>
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/frame.h"
+#include "asterisk/utils.h"
+#include "asterisk/dundi.h"
 #include "dundi-parser.h"
-#include <asterisk/dundi.h>
+#include "asterisk/dundi.h"
+
 static void internaloutput(const char *str)
 {
-       printf(str);
+       fputs(str, stdout);
 }
 
 static void internalerror(const char *str)
@@ -91,6 +110,7 @@ int dundi_str_short_to_eid(dundi_eid *eid, char *s)
                eid->eid[x] = eid_int[x];
        return 0;
 }
+
 int dundi_eid_zero(dundi_eid *eid)
 {
        int x;
@@ -113,6 +133,13 @@ static void dump_string(char *output, int maxlen, void *value, int len)
        output[maxlen] = '\0';
 }
 
+static void dump_cbypass(char *output, int maxlen, void *value, int len)
+{
+       maxlen--;
+       strncpy(output, "Bypass Caches", maxlen);
+       output[maxlen] = '\0';
+}
+
 static void dump_eid(char *output, int maxlen, void *value, int len)
 {
        if (len == 6)
@@ -222,8 +249,7 @@ static void dump_byte(char *output, int maxlen, void *value, int len)
 }
 
 static char *proto2str(int proto, char *buf, int bufsiz)
-{
-       
+{      
        switch(proto) {
        case DUNDI_PROTO_NONE:
                strncpy(buf, "None", bufsiz - 1);
@@ -357,6 +383,7 @@ static struct dundi_ie {
        { DUNDI_IE_EMAIL, "EMAIL", dump_string },
        { DUNDI_IE_PHONE, "PHONE", dump_string },
        { DUNDI_IE_IPADDR, "ADDRESS", dump_string },
+       { DUNDI_IE_CACHEBYPASS, "CBYPASS", dump_cbypass },
 };
 
 const char *dundi_ie2str(int ie)
@@ -379,7 +406,7 @@ static void dump_ies(unsigned char *iedata, int spaces, int len)
        char tmp[1024];
        if (len < 2)
                return;
-       while(len > 2) {
+       while(len >= 2) {
                ie = iedata[0];
                ielen = iedata[1];
                /* Encrypted data is the remainder */
@@ -431,8 +458,8 @@ void dundi_showframe(struct dundi_hdr *fhi, int rx, struct sockaddr_in *sin, int
                "DPRESPONSE  ",
                "EIDQUERY    ",
                "EIDRESPONSE ",
-               "UNKNOWN (5)?",
-               "UNKNOWN (6)?",
+               "PRECACHERQ  ",
+               "PRECACHERP  ",
                "INVALID     ",
                "UNKNOWN CMD ",
                "NULL        ",
@@ -464,14 +491,14 @@ void dundi_showframe(struct dundi_hdr *fhi, int rx, struct sockaddr_in *sin, int
        }
        snprintf(subclass2, (int)sizeof(subclass2), "%02x", fhi->cmdflags);
        subclass = subclass2;
-snprintf(tmp, (int)sizeof(tmp), 
-"%s-Frame Retry[%s] -- OSeqno: %3.3d ISeqno: %3.3d Type: %s (%s)\n",
-       pref[rx],
-       retries, fhi->oseqno, fhi->iseqno, class, fhi->cmdresp & 0x40 ? "Response" : "Command");
+       snprintf(tmp, (int)sizeof(tmp), 
+               "%s-Frame Retry[%s] -- OSeqno: %3.3d ISeqno: %3.3d Type: %s (%s)\n",
+               pref[rx],
+               retries, fhi->oseqno, fhi->iseqno, class, fhi->cmdresp & 0x40 ? "Response" : "Command");
        outputf(tmp);
-snprintf(tmp, (int)sizeof(tmp), 
-"%s     Flags: %s STrans: %5.5d  DTrans: %5.5d [%s:%d]%s\n", (rx > 1) ? "     " : "",
-       subclass, ntohs(fhi->strans) & ~DUNDI_FLAG_RESERVED, ntohs(fhi->dtrans) & ~DUNDI_FLAG_RETRANS,
+       snprintf(tmp, (int)sizeof(tmp), 
+               "%s     Flags: %s STrans: %5.5d  DTrans: %5.5d [%s:%d]%s\n", (rx > 1) ? "     " : "",
+               subclass, ntohs(fhi->strans) & ~DUNDI_FLAG_RESERVED, ntohs(fhi->dtrans) & ~DUNDI_FLAG_RETRANS,
                ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), ntohs(sin->sin_port),
                fhi->cmdresp & 0x80 ? " (Final)" : "");
        outputf(tmp);
@@ -493,7 +520,7 @@ int dundi_ie_append_raw(struct dundi_ie_data *ied, unsigned char ie, void *data,
        return 0;
 }
 
-int dundi_ie_append_cause(struct dundi_ie_data *ied, unsigned char ie, unsigned char cause, unsigned char *data)
+int dundi_ie_append_cause(struct dundi_ie_data *ied, unsigned char ie, unsigned char cause, char *data)
 {
        char tmp[256];
        int datalen = data ? strlen(data) + 1 : 1;
@@ -510,7 +537,7 @@ int dundi_ie_append_cause(struct dundi_ie_data *ied, unsigned char ie, unsigned
        return 0;
 }
 
-int dundi_ie_append_hint(struct dundi_ie_data *ied, unsigned char ie, unsigned short flags, unsigned char *data)
+int dundi_ie_append_hint(struct dundi_ie_data *ied, unsigned char ie, unsigned short flags, char *data)
 {
        char tmp[256];
        int datalen = data ? strlen(data) + 2 : 2;
@@ -549,7 +576,7 @@ int dundi_ie_append_encdata(struct dundi_ie_data *ied, unsigned char ie, unsigne
        return 0;
 }
 
-int dundi_ie_append_answer(struct dundi_ie_data *ied, unsigned char ie, dundi_eid *eid, unsigned char protocol, unsigned short flags, unsigned short weight, unsigned char *data)
+int dundi_ie_append_answer(struct dundi_ie_data *ied, unsigned char ie, dundi_eid *eid, unsigned char protocol, unsigned short flags, unsigned short weight, char *data)
 {
        char tmp[256];
        int datalen = data ? strlen(data) + 11 : 11;
@@ -595,7 +622,7 @@ int dundi_ie_append_short(struct dundi_ie_data *ied, unsigned char ie, unsigned
        return dundi_ie_append_raw(ied, ie, &newval, (int)sizeof(newval));
 }
 
-int dundi_ie_append_str(struct dundi_ie_data *ied, unsigned char ie, unsigned char *str)
+int dundi_ie_append_str(struct dundi_ie_data *ied, unsigned char ie, char *str)
 {
        return dundi_ie_append_raw(ied, ie, str, strlen(str));
 }
@@ -662,10 +689,10 @@ int dundi_parse_ies(struct dundi_ies *ies, unsigned char *data, int datalen)
                                ies->reqeid = (dundi_eid *)(data + 2);
                        break;
                case DUNDI_IE_CALLED_CONTEXT:
-                       ies->called_context = data + 2;
+                       ies->called_context = (char *)data + 2;
                        break;
                case DUNDI_IE_CALLED_NUMBER:
-                       ies->called_number = data + 2;
+                       ies->called_number = (char *)data + 2;
                        break;
                case DUNDI_IE_ANSWER:
                        if (len < sizeof(struct dundi_answer)) {
@@ -717,7 +744,7 @@ int dundi_parse_ies(struct dundi_ies *ies, unsigned char *data, int datalen)
                case DUNDI_IE_CAUSE:
                        if (len >= 1) {
                                ies->cause = data[2];
-                               ies->causestr = data + 3;
+                               ies->causestr = (char *)data + 3;
                        } else {
                                snprintf(tmp, (int)sizeof(tmp), "Expected at least one byte cause, but was %d long\n", len);
                                errorf(tmp);
@@ -732,28 +759,28 @@ int dundi_parse_ies(struct dundi_ies *ies, unsigned char *data, int datalen)
                        }
                        break;
                case DUNDI_IE_DEPARTMENT:
-                       ies->q_dept = data + 2;
+                       ies->q_dept = (char *)data + 2;
                        break;
                case DUNDI_IE_ORGANIZATION:
-                       ies->q_org = data + 2;
+                       ies->q_org = (char *)data + 2;
                        break;
                case DUNDI_IE_LOCALITY:
-                       ies->q_locality = data + 2;
+                       ies->q_locality = (char *)data + 2;
                        break;
                case DUNDI_IE_STATE_PROV:
-                       ies->q_stateprov = data + 2;
+                       ies->q_stateprov = (char *)data + 2;
                        break;
                case DUNDI_IE_COUNTRY:
-                       ies->q_country = data + 2;
+                       ies->q_country = (char *)data + 2;
                        break;
                case DUNDI_IE_EMAIL:
-                       ies->q_email = data + 2;
+                       ies->q_email = (char *)data + 2;
                        break;
                case DUNDI_IE_PHONE:
-                       ies->q_phone = data + 2;
+                       ies->q_phone = (char *)data + 2;
                        break;
                case DUNDI_IE_IPADDR:
-                       ies->q_ipaddr = data + 2;
+                       ies->q_ipaddr = (char *)data + 2;
                        break;
                case DUNDI_IE_ENCDATA:
                        /* Recalculate len as the remainder of the message, regardless of
@@ -783,6 +810,9 @@ int dundi_parse_ies(struct dundi_ies *ies, unsigned char *data, int datalen)
                                errorf(tmp);
                        }
                        break;
+               case DUNDI_IE_CACHEBYPASS:
+                       ies->cbypass = 1;
+                       break;
                default:
                        snprintf(tmp, (int)sizeof(tmp), "Ignoring unknown information element '%s' (%d) of length %d\n", dundi_ie2str(ie), ie, len);
                        outputf(tmp);