diff --git a/tests/gnrc_sock_udp/Makefile b/tests/gnrc_sock_udp/Makefile index 4dbc8d4065..ac0652b55a 100644 --- a/tests/gnrc_sock_udp/Makefile +++ b/tests/gnrc_sock_udp/Makefile @@ -1,11 +1,16 @@ include ../Makefile.tests_common AUX_LOCAL ?= 1 +AUX_TIMESTAMP ?= 1 ifeq (1, $(AUX_LOCAL)) USEMODULE += sock_aux_local endif +ifeq (1, $(AUX_LOCAL)) + USEMODULE += sock_aux_timestamp +endif + USEMODULE += gnrc_sock_check_reuse USEMODULE += sock_udp USEMODULE += gnrc_ipv6 diff --git a/tests/gnrc_sock_udp/main.c b/tests/gnrc_sock_udp/main.c index ae8bf75d9e..c12e06d893 100644 --- a/tests/gnrc_sock_udp/main.c +++ b/tests/gnrc_sock_udp/main.c @@ -429,13 +429,16 @@ static void test_sock_udp_recv__aux(void) static const ipv6_addr_t dst_addr = { .u8 = _TEST_ADDR_LOCAL }; static const sock_udp_ep_t local = { .family = AF_INET6, .port = _TEST_PORT_LOCAL }; + static const inject_aux_t inject_aux = { .timestamp = 1337 }; sock_udp_ep_t result; - sock_udp_aux_rx_t aux = { .flags = SOCK_AUX_GET_LOCAL }; + sock_udp_aux_rx_t aux = { + .flags = SOCK_AUX_GET_LOCAL | SOCK_AUX_GET_TIMESTAMP + }; expect(0 == sock_udp_create(&_sock, &local, NULL, SOCK_FLAGS_REUSE_EP)); - expect(_inject_packet(&src_addr, &dst_addr, _TEST_PORT_REMOTE, - _TEST_PORT_LOCAL, "ABCD", sizeof("ABCD"), - _TEST_NETIF)); + expect(_inject_packet_aux(&src_addr, &dst_addr, _TEST_PORT_REMOTE, + _TEST_PORT_LOCAL, "ABCD", sizeof("ABCD"), + _TEST_NETIF, &inject_aux)); expect(sizeof("ABCD") == sock_udp_recv_aux(&_sock, _test_buffer, sizeof(_test_buffer), 0, &result, &aux)); @@ -449,6 +452,12 @@ static void test_sock_udp_recv__aux(void) expect(_TEST_PORT_LOCAL == aux.local.port); #else expect(aux.flags & SOCK_AUX_GET_LOCAL); +#endif +#if IS_USED(MODULE_SOCK_AUX_TIMESTAMP) + expect(!(aux.flags & SOCK_AUX_GET_TIMESTAMP)); + expect(inject_aux.timestamp == aux.timestamp); +#else + expect(aux.flags & SOCK_AUX_GET_TIMESTAMP); #endif expect(_check_net()); } diff --git a/tests/gnrc_sock_udp/stack.c b/tests/gnrc_sock_udp/stack.c index 90305d5076..a5c7556117 100644 --- a/tests/gnrc_sock_udp/stack.c +++ b/tests/gnrc_sock_udp/stack.c @@ -47,9 +47,10 @@ static gnrc_pktsnip_t *_build_udp_packet(const ipv6_addr_t *src, const ipv6_addr_t *dst, uint16_t src_port, uint16_t dst_port, void *data, size_t data_len, - uint16_t netif) + uint16_t netif, + const inject_aux_t *aux) { - gnrc_pktsnip_t *netif_hdr, *ipv6, *udp; + gnrc_pktsnip_t *netif_hdr_snip, *ipv6, *udp; udp_hdr_t *udp_hdr; ipv6_hdr_t *ipv6_hdr; uint16_t csum = 0; @@ -86,21 +87,25 @@ static gnrc_pktsnip_t *_build_udp_packet(const ipv6_addr_t *src, udp_hdr->checksum = byteorder_htons(~csum); } udp = gnrc_pkt_append(udp, ipv6); - netif_hdr = gnrc_netif_hdr_build(NULL, 0, NULL, 0); - if (netif_hdr == NULL) { + netif_hdr_snip = gnrc_netif_hdr_build(NULL, 0, NULL, 0); + if (netif_hdr_snip == NULL) { return NULL; } - ((gnrc_netif_hdr_t *)netif_hdr->data)->if_pid = (kernel_pid_t)netif; - return gnrc_pkt_append(udp, netif_hdr); + gnrc_netif_hdr_t *netif_hdr = netif_hdr_snip->data; + netif_hdr->if_pid = (kernel_pid_t)netif; + if (aux) { + gnrc_netif_hdr_set_timestamp(netif_hdr, aux->timestamp); + } + return gnrc_pkt_append(udp, netif_hdr_snip); } - -bool _inject_packet(const ipv6_addr_t *src, const ipv6_addr_t *dst, - uint16_t src_port, uint16_t dst_port, - void *data, size_t data_len, uint16_t netif) +bool _inject_packet_aux(const ipv6_addr_t *src, const ipv6_addr_t *dst, + uint16_t src_port, uint16_t dst_port, + void *data, size_t data_len, uint16_t netif, + const inject_aux_t *aux) { gnrc_pktsnip_t *pkt = _build_udp_packet(src, dst, src_port, dst_port, - data, data_len, netif); + data, data_len, netif, aux); if (pkt == NULL) { return false; diff --git a/tests/gnrc_sock_udp/stack.h b/tests/gnrc_sock_udp/stack.h index 73aaf6b3e5..6975c1c01e 100644 --- a/tests/gnrc_sock_udp/stack.h +++ b/tests/gnrc_sock_udp/stack.h @@ -39,6 +39,13 @@ void _net_init(void); */ void _prepare_send_checks(void); +/** + * @brief Auxiliary data to inject + */ +typedef struct { + uint64_t timestamp; /**< Timestamp of reception */ +} inject_aux_t; + /** * @brief Injects a received UDP packet into the stack * @@ -53,9 +60,33 @@ void _prepare_send_checks(void); * @return true, if packet was successfully injected * @return false, if an error occurred during injection */ -bool _inject_packet(const ipv6_addr_t *src, const ipv6_addr_t *dst, - uint16_t src_port, uint16_t dst_port, - void *data, size_t data_len, uint16_t netif); +bool _inject_packet_aux(const ipv6_addr_t *src, const ipv6_addr_t *dst, + uint16_t src_port, uint16_t dst_port, + void *data, size_t data_len, uint16_t netif, + const inject_aux_t *aux); + +/** + * @brief Injects a received UDP packet into the stack + * + * @param[in] src The source address of the UDP packet + * @param[in] dst The destination address of the UDP packet + * @param[in] src_port The source port of the UDP packet + * @param[in] dst_port The destination port of the UDP packet + * @param[in] data The payload of the UDP packet + * @param[in] data_len The payload length of the UDP packet + * @param[in] netif The interface the packet came over + * + * @return true, if packet was successfully injected + * @return false, if an error occurred during injection + */ +static inline bool _inject_packet(const ipv6_addr_t *src, + const ipv6_addr_t *dst, + uint16_t src_port, uint16_t dst_port, + void *data, size_t data_len, uint16_t netif) +{ + return _inject_packet_aux(src, dst, src_port, dst_port, data, data_len, + netif, NULL); +} /** * @brief Checks networking state (e.g. packet buffer state)