| Wireshark 4.7.0 The Wireshark network protocol analyzer

Loading...
Searching...
No Matches
inet_addr.h
Go to the documentation of this file.
1
10#ifndef __WS_INET_ADDR_H__
11#define __WS_INET_ADDR_H__
12
13#include <wireshark.h>
14
15#ifdef __cplusplus
16 extern "C" {
17#endif /* __cplusplus */
18
22 typedef uint32_t ws_in4_addr;
23
27 typedef struct e_in6_addr {
28 uint8_t bytes[16];
29} ws_in6_addr;
30
31
37#define in4_addr_is_local_network_control_block(addr) \
38 ((addr & 0xffffff00) == 0xe0000000)
39
44#define in4_addr_is_multicast(addr) \
45 ((addr & 0xf0000000) == 0xe0000000)
46
53#define in4_addr_is_private(addr) \
54 (((addr & 0xff000000) == 0x0a000000) || \
55 ((addr & 0xfff00000) == 0xac100000) || \
56 ((addr & 0xffff0000) == 0xc0a80000))
57
62#define in4_addr_is_link_local(addr) \
63 ((addr & 0xffff0000) == 0xa9fe0000)
64
69 static inline bool
70in6_addr_is_linklocal(const ws_in6_addr *a)
71{
72 return (a->bytes[0] == 0xfe) && ((a->bytes[1] & 0xc0) == 0x80);
73}
74
83 static inline bool
84in6_addr_is_sitelocal(const ws_in6_addr *a)
85{
86 return (a->bytes[0] == 0xfe) && ((a->bytes[1] & 0xc0) == 0xc0);
87}
88
95 static inline bool in6_addr_is_uniquelocal(const ws_in6_addr *a)
96{
97 return (a->bytes[0] & 0xfe) == 0xfc;
98}
99
103 static inline bool
104in6_addr_is_multicast(const ws_in6_addr *a)
105{
106 return a->bytes[0] == 0xff;
107}
108
109/*
110 * These are the values specified by RFC 2133 and its successors for
111 * INET_ADDRSTRLEN and INET6_ADDRSTRLEN.
112 *
113 * On UN*X systems, INET_ADDRSTRLEN and INET6_ADDRSTRLEN are defined
114 * to the values from RFC 2133 and its successors.
115 *
116 * However, on Windows:
117 *
118 * There are APIs RtlIpv4AddressToStringEx(), which converts an
119 * IPv4 address *and transport-layer port* to the address in the
120 * standard text form, followed by a colon and the port number,
121 * and RtlIpv6AddressToStringEx(), which converts an IPv6 address
122 * *and scope ID and transport-layer port* to the address in the
123 * standard text form, followed by a percent sign and the scope
124 * ID (with the address and scope ID in square brackets), followed
125 * by a colon and the port number.
126 *
127 * Instead of defining INET_ADDRSTRLEN_EX as 22 and INET6_ADDRSTRLEN_EX
128 * as 65, and saying *those* were the buffer sizes to use for
129 * RtlIpv4AddressToStringEx() and RtlIpv6AddressToStringEx(), they
130 * defined INET_ADDRSTRLEN to be 22 and INET6_ADDRSTRLEN to be 65 - and
131 * recommend using those as the size for the buffers passed to
132 * RtlIpv4AddressToStringEx() and RtlIpv6AddressToStringEx().
133 *
134 * At least they document inet_ntop() as requiring a 16-byte or larger
135 * buffer for IPv4 addresses and a 46-byte or larger buffer for
136 * IPv6 addresses. For this reason, use hard-coded numeric constants rather than
137 * INET_ADDRSTRLEN and INET6_ADDRSTRLEN.
138 */
139#define WS_INET_ADDRSTRLEN 16
140#define WS_INET6_ADDRSTRLEN 46
141
152/*
153 * Utility for CIDR notation of subnets
154 */
155#define WS_INET_CIDRADDRSTRLEN 19
156#define WS_INET6_CIDRADDRSTRLEN 50 /* max "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/128\0" */
157
158/*
159 * To check for errors set errno to zero before calling ws_inet_ntop{4,6}.
160 * ENOSPC is set if the result exceeds the given buffer size.
161 */
162
171WS_DLL_PUBLIC WS_RETNONNULL
172 const char *
173 ws_inet_ntop4(const void *src, char *dst, size_t dst_size);
174
175WS_DLL_PUBLIC WS_RETNONNULL
176 const char *
177ws_inet_ntop6(const void *src, char *dst, size_t dst_size);
178
190WS_DLL_PUBLIC
191 bool
192 ws_inet_pton4(const char *src, ws_in4_addr *dst);
193
205WS_DLL_PUBLIC
206 bool
207 ws_inet_pton6(const char *src, ws_in6_addr *dst);
208
209#ifdef __cplusplus
210}
211#endif /* __cplusplus */
212
213#endif
ws_inet_pton6
WS_DLL_PUBLIC bool ws_inet_pton6(const char *src, ws_in6_addr *dst)
Parses a string-form IPv6 address into binary format.
Definition inet_addr.c:99
ws_in6_addr
struct e_in6_addr ws_in6_addr
Represents a 128-bit IPv6 address.
ws_in4_addr
uint32_t ws_in4_addr
Represents a 32-bit IPv4 address in network byte order.
Definition inet_addr.h:22
ws_inet_pton4
WS_DLL_PUBLIC bool ws_inet_pton4(const char *src, ws_in4_addr *dst)
Parses a string-form IPv4 address into binary format.
Definition inet_addr.c:87
ws_inet_ntop4
WS_DLL_PUBLIC WS_RETNONNULL const char * ws_inet_ntop4(const void *src, char *dst, size_t dst_size)
Converts a binary IPv4 address to its string representation.
Definition inet_addr.c:81
e_in6_addr
Represents a 128-bit IPv6 address.
Definition inet_addr.h:27
e_in6_addr::bytes
uint8_t bytes[16]
Definition inet_addr.h:28