41 #include <sys/types.h> 43 #include <sys/socket.h> 46 #include <sys/ioctl.h> 47 #include <sys/param.h> 48 #include <netinet/in.h> 49 #include <arpa/inet.h> 65 #include <qb/qbdefs.h> 66 #include <qb/qbloop.h> 67 #define LOGSYS_UTILS_ONLY 1 80 #define MSG_NOSIGNAL 0 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 87 #define BIND_STATE_UNBOUND 0 88 #define BIND_STATE_REGULAR 1 89 #define BIND_STATE_LOOPBACK 2 117 void (*totemudp_deliver_fn) (
120 unsigned int msg_len);
122 void (*totemudp_iface_change_fn) (
126 void (*totemudp_target_set_completed) (
void *context);
143 void (*totemudp_log_printf) (
146 const char *
function,
158 struct iovec totemudp_iov_recv;
160 struct iovec totemudp_iov_recv_flush;
176 struct timeval stats_tv_start;
201 static int totemudp_build_sockets (
229 #define log_printf(level, format, args...) \ 231 instance->totemudp_log_printf ( \ 232 level, instance->totemudp_subsys_id, \ 233 __FUNCTION__, __FILE__, __LINE__, \ 234 (const char *)format, ##args); \ 237 #define LOGSYS_PERROR(err_num, level, fmt, args...) \ 239 char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \ 240 const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \ 241 instance->totemudp_log_printf ( \ 242 level, instance->totemudp_subsys_id, \ 243 __FUNCTION__, __FILE__, __LINE__, \ 244 fmt ": %s (%d)\n", ##args, _error_ptr, err_num); \ 249 const char *cipher_type,
250 const char *hash_type)
257 static inline void ucast_sendmsg (
261 unsigned int msg_len)
263 struct msghdr msg_ucast;
267 struct sockaddr_storage sockaddr;
282 (
const unsigned char *)msg,
285 &buf_out_len) != 0) {
290 iovec.iov_base = (
void *)buf_out;
291 iovec.iov_len = buf_out_len;
296 memset(&msg_ucast, 0,
sizeof(msg_ucast));
299 msg_ucast.msg_name = &sockaddr;
300 msg_ucast.msg_namelen = addrlen;
301 msg_ucast.msg_iov = (
void *)&iovec;
302 msg_ucast.msg_iovlen = 1;
303 #ifdef HAVE_MSGHDR_CONTROL 304 msg_ucast.msg_control = 0;
306 #ifdef HAVE_MSGHDR_CONTROLLEN 307 msg_ucast.msg_controllen = 0;
309 #ifdef HAVE_MSGHDR_FLAGS 310 msg_ucast.msg_flags = 0;
312 #ifdef HAVE_MSGHDR_ACCRIGHTS 313 msg_ucast.msg_accrights = NULL;
315 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN 316 msg_ucast.msg_accrightslen = 0;
328 "sendmsg(ucast) failed (non-critical)");
332 static inline void mcast_sendmsg (
335 unsigned int msg_len)
337 struct msghdr msg_mcast;
342 struct sockaddr_storage sockaddr;
356 (
const unsigned char *)msg,
359 &buf_out_len) != 0) {
364 iovec.iov_base = (
void *)&buf_out;
365 iovec.iov_len = buf_out_len;
372 memset(&msg_mcast, 0,
sizeof(msg_mcast));
373 msg_mcast.msg_name = &sockaddr;
374 msg_mcast.msg_namelen = addrlen;
375 msg_mcast.msg_iov = (
void *)&iovec;
376 msg_mcast.msg_iovlen = 1;
377 #ifdef HAVE_MSGHDR_CONTROL 378 msg_mcast.msg_control = 0;
380 #ifdef HAVE_MSGHDR_CONTROLLEN 381 msg_mcast.msg_controllen = 0;
383 #ifdef HAVE_MSGHDR_FLAGS 384 msg_mcast.msg_flags = 0;
386 #ifdef HAVE_MSGHDR_ACCRIGHTS 387 msg_mcast.msg_accrights = NULL;
389 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN 390 msg_mcast.msg_accrightslen = 0;
401 "sendmsg(mcast) failed (non-critical)");
411 msg_mcast.msg_name = NULL;
412 msg_mcast.msg_namelen = 0;
418 "sendmsg(local mcast loop) failed (non-critical)");
456 static int net_deliver_fn (
462 struct msghdr msg_recv;
464 struct sockaddr_storage system_from;
478 msg_recv.msg_namelen =
sizeof (
struct sockaddr_storage);
479 msg_recv.msg_iov = iovec;
480 msg_recv.msg_iovlen = 1;
481 #ifdef HAVE_MSGHDR_CONTROL 482 msg_recv.msg_control = 0;
484 #ifdef HAVE_MSGHDR_CONTROLLEN 485 msg_recv.msg_controllen = 0;
487 #ifdef HAVE_MSGHDR_FLAGS 488 msg_recv.msg_flags = 0;
490 #ifdef HAVE_MSGHDR_ACCRIGHTS 491 msg_recv.msg_accrights = NULL;
493 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN 494 msg_recv.msg_accrightslen = 0;
497 bytes_received = recvmsg (fd, &msg_recv,
MSG_NOSIGNAL | MSG_DONTWAIT);
498 if (bytes_received == -1) {
511 "Invalid packet data");
515 iovec->iov_len = bytes_received;
529 static int netif_determine (
539 interface_up, interface_num,
551 static void timer_function_netif_check_timeout (
562 netif_determine (instance,
565 &interface_up, &interface_num);
571 interface_up == 0) ||
575 interface_up == 1)) {
581 timer_function_netif_check_timeout,
582 &instance->timer_netif_check_timeout);
610 if (interface_up == 0) {
615 bind_address = &localhost;
624 timer_function_netif_check_timeout,
625 &instance->timer_netif_check_timeout);
636 (void)totemudp_build_sockets (instance,
646 POLLIN, instance, net_deliver_fn);
652 POLLIN, instance, net_deliver_fn);
658 POLLIN, instance, net_deliver_fn);
668 "The network interface [%s] is now up.",
681 timer_function_netif_check_timeout,
682 &instance->timer_netif_check_timeout);
688 "The network interface is down.");
698 static void totemudp_traffic_control_set(
struct totemudp_instance *instance,
int sock)
703 if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio,
sizeof(
int))) {
709 static int totemudp_build_sockets_ip (
717 struct sockaddr_storage sockaddr;
718 struct ipv6_mreq mreq6;
720 struct sockaddr_storage mcast_ss, boundto_ss;
721 struct sockaddr_in6 *mcast_sin6 = (
struct sockaddr_in6 *)&mcast_ss;
722 struct sockaddr_in *mcast_sin = (
struct sockaddr_in *)&mcast_ss;
723 struct sockaddr_in *boundto_sin = (
struct sockaddr_in *)&boundto_ss;
724 unsigned int sendbuf_size;
725 unsigned int recvbuf_size;
726 unsigned int optlen =
sizeof (sendbuf_size);
727 unsigned int retries;
745 res = fcntl (sockets->
mcast_recv, F_SETFL, O_NONBLOCK);
748 "Could not set non-blocking operation on multicast socket");
756 if ( setsockopt(sockets->
mcast_recv, SOL_SOCKET, SO_REUSEADDR, (
char *)&flag, sizeof (flag)) < 0) {
758 "setsockopt(SO_REUSEADDR) failed");
771 for (i = 0; i < 2; i++) {
776 "Could not set non-blocking operation on multicast socket");
794 res = fcntl (sockets->
mcast_send, F_SETFL, O_NONBLOCK);
797 "Could not set non-blocking operation on multicast socket");
805 if ( setsockopt(sockets->
mcast_send, SOL_SOCKET, SO_REUSEADDR, (
char *)&flag, sizeof (flag)) < 0) {
807 "setsockopt(SO_REUSEADDR) failed");
812 &sockaddr, &addrlen);
816 res = bind (sockets->
mcast_send, (
struct sockaddr *)&sockaddr, addrlen);
821 "Unable to bind the socket to send multicast packets");
838 sockets->
token = socket (bindnet_address->
family, SOCK_DGRAM, 0);
839 if (sockets->
token == -1) {
846 res = fcntl (sockets->
token, F_SETFL, O_NONBLOCK);
849 "Could not set non-blocking operation on token socket");
857 if ( setsockopt(sockets->
token, SOL_SOCKET, SO_REUSEADDR, (
char *)&flag, sizeof (flag)) < 0) {
859 "setsockopt(SO_REUSEADDR) failed");
871 res = bind (sockets->
token, (
struct sockaddr *)&sockaddr, addrlen);
876 "Unable to bind UDP unicast socket");
895 res = setsockopt (sockets->
mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
898 "Unable to set SO_RCVBUF size on UDP mcast socket");
901 res = setsockopt (sockets->
mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
904 "Unable to set SO_SNDBUF size on UDP mcast socket");
907 res = setsockopt (sockets->
local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
910 "Unable to set SO_RCVBUF size on UDP local mcast loop socket");
913 res = setsockopt (sockets->
local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
916 "Unable to set SO_SNDBUF size on UDP local mcast loop socket");
920 res = getsockopt (sockets->
mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
923 "Receive multicast socket recv buffer size (%d bytes).", recvbuf_size);
926 res = getsockopt (sockets->
mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
929 "Transmit multicast socket send buffer size (%d bytes).", sendbuf_size);
932 res = getsockopt (sockets->
local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
935 "Local receive multicast loop socket recv buffer size (%d bytes).", recvbuf_size);
938 res = getsockopt (sockets->
local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
941 "Local transmit multicast loop socket send buffer size (%d bytes).", sendbuf_size);
952 unsigned int broadcast = 1;
954 if ((setsockopt(sockets->
mcast_recv, SOL_SOCKET,
955 SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
957 "setting broadcast option failed");
960 if ((setsockopt(sockets->
mcast_send, SOL_SOCKET,
961 SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
963 "setting broadcast option failed");
967 switch (bindnet_address->
family) {
969 memset(&mreq, 0,
sizeof(mreq));
970 mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
971 mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
972 res = setsockopt (sockets->
mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
973 &mreq, sizeof (mreq));
976 "join ipv4 multicast group failed");
981 memset(&mreq6, 0,
sizeof(mreq6));
982 memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr,
sizeof(
struct in6_addr));
983 mreq6.ipv6mr_interface = interface_num;
985 res = setsockopt (sockets->
mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
986 &mreq6, sizeof (mreq6));
989 "join ipv6 multicast group failed");
1001 switch ( bindnet_address->
family ) {
1004 res = setsockopt (sockets->
mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
1005 &sflag, sizeof (sflag));
1008 res = setsockopt (sockets->
mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1009 &flag, sizeof (flag));
1013 "Unable to turn off multicast loopback");
1021 if (bindnet_address->
family == AF_INET6) {
1022 res = setsockopt (sockets->
mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
1023 &flag, sizeof (flag));
1026 "set mcast v6 TTL failed");
1031 res = setsockopt(sockets->
mcast_send, IPPROTO_IP, IP_MULTICAST_TTL,
1032 &sflag,
sizeof(sflag));
1035 "set mcast v4 TTL failed");
1043 switch ( bindnet_address->
family ) {
1045 if (setsockopt (sockets->
mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
1046 &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
1048 "cannot select interface for multicast packets (send)");
1051 if (setsockopt (sockets->
mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
1052 &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
1054 "cannot select interface for multicast packets (recv)");
1059 if (setsockopt (sockets->
mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1060 &interface_num, sizeof (interface_num)) < 0) {
1062 "cannot select interface for multicast packets (send v6)");
1065 if (setsockopt (sockets->
mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1066 &interface_num, sizeof (interface_num)) < 0) {
1068 "cannot select interface for multicast packets (recv v6)");
1085 res = bind (sockets->
mcast_recv, (
struct sockaddr *)&sockaddr, addrlen);
1090 "Unable to bind the socket to receive multicast packets");
1107 static int totemudp_build_sockets (
1121 res = netif_determine (instance,
1133 res = totemudp_build_sockets_ip (instance, mcast_address,
1134 bindnet_address, sockets, bound_to, interface_num);
1139 "Unable to create sockets, exiting");
1144 totemudp_traffic_control_set(instance, sockets->
token);
1157 qb_loop_t *poll_handle,
1164 void (*deliver_fn) (
1167 unsigned int msg_len),
1169 void (*iface_change_fn) (
1173 void (*target_set_completed) (
1179 if (instance == NULL) {
1183 totemudp_instance_initialize (instance);
1242 100*QB_TIME_NS_IN_MSEC,
1244 timer_function_netif_check_timeout,
1245 &instance->timer_netif_check_timeout);
1247 *udp_context = instance;
1263 int processor_count)
1271 if (processor_count == 1) {
1276 timer_function_netif_check_timeout,
1277 &instance->timer_netif_check_timeout);
1294 for (i = 0; i < 2; i++) {
1306 ufd.events = POLLIN;
1307 nfds = poll (&ufd, 1, 0);
1308 if (nfds == 1 && ufd.revents & POLLIN) {
1309 net_deliver_fn (sock, ufd.revents, instance);
1311 }
while (nfds == 1);
1327 unsigned int msg_len)
1332 ucast_sendmsg (instance, &instance->
token_target, msg, msg_len);
1339 unsigned int msg_len)
1344 mcast_sendmsg (instance, msg, msg_len);
1352 unsigned int msg_len)
1357 mcast_sendmsg (instance, msg, msg_len);
1367 timer_function_netif_check_timeout (instance);
1384 const char *ret_char;
1423 struct sockaddr_storage system_from;
1424 struct msghdr msg_recv;
1427 int msg_processed = 0;
1435 msg_recv.msg_namelen =
sizeof (
struct sockaddr_storage);
1437 msg_recv.msg_iovlen = 1;
1438 #ifdef HAVE_MSGHDR_CONTROL 1439 msg_recv.msg_control = 0;
1441 #ifdef HAVE_MSGHDR_CONTROLLEN 1442 msg_recv.msg_controllen = 0;
1444 #ifdef HAVE_MSGHDR_FLAGS 1445 msg_recv.msg_flags = 0;
1447 #ifdef HAVE_MSGHDR_ACCRIGHTS 1448 msg_recv.msg_accrights = NULL;
1450 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN 1451 msg_recv.msg_accrightslen = 0;
1454 for (i = 0; i < 2; i++) {
1466 ufd.events = POLLIN;
1467 nfds = poll (&ufd, 1, 0);
1468 if (nfds == 1 && ufd.revents & POLLIN) {
1469 res = recvmsg (sock, &msg_recv,
MSG_NOSIGNAL | MSG_DONTWAIT);
1476 }
while (nfds == 1);
1479 return (msg_processed);
unsigned int clear_node_high_bit
int totemudp_crypto_set(void *udp_context, const char *cipher_type, const char *hash_type)
int totemudp_processor_count_set(void *udp_context, int processor_count)
int totemip_localhost(int family, struct totem_ip_address *localhost)
struct iovec totemudp_iov_recv_flush
struct totem_interface * interfaces
unsigned int interface_count
struct totemudp_instance * instance
struct crypto_instance * crypto_inst
size_t crypto_sec_header_size(const char *crypto_cipher_type, const char *crypto_hash_type)
The totem_ip_address struct.
const char * totemip_print(const struct totem_ip_address *addr)
void(* totemudp_iface_change_fn)(void *context, const struct totem_ip_address *iface_address)
size_t crypto_get_current_sec_header_size(const struct crypto_instance *instance)
void(* totemudp_deliver_fn)(void *context, const void *msg, unsigned int msg_len)
void totemudp_net_mtu_adjust(void *udp_context, struct totem_config *totem_config)
void * totemudp_buffer_alloc(void)
int totemudp_mcast_flush_send(void *udp_context, const void *msg, unsigned int msg_len)
int totemudp_token_target_set(void *udp_context, const struct totem_ip_address *token_target)
unsigned char private_key[TOTEM_PRIVATE_KEY_LEN]
char iov_buffer[FRAME_SIZE_MAX]
struct totemudp_socket totemudp_sockets
unsigned char addr[TOTEMIP_ADDRLEN]
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)
void totemip_copy(struct totem_ip_address *addr1, const struct totem_ip_address *addr2)
struct totem_ip_address mcast_address
unsigned int downcheck_timeout
int totemudp_mcast_noflush_send(void *udp_context, const void *msg, unsigned int msg_len)
unsigned int private_key_len
int totemudp_log_level_security
#define BIND_STATE_REGULAR
struct totem_config * totem_config
#define NETIF_STATE_REPORT_DOWN
#define totemip_nosigpipe(s)
int totemudp_log_level_error
int totemudp_send_flush(void *udp_context)
int totemudp_recv_flush(void *udp_context)
struct iovec totemudp_iov_recv
int totemip_iface_check(struct totem_ip_address *bindnet, struct totem_ip_address *boundto, int *interface_up, int *interface_num, int mask_high_bit)
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)
int totemudp_token_send(void *udp_context, const void *msg, unsigned int msg_len)
struct totem_interface * totem_interface
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.
qb_loop_t * totemudp_poll_handle
unsigned int my_memb_entries
struct totem_ip_address mcast_addr
#define BIND_RETRIES_INTERVAL
void(* totemudp_log_printf)(int level, int subsys, const char *function, const char *file, int line, const char *format,...) __attribute__((format(printf
int totemudp_finalize(void *udp_context)
struct totem_ip_address boundto
#define BIND_STATE_LOOPBACK
size_t totemip_udpip_header_size(int family)
const char * totemudp_iface_print(void *udp_context)
#define NETIF_STATE_REPORT_UP
void(* log_printf)(int level, int subsys, const char *function_name, const char *file_name, int file_line, const char *format,...) __attribute__((format(printf
#define MCAST_SOCKET_BUFFER_SIZE
void(*) void udp_context)
char iov_buffer_flush[FRAME_SIZE_MAX]
int totemudp_log_level_debug
int crypto_authenticate_and_decrypt(struct crypto_instance *instance, unsigned char *buf, int *buf_len)
int totemudp_log_level_notice
unsigned int broadcast_use
int totemudp_iface_get(void *udp_context, struct totem_ip_address *addr)
#define LOGSYS_LEVEL_CRIT
void(* totemudp_target_set_completed)(void *context)
int totemip_totemip_to_sockaddr_convert(struct totem_ip_address *ip_addr, uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
struct totem_logging_configuration totem_logging_configuration
#define LOGSYS_PERROR(err_num, level, fmt, args...)
struct srp_addr system_from
#define log_printf(level, format, args...)
char * crypto_cipher_type
struct totem_ip_address my_id
int totemudp_recv_mcast_empty(void *udp_context)
int totemudp_iface_check(void *udp_context)
struct totem_ip_address bindnet
uint32_t continuous_sendmsg_failures
struct totem_ip_address token_target
qb_loop_timer_handle timer_netif_check_timeout
void totemudp_buffer_release(void *ptr)
int totemudp_log_level_warning