]>
Commit | Line | Data |
---|---|---|
482c0fb0 | 1 | /* |
2 | * IP address structure (for generic IPv4 or IPv6 address) | |
3 | * Copyright (C) 2016, 2017 Cumulus Networks, Inc. | |
4 | * | |
5 | * This file is part of FRR. | |
6 | * | |
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 | |
10 | * later version. | |
11 | * | |
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. | |
16 | * | |
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 | |
20 | * 02111-1307, USA. | |
21 | */ | |
22 | ||
23 | #ifndef __IPADDR_H__ | |
24 | #define __IPADDR_H__ | |
25 | ||
26 | #include <zebra.h> | |
27 | ||
31f937fb SM |
28 | #include "lib/log.h" |
29 | ||
5e244469 RW |
30 | #ifdef __cplusplus |
31 | extern "C" { | |
32 | #endif | |
33 | ||
482c0fb0 | 34 | /* |
35 | * Generic IP address - union of IPv4 and IPv6 address. | |
36 | */ | |
d62a17ae | 37 | enum ipaddr_type_t { |
713de9e3 DL |
38 | IPADDR_NONE = AF_UNSPEC, |
39 | IPADDR_V4 = AF_INET, | |
40 | IPADDR_V6 = AF_INET6, | |
482c0fb0 | 41 | }; |
42 | ||
d62a17ae | 43 | struct ipaddr { |
44 | enum ipaddr_type_t ipa_type; | |
45 | union { | |
d7c0a89a | 46 | uint8_t addr; |
d62a17ae | 47 | struct in_addr _v4_addr; |
48 | struct in6_addr _v6_addr; | |
49 | } ip; | |
482c0fb0 | 50 | #define ipaddr_v4 ip._v4_addr |
51 | #define ipaddr_v6 ip._v6_addr | |
7fb9d20f | 52 | }; |
482c0fb0 | 53 | |
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) | |
57 | ||
58 | #define SET_IPADDR_V4(p) (p)->ipa_type = IPADDR_V4 | |
59 | #define SET_IPADDR_V6(p) (p)->ipa_type = IPADDR_V6 | |
60 | ||
4f838de4 | 61 | #define IPADDRSZ(p) \ |
2fff50ec | 62 | (IS_IPADDR_V4((p)) ? sizeof(struct in_addr) : sizeof(struct in6_addr)) |
4f838de4 | 63 | |
31f937fb SM |
64 | static inline int ipaddr_family(const struct ipaddr *ip) |
65 | { | |
66 | switch (ip->ipa_type) { | |
67 | case IPADDR_V4: | |
68 | return AF_INET; | |
69 | case IPADDR_V6: | |
70 | return AF_INET6; | |
71 | default: | |
72 | return AF_UNSPEC; | |
73 | } | |
74 | } | |
75 | ||
d62a17ae | 76 | static inline int str2ipaddr(const char *str, struct ipaddr *ip) |
482c0fb0 | 77 | { |
d62a17ae | 78 | int ret; |
482c0fb0 | 79 | |
d62a17ae | 80 | memset(ip, 0, sizeof(struct ipaddr)); |
482c0fb0 | 81 | |
d62a17ae | 82 | ret = inet_pton(AF_INET, str, &ip->ipaddr_v4); |
83 | if (ret > 0) /* Valid IPv4 address. */ | |
84 | { | |
85 | ip->ipa_type = IPADDR_V4; | |
86 | return 0; | |
87 | } | |
88 | ret = inet_pton(AF_INET6, str, &ip->ipaddr_v6); | |
89 | if (ret > 0) /* Valid IPv6 address. */ | |
90 | { | |
91 | ip->ipa_type = IPADDR_V6; | |
92 | return 0; | |
93 | } | |
482c0fb0 | 94 | |
d62a17ae | 95 | return -1; |
482c0fb0 | 96 | } |
97 | ||
24774285 | 98 | static inline char *ipaddr2str(const struct ipaddr *ip, char *buf, int size) |
482c0fb0 | 99 | { |
d62a17ae | 100 | buf[0] = '\0'; |
713de9e3 DL |
101 | if (ip) |
102 | inet_ntop(ip->ipa_type, &ip->ip.addr, buf, size); | |
d62a17ae | 103 | return buf; |
482c0fb0 | 104 | } |
1ec31309 | 105 | |
92d6f769 K |
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) \ | |
110 | : 0) \ | |
111 | : 0) | |
112 | ||
bfd498f0 | 113 | /* |
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 | |
117 | * is required. | |
118 | */ | |
1ec31309 | 119 | static inline void ipv4_to_ipv4_mapped_ipv6(struct in6_addr *in6, |
120 | struct in_addr in) | |
121 | { | |
d7c0a89a | 122 | uint32_t addr_type = htonl(0xFFFF); |
55828363 | 123 | |
1ec31309 | 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)); | |
127 | } | |
128 | ||
f50dc5e6 MK |
129 | /* |
130 | * convert an ipv4 mapped ipv6 address back to ipv4 address | |
131 | */ | |
e4a1ec74 | 132 | static inline void ipv4_mapped_ipv6_to_ipv4(const struct in6_addr *in6, |
f50dc5e6 MK |
133 | struct in_addr *in) |
134 | { | |
135 | memset(in, 0, sizeof(struct in_addr)); | |
136 | memcpy(in, (char *)in6 + 12, sizeof(struct in_addr)); | |
137 | } | |
138 | ||
598b0dfc SM |
139 | /* |
140 | * generic ordering comparison between IP addresses | |
141 | */ | |
142 | static inline int ipaddr_cmp(const struct ipaddr *a, const struct ipaddr *b) | |
143 | { | |
144 | uint32_t va, vb; | |
145 | va = a->ipa_type; | |
146 | vb = b->ipa_type; | |
147 | if (va != vb) | |
148 | return (va < vb) ? -1 : 1; | |
149 | switch (a->ipa_type) { | |
150 | case IPADDR_V4: | |
151 | va = ntohl(a->ipaddr_v4.s_addr); | |
152 | vb = ntohl(b->ipaddr_v4.s_addr); | |
153 | if (va != vb) | |
154 | return (va < vb) ? -1 : 1; | |
155 | return 0; | |
156 | case IPADDR_V6: | |
157 | return memcmp((void *)&a->ipaddr_v6, (void *)&b->ipaddr_v6, | |
158 | sizeof(a->ipaddr_v6)); | |
159 | default: | |
160 | return 0; | |
161 | } | |
162 | } | |
163 | ||
860e740b IR |
164 | static inline bool ipaddr_is_zero(const struct ipaddr *ip) |
165 | { | |
166 | switch (ip->ipa_type) { | |
167 | case IPADDR_NONE: | |
168 | return true; | |
169 | case IPADDR_V4: | |
170 | return ip->ipaddr_v4.s_addr == INADDR_ANY; | |
171 | case IPADDR_V6: | |
172 | return IN6_IS_ADDR_UNSPECIFIED(&ip->ipaddr_v6); | |
173 | } | |
174 | return true; | |
175 | } | |
176 | ||
dc5d0186 DL |
177 | #ifdef _FRR_ATTRIBUTE_PRINTFRR |
178 | #pragma FRR printfrr_ext "%pIA" (struct ipaddr *) | |
179 | #endif | |
180 | ||
5e244469 RW |
181 | #ifdef __cplusplus |
182 | } | |
183 | #endif | |
184 | ||
482c0fb0 | 185 | #endif /* __IPADDR_H__ */ |