2013-07-04 20:56:19 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Global Peer to Peer Ethernet
|
|
|
|
* Copyright (C) 2012-2013 ZeroTier Networks LLC
|
|
|
|
*
|
|
|
|
* 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_ETHERNETTAP_HPP
|
|
|
|
#define ZT_ETHERNETTAP_HPP
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-08-23 02:33:32 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <map>
|
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
2013-08-23 02:33:32 +00:00
|
|
|
#include <queue>
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <stdexcept>
|
2013-08-23 02:33:32 +00:00
|
|
|
|
2013-07-09 18:06:55 +00:00
|
|
|
#include "Constants.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "InetAddress.hpp"
|
|
|
|
#include "MAC.hpp"
|
|
|
|
#include "Mutex.hpp"
|
2013-08-23 21:39:21 +00:00
|
|
|
#include "Condition.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "MulticastGroup.hpp"
|
2013-07-09 18:06:55 +00:00
|
|
|
#include "Thread.hpp"
|
|
|
|
#include "Buffer.hpp"
|
2013-08-23 02:33:32 +00:00
|
|
|
#include "Array.hpp"
|
|
|
|
|
2013-08-25 22:18:02 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
#include <WinSock2.h>
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
class RuntimeEnvironment;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* System ethernet tap device
|
|
|
|
*/
|
2013-08-05 20:06:16 +00:00
|
|
|
class EthernetTap
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Construct a new TAP device
|
|
|
|
*
|
2013-07-09 18:06:55 +00:00
|
|
|
* Handler arguments: arg,from,to,etherType,data
|
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param renv Runtime environment
|
2013-08-21 12:13:48 +00:00
|
|
|
* @param tag A tag used to identify persistent taps at the OS layer (e.g. nwid in hex)
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param mac MAC address of device
|
|
|
|
* @param mtu MTU of device
|
2013-08-19 21:44:46 +00:00
|
|
|
* @param desc If non-NULL, a description (not used on all OSes)
|
2013-07-09 18:06:55 +00:00
|
|
|
* @param handler Handler function to be called when data is received from the tap
|
|
|
|
* @param arg First argument to handler function
|
2013-07-04 20:56:19 +00:00
|
|
|
* @throws std::runtime_error Unable to allocate device
|
|
|
|
*/
|
2013-07-09 18:06:55 +00:00
|
|
|
EthernetTap(
|
|
|
|
const RuntimeEnvironment *renv,
|
2013-08-21 12:13:48 +00:00
|
|
|
const char *tag,
|
2013-07-09 18:06:55 +00:00
|
|
|
const MAC &mac,
|
|
|
|
unsigned int mtu,
|
|
|
|
void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
|
|
|
|
void *arg)
|
2013-07-04 20:56:19 +00:00
|
|
|
throw(std::runtime_error);
|
|
|
|
|
2013-07-09 18:06:55 +00:00
|
|
|
/**
|
|
|
|
* Close tap and shut down thread
|
|
|
|
*
|
|
|
|
* This may block for a few seconds while thread exits.
|
|
|
|
*/
|
2013-08-05 20:06:16 +00:00
|
|
|
~EthernetTap();
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-07-09 00:36:33 +00:00
|
|
|
/**
|
|
|
|
* Perform OS dependent actions on network configuration change detection
|
|
|
|
*/
|
|
|
|
void whack();
|
|
|
|
|
2013-08-27 19:55:32 +00:00
|
|
|
/**
|
|
|
|
* Set whether or not DHCP is enabled (disabled by default)
|
|
|
|
*
|
|
|
|
* @param dhcp DHCP status
|
|
|
|
* @return New state of DHCP (may be false even on 'true' if DHCP enable failed)
|
|
|
|
*/
|
|
|
|
bool setDhcpEnabled(bool dhcp);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether or not DHCP6 is enabled (disabled by default)
|
|
|
|
*
|
|
|
|
* @param dhcp DHCP6 status
|
|
|
|
* @return New state of DHCP6 (may be false even on 'true' if DHCP enable failed)
|
|
|
|
*/
|
|
|
|
bool setDhcp6Enabled(bool dhcp);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the user display name for this connection
|
|
|
|
*
|
|
|
|
* This does nothing on platforms that don't have this concept.
|
|
|
|
*
|
|
|
|
* @param dn User display name
|
|
|
|
*/
|
|
|
|
void setDisplayName(const char *dn);
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* @return MAC address of this interface
|
|
|
|
*/
|
|
|
|
inline const MAC &mac() const throw() { return _mac; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return MTU of this interface
|
|
|
|
*/
|
|
|
|
inline unsigned int mtu() const throw() { return _mtu; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an IP to this interface
|
|
|
|
*
|
|
|
|
* @param ip IP and netmask (netmask stored in port field)
|
|
|
|
* @return True if IP added successfully
|
|
|
|
*/
|
|
|
|
bool addIP(const InetAddress &ip);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an IP from this interface
|
|
|
|
*
|
|
|
|
* @param ip IP and netmask (netmask stored in port field)
|
|
|
|
* @return True if IP removed successfully
|
|
|
|
*/
|
|
|
|
bool removeIP(const InetAddress &ip);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Set of IP addresses / netmasks
|
|
|
|
*/
|
|
|
|
inline std::set<InetAddress> ips() const
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_ips_m);
|
|
|
|
return _ips;
|
|
|
|
}
|
|
|
|
|
2013-08-26 21:22:20 +00:00
|
|
|
/**
|
|
|
|
* @return Set of IP addresses / netmasks included any we did not assign, link-local, etc.
|
|
|
|
*/
|
|
|
|
std::set<InetAddress> allIps() const;
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Set this tap's IP addresses to exactly this set of IPs
|
|
|
|
*
|
|
|
|
* New IPs are created, ones not in this list are removed.
|
|
|
|
*
|
|
|
|
* @param ips IP addresses with netmask in port field
|
|
|
|
*/
|
|
|
|
inline void setIps(const std::set<InetAddress> &allIps)
|
|
|
|
{
|
|
|
|
for(std::set<InetAddress>::iterator i(allIps.begin());i!=allIps.end();++i)
|
|
|
|
addIP(*i);
|
|
|
|
std::set<InetAddress> myIps(ips());
|
|
|
|
for(std::set<InetAddress>::iterator i(myIps.begin());i!=myIps.end();++i) {
|
|
|
|
if (!allIps.count(*i))
|
|
|
|
removeIP(*i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Put a frame, making it available to the OS for processing
|
|
|
|
*
|
|
|
|
* @param from MAC address from which frame originated
|
|
|
|
* @param to MAC address of destination (typically MAC of tap itself)
|
|
|
|
* @param etherType Ethernet protocol ID
|
|
|
|
* @param data Frame payload
|
|
|
|
* @param len Length of frame
|
|
|
|
*/
|
|
|
|
void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return OS-specific device or connection name
|
|
|
|
*/
|
2013-07-09 18:06:55 +00:00
|
|
|
std::string deviceName() const;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fill or modify a set to contain multicast groups for this device
|
|
|
|
*
|
|
|
|
* This populates a set or, if already populated, modifies it to contain
|
|
|
|
* only multicast groups in which this device is interested.
|
|
|
|
*
|
2013-07-06 19:56:12 +00:00
|
|
|
* This should always include the blind wildcard MulticastGroup (MAC of
|
|
|
|
* ff:ff:ff:ff:ff:ff and 0 ADI field).
|
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param groups Set to modify in place
|
|
|
|
* @return True if set was changed since last call
|
|
|
|
*/
|
|
|
|
bool updateMulticastGroups(std::set<MulticastGroup> &groups);
|
|
|
|
|
2013-08-05 20:06:16 +00:00
|
|
|
/**
|
|
|
|
* Thread main method; do not call elsewhere
|
|
|
|
*/
|
|
|
|
void threadMain()
|
2013-07-09 18:06:55 +00:00
|
|
|
throw();
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
private:
|
|
|
|
const MAC _mac;
|
|
|
|
const unsigned int _mtu;
|
|
|
|
|
|
|
|
const RuntimeEnvironment *_r;
|
|
|
|
|
|
|
|
std::set<InetAddress> _ips;
|
|
|
|
Mutex _ips_m;
|
|
|
|
|
2013-07-09 18:06:55 +00:00
|
|
|
void (*_handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &);
|
|
|
|
void *_arg;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-08-27 19:55:32 +00:00
|
|
|
bool _dhcp;
|
|
|
|
bool _dhcp6;
|
|
|
|
|
2013-08-23 02:33:32 +00:00
|
|
|
Thread _thread;
|
2013-08-25 22:18:02 +00:00
|
|
|
|
|
|
|
#ifdef __UNIX_LIKE__
|
2013-07-04 20:56:19 +00:00
|
|
|
char _dev[16];
|
|
|
|
int _fd;
|
2013-07-09 18:06:55 +00:00
|
|
|
int _shutdownSignalPipe[2];
|
|
|
|
#endif
|
2013-08-23 02:33:32 +00:00
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
2013-08-25 22:18:02 +00:00
|
|
|
HANDLE _tap;
|
|
|
|
OVERLAPPED _tapOvlRead,_tapOvlWrite;
|
|
|
|
char _tapReadBuf[ZT_IF_MTU + 32];
|
|
|
|
HANDLE _injectSemaphore;
|
2013-08-26 21:22:20 +00:00
|
|
|
GUID _deviceGuid;
|
2013-08-26 21:48:47 +00:00
|
|
|
std::string _myDeviceInstanceId; // NetCfgInstanceId, a GUID
|
|
|
|
std::string _myDeviceInstanceIdPath; // DeviceInstanceID, another kind of "instance ID"
|
2013-08-23 02:33:32 +00:00
|
|
|
std::queue< std::pair< Array<char,ZT_IF_MTU + 32>,unsigned int > > _injectPending;
|
|
|
|
Mutex _injectPending_m;
|
2013-08-25 22:18:02 +00:00
|
|
|
volatile bool _run;
|
2013-08-23 02:33:32 +00:00
|
|
|
#endif
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|