2017-04-06 19:16:01 -07:00
|
|
|
/*
|
2017-04-14 17:23:28 -07:00
|
|
|
* ZeroTier SDK - Network Virtualization Everywhere
|
|
|
|
|
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
|
2017-04-06 19:16:01 -07:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-04-14 17:23:28 -07:00
|
|
|
#ifndef ZT_SOCKETTAP_HPP
|
|
|
|
|
#define ZT_SOCKETTAP_HPP
|
2017-04-06 19:16:01 -07:00
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
2017-04-14 17:23:28 -07:00
|
|
|
#include <queue>
|
2017-04-06 19:16:01 -07:00
|
|
|
#include <utility>
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
#include "Constants.hpp"
|
|
|
|
|
#include "MulticastGroup.hpp"
|
|
|
|
|
#include "Mutex.hpp"
|
|
|
|
|
#include "InetAddress.hpp"
|
|
|
|
|
#include "Thread.hpp"
|
|
|
|
|
#include "Phy.hpp"
|
|
|
|
|
|
|
|
|
|
#include "ZeroTierSDK.h"
|
|
|
|
|
#include "picoTCP.hpp"
|
2017-04-14 17:23:28 -07:00
|
|
|
#include "Connection.hpp"
|
2017-04-06 19:16:01 -07:00
|
|
|
|
|
|
|
|
#include "pico_protocol.h"
|
|
|
|
|
#include "pico_stack.h"
|
|
|
|
|
#include "pico_ipv4.h"
|
|
|
|
|
#include "pico_icmp4.h"
|
|
|
|
|
#include "pico_dev_tap.h"
|
|
|
|
|
#include "pico_protocol.h"
|
|
|
|
|
#include "pico_socket.h"
|
|
|
|
|
#include "pico_device.h"
|
|
|
|
|
#include "pico_ipv6.h"
|
|
|
|
|
|
|
|
|
|
// ZT RPC structs
|
|
|
|
|
struct socket_st;
|
|
|
|
|
struct listen_st;
|
|
|
|
|
struct bind_st;
|
|
|
|
|
struct connect_st;
|
|
|
|
|
struct getsockname_st;
|
|
|
|
|
struct accept_st;
|
|
|
|
|
|
|
|
|
|
struct pico_socket;
|
|
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
|
|
extern SocketTap *picotap;
|
|
|
|
|
|
|
|
|
|
/*
|
2017-04-07 17:56:05 -07:00
|
|
|
* A helper for passing a reference to _phy to stackrpc callbacks as a "state"
|
2017-04-06 19:16:01 -07:00
|
|
|
*/
|
|
|
|
|
struct Larg
|
|
|
|
|
{
|
|
|
|
|
SocketTap *tap;
|
|
|
|
|
Connection *conn;
|
|
|
|
|
Larg(SocketTap *_tap, Connection *conn) : tap(_tap), conn(conn) {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
2017-04-07 17:56:05 -07:00
|
|
|
* Socket Tap -- emulates an Ethernet tap device
|
2017-04-06 19:16:01 -07:00
|
|
|
*/
|
|
|
|
|
class SocketTap
|
|
|
|
|
{
|
|
|
|
|
friend class Phy<SocketTap *>;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
SocketTap(
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
~SocketTap();
|
|
|
|
|
|
|
|
|
|
void setEnabled(bool en);
|
|
|
|
|
bool enabled() const;
|
2017-04-07 17:56:05 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
2017-04-06 19:16:01 -07:00
|
|
|
bool addIp(const InetAddress &ip);
|
2017-04-07 17:56:05 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
2017-04-06 19:16:01 -07:00
|
|
|
bool removeIp(const InetAddress &ip);
|
|
|
|
|
std::vector<InetAddress> ips() const;
|
|
|
|
|
std::vector<InetAddress> _ips;
|
2017-04-07 17:56:05 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,
|
|
|
|
|
unsigned int len);
|
2017-04-06 19:16:01 -07:00
|
|
|
|
2017-04-07 17:56:05 -07:00
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
2017-04-06 19:16:01 -07:00
|
|
|
std::string deviceName() const;
|
2017-04-07 17:56:05 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
2017-04-06 19:16:01 -07:00
|
|
|
void setFriendlyName(const char *friendlyName);
|
2017-04-07 17:56:05 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
2017-04-06 19:16:01 -07:00
|
|
|
void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
|
2017-04-14 17:23:28 -07:00
|
|
|
|
2017-04-07 17:56:05 -07:00
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
2017-04-06 19:16:01 -07:00
|
|
|
void threadMain()
|
|
|
|
|
throw();
|
|
|
|
|
|
|
|
|
|
std::string _homePath;
|
|
|
|
|
MAC _mac;
|
|
|
|
|
unsigned int _mtu;
|
|
|
|
|
uint64_t _nwid;
|
2017-04-07 17:56:05 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void (*_handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,
|
|
|
|
|
const void *,unsigned int);
|
|
|
|
|
|
2017-04-06 19:16:01 -07:00
|
|
|
void *_arg;
|
|
|
|
|
Phy<SocketTap *> _phy;
|
|
|
|
|
PhySocket *_unixListenSocket;
|
|
|
|
|
volatile bool _enabled;
|
|
|
|
|
volatile bool _run;
|
|
|
|
|
|
|
|
|
|
// picoTCP
|
|
|
|
|
unsigned char pico_frame_rxbuf[MAX_PICO_FRAME_RX_BUF_SZ];
|
|
|
|
|
int pico_frame_rxbuf_tot;
|
|
|
|
|
Mutex _pico_frame_rxbuf_m;
|
|
|
|
|
|
2017-04-14 17:23:28 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
/* In these, we will call the stack's corresponding functions, this is *
|
|
|
|
|
* where one would put logic to select between different stacks */
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int Connect(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
|
|
|
|
|
int Bind(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
|
|
|
|
|
void Listen(Connection *conn, int fd, int backlog);
|
|
|
|
|
int Accept(Connection *conn);
|
|
|
|
|
|
|
|
|
|
|
2017-04-06 19:16:01 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Return the address that the socket is bound to
|
|
|
|
|
*/
|
|
|
|
|
void handleGetsockname(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct getsockname_st *getsockname_rpc);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Return the address of the peer connected to this socket
|
|
|
|
|
*/
|
|
|
|
|
void handleGetpeername(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct getsockname_st *getsockname_rpc);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Writes data from the application's socket to the LWIP connection
|
|
|
|
|
*/
|
|
|
|
|
void handleWrite(Connection *conn);
|
|
|
|
|
|
|
|
|
|
// Unused -- no UDP or TCP from this thread/Phy<>
|
2017-04-07 17:56:05 -07:00
|
|
|
void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *local_address,
|
|
|
|
|
const struct sockaddr *from,void *data,unsigned long len);
|
2017-04-06 19:16:01 -07:00
|
|
|
void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success);
|
2017-04-07 17:56:05 -07:00
|
|
|
void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,
|
|
|
|
|
const struct sockaddr *from);
|
2017-04-06 19:16:01 -07:00
|
|
|
void phyOnTcpClose(PhySocket *sock,void **uptr);
|
|
|
|
|
void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len);
|
|
|
|
|
void phyOnTcpWritable(PhySocket *sock,void **uptr, bool stack_invoked);
|
|
|
|
|
|
2017-04-07 17:56:05 -07:00
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
2017-04-06 19:16:01 -07:00
|
|
|
void handleRead(PhySocket *sock,void **uptr,bool stack_invoked);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Signals us to close the TcpConnection 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 lwip_invoked);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Returns a pointer to a TcpConnection associated with a given PhySocket
|
|
|
|
|
*/
|
|
|
|
|
Connection *getConnection(PhySocket *sock);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Returns a pointer to a TcpConnection associated with a given pico_socket
|
|
|
|
|
*/
|
|
|
|
|
Connection *getConnection(struct pico_socket *socket);
|
|
|
|
|
|
|
|
|
|
/*
|
2017-04-07 17:56:05 -07:00
|
|
|
* Closes a TcpConnection, associated connection strcuture,
|
2017-04-06 19:16:01 -07:00
|
|
|
* PhySocket, and underlying file descriptor
|
|
|
|
|
*/
|
|
|
|
|
void closeConnection(PhySocket *sock);
|
|
|
|
|
|
|
|
|
|
std::vector<Connection*> _Connections;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
|
|
#endif
|