2015-02-02 23:24:05 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2016-01-12 22:04:55 +00:00
|
|
|
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
|
2015-02-02 23:24:05 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2015-04-07 03:17:21 +00:00
|
|
|
#ifndef ZT_SELFAWARENESS_HPP
|
|
|
|
#define ZT_SELFAWARENESS_HPP
|
2015-02-02 23:24:05 +00:00
|
|
|
|
2015-09-04 21:24:31 +00:00
|
|
|
#include "Constants.hpp"
|
2015-02-02 23:24:05 +00:00
|
|
|
#include "InetAddress.hpp"
|
2015-09-04 21:24:31 +00:00
|
|
|
#include "Hashtable.hpp"
|
2015-04-17 19:19:01 +00:00
|
|
|
#include "Address.hpp"
|
2015-04-07 17:57:59 +00:00
|
|
|
#include "Mutex.hpp"
|
2015-02-02 23:24:05 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2015-04-07 03:17:21 +00:00
|
|
|
class RuntimeEnvironment;
|
|
|
|
|
2015-02-02 23:24:05 +00:00
|
|
|
/**
|
|
|
|
* Tracks changes to this peer's real world addresses
|
|
|
|
*/
|
2015-04-07 03:17:21 +00:00
|
|
|
class SelfAwareness
|
2015-02-02 23:24:05 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-04-07 03:17:21 +00:00
|
|
|
SelfAwareness(const RuntimeEnvironment *renv);
|
2015-02-02 23:24:05 +00:00
|
|
|
|
|
|
|
/**
|
2015-04-07 03:17:21 +00:00
|
|
|
* Called when a trusted remote peer informs us of our external network address
|
2015-02-02 23:24:05 +00:00
|
|
|
*
|
2015-04-17 19:19:01 +00:00
|
|
|
* @param reporter ZeroTier address of reporting peer
|
2016-02-22 17:47:50 +00:00
|
|
|
* @param receivedOnLocalAddress Local address on which report was received
|
2015-04-07 18:56:10 +00:00
|
|
|
* @param reporterPhysicalAddress Physical address that reporting peer seems to have
|
|
|
|
* @param myPhysicalAddress Physical address that peer says we have
|
|
|
|
* @param trusted True if this peer is trusted as an authority to inform us of external address changes
|
2015-05-05 01:34:30 +00:00
|
|
|
* @param now Current time
|
|
|
|
*/
|
2016-02-22 17:47:50 +00:00
|
|
|
void iam(const Address &reporter,const InetAddress &receivedOnLocalAddress,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted,uint64_t now);
|
2015-05-05 01:34:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up database periodically
|
|
|
|
*
|
|
|
|
* @param now Current time
|
2015-02-02 23:24:05 +00:00
|
|
|
*/
|
2015-05-05 01:34:30 +00:00
|
|
|
void clean(uint64_t now);
|
2015-02-02 23:24:05 +00:00
|
|
|
|
2016-02-11 02:41:39 +00:00
|
|
|
/**
|
|
|
|
* If we appear to be behind a symmetric NAT, get predictions for possible external endpoints
|
|
|
|
*
|
|
|
|
* @return Symmetric NAT predictions or empty vector if none
|
|
|
|
*/
|
|
|
|
std::vector<InetAddress> getSymmetricNatPredictions();
|
|
|
|
|
2015-02-02 23:24:05 +00:00
|
|
|
private:
|
2015-05-05 01:34:30 +00:00
|
|
|
struct PhySurfaceKey
|
|
|
|
{
|
|
|
|
Address reporter;
|
2016-02-22 17:47:50 +00:00
|
|
|
InetAddress receivedOnLocalAddress;
|
2015-10-23 20:03:34 +00:00
|
|
|
InetAddress reporterPhysicalAddress;
|
2015-05-05 01:34:30 +00:00
|
|
|
InetAddress::IpScope scope;
|
|
|
|
|
|
|
|
PhySurfaceKey() : reporter(),scope(InetAddress::IP_SCOPE_NONE) {}
|
2016-02-22 17:47:50 +00:00
|
|
|
PhySurfaceKey(const Address &r,const InetAddress &rol,const InetAddress &ra,InetAddress::IpScope s) : reporter(r),receivedOnLocalAddress(rol),reporterPhysicalAddress(ra),scope(s) {}
|
2015-10-23 20:03:34 +00:00
|
|
|
|
|
|
|
inline unsigned long hashCode() const throw() { return ((unsigned long)reporter.toInt() + (unsigned long)scope); }
|
2016-02-22 17:47:50 +00:00
|
|
|
inline bool operator==(const PhySurfaceKey &k) const throw() { return ((reporter == k.reporter)&&(receivedOnLocalAddress == k.receivedOnLocalAddress)&&(reporterPhysicalAddress == k.reporterPhysicalAddress)&&(scope == k.scope)); }
|
2015-05-05 01:34:30 +00:00
|
|
|
};
|
|
|
|
struct PhySurfaceEntry
|
|
|
|
{
|
|
|
|
InetAddress mySurface;
|
|
|
|
uint64_t ts;
|
2016-08-10 00:00:01 +00:00
|
|
|
bool trusted;
|
2015-05-05 01:34:30 +00:00
|
|
|
|
2016-08-10 00:00:01 +00:00
|
|
|
PhySurfaceEntry() : mySurface(),ts(0),trusted(false) {}
|
|
|
|
PhySurfaceEntry(const InetAddress &a,const uint64_t t) : mySurface(a),ts(t),trusted(false) {}
|
2015-05-05 01:34:30 +00:00
|
|
|
};
|
|
|
|
|
2015-04-07 17:57:59 +00:00
|
|
|
const RuntimeEnvironment *RR;
|
2015-05-05 01:34:30 +00:00
|
|
|
|
2015-09-04 21:24:31 +00:00
|
|
|
Hashtable< PhySurfaceKey,PhySurfaceEntry > _phy;
|
2015-05-05 01:34:30 +00:00
|
|
|
Mutex _phy_m;
|
2015-02-02 23:24:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|