Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netutils: introduce libwebsockets support #2733

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions netutils/libwebsockets/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#

config NETUTILS_LIBWEBSOCKETS
bool "libwebsockets library (current version)"
default n
depends on NET && OPENSSL_MBEDTLS_WRAPPER
---help---
Enables the libwebsockets library.

if NETUTILS_LIBWEBSOCKETS

config NETUTILS_LIBWEBSOCKETS_VERSION
string "Version number"
default "4.3.1"

endif
29 changes: 29 additions & 0 deletions netutils/libwebsockets/Make.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#############################################################################
# apps/netutils/libwebsockets/Make.defs
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
#############################################################################

ifeq ($(CONFIG_NETUTILS_LIBWEBSOCKETS),y)
CONFIGURED_APPS += $(APPDIR)/netutils/libwebsockets

# Allows `<libwebsockets/<>.h>` import.

CFLAGS += ${INCDIR_PREFIX}$(APPDIR)/netutils/libwebsockets/libwebsockets/include
CXXFLAGS += ${INCDIR_PREFIX}$(APPDIR)/netutils/libwebsockets/libwebsockets/include

endif
162 changes: 162 additions & 0 deletions netutils/libwebsockets/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#############################################################################
# apps/netutils/libwebsockets/Makefile
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
#############################################################################

include $(APPDIR)/Make.defs

LIBWEBSOCKETS_URL ?= "https://github.com/warmcat/libwebsockets/archive"

LIBWEBSOCKETS_VERSION := $(patsubst "%",%,$(CONFIG_NETUTILS_LIBWEBSOCKETS_VERSION))
LIBWEBSOCKETS_TARBALL = v$(LIBWEBSOCKETS_VERSION).tar.gz
LIBWEBSOCKETS_UNPACK = libwebsockets

word-dot = $(word $2,$(subst ., ,$1))

CFLAGS += \
-DLWS_LIBRARY_VERSION_MAJOR=$(call word-dot,$(LIBWEBSOCKETS_VERSION),1) \
-DLWS_LIBRARY_VERSION_MINOR=$(call word-dot,$(LIBWEBSOCKETS_VERSION),2) \
-DLWS_LIBRARY_VERSION_PATCH=$(call word-dot,$(LIBWEBSOCKETS_VERSION),3) \
-DLWS_LIBRARY_VERSION_PATCH_ELABORATED=$(call word-dot,$(LIBWEBSOCKETS_VERSION),3)-unknown \
-DLWS_LIBRARY_VERSION=\"$(LIBWEBSOCKETS_VERSION)-unknown\"

CFLAGS += \
-I. \
-I$(LIBWEBSOCKETS_UNPACK)/lib/core \
-I$(LIBWEBSOCKETS_UNPACK)/lib/plat/unix \
-I$(LIBWEBSOCKETS_UNPACK)/lib/event-libs \
-I$(LIBWEBSOCKETS_UNPACK)/lib/system/smd \
-I$(LIBWEBSOCKETS_UNPACK)/lib/system/metrics \
-I$(LIBWEBSOCKETS_UNPACK)/lib/core-net \
-I$(LIBWEBSOCKETS_UNPACK)/lib/roles \
-I$(LIBWEBSOCKETS_UNPACK)/lib/roles/http \
-I$(LIBWEBSOCKETS_UNPACK)/lib/roles/h1 \
-I$(LIBWEBSOCKETS_UNPACK)/lib/roles/h2 \
-I$(LIBWEBSOCKETS_UNPACK)/lib/roles/ws \
-I$(LIBWEBSOCKETS_UNPACK)/lib/tls

CSRCS += \
$(LIBWEBSOCKETS_UNPACK)/lib/plat/unix/unix-caps.c \
$(LIBWEBSOCKETS_UNPACK)/lib/plat/unix/unix-misc.c \
$(LIBWEBSOCKETS_UNPACK)/lib/plat/unix/unix-init.c \
$(LIBWEBSOCKETS_UNPACK)/lib/plat/unix/unix-file.c \
$(LIBWEBSOCKETS_UNPACK)/lib/plat/unix/unix-pipe.c \
$(LIBWEBSOCKETS_UNPACK)/lib/plat/unix/unix-service.c \
$(LIBWEBSOCKETS_UNPACK)/lib/plat/unix/unix-sockets.c \
$(LIBWEBSOCKETS_UNPACK)/lib/plat/unix/unix-fds.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core/alloc.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core/buflist.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core/context.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core/lws_dll2.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core/lws_map.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core/libwebsockets.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core/logs.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core/vfs.c \
$(LIBWEBSOCKETS_UNPACK)/lib/misc/base64-decode.c \
$(LIBWEBSOCKETS_UNPACK)/lib/misc/cache-ttl/lws-cache-ttl.c \
$(LIBWEBSOCKETS_UNPACK)/lib/misc/cache-ttl/heap.c \
$(LIBWEBSOCKETS_UNPACK)/lib/misc/cache-ttl/file.c \
$(LIBWEBSOCKETS_UNPACK)/lib/misc/dir.c \
$(LIBWEBSOCKETS_UNPACK)/lib/misc/prng.c \
$(LIBWEBSOCKETS_UNPACK)/lib/misc/lws-ring.c \
$(LIBWEBSOCKETS_UNPACK)/lib/misc/lwsac/lwsac.c \
$(LIBWEBSOCKETS_UNPACK)/lib/misc/lwsac/cached-file.c \
$(LIBWEBSOCKETS_UNPACK)/lib/misc/lejp.c \
$(LIBWEBSOCKETS_UNPACK)/lib/misc/sha-1.c \
$(LIBWEBSOCKETS_UNPACK)/lib/system/system.c \
$(LIBWEBSOCKETS_UNPACK)/lib/system/smd/smd.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/dummy-callback.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/output.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/close.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/network.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/vhost.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/pollfd.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/service.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/sorted-usec-list.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/wsi.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/wsi-timeout.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/adopt.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/state.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/client/client.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/client/connect.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/client/connect2.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/client/connect3.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/client/connect4.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/client/sort-dns.c \
$(LIBWEBSOCKETS_UNPACK)/lib/core-net/client/conmon.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/pipe/ops-pipe.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/http/header.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/http/date.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/http/parsers.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/http/cookie.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/h1/ops-h1.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/h2/http2.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/h2/hpack.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/h2/ops-h2.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/ws/ops-ws.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/ws/client-ws.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/ws/client-parser-ws.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/raw-skt/ops-raw-skt.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/raw-file/ops-raw-file.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/http/client/client-http.c \
$(LIBWEBSOCKETS_UNPACK)/lib/event-libs/poll/poll.c

CSRCS += \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be wrapped with if for MBED_TLS config?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@W-M-R please take a look at this.

$(LIBWEBSOCKETS_UNPACK)/lib/tls/tls.c \
$(LIBWEBSOCKETS_UNPACK)/lib/tls/tls-network.c \
$(LIBWEBSOCKETS_UNPACK)/lib/tls/tls-sessions.c \
$(LIBWEBSOCKETS_UNPACK)/lib/tls/tls-client.c \
$(LIBWEBSOCKETS_UNPACK)/lib/tls/mbedtls/mbedtls-tls.c \
$(LIBWEBSOCKETS_UNPACK)/lib/tls/mbedtls/mbedtls-extensions.c \
$(LIBWEBSOCKETS_UNPACK)/lib/tls/mbedtls/mbedtls-x509.c \
$(LIBWEBSOCKETS_UNPACK)/lib/tls/mbedtls/mbedtls-ssl.c \
$(LIBWEBSOCKETS_UNPACK)/lib/tls/mbedtls/mbedtls-session.c \
$(LIBWEBSOCKETS_UNPACK)/lib/tls/mbedtls/mbedtls-client.c \

ifneq ($(CONFIG_NETUTILS_MQTTC),)
CFLAGS += -I$(LIBWEBSOCKETS_UNPACK)/lib/roles/mqtt

CSRCS += \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/mqtt/mqtt.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/mqtt/ops-mqtt.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/mqtt/primitives.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/mqtt/client/client-mqtt.c \
$(LIBWEBSOCKETS_UNPACK)/lib/roles/mqtt/client/client-mqtt-handshake.c
endif

ifeq ($(wildcard $(LIBWEBSOCKETS_UNPACK)/.git),)
$(LIBWEBSOCKETS_TARBALL):
$(Q) echo "Downloading libwebsockets-$(LIBWEBSOCKETS_VERSION)"
$(Q) curl -O -L $(LIBWEBSOCKETS_URL)/$(LIBWEBSOCKETS_TARBALL)

$(LIBWEBSOCKETS_UNPACK): $(LIBWEBSOCKETS_TARBALL)
$(Q) echo "Unpacking: $(LIBWEBSOCKETS_TARBALL) -> $(LIBWEBSOCKETS_UNPACK)"
$(Q) tar zxf $(LIBWEBSOCKETS_TARBALL)
$(Q) mv libwebsockets-$(LIBWEBSOCKETS_VERSION) $(LIBWEBSOCKETS_UNPACK)
$(Q) echo "Patching $(LIBWEBSOCKETS_UNPACK)"
$(Q) patch -p0 < libwebsockets.patch
$(Q) touch $(LIBWEBSOCKETS_UNPACK)

context:: $(LIBWEBSOCKETS_UNPACK)

distclean::
$(call DELFILE, $(LIBWEBSOCKETS_TARBALL))
$(call DELDIR, $(LIBWEBSOCKETS_UNPACK))
endif

include $(APPDIR)/Application.mk
112 changes: 112 additions & 0 deletions netutils/libwebsockets/libwebsockets.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
--- libwebsockets/include/libwebsockets.h
+++ libwebsockets/include/libwebsockets.h
@@ -146,7 +146,7 @@ typedef int suseconds_t;
#include <sys/capability.h>
#endif

-#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__QNX__) || defined(__OpenBSD__)
+#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__QNX__) || defined(__OpenBSD__) || defined(__NuttX__)
#include <sys/socket.h>
#include <netinet/in.h>
#endif

--- libwebsockets/lib/plat/unix/private-lib-plat-unix.h
+++ libwebsockets/lib/plat/unix/private-lib-plat-unix.h
@@ -123,7 +123,7 @@ typedef pthread_mutex_t lws_mutex_t;

#endif

-#if defined (__sun) || defined(__HAIKU__) || defined(__QNX__) || defined(__ANDROID__)
+#if defined (__sun) || defined(__HAIKU__) || defined(__QNX__) || defined(__ANDROID__) || defined(__NuttX__)
#include <syslog.h>

#if defined(__ANDROID__)

--- libwebsockets/lib/plat/unix/unix-sockets.c
+++ libwebsockets/lib/plat/unix/unix-sockets.c
@@ -171,7 +171,7 @@ lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)

/* Disable Nagle */
optval = 1;
-#if defined (__sun) || defined(__QNX__)
+#if defined (__sun) || defined(__QNX__) || defined(__NuttX__)
if (!unix_skt && setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
return 1;
#elif !defined(__APPLE__) && \
@@ -190,6 +190,7 @@ lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
return lws_plat_set_nonblocking(fd);
}

+#if !defined(__NuttX__)
static const int ip_opt_lws_flags[] = {
LCCSCF_IP_LOW_LATENCY, LCCSCF_IP_HIGH_THROUGHPUT,
LCCSCF_IP_HIGH_RELIABILITY
@@ -210,6 +211,7 @@ static const char *ip_opt_names[] = {
#endif
};
#endif
+#endif

int
lws_plat_set_socket_options_ip(lws_sockfd_type fd, uint8_t pri, int lws_flags)
@@ -237,7 +239,8 @@ lws_plat_set_socket_options_ip(lws_sockfd_type fd, uint8_t pri, int lws_flags)
!defined(__sun) && \
!defined(__HAIKU__) && \
!defined(__CYGWIN__) && \
- !defined(__QNX__)
+ !defined(__QNX__) && \
+ !defined(__NuttX__)

/* the BSDs don't have SO_PRIORITY */

@@ -255,6 +258,7 @@ lws_plat_set_socket_options_ip(lws_sockfd_type fd, uint8_t pri, int lws_flags)
}
#endif

+#if !defined(__NuttX__)
for (n = 0; n < 4; n++) {
if (!(lws_flags & ip_opt_lws_flags[n]))
continue;
@@ -272,6 +276,7 @@ lws_plat_set_socket_options_ip(lws_sockfd_type fd, uint8_t pri, int lws_flags)
lwsl_notice("%s: set ip flag %s\n", __func__,
ip_opt_names[n]);
}
+#endif

return ret;
}

--- libwebsockets/lib/roles/ws/client-ws.c
+++ libwebsockets/lib/roles/ws/client-ws.c
@@ -257,15 +257,15 @@ lws_client_ws_upgrade(struct lws *wsi, const char **cce)
}

if (wsi->http.ah->http_response == 401) {
- lwsl_wsi_warn(wsi, "got bad HTTP response '%d'",
- wsi->http.ah->http_response);
+ lwsl_wsi_warn(wsi, "got bad HTTP response '%ld'",
+ (long)wsi->http.ah->http_response);
*cce = "HS: ws upgrade unauthorized";
goto bail3;
}

if (wsi->http.ah->http_response != 101) {
- lwsl_wsi_warn(wsi, "got bad HTTP response '%d'",
- wsi->http.ah->http_response);
+ lwsl_wsi_warn(wsi, "got bad HTTP response '%ld'",
+ (long)wsi->http.ah->http_response);
*cce = "HS: ws upgrade response not 101";
goto bail3;
}

--- libwebsockets/lib/secure-streams/system/auth-sigv4/sign.c
+++ libwebsockets/lib/secure-streams/system/auth-sigv4/sign.c
@@ -459,7 +459,7 @@ lws_ss_sigv4_set_aws_key(struct lws_context* context, uint8_t idx,

#if defined(__linux__) || defined(__APPLE__) || defined(WIN32) || \
defined(__FreeBSD__) || defined(__NetBSD__) || defined(__ANDROID__) || \
- defined(__sun) || defined(__OpenBSD__)
+ defined(__sun) || defined(__OpenBSD__) || defined(__NuttX__)

/* ie, if we have filesystem ops */

Loading
Loading