checkAPIs.pl: support for new-style dissectors in check_hf_entries
[metze/wireshark/wip.git] / epan / address_types.c
index 52d2639430dfaad2e5a29cba77f9e1e2478e6313..9e575a1f15a7f97650b4faadd8a86fe9d621d543 100644 (file)
@@ -4,19 +4,7 @@
  * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #include "config.h"
 #include "wsutil/str_util.h"
 #include "wsutil/inet_addr.h"
 
-#include <epan/dissectors/packet-mtp3.h>
-
 struct _address_type_t {
     int                     addr_type; /* From address_type enumeration or registered value */
     const char             *name;
     const char             *pretty_name;
     AddrValueToString       addr_to_str;
     AddrValueToStringLen    addr_str_len;
+    AddrValueToByte         addr_to_byte;
     AddrColFilterString     addr_col_filter;
     AddrFixedLen            addr_fixed_len;
     AddrNameResolutionToString addr_name_res_str;
@@ -47,14 +34,14 @@ struct _address_type_t {
     /* XXX - Some sort of compare functions (like ftype)? ***/
 };
 
-#define MAX_DISSECTOR_ADDR_TYPE     20
+#define MAX_DISSECTOR_ADDR_TYPE     30
 #define MAX_ADDR_TYPE_VALUE (AT_END_OF_LIST+MAX_DISSECTOR_ADDR_TYPE)
 
 static int num_dissector_addr_type;
 static address_type_t dissector_type_addresses[MAX_DISSECTOR_ADDR_TYPE];
 
 /* Keep track of address_type_t's via their id number */
-static address_type_t* type_list[MAX_ADDR_TYPE_VALUE];
+static address_type_t* type_list[MAX_ADDR_TYPE_VALUE + 1];
 
 /*
  * If a user _does_ pass in a too-small buffer, this is probably
@@ -74,11 +61,11 @@ static void address_type_register(int addr_type, address_type_t *at)
     g_assert(type_list[addr_type] == NULL);
 
     /* Sanity check */
-    DISSECTOR_ASSERT(at->name);
-    DISSECTOR_ASSERT(at->pretty_name);
-    DISSECTOR_ASSERT(at->addr_to_str);
-    DISSECTOR_ASSERT(at->addr_str_len);
-    DISSECTOR_ASSERT(((at->addr_name_res_str != NULL) && (at->addr_name_res_len != NULL)) ||
+    g_assert(at->name);
+    g_assert(at->pretty_name);
+    g_assert(at->addr_to_str);
+    g_assert(at->addr_str_len);
+    g_assert(((at->addr_name_res_str != NULL) && (at->addr_name_res_len != NULL)) ||
                      ((at->addr_name_res_str == NULL) && (at->addr_name_res_len == NULL)));
 
     type_list[addr_type] = at;
@@ -86,22 +73,22 @@ static void address_type_register(int addr_type, address_type_t *at)
 
 int address_type_dissector_register(const char* name, const char* pretty_name,
                                     AddrValueToString to_str_func, AddrValueToStringLen str_len_func,
-                                    AddrColFilterString col_filter_str_func, AddrFixedLen fixed_len_func,
+                                    AddrValueToByte to_bytes_func, AddrColFilterString col_filter_str_func, AddrFixedLen fixed_len_func,
                                     AddrNameResolutionToString name_res_str_func, AddrNameResolutionLen name_res_len_func)
 {
     int addr_type;
 
     /* Ensure valid data/functions for required fields */
-    DISSECTOR_ASSERT(name);
-    DISSECTOR_ASSERT(pretty_name);
-    DISSECTOR_ASSERT(to_str_func);
-    DISSECTOR_ASSERT(str_len_func);
+    g_assert(name);
+    g_assert(pretty_name);
+    g_assert(to_str_func);
+    g_assert(str_len_func);
     /* Either have both or neither */
-    DISSECTOR_ASSERT(((name_res_str_func != NULL) && (name_res_len_func != NULL)) ||
+    g_assert(((name_res_str_func != NULL) && (name_res_len_func != NULL)) ||
                      ((name_res_str_func == NULL) && (name_res_len_func == NULL)));
 
     /* This shouldn't happen, so flag it for fixing */
-    DISSECTOR_ASSERT(num_dissector_addr_type < MAX_DISSECTOR_ADDR_TYPE);
+    g_assert(num_dissector_addr_type < MAX_DISSECTOR_ADDR_TYPE);
 
     addr_type = AT_END_OF_LIST+num_dissector_addr_type;
     dissector_type_addresses[num_dissector_addr_type].addr_type = addr_type;
@@ -109,6 +96,7 @@ int address_type_dissector_register(const char* name, const char* pretty_name,
     dissector_type_addresses[num_dissector_addr_type].pretty_name = pretty_name;
     dissector_type_addresses[num_dissector_addr_type].addr_to_str = to_str_func;
     dissector_type_addresses[num_dissector_addr_type].addr_str_len = str_len_func;
+    dissector_type_addresses[num_dissector_addr_type].addr_to_byte = to_bytes_func;
     dissector_type_addresses[num_dissector_addr_type].addr_col_filter = col_filter_str_func;
     dissector_type_addresses[num_dissector_addr_type].addr_fixed_len = fixed_len_func;
     dissector_type_addresses[num_dissector_addr_type].addr_name_res_str = name_res_str_func;
@@ -121,6 +109,21 @@ int address_type_dissector_register(const char* name, const char* pretty_name,
     return addr_type;
 }
 
+int address_type_get_by_name(const char* name)
+{
+    address_type_t** addr;
+
+    for (addr = type_list; *addr != NULL; addr++)
+    {
+        if (!strcmp((*addr)->name, name))
+        {
+            return (*addr)->addr_type;
+        }
+    }
+
+    return -1;
+}
+
 /******************************************************************************
  * AT_NONE
  ******************************************************************************/
@@ -199,7 +202,7 @@ static int ipv4_to_str(const address* addr, gchar *buf, int buf_len)
 
 static int ipv4_str_len(const address* addr _U_)
 {
-    return MAX_IP_STR_LEN;
+    return WS_INET_ADDRSTRLEN;
 }
 
 static const char* ipv4_col_filter_str(const address* addr _U_, gboolean is_src)
@@ -232,13 +235,12 @@ static int ipv4_name_res_len(void)
  ******************************************************************************/
 static int ipv6_to_str(const address* addr, gchar *buf, int buf_len)
 {
-    ip6_to_str_buf((const struct e_in6_addr *)addr->data, buf, buf_len);
-    return (int)(strlen(buf)+1);
+    return ip6_to_str_buf((const ws_in6_addr *)addr->data, buf, buf_len) + 1;
 }
 
 static int ipv6_str_len(const address* addr _U_)
 {
-    return MAX_IP6_STR_LEN;
+    return WS_INET6_ADDRSTRLEN;
 }
 
 static const char* ipv6_col_filter_str(const address* addr _U_, gboolean is_src)
@@ -256,7 +258,7 @@ static int ipv6_len(void)
 
 static const gchar* ipv6_name_res_str(const address* addr)
 {
-    struct e_in6_addr ip6_addr;
+    ws_in6_addr ip6_addr;
     memcpy(&ip6_addr.bytes, addr->data, sizeof ip6_addr.bytes);
     return get_hostname6(&ip6_addr);
 }
@@ -291,34 +293,6 @@ static int ipx_len(void)
     return 10;
 }
 
-/******************************************************************************
- * AT_VINES
- * XXX - This functionality should really be in packet-vines.c as a dissector
- * address type, but need to resolve "address type" as "field type"
- ******************************************************************************/
-static int vines_to_str(const address* addr, gchar *buf, int buf_len _U_)
-{
-    const guint8 *addr_data = (const guint8 *)addr->data;
-    gchar *bufp = buf;
-
-    bufp = dword_to_hex(bufp, pntoh32(&addr_data[0])); /* 8 bytes */
-    *bufp++ = '.'; /* 1 byte */
-    bufp = word_to_hex(bufp, pntoh16(&addr_data[4])); /* 4 bytes */
-    *bufp++ = '\0'; /* NULL terminate */
-
-    return (int)(bufp - buf);
-}
-
-static int vines_str_len(const address* addr _U_)
-{
-    return 14;
-}
-
-static int vines_len(void)
-{
-    return VINES_ADDR_LEN;
-}
-
 /******************************************************************************
  * AT_FC
  ******************************************************************************/
@@ -411,22 +385,6 @@ static int fcwwn_name_res_len(void)
     return MAX_ADDR_STR_LEN; /* XXX - This can be lower */
 }
 
-/******************************************************************************
- * AT_SS7PC
- * XXX - This should really be a dissector address type as its address string
- * is partially determined by a dissector preference.
- ******************************************************************************/
-static int ss7pc_to_str(const address* addr, gchar *buf, int buf_len)
-{
-    mtp3_addr_to_str_buf((const mtp3_addr_pc_t *)addr->data, buf, buf_len);
-    return (int)(strlen(buf)+1);
-}
-
-static int ss7pc_str_len(const address* addr _U_)
-{
-    return 50;
-}
-
 /******************************************************************************
  * AT_STRINGZ
  ******************************************************************************/
@@ -448,7 +406,7 @@ static int eui64_addr_to_str(const address* addr, gchar *buf, int buf_len _U_)
 {
     buf = bytes_to_hexstr_punct(buf, (const guint8 *)addr->data, 8, ':');
     *buf = '\0'; /* NULL terminate */
-    return sizeof(buf) + 1;
+    return EUI64_STR_LEN;
 }
 
 static int eui64_str_len(const address* addr _U_)
@@ -465,51 +423,21 @@ static int eui64_len(void)
  * AT_IB
  ******************************************************************************/
 static int
-ib_addr_to_str( const address *addr, gchar *buf, int buf_len){
+ib_addr_to_str(const address *addr, gchar *buf, int buf_len)
+{
     if (addr->len >= 16) { /* GID is 128bits */
-        #define PREAMBLE_STR_LEN ((int)(sizeof("GID: ") - 1))
-        g_strlcpy(buf, "GID: ", buf_len);
-        if (buf_len < PREAMBLE_STR_LEN ||
-                ws_inet_ntop6(addr->data, buf + PREAMBLE_STR_LEN,
-                          buf_len - PREAMBLE_STR_LEN) == NULL ) /* Returns NULL if no space and does not touch buf */
-            g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len); /* Let the unexpected value alert user */
-    } else {    /* this is a LID (16 bits) */
-        guint16 lid_number;
-
-        memcpy((void *)&lid_number, addr->data, sizeof lid_number);
-        g_snprintf(buf,buf_len,"LID: %u",lid_number);
+        return ip6_to_str_buf_with_pfx((const ws_in6_addr *)addr->data, buf, buf_len, "GID: ");
     }
 
-    return sizeof(buf) + 1;
-}
-
-static int ib_str_len(const address* addr _U_)
-{
-    return MAX_ADDR_STR_LEN; /* XXX - This is overkill */
-}
-
-/******************************************************************************
- * AT_USB
- * XXX - This functionality should really be in packet-usb.c as a dissector
- * address type, but currently need support of AT_USB in conversation_table.c
- ******************************************************************************/
-static int usb_addr_to_str(const address* addr, gchar *buf, int buf_len _U_)
-{
-    const guint8 *addrp = (const guint8 *)addr->data;
-
-    if(pletoh32(&addrp[0])==0xffffffff){
-        g_strlcpy(buf, "host", buf_len);
-    } else {
-        g_snprintf(buf, buf_len, "%d.%d.%d", pletoh16(&addrp[8]),
-                        pletoh32(&addrp[0]), pletoh32(&addrp[4]));
-    }
+    /* this is a LID (16 bits) */
+    g_snprintf(buf,buf_len,"LID: %u", *(const guint16 *)addr->data);
 
     return (int)(strlen(buf)+1);
 }
 
-static int usb_addr_str_len(const address* addr _U_)
+static int ib_str_len(const address* addr _U_)
 {
-    return 50;
+    return MAX_ADDR_STR_LEN; /* XXX - This is overkill */
 }
 
 /******************************************************************************
@@ -551,6 +479,33 @@ static int ax25_len(void)
     return AX25_ADDR_LEN;
 }
 
+/******************************************************************************
+ * AT_VINES
+ ******************************************************************************/
+
+static int vines_addr_to_str(const address* addr, gchar *buf, int buf_len _U_)
+{
+       const guint8 *addr_data = (const guint8 *)addr->data;
+       gchar *bufp = buf;
+
+       bufp = dword_to_hex(bufp, pntoh32(&addr_data[0])); /* 8 bytes */
+       *bufp++ = '.'; /* 1 byte */
+       bufp = word_to_hex(bufp, pntoh16(&addr_data[4])); /* 4 bytes */
+       *bufp++ = '\0'; /* NULL terminate */
+
+       return (int)(bufp - buf);
+}
+
+static int vines_addr_str_len(const address* addr _U_)
+{
+       return 14;
+}
+
+static int vines_len(void)
+{
+       return VINES_ADDR_LEN;
+}
+
 /******************************************************************************
  * END OF PROVIDED ADDRESS TYPES
  ******************************************************************************/
@@ -566,6 +521,7 @@ void address_types_initialize(void)
         "No address",       /* pretty_name */
         none_addr_to_str,   /* addr_to_str */
         none_addr_str_len,  /* addr_str_len */
+        NULL,               /* addr_to_byte */
         NULL,               /* addr_col_filter */
         none_addr_len,      /* addr_fixed_len */
         none_name_res_str, /* addr_name_res_str */
@@ -578,6 +534,7 @@ void address_types_initialize(void)
         "Ethernet address", /* pretty_name */
         ether_to_str,       /* addr_to_str */
         ether_str_len,      /* addr_str_len */
+        NULL,               /* addr_to_byte */
         ether_col_filter_str, /* addr_col_filter */
         ether_len,          /* addr_fixed_len */
         ether_name_resolution_str, /* addr_name_res_str */
@@ -590,6 +547,7 @@ void address_types_initialize(void)
         "IPv4 address",     /* pretty_name */
         ipv4_to_str,        /* addr_to_str */
         ipv4_str_len,       /* addr_str_len */
+        NULL,               /* addr_to_byte */
         ipv4_col_filter_str, /* addr_col_filter */
         ipv4_len,           /* addr_fixed_len */
         ipv4_name_res_str, /* addr_name_res_str */
@@ -602,6 +560,7 @@ void address_types_initialize(void)
         "IPv6 address",     /* pretty_name */
         ipv6_to_str,        /* addr_to_str */
         ipv6_str_len,       /* addr_str_len */
+        NULL,               /* addr_to_byte */
         ipv6_col_filter_str, /* addr_col_filter */
         ipv6_len,            /* addr_fixed_len */
         ipv6_name_res_str, /* addr_name_res_str */
@@ -614,30 +573,20 @@ void address_types_initialize(void)
         "IPX address",      /* pretty_name */
         ipx_to_str,         /* addr_to_str */
         ipx_str_len,        /* addr_str_len */
+        NULL,               /* addr_to_byte */
         NULL,               /* addr_col_filter */
         ipx_len,            /* addr_fixed_len */
         NULL,               /* addr_name_res_str */
         NULL,               /* addr_name_res_len */
     };
 
-    static address_type_t vines_address = {
-        AT_VINES,           /* addr_type */
-        "AT_VINES",         /* name */
-        "Banyan Vines address", /* pretty_name */
-        vines_to_str,       /* addr_to_str */
-        vines_str_len,      /* addr_str_len */
-        NULL,               /* addr_col_filter */
-        vines_len,          /* addr_fixed_len */
-        NULL,               /* addr_name_res_str */
-        NULL,               /* addr_name_res_len */
-    };
-
     static address_type_t fc_address = {
         AT_FC,          /* addr_type */
         "AT_FC",        /* name */
         "FC address",   /* pretty_name */
         fc_to_str,      /* addr_to_str */
         fc_str_len,     /* addr_str_len */
+        NULL,           /* addr_to_byte */
         NULL,           /* addr_col_filter */
         fc_len,         /* addr_fixed_len */
         NULL,           /* addr_name_res_str */
@@ -650,30 +599,20 @@ void address_types_initialize(void)
         "Fibre Channel WWN",    /* pretty_name */
         fcwwn_to_str,   /* addr_to_str */
         fcwwn_str_len,  /* addr_str_len */
+        NULL,           /* addr_to_byte */
         NULL,           /* addr_col_filter */
         fcwwn_len,         /* addr_fixed_len */
         fcwwn_name_res_str, /* addr_name_res_str */
         fcwwn_name_res_len, /* addr_name_res_len */
     };
 
-    static address_type_t ss7pc_address = {
-        AT_SS7PC,          /* addr_type */
-        "AT_SS7PC",        /* name */
-        "SS7 Point Code",  /* pretty_name */
-        ss7pc_to_str,      /* addr_to_str */
-        ss7pc_str_len,     /* addr_str_len */
-        NULL,              /* addr_col_filter */
-        NULL,              /* addr_fixed_len */
-        NULL,              /* addr_name_res_str */
-        NULL,              /* addr_name_res_len */
-    };
-
     static address_type_t stringz_address = {
         AT_STRINGZ,          /* addr_type */
         "AT_STRINGZ",        /* name */
         "String address",   /* pretty_name */
         stringz_addr_to_str, /* addr_to_str */
         stringz_addr_str_len, /* addr_str_len */
+        NULL,              /* addr_to_byte */
         NULL,              /* addr_col_filter */
         NULL,              /* addr_fixed_len */
         NULL,              /* addr_name_res_str */
@@ -686,6 +625,7 @@ void address_types_initialize(void)
         "IEEE EUI-64",     /* pretty_name */
         eui64_addr_to_str, /* addr_to_str */
         eui64_str_len,     /* addr_str_len */
+        NULL,              /* addr_to_byte */
         NULL,              /* addr_col_filter */
         eui64_len,         /* addr_fixed_len */
         NULL,              /* addr_name_res_str */
@@ -698,18 +638,7 @@ void address_types_initialize(void)
         "Infiniband GID/LID",   /* pretty_name */
         ib_addr_to_str,  /* addr_to_str */
         ib_str_len,      /* addr_str_len */
-        NULL,              /* addr_col_filter */
-        NULL,              /* addr_fixed_len */
-        NULL,              /* addr_name_res_str */
-        NULL,              /* addr_name_res_len */
-    };
-
-    static address_type_t usb_address = {
-        AT_USB,          /* addr_type */
-        "AT_USB",        /* name */
-        "USB Address",   /* pretty_name */
-        usb_addr_to_str, /* addr_to_str */
-        usb_addr_str_len, /* addr_str_len */
+        NULL,              /* addr_to_byte */
         NULL,              /* addr_col_filter */
         NULL,              /* addr_fixed_len */
         NULL,              /* addr_name_res_str */
@@ -722,32 +651,43 @@ void address_types_initialize(void)
         "AX.25 Address",  /* pretty_name */
         ax25_addr_to_str, /* addr_to_str */
         ax25_addr_str_len,/* addr_str_len */
+        NULL,             /* addr_to_byte */
         ax25_col_filter_str, /* addr_col_filter */
         ax25_len,          /* addr_fixed_len */
         NULL,              /* addr_name_res_str */
         NULL,              /* addr_name_res_len */
     };
+    static address_type_t vines_address = {
+        AT_VINES,          /* addr_type */
+        "AT_VINES",        /* name */
+        "Banyan Vines Address",  /* pretty_name */
+        vines_addr_to_str, /* addr_to_str */
+        vines_addr_str_len,/* addr_str_len */
+        NULL,             /* addr_to_byte */
+        NULL,              /* addr_col_filter */
+        vines_len,         /* addr_fixed_len */
+        NULL,              /* addr_name_res_str */
+        NULL,              /* addr_name_res_len */
+    };
 
     num_dissector_addr_type = 0;
 
     /* Initialize the type array.  This is mostly for handling
        "dissector registered" address type range (for NULL checking) */
-    memset(type_list, 0, MAX_ADDR_TYPE_VALUE*sizeof(address_type_t*));
+    memset(type_list, 0, (MAX_ADDR_TYPE_VALUE + 1)*sizeof(address_type_t*));
 
     address_type_register(AT_NONE, &none_address );
     address_type_register(AT_ETHER, &ether_address );
     address_type_register(AT_IPv4, &ipv4_address );
     address_type_register(AT_IPv6, &ipv6_address );
     address_type_register(AT_IPX, &ipx_address );
-    address_type_register(AT_VINES, &vines_address );
     address_type_register(AT_FC, &fc_address );
     address_type_register(AT_FCWWN, &fcwwn_address );
-    address_type_register(AT_SS7PC, &ss7pc_address );
     address_type_register(AT_STRINGZ, &stringz_address );
     address_type_register(AT_EUI64, &eui64_address );
     address_type_register(AT_IB, &ib_address );
-    address_type_register(AT_USB, &usb_address );
     address_type_register(AT_AX25, &ax25_address );
+    address_type_register(AT_VINES, &vines_address );
 }
 
 /* Given an address type id, return an address_type_t* */
@@ -800,6 +740,34 @@ void address_to_str_buf(const address* addr, gchar *buf, int buf_len)
     at->addr_to_str(addr, buf, buf_len);
 }
 
+
+guint address_to_bytes(const address *addr, guint8 *buf, guint buf_len)
+{
+    address_type_t *at;
+    guint copy_len = 0;
+
+    if (!buf || !buf_len)
+        return 0;
+
+    ADDR_TYPE_LOOKUP(addr->type, at);
+
+    if (at == NULL)
+        return 0;
+
+    if (at->addr_to_byte == NULL)
+    {
+        /* If a specific function isn't provided, just do a memcpy */
+        copy_len = MIN(((guint)addr->len), buf_len);
+        memcpy(buf, addr->data, copy_len);
+    }
+    else
+    {
+        copy_len = at->addr_to_byte(addr, buf, buf_len);
+    }
+
+    return copy_len;
+}
+
 const gchar *
 address_to_name(const address *addr)
 {