Skip to content

Commit 09f957c

Browse files
cfriedtcarlescufi
authored andcommitted
net: socket: syscall for socketpair(2)
Working: * non-blocking reads / writes * blocking reads / writes * send(2) / recv(2) / sendto(2) / recvfrom(2) / sendmsg(2) * select(2) * poll(2) Fixes #24366 Signed-off-by: Christopher Friedt <[email protected]>
1 parent effac5b commit 09f957c

File tree

7 files changed

+1174
-0
lines changed

7 files changed

+1174
-0
lines changed

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@
457457
/subsys/net/lib/config/ @jukkar @tbursztyka @pfalcon
458458
/subsys/net/lib/mqtt/ @jukkar @tbursztyka @rlubos
459459
/subsys/net/lib/coap/ @rveerama1
460+
/subsys/net/lib/sockets/socketpair.c @cfriedt
460461
/subsys/net/lib/sockets/ @jukkar @tbursztyka @pfalcon
461462
/subsys/net/lib/tls_credentials/ @rlubos
462463
/subsys/net/l2/ @jukkar @tbursztyka

include/net/net_ip.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ extern "C" {
4545
#define PF_PACKET 3 /**< Packet family. */
4646
#define PF_CAN 4 /**< Controller Area Network. */
4747
#define PF_NET_MGMT 5 /**< Network management info. */
48+
#define PF_LOCAL 6 /**< Inter-process communication */
49+
#define PF_UNIX PF_LOCAL /**< Inter-process communication */
4850

4951
/* Address families. */
5052
#define AF_UNSPEC PF_UNSPEC /**< Unspecified address family. */
@@ -53,6 +55,8 @@ extern "C" {
5355
#define AF_PACKET PF_PACKET /**< Packet family. */
5456
#define AF_CAN PF_CAN /**< Controller Area Network. */
5557
#define AF_NET_MGMT PF_NET_MGMT /**< Network management info. */
58+
#define AF_LOCAL PF_LOCAL /**< Inter-process communication */
59+
#define AF_UNIX PF_UNIX /**< Inter-process communication */
5660

5761
/** Protocol numbers from IANA/BSD */
5862
enum net_ip_protocol {
@@ -341,6 +345,12 @@ struct sockaddr_storage {
341345
char data[NET_SOCKADDR_MAX_SIZE - sizeof(sa_family_t)];
342346
};
343347

348+
/* Socket address struct for UNIX domain sockets */
349+
struct sockaddr_un {
350+
sa_family_t sun_family; /* AF_UNIX */
351+
char sun_path[NET_SOCKADDR_MAX_SIZE - sizeof(sa_family_t)];
352+
};
353+
344354
struct net_addr {
345355
sa_family_t family;
346356
union {

include/net/socket.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,20 @@ struct zsock_addrinfo {
159159
*/
160160
__syscall int zsock_socket(int family, int type, int proto);
161161

162+
/**
163+
* @brief Create an unnamed pair of connected sockets
164+
*
165+
* @details
166+
* @rst
167+
* See `POSIX.1-2017 article
168+
* <https://pubs.opengroup.org/onlinepubs/009695399/functions/socketpair.html>`__
169+
* for normative description.
170+
* This function is also exposed as ``socketpair()``
171+
* if :option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
172+
* @endrst
173+
*/
174+
__syscall int zsock_socketpair(int family, int type, int proto, int *sv);
175+
162176
/**
163177
* @brief Close a network socket
164178
*
@@ -566,6 +580,11 @@ static inline int socket(int family, int type, int proto)
566580
return zsock_socket(family, type, proto);
567581
}
568582

583+
static inline int socketpair(int family, int type, int proto, int sv[2])
584+
{
585+
return zsock_socketpair(family, type, proto, sv);
586+
}
587+
569588
static inline int close(int sock)
570589
{
571590
return zsock_close(sock);

include/posix/sys/socket.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ static inline int socket(int family, int type, int proto)
1818
return zsock_socket(family, type, proto);
1919
}
2020

21+
static inline int socketpair(int family, int type, int proto, int sv[2])
22+
{
23+
return zsock_socketpair(family, type, proto, sv);
24+
}
25+
2126
#define SHUT_RD ZSOCK_SHUT_RD
2227
#define SHUT_WR ZSOCK_SHUT_WR
2328
#define SHUT_RDWR ZSOCK_SHUT_RDWR

subsys/net/lib/sockets/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ if(CONFIG_SOCKS)
2828
zephyr_include_directories(${ZEPHYR_BASE}/subsys/net/lib/socks)
2929
endif()
3030

31+
zephyr_sources_ifdef(CONFIG_NET_SOCKETPAIR socketpair.c)
32+
3133
zephyr_link_libraries_ifdef(CONFIG_MBEDTLS mbedTLS)

subsys/net/lib/sockets/Kconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,21 @@ config NET_SOCKETS_CAN_RECEIVERS
139139
The value tells how many sockets can receive data from same
140140
Socket-CAN interface.
141141

142+
config NET_SOCKETPAIR
143+
bool "Support for the socketpair syscall [EXPERIMENTAL]"
144+
depends on HEAP_MEM_POOL_SIZE != 0
145+
help
146+
Choose y here if you would like to use the socketpair(2)
147+
system call.
148+
149+
config NET_SOCKETPAIR_BUFFER_SIZE
150+
int "Size of the intermediate buffer, in bytes"
151+
default 64
152+
range 1 4096
153+
depends on NET_SOCKETPAIR
154+
help
155+
Buffer size for socketpair(2)
156+
142157
config NET_SOCKETS_NET_MGMT
143158
bool "Enable network management socket support [EXPERIMENTAL]"
144159
depends on NET_MGMT_EVENT

0 commit comments

Comments
 (0)