Skip to content

Commit 91463b9

Browse files
committed
Fix emNET support and add tests
The emNET `wolfSSL_LastError` branches were incorrect. The second one was never hit and would never compile. The first one inverts error codes that should not be inverted. This fixes that code and adds a test with a shim layer to test emNET calls without using emNET.
1 parent 43e44cb commit 91463b9

7 files changed

Lines changed: 488 additions & 6 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: emNET non-blocking handshake test
2+
3+
# START OF COMMON SECTION
4+
on:
5+
push:
6+
branches: [ 'master', 'main', 'release/**' ]
7+
pull_request:
8+
branches: [ '*' ]
9+
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.ref }}
12+
cancel-in-progress: true
13+
# END OF COMMON SECTION
14+
15+
# Build wolfSSL with -DWOLFSSL_EMNET using the clean-room shim in
16+
# tests/emnet/ (IP/IP.h + emnet_shim.c), link a non-blocking TLS 1.3
17+
# handshake test with -Wl,--wrap=recv,--wrap=send, and run it. The
18+
# test exercises the WOLFSSL_EMNET path in wolfSSL_LastError() and
19+
# asserts that would-block events surface as WANT_READ/WANT_WRITE and
20+
# the handshake completes, guarding against regressions in the emNET
21+
# error-translation logic.
22+
23+
jobs:
24+
emnet_nonblock:
25+
name: wolfSSL emNET non-blocking handshake
26+
if: github.repository_owner == 'wolfssl'
27+
runs-on: ubuntu-24.04
28+
timeout-minutes: 20
29+
steps:
30+
- name: Checkout wolfSSL
31+
uses: actions/checkout@v4
32+
33+
- name: Install build deps
34+
uses: ./.github/actions/install-apt-deps
35+
with:
36+
packages: autoconf automake libtool build-essential
37+
38+
- name: Bootstrap
39+
run: ./autogen.sh
40+
41+
- name: Configure wolfSSL (WOLFSSL_EMNET + emNET shim headers)
42+
run: |
43+
./configure \
44+
--enable-static --disable-shared \
45+
--enable-tls13 --disable-oldtls \
46+
--enable-ecc --disable-examples \
47+
CFLAGS="-DWOLFSSL_EMNET -I$(pwd)/tests/emnet"
48+
49+
- name: Build wolfSSL
50+
run: make -j$(nproc)
51+
52+
- name: Build emNET non-blocking test
53+
run: make -C tests/emnet
54+
55+
- name: Run emNET non-blocking test
56+
run: make -C tests/emnet run

src/wolfio.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,12 @@ static WC_INLINE int wolfSSL_LastError(int err, SOCKET_T sd)
151151
return WSAGetLastError();
152152
#elif defined(EBSNET)
153153
return xn_getlasterror();
154-
#elif defined(WOLFSSL_LINUXKM) || defined(WOLFSSL_EMNET)
154+
#elif defined(WOLFSSL_LINUXKM)
155155
return -err; /* Return provided error value with corrected sign. */
156+
#elif defined(WOLFSSL_EMNET)
157+
/* emNET BSD sockets return the IP_ERR_* value (negative) directly
158+
* from send/recv on failure; no translation needed. */
159+
return err;
156160
#elif defined(FUSION_RTOS)
157161
#include <fclerrno.h>
158162
return FCL_GET_ERRNO;
@@ -170,10 +174,6 @@ static WC_INLINE int wolfSSL_LastError(int err, SOCKET_T sd)
170174
}
171175
return err;
172176
}
173-
#elif defined(WOLFSSL_EMNET)
174-
/* Get the real socket error */
175-
IP_SOCK_getsockopt(sd, SOL_SOCKET, SO_ERROR, &err, (int)sizeof(old));
176-
return err;
177177
#else
178178
return errno;
179179
#endif

tests/emnet/IP/IP.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* IP.h -- clean-room shim for the subset of SEGGER emNET (embOS/IP) API
2+
* that wolfSSL's WOLFSSL_EMNET port compiles against. Written from the
3+
* public API surface documented in SEGGER UM07001; contains no SEGGER
4+
* source.
5+
*
6+
* Scope: enough to build wolfSSL with -DWOLFSSL_EMNET on a POSIX host
7+
* for CI test purposes. Only error constants and IP_SOCK_getsockopt are
8+
* provided here; the runtime behaviour of send/recv under emNET is
9+
* emulated by emnet_shim.c via linker --wrap.
10+
*/
11+
12+
#ifndef WOLFSSL_EMNET_SHIM_IP_H
13+
#define WOLFSSL_EMNET_SHIM_IP_H
14+
15+
#include <sys/types.h>
16+
#include <sys/socket.h>
17+
#include <netinet/in.h>
18+
#include <arpa/inet.h>
19+
#include <unistd.h>
20+
#include <fcntl.h>
21+
#include <errno.h>
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
/* emNET error codes (UM07001). Values match the public ABI. */
28+
#define IP_ERR_CONN_ABORTED (-5)
29+
#define IP_ERR_WOULD_BLOCK (-6)
30+
#define IP_ERR_CONN_REFUSED (-7)
31+
#define IP_ERR_CONN_RESET (-8)
32+
#define IP_ERR_PIPE (-13)
33+
#define IP_ERR_FAULT (-25)
34+
35+
/* BSD-style socket option retrieval. Signature matches the SEGGER API:
36+
* length is passed by pointer of type int*, unlike POSIX socklen_t*. */
37+
int IP_SOCK_getsockopt(int sd, int level, int optname,
38+
void *optval, int *optlen);
39+
40+
#ifdef __cplusplus
41+
}
42+
#endif
43+
44+
#endif /* WOLFSSL_EMNET_SHIM_IP_H */

tests/emnet/Makefile

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Build the emNET non-blocking handshake test.
2+
# Requires wolfSSL already configured + built at the repo root with
3+
# WOLFSSL_EMNET and the emnet IP include path, e.g.:
4+
#
5+
# cd $(repo_root)
6+
# ./autogen.sh
7+
# ./configure --enable-static --disable-shared --enable-tls13 \
8+
# --disable-oldtls CFLAGS="-DWOLFSSL_EMNET \
9+
# -I$$(pwd)/tests/emnet"
10+
# make
11+
# make -C tests/emnet run
12+
13+
CURDIR := $(shell pwd)
14+
WOLFSSL_ROOT := $(abspath $(CURDIR)/../..)
15+
WOLFSSL_LIB := $(WOLFSSL_ROOT)/src/.libs/libwolfssl.a
16+
17+
CC ?= cc
18+
CFLAGS_TEST := -Wall -Wextra -O2 -g \
19+
-DWOLFSSL_EMNET \
20+
-I$(CURDIR) \
21+
-I$(WOLFSSL_ROOT)
22+
LDFLAGS_TEST := -Wl,--wrap=recv,--wrap=send
23+
LIBS_TEST := $(WOLFSSL_LIB) -lpthread -lm
24+
25+
TEST_BIN := emnet_nonblock_test
26+
27+
.PHONY: all run clean check-lib
28+
29+
all: $(TEST_BIN)
30+
31+
check-lib:
32+
@if [ ! -f "$(WOLFSSL_LIB)" ]; then \
33+
echo "error: $(WOLFSSL_LIB) not found."; \
34+
echo "Build wolfSSL first (see header of this Makefile)."; \
35+
exit 1; \
36+
fi
37+
38+
$(TEST_BIN): emnet_nonblock_test.o emnet_shim.o check-lib
39+
$(CC) $(LDFLAGS_TEST) -o $@ emnet_nonblock_test.o emnet_shim.o $(LIBS_TEST)
40+
41+
emnet_nonblock_test.o: emnet_nonblock_test.c
42+
$(CC) $(CFLAGS_TEST) -c $< -o $@
43+
44+
emnet_shim.o: emnet_shim.c IP/IP.h
45+
$(CC) $(CFLAGS_TEST) -c $< -o $@
46+
47+
# Run from the repo root so the relative cert paths resolve.
48+
run: $(TEST_BIN)
49+
cd $(WOLFSSL_ROOT) && ./tests/emnet/$(TEST_BIN)
50+
51+
clean:
52+
rm -f *.o $(TEST_BIN)

0 commit comments

Comments
 (0)