|
| 1 | +/* |
| 2 | + * Copyright 2026 The Emscripten Authors. All rights reserved. |
| 3 | + * Emscripten is available under two separate licenses, the MIT license and the |
| 4 | + * University of Illinois/NCSA Open Source License. Both these licenses can be |
| 5 | + * found in the LICENSE file. |
| 6 | + */ |
| 7 | + |
| 8 | +#define _GNU_SOURCE |
| 9 | +#include <arpa/inet.h> |
| 10 | +#include <assert.h> |
| 11 | +#include <errno.h> |
| 12 | +#include <fcntl.h> |
| 13 | +#include <stdbool.h> |
| 14 | +#include <stdio.h> |
| 15 | +#include <string.h> |
| 16 | +#include <sys/select.h> |
| 17 | +#include <sys/socket.h> |
| 18 | +#include <unistd.h> |
| 19 | + |
| 20 | +#ifdef __EMSCRIPTEN__ |
| 21 | +#include <emscripten.h> |
| 22 | +#endif |
| 23 | + |
| 24 | +static int server_fd; |
| 25 | +static int client_fd; |
| 26 | +static struct sockaddr_in destination; |
| 27 | +static bool sent; |
| 28 | +static unsigned int received; |
| 29 | + |
| 30 | +static void succeed(void) { |
| 31 | + close(server_fd); |
| 32 | + close(client_fd); |
| 33 | + puts("done"); |
| 34 | +#ifdef __EMSCRIPTEN__ |
| 35 | + emscripten_cancel_main_loop(); |
| 36 | +#else |
| 37 | + _exit(0); |
| 38 | +#endif |
| 39 | +} |
| 40 | + |
| 41 | +static void main_loop(void) { |
| 42 | + if (!sent) { |
| 43 | + struct iovec siov[2] = { |
| 44 | + {.iov_base = "one", .iov_len = 3}, |
| 45 | + {.iov_base = "two", .iov_len = 3}, |
| 46 | + }; |
| 47 | + struct mmsghdr smsgs[2]; |
| 48 | + memset(smsgs, 0, sizeof(smsgs)); |
| 49 | + for (int i = 0; i < 2; i++) { |
| 50 | + smsgs[i].msg_hdr.msg_name = &destination; |
| 51 | + smsgs[i].msg_hdr.msg_namelen = sizeof(destination); |
| 52 | + smsgs[i].msg_hdr.msg_iov = &siov[i]; |
| 53 | + smsgs[i].msg_hdr.msg_iovlen = 1; |
| 54 | + } |
| 55 | + assert(sendmmsg(client_fd, smsgs, 2, 0) == 2); |
| 56 | + assert(smsgs[0].msg_len == 3 && smsgs[1].msg_len == 3); |
| 57 | + sent = true; |
| 58 | + } |
| 59 | + |
| 60 | + char buffers[2][3]; |
| 61 | + char controls[2][16]; |
| 62 | + struct sockaddr_in sources[2]; |
| 63 | + struct iovec iovecs[2]; |
| 64 | + struct mmsghdr messages[2]; |
| 65 | + memset(messages, 0, sizeof(messages)); |
| 66 | + for (int i = 0; i < 2; i++) { |
| 67 | + iovecs[i] = (struct iovec){.iov_base = buffers[i], .iov_len = sizeof(buffers[i])}; |
| 68 | + messages[i].msg_hdr.msg_name = &sources[i]; |
| 69 | + messages[i].msg_hdr.msg_namelen = sizeof(sources[i]); |
| 70 | + messages[i].msg_hdr.msg_iov = &iovecs[i]; |
| 71 | + messages[i].msg_hdr.msg_iovlen = 1; |
| 72 | + messages[i].msg_hdr.msg_control = controls[i]; |
| 73 | + messages[i].msg_hdr.msg_controllen = sizeof(controls[i]); |
| 74 | + } |
| 75 | + |
| 76 | + int count = recvmmsg(server_fd, messages, 2, MSG_DONTWAIT, NULL); |
| 77 | + if (count < 0) { |
| 78 | + assert(errno == EAGAIN || errno == EWOULDBLOCK); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + for (int i = 0; i < count; i++) { |
| 83 | + assert(messages[i].msg_len == 3); |
| 84 | + assert(memcmp(buffers[i], received == 0 ? "one" : "two", 3) == 0); |
| 85 | + received++; |
| 86 | + } |
| 87 | + if (received == 2) { |
| 88 | + succeed(); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +int main(void) { |
| 93 | + server_fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 94 | + client_fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 95 | + assert(server_fd >= 0 && client_fd >= 0); |
| 96 | + |
| 97 | + struct sockaddr_in address = { |
| 98 | + .sin_family = AF_INET, |
| 99 | + .sin_port = htons(0), |
| 100 | + }; |
| 101 | + inet_pton(AF_INET, "127.0.0.1", &address.sin_addr); |
| 102 | + assert(bind(server_fd, (struct sockaddr*)&address, sizeof(address)) == 0); |
| 103 | + |
| 104 | + socklen_t length = sizeof(destination); |
| 105 | + assert(getsockname(server_fd, (struct sockaddr*)&destination, &length) == 0); |
| 106 | + assert(fcntl(server_fd, F_SETFL, O_NONBLOCK) == 0); |
| 107 | + |
| 108 | +#ifdef __EMSCRIPTEN__ |
| 109 | + emscripten_set_main_loop(main_loop, 0, 0); |
| 110 | +#else |
| 111 | + while (true) { |
| 112 | + main_loop(); |
| 113 | + usleep(1000); |
| 114 | + } |
| 115 | +#endif |
| 116 | + return 0; |
| 117 | +} |
0 commit comments