]>
git.proxmox.com Git - mirror_frr.git/blob - lib/ipaddr.h
2 * IP address structure (for generic IPv4 or IPv6 address)
3 * Copyright (C) 2016, 2017 Cumulus Networks, Inc.
5 * This file is part of FRR.
7 * FRR is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * FRR is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with FRR; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
35 * Generic IP address - union of IPv4 and IPv6 address.
38 IPADDR_NONE
= AF_UNSPEC
,
44 enum ipaddr_type_t ipa_type
;
47 struct in_addr _v4_addr
;
48 struct in6_addr _v6_addr
;
50 #define ipaddr_v4 ip._v4_addr
51 #define ipaddr_v6 ip._v6_addr
54 #define IS_IPADDR_NONE(p) ((p)->ipa_type == IPADDR_NONE)
55 #define IS_IPADDR_V4(p) ((p)->ipa_type == IPADDR_V4)
56 #define IS_IPADDR_V6(p) ((p)->ipa_type == IPADDR_V6)
58 #define SET_IPADDR_V4(p) (p)->ipa_type = IPADDR_V4
59 #define SET_IPADDR_V6(p) (p)->ipa_type = IPADDR_V6
62 (IS_IPADDR_V4((p)) ? sizeof(struct in_addr) : sizeof(struct in6_addr))
64 static inline int ipaddr_family(const struct ipaddr
*ip
)
66 switch (ip
->ipa_type
) {
76 static inline int str2ipaddr(const char *str
, struct ipaddr
*ip
)
80 memset(ip
, 0, sizeof(struct ipaddr
));
82 ret
= inet_pton(AF_INET
, str
, &ip
->ipaddr_v4
);
83 if (ret
> 0) /* Valid IPv4 address. */
85 ip
->ipa_type
= IPADDR_V4
;
88 ret
= inet_pton(AF_INET6
, str
, &ip
->ipaddr_v6
);
89 if (ret
> 0) /* Valid IPv6 address. */
91 ip
->ipa_type
= IPADDR_V6
;
98 static inline char *ipaddr2str(const struct ipaddr
*ip
, char *buf
, int size
)
102 inet_ntop(ip
->ipa_type
, &ip
->ip
.addr
, buf
, size
);
106 #define IS_MAPPED_IPV6(A) \
107 ((A)->s6_addr32[0] == 0x00000000 \
108 ? ((A)->s6_addr32[1] == 0x00000000 \
109 ? (ntohl((A)->s6_addr32[2]) == 0xFFFF ? 1 : 0) \
114 * Convert IPv4 address to IPv4-mapped IPv6 address which is of the
115 * form ::FFFF:<IPv4 address> (RFC 4291). This IPv6 address can then
116 * be used to represent the IPv4 address, wherever only an IPv6 address
119 static inline void ipv4_to_ipv4_mapped_ipv6(struct in6_addr
*in6
,
122 uint32_t addr_type
= htonl(0xFFFF);
124 memset(in6
, 0, sizeof(struct in6_addr
));
125 memcpy((char *)in6
+ 8, &addr_type
, sizeof(addr_type
));
126 memcpy((char *)in6
+ 12, &in
, sizeof(struct in_addr
));
130 * convert an ipv4 mapped ipv6 address back to ipv4 address
132 static inline void ipv4_mapped_ipv6_to_ipv4(const struct in6_addr
*in6
,
135 memset(in
, 0, sizeof(struct in_addr
));
136 memcpy(in
, (char *)in6
+ 12, sizeof(struct in_addr
));
140 * generic ordering comparison between IP addresses
142 static inline int ipaddr_cmp(const struct ipaddr
*a
, const struct ipaddr
*b
)
148 return (va
< vb
) ? -1 : 1;
149 switch (a
->ipa_type
) {
151 va
= ntohl(a
->ipaddr_v4
.s_addr
);
152 vb
= ntohl(b
->ipaddr_v4
.s_addr
);
154 return (va
< vb
) ? -1 : 1;
157 return memcmp((void *)&a
->ipaddr_v6
, (void *)&b
->ipaddr_v6
,
158 sizeof(a
->ipaddr_v6
));
164 static inline bool ipaddr_is_zero(const struct ipaddr
*ip
)
166 switch (ip
->ipa_type
) {
170 return ip
->ipaddr_v4
.s_addr
== INADDR_ANY
;
172 return IN6_IS_ADDR_UNSPECIFIED(&ip
->ipaddr_v6
);
177 #ifdef _FRR_ATTRIBUTE_PRINTFRR
178 #pragma FRR printfrr_ext "%pIA" (struct ipaddr *)
185 #endif /* __IPADDR_H__ */