summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Henry <joseph.henry@zerotier.com>2015-09-15 18:47:40 -0400
committerJoseph Henry <joseph.henry@zerotier.com>2015-09-15 18:47:40 -0400
commit732de5bf7f5ecf488e0e00e851bb7fd53e0cf69f (patch)
treefc0aed5bb53436c766413dfedd15a5cccc50c01d
parent11731af45ae2638fa5a20abf8944725ef3cccdd6 (diff)
downloadinfinitytier-732de5bf7f5ecf488e0e00e851bb7fd53e0cf69f.tar.gz
infinitytier-732de5bf7f5ecf488e0e00e851bb7fd53e0cf69f.zip
some cleanup
-rw-r--r--netcon/NetconEthernetTap.cpp152
-rw-r--r--netcon/NetconEthernetTap.hpp110
2 files changed, 93 insertions, 169 deletions
diff --git a/netcon/NetconEthernetTap.cpp b/netcon/NetconEthernetTap.cpp
index 5b6e43bb..68c50515 100644
--- a/netcon/NetconEthernetTap.cpp
+++ b/netcon/NetconEthernetTap.cpp
@@ -156,72 +156,44 @@ void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType
if (!_enabled)
return;
- fprintf(stderr, "put\n");
-/*
- 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)
- fprintf(stderr, "ARP reply generated\n");
- _handler(_arg,_nwid,_mac,from,ZT_ETHERTYPE_ARP,0,arpReplyBuf,arpReplyLen);
- }
-*/
- // Copy data into a pbuf chain
- struct pbuf *p, *q;
- //u16_t len;
- //char buf[1514];
- const char *bufptr;
- // Assemble ethernet header and call netif->output
- struct eth_hdr *ethhdr = NULL;
-
- // We allocate a pbuf chain of pbufs from the pool.
- p = lwipstack->pbuf_alloc(PBUF_RAW, len+sizeof(struct eth_hdr), PBUF_POOL);
-
- if(p != NULL) {
- fprintf(stderr, "p != NULL\n");
- /* We iterate over the pbuf chain until we have read the entire
- packet into the pbuf. */
- bufptr = (const char *)data;
- for(q = p; q != NULL; q = q->next) {
- /* 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); */
- 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;
+ struct pbuf *p, *q;
+ const char *bufptr;
+ struct eth_hdr *ethhdr = NULL;
+
+ // We allocate a pbuf chain of pbufs from the pool.
+ p = lwipstack->pbuf_alloc(PBUF_RAW, len+sizeof(struct eth_hdr), PBUF_POOL);
+
+ if(p != NULL) {
+ /* We iterate over the pbuf chain until we have read the entire
+ packet into the pbuf. */
+ bufptr = (const char *)data;
+ for(q = p; q != NULL; q = q->next) {
+ /* 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); */
+ 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);
}
- /* acknowledge that packet has been read(); */
- } else {
- return;
- /* drop packet(); */
+ memcpy(pload, bufptr, plen);
+ bufptr += plen;
}
+ /* acknowledge that packet has been read(); */
+ } else {
+ return;
+ /* drop packet(); */
+ }
+ from.copyTo(ethhdr->src.addr, 6);
+ _mac.copyTo(ethhdr->dest.addr, 6);
+ ethhdr->type = Utils::hton((uint16_t)etherType);
- from.copyTo(ethhdr->src.addr, 6);
- _mac.copyTo(ethhdr->dest.addr, 6);
- 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);
-
- if(interface.input(p, &interface) != ERR_OK) {
- fprintf(stderr, "IP error (netif->input)\n");
- }
- else {
- fprintf(stderr, "interface.input(...) len = %d\n", len);
- }
+ if(interface.input(p, &interface) != ERR_OK) {
+ fprintf(stderr, "Error while RXing packet (netif->input)\n");
+ }
}
std::string NetconEthernetTap::deviceName() const
@@ -294,7 +266,6 @@ NetconClient *NetconEthernetTap::getClientByPCB(struct tcp_pcb *pcb)
return NULL;
}
-
void NetconEthernetTap::closeClient(NetconClient *client)
{
fprintf(stderr, "closeClient\n");
@@ -308,6 +279,13 @@ void NetconEthernetTap::closeClient(NetconClient *client)
delete client;
}
+void NetconEthernetTap::closeAllClients()
+{
+ for(int i=0; i<clients.size(); i++){
+ closeClient(clients[i]);
+ }
+}
+
void NetconEthernetTap::closeConnection(NetconConnection *conn)
{
fprintf(stderr, "closeConnection\n");
@@ -326,10 +304,8 @@ void NetconEthernetTap::closeConnection(NetconConnection *conn)
void NetconEthernetTap::threadMain()
throw()
{
- fprintf(stderr, "starting threadMain()\n");
-
+ // Set up IPs (will be moved into add/removeIP later)
static ip_addr_t ipaddr, netmask, gw;
- char ip_str[16] = {0}, nm_str[16] = {0}, gw_str[16] = {0};
if(_ips.size() == 0) {
fprintf(stderr, "no IP assigned. Exiting.\n");
@@ -340,13 +316,13 @@ void NetconEthernetTap::threadMain()
ipaddr.addr = *((u32_t *)_ips[0].rawIpData());
netmask.addr = *((u32_t *)_ips[0].netmask().rawIpData());
- 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));
-
- fprintf(stderr, "ip_str = %s\n", ip_str);
- fprintf(stderr, "nm_str = %s\n", nm_str);
- fprintf(stderr, "gw_str = %s\n", gw_str);
+ //char ip_str[16] = {0}, nm_str[16] = {0}, gw_str[16] = {0};
+ //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));
+ //fprintf(stderr, "ip_str = %s\n", ip_str);
+ //fprintf(stderr, "nm_str = %s\n", nm_str);
+ //fprintf(stderr, "gw_str = %s\n", gw_str);
unsigned long tcp_time = ARP_TMR_INTERVAL / 5000;
unsigned long etharp_time = IP_TMR_INTERVAL / 1000;
@@ -357,35 +333,22 @@ void NetconEthernetTap::threadMain()
unsigned long since_etharp;
struct timeval tv;
+ // set up the lwip-netif for LWIP's sake
fprintf(stderr, "initializing interface\n");
-
- /* 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;
interface.output = lwipstack->etharp_output;
_mac.copyTo(interface.hwaddr, 6);
- interface.mtu = 2800;
-
- /*
+ interface.mtu = _mtu;
interface.name[0] = 't';
interface.name[1] = 'p';
- interface.output = lwipstack->etharp_output;
interface.linkoutput = low_level_output;
-
interface.hwaddr_len = 6;
-
interface.flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
- */
-
- 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");
-
+ // Main timer loop
while (_run) {
gettimeofday(&tv, NULL);
curr_time = (unsigned long)(tv.tv_sec) * 1000 + (unsigned long)(tv.tv_usec) / 1000;
@@ -396,24 +359,19 @@ void NetconEthernetTap::threadMain()
if(since_tcp > tcp_time)
{
prev_tcp_time = curr_time+1;
- //fprintf(stderr, "tcp_tmr\n");
lwipstack->tcp_tmr();
}
if(since_etharp > etharp_time)
{
prev_etharp_time = curr_time;
- //fprintf(stderr, "etharp_tmr\n");
lwipstack->etharp_tmr();
}
- //fprintf(stderr, "_run\n");
- //lwipstack->netif_poll(&interface);
- _phy.poll(100); // conversion from usec to millisec, TODO: double check
-
+ _phy.poll(min_time / 1000); // conversion from usec to millisec, TODO: double check
}
+ closeAllClients();
// TODO: cleanup -- destroy LWIP state, kill any clients, unload .so, etc.
}
-
void NetconEthernetTap::phyOnSocketPairEndpointClose(PhySocket *sock, void **uptr)
{
fprintf(stderr, "phyOnSocketPairEndpointClose\n");
diff --git a/netcon/NetconEthernetTap.hpp b/netcon/NetconEthernetTap.hpp
index 273dd8f4..9dbf4fac 100644
--- a/netcon/NetconEthernetTap.hpp
+++ b/netcon/NetconEthernetTap.hpp
@@ -50,12 +50,6 @@
#include "netif/etharp.h"
-struct tapif {
- struct eth_addr *ethaddr;
- /* Add whatever per-interface state that is needed here. */
- int fd;
-};
-
namespace ZeroTier {
class NetconEthernetTap;
@@ -152,16 +146,15 @@ private:
NetconConnection *getConnectionByPCB(struct tcp_pcb *pcb);
NetconClient *getClientByPCB(struct tcp_pcb *pcb);
void closeClient(NetconClient *client);
+ void closeAllClients();
void closeConnection(NetconConnection *conn);
-
Phy<NetconEthernetTap *> _phy;
PhySocket *_unixListenSocket;
std::vector<NetconClient*> clients;
netif interface;
-
MAC _mac;
Thread _thread;
std::string _homePath;
@@ -188,76 +181,49 @@ private:
static err_t low_level_output(struct netif *netif, struct pbuf *p);
+static err_t tapif_init(struct netif *netif)
+{
+ // Actual init functionality is in threadMain() of tap
+ return ERR_OK;
+}
- static void low_level_init(struct netif *netif)
- {
- fprintf(stderr, "low_level_init\n");
- }
-
- static err_t tapif_init(struct netif *netif)
- {
- //netif->state = tapif;
- netif->name[0] = 't';
- netif->name[1] = 'p';
- //netif->output = netif->state.lwipstack->etharp_output;
- netif->linkoutput = low_level_output;
- //netif->mtu = 1500;
- /* hardware address length */
- netif->hwaddr_len = 6;
-
- //tapif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
- netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
- //low_level_init(netif);
- return ERR_OK;
+static err_t low_level_output(struct netif *netif, struct pbuf *p)
+{
+ struct pbuf *q;
+ char buf[ZT1_MAX_MTU+32];
+ char *bufptr;
+ int tot_len = 0;
+
+ ZeroTier::NetconEthernetTap *tap = (ZeroTier::NetconEthernetTap*)netif->state;
+
+ /* initiate transfer(); */
+ bufptr = &buf[0];
+
+ for(q = p; q != NULL; q = q->next) {
+ /* Send the data from the pbuf to the interface, one pbuf at a
+ time. The size of the data in each pbuf is kept in the ->len
+ variable. */
+ /* send data from(q->payload, q->len); */
+ memcpy(bufptr, q->payload, q->len);
+ bufptr += q->len;
+ tot_len += q->len;
}
- static err_t low_level_output(struct netif *netif, struct pbuf *p)
- {
- struct pbuf *q;
- char buf[2800+14];
- char *bufptr;
- //struct tapif *tapif;
- int tot_len = 0;
+ // [Send packet to network]
+ // Split ethernet header and feed into handler
+ struct eth_hdr *ethhdr;
+ ethhdr = (struct eth_hdr *)p->payload;
- //tapif = (struct tapif *)netif->state;
- ZeroTier::NetconEthernetTap *tap = (ZeroTier::NetconEthernetTap*)netif->state;
+ ZeroTier::MAC src_mac;
+ ZeroTier::MAC dest_mac;
- /* initiate transfer(); */
+ src_mac.setTo(ethhdr->src.addr, 6);
+ dest_mac.setTo(ethhdr->dest.addr, 6);
- bufptr = &buf[0];
-
- for(q = p; q != NULL; q = q->next) {
- /* Send the data from the pbuf to the interface, one pbuf at a
- time. The size of the data in each pbuf is kept in the ->len
- variable. */
- /* send data from(q->payload, q->len); */
- memcpy(bufptr, q->payload, q->len);
- bufptr += q->len;
- tot_len += q->len;
- }
-
- // signal that packet should be sent();
- // Split ethernet header and feed into handler
- struct eth_hdr *ethhdr;
- ethhdr = (struct eth_hdr *)p->payload;
-
- ZeroTier::MAC src_mac;
- ZeroTier::MAC dest_mac;
-
- src_mac.setTo(ethhdr->src.addr, 6);
- dest_mac.setTo(ethhdr->dest.addr, 6);
-
- tap->_handler(tap->_arg,tap->_nwid,src_mac,dest_mac,Utils::ntoh((uint16_t)ethhdr->type),0,buf + sizeof(struct eth_hdr),p->tot_len);
- fprintf(stderr, "ethhdr->type = %x\n", ethhdr->type);
- fprintf(stderr, "p->tot_len = %x\n", p->tot_len);
- fprintf(stderr, "src_mac = %s\n", src_mac.toString().c_str());
- fprintf(stderr, "dest_mac = %s\n", dest_mac.toString().c_str());
-
- //fprintf(stderr, "htons(ethhdr->type) = %x\n", Utils::hton((uint16_t)ethhdr->type));
- fprintf(stderr, "low_level_output(%x)\n", tap->_nwid);
-
- return ERR_OK;
- }
+ tap->_handler(tap->_arg,tap->_nwid,src_mac,dest_mac,
+ Utils::ntoh((uint16_t)ethhdr->type),0,buf + sizeof(struct eth_hdr),p->tot_len);
+ return ERR_OK;
+}
} // namespace ZeroTier