corosync  2.4.4
totemip.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005-2011 Red Hat, Inc.
3  *
4  * All rights reserved.
5  *
6  * Author: Patrick Caulfield (pcaulfie@redhat.com)
7  *
8  * This software licensed under BSD license, the text of which follows:
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * - Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  * - Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  * - Neither the name of the MontaVista Software, Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /* IPv4/6 abstraction */
36 
37 #include <config.h>
38 
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <netdb.h>
45 #include <net/if.h>
46 #include <string.h>
47 #include <stdio.h>
48 #include <errno.h>
49 #include <assert.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 #include <ifaddrs.h>
53 
54 #include <corosync/totem/totemip.h>
55 #include <corosync/swab.h>
56 
57 #define LOCALHOST_IPV4 "127.0.0.1"
58 #define LOCALHOST_IPV6 "::1"
59 
60 #define NETLINK_BUFSIZE 16384
61 
62 #ifdef SO_NOSIGPIPE
63 void totemip_nosigpipe(int s)
64 {
65  int on = 1;
66  setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
67 }
68 #endif
69 
70 /* Compare two addresses */
71 int totemip_equal(const struct totem_ip_address *addr1,
72  const struct totem_ip_address *addr2)
73 {
74  int addrlen = 0;
75 
76  if (addr1->family != addr2->family)
77  return 0;
78 
79  if (addr1->family == AF_INET) {
80  addrlen = sizeof(struct in_addr);
81  }
82  if (addr1->family == AF_INET6) {
83  addrlen = sizeof(struct in6_addr);
84  }
85  assert(addrlen);
86 
87  if (memcmp(addr1->addr, addr2->addr, addrlen) == 0)
88  return 1;
89  else
90  return 0;
91 
92 }
93 
94 /* Copy a totem_ip_address */
95 void totemip_copy(struct totem_ip_address *addr1,
96  const struct totem_ip_address *addr2)
97 {
98  memcpy(addr1, addr2, sizeof(struct totem_ip_address));
99 }
100 
102  const struct totem_ip_address *addr2)
103 {
104  addr1->nodeid = swab32(addr2->nodeid);
105  addr1->family = swab16(addr2->family);
106  memcpy(addr1->addr, addr2->addr, TOTEMIP_ADDRLEN);
107 }
108 
109 /*
110  * Multicast address range is 224.0.0.0 to 239.255.255.255 this
111  * translates to the first 4 bits == 1110 (0xE).
112  * http://en.wikipedia.org/wiki/Multicast_address
113  */
114 int32_t totemip_is_mcast(struct totem_ip_address *ip_addr)
115 {
116  uint32_t addr = 0;
117 
118  memcpy (&addr, ip_addr->addr, sizeof (uint32_t));
119 
120  if (ip_addr->family == AF_INET) {
121  addr = ntohl(addr);
122  if ((addr >> 28) != 0xE) {
123  return -1;
124  }
125  }
126  return 0;
127 }
128 
129 /* For sorting etc. params are void * for qsort's benefit */
130 int totemip_compare(const void *a, const void *b)
131 {
132  int i;
133  const struct totem_ip_address *totemip_a = (const struct totem_ip_address *)a;
134  const struct totem_ip_address *totemip_b = (const struct totem_ip_address *)b;
135  struct in_addr ipv4_a1;
136  struct in_addr ipv4_a2;
137  struct in6_addr ipv6_a1;
138  struct in6_addr ipv6_a2;
139  unsigned short family;
140 
141  /*
142  * Use memcpy to align since totem_ip_address is unaligned on various archs
143  */
144  memcpy (&family, &totemip_a->family, sizeof (unsigned short));
145 
146  if (family == AF_INET) {
147  memcpy (&ipv4_a1, totemip_a->addr, sizeof (struct in_addr));
148  memcpy (&ipv4_a2, totemip_b->addr, sizeof (struct in_addr));
149  if (ipv4_a1.s_addr == ipv4_a2.s_addr) {
150  return (0);
151  }
152  if (htonl(ipv4_a1.s_addr) < htonl(ipv4_a2.s_addr)) {
153  return -1;
154  } else {
155  return +1;
156  }
157  } else
158  if (family == AF_INET6) {
159  /*
160  * We can only compare 8 bits at time for portability reasons
161  */
162  memcpy (&ipv6_a1, totemip_a->addr, sizeof (struct in6_addr));
163  memcpy (&ipv6_a2, totemip_b->addr, sizeof (struct in6_addr));
164  for (i = 0; i < 16; i++) {
165  int res = ipv6_a1.s6_addr[i] -
166  ipv6_a2.s6_addr[i];
167  if (res) {
168  return res;
169  }
170  }
171  return 0;
172  } else {
173  /*
174  * Family not set, should be!
175  */
176  assert (0);
177  }
178  return 0;
179 }
180 
181 /* Build a localhost totem_ip_address */
182 int totemip_localhost(int family, struct totem_ip_address *localhost)
183 {
184  const char *addr_text;
185 
186  memset (localhost, 0, sizeof (struct totem_ip_address));
187 
188  if (family == AF_INET) {
189  addr_text = LOCALHOST_IPV4;
190  if (inet_pton(family, addr_text, (char *)&localhost->nodeid) <= 0) {
191  return -1;
192  }
193  } else {
194  addr_text = LOCALHOST_IPV6;
195  }
196 
197  if (inet_pton(family, addr_text, (char *)localhost->addr) <= 0)
198  return -1;
199 
200  localhost->family = family;
201 
202  return 0;
203 }
204 
206 {
207  struct totem_ip_address localhost;
208 
209  if (totemip_localhost(addr->family, &localhost))
210  return 0;
211  return totemip_equal(addr, &localhost);
212 }
213 
214 const char *totemip_print(const struct totem_ip_address *addr)
215 {
216  static char buf[INET6_ADDRSTRLEN];
217 
218  return (inet_ntop(addr->family, addr->addr, buf, sizeof(buf)));
219 }
220 
221 /* Make a totem_ip_address into a usable sockaddr_storage */
223  uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
224 {
225  int ret = -1;
226 
227  if (ip_addr->family == AF_INET) {
228  struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
229 
230  memset(sin, 0, sizeof(struct sockaddr_in));
231 #ifdef HAVE_SOCK_SIN_LEN
232  sin->sin_len = sizeof(struct sockaddr_in);
233 #endif
234  sin->sin_family = ip_addr->family;
235  sin->sin_port = ntohs(port);
236  memcpy(&sin->sin_addr, ip_addr->addr, sizeof(struct in_addr));
237  *addrlen = sizeof(struct sockaddr_in);
238  ret = 0;
239  }
240 
241  if (ip_addr->family == AF_INET6) {
242  struct sockaddr_in6 *sin = (struct sockaddr_in6 *)saddr;
243 
244  memset(sin, 0, sizeof(struct sockaddr_in6));
245 #ifdef HAVE_SOCK_SIN6_LEN
246  sin->sin6_len = sizeof(struct sockaddr_in6);
247 #endif
248  sin->sin6_family = ip_addr->family;
249  sin->sin6_port = ntohs(port);
250  sin->sin6_scope_id = 2;
251  memcpy(&sin->sin6_addr, ip_addr->addr, sizeof(struct in6_addr));
252 
253  *addrlen = sizeof(struct sockaddr_in6);
254  ret = 0;
255  }
256 
257  return ret;
258 }
259 
260 /* Converts an address string string into a totem_ip_address.
261  family can be AF_INET, AF_INET6 or 0 ("for "don't care")
262 */
263 int totemip_parse(struct totem_ip_address *totemip, const char *addr, int family)
264 {
265  struct addrinfo *ainfo;
266  struct addrinfo ahints;
267  struct sockaddr_in *sa;
268  struct sockaddr_in6 *sa6;
269  int ret;
270 
271  memset(&ahints, 0, sizeof(ahints));
272  ahints.ai_socktype = SOCK_DGRAM;
273  ahints.ai_protocol = IPPROTO_UDP;
274  ahints.ai_family = family;
275 
276  /* Lookup the nodename address */
277  ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
278  if (ret)
279  return -1;
280 
281  sa = (struct sockaddr_in *)ainfo->ai_addr;
282  sa6 = (struct sockaddr_in6 *)ainfo->ai_addr;
283  totemip->family = ainfo->ai_family;
284 
285  if (ainfo->ai_family == AF_INET)
286  memcpy(totemip->addr, &sa->sin_addr, sizeof(struct in_addr));
287  else
288  memcpy(totemip->addr, &sa6->sin6_addr, sizeof(struct in6_addr));
289 
290  freeaddrinfo(ainfo);
291  return 0;
292 }
293 
294 /* Make a sockaddr_* into a totem_ip_address */
295 int totemip_sockaddr_to_totemip_convert(const struct sockaddr_storage *saddr,
296  struct totem_ip_address *ip_addr)
297 {
298  int ret = -1;
299 
300  ip_addr->family = saddr->ss_family;
301  ip_addr->nodeid = 0;
302 
303  if (saddr->ss_family == AF_INET) {
304  const struct sockaddr_in *sin = (const struct sockaddr_in *)saddr;
305 
306  memcpy(ip_addr->addr, &sin->sin_addr, sizeof(struct in_addr));
307  ret = 0;
308  }
309 
310  if (saddr->ss_family == AF_INET6) {
311  const struct sockaddr_in6 *sin
312  = (const struct sockaddr_in6 *)saddr;
313 
314  memcpy(ip_addr->addr, &sin->sin6_addr, sizeof(struct in6_addr));
315 
316  ret = 0;
317  }
318  return ret;
319 }
320 
321 int totemip_getifaddrs(struct list_head *addrs)
322 {
323  struct ifaddrs *ifap, *ifa;
324  struct totem_ip_if_address *if_addr;
325 
326  if (getifaddrs(&ifap) != 0)
327  return (-1);
328 
329  list_init(addrs);
330 
331  for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
332  if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
333  continue ;
334 
335  if ((ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) ||
336  (ifa->ifa_netmask->sa_family != AF_INET && ifa->ifa_netmask->sa_family != AF_INET6 &&
337  ifa->ifa_netmask->sa_family != 0))
338  continue ;
339 
340  if (ifa->ifa_netmask->sa_family == 0) {
341  ifa->ifa_netmask->sa_family = ifa->ifa_addr->sa_family;
342  }
343 
344  if_addr = malloc(sizeof(struct totem_ip_if_address));
345  if (if_addr == NULL) {
346  goto error_free_ifaddrs;
347  }
348 
349  list_init(&if_addr->list);
350 
351  memset(if_addr, 0, sizeof(struct totem_ip_if_address));
352 
353  if_addr->interface_up = ifa->ifa_flags & IFF_UP;
354  if_addr->interface_num = if_nametoindex(ifa->ifa_name);
355  if_addr->name = strdup(ifa->ifa_name);
356  if (if_addr->name == NULL) {
357  goto error_free_addr;
358  }
359 
360  if (totemip_sockaddr_to_totemip_convert((const struct sockaddr_storage *)ifa->ifa_addr,
361  &if_addr->ip_addr) == -1) {
362  goto error_free_addr_name;
363  }
364 
365  if (totemip_sockaddr_to_totemip_convert((const struct sockaddr_storage *)ifa->ifa_netmask,
366  &if_addr->mask_addr) == -1) {
367  goto error_free_addr_name;
368  }
369 
370  list_add_tail(&if_addr->list, addrs);
371  }
372 
373  freeifaddrs(ifap);
374 
375  return (0);
376 
377 error_free_addr_name:
378  free(if_addr->name);
379 
380 error_free_addr:
381  free(if_addr);
382 
383 error_free_ifaddrs:
384  totemip_freeifaddrs(addrs);
385  freeifaddrs(ifap);
386  return (-1);
387 }
388 
389 void totemip_freeifaddrs(struct list_head *addrs)
390 {
391  struct totem_ip_if_address *if_addr;
392  struct list_head *list;
393 
394  for (list = addrs->next; list != addrs;) {
395  if_addr = list_entry(list, struct totem_ip_if_address, list);
396  list = list->next;
397 
398  free(if_addr->name);
399  list_del(&if_addr->list);
400  free(if_addr);
401  }
402  list_init(addrs);
403 }
404 
406  struct totem_ip_address *boundto,
407  int *interface_up,
408  int *interface_num,
409  int mask_high_bit)
410 {
411  struct list_head addrs;
412  struct list_head *list;
413  struct totem_ip_if_address *if_addr;
414  struct totem_ip_address bn_netaddr, if_netaddr;
415  socklen_t addr_len;
416  socklen_t si;
417  int res = -1;
418  int exact_match_found = 0;
419  int net_match_found = 0;
420 
421  *interface_up = 0;
422  *interface_num = 0;
423 
424  if (totemip_getifaddrs(&addrs) == -1) {
425  return (-1);
426  }
427 
428  for (list = addrs.next; list != &addrs; list = list->next) {
429  if_addr = list_entry(list, struct totem_ip_if_address, list);
430 
431  if (bindnet->family != if_addr->ip_addr.family)
432  continue ;
433 
434  addr_len = 0;
435 
436  switch (bindnet->family) {
437  case AF_INET:
438  addr_len = sizeof(struct in_addr);
439  break;
440  case AF_INET6:
441  addr_len = sizeof(struct in6_addr);
442  break;
443  }
444 
445  if (addr_len == 0)
446  continue ;
447 
448  totemip_copy(&bn_netaddr, bindnet);
449  totemip_copy(&if_netaddr, &if_addr->ip_addr);
450 
451  if (totemip_equal(&bn_netaddr, &if_netaddr)) {
452  exact_match_found = 1;
453  }
454 
455  for (si = 0; si < addr_len; si++) {
456  bn_netaddr.addr[si] = bn_netaddr.addr[si] & if_addr->mask_addr.addr[si];
457  if_netaddr.addr[si] = if_netaddr.addr[si] & if_addr->mask_addr.addr[si];
458  }
459 
460  if (exact_match_found || (!net_match_found && totemip_equal(&bn_netaddr, &if_netaddr))) {
461  totemip_copy(boundto, &if_addr->ip_addr);
462  boundto->nodeid = bindnet->nodeid;
463  *interface_up = if_addr->interface_up;
464  *interface_num = if_addr->interface_num;
465 
466  if (boundto->family == AF_INET && boundto->nodeid == 0) {
467  unsigned int nodeid = 0;
468  memcpy (&nodeid, boundto->addr, sizeof (int));
469 #if __BYTE_ORDER == __LITTLE_ENDIAN
470  nodeid = swab32 (nodeid);
471 #endif
472  if (mask_high_bit) {
473  nodeid &= 0x7FFFFFFF;
474  }
475  boundto->nodeid = nodeid;
476  }
477 
478  net_match_found = 1;
479  res = 0;
480 
481  if (exact_match_found) {
482  goto finished;
483  }
484  }
485  }
486 
487 finished:
488  totemip_freeifaddrs(&addrs);
489  return (res);
490 }
491 
492 #define TOTEMIP_UDP_HEADER_SIZE 8
493 #define TOTEMIP_IPV4_HEADER_SIZE 20
494 #define TOTEMIP_IPV6_HEADER_SIZE 40
495 
497 {
498  size_t header_size;
499 
500  header_size = 0;
501 
502  switch (family) {
503  case AF_INET:
505  break;
506  case AF_INET6:
508  break;
509  }
510 
511  return (header_size);
512 }
unsigned short family
Definition: coroapi.h:113
void totemip_freeifaddrs(struct list_head *addrs)
Definition: totemip.c:389
#define TOTEMIP_IPV4_HEADER_SIZE
Definition: totemip.c:493
struct list_head * next
Definition: list.h:47
The totem_ip_address struct.
Definition: coroapi.h:111
unsigned char addr[TOTEMIP_ADDRLEN]
Definition: coroapi.h:114
void totemip_copy(struct totem_ip_address *addr1, const struct totem_ip_address *addr2)
Definition: totemip.c:95
struct list_head list
Definition: totemip.h:77
unsigned char addr[TOTEMIP_ADDRLEN]
Definition: coroapi.h:77
int totemip_localhost(int family, struct totem_ip_address *localhost)
Definition: totemip.c:182
int totemip_parse(struct totem_ip_address *totemip, const char *addr, int family)
Definition: totemip.c:263
Definition: list.h:46
#define totemip_nosigpipe(s)
Definition: totemip.h:56
const char * totemip_print(const struct totem_ip_address *addr)
Definition: totemip.c:214
int32_t totemip_is_mcast(struct totem_ip_address *ip_addr)
Definition: totemip.c:114
int totemip_localhost_check(const struct totem_ip_address *addr)
Definition: totemip.c:205
int totemip_totemip_to_sockaddr_convert(struct totem_ip_address *ip_addr, uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
Definition: totemip.c:222
int totemip_iface_check(struct totem_ip_address *bindnet, struct totem_ip_address *boundto, int *interface_up, int *interface_num, int mask_high_bit)
Definition: totemip.c:405
unsigned int nodeid
Definition: coroapi.h:112
size_t totemip_udpip_header_size(int family)
Definition: totemip.c:496
int totemip_getifaddrs(struct list_head *addrs)
Definition: totemip.c:321
struct totem_ip_address mask_addr
Definition: totemip.h:73
#define LOCALHOST_IPV4
Definition: totemip.c:57
int totemip_compare(const void *a, const void *b)
Definition: totemip.c:130
#define swab32(x)
The swab32 macro.
Definition: swab.h:51
#define TOTEMIP_ADDRLEN
Definition: coroapi.h:86
#define LOCALHOST_IPV6
Definition: totemip.c:58
int totemip_equal(const struct totem_ip_address *addr1, const struct totem_ip_address *addr2)
Definition: totemip.c:71
#define swab16(x)
The swab16 macro.
Definition: swab.h:39
unsigned short family
Definition: coroapi.h:76
int totemip_sockaddr_to_totemip_convert(const struct sockaddr_storage *saddr, struct totem_ip_address *ip_addr)
Definition: totemip.c:295
#define TOTEMIP_IPV6_HEADER_SIZE
Definition: totemip.c:494
#define list_entry(ptr, type, member)
Definition: list.h:84
#define TOTEMIP_UDP_HEADER_SIZE
Definition: totemip.c:492
unsigned int nodeid
Definition: coroapi.h:75
struct totem_ip_address ip_addr
Definition: totemip.h:72
void totemip_copy_endian_convert(struct totem_ip_address *addr1, const struct totem_ip_address *addr2)
Definition: totemip.c:101