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