Browse Source
also update for changed function calls that now accept a local address # Conflicts: # include/ZeroTierOne.h # java/CMakeLists.txt # java/jni/Android.mk # java/jni/ZT1_jnicache.cpp # java/jni/ZT1_jnilookup.h # java/jni/ZT1_jniutils.cpp # java/jni/com_zerotierone_sdk_Node.cpppull/1/head
55 changed files with 3825 additions and 2242 deletions
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,419 @@
|
||||
/*
|
||||
* 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
|
||||
*/ |
||||
|
||||
#ifndef ZT_HASHTABLE_HPP |
||||
#define ZT_HASHTABLE_HPP |
||||
|
||||
#include <stdint.h> |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
|
||||
#include <stdexcept> |
||||
#include <vector> |
||||
#include <utility> |
||||
#include <algorithm> |
||||
|
||||
namespace ZeroTier { |
||||
|
||||
/**
|
||||
* A minimal hash table implementation for the ZeroTier core |
||||
* |
||||
* This is not a drop-in replacement for STL containers, and has several |
||||
* limitations. Keys can be uint64_t or an object, and if the latter they |
||||
* must implement a method called hashCode() that returns an unsigned long |
||||
* value that is evenly distributed. |
||||
*/ |
||||
template<typename K,typename V> |
||||
class Hashtable |
||||
{ |
||||
private: |
||||
struct _Bucket |
||||
{ |
||||
_Bucket(const K &k,const V &v) : k(k),v(v) {} |
||||
_Bucket(const K &k) : k(k),v() {} |
||||
_Bucket(const _Bucket &b) : k(b.k),v(b.v) {} |
||||
inline _Bucket &operator=(const _Bucket &b) { k = b.k; v = b.v; return *this; } |
||||
K k; |
||||
V v; |
||||
_Bucket *next; // must be set manually for each _Bucket
|
||||
}; |
||||
|
||||
public: |
||||
/**
|
||||
* A simple forward iterator (different from STL) |
||||
* |
||||
* It's safe to erase the last key, but not others. Don't use set() since that |
||||
* may rehash and invalidate the iterator. Note the erasing the key will destroy |
||||
* the targets of the pointers returned by next(). |
||||
*/ |
||||
class Iterator |
||||
{ |
||||
public: |
||||
/**
|
||||
* @param ht Hash table to iterate over |
||||
*/ |
||||
Iterator(Hashtable &ht) : |
||||
_idx(0), |
||||
_ht(&ht), |
||||
_b(ht._t[0]) |
||||
{ |
||||
} |
||||
|
||||
/**
|
||||
* @param kptr Pointer to set to point to next key |
||||
* @param vptr Pointer to set to point to next value |
||||
* @return True if kptr and vptr are set, false if no more entries |
||||
*/ |
||||
inline bool next(K *&kptr,V *&vptr) |
||||
{ |
||||
for(;;) { |
||||
if (_b) { |
||||
kptr = &(_b->k); |
||||
vptr = &(_b->v); |
||||
_b = _b->next; |
||||
return true; |
||||
} |
||||
++_idx; |
||||
if (_idx >= _ht->_bc) |
||||
return false; |
||||
_b = _ht->_t[_idx]; |
||||
} |
||||
} |
||||
|
||||
private: |
||||
unsigned long _idx; |
||||
Hashtable *_ht; |
||||
Hashtable::_Bucket *_b; |
||||
}; |
||||
friend class Hashtable::Iterator; |
||||
|
||||
/**
|
||||
* @param bc Initial capacity in buckets (default: 128, must be nonzero) |
||||
*/ |
||||
Hashtable(unsigned long bc = 128) : |
||||
_t(reinterpret_cast<_Bucket **>(::malloc(sizeof(_Bucket *) * bc))), |
||||
_bc(bc), |
||||
_s(0) |
||||
{ |
||||
if (!_t) |
||||
throw std::bad_alloc(); |
||||
for(unsigned long i=0;i<bc;++i) |
||||
_t[i] = (_Bucket *)0; |
||||
} |
||||
|
||||
Hashtable(const Hashtable<K,V> &ht) : |
||||
_t(reinterpret_cast<_Bucket **>(::malloc(sizeof(_Bucket *) * ht._bc))), |
||||
_bc(ht._bc), |
||||
_s(ht._s) |
||||
{ |
||||
if (!_t) |
||||
throw std::bad_alloc(); |
||||
for(unsigned long i=0;i<_bc;++i) |
||||
_t[i] = (_Bucket *)0; |
||||
for(unsigned long i=0;i<_bc;++i) { |
||||
const _Bucket *b = ht._t[i]; |
||||
while (b) { |
||||
_Bucket *nb = new _Bucket(*b); |
||||
nb->next = _t[i]; |
||||
_t[i] = nb; |
||||
b = b->next; |
||||
} |
||||
} |
||||
} |
||||
|
||||
~Hashtable() |
||||
{ |
||||
this->clear(); |
||||
::free(_t); |
||||
} |
||||
|
||||
inline Hashtable &operator=(const Hashtable<K,V> &ht) |
||||
{ |
||||
this->clear(); |
||||
if (ht._s) { |
||||
for(unsigned long i=0;i<ht._bc;++i) { |
||||
const _Bucket *b = ht._t[i]; |
||||
while (b) { |
||||
this->set(b->k,b->v); |
||||
b = b->next; |
||||
} |
||||
} |
||||
} |
||||
return *this; |
||||
} |
||||
|
||||
/**
|
||||
* Erase all entries |
||||
*/ |
||||
inline void clear() |
||||
{ |
||||
if (_s) { |
||||
for(unsigned long i=0;i<_bc;++i) { |
||||
_Bucket *b = _t[i]; |
||||
while (b) { |
||||
_Bucket *const nb = b->next; |
||||
delete b; |
||||
b = nb; |
||||
} |
||||
_t[i] = (_Bucket *)0; |
||||
} |
||||
_s = 0; |
||||
} |
||||
} |
||||
|
||||
/**
|
||||
* @return Vector of all keys |
||||
*/ |
||||
inline typename std::vector<K> keys() const |
||||
{ |
||||
typename std::vector<K> k; |
||||
if (_s) { |
||||
k.reserve(_s); |
||||
for(unsigned long i=0;i<_bc;++i) { |
||||
_Bucket *b = _t[i]; |
||||
while (b) { |
||||
k.push_back(b->k); |
||||
b = b->next; |
||||
} |
||||
} |
||||
} |
||||
return k; |
||||
} |
||||
|
||||
/**
|
||||
* Append all keys (in unspecified order) to the supplied vector or list |
||||
* |
||||
* @param v Vector, list, or other compliant container |
||||
* @tparam Type of V (generally inferred) |
||||
*/ |
||||
template<typename C> |
||||
inline void appendKeys(C &v) const |
||||
{ |
||||
if (_s) { |
||||
for(unsigned long i=0;i<_bc;++i) { |
||||
_Bucket *b = _t[i]; |
||||
while (b) { |
||||
v.push_back(b->k); |
||||
b = b->next; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/**
|
||||
* @return Vector of all entries (pairs of K,V) |
||||
*/ |
||||
inline typename std::vector< std::pair<K,V> > entries() const |
||||
{ |
||||
typename std::vector< std::pair<K,V> > k; |
||||
if (_s) { |
||||
k.reserve(_s); |
||||
for(unsigned long i=0;i<_bc;++i) { |
||||
_Bucket *b = _t[i]; |
||||
while (b) { |
||||
k.push_back(std::pair<K,V>(b->k,b->v)); |
||||
b = b->next; |
||||
} |
||||
} |
||||
} |
||||
return k; |
||||
} |
||||
|
||||
/**
|
||||
* @param k Key |
||||
* @return Pointer to value or NULL if not found |
||||
*/ |
||||
inline V *get(const K &k) |
||||
{ |
||||
_Bucket *b = _t[_hc(k) % _bc]; |
||||
while (b) { |
||||
if (b->k == k) |
||||
return &(b->v); |
||||
b = b->next; |
||||
} |
||||
return (V *)0; |
||||
} |
||||
inline const V *get(const K &k) const { return const_cast<Hashtable *>(this)->get(k); } |
||||
|
||||
/**
|
||||
* @param k Key to check |
||||
* @return True if key is present |
||||
*/ |
||||
inline bool contains(const K &k) const |
||||
{ |
||||
_Bucket *b = _t[_hc(k) % _bc]; |
||||
while (b) { |
||||
if (b->k == k) |
||||
return true; |
||||
b = b->next; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/**
|
||||
* @param k Key |
||||
* @return True if value was present |
||||
*/ |
||||
inline bool erase(const K &k) |
||||
{ |
||||
const unsigned long bidx = _hc(k) % _bc; |
||||
_Bucket *lastb = (_Bucket *)0; |
||||
_Bucket *b = _t[bidx]; |
||||
while (b) { |
||||
if (b->k == k) { |
||||
if (lastb) |
||||
lastb->next = b->next; |
||||
else _t[bidx] = b->next; |
||||
delete b; |
||||
--_s; |
||||
return true; |
||||
} |
||||
lastb = b; |
||||
b = b->next; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/**
|
||||
* @param k Key |
||||
* @param v Value |
||||
* @return Reference to value in table |
||||
*/ |
||||
inline V &set(const K &k,const V &v) |
||||
{ |
||||
const unsigned long h = _hc(k); |
||||
unsigned long bidx = h % _bc; |
||||
|
||||
_Bucket *b = _t[bidx]; |
||||
while (b) { |
||||
if (b->k == k) { |
||||
b->v = v; |
||||
return b->v; |
||||
} |
||||
b = b->next; |
||||
} |
||||
|
||||
if (_s >= _bc) { |
||||
_grow(); |
||||
bidx = h % _bc; |
||||
} |
||||
|
||||
b = new _Bucket(k,v); |
||||
b->next = _t[bidx]; |
||||
_t[bidx] = b; |
||||
++_s; |
||||
|
||||
return b->v; |
||||
} |
||||
|
||||
/**
|
||||
* @param k Key |
||||
* @return Value, possibly newly created |
||||
*/ |
||||
inline V &operator[](const K &k) |
||||
{ |
||||
const unsigned long h = _hc(k); |
||||
unsigned long bidx = h % _bc; |
||||
|
||||
_Bucket *b = _t[bidx]; |
||||
while (b) { |
||||
if (b->k == k) |
||||
return b->v; |
||||
b = b->next; |
||||
} |
||||
|
||||
if (_s >= _bc) { |
||||
_grow(); |
||||
bidx = h % _bc; |
||||
} |
||||
|
||||
b = new _Bucket(k); |
||||
b->next = _t[bidx]; |
||||
_t[bidx] = b; |
||||
++_s; |
||||
|
||||
return b->v; |
||||
} |
||||
|
||||
/**
|
||||
* @return Number of entries |
||||
*/ |
||||
inline unsigned long size() const throw() { return _s; } |
||||
|
||||
/**
|
||||
* @return True if table is empty |
||||
*/ |
||||
inline bool empty() const throw() { return (_s == 0); } |
||||
|
||||
private: |
||||
template<typename O> |
||||
static inline unsigned long _hc(const O &obj) |
||||
{ |
||||
return obj.hashCode(); |
||||
} |
||||
static inline unsigned long _hc(const uint64_t i) |
||||
{ |
||||
/* NOTE: this assumes that 'i' is evenly distributed, which is the case for
|
||||
* packet IDs and network IDs -- the two use cases in ZT for uint64_t keys. |
||||
* These values are also greater than 0xffffffff so they'll map onto a full |
||||
* bucket count just fine no matter what happens. Normally you'd want to |
||||
* hash an integer key index in a hash table. */ |
||||
return (unsigned long)i; |
||||
} |
||||
static inline unsigned long _hc(const uint32_t i) |
||||
{ |
||||
// In the uint32_t case we use a simple multiplier for hashing to ensure coverage
|
||||
return ((unsigned long)i * (unsigned long)0x9e3779b1); |
||||
} |
||||
|
||||
inline void _grow() |
||||
{ |
||||
const unsigned long nc = _bc * 2; |
||||
_Bucket **nt = reinterpret_cast<_Bucket **>(::malloc(sizeof(_Bucket *) * nc)); |
||||
if (nt) { |
||||
for(unsigned long i=0;i<nc;++i) |
||||
nt[i] = (_Bucket *)0; |
||||
for(unsigned long i=0;i<_bc;++i) { |
||||
_Bucket *b = _t[i]; |
||||
while (b) { |
||||
_Bucket *const nb = b->next; |
||||
const unsigned long nidx = _hc(b->k) % nc; |
||||
b->next = nt[nidx]; |
||||
nt[nidx] = b; |
||||
b = nb; |
||||
} |
||||
} |
||||
::free(_t); |
||||
_t = nt; |
||||
_bc = nc; |
||||
} |
||||
} |
||||
|
||||
_Bucket **_t; |
||||
unsigned long _bc; |
||||
unsigned long _s; |
||||
}; |
||||
|
||||
} // namespace ZeroTier
|
||||
|
||||
#endif |
||||
@ -0,0 +1,134 @@
|
||||
/*
|
||||
* 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/
|
||||
*/ |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#include <stdlib.h> |
||||
|
||||
#include "Arp.hpp" |
||||
#include "OSUtils.hpp" |
||||
|
||||
namespace ZeroTier { |
||||
|
||||
static const uint8_t ARP_REQUEST_HEADER[8] = { 0x00,0x01,0x08,0x00,0x06,0x04,0x00,0x01 }; |
||||
static const uint8_t ARP_RESPONSE_HEADER[8] = { 0x00,0x01,0x08,0x00,0x06,0x04,0x00,0x02 }; |
||||
|
||||
Arp::Arp() : |
||||
_cache(256), |
||||
_lastCleaned(OSUtils::now()) |
||||
{ |
||||
} |
||||
|
||||
void Arp::addLocal(uint32_t ip,const MAC &mac) |
||||
{ |
||||
_ArpEntry &e = _cache[ip]; |
||||
e.lastQuerySent = 0; // local IP
|
||||
e.lastResponseReceived = 0; // local IP
|
||||
e.mac = mac; |
||||
e.local = true; |
||||
} |
||||
|
||||
void Arp::remove(uint32_t ip) |
||||
{ |
||||
_cache.erase(ip); |
||||
} |
||||
|
||||
uint32_t Arp::processIncomingArp(const void *arp,unsigned int len,void *response,unsigned int &responseLen,MAC &responseDest) |
||||
{ |
||||
const uint64_t now = OSUtils::now(); |
||||
uint32_t ip = 0; |
||||
|
||||
responseLen = 0; |
||||
responseDest.zero(); |
||||
|
||||
if (len > 28) { |
||||
if (!memcmp(arp,ARP_REQUEST_HEADER,8)) { |
||||
// Respond to ARP requests for locally-known IPs
|
||||
_ArpEntry *targetEntry = _cache.get(reinterpret_cast<const uint32_t *>(arp)[6]); |
||||
if ((targetEntry)&&(targetEntry->local)) { |
||||
memcpy(response,ARP_RESPONSE_HEADER,8); |
||||
targetEntry->mac.copyTo(reinterpret_cast<uint8_t *>(response) + 8,6); |
||||
memcpy(reinterpret_cast<uint8_t *>(response) + 14,reinterpret_cast<const uint8_t *>(arp) + 24,4); |
||||
memcpy(reinterpret_cast<uint8_t *>(response) + 18,reinterpret_cast<const uint8_t *>(arp) + 8,10); |
||||
responseLen = 28; |
||||
responseDest.setTo(reinterpret_cast<const uint8_t *>(arp) + 8,6); |
||||
} |
||||
} else if (!memcmp(arp,ARP_RESPONSE_HEADER,8)) { |
||||
// Learn cache entries for remote IPs from relevant ARP replies
|
||||
uint32_t responseIp = 0; |
||||
memcpy(&responseIp,reinterpret_cast<const uint8_t *>(arp) + 14,4); |
||||
_ArpEntry *queryEntry = _cache.get(responseIp); |
||||
if ((queryEntry)&&(!queryEntry->local)&&((now - queryEntry->lastQuerySent) <= ZT_ARP_QUERY_MAX_TTL)) { |
||||
queryEntry->lastResponseReceived = now; |
||||
queryEntry->mac.setTo(reinterpret_cast<const uint8_t *>(arp) + 8,6); |
||||
ip = responseIp; |
||||
} |
||||
} |
||||
} |
||||
|
||||
if ((now - _lastCleaned) >= ZT_ARP_EXPIRE) { |
||||
_lastCleaned = now; |
||||
Hashtable< uint32_t,_ArpEntry >::Iterator i(_cache); |
||||
uint32_t *k = (uint32_t *)0; |
||||
_ArpEntry *v = (_ArpEntry *)0; |
||||
while (i.next(k,v)) { |
||||
if ((!v->local)&&((now - v->lastResponseReceived) >= ZT_ARP_EXPIRE)) |
||||
_cache.erase(*k); |
||||
} |
||||
} |
||||
|
||||
return ip; |
||||
} |
||||
|
||||
MAC Arp::query(const MAC &localMac,uint32_t ip,void *query,unsigned int &queryLen,MAC &queryDest) |
||||
{ |
||||
const uint64_t now = OSUtils::now(); |
||||
|
||||
_ArpEntry &e = _cache[ip]; |
||||
|
||||
if ( ((e.mac)&&((now - e.lastResponseReceived) >= (ZT_ARP_EXPIRE / 3))) || |
||||
((!e.mac)&&((now - e.lastQuerySent) >= ZT_ARP_QUERY_INTERVAL)) ) { |
||||
e.lastQuerySent = now; |
||||
|
||||
uint8_t *q = reinterpret_cast<uint8_t *>(query); |
||||
memcpy(q,ARP_REQUEST_HEADER,8); q += 8; // ARP request header information, always the same
|
||||
localMac.copyTo(q,6); q += 6; // sending host address
|
||||
memset(q,0,10); q += 10; // sending IP and target media address are ignored in requests
|
||||
memcpy(q,&ip,4); // target IP address for resolution (IP already in big-endian byte order)
|
||||
queryLen = 28; |
||||
if (e.mac) |
||||
queryDest = e.mac; // confirmation query, send directly to address holder
|
||||
else queryDest = (uint64_t)0xffffffffffffULL; // broadcast query
|
||||
} else { |
||||
queryLen = 0; |
||||
queryDest.zero(); |
||||
} |
||||
|
||||
return e.mac; |
||||
} |
||||
|
||||
} // namespace ZeroTier
|
||||
@ -0,0 +1,156 @@
|
||||
/*
|
||||
* 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/
|
||||
*/ |
||||
|
||||
#ifndef ZT_ARP_HPP |
||||
#define ZT_ARP_HPP |
||||
|
||||
#include <stdint.h> |
||||
|
||||
#include <utility> |
||||
|
||||
#include "../node/Constants.hpp" |
||||
#include "../node/Hashtable.hpp" |
||||
#include "../node/MAC.hpp" |
||||
|
||||
/**
|
||||
* Maximum possible ARP length |
||||
* |
||||
* ARPs are 28 bytes in length, but specify a 128 byte buffer since |
||||
* some weird extensions we may support in the future can pad them |
||||
* out to as long as 72 bytes. |
||||
*/ |
||||
#define ZT_ARP_BUF_LENGTH 128 |
||||
|
||||
/**
|
||||
* Minimum permitted interval between sending ARP queries for a given IP |
||||
*/ |
||||
#define ZT_ARP_QUERY_INTERVAL 2000 |
||||
|
||||
/**
|
||||
* Maximum time between query and response, otherwise responses are discarded to prevent poisoning |
||||
*/ |
||||
#define ZT_ARP_QUERY_MAX_TTL 5000 |
||||
|
||||
/**
|
||||
* ARP expiration time |
||||
*/ |
||||
#define ZT_ARP_EXPIRE 600000 |
||||
|
||||
namespace ZeroTier { |
||||
|
||||
/**
|
||||
* ARP cache and resolver |
||||
* |
||||
* To implement ARP: |
||||
* |
||||
* (1) Call processIncomingArp() on all ARP packets received and then always |
||||
* check responseLen after calling. If it is non-zero, send the contents |
||||
* of response to responseDest. |
||||
* |
||||
* (2) Call query() to look up IP addresses, and then check queryLen. If it |
||||
* is non-zero, send the contents of query to queryDest (usually broadcast). |
||||
* |
||||
* Note that either of these functions can technically generate a response or |
||||
* a query at any time, so their result parameters for sending ARPs should |
||||
* always be checked. |
||||
* |
||||
* This class is not thread-safe and must be guarded if used in multi-threaded |
||||
* code. |
||||
*/ |
||||
class Arp |
||||
{ |
||||
public: |
||||
Arp(); |
||||
|
||||
/**
|
||||
* Set a local IP entry that we should respond to ARPs for |
||||
* |
||||
* @param mac Our local MAC address |
||||
* @param ip IP in big-endian byte order (sin_addr.s_addr) |
||||
*/ |
||||
void addLocal(uint32_t ip,const MAC &mac); |
||||
|
||||
/**
|
||||
* Delete a local IP entry or a cached ARP entry |
||||
* |
||||
* @param ip IP in big-endian byte order (sin_addr.s_addr) |
||||
*/ |
||||
void remove(uint32_t ip); |
||||
|
||||
/**
|
||||
* Process ARP packets |
||||
* |
||||
* For ARP queries, a response is generated and responseLen is set to its |
||||
* frame payload length in bytes. |
||||
* |
||||
* For ARP responses, the cache is populated and the IP address entry that |
||||
* was learned is returned. |
||||
* |
||||
* @param arp ARP frame data |
||||
* @param len Length of ARP frame (usually 28) |
||||
* @param response Response buffer -- MUST be a minimum of ZT_ARP_BUF_LENGTH in size |
||||
* @param responseLen Response length, or set to 0 if no response |
||||
* @param responseDest Destination of response, or set to null if no response |
||||
* @return IP address learned or 0 if no new IPs in cache |
||||
*/ |
||||
uint32_t processIncomingArp(const void *arp,unsigned int len,void *response,unsigned int &responseLen,MAC &responseDest); |
||||
|
||||
/**
|
||||
* Get the MAC corresponding to an IP, generating a query if needed |
||||
* |
||||
* This returns a MAC for a remote IP. The local MAC is returned for local |
||||
* IPs as well. It may also generate a query if the IP is not known or the |
||||
* entry needs to be refreshed. In this case queryLen will be set to a |
||||
* non-zero value, so this should always be checked on return even if the |
||||
* MAC returned is non-null. |
||||
* |
||||
* @param localMac Local MAC address of host interface |
||||
* @param ip IP to look up |
||||
* @param query Buffer for generated query -- MUST be a minimum of ZT_ARP_BUF_LENGTH in size |
||||
* @param queryLen Length of generated query, or set to 0 if no query generated |
||||
* @param queryDest Destination of query, or set to null if no query generated |
||||
* @return MAC or 0 if no cached entry for this IP |
||||
*/ |
||||
MAC query(const MAC &localMac,uint32_t ip,void *query,unsigned int &queryLen,MAC &queryDest); |
||||
|
||||
private: |
||||
struct _ArpEntry |
||||
{ |
||||
_ArpEntry() : lastQuerySent(0),lastResponseReceived(0),mac(),local(false) {} |
||||
uint64_t lastQuerySent; // Time last query was sent or 0 for local IP
|
||||
uint64_t lastResponseReceived; // Time of last ARP response or 0 for local IP
|
||||
MAC mac; // MAC address of device responsible for IP or null if not known yet
|
||||
bool local; // True if this is a local ARP entry
|
||||
}; |
||||
|
||||
Hashtable< uint32_t,_ArpEntry > _cache; |
||||
uint64_t _lastCleaned; |
||||
}; |
||||
|
||||
} // namespace ZeroTier
|
||||
|
||||
#endif |
||||
Loading…
Reference in new issue