Wireshark  4.3.0
The Wireshark network protocol analyzer
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 
19 typedef uint32_t ws_in4_addr; /* 32 bit IPv4 address, in network byte order */
20 
21 typedef struct e_in6_addr {
22  uint8_t bytes[16]; /* 128 bit IPv6 address */
23 } ws_in6_addr;
24 
25 
31 #define in4_addr_is_local_network_control_block(addr) \
32  ((addr & 0xffffff00) == 0xe0000000)
33 
38 #define in4_addr_is_multicast(addr) \
39  ((addr & 0xf0000000) == 0xe0000000)
40 
47 #define in4_addr_is_private(addr) \
48  (((addr & 0xff000000) == 0x0a000000) || \
49  ((addr & 0xfff00000) == 0xac100000) || \
50  ((addr & 0xffff0000) == 0xc0a80000))
51 
56 #define in4_addr_is_link_local(addr) \
57  ((addr & 0xffff0000) == 0xa9fe0000)
58 
63 static inline bool
64 in6_addr_is_linklocal(const ws_in6_addr *a)
65 {
66  return (a->bytes[0] == 0xfe) && ((a->bytes[1] & 0xc0) == 0x80);
67 }
68 
69 static inline bool
70 in6_addr_is_sitelocal(const ws_in6_addr *a)
71 {
72  return (a->bytes[0] == 0xfe) && ((a->bytes[1] & 0xc0) == 0xc0);
73 }
74 
75 static inline bool in6_addr_is_uniquelocal(const ws_in6_addr *a)
76 {
77  return (a->bytes[0] & 0xfe) == 0xfc;
78 }
79 
83 static inline bool
84 in6_addr_is_multicast(const ws_in6_addr *a)
85 {
86  return a->bytes[0] == 0xff;
87 }
88 
89 /*
90  * These are the values specified by RFC 2133 and its successors for
91  * INET_ADDRSTRLEN and INET6_ADDRSTRLEN.
92  *
93  * On UN*X systems, INET_ADDRSTRLEN and INET6_ADDRSTRLEN are defined
94  * to the values from RFC 2133 and its successors.
95  *
96  * However, on Windows:
97  *
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.
106  *
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().
113  *
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.
118  */
119 #define WS_INET_ADDRSTRLEN 16
120 #define WS_INET6_ADDRSTRLEN 46
121 
122 /*
123  * To check for errors set errno to zero before calling ws_inet_ntop{4,6}.
124  * ENOSPC is set if the result exceeds the given buffer size.
125  */
126 WS_DLL_PUBLIC WS_RETNONNULL
127 const char *
128 ws_inet_ntop4(const void *src, char *dst, size_t dst_size);
129 
130 WS_DLL_PUBLIC WS_RETNONNULL
131 const char *
132 ws_inet_ntop6(const void *src, char *dst, size_t dst_size);
133 
134 WS_DLL_PUBLIC
135 bool
136 ws_inet_pton4(const char *src, ws_in4_addr *dst);
137 
138 WS_DLL_PUBLIC
139 bool
140 ws_inet_pton6(const char *src, ws_in6_addr *dst);
141 
142 #ifdef __cplusplus
143 }
144 #endif /* __cplusplus */
145 
146 #endif
Definition: inet_addr.h:21