Normalized indentation for entire project to TABS

This commit is contained in:
Joseph Henry
2017-08-02 14:54:29 -07:00
parent 7e30474ee4
commit 7e00eda5a0
16 changed files with 2557 additions and 2557 deletions

View File

@@ -32,158 +32,158 @@
namespace ZeroTier {
template<typename T> class RingBuffer {
template<typename T> class RingBuffer {
private:
T * buf;
size_t size;
size_t begin;
size_t end;
bool wrap;
private:
T * buf;
size_t size;
size_t begin;
size_t end;
bool wrap;
public:
/**
* create a RingBuffer with space for up to size elements.
*/
explicit RingBuffer(size_t size)
: size(size),
begin(0),
end(0),
wrap(false)
{
buf = new T[size];
}
public:
/**
* create a RingBuffer with space for up to size elements.
*/
explicit RingBuffer(size_t size)
: size(size),
begin(0),
end(0),
wrap(false)
{
buf = new T[size];
}
RingBuffer(const RingBuffer<T> & ring)
{
this(ring.size);
begin = ring.begin;
end = ring.end;
memcpy(buf, ring.buf, sizeof(T) * size);
}
RingBuffer(const RingBuffer<T> & ring)
{
this(ring.size);
begin = ring.begin;
end = ring.end;
memcpy(buf, ring.buf, sizeof(T) * size);
}
~RingBuffer()
{
delete[] buf;
}
~RingBuffer()
{
delete[] buf;
}
// get a reference to the underlying buffer
T* get_buf()
{
return buf + begin;
}
// get a reference to the underlying buffer
T* get_buf()
{
return buf + begin;
}
// adjust buffer index pointer as if we copied data in
size_t produce(size_t n)
{
n = std::min(n, getFree());
// adjust buffer index pointer as if we copied data in
size_t produce(size_t n)
{
n = std::min(n, getFree());
if (n == 0) {
return n;
}
if (n == 0) {
return n;
}
const size_t first_chunk = std::min(n, size - end);
end = (end + first_chunk) % size;
const size_t first_chunk = std::min(n, size - end);
end = (end + first_chunk) % size;
if (first_chunk < n) {
const size_t second_chunk = n - first_chunk;
end = (end + second_chunk) % size;
}
if (first_chunk < n) {
const size_t second_chunk = n - first_chunk;
end = (end + second_chunk) % size;
}
if (begin == end) {
wrap = true;
}
if (begin == end) {
wrap = true;
}
return n;
}
return n;
}
// adjust buffer index pointer as if we copied data out
size_t consume(size_t n)
{
n = std::min(n, count());
// adjust buffer index pointer as if we copied data out
size_t consume(size_t n)
{
n = std::min(n, count());
if (n == 0) {
return n;
}
if (n == 0) {
return n;
}
if (wrap) {
wrap = false;
}
if (wrap) {
wrap = false;
}
const size_t first_chunk = std::min(n, size - begin);
begin = (begin + first_chunk) % size;
const size_t first_chunk = std::min(n, size - begin);
begin = (begin + first_chunk) % size;
if (first_chunk < n) {
const size_t second_chunk = n - first_chunk;
begin = (begin + second_chunk) % size;
}
return n;
}
if (first_chunk < n) {
const size_t second_chunk = n - first_chunk;
begin = (begin + second_chunk) % size;
}
return n;
}
size_t write(const T * data, size_t n)
{
n = std::min(n, getFree());
size_t write(const T * data, size_t n)
{
n = std::min(n, getFree());
if (n == 0) {
return n;
}
if (n == 0) {
return n;
}
const size_t first_chunk = std::min(n, size - end);
memcpy(buf + end, data, first_chunk * sizeof(T));
end = (end + first_chunk) % size;
const size_t first_chunk = std::min(n, size - end);
memcpy(buf + end, data, first_chunk * sizeof(T));
end = (end + first_chunk) % size;
if (first_chunk < n) {
const size_t second_chunk = n - first_chunk;
memcpy(buf + end, data + first_chunk, second_chunk * sizeof(T));
end = (end + second_chunk) % size;
}
if (first_chunk < n) {
const size_t second_chunk = n - first_chunk;
memcpy(buf + end, data + first_chunk, second_chunk * sizeof(T));
end = (end + second_chunk) % size;
}
if (begin == end) {
wrap = true;
}
if (begin == end) {
wrap = true;
}
return n;
}
return n;
}
size_t read(T * dest, size_t n)
{
n = std::min(n, count());
size_t read(T * dest, size_t n)
{
n = std::min(n, count());
if (n == 0) {
return n;
}
if (n == 0) {
return n;
}
if (wrap) {
wrap = false;
}
if (wrap) {
wrap = false;
}
const size_t first_chunk = std::min(n, size - begin);
memcpy(dest, buf + begin, first_chunk * sizeof(T));
begin = (begin + first_chunk) % size;
const size_t first_chunk = std::min(n, size - begin);
memcpy(dest, buf + begin, first_chunk * sizeof(T));
begin = (begin + first_chunk) % size;
if (first_chunk < n) {
const size_t second_chunk = n - first_chunk;
memcpy(dest + first_chunk, buf + begin, second_chunk * sizeof(T));
begin = (begin + second_chunk) % size;
}
return n;
}
if (first_chunk < n) {
const size_t second_chunk = n - first_chunk;
memcpy(dest + first_chunk, buf + begin, second_chunk * sizeof(T));
begin = (begin + second_chunk) % size;
}
return n;
}
size_t count() {
if (end == begin) {
return wrap ? size : 0;
}
else if (end > begin) {
return end - begin;
}
else {
return size + end - begin;
}
}
size_t count() {
if (end == begin) {
return wrap ? size : 0;
}
else if (end > begin) {
return end - begin;
}
else {
return size + end - begin;
}
}
size_t getFree() {
return size - count();
}
};
size_t getFree() {
return size - count();
}
};
}
#endif // ZT_RINGBUFFER_HPP

View File

@@ -81,7 +81,7 @@ namespace ZeroTier {
void (*handler)(void *,void*,uint64_t,const MAC &,const MAC &,
unsigned int,unsigned int,const void *,unsigned int),
void *arg) :
_handler(handler),
_handler(handler),
_homePath(homePath),
_arg(arg),
_enabled(true),
@@ -185,12 +185,12 @@ namespace ZeroTier {
const void *data,unsigned int len)
{
#if defined(STACK_PICO)
if(picostack)
picostack->pico_rx(this,from,to,etherType,data,len);
if(picostack)
picostack->pico_rx(this,from,to,etherType,data,len);
#endif
#if defined(STACK_LWIP)
if(lwipstack)
lwipstack->lwip_rx(this,from,to,etherType,data,len);
if(lwipstack)
lwipstack->lwip_rx(this,from,to,etherType,data,len);
#endif
}
@@ -264,11 +264,11 @@ namespace ZeroTier {
Connection *conn = (Connection*)*uptr;
if(!conn)
return;
if(len){
if(len){
Write(conn, data, len);
}
return;
Write(conn, data, len);
}
return;
}
void SocketTap::phyOnUnixWritable(PhySocket *sock,void **uptr,bool stack_invoked)
@@ -337,13 +337,13 @@ namespace ZeroTier {
int SocketTap::Write(Connection *conn, void *data, ssize_t len) {
if(conn->socket_type == SOCK_RAW) { // we don't want to use a stack, just VL2
struct ether_header *eh = (struct ether_header *) data;
MAC src_mac;
MAC dest_mac;
src_mac.setTo(eh->ether_shost, 6);
dest_mac.setTo(eh->ether_dhost, 6);
_handler(_arg,NULL,_nwid,src_mac,dest_mac, Utils::ntoh((uint16_t)eh->ether_type),0, ((char*)data) + sizeof(struct ether_header),len - sizeof(struct ether_header));
return len;
struct ether_header *eh = (struct ether_header *) data;
MAC src_mac;
MAC dest_mac;
src_mac.setTo(eh->ether_shost, 6);
dest_mac.setTo(eh->ether_dhost, 6);
_handler(_arg,NULL,_nwid,src_mac,dest_mac, Utils::ntoh((uint16_t)eh->ether_type),0, ((char*)data) + sizeof(struct ether_header),len - sizeof(struct ether_header));
return len;
}
#if defined(STACK_PICO)

View File

@@ -137,22 +137,22 @@ namespace ZeroTier {
/*
* For moving data onto the ZeroTier virtual wire
*/
void (*_handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,
const void *,unsigned int);
void (*_handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,
const void *,unsigned int);
/*
* Signals us to close the TcpConnection associated with this PhySocket
*/
* Signals us to close the TcpConnection associated with this PhySocket
*/
void phyOnUnixClose(PhySocket *sock,void **uptr);
/*
* Notifies us that there is data to be read from an application's socket
*/
/*
* Notifies us that there is data to be read from an application's socket
*/
void phyOnUnixData(PhySocket *sock,void **uptr,void *data,ssize_t len);
/*
* Notifies us that we can write to an application's socket
*/
* Notifies us that we can write to an application's socket
*/
void phyOnUnixWritable(PhySocket *sock,void **uptr,bool lwip_invoked);
/****************************************************************************/
@@ -173,14 +173,14 @@ namespace ZeroTier {
/* Guarded RX Frame Buffer for picoTCP */
/****************************************************************************/
unsigned char pico_frame_rxbuf[MAX_PICO_FRAME_RX_BUF_SZ];
int pico_frame_rxbuf_tot;
Mutex _pico_frame_rxbuf_m;
unsigned char pico_frame_rxbuf[MAX_PICO_FRAME_RX_BUF_SZ];
int pico_frame_rxbuf_tot;
Mutex _pico_frame_rxbuf_m;
#endif
#if defined(STACK_LWIP)
netif lwipdev;
netif lwipdev6;
netif lwipdev;
netif lwipdev6;
#endif
static int devno;
@@ -190,13 +190,13 @@ namespace ZeroTier {
std::vector<InetAddress> _ips;
std::string _homePath;
void *_arg;
void *_arg;
volatile bool _enabled;
volatile bool _run;
MAC _mac;
unsigned int _mtu;
uint64_t _nwid;
PhySocket *_unixListenSocket;
unsigned int _mtu;
uint64_t _nwid;
PhySocket *_unixListenSocket;
Phy<SocketTap *> _phy;
std::vector<Connection*> _Connections;
@@ -245,14 +245,14 @@ namespace ZeroTier {
int Read(PhySocket *sock,void **uptr,bool stack_invoked);
/*
* Move data from application's "socket" into network stack
*/
* Move data from application's "socket" into network stack
*/
int Write(Connection *conn, void *data, ssize_t len);
/*
* Closes a Connection
*/
int Close(Connection *conn);
int Close(Connection *conn);
/*
* Disposes of previously-closed Connections

View File

@@ -38,14 +38,14 @@
#define ip4_addr4b(ipaddr) (((u8_t*)(ipaddr))[3])
inline ip_addr_t convert_ip(struct sockaddr_in * addr)
{
ip_addr_t conn_addr;
struct sockaddr_in *ipv4 = addr;
short a = ip4_addr1b(&(ipv4->sin_addr));
short b = ip4_addr2b(&(ipv4->sin_addr));
short c = ip4_addr3b(&(ipv4->sin_addr));
short d = ip4_addr4b(&(ipv4->sin_addr));
IP4_ADDR(&conn_addr, a,b,c,d);
return conn_addr;
ip_addr_t conn_addr;
struct sockaddr_in *ipv4 = addr;
short a = ip4_addr1b(&(ipv4->sin_addr));
short b = ip4_addr2b(&(ipv4->sin_addr));
short c = ip4_addr3b(&(ipv4->sin_addr));
short d = ip4_addr4b(&(ipv4->sin_addr));
IP4_ADDR(&conn_addr, a,b,c,d);
return conn_addr;
}
#endif // STACK_LWIP && LIBZT_IPV4

File diff suppressed because it is too large Load Diff

View File

@@ -42,352 +42,352 @@ err_t tapif_init(struct netif *netif)
err_t low_level_output(struct netif *netif, struct pbuf *p)
{
DEBUG_INFO();
struct pbuf *q;
char buf[ZT_MAX_MTU+32];
char *bufptr;
int totalLength = 0;
DEBUG_INFO();
struct pbuf *q;
char buf[ZT_MAX_MTU+32];
char *bufptr;
int totalLength = 0;
ZeroTier::SocketTap *tap = (ZeroTier::SocketTap*)netif->state;
bufptr = buf;
// Copy data from each pbuf, one at a time
for(q = p; q != NULL; q = q->next) {
memcpy(bufptr, q->payload, q->len);
bufptr += q->len;
totalLength += q->len;
}
// Split ethernet header and feed into handler
struct eth_hdr *ethhdr;
ethhdr = (struct eth_hdr *)buf;
ZeroTier::SocketTap *tap = (ZeroTier::SocketTap*)netif->state;
bufptr = buf;
// Copy data from each pbuf, one at a time
for(q = p; q != NULL; q = q->next) {
memcpy(bufptr, q->payload, q->len);
bufptr += q->len;
totalLength += q->len;
}
// Split ethernet header and feed into handler
struct eth_hdr *ethhdr;
ethhdr = (struct eth_hdr *)buf;
ZeroTier::MAC src_mac;
ZeroTier::MAC dest_mac;
src_mac.setTo(ethhdr->src.addr, 6);
dest_mac.setTo(ethhdr->dest.addr, 6);
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,NULL,tap->_nwid,src_mac,dest_mac,
ZeroTier::Utils::ntoh((uint16_t)ethhdr->type),0,buf + sizeof(struct eth_hdr),totalLength - sizeof(struct eth_hdr));
return ERR_OK;
tap->_handler(tap->_arg,NULL,tap->_nwid,src_mac,dest_mac,
ZeroTier::Utils::ntoh((uint16_t)ethhdr->type),0,buf + sizeof(struct eth_hdr),totalLength - sizeof(struct eth_hdr));
return ERR_OK;
}
namespace ZeroTier
{
void lwIP::lwip_init_interface(SocketTap *tap, const InetAddress &ip)
{
DEBUG_INFO();
Mutex::Lock _l(tap->_ips_m);
void lwIP::lwip_init_interface(SocketTap *tap, const InetAddress &ip)
{
DEBUG_INFO();
Mutex::Lock _l(tap->_ips_m);
if (std::find(tap->_ips.begin(),tap->_ips.end(),ip) == tap->_ips.end()) {
tap->_ips.push_back(ip);
std::sort(tap->_ips.begin(),tap->_ips.end());
if (std::find(tap->_ips.begin(),tap->_ips.end(),ip) == tap->_ips.end()) {
tap->_ips.push_back(ip);
std::sort(tap->_ips.begin(),tap->_ips.end());
#if defined(LIBZT_IPV4)
if (ip.isV4()) {
// Set IP
static ip_addr_t ipaddr, netmask, gw;
IP4_ADDR(&gw,127,0,0,1);
ipaddr.addr = *((u32_t *)ip.rawIpData());
netmask.addr = *((u32_t *)ip.netmask().rawIpData());
netif_add(&(tap->lwipdev),&ipaddr, &netmask, &gw, NULL, tapif_init, ethernet_input);
tap->lwipdev.state = tap;
tap->lwipdev.output = etharp_output;
tap->_mac.copyTo(tap->lwipdev.hwaddr, 6);
tap->lwipdev.mtu = tap->_mtu;
tap->lwipdev.name[0] = 'l';
tap->lwipdev.name[1] = '4';
tap->lwipdev.linkoutput = low_level_output;
tap->lwipdev.hwaddr_len = 6;
tap->lwipdev.flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
netif_set_default(&(tap->lwipdev));
netif_set_up(&(tap->lwipdev));
DEBUG_INFO("addr=%s, netmask=%s", ip.toString().c_str(), ip.netmask().toString().c_str());
}
if (ip.isV4()) {
// Set IP
static ip_addr_t ipaddr, netmask, gw;
IP4_ADDR(&gw,127,0,0,1);
ipaddr.addr = *((u32_t *)ip.rawIpData());
netmask.addr = *((u32_t *)ip.netmask().rawIpData());
netif_add(&(tap->lwipdev),&ipaddr, &netmask, &gw, NULL, tapif_init, ethernet_input);
tap->lwipdev.state = tap;
tap->lwipdev.output = etharp_output;
tap->_mac.copyTo(tap->lwipdev.hwaddr, 6);
tap->lwipdev.mtu = tap->_mtu;
tap->lwipdev.name[0] = 'l';
tap->lwipdev.name[1] = '4';
tap->lwipdev.linkoutput = low_level_output;
tap->lwipdev.hwaddr_len = 6;
tap->lwipdev.flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
netif_set_default(&(tap->lwipdev));
netif_set_up(&(tap->lwipdev));
DEBUG_INFO("addr=%s, netmask=%s", ip.toString().c_str(), ip.netmask().toString().c_str());
}
#endif
#if defined(LIBZT_IPV6)
if(ip.isV6()) {
DEBUG_INFO("local_addr=%s", ip.toString().c_str());
static ip6_addr_t addr6;
struct sockaddr_in6 in6;
memcpy(in6.sin6_addr.s6_addr,ip.rawIpData(),16);
in6_to_ip6((ip6_addr *)&addr6, &in6);
tap->lwipdev6.mtu = tap->_mtu;
tap->lwipdev6.name[0] = 'l';
tap->lwipdev6.name[1] = '6';
tap->lwipdev6.hwaddr_len = 6;
tap->lwipdev6.linkoutput = low_level_output;
tap->lwipdev6.ip6_autoconfig_enabled = 1;
tap->_mac.copyTo(tap->lwipdev6.hwaddr, tap->lwipdev6.hwaddr_len);
netif_create_ip6_linklocal_address(&(tap->lwipdev6), 1);
netif_add(&(tap->lwipdev6), NULL, tapif_init, ethernet_input);
netif_set_default(&(tap->lwipdev6));
netif_set_up(&(tap->lwipdev6));
netif_ip6_addr_set_state(&(tap->lwipdev6), 1, IP6_ADDR_TENTATIVE);
ip6_addr_copy(ip_2_ip6(tap->lwipdev6.ip6_addr[1]), addr6);
tap->lwipdev6.output_ip6 = ethip6_output;
tap->lwipdev6.state = tap;
tap->lwipdev6.flags = NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
DEBUG_INFO("addr=%s, netmask=%s", ip.toString().c_str(), ip.netmask().toString().c_str());
}
if(ip.isV6()) {
DEBUG_INFO("local_addr=%s", ip.toString().c_str());
static ip6_addr_t addr6;
struct sockaddr_in6 in6;
memcpy(in6.sin6_addr.s6_addr,ip.rawIpData(),16);
in6_to_ip6((ip6_addr *)&addr6, &in6);
tap->lwipdev6.mtu = tap->_mtu;
tap->lwipdev6.name[0] = 'l';
tap->lwipdev6.name[1] = '6';
tap->lwipdev6.hwaddr_len = 6;
tap->lwipdev6.linkoutput = low_level_output;
tap->lwipdev6.ip6_autoconfig_enabled = 1;
tap->_mac.copyTo(tap->lwipdev6.hwaddr, tap->lwipdev6.hwaddr_len);
netif_create_ip6_linklocal_address(&(tap->lwipdev6), 1);
netif_add(&(tap->lwipdev6), NULL, tapif_init, ethernet_input);
netif_set_default(&(tap->lwipdev6));
netif_set_up(&(tap->lwipdev6));
netif_ip6_addr_set_state(&(tap->lwipdev6), 1, IP6_ADDR_TENTATIVE);
ip6_addr_copy(ip_2_ip6(tap->lwipdev6.ip6_addr[1]), addr6);
tap->lwipdev6.output_ip6 = ethip6_output;
tap->lwipdev6.state = tap;
tap->lwipdev6.flags = NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
DEBUG_INFO("addr=%s, netmask=%s", ip.toString().c_str(), ip.netmask().toString().c_str());
}
#endif
}
}
}
}
void lwIP::lwip_loop(SocketTap *tap)
{
DEBUG_INFO();
uint64_t prev_tcp_time = 0, prev_status_time = 0, prev_discovery_time = 0;
while(tap->_run)
{
uint64_t now = OSUtils::now();
uint64_t since_tcp = now - prev_tcp_time;
uint64_t since_discovery = now - prev_discovery_time;
uint64_t since_status = now - prev_status_time;
uint64_t tcp_remaining = LWIP_TCP_TIMER_INTERVAL;
uint64_t discovery_remaining = 5000;
void lwIP::lwip_loop(SocketTap *tap)
{
DEBUG_INFO();
uint64_t prev_tcp_time = 0, prev_status_time = 0, prev_discovery_time = 0;
while(tap->_run)
{
uint64_t now = OSUtils::now();
uint64_t since_tcp = now - prev_tcp_time;
uint64_t since_discovery = now - prev_discovery_time;
uint64_t since_status = now - prev_status_time;
uint64_t tcp_remaining = LWIP_TCP_TIMER_INTERVAL;
uint64_t discovery_remaining = 5000;
#if defined(LIBZT_IPV6)
#define DISCOVERY_INTERVAL 1000
#define DISCOVERY_INTERVAL 1000
#elif defined(LIBZT_IPV4)
#define DISCOVERY_INTERVAL ARP_TMR_INTERVAL
#define DISCOVERY_INTERVAL ARP_TMR_INTERVAL
#endif
// Main TCP/ETHARP timer section
if (since_tcp >= LWIP_TCP_TIMER_INTERVAL) {
prev_tcp_time = now;
tcp_tmr();
}
else {
tcp_remaining = LWIP_TCP_TIMER_INTERVAL - since_tcp;
}
if (since_discovery >= DISCOVERY_INTERVAL) {
prev_discovery_time = now;
// Main TCP/ETHARP timer section
if (since_tcp >= LWIP_TCP_TIMER_INTERVAL) {
prev_tcp_time = now;
tcp_tmr();
}
else {
tcp_remaining = LWIP_TCP_TIMER_INTERVAL - since_tcp;
}
if (since_discovery >= DISCOVERY_INTERVAL) {
prev_discovery_time = now;
#if defined(LIBZT_IPV4)
etharp_tmr();
etharp_tmr();
#endif
#if defined(LIBZT_IPV6)
nd6_tmr();
nd6_tmr();
#endif
} else {
discovery_remaining = DISCOVERY_INTERVAL - since_discovery;
}
tap->_phy.poll((unsigned long)std::min(tcp_remaining,discovery_remaining));
}
}
} else {
discovery_remaining = DISCOVERY_INTERVAL - since_discovery;
}
tap->_phy.poll((unsigned long)std::min(tcp_remaining,discovery_remaining));
}
}
void lwIP::lwip_rx(SocketTap *tap, const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
{
DEBUG_INFO();
struct pbuf *p,*q;
if (!tap->_enabled)
return;
struct eth_hdr ethhdr;
from.copyTo(ethhdr.src.addr, 6);
to.copyTo(ethhdr.dest.addr, 6);
ethhdr.type = ZeroTier::Utils::hton((uint16_t)etherType);
void lwIP::lwip_rx(SocketTap *tap, const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
{
DEBUG_INFO();
struct pbuf *p,*q;
if (!tap->_enabled)
return;
struct eth_hdr ethhdr;
from.copyTo(ethhdr.src.addr, 6);
to.copyTo(ethhdr.dest.addr, 6);
ethhdr.type = ZeroTier::Utils::hton((uint16_t)etherType);
p = pbuf_alloc(PBUF_RAW, len+sizeof(struct eth_hdr), PBUF_POOL);
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)) {
DEBUG_ERROR("dropped packet: first pbuf smaller than ethernet header");
return;
}
memcpy(q->payload,&ethhdr,sizeof(ethhdr));
memcpy((char*)q->payload + sizeof(ethhdr),dataptr,q->len - sizeof(ethhdr));
dataptr += q->len - sizeof(ethhdr);
p = pbuf_alloc(PBUF_RAW, len+sizeof(struct eth_hdr), PBUF_POOL);
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)) {
DEBUG_ERROR("dropped packet: first pbuf smaller than ethernet header");
return;
}
memcpy(q->payload,&ethhdr,sizeof(ethhdr));
memcpy((char*)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;
}
}
else {
DEBUG_ERROR("dropped packet: no pbufs available");
return;
}
{
// Remaining pbufs (if any) get rest of data
while ((q = q->next)) {
memcpy(q->payload,dataptr,q->len);
dataptr += q->len;
}
}
else {
DEBUG_ERROR("dropped packet: no pbufs available");
return;
}
{
#if defined(LIBZT_IPV6)
if(tap->lwipdev6.input(p, &(tap->lwipdev6)) != ERR_OK) {
DEBUG_ERROR("error while feeding frame into stack lwipdev6");
}
if(tap->lwipdev6.input(p, &(tap->lwipdev6)) != ERR_OK) {
DEBUG_ERROR("error while feeding frame into stack lwipdev6");
}
#endif
#if defined(LIBZT_IPV4)
if(tap->lwipdev.input(p, &(tap->lwipdev)) != ERR_OK) {
DEBUG_ERROR("error while feeding frame into stack lwipdev");
}
if(tap->lwipdev.input(p, &(tap->lwipdev)) != ERR_OK) {
DEBUG_ERROR("error while feeding frame into stack lwipdev");
}
#endif
}
}
}
}
int lwIP::lwip_Socket(void **pcb, int socket_family, int socket_type, int protocol)
{
// TODO: check lwIP timers, and max sockets
DEBUG_INFO();
if(socket_type == SOCK_STREAM) {
struct tcp_pcb *new_tcp_PCB = tcp_new();
*pcb = new_tcp_PCB;
return ERR_OK;
}
if(socket_type == SOCK_DGRAM) {
struct udp_pcb *new_udp_PCB = udp_new();
*pcb = new_udp_PCB;
return ERR_OK;
}
if(socket_type == SOCK_RAW) {
DEBUG_ERROR("SOCK_RAW, not currently supported.");
return -1;
}
return -1;
}
int lwIP::lwip_Socket(void **pcb, int socket_family, int socket_type, int protocol)
{
// TODO: check lwIP timers, and max sockets
DEBUG_INFO();
if(socket_type == SOCK_STREAM) {
struct tcp_pcb *new_tcp_PCB = tcp_new();
*pcb = new_tcp_PCB;
return ERR_OK;
}
if(socket_type == SOCK_DGRAM) {
struct udp_pcb *new_udp_PCB = udp_new();
*pcb = new_udp_PCB;
return ERR_OK;
}
if(socket_type == SOCK_RAW) {
DEBUG_ERROR("SOCK_RAW, not currently supported.");
return -1;
}
return -1;
}
int lwIP::lwip_Connect(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen)
{
DEBUG_INFO();
}
int lwIP::lwip_Connect(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen)
{
DEBUG_INFO();
}
int lwIP::lwip_Bind(SocketTap *tap, Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen)
{
DEBUG_INFO();
ip_addr_t ba;
char addrstr[INET6_ADDRSTRLEN];
int port = 0, err = 0;
int lwIP::lwip_Bind(SocketTap *tap, Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen)
{
DEBUG_INFO();
ip_addr_t ba;
char addrstr[INET6_ADDRSTRLEN];
int port = 0, err = 0;
#if defined(LIBZT_IPV4)
DEBUG_ERROR("A");
struct sockaddr_in *in4;
if(addr->sa_family == AF_INET) {
DEBUG_ERROR("A");
in4 = (struct sockaddr_in *)addr;
DEBUG_ERROR("A");
inet_ntop(AF_INET, &(in4->sin_addr), addrstr, INET_ADDRSTRLEN);
DEBUG_ERROR("A");
DEBUG_INFO("%s:%d", addrstr, lwip_ntohs(in4->sin_port));
}
ba = convert_ip(in4);
port = lwip_ntohs(in4->sin_port);
DEBUG_INFO("port=%d", port);
DEBUG_INFO("port=%d", lwip_ntohs(port));
DEBUG_ERROR("A");
struct sockaddr_in *in4;
if(addr->sa_family == AF_INET) {
DEBUG_ERROR("A");
in4 = (struct sockaddr_in *)addr;
DEBUG_ERROR("A");
inet_ntop(AF_INET, &(in4->sin_addr), addrstr, INET_ADDRSTRLEN);
DEBUG_ERROR("A");
DEBUG_INFO("%s:%d", addrstr, lwip_ntohs(in4->sin_port));
}
ba = convert_ip(in4);
port = lwip_ntohs(in4->sin_port);
DEBUG_INFO("port=%d", port);
DEBUG_INFO("port=%d", lwip_ntohs(port));
#endif
#if defined(LIBZT_IPV6)
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)&addr;
in6_to_ip6((ip6_addr *)&ba, in6);
if(addr->sa_family == AF_INET6) {
struct sockaddr_in6 *connaddr6 = (struct sockaddr_in6 *)addr;
inet_ntop(AF_INET6, &(connaddr6->sin6_addr), addrstr, INET6_ADDRSTRLEN);
DEBUG_INFO("%s:%d", addrstr, lwip_ntohs(connaddr6->sin6_port));
}
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)&addr;
in6_to_ip6((ip6_addr *)&ba, in6);
if(addr->sa_family == AF_INET6) {
struct sockaddr_in6 *connaddr6 = (struct sockaddr_in6 *)addr;
inet_ntop(AF_INET6, &(connaddr6->sin6_addr), addrstr, INET6_ADDRSTRLEN);
DEBUG_INFO("%s:%d", addrstr, lwip_ntohs(connaddr6->sin6_port));
}
#endif
if(conn->socket_type == SOCK_DGRAM) {
err = udp_bind((struct udp_pcb*)conn->pcb, (const ip_addr_t *)&ba, port);
if(err == ERR_USE) {
err = -1;
errno = EADDRINUSE; // port in use
}
else {
// set the recv callback
udp_recv((struct udp_pcb*)conn->pcb, nc_udp_recved, new ConnectionPair(tap, conn));
err = ERR_OK;
errno = ERR_OK; // success
}
}
else if (conn->socket_type == SOCK_STREAM) {
err = tcp_bind((struct tcp_pcb*)conn->pcb, (const ip_addr_t *)&ba, port);
if(err != ERR_OK) {
DEBUG_ERROR("err=%d", err);
if(err == ERR_USE){
err = -1;
errno = EADDRINUSE;
}
if(err == ERR_MEM){
err = -1;
errno = ENOMEM;
}
if(err == ERR_BUF){
err = -1;
errno = ENOMEM;
}
}
else {
err = ERR_OK;
errno = ERR_OK; // success
}
}
return err;
}
if(conn->socket_type == SOCK_DGRAM) {
err = udp_bind((struct udp_pcb*)conn->pcb, (const ip_addr_t *)&ba, port);
if(err == ERR_USE) {
err = -1;
errno = EADDRINUSE; // port in use
}
else {
// set the recv callback
udp_recv((struct udp_pcb*)conn->pcb, nc_udp_recved, new ConnectionPair(tap, conn));
err = ERR_OK;
errno = ERR_OK; // success
}
}
else if (conn->socket_type == SOCK_STREAM) {
err = tcp_bind((struct tcp_pcb*)conn->pcb, (const ip_addr_t *)&ba, port);
if(err != ERR_OK) {
DEBUG_ERROR("err=%d", err);
if(err == ERR_USE){
err = -1;
errno = EADDRINUSE;
}
if(err == ERR_MEM){
err = -1;
errno = ENOMEM;
}
if(err == ERR_BUF){
err = -1;
errno = ENOMEM;
}
}
else {
err = ERR_OK;
errno = ERR_OK; // success
}
}
return err;
}
int lwIP::lwip_Listen(SocketTap *tap, PhySocket *sock, PhySocket *rpcSock, void **uptr, struct listen_st *listen_rpc)
{
DEBUG_INFO();
// to be implemented
}
int lwIP::lwip_Listen(SocketTap *tap, PhySocket *sock, PhySocket *rpcSock, void **uptr, struct listen_st *listen_rpc)
{
DEBUG_INFO();
// to be implemented
}
int lwIP::lwip_Read(SocketTap *tap, PhySocket *sock, void **uptr, bool lwip_invoked)
{
DEBUG_EXTRA();
// to be implemented
}
int lwIP::lwip_Read(SocketTap *tap, PhySocket *sock, void **uptr, bool lwip_invoked)
{
DEBUG_EXTRA();
// to be implemented
}
int lwIP::lwip_Write(SocketTap *tap, Connection *conn)
{
DEBUG_EXTRA("conn=%p", (void*)&conn);
// to be implemented
}
int lwIP::lwip_Write(SocketTap *tap, Connection *conn)
{
DEBUG_EXTRA("conn=%p", (void*)&conn);
// to be implemented
}
int lwIP::lwip_Close(SocketTap *tap, PhySocket *sock, Connection *conn)
{
DEBUG_INFO();
// to be implemented
}
int lwIP::lwip_Close(SocketTap *tap, PhySocket *sock, Connection *conn)
{
DEBUG_INFO();
// to be implemented
}
/****************************************************************************/
/* Callbacks from lwIP stack */
/****************************************************************************/
/****************************************************************************/
/* Callbacks from lwIP stack */
/****************************************************************************/
err_t lwIP::nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *p, err_t err)
{
DEBUG_INFO();
// to be implemented
return ERR_OK;
}
err_t lwIP::nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *p, err_t err)
{
DEBUG_INFO();
// to be implemented
return ERR_OK;
}
err_t lwIP::nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err)
{
DEBUG_INFO();
// to be implemented
return -1;
}
void lwIP::nc_udp_recved(void * arg, struct udp_pcb * upcb, struct pbuf * p, const ip_addr_t * addr, u16_t port)
{
DEBUG_INFO();
// to be implemented
}
err_t lwIP::nc_sent(void* arg, struct tcp_pcb *PCB, u16_t len)
{
DEBUG_EXTRA("pcb=%p", (void*)&PCB);
// to be implemented
return ERR_OK;
}
err_t lwIP::nc_connected(void *arg, struct tcp_pcb *PCB, err_t err)
{
DEBUG_ATTN("pcb=%p", (void*)&PCB);
// to be implemented
return ERR_OK;
}
err_t lwIP::nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err)
{
DEBUG_INFO();
// to be implemented
return -1;
}
void lwIP::nc_udp_recved(void * arg, struct udp_pcb * upcb, struct pbuf * p, const ip_addr_t * addr, u16_t port)
{
DEBUG_INFO();
// to be implemented
}
err_t lwIP::nc_sent(void* arg, struct tcp_pcb *PCB, u16_t len)
{
DEBUG_EXTRA("pcb=%p", (void*)&PCB);
// to be implemented
return ERR_OK;
}
err_t lwIP::nc_connected(void *arg, struct tcp_pcb *PCB, err_t err)
{
DEBUG_ATTN("pcb=%p", (void*)&PCB);
// to be implemented
return ERR_OK;
}
err_t lwIP::nc_poll(void* arg, struct tcp_pcb *PCB)
{
DEBUG_INFO();
// to be implemented
return ERR_OK;
}
err_t lwIP::nc_poll(void* arg, struct tcp_pcb *PCB)
{
DEBUG_INFO();
// to be implemented
return ERR_OK;
}
void lwIP::nc_err(void *arg, err_t err)
{
DEBUG_INFO();
// to be implemented
}
void lwIP::nc_err(void *arg, err_t err)
{
DEBUG_INFO();
// to be implemented
}
}

View File

@@ -51,16 +51,16 @@ struct tcp_pcb;
struct netif;
#if defined(LIBZT_IPV4)
#define LWIP_NETIF_ADD_SIG struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input
#define LWIP_ETHARP_OUTPUT_SIG struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr
#define LWIP_NETIF_ADD_SIG struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input
#define LWIP_ETHARP_OUTPUT_SIG struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr
#endif
#if defined(LIBZT_IPV6)
#include "lwip/ip6_addr.h"
#define LWIP_NETIF_ADD_SIG struct netif *netif, void *state, netif_init_fn init, netif_input_fn input
#define LWIP_ETHIP6_OUTPUT_SIG struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr
#define LWIP_ETHARP_OUTPUT_SIG struct netif *netif, struct pbuf *q, const ip6_addr_t *ipaddr
#define LWIP_NETIF_IP6_ADDR_SET_STATE_SIG struct netif* netif, s8_t addr_idx, u8_t state
#define LWIP_NETIF_CREATE_IP6_LINKLOCAL_ADDRESS_SIG struct netif *netif, u8_t from_mac_48bit
#define LWIP_NETIF_ADD_SIG struct netif *netif, void *state, netif_init_fn init, netif_input_fn input
#define LWIP_ETHIP6_OUTPUT_SIG struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr
#define LWIP_ETHARP_OUTPUT_SIG struct netif *netif, struct pbuf *q, const ip6_addr_t *ipaddr
#define LWIP_NETIF_IP6_ADDR_SET_STATE_SIG struct netif* netif, s8_t addr_idx, u8_t state
#define LWIP_NETIF_CREATE_IP6_LINKLOCAL_ADDRESS_SIG struct netif *netif, u8_t from_mac_48bit
#endif
#define LWIP_PBUF_FREE_SIG struct pbuf *p
@@ -105,13 +105,13 @@ struct netif;
#if defined(LIBZT_IPV4)
extern "C" err_t etharp_output(LWIP_ETHARP_OUTPUT_SIG);
extern "C" err_t etharp_output(LWIP_ETHARP_OUTPUT_SIG);
#endif
#if defined(LIBZT_IPV6)
extern "C" void nd6_tmr(void);
extern "C" void netif_ip6_addr_set_state(LWIP_NETIF_IP6_ADDR_SET_STATE_SIG);
extern "C" void netif_create_ip6_linklocal_address(LWIP_NETIF_CREATE_IP6_LINKLOCAL_ADDRESS_SIG);
extern "C" err_t _ethip6_output(LWIP_ETHIP6_OUTPUT_SIG);
extern "C" void nd6_tmr(void);
extern "C" void netif_ip6_addr_set_state(LWIP_NETIF_IP6_ADDR_SET_STATE_SIG);
extern "C" void netif_create_ip6_linklocal_address(LWIP_NETIF_CREATE_IP6_LINKLOCAL_ADDRESS_SIG);
extern "C" err_t _ethip6_output(LWIP_ETHIP6_OUTPUT_SIG);
#endif
extern "C" void lwip_init();
@@ -161,45 +161,45 @@ extern "C" err_t ip_input(LWIP_IP_INPUT_SIG);
namespace ZeroTier {
class SocketTap;
struct Connection;
class SocketTap;
struct Connection;
class lwIP
{
public:
class lwIP
{
public:
/*
* Set up an interface in the network stack for the SocketTap
*/
void lwip_init_interface(SocketTap *tap, const InetAddress &ip);
/*
* Set up an interface in the network stack for the SocketTap
*/
void lwip_init_interface(SocketTap *tap, const InetAddress &ip);
/*
* Main stack loop
*/
void lwip_loop(SocketTap *tap);
/*
* Main stack loop
*/
void lwip_loop(SocketTap *tap);
/*
* Packets from the ZeroTier virtual wire enter the stack here
*/
void lwip_rx(SocketTap *tap, const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
int lwip_Socket(void **pcb, int socket_family, int socket_type, int protocol);
int lwip_Connect(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
int lwip_Bind(SocketTap *tap, Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
int lwip_Listen(SocketTap *tap, PhySocket *sock, PhySocket *rpcSock, void **uptr, struct listen_st *listen_rpc);
int lwip_Read(SocketTap *tap, PhySocket *sock, void **uptr, bool lwip_invoked);
int lwip_Write(SocketTap *tap, Connection *conn);
int lwip_Close(SocketTap *tap, PhySocket *sock, Connection *conn);
/*
* Packets from the ZeroTier virtual wire enter the stack here
*/
void lwip_rx(SocketTap *tap, const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
int lwip_Socket(void **pcb, int socket_family, int socket_type, int protocol);
int lwip_Connect(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
int lwip_Bind(SocketTap *tap, Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
int lwip_Listen(SocketTap *tap, PhySocket *sock, PhySocket *rpcSock, void **uptr, struct listen_st *listen_rpc);
int lwip_Read(SocketTap *tap, PhySocket *sock, void **uptr, bool lwip_invoked);
int lwip_Write(SocketTap *tap, Connection *conn);
int lwip_Close(SocketTap *tap, PhySocket *sock, Connection *conn);
static err_t nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *p, err_t err);
static err_t nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err);
static void nc_udp_recved(void * arg, struct udp_pcb * upcb, struct pbuf * p, const ip_addr_t * addr, u16_t port);
static void nc_err(void *arg, err_t err);
static err_t nc_poll(void* arg, struct tcp_pcb *PCB);
static err_t nc_sent(void *arg, struct tcp_pcb *PCB, u16_t len);
static err_t nc_connected(void *arg, struct tcp_pcb *PCB, err_t err);
};
static err_t nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *p, err_t err);
static err_t nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err);
static void nc_udp_recved(void * arg, struct udp_pcb * upcb, struct pbuf * p, const ip_addr_t * addr, u16_t port);
static void nc_err(void *arg, err_t err);
static err_t nc_poll(void* arg, struct tcp_pcb *PCB);
static err_t nc_sent(void *arg, struct tcp_pcb *PCB, u16_t len);
static err_t nc_connected(void *arg, struct tcp_pcb *PCB, err_t err);
};
}
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -73,12 +73,12 @@ namespace ZeroTier
/*
* Send raw frames from the stack to the ZeroTier virtual wire
*/
int pico_eth_send(struct pico_device *dev, void *buf, int len);
int pico_eth_send(struct pico_device *dev, void *buf, int len);
/*
/*
* Read raw frames from RX frame buffer into the stack
*/
int pico_eth_poll(struct pico_device *dev, int loop_score);
int pico_eth_poll(struct pico_device *dev, int loop_score);
class SocketTap;
struct Connection;
@@ -88,38 +88,38 @@ namespace ZeroTier
public:
/*
* Set up an interface in the network stack for the SocketTap
*/
* Set up an interface in the network stack for the SocketTap
*/
bool pico_init_interface(ZeroTier::SocketTap *tap, const ZeroTier::InetAddress &ip);
/*
* Main stack loop
*/
* Main stack loop
*/
void pico_loop(SocketTap *tap);
/*
* Read bytes from the stack to the RX buffer (prepare to be read by app)
*/
* Read bytes from the stack to the RX buffer (prepare to be read by app)
*/
static void pico_cb_tcp_read(SocketTap *tap, struct pico_socket *s);
/*
* Read bytes from the stack to the RX buffer (prepare to be read by app)
*/
static void pico_cb_udp_read(SocketTap *tap, struct pico_socket *s);
* Read bytes from the stack to the RX buffer (prepare to be read by app)
*/
static void pico_cb_udp_read(SocketTap *tap, struct pico_socket *s);
/*
* Write bytes from TX buffer to stack (prepare to be sent to ZT virtual wire)
*/
/*
* Write bytes from TX buffer to stack (prepare to be sent to ZT virtual wire)
*/
static void pico_cb_tcp_write(SocketTap *tap, struct pico_socket *s);
/*
* Write bytes from TX buffer to stack (prepare to be sent to ZT virtual wire)
*/
static void pico_cb_socket_activity(uint16_t ev, struct pico_socket *s);
* Write bytes from TX buffer to stack (prepare to be sent to ZT virtual wire)
*/
static void pico_cb_socket_activity(uint16_t ev, struct pico_socket *s);
/*
* Packets from the ZeroTier virtual wire enter the stack here
*/
* Packets from the ZeroTier virtual wire enter the stack here
*/
void pico_rx(SocketTap *tap, const ZeroTier::MAC &from,const ZeroTier::MAC &to,unsigned int etherType,const void *data,unsigned int len);
/*
@@ -130,39 +130,39 @@ namespace ZeroTier
/*
* Connect to remote host via userspace network stack interface - Called from SocketTap
*/
int pico_Connect(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
/*
int pico_Connect(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
/*
* Bind to a userspace network stack interface - Called from SocketTap
*/
int pico_Bind(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
/*
int pico_Bind(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
/*
* Listen for incoming connections - Called from SocketTap
*/
int pico_Listen(Connection *conn, int fd, int backlog);
int pico_Listen(Connection *conn, int fd, int backlog);
/*
/*
* Accept an incoming connection - Called from SocketTap
*/
Connection* pico_Accept(Connection *conn);
Connection* pico_Accept(Connection *conn);
/*
/*
* Read from RX buffer to application - Called from SocketTap
*/
int pico_Read(SocketTap *tap, ZeroTier::PhySocket *sock,Connection *conn,bool stack_invoked);
int pico_Read(SocketTap *tap, ZeroTier::PhySocket *sock,Connection *conn,bool stack_invoked);
/*
/*
* Write to userspace network stack - Called from SocketTap
*/
int pico_Write(Connection *conn, void *data, ssize_t len);
int pico_Write(Connection *conn, void *data, ssize_t len);
/*
/*
* Close a Connection - Called from SocketTap
*/
int pico_Close(Connection *conn);
/*
/*
* Converts picoTCP error codes to pretty string
*/
static char *beautify_pico_error(int err);