]> git.proxmox.com Git - mirror_frr.git/blame - lib/ipaddr.h
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / lib / ipaddr.h
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
482c0fb0 2/*
3 * IP address structure (for generic IPv4 or IPv6 address)
4 * Copyright (C) 2016, 2017 Cumulus Networks, Inc.
482c0fb0 5 */
6
7#ifndef __IPADDR_H__
8#define __IPADDR_H__
9
10#include <zebra.h>
11
31f937fb
SM
12#include "lib/log.h"
13
5e244469
RW
14#ifdef __cplusplus
15extern "C" {
16#endif
17
482c0fb0 18/*
19 * Generic IP address - union of IPv4 and IPv6 address.
20 */
d62a17ae 21enum ipaddr_type_t {
713de9e3
DL
22 IPADDR_NONE = AF_UNSPEC,
23 IPADDR_V4 = AF_INET,
24 IPADDR_V6 = AF_INET6,
482c0fb0 25};
26
d62a17ae 27struct ipaddr {
28 enum ipaddr_type_t ipa_type;
29 union {
d7c0a89a 30 uint8_t addr;
d62a17ae 31 struct in_addr _v4_addr;
32 struct in6_addr _v6_addr;
33 } ip;
482c0fb0 34#define ipaddr_v4 ip._v4_addr
35#define ipaddr_v6 ip._v6_addr
7fb9d20f 36};
482c0fb0 37
38#define IS_IPADDR_NONE(p) ((p)->ipa_type == IPADDR_NONE)
39#define IS_IPADDR_V4(p) ((p)->ipa_type == IPADDR_V4)
40#define IS_IPADDR_V6(p) ((p)->ipa_type == IPADDR_V6)
41
42#define SET_IPADDR_V4(p) (p)->ipa_type = IPADDR_V4
43#define SET_IPADDR_V6(p) (p)->ipa_type = IPADDR_V6
44
4f838de4 45#define IPADDRSZ(p) \
2fff50ec 46 (IS_IPADDR_V4((p)) ? sizeof(struct in_addr) : sizeof(struct in6_addr))
4f838de4 47
c1657d26
PG
48#define IPADDR_STRING_SIZE 46
49
31f937fb
SM
50static inline int ipaddr_family(const struct ipaddr *ip)
51{
52 switch (ip->ipa_type) {
53 case IPADDR_V4:
54 return AF_INET;
55 case IPADDR_V6:
56 return AF_INET6;
bde30e78 57 case IPADDR_NONE:
31f937fb
SM
58 return AF_UNSPEC;
59 }
bde30e78
DS
60
61 assert(!"Reached end of function where we should never hit");
31f937fb
SM
62}
63
d62a17ae 64static inline int str2ipaddr(const char *str, struct ipaddr *ip)
482c0fb0 65{
d62a17ae 66 int ret;
482c0fb0 67
d62a17ae 68 memset(ip, 0, sizeof(struct ipaddr));
482c0fb0 69
d62a17ae 70 ret = inet_pton(AF_INET, str, &ip->ipaddr_v4);
71 if (ret > 0) /* Valid IPv4 address. */
72 {
73 ip->ipa_type = IPADDR_V4;
74 return 0;
75 }
76 ret = inet_pton(AF_INET6, str, &ip->ipaddr_v6);
77 if (ret > 0) /* Valid IPv6 address. */
78 {
79 ip->ipa_type = IPADDR_V6;
80 return 0;
81 }
482c0fb0 82
d62a17ae 83 return -1;
482c0fb0 84}
85
24774285 86static inline char *ipaddr2str(const struct ipaddr *ip, char *buf, int size)
482c0fb0 87{
d62a17ae 88 buf[0] = '\0';
713de9e3
DL
89 if (ip)
90 inet_ntop(ip->ipa_type, &ip->ip.addr, buf, size);
d62a17ae 91 return buf;
482c0fb0 92}
1ec31309 93
92d6f769
K
94#define IS_MAPPED_IPV6(A) \
95 ((A)->s6_addr32[0] == 0x00000000 \
96 ? ((A)->s6_addr32[1] == 0x00000000 \
97 ? (ntohl((A)->s6_addr32[2]) == 0xFFFF ? 1 : 0) \
98 : 0) \
99 : 0)
100
bfd498f0 101/*
102 * Convert IPv4 address to IPv4-mapped IPv6 address which is of the
103 * form ::FFFF:<IPv4 address> (RFC 4291). This IPv6 address can then
104 * be used to represent the IPv4 address, wherever only an IPv6 address
105 * is required.
106 */
1ec31309 107static inline void ipv4_to_ipv4_mapped_ipv6(struct in6_addr *in6,
108 struct in_addr in)
109{
d7c0a89a 110 uint32_t addr_type = htonl(0xFFFF);
55828363 111
1ec31309 112 memset(in6, 0, sizeof(struct in6_addr));
113 memcpy((char *)in6 + 8, &addr_type, sizeof(addr_type));
114 memcpy((char *)in6 + 12, &in, sizeof(struct in_addr));
115}
116
f50dc5e6
MK
117/*
118 * convert an ipv4 mapped ipv6 address back to ipv4 address
119 */
e4a1ec74 120static inline void ipv4_mapped_ipv6_to_ipv4(const struct in6_addr *in6,
f50dc5e6
MK
121 struct in_addr *in)
122{
123 memset(in, 0, sizeof(struct in_addr));
124 memcpy(in, (char *)in6 + 12, sizeof(struct in_addr));
125}
126
598b0dfc
SM
127/*
128 * generic ordering comparison between IP addresses
129 */
130static inline int ipaddr_cmp(const struct ipaddr *a, const struct ipaddr *b)
131{
132 uint32_t va, vb;
133 va = a->ipa_type;
134 vb = b->ipa_type;
135 if (va != vb)
136 return (va < vb) ? -1 : 1;
137 switch (a->ipa_type) {
138 case IPADDR_V4:
139 va = ntohl(a->ipaddr_v4.s_addr);
140 vb = ntohl(b->ipaddr_v4.s_addr);
141 if (va != vb)
142 return (va < vb) ? -1 : 1;
143 return 0;
144 case IPADDR_V6:
145 return memcmp((void *)&a->ipaddr_v6, (void *)&b->ipaddr_v6,
146 sizeof(a->ipaddr_v6));
bde30e78 147 case IPADDR_NONE:
598b0dfc
SM
148 return 0;
149 }
bde30e78
DS
150
151 assert(!"Reached end of function we should never hit");
598b0dfc
SM
152}
153
860e740b
IR
154static inline bool ipaddr_is_zero(const struct ipaddr *ip)
155{
156 switch (ip->ipa_type) {
157 case IPADDR_NONE:
158 return true;
159 case IPADDR_V4:
160 return ip->ipaddr_v4.s_addr == INADDR_ANY;
161 case IPADDR_V6:
162 return IN6_IS_ADDR_UNSPECIFIED(&ip->ipaddr_v6);
163 }
164 return true;
165}
166
dc5d0186
DL
167#ifdef _FRR_ATTRIBUTE_PRINTFRR
168#pragma FRR printfrr_ext "%pIA" (struct ipaddr *)
169#endif
170
5e244469
RW
171#ifdef __cplusplus
172}
173#endif
174
482c0fb0 175#endif /* __IPADDR_H__ */