corosync  2.4.5
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 int totemip_sa_equal(const struct totem_ip_address *totem_ip,
95  const struct sockaddr *sa)
96 {
97  int res;
98 
99  res = 0;
100 
101  if (totem_ip->family != sa->sa_family) {
102  return (res);
103  }
104 
105  switch (totem_ip->family) {
106  case AF_INET:
107  res = (memcmp(totem_ip->addr,
108  &((const struct sockaddr_in *)sa)->sin_addr, sizeof(struct in_addr)) == 0);
109  break;
110  case AF_INET6:
111  res = (memcmp(totem_ip->addr,
112  &((const struct sockaddr_in6 *)sa)->sin6_addr, sizeof(struct in6_addr)) == 0);
113  break;
114  default:
115  assert(0);
116  }
117 
118  return (res);
119 }
120 
121 /* Copy a totem_ip_address */
122 void totemip_copy(struct totem_ip_address *addr1,
123  const struct totem_ip_address *addr2)
124 {
125  memcpy(addr1, addr2, sizeof(struct totem_ip_address));
126 }
127 
129  const struct totem_ip_address *addr2)
130 {
131  addr1->nodeid = swab32(addr2->nodeid);
132  addr1->family = swab16(addr2->family);
133  memcpy(addr1->addr, addr2->addr, TOTEMIP_ADDRLEN);
134 }
135 
136 /*
137  * Multicast address range is 224.0.0.0 to 239.255.255.255 this
138  * translates to the first 4 bits == 1110 (0xE).
139  * http://en.wikipedia.org/wiki/Multicast_address
140  */
141 int32_t totemip_is_mcast(struct totem_ip_address *ip_addr)
142 {
143  uint32_t addr = 0;
144 
145  memcpy (&addr, ip_addr->addr, sizeof (uint32_t));
146 
147  if (ip_addr->family == AF_INET) {
148  addr = ntohl(addr);
149  if ((addr >> 28) != 0xE) {
150  return -1;
151  }
152  }
153  return 0;
154 }
155 
156 /* For sorting etc. params are void * for qsort's benefit */
157 int totemip_compare(const void *a, const void *b)
158 {
159  int i;
160  const struct totem_ip_address *totemip_a = (const struct totem_ip_address *)a;
161  const struct totem_ip_address *totemip_b = (const struct totem_ip_address *)b;
162  struct in_addr ipv4_a1;
163  struct in_addr ipv4_a2;
164  struct in6_addr ipv6_a1;
165  struct in6_addr ipv6_a2;
166  unsigned short family;
167 
168  /*
169  * Use memcpy to align since totem_ip_address is unaligned on various archs
170  */
171  memcpy (&family, &totemip_a->family, sizeof (unsigned short));
172 
173  if (family == AF_INET) {
174  memcpy (&ipv4_a1, totemip_a->addr, sizeof (struct in_addr));
175  memcpy (&ipv4_a2, totemip_b->addr, sizeof (struct in_addr));
176  if (ipv4_a1.s_addr == ipv4_a2.s_addr) {
177  return (0);
178  }
179  if (htonl(ipv4_a1.s_addr) < htonl(ipv4_a2.s_addr)) {
180  return -1;
181  } else {
182  return +1;
183  }
184  } else
185  if (family == AF_INET6) {
186  /*
187  * We can only compare 8 bits at time for portability reasons
188  */
189  memcpy (&ipv6_a1, totemip_a->addr, sizeof (struct in6_addr));
190  memcpy (&ipv6_a2, totemip_b->addr, sizeof (struct in6_addr));
191  for (i = 0; i < 16; i++) {
192  int res = ipv6_a1.s6_addr[i] -
193  ipv6_a2.s6_addr[i];
194  if (res) {
195  return res;
196  }
197  }
198  return 0;
199  } else {
200  /*
201  * Family not set, should be!
202  */
203  assert (0);
204  }
205  return 0;
206 }
207 
208 /* Build a localhost totem_ip_address */
209 int totemip_localhost(int family, struct totem_ip_address *localhost)
210 {
211  const char *addr_text;
212 
213  memset (localhost, 0, sizeof (struct totem_ip_address));
214 
215  if (family == AF_INET) {
216  addr_text = LOCALHOST_IPV4;
217  if (inet_pton(family, addr_text, (char *)&localhost->nodeid) <= 0) {
218  return -1;
219  }
220  } else {
221  addr_text = LOCALHOST_IPV6;
222  }
223 
224  if (inet_pton(family, addr_text, (char *)localhost->addr) <= 0)
225  return -1;
226 
227  localhost->family = family;
228 
229  return 0;
230 }
231 
233 {
234  struct totem_ip_address localhost;
235 
236  if (totemip_localhost(addr->family, &localhost))
237  return 0;
238  return totemip_equal(addr, &localhost);
239 }
240 
241 const char *totemip_sa_print(const struct sockaddr *sa)
242 {
243  static char buf[INET6_ADDRSTRLEN];
244 
245  buf[0] = 0;
246 
247  switch (sa->sa_family) {
248  case AF_INET:
249  inet_ntop(sa->sa_family, &((struct sockaddr_in *)(sa))->sin_addr, buf,
250  INET6_ADDRSTRLEN);
251  break;
252  case AF_INET6:
253  inet_ntop(sa->sa_family, &((struct sockaddr_in6 *)(sa))->sin6_addr, buf,
254  INET6_ADDRSTRLEN);
255  break;
256  default:
257  return (NULL);
258  }
259 
260  return (buf);
261 }
262 
263 const char *totemip_print(const struct totem_ip_address *addr)
264 {
265  static char buf[INET6_ADDRSTRLEN];
266 
267  return (inet_ntop(addr->family, addr->addr, buf, sizeof(buf)));
268 }
269 
270 /* Make a totem_ip_address into a usable sockaddr_storage */
272  uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
273 {
274  int ret = -1;
275 
276  if (ip_addr->family == AF_INET) {
277  struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
278 
279  memset(sin, 0, sizeof(struct sockaddr_in));
280 #ifdef HAVE_SOCK_SIN_LEN
281  sin->sin_len = sizeof(struct sockaddr_in);
282 #endif
283  sin->sin_family = ip_addr->family;
284  sin->sin_port = ntohs(port);
285  memcpy(&sin->sin_addr, ip_addr->addr, sizeof(struct in_addr));
286  *addrlen = sizeof(struct sockaddr_in);
287  ret = 0;
288  }
289 
290  if (ip_addr->family == AF_INET6) {
291  struct sockaddr_in6 *sin = (struct sockaddr_in6 *)saddr;
292 
293  memset(sin, 0, sizeof(struct sockaddr_in6));
294 #ifdef HAVE_SOCK_SIN6_LEN
295  sin->sin6_len = sizeof(struct sockaddr_in6);
296 #endif
297  sin->sin6_family = ip_addr->family;
298  sin->sin6_port = ntohs(port);
299  sin->sin6_scope_id = 2;
300  memcpy(&sin->sin6_addr, ip_addr->addr, sizeof(struct in6_addr));
301 
302  *addrlen = sizeof(struct sockaddr_in6);
303  ret = 0;
304  }
305 
306  return ret;
307 }
308 
309 /* Converts an address string string into a totem_ip_address.
310  family can be AF_INET, AF_INET6 or 0 ("for "don't care")
311 */
312 int totemip_parse(struct totem_ip_address *totemip, const char *addr, int family)
313 {
314  struct addrinfo *ainfo;
315  struct addrinfo ahints;
316  struct sockaddr_in *sa;
317  struct sockaddr_in6 *sa6;
318  int ret;
319 
320  memset(&ahints, 0, sizeof(ahints));
321  ahints.ai_socktype = SOCK_DGRAM;
322  ahints.ai_protocol = IPPROTO_UDP;
323  ahints.ai_family = family;
324 
325  /* Lookup the nodename address */
326  ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
327  if (ret)
328  return -1;
329 
330  sa = (struct sockaddr_in *)ainfo->ai_addr;
331  sa6 = (struct sockaddr_in6 *)ainfo->ai_addr;
332  totemip->family = ainfo->ai_family;
333 
334  if (ainfo->ai_family == AF_INET)
335  memcpy(totemip->addr, &sa->sin_addr, sizeof(struct in_addr));
336  else
337  memcpy(totemip->addr, &sa6->sin6_addr, sizeof(struct in6_addr));
338 
339  freeaddrinfo(ainfo);
340  return 0;
341 }
342 
343 /* Make a sockaddr_* into a totem_ip_address */
344 int totemip_sockaddr_to_totemip_convert(const struct sockaddr_storage *saddr,
345  struct totem_ip_address *ip_addr)
346 {
347  int ret = -1;
348 
349  ip_addr->family = saddr->ss_family;
350  ip_addr->nodeid = 0;
351 
352  if (saddr->ss_family == AF_INET) {
353  const struct sockaddr_in *sin = (const struct sockaddr_in *)saddr;
354 
355  memcpy(ip_addr->addr, &sin->sin_addr, sizeof(struct in_addr));
356  ret = 0;
357  }
358 
359  if (saddr->ss_family == AF_INET6) {
360  const struct sockaddr_in6 *sin
361  = (const struct sockaddr_in6 *)saddr;
362 
363  memcpy(ip_addr->addr, &sin->sin6_addr, sizeof(struct in6_addr));
364 
365  ret = 0;
366  }
367  return ret;
368 }
369 
370 int totemip_getifaddrs(struct list_head *addrs)
371 {
372  struct ifaddrs *ifap, *ifa;
373  struct totem_ip_if_address *if_addr;
374 
375  if (getifaddrs(&ifap) != 0)
376  return (-1);
377 
378  list_init(addrs);
379 
380  for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
381  if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
382  continue ;
383 
384  if ((ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) ||
385  (ifa->ifa_netmask->sa_family != AF_INET && ifa->ifa_netmask->sa_family != AF_INET6 &&
386  ifa->ifa_netmask->sa_family != 0))
387  continue ;
388 
389  if (ifa->ifa_netmask->sa_family == 0) {
390  ifa->ifa_netmask->sa_family = ifa->ifa_addr->sa_family;
391  }
392 
393  if_addr = malloc(sizeof(struct totem_ip_if_address));
394  if (if_addr == NULL) {
395  goto error_free_ifaddrs;
396  }
397 
398  list_init(&if_addr->list);
399 
400  memset(if_addr, 0, sizeof(struct totem_ip_if_address));
401 
402  if_addr->interface_up = ifa->ifa_flags & IFF_UP;
403  if_addr->interface_num = if_nametoindex(ifa->ifa_name);
404  if_addr->name = strdup(ifa->ifa_name);
405  if (if_addr->name == NULL) {
406  goto error_free_addr;
407  }
408 
409  if (totemip_sockaddr_to_totemip_convert((const struct sockaddr_storage *)ifa->ifa_addr,
410  &if_addr->ip_addr) == -1) {
411  goto error_free_addr_name;
412  }
413 
414  if (totemip_sockaddr_to_totemip_convert((const struct sockaddr_storage *)ifa->ifa_netmask,
415  &if_addr->mask_addr) == -1) {
416  goto error_free_addr_name;
417  }
418 
419  list_add_tail(&if_addr->list, addrs);
420  }
421 
422  freeifaddrs(ifap);
423 
424  return (0);
425 
426 error_free_addr_name:
427  free(if_addr->name);
428 
429 error_free_addr:
430  free(if_addr);
431 
432 error_free_ifaddrs:
433  totemip_freeifaddrs(addrs);
434  freeifaddrs(ifap);
435  return (-1);
436 }
437 
438 void totemip_freeifaddrs(struct list_head *addrs)
439 {
440  struct totem_ip_if_address *if_addr;
441  struct list_head *list;
442 
443  for (list = addrs->next; list != addrs;) {
444  if_addr = list_entry(list, struct totem_ip_if_address, list);
445  list = list->next;
446 
447  free(if_addr->name);
448  list_del(&if_addr->list);
449  free(if_addr);
450  }
451  list_init(addrs);
452 }
453 
455  struct totem_ip_address *boundto,
456  int *interface_up,
457  int *interface_num,
458  int mask_high_bit)
459 {
460  struct list_head addrs;
461  struct list_head *list;
462  struct totem_ip_if_address *if_addr;
463  struct totem_ip_address bn_netaddr, if_netaddr;
464  socklen_t addr_len;
465  socklen_t si;
466  int res = -1;
467  int exact_match_found = 0;
468  int net_match_found = 0;
469 
470  *interface_up = 0;
471  *interface_num = 0;
472 
473  if (totemip_getifaddrs(&addrs) == -1) {
474  return (-1);
475  }
476 
477  for (list = addrs.next; list != &addrs; list = list->next) {
478  if_addr = list_entry(list, struct totem_ip_if_address, list);
479 
480  if (bindnet->family != if_addr->ip_addr.family)
481  continue ;
482 
483  addr_len = 0;
484 
485  switch (bindnet->family) {
486  case AF_INET:
487  addr_len = sizeof(struct in_addr);
488  break;
489  case AF_INET6:
490  addr_len = sizeof(struct in6_addr);
491  break;
492  }
493 
494  if (addr_len == 0)
495  continue ;
496 
497  totemip_copy(&bn_netaddr, bindnet);
498  totemip_copy(&if_netaddr, &if_addr->ip_addr);
499 
500  if (totemip_equal(&bn_netaddr, &if_netaddr)) {
501  exact_match_found = 1;
502  }
503 
504  for (si = 0; si < addr_len; si++) {
505  bn_netaddr.addr[si] = bn_netaddr.addr[si] & if_addr->mask_addr.addr[si];
506  if_netaddr.addr[si] = if_netaddr.addr[si] & if_addr->mask_addr.addr[si];
507  }
508 
509  if (exact_match_found || (!net_match_found && totemip_equal(&bn_netaddr, &if_netaddr))) {
510  totemip_copy(boundto, &if_addr->ip_addr);
511  boundto->nodeid = bindnet->nodeid;
512  *interface_up = if_addr->interface_up;
513  *interface_num = if_addr->interface_num;
514 
515  if (boundto->family == AF_INET && boundto->nodeid == 0) {
516  unsigned int nodeid = 0;
517  memcpy (&nodeid, boundto->addr, sizeof (int));
518 #if __BYTE_ORDER == __LITTLE_ENDIAN
519  nodeid = swab32 (nodeid);
520 #endif
521  if (mask_high_bit) {
522  nodeid &= 0x7FFFFFFF;
523  }
524  boundto->nodeid = nodeid;
525  }
526 
527  net_match_found = 1;
528  res = 0;
529 
530  if (exact_match_found) {
531  goto finished;
532  }
533  }
534  }
535 
536 finished:
537  totemip_freeifaddrs(&addrs);
538  return (res);
539 }
540 
541 #define TOTEMIP_UDP_HEADER_SIZE 8
542 #define TOTEMIP_IPV4_HEADER_SIZE 20
543 #define TOTEMIP_IPV6_HEADER_SIZE 40
544 
546 {
547  size_t header_size;
548 
549  header_size = 0;
550 
551  switch (family) {
552  case AF_INET:
554  break;
555  case AF_INET6:
557  break;
558  }
559 
560  return (header_size);
561 }
unsigned short family
Definition: coroapi.h:113
void totemip_freeifaddrs(struct list_head *addrs)
Definition: totemip.c:438
#define TOTEMIP_IPV4_HEADER_SIZE
Definition: totemip.c:542
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:122
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:209
int totemip_parse(struct totem_ip_address *totemip, const char *addr, int family)
Definition: totemip.c:312
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:263
int32_t totemip_is_mcast(struct totem_ip_address *ip_addr)
Definition: totemip.c:141
int totemip_localhost_check(const struct totem_ip_address *addr)
Definition: totemip.c:232
int totemip_totemip_to_sockaddr_convert(struct totem_ip_address *ip_addr, uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
Definition: totemip.c:271
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:454
unsigned int nodeid
Definition: coroapi.h:112
size_t totemip_udpip_header_size(int family)
Definition: totemip.c:545
const char * totemip_sa_print(const struct sockaddr *sa)
Definition: totemip.c:241
int totemip_getifaddrs(struct list_head *addrs)
Definition: totemip.c:370
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:157
#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:344
#define TOTEMIP_IPV6_HEADER_SIZE
Definition: totemip.c:543
#define list_entry(ptr, type, member)
Definition: list.h:84
int totemip_sa_equal(const struct totem_ip_address *totem_ip, const struct sockaddr *sa)
Definition: totemip.c:94
#define TOTEMIP_UDP_HEADER_SIZE
Definition: totemip.c:541
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:128