Preparation for C--, removed classes, namespaces, advanced build options are now controlled via include/libztDefs.h
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user