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>
53 char buf[MAX(INET6_ADDRSTRLEN, INET_ADDRSTRLEN)];
54 } __attribute__ ((unused));
57 * Macros for comparing and copying IP addresses
60 ip4cmp(const struct in_addr *a, const struct in_addr *b)
62 return a->s_addr > b->s_addr ? +1 : a->s_addr < b->s_addr ? -1 : 0;
65 ip4equal(const struct in_addr *a, const struct in_addr *b)
67 return a->s_addr == b->s_addr;
71 ip6cmp(const struct in6_addr *a, const struct in6_addr *b)
73 return memcmp(a, b, sizeof(*a));
76 ip6equal(const struct in6_addr *a, const struct in6_addr *b)
78 return ip6cmp(a, b) == 0;
82 ipequal(const union olsr_ip_addr *a, const union olsr_ip_addr *b)
84 return olsr_cnf->ip_version == AF_INET ? ip4equal(&a->v4, &b->v4) : ip6equal(&a->v6, &b->v6);
87 /* Do not use this - this is as evil as the COPY_IP() macro was and only used in
88 * source which also needs cleanups.
91 genipcopy(void *dst, const void *src)
93 memcpy(dst, src, olsr_cnf->ipsize);
96 int ip_in_net(const union olsr_ip_addr *ipaddr, const struct olsr_ip_prefix *net);
98 int prefix_to_netmask(uint8_t *, int, uint8_t);
101 olsr_prefix_to_netmask(union olsr_ip_addr *adr, uint8_t prefixlen)
103 return prefix_to_netmask(adr->v6.s6_addr, olsr_cnf->ipsize, prefixlen);
106 uint8_t netmask_to_prefix(const uint8_t *, int);
108 static INLINE uint8_t
109 olsr_netmask_to_prefix(const union olsr_ip_addr *adr)
111 return netmask_to_prefix(adr->v6.s6_addr, olsr_cnf->ipsize);
114 static INLINE uint8_t
115 olsr_netmask4_to_prefix(const uint32_t * a)
117 return netmask_to_prefix((const uint8_t *)a, sizeof(*a));
119 static INLINE uint8_t
120 olsr_netmask6_to_prefix(const struct in6_addr *a)
122 return netmask_to_prefix((const uint8_t *)a, sizeof(*a));
125 static INLINE const char *
126 ip4_to_string(struct ipaddr_str *const buf, const struct in_addr addr4)
128 return inet_ntop(AF_INET, &addr4, buf->buf, sizeof(buf->buf));
131 static INLINE const char *
132 ip6_to_string(struct ipaddr_str *const buf, const struct in6_addr *const addr6)
134 return inet_ntop(AF_INET6, addr6, buf->buf, sizeof(buf->buf));
137 static INLINE const char *
138 olsr_ip_to_string(struct ipaddr_str *const buf, const union olsr_ip_addr *addr)
140 return inet_ntop(olsr_cnf->ip_version, addr, buf->buf, sizeof(buf->buf));
144 olsr_ip_prefix_to_string(const struct olsr_ip_prefix *prefix);
147 olsr_string_to_prefix(int ipversion, struct olsr_ip_prefix *dst, const char *buf);
149 static INLINE const char *
150 sockaddr4_to_string(struct ipaddr_str *const buf, const struct sockaddr *const addr)
153 const struct sockaddr *a_sockaddr;
154 const struct sockaddr_in *a_sockaddr_in;
156 nowarn.a_sockaddr = addr;
157 return ip4_to_string(buf, nowarn.a_sockaddr_in->sin_addr);
160 /* we need to handle one value specifically since shifting 32 bits of a 32 bit integer is the same as shifting 0 bits.
161 * The result is in host-byte-order.
163 static INLINE uint32_t
164 prefix_to_netmask4(uint8_t prefixlen)
166 return prefixlen == 0 ? 0 : (~0U << (32 - prefixlen));
170 olsr_is_niit_ip(const union olsr_ip_addr *ip) {
171 return olsr_cnf->ip_version == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&ip->v6);
174 static INLINE union olsr_ip_addr *
175 olsr_ipv6_to_ipv4(const union olsr_ip_addr *ipv6, union olsr_ip_addr *ipv4) {
176 memcpy(&ipv4->v4, &ipv6->v6.s6_addr[12], sizeof(ipv4->v4));
185 * indent-tabs-mode: nil