From d9345798171da15842ad6b0da80d8a731a89b4b3 Mon Sep 17 00:00:00 2001 From: "Martine S. Lenders" Date: Wed, 1 Apr 2020 13:00:54 +0200 Subject: [PATCH] gnrc_sock: adapt for *_recv_buf() API change --- sys/net/gnrc/sock/gnrc_sock.c | 7 ------- sys/net/gnrc/sock/ip/gnrc_sock_ip.c | 25 ++++++++++++++++--------- sys/net/gnrc/sock/udp/gnrc_sock_udp.c | 25 ++++++++++++++++--------- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/sys/net/gnrc/sock/gnrc_sock.c b/sys/net/gnrc/sock/gnrc_sock.c index 585f763345..92c375b8c1 100644 --- a/sys/net/gnrc/sock/gnrc_sock.c +++ b/sys/net/gnrc/sock/gnrc_sock.c @@ -45,13 +45,6 @@ static void _callback_put(void *arg) } #endif -void sock_recv_buf_free(void *buf_ctx) -{ - if (buf_ctx) { - gnrc_pktbuf_release(buf_ctx); - } -} - #ifdef SOCK_HAS_ASYNC static void _netapi_cb(uint16_t cmd, gnrc_pktsnip_t *pkt, void *ctx) { diff --git a/sys/net/gnrc/sock/ip/gnrc_sock_ip.c b/sys/net/gnrc/sock/ip/gnrc_sock_ip.c index e0bb996f6c..c79cc7eda0 100644 --- a/sys/net/gnrc/sock/ip/gnrc_sock_ip.c +++ b/sys/net/gnrc/sock/ip/gnrc_sock_ip.c @@ -90,20 +90,21 @@ ssize_t sock_ip_recv(sock_ip_t *sock, void *data, size_t max_len, uint32_t timeout, sock_ip_ep_t *remote) { void *pkt = NULL, *ctx = NULL; - ssize_t res; + uint8_t *ptr = data; + ssize_t res, ret = 0; + bool nobufs = false; assert((sock != NULL) && (data != NULL) && (max_len > 0)); - res = sock_ip_recv_buf(sock, &pkt, &ctx, timeout, remote); - if (res >= 0) { + while ((res = sock_ip_recv_buf(sock, &pkt, &ctx, timeout, remote)) > 0) { if (res > (ssize_t)max_len) { - res = -ENOBUFS; + nobufs = true; + continue; } - else if (res != 0) { - memcpy(data, pkt, res); - } - sock_recv_buf_free(ctx); + memcpy(ptr, pkt, res); + ptr += res; + ret += res; } - return res; + return (nobufs) ? -ENOBUFS : ((res < 0) ? res : ret); } ssize_t sock_ip_recv_buf(sock_ip_t *sock, void **data, void **buf_ctx, @@ -114,6 +115,12 @@ ssize_t sock_ip_recv_buf(sock_ip_t *sock, void **data, void **buf_ctx, int res; assert((sock != NULL) && (data != NULL) && (buf_ctx != NULL)); + if (*buf_ctx != NULL) { + *data = NULL; + gnrc_pktbuf_release(*buf_ctx); + *buf_ctx = NULL; + return 0; + } if (sock->local.family == 0) { return -EADDRNOTAVAIL; } diff --git a/sys/net/gnrc/sock/udp/gnrc_sock_udp.c b/sys/net/gnrc/sock/udp/gnrc_sock_udp.c index fb45d4f9ff..1aaa2794b7 100644 --- a/sys/net/gnrc/sock/udp/gnrc_sock_udp.c +++ b/sys/net/gnrc/sock/udp/gnrc_sock_udp.c @@ -177,20 +177,21 @@ ssize_t sock_udp_recv(sock_udp_t *sock, void *data, size_t max_len, uint32_t timeout, sock_udp_ep_t *remote) { void *pkt = NULL, *ctx = NULL; - ssize_t res; + uint8_t *ptr = data; + ssize_t res, ret = 0; + bool nobufs = false; assert((sock != NULL) && (data != NULL) && (max_len > 0)); - res = sock_udp_recv_buf(sock, &pkt, &ctx, timeout, remote); - if (res >= 0) { + while ((res = sock_udp_recv_buf(sock, &pkt, &ctx, timeout, remote)) > 0) { if (res > (ssize_t)max_len) { - res = -ENOBUFS; + nobufs = true; + continue; } - else if (res != 0) { - memcpy(data, pkt, res); - } - sock_recv_buf_free(ctx); + memcpy(ptr, pkt, res); + ptr += res; + ret += res; } - return res; + return (nobufs) ? -ENOBUFS : ((res < 0) ? res : ret); } ssize_t sock_udp_recv_buf(sock_udp_t *sock, void **data, void **buf_ctx, @@ -202,6 +203,12 @@ ssize_t sock_udp_recv_buf(sock_udp_t *sock, void **data, void **buf_ctx, int res; assert((sock != NULL) && (data != NULL) && (buf_ctx != NULL)); + if (*buf_ctx != NULL) { + *data = NULL; + gnrc_pktbuf_release(*buf_ctx); + *buf_ctx = NULL; + return 0; + } if (sock->local.family == AF_UNSPEC) { return -EADDRNOTAVAIL; }