2015-09-02 22:17:38 +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/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef ZT_ENABLE_NETCON
|
|
|
|
|
2015-09-03 23:05:18 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
2015-09-11 00:02:13 +00:00
|
|
|
#include <dlfcn.h>
|
2015-09-24 19:30:07 +00:00
|
|
|
//#include <sys/types.h>
|
2015-09-11 00:02:13 +00:00
|
|
|
|
2015-09-02 22:17:38 +00:00
|
|
|
#include "NetconEthernetTap.hpp"
|
|
|
|
|
2015-09-02 22:51:28 +00:00
|
|
|
#include "../node/Utils.hpp"
|
2015-09-02 22:17:38 +00:00
|
|
|
#include "../osdep/OSUtils.hpp"
|
|
|
|
#include "../osdep/Phy.hpp"
|
|
|
|
|
2015-09-10 17:56:01 +00:00
|
|
|
#include "lwip/tcp_impl.h"
|
|
|
|
#include "netif/etharp.h"
|
|
|
|
#include "lwip/ip.h"
|
|
|
|
#include "lwip/ip_addr.h"
|
|
|
|
#include "lwip/ip_frag.h"
|
2015-09-15 18:15:59 +00:00
|
|
|
#include "lwip/tcp.h"
|
2015-09-10 17:56:01 +00:00
|
|
|
|
|
|
|
#include "LWIPStack.hpp"
|
2015-09-11 00:02:13 +00:00
|
|
|
#include "NetconService.hpp"
|
2015-09-10 17:56:01 +00:00
|
|
|
#include "Intercept.h"
|
|
|
|
#include "NetconUtilities.hpp"
|
|
|
|
|
|
|
|
#define APPLICATION_POLL_FREQ 1
|
|
|
|
|
2015-09-02 22:17:38 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
2015-09-15 18:15:59 +00:00
|
|
|
|
2015-09-02 22:17:38 +00:00
|
|
|
NetconEthernetTap::NetconEthernetTap(
|
|
|
|
const char *homePath,
|
|
|
|
const MAC &mac,
|
|
|
|
unsigned int mtu,
|
|
|
|
unsigned int metric,
|
|
|
|
uint64_t nwid,
|
|
|
|
const char *friendlyName,
|
|
|
|
void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
|
|
|
|
void *arg) :
|
2015-09-03 23:05:18 +00:00
|
|
|
_phy(this,false,true),
|
2015-09-02 22:51:28 +00:00
|
|
|
_unixListenSocket((PhySocket *)0),
|
2015-09-02 22:17:38 +00:00
|
|
|
_handler(handler),
|
|
|
|
_arg(arg),
|
|
|
|
_nwid(nwid),
|
2015-09-11 19:12:45 +00:00
|
|
|
_mac(mac),
|
2015-09-02 22:17:38 +00:00
|
|
|
_homePath(homePath),
|
|
|
|
_mtu(mtu),
|
2015-09-02 22:51:28 +00:00
|
|
|
_enabled(true),
|
|
|
|
_run(true)
|
2015-09-02 22:17:38 +00:00
|
|
|
{
|
2015-09-02 22:51:28 +00:00
|
|
|
char sockPath[4096];
|
|
|
|
Utils::snprintf(sockPath,sizeof(sockPath),"/tmp/.ztnc_%.16llx",(unsigned long long)nwid);
|
|
|
|
_dev = sockPath;
|
|
|
|
|
2015-09-28 00:51:32 +00:00
|
|
|
lwipstack = new LWIPStack("lib/liblwip.so"); // ext/bin/liblwip.so.debug for debug symbols
|
2015-09-10 17:56:01 +00:00
|
|
|
if(!lwipstack) // TODO double check this check
|
|
|
|
throw std::runtime_error("unable to load lwip lib.");
|
|
|
|
lwipstack->lwip_init();
|
|
|
|
|
2015-09-03 23:05:18 +00:00
|
|
|
_unixListenSocket = _phy.unixListen(sockPath,(void *)this);
|
2015-09-02 23:15:22 +00:00
|
|
|
if (!_unixListenSocket)
|
2015-09-02 22:51:28 +00:00
|
|
|
throw std::runtime_error(std::string("unable to bind to ")+sockPath);
|
2015-09-02 22:17:38 +00:00
|
|
|
_thread = Thread::start(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NetconEthernetTap::~NetconEthernetTap()
|
|
|
|
{
|
2015-09-02 22:51:28 +00:00
|
|
|
_run = false;
|
2015-09-03 23:05:18 +00:00
|
|
|
_phy.whack();
|
|
|
|
_phy.whack();
|
2015-09-02 22:51:28 +00:00
|
|
|
Thread::join(_thread);
|
2015-09-03 23:05:18 +00:00
|
|
|
_phy.close(_unixListenSocket,false);
|
2015-09-24 18:11:16 +00:00
|
|
|
delete lwipstack;
|
2015-09-02 22:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetconEthernetTap::setEnabled(bool en)
|
|
|
|
{
|
|
|
|
_enabled = en;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NetconEthernetTap::enabled() const
|
|
|
|
{
|
|
|
|
return _enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NetconEthernetTap::addIp(const InetAddress &ip)
|
|
|
|
{
|
2015-09-03 23:05:18 +00:00
|
|
|
Mutex::Lock _l(_ips_m);
|
|
|
|
if (std::find(_ips.begin(),_ips.end(),ip) == _ips.end()) {
|
|
|
|
_ips.push_back(ip);
|
|
|
|
std::sort(_ips.begin(),_ips.end());
|
|
|
|
|
2015-09-11 19:12:45 +00:00
|
|
|
if (ip.isV4()) {
|
2015-09-22 21:43:20 +00:00
|
|
|
// Set IP
|
|
|
|
static ip_addr_t ipaddr, netmask, gw;
|
|
|
|
IP4_ADDR(&gw,192,168,0,1);
|
|
|
|
ipaddr.addr = *((u32_t *)ip.rawIpData());
|
|
|
|
netmask.addr = *((u32_t *)ip.netmask().rawIpData());
|
|
|
|
|
|
|
|
// Set up the lwip-netif for LWIP's sake
|
2015-09-24 19:56:48 +00:00
|
|
|
lwipstack->netif_add(&interface,&ipaddr, &netmask, &gw, NULL, tapif_init, lwipstack->_ethernet_input);
|
2015-09-22 21:43:20 +00:00
|
|
|
interface.state = this;
|
2015-09-24 19:56:48 +00:00
|
|
|
interface.output = lwipstack->_etharp_output;
|
2015-09-22 21:43:20 +00:00
|
|
|
_mac.copyTo(interface.hwaddr, 6);
|
|
|
|
interface.mtu = _mtu;
|
|
|
|
interface.name[0] = 't';
|
|
|
|
interface.name[1] = 'p';
|
|
|
|
interface.linkoutput = low_level_output;
|
|
|
|
interface.hwaddr_len = 6;
|
|
|
|
interface.flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
|
|
|
|
lwipstack->netif_set_default(&interface);
|
|
|
|
lwipstack->netif_set_up(&interface);
|
2015-09-11 19:12:45 +00:00
|
|
|
}
|
2015-09-03 23:05:18 +00:00
|
|
|
}
|
2015-09-15 23:06:16 +00:00
|
|
|
return true;
|
2015-09-02 22:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NetconEthernetTap::removeIp(const InetAddress &ip)
|
|
|
|
{
|
2015-09-03 23:05:18 +00:00
|
|
|
Mutex::Lock _l(_ips_m);
|
2015-09-03 23:44:04 +00:00
|
|
|
std::vector<InetAddress>::iterator i(std::find(_ips.begin(),_ips.end(),ip));
|
2015-09-03 23:05:18 +00:00
|
|
|
if (i == _ips.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
_ips.erase(i);
|
2015-09-11 19:12:45 +00:00
|
|
|
|
|
|
|
if (ip.isV4()) {
|
2015-09-22 21:43:20 +00:00
|
|
|
// TODO: dealloc from LWIP
|
2015-09-11 19:12:45 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 23:05:18 +00:00
|
|
|
return true;
|
2015-09-02 22:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<InetAddress> NetconEthernetTap::ips() const
|
|
|
|
{
|
2015-09-03 23:05:18 +00:00
|
|
|
Mutex::Lock _l(_ips_m);
|
|
|
|
return _ips;
|
2015-09-02 22:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
|
|
|
|
{
|
2015-09-22 21:43:20 +00:00
|
|
|
struct pbuf *p,*q;
|
2015-09-23 18:23:36 +00:00
|
|
|
//fprintf(stderr, "_put(%s,%s,%.4x,[data],%u)\n",from.toString().c_str(),to.toString().c_str(),etherType,len);
|
2015-09-02 22:51:28 +00:00
|
|
|
if (!_enabled)
|
|
|
|
return;
|
2015-09-11 19:12:45 +00:00
|
|
|
|
2015-09-22 21:43:20 +00:00
|
|
|
//printf(">> %.4x %s\n",etherType,Utils::hex(data,len).c_str());
|
|
|
|
struct eth_hdr ethhdr;
|
|
|
|
from.copyTo(ethhdr.src.addr, 6);
|
|
|
|
to.copyTo(ethhdr.dest.addr, 6);
|
|
|
|
ethhdr.type = Utils::hton((uint16_t)etherType);
|
2015-09-15 22:47:40 +00:00
|
|
|
|
|
|
|
// We allocate a pbuf chain of pbufs from the pool.
|
|
|
|
p = lwipstack->pbuf_alloc(PBUF_RAW, len+sizeof(struct eth_hdr), PBUF_POOL);
|
|
|
|
|
2015-09-22 21:43:20 +00:00
|
|
|
if (p != NULL) {
|
|
|
|
const char *dataptr = reinterpret_cast<const char *>(data);
|
|
|
|
|
|
|
|
// First pbuf gets ethernet header at start
|
|
|
|
q = p;
|
|
|
|
if (q->len < sizeof(ethhdr)) {
|
|
|
|
fprintf(stderr,"_put(): Dropped packet: first pbuf smaller than ethernet header\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy(q->payload,ðhdr,sizeof(ethhdr));
|
|
|
|
memcpy(q->payload + sizeof(ethhdr),dataptr,q->len - sizeof(ethhdr));
|
|
|
|
dataptr += q->len - sizeof(ethhdr);
|
|
|
|
|
|
|
|
// Remaining pbufs (if any) get rest of data
|
|
|
|
while ((q = q->next)) {
|
|
|
|
memcpy(q->payload,dataptr,q->len);
|
|
|
|
dataptr += q->len;
|
2015-09-15 18:15:59 +00:00
|
|
|
}
|
2015-09-15 22:47:40 +00:00
|
|
|
} else {
|
2015-09-22 21:43:20 +00:00
|
|
|
fprintf(stderr, "_put(): Dropped packet: no pbufs available\n");
|
2015-09-15 22:47:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-09-15 18:15:59 +00:00
|
|
|
|
2015-09-22 21:43:20 +00:00
|
|
|
//printf("p->len == %u, p->payload == %s\n",p->len,Utils::hex(p->payload,p->len).c_str());
|
2015-09-24 20:01:19 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _l2(lwipstack->_lock);
|
|
|
|
if(interface.input(p, &interface) != ERR_OK) {
|
|
|
|
fprintf(stderr, "_put(): Error while RXing packet (netif->input)\n");
|
|
|
|
}
|
2015-09-15 22:47:40 +00:00
|
|
|
}
|
2015-09-02 22:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string NetconEthernetTap::deviceName() const
|
|
|
|
{
|
|
|
|
return _dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetconEthernetTap::setFriendlyName(const char *friendlyName)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetconEthernetTap::scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed)
|
|
|
|
{
|
2015-09-11 19:12:45 +00:00
|
|
|
std::vector<MulticastGroup> newGroups;
|
|
|
|
Mutex::Lock _l(_multicastGroups_m);
|
|
|
|
|
2015-09-03 23:05:18 +00:00
|
|
|
// TODO: get multicast subscriptions from LWIP
|
2015-09-02 22:17:38 +00:00
|
|
|
|
2015-09-11 19:12:45 +00:00
|
|
|
std::vector<InetAddress> allIps(ips());
|
|
|
|
for(std::vector<InetAddress>::iterator ip(allIps.begin());ip!=allIps.end();++ip)
|
|
|
|
newGroups.push_back(MulticastGroup::deriveMulticastGroupForAddressResolution(*ip));
|
|
|
|
|
|
|
|
std::sort(newGroups.begin(),newGroups.end());
|
|
|
|
std::unique(newGroups.begin(),newGroups.end());
|
|
|
|
|
|
|
|
for(std::vector<MulticastGroup>::iterator m(newGroups.begin());m!=newGroups.end();++m) {
|
|
|
|
if (!std::binary_search(_multicastGroups.begin(),_multicastGroups.end(),*m))
|
|
|
|
added.push_back(*m);
|
|
|
|
}
|
|
|
|
for(std::vector<MulticastGroup>::iterator m(_multicastGroups.begin());m!=_multicastGroups.end();++m) {
|
|
|
|
if (!std::binary_search(newGroups.begin(),newGroups.end(),*m))
|
|
|
|
removed.push_back(*m);
|
|
|
|
}
|
|
|
|
|
|
|
|
_multicastGroups.swap(newGroups);
|
|
|
|
}
|
2015-09-10 21:44:01 +00:00
|
|
|
|
2015-09-27 10:08:39 +00:00
|
|
|
TcpConnection *NetconEthernetTap::getConnectionByPCB(struct tcp_pcb *pcb)
|
2015-09-10 21:44:01 +00:00
|
|
|
{
|
2015-09-27 10:08:39 +00:00
|
|
|
for(size_t i=0; i<tcp_connections.size(); i++) {
|
|
|
|
if(tcp_connections[i]->pcb == pcb)
|
|
|
|
return tcp_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-27 10:08:39 +00:00
|
|
|
TcpConnection *NetconEthernetTap::getConnectionByTheirFD(int fd)
|
2015-09-10 21:44:01 +00:00
|
|
|
{
|
2015-09-27 10:08:39 +00:00
|
|
|
for(size_t i=0; i<tcp_connections.size(); i++) {
|
|
|
|
if(tcp_connections[i]->perceived_fd == fd)
|
|
|
|
return tcp_connections[i];
|
2015-09-10 22:19:43 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:23:26 +00:00
|
|
|
/*
|
2015-09-27 10:08:39 +00:00
|
|
|
* Closes a TcpConnection and associated LWIP PCB strcuture.
|
2015-09-23 19:23:26 +00:00
|
|
|
*/
|
2015-09-27 10:08:39 +00:00
|
|
|
void NetconEthernetTap::closeConnection(TcpConnection *conn)
|
|
|
|
{
|
|
|
|
//fprintf(stderr, "closeConnection(): closing: conn->type = %d, fd=%d\n", conn->type, _phy.getDescriptor(conn->sock));
|
|
|
|
lwipstack->_tcp_arg(conn->pcb, NULL);
|
|
|
|
lwipstack->_tcp_sent(conn->pcb, NULL);
|
|
|
|
lwipstack->_tcp_recv(conn->pcb, NULL);
|
|
|
|
lwipstack->_tcp_err(conn->pcb, NULL);
|
|
|
|
lwipstack->_tcp_poll(conn->pcb, NULL, 0);
|
|
|
|
lwipstack->_tcp_close(conn->pcb);
|
|
|
|
close(_phy.getDescriptor(conn->dataSock));
|
|
|
|
close(conn->their_fd);
|
|
|
|
_phy.close(conn->dataSock);
|
2015-09-24 23:22:56 +00:00
|
|
|
|
2015-09-27 10:08:39 +00:00
|
|
|
for(int i=0; i<tcp_connections.size(); i++) {
|
|
|
|
if(tcp_connections[i] == conn) {
|
|
|
|
tcp_connections.erase(tcp_connections.begin() + i);
|
|
|
|
}
|
2015-09-24 23:22:56 +00:00
|
|
|
}
|
2015-09-24 21:10:30 +00:00
|
|
|
delete conn;
|
2015-09-11 18:00:42 +00:00
|
|
|
}
|
|
|
|
|
2015-09-27 10:08:39 +00:00
|
|
|
void NetconEthernetTap::closeClient(PhySocket *sock)
|
2015-09-22 23:53:36 +00:00
|
|
|
{
|
2015-09-27 10:08:39 +00:00
|
|
|
for(int i=0; i<rpc_sockets.size(); i++) {
|
|
|
|
if(rpc_sockets[i] == sock)
|
|
|
|
rpc_sockets.erase(rpc_sockets.begin() + i);
|
2015-09-22 23:53:36 +00:00
|
|
|
}
|
2015-09-27 10:08:39 +00:00
|
|
|
close(_phy.getDescriptor(sock));
|
|
|
|
_phy.close(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetconEthernetTap::closeAll()
|
|
|
|
{
|
|
|
|
while(rpc_sockets.size())
|
|
|
|
closeClient(rpc_sockets.front());
|
|
|
|
while(tcp_connections.size())
|
|
|
|
closeConnection(tcp_connections.front());
|
2015-09-22 23:53:36 +00:00
|
|
|
}
|
2015-09-10 21:44:01 +00:00
|
|
|
|
2015-09-22 21:43:20 +00:00
|
|
|
#define ZT_LWIP_TCP_TIMER_INTERVAL 10
|
2015-09-15 18:15:59 +00:00
|
|
|
|
2015-09-02 22:17:38 +00:00
|
|
|
void NetconEthernetTap::threadMain()
|
|
|
|
throw()
|
2015-09-02 22:51:28 +00:00
|
|
|
{
|
2015-09-27 10:08:39 +00:00
|
|
|
fprintf(stderr, "_threadMain()\n");
|
2015-09-22 17:00:00 +00:00
|
|
|
uint64_t prev_tcp_time = 0;
|
|
|
|
uint64_t prev_etharp_time = 0;
|
2015-09-02 22:51:28 +00:00
|
|
|
|
2015-09-23 18:23:36 +00:00
|
|
|
/*
|
2015-09-21 23:39:26 +00:00
|
|
|
fprintf(stderr, "- MEM_SIZE = %dM\n", MEM_SIZE / (1024*1024));
|
|
|
|
fprintf(stderr, "- TCP_SND_BUF = %dK\n", TCP_SND_BUF / 1024);
|
|
|
|
fprintf(stderr, "- MEMP_NUM_PBUF = %d\n", MEMP_NUM_PBUF);
|
|
|
|
fprintf(stderr, "- MEMP_NUM_TCP_PCB = %d\n", MEMP_NUM_TCP_PCB);
|
|
|
|
fprintf(stderr, "- MEMP_NUM_TCP_PCB_LISTEN = %d\n", MEMP_NUM_TCP_PCB_LISTEN);
|
|
|
|
fprintf(stderr, "- MEMP_NUM_TCP_SEG = %d\n", MEMP_NUM_TCP_SEG);
|
|
|
|
fprintf(stderr, "- PBUF_POOL_SIZE = %d\n", PBUF_POOL_SIZE);
|
|
|
|
fprintf(stderr, "- TCP_SND_QUEUELEN = %d\n", TCP_SND_QUEUELEN);
|
|
|
|
fprintf(stderr, "- IP_REASSEMBLY = %d\n", IP_REASSEMBLY);
|
|
|
|
fprintf(stderr, "- TCP_WND = %d\n", TCP_WND);
|
|
|
|
fprintf(stderr, "- TCP_MSS = %d\n", TCP_MSS);
|
|
|
|
fprintf(stderr, "- NO_SYS = %d\n", NO_SYS);
|
|
|
|
fprintf(stderr, "- LWIP_SOCKET = %d\n", LWIP_SOCKET);
|
|
|
|
fprintf(stderr, "- LWIP_NETCONN = %d\n", LWIP_NETCONN);
|
|
|
|
fprintf(stderr, "- ARP_TMR_INTERVAL = %d\n", ARP_TMR_INTERVAL);
|
|
|
|
fprintf(stderr, "- TCP_TMR_INTERVAL = %d\n", TCP_TMR_INTERVAL);
|
|
|
|
fprintf(stderr, "- IP_TMR_INTERVAL = %d\n", IP_TMR_INTERVAL);
|
|
|
|
fprintf(stderr, "- DEFAULT_READ_BUFFER_SIZE = %d\n", DEFAULT_READ_BUFFER_SIZE);
|
2015-09-23 18:23:36 +00:00
|
|
|
*/
|
2015-09-23 21:55:15 +00:00
|
|
|
|
2015-09-15 22:47:40 +00:00
|
|
|
// Main timer loop
|
2015-09-10 17:56:01 +00:00
|
|
|
while (_run) {
|
2015-09-22 17:00:00 +00:00
|
|
|
uint64_t now = OSUtils::now();
|
|
|
|
|
|
|
|
uint64_t since_tcp = now - prev_tcp_time;
|
|
|
|
uint64_t since_etharp = now - prev_etharp_time;
|
|
|
|
|
|
|
|
uint64_t tcp_remaining = ZT_LWIP_TCP_TIMER_INTERVAL;
|
2015-09-22 21:43:20 +00:00
|
|
|
uint64_t etharp_remaining = ARP_TMR_INTERVAL;
|
2015-09-22 17:00:00 +00:00
|
|
|
|
|
|
|
if (since_tcp >= ZT_LWIP_TCP_TIMER_INTERVAL) {
|
|
|
|
prev_tcp_time = now;
|
|
|
|
lwipstack->tcp_tmr();
|
|
|
|
} else {
|
|
|
|
tcp_remaining = ZT_LWIP_TCP_TIMER_INTERVAL - since_tcp;
|
|
|
|
}
|
2015-09-22 21:43:20 +00:00
|
|
|
if (since_etharp >= ARP_TMR_INTERVAL) {
|
2015-09-22 17:00:00 +00:00
|
|
|
prev_etharp_time = now;
|
|
|
|
lwipstack->etharp_tmr();
|
|
|
|
} else {
|
2015-09-22 21:43:20 +00:00
|
|
|
etharp_remaining = ARP_TMR_INTERVAL - since_etharp;
|
2015-09-22 17:00:00 +00:00
|
|
|
}
|
|
|
|
_phy.poll((unsigned long)std::min(tcp_remaining,etharp_remaining));
|
2015-09-02 22:51:28 +00:00
|
|
|
}
|
2015-09-27 10:08:39 +00:00
|
|
|
//closeAllClients();
|
2015-09-02 22:51:28 +00:00
|
|
|
// TODO: cleanup -- destroy LWIP state, kill any clients, unload .so, etc.
|
|
|
|
}
|
|
|
|
|
2015-09-23 23:45:44 +00:00
|
|
|
void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr)
|
2015-09-11 00:22:35 +00:00
|
|
|
{
|
2015-09-27 10:08:39 +00:00
|
|
|
//fprintf(stderr, "phyOnUnixClose() CLOSING: %d\n", _phy.getDescriptor(sock));
|
|
|
|
//closeClient(sock);
|
|
|
|
// FIXME:
|
2015-09-22 22:44:54 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:23:36 +00:00
|
|
|
/*
|
|
|
|
* Handles data on a client's data buffer. Data is sent to LWIP to be enqueued.
|
|
|
|
*/
|
2015-09-22 22:44:54 +00:00
|
|
|
void NetconEthernetTap::phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable)
|
|
|
|
{
|
2015-09-23 18:23:36 +00:00
|
|
|
if(readable) {
|
2015-09-22 22:44:54 +00:00
|
|
|
int r;
|
2015-09-27 10:08:39 +00:00
|
|
|
TcpConnection *conn = (TcpConnection*)*uptr;
|
|
|
|
if(conn->idx < DEFAULT_READ_BUFFER_SIZE) {
|
2015-09-24 18:33:15 +00:00
|
|
|
int read_fd = _phy.getDescriptor(sock);
|
2015-09-27 10:08:39 +00:00
|
|
|
if((r = read(read_fd, (&conn->buf)+conn->idx, DEFAULT_READ_BUFFER_SIZE-(conn->idx))) > 0) {
|
|
|
|
conn->idx += r;
|
2015-09-24 20:01:19 +00:00
|
|
|
Mutex::Lock _l(lwipstack->_lock);
|
2015-09-27 10:08:39 +00:00
|
|
|
handle_write(conn);
|
2015-09-22 22:44:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 00:22:35 +00:00
|
|
|
}
|
2015-09-11 00:34:48 +00:00
|
|
|
|
2015-09-03 23:05:18 +00:00
|
|
|
// Unused -- no UDP or TCP from this thread/Phy<>
|
2015-09-02 22:51:28 +00:00
|
|
|
void NetconEthernetTap::phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len) {}
|
|
|
|
void NetconEthernetTap::phyOnTcpConnect(PhySocket *sock,void **uptr,bool success) {}
|
|
|
|
void NetconEthernetTap::phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) {}
|
|
|
|
void NetconEthernetTap::phyOnTcpClose(PhySocket *sock,void **uptr) {}
|
|
|
|
void NetconEthernetTap::phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) {}
|
|
|
|
void NetconEthernetTap::phyOnTcpWritable(PhySocket *sock,void **uptr) {}
|
|
|
|
|
2015-09-23 18:23:36 +00:00
|
|
|
/*
|
|
|
|
* Creates a new NetconClient for the accepted RPC connection (unix domain socket)
|
|
|
|
*
|
|
|
|
* Subsequent socket connections from this client will be associated with this
|
|
|
|
* NetconClient object.
|
|
|
|
*/
|
2015-09-02 22:51:28 +00:00
|
|
|
void NetconEthernetTap::phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN)
|
|
|
|
{
|
2015-09-27 10:08:39 +00:00
|
|
|
//fprintf(stderr, "phyOnUnixAccept() NEW CLIENT RPC: %d\n", _phy.getDescriptor(sockN));
|
|
|
|
rpc_sockets.push_back(sockN);
|
2015-09-02 22:51:28 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:23:36 +00:00
|
|
|
/*
|
|
|
|
* Processes incoming data on a client-specific RPC connection
|
|
|
|
*/
|
2015-09-02 22:51:28 +00:00
|
|
|
void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len)
|
|
|
|
{
|
2015-09-10 22:48:45 +00:00
|
|
|
unsigned char *buf = (unsigned char*)data;
|
2015-09-11 00:34:48 +00:00
|
|
|
switch(buf[0])
|
2015-09-10 17:56:01 +00:00
|
|
|
{
|
2015-09-11 00:34:48 +00:00
|
|
|
case RPC_SOCKET:
|
2015-09-23 23:45:44 +00:00
|
|
|
fprintf(stderr, "RPC_SOCKET\n");
|
2015-09-11 00:34:48 +00:00
|
|
|
struct socket_st socket_rpc;
|
|
|
|
memcpy(&socket_rpc, &buf[1], sizeof(struct socket_st));
|
2015-09-27 10:08:39 +00:00
|
|
|
handle_socket(sock, uptr, &socket_rpc);
|
2015-09-11 00:34:48 +00:00
|
|
|
break;
|
|
|
|
case RPC_LISTEN:
|
2015-09-23 23:45:44 +00:00
|
|
|
fprintf(stderr, "RPC_LISTEN\n");
|
2015-09-11 00:34:48 +00:00
|
|
|
struct listen_st listen_rpc;
|
|
|
|
memcpy(&listen_rpc, &buf[1], sizeof(struct listen_st));
|
2015-09-27 10:08:39 +00:00
|
|
|
handle_listen(sock, uptr, &listen_rpc);
|
2015-09-11 00:34:48 +00:00
|
|
|
break;
|
|
|
|
case RPC_BIND:
|
2015-09-23 23:45:44 +00:00
|
|
|
fprintf(stderr, "RPC_BIND\n");
|
2015-09-11 00:34:48 +00:00
|
|
|
struct bind_st bind_rpc;
|
|
|
|
memcpy(&bind_rpc, &buf[1], sizeof(struct bind_st));
|
2015-09-27 10:08:39 +00:00
|
|
|
handle_bind(sock, uptr, &bind_rpc);
|
2015-09-11 00:34:48 +00:00
|
|
|
break;
|
|
|
|
case RPC_KILL_INTERCEPT:
|
2015-09-23 23:45:44 +00:00
|
|
|
fprintf(stderr, "RPC_KILL_INTERCEPT\n");
|
2015-09-27 10:08:39 +00:00
|
|
|
//scloseClient(sock);
|
2015-09-11 00:34:48 +00:00
|
|
|
break;
|
|
|
|
case RPC_CONNECT:
|
2015-09-23 23:45:44 +00:00
|
|
|
fprintf(stderr, "RPC_CONNECT\n");
|
2015-09-11 00:34:48 +00:00
|
|
|
struct connect_st connect_rpc;
|
|
|
|
memcpy(&connect_rpc, &buf[1], sizeof(struct connect_st));
|
2015-09-27 10:08:39 +00:00
|
|
|
handle_connect(sock, uptr, &connect_rpc);
|
2015-09-11 00:34:48 +00:00
|
|
|
break;
|
|
|
|
case RPC_FD_MAP_COMPLETION:
|
2015-09-23 23:45:44 +00:00
|
|
|
fprintf(stderr, "RPC_FD_MAP_COMPLETION\n");
|
2015-09-27 10:08:39 +00:00
|
|
|
handle_retval(sock, uptr, buf);
|
2015-09-11 00:34:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
2015-09-02 22:51:28 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 19:23:26 +00:00
|
|
|
/*
|
|
|
|
* Send a return value to the client for an RPC
|
|
|
|
*/
|
2015-09-27 10:08:39 +00:00
|
|
|
int NetconEthernetTap::send_return_value(TcpConnection *conn, int retval)
|
2015-09-10 17:56:01 +00:00
|
|
|
{
|
|
|
|
char retmsg[4];
|
|
|
|
memset(&retmsg, '\0', sizeof(retmsg));
|
|
|
|
retmsg[0]=RPC_RETVAL;
|
|
|
|
memcpy(&retmsg[1], &retval, sizeof(retval));
|
2015-09-27 10:08:39 +00:00
|
|
|
int n = write(_phy.getDescriptor(conn->rpcSock), &retmsg, sizeof(retmsg));
|
2015-09-10 17:56:01 +00:00
|
|
|
if(n > 0) {
|
2015-09-10 21:44:01 +00:00
|
|
|
// signal that we've satisfied this requirement
|
2015-09-27 10:08:39 +00:00
|
|
|
conn->pending = false;
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "unable to send return value to the intercept\n");
|
2015-09-27 10:08:39 +00:00
|
|
|
closeConnection(conn);
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
--------------------------------- LWIP callbacks -------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
2015-09-24 20:01:19 +00:00
|
|
|
// NOTE: these are called from within LWIP, meaning that lwipstack->_lock is ALREADY
|
|
|
|
// locked in this case!
|
|
|
|
|
2015-09-23 18:23:36 +00:00
|
|
|
/*
|
|
|
|
* Callback from LWIP to do whatever work we might need to do.
|
2015-09-23 19:23:26 +00:00
|
|
|
*
|
|
|
|
* @param associated service state object
|
|
|
|
* @param PCB we're polling on
|
|
|
|
* @return ERR_OK if everything is ok, -1 otherwise
|
|
|
|
*
|
2015-09-23 18:23:36 +00:00
|
|
|
*/
|
2015-09-11 00:22:35 +00:00
|
|
|
err_t NetconEthernetTap::nc_poll(void* arg, struct tcp_pcb *tpcb)
|
|
|
|
{
|
2015-09-24 18:33:15 +00:00
|
|
|
//fprintf(stderr, "nc_poll\n");
|
2015-09-11 00:22:35 +00:00
|
|
|
Larg *l = (Larg*)arg;
|
2015-09-27 10:08:39 +00:00
|
|
|
TcpConnection *conn = l->conn;
|
2015-09-11 00:22:35 +00:00
|
|
|
NetconEthernetTap *tap = l->tap;
|
2015-09-27 10:08:39 +00:00
|
|
|
if(conn && conn->idx) // if valid connection and non-zero index (indicating data present)
|
|
|
|
tap->handle_write(conn);
|
2015-09-11 00:22:35 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
2015-09-10 22:19:43 +00:00
|
|
|
|
2015-09-23 18:23:36 +00:00
|
|
|
/*
|
|
|
|
* Callback from LWIP for when a connection has been accepted and the PCB has been
|
|
|
|
* put into an ACCEPT state.
|
|
|
|
*
|
|
|
|
* A socketpair is created, one end is kept and wrapped into a PhySocket object
|
|
|
|
* for use in the main ZT I/O loop, and one end is sent to the client. The client
|
|
|
|
* is then required to tell the service what new file descriptor it has allocated
|
|
|
|
* for this connection. After the mapping is complete, the accepted socket can be
|
|
|
|
* used.
|
2015-09-23 19:23:26 +00:00
|
|
|
*
|
|
|
|
* @param associated service state object
|
|
|
|
* @param newly allocated PCB
|
|
|
|
* @param error code
|
|
|
|
* @return ERR_OK if everything is ok, -1 otherwise
|
|
|
|
*
|
2015-09-23 18:23:36 +00:00
|
|
|
*/
|
2015-09-11 00:22:35 +00:00
|
|
|
err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
|
|
|
|
{
|
2015-09-27 10:08:39 +00:00
|
|
|
fprintf(stderr, "nc_accept()\n");
|
2015-09-16 00:32:00 +00:00
|
|
|
Larg *l = (Larg*)arg;
|
2015-09-27 10:08:39 +00:00
|
|
|
TcpConnection *conn = l->conn;
|
2015-09-16 00:32:00 +00:00
|
|
|
NetconEthernetTap *tap = l->tap;
|
2015-09-27 10:08:39 +00:00
|
|
|
int larg_fd = tap->_phy.getDescriptor(conn->dataSock);
|
|
|
|
|
|
|
|
if(conn) {
|
2015-09-22 22:44:54 +00:00
|
|
|
ZT_PHY_SOCKFD_TYPE fds[2];
|
|
|
|
socketpair(PF_LOCAL, SOCK_STREAM, 0, fds);
|
2015-09-27 10:08:39 +00:00
|
|
|
|
|
|
|
TcpConnection *new_tcp_conn = new TcpConnection();
|
|
|
|
new_tcp_conn->dataSock = tap->_phy.wrapSocket(fds[0], new_tcp_conn);
|
|
|
|
new_tcp_conn->rpcSock = conn->rpcSock;
|
|
|
|
new_tcp_conn->pcb = newpcb;
|
|
|
|
new_tcp_conn->their_fd = fds[1];
|
|
|
|
tap->tcp_connections.push_back(new_tcp_conn);
|
|
|
|
|
|
|
|
int send_fd = tap->_phy.getDescriptor(conn->rpcSock);
|
|
|
|
|
2015-09-22 21:43:20 +00:00
|
|
|
int n = write(larg_fd, "z", 1);
|
2015-09-16 00:32:00 +00:00
|
|
|
if(n > 0) {
|
2015-09-23 21:55:15 +00:00
|
|
|
if(sock_fd_write(send_fd, fds[1]) > 0) {
|
2015-09-27 10:08:39 +00:00
|
|
|
new_tcp_conn->pending = true;
|
2015-09-24 18:33:15 +00:00
|
|
|
fprintf(stderr, "nc_accept(): socketpair = { our=%d, their=%d}\n", fds[0], fds[1]);
|
2015-09-23 18:23:36 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "nc_accept(%d): unable to send fd to client\n", larg_fd);
|
|
|
|
}
|
2015-09-16 00:32:00 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-09-24 21:33:25 +00:00
|
|
|
fprintf(stderr, "nc_accept(%d): error writing signal byte (send_fd = %d, perceived_fd = %d)\n", larg_fd, send_fd, fds[1]);
|
2015-09-16 00:32:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-09-27 10:08:39 +00:00
|
|
|
tap->lwipstack->_tcp_arg(newpcb, new Larg(tap, new_tcp_conn));
|
2015-09-24 20:01:19 +00:00
|
|
|
tap->lwipstack->_tcp_recv(newpcb, nc_recved);
|
|
|
|
tap->lwipstack->_tcp_err(newpcb, nc_err);
|
|
|
|
tap->lwipstack->_tcp_sent(newpcb, nc_sent);
|
|
|
|
tap->lwipstack->_tcp_poll(newpcb, nc_poll, 1);
|
2015-09-27 10:08:39 +00:00
|
|
|
tcp_accepted(conn->pcb);
|
2015-09-16 00:32:00 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
else {
|
2015-09-23 18:23:36 +00:00
|
|
|
fprintf(stderr, "nc_accept(%d): can't locate Connection object for PCB.\n", larg_fd);
|
2015-09-16 00:32:00 +00:00
|
|
|
}
|
|
|
|
return -1;
|
2015-09-11 00:22:35 +00:00
|
|
|
}
|
2015-09-10 17:56:01 +00:00
|
|
|
|
2015-09-23 18:23:36 +00:00
|
|
|
/*
|
|
|
|
* Callback from LWIP for when data is available to be read from the network.
|
|
|
|
*
|
|
|
|
* Data is in the form of a linked list of struct pbufs, it is then recombined and
|
|
|
|
* send to the client over the associated unix socket.
|
2015-09-23 19:23:26 +00:00
|
|
|
*
|
|
|
|
* @param associated service state object
|
|
|
|
* @param allocated PCB
|
|
|
|
* @param chain of pbufs
|
|
|
|
* @param error code
|
|
|
|
* @return ERR_OK if everything is ok, -1 otherwise
|
|
|
|
*
|
2015-09-23 18:23:36 +00:00
|
|
|
*/
|
2015-09-10 17:56:01 +00:00
|
|
|
err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
|
|
|
|
{
|
2015-09-27 10:08:39 +00:00
|
|
|
fprintf(stderr, "nc_recved()\n");
|
2015-09-11 00:02:13 +00:00
|
|
|
Larg *l = (Larg*)arg;
|
2015-09-10 17:56:01 +00:00
|
|
|
int n;
|
|
|
|
struct pbuf* q = p;
|
2015-09-22 21:43:20 +00:00
|
|
|
|
2015-09-27 10:08:39 +00:00
|
|
|
if(!l->conn) {
|
2015-09-22 21:43:20 +00:00
|
|
|
fprintf(stderr, "nc_recved(): no connection object\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
return ERR_OK; // ?
|
|
|
|
}
|
|
|
|
if(p == NULL) {
|
2015-09-27 10:08:39 +00:00
|
|
|
if(l->conn) {
|
2015-09-23 19:23:26 +00:00
|
|
|
fprintf(stderr, "nc_recved(): closing connection\n");
|
2015-09-27 10:08:39 +00:00
|
|
|
l->tap->closeConnection(l->conn);
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-09-23 18:23:36 +00:00
|
|
|
fprintf(stderr, "nc_recved(): can't locate connection via (arg)\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
q = p;
|
|
|
|
while(p != NULL) { // Cycle through pbufs and write them to the socket
|
|
|
|
if(p->len <= 0)
|
|
|
|
break; // ?
|
2015-09-27 10:08:39 +00:00
|
|
|
if((n = l->tap->_phy.streamSend(l->conn->dataSock,p->payload, p->len)) > 0) {
|
2015-09-10 17:56:01 +00:00
|
|
|
if(n < p->len) {
|
2015-09-23 18:23:36 +00:00
|
|
|
fprintf(stderr, "nc_recved(): unable to write entire pbuf to buffer\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
2015-09-27 10:08:39 +00:00
|
|
|
l->tap->lwipstack->_tcp_recved(tpcb, n); // TODO: would it be more efficient to call this once at the end?
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-09-23 18:23:36 +00:00
|
|
|
fprintf(stderr, "nc_recved(): No data written to intercept buffer\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
p = p->next;
|
|
|
|
}
|
2015-09-27 10:08:39 +00:00
|
|
|
l->tap->lwipstack->_pbuf_free(q); // free pbufs
|
2015-09-10 17:56:01 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-23 18:23:36 +00:00
|
|
|
/*
|
|
|
|
* Callback from LWIP when an internal error is associtated with the given (arg)
|
|
|
|
*
|
|
|
|
* Since the PCB related to this error might no longer exist, only its perviously
|
|
|
|
* associated (arg) is provided to us.
|
2015-09-23 19:23:26 +00:00
|
|
|
*
|
|
|
|
* @param associated service state object
|
|
|
|
* @param error code
|
|
|
|
*
|
2015-09-23 18:23:36 +00:00
|
|
|
*/
|
2015-09-10 17:56:01 +00:00
|
|
|
void NetconEthernetTap::nc_err(void *arg, err_t err)
|
|
|
|
{
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "nc_err\n");
|
2015-09-11 00:02:13 +00:00
|
|
|
Larg *l = (Larg*)arg;
|
2015-09-27 10:08:39 +00:00
|
|
|
if(l->conn) {
|
|
|
|
l->tap->closeConnection(l->conn);
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-09-27 10:08:39 +00:00
|
|
|
fprintf(stderr, "nc_err(): can't locate connection object for PCB\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 18:23:36 +00:00
|
|
|
/*
|
|
|
|
* Callback from LWIP
|
2015-09-23 19:23:26 +00:00
|
|
|
*
|
|
|
|
* This could be used to track the amount of data sent by a connection.
|
|
|
|
*
|
|
|
|
* @param associated service state object
|
|
|
|
* @param relevant PCB
|
|
|
|
* @param length of data sent
|
|
|
|
* @return ERR_OK if everything is ok, -1 otherwise
|
|
|
|
*
|
2015-09-23 18:23:36 +00:00
|
|
|
*/
|
2015-09-10 17:56:01 +00:00
|
|
|
err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *tpcb, u16_t len)
|
|
|
|
{
|
2015-09-27 10:08:39 +00:00
|
|
|
fprintf(stderr, "nc_sent\n");
|
2015-09-24 19:30:07 +00:00
|
|
|
return ERR_OK;
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:23:36 +00:00
|
|
|
/*
|
|
|
|
* Callback from LWIP which sends a return value to the client to signal that
|
|
|
|
* a connection was established for this PCB
|
2015-09-23 19:23:26 +00:00
|
|
|
*
|
|
|
|
* @param associated service state object
|
|
|
|
* @param relevant PCB
|
|
|
|
* @param error code
|
|
|
|
* @return ERR_OK if everything is ok, -1 otherwise
|
|
|
|
*
|
2015-09-23 18:23:36 +00:00
|
|
|
*/
|
2015-09-10 17:56:01 +00:00
|
|
|
err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
|
|
|
|
{
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "nc_connected\n");
|
2015-09-11 00:02:13 +00:00
|
|
|
Larg *l = (Larg*)arg;
|
2015-09-27 10:08:39 +00:00
|
|
|
l->tap->send_return_value(l->conn, err);
|
2015-09-24 19:30:07 +00:00
|
|
|
return ERR_OK;
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
----------------------------- RPC Handler functions ----------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
2015-09-23 18:23:36 +00:00
|
|
|
/*
|
|
|
|
* Handles an RPC to bind an LWIP PCB to a given address and port
|
2015-09-23 19:23:26 +00:00
|
|
|
*
|
|
|
|
* @param Client that is making the RPC
|
|
|
|
* @param structure containing the data and parameters for this client's RPC
|
|
|
|
*
|
2015-09-23 18:23:36 +00:00
|
|
|
*/
|
2015-09-27 10:08:39 +00:00
|
|
|
void NetconEthernetTap::handle_bind(PhySocket *sock, void **uptr, struct bind_st *bind_rpc)
|
2015-09-10 17:56:01 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in *connaddr;
|
|
|
|
connaddr = (struct sockaddr_in *) &bind_rpc->addr;
|
|
|
|
int conn_port = lwipstack->ntohs(connaddr->sin_port);
|
|
|
|
ip_addr_t conn_addr;
|
2015-09-15 19:41:57 +00:00
|
|
|
conn_addr.addr = *((u32_t *)_ips[0].rawIpData());
|
2015-09-27 10:08:39 +00:00
|
|
|
|
|
|
|
TcpConnection *conn = getConnectionByTheirFD(bind_rpc->sockfd);
|
|
|
|
|
|
|
|
if(conn) {
|
|
|
|
if(conn->pcb->state == CLOSED){
|
|
|
|
int err = lwipstack->tcp_bind(conn->pcb, &conn_addr, conn_port);
|
2015-09-10 17:56:01 +00:00
|
|
|
if(err != ERR_OK) {
|
2015-09-23 19:23:26 +00:00
|
|
|
int ip = connaddr->sin_addr.s_addr;
|
|
|
|
unsigned char d[4];
|
|
|
|
d[0] = ip & 0xFF;
|
|
|
|
d[1] = (ip >> 8) & 0xFF;
|
|
|
|
d[2] = (ip >> 16) & 0xFF;
|
|
|
|
d[3] = (ip >> 24) & 0xFF;
|
|
|
|
fprintf(stderr, "handle_bind(): error binding to %d.%d.%d.%d : %d\n", d[0],d[1],d[2],d[3], conn_port);
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-23 19:23:26 +00:00
|
|
|
else fprintf(stderr, "handle_bind(): PCB not in CLOSED state. Ignoring BIND request.\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
2015-09-23 19:23:26 +00:00
|
|
|
else fprintf(stderr, "handle_bind(): can't locate connection for PCB\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 18:23:36 +00:00
|
|
|
/*
|
|
|
|
* Handles an RPC to put an LWIP PCB into LISTEN mode
|
2015-09-23 19:23:26 +00:00
|
|
|
*
|
|
|
|
* @param Client that is making the RPC
|
|
|
|
* @param structure containing the data and parameters for this client's RPC
|
|
|
|
*
|
2015-09-23 18:23:36 +00:00
|
|
|
*/
|
2015-09-27 10:08:39 +00:00
|
|
|
void NetconEthernetTap::handle_listen(PhySocket *sock, void **uptr, struct listen_st *listen_rpc)
|
2015-09-10 17:56:01 +00:00
|
|
|
{
|
2015-09-27 10:08:39 +00:00
|
|
|
TcpConnection *conn = getConnectionByTheirFD(listen_rpc->sockfd);
|
|
|
|
if(conn) {
|
|
|
|
if(conn->pcb->state == LISTEN) {
|
2015-09-23 18:23:36 +00:00
|
|
|
fprintf(stderr, "handle_listen(): PCB is already in listening state.\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-09-27 10:08:39 +00:00
|
|
|
struct tcp_pcb* listening_pcb = lwipstack->tcp_listen(conn->pcb);
|
2015-09-10 17:56:01 +00:00
|
|
|
if(listening_pcb != NULL) {
|
2015-09-27 10:08:39 +00:00
|
|
|
conn->pcb = listening_pcb;
|
2015-09-10 17:56:01 +00:00
|
|
|
lwipstack->tcp_accept(listening_pcb, nc_accept);
|
2015-09-27 10:08:39 +00:00
|
|
|
lwipstack->tcp_arg(listening_pcb, new Larg(this, conn));
|
2015-09-23 18:23:36 +00:00
|
|
|
/* we need to wait for the client to send us the fd allocated on their end
|
|
|
|
for this listening socket */
|
2015-09-27 10:08:39 +00:00
|
|
|
conn->pending = true;
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-09-23 18:23:36 +00:00
|
|
|
fprintf(stderr, "handle_listen(): unable to allocate memory for new listening PCB\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-09-23 18:23:36 +00:00
|
|
|
fprintf(stderr, "handle_listen(): can't locate connection for PCB\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 18:23:36 +00:00
|
|
|
/**
|
|
|
|
* Handles a return value (client's perceived fd) and completes a mapping
|
|
|
|
* so that we know what connection an RPC call should be associated with.
|
2015-09-23 19:23:26 +00:00
|
|
|
*
|
|
|
|
* @param Client that is making the RPC
|
|
|
|
* @param structure containing the data and parameters for this client's RPC
|
|
|
|
*
|
2015-09-23 18:23:36 +00:00
|
|
|
*/
|
2015-09-27 10:08:39 +00:00
|
|
|
void NetconEthernetTap::handle_retval(PhySocket *sock, void **uptr, unsigned char* buf)
|
|
|
|
{
|
|
|
|
TcpConnection *conn = (TcpConnection*)*uptr;
|
|
|
|
if(conn->pending) {
|
|
|
|
memcpy(&(conn->perceived_fd), &buf[1], sizeof(int));
|
|
|
|
fprintf(stderr, "handle_retval(): Mapping [our=%d -> their=%d]\n",
|
|
|
|
_phy.getDescriptor(conn->dataSock), conn->perceived_fd);
|
|
|
|
conn->pending = false;
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 18:23:36 +00:00
|
|
|
/*
|
|
|
|
* Handles an RPC to create a socket (LWIP PCB and associated socketpair)
|
|
|
|
*
|
|
|
|
* A socketpair is created, one end is kept and wrapped into a PhySocket object
|
|
|
|
* for use in the main ZT I/O loop, and one end is sent to the client. The client
|
|
|
|
* is then required to tell the service what new file descriptor it has allocated
|
|
|
|
* for this connection. After the mapping is complete, the socket can be used.
|
2015-09-23 19:23:26 +00:00
|
|
|
*
|
|
|
|
* @param Client that is making the RPC
|
|
|
|
* @param structure containing the data and parameters for this client's RPC
|
|
|
|
*
|
2015-09-23 18:23:36 +00:00
|
|
|
*/
|
2015-09-27 10:08:39 +00:00
|
|
|
void NetconEthernetTap::handle_socket(PhySocket *sock, void **uptr, struct socket_st* socket_rpc)
|
2015-09-10 17:56:01 +00:00
|
|
|
{
|
2015-09-27 10:08:39 +00:00
|
|
|
struct tcp_pcb *newpcb = lwipstack->tcp_new();
|
|
|
|
if(newpcb != NULL) {
|
2015-09-22 22:44:54 +00:00
|
|
|
ZT_PHY_SOCKFD_TYPE fds[2];
|
|
|
|
socketpair(PF_LOCAL, SOCK_STREAM, 0, fds);
|
2015-09-27 10:08:39 +00:00
|
|
|
TcpConnection *new_conn = new TcpConnection();
|
|
|
|
new_conn->dataSock = _phy.wrapSocket(fds[0], new_conn);
|
|
|
|
*uptr = new_conn;
|
|
|
|
new_conn->rpcSock = sock;
|
|
|
|
new_conn->pcb = newpcb;
|
2015-09-24 21:33:25 +00:00
|
|
|
new_conn->their_fd = fds[1];
|
2015-09-27 10:08:39 +00:00
|
|
|
tcp_connections.push_back(new_conn);
|
2015-09-23 18:23:36 +00:00
|
|
|
sock_fd_write(_phy.getDescriptor(sock), fds[1]);
|
2015-09-24 18:33:15 +00:00
|
|
|
fprintf(stderr, "handle_socket(): socketpair = { our=%d, their=%d}\n", fds[0], fds[1]);
|
2015-09-23 18:23:36 +00:00
|
|
|
/* Once the client tells us what its fd is for the other end,
|
|
|
|
we can then complete the mapping */
|
2015-09-27 10:08:39 +00:00
|
|
|
new_conn->pending = true;
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-09-23 18:23:36 +00:00
|
|
|
fprintf(stderr, "handle_socket(): Memory not available for new PCB\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 18:23:36 +00:00
|
|
|
/*
|
|
|
|
* Handles an RPC to connect to a given address and port
|
2015-09-23 19:23:26 +00:00
|
|
|
*
|
|
|
|
* @param Client that is making the RPC
|
|
|
|
* @param structure containing the data and parameters for this client's RPC
|
|
|
|
*
|
2015-09-23 18:23:36 +00:00
|
|
|
*/
|
2015-09-27 10:08:39 +00:00
|
|
|
void NetconEthernetTap::handle_connect(PhySocket *sock, void **uptr, struct connect_st* connect_rpc)
|
2015-09-10 17:56:01 +00:00
|
|
|
{
|
2015-09-27 10:08:39 +00:00
|
|
|
TcpConnection *conn = (TcpConnection*)*uptr;
|
2015-09-10 17:56:01 +00:00
|
|
|
struct sockaddr_in *connaddr;
|
|
|
|
connaddr = (struct sockaddr_in *) &connect_rpc->__addr;
|
|
|
|
int conn_port = lwipstack->ntohs(connaddr->sin_port);
|
|
|
|
ip_addr_t conn_addr = convert_ip((struct sockaddr_in *)&connect_rpc->__addr);
|
|
|
|
|
2015-09-27 10:08:39 +00:00
|
|
|
if(conn != NULL) {
|
|
|
|
lwipstack->tcp_sent(conn->pcb, nc_sent); // FIXME: Move?
|
|
|
|
lwipstack->tcp_recv(conn->pcb, nc_recved);
|
|
|
|
lwipstack->tcp_err(conn->pcb, nc_err);
|
|
|
|
lwipstack->tcp_poll(conn->pcb, nc_poll, APPLICATION_POLL_FREQ);
|
|
|
|
lwipstack->tcp_arg(conn->pcb, new Larg(this, conn));
|
2015-09-10 17:56:01 +00:00
|
|
|
|
|
|
|
int err = 0;
|
2015-09-27 10:08:39 +00:00
|
|
|
if((err = lwipstack->tcp_connect(conn->pcb,&conn_addr,conn_port, nc_connected)) < 0)
|
2015-09-10 17:56:01 +00:00
|
|
|
{
|
2015-09-23 18:23:36 +00:00
|
|
|
fprintf(stderr, "handle_connect(): unable to connect\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
// We should only return a value if failure happens immediately
|
|
|
|
// Otherwise, we still need to wait for a callback from lwIP.
|
|
|
|
// - This is because an ERR_OK from tcp_connect() only verifies
|
|
|
|
// that the SYN packet was enqueued onto the stack properly,
|
|
|
|
// that's it!
|
|
|
|
// - Most instances of a retval for a connect() should happen
|
|
|
|
// in the nc_connect() and nc_err() callbacks!
|
2015-09-27 10:08:39 +00:00
|
|
|
send_return_value(conn, err);
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
// Everything seems to be ok, but we don't have enough info to retval
|
2015-09-27 10:08:39 +00:00
|
|
|
conn->pending=true;
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "could not locate PCB based on their fd\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 18:23:36 +00:00
|
|
|
/*
|
|
|
|
* Writes data pulled from the client's socket buffer to LWIP. This merely sends the
|
|
|
|
* data to LWIP to be enqueued and eventually sent to the network.
|
2015-09-23 19:23:26 +00:00
|
|
|
* *
|
|
|
|
* @param Client that is making the RPC
|
|
|
|
* @param structure containing the data and parameters for this client's RPC
|
2015-09-23 18:23:36 +00:00
|
|
|
*
|
|
|
|
* TODO: Optimize write logic (should we stop using poll?)
|
|
|
|
*/
|
2015-09-27 10:08:39 +00:00
|
|
|
void NetconEthernetTap::handle_write(TcpConnection *conn)
|
2015-09-10 17:56:01 +00:00
|
|
|
{
|
2015-09-27 10:08:39 +00:00
|
|
|
if(conn) {
|
|
|
|
int sndbuf = conn->pcb->snd_buf;
|
2015-09-10 17:56:01 +00:00
|
|
|
float avail = (float)sndbuf;
|
|
|
|
float max = (float)TCP_SND_BUF;
|
|
|
|
float load = 1.0 - (avail / max);
|
|
|
|
|
|
|
|
if(load >= 0.9) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-24 18:33:15 +00:00
|
|
|
|
2015-09-27 10:08:39 +00:00
|
|
|
int sz, write_allowance = sndbuf < conn->idx ? sndbuf : conn->idx;
|
2015-09-10 20:52:18 +00:00
|
|
|
if(write_allowance > 0) {
|
2015-09-24 20:01:19 +00:00
|
|
|
// NOTE: this assumes that lwipstack->_lock is locked, either
|
|
|
|
// because we are in a callback or have locked it manually.
|
2015-09-27 10:08:39 +00:00
|
|
|
int err = lwipstack->_tcp_write(conn->pcb, &conn->buf, write_allowance, TCP_WRITE_FLAG_COPY);
|
2015-09-10 17:56:01 +00:00
|
|
|
if(err != ERR_OK) {
|
2015-09-23 18:23:36 +00:00
|
|
|
fprintf(stderr, "handle_write(): error while writing to PCB\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
2015-09-27 10:08:39 +00:00
|
|
|
sz = (conn->idx)-write_allowance;
|
2015-09-10 17:56:01 +00:00
|
|
|
if(sz) {
|
2015-09-27 10:08:39 +00:00
|
|
|
memmove(&conn->buf, (conn->buf+write_allowance), sz);
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
2015-09-27 10:08:39 +00:00
|
|
|
conn->idx -= write_allowance;
|
2015-09-10 17:56:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-09-23 18:23:36 +00:00
|
|
|
fprintf(stderr, "handle_write(): LWIP stack full\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-09-21 23:39:26 +00:00
|
|
|
fprintf(stderr, "handle_write(): could not locate connection for this fd\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 22:17:38 +00:00
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif // ZT_ENABLE_NETCON
|