mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-01 23:10:52 +00:00
some cleanup
This commit is contained in:
parent
11731af45a
commit
732de5bf7f
@ -156,72 +156,44 @@ void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType
|
|||||||
if (!_enabled)
|
if (!_enabled)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
fprintf(stderr, "put\n");
|
struct pbuf *p, *q;
|
||||||
/*
|
const char *bufptr;
|
||||||
if (etherType == ZT_ETHERTYPE_ARP) {
|
struct eth_hdr *ethhdr = NULL;
|
||||||
char arpReplyBuf[ZT_ARP_BUF_LENGTH];
|
|
||||||
unsigned int arpReplyLen = 0;
|
|
||||||
MAC arpReplyDest;
|
|
||||||
|
|
||||||
Mutex::Lock _l2(_arp_m);
|
// We allocate a pbuf chain of pbufs from the pool.
|
||||||
_arp.processIncomingArp(data,len,arpReplyBuf,arpReplyLen,arpReplyDest);
|
p = lwipstack->pbuf_alloc(PBUF_RAW, len+sizeof(struct eth_hdr), PBUF_POOL);
|
||||||
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.
|
if(p != NULL) {
|
||||||
p = lwipstack->pbuf_alloc(PBUF_RAW, len+sizeof(struct eth_hdr), PBUF_POOL);
|
/* We iterate over the pbuf chain until we have read the entire
|
||||||
|
packet into the pbuf. */
|
||||||
if(p != NULL) {
|
bufptr = (const char *)data;
|
||||||
fprintf(stderr, "p != NULL\n");
|
for(q = p; q != NULL; q = q->next) {
|
||||||
/* We iterate over the pbuf chain until we have read the entire
|
/* Read enough bytes to fill this pbuf in the chain. The
|
||||||
packet into the pbuf. */
|
available data in the pbuf is given by the q->len
|
||||||
bufptr = (const char *)data;
|
variable. */
|
||||||
for(q = p; q != NULL; q = q->next) {
|
/* read data into(q->payload, q->len); */
|
||||||
/* Read enough bytes to fill this pbuf in the chain. The
|
char *pload = (char*)q->payload;
|
||||||
available data in the pbuf is given by the q->len
|
int plen = q->len;
|
||||||
variable. */
|
if (!ethhdr) {
|
||||||
/* read data into(q->payload, q->len); */
|
ethhdr = (struct eth_hdr *)p->payload;
|
||||||
char *pload = (char*)q->payload;
|
pload += sizeof(struct eth_hdr);
|
||||||
int plen = q->len;
|
plen -= sizeof(struct eth_hdr);
|
||||||
if (!ethhdr) {
|
|
||||||
ethhdr = (struct eth_hdr *)p->payload;
|
|
||||||
pload += sizeof(struct eth_hdr);
|
|
||||||
plen -= sizeof(struct eth_hdr);
|
|
||||||
}
|
|
||||||
memcpy(pload, bufptr, plen);
|
|
||||||
bufptr += plen;
|
|
||||||
}
|
}
|
||||||
/* acknowledge that packet has been read(); */
|
memcpy(pload, bufptr, plen);
|
||||||
} else {
|
bufptr += plen;
|
||||||
return;
|
|
||||||
/* drop packet(); */
|
|
||||||
}
|
}
|
||||||
|
/* 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);
|
if(interface.input(p, &interface) != ERR_OK) {
|
||||||
_mac.copyTo(ethhdr->dest.addr, 6);
|
fprintf(stderr, "Error while RXing packet (netif->input)\n");
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string NetconEthernetTap::deviceName() const
|
std::string NetconEthernetTap::deviceName() const
|
||||||
@ -294,7 +266,6 @@ NetconClient *NetconEthernetTap::getClientByPCB(struct tcp_pcb *pcb)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void NetconEthernetTap::closeClient(NetconClient *client)
|
void NetconEthernetTap::closeClient(NetconClient *client)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "closeClient\n");
|
fprintf(stderr, "closeClient\n");
|
||||||
@ -308,6 +279,13 @@ void NetconEthernetTap::closeClient(NetconClient *client)
|
|||||||
delete client;
|
delete client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NetconEthernetTap::closeAllClients()
|
||||||
|
{
|
||||||
|
for(int i=0; i<clients.size(); i++){
|
||||||
|
closeClient(clients[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void NetconEthernetTap::closeConnection(NetconConnection *conn)
|
void NetconEthernetTap::closeConnection(NetconConnection *conn)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "closeConnection\n");
|
fprintf(stderr, "closeConnection\n");
|
||||||
@ -326,10 +304,8 @@ void NetconEthernetTap::closeConnection(NetconConnection *conn)
|
|||||||
void NetconEthernetTap::threadMain()
|
void NetconEthernetTap::threadMain()
|
||||||
throw()
|
throw()
|
||||||
{
|
{
|
||||||
fprintf(stderr, "starting threadMain()\n");
|
// Set up IPs (will be moved into add/removeIP later)
|
||||||
|
|
||||||
static ip_addr_t ipaddr, netmask, gw;
|
static ip_addr_t ipaddr, netmask, gw;
|
||||||
char ip_str[16] = {0}, nm_str[16] = {0}, gw_str[16] = {0};
|
|
||||||
|
|
||||||
if(_ips.size() == 0) {
|
if(_ips.size() == 0) {
|
||||||
fprintf(stderr, "no IP assigned. Exiting.\n");
|
fprintf(stderr, "no IP assigned. Exiting.\n");
|
||||||
@ -340,13 +316,13 @@ void NetconEthernetTap::threadMain()
|
|||||||
ipaddr.addr = *((u32_t *)_ips[0].rawIpData());
|
ipaddr.addr = *((u32_t *)_ips[0].rawIpData());
|
||||||
netmask.addr = *((u32_t *)_ips[0].netmask().rawIpData());
|
netmask.addr = *((u32_t *)_ips[0].netmask().rawIpData());
|
||||||
|
|
||||||
strncpy(ip_str, lwipstack->ipaddr_ntoa(&ipaddr), sizeof(ip_str));
|
//char ip_str[16] = {0}, nm_str[16] = {0}, gw_str[16] = {0};
|
||||||
strncpy(nm_str, lwipstack->ipaddr_ntoa(&netmask), sizeof(nm_str));
|
//strncpy(ip_str, lwipstack->ipaddr_ntoa(&ipaddr), sizeof(ip_str));
|
||||||
strncpy(gw_str, lwipstack->ipaddr_ntoa(&gw), sizeof(gw_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, "ip_str = %s\n", ip_str);
|
||||||
fprintf(stderr, "nm_str = %s\n", nm_str);
|
//fprintf(stderr, "nm_str = %s\n", nm_str);
|
||||||
fprintf(stderr, "gw_str = %s\n", gw_str);
|
//fprintf(stderr, "gw_str = %s\n", gw_str);
|
||||||
|
|
||||||
unsigned long tcp_time = ARP_TMR_INTERVAL / 5000;
|
unsigned long tcp_time = ARP_TMR_INTERVAL / 5000;
|
||||||
unsigned long etharp_time = IP_TMR_INTERVAL / 1000;
|
unsigned long etharp_time = IP_TMR_INTERVAL / 1000;
|
||||||
@ -357,35 +333,22 @@ void NetconEthernetTap::threadMain()
|
|||||||
unsigned long since_etharp;
|
unsigned long since_etharp;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
|
|
||||||
|
// set up the lwip-netif for LWIP's sake
|
||||||
fprintf(stderr, "initializing interface\n");
|
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);
|
lwipstack->netif_add(&interface,&ipaddr, &netmask, &gw, NULL, tapif_init, lwipstack->ethernet_input);
|
||||||
|
|
||||||
interface.state = this;
|
interface.state = this;
|
||||||
interface.output = lwipstack->etharp_output;
|
interface.output = lwipstack->etharp_output;
|
||||||
_mac.copyTo(interface.hwaddr, 6);
|
_mac.copyTo(interface.hwaddr, 6);
|
||||||
interface.mtu = 2800;
|
interface.mtu = _mtu;
|
||||||
|
|
||||||
/*
|
|
||||||
interface.name[0] = 't';
|
interface.name[0] = 't';
|
||||||
interface.name[1] = 'p';
|
interface.name[1] = 'p';
|
||||||
interface.output = lwipstack->etharp_output;
|
|
||||||
interface.linkoutput = low_level_output;
|
interface.linkoutput = low_level_output;
|
||||||
|
|
||||||
interface.hwaddr_len = 6;
|
interface.hwaddr_len = 6;
|
||||||
|
|
||||||
interface.flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
|
interface.flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
|
||||||
*/
|
|
||||||
|
|
||||||
fprintf(stderr, "netif_set_default\n");
|
|
||||||
lwipstack->netif_set_default(&interface);
|
lwipstack->netif_set_default(&interface);
|
||||||
fprintf(stderr, "netif_set_up\n");
|
|
||||||
lwipstack->netif_set_up(&interface);
|
lwipstack->netif_set_up(&interface);
|
||||||
fprintf(stderr, "complete\n");
|
|
||||||
|
|
||||||
|
|
||||||
|
// Main timer loop
|
||||||
while (_run) {
|
while (_run) {
|
||||||
gettimeofday(&tv, NULL);
|
gettimeofday(&tv, NULL);
|
||||||
curr_time = (unsigned long)(tv.tv_sec) * 1000 + (unsigned long)(tv.tv_usec) / 1000;
|
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)
|
if(since_tcp > tcp_time)
|
||||||
{
|
{
|
||||||
prev_tcp_time = curr_time+1;
|
prev_tcp_time = curr_time+1;
|
||||||
//fprintf(stderr, "tcp_tmr\n");
|
|
||||||
lwipstack->tcp_tmr();
|
lwipstack->tcp_tmr();
|
||||||
}
|
}
|
||||||
if(since_etharp > etharp_time)
|
if(since_etharp > etharp_time)
|
||||||
{
|
{
|
||||||
prev_etharp_time = curr_time;
|
prev_etharp_time = curr_time;
|
||||||
//fprintf(stderr, "etharp_tmr\n");
|
|
||||||
lwipstack->etharp_tmr();
|
lwipstack->etharp_tmr();
|
||||||
}
|
}
|
||||||
//fprintf(stderr, "_run\n");
|
_phy.poll(min_time / 1000); // conversion from usec to millisec, TODO: double check
|
||||||
//lwipstack->netif_poll(&interface);
|
|
||||||
_phy.poll(100); // conversion from usec to millisec, TODO: double check
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
closeAllClients();
|
||||||
// TODO: cleanup -- destroy LWIP state, kill any clients, unload .so, etc.
|
// TODO: cleanup -- destroy LWIP state, kill any clients, unload .so, etc.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void NetconEthernetTap::phyOnSocketPairEndpointClose(PhySocket *sock, void **uptr)
|
void NetconEthernetTap::phyOnSocketPairEndpointClose(PhySocket *sock, void **uptr)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "phyOnSocketPairEndpointClose\n");
|
fprintf(stderr, "phyOnSocketPairEndpointClose\n");
|
||||||
|
@ -50,12 +50,6 @@
|
|||||||
|
|
||||||
#include "netif/etharp.h"
|
#include "netif/etharp.h"
|
||||||
|
|
||||||
struct tapif {
|
|
||||||
struct eth_addr *ethaddr;
|
|
||||||
/* Add whatever per-interface state that is needed here. */
|
|
||||||
int fd;
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace ZeroTier {
|
namespace ZeroTier {
|
||||||
|
|
||||||
class NetconEthernetTap;
|
class NetconEthernetTap;
|
||||||
@ -152,16 +146,15 @@ private:
|
|||||||
NetconConnection *getConnectionByPCB(struct tcp_pcb *pcb);
|
NetconConnection *getConnectionByPCB(struct tcp_pcb *pcb);
|
||||||
NetconClient *getClientByPCB(struct tcp_pcb *pcb);
|
NetconClient *getClientByPCB(struct tcp_pcb *pcb);
|
||||||
void closeClient(NetconClient *client);
|
void closeClient(NetconClient *client);
|
||||||
|
void closeAllClients();
|
||||||
void closeConnection(NetconConnection *conn);
|
void closeConnection(NetconConnection *conn);
|
||||||
|
|
||||||
|
|
||||||
Phy<NetconEthernetTap *> _phy;
|
Phy<NetconEthernetTap *> _phy;
|
||||||
PhySocket *_unixListenSocket;
|
PhySocket *_unixListenSocket;
|
||||||
|
|
||||||
std::vector<NetconClient*> clients;
|
std::vector<NetconClient*> clients;
|
||||||
netif interface;
|
netif interface;
|
||||||
|
|
||||||
|
|
||||||
MAC _mac;
|
MAC _mac;
|
||||||
Thread _thread;
|
Thread _thread;
|
||||||
std::string _homePath;
|
std::string _homePath;
|
||||||
@ -188,76 +181,49 @@ private:
|
|||||||
|
|
||||||
static err_t low_level_output(struct netif *netif, struct pbuf *p);
|
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)
|
static err_t low_level_output(struct netif *netif, struct pbuf *p)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "low_level_init\n");
|
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 tapif_init(struct netif *netif)
|
// [Send packet to network]
|
||||||
{
|
// Split ethernet header and feed into handler
|
||||||
//netif->state = tapif;
|
struct eth_hdr *ethhdr;
|
||||||
netif->name[0] = 't';
|
ethhdr = (struct eth_hdr *)p->payload;
|
||||||
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]);
|
ZeroTier::MAC src_mac;
|
||||||
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
|
ZeroTier::MAC dest_mac;
|
||||||
//low_level_init(netif);
|
|
||||||
return ERR_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
static err_t low_level_output(struct netif *netif, struct pbuf *p)
|
src_mac.setTo(ethhdr->src.addr, 6);
|
||||||
{
|
dest_mac.setTo(ethhdr->dest.addr, 6);
|
||||||
struct pbuf *q;
|
|
||||||
char buf[2800+14];
|
|
||||||
char *bufptr;
|
|
||||||
//struct tapif *tapif;
|
|
||||||
int tot_len = 0;
|
|
||||||
|
|
||||||
//tapif = (struct tapif *)netif->state;
|
tap->_handler(tap->_arg,tap->_nwid,src_mac,dest_mac,
|
||||||
ZeroTier::NetconEthernetTap *tap = (ZeroTier::NetconEthernetTap*)netif->state;
|
Utils::ntoh((uint16_t)ethhdr->type),0,buf + sizeof(struct eth_hdr),p->tot_len);
|
||||||
|
return ERR_OK;
|
||||||
/* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace ZeroTier
|
} // namespace ZeroTier
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user