Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sockets: tls: Make secure sockets support posix APIs #11438

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions include/misc/fdtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ void z_free_fd(int fd);
*/
void *z_get_fd_obj(int fd, const struct fd_op_vtable *vtable, int err);

/**
* @brief Get underlying object pointer and vtable pointer from file descriptor.
*
* @param fd File descriptor previously returned by z_reserve_fd()
* @param vtable A pointer to a pointer variable to store the vtable
*
* @return Object pointer or NULL, with errno set
*/
void *z_get_fd_obj_and_vtable(int fd, const struct fd_op_vtable **vtable);

/**
* Request codes for fd_op_vtable.ioctl().
*
Expand All @@ -101,6 +111,9 @@ enum {
ZFD_IOCTL_CLOSE = 1,
ZFD_IOCTL_FSYNC,
ZFD_IOCTL_LSEEK,
ZFD_IOCTL_FCNTL,
ZFD_IOCTL_POLL_PREPARE,
ZFD_IOCTL_POLL_UPDATE,
};

#ifdef __cplusplus
Expand Down
79 changes: 0 additions & 79 deletions include/net/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,160 +169,81 @@ int zsock_getaddrinfo(const char *host, const char *service,
const struct zsock_addrinfo *hints,
struct zsock_addrinfo **res);

#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)

int ztls_socket(int family, int type, int proto);
int ztls_close(int sock);
int ztls_bind(int sock, const struct sockaddr *addr, socklen_t addrlen);
int ztls_connect(int sock, const struct sockaddr *addr, socklen_t addrlen);
int ztls_listen(int sock, int backlog);
int ztls_accept(int sock, struct sockaddr *addr, socklen_t *addrlen);
ssize_t ztls_send(int sock, const void *buf, size_t len, int flags);
ssize_t ztls_recv(int sock, void *buf, size_t max_len, int flags);
ssize_t ztls_sendto(int sock, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen);
ssize_t ztls_recvfrom(int sock, void *buf, size_t max_len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);
int ztls_fcntl(int sock, int cmd, int flags);
int ztls_poll(struct zsock_pollfd *fds, int nfds, int timeout);
int ztls_getsockopt(int sock, int level, int optname,
void *optval, socklen_t *optlen);
int ztls_setsockopt(int sock, int level, int optname,
const void *optval, socklen_t optlen);

#endif /* defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) */

#if defined(CONFIG_NET_SOCKETS_POSIX_NAMES)
#define pollfd zsock_pollfd
#if !defined(CONFIG_NET_SOCKETS_OFFLOAD)
static inline int socket(int family, int type, int proto)
{
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
return ztls_socket(family, type, proto);
#else
return zsock_socket(family, type, proto);
#endif /* defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) */
}

static inline int close(int sock)
{
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
return ztls_close(sock);
#else
return zsock_close(sock);
#endif /* defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) */
}

static inline int bind(int sock, const struct sockaddr *addr, socklen_t addrlen)
{
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
return ztls_bind(sock, addr, addrlen);
#else
return zsock_bind(sock, addr, addrlen);
#endif /* defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) */
}

static inline int connect(int sock, const struct sockaddr *addr,
socklen_t addrlen)
{
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
return ztls_connect(sock, addr, addrlen);
#else
return zsock_connect(sock, addr, addrlen);
#endif /* defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) */
}

static inline int listen(int sock, int backlog)
{
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
return ztls_listen(sock, backlog);
#else
return zsock_listen(sock, backlog);
#endif /* defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) */
}

static inline int accept(int sock, struct sockaddr *addr, socklen_t *addrlen)
{
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
return ztls_accept(sock, addr, addrlen);
#else
return zsock_accept(sock, addr, addrlen);
#endif /* defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) */
}

static inline ssize_t send(int sock, const void *buf, size_t len, int flags)
{
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
return ztls_send(sock, buf, len, flags);
#else
return zsock_send(sock, buf, len, flags);
#endif /* defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) */
}

static inline ssize_t recv(int sock, void *buf, size_t max_len, int flags)
{
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
return ztls_recv(sock, buf, max_len, flags);
#else
return zsock_recv(sock, buf, max_len, flags);
#endif /* defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) */
}

/* This conflicts with fcntl.h, so code must include fcntl.h before socket.h: */
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
#define fcntl ztls_fcntl
#else
#define fcntl zsock_fcntl
#endif /* defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) */

static inline ssize_t sendto(int sock, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr,
socklen_t addrlen)
{
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
return ztls_sendto(sock, buf, len, flags, dest_addr, addrlen);
#else
return zsock_sendto(sock, buf, len, flags, dest_addr, addrlen);
#endif /* defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) */
}

static inline ssize_t recvfrom(int sock, void *buf, size_t max_len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen)
{
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
return ztls_recvfrom(sock, buf, max_len, flags, src_addr, addrlen);
#else
return zsock_recvfrom(sock, buf, max_len, flags, src_addr, addrlen);
#endif /* defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) */
}

static inline int poll(struct zsock_pollfd *fds, int nfds, int timeout)
{
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
return ztls_poll(fds, nfds, timeout);
#else
return zsock_poll(fds, nfds, timeout);
#endif /* defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) */
}

static inline int getsockopt(int sock, int level, int optname,
void *optval, socklen_t *optlen)
{
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
return ztls_getsockopt(sock, level, optname, optval, optlen);
#else
return zsock_getsockopt(sock, level, optname, optval, optlen);
#endif
}

static inline int setsockopt(int sock, int level, int optname,
const void *optval, socklen_t optlen)
{
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
return ztls_setsockopt(sock, level, optname, optval, optlen);
#else
return zsock_setsockopt(sock, level, optname, optval, optlen);
#endif
}

static inline int getaddrinfo(const char *host, const char *service,
Expand Down
14 changes: 14 additions & 0 deletions lib/fdtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ void *z_get_fd_obj(int fd, const struct fd_op_vtable *vtable, int err)
return fd_entry->obj;
}

void *z_get_fd_obj_and_vtable(int fd, const struct fd_op_vtable **vtable)
{
struct fd_entry *fd_entry;

if (_check_fd(fd) < 0) {
return NULL;
}

fd_entry = &fdtable[fd];
*vtable = fd_entry->vtable;

return fd_entry->obj;
}

int z_reserve_fd(void)
{
int fd;
Expand Down
Loading