]>
git.proxmox.com Git - mirror_frr.git/blob - lib/ipaddr.h
2 * IP address structure (for generic IPv4 or IPv6 address)
3 * Copyright (C) 2016, 2017 Cumulus Networks, Inc.
5 * This file is part of FRR.
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
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.
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
33 * Generic IP address - union of IPv4 and IPv6 address.
37 IPADDR_V4
= 1, /* IPv4 */
38 IPADDR_V6
= 2, /* IPv6 */
42 enum ipaddr_type_t ipa_type
;
45 struct in_addr _v4_addr
;
46 struct in6_addr _v6_addr
;
48 #define ipaddr_v4 ip._v4_addr
49 #define ipaddr_v6 ip._v6_addr
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)
56 #define SET_IPADDR_V4(p) (p)->ipa_type = IPADDR_V4
57 #define SET_IPADDR_V6(p) (p)->ipa_type = IPADDR_V6
59 static inline int str2ipaddr(const char *str
, struct ipaddr
*ip
)
63 memset(ip
, 0, sizeof(struct ipaddr
));
65 ret
= inet_pton(AF_INET
, str
, &ip
->ipaddr_v4
);
66 if (ret
> 0) /* Valid IPv4 address. */
68 ip
->ipa_type
= IPADDR_V4
;
71 ret
= inet_pton(AF_INET6
, str
, &ip
->ipaddr_v6
);
72 if (ret
> 0) /* Valid IPv6 address. */
74 ip
->ipa_type
= IPADDR_V6
;
81 static inline char *ipaddr2str(struct ipaddr
*ip
, char *buf
, int size
)
86 inet_ntop(AF_INET
, &ip
->ip
.addr
, buf
, size
);
87 else if (IS_IPADDR_V6(ip
))
88 inet_ntop(AF_INET6
, &ip
->ip
.addr
, buf
, size
);
94 * Convert IPv4 address to IPv4-mapped IPv6 address which is of the
95 * form ::FFFF:<IPv4 address> (RFC 4291). This IPv6 address can then
96 * be used to represent the IPv4 address, wherever only an IPv6 address
99 static inline void ipv4_to_ipv4_mapped_ipv6(struct in6_addr
*in6
,
102 uint32_t addr_type
= htonl(0xFFFF);
104 memset(in6
, 0, sizeof(struct in6_addr
));
105 memcpy((char *)in6
+ 8, &addr_type
, sizeof(addr_type
));
106 memcpy((char *)in6
+ 12, &in
, sizeof(struct in_addr
));
110 * convert an ipv4 mapped ipv6 address back to ipv4 address
112 static inline void ipv4_mapped_ipv6_to_ipv4(struct in6_addr
*in6
,
115 memset(in
, 0, sizeof(struct in_addr
));
116 memcpy(in
, (char *)in6
+ 12, sizeof(struct in_addr
));
123 #endif /* __IPADDR_H__ */