posix: convert static inline functions to library functions

With CONFIG_NET_POSIX_NAMES being deprecated, convert static
inlines in headers to prototypes, and move implementations
to lib/posix/options/net.c .

Since select and poll should technically also operate on
non-socket file descriptors, these may be relocated in the
future.

Below are some reasons for this change.
1. So posix calls are regular symbols and not directly inlined
   as syscalls. This is also the most portable linkage type.
2. Many posix calls are cancellation points and additional
   checks are necessary within the implementation.
3. The more we add to an inline call, the less sense it makes
   for that call to be inline.
4. When the implementation of a posix function changes, the
   interface for it should still remain consistent.

Note: the same principles have been applied in Zephyr's
posix implementation already, e.g. clock_gettime(), so this
is nothing new.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
This commit is contained in:
Chris Friedt 2024-03-30 10:58:26 -04:00 committed by Anas Nashif
parent f010f40c8c
commit 5ab984a834
6 changed files with 170 additions and 161 deletions

View file

@ -17,25 +17,12 @@
extern "C" { extern "C" {
#endif #endif
#ifndef CONFIG_NET_SOCKETS_POSIX_NAMES
static inline char *inet_ntop(sa_family_t family, const void *src, char *dst,
size_t size)
{
return zsock_inet_ntop(family, src, dst, size);
}
static inline int inet_pton(sa_family_t family, const char *src, void *dst)
{
return zsock_inet_pton(family, src, dst);
}
#endif /* CONFIG_NET_SOCKETS_POSIX_NAMES */
typedef uint32_t in_addr_t; typedef uint32_t in_addr_t;
in_addr_t inet_addr(const char *cp); in_addr_t inet_addr(const char *cp);
char *inet_ntoa(struct in_addr in); char *inet_ntoa(struct in_addr in);
char *inet_ntop(sa_family_t family, const void *src, char *dst, size_t size);
int inet_pton(sa_family_t family, const char *src, void *dst);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -57,42 +57,19 @@ struct servent {
char *s_proto; char *s_proto;
}; };
#ifndef CONFIG_NET_SOCKETS_POSIX_NAMES
#define addrinfo zsock_addrinfo #define addrinfo zsock_addrinfo
static inline int getaddrinfo(const char *host, const char *service,
const struct zsock_addrinfo *hints,
struct zsock_addrinfo **res)
{
return zsock_getaddrinfo(host, service, hints, res);
}
static inline void freeaddrinfo(struct zsock_addrinfo *ai)
{
zsock_freeaddrinfo(ai);
}
static inline const char *gai_strerror(int errcode)
{
return zsock_gai_strerror(errcode);
}
static inline int getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
char *host, socklen_t hostlen,
char *serv, socklen_t servlen, int flags)
{
return zsock_getnameinfo(addr, addrlen, host, hostlen,
serv, servlen, flags);
}
#endif /* CONFIG_NET_SOCKETS_POSIX_NAMES */
void endhostent(void); void endhostent(void);
void endnetent(void); void endnetent(void);
void endprotoent(void); void endprotoent(void);
void endservent(void); void endservent(void);
void freeaddrinfo(struct zsock_addrinfo *ai);
const char *gai_strerror(int errcode);
int getaddrinfo(const char *host, const char *service, const struct zsock_addrinfo *hints,
struct zsock_addrinfo **res);
struct hostent *gethostent(void); struct hostent *gethostent(void);
int getnameinfo(const struct sockaddr *addr, socklen_t addrlen, char *host, socklen_t hostlen,
char *serv, socklen_t servlen, int flags);
struct netent *getnetbyaddr(uint32_t net, int type); struct netent *getnetbyaddr(uint32_t net, int type);
struct netent *getnetbyname(const char *name); struct netent *getnetbyname(const char *name);
struct netent *getnetent(void); struct netent *getnetent(void);

View file

@ -12,8 +12,6 @@
extern "C" { extern "C" {
#endif #endif
#ifndef CONFIG_NET_SOCKETS_POSIX_NAMES
#define pollfd zsock_pollfd #define pollfd zsock_pollfd
#define POLLIN ZSOCK_POLLIN #define POLLIN ZSOCK_POLLIN
@ -22,12 +20,7 @@ extern "C" {
#define POLLHUP ZSOCK_POLLHUP #define POLLHUP ZSOCK_POLLHUP
#define POLLNVAL ZSOCK_POLLNVAL #define POLLNVAL ZSOCK_POLLNVAL
static inline int poll(struct pollfd *fds, int nfds, int timeout) int poll(struct pollfd *fds, int nfds, int timeout);
{
return zsock_poll(fds, nfds, timeout);
}
#endif /* CONFIG_NET_SOCKETS_POSIX_NAMES */
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -13,8 +13,6 @@
extern "C" { extern "C" {
#endif #endif
#ifndef CONFIG_NET_SOCKETS_POSIX_NAMES
#define fd_set zsock_fd_set #define fd_set zsock_fd_set
#define FD_SETSIZE ZSOCK_FD_SETSIZE #define FD_SETSIZE ZSOCK_FD_SETSIZE
#define FD_ZERO ZSOCK_FD_ZERO #define FD_ZERO ZSOCK_FD_ZERO
@ -24,15 +22,7 @@ extern "C" {
struct timeval; struct timeval;
static inline int select(int nfds, fd_set *readfds, int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
fd_set *writefds, fd_set *exceptfds,
struct timeval *timeout)
{
return zsock_select(nfds, readfds, writefds, exceptfds,
(struct zsock_timeval *)timeout);
}
#endif /* CONFIG_NET_SOCKETS_POSIX_NAMES */
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -9,8 +9,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <zephyr/net/socket.h> #include <zephyr/net/socket.h>
#ifndef CONFIG_NET_SOCKETS_POSIX_NAMES
#define SHUT_RD ZSOCK_SHUT_RD #define SHUT_RD ZSOCK_SHUT_RD
#define SHUT_WR ZSOCK_SHUT_WR #define SHUT_WR ZSOCK_SHUT_WR
#define SHUT_RDWR ZSOCK_SHUT_RDWR #define SHUT_RDWR ZSOCK_SHUT_RDWR
@ -29,103 +27,26 @@ struct linger {
int l_linger; int l_linger;
}; };
static inline int socket(int family, int type, int proto) int accept(int sock, struct sockaddr *addr, socklen_t *addrlen);
{ int bind(int sock, const struct sockaddr *addr, socklen_t addrlen);
return zsock_socket(family, type, proto); int connect(int sock, const struct sockaddr *addr, socklen_t addrlen);
} int getpeername(int sock, struct sockaddr *addr, socklen_t *addrlen);
int getsockname(int sock, struct sockaddr *addr, socklen_t *addrlen);
static inline int socketpair(int family, int type, int proto, int sv[2]) int getsockopt(int sock, int level, int optname, void *optval, socklen_t *optlen);
{ int listen(int sock, int backlog);
return zsock_socketpair(family, type, proto, sv); ssize_t recv(int sock, void *buf, size_t max_len, int flags);
} ssize_t recvfrom(int sock, void *buf, size_t max_len, int flags, struct sockaddr *src_addr,
socklen_t *addrlen);
static inline int shutdown(int sock, int how) ssize_t recvmsg(int sock, struct msghdr *msg, int flags);
{ ssize_t send(int sock, const void *buf, size_t len, int flags);
return zsock_shutdown(sock, how); ssize_t sendmsg(int sock, const struct msghdr *message, int flags);
} ssize_t sendto(int sock, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr,
socklen_t addrlen);
static inline int bind(int sock, const struct sockaddr *addr, socklen_t addrlen) int setsockopt(int sock, int level, int optname, const void *optval, socklen_t optlen);
{ int shutdown(int sock, int how);
return zsock_bind(sock, addr, addrlen);
}
static inline int connect(int sock, const struct sockaddr *addr,
socklen_t addrlen)
{
return zsock_connect(sock, addr, addrlen);
}
static inline int listen(int sock, int backlog)
{
return zsock_listen(sock, backlog);
}
static inline int accept(int sock, struct sockaddr *addr, socklen_t *addrlen)
{
return zsock_accept(sock, addr, addrlen);
}
static inline ssize_t send(int sock, const void *buf, size_t len, int flags)
{
return zsock_send(sock, buf, len, flags);
}
static inline ssize_t recv(int sock, void *buf, size_t max_len, int flags)
{
return zsock_recv(sock, buf, max_len, flags);
}
static inline ssize_t sendto(int sock, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr,
socklen_t addrlen)
{
return zsock_sendto(sock, buf, len, flags, dest_addr, addrlen);
}
static inline ssize_t sendmsg(int sock, const struct msghdr *message,
int flags)
{
return zsock_sendmsg(sock, message, flags);
}
static inline ssize_t recvmsg(int sock, struct msghdr *msg, int flags)
{
return zsock_recvmsg(sock, msg, flags);
}
static inline ssize_t recvfrom(int sock, void *buf, size_t max_len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen)
{
return zsock_recvfrom(sock, buf, max_len, flags, src_addr, addrlen);
}
static inline int getsockopt(int sock, int level, int optname,
void *optval, socklen_t *optlen)
{
return zsock_getsockopt(sock, level, optname, optval, optlen);
}
static inline int setsockopt(int sock, int level, int optname,
const void *optval, socklen_t optlen)
{
return zsock_setsockopt(sock, level, optname, optval, optlen);
}
static inline int getpeername(int sock, struct sockaddr *addr,
socklen_t *addrlen)
{
return zsock_getpeername(sock, addr, addrlen);
}
static inline int getsockname(int sock, struct sockaddr *addr,
socklen_t *addrlen)
{
return zsock_getsockname(sock, addr, addrlen);
}
#endif /* CONFIG_NET_SOCKETS_POSIX_NAMES */
int sockatmark(int s); int sockatmark(int s);
int socket(int family, int type, int proto);
int socketpair(int family, int type, int proto, int sv[2]);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -1,4 +1,5 @@
/* /*
* Copyright (c) 2019 Linaro Limited
* Copyright (c) 2024, Friedt Professional Engineering Services, Inc * Copyright (c) 2024, Friedt Professional Engineering Services, Inc
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
@ -12,6 +13,11 @@
#include <zephyr/posix/arpa/inet.h> #include <zephyr/posix/arpa/inet.h>
#include <zephyr/posix/netinet/in.h> #include <zephyr/posix/netinet/in.h>
#include <zephyr/posix/net/if.h> #include <zephyr/posix/net/if.h>
#include <zephyr/posix/poll.h>
#include <zephyr/posix/sys/select.h>
#include <zephyr/posix/sys/socket.h>
/* From arpa/inet.h */
in_addr_t inet_addr(const char *cp) in_addr_t inet_addr(const char *cp)
{ {
@ -80,6 +86,18 @@ char *inet_ntoa(struct in_addr in)
return buf; return buf;
} }
char *inet_ntop(sa_family_t family, const void *src, char *dst, size_t size)
{
return zsock_inet_ntop(family, src, dst, size);
}
int inet_pton(sa_family_t family, const char *src, void *dst)
{
return zsock_inet_pton(family, src, dst);
}
/* From net/if.h */
char *if_indextoname(unsigned int ifindex, char *ifname) char *if_indextoname(unsigned int ifindex, char *ifname)
{ {
int ret; int ret;
@ -175,6 +193,8 @@ unsigned int if_nametoindex(const char *ifname)
return ret; return ret;
} }
/* From netdb.h */
void endhostent(void) void endhostent(void)
{ {
} }
@ -191,11 +211,33 @@ void endservent(void)
{ {
} }
void freeaddrinfo(struct zsock_addrinfo *ai)
{
zsock_freeaddrinfo(ai);
}
const char *gai_strerror(int errcode)
{
return zsock_gai_strerror(errcode);
}
int getaddrinfo(const char *host, const char *service, const struct zsock_addrinfo *hints,
struct zsock_addrinfo **res)
{
return zsock_getaddrinfo(host, service, hints, res);
}
struct hostent *gethostent(void) struct hostent *gethostent(void)
{ {
return NULL; return NULL;
} }
int getnameinfo(const struct sockaddr *addr, socklen_t addrlen, char *host, socklen_t hostlen,
char *serv, socklen_t servlen, int flags)
{
return zsock_getnameinfo(addr, addrlen, host, hostlen, serv, servlen, flags);
}
struct netent *getnetbyaddr(uint32_t net, int type) struct netent *getnetbyaddr(uint32_t net, int type)
{ {
ARG_UNUSED(net); ARG_UNUSED(net);
@ -216,6 +258,11 @@ struct netent *getnetent(void)
return NULL; return NULL;
} }
int getpeername(int sock, struct sockaddr *addr, socklen_t *addrlen)
{
return zsock_getpeername(sock, addr, addrlen);
}
struct protoent *getprotobyname(const char *name) struct protoent *getprotobyname(const char *name)
{ {
ARG_UNUSED(name); ARG_UNUSED(name);
@ -276,6 +323,90 @@ void setservent(int stayopen)
ARG_UNUSED(stayopen); ARG_UNUSED(stayopen);
} }
/* From sys/socket.h */
int accept(int sock, struct sockaddr *addr, socklen_t *addrlen)
{
return zsock_accept(sock, addr, addrlen);
}
int bind(int sock, const struct sockaddr *addr, socklen_t addrlen)
{
return zsock_bind(sock, addr, addrlen);
}
int connect(int sock, const struct sockaddr *addr, socklen_t addrlen)
{
return zsock_connect(sock, addr, addrlen);
}
int getsockname(int sock, struct sockaddr *addr, socklen_t *addrlen)
{
return zsock_getsockname(sock, addr, addrlen);
}
int getsockopt(int sock, int level, int optname, void *optval, socklen_t *optlen)
{
return zsock_getsockopt(sock, level, optname, optval, optlen);
}
int listen(int sock, int backlog)
{
return zsock_listen(sock, backlog);
}
int poll(struct pollfd *fds, int nfds, int timeout)
{
return zsock_poll(fds, nfds, timeout);
}
ssize_t recv(int sock, void *buf, size_t max_len, int flags)
{
return zsock_recv(sock, buf, max_len, flags);
}
ssize_t recvfrom(int sock, void *buf, size_t max_len, int flags, struct sockaddr *src_addr,
socklen_t *addrlen)
{
return zsock_recvfrom(sock, buf, max_len, flags, src_addr, addrlen);
}
ssize_t recvmsg(int sock, struct msghdr *msg, int flags)
{
return zsock_recvmsg(sock, msg, flags);
}
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
return zsock_select(nfds, readfds, writefds, exceptfds, (struct zsock_timeval *)timeout);
}
ssize_t send(int sock, const void *buf, size_t len, int flags)
{
return zsock_send(sock, buf, len, flags);
}
ssize_t sendmsg(int sock, const struct msghdr *message, int flags)
{
return zsock_sendmsg(sock, message, flags);
}
ssize_t sendto(int sock, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr,
socklen_t addrlen)
{
return zsock_sendto(sock, buf, len, flags, dest_addr, addrlen);
}
int setsockopt(int sock, int level, int optname, const void *optval, socklen_t optlen)
{
return zsock_setsockopt(sock, level, optname, optval, optlen);
}
int shutdown(int sock, int how)
{
return zsock_shutdown(sock, how);
}
int sockatmark(int s) int sockatmark(int s)
{ {
ARG_UNUSED(s); ARG_UNUSED(s);
@ -283,3 +414,13 @@ int sockatmark(int s)
errno = ENOSYS; errno = ENOSYS;
return -1; return -1;
} }
int socket(int family, int type, int proto)
{
return zsock_socket(family, type, proto);
}
int socketpair(int family, int type, int proto, int sv[2])
{
return zsock_socketpair(family, type, proto, sv);
}