Skip to content

Commit

Permalink
net: socket: syscall for socketpair(2)
Browse files Browse the repository at this point in the history
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 <chrisfriedt@gmail.com>
  • Loading branch information
cfriedt authored and carlescufi committed May 10, 2020
1 parent effac5b commit 09f957c
Show file tree
Hide file tree
Showing 7 changed files with 1,174 additions and 0 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@
/subsys/net/lib/config/ @jukkar @tbursztyka @pfalcon
/subsys/net/lib/mqtt/ @jukkar @tbursztyka @rlubos
/subsys/net/lib/coap/ @rveerama1
/subsys/net/lib/sockets/socketpair.c @cfriedt
/subsys/net/lib/sockets/ @jukkar @tbursztyka @pfalcon
/subsys/net/lib/tls_credentials/ @rlubos
/subsys/net/l2/ @jukkar @tbursztyka
Expand Down
10 changes: 10 additions & 0 deletions include/net/net_ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ extern "C" {
#define PF_PACKET 3 /**< Packet family. */
#define PF_CAN 4 /**< Controller Area Network. */
#define PF_NET_MGMT 5 /**< Network management info. */
#define PF_LOCAL 6 /**< Inter-process communication */
#define PF_UNIX PF_LOCAL /**< Inter-process communication */

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

/** Protocol numbers from IANA/BSD */
enum net_ip_protocol {
Expand Down Expand Up @@ -341,6 +345,12 @@ struct sockaddr_storage {
char data[NET_SOCKADDR_MAX_SIZE - sizeof(sa_family_t)];
};

/* Socket address struct for UNIX domain sockets */
struct sockaddr_un {
sa_family_t sun_family; /* AF_UNIX */
char sun_path[NET_SOCKADDR_MAX_SIZE - sizeof(sa_family_t)];
};

struct net_addr {
sa_family_t family;
union {
Expand Down
19 changes: 19 additions & 0 deletions include/net/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ struct zsock_addrinfo {
*/
__syscall int zsock_socket(int family, int type, int proto);

/**
* @brief Create an unnamed pair of connected sockets
*
* @details
* @rst
* See `POSIX.1-2017 article
* <https://pubs.opengroup.org/onlinepubs/009695399/functions/socketpair.html>`__
* for normative description.
* This function is also exposed as ``socketpair()``
* if :option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
* @endrst
*/
__syscall int zsock_socketpair(int family, int type, int proto, int *sv);

/**
* @brief Close a network socket
*
Expand Down Expand Up @@ -566,6 +580,11 @@ static inline int socket(int family, int type, int proto)
return zsock_socket(family, type, proto);
}

static inline int socketpair(int family, int type, int proto, int sv[2])
{
return zsock_socketpair(family, type, proto, sv);
}

static inline int close(int sock)
{
return zsock_close(sock);
Expand Down
5 changes: 5 additions & 0 deletions include/posix/sys/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ static inline int socket(int family, int type, int proto)
return zsock_socket(family, type, proto);
}

static inline int socketpair(int family, int type, int proto, int sv[2])
{
return zsock_socketpair(family, type, proto, sv);
}

#define SHUT_RD ZSOCK_SHUT_RD
#define SHUT_WR ZSOCK_SHUT_WR
#define SHUT_RDWR ZSOCK_SHUT_RDWR
Expand Down
2 changes: 2 additions & 0 deletions subsys/net/lib/sockets/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ if(CONFIG_SOCKS)
zephyr_include_directories(${ZEPHYR_BASE}/subsys/net/lib/socks)
endif()

zephyr_sources_ifdef(CONFIG_NET_SOCKETPAIR socketpair.c)

zephyr_link_libraries_ifdef(CONFIG_MBEDTLS mbedTLS)
15 changes: 15 additions & 0 deletions subsys/net/lib/sockets/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ config NET_SOCKETS_CAN_RECEIVERS
The value tells how many sockets can receive data from same
Socket-CAN interface.

config NET_SOCKETPAIR
bool "Support for the socketpair syscall [EXPERIMENTAL]"
depends on HEAP_MEM_POOL_SIZE != 0
help
Choose y here if you would like to use the socketpair(2)
system call.

config NET_SOCKETPAIR_BUFFER_SIZE
int "Size of the intermediate buffer, in bytes"
default 64
range 1 4096
depends on NET_SOCKETPAIR
help
Buffer size for socketpair(2)

config NET_SOCKETS_NET_MGMT
bool "Enable network management socket support [EXPERIMENTAL]"
depends on NET_MGMT_EVENT
Expand Down
Loading

0 comments on commit 09f957c

Please sign in to comment.