2013-07-04 20:56:19 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
|
|
|
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
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"
|
2015-04-01 00:53:34 +00:00
|
|
|
#include "../include/ZeroTierOne.h"
|
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
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
2015-04-01 00:53:34 +00:00
|
|
|
* Extends sockaddr_storage with friendly C++ methods
|
2014-03-31 18:41:14 +00:00
|
|
|
*
|
2015-04-07 17:57:59 +00:00
|
|
|
* This is basically a "mixin" for sockaddr_storage. It adds methods and
|
|
|
|
* operators, but does not modify the structure. This can be cast to/from
|
|
|
|
* sockaddr_storage and used interchangeably. Don't change this as it's
|
|
|
|
* used in a few places.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
struct InetAddress : public sockaddr_storage
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Loopback IPv4 address (no port)
|
|
|
|
*/
|
|
|
|
static const InetAddress LO4;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loopback IPV6 address (no port)
|
|
|
|
*/
|
|
|
|
static const InetAddress LO6;
|
|
|
|
|
2015-04-07 17:57:59 +00:00
|
|
|
/**
|
|
|
|
* IP address scope
|
|
|
|
*/
|
|
|
|
enum IpScope
|
|
|
|
{
|
2015-04-07 18:56:10 +00:00
|
|
|
IP_SCOPE_NONE = 0, // not an IP address -- also the number of classes, must be last entry
|
|
|
|
IP_SCOPE_LINK_LOCAL = 1, // 169.254.x.x, IPv6 LL
|
|
|
|
IP_SCOPE_PRIVATE = 2, // 10.x.x.x, etc.
|
|
|
|
IP_SCOPE_PSEUDOPRIVATE = 3, // 28.x.x.x, etc. -- unofficially unrouted IP blocks often "bogarted"
|
|
|
|
IP_SCOPE_SHARED = 4, // 100.64.0.0/10, shared space for e.g. carrier-grade NAT
|
|
|
|
IP_SCOPE_GLOBAL = 5, // globally routable IP address (all others)
|
|
|
|
IP_SCOPE_LOOPBACK = 6, // 127.0.0.1
|
|
|
|
IP_SCOPE_MULTICAST = 7 // 224.0.0.0 and other multicast IPs
|
2015-04-07 17:57:59 +00:00
|
|
|
};
|
|
|
|
|
2015-04-01 00:53:34 +00:00
|
|
|
InetAddress() throw() { memset(this,0,sizeof(InetAddress)); }
|
|
|
|
InetAddress(const InetAddress &a) throw() { memcpy(this,&a,sizeof(InetAddress)); }
|
2015-04-14 21:49:34 +00:00
|
|
|
InetAddress(const InetAddress *a) throw() { memcpy(this,a,sizeof(InetAddress)); }
|
2015-04-01 00:53:34 +00:00
|
|
|
InetAddress(const struct sockaddr_storage &ss) throw() { *this = ss; }
|
|
|
|
InetAddress(const struct sockaddr_storage *ss) throw() { *this = ss; }
|
|
|
|
InetAddress(const struct sockaddr &sa) throw() { *this = sa; }
|
|
|
|
InetAddress(const struct sockaddr *sa) throw() { *this = sa; }
|
|
|
|
InetAddress(const struct sockaddr_in &sa) throw() { *this = sa; }
|
|
|
|
InetAddress(const struct sockaddr_in *sa) throw() { *this = sa; }
|
|
|
|
InetAddress(const struct sockaddr_in6 &sa) throw() { *this = sa; }
|
|
|
|
InetAddress(const struct sockaddr_in6 *sa) throw() { *this = sa; }
|
2014-07-02 22:59:08 +00:00
|
|
|
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()
|
|
|
|
{
|
2015-04-01 00:53:34 +00:00
|
|
|
memcpy(this,&a,sizeof(InetAddress));
|
2013-07-04 20:56:19 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-04-14 21:49:34 +00:00
|
|
|
inline InetAddress &operator=(const InetAddress *a)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
memcpy(this,a,sizeof(InetAddress));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-04-01 00:53:34 +00:00
|
|
|
inline InetAddress &operator=(const struct sockaddr_storage &ss)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
memcpy(this,&ss,sizeof(InetAddress));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline InetAddress &operator=(const struct sockaddr_storage *ss)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
memcpy(this,ss,sizeof(InetAddress));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline InetAddress &operator=(const struct sockaddr_in &sa)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
memset(this,0,sizeof(InetAddress));
|
|
|
|
memcpy(this,&sa,sizeof(struct sockaddr_in));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline InetAddress &operator=(const struct sockaddr_in *sa)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
memset(this,0,sizeof(InetAddress));
|
|
|
|
memcpy(this,sa,sizeof(struct sockaddr_in));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline InetAddress &operator=(const struct sockaddr_in6 &sa)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
memset(this,0,sizeof(InetAddress));
|
|
|
|
memcpy(this,&sa,sizeof(struct sockaddr_in6));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline InetAddress &operator=(const struct sockaddr_in6 *sa)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
memset(this,0,sizeof(InetAddress));
|
|
|
|
memcpy(this,sa,sizeof(struct sockaddr_in6));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline InetAddress &operator=(const struct sockaddr &sa)
|
2013-07-04 20:56:19 +00:00
|
|
|
throw()
|
|
|
|
{
|
2015-04-01 00:53:34 +00:00
|
|
|
memset(this,0,sizeof(InetAddress));
|
|
|
|
switch(sa.sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
memcpy(this,&sa,sizeof(struct sockaddr_in));
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
memcpy(this,&sa,sizeof(struct sockaddr_in6));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline InetAddress &operator=(const struct sockaddr *sa)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
memset(this,0,sizeof(InetAddress));
|
2013-07-04 20:56:19 +00:00
|
|
|
switch(sa->sa_family) {
|
2015-04-01 00:53:34 +00:00
|
|
|
case AF_INET:
|
|
|
|
memcpy(this,sa,sizeof(struct sockaddr_in));
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
memcpy(this,sa,sizeof(struct sockaddr_in6));
|
|
|
|
break;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
2015-04-01 00:53:34 +00:00
|
|
|
return *this;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 17:57:59 +00:00
|
|
|
/**
|
|
|
|
* @return IP scope classification (e.g. loopback, link-local, private, global)
|
|
|
|
*/
|
|
|
|
IpScope ipScope() const
|
|
|
|
throw();
|
|
|
|
|
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()
|
|
|
|
{
|
2015-04-01 00:53:34 +00:00
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
reinterpret_cast<struct sockaddr_in *>(this)->sin_port = Utils::hton((uint16_t)port);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
reinterpret_cast<struct sockaddr_in6 *>(this)->sin6_port = Utils::hton((uint16_t)port);
|
|
|
|
break;
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ASCII IP/port format representation
|
|
|
|
*/
|
|
|
|
std::string toString() const;
|
|
|
|
|
|
|
|
/**
|
2015-04-01 00:53:34 +00:00
|
|
|
* @return IP portion only, in ASCII string format
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
std::string toIpString() const;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2015-04-01 00:53:34 +00:00
|
|
|
* @param ipSlashPort ASCII IP/port format notation
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
void fromString(const std::string &ipSlashPort);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Port or 0 if no port component defined
|
|
|
|
*/
|
|
|
|
inline unsigned int port() const
|
|
|
|
throw()
|
|
|
|
{
|
2015-04-01 00:53:34 +00:00
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET: return Utils::ntoh((uint16_t)(reinterpret_cast<const struct sockaddr_in *>(this)->sin_port));
|
|
|
|
case AF_INET6: return Utils::ntoh((uint16_t)(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_port));
|
2014-07-02 22:59:08 +00:00
|
|
|
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
|
|
|
|
2015-06-13 09:34:31 +00:00
|
|
|
/**
|
|
|
|
* Alias for port()
|
|
|
|
*
|
|
|
|
* This just aliases port() because for gateways we use this field to
|
|
|
|
* store the gateway metric.
|
|
|
|
*
|
|
|
|
* @return Gateway metric
|
|
|
|
*/
|
|
|
|
inline unsigned int metric() const throw() { return port(); }
|
|
|
|
|
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
|
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
inline bool isV4() const throw() { return (ss_family == AF_INET); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return True if this is an IPv6 address
|
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
inline bool isV6() const throw() { return (ss_family == AF_INET6); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2015-04-01 00:53:34 +00:00
|
|
|
* @return pointer to raw IP address bytes
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
inline const void *rawIpData() const
|
2013-07-04 20:56:19 +00:00
|
|
|
throw()
|
|
|
|
{
|
2015-04-01 00:53:34 +00:00
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET: return (const void *)&(reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr);
|
|
|
|
case AF_INET6: return (const void *)(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
|
|
|
|
default: return 0;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-01 00:53:34 +00:00
|
|
|
* @return pointer to raw IP address bytes
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
inline void *rawIpData()
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET: return (void *)&(reinterpret_cast<struct sockaddr_in *>(this)->sin_addr.s_addr);
|
|
|
|
case AF_INET6: return (void *)(reinterpret_cast<struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
|
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2015-04-10 03:54:00 +00:00
|
|
|
/**
|
|
|
|
* @param a InetAddress to compare again
|
|
|
|
* @return True if only IP portions are equal (false for non-IP or null addresses)
|
|
|
|
*/
|
|
|
|
inline bool ipsEqual(const InetAddress &a) const
|
|
|
|
{
|
|
|
|
switch(ss_family) {
|
|
|
|
case AF_INET: return (reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr == reinterpret_cast<const struct sockaddr_in *>(&a)->sin_addr.s_addr);
|
|
|
|
case AF_INET6: return (memcmp(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr,reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_addr.s6_addr,16) == 0);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Set to null/zero
|
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
inline void zero() throw() { memset(this,0,sizeof(InetAddress)); }
|
2014-07-02 22:59:08 +00:00
|
|
|
|
2015-06-29 17:21:28 +00:00
|
|
|
/**
|
|
|
|
* Check whether this is a network/route rather than an IP assignment
|
|
|
|
*
|
|
|
|
* A network is an IP/netmask where everything after the netmask is
|
|
|
|
* zero e.g. 10.0.0.0/8.
|
|
|
|
*
|
|
|
|
* @return True if everything after netmask bits is zero
|
|
|
|
*/
|
|
|
|
bool isNetwork() const
|
|
|
|
throw();
|
|
|
|
|
2014-07-02 22:59:08 +00:00
|
|
|
/**
|
|
|
|
* @return True if address family is non-zero
|
|
|
|
*/
|
2015-04-01 00:53:34 +00:00
|
|
|
inline operator bool() const throw() { return (ss_family != 0); }
|
2014-07-02 22:59:08 +00:00
|
|
|
|
2015-04-14 21:49:34 +00:00
|
|
|
bool operator==(const InetAddress &a) const throw();
|
|
|
|
bool operator<(const InetAddress &a) const throw();
|
2014-07-02 22:59:08 +00:00
|
|
|
inline bool operator!=(const InetAddress &a) const throw() { return !(*this == a); }
|
|
|
|
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();
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|