diff options
author | Joseph Henry <joseph.henry@zerotier.com> | 2015-09-15 18:47:40 -0400 |
---|---|---|
committer | Joseph Henry <joseph.henry@zerotier.com> | 2015-09-15 18:47:40 -0400 |
commit | 732de5bf7f5ecf488e0e00e851bb7fd53e0cf69f (patch) | |
tree | fc0aed5bb53436c766413dfedd15a5cccc50c01d /netcon/NetconEthernetTap.cpp | |
parent | 11731af45ae2638fa5a20abf8944725ef3cccdd6 (diff) | |
download | infinitytier-732de5bf7f5ecf488e0e00e851bb7fd53e0cf69f.tar.gz infinitytier-732de5bf7f5ecf488e0e00e851bb7fd53e0cf69f.zip |
some cleanup
Diffstat (limited to 'netcon/NetconEthernetTap.cpp')
-rw-r--r-- | netcon/NetconEthernetTap.cpp | 152 |
1 files changed, 55 insertions, 97 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"); |