corosync  2.3.5
totemudp.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005 MontaVista Software, Inc.
3  * Copyright (c) 2006-2012 Red Hat, Inc.
4  *
5  * All rights reserved.
6  *
7  * Author: Steven Dake (sdake@redhat.com)
8 
9  * This software licensed under BSD license, the text of which follows:
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *
14  * - Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  * - Redistributions in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  * - Neither the name of the MontaVista Software, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived from this
21  * software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33  * THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <config.h>
37 
38 #include <assert.h>
39 #include <pthread.h>
40 #include <sys/mman.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/socket.h>
44 #include <netdb.h>
45 #include <sys/un.h>
46 #include <sys/ioctl.h>
47 #include <sys/param.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <stdlib.h>
53 #include <stdio.h>
54 #include <errno.h>
55 #include <sched.h>
56 #include <time.h>
57 #include <sys/time.h>
58 #include <sys/poll.h>
59 #include <sys/uio.h>
60 #include <limits.h>
61 
62 #include <corosync/sq.h>
63 #include <corosync/swab.h>
64 #include <corosync/list.h>
65 #include <qb/qbdefs.h>
66 #include <qb/qbloop.h>
67 #define LOGSYS_UTILS_ONLY 1
68 #include <corosync/logsys.h>
69 #include "totemudp.h"
70 
71 #include "util.h"
72 #include "totemcrypto.h"
73 
74 #include <nss.h>
75 #include <pk11pub.h>
76 #include <pkcs11.h>
77 #include <prerror.h>
78 
79 #ifndef MSG_NOSIGNAL
80 #define MSG_NOSIGNAL 0
81 #endif
82 
83 #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
84 #define NETIF_STATE_REPORT_UP 1
85 #define NETIF_STATE_REPORT_DOWN 2
86 
87 #define BIND_STATE_UNBOUND 0
88 #define BIND_STATE_REGULAR 1
89 #define BIND_STATE_LOOPBACK 2
90 
91 #define MESSAGE_TYPE_MEMB_JOIN 3
92 
96  int token;
97  /*
98  * Socket used for local multicast delivery. We don't rely on multicast
99  * loop and rather this UNIX DGRAM socket is used. Socket is created by
100  * socketpair call and they are used in same way as pipe (so [0] is read
101  * end and [1] is write end)
102  */
104 };
105 
108 
110 
112 
114 
116 
117  void *context;
118 
119  void (*totemudp_deliver_fn) (
120  void *context,
121  const void *msg,
122  unsigned int msg_len);
123 
124  void (*totemudp_iface_change_fn) (
125  void *context,
126  const struct totem_ip_address *iface_address);
127 
128  void (*totemudp_target_set_completed) (void *context);
129 
130  /*
131  * Function and data used to log messages
132  */
134 
136 
138 
140 
142 
144 
145  void (*totemudp_log_printf) (
146  int level,
147  int subsys,
148  const char *function,
149  const char *file,
150  int line,
151  const char *format,
152  ...)__attribute__((format(printf, 6, 7)));
153 
154  void *udp_context;
155 
156  char iov_buffer[FRAME_SIZE_MAX];
157 
158  char iov_buffer_flush[FRAME_SIZE_MAX];
159 
160  struct iovec totemudp_iov_recv;
161 
162  struct iovec totemudp_iov_recv_flush;
163 
164  struct totemudp_socket totemudp_sockets;
165 
166  struct totem_ip_address mcast_address;
167 
169 
171 
173 
175 
177 
178  struct timeval stats_tv_start;
179 
180  struct totem_ip_address my_id;
181 
182  int firstrun;
183 
184  qb_loop_timer_handle timer_netif_check_timeout;
185 
186  unsigned int my_memb_entries;
187 
188  int flushing;
189 
191 
193 
194  struct totem_ip_address token_target;
195 };
196 
197 struct work_item {
198  const void *msg;
199  unsigned int msg_len;
201 };
202 
203 static int totemudp_build_sockets (
204  struct totemudp_instance *instance,
205  struct totem_ip_address *bindnet_address,
206  struct totem_ip_address *mcastaddress,
207  struct totemudp_socket *sockets,
208  struct totem_ip_address *bound_to);
209 
210 static struct totem_ip_address localhost;
211 
212 static void totemudp_instance_initialize (struct totemudp_instance *instance)
213 {
214  memset (instance, 0, sizeof (struct totemudp_instance));
215 
217 
218  instance->totemudp_iov_recv.iov_base = instance->iov_buffer;
219 
220  instance->totemudp_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
221  instance->totemudp_iov_recv_flush.iov_base = instance->iov_buffer_flush;
222 
223  instance->totemudp_iov_recv_flush.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
224 
225  /*
226  * There is always atleast 1 processor
227  */
228  instance->my_memb_entries = 1;
229 }
230 
231 #define log_printf(level, format, args...) \
232 do { \
233  instance->totemudp_log_printf ( \
234  level, instance->totemudp_subsys_id, \
235  __FUNCTION__, __FILE__, __LINE__, \
236  (const char *)format, ##args); \
237 } while (0);
238 
239 #define LOGSYS_PERROR(err_num, level, fmt, args...) \
240 do { \
241  char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
242  const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
243  instance->totemudp_log_printf ( \
244  level, instance->totemudp_subsys_id, \
245  __FUNCTION__, __FILE__, __LINE__, \
246  fmt ": %s (%d)\n", ##args, _error_ptr, err_num); \
247  } while(0)
248 
250  void *udp_context,
251  const char *cipher_type,
252  const char *hash_type)
253 {
254 
255  return (0);
256 }
257 
258 
259 static inline void ucast_sendmsg (
260  struct totemudp_instance *instance,
261  struct totem_ip_address *system_to,
262  const void *msg,
263  unsigned int msg_len)
264 {
265  struct msghdr msg_ucast;
266  int res = 0;
267  size_t buf_out_len;
268  unsigned char buf_out[FRAME_SIZE_MAX];
269  struct sockaddr_storage sockaddr;
270  struct iovec iovec;
271  int addrlen;
272 
273  if (msg_len + crypto_get_current_sec_header_size(instance->crypto_inst) > sizeof(buf_out)) {
274  log_printf(LOGSYS_LEVEL_CRIT, "UDP message for ucast is too big. Ignoring message");
275 
276  return ;
277  }
278 
279  /*
280  * Encrypt and digest the message
281  */
283  instance->crypto_inst,
284  (const unsigned char *)msg,
285  msg_len,
286  buf_out,
287  &buf_out_len) != 0) {
288  log_printf(LOGSYS_LEVEL_CRIT, "Error encrypting/signing packet (non-critical)");
289  return;
290  }
291 
292  iovec.iov_base = (void *)buf_out;
293  iovec.iov_len = buf_out_len;
294 
295  /*
296  * Build unicast message
297  */
298  memset(&msg_ucast, 0, sizeof(msg_ucast));
300  instance->totem_interface->ip_port, &sockaddr, &addrlen);
301  msg_ucast.msg_name = &sockaddr;
302  msg_ucast.msg_namelen = addrlen;
303  msg_ucast.msg_iov = (void *)&iovec;
304  msg_ucast.msg_iovlen = 1;
305 #ifdef HAVE_MSGHDR_CONTROL
306  msg_ucast.msg_control = 0;
307 #endif
308 #ifdef HAVE_MSGHDR_CONTROLLEN
309  msg_ucast.msg_controllen = 0;
310 #endif
311 #ifdef HAVE_MSGHDR_FLAGS
312  msg_ucast.msg_flags = 0;
313 #endif
314 #ifdef HAVE_MSGHDR_ACCRIGHTS
315  msg_ucast.msg_accrights = NULL;
316 #endif
317 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
318  msg_ucast.msg_accrightslen = 0;
319 #endif
320 
321 
322  /*
323  * Transmit unicast message
324  * An error here is recovered by totemsrp
325  */
326  res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_ucast,
327  MSG_NOSIGNAL);
328  if (res < 0) {
329  LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
330  "sendmsg(ucast) failed (non-critical)");
331  }
332 }
333 
334 static inline void mcast_sendmsg (
335  struct totemudp_instance *instance,
336  const void *msg,
337  unsigned int msg_len)
338 {
339  struct msghdr msg_mcast;
340  int res = 0;
341  size_t buf_out_len;
342  unsigned char buf_out[FRAME_SIZE_MAX];
343  struct iovec iovec;
344  struct sockaddr_storage sockaddr;
345  int addrlen;
346 
347  if (msg_len + crypto_get_current_sec_header_size(instance->crypto_inst) > sizeof(buf_out)) {
348  log_printf(LOGSYS_LEVEL_CRIT, "UDP message for mcast is too big. Ignoring message");
349 
350  return ;
351  }
352 
353  /*
354  * Encrypt and digest the message
355  */
357  instance->crypto_inst,
358  (const unsigned char *)msg,
359  msg_len,
360  buf_out,
361  &buf_out_len) != 0) {
362  log_printf(LOGSYS_LEVEL_CRIT, "Error encrypting/signing packet (non-critical)");
363  return;
364  }
365 
366  iovec.iov_base = (void *)&buf_out;
367  iovec.iov_len = buf_out_len;
368 
369  /*
370  * Build multicast message
371  */
373  instance->totem_interface->ip_port, &sockaddr, &addrlen);
374  memset(&msg_mcast, 0, sizeof(msg_mcast));
375  msg_mcast.msg_name = &sockaddr;
376  msg_mcast.msg_namelen = addrlen;
377  msg_mcast.msg_iov = (void *)&iovec;
378  msg_mcast.msg_iovlen = 1;
379 #ifdef HAVE_MSGHDR_CONTROL
380  msg_mcast.msg_control = 0;
381 #endif
382 #ifdef HAVE_MSGHDR_CONTROLLEN
383  msg_mcast.msg_controllen = 0;
384 #endif
385 #ifdef HAVE_MSGHDR_FLAGS
386  msg_mcast.msg_flags = 0;
387 #endif
388 #ifdef HAVE_MSGHDR_ACCRIGHTS
389  msg_mcast.msg_accrights = NULL;
390 #endif
391 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
392  msg_mcast.msg_accrightslen = 0;
393 #endif
394 
395  /*
396  * Transmit multicast message
397  * An error here is recovered by totemsrp
398  */
399  res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_mcast,
400  MSG_NOSIGNAL);
401  if (res < 0) {
402  LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
403  "sendmsg(mcast) failed (non-critical)");
404  instance->stats->continuous_sendmsg_failures++;
405  } else {
406  instance->stats->continuous_sendmsg_failures = 0;
407  }
408 
409  /*
410  * Transmit multicast message to local unix mcast loop
411  * An error here is recovered by totemsrp
412  */
413  msg_mcast.msg_name = NULL;
414  msg_mcast.msg_namelen = 0;
415 
416  res = sendmsg (instance->totemudp_sockets.local_mcast_loop[1], &msg_mcast,
417  MSG_NOSIGNAL);
418  if (res < 0) {
419  LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
420  "sendmsg(local mcast loop) failed (non-critical)");
421  }
422 }
423 
424 
426  void *udp_context)
427 {
428  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
429  int res = 0;
430 
431  if (instance->totemudp_sockets.mcast_recv > 0) {
432  qb_loop_poll_del (instance->totemudp_poll_handle,
433  instance->totemudp_sockets.mcast_recv);
434  close (instance->totemudp_sockets.mcast_recv);
435  }
436  if (instance->totemudp_sockets.mcast_send > 0) {
437  close (instance->totemudp_sockets.mcast_send);
438  }
439  if (instance->totemudp_sockets.local_mcast_loop[0] > 0) {
440  qb_loop_poll_del (instance->totemudp_poll_handle,
441  instance->totemudp_sockets.local_mcast_loop[0]);
442  close (instance->totemudp_sockets.local_mcast_loop[0]);
443  close (instance->totemudp_sockets.local_mcast_loop[1]);
444  }
445  if (instance->totemudp_sockets.token > 0) {
446  qb_loop_poll_del (instance->totemudp_poll_handle,
447  instance->totemudp_sockets.token);
448  close (instance->totemudp_sockets.token);
449  }
450 
451  return (res);
452 }
453 
454 /*
455  * Only designed to work with a message with one iov
456  */
457 
458 static int net_deliver_fn (
459  int fd,
460  int revents,
461  void *data)
462 {
463  struct totemudp_instance *instance = (struct totemudp_instance *)data;
464  struct msghdr msg_recv;
465  struct iovec *iovec;
466  struct sockaddr_storage system_from;
467  int bytes_received;
468  int res = 0;
469  char *message_type;
470 
471  if (instance->flushing == 1) {
472  iovec = &instance->totemudp_iov_recv_flush;
473  } else {
474  iovec = &instance->totemudp_iov_recv;
475  }
476 
477  /*
478  * Receive datagram
479  */
480  msg_recv.msg_name = &system_from;
481  msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
482  msg_recv.msg_iov = iovec;
483  msg_recv.msg_iovlen = 1;
484 #ifdef HAVE_MSGHDR_CONTROL
485  msg_recv.msg_control = 0;
486 #endif
487 #ifdef HAVE_MSGHDR_CONTROLLEN
488  msg_recv.msg_controllen = 0;
489 #endif
490 #ifdef HAVE_MSGHDR_FLAGS
491  msg_recv.msg_flags = 0;
492 #endif
493 #ifdef HAVE_MSGHDR_ACCRIGHTS
494  msg_recv.msg_accrights = NULL;
495 #endif
496 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
497  msg_recv.msg_accrightslen = 0;
498 #endif
499 
500  bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
501  if (bytes_received == -1) {
502  return (0);
503  } else {
504  instance->stats_recv += bytes_received;
505  }
506 
507  /*
508  * Authenticate and if authenticated, decrypt datagram
509  */
510  res = crypto_authenticate_and_decrypt (instance->crypto_inst, iovec->iov_base, &bytes_received);
511  if (res == -1) {
512  log_printf (instance->totemudp_log_level_security, "Received message has invalid digest... ignoring.");
514  "Invalid packet data");
515  iovec->iov_len = FRAME_SIZE_MAX;
516  return 0;
517  }
518  iovec->iov_len = bytes_received;
519 
520  /*
521  * Drop all non-mcast messages (more specifically join
522  * messages should be dropped)
523  */
524  message_type = (char *)iovec->iov_base;
525  if (instance->flushing == 1 && *message_type == MESSAGE_TYPE_MEMB_JOIN) {
526  log_printf(instance->totemudp_log_level_warning, "JOIN or LEAVE message was thrown away during flush operation.");
527  iovec->iov_len = FRAME_SIZE_MAX;
528  return (0);
529  }
530 
531  /*
532  * Handle incoming message
533  */
534  instance->totemudp_deliver_fn (
535  instance->context,
536  iovec->iov_base,
537  iovec->iov_len);
538 
539  iovec->iov_len = FRAME_SIZE_MAX;
540  return (0);
541 }
542 
543 static int netif_determine (
544  struct totemudp_instance *instance,
545  struct totem_ip_address *bindnet,
546  struct totem_ip_address *bound_to,
547  int *interface_up,
548  int *interface_num)
549 {
550  int res;
551 
552  res = totemip_iface_check (bindnet, bound_to,
553  interface_up, interface_num,
554  instance->totem_config->clear_node_high_bit);
555 
556 
557  return (res);
558 }
559 
560 
561 /*
562  * If the interface is up, the sockets for totem are built. If the interface is down
563  * this function is requeued in the timer list to retry building the sockets later.
564  */
565 static void timer_function_netif_check_timeout (
566  void *data)
567 {
568  struct totemudp_instance *instance = (struct totemudp_instance *)data;
569  int interface_up;
570  int interface_num;
571  struct totem_ip_address *bind_address;
572 
573  /*
574  * Build sockets for every interface
575  */
576  netif_determine (instance,
577  &instance->totem_interface->bindnet,
578  &instance->totem_interface->boundto,
579  &interface_up, &interface_num);
580  /*
581  * If the network interface isn't back up and we are already
582  * in loopback mode, add timer to check again and return
583  */
584  if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
585  interface_up == 0) ||
586 
587  (instance->my_memb_entries == 1 &&
588  instance->netif_bind_state == BIND_STATE_REGULAR &&
589  interface_up == 1)) {
590 
591  qb_loop_timer_add (instance->totemudp_poll_handle,
592  QB_LOOP_MED,
593  instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
594  (void *)instance,
595  timer_function_netif_check_timeout,
596  &instance->timer_netif_check_timeout);
597 
598  /*
599  * Add a timer to check for a downed regular interface
600  */
601  return;
602  }
603 
604  if (instance->totemudp_sockets.mcast_recv > 0) {
605  qb_loop_poll_del (instance->totemudp_poll_handle,
606  instance->totemudp_sockets.mcast_recv);
607  close (instance->totemudp_sockets.mcast_recv);
608  }
609  if (instance->totemudp_sockets.mcast_send > 0) {
610  close (instance->totemudp_sockets.mcast_send);
611  }
612  if (instance->totemudp_sockets.local_mcast_loop[0] > 0) {
613  qb_loop_poll_del (instance->totemudp_poll_handle,
614  instance->totemudp_sockets.local_mcast_loop[0]);
615  close (instance->totemudp_sockets.local_mcast_loop[0]);
616  close (instance->totemudp_sockets.local_mcast_loop[1]);
617  }
618  if (instance->totemudp_sockets.token > 0) {
619  qb_loop_poll_del (instance->totemudp_poll_handle,
620  instance->totemudp_sockets.token);
621  close (instance->totemudp_sockets.token);
622  }
623 
624  if (interface_up == 0) {
625  /*
626  * Interface is not up
627  */
629  bind_address = &localhost;
630 
631  /*
632  * Add a timer to retry building interfaces and request memb_gather_enter
633  */
634  qb_loop_timer_add (instance->totemudp_poll_handle,
635  QB_LOOP_MED,
636  instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
637  (void *)instance,
638  timer_function_netif_check_timeout,
639  &instance->timer_netif_check_timeout);
640  } else {
641  /*
642  * Interface is up
643  */
645  bind_address = &instance->totem_interface->bindnet;
646  }
647  /*
648  * Create and bind the multicast and unicast sockets
649  */
650  (void)totemudp_build_sockets (instance,
651  &instance->mcast_address,
652  bind_address,
653  &instance->totemudp_sockets,
654  &instance->totem_interface->boundto);
655 
656  qb_loop_poll_add (
657  instance->totemudp_poll_handle,
658  QB_LOOP_MED,
659  instance->totemudp_sockets.mcast_recv,
660  POLLIN, instance, net_deliver_fn);
661 
662  qb_loop_poll_add (
663  instance->totemudp_poll_handle,
664  QB_LOOP_MED,
665  instance->totemudp_sockets.local_mcast_loop[0],
666  POLLIN, instance, net_deliver_fn);
667 
668  qb_loop_poll_add (
669  instance->totemudp_poll_handle,
670  QB_LOOP_MED,
671  instance->totemudp_sockets.token,
672  POLLIN, instance, net_deliver_fn);
673 
674  totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
675 
676  /*
677  * This reports changes in the interface to the user and totemsrp
678  */
679  if (instance->netif_bind_state == BIND_STATE_REGULAR) {
680  if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
682  "The network interface [%s] is now up.",
683  totemip_print (&instance->totem_interface->boundto));
685  instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
686  }
687  /*
688  * Add a timer to check for interface going down in single membership
689  */
690  if (instance->my_memb_entries == 1) {
691  qb_loop_timer_add (instance->totemudp_poll_handle,
692  QB_LOOP_MED,
693  instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
694  (void *)instance,
695  timer_function_netif_check_timeout,
696  &instance->timer_netif_check_timeout);
697  }
698 
699  } else {
702  "The network interface is down.");
703  instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
704  }
706 
707  }
708 }
709 
710 /* Set the socket priority to INTERACTIVE to ensure
711  that our messages don't get queued behind anything else */
712 static void totemudp_traffic_control_set(struct totemudp_instance *instance, int sock)
713 {
714 #ifdef SO_PRIORITY
715  int prio = 6; /* TC_PRIO_INTERACTIVE */
716 
717  if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
718  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning, "Could not set traffic priority");
719  }
720 #endif
721 }
722 
723 static int totemudp_build_sockets_ip (
724  struct totemudp_instance *instance,
725  struct totem_ip_address *mcast_address,
726  struct totem_ip_address *bindnet_address,
727  struct totemudp_socket *sockets,
728  struct totem_ip_address *bound_to,
729  int interface_num)
730 {
731  struct sockaddr_storage sockaddr;
732  struct ipv6_mreq mreq6;
733  struct ip_mreq mreq;
734  struct sockaddr_storage mcast_ss, boundto_ss;
735  struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
736  struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
737  struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
738  unsigned int sendbuf_size;
739  unsigned int recvbuf_size;
740  unsigned int optlen = sizeof (sendbuf_size);
741  int addrlen;
742  int res;
743  int flag;
744  uint8_t sflag;
745  int i;
746 
747  /*
748  * Create multicast recv socket
749  */
750  sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
751  if (sockets->mcast_recv == -1) {
752  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
753  "socket() failed");
754  return (-1);
755  }
756 
757  totemip_nosigpipe (sockets->mcast_recv);
758  res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
759  if (res == -1) {
760  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
761  "Could not set non-blocking operation on multicast socket");
762  return (-1);
763  }
764 
765  /*
766  * Force reuse
767  */
768  flag = 1;
769  if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
770  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
771  "setsockopt(SO_REUSEADDR) failed");
772  return (-1);
773  }
774 
775  /*
776  * Bind to multicast socket used for multicast receives
777  */
779  instance->totem_interface->ip_port, &sockaddr, &addrlen);
780  res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
781  if (res == -1) {
782  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
783  "Unable to bind the socket to receive multicast packets");
784  return (-1);
785  }
786 
787  /*
788  * Create local multicast loop socket
789  */
790  if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets->local_mcast_loop) == -1) {
791  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
792  "socket() failed");
793  return (-1);
794  }
795 
796  for (i = 0; i < 2; i++) {
797  totemip_nosigpipe (sockets->local_mcast_loop[i]);
798  res = fcntl (sockets->local_mcast_loop[i], F_SETFL, O_NONBLOCK);
799  if (res == -1) {
800  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
801  "Could not set non-blocking operation on multicast socket");
802  return (-1);
803  }
804  }
805 
806 
807 
808  /*
809  * Setup mcast send socket
810  */
811  sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
812  if (sockets->mcast_send == -1) {
813  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
814  "socket() failed");
815  return (-1);
816  }
817 
818  totemip_nosigpipe (sockets->mcast_send);
819  res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
820  if (res == -1) {
821  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
822  "Could not set non-blocking operation on multicast socket");
823  return (-1);
824  }
825 
826  /*
827  * Force reuse
828  */
829  flag = 1;
830  if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
831  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
832  "setsockopt(SO_REUSEADDR) failed");
833  return (-1);
834  }
835 
837  &sockaddr, &addrlen);
838  res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
839  if (res == -1) {
840  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
841  "Unable to bind the socket to send multicast packets");
842  return (-1);
843  }
844 
845  /*
846  * Setup unicast socket
847  */
848  sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
849  if (sockets->token == -1) {
850  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
851  "socket() failed");
852  return (-1);
853  }
854 
855  totemip_nosigpipe (sockets->token);
856  res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
857  if (res == -1) {
858  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
859  "Could not set non-blocking operation on token socket");
860  return (-1);
861  }
862 
863  /*
864  * Force reuse
865  */
866  flag = 1;
867  if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
868  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
869  "setsockopt(SO_REUSEADDR) failed");
870  return (-1);
871  }
872 
873  /*
874  * Bind to unicast socket used for token send/receives
875  * This has the side effect of binding to the correct interface
876  */
877  totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
878  res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
879  if (res == -1) {
880  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
881  "Unable to bind UDP unicast socket");
882  return (-1);
883  }
884 
885  recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
886  sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
887  /*
888  * Set buffer sizes to avoid overruns
889  */
890  res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
891  if (res == -1) {
892  LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
893  "Unable to set SO_RCVBUF size on UDP mcast socket");
894  return (-1);
895  }
896  res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
897  if (res == -1) {
898  LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
899  "Unable to set SO_SNDBUF size on UDP mcast socket");
900  return (-1);
901  }
902  res = setsockopt (sockets->local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
903  if (res == -1) {
904  LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
905  "Unable to set SO_RCVBUF size on UDP local mcast loop socket");
906  return (-1);
907  }
908  res = setsockopt (sockets->local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
909  if (res == -1) {
910  LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
911  "Unable to set SO_SNDBUF size on UDP local mcast loop socket");
912  return (-1);
913  }
914 
915  res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
916  if (res == 0) {
918  "Receive multicast socket recv buffer size (%d bytes).", recvbuf_size);
919  }
920 
921  res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
922  if (res == 0) {
924  "Transmit multicast socket send buffer size (%d bytes).", sendbuf_size);
925  }
926 
927  res = getsockopt (sockets->local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
928  if (res == 0) {
930  "Local receive multicast loop socket recv buffer size (%d bytes).", recvbuf_size);
931  }
932 
933  res = getsockopt (sockets->local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
934  if (res == 0) {
936  "Local transmit multicast loop socket send buffer size (%d bytes).", sendbuf_size);
937  }
938 
939 
940  /*
941  * Join group membership on socket
942  */
943  totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
944  totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
945 
946  if (instance->totem_config->broadcast_use == 1) {
947  unsigned int broadcast = 1;
948 
949  if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
950  SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
951  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
952  "setting broadcast option failed");
953  return (-1);
954  }
955  if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
956  SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
957  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
958  "setting broadcast option failed");
959  return (-1);
960  }
961  } else {
962  switch (bindnet_address->family) {
963  case AF_INET:
964  memset(&mreq, 0, sizeof(mreq));
965  mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
966  mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
967  res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
968  &mreq, sizeof (mreq));
969  if (res == -1) {
970  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
971  "join ipv4 multicast group failed");
972  return (-1);
973  }
974  break;
975  case AF_INET6:
976  memset(&mreq6, 0, sizeof(mreq6));
977  memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
978  mreq6.ipv6mr_interface = interface_num;
979 
980  res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
981  &mreq6, sizeof (mreq6));
982  if (res == -1) {
983  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
984  "join ipv6 multicast group failed");
985  return (-1);
986  }
987  break;
988  }
989  }
990 
991  /*
992  * Turn off multicast loopback
993  */
994 
995  flag = 0;
996  switch ( bindnet_address->family ) {
997  case AF_INET:
998  sflag = 0;
999  res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
1000  &sflag, sizeof (sflag));
1001  break;
1002  case AF_INET6:
1003  res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1004  &flag, sizeof (flag));
1005  }
1006  if (res == -1) {
1007  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1008  "Unable to turn off multicast loopback");
1009  return (-1);
1010  }
1011 
1012  /*
1013  * Set multicast packets TTL
1014  */
1015  flag = instance->totem_interface->ttl;
1016  if (bindnet_address->family == AF_INET6) {
1017  res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
1018  &flag, sizeof (flag));
1019  if (res == -1) {
1020  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1021  "set mcast v6 TTL failed");
1022  return (-1);
1023  }
1024  } else {
1025  sflag = flag;
1026  res = setsockopt(sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_TTL,
1027  &sflag, sizeof(sflag));
1028  if (res == -1) {
1029  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1030  "set mcast v4 TTL failed");
1031  return (-1);
1032  }
1033  }
1034 
1035  /*
1036  * Bind to a specific interface for multicast send and receive
1037  */
1038  switch ( bindnet_address->family ) {
1039  case AF_INET:
1040  if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
1041  &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
1042  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1043  "cannot select interface for multicast packets (send)");
1044  return (-1);
1045  }
1046  if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
1047  &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
1048  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1049  "cannot select interface for multicast packets (recv)");
1050  return (-1);
1051  }
1052  break;
1053  case AF_INET6:
1054  if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1055  &interface_num, sizeof (interface_num)) < 0) {
1056  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1057  "cannot select interface for multicast packets (send v6)");
1058  return (-1);
1059  }
1060  if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1061  &interface_num, sizeof (interface_num)) < 0) {
1062  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1063  "cannot select interface for multicast packets (recv v6)");
1064  return (-1);
1065  }
1066  break;
1067  }
1068 
1069  return 0;
1070 }
1071 
1072 static int totemudp_build_sockets (
1073  struct totemudp_instance *instance,
1074  struct totem_ip_address *mcast_address,
1075  struct totem_ip_address *bindnet_address,
1076  struct totemudp_socket *sockets,
1077  struct totem_ip_address *bound_to)
1078 {
1079  int interface_num;
1080  int interface_up;
1081  int res;
1082 
1083  /*
1084  * Determine the ip address bound to and the interface name
1085  */
1086  res = netif_determine (instance,
1087  bindnet_address,
1088  bound_to,
1089  &interface_up,
1090  &interface_num);
1091 
1092  if (res == -1) {
1093  return (-1);
1094  }
1095 
1096  totemip_copy(&instance->my_id, bound_to);
1097 
1098  res = totemudp_build_sockets_ip (instance, mcast_address,
1099  bindnet_address, sockets, bound_to, interface_num);
1100 
1101  /* We only send out of the token socket */
1102  totemudp_traffic_control_set(instance, sockets->token);
1103  return res;
1104 }
1105 
1106 /*
1107  * Totem Network interface - also does encryption/decryption
1108  * depends on poll abstraction, POSIX, IPV4
1109  */
1110 
1111 /*
1112  * Create an instance
1113  */
1115  qb_loop_t *poll_handle,
1116  void **udp_context,
1117  struct totem_config *totem_config,
1118  totemsrp_stats_t *stats,
1119  int interface_no,
1120  void *context,
1121 
1122  void (*deliver_fn) (
1123  void *context,
1124  const void *msg,
1125  unsigned int msg_len),
1126 
1127  void (*iface_change_fn) (
1128  void *context,
1129  const struct totem_ip_address *iface_address),
1130 
1131  void (*target_set_completed) (
1132  void *context))
1133 {
1134  struct totemudp_instance *instance;
1135 
1136  instance = malloc (sizeof (struct totemudp_instance));
1137  if (instance == NULL) {
1138  return (-1);
1139  }
1140 
1141  totemudp_instance_initialize (instance);
1142 
1143  instance->totem_config = totem_config;
1144  instance->stats = stats;
1145 
1146  /*
1147  * Configure logging
1148  */
1149  instance->totemudp_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
1156 
1157  /*
1158  * Initialize random number generator for later use to generate salt
1159  */
1160  instance->crypto_inst = crypto_init (totem_config->private_key,
1161  totem_config->private_key_len,
1162  totem_config->crypto_cipher_type,
1163  totem_config->crypto_hash_type,
1164  instance->totemudp_log_printf,
1165  instance->totemudp_log_level_security,
1166  instance->totemudp_log_level_notice,
1167  instance->totemudp_log_level_error,
1168  instance->totemudp_subsys_id);
1169  if (instance->crypto_inst == NULL) {
1170  free(instance);
1171  return (-1);
1172  }
1173  /*
1174  * Initialize local variables for totemudp
1175  */
1176  instance->totem_interface = &totem_config->interfaces[interface_no];
1177  totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
1178  memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
1179 
1180  instance->totemudp_poll_handle = poll_handle;
1181 
1182  instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
1183 
1184  instance->context = context;
1185  instance->totemudp_deliver_fn = deliver_fn;
1186 
1187  instance->totemudp_iface_change_fn = iface_change_fn;
1188 
1189  instance->totemudp_target_set_completed = target_set_completed;
1190 
1191  totemip_localhost (instance->mcast_address.family, &localhost);
1192  localhost.nodeid = instance->totem_config->node_id;
1193 
1194  /*
1195  * RRP layer isn't ready to receive message because it hasn't
1196  * initialized yet. Add short timer to check the interfaces.
1197  */
1198  qb_loop_timer_add (instance->totemudp_poll_handle,
1199  QB_LOOP_MED,
1200  100*QB_TIME_NS_IN_MSEC,
1201  (void *)instance,
1202  timer_function_netif_check_timeout,
1203  &instance->timer_netif_check_timeout);
1204 
1205  *udp_context = instance;
1206  return (0);
1207 }
1208 
1210 {
1211  return malloc (FRAME_SIZE_MAX);
1212 }
1213 
1214 void totemudp_buffer_release (void *ptr)
1215 {
1216  return free (ptr);
1217 }
1218 
1220  void *udp_context,
1221  int processor_count)
1222 {
1223  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1224  int res = 0;
1225 
1226  instance->my_memb_entries = processor_count;
1227  qb_loop_timer_del (instance->totemudp_poll_handle,
1228  instance->timer_netif_check_timeout);
1229  if (processor_count == 1) {
1230  qb_loop_timer_add (instance->totemudp_poll_handle,
1231  QB_LOOP_MED,
1232  instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
1233  (void *)instance,
1234  timer_function_netif_check_timeout,
1235  &instance->timer_netif_check_timeout);
1236  }
1237 
1238  return (res);
1239 }
1240 
1241 int totemudp_recv_flush (void *udp_context)
1242 {
1243  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1244  struct pollfd ufd;
1245  int nfds;
1246  int res = 0;
1247  int i;
1248  int sock;
1249 
1250  instance->flushing = 1;
1251 
1252  for (i = 0; i < 2; i++) {
1253  sock = -1;
1254  if (i == 0) {
1255  sock = instance->totemudp_sockets.mcast_recv;
1256  }
1257  if (i == 1) {
1258  sock = instance->totemudp_sockets.local_mcast_loop[0];
1259  }
1260  assert(sock != -1);
1261 
1262  do {
1263  ufd.fd = sock;
1264  ufd.events = POLLIN;
1265  nfds = poll (&ufd, 1, 0);
1266  if (nfds == 1 && ufd.revents & POLLIN) {
1267  net_deliver_fn (sock, ufd.revents, instance);
1268  }
1269  } while (nfds == 1);
1270  }
1271 
1272  instance->flushing = 0;
1273 
1274  return (res);
1275 }
1276 
1277 int totemudp_send_flush (void *udp_context)
1278 {
1279  return 0;
1280 }
1281 
1283  void *udp_context,
1284  const void *msg,
1285  unsigned int msg_len)
1286 {
1287  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1288  int res = 0;
1289 
1290  ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
1291 
1292  return (res);
1293 }
1295  void *udp_context,
1296  const void *msg,
1297  unsigned int msg_len)
1298 {
1299  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1300  int res = 0;
1301 
1302  mcast_sendmsg (instance, msg, msg_len);
1303 
1304  return (res);
1305 }
1306 
1308  void *udp_context,
1309  const void *msg,
1310  unsigned int msg_len)
1311 {
1312  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1313  int res = 0;
1314 
1315  mcast_sendmsg (instance, msg, msg_len);
1316 
1317  return (res);
1318 }
1319 
1320 extern int totemudp_iface_check (void *udp_context)
1321 {
1322  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1323  int res = 0;
1324 
1325  timer_function_netif_check_timeout (instance);
1326 
1327  return (res);
1328 }
1329 
1330 extern void totemudp_net_mtu_adjust (void *udp_context, struct totem_config *totem_config)
1331 {
1332 
1333  assert(totem_config->interface_count > 0);
1334 
1335  totem_config->net_mtu -= crypto_sec_header_size(totem_config->crypto_cipher_type,
1336  totem_config->crypto_hash_type) +
1338 }
1339 
1340 const char *totemudp_iface_print (void *udp_context) {
1341  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1342  const char *ret_char;
1343 
1344  ret_char = totemip_print (&instance->my_id);
1345 
1346  return (ret_char);
1347 }
1348 
1350  void *udp_context,
1351  struct totem_ip_address *addr)
1352 {
1353  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1354  int res = 0;
1355 
1356  memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
1357 
1358  return (res);
1359 }
1360 
1362  void *udp_context,
1363  const struct totem_ip_address *token_target)
1364 {
1365  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1366  int res = 0;
1367 
1368  memcpy (&instance->token_target, token_target,
1369  sizeof (struct totem_ip_address));
1370 
1371  instance->totemudp_target_set_completed (instance->context);
1372 
1373  return (res);
1374 }
1375 
1377  void *udp_context)
1378 {
1379  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1380  unsigned int res;
1381  struct sockaddr_storage system_from;
1382  struct msghdr msg_recv;
1383  struct pollfd ufd;
1384  int nfds;
1385  int msg_processed = 0;
1386  int i;
1387  int sock;
1388 
1389  /*
1390  * Receive datagram
1391  */
1392  msg_recv.msg_name = &system_from;
1393  msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
1394  msg_recv.msg_iov = &instance->totemudp_iov_recv_flush;
1395  msg_recv.msg_iovlen = 1;
1396 #ifdef HAVE_MSGHDR_CONTROL
1397  msg_recv.msg_control = 0;
1398 #endif
1399 #ifdef HAVE_MSGHDR_CONTROLLEN
1400  msg_recv.msg_controllen = 0;
1401 #endif
1402 #ifdef HAVE_MSGHDR_FLAGS
1403  msg_recv.msg_flags = 0;
1404 #endif
1405 #ifdef HAVE_MSGHDR_ACCRIGHTS
1406  msg_recv.msg_accrights = NULL;
1407 #endif
1408 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
1409  msg_recv.msg_accrightslen = 0;
1410 #endif
1411 
1412  for (i = 0; i < 2; i++) {
1413  sock = -1;
1414  if (i == 0) {
1415  sock = instance->totemudp_sockets.mcast_recv;
1416  }
1417  if (i == 1) {
1418  sock = instance->totemudp_sockets.local_mcast_loop[0];
1419  }
1420  assert(sock != -1);
1421 
1422  do {
1423  ufd.fd = sock;
1424  ufd.events = POLLIN;
1425  nfds = poll (&ufd, 1, 0);
1426  if (nfds == 1 && ufd.revents & POLLIN) {
1427  res = recvmsg (sock, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
1428  if (res != -1) {
1429  msg_processed = 1;
1430  } else {
1431  msg_processed = -1;
1432  }
1433  }
1434  } while (nfds == 1);
1435  }
1436 
1437  return (msg_processed);
1438 }
1439 
unsigned int clear_node_high_bit
Definition: totem.h:117
unsigned short family
Definition: coroapi.h:97
int totemudp_crypto_set(void *udp_context, const char *cipher_type, const char *hash_type)
Definition: totemudp.c:249
int totemudp_processor_count_set(void *udp_context, int processor_count)
Definition: totemudp.c:1219
int totemip_localhost(int family, struct totem_ip_address *localhost)
Definition: totemip.c:182
int totemudp_subsys_id
Definition: totemudp.c:143
struct iovec totemudp_iov_recv_flush
Definition: totemudp.c:162
struct totem_interface * interfaces
Definition: totem.h:114
unsigned int interface_count
Definition: totem.h:115
totemsrp_stats_t * stats
Definition: totemudp.c:192
struct totemudp_instance * instance
Definition: totemudp.c:200
struct crypto_instance * crypto_inst
Definition: totemudp.c:107
size_t crypto_sec_header_size(const char *crypto_cipher_type, const char *crypto_hash_type)
Definition: totemcrypto.c:667
const char * totemip_print(const struct totem_ip_address *addr)
Definition: totemip.c:214
void(* totemudp_iface_change_fn)(void *context, const struct totem_ip_address *iface_address)
Definition: totemudp.c:124
size_t crypto_get_current_sec_header_size(const struct crypto_instance *instance)
Definition: totemcrypto.c:702
void(* totemudp_deliver_fn)(void *context, const void *msg, unsigned int msg_len)
Definition: totemudp.c:119
void totemudp_net_mtu_adjust(void *udp_context, struct totem_config *totem_config)
Definition: totemudp.c:1330
void * totemudp_buffer_alloc(void)
Definition: totemudp.c:1209
int totemudp_mcast_flush_send(void *udp_context, const void *msg, unsigned int msg_len)
Definition: totemudp.c:1294
int totemudp_token_target_set(void *udp_context, const struct totem_ip_address *token_target)
Definition: totemudp.c:1361
unsigned char private_key[TOTEM_PRIVATE_KEY_LEN]
Definition: totem.h:122
char iov_buffer[FRAME_SIZE_MAX]
Definition: totemudp.c:156
#define MSG_NOSIGNAL
Definition: totemudp.c:80
struct totemudp_socket totemudp_sockets
Definition: totemudp.c:164
unsigned char addr[TOTEMIP_ADDRLEN]
Definition: coroapi.h:67
struct crypto_instance * crypto_init(const unsigned char *private_key, unsigned int private_key_len, const char *crypto_cipher_type, const char *crypto_hash_type, void(*log_printf_func)(int level, int subsys, const char *function, const char *file, int line, const char *format,...) __attribute__((format(printf, 6, 7))), int log_level_security, int log_level_notice, int log_level_error, int log_subsys_id)
Definition: totemcrypto.c:803
void totemip_copy(struct totem_ip_address *addr1, const struct totem_ip_address *addr2)
Definition: totemip.c:95
struct totem_ip_address mcast_address
Definition: totemudp.c:166
unsigned int downcheck_timeout
Definition: totem.h:145
int totemudp_mcast_noflush_send(void *udp_context, const void *msg, unsigned int msg_len)
Definition: totemudp.c:1307
unsigned int private_key_len
Definition: totem.h:124
int totemudp_log_level_security
Definition: totemudp.c:133
#define BIND_STATE_REGULAR
Definition: totemudp.c:88
struct totem_config * totem_config
Definition: totemudp.c:190
#define NETIF_STATE_REPORT_DOWN
Definition: totemudp.c:85
#define totemip_nosigpipe(s)
Definition: totemip.h:56
int totemudp_log_level_error
Definition: totemudp.c:135
message_type
Definition: totemsrp.c:139
int totemudp_send_flush(void *udp_context)
Definition: totemudp.c:1277
uint16_t ttl
Definition: totem.h:69
int totemudp_recv_flush(void *udp_context)
Definition: totemudp.c:1241
unsigned int node_id
Definition: totem.h:116
struct iovec totemudp_iov_recv
Definition: totemudp.c:160
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
int crypto_encrypt_and_sign(struct crypto_instance *instance, const unsigned char *buf_in, const size_t buf_in_len, unsigned char *buf_out, size_t *buf_out_len)
Definition: totemcrypto.c:723
int totemudp_token_send(void *udp_context, const void *msg, unsigned int msg_len)
Definition: totemudp.c:1282
struct totem_interface * totem_interface
Definition: totemudp.c:111
int totemudp_initialize(qb_loop_t *poll_handle, void **udp_context, struct totem_config *totem_config, totemsrp_stats_t *stats, int interface_no, void *context, void(*deliver_fn)(void *context, const void *msg, unsigned int msg_len), void(*iface_change_fn)(void *context, const struct totem_ip_address *iface_address), void(*target_set_completed)(void *context))
Create an instance.
Definition: totemudp.c:1114
unsigned int nodeid
Definition: coroapi.h:96
qb_loop_t * totemudp_poll_handle
Definition: totemudp.c:109
char * crypto_hash_type
Definition: totem.h:183
int netif_state_report
Definition: totemudp.c:113
unsigned int my_memb_entries
Definition: totemudp.c:186
struct totem_ip_address mcast_addr
Definition: totem.h:67
Linked list API.
void(* totemudp_log_printf)(int level, int subsys, const char *function, const char *file, int line, const char *format,...) __attribute__((format(printf
Definition: totemudp.c:145
int totemudp_finalize(void *udp_context)
Definition: totemudp.c:425
struct totem_ip_address boundto
Definition: totem.h:66
#define BIND_STATE_LOOPBACK
Definition: totemudp.c:89
typedef __attribute__
size_t totemip_udpip_header_size(int family)
Definition: totemip.c:496
const char * totemudp_iface_print(void *udp_context)
Definition: totemudp.c:1340
#define NETIF_STATE_REPORT_UP
Definition: totemudp.c:84
void(* log_printf)(int level, int subsys, const char *function_name, const char *file_name, int file_line, const char *format,...) __attribute__((format(printf
Definition: totem.h:75
uint16_t ip_port
Definition: totem.h:68
#define MCAST_SOCKET_BUFFER_SIZE
Definition: totemudp.c:83
void(*) void udp_context)
Definition: totemudp.c:152
unsigned int net_mtu
Definition: totem.h:165
char iov_buffer_flush[FRAME_SIZE_MAX]
Definition: totemudp.c:158
int totemudp_log_level_debug
Definition: totemudp.c:141
int crypto_authenticate_and_decrypt(struct crypto_instance *instance, unsigned char *buf, int *buf_len)
Definition: totemcrypto.c:745
int totemudp_log_level_notice
Definition: totemudp.c:139
#define MESSAGE_TYPE_MEMB_JOIN
Definition: totemudp.c:91
unsigned int broadcast_use
Definition: totem.h:179
int totemudp_iface_get(void *udp_context, struct totem_ip_address *addr)
Definition: totemudp.c:1349
#define FRAME_SIZE_MAX
Definition: totem.h:50
#define LOGSYS_LEVEL_CRIT
Definition: logsys.h:69
void(* totemudp_target_set_completed)(void *context)
Definition: totemudp.c:128
const void * msg
Definition: totemudp.c:198
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
struct totem_logging_configuration totem_logging_configuration
Definition: totem.h:163
#define LOGSYS_PERROR(err_num, level, fmt, args...)
Definition: totemudp.c:239
struct srp_addr system_from
Definition: totemsrp.c:61
#define log_printf(level, format, args...)
Definition: totemudp.c:231
char * crypto_cipher_type
Definition: totem.h:181
struct totem_ip_address my_id
Definition: totemudp.c:180
int totemudp_recv_mcast_empty(void *udp_context)
Definition: totemudp.c:1376
int totemudp_iface_check(void *udp_context)
Definition: totemudp.c:1320
struct totem_ip_address bindnet
Definition: totem.h:65
uint32_t continuous_sendmsg_failures
Definition: totem.h:269
unsigned int msg_len
Definition: totemudp.c:199
int local_mcast_loop[2]
Definition: totemudp.c:103
struct totem_ip_address token_target
Definition: totemudp.c:194
qb_loop_timer_handle timer_netif_check_timeout
Definition: totemudp.c:184
void totemudp_buffer_release(void *ptr)
Definition: totemudp.c:1214
int totemudp_log_level_warning
Definition: totemudp.c:137