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