]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_addr.h
Merge pull request #10770 from chiragshah6/evpn_dev3
[mirror_frr.git] / pimd / pim_addr.h
1 /*
2 * PIM address generalizations
3 * Copyright (C) 2022 David Lamparter for NetDEF, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #ifndef _PIMD_PIM_ADDR_H
21 #define _PIMD_PIM_ADDR_H
22
23 #include "jhash.h"
24 #include "prefix.h"
25
26 /* clang-format off */
27
28 #if PIM_IPV == 4
29 typedef struct in_addr pim_addr;
30
31 #define PIM_ADDRSTRLEN INET_ADDRSTRLEN
32 #define PIM_AF AF_INET
33 #define PIM_AFI AFI_IP
34 #define PIM_MAX_BITLEN IPV4_MAX_BITLEN
35 #define PIM_AF_NAME "ip"
36
37 union pimprefixptr {
38 prefixtype(pimprefixptr, struct prefix, p)
39 prefixtype(pimprefixptr, struct prefix_ipv4, p4)
40 } TRANSPARENT_UNION;
41
42 union pimprefixconstptr {
43 prefixtype(pimprefixconstptr, const struct prefix, p)
44 prefixtype(pimprefixconstptr, const struct prefix_ipv4, p4)
45 } TRANSPARENT_UNION;
46
47 #else
48 typedef struct in6_addr pim_addr;
49
50 #define PIM_ADDRSTRLEN INET6_ADDRSTRLEN
51 #define PIM_AF AF_INET6
52 #define PIM_AFI AFI_IP6
53 #define PIM_MAX_BITLEN IPV6_MAX_BITLEN
54 #define PIM_AF_NAME "ipv6"
55
56 union pimprefixptr {
57 prefixtype(pimprefixptr, struct prefix, p)
58 prefixtype(pimprefixptr, struct prefix_ipv6, p6)
59 } TRANSPARENT_UNION;
60
61 union pimprefixconstptr {
62 prefixtype(pimprefixconstptr, const struct prefix, p)
63 prefixtype(pimprefixconstptr, const struct prefix_ipv6, p6)
64 } TRANSPARENT_UNION;
65 #endif
66
67 /* for assignment/initialization (C99 compound literal)
68 * named PIMADDR_ANY (not PIM_ADDR_ANY) to match INADDR_ANY
69 */
70 #define PIMADDR_ANY (pim_addr){ }
71
72 /* clang-format on */
73
74 static inline bool pim_addr_is_any(pim_addr addr)
75 {
76 pim_addr zero = {};
77
78 return memcmp(&addr, &zero, sizeof(zero)) == 0;
79 }
80
81 static inline int pim_addr_cmp(pim_addr a, pim_addr b)
82 {
83 return memcmp(&a, &b, sizeof(a));
84 }
85
86 static inline void pim_addr_to_prefix(union pimprefixptr out, pim_addr in)
87 {
88 out.p->family = PIM_AF;
89 out.p->prefixlen = PIM_MAX_BITLEN;
90 memcpy(out.p->u.val, &in, sizeof(in));
91 }
92
93 static inline pim_addr pim_addr_from_prefix(union pimprefixconstptr in)
94 {
95 pim_addr ret;
96
97 if (in.p->family != PIM_AF)
98 return PIMADDR_ANY;
99
100 memcpy(&ret, in.p->u.val, sizeof(ret));
101 return ret;
102 }
103
104 /* don't use this struct directly, use the pim_sgaddr typedef */
105 struct _pim_sgaddr {
106 pim_addr grp;
107 pim_addr src;
108 };
109
110 typedef struct _pim_sgaddr pim_sgaddr;
111
112 static inline int pim_sgaddr_cmp(const pim_sgaddr a, const pim_sgaddr b)
113 {
114 /* memcmp over the entire struct = memcmp(grp) + memcmp(src) */
115 return memcmp(&a, &b, sizeof(a));
116 }
117
118 static inline uint32_t pim_sgaddr_hash(const pim_sgaddr a, uint32_t initval)
119 {
120 return jhash2((uint32_t *)&a, sizeof(a) / sizeof(uint32_t), initval);
121 }
122
123 #ifdef _FRR_ATTRIBUTE_PRINTFRR
124 #pragma FRR printfrr_ext "%pPA" (pim_addr *)
125 #pragma FRR printfrr_ext "%pSG" (pim_sgaddr *)
126 #endif
127
128 /*
129 * There is no pim_sgaddr2str(). This is intentional. Instead, use:
130 * snprintfrr(buf, sizeof(buf), "%pPA", sgaddr)
131 * (and note that snprintfrr is implicit for vty_out and zlog_*)
132 */
133
134 #endif /* _PIMD_PIM_ADDR_H */