2017-04-14 17:23:28 -07:00
|
|
|
/*
|
2017-05-04 15:53:38 -07:00
|
|
|
* ZeroTier SDK - Network Virtualization Everywhere
|
2017-05-04 15:35:50 -07:00
|
|
|
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
2017-04-14 17:23:28 -07:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-05-04 15:35:50 -07:00
|
|
|
*
|
|
|
|
|
* --
|
|
|
|
|
*
|
|
|
|
|
* You can be released from the requirements of the license by purchasing
|
|
|
|
|
* a commercial license. Buying such a license is mandatory as soon as you
|
|
|
|
|
* develop commercial closed-source software that incorporates or links
|
|
|
|
|
* directly against ZeroTier software without disclosing the source code
|
|
|
|
|
* of your own application.
|
2017-04-14 17:23:28 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
// General connection object used by VirtualTap and network stack drivers
|
2017-07-25 23:40:24 -07:00
|
|
|
|
2017-04-14 17:23:28 -07:00
|
|
|
#ifndef ZT_CONNECTION_HPP
|
|
|
|
|
#define ZT_CONNECTION_HPP
|
|
|
|
|
|
2017-06-05 14:26:06 -07:00
|
|
|
#include <ctime>
|
2017-04-14 17:23:28 -07:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
2017-07-25 23:40:24 -07:00
|
|
|
#if defined(STACK_PICO)
|
2017-07-26 02:12:28 -07:00
|
|
|
#include "pico_socket.h"
|
2017-07-25 23:40:24 -07:00
|
|
|
#endif
|
2017-04-14 17:23:28 -07:00
|
|
|
|
|
|
|
|
#include "Phy.hpp"
|
|
|
|
|
|
2017-06-14 16:53:59 -07:00
|
|
|
#include "libzt.h"
|
2017-08-15 18:15:06 -07:00
|
|
|
#include "VirtualTap.hpp"
|
2017-07-12 11:44:31 -07:00
|
|
|
#include "RingBuffer.hpp"
|
2017-04-14 17:23:28 -07:00
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
2017-07-25 23:40:24 -07:00
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
class VirtualTap;
|
2017-04-14 17:23:28 -07:00
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
/*
|
|
|
|
|
* Something analogous to a socket. This is a common object used by the
|
|
|
|
|
* libzt API, VirtualTap, and the userspace network stack driver implementations.
|
|
|
|
|
* In some situations the word 'Connection' would capture the meaning and
|
|
|
|
|
* function of this object, however I'd like to discourage this since this
|
|
|
|
|
* object also handles non-connection-based traffic as well.
|
|
|
|
|
*/
|
|
|
|
|
struct VirtualSocket
|
2017-04-14 17:23:28 -07:00
|
|
|
{
|
2017-07-12 11:44:31 -07:00
|
|
|
RingBuffer<unsigned char> *TXbuf;
|
|
|
|
|
RingBuffer<unsigned char> *RXbuf;
|
2017-06-16 16:58:30 -07:00
|
|
|
Mutex _tx_m, _rx_m;
|
2017-05-02 09:32:10 -07:00
|
|
|
|
2017-07-25 23:40:24 -07:00
|
|
|
PhySocket *sock;
|
2017-04-14 17:23:28 -07:00
|
|
|
|
2017-07-25 23:40:24 -07:00
|
|
|
#if defined(STACK_PICO)
|
|
|
|
|
struct pico_socket *picosock;
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
|
|
|
|
void *pcb;
|
|
|
|
|
#endif
|
2017-08-15 18:15:06 -07:00
|
|
|
|
2017-04-14 17:23:28 -07:00
|
|
|
// TODO: For getsockname, etc
|
|
|
|
|
struct sockaddr_storage *local_addr; // Address we've bound to locally
|
2017-06-16 16:58:30 -07:00
|
|
|
struct sockaddr_storage *peer_addr; // Address of connection call to remote host
|
2017-04-14 17:23:28 -07:00
|
|
|
|
2017-08-02 14:39:21 -07:00
|
|
|
int socket_family, socket_type, protocol;
|
2017-04-14 17:23:28 -07:00
|
|
|
|
2017-07-25 23:40:24 -07:00
|
|
|
int app_fd; // used by app for I/O
|
|
|
|
|
int sdk_fd; // used by lib for I/O
|
2017-04-14 17:23:28 -07:00
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
std::queue<VirtualSocket*> _AcceptedConnections;
|
|
|
|
|
VirtualTap *tap;
|
2017-06-16 16:58:30 -07:00
|
|
|
int state; // See libzt.h for (ZT_SOCK_STATE_*)
|
2017-04-14 17:23:28 -07:00
|
|
|
|
2017-06-05 14:26:06 -07:00
|
|
|
// timestamp for closure event
|
|
|
|
|
std::time_t closure_ts;
|
|
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
VirtualSocket() {
|
2017-07-12 11:44:31 -07:00
|
|
|
TXbuf = new RingBuffer<unsigned char>(ZT_TCP_TX_BUF_SZ);
|
|
|
|
|
RXbuf = new RingBuffer<unsigned char>(ZT_TCP_RX_BUF_SZ);
|
|
|
|
|
|
2017-06-05 14:26:06 -07:00
|
|
|
closure_ts = -1;
|
2017-04-14 17:23:28 -07:00
|
|
|
ZT_PHY_SOCKFD_TYPE fdpair[2];
|
|
|
|
|
if(socketpair(PF_LOCAL, SOCK_STREAM, 0, fdpair) < 0) {
|
|
|
|
|
if(errno < 0) {
|
|
|
|
|
DEBUG_ERROR("unable to create socketpair");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sdk_fd = fdpair[0];
|
|
|
|
|
app_fd = fdpair[1];
|
2017-04-21 14:56:42 -07:00
|
|
|
}
|
2017-08-15 18:15:06 -07:00
|
|
|
~VirtualSocket() { }
|
2017-04-14 17:23:28 -07:00
|
|
|
};
|
2017-04-20 13:39:46 -07:00
|
|
|
|
|
|
|
|
/*
|
2017-08-15 18:15:06 -07:00
|
|
|
* A helper object for passing VirtualTap(s) and VirtualSocket(s) through the stack
|
2017-04-20 13:39:46 -07:00
|
|
|
*/
|
2017-08-15 18:15:06 -07:00
|
|
|
struct VirtualBindingPair
|
2017-04-20 13:39:46 -07:00
|
|
|
{
|
2017-08-15 18:15:06 -07:00
|
|
|
VirtualTap *tap;
|
|
|
|
|
VirtualSocket *vs;
|
|
|
|
|
VirtualBindingPair(VirtualTap *_tap, VirtualSocket *_vs) : tap(_tap), vs(_vs) {}
|
2017-04-20 13:39:46 -07:00
|
|
|
};
|
2017-04-14 17:23:28 -07:00
|
|
|
}
|
|
|
|
|
#endif
|