]> git.proxmox.com Git - mirror_frr.git/blob - lib/ipaddr.h
Merge pull request #7220 from idryzhov/fix-clear-isis
[mirror_frr.git] / lib / ipaddr.h
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
28 #include "lib/log.h"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 /*
35 * Generic IP address - union of IPv4 and IPv6 address.
36 */
37 enum ipaddr_type_t {
38 IPADDR_NONE = AF_UNSPEC,
39 IPADDR_V4 = AF_INET,
40 IPADDR_V6 = AF_INET6,
41 };
42
43 struct ipaddr {
44 enum ipaddr_type_t ipa_type;
45 union {
46 uint8_t addr;
47 struct in_addr _v4_addr;
48 struct in6_addr _v6_addr;
49 } ip;
50 #define ipaddr_v4 ip._v4_addr
51 #define ipaddr_v6 ip._v6_addr
52 };
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
61 #define IPADDRSZ(p) \
62 (IS_IPADDR_V4((p)) ? sizeof(struct in_addr) : sizeof(struct in6_addr))
63
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
76 static inline int str2ipaddr(const char *str, struct ipaddr *ip)
77 {
78 int ret;
79
80 memset(ip, 0, sizeof(struct ipaddr));
81
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 }
94
95 return -1;
96 }
97
98 static inline char *ipaddr2str(const struct ipaddr *ip, char *buf, int size)
99 {
100 buf[0] = '\0';
101 if (ip)
102 inet_ntop(ip->ipa_type, &ip->ip.addr, buf, size);
103 return buf;
104 }
105
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
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 */
119 static inline void ipv4_to_ipv4_mapped_ipv6(struct in6_addr *in6,
120 struct in_addr in)
121 {
122 uint32_t addr_type = htonl(0xFFFF);
123
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
129 /*
130 * convert an ipv4 mapped ipv6 address back to ipv4 address
131 */
132 static inline void ipv4_mapped_ipv6_to_ipv4(const struct in6_addr *in6,
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
139 /*
140 * Check if a struct ipaddr has nonzero value
141 */
142 static inline bool ipaddr_isset(struct ipaddr *ip)
143 {
144 static struct ipaddr a = {};
145 return (0 != memcmp(&a, ip, sizeof(struct ipaddr)));
146 }
147
148 /*
149 * generic ordering comparison between IP addresses
150 */
151 static inline int ipaddr_cmp(const struct ipaddr *a, const struct ipaddr *b)
152 {
153 uint32_t va, vb;
154 va = a->ipa_type;
155 vb = b->ipa_type;
156 if (va != vb)
157 return (va < vb) ? -1 : 1;
158 switch (a->ipa_type) {
159 case IPADDR_V4:
160 va = ntohl(a->ipaddr_v4.s_addr);
161 vb = ntohl(b->ipaddr_v4.s_addr);
162 if (va != vb)
163 return (va < vb) ? -1 : 1;
164 return 0;
165 case IPADDR_V6:
166 return memcmp((void *)&a->ipaddr_v6, (void *)&b->ipaddr_v6,
167 sizeof(a->ipaddr_v6));
168 default:
169 return 0;
170 }
171 }
172
173 #ifdef _FRR_ATTRIBUTE_PRINTFRR
174 #pragma FRR printfrr_ext "%pIA" (struct ipaddr *)
175 #endif
176
177 #ifdef __cplusplus
178 }
179 #endif
180
181 #endif /* __IPADDR_H__ */