From f4666c2ea9d22dbbec24459246ff0ce9c02d9105 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Mon, 5 Jun 2023 12:10:45 +0100 Subject: [PATCH] LwIP: Support disabling LwIP's IPv6 support --- examples/lwip/client.c | 1 + examples/lwip/config/lwipopts.h | 5 ++++ examples/lwip/server.c | 4 +-- include/coap3/coap_asn1_internal.h | 4 +-- include/coap3/coap_debug.h | 4 +-- include/coap3/coap_mutex_internal.h | 8 ++++- src/coap_address.c | 4 +++ src/coap_asn1.c | 4 +-- src/coap_debug.c | 10 +++++-- src/coap_io_lwip.c | 46 ++++++++++++++++++++++++++--- src/net.c | 3 ++ 11 files changed, 77 insertions(+), 16 deletions(-) diff --git a/examples/lwip/client.c b/examples/lwip/client.c index 9c90c85b..8a4c1183 100644 --- a/examples/lwip/client.c +++ b/examples/lwip/client.c @@ -98,6 +98,7 @@ main(int argc, char **argv) { netif_set_up(&netif); #if LWIP_IPV6 netif_create_ip6_linklocal_address(&netif, 1); + netif_ip6_addr_set_state(&netif, 0, IP6_ADDR_PREFERRED); #endif /* start applications here */ diff --git a/examples/lwip/config/lwipopts.h b/examples/lwip/config/lwipopts.h index 4c4744c7..4a59a05a 100644 --- a/examples/lwip/config/lwipopts.h +++ b/examples/lwip/config/lwipopts.h @@ -29,6 +29,11 @@ #define netif_get_index(netif) ((u8_t)((netif)->num + 1)) #endif +#if NO_SYS +#define LOCK_TCPIP_CORE() +#define UNLOCK_TCPIP_CORE() +#endif + #define MEMP_USE_CUSTOM_POOLS 1 #define MEM_SIZE (4 * 1024) /* Support a 1500 MTU packet */ diff --git a/examples/lwip/server.c b/examples/lwip/server.c index dfcc4f52..0ba53a53 100644 --- a/examples/lwip/server.c +++ b/examples/lwip/server.c @@ -94,11 +94,11 @@ main(int argc, char **argv) { netif.flags |= NETIF_FLAG_ETHARP; netif_set_default(&netif); netif_set_up(&netif); - printf("IP4 %s\n", ip4addr_ntoa(&netif.ip_addr.u_addr.ip4)); + printf("IP4 %s\n", ip4addr_ntoa(ip_2_ip4(&netif.ip_addr))); #if LWIP_IPV6 netif_create_ip6_linklocal_address(&netif, 1); printf("IP6 [%s]\n", ip6addr_ntoa(&netif.ip6_addr[0].u_addr.ip6)); -#endif +#endif /* LWIP_IPV6 */ /* start applications here */ diff --git a/include/coap3/coap_asn1_internal.h b/include/coap3/coap_asn1_internal.h index fd7e8f77..d2f36ecd 100644 --- a/include/coap3/coap_asn1_internal.h +++ b/include/coap3/coap_asn1_internal.h @@ -64,11 +64,11 @@ size_t asn1_len(const uint8_t **ptr); * * @param ptr The current asn.1 object tag pointer * @param constructed 1 if current tag is constructed - * @param class The current class of the tag + * @param cls The current class of the tag * * @return The tag value.@p ptr is updated to be after the tag. */ -coap_asn1_tag_t asn1_tag_c(const uint8_t **ptr, int *constructed, int *class); +coap_asn1_tag_t asn1_tag_c(const uint8_t **ptr, int *constructed, int *cls); /** * Get the asn1 tag and data from the current @p ptr. diff --git a/include/coap3/coap_debug.h b/include/coap3/coap_debug.h index 23bce54e..b479d226 100644 --- a/include/coap3/coap_debug.h +++ b/include/coap3/coap_debug.h @@ -136,7 +136,7 @@ COAP_STATIC_INLINE void coap_no_log(void) { } * * In a future update, they will get removed. */ -#if !defined(RIOT_VERSION) +#if !defined(RIOT_VERSION) && !defined(WITH_LWIP) && !defined(WITH_CONTIKI) #ifndef LOG_EMERG # define LOG_EMERG COAP_LOG_EMERG #endif @@ -161,7 +161,7 @@ COAP_STATIC_INLINE void coap_no_log(void) { } #ifndef LOG_DEBUG # define LOG_DEBUG COAP_LOG_DEBUG #endif -#endif /* ! RIOT_VERSION */ +#endif /* ! RIOT_VERSION && ! WITH_LWIP && ! WITH_CONTIKI */ /** * Get the current logging level. diff --git a/include/coap3/coap_mutex_internal.h b/include/coap3/coap_mutex_internal.h index 1b08e35f..d6249a17 100644 --- a/include/coap3/coap_mutex_internal.h +++ b/include/coap3/coap_mutex_internal.h @@ -58,7 +58,13 @@ typedef int coap_mutex_t; #define coap_mutex_trylock(a) *(a) = 1 #define coap_mutex_unlock(a) *(a) = 0 #else /* !NO SYS */ -#error Need support for LwIP mutex +#include +typedef sys_mutex_t *coap_mutex_t; +#define TOKENPASTE(x, y) x ## y +#define TOKENPASTE2(x, y) TOKENPASTE(x, y) +#define COAP_MUTEX_INITIALIZER (&TOKENPASTE2(coapMutexAt, __LINE__)) +#define coap_mutex_lock(a) sys_mutex_lock(*a) +#define coap_mutex_unlock(a) sys_mutex_unlock(*a) #endif /* !NO SYS */ #elif defined(WITH_CONTIKI) diff --git a/src/coap_address.c b/src/coap_address.c index b8f33148..b7c5bc5f 100644 --- a/src/coap_address.c +++ b/src/coap_address.c @@ -455,13 +455,17 @@ coap_resolve_address_info(const coap_str_const_t *server, sock4 = (struct sockaddr_in *)ainfo->ai_addr; info->addr.port = ntohs(sock4->sin_port); memcpy(&info->addr.addr, &sock4->sin_addr, 4); +#if LWIP_IPV6 info->addr.addr.type = IPADDR_TYPE_V4; +#endif /* LWIP_IPV6 */ break; case AF_INET6: sock6 = (struct sockaddr_in6 *)ainfo->ai_addr; info->addr.port = ntohs(sock6->sin6_port); memcpy(&info->addr.addr, &sock6->sin6_addr, 16); +#if LWIP_IPV6 info->addr.addr.type = IPADDR_TYPE_V6; +#endif /* LWIP_IPV6 */ break; default: ; diff --git a/src/coap_asn1.c b/src/coap_asn1.c index bfa0907e..124d631c 100644 --- a/src/coap_asn1.c +++ b/src/coap_asn1.c @@ -37,14 +37,14 @@ asn1_len(const uint8_t **ptr) } coap_asn1_tag_t -asn1_tag_c(const uint8_t **ptr, int *constructed, int *class) +asn1_tag_c(const uint8_t **ptr, int *constructed, int *cls) { coap_asn1_tag_t tag = 0; uint8_t byte; byte = (**ptr); *constructed = (byte & 0x20) ? 1 : 0; - *class = byte >> 6; + *cls = byte >> 6; tag = byte & 0x1F; (*ptr)++; if (tag < 0x1F) diff --git a/src/coap_debug.c b/src/coap_debug.c index af7ceedc..34f06d88 100644 --- a/src/coap_debug.c +++ b/src/coap_debug.c @@ -303,13 +303,14 @@ coap_print_addr(const coap_address_t *addr, unsigned char *buf, size_t len) { assert(len); buf[0] = '\000'; - switch (addr->addr.type) { + switch (IP_GET_TYPE(addr->addr)) { case IPADDR_TYPE_V4: if (len < IP4ADDR_STRLEN_MAX + 6) return 0; memcpy(buf, coap_print_ip_addr(addr, scratch, sizeof(scratch)), IP4ADDR_STRLEN_MAX); p += strlen((char *)buf); break; +#if LWIP_IPV6 case IPADDR_TYPE_V6: case IPADDR_TYPE_ANY: if (len < 40 + 2 + 6) @@ -319,6 +320,7 @@ coap_print_addr(const coap_address_t *addr, unsigned char *buf, size_t len) { p += strlen((char *)buf); *p++ = ']'; break; +#endif /* LWIP_IPV6 */ } *p++ = ':'; @@ -426,18 +428,20 @@ coap_print_ip_addr(const coap_address_t *addr, char *buf, size_t len) { assert(len); buf[0] = '\000'; - switch (addr->addr.type) { + switch (IP_GET_TYPE(&addr->addr)) { case IPADDR_TYPE_V4: if (len < IP4ADDR_STRLEN_MAX) return buf; - memcpy(buf, ip4addr_ntoa(&addr->addr.u_addr.ip4), IP4ADDR_STRLEN_MAX); + memcpy(buf, ip4addr_ntoa(ip_2_ip4(&addr->addr)), IP4ADDR_STRLEN_MAX); break; +#if LWIP_IPV6 case IPADDR_TYPE_V6: case IPADDR_TYPE_ANY: if (len < 40) return buf; memcpy(buf, ip6addr_ntoa(&addr->addr.u_addr.ip6), 40); break; +#endif /* LWIP_IPV6 */ } return buf; diff --git a/src/coap_io_lwip.c b/src/coap_io_lwip.c index 895e3ddd..c8cfbc8a 100644 --- a/src/coap_io_lwip.c +++ b/src/coap_io_lwip.c @@ -17,6 +17,7 @@ #include "coap3/coap_internal.h" #include #include +#include void coap_lwip_dump_memory_pools(coap_log_t log_level) { @@ -88,6 +89,9 @@ coap_io_process(coap_context_t *context, uint32_t timeout_ms) { timeout > timeout_ms) { timeout = timeout_ms; } + + LOCK_TCPIP_CORE(); + if (context->timer_configured) { sys_untimeout(coap_io_process_timeout, (void*)context); context->timer_configured = 0; @@ -102,10 +106,19 @@ coap_io_process(coap_context_t *context, uint32_t timeout_ms) { #endif /* COAP_DEBUG_WAKEUP_TIMES */ sys_timeout(timeout, coap_io_process_timeout, context); context->timer_configured = 1; + + UNLOCK_TCPIP_CORE(); + if (context->input_wait) { context->input_wait(context->input_arg, timeout); } + + LOCK_TCPIP_CORE(); + sys_check_timeouts(); + + UNLOCK_TCPIP_CORE(); + coap_ticks(&now); return (int)(((now - before) * 1000) / COAP_TICKS_PER_SECOND); } @@ -316,9 +329,14 @@ coap_socket_send(coap_socket_t *sock, const coap_session_t *session, if (pbuf == NULL) return -1; memcpy(pbuf->payload, data, data_len); + + LOCK_TCPIP_CORE(); + err = udp_sendto(sock->pcb, pbuf, &session->addr_info.remote.addr, session->addr_info.remote.port); + UNLOCK_TCPIP_CORE(); + pbuf_free(pbuf); if (err < 0) return -1; @@ -338,8 +356,10 @@ coap_socket_bind_udp(coap_socket_t *sock, if (sock->pcb == NULL) return 0; +#if LWIP_IPV6 if (l_listen.addr.type == IPADDR_TYPE_V6) l_listen.addr.type = IPADDR_TYPE_ANY; +#endif /* LWIP_IPV6 */ udp_recv(sock->pcb, coap_recvs, (void*)sock->endpoint); err = udp_bind(sock->pcb, &l_listen.addr, l_listen.port); if (err) { @@ -366,31 +386,47 @@ coap_socket_connect_udp(coap_socket_t *sock, (void)default_port; (void)local_addr; (void)remote_addr; + + LOCK_TCPIP_CORE(); + pcb = udp_new(); if (!pcb) { - return 0; + goto err_unlock; } err = udp_bind(pcb, &pcb->local_ip, pcb->local_port); if (err) { LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("coap_socket_connect_udp: port bind failed\n")); - return 0; + goto err_udp_remove; } sock->session->addr_info.local.port = pcb->local_port; err = udp_connect(pcb, &server->addr, server->port); if (err) { - return 0; + goto err_udp_unbind; } +#if LWIP_IPV6 + pcb->local_ip.type = pcb->remote_ip.type; +#endif /* LWIP_IPV6 */ + sock->pcb = pcb; udp_recv(sock->pcb, coap_recvc, (void*)sock->session); + UNLOCK_TCPIP_CORE(); + return 1; + +err_udp_unbind: +err_udp_remove: + udp_remove(pcb); +err_unlock: + UNLOCK_TCPIP_CORE(); + return 0; } #endif /* ! COAP_CLIENT_SUPPORT */ @@ -461,8 +497,10 @@ coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len) { } void coap_socket_close(coap_socket_t *sock) { - if (sock->pcb){ + if (sock->pcb) { + LOCK_TCPIP_CORE(); udp_remove(sock->pcb); + UNLOCK_TCPIP_CORE(); } sock->pcb = NULL; return; diff --git a/src/net.c b/src/net.c index f0c93d94..3e6544fe 100644 --- a/src/net.c +++ b/src/net.c @@ -61,6 +61,7 @@ #include #include #include +#include #endif #ifndef INET6_ADDRSTRLEN @@ -569,7 +570,9 @@ coap_free_context(coap_context_t *context) { #ifdef WITH_LWIP context->sendqueue = NULL; if (context->timer_configured) { + LOCK_TCPIP_CORE(); sys_untimeout(coap_io_process_timeout, (void*)context); + UNLOCK_TCPIP_CORE(); context->timer_configured = 0; } #endif /* WITH_LWIP */