sq epan/dissectors/pidl/rcg/rcg.cnf
[wireshark-sm.git] / epan / iana-ip.c
blob891a24f69ec804fc2ac245a595253e6111a4ffda
1 /* iana-ip.c
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
10 #include "iana-ip.h"
12 #include "iana-ip-data.c"
14 static int
15 compare_ipv4_block(const void *key, const void *element)
17 const uint32_t *ipnum = key;
18 const struct ws_iana_ip_special_block *ptr = element;
20 uint32_t val = *ipnum & ptr->u_ip.ipv4.nmask;
22 if (val < ptr->u_ip.ipv4.addr)
23 return -1;
24 if (val > ptr->u_ip.ipv4.addr)
25 return 1;
26 return 0;
29 const struct ws_iana_ip_special_block *
30 ws_iana_ipv4_special_block_lookup(uint32_t ipnum)
32 return bsearch(&ipnum, __ipv4_special_block, G_N_ELEMENTS(__ipv4_special_block),
33 sizeof(struct ws_iana_ip_special_block), compare_ipv4_block);
36 static const uint8_t bitmasks[9] =
37 { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
39 static int
40 compare_ipv6_block(const void *key, const void *element)
42 const ws_in6_addr *addr = key;
43 const struct ws_iana_ip_special_block *ptr = element;
44 uint32_t prefix;
45 int pos = 0;
46 int byte_a, byte_b;
48 prefix = ptr->u_ip.ipv6.prefix;
50 while (prefix >= 8) {
51 byte_a = addr->bytes[pos];
52 byte_b = ptr->u_ip.ipv6.addr.bytes[pos];
53 if (byte_a != byte_b)
54 return byte_a - byte_b;
56 prefix -= 8;
57 pos++;
60 if (prefix != 0) {
61 byte_a = addr->bytes[pos] & bitmasks[prefix];
62 byte_b = ptr->u_ip.ipv6.addr.bytes[pos];
63 if (byte_a != byte_b)
64 return byte_a - byte_b;
67 return 0;
70 const struct ws_iana_ip_special_block *
71 ws_iana_ipv6_special_block_lookup(const ws_in6_addr *addr)
73 return bsearch(addr, __ipv6_special_block, G_N_ELEMENTS(__ipv6_special_block),
74 sizeof(struct ws_iana_ip_special_block), compare_ipv6_block);