|
| 1 | +/* test_multicast_interop.c |
| 2 | + * |
| 3 | + * Linux TAP interop smoke tests for wolfIP IPv4 UDP multicast. |
| 4 | + */ |
| 5 | +#include <arpa/inet.h> |
| 6 | +#include <errno.h> |
| 7 | +#include <fcntl.h> |
| 8 | +#include <netinet/in.h> |
| 9 | +#include <stdio.h> |
| 10 | +#include <string.h> |
| 11 | +#include <sys/select.h> |
| 12 | +#include <sys/socket.h> |
| 13 | +#include <sys/time.h> |
| 14 | +#include <unistd.h> |
| 15 | + |
| 16 | +#include "config.h" |
| 17 | +#include "wolfip.h" |
| 18 | + |
| 19 | +#ifndef IP_MULTICAST |
| 20 | +#error "test_multicast_interop requires IP_MULTICAST" |
| 21 | +#endif |
| 22 | + |
| 23 | +#define MCAST_GROUP "239.1.2.9" |
| 24 | +#define MCAST_PORT 19009 |
| 25 | +#define WOLFIP_MCAST_PORT 19010 |
| 26 | + |
| 27 | +extern int tap_init(struct wolfIP_ll_dev *dev, const char *name, uint32_t host_ip); |
| 28 | + |
| 29 | +static uint64_t now_ms(void) |
| 30 | +{ |
| 31 | + struct timeval tv; |
| 32 | + |
| 33 | + gettimeofday(&tv, NULL); |
| 34 | + return (uint64_t)tv.tv_sec * 1000U + (uint64_t)tv.tv_usec / 1000U; |
| 35 | +} |
| 36 | + |
| 37 | +static void poll_stack_for(struct wolfIP *s, unsigned int ms) |
| 38 | +{ |
| 39 | + uint64_t end = now_ms() + ms; |
| 40 | + |
| 41 | + while (now_ms() < end) { |
| 42 | + (void)wolfIP_poll(s, now_ms()); |
| 43 | + usleep(1000); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +static int host_udp_socket(void) |
| 48 | +{ |
| 49 | + int fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 50 | + int one = 1; |
| 51 | + |
| 52 | + if (fd < 0) { |
| 53 | + perror("socket"); |
| 54 | + return -1; |
| 55 | + } |
| 56 | + (void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); |
| 57 | + return fd; |
| 58 | +} |
| 59 | + |
| 60 | +static int test_host_to_wolfip(struct wolfIP *s, uint32_t host_ip) |
| 61 | +{ |
| 62 | + int host_fd; |
| 63 | + int wolf_fd; |
| 64 | + struct sockaddr_in host_if; |
| 65 | + struct sockaddr_in dst; |
| 66 | + struct wolfIP_sockaddr_in bind_addr; |
| 67 | + struct wolfIP_ip_mreq mreq; |
| 68 | + char rx[32]; |
| 69 | + const char payload[] = "linux-to-wolfip"; |
| 70 | + unsigned int i; |
| 71 | + |
| 72 | + wolf_fd = wolfIP_sock_socket(s, AF_INET, IPSTACK_SOCK_DGRAM, 17); |
| 73 | + if (wolf_fd < 0) |
| 74 | + return -1; |
| 75 | + memset(&bind_addr, 0, sizeof(bind_addr)); |
| 76 | + bind_addr.sin_family = AF_INET; |
| 77 | + bind_addr.sin_port = htons(MCAST_PORT); |
| 78 | + bind_addr.sin_addr.s_addr = 0; |
| 79 | + if (wolfIP_sock_bind(s, wolf_fd, (struct wolfIP_sockaddr *)&bind_addr, |
| 80 | + sizeof(bind_addr)) < 0) |
| 81 | + return -1; |
| 82 | + memset(&mreq, 0, sizeof(mreq)); |
| 83 | + inet_pton(AF_INET, MCAST_GROUP, &mreq.imr_multiaddr.s_addr); |
| 84 | + mreq.imr_interface.s_addr = htonl(INADDR_ANY); |
| 85 | + if (wolfIP_sock_setsockopt(s, wolf_fd, WOLFIP_SOL_IP, |
| 86 | + WOLFIP_IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) |
| 87 | + return -1; |
| 88 | + |
| 89 | + host_fd = host_udp_socket(); |
| 90 | + if (host_fd < 0) |
| 91 | + return -1; |
| 92 | + memset(&host_if, 0, sizeof(host_if)); |
| 93 | + host_if.sin_addr.s_addr = host_ip; |
| 94 | + if (setsockopt(host_fd, IPPROTO_IP, IP_MULTICAST_IF, |
| 95 | + &host_if.sin_addr, sizeof(host_if.sin_addr)) < 0) { |
| 96 | + perror("setsockopt IP_MULTICAST_IF"); |
| 97 | + close(host_fd); |
| 98 | + return -1; |
| 99 | + } |
| 100 | + memset(&dst, 0, sizeof(dst)); |
| 101 | + dst.sin_family = AF_INET; |
| 102 | + dst.sin_port = htons(MCAST_PORT); |
| 103 | + inet_pton(AF_INET, MCAST_GROUP, &dst.sin_addr); |
| 104 | + |
| 105 | + if (sendto(host_fd, payload, sizeof(payload), 0, |
| 106 | + (struct sockaddr *)&dst, sizeof(dst)) != (ssize_t)sizeof(payload)) { |
| 107 | + perror("sendto host multicast"); |
| 108 | + close(host_fd); |
| 109 | + return -1; |
| 110 | + } |
| 111 | + for (i = 0; i < 1000; i++) { |
| 112 | + int ret; |
| 113 | + |
| 114 | + (void)wolfIP_poll(s, now_ms()); |
| 115 | + ret = wolfIP_sock_recvfrom(s, wolf_fd, rx, sizeof(rx), 0, NULL, NULL); |
| 116 | + if (ret == (int)sizeof(payload) && memcmp(rx, payload, sizeof(payload)) == 0) { |
| 117 | + close(host_fd); |
| 118 | + return 0; |
| 119 | + } |
| 120 | + usleep(1000); |
| 121 | + } |
| 122 | + close(host_fd); |
| 123 | + fprintf(stderr, "wolfIP did not receive Linux multicast payload\n"); |
| 124 | + return -1; |
| 125 | +} |
| 126 | + |
| 127 | +static int test_wolfip_to_host(struct wolfIP *s, uint32_t host_ip) |
| 128 | +{ |
| 129 | + int host_fd; |
| 130 | + int wolf_fd; |
| 131 | + int ttl = 3; |
| 132 | + struct sockaddr_in bind_addr; |
| 133 | + struct ip_mreq host_mreq; |
| 134 | + struct wolfIP_sockaddr_in dst; |
| 135 | + fd_set rfds; |
| 136 | + struct timeval tv; |
| 137 | + char rx[32]; |
| 138 | + const char payload[] = "wolfip-to-linux"; |
| 139 | + |
| 140 | + host_fd = host_udp_socket(); |
| 141 | + if (host_fd < 0) |
| 142 | + return -1; |
| 143 | + memset(&bind_addr, 0, sizeof(bind_addr)); |
| 144 | + bind_addr.sin_family = AF_INET; |
| 145 | + bind_addr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 146 | + bind_addr.sin_port = htons(WOLFIP_MCAST_PORT); |
| 147 | + if (bind(host_fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) { |
| 148 | + perror("bind host multicast"); |
| 149 | + close(host_fd); |
| 150 | + return -1; |
| 151 | + } |
| 152 | + memset(&host_mreq, 0, sizeof(host_mreq)); |
| 153 | + inet_pton(AF_INET, MCAST_GROUP, &host_mreq.imr_multiaddr); |
| 154 | + host_mreq.imr_interface.s_addr = host_ip; |
| 155 | + if (setsockopt(host_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 156 | + &host_mreq, sizeof(host_mreq)) < 0) { |
| 157 | + perror("host IP_ADD_MEMBERSHIP"); |
| 158 | + close(host_fd); |
| 159 | + return -1; |
| 160 | + } |
| 161 | + |
| 162 | + wolf_fd = wolfIP_sock_socket(s, AF_INET, IPSTACK_SOCK_DGRAM, 17); |
| 163 | + if (wolf_fd < 0) |
| 164 | + return -1; |
| 165 | + if (wolfIP_sock_setsockopt(s, wolf_fd, WOLFIP_SOL_IP, |
| 166 | + WOLFIP_IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) |
| 167 | + return -1; |
| 168 | + memset(&dst, 0, sizeof(dst)); |
| 169 | + dst.sin_family = AF_INET; |
| 170 | + dst.sin_port = htons(WOLFIP_MCAST_PORT); |
| 171 | + inet_pton(AF_INET, MCAST_GROUP, &dst.sin_addr.s_addr); |
| 172 | + if (wolfIP_sock_sendto(s, wolf_fd, payload, sizeof(payload), 0, |
| 173 | + (struct wolfIP_sockaddr *)&dst, sizeof(dst)) != (int)sizeof(payload)) |
| 174 | + return -1; |
| 175 | + poll_stack_for(s, 50); |
| 176 | + |
| 177 | + FD_ZERO(&rfds); |
| 178 | + FD_SET(host_fd, &rfds); |
| 179 | + tv.tv_sec = 2; |
| 180 | + tv.tv_usec = 0; |
| 181 | + if (select(host_fd + 1, &rfds, NULL, NULL, &tv) <= 0) { |
| 182 | + fprintf(stderr, "Linux did not receive wolfIP multicast payload\n"); |
| 183 | + close(host_fd); |
| 184 | + return -1; |
| 185 | + } |
| 186 | + if (recv(host_fd, rx, sizeof(rx), 0) != (ssize_t)sizeof(payload) || |
| 187 | + memcmp(rx, payload, sizeof(payload)) != 0) { |
| 188 | + fprintf(stderr, "Linux received unexpected multicast payload\n"); |
| 189 | + close(host_fd); |
| 190 | + return -1; |
| 191 | + } |
| 192 | + close(host_fd); |
| 193 | + return 0; |
| 194 | +} |
| 195 | + |
| 196 | +int main(void) |
| 197 | +{ |
| 198 | + struct wolfIP *s; |
| 199 | + struct wolfIP_ll_dev *tapdev; |
| 200 | + struct in_addr host; |
| 201 | + |
| 202 | + wolfIP_init_static(&s); |
| 203 | + tapdev = wolfIP_getdev(s); |
| 204 | + if (!tapdev) |
| 205 | + return 1; |
| 206 | + inet_aton(HOST_STACK_IP, &host); |
| 207 | + if (tap_init(tapdev, "wmcast0", host.s_addr) < 0) |
| 208 | + return 2; |
| 209 | + wolfIP_ipconfig_set(s, atoip4(WOLFIP_IP), atoip4("255.255.255.0"), |
| 210 | + atoip4(HOST_STACK_IP)); |
| 211 | + poll_stack_for(s, 50); |
| 212 | + |
| 213 | + if (test_host_to_wolfip(s, host.s_addr) < 0) |
| 214 | + return 3; |
| 215 | + if (test_wolfip_to_host(s, host.s_addr) < 0) |
| 216 | + return 4; |
| 217 | + printf("multicast interop ok\n"); |
| 218 | + return 0; |
| 219 | +} |
0 commit comments