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-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-10 17:56:01 +00:00
|
|
|
lwipstack = new LWIPStack("/root/dev/netcon/liblwip.so");
|
|
|
|
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-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()) {
|
|
|
|
Mutex::Lock _l2(_arp_m);
|
2015-09-11 19:26:39 +00:00
|
|
|
_arp.addLocal((uint32_t)(reinterpret_cast<const struct sockaddr_in *>(&ip)->sin_addr.s_addr),_mac);
|
2015-09-11 19:12:45 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 23:05:18 +00:00
|
|
|
// TODO: alloc IP in LWIP
|
2015-09-15 18:15:59 +00:00
|
|
|
fprintf(stderr, "ASSIGNING IP = %s\n", ip.toIpString().c_str());
|
2015-09-03 23:05:18 +00:00
|
|
|
}
|
2015-09-11 00:02:13 +00:00
|
|
|
return true; // TODO: what is exapected?
|
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()) {
|
|
|
|
Mutex::Lock _l2(_arp_m);
|
2015-09-11 19:26:39 +00:00
|
|
|
_arp.remove((uint32_t)(reinterpret_cast<const struct sockaddr_in *>(&ip)->sin_addr.s_addr));
|
2015-09-11 19:12:45 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 23:05:18 +00:00
|
|
|
// TODO: dealloc IP from LWIP
|
|
|
|
|
|
|
|
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-02 22:51:28 +00:00
|
|
|
if (!_enabled)
|
|
|
|
return;
|
2015-09-11 19:12:45 +00:00
|
|
|
|
2015-09-15 19:41:57 +00:00
|
|
|
fprintf(stderr, "put\n");
|
|
|
|
/*
|
2015-09-11 19:12:45 +00:00
|
|
|
if (etherType == ZT_ETHERTYPE_ARP) {
|
|
|
|
char arpReplyBuf[ZT_ARP_BUF_LENGTH];
|
|
|
|
unsigned int arpReplyLen = 0;
|
|
|
|
MAC arpReplyDest;
|
|
|
|
|
|
|
|
Mutex::Lock _l2(_arp_m);
|
|
|
|
_arp.processIncomingArp(data,len,arpReplyBuf,arpReplyLen,arpReplyDest);
|
|
|
|
if (arpReplyLen > 0)
|
2015-09-15 18:15:59 +00:00
|
|
|
fprintf(stderr, "ARP reply generated\n");
|
2015-09-11 19:12:45 +00:00
|
|
|
_handler(_arg,_nwid,_mac,from,ZT_ETHERTYPE_ARP,0,arpReplyBuf,arpReplyLen);
|
2015-09-15 18:15:59 +00:00
|
|
|
}
|
2015-09-15 19:41:57 +00:00
|
|
|
*/
|
2015-09-15 18:15:59 +00:00
|
|
|
// Copy data into a pbuf chain
|
2015-09-11 19:26:39 +00:00
|
|
|
struct pbuf *p, *q;
|
2015-09-15 18:15:59 +00:00
|
|
|
//u16_t len;
|
2015-09-15 22:27:52 +00:00
|
|
|
//char buf[1514];
|
|
|
|
const char *bufptr;
|
|
|
|
// Assemble ethernet header and call netif->output
|
|
|
|
struct eth_hdr *ethhdr = NULL;
|
2015-09-11 19:26:39 +00:00
|
|
|
|
2015-09-15 18:15:59 +00:00
|
|
|
// We allocate a pbuf chain of pbufs from the pool.
|
2015-09-15 22:27:52 +00:00
|
|
|
p = lwipstack->pbuf_alloc(PBUF_RAW, len+sizeof(struct eth_hdr), PBUF_POOL);
|
2015-09-11 19:26:39 +00:00
|
|
|
|
2015-09-15 18:15:59 +00:00
|
|
|
if(p != NULL) {
|
2015-09-15 22:27:52 +00:00
|
|
|
fprintf(stderr, "p != NULL\n");
|
2015-09-15 18:15:59 +00:00
|
|
|
/* We iterate over the pbuf chain until we have read the entire
|
|
|
|
packet into the pbuf. */
|
2015-09-15 22:27:52 +00:00
|
|
|
bufptr = (const char *)data;
|
2015-09-11 19:26:39 +00:00
|
|
|
for(q = p; q != NULL; q = q->next) {
|
2015-09-15 18:15:59 +00:00
|
|
|
/* Read enough bytes to fill this pbuf in the chain. The
|
|
|
|
available data in the pbuf is given by the q->len
|
|
|
|
variable. */
|
|
|
|
/* read data into(q->payload, q->len); */
|
2015-09-15 22:27:52 +00:00
|
|
|
char *pload = (char*)q->payload;
|
|
|
|
int plen = q->len;
|
|
|
|
if (!ethhdr) {
|
|
|
|
ethhdr = (struct eth_hdr *)p->payload;
|
|
|
|
pload += sizeof(struct eth_hdr);
|
|
|
|
plen -= sizeof(struct eth_hdr);
|
|
|
|
}
|
|
|
|
memcpy(pload, bufptr, plen);
|
|
|
|
bufptr += plen;
|
2015-09-15 18:15:59 +00:00
|
|
|
}
|
|
|
|
/* acknowledge that packet has been read(); */
|
|
|
|
} else {
|
2015-09-15 22:27:52 +00:00
|
|
|
return;
|
2015-09-15 18:15:59 +00:00
|
|
|
/* drop packet(); */
|
|
|
|
}
|
|
|
|
|
|
|
|
from.copyTo(ethhdr->src.addr, 6);
|
|
|
|
_mac.copyTo(ethhdr->dest.addr, 6);
|
2015-09-15 22:27:52 +00:00
|
|
|
ethhdr->type = Utils::hton((uint16_t)etherType);
|
|
|
|
|
|
|
|
fprintf(stderr, "from = %s\n", from.toString().c_str());
|
|
|
|
fprintf(stderr, "_mac = %s\n", _mac.toString().c_str());
|
|
|
|
fprintf(stderr, "ethhdr->type = %x\n", ethhdr->type);
|
|
|
|
fprintf(stderr, "ethhdr->type = %x\n", ethhdr->type);
|
2015-09-15 18:15:59 +00:00
|
|
|
|
2015-09-15 19:41:57 +00:00
|
|
|
if(interface.input(p, &interface) != ERR_OK) {
|
2015-09-15 18:15:59 +00:00
|
|
|
fprintf(stderr, "IP error (netif->input)\n");
|
|
|
|
}
|
2015-09-15 19:41:57 +00:00
|
|
|
else {
|
|
|
|
fprintf(stderr, "interface.input(...) len = %d\n", len);
|
|
|
|
}
|
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 22:22:41 +00:00
|
|
|
fprintf(stderr, "scanMulticastGroups\n");
|
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
|
|
|
|
|
|
|
NetconConnection *NetconEthernetTap::getConnectionByPCB(struct tcp_pcb *pcb)
|
|
|
|
{
|
|
|
|
NetconConnection *c;
|
2015-09-10 22:48:45 +00:00
|
|
|
for(size_t i=0; i<clients.size(); i++) {
|
2015-09-10 21:44:01 +00:00
|
|
|
c = clients[i]->containsPCB(pcb);
|
|
|
|
if(c) {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
2015-09-10 22:19:43 +00:00
|
|
|
return NULL;
|
2015-09-10 21:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NetconConnection *NetconEthernetTap::getConnectionByThisFD(int fd)
|
|
|
|
{
|
2015-09-10 22:48:45 +00:00
|
|
|
for(size_t i=0; i<clients.size(); i++) {
|
|
|
|
for(size_t j=0; j<clients[i]->connections.size(); j++) {
|
|
|
|
if(_phy.getDescriptor(clients[i]->connections[j]->sock) == fd) {
|
|
|
|
return clients[i]->connections[j];
|
|
|
|
}
|
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 22:19:43 +00:00
|
|
|
NetconClient *NetconEthernetTap::getClientByPCB(struct tcp_pcb *pcb)
|
|
|
|
{
|
2015-09-10 22:48:45 +00:00
|
|
|
for(size_t i=0; i<clients.size(); i++) {
|
|
|
|
if(clients[i]->containsPCB(pcb)) {
|
2015-09-10 22:19:43 +00:00
|
|
|
return clients[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-10 21:44:01 +00:00
|
|
|
void NetconEthernetTap::closeClient(NetconClient *client)
|
|
|
|
{
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "closeClient\n");
|
2015-09-11 18:12:27 +00:00
|
|
|
NetconConnection *temp_conn;
|
|
|
|
closeConnection(client->rpc);
|
|
|
|
for(size_t i=0; i<client->connections.size(); i++) {
|
|
|
|
temp_conn = client->connections[i];
|
|
|
|
closeConnection(client->connections[i]);
|
|
|
|
delete temp_conn;
|
|
|
|
}
|
|
|
|
delete client;
|
2015-09-10 21:44:01 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 18:00:42 +00:00
|
|
|
void NetconEthernetTap::closeConnection(NetconConnection *conn)
|
|
|
|
{
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "closeConnection\n");
|
2015-09-11 18:00:42 +00:00
|
|
|
NetconClient *client = conn->owner;
|
|
|
|
_phy.close(conn->sock);
|
|
|
|
lwipstack->tcp_close(conn->pcb);
|
|
|
|
client->removeConnection(conn->sock);
|
|
|
|
}
|
|
|
|
|
2015-09-10 21:44:01 +00:00
|
|
|
|
2015-09-15 18:15:59 +00:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
------------------------ low-level Interface functions -------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
2015-09-02 22:17:38 +00:00
|
|
|
void NetconEthernetTap::threadMain()
|
|
|
|
throw()
|
2015-09-02 22:51:28 +00:00
|
|
|
{
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "starting threadMain()\n");
|
2015-09-15 18:15:59 +00:00
|
|
|
|
2015-09-10 17:56:01 +00:00
|
|
|
static ip_addr_t ipaddr, netmask, gw;
|
|
|
|
char ip_str[16] = {0}, nm_str[16] = {0}, gw_str[16] = {0};
|
2015-09-15 18:15:59 +00:00
|
|
|
|
|
|
|
if(_ips.size() == 0) {
|
|
|
|
fprintf(stderr, "no IP assigned. Exiting.\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
IP4_ADDR(&gw,0,0,0,0);
|
|
|
|
ipaddr.addr = *((u32_t *)_ips[0].rawIpData());
|
|
|
|
netmask.addr = *((u32_t *)_ips[0].netmask().rawIpData());
|
|
|
|
|
2015-09-10 17:56:01 +00:00
|
|
|
strncpy(ip_str, lwipstack->ipaddr_ntoa(&ipaddr), sizeof(ip_str));
|
|
|
|
strncpy(nm_str, lwipstack->ipaddr_ntoa(&netmask), sizeof(nm_str));
|
|
|
|
strncpy(gw_str, lwipstack->ipaddr_ntoa(&gw), sizeof(gw_str));
|
|
|
|
|
2015-09-15 18:15:59 +00:00
|
|
|
fprintf(stderr, "ip_str = %s\n", ip_str);
|
|
|
|
fprintf(stderr, "nm_str = %s\n", nm_str);
|
|
|
|
fprintf(stderr, "gw_str = %s\n", gw_str);
|
|
|
|
|
2015-09-10 17:56:01 +00:00
|
|
|
unsigned long tcp_time = ARP_TMR_INTERVAL / 5000;
|
|
|
|
unsigned long etharp_time = IP_TMR_INTERVAL / 1000;
|
|
|
|
unsigned long prev_tcp_time = 0;
|
|
|
|
unsigned long prev_etharp_time = 0;
|
|
|
|
unsigned long curr_time;
|
|
|
|
unsigned long since_tcp;
|
|
|
|
unsigned long since_etharp;
|
|
|
|
struct timeval tv;
|
2015-09-02 22:51:28 +00:00
|
|
|
|
2015-09-15 19:41:57 +00:00
|
|
|
fprintf(stderr, "initializing interface\n");
|
|
|
|
|
2015-09-15 18:15:59 +00:00
|
|
|
/* set up the faux-netif for LWIP's sake */
|
|
|
|
fprintf(stderr, "netif_add\n");
|
|
|
|
lwipstack->netif_add(&interface,&ipaddr, &netmask, &gw, NULL, tapif_init, lwipstack->ethernet_input);
|
|
|
|
|
|
|
|
interface.state = this;
|
2015-09-15 22:27:52 +00:00
|
|
|
interface.output = lwipstack->etharp_output;
|
|
|
|
_mac.copyTo(interface.hwaddr, 6);
|
|
|
|
interface.mtu = 2800;
|
|
|
|
|
|
|
|
/*
|
2015-09-15 18:15:59 +00:00
|
|
|
interface.name[0] = 't';
|
|
|
|
interface.name[1] = 'p';
|
|
|
|
interface.output = lwipstack->etharp_output;
|
|
|
|
interface.linkoutput = low_level_output;
|
2015-09-15 22:27:52 +00:00
|
|
|
|
2015-09-15 18:15:59 +00:00
|
|
|
interface.hwaddr_len = 6;
|
2015-09-15 22:27:52 +00:00
|
|
|
|
2015-09-15 18:15:59 +00:00
|
|
|
interface.flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
|
2015-09-15 22:27:52 +00:00
|
|
|
*/
|
2015-09-15 18:15:59 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "netif_set_default\n");
|
|
|
|
lwipstack->netif_set_default(&interface);
|
|
|
|
fprintf(stderr, "netif_set_up\n");
|
|
|
|
lwipstack->netif_set_up(&interface);
|
|
|
|
fprintf(stderr, "complete\n");
|
|
|
|
|
|
|
|
|
2015-09-10 17:56:01 +00:00
|
|
|
while (_run) {
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
curr_time = (unsigned long)(tv.tv_sec) * 1000 + (unsigned long)(tv.tv_usec) / 1000;
|
|
|
|
since_tcp = curr_time - prev_tcp_time;
|
|
|
|
since_etharp = curr_time - prev_etharp_time;
|
|
|
|
int min_time = min(since_tcp, since_etharp) * 1000; // usec
|
|
|
|
|
|
|
|
if(since_tcp > tcp_time)
|
|
|
|
{
|
|
|
|
prev_tcp_time = curr_time+1;
|
2015-09-15 22:27:52 +00:00
|
|
|
//fprintf(stderr, "tcp_tmr\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
lwipstack->tcp_tmr();
|
|
|
|
}
|
|
|
|
if(since_etharp > etharp_time)
|
|
|
|
{
|
|
|
|
prev_etharp_time = curr_time;
|
2015-09-15 22:27:52 +00:00
|
|
|
//fprintf(stderr, "etharp_tmr\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
lwipstack->etharp_tmr();
|
|
|
|
}
|
2015-09-15 22:27:52 +00:00
|
|
|
//fprintf(stderr, "_run\n");
|
|
|
|
//lwipstack->netif_poll(&interface);
|
2015-09-15 18:15:59 +00:00
|
|
|
_phy.poll(100); // conversion from usec to millisec, TODO: double check
|
|
|
|
|
2015-09-02 22:51:28 +00:00
|
|
|
}
|
|
|
|
// TODO: cleanup -- destroy LWIP state, kill any clients, unload .so, etc.
|
|
|
|
}
|
|
|
|
|
2015-09-11 00:22:35 +00:00
|
|
|
|
|
|
|
void NetconEthernetTap::phyOnSocketPairEndpointClose(PhySocket *sock, void **uptr)
|
|
|
|
{
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "phyOnSocketPairEndpointClose\n");
|
2015-09-11 23:02:44 +00:00
|
|
|
_phy.setNotifyWritable(sock, false);
|
2015-09-11 00:34:48 +00:00
|
|
|
NetconClient *client = (NetconClient*)*uptr;
|
2015-09-11 18:00:42 +00:00
|
|
|
closeConnection(client->getConnection(sock));
|
2015-09-11 00:22:35 +00:00
|
|
|
}
|
2015-09-11 00:34:48 +00:00
|
|
|
|
2015-09-11 00:22:35 +00:00
|
|
|
void NetconEthernetTap::phyOnSocketPairEndpointData(PhySocket *sock, void **uptr, void *buf, unsigned long n)
|
|
|
|
{
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "phyOnSocketPairEndpointData\n");
|
2015-09-11 00:34:48 +00:00
|
|
|
int r;
|
|
|
|
NetconConnection *c = ((NetconClient*)*uptr)->getConnection(sock);
|
|
|
|
if(c) {
|
|
|
|
if(c->idx < DEFAULT_READ_BUFFER_SIZE) {
|
|
|
|
if((r = read(_phy.getDescriptor(c->sock), (&c->buf)+c->idx, DEFAULT_READ_BUFFER_SIZE-(c->idx))) > 0) {
|
|
|
|
c->idx += r;
|
|
|
|
handle_write(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 00:22:35 +00:00
|
|
|
}
|
2015-09-11 00:34:48 +00:00
|
|
|
|
2015-09-11 00:22:35 +00:00
|
|
|
void NetconEthernetTap::phyOnSocketPairEndpointWritable(PhySocket *sock, void **uptr)
|
|
|
|
{
|
2015-09-11 18:00:42 +00:00
|
|
|
//_phy.setNotifyWritable(sock, false);
|
2015-09-11 00:22:35 +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) {}
|
2015-09-10 17:56:01 +00:00
|
|
|
|
2015-09-02 22:51:28 +00:00
|
|
|
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) {}
|
|
|
|
|
|
|
|
void NetconEthernetTap::phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN)
|
|
|
|
{
|
2015-09-11 23:02:44 +00:00
|
|
|
fprintf(stderr, "phyOnUnixAccept\n");
|
2015-09-10 21:44:01 +00:00
|
|
|
NetconClient *newClient = new NetconClient();
|
2015-09-11 23:02:44 +00:00
|
|
|
newClient->rpc = newClient->addConnection(RPC, sockN);
|
|
|
|
*uptrN = newClient;
|
2015-09-02 22:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr)
|
|
|
|
{
|
2015-09-11 23:02:44 +00:00
|
|
|
_phy.setNotifyWritable(sock, false);
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "phyOnUnixClose\n");
|
2015-09-11 18:47:16 +00:00
|
|
|
closeClient(((NetconClient*)*uptr));
|
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
|
|
|
NetconClient *client = (NetconClient*)*uptr;
|
2015-09-11 23:02:44 +00:00
|
|
|
if(!client)
|
|
|
|
fprintf(stderr, "!client\n");
|
|
|
|
|
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-11 22:22:41 +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));
|
|
|
|
client->tid = socket_rpc.__tid;
|
|
|
|
handle_socket(client, &socket_rpc);
|
|
|
|
break;
|
|
|
|
case RPC_LISTEN:
|
2015-09-11 22:22:41 +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));
|
|
|
|
client->tid = listen_rpc.__tid;
|
|
|
|
handle_listen(client, &listen_rpc);
|
|
|
|
break;
|
|
|
|
case RPC_BIND:
|
2015-09-11 22:22:41 +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));
|
|
|
|
client->tid = bind_rpc.__tid;
|
|
|
|
handle_bind(client, &bind_rpc);
|
|
|
|
break;
|
|
|
|
case RPC_KILL_INTERCEPT:
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "RPC_KILL_INTERCEPT\n");
|
2015-09-11 18:47:16 +00:00
|
|
|
closeClient(client);
|
2015-09-11 00:34:48 +00:00
|
|
|
break;
|
|
|
|
case RPC_CONNECT:
|
2015-09-11 22:22:41 +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));
|
|
|
|
client->tid = connect_rpc.__tid;
|
|
|
|
handle_connect(client, &connect_rpc);
|
|
|
|
break;
|
|
|
|
case RPC_FD_MAP_COMPLETION:
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "RPC_FD_MAP_COMPLETION\n");
|
2015-09-11 00:34:48 +00:00
|
|
|
handle_retval(client, buf);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
2015-09-02 22:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetconEthernetTap::phyOnUnixWritable(PhySocket *sock,void **uptr)
|
2015-09-02 22:17:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-09-10 21:44:01 +00:00
|
|
|
int NetconEthernetTap::send_return_value(NetconClient *client, int retval)
|
2015-09-10 17:56:01 +00:00
|
|
|
{
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "send_return_value\n");
|
2015-09-10 21:44:01 +00:00
|
|
|
if(!client->waiting_for_retval){
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "intercept isn't waiting for return value. Why are we here?\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
char retmsg[4];
|
|
|
|
memset(&retmsg, '\0', sizeof(retmsg));
|
|
|
|
retmsg[0]=RPC_RETVAL;
|
|
|
|
memcpy(&retmsg[1], &retval, sizeof(retval));
|
2015-09-10 21:44:01 +00:00
|
|
|
int n = write(_phy.getDescriptor(client->rpc->sock), &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
|
|
|
|
client->waiting_for_retval = 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-10 21:44:01 +00:00
|
|
|
closeClient(client);
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
--------------------------------- LWIP callbacks -------------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
2015-09-11 00:22:35 +00:00
|
|
|
err_t NetconEthernetTap::nc_poll(void* arg, struct tcp_pcb *tpcb)
|
|
|
|
{
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "nc_poll\n");
|
2015-09-11 00:22:35 +00:00
|
|
|
Larg *l = (Larg*)arg;
|
|
|
|
NetconConnection *c = l->tap->getConnectionByPCB(tpcb);
|
|
|
|
NetconEthernetTap *tap = l->tap;
|
|
|
|
if(c)
|
|
|
|
tap->handle_write(c);
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
2015-09-10 22:19:43 +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-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "nc_accept\n");
|
2015-09-11 00:22:35 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
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-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "nc_recved\n");
|
2015-09-11 00:02:13 +00:00
|
|
|
Larg *l = (Larg*)arg;
|
|
|
|
NetconConnection *c = l->tap->getConnectionByPCB(tpcb);
|
|
|
|
NetconEthernetTap *tap = l->tap;
|
|
|
|
|
2015-09-10 17:56:01 +00:00
|
|
|
int n;
|
|
|
|
struct pbuf* q = p;
|
2015-09-11 00:02:13 +00:00
|
|
|
int our_fd = tap->_phy.getDescriptor(c->sock);
|
2015-09-10 22:19:43 +00:00
|
|
|
|
2015-09-11 00:34:48 +00:00
|
|
|
if(!c) {
|
2015-09-10 17:56:01 +00:00
|
|
|
return ERR_OK; // ?
|
|
|
|
}
|
|
|
|
if(p == NULL) {
|
|
|
|
if(c) {
|
|
|
|
nc_close(tpcb);
|
2015-09-11 00:34:48 +00:00
|
|
|
close(our_fd); // TODO: Check logic
|
2015-09-11 18:00:42 +00:00
|
|
|
tap->closeConnection(c);
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "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-10 22:19:43 +00:00
|
|
|
if((n = write(our_fd, p->payload, p->len)) > 0) {
|
2015-09-10 17:56:01 +00:00
|
|
|
if(n < p->len) {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "ERROR: unable to write entire pbuf to buffer\n");
|
2015-09-11 18:00:42 +00:00
|
|
|
//tap->_phy.setNotifyWritable(l->sock, true);
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
2015-09-11 00:02:13 +00:00
|
|
|
tap->lwipstack->tcp_recved(tpcb, n);
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "Error: No data written to intercept buffer\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
p = p->next;
|
|
|
|
}
|
2015-09-11 00:02:13 +00:00
|
|
|
tap->lwipstack->pbuf_free(q); // free pbufs
|
2015-09-10 17:56:01 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
NetconEthernetTap *tap = l->tap;
|
|
|
|
NetconConnection *c = tap->getConnectionByThisFD(tap->_phy.getDescriptor(l->sock));
|
2015-09-10 17:56:01 +00:00
|
|
|
if(c) {
|
2015-09-11 18:12:27 +00:00
|
|
|
tap->closeConnection(c);
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "can't locate connection object for PCB\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetconEthernetTap::nc_close(struct tcp_pcb* tpcb)
|
|
|
|
{
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "nc_close\n");
|
2015-09-11 18:47:16 +00:00
|
|
|
//closeConnection(getConnectionByPCB(tpcb));
|
2015-09-11 00:02:13 +00:00
|
|
|
/*
|
2015-09-10 17:56:01 +00:00
|
|
|
lwipstack->tcp_arg(tpcb, NULL);
|
|
|
|
lwipstack->tcp_sent(tpcb, NULL);
|
|
|
|
lwipstack->tcp_recv(tpcb, NULL);
|
|
|
|
lwipstack->tcp_err(tpcb, NULL);
|
|
|
|
lwipstack->tcp_poll(tpcb, NULL, 0);
|
2015-09-10 21:44:01 +00:00
|
|
|
lwipstack->tcp_close(tpcb);
|
2015-09-11 18:47:16 +00:00
|
|
|
*/
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err_t NetconEthernetTap::nc_send(struct tcp_pcb *tpcb)
|
|
|
|
{
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "nc_send\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *tpcb, u16_t len)
|
|
|
|
{
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "nc_sent\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
NetconEthernetTap *tap = l->tap;
|
|
|
|
for(size_t i=0; i<tap->clients.size(); i++) {
|
|
|
|
if(tap->clients[i]->containsPCB(tpcb)) {
|
|
|
|
tap->send_return_value(tap->clients[i],err);
|
2015-09-10 21:44:01 +00:00
|
|
|
}
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-09-11 00:22:35 +00:00
|
|
|
|
|
|
|
|
2015-09-10 17:56:01 +00:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
----------------------------- RPC Handler functions ----------------------------
|
|
|
|
------------------------------------------------------------------------------*/
|
|
|
|
|
2015-09-10 20:52:18 +00:00
|
|
|
void NetconEthernetTap::handle_bind(NetconClient *client, struct bind_st *bind_rpc)
|
2015-09-10 17:56:01 +00:00
|
|
|
{
|
|
|
|
// FIXME: Is this hack still needed?
|
|
|
|
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
|
|
|
//IP4_ADDR(&conn_addr, 192,168,0,2);
|
|
|
|
conn_addr.addr = *((u32_t *)_ips[0].rawIpData());
|
|
|
|
|
2015-09-10 17:56:01 +00:00
|
|
|
|
2015-09-11 00:02:13 +00:00
|
|
|
/*
|
2015-09-10 17:56:01 +00:00
|
|
|
int ip = connaddr->sin_addr.s_addr;
|
|
|
|
unsigned char bytes[4];
|
|
|
|
bytes[0] = ip & 0xFF;
|
|
|
|
bytes[1] = (ip >> 8) & 0xFF;
|
|
|
|
bytes[2] = (ip >> 16) & 0xFF;
|
|
|
|
bytes[3] = (ip >> 24) & 0xFF;
|
2015-09-11 00:02:13 +00:00
|
|
|
"binding to: %d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]
|
|
|
|
*/
|
2015-09-15 19:41:57 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "PORT = %d\n", conn_port);
|
2015-09-11 00:02:13 +00:00
|
|
|
NetconConnection *c = client->getConnectionByTheirFD(bind_rpc->sockfd);
|
2015-09-10 20:52:18 +00:00
|
|
|
if(c) {
|
2015-09-10 17:56:01 +00:00
|
|
|
if(c->pcb->state == CLOSED){
|
|
|
|
int err = lwipstack->tcp_bind(c->pcb, &conn_addr, conn_port);
|
|
|
|
if(err != ERR_OK) {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "error while binding to addr/port\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "bind successful\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "PCB not in CLOSED state. Ignoring BIND request.\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "can't locate connection for PCB\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 20:52:18 +00:00
|
|
|
void NetconEthernetTap::handle_listen(NetconClient *client, struct listen_st *listen_rpc)
|
2015-09-10 17:56:01 +00:00
|
|
|
{
|
2015-09-10 20:52:18 +00:00
|
|
|
NetconConnection *c = client->getConnectionByTheirFD(listen_rpc->sockfd);
|
2015-09-10 17:56:01 +00:00
|
|
|
if(c) {
|
|
|
|
if(c->pcb->state == LISTEN) {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "PCB is already in listening state.\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct tcp_pcb* listening_pcb = lwipstack->tcp_listen(c->pcb);
|
|
|
|
if(listening_pcb != NULL) {
|
|
|
|
c->pcb = listening_pcb;
|
|
|
|
lwipstack->tcp_accept(listening_pcb, nc_accept);
|
2015-09-11 00:02:13 +00:00
|
|
|
lwipstack->tcp_arg(listening_pcb, new Larg(this, c->sock));
|
2015-09-10 20:52:18 +00:00
|
|
|
client->waiting_for_retval=true;
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "unable to allocate memory for new listening PCB\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "can't locate connection for PCB\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 20:52:18 +00:00
|
|
|
void NetconEthernetTap::handle_retval(NetconClient *client, unsigned char* buf)
|
2015-09-10 17:56:01 +00:00
|
|
|
{
|
2015-09-10 20:52:18 +00:00
|
|
|
if(client->unmapped_conn != NULL) {
|
|
|
|
memcpy(&(client->unmapped_conn->their_fd), &buf[1], sizeof(int));
|
2015-09-11 23:29:35 +00:00
|
|
|
client->connections.push_back(client->unmapped_conn);
|
2015-09-10 21:44:01 +00:00
|
|
|
client->unmapped_conn = NULL;
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 20:52:18 +00:00
|
|
|
void NetconEthernetTap::handle_socket(NetconClient *client, struct socket_st* socket_rpc)
|
2015-09-10 17:56:01 +00:00
|
|
|
{
|
|
|
|
struct tcp_pcb *pcb = lwipstack->tcp_new();
|
|
|
|
if(pcb != NULL) {
|
2015-09-11 23:02:44 +00:00
|
|
|
int their_fd;
|
|
|
|
NetconConnection *new_conn = client->addConnection(BUFFER, _phy.createSocketPair(their_fd, client));
|
|
|
|
new_conn->their_fd = their_fd;
|
2015-09-10 20:52:18 +00:00
|
|
|
new_conn->pcb = pcb;
|
2015-09-11 23:02:44 +00:00
|
|
|
PhySocket *sock = client->rpc->sock;
|
|
|
|
int send_fd = _phy.getDescriptor(sock);
|
|
|
|
sock_fd_write(send_fd, their_fd);
|
2015-09-10 21:44:01 +00:00
|
|
|
client->unmapped_conn = new_conn;
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "Memory not available for new PCB\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 20:52:18 +00:00
|
|
|
void NetconEthernetTap::handle_connect(NetconClient *client, struct connect_st* connect_rpc)
|
2015-09-10 17:56:01 +00:00
|
|
|
{
|
|
|
|
// FIXME: Parse out address information -- Probably a more elegant way to do this
|
|
|
|
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-11 00:22:35 +00:00
|
|
|
|
2015-09-11 23:02:44 +00:00
|
|
|
fprintf(stderr, "getConnectionByTheirFD(%d)\n", connect_rpc->__fd);
|
2015-09-10 20:52:18 +00:00
|
|
|
NetconConnection *c = client->getConnectionByTheirFD(connect_rpc->__fd);
|
2015-09-10 17:56:01 +00:00
|
|
|
|
|
|
|
if(c!= NULL) {
|
2015-09-11 23:02:44 +00:00
|
|
|
lwipstack->tcp_sent(c->pcb, nc_sent); // FIXME: Move?
|
2015-09-10 17:56:01 +00:00
|
|
|
lwipstack->tcp_recv(c->pcb, nc_recved);
|
2015-09-10 19:46:37 +00:00
|
|
|
lwipstack->tcp_err(c->pcb, nc_err);
|
2015-09-10 17:56:01 +00:00
|
|
|
lwipstack->tcp_poll(c->pcb, nc_poll, APPLICATION_POLL_FREQ);
|
2015-09-11 00:02:13 +00:00
|
|
|
lwipstack->tcp_arg(c->pcb, new Larg(this, c->sock));
|
2015-09-10 17:56:01 +00:00
|
|
|
|
|
|
|
int err = 0;
|
|
|
|
if((err = lwipstack->tcp_connect(c->pcb,&conn_addr,conn_port, nc_connected)) < 0)
|
|
|
|
{
|
|
|
|
// dwr(h->tid, "tcp_connect() = %s\n", lwiperror(err));
|
|
|
|
// 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-15 18:15:59 +00:00
|
|
|
//fprintf(stderr, "failed to connect: %s\n", lwiperror(err));
|
2015-09-10 20:52:18 +00:00
|
|
|
send_return_value(client, 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-10 20:52:18 +00:00
|
|
|
client->waiting_for_retval=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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetconEthernetTap::handle_write(NetconConnection *c)
|
|
|
|
{
|
2015-09-15 18:15:59 +00:00
|
|
|
fprintf(stderr, "handle_write");
|
2015-09-10 17:56:01 +00:00
|
|
|
if(c) {
|
|
|
|
int sndbuf = c->pcb->snd_buf;
|
|
|
|
float avail = (float)sndbuf;
|
|
|
|
float max = (float)TCP_SND_BUF;
|
|
|
|
float load = 1.0 - (avail / max);
|
|
|
|
|
|
|
|
if(load >= 0.9) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int write_allowance = sndbuf < c->idx ? sndbuf : c->idx;
|
|
|
|
int sz;
|
|
|
|
|
2015-09-10 20:52:18 +00:00
|
|
|
if(write_allowance > 0) {
|
2015-09-10 17:56:01 +00:00
|
|
|
int err = lwipstack->tcp_write(c->pcb, &c->buf, write_allowance, TCP_WRITE_FLAG_COPY);
|
|
|
|
if(err != ERR_OK) {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "error while writing to PCB\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sz = (c->idx)-write_allowance;
|
|
|
|
if(sz) {
|
|
|
|
memmove(&c->buf, (c->buf+write_allowance), sz);
|
|
|
|
}
|
|
|
|
c->idx -= write_allowance;
|
2015-09-10 21:44:01 +00:00
|
|
|
//c->data_sent += write_allowance;
|
2015-09-10 17:56:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "lwIP stack full\n");
|
2015-09-10 17:56:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-09-11 22:22:41 +00:00
|
|
|
fprintf(stderr, "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
|