2017-04-06 19:16:01 -07:00
|
|
|
/*
|
2021-02-04 11:03:55 -08:00
|
|
|
* Copyright (c)2013-2021 ZeroTier, Inc.
|
2017-04-06 19:16:01 -07:00
|
|
|
*
|
2020-04-13 23:38:06 -07:00
|
|
|
* Use of this software is governed by the Business Source License included
|
|
|
|
|
* in the LICENSE.TXT file in the project's root directory.
|
2017-04-06 19:16:01 -07:00
|
|
|
*
|
2021-02-04 11:03:55 -08:00
|
|
|
* Change Date: 2025-01-01
|
2017-04-06 19:16:01 -07:00
|
|
|
*
|
2020-04-13 23:38:06 -07:00
|
|
|
* On the date above, in accordance with the Business Source License, use
|
|
|
|
|
* of this software will be governed by version 2.0 of the Apache License.
|
2017-04-06 19:16:01 -07:00
|
|
|
*/
|
2020-04-13 23:38:06 -07:00
|
|
|
/****/
|
2017-04-06 19:16:01 -07:00
|
|
|
|
2017-09-27 02:29:04 -07:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
*
|
2019-01-14 12:01:29 -08:00
|
|
|
* ZeroTier Socket API
|
2017-09-27 02:29:04 -07:00
|
|
|
*/
|
2017-04-06 19:16:01 -07:00
|
|
|
|
2017-09-27 02:29:04 -07:00
|
|
|
#include "lwip/sockets.h"
|
2019-02-06 22:00:39 -08:00
|
|
|
#include "lwip/def.h"
|
2020-05-01 19:15:38 -07:00
|
|
|
#include "lwip/inet.h"
|
|
|
|
|
#include "lwip/stats.h"
|
2021-03-16 00:31:45 -07:00
|
|
|
#include "lwip/netdb.h"
|
|
|
|
|
#include "lwip/dns.h"
|
|
|
|
|
#include "lwip/ip_addr.h"
|
2019-02-06 22:00:39 -08:00
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
#include "ZeroTierSockets.h"
|
2020-05-30 18:29:04 -07:00
|
|
|
|
2021-02-24 01:25:15 -08:00
|
|
|
#define ZTS_STATE_NODE_RUNNING 0x01
|
|
|
|
|
#define ZTS_STATE_STACK_RUNNING 0x02
|
|
|
|
|
#define ZTS_STATE_NET_SERVICE_RUNNING 0x04
|
|
|
|
|
#define ZTS_STATE_CALLBACKS_RUNNING 0x08
|
|
|
|
|
#define ZTS_STATE_FREE_CALLED 0x10
|
2019-01-14 12:01:29 -08:00
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
extern int zts_errno;
|
2019-02-06 22:00:39 -08:00
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
namespace ZeroTier {
|
2017-10-18 17:57:51 -07:00
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
extern uint8_t _serviceStateFlags;
|
2019-02-06 22:00:39 -08:00
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
2017-10-18 17:57:51 -07:00
|
|
|
#endif
|
|
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
int zts_socket(const int socket_family, const int socket_type, const int protocol)
|
2017-04-07 17:56:05 -07:00
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
|
|
|
|
return lwip_socket(socket_family, socket_type, protocol);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2017-04-07 17:56:05 -07:00
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
int zts_connect(int fd, const struct zts_sockaddr *addr, zts_socklen_t addrlen)
|
2019-01-14 12:01:29 -08:00
|
|
|
{
|
2021-03-14 01:02:43 -08:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
if (!addr) {
|
2020-05-01 19:15:38 -07:00
|
|
|
return ZTS_ERR_ARG;
|
|
|
|
|
}
|
|
|
|
|
if (addrlen > (int)sizeof(struct zts_sockaddr_storage) || addrlen < (int)sizeof(struct zts_sockaddr_in)) {
|
|
|
|
|
return ZTS_ERR_ARG;
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
return lwip_connect(fd, (sockaddr*)addr, addrlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2017-04-07 17:56:05 -07:00
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
int zts_bind(int fd, const struct zts_sockaddr *addr, zts_socklen_t addrlen)
|
2019-01-14 12:01:29 -08:00
|
|
|
{
|
2021-03-14 01:02:43 -08:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
if (!addr) {
|
2020-05-01 19:15:38 -07:00
|
|
|
return ZTS_ERR_ARG;
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
if (addrlen > (int)sizeof(struct zts_sockaddr_storage) || addrlen < (int)sizeof(struct zts_sockaddr_in)) {
|
|
|
|
|
return ZTS_ERR_ARG;
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
return lwip_bind(fd, (sockaddr*)addr, addrlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2021-02-24 01:25:15 -08:00
|
|
|
|
2017-09-27 13:42:27 -07:00
|
|
|
int zts_listen(int fd, int backlog)
|
2017-04-07 17:56:05 -07:00
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
|
|
|
|
return lwip_listen(fd, backlog);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2017-09-22 14:14:14 -07:00
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
int zts_accept(int fd, struct zts_sockaddr *addr, zts_socklen_t *addrlen)
|
2017-09-27 02:29:04 -07:00
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
|
|
|
|
return lwip_accept(fd, (sockaddr*)addr, (socklen_t*)addrlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2017-04-07 17:56:05 -07:00
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
int zts_setsockopt(int fd, int level, int optname, const void *optval,zts_socklen_t optlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
|
|
|
|
return lwip_setsockopt(fd, level, optname, optval, optlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2017-04-14 17:23:28 -07:00
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
int zts_getsockopt(int fd, int level, int optname, void *optval, zts_socklen_t *optlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
|
|
|
|
return lwip_getsockopt(fd, level, optname, optval, (socklen_t*)optlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2017-04-14 17:23:28 -07:00
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
int zts_getsockname(int fd, struct zts_sockaddr *addr, zts_socklen_t *addrlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2021-03-14 01:02:43 -08:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
if (!addr) {
|
2020-05-01 19:15:38 -07:00
|
|
|
return ZTS_ERR_ARG;
|
|
|
|
|
}
|
|
|
|
|
if (*addrlen > (int)sizeof(struct zts_sockaddr_storage) || *addrlen < (int)sizeof(struct zts_sockaddr_in)) {
|
|
|
|
|
return ZTS_ERR_ARG;
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
return lwip_getsockname(fd, (sockaddr*)addr, (socklen_t*)addrlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2017-04-14 17:23:28 -07:00
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
int zts_getpeername(int fd, struct zts_sockaddr *addr, zts_socklen_t *addrlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2021-03-14 01:02:43 -08:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
if (!addr) {
|
2020-05-01 19:15:38 -07:00
|
|
|
return ZTS_ERR_ARG;
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
if (*addrlen > (int)sizeof(struct zts_sockaddr_storage) || *addrlen < (int)sizeof(struct zts_sockaddr_in)) {
|
|
|
|
|
return ZTS_ERR_ARG;
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
return lwip_getpeername(fd, (sockaddr*)addr, (socklen_t*)addrlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2017-09-22 14:14:14 -07:00
|
|
|
|
|
|
|
|
int zts_close(int fd)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
|
|
|
|
return lwip_close(fd);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2017-07-12 11:44:31 -07:00
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
int zts_select(int nfds, zts_fd_set *readfds, zts_fd_set *writefds, zts_fd_set *exceptfds,
|
|
|
|
|
struct zts_timeval *timeout)
|
2017-07-12 11:44:31 -07:00
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
|
|
|
|
return lwip_select(nfds, (fd_set*)readfds, (fd_set*)writefds, (fd_set*)exceptfds, (timeval*)timeout);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
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
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
2020-05-05 14:35:22 -07:00
|
|
|
return lwip_fcntl(fd, cmd, flags);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2017-09-22 14:14:14 -07:00
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
int zts_poll(struct zts_pollfd *fds, nfds_t nfds, int timeout)
|
|
|
|
|
{
|
|
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
|
|
|
|
return lwip_poll((pollfd*)fds, nfds, timeout);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
2021-03-14 01:02:43 -08:00
|
|
|
if (!argp) {
|
|
|
|
|
return ZTS_ERR_ARG;
|
|
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
return lwip_ioctl(fd, request, argp);
|
2017-08-02 14:39:21 -07:00
|
|
|
}
|
2019-01-14 12:01:29 -08:00
|
|
|
|
|
|
|
|
ssize_t zts_send(int fd, const void *buf, size_t len, int flags)
|
|
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2021-03-14 01:02:43 -08:00
|
|
|
if (!buf) {
|
|
|
|
|
return ZTS_ERR_ARG;
|
|
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
return lwip_send(fd, buf, len, flags);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2017-08-02 14:39:21 -07:00
|
|
|
|
2021-03-01 22:34:12 -08:00
|
|
|
ssize_t zts_sendto(int fd, const void *buf, size_t len, int flags,
|
2020-05-01 19:15:38 -07:00
|
|
|
const struct zts_sockaddr *addr,zts_socklen_t addrlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2021-03-14 01:02:43 -08:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
2021-02-02 11:36:51 -08:00
|
|
|
if (!addr || !buf) {
|
2020-05-01 19:15:38 -07:00
|
|
|
return ZTS_ERR_ARG;
|
|
|
|
|
}
|
|
|
|
|
if (addrlen > (int)sizeof(struct zts_sockaddr_storage) || addrlen < (int)sizeof(struct zts_sockaddr_in)) {
|
|
|
|
|
return ZTS_ERR_ARG;
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
return lwip_sendto(fd, buf, len, flags, (sockaddr*)addr, addrlen);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
2017-08-18 07:43:29 -07:00
|
|
|
|
2021-03-15 01:59:18 -07:00
|
|
|
ssize_t zts_sendmsg(int fd, const struct zts_msghdr *msg, int flags)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
2021-03-15 01:59:18 -07:00
|
|
|
return lwip_sendmsg(fd, (const struct msghdr *)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
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2021-03-14 01:02:43 -08:00
|
|
|
if (!buf) {
|
|
|
|
|
return ZTS_ERR_ARG;
|
|
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
return lwip_recv(fd, buf, len, flags);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2017-08-18 07:43:29 -07:00
|
|
|
|
2021-03-01 22:34:12 -08:00
|
|
|
ssize_t zts_recvfrom(int fd, void *buf, size_t len, int flags,
|
2020-05-01 19:15:38 -07:00
|
|
|
struct zts_sockaddr *addr, zts_socklen_t *addrlen)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2021-03-14 01:02:43 -08:00
|
|
|
if (!buf) {
|
|
|
|
|
return ZTS_ERR_ARG;
|
|
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
return lwip_recvfrom(fd, buf, len, flags, (sockaddr*)addr, (socklen_t*)addrlen);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2017-04-14 17:23:28 -07:00
|
|
|
|
2021-03-15 01:59:18 -07:00
|
|
|
ssize_t zts_recvmsg(int fd, struct zts_msghdr *msg, int flags)
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
2021-03-14 01:02:43 -08:00
|
|
|
if (!msg) {
|
|
|
|
|
return ZTS_ERR_ARG;
|
|
|
|
|
}
|
2021-03-15 01:59:18 -07:00
|
|
|
return lwip_recvmsg(fd, (struct msghdr *)msg, flags);
|
2017-09-22 14:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
ssize_t zts_read(int fd, void *buf, size_t len)
|
2017-09-29 15:37:50 -07:00
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2021-03-14 01:02:43 -08:00
|
|
|
if (!buf) {
|
|
|
|
|
return ZTS_ERR_ARG;
|
|
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
return lwip_read(fd, buf, len);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2017-09-22 14:14:14 -07:00
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
ssize_t zts_readv(int s, const struct zts_iovec *iov, int iovcnt)
|
|
|
|
|
{
|
|
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
|
|
|
|
return lwip_readv(s, (iovec*)iov, iovcnt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ssize_t zts_write(int fd, const void *buf, size_t len)
|
2017-09-29 15:37:50 -07:00
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
2021-03-14 01:02:43 -08:00
|
|
|
if (!buf) {
|
|
|
|
|
return ZTS_ERR_ARG;
|
|
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
return lwip_write(fd, buf, len);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2017-09-22 14:14:14 -07:00
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
ssize_t zts_writev(int fd, const struct zts_iovec *iov, int iovcnt)
|
|
|
|
|
{
|
|
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
|
|
|
|
return lwip_writev(fd, (iovec*)iov, iovcnt);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 14:14:14 -07:00
|
|
|
int zts_shutdown(int fd, int how)
|
2017-07-12 11:44:31 -07:00
|
|
|
{
|
2020-05-01 19:15:38 -07:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
|
|
|
|
return lwip_shutdown(fd, how);
|
2019-01-14 12:01:29 -08:00
|
|
|
}
|
2017-08-24 11:45:39 -07:00
|
|
|
|
2021-03-16 00:31:45 -07:00
|
|
|
struct zts_hostent *zts_gethostbyname(const char *name)
|
2020-05-01 19:15:38 -07:00
|
|
|
{
|
|
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
2021-03-16 00:31:45 -07:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (!name) {
|
|
|
|
|
return NULL;
|
2020-05-01 19:15:38 -07:00
|
|
|
}
|
2021-03-16 00:31:45 -07:00
|
|
|
return (struct zts_hostent *)lwip_gethostbyname(name);
|
2020-05-01 19:15:38 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-16 00:31:45 -07:00
|
|
|
int zts_dns_set_server(uint8_t index, const zts_ip_addr *addr)
|
2020-05-01 19:15:38 -07:00
|
|
|
{
|
|
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
2021-03-16 00:31:45 -07:00
|
|
|
if (index >= DNS_MAX_SERVERS) {
|
|
|
|
|
return ZTS_ERR_ARG;
|
|
|
|
|
}
|
|
|
|
|
if (!addr) {
|
|
|
|
|
return ZTS_ERR_ARG;
|
|
|
|
|
}
|
|
|
|
|
dns_setserver(index, (const ip_addr_t *)addr);
|
|
|
|
|
return ZTS_ERR_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const zts_ip_addr *zts_dns_get_server(uint8_t index)
|
|
|
|
|
{
|
|
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (index >= DNS_MAX_SERVERS) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return (const zts_ip_addr *)dns_getserver(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *zts_ipaddr_ntoa(const zts_ip_addr *addr)
|
|
|
|
|
{
|
|
|
|
|
return ipaddr_ntoa((ip_addr_t *)addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int zts_ipaddr_aton(const char *cp, zts_ip_addr *addr)
|
|
|
|
|
{
|
|
|
|
|
return ipaddr_aton(cp, (ip_addr_t *)addr);
|
2020-05-01 19:15:38 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-16 00:31:45 -07:00
|
|
|
const char *zts_inet_ntop(int af, const void *src, char *dst, zts_socklen_t size)
|
2020-05-01 19:15:38 -07:00
|
|
|
{
|
|
|
|
|
return lwip_inet_ntop(af,src,dst,size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int zts_inet_pton(int af, const char *src, void *dst)
|
|
|
|
|
{
|
|
|
|
|
return lwip_inet_pton(af,src,dst);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Statistics //
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2021-02-24 01:25:15 -08:00
|
|
|
#ifdef ZTS_ENABLE_STATS
|
|
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
extern struct stats_ lwip_stats;
|
|
|
|
|
|
|
|
|
|
int zts_get_all_stats(struct zts_stats *statsDest)
|
|
|
|
|
{
|
2021-03-14 01:02:43 -08:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
#if LWIP_STATS
|
|
|
|
|
if (!statsDest) {
|
|
|
|
|
return ZTS_ERR_ARG;
|
|
|
|
|
}
|
|
|
|
|
memset(statsDest, 0, sizeof(struct zts_stats));
|
|
|
|
|
// Copy lwIP stats
|
|
|
|
|
memcpy(&(statsDest->link), &(lwip_stats.link), sizeof(struct stats_proto));
|
|
|
|
|
memcpy(&(statsDest->etharp), &(lwip_stats.etharp), sizeof(struct stats_proto));
|
|
|
|
|
memcpy(&(statsDest->ip_frag), &(lwip_stats.ip_frag), sizeof(struct stats_proto));
|
|
|
|
|
memcpy(&(statsDest->ip), &(lwip_stats.ip), sizeof(struct stats_proto));
|
|
|
|
|
memcpy(&(statsDest->icmp), &(lwip_stats.icmp), sizeof(struct stats_proto));
|
|
|
|
|
//memcpy(&(statsDest->igmp), &(lwip_stats.igmp), sizeof(struct stats_igmp));
|
|
|
|
|
memcpy(&(statsDest->udp), &(lwip_stats.udp), sizeof(struct stats_proto));
|
|
|
|
|
memcpy(&(statsDest->tcp), &(lwip_stats.tcp), sizeof(struct stats_proto));
|
|
|
|
|
// mem omitted
|
|
|
|
|
// memp omitted
|
|
|
|
|
memcpy(&(statsDest->sys), &(lwip_stats.sys), sizeof(struct stats_sys));
|
|
|
|
|
memcpy(&(statsDest->ip6), &(lwip_stats.ip6), sizeof(struct stats_proto));
|
|
|
|
|
memcpy(&(statsDest->icmp6), &(lwip_stats.icmp6), sizeof(struct stats_proto));
|
|
|
|
|
memcpy(&(statsDest->ip6_frag), &(lwip_stats.ip6_frag), sizeof(struct stats_proto));
|
|
|
|
|
memcpy(&(statsDest->mld6), &(lwip_stats.mld6), sizeof(struct stats_igmp));
|
|
|
|
|
memcpy(&(statsDest->nd6), &(lwip_stats.nd6), sizeof(struct stats_proto));
|
|
|
|
|
memcpy(&(statsDest->ip_frag), &(lwip_stats.ip_frag), sizeof(struct stats_proto));
|
|
|
|
|
// mib2 omitted
|
|
|
|
|
// Copy ZT stats
|
|
|
|
|
// ...
|
|
|
|
|
return ZTS_ERR_OK;
|
|
|
|
|
#else
|
|
|
|
|
return ZTS_ERR_NO_RESULT;
|
|
|
|
|
#endif
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
|
|
|
|
|
2020-05-01 19:15:38 -07:00
|
|
|
int zts_get_protocol_stats(int protocolType, void *protoStatsDest)
|
2017-08-24 11:45:39 -07:00
|
|
|
{
|
2021-03-14 01:02:43 -08:00
|
|
|
if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) {
|
|
|
|
|
return ZTS_ERR_SERVICE;
|
|
|
|
|
}
|
2020-05-01 19:15:38 -07:00
|
|
|
#if LWIP_STATS
|
|
|
|
|
if (!protoStatsDest) {
|
|
|
|
|
return ZTS_ERR_ARG;
|
|
|
|
|
}
|
|
|
|
|
memset(protoStatsDest, 0, sizeof(struct stats_proto));
|
|
|
|
|
switch (protocolType)
|
|
|
|
|
{
|
|
|
|
|
case ZTS_STATS_PROTOCOL_LINK:
|
|
|
|
|
memcpy(protoStatsDest, &(lwip_stats.link), sizeof(struct stats_proto));
|
|
|
|
|
break;
|
|
|
|
|
case ZTS_STATS_PROTOCOL_ETHARP:
|
|
|
|
|
memcpy(protoStatsDest, &(lwip_stats.etharp), sizeof(struct stats_proto));
|
|
|
|
|
break;
|
|
|
|
|
case ZTS_STATS_PROTOCOL_IP:
|
|
|
|
|
memcpy(protoStatsDest, &(lwip_stats.ip), sizeof(struct stats_proto));
|
|
|
|
|
break;
|
|
|
|
|
case ZTS_STATS_PROTOCOL_UDP:
|
|
|
|
|
memcpy(protoStatsDest, &(lwip_stats.udp), sizeof(struct stats_proto));
|
|
|
|
|
break;
|
|
|
|
|
case ZTS_STATS_PROTOCOL_TCP:
|
|
|
|
|
memcpy(protoStatsDest, &(lwip_stats.tcp), sizeof(struct stats_proto));
|
|
|
|
|
break;
|
|
|
|
|
case ZTS_STATS_PROTOCOL_ICMP:
|
|
|
|
|
memcpy(protoStatsDest, &(lwip_stats.icmp), sizeof(struct stats_proto));
|
|
|
|
|
break;
|
|
|
|
|
case ZTS_STATS_PROTOCOL_IP_FRAG:
|
|
|
|
|
memcpy(protoStatsDest, &(lwip_stats.ip_frag), sizeof(struct stats_proto));
|
|
|
|
|
break;
|
|
|
|
|
case ZTS_STATS_PROTOCOL_IP6:
|
|
|
|
|
memcpy(protoStatsDest, &(lwip_stats.ip6), sizeof(struct stats_proto));
|
|
|
|
|
break;
|
|
|
|
|
case ZTS_STATS_PROTOCOL_ICMP6:
|
|
|
|
|
memcpy(protoStatsDest, &(lwip_stats.icmp6), sizeof(struct stats_proto));
|
|
|
|
|
break;
|
|
|
|
|
case ZTS_STATS_PROTOCOL_IP6_FRAG:
|
|
|
|
|
memcpy(protoStatsDest, &(lwip_stats.ip6_frag), sizeof(struct stats_proto));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return ZTS_ERR_ARG;
|
|
|
|
|
}
|
|
|
|
|
return ZTS_ERR_OK;
|
|
|
|
|
#else
|
|
|
|
|
return ZTS_ERR_NO_RESULT;
|
|
|
|
|
#endif
|
2017-11-06 13:50:20 -08:00
|
|
|
}
|
|
|
|
|
|
2021-02-24 01:25:15 -08:00
|
|
|
#endif // ZTS_ENABLE_STATS
|
|
|
|
|
|
2017-04-06 19:16:01 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
2017-09-27 13:42:27 -07:00
|
|
|
#endif
|
2019-02-06 22:00:39 -08:00
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|