2016-04-05 01:46:41 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
|
|
|
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZT_BINDER_HPP
|
|
|
|
#define ZT_BINDER_HPP
|
|
|
|
|
|
|
|
#include "../node/Constants.hpp"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
#include <WinSock2.h>
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <ShlObj.h>
|
|
|
|
#include <netioapi.h>
|
|
|
|
#include <iphlpapi.h>
|
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ifaddrs.h>
|
2016-06-21 19:32:58 +00:00
|
|
|
#ifdef __LINUX__
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#endif
|
2016-04-05 01:46:41 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
2016-06-21 19:32:58 +00:00
|
|
|
#include <map>
|
2016-04-05 01:46:41 +00:00
|
|
|
|
2016-04-05 22:44:08 +00:00
|
|
|
#include "../node/NonCopyable.hpp"
|
2016-04-05 01:46:41 +00:00
|
|
|
#include "../node/InetAddress.hpp"
|
|
|
|
#include "../node/Mutex.hpp"
|
|
|
|
|
|
|
|
#include "Phy.hpp"
|
|
|
|
|
2016-04-05 22:44:08 +00:00
|
|
|
/**
|
|
|
|
* Period between binder rescans/refreshes
|
|
|
|
*
|
|
|
|
* OneService also does this on detected restarts.
|
|
|
|
*/
|
|
|
|
#define ZT_BINDER_REFRESH_PERIOD 30000
|
|
|
|
|
2016-04-05 01:46:41 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
2016-04-05 18:59:46 +00:00
|
|
|
* Enumerates local devices and binds to all potential ZeroTier path endpoints
|
2016-04-05 01:46:41 +00:00
|
|
|
*
|
|
|
|
* This replaces binding to wildcard (0.0.0.0 and ::0) with explicit binding
|
|
|
|
* as part of the path to default gateway support. Under the hood it uses
|
|
|
|
* different queries on different OSes to enumerate devices, and also exposes
|
|
|
|
* device enumeration and endpoint IP data for use elsewhere.
|
|
|
|
*
|
|
|
|
* On OSes that do not support local port enumeration or where this is not
|
|
|
|
* meaningful, this degrades to binding to wildcard.
|
|
|
|
*/
|
2016-04-05 22:44:08 +00:00
|
|
|
class Binder : NonCopyable
|
2016-04-05 01:46:41 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
struct _Binding
|
|
|
|
{
|
2016-04-05 22:44:08 +00:00
|
|
|
_Binding() :
|
|
|
|
udpSock((PhySocket *)0),
|
|
|
|
tcpListenSock((PhySocket *)0),
|
|
|
|
address() {}
|
|
|
|
|
2016-04-05 01:46:41 +00:00
|
|
|
PhySocket *udpSock;
|
|
|
|
PhySocket *tcpListenSock;
|
|
|
|
InetAddress address;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2016-04-05 22:44:08 +00:00
|
|
|
Binder() {}
|
2016-04-05 01:46:41 +00:00
|
|
|
|
|
|
|
/**
|
2016-04-05 22:44:08 +00:00
|
|
|
* Close all bound ports
|
|
|
|
*
|
|
|
|
* This should be called on shutdown. It closes listen sockets and UDP ports
|
|
|
|
* but not TCP connections from any TCP listen sockets.
|
|
|
|
*
|
|
|
|
* @param phy Physical interface
|
2016-04-05 01:46:41 +00:00
|
|
|
*/
|
2016-04-05 22:44:08 +00:00
|
|
|
template<typename PHY_HANDLER_TYPE>
|
|
|
|
void closeAll(Phy<PHY_HANDLER_TYPE> &phy)
|
2016-04-05 01:46:41 +00:00
|
|
|
{
|
2016-04-05 22:44:08 +00:00
|
|
|
Mutex::Lock _l(_lock);
|
2016-04-05 18:59:46 +00:00
|
|
|
for(typename std::vector<_Binding>::const_iterator i(_bindings.begin());i!=_bindings.end();++i) {
|
2016-04-05 22:44:08 +00:00
|
|
|
phy.close(i->udpSock,false);
|
|
|
|
phy.close(i->tcpListenSock,false);
|
2016-04-05 18:59:46 +00:00
|
|
|
}
|
2016-04-05 01:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scan local devices and addresses and rebind TCP and UDP
|
|
|
|
*
|
|
|
|
* This should be called after wake from sleep, on detected network device
|
|
|
|
* changes, on startup, or periodically (e.g. every 30-60s).
|
|
|
|
*
|
2016-04-05 22:44:08 +00:00
|
|
|
* @param phy Physical interface
|
|
|
|
* @param port Port to bind to on all interfaces (TCP and UDP)
|
2016-04-05 01:46:41 +00:00
|
|
|
* @param ignoreInterfacesByName Ignore these interfaces by name
|
|
|
|
* @param ignoreInterfacesByNamePrefix Ignore these interfaces by name-prefix (starts-with, e.g. zt ignores zt*)
|
|
|
|
* @param ignoreInterfacesByAddress Ignore these interfaces by address
|
2016-04-05 22:44:08 +00:00
|
|
|
* @tparam PHY_HANDLER_TYPE Type for Phy<> template
|
|
|
|
* @tparam INTERFACE_CHECKER Type for class containing shouldBindInterface() method
|
2016-04-05 01:46:41 +00:00
|
|
|
*/
|
2016-04-05 22:44:08 +00:00
|
|
|
template<typename PHY_HANDLER_TYPE,typename INTERFACE_CHECKER>
|
|
|
|
void refresh(Phy<PHY_HANDLER_TYPE> &phy,unsigned int port,INTERFACE_CHECKER &ifChecker)
|
2016-04-05 01:46:41 +00:00
|
|
|
{
|
2016-06-21 19:32:58 +00:00
|
|
|
std::map<InetAddress,std::string> localIfAddrs;
|
2016-04-05 22:44:08 +00:00
|
|
|
PhySocket *udps;
|
|
|
|
//PhySocket *tcps;
|
2016-04-05 01:46:41 +00:00
|
|
|
Mutex::Lock _l(_lock);
|
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
|
2016-04-05 22:53:19 +00:00
|
|
|
char aabuf[32768];
|
|
|
|
ULONG aalen = sizeof(aabuf);
|
|
|
|
if (GetAdaptersAddresses(AF_UNSPEC,GAA_FLAG_SKIP_ANYCAST|GAA_FLAG_SKIP_MULTICAST|GAA_FLAG_SKIP_DNS_SERVER,(void *)0,reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf),&aalen) == NO_ERROR) {
|
|
|
|
PIP_ADAPTER_ADDRESSES a = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf);
|
|
|
|
while (a) {
|
|
|
|
PIP_ADAPTER_UNICAST_ADDRESS ua = a->FirstUnicastAddress;
|
|
|
|
while (ua) {
|
|
|
|
InetAddress ip(ua->Address.lpSockaddr);
|
|
|
|
if (ifChecker.shouldBindInterface("",ip)) {
|
|
|
|
switch(ip.ipScope()) {
|
|
|
|
default: break;
|
|
|
|
case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
|
|
|
|
case InetAddress::IP_SCOPE_GLOBAL:
|
|
|
|
case InetAddress::IP_SCOPE_SHARED:
|
|
|
|
case InetAddress::IP_SCOPE_PRIVATE:
|
|
|
|
ip.setPort(port);
|
2016-06-21 19:32:58 +00:00
|
|
|
localIfAddrs.insert(std::pair<InetAddress,std::string>(ip,std::string()));
|
2016-04-05 22:53:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ua = ua->Next;
|
|
|
|
}
|
|
|
|
a = a->Next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 01:46:41 +00:00
|
|
|
#else // not __WINDOWS__
|
|
|
|
|
|
|
|
struct ifaddrs *ifatbl = (struct ifaddrs *)0;
|
|
|
|
struct ifaddrs *ifa;
|
|
|
|
if ((getifaddrs(&ifatbl) == 0)&&(ifatbl)) {
|
|
|
|
ifa = ifatbl;
|
|
|
|
while (ifa) {
|
|
|
|
if ((ifa->ifa_name)&&(ifa->ifa_addr)) {
|
2016-06-21 19:32:58 +00:00
|
|
|
InetAddress ip = *(ifa->ifa_addr);
|
2016-04-05 22:44:08 +00:00
|
|
|
if (ifChecker.shouldBindInterface(ifa->ifa_name,ip)) {
|
|
|
|
switch(ip.ipScope()) {
|
|
|
|
default: break;
|
|
|
|
case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
|
|
|
|
case InetAddress::IP_SCOPE_GLOBAL:
|
|
|
|
case InetAddress::IP_SCOPE_SHARED:
|
|
|
|
case InetAddress::IP_SCOPE_PRIVATE:
|
|
|
|
ip.setPort(port);
|
2016-06-21 19:32:58 +00:00
|
|
|
localIfAddrs.insert(std::pair<InetAddress,std::string>(ip,std::string(ifa->ifa_name)));
|
2016-04-05 22:44:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-04-05 01:46:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ifa = ifa->ifa_next;
|
|
|
|
}
|
2016-04-05 22:53:19 +00:00
|
|
|
freeifaddrs(ifatbl);
|
2016-04-05 01:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-04-05 17:47:13 +00:00
|
|
|
// Default to binding to wildcard if we can't enumerate addresses
|
2016-06-21 19:32:58 +00:00
|
|
|
if (localIfAddrs.empty()) {
|
|
|
|
localIfAddrs.insert(std::pair<InetAddress,std::string>(InetAddress((uint32_t)0,port),std::string()));
|
|
|
|
localIfAddrs.insert(std::pair<InetAddress,std::string>(InetAddress((const void *)"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",16,port),std::string()));
|
2016-04-05 01:46:41 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 22:44:08 +00:00
|
|
|
// Close any old bindings to anything that doesn't exist anymore
|
2016-06-21 19:32:58 +00:00
|
|
|
for(typename std::vector<_Binding>::const_iterator bi(_bindings.begin());bi!=_bindings.end();++bi) {
|
|
|
|
if (localIfAddrs.find(bi->address) == localIfAddrs.end()) {
|
2016-04-05 22:44:08 +00:00
|
|
|
phy.close(bi->udpSock,false);
|
|
|
|
phy.close(bi->tcpListenSock,false);
|
2016-04-05 01:46:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 19:32:58 +00:00
|
|
|
std::vector<_Binding> newBindings;
|
|
|
|
for(std::map<InetAddress,std::string>::const_iterator ii(localIfAddrs.begin());ii!=localIfAddrs.end();++ii) {
|
|
|
|
typename std::vector<_Binding>::const_iterator bi(_bindings.begin());
|
|
|
|
while (bi != _bindings.end()) {
|
|
|
|
if (bi->address == ii->first) {
|
2016-04-05 01:46:41 +00:00
|
|
|
newBindings.push_back(*bi);
|
|
|
|
break;
|
|
|
|
}
|
2016-06-21 19:32:58 +00:00
|
|
|
++bi;
|
2016-04-05 01:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bi == _bindings.end()) {
|
2016-06-21 19:32:58 +00:00
|
|
|
udps = phy.udpBind(reinterpret_cast<const struct sockaddr *>(&(ii->first)),(void *)0,ZT_UDP_DESIRED_BUF_SIZE);
|
2016-04-05 01:46:41 +00:00
|
|
|
if (udps) {
|
2016-04-05 22:44:08 +00:00
|
|
|
//tcps = phy.tcpListen(reinterpret_cast<const struct sockaddr *>(&ii),(void *)0);
|
|
|
|
//if (tcps) {
|
2016-06-21 19:32:58 +00:00
|
|
|
#ifdef __LINUX__
|
|
|
|
// Bind Linux sockets to their device so routes tha we manage do not override physical routes (wish all platforms had this!)
|
|
|
|
if (ii->second.length() > 0) {
|
|
|
|
int fd = (int)Phy<PHY_HANDLER_TYPE>::getDescriptor(udps);
|
|
|
|
char tmp[256];
|
|
|
|
Utils::scopy(tmp,sizeof(tmp),ii->second.c_str());
|
|
|
|
if (fd >= 0) {
|
|
|
|
if (setsockopt(fd,SOL_SOCKET,SO_BINDTODEVICE,tmp,strlen(tmp)) != 0) {
|
|
|
|
fprintf(stderr,"WARNING: unable to set SO_BINDTODEVICE to bind %s to %s\n",ii->first.toIpString().c_str(),ii->second.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // __LINUX__
|
2016-04-05 01:46:41 +00:00
|
|
|
newBindings.push_back(_Binding());
|
|
|
|
newBindings.back().udpSock = udps;
|
2016-04-05 22:44:08 +00:00
|
|
|
//newBindings.back().tcpListenSock = tcps;
|
2016-06-21 19:32:58 +00:00
|
|
|
newBindings.back().address = ii->first;
|
2016-04-05 22:44:08 +00:00
|
|
|
//} else {
|
|
|
|
// phy.close(udps,false);
|
|
|
|
//}
|
2016-04-05 01:46:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 17:47:13 +00:00
|
|
|
// Swapping pointers and then letting the old one fall out of scope is faster than copying again
|
2016-04-05 01:46:41 +00:00
|
|
|
_bindings.swap(newBindings);
|
|
|
|
}
|
|
|
|
|
2016-04-05 17:47:13 +00:00
|
|
|
/**
|
|
|
|
* Send a UDP packet from the specified local interface, or all
|
|
|
|
*
|
|
|
|
* Unfortunately even by examining the routing table there is no ultimately
|
|
|
|
* robust way to tell where we might reach another host that works in all
|
|
|
|
* environments. As a result, we send packets with null (wildcard) local
|
|
|
|
* addresses from *every* bound interface.
|
|
|
|
*
|
|
|
|
* These are typically initial HELLOs, path probes, etc., since normal
|
|
|
|
* conversations will have a local endpoint address. So the cost is low and
|
|
|
|
* if the peer is not reachable via that route then the packet will go
|
|
|
|
* nowhere and nothing will happen.
|
|
|
|
*
|
|
|
|
* It will of course only send via interface bindings of the same socket
|
|
|
|
* family. No point in sending V4 via V6 or vice versa.
|
|
|
|
*
|
|
|
|
* In any case on most hosts there's only one or two interfaces that we
|
|
|
|
* will use, so none of this is particularly costly.
|
|
|
|
*
|
|
|
|
* @param local Local interface address or null address for 'all'
|
|
|
|
* @param remote Remote address
|
|
|
|
* @param data Data to send
|
|
|
|
* @param len Length of data
|
2016-04-05 22:44:08 +00:00
|
|
|
* @param v4ttl If non-zero, send this packet with the specified IP TTL (IPv4 only)
|
2016-04-05 17:47:13 +00:00
|
|
|
*/
|
2016-04-05 22:44:08 +00:00
|
|
|
template<typename PHY_HANDLER_TYPE>
|
|
|
|
inline bool udpSend(Phy<PHY_HANDLER_TYPE> &phy,const InetAddress &local,const InetAddress &remote,const void *data,unsigned int len,unsigned int v4ttl = 0) const
|
2016-04-05 17:47:13 +00:00
|
|
|
{
|
2016-04-05 22:44:08 +00:00
|
|
|
Mutex::Lock _l(_lock);
|
2016-04-05 17:47:13 +00:00
|
|
|
if (local) {
|
2016-04-05 18:59:46 +00:00
|
|
|
for(typename std::vector<_Binding>::const_iterator i(_bindings.begin());i!=_bindings.end();++i) {
|
2016-04-05 22:44:08 +00:00
|
|
|
if (i->address == local) {
|
|
|
|
if ((v4ttl)&&(local.ss_family == AF_INET))
|
|
|
|
phy.setIp4UdpTtl(i->udpSock,v4ttl);
|
|
|
|
const bool result = phy.udpSend(i->udpSock,reinterpret_cast<const struct sockaddr *>(&remote),data,len);
|
|
|
|
if ((v4ttl)&&(local.ss_family == AF_INET))
|
|
|
|
phy.setIp4UdpTtl(i->udpSock,255);
|
|
|
|
return result;
|
|
|
|
}
|
2016-04-05 17:47:13 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
bool result = false;
|
2016-04-05 18:59:46 +00:00
|
|
|
for(typename std::vector<_Binding>::const_iterator i(_bindings.begin());i!=_bindings.end();++i) {
|
2016-04-05 22:44:08 +00:00
|
|
|
if (i->address.ss_family == remote.ss_family) {
|
|
|
|
if ((v4ttl)&&(remote.ss_family == AF_INET))
|
|
|
|
phy.setIp4UdpTtl(i->udpSock,v4ttl);
|
|
|
|
result |= phy.udpSend(i->udpSock,reinterpret_cast<const struct sockaddr *>(&remote),data,len);
|
|
|
|
if ((v4ttl)&&(remote.ss_family == AF_INET))
|
|
|
|
phy.setIp4UdpTtl(i->udpSock,255);
|
|
|
|
}
|
2016-04-05 17:47:13 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 22:53:19 +00:00
|
|
|
/**
|
|
|
|
* @return All currently bound local interface addresses
|
|
|
|
*/
|
|
|
|
inline std::vector<InetAddress> allBoundLocalInterfaceAddresses()
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_lock);
|
|
|
|
std::vector<InetAddress> aa;
|
|
|
|
for(std::vector<_Binding>::const_iterator i(_bindings.begin());i!=_bindings.end();++i)
|
|
|
|
aa.push_back(i->address);
|
|
|
|
return aa;
|
|
|
|
}
|
|
|
|
|
2016-04-05 01:46:41 +00:00
|
|
|
private:
|
|
|
|
std::vector<_Binding> _bindings;
|
|
|
|
Mutex _lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|