]> git.proxmox.com Git - mirror_frr.git/blob - lib/ipaddr.h
*: auto-convert to SPDX License IDs
[mirror_frr.git] / lib / ipaddr.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * IP address structure (for generic IPv4 or IPv6 address)
4 * Copyright (C) 2016, 2017 Cumulus Networks, Inc.
5 */
6
7 #ifndef __IPADDR_H__
8 #define __IPADDR_H__
9
10 #include <zebra.h>
11
12 #include "lib/log.h"
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17
18 /*
19 * Generic IP address - union of IPv4 and IPv6 address.
20 */
21 enum ipaddr_type_t {
22 IPADDR_NONE = AF_UNSPEC,
23 IPADDR_V4 = AF_INET,
24 IPADDR_V6 = AF_INET6,
25 };
26
27 struct ipaddr {
28 enum ipaddr_type_t ipa_type;
29 union {
30 uint8_t addr;
31 struct in_addr _v4_addr;
32 struct in6_addr _v6_addr;
33 } ip;
34 #define ipaddr_v4 ip._v4_addr
35 #define ipaddr_v6 ip._v6_addr
36 };
37
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)
41
42 #define SET_IPADDR_V4(p) (p)->ipa_type = IPADDR_V4
43 #define SET_IPADDR_V6(p) (p)->ipa_type = IPADDR_V6
44
45 #define IPADDRSZ(p) \
46 (IS_IPADDR_V4((p)) ? sizeof(struct in_addr) : sizeof(struct in6_addr))
47
48 #define IPADDR_STRING_SIZE 46
49
50 static inline int ipaddr_family(const struct ipaddr *ip)
51 {
52 switch (ip->ipa_type) {
53 case IPADDR_V4:
54 return AF_INET;
55 case IPADDR_V6:
56 return AF_INET6;
57 case IPADDR_NONE:
58 return AF_UNSPEC;
59 }
60
61 assert(!"Reached end of function where we should never hit");
62 }
63
64 static inline int str2ipaddr(const char *str, struct ipaddr *ip)
65 {
66 int ret;
67
68 memset(ip, 0, sizeof(struct ipaddr));
69
70 ret = inet_pton(AF_INET, str, &ip->ipaddr_v4);
71 if (ret > 0) /* Valid IPv4 address. */
72 {
73 ip->ipa_type = IPADDR_V4;
74 return 0;
75 }
76 ret = inet_pton(AF_INET6, str, &ip->ipaddr_v6);
77 if (ret > 0) /* Valid IPv6 address. */
78 {
79 ip->ipa_type = IPADDR_V6;
80 return 0;
81 }
82
83 return -1;
84 }
85
86 static inline char *ipaddr2str(const struct ipaddr *ip, char *buf, int size)
87 {
88 buf[0] = '\0';
89 if (ip)
90 inet_ntop(ip->ipa_type, &ip->ip.addr, buf, size);
91 return buf;
92 }
93
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) \
98 : 0) \
99 : 0)
100
101 /*
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
105 * is required.
106 */
107 static inline void ipv4_to_ipv4_mapped_ipv6(struct in6_addr *in6,
108 struct in_addr in)
109 {
110 uint32_t addr_type = htonl(0xFFFF);
111
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));
115 }
116
117 /*
118 * convert an ipv4 mapped ipv6 address back to ipv4 address
119 */
120 static inline void ipv4_mapped_ipv6_to_ipv4(const struct in6_addr *in6,
121 struct in_addr *in)
122 {
123 memset(in, 0, sizeof(struct in_addr));
124 memcpy(in, (char *)in6 + 12, sizeof(struct in_addr));
125 }
126
127 /*
128 * generic ordering comparison between IP addresses
129 */
130 static inline int ipaddr_cmp(const struct ipaddr *a, const struct ipaddr *b)
131 {
132 uint32_t va, vb;
133 va = a->ipa_type;
134 vb = b->ipa_type;
135 if (va != vb)
136 return (va < vb) ? -1 : 1;
137 switch (a->ipa_type) {
138 case IPADDR_V4:
139 va = ntohl(a->ipaddr_v4.s_addr);
140 vb = ntohl(b->ipaddr_v4.s_addr);
141 if (va != vb)
142 return (va < vb) ? -1 : 1;
143 return 0;
144 case IPADDR_V6:
145 return memcmp((void *)&a->ipaddr_v6, (void *)&b->ipaddr_v6,
146 sizeof(a->ipaddr_v6));
147 case IPADDR_NONE:
148 return 0;
149 }
150
151 assert(!"Reached end of function we should never hit");
152 }
153
154 static inline bool ipaddr_is_zero(const struct ipaddr *ip)
155 {
156 switch (ip->ipa_type) {
157 case IPADDR_NONE:
158 return true;
159 case IPADDR_V4:
160 return ip->ipaddr_v4.s_addr == INADDR_ANY;
161 case IPADDR_V6:
162 return IN6_IS_ADDR_UNSPECIFIED(&ip->ipaddr_v6);
163 }
164 return true;
165 }
166
167 #ifdef _FRR_ATTRIBUTE_PRINTFRR
168 #pragma FRR printfrr_ext "%pIA" (struct ipaddr *)
169 #endif
170
171 #ifdef __cplusplus
172 }
173 #endif
174
175 #endif /* __IPADDR_H__ */