Preparation for C--, removed classes, namespaces, advanced build options are now controlled via include/libztDefs.h
This commit is contained in:
143
src/RingBuffer.cpp
Normal file
143
src/RingBuffer.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* Ring buffer implementation for network stack drivers
|
||||
*/
|
||||
|
||||
#include <memory.h>
|
||||
#include <algorithm>
|
||||
|
||||
#include "RingBuffer.h"
|
||||
|
||||
bufElementType* RingBuffer::get_buf()
|
||||
{
|
||||
return buf + begin;
|
||||
}
|
||||
|
||||
size_t RingBuffer::produce(size_t n)
|
||||
{
|
||||
n = std::min(n, getFree());
|
||||
if (n == 0) {
|
||||
return n;
|
||||
}
|
||||
const size_t first_chunk = std::min(n, size - end);
|
||||
end = (end + first_chunk) % size;
|
||||
if (first_chunk < n) {
|
||||
const size_t second_chunk = n - first_chunk;
|
||||
end = (end + second_chunk) % size;
|
||||
}
|
||||
if (begin == end) {
|
||||
wrap = true;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
void RingBuffer::reset()
|
||||
{
|
||||
consume(count());
|
||||
}
|
||||
|
||||
size_t RingBuffer::consume(size_t n)
|
||||
{
|
||||
n = std::min(n, count());
|
||||
if (n == 0) {
|
||||
return n;
|
||||
}
|
||||
if (wrap) {
|
||||
wrap = false;
|
||||
}
|
||||
const size_t first_chunk = std::min(n, size - begin);
|
||||
begin = (begin + first_chunk) % size;
|
||||
if (first_chunk < n) {
|
||||
const size_t second_chunk = n - first_chunk;
|
||||
begin = (begin + second_chunk) % size;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t RingBuffer::write(const bufElementType * data, size_t n)
|
||||
{
|
||||
n = std::min(n, getFree());
|
||||
|
||||
if (n == 0) {
|
||||
return n;
|
||||
}
|
||||
const size_t first_chunk = std::min(n, size - end);
|
||||
memcpy(buf + end, data, first_chunk * sizeof(bufElementType));
|
||||
end = (end + first_chunk) % size;
|
||||
if (first_chunk < n) {
|
||||
const size_t second_chunk = n - first_chunk;
|
||||
memcpy(buf + end, data + first_chunk, second_chunk * sizeof(bufElementType));
|
||||
end = (end + second_chunk) % size;
|
||||
}
|
||||
if (begin == end) {
|
||||
wrap = true;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t RingBuffer::read(bufElementType * dest, size_t n)
|
||||
{
|
||||
n = std::min(n, count());
|
||||
|
||||
if (n == 0) {
|
||||
return n;
|
||||
}
|
||||
if (wrap) {
|
||||
wrap = false;
|
||||
}
|
||||
const size_t first_chunk = std::min(n, size - begin);
|
||||
memcpy(dest, buf + begin, first_chunk * sizeof(bufElementType));
|
||||
begin = (begin + first_chunk) % size;
|
||||
if (first_chunk < n) {
|
||||
const size_t second_chunk = n - first_chunk;
|
||||
memcpy(dest + first_chunk, buf + begin, second_chunk * sizeof(bufElementType));
|
||||
begin = (begin + second_chunk) % size;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t RingBuffer::count()
|
||||
{
|
||||
if (end == begin) {
|
||||
return wrap ? size : 0;
|
||||
}
|
||||
else if (end > begin) {
|
||||
return end - begin;
|
||||
}
|
||||
else {
|
||||
return size + end - begin;
|
||||
}
|
||||
}
|
||||
|
||||
size_t RingBuffer::getFree()
|
||||
{
|
||||
return size - count();
|
||||
}
|
||||
|
||||
@@ -1,185 +0,0 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* Ring buffer implementation for network stack drivers
|
||||
*/
|
||||
|
||||
#ifndef ZT_RINGBUFFER_HPP
|
||||
#define ZT_RINGBUFFER_HPP
|
||||
|
||||
#include <memory.h>
|
||||
#include <algorithm>
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
template<typename T> class RingBuffer {
|
||||
|
||||
private:
|
||||
T * buf;
|
||||
size_t size;
|
||||
size_t begin;
|
||||
size_t end;
|
||||
bool wrap;
|
||||
|
||||
public:
|
||||
/**
|
||||
* create a RingBuffer with space for up to size elements.
|
||||
*/
|
||||
explicit RingBuffer(size_t size)
|
||||
: size(size),
|
||||
begin(0),
|
||||
end(0),
|
||||
wrap(false)
|
||||
{
|
||||
buf = new T[size];
|
||||
}
|
||||
|
||||
RingBuffer(const RingBuffer<T> & ring)
|
||||
{
|
||||
this(ring.size);
|
||||
begin = ring.begin;
|
||||
end = ring.end;
|
||||
memcpy(buf, ring.buf, sizeof(T) * size);
|
||||
}
|
||||
|
||||
~RingBuffer()
|
||||
{
|
||||
delete[] buf;
|
||||
}
|
||||
|
||||
// get a reference to the underlying buffer
|
||||
T* get_buf()
|
||||
{
|
||||
return buf + begin;
|
||||
}
|
||||
|
||||
// adjust buffer index pointer as if we copied data in
|
||||
size_t produce(size_t n)
|
||||
{
|
||||
n = std::min(n, getFree());
|
||||
if (n == 0) {
|
||||
return n;
|
||||
}
|
||||
const size_t first_chunk = std::min(n, size - end);
|
||||
end = (end + first_chunk) % size;
|
||||
if (first_chunk < n) {
|
||||
const size_t second_chunk = n - first_chunk;
|
||||
end = (end + second_chunk) % size;
|
||||
}
|
||||
if (begin == end) {
|
||||
wrap = true;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
// merely reset the buffer pointer, doesn't erase contents
|
||||
void reset()
|
||||
{
|
||||
consume(count());
|
||||
}
|
||||
|
||||
// adjust buffer index pointer as if we copied data out
|
||||
size_t consume(size_t n)
|
||||
{
|
||||
n = std::min(n, count());
|
||||
if (n == 0) {
|
||||
return n;
|
||||
}
|
||||
if (wrap) {
|
||||
wrap = false;
|
||||
}
|
||||
const size_t first_chunk = std::min(n, size - begin);
|
||||
begin = (begin + first_chunk) % size;
|
||||
if (first_chunk < n) {
|
||||
const size_t second_chunk = n - first_chunk;
|
||||
begin = (begin + second_chunk) % size;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t write(const T * data, size_t n)
|
||||
{
|
||||
n = std::min(n, getFree());
|
||||
|
||||
if (n == 0) {
|
||||
return n;
|
||||
}
|
||||
const size_t first_chunk = std::min(n, size - end);
|
||||
memcpy(buf + end, data, first_chunk * sizeof(T));
|
||||
end = (end + first_chunk) % size;
|
||||
if (first_chunk < n) {
|
||||
const size_t second_chunk = n - first_chunk;
|
||||
memcpy(buf + end, data + first_chunk, second_chunk * sizeof(T));
|
||||
end = (end + second_chunk) % size;
|
||||
}
|
||||
if (begin == end) {
|
||||
wrap = true;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t read(T * dest, size_t n)
|
||||
{
|
||||
n = std::min(n, count());
|
||||
|
||||
if (n == 0) {
|
||||
return n;
|
||||
}
|
||||
if (wrap) {
|
||||
wrap = false;
|
||||
}
|
||||
const size_t first_chunk = std::min(n, size - begin);
|
||||
memcpy(dest, buf + begin, first_chunk * sizeof(T));
|
||||
begin = (begin + first_chunk) % size;
|
||||
if (first_chunk < n) {
|
||||
const size_t second_chunk = n - first_chunk;
|
||||
memcpy(dest + first_chunk, buf + begin, second_chunk * sizeof(T));
|
||||
begin = (begin + second_chunk) % size;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t count() {
|
||||
if (end == begin) {
|
||||
return wrap ? size : 0;
|
||||
}
|
||||
else if (end > begin) {
|
||||
return end - begin;
|
||||
}
|
||||
else {
|
||||
return size + end - begin;
|
||||
}
|
||||
}
|
||||
|
||||
size_t getFree() {
|
||||
return size - count();
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif // ZT_RINGBUFFER_HPP
|
||||
@@ -30,7 +30,20 @@
|
||||
* Platform-specific implementations of common functions
|
||||
*/
|
||||
|
||||
#include "Platform.h"
|
||||
#if defined(STACK_LWIP)
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/netdb.h"
|
||||
#include "dns.h"
|
||||
#endif
|
||||
#if defined(NO_STACK)
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include "SysUtils.h"
|
||||
#include <stdint.h>
|
||||
#include <pthread.h>
|
||||
|
||||
@@ -39,17 +52,6 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void handle_general_failure() {
|
||||
#ifdef ZT_EXIT_ON_GENERAL_FAIL
|
||||
DEBUG_ERROR("exiting (ZT_EXIT_ON_GENERAL_FAIL==1)");
|
||||
exit(-1);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline unsigned int gettid()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
@@ -63,7 +65,3 @@ inline unsigned int gettid()
|
||||
return static_cast<unsigned int>(tid64);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -30,8 +30,7 @@
|
||||
* Misc utilities
|
||||
*/
|
||||
|
||||
#include "InetAddress.hpp"
|
||||
#include "Debug.hpp"
|
||||
//#include "libztDebug.h"
|
||||
#include "Utilities.h"
|
||||
|
||||
#if defined(__MINGW32__)
|
||||
@@ -194,10 +193,6 @@ int inet_pton(int af, const char *src, void *dst)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
char *beautify_eth_proto_nums(int proto)
|
||||
{
|
||||
if (proto == 0x0800) return (char*)"IPv4";
|
||||
@@ -253,81 +248,8 @@ char *beautify_eth_proto_nums(int proto)
|
||||
return (char*)"UNKNOWN";
|
||||
}
|
||||
|
||||
bool ipv6_in_subnet(ZeroTier::InetAddress *subnet, ZeroTier::InetAddress *addr)
|
||||
{
|
||||
ZeroTier::InetAddress r(addr);
|
||||
ZeroTier::InetAddress b(subnet);
|
||||
const unsigned int bits = subnet->netmaskBits();
|
||||
switch(r.ss_family) {
|
||||
case AF_INET:
|
||||
reinterpret_cast<struct sockaddr_in *>(&r)->sin_addr.s_addr &= ZeroTier::Utils::hton((uint32_t)(0xffffffff << (32 - bits)));
|
||||
break;
|
||||
case AF_INET6: {
|
||||
uint64_t nm[2];
|
||||
uint64_t nm2[2];
|
||||
memcpy(nm,reinterpret_cast<struct sockaddr_in6 *>(&r)->sin6_addr.s6_addr,16);
|
||||
memcpy(nm2,reinterpret_cast<struct sockaddr_in6 *>(&b)->sin6_addr.s6_addr,16);
|
||||
|
||||
nm[0] &= ZeroTier::Utils::hton((uint64_t)((bits >= 64) ? 0xffffffffffffffffULL : (0xffffffffffffffffULL << (64 - bits))));
|
||||
nm[1] &= ZeroTier::Utils::hton((uint64_t)((bits <= 64) ? 0ULL : (0xffffffffffffffffULL << (128 - bits))));
|
||||
|
||||
nm2[0] &= ZeroTier::Utils::hton((uint64_t)((bits >= 64) ? 0xffffffffffffffffULL : (0xffffffffffffffffULL << (64 - bits))));
|
||||
nm2[1] &= ZeroTier::Utils::hton((uint64_t)((bits <= 64) ? 0ULL : (0xffffffffffffffffULL << (128 - bits))));
|
||||
|
||||
memcpy(reinterpret_cast<struct sockaddr_in6 *>(&r)->sin6_addr.s6_addr,nm,16);
|
||||
memcpy(reinterpret_cast<struct sockaddr_in6 *>(&b)->sin6_addr.s6_addr,nm2,16);
|
||||
}
|
||||
break;
|
||||
}
|
||||
char b0[64], b1[64];
|
||||
memset(b0, 0, 64);
|
||||
memset(b1, 0, 64);
|
||||
return !strcmp(r.toIpString(b0), b.toIpString(b1));
|
||||
}
|
||||
|
||||
/*
|
||||
void sockaddr2inet(int socket_family, const struct sockaddr *addr, ZeroTier::InetAddress *inet)
|
||||
{
|
||||
char ipstr[INET6_ADDRSTRLEN];
|
||||
memset(ipstr, 0, INET6_ADDRSTRLEN);
|
||||
if (socket_family == AF_INET) {
|
||||
inet_ntop(AF_INET,
|
||||
(const void *)&((struct sockaddr_in *)addr)->sin_addr.s_addr, ipstr, INET_ADDRSTRLEN);
|
||||
inet->fromString(ipstr);
|
||||
}
|
||||
if (socket_family == AF_INET6) {
|
||||
inet_ntop(AF_INET6,
|
||||
(const void *)&((struct sockaddr_in6 *)addr)->sin6_addr.s6_addr, ipstr, INET6_ADDRSTRLEN);
|
||||
char addrstr[64];
|
||||
sprintf(addrstr, "%s", ipstr);
|
||||
inet->fromString(addrstr);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
void mac2str(char *macbuf, int len, unsigned char* addr)
|
||||
{
|
||||
snprintf(macbuf, len, "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert from standard IPV6 address structure to an lwIP native structure
|
||||
*/
|
||||
/*
|
||||
inline void in6_to_ip6(ip6_addr_t *ba, struct sockaddr_in6 *in6)
|
||||
{
|
||||
uint8_t *ip = &(in6->sin6_addr).s6_addr[0];
|
||||
IP6_ADDR2(ba,
|
||||
(((ip[ 0] & 0xffff) << 8) | ((ip[ 1]) & 0xffff)),
|
||||
(((ip[ 2] & 0xffff) << 8) | ((ip[ 3]) & 0xffff)),
|
||||
(((ip[ 4] & 0xffff) << 8) | ((ip[ 5]) & 0xffff)),
|
||||
(((ip[ 6] & 0xffff) << 8) | ((ip[ 7]) & 0xffff)),
|
||||
(((ip[ 8] & 0xffff) << 8) | ((ip[ 9]) & 0xffff)),
|
||||
(((ip[10] & 0xffff) << 8) | ((ip[11]) & 0xffff)),
|
||||
(((ip[12] & 0xffff) << 8) | ((ip[13]) & 0xffff)),
|
||||
(((ip[14] & 0xffff) << 8) | ((ip[15]) & 0xffff))
|
||||
);
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
// Only available in experimental branch
|
||||
@@ -27,97 +27,79 @@
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* Platform-agnostic implementation of a socket-like object
|
||||
* Platform- and stack-agnostic implementation of a socket-like object
|
||||
*/
|
||||
|
||||
#ifndef ZT_VIRTUALSOCKET_HPP
|
||||
#define ZT_VIRTUALSOCKET_HPP
|
||||
#include "libztDefs.h"
|
||||
|
||||
#ifdef ZT_VIRTUAL_SOCKET
|
||||
|
||||
#include <ctime>
|
||||
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
#include "Phy.hpp"
|
||||
|
||||
#include "libzt.h"
|
||||
#include "libztDebug.h"
|
||||
#include "VirtualSocket.h"
|
||||
#include "VirtualBindingPair.h"
|
||||
#include "RingBuffer.hpp"
|
||||
#include "VirtualTap.h"
|
||||
#include "RingBuffer.h"
|
||||
|
||||
#define VS_STATE_INACTIVE 0x000000u // Default value for newly created VirtualSocket
|
||||
#define VS_STATE_ACTIVE 0x000001u // VirtualSocket is RX'ing or TX'ing without issue
|
||||
#define VS_STATE_SHOULD_SHUTDOWN 0x000002u // Application, stack driver, or stack marked this VirtualSocket for death
|
||||
#define VS_STATE_SHUTDOWN 0x000004u // VirtualSocket and underlying protocol control structures will not RX/TX
|
||||
#define VS_STATE_CLOSED 0x000008u // VirtualSocket and underlying protocol control structures are closed
|
||||
#define VS_STATE_UNHANDLED_CONNECTED 0x000010u // stack callback has received a connection but we haven't dealt with it
|
||||
#define VS_STATE_CONNECTED 0x000020u // stack driver has akwnowledged new connection
|
||||
#define VS_STATE_LISTENING 0x000040u // virtual socket is listening for incoming connections
|
||||
class VirtualTap;
|
||||
|
||||
#define VS_OPT_TCP_NODELAY 0x000000u // Nagle's algorithm
|
||||
#define VS_OPT_SO_LINGER 0x000001u // VirtualSocket waits for data transmission before closure
|
||||
/*
|
||||
#define VS_RESERVED 0x000002u //
|
||||
#define VS_RESERVED 0x000004u //
|
||||
#define VS_RESERVED 0x000008u //
|
||||
#define VS_RESERVED 0x000010u //
|
||||
#define VS_RESERVED 0x000020u //
|
||||
#define VS_RESERVED 0x000040u //
|
||||
*/
|
||||
#define VS_OPT_FD_NONBLOCKING 0x000080u // Whether the VirtualSocket exhibits non-blocking behaviour
|
||||
/*
|
||||
#define VS_RESERVED 0x000100u //
|
||||
#define VS_RESERVED 0x000200u //
|
||||
#define VS_RESERVED 0x000400u //
|
||||
#define VS_RESERVED 0x000800u //
|
||||
#define VS_RESERVED 0x001000u //
|
||||
#define VS_RESERVED 0x002000u //
|
||||
#define VS_RESERVED 0x004000u //
|
||||
#define VS_RESERVED 0x008000u //
|
||||
#define VS_RESERVED 0x010000u //
|
||||
#define VS_RESERVED 0x020000u //
|
||||
#define VS_RESERVED 0x040000u //
|
||||
#define VS_RESERVED 0x080000u //
|
||||
#define VS_RESERVED 0x100000u //
|
||||
#define VS_RESERVED 0x200000u //
|
||||
#define VS_RESERVED 0x400000u //
|
||||
#define VS_RESERVED 0x800000u //
|
||||
*/
|
||||
|
||||
#define vs_is_nonblocking(vs) (((vs)->optflags & VS_OPT_FD_NONBLOCKING) != 0)
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
class VirtualTap;
|
||||
|
||||
class VirtualSocket
|
||||
{
|
||||
private:
|
||||
int _state = VS_STATE_INACTIVE;
|
||||
public:
|
||||
|
||||
/**
|
||||
* Sets the VirtualSocket's state value
|
||||
*/
|
||||
void apply_state(int state) {
|
||||
_state &= state;
|
||||
}
|
||||
/**
|
||||
* Sets the VirtualSocket's state value
|
||||
*/
|
||||
void set_state(int state) {
|
||||
_state = state;
|
||||
}
|
||||
/**
|
||||
* Gets the VirtualSocket's state value
|
||||
*/
|
||||
int get_state() {
|
||||
return _state;
|
||||
}
|
||||
|
||||
VirtualSocket() {
|
||||
// Only available in experimental branch
|
||||
}
|
||||
~VirtualSocket() {
|
||||
}
|
||||
};
|
||||
void VirtualSocket::apply_state(int state) {
|
||||
// states may be set by application or by stack callbacks, thus this must be guarded
|
||||
_op_m.lock();
|
||||
_state &= state;
|
||||
_op_m.unlock();
|
||||
}
|
||||
#endif
|
||||
|
||||
void VirtualSocket::set_state(int state) {
|
||||
_op_m.lock();
|
||||
_state = state;
|
||||
_op_m.unlock();
|
||||
}
|
||||
|
||||
int VirtualSocket::get_state() {
|
||||
return _state;
|
||||
}
|
||||
|
||||
VirtualSocket::VirtualSocket() {
|
||||
DEBUG_EXTRA("this=%p",this);
|
||||
memset(&local_addr, 0, sizeof(sockaddr_storage));
|
||||
memset(&peer_addr, 0, sizeof(sockaddr_storage));
|
||||
|
||||
// ringbuffer used for incoming and outgoing traffic between app, stack, stack drivers, and ZT
|
||||
TXbuf = new RingBuffer(ZT_TCP_TX_BUF_SZ);
|
||||
RXbuf = new RingBuffer(ZT_TCP_RX_BUF_SZ);
|
||||
|
||||
// socketpair, I/O channel between app and stack drivers
|
||||
ZT_PHY_SOCKFD_TYPE fdpair[2];
|
||||
if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fdpair) < 0) {
|
||||
if (errno < 0) {
|
||||
DEBUG_ERROR("unable to create socketpair, errno=%d", errno);
|
||||
return;
|
||||
}
|
||||
}
|
||||
sdk_fd = fdpair[0];
|
||||
app_fd = fdpair[1];
|
||||
|
||||
// set to non-blocking since these are used as the primary I/O channel
|
||||
if (fcntl(sdk_fd, F_SETFL, O_NONBLOCK) < 0) {
|
||||
DEBUG_ERROR("error while setting virtual socket to NONBLOCKING. exiting", errno);
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
VirtualSocket::~VirtualSocket() {
|
||||
DEBUG_EXTRA("this=%p",this);
|
||||
close(app_fd);
|
||||
close(sdk_fd);
|
||||
delete TXbuf;
|
||||
delete RXbuf;
|
||||
TXbuf = RXbuf = NULL;
|
||||
//picosock->priv = NULL;
|
||||
pcb = NULL;
|
||||
}
|
||||
|
||||
#endif // ZT_VIRTUAL_SOCKET
|
||||
|
||||
1250
src/VirtualSocketLayer.cpp
Normal file
1250
src/VirtualSocketLayer.cpp
Normal file
File diff suppressed because it is too large
Load Diff
1061
src/VirtualTap.cpp
1061
src/VirtualTap.cpp
File diff suppressed because it is too large
Load Diff
@@ -1,311 +0,0 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* Virtual Ethernet tap device
|
||||
*/
|
||||
|
||||
#ifndef ZT_VIRTUALTAP_HPP
|
||||
#define ZT_VIRTUALTAP_HPP
|
||||
|
||||
#include <ctime>
|
||||
|
||||
#include "Mutex.hpp"
|
||||
#include "MulticastGroup.hpp"
|
||||
#include "InetAddress.hpp"
|
||||
#include "Thread.hpp"
|
||||
#include "Phy.hpp"
|
||||
|
||||
#if defined(__MINGW32__) || defined(__MINGW64__)
|
||||
#include <WinSock2.h>
|
||||
#include <Windows.h>
|
||||
#include <IPHlpApi.h>
|
||||
#include <Ifdef.h>
|
||||
#endif
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
/**
|
||||
* emulates an Ethernet tap device
|
||||
*/
|
||||
class VirtualTap
|
||||
{
|
||||
friend class Phy<VirtualTap *>;
|
||||
|
||||
public:
|
||||
VirtualTap(
|
||||
const char *homePath,
|
||||
const MAC &mac,
|
||||
unsigned int mtu,
|
||||
unsigned int metric,
|
||||
uint64_t nwid,
|
||||
const char *friendlyName,
|
||||
void (*handler)(void *, void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
|
||||
void *arg);
|
||||
|
||||
~VirtualTap();
|
||||
|
||||
void setEnabled(bool en);
|
||||
bool enabled() const;
|
||||
|
||||
/**
|
||||
* Registers a device with the given address
|
||||
*/
|
||||
bool registerIpWithStack(const InetAddress &ip);
|
||||
|
||||
/**
|
||||
* Adds an address to the userspace stack interface associated with this VirtualTap
|
||||
* - Starts VirtualTap main thread ONLY if successful
|
||||
*/
|
||||
bool addIp(const InetAddress &ip);
|
||||
|
||||
/**
|
||||
* Removes an address from the userspace stack interface associated with this VirtualTap
|
||||
*/
|
||||
bool removeIp(const InetAddress &ip);
|
||||
|
||||
/**
|
||||
* Presents data to the userspace stack
|
||||
*/
|
||||
void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,
|
||||
unsigned int len);
|
||||
|
||||
/**
|
||||
* Get VirtualTap device name (e.g. 'libzt4-17d72843bc2c5760')
|
||||
*/
|
||||
std::string deviceName() const;
|
||||
|
||||
/**
|
||||
* Get Node ID (ZT address)
|
||||
*/
|
||||
std::string nodeId() const;
|
||||
|
||||
/**
|
||||
* Set friendly name
|
||||
*/
|
||||
void setFriendlyName(const char *friendlyName);
|
||||
|
||||
/**
|
||||
* Scan multicast groups
|
||||
*/
|
||||
void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
|
||||
|
||||
/**
|
||||
* Set MTU
|
||||
*/
|
||||
void setMtu(unsigned int mtu);
|
||||
|
||||
/**
|
||||
* Calls main network stack loops
|
||||
*/
|
||||
void threadMain()
|
||||
throw();
|
||||
|
||||
#if defined(__MINGW32__)
|
||||
/* The following is merely to make ZeroTier's OneService happy while building on Windows.
|
||||
we won't use these in libzt */
|
||||
NET_LUID _deviceLuid;
|
||||
std::string _deviceInstanceId;
|
||||
|
||||
/**
|
||||
* Returns whether the VirtualTap interface has been initialized
|
||||
*/
|
||||
bool isInitialized() const { return _initialized; };
|
||||
|
||||
inline const NET_LUID &luid() const { return _deviceLuid; }
|
||||
inline const std::string &instanceId() const { return _deviceInstanceId; }
|
||||
#endif
|
||||
/**
|
||||
* For moving data onto the ZeroTier virtual wire
|
||||
*/
|
||||
void (*_handler)(void *, void *, uint64_t, const MAC &, const MAC &, unsigned int, unsigned int,
|
||||
const void *, unsigned int);
|
||||
|
||||
/**
|
||||
* Signals us to close the TcpVirtualSocket associated with this PhySocket
|
||||
*/
|
||||
void phyOnUnixClose(PhySocket *sock, void **uptr);
|
||||
|
||||
/**
|
||||
* Notifies us that there is data to be read from an application's socket
|
||||
*/
|
||||
void phyOnUnixData(PhySocket *sock, void **uptr, void *data, ssize_t len);
|
||||
|
||||
/**
|
||||
* Notifies us that we can write to an application's socket
|
||||
*/
|
||||
void phyOnUnixWritable(PhySocket *sock, void **uptr, bool stack_invoked);
|
||||
|
||||
/**
|
||||
* Adds a route to the virtual tap
|
||||
*/
|
||||
bool routeAdd(const InetAddress &ip, const InetAddress &nm, const InetAddress &gw);
|
||||
|
||||
/**
|
||||
* Deletes a route from the virtual tap
|
||||
*/
|
||||
bool routeDelete(const InetAddress &ip, const InetAddress &nm);
|
||||
|
||||
/**
|
||||
* Assign a VirtualSocket to the VirtualTap
|
||||
*/
|
||||
void addVirtualSocket();
|
||||
|
||||
/**
|
||||
* Remove a VirtualSocket from the VirtualTap
|
||||
*/
|
||||
void removeVirtualSocket();
|
||||
|
||||
/****************************************************************************/
|
||||
/* DNS */
|
||||
/****************************************************************************/
|
||||
|
||||
/**
|
||||
* Registers a DNS nameserver with the network stack
|
||||
*/
|
||||
int add_DNS_Nameserver(struct sockaddr *addr);
|
||||
|
||||
/**
|
||||
* Un-registers a DNS nameserver from the network stack
|
||||
*/
|
||||
int del_DNS_Nameserver(struct sockaddr *addr);
|
||||
|
||||
/****************************************************************************/
|
||||
/* Vars */
|
||||
/****************************************************************************/
|
||||
|
||||
std::vector<std::pair<ZeroTier::InetAddress, ZeroTier::InetAddress>> routes;
|
||||
void *zt1ServiceRef = NULL;
|
||||
|
||||
char vtap_full_name[64];
|
||||
char vtap_abbr_name[16];
|
||||
|
||||
static int devno;
|
||||
size_t ifindex = 0;
|
||||
|
||||
std::vector<InetAddress> ips() const;
|
||||
std::vector<InetAddress> _ips;
|
||||
|
||||
std::string _homePath;
|
||||
void *_arg;
|
||||
volatile bool _initialized;
|
||||
volatile bool _enabled;
|
||||
volatile bool _run;
|
||||
MAC _mac;
|
||||
unsigned int _mtu;
|
||||
uint64_t _nwid;
|
||||
PhySocket *_unixListenSocket;
|
||||
Phy<VirtualTap *> _phy;
|
||||
|
||||
//std::vector<VirtualSocket*> _VirtualSockets;
|
||||
|
||||
Thread _thread;
|
||||
std::string _dev; // path to Unix domain socket
|
||||
|
||||
std::vector<MulticastGroup> _multicastGroups;
|
||||
Mutex _multicastGroups_m;
|
||||
Mutex _ips_m, _tcpconns_m, _rx_buf_m, _close_m;
|
||||
|
||||
/*
|
||||
* Timestamp of last run of housekeeping
|
||||
* SEE: ZT_HOUSEKEEPING_INTERVAL in libzt.h
|
||||
*/
|
||||
std::time_t last_housekeeping_ts = 0;
|
||||
|
||||
/****************************************************************************/
|
||||
/* In these, we will call the stack's corresponding functions, this is */
|
||||
/* where one would put logic to select between different stacks */
|
||||
/****************************************************************************/
|
||||
|
||||
/**
|
||||
* Connect to a remote host via the userspace stack interface associated with this VirtualTap
|
||||
*/
|
||||
int Connect( const struct sockaddr *addr, socklen_t addrlen);
|
||||
|
||||
/**
|
||||
* Bind to the userspace stack interface associated with this VirtualTap
|
||||
*/
|
||||
int Bind(const struct sockaddr *addr, socklen_t addrlen);
|
||||
|
||||
/**
|
||||
* Listen for a VirtualSocket
|
||||
*/
|
||||
int Listen(int backlog);
|
||||
|
||||
/**
|
||||
* Accepts an incoming VirtualSocket
|
||||
*/
|
||||
void Accept();
|
||||
|
||||
/**
|
||||
* Move data from RX buffer to application's "socket"
|
||||
*/
|
||||
int Read(PhySocket *sock,void **uptr,bool stack_invoked);
|
||||
|
||||
/**
|
||||
* Move data from application's "socket" into network stack
|
||||
*/
|
||||
int Write(void *data, ssize_t len);
|
||||
|
||||
/**
|
||||
* Send data to specified host
|
||||
*/
|
||||
int SendTo(const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen);
|
||||
|
||||
/**
|
||||
* Closes a VirtualSocket
|
||||
*/
|
||||
int Close();
|
||||
|
||||
/**
|
||||
* Shuts down some aspect of a VirtualSocket
|
||||
*/
|
||||
int Shutdown(int how);
|
||||
|
||||
/**
|
||||
* Disposes of previously-closed VirtualSockets
|
||||
*/
|
||||
void Housekeeping();
|
||||
|
||||
/****************************************************************************/
|
||||
/* Not used in this implementation */
|
||||
/****************************************************************************/
|
||||
|
||||
void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *local_address,
|
||||
const struct sockaddr *from,void *data,unsigned long len);
|
||||
void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success);
|
||||
void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,
|
||||
const struct sockaddr *from);
|
||||
void phyOnTcpClose(PhySocket *sock,void **uptr);
|
||||
void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len);
|
||||
void phyOnTcpWritable(PhySocket *sock,void **uptr);
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
||||
#endif // _H
|
||||
@@ -31,29 +31,27 @@
|
||||
*/
|
||||
|
||||
#include "ZT1Service.h"
|
||||
|
||||
#include "Debug.hpp"
|
||||
#include "libztDebug.h"
|
||||
#include "SysUtils.h"
|
||||
|
||||
#include "Phy.hpp"
|
||||
#include "OneService.hpp"
|
||||
#include "Utilities.h"
|
||||
#include "InetAddress.hpp"
|
||||
#include "OSUtils.hpp"
|
||||
|
||||
std::vector<void*> vtaps;
|
||||
ZeroTier::Mutex _vtaps_lock;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
namespace ZeroTier {
|
||||
std::vector<void*> vtaps;
|
||||
static ZeroTier::OneService *zt1Service;
|
||||
|
||||
static ZeroTier::OneService *zt1Service;
|
||||
std::string homeDir; // Platform-specific dir we *must* use internally
|
||||
std::string netDir; // Where network .conf files are to be written
|
||||
|
||||
std::string homeDir; // Platform-specific dir we *must* use internally
|
||||
std::string netDir; // Where network .conf files are to be written
|
||||
|
||||
ZeroTier::Mutex _vtaps_lock;
|
||||
ZeroTier::Mutex _multiplexer_lock;
|
||||
}
|
||||
ZeroTier::Mutex _multiplexer_lock;
|
||||
|
||||
#if defined(__MINGW32__) || defined(__MINGW64__)
|
||||
WSADATA wsaData;
|
||||
@@ -63,132 +61,152 @@ WSADATA wsaData;
|
||||
/* ZeroTier Core helper functions for libzt - DON'T CALL THESE DIRECTLY */
|
||||
/****************************************************************************/
|
||||
|
||||
std::vector<ZT_VirtualNetworkRoute> *zts_get_network_routes(char *nwid)
|
||||
std::vector<ZT_VirtualNetworkRoute> *zts_get_network_routes(const uint64_t nwid)
|
||||
{
|
||||
uint64_t nwid_int = strtoull(nwid, NULL, 16);
|
||||
return ZeroTier::zt1Service->getRoutes(nwid_int);
|
||||
return zt1Service->getRoutes(nwid);
|
||||
}
|
||||
|
||||
ZeroTier::VirtualTap *getTapByNWID(uint64_t nwid)
|
||||
VirtualTap *getTapByNWID(uint64_t nwid)
|
||||
{
|
||||
ZeroTier::_vtaps_lock.lock();
|
||||
ZeroTier::VirtualTap *s, *tap = nullptr;
|
||||
for (size_t i=0; i<ZeroTier::vtaps.size(); i++) {
|
||||
s = (ZeroTier::VirtualTap*)ZeroTier::vtaps[i];
|
||||
_vtaps_lock.lock();
|
||||
VirtualTap *s, *tap = nullptr;
|
||||
for (size_t i=0; i<vtaps.size(); i++) {
|
||||
s = (VirtualTap*)vtaps[i];
|
||||
if (s->_nwid == nwid) { tap = s; }
|
||||
}
|
||||
ZeroTier::_vtaps_lock.unlock();
|
||||
_vtaps_lock.unlock();
|
||||
return tap;
|
||||
}
|
||||
|
||||
ZeroTier::VirtualTap *getTapByAddr(ZeroTier::InetAddress *addr)
|
||||
VirtualTap *getTapByAddr(ZeroTier::InetAddress *addr)
|
||||
{
|
||||
ZeroTier::_vtaps_lock.lock();
|
||||
ZeroTier::VirtualTap *s, *tap = nullptr;
|
||||
_vtaps_lock.lock();
|
||||
VirtualTap *s, *tap = nullptr;
|
||||
//char ipbuf[64], ipbuf2[64], ipbuf3[64];
|
||||
for (size_t i=0; i<ZeroTier::vtaps.size(); i++) {
|
||||
s = (ZeroTier::VirtualTap*)ZeroTier::vtaps[i];
|
||||
for (size_t i=0; i<vtaps.size(); i++) {
|
||||
s = (VirtualTap*)vtaps[i];
|
||||
// check address schemes
|
||||
for (ssize_t j=0; j<s->_ips.size(); j++) {
|
||||
if ((s->_ips[j].isV4() && addr->isV4()) || (s->_ips[j].isV6() && addr->isV6())) {
|
||||
//DEBUG_EXTRA("looking at tap %s, <addr=%s> --- for <%s>", s->_dev.c_str(), s->_ips[j].toString(ipbuf), addr->toIpString(ipbuf2));
|
||||
/* DEBUG_EXTRA("looking at tap %s, <addr=%s> --- for <%s>", s->_dev.c_str(),
|
||||
s->_ips[j].toString(ipbuf), addr->toIpString(ipbuf2)); */
|
||||
if (s->_ips[j].isEqualPrefix(addr)
|
||||
|| s->_ips[j].ipsEqual(addr)
|
||||
|| s->_ips[j].containsAddress(addr)
|
||||
|| (addr->isV6() && ipv6_in_subnet(&s->_ips[j], addr))
|
||||
|| (addr->isV6() && _ipv6_in_subnet(&s->_ips[j], addr))
|
||||
)
|
||||
{
|
||||
//DEBUG_EXTRA("selected tap %s, <addr=%s>", s->_dev.c_str(), s->_ips[j].toString(ipbuf));
|
||||
ZeroTier::_vtaps_lock.unlock();
|
||||
_vtaps_lock.unlock();
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
// check managed routes
|
||||
if (tap == NULL) {
|
||||
std::vector<ZT_VirtualNetworkRoute> *managed_routes = ZeroTier::zt1Service->getRoutes(s->_nwid);
|
||||
std::vector<ZT_VirtualNetworkRoute> *managed_routes = zt1Service->getRoutes(s->_nwid);
|
||||
ZeroTier::InetAddress target, nm, via;
|
||||
for (size_t i=0; i<managed_routes->size(); i++) {
|
||||
target = managed_routes->at(i).target;
|
||||
nm = target.netmask();
|
||||
via = managed_routes->at(i).via;
|
||||
if (target.containsAddress(addr)) {
|
||||
//DEBUG_EXTRA("chose tap with route <target=%s, nm=%s, via=%s>", target.toString(ipbuf), nm.toString(ipbuf2), via.toString(ipbuf3));
|
||||
ZeroTier::_vtaps_lock.unlock();
|
||||
/* DEBUG_EXTRA("chose tap with route <target=%s, nm=%s, via=%s>", target.toString(ipbuf),
|
||||
nm.toString(ipbuf2), via.toString(ipbuf3)); */
|
||||
_vtaps_lock.unlock();
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ZeroTier::_vtaps_lock.unlock();
|
||||
_vtaps_lock.unlock();
|
||||
return tap;
|
||||
}
|
||||
|
||||
ZeroTier::VirtualTap *getTapByName(char *ifname)
|
||||
VirtualTap *getTapByName(char *ifname)
|
||||
{
|
||||
ZeroTier::_vtaps_lock.lock();
|
||||
ZeroTier::VirtualTap *s, *tap = nullptr;
|
||||
for (size_t i=0; i<ZeroTier::vtaps.size(); i++) {
|
||||
s = (ZeroTier::VirtualTap*)ZeroTier::vtaps[i];
|
||||
_vtaps_lock.lock();
|
||||
VirtualTap *s, *tap = nullptr;
|
||||
for (size_t i=0; i<vtaps.size(); i++) {
|
||||
s = (VirtualTap*)vtaps[i];
|
||||
if (strcmp(s->_dev.c_str(), ifname) == false) {
|
||||
tap = s;
|
||||
}
|
||||
}
|
||||
ZeroTier::_vtaps_lock.unlock();
|
||||
_vtaps_lock.unlock();
|
||||
return tap;
|
||||
}
|
||||
|
||||
ZeroTier::VirtualTap *getTapByIndex(size_t index)
|
||||
VirtualTap *getTapByIndex(size_t index)
|
||||
{
|
||||
ZeroTier::_vtaps_lock.lock();
|
||||
ZeroTier::VirtualTap *s, *tap = nullptr;
|
||||
for (size_t i=0; i<ZeroTier::vtaps.size(); i++) {
|
||||
s = (ZeroTier::VirtualTap*)ZeroTier::vtaps[i];
|
||||
_vtaps_lock.lock();
|
||||
VirtualTap *s, *tap = nullptr;
|
||||
for (size_t i=0; i<vtaps.size(); i++) {
|
||||
s = (VirtualTap*)vtaps[i];
|
||||
if (s->ifindex == index) {
|
||||
tap = s;
|
||||
}
|
||||
}
|
||||
ZeroTier::_vtaps_lock.unlock();
|
||||
_vtaps_lock.unlock();
|
||||
return tap;
|
||||
}
|
||||
|
||||
ZeroTier::VirtualTap *getAnyTap()
|
||||
VirtualTap *getAnyTap()
|
||||
{
|
||||
ZeroTier::_vtaps_lock.lock();
|
||||
ZeroTier::VirtualTap *vtap = NULL;
|
||||
if (ZeroTier::vtaps.size()) {
|
||||
vtap = (ZeroTier::VirtualTap *)ZeroTier::vtaps[0];
|
||||
_vtaps_lock.lock();
|
||||
VirtualTap *vtap = NULL;
|
||||
if (vtaps.size()) {
|
||||
vtap = (VirtualTap *)vtaps[0];
|
||||
}
|
||||
ZeroTier::_vtaps_lock.unlock();
|
||||
_vtaps_lock.unlock();
|
||||
return vtap;
|
||||
}
|
||||
|
||||
int zts_get_id_from_file(const char *filepath, char *devID)
|
||||
/*
|
||||
|
||||
else // Service isn't online, try to read ID from file
|
||||
{
|
||||
std::string fname("identity.public");
|
||||
std::string fpath(homeDir);
|
||||
if (ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) {
|
||||
std::string oldid;
|
||||
ZeroTier::OSUtils::readFile((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),oldid);
|
||||
memcpy(devID, oldid.c_str(), ZTO_ID_LEN); // first 10 bytes of file
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
int zts_get_id_from_file(const char *filepath, uint64_t *nodeId)
|
||||
{
|
||||
/*
|
||||
DEBUG_EXTRA();
|
||||
std::string fname("identity.public");
|
||||
std::string fpath(filepath);
|
||||
if (ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) {
|
||||
std::string oldid;
|
||||
ZeroTier::OSUtils::readFile((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),oldid);
|
||||
memcpy(devID, oldid.c_str(), 10); // first 10 bytes of file
|
||||
uint64_t value = Utils::hexStrToU64(oldid);
|
||||
memcpy(nodeId, value, sizeof(uint64_t)); // first 10 bytes of file
|
||||
// TOmorrow
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Starts a ZeroTier service in the background
|
||||
void *zts_start_service(void *thread_id)
|
||||
{
|
||||
DEBUG_INFO("zto-thread, path=%s", ZeroTier::homeDir.c_str());
|
||||
DEBUG_INFO("zto-thread, path=%s", homeDir.c_str());
|
||||
// Where network .conf files will be stored
|
||||
ZeroTier::netDir = ZeroTier::homeDir + "/networks.d";
|
||||
ZeroTier::zt1Service = (ZeroTier::OneService *)0;
|
||||
netDir = homeDir + "/networks.d";
|
||||
zt1Service = (ZeroTier::OneService *)0;
|
||||
// Construct path for network config and supporting service files
|
||||
if (ZeroTier::homeDir.length()) {
|
||||
std::vector<std::string> hpsp(ZeroTier::OSUtils::split(ZeroTier::homeDir.c_str(), ZT_PATH_SEPARATOR_S,"",""));
|
||||
if (homeDir.length()) {
|
||||
std::vector<std::string> hpsp(ZeroTier::OSUtils::split(homeDir.c_str(), ZT_PATH_SEPARATOR_S,"",""));
|
||||
std::string ptmp;
|
||||
if (ZeroTier::homeDir[0] == ZT_PATH_SEPARATOR) {
|
||||
if (homeDir[0] == ZT_PATH_SEPARATOR) {
|
||||
ptmp.push_back(ZT_PATH_SEPARATOR);
|
||||
}
|
||||
for (std::vector<std::string>::iterator pi(hpsp.begin());pi!=hpsp.end();++pi) {
|
||||
@@ -199,7 +217,6 @@ void *zts_start_service(void *thread_id)
|
||||
if ((*pi != ".")&&(*pi != "..")) {
|
||||
if (ZeroTier::OSUtils::mkdir(ptmp) == false) {
|
||||
DEBUG_ERROR("home path does not exist, and could not create");
|
||||
handle_general_failure();
|
||||
perror("error\n");
|
||||
}
|
||||
}
|
||||
@@ -207,7 +224,6 @@ void *zts_start_service(void *thread_id)
|
||||
}
|
||||
else {
|
||||
DEBUG_ERROR("homeDir is empty, could not construct path");
|
||||
handle_general_failure();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -217,27 +233,27 @@ void *zts_start_service(void *thread_id)
|
||||
// TODO: Better port random range selection
|
||||
int servicePort = 9000 + (randp % 1000);
|
||||
for (;;) {
|
||||
ZeroTier::zt1Service = ZeroTier::OneService::newInstance(ZeroTier::homeDir.c_str(),servicePort);
|
||||
switch(ZeroTier::zt1Service->run()) {
|
||||
zt1Service = ZeroTier::OneService::newInstance(homeDir.c_str(),servicePort);
|
||||
switch(zt1Service->run()) {
|
||||
case ZeroTier::OneService::ONE_STILL_RUNNING:
|
||||
case ZeroTier::OneService::ONE_NORMAL_TERMINATION:
|
||||
break;
|
||||
case ZeroTier::OneService::ONE_UNRECOVERABLE_ERROR:
|
||||
DEBUG_ERROR("ZTO service port = %d", servicePort);
|
||||
DEBUG_ERROR("fatal error: %s",ZeroTier::zt1Service->fatalErrorMessage().c_str());
|
||||
DEBUG_ERROR("fatal error: %s",zt1Service->fatalErrorMessage().c_str());
|
||||
break;
|
||||
case ZeroTier::OneService::ONE_IDENTITY_COLLISION: {
|
||||
delete ZeroTier::zt1Service;
|
||||
ZeroTier::zt1Service = (ZeroTier::OneService *)0;
|
||||
delete zt1Service;
|
||||
zt1Service = (ZeroTier::OneService *)0;
|
||||
std::string oldid;
|
||||
ZeroTier::OSUtils::readFile((ZeroTier::homeDir + ZT_PATH_SEPARATOR_S
|
||||
ZeroTier::OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S
|
||||
+ "identity.secret").c_str(),oldid);
|
||||
if (oldid.length()) {
|
||||
ZeroTier::OSUtils::writeFile((ZeroTier::homeDir + ZT_PATH_SEPARATOR_S
|
||||
ZeroTier::OSUtils::writeFile((homeDir + ZT_PATH_SEPARATOR_S
|
||||
+ "identity.secret.saved_after_collision").c_str(),oldid);
|
||||
ZeroTier::OSUtils::rm((ZeroTier::homeDir + ZT_PATH_SEPARATOR_S
|
||||
ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S
|
||||
+ "identity.secret").c_str());
|
||||
ZeroTier::OSUtils::rm((ZeroTier::homeDir + ZT_PATH_SEPARATOR_S
|
||||
ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S
|
||||
+ "identity.public").c_str());
|
||||
}
|
||||
}
|
||||
@@ -245,179 +261,89 @@ void *zts_start_service(void *thread_id)
|
||||
}
|
||||
break; // terminate loop -- normally we don't keep restarting
|
||||
}
|
||||
delete ZeroTier::zt1Service;
|
||||
ZeroTier::zt1Service = (ZeroTier::OneService *)0;
|
||||
delete zt1Service;
|
||||
zt1Service = (ZeroTier::OneService *)0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void disableTaps()
|
||||
void zts_get_address(const uint64_t nwid, struct sockaddr_storage *addr, const size_t addrlen)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
ZeroTier::_vtaps_lock.lock();
|
||||
for (size_t i=0; i<ZeroTier::vtaps.size(); i++) {
|
||||
DEBUG_EXTRA("vt=%p", ZeroTier::vtaps[i]);
|
||||
((ZeroTier::VirtualTap*)ZeroTier::vtaps[i])->_enabled = false;
|
||||
if(!zt1Service) {
|
||||
return;
|
||||
}
|
||||
ZeroTier::_vtaps_lock.unlock();
|
||||
}
|
||||
|
||||
void zts_get_ipv4_address(const char *nwid, char *addrstr, const size_t addrlen)
|
||||
{
|
||||
// DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
uint64_t nwid_int = strtoull(nwid, NULL, 16);
|
||||
ZeroTier::VirtualTap *tap = getTapByNWID(nwid_int);
|
||||
if (tap && tap->_ips.size()) {
|
||||
for (size_t i=0; i<tap->_ips.size(); i++) {
|
||||
if (tap->_ips[i].isV4()) {
|
||||
char ipbuf[INET_ADDRSTRLEN];
|
||||
std::string addr = tap->_ips[i].toString(ipbuf);
|
||||
int len = addrlen < addr.length() ? addrlen : addr.length();
|
||||
memset(addrstr, 0, len);
|
||||
memcpy(addrstr, addr.c_str(), len);
|
||||
return;
|
||||
}
|
||||
VirtualTap *tap = getTapByNWID(nwid);
|
||||
if (tap && tap->_ips.size()) {
|
||||
for (size_t i=0; i<tap->_ips.size(); i++) {
|
||||
if (tap->_ips[i].isV4()) {
|
||||
memcpy(addr, &(tap->_ips[i]), addrlen);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
memcpy(addrstr, "\0", 1);
|
||||
}
|
||||
|
||||
void zts_get_ipv6_address(const char *nwid, char *addrstr, size_t addrlen)
|
||||
int zts_has_address(const uint64_t nwid)
|
||||
{
|
||||
// DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
uint64_t nwid_int = strtoull(nwid, NULL, 16);
|
||||
ZeroTier::VirtualTap *tap = getTapByNWID(nwid_int);
|
||||
if (tap && tap->_ips.size()) {
|
||||
for (size_t i=0; i<tap->_ips.size(); i++) {
|
||||
if (tap->_ips[i].isV6()) {
|
||||
char ipbuf[INET6_ADDRSTRLEN];
|
||||
std::string addr = tap->_ips[i].toString(ipbuf);
|
||||
int len = addrlen < addr.length() ? addrlen : addr.length();
|
||||
memset(addrstr, 0, len);
|
||||
memcpy(addrstr, addr.c_str(), len);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
memcpy(addrstr, "\0", 1);
|
||||
struct sockaddr_storage ss;
|
||||
memset(&ss, 0, sizeof(ss));
|
||||
zts_get_address(nwid, &ss, sizeof(ss));
|
||||
return ss.ss_family == AF_INET || ss.ss_family == AF_INET6;
|
||||
}
|
||||
|
||||
int zts_has_ipv4_address(const char *nwid)
|
||||
void zts_get_6plane_addr(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId)
|
||||
{
|
||||
// DEBUG_EXTRA();
|
||||
char ipv4_addr[INET_ADDRSTRLEN];
|
||||
memset(ipv4_addr, 0, INET_ADDRSTRLEN);
|
||||
zts_get_ipv4_address(nwid, ipv4_addr, INET_ADDRSTRLEN);
|
||||
return strcmp(ipv4_addr, "\0");
|
||||
ZeroTier::InetAddress _6planeAddr = ZeroTier::InetAddress::makeIpv66plane(nwid,nodeId);
|
||||
memcpy(addr, _6planeAddr.rawIpData(), sizeof(struct sockaddr_storage));
|
||||
}
|
||||
|
||||
int zts_has_ipv6_address(const char *nwid)
|
||||
void zts_get_rfc4193_addr(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId)
|
||||
{
|
||||
// DEBUG_EXTRA();
|
||||
char ipv6_addr[INET6_ADDRSTRLEN];
|
||||
memset(ipv6_addr, 0, INET6_ADDRSTRLEN);
|
||||
zts_get_ipv6_address(nwid, ipv6_addr, INET6_ADDRSTRLEN);
|
||||
return strcmp(ipv6_addr, "\0");
|
||||
ZeroTier::InetAddress _rfc4193Addr = ZeroTier::InetAddress::makeIpv6rfc4193(nwid,nodeId);
|
||||
memcpy(addr, _rfc4193Addr.rawIpData(), sizeof(struct sockaddr_storage));
|
||||
}
|
||||
|
||||
int zts_has_address(const char *nwid)
|
||||
{
|
||||
// DEBUG_EXTRA();
|
||||
return zts_has_ipv4_address(nwid) || zts_has_ipv6_address(nwid);
|
||||
}
|
||||
|
||||
void zts_get_6plane_addr(char *addr, const char *nwid, const char *devID)
|
||||
void zts_join(const uint64_t nwid)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
ZeroTier::InetAddress _6planeAddr = ZeroTier::InetAddress::makeIpv66plane(
|
||||
ZeroTier::Utils::hexStrToU64(nwid),ZeroTier::Utils::hexStrToU64(devID));
|
||||
char ipbuf[INET6_ADDRSTRLEN];
|
||||
memcpy(addr, _6planeAddr.toIpString(ipbuf), 40);
|
||||
}
|
||||
|
||||
void zts_get_rfc4193_addr(char *addr, const char *nwid, const char *devID)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
ZeroTier::InetAddress _6planeAddr = ZeroTier::InetAddress::makeIpv6rfc4193(
|
||||
ZeroTier::Utils::hexStrToU64(nwid),ZeroTier::Utils::hexStrToU64(devID));
|
||||
char ipbuf[INET6_ADDRSTRLEN];
|
||||
memcpy(addr, _6planeAddr.toIpString(ipbuf), 40);
|
||||
}
|
||||
|
||||
void zts_join(const char * nwid)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
std::string confFile = ZeroTier::zt1Service->givenHomePath() + "/networks.d/" + nwid + ".conf";
|
||||
if (ZeroTier::OSUtils::mkdir(ZeroTier::netDir) == false) {
|
||||
DEBUG_ERROR("unable to create: %s", ZeroTier::netDir.c_str());
|
||||
handle_general_failure();
|
||||
if (zt1Service) {
|
||||
std::string confFile = zt1Service->givenHomePath() + "/networks.d/" + std::to_string(nwid) + ".conf";
|
||||
if (ZeroTier::OSUtils::mkdir(netDir) == false) {
|
||||
DEBUG_ERROR("unable to create: %s", netDir.c_str());
|
||||
}
|
||||
if (ZeroTier::OSUtils::writeFile(confFile.c_str(), "") == false) {
|
||||
DEBUG_ERROR("unable to write network conf file: %s", confFile.c_str());
|
||||
handle_general_failure();
|
||||
}
|
||||
ZeroTier::zt1Service->join(nwid);
|
||||
zt1Service->join(nwid);
|
||||
}
|
||||
// provide ZTO service reference to virtual taps
|
||||
// TODO: This might prove to be unreliable, but it works for now
|
||||
for (size_t i=0;i<ZeroTier::vtaps.size(); i++) {
|
||||
ZeroTier::VirtualTap *s = (ZeroTier::VirtualTap*)ZeroTier::vtaps[i];
|
||||
s->zt1ServiceRef=(void*)ZeroTier::zt1Service;
|
||||
for (size_t i=0;i<vtaps.size(); i++) {
|
||||
VirtualTap *s = (VirtualTap*)vtaps[i];
|
||||
s->zt1ServiceRef=(void*)zt1Service;
|
||||
}
|
||||
}
|
||||
|
||||
void zts_join_soft(const char * filepath, const char * nwid)
|
||||
void zts_leave(const uint64_t nwid)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
std::string net_dir = std::string(filepath) + "/networks.d/";
|
||||
std::string confFile = net_dir + std::string(nwid) + ".conf";
|
||||
if (ZeroTier::OSUtils::mkdir(net_dir) == false) {
|
||||
DEBUG_ERROR("unable to create: %s", net_dir.c_str());
|
||||
handle_general_failure();
|
||||
}
|
||||
if (ZeroTier::OSUtils::fileExists(confFile.c_str(), false) == false) {
|
||||
if (ZeroTier::OSUtils::writeFile(confFile.c_str(), "") == false) {
|
||||
DEBUG_ERROR("unable to write network conf file: %s", confFile.c_str());
|
||||
handle_general_failure();
|
||||
}
|
||||
if (zt1Service) {
|
||||
zt1Service->leave(nwid);
|
||||
}
|
||||
}
|
||||
|
||||
void zts_leave(const char * nwid)
|
||||
int zts_running()
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
ZeroTier::zt1Service->leave(nwid);
|
||||
}
|
||||
}
|
||||
|
||||
void zts_leave_soft(const char * filepath, const char * nwid)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
std::string net_dir = std::string(filepath) + "/networks.d/";
|
||||
ZeroTier::OSUtils::rm((net_dir + nwid + ".conf").c_str());
|
||||
}
|
||||
|
||||
int zts_running()
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
return ZeroTier::zt1Service == NULL ? false : ZeroTier::zt1Service->isRunning();
|
||||
return zt1Service == NULL ? false : zt1Service->isRunning();
|
||||
}
|
||||
|
||||
int zts_start(const char *path, bool blocking = false)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
if (zt1Service) {
|
||||
return 0; // already initialized, ok
|
||||
}
|
||||
if (path) {
|
||||
ZeroTier::homeDir = path;
|
||||
homeDir = path;
|
||||
}
|
||||
#if defined(__MINGW32__) || defined(__MINGW64__)
|
||||
WSAStartup(MAKEWORD(2, 2), &wsaData); // initialize WinSock. Used in Phy for loopback pipe
|
||||
@@ -426,21 +352,21 @@ int zts_start(const char *path, bool blocking = false)
|
||||
int err = pthread_create(&service_thread, NULL, zts_start_service, NULL);
|
||||
if (blocking) { // block to prevent service calls before we're ready
|
||||
ZT_NodeStatus status;
|
||||
while (zts_running() == false || ZeroTier::zt1Service->getNode() == NULL) {
|
||||
while (zts_running() == false || zt1Service->getNode() == NULL) {
|
||||
nanosleep((const struct timespec[]) {{0, (ZTO_WRAPPER_CHECK_INTERVAL * 500000)}}, NULL);
|
||||
}
|
||||
while (ZeroTier::zt1Service->getNode()->address() <= 0) {
|
||||
while (zt1Service->getNode()->address() <= 0) {
|
||||
nanosleep((const struct timespec[]) {{0, (ZTO_WRAPPER_CHECK_INTERVAL * 500000)}}, NULL);
|
||||
}
|
||||
while (status.online <= 0) {
|
||||
nanosleep((const struct timespec[]) {{0, (ZTO_WRAPPER_CHECK_INTERVAL * 500000)}}, NULL);
|
||||
ZeroTier::zt1Service->getNode()->status(&status);
|
||||
zt1Service->getNode()->status(&status);
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
int zts_startjoin(const char *path, const char *nwid)
|
||||
int zts_startjoin(const char *path, const uint64_t nwid)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
int err = zts_start(path, true);
|
||||
@@ -452,7 +378,6 @@ int zts_startjoin(const char *path, const char *nwid)
|
||||
}
|
||||
catch( ... ) {
|
||||
DEBUG_ERROR("there was a problem joining the virtual network %s", nwid);
|
||||
handle_general_failure();
|
||||
}
|
||||
}
|
||||
while (zts_has_address(nwid) == false) {
|
||||
@@ -461,67 +386,53 @@ int zts_startjoin(const char *path, const char *nwid)
|
||||
return err;
|
||||
}
|
||||
|
||||
void zts_stop()
|
||||
void zts_stop()
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
ZeroTier::zt1Service->terminate();
|
||||
disableTaps();
|
||||
if (zt1Service) {
|
||||
zt1Service->terminate();
|
||||
// disableTaps();
|
||||
}
|
||||
#if defined(__MINGW32__) || defined(__MINGW64__)
|
||||
WSACleanup(); // clean up WinSock
|
||||
#endif
|
||||
}
|
||||
|
||||
void zts_get_homepath(char *homePath, size_t len)
|
||||
void zts_get_homepath(char *homePath, size_t len)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
if (ZeroTier::homeDir.length()) {
|
||||
if (homeDir.length()) {
|
||||
memset(homePath, 0, len);
|
||||
size_t buf_len = len < ZeroTier::homeDir.length() ? len : ZeroTier::homeDir.length();
|
||||
memcpy(homePath, ZeroTier::homeDir.c_str(), buf_len);
|
||||
size_t buf_len = len < homeDir.length() ? len : homeDir.length();
|
||||
memcpy(homePath, homeDir.c_str(), buf_len);
|
||||
}
|
||||
}
|
||||
|
||||
int zts_get_id(char *devID)
|
||||
uint64_t zts_get_node_id()
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
char id[ZTO_ID_LEN];
|
||||
sprintf(id, "%lx",ZeroTier::zt1Service->getNode()->address());
|
||||
memcpy(devID, id, ZTO_ID_LEN);
|
||||
return 0;
|
||||
}
|
||||
else // Service isn't online, try to read ID from file
|
||||
{
|
||||
std::string fname("identity.public");
|
||||
std::string fpath(ZeroTier::homeDir);
|
||||
if (ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) {
|
||||
std::string oldid;
|
||||
ZeroTier::OSUtils::readFile((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),oldid);
|
||||
memcpy(devID, oldid.c_str(), ZTO_ID_LEN); // first 10 bytes of file
|
||||
return 0;
|
||||
}
|
||||
if (zt1Service) {
|
||||
return zt1Service->getNode()->address();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned long zts_get_peer_count()
|
||||
unsigned long zts_get_peer_count()
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
return ZeroTier::zt1Service->getNode()->peers()->peerCount;
|
||||
if (zt1Service) {
|
||||
return zt1Service->getNode()->peers()->peerCount;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int zts_get_peer_address(char *peer, const char *devID)
|
||||
int zts_get_peer_address(char *peer, const uint64_t nodeId)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
ZT_PeerList *pl = ZeroTier::zt1Service->getNode()->peers();
|
||||
if (zt1Service) {
|
||||
ZT_PeerList *pl = zt1Service->getNode()->peers();
|
||||
// uint64_t addr;
|
||||
for (size_t i=0; i<pl->peerCount; i++) {
|
||||
// ZT_Peer *p = &(pl->peers[i]);
|
||||
@@ -539,6 +450,37 @@ void zts_allow_http_control(bool allowed)
|
||||
// TODO
|
||||
}
|
||||
|
||||
bool _ipv6_in_subnet(ZeroTier::InetAddress *subnet, ZeroTier::InetAddress *addr)
|
||||
{
|
||||
ZeroTier::InetAddress r(addr);
|
||||
ZeroTier::InetAddress b(subnet);
|
||||
const unsigned int bits = subnet->netmaskBits();
|
||||
switch(r.ss_family) {
|
||||
case AF_INET:
|
||||
reinterpret_cast<struct sockaddr_in *>(&r)->sin_addr.s_addr &= ZeroTier::Utils::hton((uint32_t)(0xffffffff << (32 - bits)));
|
||||
break;
|
||||
case AF_INET6: {
|
||||
uint64_t nm[2];
|
||||
uint64_t nm2[2];
|
||||
memcpy(nm,reinterpret_cast<struct sockaddr_in6 *>(&r)->sin6_addr.s6_addr,16);
|
||||
memcpy(nm2,reinterpret_cast<struct sockaddr_in6 *>(&b)->sin6_addr.s6_addr,16);
|
||||
|
||||
nm[0] &= ZeroTier::Utils::hton((uint64_t)((bits >= 64) ? 0xffffffffffffffffULL : (0xffffffffffffffffULL << (64 - bits))));
|
||||
nm[1] &= ZeroTier::Utils::hton((uint64_t)((bits <= 64) ? 0ULL : (0xffffffffffffffffULL << (128 - bits))));
|
||||
|
||||
nm2[0] &= ZeroTier::Utils::hton((uint64_t)((bits >= 64) ? 0xffffffffffffffffULL : (0xffffffffffffffffULL << (64 - bits))));
|
||||
nm2[1] &= ZeroTier::Utils::hton((uint64_t)((bits <= 64) ? 0ULL : (0xffffffffffffffffULL << (128 - bits))));
|
||||
|
||||
memcpy(reinterpret_cast<struct sockaddr_in6 *>(&r)->sin6_addr.s6_addr,nm,16);
|
||||
memcpy(reinterpret_cast<struct sockaddr_in6 *>(&b)->sin6_addr.s6_addr,nm2,16);
|
||||
}
|
||||
break;
|
||||
}
|
||||
char b0[64], b1[64];
|
||||
memset(b0, 0, 64);
|
||||
memset(b1, 0, 64);
|
||||
return !strcmp(r.toIpString(b0), b.toIpString(b1));
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
1068
src/libzt.cpp
1068
src/libzt.cpp
File diff suppressed because it is too large
Load Diff
333
src/libztJNI.cpp
333
src/libztJNI.cpp
@@ -28,11 +28,9 @@
|
||||
* @file
|
||||
*
|
||||
* Javs JNI wrapper for partially-POSIX-compliant socket API
|
||||
* JNI naming convention: Java_PACKAGENAME_CLASSNAME_METHODNAME
|
||||
* JNI naming convention: Java_PACKAGENAME_CLASSNAME_METHODNAME
|
||||
*/
|
||||
|
||||
#if defined(SDK_JNI)
|
||||
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include "libzt.h"
|
||||
@@ -50,14 +48,14 @@ namespace ZeroTier {
|
||||
/* ZeroTier Socket API (for JNI wrapper) */
|
||||
/****************************************************************************/
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_socket(JNIEnv *env, jobject thisObj,
|
||||
jint family, jint type, jint protocol)
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_socket(JNIEnv *env, jobject thisObj,
|
||||
jint family, jint type, jint protocol)
|
||||
{
|
||||
return zts_socket(family, type, protocol);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_connect(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jstring addrstr, jint port)
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_connect(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jstring addrstr, jint port)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t namelen = sizeof(ss);
|
||||
@@ -91,8 +89,41 @@ namespace ZeroTier {
|
||||
return zts_connect(fd, (struct sockaddr *)&ss, sizeof(in_addr));
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_bind(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jstring addrstr, jint port)
|
||||
/*
|
||||
|
||||
JNIEXPORT jobject JNICALL Java_zerotier_ZeroTier_get_address(
|
||||
JNIEnv *env, jobject thisObj, jstring nwid)
|
||||
{
|
||||
// get address
|
||||
const char *nwid_str = env->GetStringUTFChars(nwid, NULL);
|
||||
struct sockaddr_storage ss;
|
||||
zts_get_address(nwid_str, &ss, INET_ADDRSTRLEN);
|
||||
// map sockaddr_storage contents to java InetAddress
|
||||
jclass c = (*env).FindClass("java/net/InetSocketAddress");
|
||||
if (c) {
|
||||
|
||||
jobject addresses = (*env).NewObject(c, (*env).GetMethodID(c, "<init>", "()V"));
|
||||
env->CallBooleanMethod(addresses, env->GetMethodID(c, "getPort", "(Ljava/lang/Object;)Z"), _str);
|
||||
|
||||
DEBUG_INFO("port from JNI layer = %d", port);
|
||||
|
||||
fid = (*env).GetFieldID(c, "port", "I");
|
||||
(*env).SetIntField(ztaddr, fid, addr.sin_port);
|
||||
fid = (*env).GetFieldID(c,"_rawAddr", "J");
|
||||
(*env).SetLongField(ztaddr, fid,addr.sin_addr.s_addr);
|
||||
//jobject addresses = (*env).NewObject(c, (*env).GetMethodID(c, "<init>", "()V"));
|
||||
//env->CallBooleanMethod(addresses, env->GetMethodID(c, "add", "(Ljava/lang/Object;)Z"), _str);
|
||||
//return addresses;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_bind(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jstring addrstr, jint port)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t namelen = sizeof(ss);
|
||||
@@ -125,10 +156,132 @@ namespace ZeroTier {
|
||||
DEBUG_INFO("fd=%d, addr=%s, port=%d", fd, str, port);
|
||||
return zts_bind(fd, (struct sockaddr *)&ss, sizeof(in_addr));
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
jfieldID IPv4 = (*env).GetFieldID(inetClass, "IPv4", "I");
|
||||
if (IPv4) {
|
||||
ipv4 = (*env).GetIntField(inetClass, IPv4);
|
||||
}
|
||||
else {
|
||||
DEBUG_ERROR("No field IPv4");
|
||||
}
|
||||
jfieldID IPv6 = (*env).GetFieldID(inetClass, "IPv6", "I");
|
||||
if (IPv6) {
|
||||
ipv6 = (*env).GetIntField(inetClass, IPv6);
|
||||
}
|
||||
else {
|
||||
DEBUG_ERROR("No field IPv6");
|
||||
}
|
||||
ipv6 = (*env).GetIntField(inetClass, IPv6);
|
||||
*/
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_bind(JNIEnv *env, jobject thisObj, jint fd, jobject addr)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
memset(&ss, 0, sizeof(ss));
|
||||
struct sockaddr_in *in4 = (struct sockaddr_in*)&ss;
|
||||
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)&ss;
|
||||
int err = -1;
|
||||
int port = 0;
|
||||
int socket_family = 0;
|
||||
socklen_t addrlen;
|
||||
|
||||
jclass c = (*env).GetObjectClass(addr);
|
||||
if (!c) {
|
||||
return -1;
|
||||
}
|
||||
// get port
|
||||
jmethodID getPort = (*env).GetMethodID(c, "getPort", "()I");
|
||||
if (!getPort) {
|
||||
return -1;
|
||||
}
|
||||
port = (*env).CallIntMethod(addr, getPort);
|
||||
// get internal InetAddress
|
||||
jobject inetaddr;
|
||||
int family = 0;
|
||||
jmethodID getAddress = (*env).GetMethodID(c, "getAddress", "()Ljava/net/InetAddress;");
|
||||
if (!getAddress) {
|
||||
return -1;
|
||||
}
|
||||
inetaddr = (*env).CallObjectMethod(addr, getAddress);
|
||||
if(!inetaddr) {
|
||||
return -1;
|
||||
}
|
||||
jclass inetClass = (*env).GetObjectClass(inetaddr);
|
||||
if(!inetClass) {
|
||||
return -1;
|
||||
}
|
||||
// string representation of IP address
|
||||
jmethodID getHostAddress = (*env).GetMethodID(inetClass, "getHostAddress", "()Ljava/lang/String;");
|
||||
jstring addrstr = (jstring)(*env).CallObjectMethod(inetaddr, getHostAddress);
|
||||
const char *addr_str = (*env).GetStringUTFChars(addrstr, NULL);
|
||||
DEBUG_INFO("addr_str=%s", addr_str);
|
||||
for (int i=0; i<strlen(addr_str); i++) {
|
||||
if(addr_str[i]=='.') {
|
||||
DEBUG_INFO("ipv4, inet_addr");
|
||||
socket_family = AF_INET;
|
||||
in4->sin_family = AF_INET;
|
||||
in4->sin_port = htons(port);
|
||||
in4->sin_addr.s_addr = inet_addr(addr_str);
|
||||
/*
|
||||
if(!inet_pton(AF_INET, addr_str, &(in4->sin_addr))) {
|
||||
DEBUG_ERROR("error converting address %s", addr_str);
|
||||
}
|
||||
*/
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
break;
|
||||
}
|
||||
if(addr_str[i]==':') {
|
||||
DEBUG_INFO("ipv6");
|
||||
socket_family = AF_INET6;
|
||||
if(!inet_pton(AF_INET6, addr_str, &(in6->sin6_addr))) {
|
||||
DEBUG_ERROR("error converting address %s", addr_str);
|
||||
}
|
||||
addrlen = sizeof(struct sockaddr_in6);
|
||||
break;
|
||||
}
|
||||
}
|
||||
(*env).ReleaseStringUTFChars(addrstr, addr_str);
|
||||
|
||||
DEBUG_TEST("RESULT => %s : %d", inet_ntoa(in4->sin_addr), ntohs(in4->sin_port));
|
||||
|
||||
//
|
||||
/*
|
||||
jmethodID getRawAddress = (*env).GetMethodID(inetClass, "getAddress", "()[B"); // get raw IP bytes
|
||||
if (getRawAddress)
|
||||
{
|
||||
jbyte rawIPbuf = (*env).CallByteMethod(inetaddr, getRawAddress);
|
||||
if (socket_family == AF_INET)
|
||||
{
|
||||
DEBUG_INFO("copying buffer as AF_INET");
|
||||
in4->sin_family = socket_family;
|
||||
in4->sin_port = port;
|
||||
memcpy(in4->sin_addr, &rawIPbuf, sizeof(in4->sin_addr));
|
||||
}
|
||||
if (socket_family == AF_INET6)
|
||||
{
|
||||
DEBUG_INFO("copying buffer as AF_INET");
|
||||
in6->sin6_family = socket_family;
|
||||
}
|
||||
}
|
||||
else {
|
||||
DEBUG_ERROR("!getRawAddress");
|
||||
}
|
||||
*/
|
||||
|
||||
//DEBUG_INFO("port = %d", port);
|
||||
//DEBUG_INFO("inetaddr = %p", inetaddr);
|
||||
err = zts_bind(fd, (struct sockaddr*)&ss, addrlen);
|
||||
return err;
|
||||
}
|
||||
|
||||
#if defined(__linux__)
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_accept4(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jstring addrstr, jint port, jint flags)
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_accept4(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jstring addrstr, jint port, jint flags)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
char *str;
|
||||
@@ -141,8 +294,8 @@ namespace ZeroTier {
|
||||
}
|
||||
#endif
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_accept(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jstring addrstr, jint port)
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_accept(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jstring addrstr, jint port)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
// TODO: Send addr info back to Javaland
|
||||
@@ -152,14 +305,14 @@ namespace ZeroTier {
|
||||
return zts_accept(fd, (struct sockaddr *)&addr, (socklen_t *)sizeof(addr));
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_listen(JNIEnv *env, jobject thisObj,
|
||||
jint fd, int backlog)
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_listen(JNIEnv *env, jobject thisObj,
|
||||
jint fd, int backlog)
|
||||
{
|
||||
return zts_listen(fd, backlog);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_close(JNIEnv *env, jobject thisObj,
|
||||
jint fd)
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_close(JNIEnv *env, jobject thisObj,
|
||||
jint fd)
|
||||
{
|
||||
return zts_close(fd);
|
||||
}
|
||||
@@ -168,20 +321,23 @@ namespace ZeroTier {
|
||||
JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, jint flags, jobject ztaddr)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
jclass cls = (*env).GetObjectClass( ztaddr);
|
||||
jfieldID f = (*env).GetFieldID( cls, "port", "I");
|
||||
addr.sin_port = htons((*env).GetIntField( ztaddr, f));
|
||||
f = (*env).GetFieldID( cls, "_rawAddr", "J");
|
||||
addr.sin_addr.s_addr = (*env).GetLongField( ztaddr, f);
|
||||
addr.sin_family = AF_INET;
|
||||
//LOGV("zt_sendto(): fd = %d\naddr = %s\nport=%d", fd, inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
|
||||
// TODO: Optimize this
|
||||
jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
|
||||
char * bufp = (char *)malloc(sizeof(char)*len);
|
||||
memcpy(bufp, body, len);
|
||||
(*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
|
||||
// "connect" and send buffer contents
|
||||
int sent_bytes = zts_sendto(fd, body, len, flags, (struct sockaddr *)&addr, sizeof(addr));
|
||||
int sent_bytes = 0;
|
||||
jclass c = (*env).GetObjectClass( ztaddr);
|
||||
if (c) {
|
||||
jfieldID f = (*env).GetFieldID(c, "port", "I");
|
||||
addr.sin_port = htons((*env).GetIntField( ztaddr, f));
|
||||
f = (*env).GetFieldID(c, "_rawAddr", "J");
|
||||
addr.sin_addr.s_addr = (*env).GetLongField( ztaddr, f);
|
||||
addr.sin_family = AF_INET;
|
||||
//LOGV("zt_sendto(): fd = %d\naddr = %s\nport=%d", fd, inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
|
||||
// TODO: Optimize this
|
||||
jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
|
||||
char * bufp = (char *)malloc(sizeof(char)*len);
|
||||
memcpy(bufp, body, len);
|
||||
(*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
|
||||
// "connect" and send buffer contents
|
||||
sent_bytes = zts_sendto(fd, body, len, flags, (struct sockaddr *)&addr, sizeof(addr));
|
||||
}
|
||||
return sent_bytes;
|
||||
}
|
||||
|
||||
@@ -193,16 +349,19 @@ namespace ZeroTier {
|
||||
unsigned char buffer[ZT_SDK_MTU];
|
||||
int payload_offset = sizeof(int32_t) + sizeof(struct sockaddr_storage);
|
||||
int rxbytes = zts_recvfrom(fd, &buffer, len, flags, (struct sockaddr *)&addr, (socklen_t *)sizeof(struct sockaddr_storage));
|
||||
if (rxbytes > 0)
|
||||
if (rxbytes > 0) {
|
||||
memcpy(body, (jbyte*)buffer + payload_offset, rxbytes);
|
||||
}
|
||||
(*env).ReleaseByteArrayElements( buf, body, 0);
|
||||
// Update fields of Java ZTAddress object
|
||||
jfieldID fid;
|
||||
jclass cls = (*env).GetObjectClass( ztaddr);
|
||||
fid = (*env).GetFieldID( cls, "port", "I");
|
||||
(*env).SetIntField( ztaddr, fid, addr.sin_port);
|
||||
fid = (*env).GetFieldID( cls,"_rawAddr", "J");
|
||||
(*env).SetLongField( ztaddr, fid,addr.sin_addr.s_addr);
|
||||
jclass c = (*env).GetObjectClass( ztaddr);
|
||||
if (c) {
|
||||
fid = (*env).GetFieldID(c, "port", "I");
|
||||
(*env).SetIntField(ztaddr, fid, addr.sin_port);
|
||||
fid = (*env).GetFieldID(c,"_rawAddr", "J");
|
||||
(*env).SetLongField(ztaddr, fid,addr.sin_addr.s_addr);
|
||||
}
|
||||
return rxbytes;
|
||||
}
|
||||
|
||||
@@ -216,7 +375,7 @@ namespace ZeroTier {
|
||||
return written_bytes;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_write(JNIEnv *env, jobject thisObj,
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_write(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jarray buf, jint len)
|
||||
{
|
||||
jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
|
||||
@@ -227,7 +386,7 @@ namespace ZeroTier {
|
||||
return written_bytes;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_read(JNIEnv *env, jobject thisObj,
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_read(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jarray buf, jint len)
|
||||
{
|
||||
jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
|
||||
@@ -237,48 +396,52 @@ namespace ZeroTier {
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_setsockopt(
|
||||
JNIEnv *env, jobject thisObj,
|
||||
jint fd, jint level, jint optname, jint optval, jint optlen)
|
||||
JNIEnv *env, jobject thisObj,
|
||||
jint fd, jint level, jint optname, jint optval, jint optlen)
|
||||
{
|
||||
return zts_setsockopt(fd, level, optname, (const void*)optval, optlen);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_getsockopt(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jint level, jint optname, jint optval, jint optlen)
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_getsockopt(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jint level, jint optname, jint optval, jint optlen)
|
||||
{
|
||||
return zts_getsockopt(fd, level, optname, (void*)optval, (socklen_t *)optlen);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_getsockname(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jobject ztaddr)
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_getsockname(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jobject ztaddr)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
int err = zts_getsockname(fd, (struct sockaddr *)&addr, (socklen_t *)sizeof(struct sockaddr));
|
||||
jfieldID fid;
|
||||
jclass cls = (*env).GetObjectClass(ztaddr);
|
||||
fid = (*env).GetFieldID( cls, "port", "I");
|
||||
(*env).SetIntField( ztaddr, fid, addr.sin_port);
|
||||
fid = (*env).GetFieldID( cls,"_rawAddr", "J");
|
||||
(*env).SetLongField( ztaddr, fid,addr.sin_addr.s_addr);
|
||||
jclass c = (*env).GetObjectClass(ztaddr);
|
||||
if (c) {
|
||||
fid = (*env).GetFieldID(c, "port", "I");
|
||||
(*env).SetIntField(ztaddr, fid, addr.sin_port);
|
||||
fid = (*env).GetFieldID(c,"_rawAddr", "J");
|
||||
(*env).SetLongField(ztaddr, fid,addr.sin_addr.s_addr);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_getpeername(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jobject ztaddr)
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_getpeername(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jobject ztaddr)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
int err = zts_getpeername(fd, (struct sockaddr *)&addr, (socklen_t *)sizeof(struct sockaddr));
|
||||
jfieldID fid;
|
||||
jclass cls = (*env).GetObjectClass( ztaddr);
|
||||
fid = (*env).GetFieldID( cls, "port", "I");
|
||||
(*env).SetIntField( ztaddr, fid, addr.sin_port);
|
||||
fid = (*env).GetFieldID( cls,"_rawAddr", "J");
|
||||
(*env).SetLongField( ztaddr, fid,addr.sin_addr.s_addr);
|
||||
jclass c = (*env).GetObjectClass( ztaddr);
|
||||
if (c) {
|
||||
fid = (*env).GetFieldID(c, "port", "I");
|
||||
(*env).SetIntField(ztaddr, fid, addr.sin_port);
|
||||
fid = (*env).GetFieldID(c,"_rawAddr", "J");
|
||||
(*env).SetLongField(ztaddr, fid,addr.sin_addr.s_addr);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_fcntl(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jint cmd, jint flags)
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_fcntl(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jint cmd, jint flags)
|
||||
{
|
||||
return zts_fcntl(fd,cmd,flags);
|
||||
}
|
||||
@@ -287,21 +450,21 @@ namespace ZeroTier {
|
||||
/* ZeroTier service controls (for JNI wrapper) */
|
||||
/****************************************************************************/
|
||||
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_start(JNIEnv *env, jobject thisObj, jstring path, jboolean blocking)
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_start(JNIEnv *env, jobject thisObj, jstring path, jboolean blocking)
|
||||
{
|
||||
if (path) {
|
||||
zts_start(env->GetStringUTFChars(path, NULL), blocking);
|
||||
}
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_startjoin(JNIEnv *env, jobject thisObj, jstring path, jstring nwid)
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_startjoin(JNIEnv *env, jobject thisObj, jstring path, jstring nwid)
|
||||
{
|
||||
if (path && nwid) {
|
||||
zts_startjoin(env->GetStringUTFChars(path, NULL), env->GetStringUTFChars(nwid, NULL));
|
||||
}
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_stop(JNIEnv *env, jobject thisObj)
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_stop(JNIEnv *env, jobject thisObj)
|
||||
{
|
||||
zts_stop();
|
||||
}
|
||||
@@ -309,17 +472,17 @@ namespace ZeroTier {
|
||||
JNIEXPORT jboolean JNICALL Java_zerotier_ZeroTier_running(
|
||||
JNIEnv *env, jobject thisObj)
|
||||
{
|
||||
return zts_running();
|
||||
return zts_running();
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_join(JNIEnv *env, jobject thisObj, jstring nwid)
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_join(JNIEnv *env, jobject thisObj, jstring nwid)
|
||||
{
|
||||
if (nwid) {
|
||||
zts_join(env->GetStringUTFChars(nwid, NULL));
|
||||
}
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_leave(JNIEnv *env, jobject thisObj, jstring nwid)
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_leave(JNIEnv *env, jobject thisObj, jstring nwid)
|
||||
{
|
||||
if (nwid) {
|
||||
zts_leave(env->GetStringUTFChars(nwid, NULL));
|
||||
@@ -334,32 +497,22 @@ namespace ZeroTier {
|
||||
return (*env).NewStringUTF("");
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL Java_zerotier_ZeroTier_get_ipv4_address(
|
||||
JNIEXPORT jobject JNICALL Java_zerotier_ZeroTier_get_address(
|
||||
JNIEnv *env, jobject thisObj, jstring nwid)
|
||||
{
|
||||
// get address
|
||||
const char *nwid_str = env->GetStringUTFChars(nwid, NULL);
|
||||
char address_string[INET_ADDRSTRLEN];
|
||||
memset(address_string, 0, INET_ADDRSTRLEN);
|
||||
zts_get_ipv4_address(nwid_str, address_string, INET_ADDRSTRLEN);
|
||||
jclass clazz = (*env).FindClass("java/util/ArrayList");
|
||||
jobject addresses = (*env).NewObject(clazz, (*env).GetMethodID(clazz, "<init>", "()V"));
|
||||
jstring _str = (*env).NewStringUTF(address_string);
|
||||
env->CallBooleanMethod(addresses, env->GetMethodID(clazz, "add", "(Ljava/lang/Object;)Z"), _str);
|
||||
return addresses;
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL Java_zerotier_ZeroTier_get_ipv6_address(
|
||||
JNIEnv *env, jobject thisObj, jstring nwid)
|
||||
{
|
||||
const char *nwid_str = env->GetStringUTFChars(nwid, NULL);
|
||||
char address_string[INET6_ADDRSTRLEN];
|
||||
memset(address_string, 0, INET6_ADDRSTRLEN);
|
||||
zts_get_ipv6_address(nwid_str, address_string, INET6_ADDRSTRLEN);
|
||||
jclass clazz = (*env).FindClass("java/util/ArrayList");
|
||||
jobject addresses = (*env).NewObject(clazz, (*env).GetMethodID(clazz, "<init>", "()V"));
|
||||
jstring _str = (*env).NewStringUTF(address_string);
|
||||
env->CallBooleanMethod(addresses, env->GetMethodID(clazz, "add", "(Ljava/lang/Object;)Z"), _str);
|
||||
return addresses;
|
||||
struct sockaddr_storage ss;
|
||||
zts_get_address(nwid_str, &ss, INET_ADDRSTRLEN);
|
||||
// map sockaddr_storage contents to java InetAddress
|
||||
/*
|
||||
jclass c = (*env).FindClass("java/net/InetAddress");
|
||||
if (c) {
|
||||
jobject addresses = (*env).NewObject(c, (*env).GetMethodID(c, "<init>", "()V"));
|
||||
env->CallBooleanMethod(addresses, env->GetMethodID(c, "add", "(Ljava/lang/Object;)Z"), _str);
|
||||
return addresses;
|
||||
}*/
|
||||
return NULL;
|
||||
}
|
||||
|
||||
JNIEXPORT jint Java_zerotier_ZeroTier_get_id()
|
||||
@@ -368,8 +521,6 @@ namespace ZeroTier {
|
||||
}
|
||||
}
|
||||
|
||||
#endif // SDK_JNI
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
1838
src/lwIP.cpp
1838
src/lwIP.cpp
File diff suppressed because it is too large
Load Diff
1979
src/picoTCP.cpp
Normal file
1979
src/picoTCP.cpp
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user