diff --git a/include/libwebsockets/lws-adopt.h b/include/libwebsockets/lws-adopt.h index 5a4f1b65a..e6caf0087 100644 --- a/include/libwebsockets/lws-adopt.h +++ b/include/libwebsockets/lws-adopt.h @@ -64,12 +64,12 @@ LWS_VISIBLE LWS_EXTERN struct lws * lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd); typedef enum { - LWS_ADOPT_RAW_FILE_DESC = 0, /* convenience constant */ - LWS_ADOPT_HTTP = 1, /* flag: absent implies RAW */ - LWS_ADOPT_SOCKET = 2, /* flag: absent implies file descr */ - LWS_ADOPT_ALLOW_SSL = 4, /* flag: if set requires LWS_ADOPT_SOCKET */ - LWS_ADOPT_FLAG_UDP = 16, /* flag: socket is UDP */ - LWS_ADOPT_FLAG_RAW_PROXY = 32, /* flag: raw proxy */ + LWS_ADOPT_RAW_FILE_DESC = 0, /* convenience constant */ + LWS_ADOPT_HTTP = 1, /* flag: absent implies RAW */ + LWS_ADOPT_SOCKET = 2, /* flag: absent implies file */ + LWS_ADOPT_ALLOW_SSL = 4, /* flag: use tls */ + LWS_ADOPT_FLAG_UDP = 16, /* flag: socket is UDP */ + LWS_ADOPT_FLAG_RAW_PROXY = 32, /* flag: raw proxy */ LWS_ADOPT_RAW_SOCKET_UDP = LWS_ADOPT_SOCKET | LWS_ADOPT_FLAG_UDP, } lws_adoption_type; @@ -79,6 +79,10 @@ typedef union { lws_filefd_type filefd; } lws_sock_file_fd_type; +#if defined(LWS_ESP_PLATFORM) +#include +#endif + typedef union { #if defined(LWS_WITH_IPV6) struct sockaddr_in6 sa6; @@ -86,14 +90,23 @@ typedef union { struct sockaddr_in sa4; } lws_sockaddr46; +#define sa46_sockaddr(_sa46) ((struct sockaddr *)(_sa46)) + +#define sa46_address(_sa46) ((uint8_t *)((_sa46)->sa4.sin_family == AF_INET ? \ + &_sa46->sa4.sin_addr : &_sa46->sa6.sin6_addr )) + +#define sa46_address_len(_sa46) ((_sa46)->sa4.sin_family == AF_INET ? 4 : 16) + +#define sa46_socklen(_sa46) ((_sa46)->sa4.sin_family == AF_INET ? \ + sizeof(struct sockaddr_in) : \ + sizeof(struct sockaddr_in6)) + #if defined(LWS_WITH_UDP) struct lws_udp { - struct sockaddr sa; - socklen_t salen; - - struct sockaddr sa_pending; - socklen_t salen_pending; + lws_sockaddr46 sa46; + lws_sockaddr46 sa46_pending; }; + #endif /** diff --git a/lib/core-net/adopt.c b/lib/core-net/adopt.c index e6aa8009b..ced90ca62 100644 --- a/lib/core-net/adopt.c +++ b/lib/core-net/adopt.c @@ -709,9 +709,9 @@ lws_create_adopt_udp2(struct lws *wsi, const char *ads, goto resume; } #endif - memcpy(&wsi->udp->sa, wsi->dns_results_next->ai_addr, + memcpy(&wsi->udp->sa46, + wsi->dns_results_next->ai_addr, wsi->dns_results_next->ai_addrlen); - wsi->udp->salen = (socklen_t)wsi->dns_results_next->ai_addrlen; } /* we connected: complete the udp socket adoption flow */ diff --git a/lib/core-net/output.c b/lib/core-net/output.c index 264f88e55..ce683a219 100644 --- a/lib/core-net/output.c +++ b/lib/core-net/output.c @@ -218,11 +218,9 @@ lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len) lws_stats_bump(pt, LWSSTATS_B_PARTIALS_ACCEPTED_PARTS, m); #if defined(LWS_WITH_UDP) - if (lws_wsi_is_udp(wsi)) { + if (lws_wsi_is_udp(wsi)) /* stash original destination for fulfilling UDP partials */ - wsi->udp->sa_pending = wsi->udp->sa; - wsi->udp->salen_pending = wsi->udp->salen; - } + wsi->udp->sa46_pending = wsi->udp->sa46; #endif /* since something buffered, force it to get another chance to send */ @@ -301,9 +299,10 @@ lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len) errno = 0; #if defined(LWS_WITH_UDP) if (lws_wsi_is_udp(wsi)) { - wsi->udp->salen = sizeof(wsi->udp->sa); + socklen_t slt = sizeof(wsi->udp->sa46); + n = recvfrom(wsi->desc.sockfd, (char *)buf, len, 0, - &wsi->udp->sa, &wsi->udp->salen); + sa46_sockaddr(&wsi->udp->sa46), &slt); } else #endif n = recv(wsi->desc.sockfd, (char *)buf, len, 0); @@ -365,11 +364,12 @@ lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len) } if (lws_has_buffered_out(wsi)) n = sendto(wsi->desc.sockfd, (const char *)buf, - len, 0, &wsi->udp->sa_pending, - wsi->udp->salen_pending); + len, 0, sa46_sockaddr(&wsi->udp->sa46_pending), + sa46_socklen(&wsi->udp->sa46_pending)); else n = sendto(wsi->desc.sockfd, (const char *)buf, - len, 0, &wsi->udp->sa, wsi->udp->salen); + len, 0, sa46_sockaddr(&wsi->udp->sa46), + sa46_socklen(&wsi->udp->sa46)); } else #endif if (wsi->role_ops->file_handle) diff --git a/minimal-examples/raw/minimal-raw-adopt-udp/minimal-raw-adopt-udp.c b/minimal-examples/raw/minimal-raw-adopt-udp/minimal-raw-adopt-udp.c index 415611f5c..9a9d11422 100644 --- a/minimal-examples/raw/minimal-raw-adopt-udp/minimal-raw-adopt-udp.c +++ b/minimal-examples/raw/minimal-raw-adopt-udp/minimal-raw-adopt-udp.c @@ -113,7 +113,8 @@ callback_raw_test(struct lws *wsi, enum lws_callback_reasons reason, #if defined(WIN32) (int) #endif - sendlen, 0, &udp.sa, (socklen_t)udp.salen); + sendlen, 0, sa46_sockaddr(&udp.sa46), + sa46_socklen(&udp.sa46)); if (n < (ssize_t)len) lwsl_notice("%s: send returned %d\n", __func__, (int)n); break;