From 804da9ba252ee71c8bc9527268908d3d2931769b Mon Sep 17 00:00:00 2001 From: Hendrik van Essen Date: Wed, 22 Dec 2021 02:28:49 +0100 Subject: [PATCH] pkg/lwip: give lines some more space --- pkg/lwip/contrib/sock/ip/lwip_sock_ip.c | 13 +++++++ pkg/lwip/contrib/sock/lwip_sock.c | 41 +++++++++++++++++++++++ pkg/lwip/contrib/sock/tcp/lwip_sock_tcp.c | 25 ++++++++++++++ pkg/lwip/contrib/sock/udp/lwip_sock_udp.c | 27 ++++++++++----- 4 files changed, 98 insertions(+), 8 deletions(-) diff --git a/pkg/lwip/contrib/sock/ip/lwip_sock_ip.c b/pkg/lwip/contrib/sock/ip/lwip_sock_ip.c index e76b19bcc6..de9a337428 100644 --- a/pkg/lwip/contrib/sock/ip/lwip_sock_ip.c +++ b/pkg/lwip/contrib/sock/ip/lwip_sock_ip.c @@ -48,6 +48,7 @@ int sock_ip_create(sock_ip_t *sock, const sock_ip_ep_t *local, netconn_set_callback_arg(sock->base.conn, &sock->base); #endif } + return res; } @@ -88,6 +89,7 @@ static uint16_t _ip4_addr_to_netif(const ip4_addr_p_t *addr) } } } + return SOCK_ADDR_ANY_NETIF; } #endif @@ -111,6 +113,7 @@ static uint16_t _ip6_addr_to_netif(const ip6_addr_p_t *_addr) } UNLOCK_TCPIP_CORE(); } + return SOCK_ADDR_ANY_NETIF; } #endif @@ -168,8 +171,10 @@ static int _parse_iphdr(struct netbuf *buf, void **data, void **ctx, netbuf_delete(buf); return -EPROTO; } + *data = data_ptr; *ctx = buf; + return (ssize_t)data_len; } @@ -196,6 +201,7 @@ ssize_t sock_ip_recv_aux(sock_ip_t *sock, void *data, size_t max_len, ptr += res; ret += res; } + return (nobufs) ? -ENOBUFS : ((res < 0) ? res : ret); } @@ -209,6 +215,7 @@ ssize_t sock_ip_recv_buf_aux(sock_ip_t *sock, void **data, void **ctx, assert((sock != NULL) && (data != NULL) && (ctx != NULL)); buf = *ctx; + if (buf != NULL) { if (netbuf_next(buf) == -1) { *data = NULL; @@ -221,17 +228,22 @@ ssize_t sock_ip_recv_buf_aux(sock_ip_t *sock, void **data, void **ctx, return buf->ptr->len; } } + if ((res = lwip_sock_recv(sock->base.conn, timeout, &buf)) < 0) { return res; } + sock_ip_ep_t *local = NULL; + #if IS_USED(MODULE_SOCK_AUX_LOCAL) if (aux != NULL) { local = &aux->local; aux->flags &= ~(SOCK_AUX_GET_LOCAL); } #endif + res = _parse_iphdr(buf, data, ctx, remote, local); + return res; } @@ -242,6 +254,7 @@ ssize_t sock_ip_send_aux(sock_ip_t *sock, const void *data, size_t len, (void)aux; assert((sock != NULL) || (remote != NULL)); assert((len == 0) || (data != NULL)); /* (len != 0) => (data != NULL) */ + return lwip_sock_send(sock ? sock->base.conn : NULL, data, len, proto, (struct _sock_tl_ep *)remote, NETCONN_RAW); } diff --git a/pkg/lwip/contrib/sock/lwip_sock.c b/pkg/lwip/contrib/sock/lwip_sock.c index e91a303593..e13d7126c6 100644 --- a/pkg/lwip/contrib/sock/lwip_sock.c +++ b/pkg/lwip/contrib/sock/lwip_sock.c @@ -87,6 +87,7 @@ static bool _ep_isany(const struct _sock_tl_ep *ep) if (ep == NULL) { return true; } + ep_addr = (uint8_t *)&ep->addr; for (unsigned i = 0; i < sizeof(ep->addr); i++) { #if LWIP_IPV4 @@ -99,6 +100,7 @@ static bool _ep_isany(const struct _sock_tl_ep *ep) return false; } } + return true; } @@ -108,6 +110,7 @@ static const ip_addr_t *_netif_to_bind_addr(int family, uint16_t netif_num) if (netif_num > UINT8_MAX) { return NULL; } + /* cppcheck-suppress uninitvar ; assigned by macro */ NETIF_FOREACH(netif) { if (netif->num == (netif_num - 1)) { @@ -126,6 +129,7 @@ static const ip_addr_t *_netif_to_bind_addr(int family, uint16_t netif_num) } } } + return NULL; } @@ -156,6 +160,7 @@ static bool _addr_on_netif(int family, int netif_num, const ip_addr_t *addr) } } } + return false; } @@ -177,6 +182,7 @@ static void _convert_ip_addr(ip_addr_t *lwip_addr, int family, if (family == AF_INET6) { ip6_addr_clear_zone(&lwip_addr->u_addr.ip6); } + lwip_addr->type = lwip_af_to_ip_addr_type(family); #elif LWIP_IPV6 (void)family; @@ -212,9 +218,11 @@ static int _sock_ep_to_netconn_pars(const struct _sock_tl_ep *local, if (_af_not_supported(local->family)) { return -EAFNOSUPPORT; } + if (local->netif != SOCK_ADDR_ANY_NETIF) { netif = local->netif; } + family = local->family; _set_port(*local_port, local, *type); } @@ -223,16 +231,20 @@ static int _sock_ep_to_netconn_pars(const struct _sock_tl_ep *local, ((local != NULL) && (local->family != remote->family))) { return -EAFNOSUPPORT; } + if ((remote->netif != SOCK_ADDR_ANY_NETIF) && (local != NULL) && (local->netif != SOCK_ADDR_ANY_NETIF)) { netif = remote->netif; } + family = remote->family; _convert_ip_addr(remote_addr, family, &remote->addr, sizeof(remote->addr)); + if (ip_addr_isany(remote_addr)) { return -EINVAL; } + _set_port(*remote_port, remote, *type); } @@ -278,6 +290,7 @@ static void _netconn_cb(struct netconn *conn, enum netconn_evt evt, { #if IS_ACTIVE(SOCK_HAS_ASYNC) lwip_sock_base_t *sock = netconn_get_callback_arg(conn); + if (sock && conn->pcb.raw && /* lwIP's TCP implementation initializes callback_arg.socket with -1 * when not provided */ @@ -298,9 +311,11 @@ static void _netconn_cb(struct netconn *conn, enum netconn_evt evt, default: break; } + if (mbox_avail(&conn->acceptmbox.mbox)) { flags |= SOCK_ASYNC_CONN_RECV; } + if (mbox_avail(&conn->recvmbox.mbox)) { flags |= SOCK_ASYNC_MSG_RECV; } @@ -326,6 +341,7 @@ static void _netconn_cb(struct netconn *conn, enum netconn_evt evt, LWIP_ASSERT("unknown event", 0); break; } + if (flags && sock->async_cb.gen) { sock->async_cb.gen(sock, flags, sock->async_cb_arg); } @@ -344,6 +360,7 @@ static int _create(int type, int proto, uint16_t flags, struct netconn **out) IS_ACTIVE(SOCK_HAS_ASYNC) ? _netconn_cb : NULL)) == NULL) { return -ENOMEM; } + netconn_set_callback_arg(*out, NULL); #if SO_REUSE if (flags & SOCK_FLAGS_REUSE_EP) { @@ -376,15 +393,18 @@ int lwip_sock_create(struct netconn **conn, const struct _sock_tl_ep *local, if (bind < 0) { return bind; } + if ((remote != NULL) && ip_addr_isany_val(remote_addr)) { return -EINVAL; } /* if local or remote parameters are given */ else if ((local != NULL) || (remote != NULL)) { int res = 0; + if ((res = _create(type, proto, flags, conn)) < 0) { return res; } + /* if parameters (local->netif, remote->netif, local->addr or * local->port) demand binding */ if (bind) { @@ -403,11 +423,13 @@ int lwip_sock_create(struct netconn **conn, const struct _sock_tl_ep *local, default: break; } + if (res < 0) { netconn_delete(*conn); return res; } } + if (remote != NULL) { switch (netconn_connect(*conn, &remote_addr, remote_port)) { #if LWIP_TCP @@ -437,12 +459,14 @@ int lwip_sock_create(struct netconn **conn, const struct _sock_tl_ep *local, default: break; } + if (res < 0) { netconn_delete(*conn); return res; } } } + return 0; } @@ -474,6 +498,7 @@ uint16_t lwip_sock_bind_addr_to_netif(const ip_addr_t *bind_addr) } UNLOCK_TCPIP_CORE(); } + return SOCK_ADDR_ANY_NETIF; } @@ -495,6 +520,7 @@ int lwip_sock_get_addr(struct netconn *conn, struct _sock_tl_ep *ep, u8_t local) if (conn == NULL) { return 1; } + #ifdef MODULE_LWIP_SOCK_IP if (!(conn->type & NETCONN_RAW)) { port_ptr = &ep->port; @@ -509,6 +535,7 @@ int lwip_sock_get_addr(struct netconn *conn, struct _sock_tl_ep *ep, u8_t local) ) { return res; } + if (NETCONNTYPE_ISIPV6(conn->type)) { ep->family = AF_INET6; } @@ -518,13 +545,16 @@ int lwip_sock_get_addr(struct netconn *conn, struct _sock_tl_ep *ep, u8_t local) else { ep->family = AF_UNSPEC; } + if (local) { ep->netif = lwip_sock_bind_addr_to_netif(&addr); } else { ep->netif = SOCK_ADDR_ANY_NETIF; } + memcpy(&ep->addr, &addr, sizeof(ep->addr)); + return 0; } @@ -536,6 +566,7 @@ int lwip_sock_recv(struct netconn *conn, uint32_t timeout, struct netbuf **buf) if (conn == NULL) { return -EADDRNOTAVAIL; } + #if LWIP_SO_RCVTIMEO if ((timeout != 0) && (timeout != SOCK_NO_TIMEOUT)) { netconn_set_recvtimeout(conn, timeout / US_PER_MS); @@ -545,6 +576,7 @@ int lwip_sock_recv(struct netconn *conn, uint32_t timeout, struct netbuf **buf) if ((timeout == 0) && !mbox_avail(&conn->recvmbox.mbox)) { return -EAGAIN; } + switch (netconn_recv(conn, buf)) { case ERR_OK: res = 0; @@ -561,6 +593,7 @@ int lwip_sock_recv(struct netconn *conn, uint32_t timeout, struct netbuf **buf) res = -EPROTO; break; } + /* unset flags */ #if LWIP_SO_RCVTIMEO netconn_set_recvtimeout(conn, 0); @@ -596,6 +629,7 @@ ssize_t lwip_sock_sendv(struct netconn *conn, const iolist_t *snips, &type)) < 0) { return res; } + if (ip_addr_isany_val(remote_addr)) { DEBUG("[lwip_sock_sendv] remote_addr is all zero\n"); return -EINVAL; @@ -603,6 +637,7 @@ ssize_t lwip_sock_sendv(struct netconn *conn, const iolist_t *snips, } buf = netbuf_new(); + if (netbuf_alloc(buf, iolist_size(snips)) == NULL) { netbuf_delete(buf); return -ENOMEM; @@ -655,7 +690,9 @@ ssize_t lwip_sock_sendv(struct netconn *conn, const iolist_t *snips, netbuf_delete(buf); return -ENOTCONN; } + res = payload_len; /* set for non-TCP calls */ + if (remote != NULL) { err = netconn_sendto(tmp, buf, &remote_addr, remote_port); } @@ -667,6 +704,7 @@ ssize_t lwip_sock_sendv(struct netconn *conn, const iolist_t *snips, else { err = netconn_send(tmp, buf); } + switch (err) { case ERR_OK: break; @@ -683,10 +721,13 @@ ssize_t lwip_sock_sendv(struct netconn *conn, const iolist_t *snips, res = -EINVAL; break; } + netbuf_delete(buf); + if (conn == NULL) { netconn_delete(tmp); } + return res; } diff --git a/pkg/lwip/contrib/sock/tcp/lwip_sock_tcp.c b/pkg/lwip/contrib/sock/tcp/lwip_sock_tcp.c index c40e025557..2d3af400d1 100644 --- a/pkg/lwip/contrib/sock/tcp/lwip_sock_tcp.c +++ b/pkg/lwip/contrib/sock/tcp/lwip_sock_tcp.c @@ -53,6 +53,7 @@ int sock_tcp_connect(sock_tcp_t *sock, const sock_tcp_ep_t *remote, flags, NETCONN_TCP)) == 0) { _tcp_sock_init(sock, tmp, NULL); } + return res; } @@ -70,10 +71,12 @@ int sock_tcp_listen(sock_tcp_queue_t *queue, const sock_tcp_ep_t *local, if (queue_len > USHRT_MAX) { return -EFAULT; } + if ((res = lwip_sock_create(&tmp, (struct _sock_tl_ep *)local, NULL, 0, flags, NETCONN_TCP)) < 0) { return res; } + assert(tmp != NULL); /* just in case lwIP is trolling */ mutex_init(&queue->mutex); mutex_lock(&queue->mutex); @@ -84,6 +87,7 @@ int sock_tcp_listen(sock_tcp_queue_t *queue, const sock_tcp_ep_t *local, queue->used = 0; memset(queue->array, 0, sizeof(sock_tcp_t) * queue_len); mutex_unlock(&queue->mutex); + switch (netconn_listen_with_backlog(queue->base.conn, queue->len)) { case ERR_OK: break; @@ -98,6 +102,7 @@ int sock_tcp_listen(sock_tcp_queue_t *queue, const sock_tcp_ep_t *local, * closed and we have a TCP conn */ break; } + return 0; } @@ -105,6 +110,7 @@ void sock_tcp_disconnect(sock_tcp_t *sock) { assert(sock != NULL); mutex_lock(&sock->mutex); + if (sock->base.conn != NULL) { netconn_close(sock->base.conn); netconn_delete(sock->base.conn); @@ -118,6 +124,7 @@ void sock_tcp_disconnect(sock_tcp_t *sock) sock->queue = NULL; } } + mutex_unlock(&sock->mutex); memset(&sock->mutex, 0, sizeof(mutex_t)); } @@ -126,6 +133,7 @@ void sock_tcp_stop_listen(sock_tcp_queue_t *queue) { assert(queue != NULL); mutex_lock(&queue->mutex); + if (queue->base.conn != NULL) { netconn_close(queue->base.conn); netconn_delete(queue->base.conn); @@ -138,6 +146,7 @@ void sock_tcp_stop_listen(sock_tcp_queue_t *queue) queue->len = 0; queue->used = 0; } + mutex_unlock(&queue->mutex); memset(&queue->mutex, 0, sizeof(mutex_t)); } @@ -147,12 +156,14 @@ int sock_tcp_get_local(sock_tcp_t *sock, sock_tcp_ep_t *ep) int res = 0; assert(sock != NULL); mutex_lock(&sock->mutex); + if ((sock->base.conn == NULL) || lwip_sock_get_addr(sock->base.conn, (struct _sock_tl_ep *)ep, 1)) { res = -EADDRNOTAVAIL; } mutex_unlock(&sock->mutex); + return res; } @@ -161,12 +172,15 @@ int sock_tcp_get_remote(sock_tcp_t *sock, sock_tcp_ep_t *ep) int res = 0; assert(sock != NULL); mutex_lock(&sock->mutex); + if ((sock->base.conn == NULL) || lwip_sock_get_addr(sock->base.conn, (struct _sock_tl_ep *)ep, 0)) { res = -ENOTCONN; } + mutex_unlock(&sock->mutex); + return res; } @@ -176,12 +190,15 @@ int sock_tcp_queue_get_local(sock_tcp_queue_t *queue, sock_tcp_ep_t *ep) assert(queue != NULL); mutex_lock(&queue->mutex); + if ((queue->base.conn == NULL) || lwip_sock_get_addr(queue->base.conn, (struct _sock_tl_ep *)ep, 1)) { res = -EADDRNOTAVAIL; } + mutex_unlock(&queue->mutex); + return res; } @@ -192,9 +209,11 @@ int sock_tcp_accept(sock_tcp_queue_t *queue, sock_tcp_t **sock, int res = 0; assert((queue != NULL) && (sock != NULL)); + if (queue->base.conn == NULL) { return -EINVAL; } + if (timeout == 0) { if (!mutex_trylock(&queue->mutex)) { return -EAGAIN; @@ -203,6 +222,7 @@ int sock_tcp_accept(sock_tcp_queue_t *queue, sock_tcp_t **sock, else if (timeout != 0) { mutex_lock(&queue->mutex); } + if (queue->used < queue->len) { #if LWIP_SO_RCVTIMEO if ((timeout != 0) && (timeout != SOCK_NO_TIMEOUT)) { @@ -214,6 +234,7 @@ int sock_tcp_accept(sock_tcp_queue_t *queue, sock_tcp_t **sock, mutex_unlock(&queue->mutex); return -EAGAIN; } + switch (netconn_accept(queue->base.conn, &tmp)) { case ERR_OK: for (unsigned short i = 0; i < queue->len; i++) { @@ -265,6 +286,7 @@ int sock_tcp_accept(sock_tcp_queue_t *queue, sock_tcp_t **sock, } #endif mutex_unlock(&queue->mutex); + return res; } @@ -390,15 +412,18 @@ ssize_t sock_tcp_write(sock_tcp_t *sock, const void *data, size_t len) assert(sock != NULL); assert((len == 0) || (data != NULL)); /* (len != 0) => (data != NULL) */ mutex_lock(&sock->mutex); + if (sock->base.conn == NULL) { mutex_unlock(&sock->mutex); return -ENOTCONN; } + conn = sock->base.conn; mutex_unlock(&sock->mutex); /* we won't change anything to sock here (lwip_sock_send neither, since it remote is NULL) so we can leave the mutex */ res = lwip_sock_send(conn, data, len, 0, NULL, NETCONN_TCP); + return res; } diff --git a/pkg/lwip/contrib/sock/udp/lwip_sock_udp.c b/pkg/lwip/contrib/sock/udp/lwip_sock_udp.c index 7dead578f8..2014a9730a 100644 --- a/pkg/lwip/contrib/sock/udp/lwip_sock_udp.c +++ b/pkg/lwip/contrib/sock/udp/lwip_sock_udp.c @@ -44,6 +44,7 @@ int sock_udp_create(sock_udp_t *sock, const sock_udp_ep_t *local, netconn_set_callback_arg(sock->base.conn, &sock->base); #endif } + return res; } @@ -94,6 +95,7 @@ ssize_t sock_udp_recv_aux(sock_udp_t *sock, void *data, size_t max_len, ptr += res; ret += res; } + return (nobufs) ? -ENOBUFS : ((res < 0) ? res : ret); } @@ -107,6 +109,7 @@ ssize_t sock_udp_recv_buf_aux(sock_udp_t *sock, void **data, void **ctx, assert((sock != NULL) && (data != NULL) && (ctx != NULL)); buf = *ctx; + if (buf != NULL) { if (netbuf_next(buf) == -1) { *data = NULL; @@ -119,15 +122,18 @@ ssize_t sock_udp_recv_buf_aux(sock_udp_t *sock, void **data, void **ctx, return buf->ptr->len; } } + if ((res = lwip_sock_recv(sock->base.conn, timeout, &buf)) < 0) { return res; } + if ((remote != NULL) || ((aux != NULL) && IS_USED(MODULE_SOCK_AUX_LOCAL) && IS_ACTIVE(LWIP_NETBUF_RECVINFO))) { /* convert remote */ size_t addr_len = sizeof(ipv4_addr_t); int family = AF_INET; + if (NETCONNTYPE_ISIPV6(sock->base.conn->type)) { addr_len = sizeof(ipv6_addr_t); family = AF_INET6; @@ -136,6 +142,7 @@ ssize_t sock_udp_recv_buf_aux(sock_udp_t *sock, void **data, void **ctx, netbuf_delete(buf); return -EPROTO; } + if (remote != NULL) { remote->family = family; #if LWIP_NETBUF_RECVINFO @@ -148,18 +155,21 @@ ssize_t sock_udp_recv_buf_aux(sock_udp_t *sock, void **data, void **ctx, remote->port = buf->port; } #if IS_USED(MODULE_SOCK_AUX_LOCAL) - static_assert(IS_ACTIVE(LWIP_NETBUF_RECVINFO), - "sock_aux_local depends on LWIP_NETBUF_RECVINFO"); - if ((aux != NULL) && (aux->flags & SOCK_AUX_GET_LOCAL)) { - aux->flags &= ~(SOCK_AUX_GET_LOCAL); - aux->local.family = family; - memcpy(&aux->local.addr, &buf->toaddr, addr_len); - aux->local.port = sock->base.conn->pcb.udp->local_port; - } + static_assert(IS_ACTIVE(LWIP_NETBUF_RECVINFO), + "sock_aux_local depends on LWIP_NETBUF_RECVINFO"); + + if ((aux != NULL) && (aux->flags & SOCK_AUX_GET_LOCAL)) { + aux->flags &= ~(SOCK_AUX_GET_LOCAL); + aux->local.family = family; + memcpy(&aux->local.addr, &buf->toaddr, addr_len); + aux->local.port = sock->base.conn->pcb.udp->local_port; + } #endif /* MODULE_SOCK_AUX_LOCAL */ } + *data = buf->ptr->payload; *ctx = buf; + return (ssize_t)buf->ptr->len; } @@ -172,6 +182,7 @@ ssize_t sock_udp_sendv_aux(sock_udp_t *sock, const iolist_t *snips, if ((remote != NULL) && (remote->port == 0)) { return -EINVAL; } + return lwip_sock_sendv((sock) ? sock->base.conn : NULL, snips, 0, (struct _sock_tl_ep *)remote, NETCONN_UDP); }