Skip to content

Commit

Permalink
test/udp: Add TOS test
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilianfridrich committed Dec 12, 2024
1 parent d037a5f commit 1cff992
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 1 deletion.
1 change: 1 addition & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ static const struct test tests[] = {
TEST(test_turn),
TEST(test_turn_tcp),
TEST(test_udp),
TEST(test_udp_tos),
TEST(test_unixsock),
TEST(test_uri),
TEST(test_uri_encode),
Expand Down
1 change: 1 addition & 0 deletions test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ int test_turn(void);
int test_turn_tcp(void);
int test_turn_thread(void);
int test_udp(void);
int test_udp_tos(void);
int test_unixsock(void);
int test_uri(void);
int test_uri_encode(void);
Expand Down
165 changes: 164 additions & 1 deletion test/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,48 @@
#include <string.h>
#include <re.h>
#include "test.h"

#if defined(WIN32)
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/types.h>
#ifndef _BSD_SOCKLEN_T_
#define _BSD_SOCKLEN_T_ int /**< Defines the BSD socket length type */
#endif
#include <sys/socket.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_IO_H
#include <io.h>
#endif
#if !defined(WIN32)
#include <netdb.h>
#endif

#define DEBUG_MODULE "udptest"
#define DEBUG_LEVEL 5
#include <re_dbg.h>


/** Platform independent buffer type cast */
#ifdef WIN32
#define BUF_CAST (char *)
#define SIZ_CAST (int)
#define close closesocket
#else
#define BUF_CAST
#define SIZ_CAST
#endif


enum {
UDP_RXSZ = 1024
};


struct udp_test {
struct udp_sock *usc;
struct udp_sock *uss;
Expand All @@ -21,6 +56,8 @@ struct udp_test {
struct sa srv;
int tindex;
int err;
re_sock_t fds;
int tos;
};


Expand Down Expand Up @@ -213,3 +250,129 @@ int test_udp(void)

return err;
}


static void udp_read_msg_handler(int flags, void *arg)
{
struct cmsghdr *cmh;
struct iovec iov;
ssize_t n;
struct msghdr msg = {0};
char control_buffer[1024] = {0};
struct udp_test *ut = arg;
struct mbuf *mb = mbuf_alloc(UDP_RXSZ);
int tos = 0;
int err = 0;

(void)flags;

if (!mb)
return;

iov.iov_base = mbuf_buf(mb);
iov.iov_len = UDP_RXSZ;

msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = control_buffer;
msg.msg_controllen = sizeof(control_buffer);

n = recvmsg(ut->fds, &msg, 0);
if (n < 0)
goto out;

for (cmh = CMSG_FIRSTHDR(&msg); cmh != NULL;
cmh = CMSG_NXTHDR(&msg, cmh)) {
if (cmh->cmsg_level == IPPROTO_IP && cmh->cmsg_type == IP_TOS)
tos = *(unsigned char *)CMSG_DATA(cmh);
}

TEST_EQUALS(ut->tos, tos);

out:
if (err)
ut->err = err;
mem_deref(mb);
re_cancel();
}


int test_udp_tos(void)
{
struct udp_test *ut;
int err;
const int recvtos = 1;
struct re_fhs *fhss = NULL;
char addr[64];
char serv[6];
struct addrinfo hints, *res = NULL;

ut = mem_zalloc(sizeof(*ut), destructor);
if (!ut)
return ENOMEM;

ut->tos = 120;

err = sa_set_str(&ut->cli, "127.0.0.1", 0);
err |= sa_set_str(&ut->srv, "127.0.0.1", 0);
TEST_ERR(err)

err = udp_open(&ut->usc, AF_INET);
err |= udp_open(&ut->uss, AF_INET);
TEST_ERR(err)

err = udp_settos(ut->usc, ut->tos);
TEST_ERR(err)

/* Setup server */
memset(&hints, 0, sizeof(hints));
/* set-up hints structure */
hints.ai_family = AF_INET;
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
udp_rxsz_set(ut->uss, UDP_RXSZ);

(void)re_snprintf(addr, sizeof(addr), "%H", sa_print_addr, &ut->srv);
(void)re_snprintf(serv, sizeof(serv), "%u", sa_port(&ut->srv));

ut->fds = udp_sock_fd(ut->uss, 0);
TEST_NOT_EQUALS(RE_BAD_SOCK, ut->fds);

err = setsockopt(ut->fds, IPPROTO_IP, IP_RECVTOS, &recvtos,
sizeof(recvtos));
TEST_ERR(err);

err = net_sockopt_blocking_set(ut->fds, false);
TEST_ERR(err);

err = getaddrinfo(addr, serv, &hints, &res);
TEST_ERR(err);

err = bind(ut->fds, res->ai_addr, SIZ_CAST res->ai_addrlen);
TEST_ERR(err);

err = fd_listen(&fhss, ut->fds, FD_READ, udp_read_msg_handler, ut);
TEST_ERR(err);

err = udp_local_get(ut->uss, &ut->srv);
TEST_ERR(err)

err = send_data(ut->usc, &ut->srv, data0);
TEST_ERR(err)

err = re_main_timeout(100);
TEST_ERR(err)

if (ut->err)
err = ut->err;

out:
freeaddrinfo(res);
(void)close(ut->fds);

mem_deref(fhss);
mem_deref(ut);

return err;
}

0 comments on commit 1cff992

Please sign in to comment.