Skip to content

Commit b090a3c

Browse files
committed
feat(sockutls): Declare socketpair and gai_strerror via standard headers
Adding a reverse dependency to lwip and define macros, which enable declarations of socketpair() and gai_strerror() in standard heders (sys/socket.h and netdb.h)
1 parent e12ecb8 commit b090a3c

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

components/sock_utils/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@ idf_component_register(SRCS "src/getnameinfo.c"
55
"src/gethostname.c"
66
INCLUDE_DIRS "include"
77
PRIV_REQUIRES lwip esp_netif)
8+
9+
# To support declarations from standard headers in lwip component
10+
# - socket pair from lwip/sockets.h
11+
# - gai_strerror from lwip/netdb.h
12+
# also need to make lwip depend on the sock_utils lib
13+
idf_component_get_property(lwip lwip COMPONENT_LIB)
14+
target_compile_definitions(${lwip} PUBLIC LWIP_SOCKET_HAS_SOCKETPAIR=1)
15+
target_compile_definitions(${lwip} PUBLIC LWIP_NETDB_HAS_GAI_STRERROR=1)
16+
target_link_libraries(${lwip} PUBLIC ${COMPONENT_LIB})

components/sock_utils/README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ This component provides simplified implementations of common socket-related util
66
## Supported Functions
77

88

9-
| API | Description | Limitations |
10-
|------------------|-------------------------------------------------------------|-------------------------------------------------------------------|
11-
| `ifaddrs()` | Retrieves interface addresses using `esp_netif` | IPv4 addresses only |
12-
| `socketpair()` | Creates a pair of connected sockets using `lwIP` loopback stream sockets | IPv4 sockets only |
13-
| `pipe()` | Wraps `socketpair()` to provide unidirectional pipe-like functionality | Uses bidirectional sockets in place of true pipes |
14-
| `getnameinfo()` | Converts IP addresses to human-readable form using `lwIP`'s `inet_ntop()` | IPv4 only; supports `NI_NUMERICHOST` and `NI_NUMERICSERV` flags only |
15-
| `gai_strerror()` | Returns error code as a string | Simple numeric string representation only |
16-
| `gethostname()` | Returns lwip netif hostname | Not a system-wide hostname, but interface specific hostname |
9+
| API | Description | Limitations | Declared in |
10+
|--------------------|-------------------------------------------------------------|-------------------------------------------------------------------|----------------------------------------|
11+
| `ifaddrs()` | Retrieves interface addresses using `esp_netif` | IPv4 addresses only | `ifaddrs.h` |
12+
| `socketpair()` *) | Creates a pair of connected sockets using `lwIP` loopback stream sockets | IPv4 sockets only | `socketpair.h`, `sys/socket.h` **) |
13+
| `pipe()` *) | Wraps `socketpair()` to provide unidirectional pipe-like functionality | Uses bidirectional sockets in place of true pipes | `socketpair.h`, `unistd.h` ***) |
14+
| `getnameinfo()` | Converts IP addresses to human-readable form using `lwIP`'s `inet_ntop()` | IPv4 only; supports `NI_NUMERICHOST` and `NI_NUMERICSERV` flags only | `getnameinfo.h`, `netdb.h` in ESP-IDF |
15+
| `gai_strerror()` | Returns error code as a string | Simple numeric string representation only | `gai_strerror.h`, `netdb.h` **) |
16+
| `gethostname()` | Returns lwip netif hostname | Not a system-wide hostname, but interface specific hostname | `gethostname.h`, `unistd.h` in ESP-IDF |
1717

18-
**Note**: `socketpair()` and `pipe()` are built on top of `lwIP` TCP sockets, inheriting the same characteristics. For instance, the maximum transmit buffer size is based on the `TCP_SND_BUF` setting.
18+
**Notes**:
19+
20+
- **`*)`** `socketpair()` and `pipe()` are built on top of `lwIP` TCP sockets, inheriting the same characteristics. For instance, the maximum transmit buffer size is based on the `TCP_SND_BUF` setting.
21+
- **`**)`** `socketpair()` and `gai_strerror()` are declared in sock_utils header files, the declaration is propagated to ESP-IDF from v5.5 to the official header files. If you're using older IDF version, you need to manually pre-include related header files from the sock_utils public include directory.
22+
- **`***)`** `pipe()` is declared in compiler's `sys/unistd.h`.

components/sock_utils/include/netdb_macros.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@
3232
#ifndef AF_UNIX
3333
#define AF_UNIX 1
3434
#endif
35+
36+
#ifndef PF_LOCAL
37+
/*
38+
* In POSIX, AF_UNIX and PF_LOCAL are essentially synonymous.
39+
*/
40+
#define PF_LOCAL AF_UNIX
41+
#endif

0 commit comments

Comments
 (0)