From 8816a7e9d571fac82711a320c11536daa80335c9 Mon Sep 17 00:00:00 2001 From: James Thomas Date: Fri, 6 Dec 2024 06:50:31 -0500 Subject: [PATCH] add test, clean up api --- include/udx.h | 9 +--- src/udx.c | 10 ++-- test/socket-send-recv-multicast.c | 88 +++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 test/socket-send-recv-multicast.c diff --git a/include/udx.h b/include/udx.h index 1f8c6ce..14bee24 100644 --- a/include/udx.h +++ b/include/udx.h @@ -79,11 +79,6 @@ typedef enum { UDX_LOOKUP_FAMILY_IPV6 = 2, } udx_lookup_flags; -typedef enum { - UDX_JOIN_GROUP, - UDX_LEAVE_GROUP -} udx_multicast_membership; - typedef struct udx_lookup_s udx_lookup_t; typedef struct udx_interface_event_s udx_interface_event_t; @@ -424,10 +419,10 @@ int udx_socket_bind (udx_socket_t *socket, const struct sockaddr *addr, unsigned int flags); int -udx_socket_set_membership (udx_socket_t *socket, const char *multicast_addr, const char *interface_addr, udx_multicast_membership membership); +udx_socket_set_membership (udx_socket_t *socket, const char *multicast_addr, const char *interface_addr, uv_membership membership); int -udx_socket_set_source_membership (udx_socket_t *socket, const char *multicast_addr, const char *interface_addr, const char *source_addr, udx_multicast_membership membership); +udx_socket_set_source_membership (udx_socket_t *socket, const char *multicast_addr, const char *interface_addr, const char *source_addr, uv_membership membership); int udx_socket_set_multicast_loop (udx_socket_t *socket, int on); diff --git a/src/udx.c b/src/udx.c index 23ad9a6..f7d1039 100644 --- a/src/udx.c +++ b/src/udx.c @@ -2112,15 +2112,13 @@ udx_socket_getsockname (udx_socket_t *socket, struct sockaddr *name, int *name_l } int -udx_socket_set_membership (udx_socket_t *socket, const char *multicast_addr, const char *interface_addr, udx_multicast_membership membership) { - uv_membership m = membership == UDX_JOIN_GROUP ? UV_JOIN_GROUP : UV_LEAVE_GROUP; - return uv_udp_set_membership(&socket->handle, multicast_addr, interface_addr, m); +udx_socket_set_membership (udx_socket_t *socket, const char *multicast_addr, const char *interface_addr, uv_membership membership) { + return uv_udp_set_membership(&socket->handle, multicast_addr, interface_addr, membership); } int -udx_socket_set_source_membership (udx_socket_t *socket, const char *multicast_addr, const char *interface_addr, const char *source_addr, udx_multicast_membership membership) { - uv_membership m = membership == UDX_JOIN_GROUP ? UV_JOIN_GROUP : UV_LEAVE_GROUP; - return uv_udp_set_source_membership(&socket->handle, multicast_addr, interface_addr, source_addr, m); +udx_socket_set_source_membership (udx_socket_t *socket, const char *multicast_addr, const char *interface_addr, const char *source_addr, uv_membership membership) { + return uv_udp_set_source_membership(&socket->handle, multicast_addr, interface_addr, source_addr, membership); } int diff --git a/test/socket-send-recv-multicast.c b/test/socket-send-recv-multicast.c new file mode 100644 index 0000000..8945367 --- /dev/null +++ b/test/socket-send-recv-multicast.c @@ -0,0 +1,88 @@ + +#include +#include +#include + +#include "../include/udx.h" + +#define MULTICAST_ADDR "239.255.0.1" + +uv_loop_t loop; +udx_t udx; + +udx_socket_t asock; +udx_socket_t bsock; +udx_socket_send_t req; + +int nclient_received; +int nserver_sent; + +bool close_called; + +void +on_send (udx_socket_send_t *r, int status) { + assert(&req == r); + assert(status == 0); + + nserver_sent++; +} + +void +on_recv (udx_socket_t *handle, ssize_t read_len, const uv_buf_t *buf, const struct sockaddr *from) { + assert(buf->len == 5); + assert(buf->len == read_len); + assert(memcmp(buf->base, "hello", 5) == 0); + + int e = udx_socket_set_membership(&asock, MULTICAST_ADDR, NULL, UV_LEAVE_GROUP); + assert(e == 0); + + nclient_received++; + uv_stop(&loop); +} + +int +main () { + int e; + + uv_loop_init(&loop); + + e = udx_init(&loop, &udx); + assert(e == 0); + + e = udx_socket_init(&udx, &asock); + assert(e == 0); + + e = udx_socket_init(&udx, &bsock); + assert(e == 0); + + struct sockaddr_in baddr; + uv_ip4_addr("0.0.0.0", 8082, &baddr); + e = udx_socket_bind(&bsock, (struct sockaddr *) &baddr, 0); + assert(e == 0); + + struct sockaddr_in aaddr; + uv_ip4_addr("0.0.0.0", 8081, &aaddr); + e = udx_socket_bind(&asock, (struct sockaddr *) &aaddr, UV_UDP_REUSEADDR); + assert(e == 0); + + // e = udx_socket_set_multicast_loop(&asock, 1); + // assert(e == 0); + e = udx_socket_set_membership(&asock, MULTICAST_ADDR, NULL, UV_JOIN_GROUP); + assert(e == 0); + + udx_socket_recv_start(&asock, on_recv); + + struct sockaddr_in mcast_addr; + uv_ip4_addr(MULTICAST_ADDR, 8081, &mcast_addr); + + uv_buf_t buf = uv_buf_init("hello", 5); + // udx_socket_send(&req, &bsock, &buf, 1, (struct sockaddr *) &aaddr, on_send); + + udx_socket_send(&req, &bsock, &buf, 1, (struct sockaddr *) &mcast_addr, on_send); + + uv_run(&loop, UV_RUN_DEFAULT); + + assert(nserver_sent && nclient_received); + + return 0; +}