3 * The olsr.org Optimized Link-State Routing daemon(olsrd)
4 * Copyright (c) 2007, Bernd Petrovitsch <berndæfirmix.at>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of olsr.org, olsrd nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * Visit http://www.olsr.org for more information.
36 * If you find this software useful feel free to make a donation
37 * to the project. For more information see the website or contact
38 * the copyright holders.
45 #include "olsr_types.h"
49 #include <arpa/inet.h>
50 #include <netinet/in.h>
52 extern const struct olsr_ip_prefix ipv4_internet_route;
53 extern const struct olsr_ip_prefix ipv6_mappedv4_route;
54 extern const struct olsr_ip_prefix ipv6_internet_route;
56 extern const union olsr_ip_addr olsr_ip_zero;
58 extern const union olsr_ip_addr ipv6_def_multicast;
61 char buf[MAX(INET6_ADDRSTRLEN, INET_ADDRSTRLEN)];
62 } __attribute__ ((unused));
65 * Macros for comparing and copying IP addresses
68 ip4cmp(const struct in_addr *a, const struct in_addr *b)
70 return a->s_addr > b->s_addr ? +1 : a->s_addr < b->s_addr ? -1 : 0;
73 ip4equal(const struct in_addr *a, const struct in_addr *b)
75 return a->s_addr == b->s_addr;
79 ip6cmp(const struct in6_addr *a, const struct in6_addr *b)
81 return memcmp(a, b, sizeof(*a));
84 ip6equal(const struct in6_addr *a, const struct in6_addr *b)
86 return ip6cmp(a, b) == 0;
90 ipequal(const union olsr_ip_addr *a, const union olsr_ip_addr *b)
92 return olsr_cnf->ip_version == AF_INET ? ip4equal(&a->v4, &b->v4) : ip6equal(&a->v6, &b->v6);
95 /* Do not use this - this is as evil as the COPY_IP() macro was and only used in
96 * source which also needs cleanups.
99 genipcopy(void *dst, const void *src)
101 memcpy(dst, src, olsr_cnf->ipsize);
104 int ip_in_net(const union olsr_ip_addr *ipaddr, const struct olsr_ip_prefix *net);
106 int prefix_to_netmask(uint8_t *, int, uint8_t);
109 olsr_prefix_to_netmask(union olsr_ip_addr *adr, uint8_t prefixlen)
111 return prefix_to_netmask(adr->v6.s6_addr, olsr_cnf->ipsize, prefixlen);
114 uint8_t netmask_to_prefix(const uint8_t *, int);
116 static INLINE uint8_t
117 olsr_netmask_to_prefix(const union olsr_ip_addr *adr)
119 return netmask_to_prefix(adr->v6.s6_addr, olsr_cnf->ipsize);
122 static INLINE uint8_t
123 olsr_netmask4_to_prefix(const uint32_t * a)
125 return netmask_to_prefix((const uint8_t *)a, sizeof(*a));
127 static INLINE uint8_t
128 olsr_netmask6_to_prefix(const struct in6_addr *a)
130 return netmask_to_prefix((const uint8_t *)a, sizeof(*a));
133 static INLINE const char *
134 ip4_to_string(struct ipaddr_str *const buf, const struct in_addr addr4)
136 return inet_ntop(AF_INET, &addr4, buf->buf, sizeof(buf->buf));
139 static INLINE const char *
140 ip6_to_string(struct ipaddr_str *const buf, const struct in6_addr *const addr6)
142 return inet_ntop(AF_INET6, addr6, buf->buf, sizeof(buf->buf));
145 static INLINE const char *
146 olsr_ip_to_string(struct ipaddr_str *const buf, const union olsr_ip_addr *addr)
148 return inet_ntop(olsr_cnf->ip_version, addr, buf->buf, sizeof(buf->buf));
152 olsr_ip_prefix_to_string(const struct olsr_ip_prefix *prefix);
155 olsr_string_to_prefix(int ipversion, struct olsr_ip_prefix *dst, const char *buf);
157 static INLINE const char *
158 sockaddr4_to_string(struct ipaddr_str *const buf, const struct sockaddr *const addr)
160 const struct sockaddr_in *addr4 = (const struct sockaddr_in *)CONST_ARM_NOWARN_ALIGN(addr);
161 return ip4_to_string(buf, addr4->sin_addr);
165 is_prefix_niit_ipv6(const struct olsr_ip_prefix *p) {
166 return olsr_cnf->ip_version == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&p->prefix.v6)
167 && p->prefix_len >= ipv6_mappedv4_route.prefix_len;
170 static INLINE struct olsr_ip_prefix *
171 prefix_mappedv4_to_v4(struct olsr_ip_prefix *v4, const struct olsr_ip_prefix *v6) {
172 memcpy(&v4->prefix.v4, &v6->prefix.v6.s6_addr[12], sizeof(struct in_addr));
173 v4->prefix_len = v6->prefix_len - 96;
179 ip_is_linklocal(const union olsr_ip_addr *ip) {
180 return olsr_cnf->ip_version == AF_INET6
181 && ip->v6.s6_addr[0] == 0xfe && (ip->v6.s6_addr[1] & 0xc0) == 0x80;
185 ip_prefix_is_mappedv4(const struct olsr_ip_prefix *prefix) {
186 return prefix->prefix_len >= ipv6_mappedv4_route.prefix_len
187 && memcmp(prefix, &ipv6_mappedv4_route, ipv6_mappedv4_route.prefix_len / 8) == 0;
191 ip_prefix_is_mappedv4_inetgw(const struct olsr_ip_prefix *prefix) {
192 return prefix->prefix_len == ipv6_mappedv4_route.prefix_len
193 && memcmp(prefix, &ipv6_mappedv4_route, ipv6_mappedv4_route.prefix_len / 8) == 0;
197 ip_prefix_is_v4_inetgw(const struct olsr_ip_prefix *prefix) {
198 return prefix->prefix_len == ipv4_internet_route.prefix_len
199 && prefix->prefix.v4.s_addr == ipv4_internet_route.prefix.v4.s_addr;
203 ip_prefix_is_v6_inetgw(const struct olsr_ip_prefix *prefix) {
204 return prefix->prefix_len == ipv6_internet_route.prefix_len
205 && memcmp(prefix, &ipv6_internet_route, ipv6_internet_route.prefix_len/8) == 0;
208 extern bool is_prefix_inetgw(const struct olsr_ip_prefix *prefix);
215 * indent-tabs-mode: nil