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_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 <string>
|
2014-04-07 21:47:39 +00:00
|
|
|
#include <set>
|
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 "MAC.hpp"
|
2014-04-07 21:47:39 +00:00
|
|
|
#include "InetAddress.hpp"
|
2013-07-09 18:06:55 +00:00
|
|
|
#include "Buffer.hpp"
|
2014-04-07 21:47:39 +00:00
|
|
|
#include "MulticastGroup.hpp"
|
|
|
|
#include "NonCopyable.hpp"
|
2013-08-25 22:18:02 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
2014-04-07 21:47:39 +00:00
|
|
|
* Base class for Ethernet tap device implementations
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2014-04-07 21:47:39 +00:00
|
|
|
class EthernetTap : NonCopyable
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-04-07 21:47:39 +00:00
|
|
|
virtual ~EthernetTap() {}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
2014-04-07 21:47:39 +00:00
|
|
|
* @return Implementation class name (e.g. UnixEthernetTap)
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2014-04-07 21:47:39 +00:00
|
|
|
inline const char *implementationName() const { return _implName; }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-07-09 18:06:55 +00:00
|
|
|
/**
|
2014-04-07 21:47:39 +00:00
|
|
|
* Sets whether device is 'up'
|
|
|
|
*
|
|
|
|
* This may do nothing on some platforms.
|
2013-07-09 18:06:55 +00:00
|
|
|
*
|
2014-04-07 21:47:39 +00:00
|
|
|
* @param en Is device enabled?
|
2013-07-09 18:06:55 +00:00
|
|
|
*/
|
2014-04-07 21:47:39 +00:00
|
|
|
virtual void setEnabled(bool en) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Is device 'up'?
|
|
|
|
*/
|
|
|
|
virtual bool enabled() const = 0;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-08-27 19:55:32 +00:00
|
|
|
/**
|
|
|
|
* Set the user display name for this connection
|
|
|
|
*
|
|
|
|
* This does nothing on platforms that don't have this concept.
|
|
|
|
*
|
|
|
|
* @param dn User display name
|
|
|
|
*/
|
2014-04-07 21:47:39 +00:00
|
|
|
virtual void setDisplayName(const char *dn) = 0;
|
2013-08-27 19:55:32 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2014-04-07 21:47:39 +00:00
|
|
|
virtual bool addIP(const InetAddress &ip) = 0;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an IP from this interface
|
|
|
|
*
|
2014-01-23 22:15:00 +00:00
|
|
|
* Link-local IP addresses may not be able to be removed, depending on platform and type.
|
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param ip IP and netmask (netmask stored in port field)
|
|
|
|
* @return True if IP removed successfully
|
|
|
|
*/
|
2014-04-07 21:47:39 +00:00
|
|
|
virtual bool removeIP(const InetAddress &ip) = 0;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2014-01-23 22:15:00 +00:00
|
|
|
* @return All IP addresses (V4 and V6) assigned to this interface (including link-local)
|
2013-08-26 21:22:20 +00:00
|
|
|
*/
|
2014-04-07 21:47:39 +00:00
|
|
|
virtual std::set<InetAddress> ips() const = 0;
|
2013-08-26 21:22:20 +00:00
|
|
|
|
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());
|
2014-02-01 22:02:14 +00:00
|
|
|
#ifdef __APPLE__
|
2014-01-31 23:55:45 +00:00
|
|
|
bool haveV6LinkLocal = false;
|
2013-07-04 20:56:19 +00:00
|
|
|
for(std::set<InetAddress>::iterator i(myIps.begin());i!=myIps.end();++i) {
|
2014-02-01 22:02:14 +00:00
|
|
|
if (i->isLinkLocal()) {
|
|
|
|
if (i->isV6())
|
|
|
|
haveV6LinkLocal = true;
|
|
|
|
} else if (!allIps.count(*i))
|
2013-07-04 20:56:19 +00:00
|
|
|
removeIP(*i);
|
|
|
|
}
|
2014-01-31 23:55:45 +00:00
|
|
|
if (!haveV6LinkLocal)
|
|
|
|
addIP(InetAddress::makeIpv6LinkLocal(_mac));
|
2014-02-01 22:02:14 +00:00
|
|
|
#else
|
|
|
|
for(std::set<InetAddress>::iterator i(myIps.begin());i!=myIps.end();++i) {
|
|
|
|
if ((!i->isLinkLocal())&&(!allIps.count(*i)))
|
|
|
|
removeIP(*i);
|
|
|
|
}
|
|
|
|
#endif
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2014-04-07 21:47:39 +00:00
|
|
|
virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len) = 0;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return OS-specific device or connection name
|
|
|
|
*/
|
2014-04-07 21:47:39 +00:00
|
|
|
virtual std::string deviceName() const = 0;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2014-01-27 06:47:08 +00:00
|
|
|
/**
|
|
|
|
* @return OS-internal persistent device ID or empty string if not applicable to this platform or not persistent
|
|
|
|
*/
|
2014-04-07 21:47:39 +00:00
|
|
|
virtual std::string persistentId() const = 0;
|
2014-01-27 06:47:08 +00:00
|
|
|
|
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.
|
|
|
|
*
|
2014-05-23 22:13:34 +00:00
|
|
|
* This neither includes nor removes the broadcast (ff:ff:ff:ff:ff:ff / 0)
|
|
|
|
* group.
|
2013-07-06 19:56:12 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param groups Set to modify in place
|
|
|
|
* @return True if set was changed since last call
|
|
|
|
*/
|
2014-04-07 21:47:39 +00:00
|
|
|
virtual bool updateMulticastGroups(std::set<MulticastGroup> &groups) = 0;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2014-04-07 21:47:39 +00:00
|
|
|
protected:
|
|
|
|
EthernetTap(const char *cn,const MAC &m,unsigned int mt) :
|
|
|
|
_implName(cn),
|
|
|
|
_mac(m),
|
|
|
|
_mtu(mt) {}
|
2013-07-09 18:06:55 +00:00
|
|
|
|
2014-04-07 21:47:39 +00:00
|
|
|
const char *_implName;
|
|
|
|
MAC _mac;
|
|
|
|
unsigned int _mtu;
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|