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
10 #ifndef __WS_INET_ADDR_H__
11 #define __WS_INET_ADDR_H__
13 #include <wireshark.h>
17 #endif /* __cplusplus */
19 typedef uint32_t ws_in4_addr
; /* 32 bit IPv4 address, in network byte order */
21 typedef struct e_in6_addr
{
22 uint8_t bytes
[16]; /* 128 bit IPv6 address */
28 * Returns true if the address is in the 224.0.0.0/24 local network
31 #define in4_addr_is_local_network_control_block(addr) \
32 ((addr & 0xffffff00) == 0xe0000000)
36 * Returns true if the address is in the 224.0.0.0/4 network block
38 #define in4_addr_is_multicast(addr) \
39 ((addr & 0xf0000000) == 0xe0000000)
43 * Returns true if the address is in one of the three blocks reserved
44 * for private IPv4 addresses by section 3 of RFC 1918, namely:
45 * 10/8, 172.16/12, and 192.168/16
47 #define in4_addr_is_private(addr) \
48 (((addr & 0xff000000) == 0x0a000000) || \
49 ((addr & 0xfff00000) == 0xac100000) || \
50 ((addr & 0xffff0000) == 0xc0a80000))
54 * Returns true if the address is in the 169.254/16 network block
56 #define in4_addr_is_link_local(addr) \
57 ((addr & 0xffff0000) == 0xa9fe0000)
61 * Note that we must check topmost 10 bits only, not 16 bits (see RFC2373).
64 in6_addr_is_linklocal(const ws_in6_addr
*a
)
66 return (a
->bytes
[0] == 0xfe) && ((a
->bytes
[1] & 0xc0) == 0x80);
70 in6_addr_is_sitelocal(const ws_in6_addr
*a
)
72 return (a
->bytes
[0] == 0xfe) && ((a
->bytes
[1] & 0xc0) == 0xc0);
75 static inline bool in6_addr_is_uniquelocal(const ws_in6_addr
*a
)
77 return (a
->bytes
[0] & 0xfe) == 0xfc;
84 in6_addr_is_multicast(const ws_in6_addr
*a
)
86 return a
->bytes
[0] == 0xff;
90 * These are the values specified by RFC 2133 and its successors for
91 * INET_ADDRSTRLEN and INET6_ADDRSTRLEN.
93 * On UN*X systems, INET_ADDRSTRLEN and INET6_ADDRSTRLEN are defined
94 * to the values from RFC 2133 and its successors.
96 * However, on Windows:
98 * There are APIs RtlIpv4AddressToStringEx(), which converts an
99 * IPv4 address *and transport-layer port* to the address in the
100 * standard text form, followed by a colon and the port number,
101 * and RtlIpv6AddressToStringEx(), which converts an IPv6 address
102 * *and scope ID and transport-layer port* to the address in the
103 * standard text form, followed by a percent sign and the scope
104 * ID (with the address and scope ID in square brackets), followed
105 * by a colon and the port number.
107 * Instead of defining INET_ADDRSTRLEN_EX as 22 and INET6_ADDRSTRLEN_EX
108 * as 65, and saying *those* were the buffer sizes to use for
109 * RtlIpv4AddressToStringEx() and RtlIpv6AddressToStringEx(), they
110 * defined INET_ADDRSTRLEN to be 22 and INET6_ADDRSTRLEN to be 65 - and
111 * recommend using those as the size for the buffers passed to
112 * RtlIpv4AddressToStringEx() and RtlIpv6AddressToStringEx().
114 * At least they document inet_ntop() as requiring a 16-byte or larger
115 * buffer for IPv4 addresses and a 46-byte or larger buffer for
116 * IPv6 addresses. For this reason, use hard-coded numeric constants rather than
117 * INET_ADDRSTRLEN and INET6_ADDRSTRLEN.
119 #define WS_INET_ADDRSTRLEN 16
120 #define WS_INET6_ADDRSTRLEN 46
123 * Utility for CIDR notation of subnets
125 #define WS_INET_CIDRADDRSTRLEN 19
128 * To check for errors set errno to zero before calling ws_inet_ntop{4,6}.
129 * ENOSPC is set if the result exceeds the given buffer size.
131 WS_DLL_PUBLIC WS_RETNONNULL
133 ws_inet_ntop4(const void *src
, char *dst
, size_t dst_size
);
135 WS_DLL_PUBLIC WS_RETNONNULL
137 ws_inet_ntop6(const void *src
, char *dst
, size_t dst_size
);
141 ws_inet_pton4(const char *src
, ws_in4_addr
*dst
);
145 ws_inet_pton6(const char *src
, ws_in6_addr
*dst
);
149 #endif /* __cplusplus */