2013-07-04 20:56:19 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Global Peer to Peer Ethernet
|
2014-02-16 20:40:22 +00:00
|
|
|
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
2013-07-04 20:56:19 +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/>.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* 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/
|
|
|
|
*/
|
|
|
|
|
2013-12-07 00:49:20 +00:00
|
|
|
#ifndef ZT_N_SWITCH_HPP
|
|
|
|
#define ZT_N_SWITCH_HPP
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
2013-07-12 02:06:25 +00:00
|
|
|
#include <list>
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-09-17 19:46:56 +00:00
|
|
|
#include "Constants.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Mutex.hpp"
|
|
|
|
#include "MAC.hpp"
|
|
|
|
#include "NonCopyable.hpp"
|
|
|
|
#include "Packet.hpp"
|
|
|
|
#include "Utils.hpp"
|
|
|
|
#include "InetAddress.hpp"
|
|
|
|
#include "Topology.hpp"
|
|
|
|
#include "Array.hpp"
|
|
|
|
#include "Network.hpp"
|
|
|
|
#include "SharedPtr.hpp"
|
2013-07-11 21:52:04 +00:00
|
|
|
#include "PacketDecoder.hpp"
|
2014-03-19 20:56:48 +00:00
|
|
|
#include "Socket.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-10-17 17:07:53 +00:00
|
|
|
/* Ethernet frame types that might be relevant to us */
|
|
|
|
#define ZT_ETHERTYPE_IPV4 0x0800
|
|
|
|
#define ZT_ETHERTYPE_ARP 0x0806
|
|
|
|
#define ZT_ETHERTYPE_RARP 0x8035
|
|
|
|
#define ZT_ETHERTYPE_ATALK 0x809b
|
|
|
|
#define ZT_ETHERTYPE_AARP 0x80f3
|
|
|
|
#define ZT_ETHERTYPE_IPX_A 0x8137
|
|
|
|
#define ZT_ETHERTYPE_IPX_B 0x8138
|
|
|
|
#define ZT_ETHERTYPE_IPV6 0x86dd
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
class RuntimeEnvironment;
|
|
|
|
class EthernetTap;
|
|
|
|
class Logger;
|
|
|
|
class Node;
|
|
|
|
class Peer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Core of the distributed Ethernet switch and protocol implementation
|
|
|
|
*/
|
|
|
|
class Switch : NonCopyable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Switch(const RuntimeEnvironment *renv);
|
|
|
|
~Switch();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a packet is received from the real network
|
|
|
|
*
|
2014-03-19 20:56:48 +00:00
|
|
|
* @param fromSock Originating socket
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param fromAddr Internet IP address of origin
|
|
|
|
* @param data Packet data
|
|
|
|
*/
|
2014-03-19 20:56:48 +00:00
|
|
|
void onRemotePacket(const SharedPtr<Socket> &fromSock,const InetAddress &fromAddr,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &data);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a packet comes from a local Ethernet tap
|
|
|
|
*
|
|
|
|
* @param network Which network's TAP did this packet come from?
|
|
|
|
* @param from Originating MAC address
|
|
|
|
* @param to Destination MAC address
|
|
|
|
* @param etherType Ethernet packet type
|
|
|
|
* @param data Ethernet payload
|
|
|
|
*/
|
|
|
|
void onLocalEthernet(const SharedPtr<Network> &network,const MAC &from,const MAC &to,unsigned int etherType,const Buffer<4096> &data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a packet to a ZeroTier address (destination in packet)
|
|
|
|
*
|
|
|
|
* The packet must be fully composed with source and destination but not
|
|
|
|
* yet encrypted. If the destination peer is known the packet
|
|
|
|
* is sent immediately. Otherwise it is queued and a WHOIS is dispatched.
|
|
|
|
*
|
|
|
|
* The packet may be compressed. Compression isn't done here.
|
|
|
|
*
|
|
|
|
* Needless to say, the packet's source must be this node. Otherwise it
|
|
|
|
* won't be encrypted right. (This is not used for relaying.)
|
|
|
|
*
|
|
|
|
* @param packet Packet to send
|
|
|
|
* @param encrypt Encrypt packet payload? (always true except for HELLO)
|
|
|
|
*/
|
|
|
|
void send(const Packet &packet,bool encrypt);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a HELLO announcement
|
|
|
|
*
|
|
|
|
* @param dest Address of destination
|
|
|
|
*/
|
|
|
|
void sendHELLO(const Address &dest);
|
|
|
|
|
2013-07-06 20:20:35 +00:00
|
|
|
/**
|
|
|
|
* Send a HELLO announcement immediately to the indicated address
|
|
|
|
*
|
|
|
|
* @param dest Destination peer
|
2014-04-01 05:23:55 +00:00
|
|
|
* @param path Network path to peer
|
2013-07-06 20:20:35 +00:00
|
|
|
* @return True if send appears successful
|
|
|
|
*/
|
2014-04-01 05:23:55 +00:00
|
|
|
bool sendHELLO(const SharedPtr<Peer> &dest,const Path &path);
|
2014-03-19 20:56:48 +00:00
|
|
|
|
|
|
|
/**
|
2014-04-01 05:23:55 +00:00
|
|
|
* Send a HELLO announcement immediately to the indicated address via UDP
|
2014-03-19 20:56:48 +00:00
|
|
|
*
|
|
|
|
* @param dest Destination peer
|
2014-04-01 05:23:55 +00:00
|
|
|
* @param destUdp UDP inet address
|
2014-03-19 20:56:48 +00:00
|
|
|
* @return True if send appears successful
|
|
|
|
*/
|
2014-04-01 05:23:55 +00:00
|
|
|
bool sendHELLO(const SharedPtr<Peer> &dest,const InetAddress &destUdp);
|
2013-07-06 20:20:35 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Send RENDEZVOUS to two peers to permit them to directly connect
|
|
|
|
*
|
|
|
|
* This only works if both peers are known, with known working direct
|
|
|
|
* links to this peer. The best link for each peer is sent to the other.
|
|
|
|
*
|
|
|
|
* A rate limiter is in effect via the _lastUniteAttempt map. If force
|
|
|
|
* is true, a unite attempt is made even if one has been made less than
|
|
|
|
* ZT_MIN_UNITE_INTERVAL milliseconds ago.
|
|
|
|
*
|
|
|
|
* @param p1 One of two peers (order doesn't matter)
|
|
|
|
* @param p2 Second of pair
|
|
|
|
* @param force If true, send now regardless of interval
|
|
|
|
*/
|
|
|
|
bool unite(const Address &p1,const Address &p2,bool force);
|
|
|
|
|
2013-07-12 02:06:25 +00:00
|
|
|
/**
|
|
|
|
* Send NAT traversal messages to peer at the given candidate address
|
|
|
|
*
|
|
|
|
* @param peer Peer to contact
|
|
|
|
* @param atAddr Address of peer
|
|
|
|
*/
|
|
|
|
void contact(const SharedPtr<Peer> &peer,const InetAddress &atAddr);
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Perform retries and other periodic timer tasks
|
|
|
|
*
|
|
|
|
* @return Number of milliseconds until doTimerTasks() should be run again
|
|
|
|
*/
|
|
|
|
unsigned long doTimerTasks();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Announce multicast group memberships
|
|
|
|
*
|
2013-10-01 20:01:36 +00:00
|
|
|
* This announces all the groups for all the networks in the supplied map to
|
|
|
|
* all peers with whom we have an active direct link. Only isAllowed() peers
|
|
|
|
* and supernodes get announcements for each given network.
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* @param allMemberships Memberships for a number of networks
|
|
|
|
*/
|
|
|
|
void announceMulticastGroups(const std::map< SharedPtr<Network>,std::set<MulticastGroup> > &allMemberships);
|
|
|
|
|
2013-10-01 20:01:36 +00:00
|
|
|
/**
|
|
|
|
* Announce multicast group memberships
|
|
|
|
*
|
|
|
|
* This announces all current multicast memberships to a single peer. Only
|
|
|
|
* memberships for networks where the peer isAllowed() are included, unless
|
|
|
|
* the peer is a supernode.
|
|
|
|
*
|
|
|
|
* @param peer Peer to announce all memberships to
|
|
|
|
*/
|
|
|
|
void announceMulticastGroups(const SharedPtr<Peer> &peer);
|
|
|
|
|
2013-07-11 20:19:06 +00:00
|
|
|
/**
|
|
|
|
* Request WHOIS on a given address
|
|
|
|
*
|
|
|
|
* @param addr Address to look up
|
|
|
|
*/
|
2013-07-11 21:52:04 +00:00
|
|
|
void requestWhois(const Address &addr);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-10-16 21:47:26 +00:00
|
|
|
/**
|
|
|
|
* Cancel WHOIS for an address
|
|
|
|
*
|
|
|
|
* @param addr Address to cancel
|
|
|
|
*/
|
|
|
|
void cancelWhoisRequest(const Address &addr);
|
|
|
|
|
2013-07-12 02:06:25 +00:00
|
|
|
/**
|
|
|
|
* Run any processes that are waiting for this peer
|
|
|
|
*
|
|
|
|
* Called when we learn of a peer's identity from HELLO, OK(WHOIS), etc.
|
|
|
|
*
|
|
|
|
* @param peer New peer
|
|
|
|
*/
|
|
|
|
void doAnythingWaitingForPeer(const SharedPtr<Peer> &peer);
|
2013-07-11 20:19:06 +00:00
|
|
|
|
2013-10-17 17:07:53 +00:00
|
|
|
/**
|
|
|
|
* @param etherType Ethernet type ID
|
|
|
|
* @return Human-readable name
|
|
|
|
*/
|
|
|
|
static const char *etherTypeName(const unsigned int etherType)
|
|
|
|
throw();
|
|
|
|
|
2013-07-12 02:06:25 +00:00
|
|
|
private:
|
2014-04-10 23:30:15 +00:00
|
|
|
void _handleRemotePacketFragment(const SharedPtr<Socket> &fromSock,const InetAddress &fromAddr,const Buffer<4096> &data);
|
|
|
|
void _handleRemotePacketHead(const SharedPtr<Socket> &fromSock,const InetAddress &fromAddr,const Buffer<4096> &data);
|
|
|
|
void _handleBeacon(const SharedPtr<Socket> &fromSock,const InetAddress &fromAddr,const Buffer<4096> &data);
|
2013-07-11 20:19:06 +00:00
|
|
|
|
|
|
|
Address _sendWhoisRequest(
|
|
|
|
const Address &addr,
|
|
|
|
const Address *peersAlreadyConsulted,
|
|
|
|
unsigned int numPeersAlreadyConsulted);
|
|
|
|
|
|
|
|
bool _trySend(
|
|
|
|
const Packet &packet,
|
|
|
|
bool encrypt);
|
|
|
|
|
|
|
|
const RuntimeEnvironment *const _r;
|
2014-04-10 23:30:15 +00:00
|
|
|
volatile uint64_t _lastBeacon;
|
2013-09-27 20:03:13 +00:00
|
|
|
volatile unsigned int _multicastIdCounter;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
struct WhoisRequest
|
|
|
|
{
|
|
|
|
uint64_t lastSent;
|
|
|
|
Address peersConsulted[ZT_MAX_WHOIS_RETRIES]; // by retry
|
|
|
|
unsigned int retries; // 0..ZT_MAX_WHOIS_RETRIES
|
|
|
|
};
|
|
|
|
std::map< Address,WhoisRequest > _outstandingWhoisRequests;
|
|
|
|
Mutex _outstandingWhoisRequests_m;
|
|
|
|
|
2013-07-12 02:06:25 +00:00
|
|
|
std::list< SharedPtr<PacketDecoder> > _rxQueue;
|
2013-07-11 21:52:04 +00:00
|
|
|
Mutex _rxQueue_m;
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
struct TXQueueEntry
|
|
|
|
{
|
2013-07-11 20:19:06 +00:00
|
|
|
TXQueueEntry() {}
|
|
|
|
TXQueueEntry(uint64_t ct,const Packet &p,bool enc) :
|
|
|
|
creationTime(ct),
|
|
|
|
packet(p),
|
|
|
|
encrypt(enc) {}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
uint64_t creationTime;
|
|
|
|
Packet packet; // unencrypted/untagged for TX queue
|
|
|
|
bool encrypt;
|
|
|
|
};
|
2013-07-11 21:52:04 +00:00
|
|
|
std::multimap< Address,TXQueueEntry > _txQueue;
|
2013-07-04 20:56:19 +00:00
|
|
|
Mutex _txQueue_m;
|
|
|
|
|
|
|
|
struct DefragQueueEntry
|
|
|
|
{
|
|
|
|
uint64_t creationTime;
|
2013-07-11 21:52:04 +00:00
|
|
|
SharedPtr<PacketDecoder> frag0;
|
2013-07-04 20:56:19 +00:00
|
|
|
Packet::Fragment frags[ZT_MAX_PACKET_FRAGMENTS - 1];
|
|
|
|
unsigned int totalFragments; // 0 if only frag0 received, waiting for frags
|
|
|
|
uint32_t haveFragments; // bit mask, LSB to MSB
|
|
|
|
};
|
|
|
|
std::map< uint64_t,DefragQueueEntry > _defragQueue;
|
|
|
|
Mutex _defragQueue_m;
|
|
|
|
|
|
|
|
std::map< Array< Address,2 >,uint64_t > _lastUniteAttempt; // key is always sorted in ascending order, for set-like behavior
|
|
|
|
Mutex _lastUniteAttempt_m;
|
2013-07-12 02:25:12 +00:00
|
|
|
|
|
|
|
struct ContactQueueEntry
|
|
|
|
{
|
|
|
|
ContactQueueEntry() {}
|
2014-03-19 20:56:48 +00:00
|
|
|
ContactQueueEntry(const SharedPtr<Peer> &p,uint64_t ft,const InetAddress &a) :
|
2013-07-12 02:25:12 +00:00
|
|
|
peer(p),
|
|
|
|
fireAtTime(ft),
|
|
|
|
inaddr(a) {}
|
|
|
|
|
|
|
|
SharedPtr<Peer> peer;
|
|
|
|
uint64_t fireAtTime;
|
|
|
|
InetAddress inaddr;
|
|
|
|
};
|
|
|
|
std::list<ContactQueueEntry> _contactQueue;
|
|
|
|
Mutex _contactQueue_m;
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|