2017-04-06 19:16:01 -07:00
|
|
|
/*
|
2017-05-04 15:53:38 -07:00
|
|
|
* ZeroTier SDK - Network Virtualization Everywhere
|
2018-01-08 17:05:48 -08:00
|
|
|
* Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
|
2017-04-06 19:16:01 -07:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-05-04 15:35:50 -07:00
|
|
|
*
|
|
|
|
|
* --
|
|
|
|
|
*
|
|
|
|
|
* You can be released from the requirements of the license by purchasing
|
|
|
|
|
* a commercial license. Buying such a license is mandatory as soon as you
|
|
|
|
|
* develop commercial closed-source software that incorporates or links
|
|
|
|
|
* directly against ZeroTier software without disclosing the source code
|
|
|
|
|
* of your own application.
|
2017-04-06 19:16:01 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-09-27 02:29:04 -07:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
*
|
2018-07-19 17:19:06 -07:00
|
|
|
* Application-facing, socket-like API
|
2017-09-27 02:29:04 -07:00
|
|
|
*/
|
2017-04-06 19:16:01 -07:00
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
#include "libztDefs.h"
|
2017-05-05 16:46:07 -07:00
|
|
|
|
2017-09-27 02:29:04 -07:00
|
|
|
#include "lwip/sockets.h"
|
|
|
|
|
#include "lwip/ip_addr.h"
|
2017-10-09 00:07:31 -07:00
|
|
|
#include "lwip/netdb.h"
|
2017-04-06 19:16:01 -07:00
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
#include "libztDebug.h"
|
2017-10-18 17:57:51 -07:00
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
#include <string.h>
|
2017-10-18 17:57:51 -07:00
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
2017-10-18 17:57:51 -07:00
|
|
|
#endif
|
|
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
int platform_adjusted_socket_family(int family);
|
|
|
|
|
void fix_addr_socket_family(struct sockaddr *addr);
|
2017-11-21 15:53:31 -08:00
|
|
|
bool zts_ready();
|
2017-04-07 17:56:05 -07:00
|
|
|
|
2017-09-27 13:42:27 -07:00
|
|
|
int zts_socket(int socket_family, int socket_type, int protocol)
|
2017-04-07 17:56:05 -07:00
|
|
|
{
|
2017-09-27 02:29:04 -07:00
|
|
|
DEBUG_EXTRA("family=%d, type=%d, proto=%d", socket_family, socket_type, protocol);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
int socket_family_adj = platform_adjusted_socket_family(socket_family);
|
2017-11-21 15:53:31 -08:00
|
|
|
int err = lwip_socket(socket_family_adj, socket_type, protocol);
|
|
|
|
|
return err;
|
2017-04-07 17:56:05 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-27 13:42:27 -07:00
|
|
|
int zts_connect(int fd, const struct sockaddr *addr, socklen_t addrlen)
|
2017-04-07 17:56:05 -07:00
|
|
|
{
|
2017-09-27 02:29:04 -07:00
|
|
|
DEBUG_EXTRA("fd=%d",fd);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2017-09-27 02:29:04 -07:00
|
|
|
struct sockaddr_storage ss;
|
2017-10-18 17:57:51 -07:00
|
|
|
memcpy(&ss, addr, addrlen);
|
|
|
|
|
fix_addr_socket_family((struct sockaddr*)&ss);
|
2017-11-06 13:50:20 -08:00
|
|
|
return lwip_connect(fd, (struct sockaddr*)&ss, addrlen);
|
2017-04-06 19:16:01 -07:00
|
|
|
}
|
2017-04-07 17:56:05 -07:00
|
|
|
|
2017-09-27 13:42:27 -07:00
|
|
|
int zts_bind(int fd, const struct sockaddr *addr, socklen_t addrlen)
|
2017-04-07 17:56:05 -07:00
|
|
|
{
|
2017-09-27 13:42:27 -07:00
|
|
|
DEBUG_EXTRA("fd=%d", fd);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2017-09-27 02:29:04 -07:00
|
|
|
struct sockaddr_storage ss;
|
2017-10-18 17:57:51 -07:00
|
|
|
memcpy(&ss, addr, addrlen);
|
|
|
|
|
fix_addr_socket_family((struct sockaddr*)&ss);
|
2017-11-06 13:50:20 -08:00
|
|
|
return lwip_bind(fd, (struct sockaddr*)&ss, addrlen);
|
2017-04-06 19:16:01 -07:00
|
|
|
}
|
2017-04-07 17:56:05 -07:00
|
|
|
|
2017-09-27 13:42:27 -07:00
|
|
|
int zts_listen(int fd, int backlog)
|
2017-04-07 17:56:05 -07:00
|
|
|
{
|
2017-09-22 14:14:14 -07:00
|
|
|
DEBUG_EXTRA("fd=%d", fd);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return lwip_listen(fd, backlog);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-27 13:42:27 -07:00
|
|
|
int zts_accept(int fd, struct sockaddr *addr, socklen_t *addrlen)
|
2017-09-27 02:29:04 -07:00
|
|
|
{
|
2017-09-22 14:14:14 -07:00
|
|
|
DEBUG_EXTRA("fd=%d", fd);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return lwip_accept(fd, addr, addrlen);
|
2017-04-07 17:56:05 -07:00
|
|
|
}
|
|
|
|
|
|
2017-04-14 17:23:28 -07:00
|
|
|
#if defined(__linux__)
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_accept4(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags)
|
2017-09-14 13:17:37 -07:00
|
|
|
{
|
2017-09-27 02:29:04 -07:00
|
|
|
DEBUG_EXTRA("fd=%d", fd);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2018-07-19 17:19:06 -07:00
|
|
|
// lwip_accept4(fd, addr, addrlen, flags);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
2017-09-14 13:17:37 -07:00
|
|
|
}
|
2017-04-14 17:23:28 -07:00
|
|
|
#endif
|
|
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2017-09-22 14:14:14 -07:00
|
|
|
DEBUG_EXTRA("fd=%d, level=%d, optname=%d", fd, level, optname);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return lwip_setsockopt(fd, level, optname, optval, optlen);
|
2017-04-14 17:23:28 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2017-09-22 14:14:14 -07:00
|
|
|
DEBUG_EXTRA("fd=%d, level=%d, optname=%d", fd, level, optname);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return lwip_getsockopt(fd, level, optname, optval, optlen);
|
2017-04-14 17:23:28 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_getsockname(int fd, struct sockaddr *addr, socklen_t *addrlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2018-02-21 11:42:07 -08:00
|
|
|
DEBUG_EXTRA("fd=%d", fd);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return lwip_getsockname(fd, addr, addrlen);
|
2017-04-14 17:23:28 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_getpeername(int fd, struct sockaddr *addr, socklen_t *addrlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2017-09-27 02:29:04 -07:00
|
|
|
DEBUG_EXTRA("fd=%d", fd);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return lwip_getpeername(fd, addr, addrlen);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int zts_gethostname(char *name, size_t len)
|
2017-08-18 07:43:29 -07:00
|
|
|
{
|
2018-01-30 17:27:40 -08:00
|
|
|
DEBUG_EXTRA("");
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
2017-08-18 07:43:29 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_sethostname(const char *name, size_t len)
|
2017-08-18 07:43:29 -07:00
|
|
|
{
|
2018-01-30 17:27:40 -08:00
|
|
|
DEBUG_EXTRA("");
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
2017-08-18 07:43:29 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-09 00:07:31 -07:00
|
|
|
struct hostent *zts_gethostbyname(const char *name)
|
|
|
|
|
{
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2017-10-10 14:59:55 -07:00
|
|
|
// TODO: Test thread safety
|
2017-12-14 16:28:49 -08:00
|
|
|
/*
|
2017-10-09 00:07:31 -07:00
|
|
|
char buf[256];
|
|
|
|
|
int buflen = 256;
|
|
|
|
|
int h_err = 0;
|
|
|
|
|
struct hostent hret;
|
|
|
|
|
struct hostent **result = NULL;
|
|
|
|
|
int err = 0;
|
|
|
|
|
if ((err = lwip_gethostbyname_r(name, &hret, buf, buflen, result, &h_err)) != 0) {
|
|
|
|
|
DEBUG_ERROR("err = %d", err);
|
|
|
|
|
DEBUG_ERROR("h_err = %d", h_err);
|
|
|
|
|
errno = h_err;
|
|
|
|
|
return NULL; // failure
|
|
|
|
|
}
|
|
|
|
|
return *result;
|
2017-12-07 16:45:02 -08:00
|
|
|
|
2017-10-09 00:07:31 -07:00
|
|
|
return lwip_gethostbyname(name);
|
2017-12-14 16:28:49 -08:00
|
|
|
*/
|
|
|
|
|
return NULL;
|
2017-10-09 00:07:31 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_close(int fd)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2017-09-22 14:14:14 -07:00
|
|
|
DEBUG_EXTRA("fd=%d", fd);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return lwip_close(fd);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-09 17:56:40 -07:00
|
|
|
#if defined(__linux__)
|
2017-11-06 13:50:20 -08:00
|
|
|
/*
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_poll(struct pollfd *fds, nfds_t nfds, int timeout)
|
2017-07-12 11:44:31 -07:00
|
|
|
{
|
2017-09-29 13:25:11 -07:00
|
|
|
DEBUG_ERROR("warning, this is not implemented");
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2017-11-06 13:50:20 -08:00
|
|
|
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2018-07-19 17:19:06 -07:00
|
|
|
return poll(fds, nfds, timeout);
|
2017-07-12 11:44:31 -07:00
|
|
|
}
|
2017-11-06 13:50:20 -08:00
|
|
|
*/
|
2017-10-09 17:56:40 -07:00
|
|
|
#endif
|
2017-07-12 11:44:31 -07:00
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
int zts_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
|
2017-09-29 15:37:50 -07:00
|
|
|
struct timeval *timeout)
|
2017-07-12 11:44:31 -07:00
|
|
|
{
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return lwip_select(nfds, readfds, writefds, exceptfds, timeout);
|
2017-07-12 11:44:31 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_fcntl(int fd, int cmd, int flags)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2017-10-18 17:57:51 -07:00
|
|
|
DEBUG_EXTRA("fd=%d, cmd=%d, flags=%d", fd, cmd, flags);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2017-10-18 17:57:51 -07:00
|
|
|
// translation from platform flag values to stack flag values
|
2017-09-27 02:29:04 -07:00
|
|
|
int translated_flags = 0;
|
2017-10-18 17:57:51 -07:00
|
|
|
#if defined(__linux__)
|
2017-09-27 02:29:04 -07:00
|
|
|
if (flags == 2048) {
|
|
|
|
|
translated_flags = 1;
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
2017-10-18 17:57:51 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
if (flags == 4) {
|
|
|
|
|
translated_flags = 1;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2017-11-06 13:50:20 -08:00
|
|
|
return lwip_fcntl(fd, cmd, translated_flags);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int zts_ioctl(int fd, unsigned long request, void *argp)
|
2017-08-02 14:39:21 -07:00
|
|
|
{
|
2018-02-21 11:42:07 -08:00
|
|
|
DEBUG_EXTRA("fd=%d", fd);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return lwip_ioctl(fd, request, argp);
|
2017-08-02 14:39:21 -07:00
|
|
|
}
|
|
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
ssize_t zts_sendto(int fd, const void *buf, size_t len, int flags,
|
2017-09-29 15:37:50 -07:00
|
|
|
const struct sockaddr *addr, socklen_t addrlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2018-02-21 11:42:07 -08:00
|
|
|
DEBUG_TRANS("fd=%d, len=%zu", fd, len);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2017-09-27 02:29:04 -07:00
|
|
|
struct sockaddr_storage ss;
|
2017-10-18 17:57:51 -07:00
|
|
|
memcpy(&ss, addr, addrlen);
|
|
|
|
|
fix_addr_socket_family((struct sockaddr*)&ss);
|
2017-11-06 13:50:20 -08:00
|
|
|
return lwip_sendto(fd, buf, len, flags, (struct sockaddr*)&ss, addrlen);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ssize_t zts_send(int fd, const void *buf, size_t len, int flags)
|
2017-08-18 07:43:29 -07:00
|
|
|
{
|
2018-02-21 11:42:07 -08:00
|
|
|
DEBUG_TRANS("fd=%d, len=%zu", fd, len);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return lwip_send(fd, buf, len, flags);
|
2017-08-18 07:43:29 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
ssize_t zts_sendmsg(int fd, const struct msghdr *msg, int flags)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2017-09-22 14:14:14 -07:00
|
|
|
DEBUG_TRANS("fd=%d", fd);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return lwip_sendmsg(fd, msg, flags);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ssize_t zts_recv(int fd, void *buf, size_t len, int flags)
|
2017-08-18 07:43:29 -07:00
|
|
|
{
|
2018-02-21 12:02:16 -08:00
|
|
|
DEBUG_TRANS("fd=%d, len=%zu", fd, len);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return lwip_recv(fd, buf, len, flags);
|
2017-08-18 07:43:29 -07:00
|
|
|
}
|
|
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
ssize_t zts_recvfrom(int fd, void *buf, size_t len, int flags,
|
2017-09-29 15:37:50 -07:00
|
|
|
struct sockaddr *addr, socklen_t *addrlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2018-02-21 12:02:16 -08:00
|
|
|
DEBUG_TRANS("fd=%d, len=%zu", fd, len);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return lwip_recvfrom(fd, buf, len, flags, addr, addrlen);
|
2017-04-14 17:23:28 -07:00
|
|
|
}
|
|
|
|
|
|
2018-02-21 11:42:07 -08:00
|
|
|
ssize_t zts_recvmsg(int fd, struct msghdr *msg, int flags)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2017-09-27 02:29:04 -07:00
|
|
|
DEBUG_TRANS("fd=%d", fd);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2018-07-19 17:19:06 -07:00
|
|
|
return -1; // lwip_recvmsg(fd, msg, flags);
|
|
|
|
|
// Not currently implemented by stack
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-29 15:37:50 -07:00
|
|
|
int zts_read(int fd, void *buf, size_t len)
|
|
|
|
|
{
|
2018-02-21 11:42:07 -08:00
|
|
|
DEBUG_TRANS("fd=%d, len=%zu", fd, len);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return lwip_read(fd, buf, len);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
int zts_write(int fd, const void *buf, size_t len)
|
2017-09-29 15:37:50 -07:00
|
|
|
{
|
2018-02-21 11:42:07 -08:00
|
|
|
DEBUG_TRANS("fd=%d, len=%zu", fd, len);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return lwip_write(fd, buf, len);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int zts_shutdown(int fd, int how)
|
2017-07-12 11:44:31 -07:00
|
|
|
{
|
2017-09-27 02:29:04 -07:00
|
|
|
DEBUG_EXTRA("fd=%d, how=%d", fd, how);
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return lwip_shutdown(fd, how);
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int zts_add_dns_nameserver(struct sockaddr *addr)
|
|
|
|
|
{
|
2018-01-30 17:27:40 -08:00
|
|
|
DEBUG_EXTRA("");
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2018-07-19 17:19:06 -07:00
|
|
|
// TODO
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-30 14:13:13 -07:00
|
|
|
int zts_del_dns_nameserver(struct sockaddr *addr)
|
2017-08-24 11:45:39 -07:00
|
|
|
{
|
2018-01-30 17:27:40 -08:00
|
|
|
DEBUG_EXTRA("");
|
2017-11-21 15:53:31 -08:00
|
|
|
if (zts_ready() == false) {
|
2018-07-19 17:19:06 -07:00
|
|
|
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2018-07-19 17:19:06 -07:00
|
|
|
// TODO
|
2017-11-06 13:50:20 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The rationale for the following correctional methods is as follows:
|
|
|
|
|
|
|
|
|
|
Since we don't want the user of this library to worry about naming conflicts
|
|
|
|
|
with their native OS/platform's socket facilities we deliberately isolate what
|
|
|
|
|
is used by the userspace network stack and stack drivers from the user's
|
|
|
|
|
application. As a result of this, we must compensate for a few things on our
|
|
|
|
|
side. For instance, differing values for AF_INET6 on major operating systems, and
|
|
|
|
|
differing structure definitions for sockaddr.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* adjust socket_family value (when AF_INET6) for various platforms:
|
|
|
|
|
linux : 10
|
|
|
|
|
macOS : 30
|
|
|
|
|
windows: 23
|
|
|
|
|
*/
|
|
|
|
|
int platform_adjusted_socket_family(int family)
|
|
|
|
|
{
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
|
return family; // do nothing
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
return family == 30 ? AF_INET6 : family; // 10
|
2017-09-29 15:37:50 -07:00
|
|
|
#endif
|
2017-12-19 16:23:52 -08:00
|
|
|
#if defined(_WIN32)
|
2018-01-31 17:05:23 -08:00
|
|
|
if (family == 23) {
|
|
|
|
|
return AF_INET6;
|
|
|
|
|
}
|
|
|
|
|
if (family == 2) {
|
|
|
|
|
return AF_INET;
|
|
|
|
|
}
|
2018-07-27 09:53:08 -07:00
|
|
|
return -1;
|
2017-11-06 13:50:20 -08:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void fix_addr_socket_family(struct sockaddr *addr)
|
|
|
|
|
{
|
2018-01-31 17:05:23 -08:00
|
|
|
#if defined(__linux__) || defined(_WIN32)
|
|
|
|
|
/* struct sockaddr on Linux and Windows don't contain an sa_len field
|
2017-11-06 13:50:20 -08:00
|
|
|
so we must adjust it here before feeding it into the stack. */
|
|
|
|
|
if (addr->sa_len == 2) {
|
|
|
|
|
if (addr->sa_family == 0) {
|
|
|
|
|
addr->sa_family = addr->sa_len;
|
|
|
|
|
addr->sa_len = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (addr->sa_len == 10 || addr->sa_len == 23 || addr->sa_len == 30) {
|
|
|
|
|
if (addr->sa_family == 0) {
|
|
|
|
|
addr->sa_family = addr->sa_len;
|
|
|
|
|
addr->sa_len = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* once we've moved the value to its anticipated location, convert it from
|
|
|
|
|
its platform-specific value to one that the network stack can work with */
|
|
|
|
|
#endif
|
|
|
|
|
addr->sa_family = platform_adjusted_socket_family(addr->sa_family);
|
2017-08-18 07:43:29 -07:00
|
|
|
}
|
2017-08-02 14:39:21 -07:00
|
|
|
|
2017-04-06 19:16:01 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
2017-09-27 13:42:27 -07:00
|
|
|
#endif
|