diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c9d53c3d..77d8de81c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -140,6 +140,8 @@ option(LWS_WITH_LWS_DSH "Support lws_dsh_t Disordered Shared Heap" OFF) option(LWS_CLIENT_HTTP_PROXYING "Support external http proxies for client connections" ON) option(LWS_WITH_FILE_OPS "Support file operations vfs" ON) option(LWS_WITH_DETAILED_LATENCY "Record detailed latency stats for each read and write" OFF) +option(LWS_WITH_UDP "Platform supports UDP" ON) + # # to use miniz, enable both LWS_WITH_ZLIB and LWS_WITH_MINIZ # @@ -160,6 +162,10 @@ if (LWS_WITH_ESP32) set(LWS_PLAT_FREERTOS 1) endif() +if (LWS_PLAT_FREERTOS OR LWS_PLAT_OPTEE) + set(LWS_WITH_UDP 0) +endif() + if (WIN32 OR LWS_PLAT_FREERTOS) message(STATUS "No LWS_WITH_DIR and LWS_WITH_DIR") set(LWS_WITH_DIR OFF) diff --git a/cmake/lws_config.h.in b/cmake/lws_config.h.in index 4fbb868a7..160eee10a 100644 --- a/cmake/lws_config.h.in +++ b/cmake/lws_config.h.in @@ -143,6 +143,7 @@ #cmakedefine LWS_WITH_SYS_NTPCLIENT #cmakedefine LWS_WITH_THREADPOOL #cmakedefine LWS_WITH_TLS +#cmakedefine LWS_WITH_UDP #cmakedefine LWS_WITH_UNIX_SOCK #cmakedefine LWS_WITH_ZIP_FOPS #cmakedefine USE_OLD_CYASSL diff --git a/include/libwebsockets/lws-adopt.h b/include/libwebsockets/lws-adopt.h index 20058c4f7..cbbf29f91 100644 --- a/include/libwebsockets/lws-adopt.h +++ b/include/libwebsockets/lws-adopt.h @@ -79,7 +79,7 @@ typedef union { lws_filefd_type filefd; } lws_sock_file_fd_type; -#if !defined(LWS_PLAT_FREERTOS) && !defined(LWS_PLAT_OPTEE) +#if defined(LWS_WITH_UDP) struct lws_udp { struct sockaddr sa; socklen_t salen; @@ -168,6 +168,7 @@ lws_adopt_socket_vhost_readbuf(struct lws_vhost *vhost, #define LWS_CAUDP_BIND 1 +#if defined(LWS_WITH_UDP) /** * lws_create_adopt_udp() - create, bind and adopt a UDP socket * @@ -186,4 +187,5 @@ LWS_VISIBLE LWS_EXTERN struct lws * lws_create_adopt_udp(struct lws_vhost *vhost, const char *ads, int port, int flags, const char *protocol_name, struct lws *parent_wsi, const lws_retry_bo_t *retry_policy); +#endif ///@} diff --git a/include/libwebsockets/lws-async-dns.h b/include/libwebsockets/lws-async-dns.h index 0db2164e4..773867d4c 100644 --- a/include/libwebsockets/lws-async-dns.h +++ b/include/libwebsockets/lws-async-dns.h @@ -22,6 +22,8 @@ * IN THE SOFTWARE. */ +#if defined(LWS_WITH_UDP) + typedef enum dns_query_type { LWS_ADNS_RECORD_A = 0x01, LWS_ADNS_RECORD_CNAME = 0x05, @@ -79,3 +81,5 @@ lws_async_dns_query(struct lws_context *context, int tsi, const char *name, */ LWS_VISIBLE LWS_EXTERN void lws_async_dns_freeaddrinfo(const struct addrinfo **ai); + +#endif diff --git a/include/libwebsockets/lws-freertos.h b/include/libwebsockets/lws-freertos.h index b6899d916..4b2616b67 100644 --- a/include/libwebsockets/lws-freertos.h +++ b/include/libwebsockets/lws-freertos.h @@ -56,6 +56,7 @@ struct pollfd { #include #include #include "timers.h" +#include #else /* LWS_AMAZON_RTOS */ #include #include diff --git a/lib/core-net/adopt.c b/lib/core-net/adopt.c index 868c13ea1..c1ed82d6f 100644 --- a/lib/core-net/adopt.c +++ b/lib/core-net/adopt.c @@ -423,6 +423,7 @@ bail: return NULL; } +#if defined(LWS_WITH_UDP) #if defined(LWS_WITH_CLIENT) static struct lws * lws_create_adopt_udp2(struct lws *wsi, const char *ads, @@ -630,6 +631,7 @@ bail: #endif } #endif +#endif LWS_VISIBLE struct lws * lws_adopt_socket_readbuf(struct lws_context *context, lws_sockfd_type accept_fd, diff --git a/lib/core-net/close.c b/lib/core-net/close.c index 07375c122..65450cf08 100644 --- a/lib/core-net/close.c +++ b/lib/core-net/close.c @@ -84,7 +84,9 @@ __lws_reset_wsi(struct lws *wsi) lws_buflist_destroy_all_segments(&wsi->buflist); lws_buflist_destroy_all_segments(&wsi->buflist_out); +#if defined(LWS_WITH_UDP) lws_free_set_NULL(wsi->udp); +#endif wsi->retry = 0; #if defined(LWS_WITH_CLIENT) @@ -414,8 +416,10 @@ just_kill_connection: if (wsi->http.buflist_post_body) lws_buflist_destroy_all_segments(&wsi->http.buflist_post_body); #endif +#if defined(LWS_WITH_UDP) if (wsi->udp) lws_free_set_NULL(wsi->udp); +#endif if (wsi->role_ops->close_kill_connection) wsi->role_ops->close_kill_connection(wsi, reason); diff --git a/lib/core-net/output.c b/lib/core-net/output.c index cca8dc382..6d16875b4 100644 --- a/lib/core-net/output.c +++ b/lib/core-net/output.c @@ -205,7 +205,7 @@ lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len) lws_stats_bump(pt, LWSSTATS_C_WRITE_PARTIALS, 1); lws_stats_bump(pt, LWSSTATS_B_PARTIALS_ACCEPTED_PARTS, m); -#if !defined(LWS_PLAT_FREERTOS) && !defined(LWS_PLAT_OPTEE) +#if defined(LWS_WITH_UDP) if (lws_wsi_is_udp(wsi)) { /* stash original destination for fulfilling UDP partials */ wsi->udp->sa_pending = wsi->udp->sa; @@ -287,13 +287,13 @@ lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len) lws_stats_bump(pt, LWSSTATS_C_API_READ, 1); errno = 0; +#if defined(LWS_WITH_UDP) if (lws_wsi_is_udp(wsi)) { -#if !defined(LWS_PLAT_FREERTOS) && !defined(LWS_PLAT_OPTEE) wsi->udp->salen = sizeof(wsi->udp->sa); n = recvfrom(wsi->desc.sockfd, (char *)buf, len, 0, &wsi->udp->sa, &wsi->udp->salen); -#endif } else +#endif n = recv(wsi->desc.sockfd, (char *)buf, len, 0); if (n >= 0) { @@ -334,8 +334,8 @@ lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len) ssize_t send(int sockfd, const void *buf, size_t len, int flags); #endif +#if defined(LWS_WITH_UDP) if (lws_wsi_is_udp(wsi)) { -#if !defined(LWS_PLAT_FREERTOS) && !defined(LWS_PLAT_OPTEE) if (wsi->context->udp_loss_sim_tx_pc) { uint16_t u16; /* @@ -358,12 +358,14 @@ lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len) else n = sendto(wsi->desc.sockfd, (const char *)buf, len, 0, &wsi->udp->sa, wsi->udp->salen); -#endif } else +#endif n = send(wsi->desc.sockfd, (char *)buf, len, MSG_NOSIGNAL); // lwsl_info("%s: sent len %d result %d", __func__, len, n); +#if defined(LWS_WITH_UDP) post_send: +#endif if (n >= 0) return n; diff --git a/lib/core-net/private-lib-core-net.h b/lib/core-net/private-lib-core-net.h index 72cd46ec8..680326dbf 100644 --- a/lib/core-net/private-lib-core-net.h +++ b/lib/core-net/private-lib-core-net.h @@ -245,7 +245,9 @@ struct client_info_stash { }; #endif +#if defined(LWS_WITH_UDP) #define lws_wsi_is_udp(___wsi) (!!___wsi->udp) +#endif #define LWS_H2_FRAME_HEADER_LENGTH 9 @@ -644,7 +646,9 @@ struct lws { struct lws_peer *peer; #endif +#if defined(LWS_WITH_UDP) struct lws_udp *udp; +#endif #if defined(LWS_WITH_CLIENT) struct client_info_stash *stash; char *cli_hostname_copy; diff --git a/lib/core-net/wsi.c b/lib/core-net/wsi.c index 47a4f2177..1e07996bb 100644 --- a/lib/core-net/wsi.c +++ b/lib/core-net/wsi.c @@ -739,11 +739,13 @@ lws_protocol_get(struct lws *wsi) return wsi->protocol; } +#if defined(LWS_WITH_UDP) LWS_VISIBLE const struct lws_udp * lws_get_udp(const struct lws *wsi) { return wsi->udp; } +#endif LWS_VISIBLE LWS_EXTERN struct lws_context * lws_get_context(const struct lws *wsi) diff --git a/lib/roles/raw-proxy/ops-raw-proxy.c b/lib/roles/raw-proxy/ops-raw-proxy.c index 5c098843f..9fa3bc381 100644 --- a/lib/roles/raw-proxy/ops-raw-proxy.c +++ b/lib/roles/raw-proxy/ops-raw-proxy.c @@ -127,11 +127,13 @@ rops_adoption_bind_raw_proxy(struct lws *wsi, int type, (!(type & LWS_ADOPT_FLAG_RAW_PROXY)) || (type & _LWS_ADOPT_FINISH)) return 0; /* no match */ +#if defined(LWS_WITH_UDP) if (type & LWS_ADOPT_FLAG_UDP) /* * these can be >128 bytes, so just alloc for UDP */ wsi->udp = lws_malloc(sizeof(*wsi->udp), "udp struct"); +#endif lws_role_transition(wsi, LWSIFR_SERVER, (type & LWS_ADOPT_ALLOW_SSL) ? LRS_SSL_INIT : LRS_ESTABLISHED, diff --git a/lib/roles/raw-skt/ops-raw-skt.c b/lib/roles/raw-skt/ops-raw-skt.c index 83055f9bf..ab660230e 100644 --- a/lib/roles/raw-skt/ops-raw-skt.c +++ b/lib/roles/raw-skt/ops-raw-skt.c @@ -92,6 +92,7 @@ rops_handle_POLLIN_raw_skt(struct lws_context_per_thread *pt, struct lws *wsi, goto try_pollout; } +#if defined(LWS_WITH_UDP) if (wsi->context->udp_loss_sim_rx_pc) { uint16_t u16; /* @@ -107,12 +108,15 @@ rops_handle_POLLIN_raw_skt(struct lws_context_per_thread *pt, struct lws *wsi, goto post_rx; } } +#endif n = user_callback_handle_rxflow(wsi->protocol->callback, wsi, LWS_CALLBACK_RAW_RX, wsi->user_space, ebuf.token, ebuf.len); +#if defined(LWS_WITH_UDP) post_rx: +#endif if (n < 0) { lwsl_info("LWS_CALLBACK_RAW_RX_fail\n"); goto fail; @@ -183,7 +187,7 @@ rops_adoption_bind_raw_skt(struct lws *wsi, int type, const char *vh_prot_name) (type & _LWS_ADOPT_FINISH)) return 0; /* no match */ -#if !defined(LWS_PLAT_FREERTOS) && !defined(LWS_PLAT_OPTEE) +#if defined(LWS_WITH_UDP) if (type & LWS_ADOPT_FLAG_UDP) /* * these can be >128 bytes, so just alloc for UDP diff --git a/minimal-examples/raw/minimal-raw-adopt-udp/CMakeLists.txt b/minimal-examples/raw/minimal-raw-adopt-udp/CMakeLists.txt index aad6f0333..e3e4f5a97 100644 --- a/minimal-examples/raw/minimal-raw-adopt-udp/CMakeLists.txt +++ b/minimal-examples/raw/minimal-raw-adopt-udp/CMakeLists.txt @@ -64,6 +64,7 @@ ENDMACRO() set(requirements 1) require_lws_config(LWS_WITH_SERVER 1 requirements) require_lws_config(LWS_WITH_CLIENT 1 requirements) +require_lws_config(LWS_WITH_UDP 1 requirements) if (requirements)