]>
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 | ||
5e244469 RW |
28 | #ifdef __cplusplus |
29 | extern "C" { | |
30 | #endif | |
31 | ||
482c0fb0 | 32 | /* |
33 | * Generic IP address - union of IPv4 and IPv6 address. | |
34 | */ | |
d62a17ae | 35 | enum ipaddr_type_t { |
36 | IPADDR_NONE = 0, | |
37 | IPADDR_V4 = 1, /* IPv4 */ | |
38 | IPADDR_V6 = 2, /* IPv6 */ | |
482c0fb0 | 39 | }; |
40 | ||
d62a17ae | 41 | struct ipaddr { |
42 | enum ipaddr_type_t ipa_type; | |
43 | union { | |
d7c0a89a | 44 | uint8_t addr; |
d62a17ae | 45 | struct in_addr _v4_addr; |
46 | struct in6_addr _v6_addr; | |
47 | } ip; | |
482c0fb0 | 48 | #define ipaddr_v4 ip._v4_addr |
49 | #define ipaddr_v6 ip._v6_addr | |
7fb9d20f | 50 | }; |
482c0fb0 | 51 | |
52 | #define IS_IPADDR_NONE(p) ((p)->ipa_type == IPADDR_NONE) | |
53 | #define IS_IPADDR_V4(p) ((p)->ipa_type == IPADDR_V4) | |
54 | #define IS_IPADDR_V6(p) ((p)->ipa_type == IPADDR_V6) | |
55 | ||
56 | #define SET_IPADDR_V4(p) (p)->ipa_type = IPADDR_V4 | |
57 | #define SET_IPADDR_V6(p) (p)->ipa_type = IPADDR_V6 | |
58 | ||
4f838de4 | 59 | #define IPADDRSZ(p) \ |
2fff50ec | 60 | (IS_IPADDR_V4((p)) ? sizeof(struct in_addr) : sizeof(struct in6_addr)) |
4f838de4 | 61 | |
d62a17ae | 62 | static inline int str2ipaddr(const char *str, struct ipaddr *ip) |
482c0fb0 | 63 | { |
d62a17ae | 64 | int ret; |
482c0fb0 | 65 | |
d62a17ae | 66 | memset(ip, 0, sizeof(struct ipaddr)); |
482c0fb0 | 67 | |
d62a17ae | 68 | ret = inet_pton(AF_INET, str, &ip->ipaddr_v4); |
69 | if (ret > 0) /* Valid IPv4 address. */ | |
70 | { | |
71 | ip->ipa_type = IPADDR_V4; | |
72 | return 0; | |
73 | } | |
74 | ret = inet_pton(AF_INET6, str, &ip->ipaddr_v6); | |
75 | if (ret > 0) /* Valid IPv6 address. */ | |
76 | { | |
77 | ip->ipa_type = IPADDR_V6; | |
78 | return 0; | |
79 | } | |
482c0fb0 | 80 | |
d62a17ae | 81 | return -1; |
482c0fb0 | 82 | } |
83 | ||
24774285 | 84 | static inline char *ipaddr2str(const struct ipaddr *ip, char *buf, int size) |
482c0fb0 | 85 | { |
d62a17ae | 86 | buf[0] = '\0'; |
87 | if (ip) { | |
88 | if (IS_IPADDR_V4(ip)) | |
89 | inet_ntop(AF_INET, &ip->ip.addr, buf, size); | |
90 | else if (IS_IPADDR_V6(ip)) | |
91 | inet_ntop(AF_INET6, &ip->ip.addr, buf, size); | |
92 | } | |
93 | return buf; | |
482c0fb0 | 94 | } |
1ec31309 | 95 | |
bfd498f0 | 96 | /* |
97 | * Convert IPv4 address to IPv4-mapped IPv6 address which is of the | |
98 | * form ::FFFF:<IPv4 address> (RFC 4291). This IPv6 address can then | |
99 | * be used to represent the IPv4 address, wherever only an IPv6 address | |
100 | * is required. | |
101 | */ | |
1ec31309 | 102 | static inline void ipv4_to_ipv4_mapped_ipv6(struct in6_addr *in6, |
103 | struct in_addr in) | |
104 | { | |
d7c0a89a | 105 | uint32_t addr_type = htonl(0xFFFF); |
55828363 | 106 | |
1ec31309 | 107 | memset(in6, 0, sizeof(struct in6_addr)); |
108 | memcpy((char *)in6 + 8, &addr_type, sizeof(addr_type)); | |
109 | memcpy((char *)in6 + 12, &in, sizeof(struct in_addr)); | |
110 | } | |
111 | ||
f50dc5e6 MK |
112 | /* |
113 | * convert an ipv4 mapped ipv6 address back to ipv4 address | |
114 | */ | |
115 | static inline void ipv4_mapped_ipv6_to_ipv4(struct in6_addr *in6, | |
116 | struct in_addr *in) | |
117 | { | |
118 | memset(in, 0, sizeof(struct in_addr)); | |
119 | memcpy(in, (char *)in6 + 12, sizeof(struct in_addr)); | |
120 | } | |
121 | ||
5e244469 RW |
122 | #ifdef __cplusplus |
123 | } | |
124 | #endif | |
125 | ||
482c0fb0 | 126 | #endif /* __IPADDR_H__ */ |