ZeroTierOne/netcon/NetconService.hpp

142 lines
3.6 KiB
C++
Raw Normal View History

2015-09-10 17:56:01 +00:00
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2015 ZeroTier, Inc.
*
* 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/>.
*
* --
*
* ZeroTier may be used and distributed under the terms of the GPLv3, which
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
*
* If you would like to embed ZeroTier into a commercial application or
* redistribute it in a modified binary form, please contact ZeroTier Networks
* LLC. Start here: http://www.zerotier.com/
*/
#include <sys/poll.h>
#include <string>
2015-09-10 20:52:18 +00:00
#include "../osdep/Phy.hpp"
2015-09-11 00:02:13 +00:00
#include "NetconEthernetTap.hpp"
2015-09-10 20:52:18 +00:00
2015-09-10 17:56:01 +00:00
#include "Intercept.h"
#include "LWIPStack.hpp"
2015-09-11 00:02:13 +00:00
#ifndef _NETCON_SERVICE_HPP
#define _NETCON_SERVICE_HPP
2015-09-10 17:56:01 +00:00
using namespace std;
2015-09-10 20:52:18 +00:00
namespace ZeroTier {
2015-09-11 00:02:13 +00:00
class NetconEthernetTap;
// Helper class for passing reference to Phy to LWIP callbacks
class Larg
{
public:
NetconEthernetTap *tap;
PhySocket *sock;
Larg(NetconEthernetTap *_tap, PhySocket *_sock) : tap(_tap), sock(_sock) {}
};
2015-09-10 20:52:18 +00:00
enum NetconConnectionType { RPC, BUFFER };
2015-09-10 21:44:01 +00:00
// prototypes
2015-09-10 20:52:18 +00:00
class NetconClient;
class NetconConnection;
//
class NetconConnection
{
2015-09-10 21:44:01 +00:00
public:
2015-09-10 20:52:18 +00:00
int their_fd;
unsigned char buf[DEFAULT_READ_BUFFER_SIZE];
int idx;
NetconConnectionType type;
struct tcp_pcb *pcb;
PhySocket *sock;
NetconClient *owner;
2015-09-10 17:56:01 +00:00
2015-09-10 20:52:18 +00:00
NetconConnection(NetconConnectionType type, PhySocket *sock) : type(type), sock(sock) {}
};
2015-09-10 17:56:01 +00:00
2015-09-10 20:52:18 +00:00
//
class NetconClient
2015-09-10 17:56:01 +00:00
{
2015-09-10 22:48:45 +00:00
public:
2015-09-10 20:52:18 +00:00
vector<NetconConnection*> connections;
int tid;
2015-09-10 21:44:01 +00:00
bool waiting_for_retval;
2015-09-10 20:52:18 +00:00
NetconConnection *rpc;
2015-09-10 21:44:01 +00:00
NetconConnection* unmapped_conn;
2015-09-10 20:52:18 +00:00
2015-09-10 22:48:45 +00:00
NetconConnection *addConnection(NetconConnectionType type, PhySocket *sock)
2015-09-10 20:52:18 +00:00
{
2015-09-10 21:44:01 +00:00
NetconConnection *new_conn = new NetconConnection(type, sock);
2015-09-10 20:52:18 +00:00
new_conn->owner = this;
2015-09-10 22:48:45 +00:00
return new_conn;
2015-09-10 20:52:18 +00:00
}
// Check data and RPC connections
NetconConnection *getConnection(PhySocket *sock)
{
if(sock && sock == rpc->sock) {
return rpc;
}
2015-09-10 22:48:45 +00:00
for(size_t i=0; i<connections.size(); i++) {
2015-09-10 20:52:18 +00:00
if(sock == connections[i]->sock) { return connections[i];}
}
return NULL;
}
//
NetconConnection *getConnectionByTheirFD(int fd)
{
2015-09-10 22:48:45 +00:00
for(size_t i=0; i<connections.size(); i++) {
2015-09-10 20:52:18 +00:00
if(connections[i]->their_fd == fd) { return connections[i]; }
}
2015-09-10 22:19:43 +00:00
return NULL;
2015-09-10 20:52:18 +00:00
}
//
NetconConnection *getConnectionByPCB(struct tcp_pcb *pcb)
{
2015-09-10 22:48:45 +00:00
for(size_t i=0; i<connections.size(); i++) {
2015-09-11 00:02:13 +00:00
if(connections[i]->pcb == pcb) { return connections[i]; }
2015-09-10 20:52:18 +00:00
}
2015-09-10 22:19:43 +00:00
return NULL;
2015-09-10 20:52:18 +00:00
}
2015-09-10 21:44:01 +00:00
NetconConnection *containsPCB(struct tcp_pcb *pcb)
{
2015-09-10 22:48:45 +00:00
for(size_t i=0; i<connections.size(); i++) {
2015-09-11 00:02:13 +00:00
if(connections[i]->pcb == pcb) { return connections[i]; }
2015-09-10 21:44:01 +00:00
}
2015-09-10 22:19:43 +00:00
return NULL;
2015-09-10 21:44:01 +00:00
}
2015-09-10 17:56:01 +00:00
2015-09-11 18:00:42 +00:00
void removeConnection(PhySocket *sock)
2015-09-10 20:52:18 +00:00
{
2015-09-11 18:00:42 +00:00
for(size_t i=0; i<connections.size(); i++) {
if(connections[i]->sock == sock) { connections.erase(connections.begin() + i); }
}
2015-09-11 00:34:48 +00:00
}
2015-09-10 20:52:18 +00:00
};
} // namespace ZeroTier
2015-09-11 00:02:13 +00:00
2015-09-10 17:56:01 +00:00
#endif