]>
git.proxmox.com Git - mirror_frr.git/blob - lib/ipaddr.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IP address structure (for generic IPv4 or IPv6 address)
4 * Copyright (C) 2016, 2017 Cumulus Networks, Inc.
19 * Generic IP address - union of IPv4 and IPv6 address.
22 IPADDR_NONE
= AF_UNSPEC
,
28 enum ipaddr_type_t ipa_type
;
31 struct in_addr _v4_addr
;
32 struct in6_addr _v6_addr
;
34 #define ipaddr_v4 ip._v4_addr
35 #define ipaddr_v6 ip._v6_addr
38 #define IS_IPADDR_NONE(p) ((p)->ipa_type == IPADDR_NONE)
39 #define IS_IPADDR_V4(p) ((p)->ipa_type == IPADDR_V4)
40 #define IS_IPADDR_V6(p) ((p)->ipa_type == IPADDR_V6)
42 #define SET_IPADDR_V4(p) (p)->ipa_type = IPADDR_V4
43 #define SET_IPADDR_V6(p) (p)->ipa_type = IPADDR_V6
46 (IS_IPADDR_V4((p)) ? sizeof(struct in_addr) : sizeof(struct in6_addr))
48 #define IPADDR_STRING_SIZE 46
50 static inline int ipaddr_family(const struct ipaddr
*ip
)
52 switch (ip
->ipa_type
) {
61 assert(!"Reached end of function where we should never hit");
64 static inline int str2ipaddr(const char *str
, struct ipaddr
*ip
)
68 memset(ip
, 0, sizeof(struct ipaddr
));
70 ret
= inet_pton(AF_INET
, str
, &ip
->ipaddr_v4
);
71 if (ret
> 0) /* Valid IPv4 address. */
73 ip
->ipa_type
= IPADDR_V4
;
76 ret
= inet_pton(AF_INET6
, str
, &ip
->ipaddr_v6
);
77 if (ret
> 0) /* Valid IPv6 address. */
79 ip
->ipa_type
= IPADDR_V6
;
86 static inline char *ipaddr2str(const struct ipaddr
*ip
, char *buf
, int size
)
90 inet_ntop(ip
->ipa_type
, &ip
->ip
.addr
, buf
, size
);
94 #define IS_MAPPED_IPV6(A) \
95 ((A)->s6_addr32[0] == 0x00000000 \
96 ? ((A)->s6_addr32[1] == 0x00000000 \
97 ? (ntohl((A)->s6_addr32[2]) == 0xFFFF ? 1 : 0) \
102 * Convert IPv4 address to IPv4-mapped IPv6 address which is of the
103 * form ::FFFF:<IPv4 address> (RFC 4291). This IPv6 address can then
104 * be used to represent the IPv4 address, wherever only an IPv6 address
107 static inline void ipv4_to_ipv4_mapped_ipv6(struct in6_addr
*in6
,
110 uint32_t addr_type
= htonl(0xFFFF);
112 memset(in6
, 0, sizeof(struct in6_addr
));
113 memcpy((char *)in6
+ 8, &addr_type
, sizeof(addr_type
));
114 memcpy((char *)in6
+ 12, &in
, sizeof(struct in_addr
));
118 * convert an ipv4 mapped ipv6 address back to ipv4 address
120 static inline void ipv4_mapped_ipv6_to_ipv4(const struct in6_addr
*in6
,
123 memset(in
, 0, sizeof(struct in_addr
));
124 memcpy(in
, (char *)in6
+ 12, sizeof(struct in_addr
));
128 * generic ordering comparison between IP addresses
130 static inline int ipaddr_cmp(const struct ipaddr
*a
, const struct ipaddr
*b
)
136 return (va
< vb
) ? -1 : 1;
137 switch (a
->ipa_type
) {
139 va
= ntohl(a
->ipaddr_v4
.s_addr
);
140 vb
= ntohl(b
->ipaddr_v4
.s_addr
);
142 return (va
< vb
) ? -1 : 1;
145 return memcmp((void *)&a
->ipaddr_v6
, (void *)&b
->ipaddr_v6
,
146 sizeof(a
->ipaddr_v6
));
151 assert(!"Reached end of function we should never hit");
154 static inline bool ipaddr_is_zero(const struct ipaddr
*ip
)
156 switch (ip
->ipa_type
) {
160 return ip
->ipaddr_v4
.s_addr
== INADDR_ANY
;
162 return IN6_IS_ADDR_UNSPECIFIED(&ip
->ipaddr_v6
);
167 #ifdef _FRR_ATTRIBUTE_PRINTFRR
168 #pragma FRR printfrr_ext "%pIA" (struct ipaddr *)
175 #endif /* __IPADDR_H__ */