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_INETADDRESS_HPP
|
|
|
|
#define ZT_INETADDRESS_HPP
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-08-13 01:25:36 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <string>
|
|
|
|
|
2013-08-13 01:25:36 +00:00
|
|
|
#include "Constants.hpp"
|
2014-01-23 22:15:00 +00:00
|
|
|
#include "Utils.hpp"
|
2014-01-31 23:55:45 +00:00
|
|
|
#include "MAC.hpp"
|
2013-08-13 01:25:36 +00:00
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
#include <WinSock2.h>
|
|
|
|
#include <WS2tcpip.h>
|
|
|
|
#include <Windows.h>
|
|
|
|
#else
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for sockaddr structures for IPV4 and IPV6
|
2014-03-31 18:41:14 +00:00
|
|
|
*
|
|
|
|
* Note: this class is raw memcpy'able, which is used in a couple places.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
class InetAddress
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Address type
|
|
|
|
*/
|
|
|
|
enum AddressType
|
|
|
|
{
|
|
|
|
TYPE_NULL = 0,
|
|
|
|
TYPE_IPV4 = AF_INET,
|
|
|
|
TYPE_IPV6 = AF_INET6
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loopback IPv4 address (no port)
|
|
|
|
*/
|
|
|
|
static const InetAddress LO4;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loopback IPV6 address (no port)
|
|
|
|
*/
|
|
|
|
static const InetAddress LO6;
|
|
|
|
|
2014-08-13 00:20:34 +00:00
|
|
|
/**
|
|
|
|
* 0.0.0.0/0
|
|
|
|
*/
|
|
|
|
static const InetAddress DEFAULT4;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ::/0
|
|
|
|
*/
|
|
|
|
static const InetAddress DEFAULT6;
|
|
|
|
|
2014-07-02 22:59:08 +00:00
|
|
|
InetAddress() throw() { memset(&_sa,0,sizeof(_sa)); }
|
|
|
|
InetAddress(const InetAddress &a) throw() { memcpy(&_sa,&a._sa,sizeof(_sa)); }
|
|
|
|
InetAddress(const struct sockaddr *sa) throw() { this->set(sa); }
|
|
|
|
InetAddress(const void *ipBytes,unsigned int ipLen,unsigned int port) throw() { this->set(ipBytes,ipLen,port); }
|
|
|
|
InetAddress(const uint32_t ipv4,unsigned int port) throw() { this->set(&ipv4,4,port); }
|
|
|
|
InetAddress(const std::string &ip,unsigned int port) throw() { this->set(ip,port); }
|
|
|
|
InetAddress(const std::string &ipSlashPort) throw() { this->fromString(ipSlashPort); }
|
|
|
|
InetAddress(const char *ipSlashPort) throw() { this->fromString(std::string(ipSlashPort)); }
|
2013-10-01 20:01:36 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
inline InetAddress &operator=(const InetAddress &a)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
memcpy(&_sa,&a._sa,sizeof(_sa));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set from an OS-level sockaddr structure
|
|
|
|
*
|
|
|
|
* @param sa Socket address (V4 or V6)
|
|
|
|
*/
|
|
|
|
inline void set(const struct sockaddr *sa)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
switch(sa->sa_family) {
|
2014-07-02 22:59:08 +00:00
|
|
|
case AF_INET: memcpy(&_sa.sin,sa,sizeof(struct sockaddr_in)); break;
|
|
|
|
case AF_INET6: memcpy(&_sa.sin6,sa,sizeof(struct sockaddr_in6)); break;
|
|
|
|
default: memset(&_sa,0,sizeof(_sa)); break;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set from a string-format IP and a port
|
|
|
|
*
|
|
|
|
* @param ip IP address in V4 or V6 ASCII notation
|
|
|
|
* @param port Port or 0 for none
|
|
|
|
*/
|
|
|
|
void set(const std::string &ip,unsigned int port)
|
|
|
|
throw();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set from a raw IP and port number
|
|
|
|
*
|
|
|
|
* @param ipBytes Bytes of IP address in network byte order
|
|
|
|
* @param ipLen Length of IP address: 4 or 16
|
|
|
|
* @param port Port number or 0 for none
|
|
|
|
*/
|
2014-07-02 22:59:08 +00:00
|
|
|
void set(const void *ipBytes,unsigned int ipLen,unsigned int port)
|
|
|
|
throw();
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the port component
|
|
|
|
*
|
|
|
|
* @param port Port, 0 to 65535
|
|
|
|
*/
|
|
|
|
inline void setPort(unsigned int port)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
if (_sa.saddr.sa_family == AF_INET)
|
2014-07-02 22:59:08 +00:00
|
|
|
_sa.sin.sin_port = Utils::hton((uint16_t)port);
|
2013-07-04 20:56:19 +00:00
|
|
|
else if (_sa.saddr.sa_family == AF_INET6)
|
2014-07-02 22:59:08 +00:00
|
|
|
_sa.sin6.sin6_port = Utils::hton((uint16_t)port);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-01-23 22:15:00 +00:00
|
|
|
/**
|
|
|
|
* @return True if this is a link-local IP address
|
|
|
|
*/
|
2014-07-02 22:59:08 +00:00
|
|
|
bool isLinkLocal() const
|
|
|
|
throw();
|
2014-01-23 22:15:00 +00:00
|
|
|
|
2014-08-13 00:20:34 +00:00
|
|
|
/**
|
|
|
|
* @return True if this ip/netmask would represent a default route (e.g. 0.0.0.0/0)
|
|
|
|
*/
|
|
|
|
bool isDefaultRoute() const
|
|
|
|
throw();
|
|
|
|
|
2014-07-17 20:08:37 +00:00
|
|
|
/**
|
|
|
|
* @return True if this is a loopback address
|
|
|
|
*/
|
|
|
|
inline bool isLoopback() const
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
return ((*this == LO4)||(*this == LO6));
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* @return ASCII IP/port format representation
|
|
|
|
*/
|
|
|
|
std::string toString() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ipSlashPort ASCII IP/port format notation
|
|
|
|
*/
|
|
|
|
void fromString(const std::string &ipSlashPort);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return IP portion only, in ASCII string format
|
|
|
|
*/
|
|
|
|
std::string toIpString() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Port or 0 if no port component defined
|
|
|
|
*/
|
|
|
|
inline unsigned int port() const
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
switch(_sa.saddr.sa_family) {
|
2014-07-02 22:59:08 +00:00
|
|
|
case AF_INET: return Utils::ntoh((uint16_t)_sa.sin.sin_port);
|
|
|
|
case AF_INET6: return Utils::ntoh((uint16_t)_sa.sin6.sin6_port);
|
|
|
|
default: return 0;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for port()
|
|
|
|
*
|
|
|
|
* This just aliases port() to make code more readable when netmask bits
|
|
|
|
* are stuffed there, as they are in Network, EthernetTap, and a few other
|
|
|
|
* spots.
|
|
|
|
*
|
|
|
|
* @return Netmask bits
|
|
|
|
*/
|
2014-07-02 22:59:08 +00:00
|
|
|
inline unsigned int netmaskBits() const throw() { return port(); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2014-01-23 22:15:00 +00:00
|
|
|
/**
|
|
|
|
* Construct a full netmask as an InetAddress
|
|
|
|
*/
|
2014-07-02 22:59:08 +00:00
|
|
|
InetAddress netmask() const
|
|
|
|
throw();
|
2014-01-23 22:15:00 +00:00
|
|
|
|
2014-09-04 17:36:25 +00:00
|
|
|
/**
|
|
|
|
* Constructs a broadcast address from a network/netmask address
|
|
|
|
*
|
|
|
|
* @return Broadcast address (only IP portion is meaningful)
|
|
|
|
*/
|
|
|
|
InetAddress broadcast() const
|
|
|
|
throw();
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* @return True if this is an IPv4 address
|
|
|
|
*/
|
|
|
|
inline bool isV4() const throw() { return (_sa.saddr.sa_family == AF_INET); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return True if this is an IPv6 address
|
|
|
|
*/
|
|
|
|
inline bool isV6() const throw() { return (_sa.saddr.sa_family == AF_INET6); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Address type or TYPE_NULL if not defined
|
|
|
|
*/
|
|
|
|
inline AddressType type() const throw() { return (AddressType)_sa.saddr.sa_family; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Force type to IPv4
|
|
|
|
*/
|
|
|
|
inline void setV4() throw() { _sa.saddr.sa_family = AF_INET; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Force type to IPv6
|
|
|
|
*/
|
|
|
|
inline void setV6() throw() { _sa.saddr.sa_family = AF_INET6; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Raw sockaddr structure
|
|
|
|
*/
|
|
|
|
inline struct sockaddr *saddr() throw() { return &(_sa.saddr); }
|
|
|
|
inline const struct sockaddr *saddr() const throw() { return &(_sa.saddr); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Length of sockaddr_in if IPv4, sockaddr_in6 if IPv6
|
|
|
|
*/
|
|
|
|
inline unsigned int saddrLen() const
|
|
|
|
throw()
|
|
|
|
{
|
2014-07-02 22:59:08 +00:00
|
|
|
switch(_sa.saddr.sa_family) {
|
|
|
|
case AF_INET: return sizeof(struct sockaddr_in);
|
|
|
|
case AF_INET6: return sizeof(struct sockaddr_in6);
|
|
|
|
default: return 0;
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Combined length of internal structure, room for either V4 or V6
|
|
|
|
*/
|
2014-07-02 22:59:08 +00:00
|
|
|
inline unsigned int saddrSpaceLen() const throw() { return sizeof(_sa); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Raw sockaddr_in structure (valid if IPv4)
|
|
|
|
*/
|
|
|
|
inline const struct sockaddr_in *saddr4() const throw() { return &(_sa.sin); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Raw sockaddr_in6 structure (valid if IPv6)
|
|
|
|
*/
|
|
|
|
inline const struct sockaddr_in6 *saddr6() const throw() { return &(_sa.sin6); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Raw IP address (4 bytes for IPv4, 16 bytes for IPv6)
|
|
|
|
*/
|
|
|
|
inline void *rawIpData() throw() { return ((_sa.saddr.sa_family == AF_INET) ? (void *)(&(_sa.sin.sin_addr.s_addr)) : (void *)_sa.sin6.sin6_addr.s6_addr); }
|
|
|
|
inline const void *rawIpData() const throw() { return ((_sa.saddr.sa_family == AF_INET) ? (void *)(&(_sa.sin.sin_addr.s_addr)) : (void *)_sa.sin6.sin6_addr.s6_addr); }
|
|
|
|
|
|
|
|
/**
|
2014-07-02 22:59:08 +00:00
|
|
|
* Compare only the IP portions of addresses, ignoring port/netmask
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* @param a Address to compare
|
|
|
|
* @return True if both addresses are of the same (valid) type and their IPs match
|
|
|
|
*/
|
|
|
|
inline bool ipsEqual(const InetAddress &a) const
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
if (_sa.saddr.sa_family == a._sa.saddr.sa_family) {
|
|
|
|
switch(_sa.saddr.sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
return (_sa.sin.sin_addr.s_addr == a._sa.sin.sin_addr.s_addr);
|
|
|
|
case AF_INET6:
|
|
|
|
return (!memcmp(_sa.sin6.sin6_addr.s6_addr,a._sa.sin6.sin6_addr.s6_addr,16));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-02 22:59:08 +00:00
|
|
|
* Compare IP/netmask with another IP/netmask
|
|
|
|
*
|
|
|
|
* @param ipnet IP/netmask to compare with
|
|
|
|
* @return True if [netmask] bits match
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2014-07-02 22:59:08 +00:00
|
|
|
bool sameNetworkAs(const InetAddress &ipnet) const
|
2014-07-17 20:08:37 +00:00
|
|
|
throw();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether this address is within an ip/netmask
|
|
|
|
*
|
|
|
|
* @param ipnet IP/netmask
|
|
|
|
* @return True if this address is within this network
|
|
|
|
*/
|
|
|
|
bool within(const InetAddress &ipnet) const
|
2014-07-02 22:59:08 +00:00
|
|
|
throw();
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set to null/zero
|
|
|
|
*/
|
2014-07-02 22:59:08 +00:00
|
|
|
inline void zero() throw() { memset(&_sa,0,sizeof(_sa)); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return True if address family is non-zero
|
|
|
|
*/
|
|
|
|
inline operator bool() const throw() { return ((_sa.saddr.sa_family == AF_INET)||(_sa.saddr.sa_family == AF_INET6)); }
|
|
|
|
|
|
|
|
bool operator==(const InetAddress &a) const throw();
|
|
|
|
inline bool operator!=(const InetAddress &a) const throw() { return !(*this == a); }
|
|
|
|
bool operator<(const InetAddress &a) const throw();
|
|
|
|
inline bool operator>(const InetAddress &a) const throw() { return (a < *this); }
|
|
|
|
inline bool operator<=(const InetAddress &a) const throw() { return !(a < *this); }
|
|
|
|
inline bool operator>=(const InetAddress &a) const throw() { return !(*this < a); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2014-01-31 23:55:45 +00:00
|
|
|
/**
|
|
|
|
* @param mac MAC address seed
|
|
|
|
* @return IPv6 link-local address
|
|
|
|
*/
|
2014-07-02 22:59:08 +00:00
|
|
|
static InetAddress makeIpv6LinkLocal(const MAC &mac)
|
|
|
|
throw();
|
2014-01-31 23:55:45 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
private:
|
|
|
|
union {
|
|
|
|
struct sockaddr saddr;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
} _sa;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|