net: sockets: Remove socket offloading interface
Instead of using a custom offloading interface, users can use `NET_SOCKET_REGISTER` macro to register custom socket API provider. This solution removes a limitation, that only one offloaded interface can be registered and that it cannot be used together with native IP stack. The only exception remainig are DNS releated operations - `getaddrinfo`/`freeaddrinfo`, which, when offloaded, have to be registered specifically. Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
This commit is contained in:
parent
561973060e
commit
bea1093e62
8 changed files with 80 additions and 289 deletions
|
@ -507,10 +507,7 @@ int zsock_getaddrinfo(const char *host, const char *service,
|
|||
* if :option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
|
||||
* @endrst
|
||||
*/
|
||||
static inline void zsock_freeaddrinfo(struct zsock_addrinfo *ai)
|
||||
{
|
||||
free(ai);
|
||||
}
|
||||
void zsock_freeaddrinfo(struct zsock_addrinfo *ai);
|
||||
|
||||
/**
|
||||
* @brief Convert zsock_getaddrinfo() error code to textual message
|
||||
|
@ -564,7 +561,6 @@ int zsock_getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
|
|||
|
||||
#define pollfd zsock_pollfd
|
||||
|
||||
#if !defined(CONFIG_NET_SOCKETS_OFFLOAD)
|
||||
static inline int socket(int family, int type, int proto)
|
||||
{
|
||||
return zsock_socket(family, type, proto);
|
||||
|
@ -693,42 +689,12 @@ static inline int inet_pton(sa_family_t family, const char *src, void *dst)
|
|||
return zsock_inet_pton(family, src, dst);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
struct addrinfo {
|
||||
int ai_flags;
|
||||
int ai_family;
|
||||
int ai_socktype;
|
||||
int ai_protocol;
|
||||
socklen_t ai_addrlen;
|
||||
struct sockaddr *ai_addr;
|
||||
char *ai_canonname;
|
||||
struct addrinfo *ai_next;
|
||||
};
|
||||
|
||||
/* Legacy case: retain containing extern "C" with C++
|
||||
*
|
||||
* This header requires aliases defined within this file, and can't
|
||||
* easily be moved to the top.
|
||||
*/
|
||||
#include <net/socket_offload.h>
|
||||
|
||||
static inline int inet_pton(sa_family_t family, const char *src, void *dst)
|
||||
static inline char *inet_ntop(sa_family_t family, const void *src, char *dst,
|
||||
size_t size)
|
||||
{
|
||||
if ((family != AF_INET) && (family != AF_INET6)) {
|
||||
errno = EAFNOSUPPORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (net_addr_pton(family, src, dst) == 0) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
return zsock_inet_ntop(family, src, dst, size);
|
||||
}
|
||||
|
||||
#endif /* !defined(CONFIG_NET_SOCKETS_OFFLOAD) */
|
||||
|
||||
#define POLLIN ZSOCK_POLLIN
|
||||
#define POLLOUT ZSOCK_POLLOUT
|
||||
#define POLLERR ZSOCK_POLLERR
|
||||
|
@ -742,12 +708,6 @@ static inline int inet_pton(sa_family_t family, const char *src, void *dst)
|
|||
#define SHUT_WR ZSOCK_SHUT_WR
|
||||
#define SHUT_RDWR ZSOCK_SHUT_RDWR
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
#define EAI_BADFLAGS DNS_EAI_BADFLAGS
|
||||
#define EAI_NONAME DNS_EAI_NONAME
|
||||
#define EAI_AGAIN DNS_EAI_AGAIN
|
||||
|
|
|
@ -12,153 +12,38 @@
|
|||
#ifndef ZEPHYR_INCLUDE_NET_SOCKET_OFFLOAD_H_
|
||||
#define ZEPHYR_INCLUDE_NET_SOCKET_OFFLOAD_H_
|
||||
|
||||
#include <net/socket_offload_ops.h>
|
||||
#include <net/net_ip.h>
|
||||
#include <net/socket.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern const struct socket_offload *socket_ops;
|
||||
/**
|
||||
* @brief An offloaded Socket DNS API interface
|
||||
*
|
||||
* It is assumed that these offload functions follow the
|
||||
* POSIX socket API standard for arguments, return values and setting of errno.
|
||||
*/
|
||||
struct socket_dns_offload {
|
||||
int (*getaddrinfo)(const char *node, const char *service,
|
||||
const struct zsock_addrinfo *hints,
|
||||
struct zsock_addrinfo **res);
|
||||
void (*freeaddrinfo)(struct zsock_addrinfo *res);
|
||||
};
|
||||
|
||||
static inline int socket(int family, int type, int proto)
|
||||
{
|
||||
__ASSERT_NO_MSG(socket_ops);
|
||||
__ASSERT_NO_MSG(socket_ops->socket);
|
||||
/**
|
||||
* @brief Register an offloaded socket DNS API interface.
|
||||
*
|
||||
* @param ops A pointer to the offloaded socket DNS API interface.
|
||||
*/
|
||||
void socket_offload_dns_register(const struct socket_dns_offload *ops);
|
||||
|
||||
return socket_ops->socket(family, type, proto);
|
||||
}
|
||||
int socket_offload_getaddrinfo(const char *node, const char *service,
|
||||
const struct zsock_addrinfo *hints,
|
||||
struct zsock_addrinfo **res);
|
||||
|
||||
static inline int close(int sock)
|
||||
{
|
||||
__ASSERT_NO_MSG(socket_ops);
|
||||
__ASSERT_NO_MSG(socket_ops->close);
|
||||
|
||||
return socket_ops->close(sock);
|
||||
}
|
||||
|
||||
static inline int accept(int sock, struct sockaddr *addr,
|
||||
socklen_t *addrlen)
|
||||
{
|
||||
__ASSERT_NO_MSG(socket_ops);
|
||||
__ASSERT_NO_MSG(socket_ops->accept);
|
||||
|
||||
return socket_ops->accept(sock, addr, addrlen);
|
||||
}
|
||||
|
||||
|
||||
static inline int bind(int sock, const struct sockaddr *addr,
|
||||
socklen_t addrlen)
|
||||
{
|
||||
__ASSERT_NO_MSG(socket_ops);
|
||||
__ASSERT_NO_MSG(socket_ops->bind);
|
||||
|
||||
return socket_ops->bind(sock, addr, addrlen);
|
||||
}
|
||||
|
||||
static inline int listen(int sock, int backlog)
|
||||
{
|
||||
__ASSERT_NO_MSG(socket_ops);
|
||||
__ASSERT_NO_MSG(socket_ops->listen);
|
||||
|
||||
return socket_ops->listen(sock, backlog);
|
||||
}
|
||||
|
||||
static inline int connect(int sock, const struct sockaddr *addr,
|
||||
socklen_t addrlen)
|
||||
{
|
||||
__ASSERT_NO_MSG(socket_ops);
|
||||
__ASSERT_NO_MSG(socket_ops->connect);
|
||||
|
||||
return socket_ops->connect(sock, addr, addrlen);
|
||||
}
|
||||
|
||||
static inline int poll(struct pollfd *fds, int nfds, int timeout)
|
||||
{
|
||||
__ASSERT_NO_MSG(socket_ops);
|
||||
__ASSERT_NO_MSG(socket_ops->poll);
|
||||
|
||||
return socket_ops->poll(fds, nfds, timeout);
|
||||
}
|
||||
|
||||
static inline int setsockopt(int sock, int level, int optname,
|
||||
const void *optval,
|
||||
socklen_t optlen)
|
||||
{
|
||||
__ASSERT_NO_MSG(socket_ops);
|
||||
__ASSERT_NO_MSG(socket_ops->setsockopt);
|
||||
|
||||
return socket_ops->setsockopt(sock, level, optname, optval, optlen);
|
||||
}
|
||||
|
||||
static inline int getsockopt(int sock, int level, int optname,
|
||||
void *optval, socklen_t *optlen)
|
||||
{
|
||||
__ASSERT_NO_MSG(socket_ops);
|
||||
__ASSERT_NO_MSG(socket_ops->getsockopt);
|
||||
|
||||
return socket_ops->getsockopt(sock, level, optname, optval, optlen);
|
||||
}
|
||||
|
||||
static inline ssize_t recv(int sock, void *buf, size_t max_len,
|
||||
int flags)
|
||||
{
|
||||
__ASSERT_NO_MSG(socket_ops);
|
||||
__ASSERT_NO_MSG(socket_ops->recv);
|
||||
|
||||
return socket_ops->recv(sock, buf, max_len, flags);
|
||||
}
|
||||
|
||||
static inline ssize_t recvfrom(int sock, void *buf,
|
||||
short int len,
|
||||
short int flags,
|
||||
struct sockaddr *from,
|
||||
socklen_t *fromlen)
|
||||
{
|
||||
__ASSERT_NO_MSG(socket_ops);
|
||||
__ASSERT_NO_MSG(socket_ops->recvfrom);
|
||||
|
||||
return socket_ops->recvfrom(sock, buf, len, flags, from, fromlen);
|
||||
}
|
||||
|
||||
static inline ssize_t send(int sock, const void *buf, size_t len,
|
||||
int flags)
|
||||
{
|
||||
__ASSERT_NO_MSG(socket_ops);
|
||||
__ASSERT_NO_MSG(socket_ops->send);
|
||||
|
||||
return socket_ops->send(sock, buf, len, flags);
|
||||
}
|
||||
|
||||
static inline ssize_t sendto(int sock, const void *buf,
|
||||
size_t len, int flags,
|
||||
const struct sockaddr *to,
|
||||
socklen_t tolen)
|
||||
{
|
||||
__ASSERT_NO_MSG(socket_ops);
|
||||
__ASSERT_NO_MSG(socket_ops->sendto);
|
||||
|
||||
return socket_ops->sendto(sock, buf, len, flags, to, tolen);
|
||||
}
|
||||
|
||||
static inline int getaddrinfo(const char *node, const char *service,
|
||||
const struct addrinfo *hints,
|
||||
struct addrinfo **res)
|
||||
{
|
||||
__ASSERT_NO_MSG(socket_ops);
|
||||
__ASSERT_NO_MSG(socket_ops->getaddrinfo);
|
||||
|
||||
return socket_ops->getaddrinfo(node, service, hints, res);
|
||||
}
|
||||
|
||||
static inline void freeaddrinfo(struct addrinfo *res)
|
||||
{
|
||||
__ASSERT_NO_MSG(socket_ops);
|
||||
__ASSERT_NO_MSG(socket_ops->freeaddrinfo);
|
||||
|
||||
return socket_ops->freeaddrinfo(res);
|
||||
}
|
||||
|
||||
int fcntl(int fd, int cmd, ...);
|
||||
void socket_offload_freeaddrinfo(struct zsock_addrinfo *res);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -1,79 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Linaro Limited.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Socket Offload Redirect API
|
||||
*/
|
||||
|
||||
#ifndef ZEPHYR_INCLUDE_NET_SOCKET_OFFLOAD_OPS_H_
|
||||
#define ZEPHYR_INCLUDE_NET_SOCKET_OFFLOAD_OPS_H_
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <net/net_ip.h>
|
||||
#include <net/socket.h> /* needed for struct pollfd */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Socket Offload Redirect API
|
||||
* @defgroup socket_offload Socket offloading interface
|
||||
* @ingroup networking
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief An offloaded Socket API interface
|
||||
*
|
||||
* It is assumed that these offload functions follow the
|
||||
* POSIX socket API standard for arguments, return values and setting of errno.
|
||||
*/
|
||||
struct socket_offload {
|
||||
/* POSIX Socket Functions: */
|
||||
int (*socket)(int family, int type, int proto);
|
||||
int (*close)(int sock);
|
||||
int (*accept)(int sock, struct sockaddr *addr, socklen_t *addrlen);
|
||||
int (*bind)(int sock, const struct sockaddr *addr, socklen_t addrlen);
|
||||
int (*listen)(int sock, int backlog);
|
||||
int (*connect)(int sock, const struct sockaddr *addr,
|
||||
socklen_t addrlen);
|
||||
int (*poll)(struct pollfd *fds, int nfds, int timeout);
|
||||
int (*setsockopt)(int sock, int level, int optname,
|
||||
const void *optval, socklen_t optlen);
|
||||
int (*getsockopt)(int sock, int level, int optname, void *optval,
|
||||
socklen_t *optlen);
|
||||
ssize_t (*recv)(int sock, void *buf, size_t max_len, int flags);
|
||||
ssize_t (*recvfrom)(int sock, void *buf, short int len,
|
||||
short int flags, struct sockaddr *from,
|
||||
socklen_t *fromlen);
|
||||
ssize_t (*send)(int sock, const void *buf, size_t len, int flags);
|
||||
ssize_t (*sendto)(int sock, const void *buf, size_t len, int flags,
|
||||
const struct sockaddr *to, socklen_t tolen);
|
||||
int (*getaddrinfo)(const char *node, const char *service,
|
||||
const struct addrinfo *hints,
|
||||
struct addrinfo **res);
|
||||
void (*freeaddrinfo)(struct addrinfo *res);
|
||||
int (*fcntl)(int fd, int cmd, va_list args);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Register an offloaded socket API interface.
|
||||
*
|
||||
* @param ops A pointer to the offloaded socket API interface.
|
||||
*/
|
||||
extern void socket_offload_register(const struct socket_offload *ops);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* ZEPHYR_INCLUDE_NET_SOCKET_OFFLOAD_OPS_H_ */
|
Loading…
Add table
Add a link
Reference in a new issue