Wireshark 4.5.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
16extern "C" {
17#endif /* __cplusplus */
18
19typedef uint32_t ws_in4_addr; /* 32 bit IPv4 address, in network byte order */
20
21typedef struct e_in6_addr {
22 uint8_t bytes[16]; /* 128 bit IPv6 address */
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
63static inline bool
64in6_addr_is_linklocal(const ws_in6_addr *a)
65{
66 return (a->bytes[0] == 0xfe) && ((a->bytes[1] & 0xc0) == 0x80);
67}
68
69static inline bool
70in6_addr_is_sitelocal(const ws_in6_addr *a)
71{
72 return (a->bytes[0] == 0xfe) && ((a->bytes[1] & 0xc0) == 0xc0);
73}
74
75static inline bool in6_addr_is_uniquelocal(const ws_in6_addr *a)
76{
77 return (a->bytes[0] & 0xfe) == 0xfc;
78}
79
83static inline bool
84in6_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 * Utility for CIDR notation of subnets
124 */
125#define WS_INET_CIDRADDRSTRLEN 19
126
127/*
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.
130 */
131WS_DLL_PUBLIC WS_RETNONNULL
132const char *
133ws_inet_ntop4(const void *src, char *dst, size_t dst_size);
134
135WS_DLL_PUBLIC WS_RETNONNULL
136const char *
137ws_inet_ntop6(const void *src, char *dst, size_t dst_size);
138
139WS_DLL_PUBLIC
140bool
141ws_inet_pton4(const char *src, ws_in4_addr *dst);
142
143WS_DLL_PUBLIC
144bool
145ws_inet_pton6(const char *src, ws_in6_addr *dst);
146
147#ifdef __cplusplus
148}
149#endif /* __cplusplus */
150
151#endif
Definition inet_addr.h:21