mirror of
https://github.com/deneraraujo/OpenVPNAdapter.git
synced 2026-04-24 00:00:05 +08:00
Merge commit '86cc97e55fe346502462284d2e636a2b3708163e' as 'Sources/OpenVPN3'
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2017 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef OPENVPN_ADDR_ADDRLIST_H
|
||||
#define OPENVPN_ADDR_ADDRLIST_H
|
||||
|
||||
#include <openvpn/common/rc.hpp>
|
||||
#include <openvpn/addr/ip.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
namespace IP {
|
||||
|
||||
// A list of unique IP addresses
|
||||
class AddrList : public std::vector<IP::Addr>, public RC<thread_unsafe_refcount>
|
||||
{
|
||||
public:
|
||||
typedef RCPtr<AddrList> Ptr;
|
||||
|
||||
void add(const IP::Addr& a)
|
||||
{
|
||||
if (!exists(a))
|
||||
push_back(a);
|
||||
}
|
||||
|
||||
bool exists(const IP::Addr& a) const
|
||||
{
|
||||
for (const_iterator i = begin(); i != end(); ++i)
|
||||
{
|
||||
if (a == *i)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#if 0
|
||||
void dump() const
|
||||
{
|
||||
OPENVPN_LOG("******* AddrList::dump");
|
||||
for (const_iterator i = begin(); i != end(); ++i)
|
||||
OPENVPN_LOG(i->to_string());
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,218 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2017 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef OPENVPN_ADDR_ADDRPAIR_H
|
||||
#define OPENVPN_ADDR_ADDRPAIR_H
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include <openvpn/common/exception.hpp>
|
||||
#include <openvpn/common/number.hpp>
|
||||
#include <openvpn/common/split.hpp>
|
||||
#include <openvpn/addr/ip.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
namespace IP {
|
||||
|
||||
// AddrMaskPair is basically an object that combines an IP address (v4 or v6)
|
||||
// with a netmask or prefix length.
|
||||
struct AddrMaskPair
|
||||
{
|
||||
public:
|
||||
OPENVPN_EXCEPTION(addr_pair_mask_parse_error);
|
||||
|
||||
class StringPair {
|
||||
public:
|
||||
OPENVPN_SIMPLE_EXCEPTION(addr_pair_string_error);
|
||||
|
||||
StringPair()
|
||||
: size_(0)
|
||||
{
|
||||
}
|
||||
|
||||
explicit StringPair(const std::string& s1)
|
||||
: size_(1)
|
||||
{
|
||||
data[0] = s1;
|
||||
}
|
||||
|
||||
explicit StringPair(const std::string& s1, const std::string& s2)
|
||||
: size_(2)
|
||||
{
|
||||
data[0] = s1;
|
||||
data[1] = s2;
|
||||
}
|
||||
|
||||
void push_back(const std::string& s)
|
||||
{
|
||||
if (size_ < 2)
|
||||
data[size_++] = s;
|
||||
else
|
||||
throw addr_pair_string_error();
|
||||
}
|
||||
|
||||
const std::string& operator[](const size_t i) const
|
||||
{
|
||||
if (i >= 2)
|
||||
throw addr_pair_string_error();
|
||||
return data[i];
|
||||
}
|
||||
|
||||
std::string& operator[](const size_t i)
|
||||
{
|
||||
if (i >= 2)
|
||||
throw addr_pair_string_error();
|
||||
return data[i];
|
||||
}
|
||||
|
||||
size_t size() const { return size_; }
|
||||
|
||||
std::string render() const
|
||||
{
|
||||
switch (size_)
|
||||
{
|
||||
case 1:
|
||||
return data[0];
|
||||
case 2:
|
||||
return data[0] + "/" + data[1];
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::string data[2];
|
||||
unsigned int size_;
|
||||
};
|
||||
|
||||
static AddrMaskPair from_string(const std::string& s1, const std::string& s2, const char *title = nullptr)
|
||||
{
|
||||
try {
|
||||
if (s2.empty())
|
||||
{
|
||||
const StringPair pair = Split::by_char<StringPair, NullLex, Split::NullLimit>(s1, '/');
|
||||
return from_string_impl(pair, title);
|
||||
}
|
||||
else
|
||||
{
|
||||
const StringPair pair(s1, s2);
|
||||
return from_string_impl(pair, title);
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
const StringPair pair(s1, s2);
|
||||
error(e, pair.render(), title);
|
||||
}
|
||||
return AddrMaskPair(); // NOTREACHED
|
||||
}
|
||||
|
||||
static AddrMaskPair from_string(const std::string& s, const char *title = nullptr)
|
||||
{
|
||||
try {
|
||||
const StringPair pair = Split::by_char<StringPair, NullLex, Split::NullLimit>(s, '/');
|
||||
return from_string_impl(pair, title);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
error(e, s, title);
|
||||
}
|
||||
return AddrMaskPair(); // NOTREACHED
|
||||
}
|
||||
|
||||
static AddrMaskPair from_string(const StringPair& pair, const char *title = nullptr)
|
||||
{
|
||||
try {
|
||||
return from_string_impl(pair, title);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
error(e, pair.render(), title);
|
||||
}
|
||||
return AddrMaskPair(); // NOTREACHED
|
||||
}
|
||||
|
||||
std::string to_string(const bool netmask_form=false) const
|
||||
{
|
||||
std::ostringstream os;
|
||||
if (netmask_form)
|
||||
os << addr.to_string() << '/' << netmask.to_string();
|
||||
else
|
||||
os << addr.to_string() << '/' << netmask.prefix_len();
|
||||
return os.str();
|
||||
}
|
||||
|
||||
bool is_canonical() const
|
||||
{
|
||||
return (addr & netmask) == addr;
|
||||
}
|
||||
|
||||
Addr::Version version() const
|
||||
{
|
||||
const Addr::Version v1 = addr.version();
|
||||
const Addr::Version v2 = netmask.version();
|
||||
if (v1 == v2)
|
||||
return v1;
|
||||
else
|
||||
return Addr::UNSPEC;
|
||||
}
|
||||
|
||||
Addr addr;
|
||||
Addr netmask;
|
||||
|
||||
private:
|
||||
static void error(const std::exception& e, const std::string& s, const char *title)
|
||||
{
|
||||
if (!title)
|
||||
title = "";
|
||||
OPENVPN_THROW(addr_pair_mask_parse_error, "AddrMaskPair parse error '" << title << "': " << s << " : " << e.what());
|
||||
}
|
||||
|
||||
static AddrMaskPair from_string_impl(const StringPair& pair, const char *title = nullptr)
|
||||
{
|
||||
AddrMaskPair ret;
|
||||
if (pair.size() == 1 || pair.size() == 2)
|
||||
{
|
||||
ret.addr = Addr::from_string(pair[0], title);
|
||||
if (pair.size() == 2 && !pair[1].empty())
|
||||
{
|
||||
if (is_number(pair[1].c_str()))
|
||||
ret.netmask = Addr::netmask_from_prefix_len(ret.addr.version(),
|
||||
parse_number_throw<unsigned int>(pair[1], "prefix length"));
|
||||
else
|
||||
ret.netmask = Addr::from_string(pair[1]);
|
||||
ret.netmask.prefix_len(); // verify that netmask is ok
|
||||
}
|
||||
else
|
||||
ret.netmask = Addr::from_zero_complement(ret.addr.version());
|
||||
ret.addr.verify_version_consistency(ret.netmask);
|
||||
}
|
||||
else
|
||||
throw addr_pair_mask_parse_error("only one or two address terms allowed");
|
||||
return ret;
|
||||
}
|
||||
|
||||
};
|
||||
OPENVPN_OSTREAM(AddrMaskPair, to_string)
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,109 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2017 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// Invert a route list. Used to support excluded routes on platforms that
|
||||
// don't support them natively.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <openvpn/common/exception.hpp>
|
||||
#include <openvpn/addr/route.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
namespace IP {
|
||||
class AddressSpaceSplitter : public RouteList
|
||||
{
|
||||
public:
|
||||
OPENVPN_EXCEPTION(address_space_splitter);
|
||||
|
||||
AddressSpaceSplitter() {}
|
||||
|
||||
// NOTE: when passing AddressSpaceSplitter to this constructor, make sure
|
||||
// to static_cast it to RouteList& so as to avoid matching the
|
||||
// default copy constructor.
|
||||
explicit AddressSpaceSplitter(const RouteList& in)
|
||||
: AddressSpaceSplitter(in, in.version_mask())
|
||||
{
|
||||
}
|
||||
|
||||
AddressSpaceSplitter(const RouteList& in, const Addr::VersionMask vermask)
|
||||
{
|
||||
in.verify_canonical();
|
||||
if (vermask & Addr::V4_MASK)
|
||||
descend(in, Route(Addr::from_zero(Addr::V4), 0));
|
||||
if (vermask & Addr::V6_MASK)
|
||||
descend(in, Route(Addr::from_zero(Addr::V6), 0));
|
||||
}
|
||||
|
||||
private:
|
||||
enum Type {
|
||||
EQUAL,
|
||||
SUBROUTE,
|
||||
LEAF,
|
||||
};
|
||||
/**
|
||||
* This method construct a non-overlapping list of routes spanning the address
|
||||
* space in @param route. The routes are constructed in a way that each
|
||||
* route in the returned list is smaller or equalto each route in
|
||||
* parameter @param in
|
||||
*
|
||||
* @param route The route we currently are looking at and split if it does
|
||||
* not meet the requirements
|
||||
*/
|
||||
void descend(const RouteList& in, const Route& route)
|
||||
{
|
||||
switch (find(in, route))
|
||||
{
|
||||
case SUBROUTE:
|
||||
{
|
||||
Route r1, r2;
|
||||
if (route.split(r1, r2))
|
||||
{
|
||||
descend(in, r1);
|
||||
descend(in, r2);
|
||||
}
|
||||
else
|
||||
push_back(route);
|
||||
break;
|
||||
}
|
||||
case EQUAL:
|
||||
case LEAF:
|
||||
push_back(route);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static Type find(const RouteList& in, const Route& route)
|
||||
{
|
||||
Type type = LEAF;
|
||||
for (RouteList::const_iterator i = in.begin(); i != in.end(); ++i)
|
||||
{
|
||||
const Route& r = *i;
|
||||
if (route == r)
|
||||
type = EQUAL;
|
||||
else if (route.contains(r))
|
||||
return SUBROUTE;
|
||||
}
|
||||
return type;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,992 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2017 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef OPENVPN_ADDR_IP_H
|
||||
#define OPENVPN_ADDR_IP_H
|
||||
|
||||
#include <string>
|
||||
#include <cstring> // for std::memset
|
||||
|
||||
#include <openvpn/io/io.hpp>
|
||||
|
||||
#include <openvpn/common/size.hpp>
|
||||
#include <openvpn/common/exception.hpp>
|
||||
#include <openvpn/common/ostream.hpp>
|
||||
#include <openvpn/common/hash.hpp>
|
||||
#include <openvpn/addr/ipv4.hpp>
|
||||
#include <openvpn/addr/ipv6.hpp>
|
||||
#include <openvpn/addr/iperr.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
// This is our fundamental IP address class that handles IPv4 or IPv6
|
||||
// IP addresses. It is implemented as a discriminated union of IPv4::Addr
|
||||
// and IPv6::Addr.
|
||||
namespace IP {
|
||||
|
||||
OPENVPN_EXCEPTION(ip_exception);
|
||||
|
||||
class Addr
|
||||
{
|
||||
public:
|
||||
enum Version { UNSPEC, V4, V6 };
|
||||
|
||||
enum { V4_MASK=(1<<0), V6_MASK=(1<<1) };
|
||||
typedef unsigned int VersionMask;
|
||||
|
||||
enum VersionSize {
|
||||
V4_SIZE = IPv4::Addr::SIZE,
|
||||
V6_SIZE = IPv6::Addr::SIZE,
|
||||
};
|
||||
|
||||
Addr(const Addr& other, const char *title = nullptr, Version required_version = UNSPEC)
|
||||
: ver(other.ver)
|
||||
{
|
||||
other.validate_version(title, required_version);
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
u.v4 = other.u.v4;
|
||||
break;
|
||||
case V6:
|
||||
u.v6 = other.u.v6;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Addr(const std::string& ipstr, const char *title = nullptr, Version required_version = UNSPEC)
|
||||
: Addr(from_string(ipstr, title, required_version))
|
||||
{
|
||||
}
|
||||
|
||||
#ifndef SWIGPYTHON
|
||||
// When calling IP:Addr with None as the second parameter, Swig will
|
||||
// always pick this function and complain about not being able to convert
|
||||
// a null pointer to a const std::string reference. Hide this function, so
|
||||
// swig is forced to take the const char* variant of this function instead
|
||||
Addr(const std::string& ipstr, const std::string& title, Version required_version = UNSPEC)
|
||||
: Addr(from_string(ipstr, title.c_str(), required_version))
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
void validate_version(const char *title, Version required_version) const
|
||||
{
|
||||
if (required_version != UNSPEC && required_version != ver)
|
||||
throw ip_exception(internal::format_error(to_string(), title, version_string_static(required_version), "wrong IP version"));
|
||||
}
|
||||
|
||||
#ifndef SWIGPYTHON
|
||||
void validate_version(const std::string& title, Version required_version) const
|
||||
{
|
||||
validate_version(title.c_str(), required_version);
|
||||
}
|
||||
#endif
|
||||
|
||||
static std::string validate(const std::string& ipstr, const char *title = nullptr, Version required_version = UNSPEC)
|
||||
{
|
||||
Addr a = from_string(ipstr, title, required_version);
|
||||
return a.to_string();
|
||||
}
|
||||
|
||||
#ifndef SWIGPYTHON
|
||||
static std::string validate(const std::string& ipstr, const std::string& title, Version required_version = UNSPEC)
|
||||
{
|
||||
return validate(ipstr, title.c_str(), required_version);
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool is_valid(const std::string& ipstr)
|
||||
{
|
||||
// fast path -- rule out validity if invalid chars
|
||||
for (size_t i = 0; i < ipstr.length(); ++i)
|
||||
{
|
||||
const char c = ipstr[i];
|
||||
if (!((c >= '0' && c <= '9')
|
||||
|| (c >= 'a' && c <= 'f')
|
||||
|| (c >= 'A' && c <= 'F')
|
||||
|| (c == '.' || c == ':' || c == '%')))
|
||||
return false;
|
||||
}
|
||||
|
||||
// slow path
|
||||
{
|
||||
openvpn_io::error_code ec;
|
||||
openvpn_io::ip::make_address(ipstr, ec);
|
||||
return !ec;
|
||||
}
|
||||
}
|
||||
|
||||
static Addr from_string(const std::string& ipstr, const char *title = nullptr, Version required_version = UNSPEC)
|
||||
{
|
||||
openvpn_io::error_code ec;
|
||||
openvpn_io::ip::address a = openvpn_io::ip::make_address(ipstr, ec);
|
||||
if (ec)
|
||||
throw ip_exception(internal::format_error(ipstr, title, "", ec));
|
||||
const Addr ret = from_asio(a);
|
||||
if (required_version != UNSPEC && required_version != ret.ver)
|
||||
throw ip_exception(internal::format_error(ipstr, title, version_string_static(required_version), "wrong IP version"));
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Addr from_hex(Version v, const std::string& s)
|
||||
{
|
||||
if (v == V4)
|
||||
return from_ipv4(IPv4::Addr::from_hex(s));
|
||||
else if (v == V6)
|
||||
return from_ipv6(IPv6::Addr::from_hex(s));
|
||||
else
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
|
||||
static Addr from_ipv4(IPv4::Addr addr)
|
||||
{
|
||||
Addr a;
|
||||
a.ver = V4;
|
||||
a.u.v4 = std::move(addr);
|
||||
return a;
|
||||
}
|
||||
|
||||
static Addr from_ipv6(IPv6::Addr addr)
|
||||
{
|
||||
Addr a;
|
||||
a.ver = V6;
|
||||
a.u.v6 = std::move(addr);
|
||||
return a;
|
||||
}
|
||||
|
||||
const IPv4::Addr& to_ipv4() const
|
||||
{
|
||||
if (ver == V4)
|
||||
return u.v4;
|
||||
else
|
||||
throw ip_exception("address is not IPv4");
|
||||
}
|
||||
|
||||
const IPv6::Addr& to_ipv6() const
|
||||
{
|
||||
if (ver == V6)
|
||||
return u.v6;
|
||||
else
|
||||
throw ip_exception("address is not IPv6");
|
||||
}
|
||||
|
||||
const IPv4::Addr& to_ipv4_nocheck() const
|
||||
{
|
||||
return u.v4;
|
||||
}
|
||||
|
||||
const IPv6::Addr& to_ipv6_nocheck() const
|
||||
{
|
||||
return u.v6;
|
||||
}
|
||||
|
||||
static Addr from_sockaddr(const struct sockaddr *sa)
|
||||
{
|
||||
if (sa->sa_family == AF_INET)
|
||||
return from_ipv4(IPv4::Addr::from_sockaddr((struct sockaddr_in *)sa));
|
||||
else if (sa->sa_family == AF_INET6)
|
||||
return from_ipv6(IPv6::Addr::from_sockaddr((struct sockaddr_in6 *)sa));
|
||||
else
|
||||
return Addr();
|
||||
}
|
||||
|
||||
static bool sockaddr_defined(const struct sockaddr *sa)
|
||||
{
|
||||
return sa && (sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
|
||||
}
|
||||
|
||||
static Addr from_ulong(Version v, unsigned long ul)
|
||||
{
|
||||
if (v == V4)
|
||||
return from_ipv4(IPv4::Addr::from_ulong(ul));
|
||||
else if (v == V6)
|
||||
return from_ipv6(IPv6::Addr::from_ulong(ul));
|
||||
else
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
|
||||
// return *this as a ulong, will raise exception on overflow
|
||||
unsigned long to_ulong() const
|
||||
{
|
||||
if (ver == V4)
|
||||
return u.v4.to_ulong();
|
||||
else if (ver == V6)
|
||||
return u.v6.to_ulong();
|
||||
else
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
|
||||
static Addr from_long(Version v, long ul)
|
||||
{
|
||||
if (v == V4)
|
||||
return from_ipv4(IPv4::Addr::from_long(ul));
|
||||
else if (v == V6)
|
||||
return from_ipv6(IPv6::Addr::from_long(ul));
|
||||
else
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
|
||||
// return *this as a long, will raise exception on overflow
|
||||
long to_long() const
|
||||
{
|
||||
if (ver == V4)
|
||||
return u.v4.to_long();
|
||||
else if (ver == V6)
|
||||
return u.v6.to_long();
|
||||
else
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
|
||||
// return Addr from 16 byte binary string
|
||||
static Addr from_byte_string(const unsigned char *bytestr)
|
||||
{
|
||||
Addr a;
|
||||
if (IPv6::Addr::byte_string_is_v4(bytestr))
|
||||
{
|
||||
a.ver = V4;
|
||||
a.u.v4 = IPv4::Addr::from_uint32_net(IPv6::Addr::v4_from_byte_string(bytestr));
|
||||
}
|
||||
else
|
||||
{
|
||||
a.ver = V6;
|
||||
a.u.v6 = IPv6::Addr::from_byte_string(bytestr);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
// convert Addr to 16 byte binary string
|
||||
void to_byte_string(unsigned char *bytestr) const
|
||||
{
|
||||
if (ver == V4)
|
||||
IPv6::Addr::v4_to_byte_string(bytestr, u.v4.to_uint32_net());
|
||||
else if (ver == V6)
|
||||
u.v6.to_byte_string(bytestr);
|
||||
else
|
||||
std::memset(bytestr, 0, 16);
|
||||
}
|
||||
|
||||
// convert Addr to variable length byte string
|
||||
void to_byte_string_variable(unsigned char *bytestr) const
|
||||
{
|
||||
if (ver == V4)
|
||||
u.v4.to_byte_string(bytestr);
|
||||
else if (ver == V6)
|
||||
u.v6.to_byte_string(bytestr);
|
||||
else
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
|
||||
std::uint32_t to_uint32_net() const // return value in net byte order
|
||||
{
|
||||
if (ver == V4)
|
||||
return u.v4.to_uint32_net();
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
// construct an address where all bits are zero
|
||||
static Addr from_zero(Version v)
|
||||
{
|
||||
if (v == V4)
|
||||
return from_ipv4(IPv4::Addr::from_zero());
|
||||
else if (v == V6)
|
||||
return from_ipv6(IPv6::Addr::from_zero());
|
||||
else
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
|
||||
// construct an address where all bits are zero
|
||||
static Addr from_one(Version v)
|
||||
{
|
||||
if (v == V4)
|
||||
return from_ipv4(IPv4::Addr::from_one());
|
||||
else if (v == V6)
|
||||
return from_ipv6(IPv6::Addr::from_one());
|
||||
else
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
|
||||
// construct an address where all bits are one
|
||||
static Addr from_zero_complement(Version v)
|
||||
{
|
||||
if (v == V4)
|
||||
return from_ipv4(IPv4::Addr::from_zero_complement());
|
||||
else if (v == V6)
|
||||
return from_ipv6(IPv6::Addr::from_zero_complement());
|
||||
else
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
|
||||
// validate the prefix length for the IP version
|
||||
static bool validate_prefix_len(Version v, const unsigned int prefix_len)
|
||||
{
|
||||
if (v == V4)
|
||||
{
|
||||
if (prefix_len <= V4_SIZE)
|
||||
return true;
|
||||
}
|
||||
else if (v == V6)
|
||||
{
|
||||
if (prefix_len <= V6_SIZE)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// build a netmask using given prefix_len
|
||||
static Addr netmask_from_prefix_len(Version v, const unsigned int prefix_len)
|
||||
{
|
||||
if (v == V4)
|
||||
return from_ipv4(IPv4::Addr::netmask_from_prefix_len(prefix_len));
|
||||
else if (v == V6)
|
||||
return from_ipv6(IPv6::Addr::netmask_from_prefix_len(prefix_len));
|
||||
else
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
|
||||
// build a netmask using *this as extent
|
||||
Addr netmask_from_extent() const
|
||||
{
|
||||
if (ver == V4)
|
||||
return from_ipv4(u.v4.netmask_from_extent());
|
||||
else if (ver == V6)
|
||||
return from_ipv6(u.v6.netmask_from_extent());
|
||||
else
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
|
||||
std::string to_string() const
|
||||
{
|
||||
if (ver != UNSPEC)
|
||||
{
|
||||
const openvpn_io::ip::address a = to_asio();
|
||||
std::string ret = a.to_string();
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
return "UNSPEC";
|
||||
}
|
||||
|
||||
std::string to_string_bracket_ipv6() const
|
||||
{
|
||||
std::string ret;
|
||||
if (ver == V6)
|
||||
ret += '[';
|
||||
ret += to_string();
|
||||
if (ver == V6)
|
||||
ret += ']';
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string to_hex() const
|
||||
{
|
||||
if (ver == V4)
|
||||
return u.v4.to_hex();
|
||||
else if (ver == V6)
|
||||
return u.v6.to_hex();
|
||||
else
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
|
||||
std::string arpa() const
|
||||
{
|
||||
if (ver == V4)
|
||||
return u.v4.arpa();
|
||||
else if (ver == V6)
|
||||
return u.v6.arpa();
|
||||
else
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
|
||||
static Addr from_asio(const openvpn_io::ip::address& addr)
|
||||
{
|
||||
if (addr.is_v4())
|
||||
{
|
||||
Addr a;
|
||||
a.ver = V4;
|
||||
a.u.v4 = IPv4::Addr::from_asio(addr.to_v4());
|
||||
return a;
|
||||
}
|
||||
else if (addr.is_v6())
|
||||
{
|
||||
Addr a;
|
||||
a.ver = V6;
|
||||
a.u.v6 = IPv6::Addr::from_asio(addr.to_v6());
|
||||
return a;
|
||||
}
|
||||
else
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
|
||||
openvpn_io::ip::address to_asio() const
|
||||
{
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
return openvpn_io::ip::address_v4(u.v4.to_asio());
|
||||
case V6:
|
||||
return openvpn_io::ip::address_v6(u.v6.to_asio());
|
||||
default:
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
}
|
||||
|
||||
Addr operator+(const long delta) const {
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
{
|
||||
Addr ret;
|
||||
ret.ver = V4;
|
||||
ret.u.v4 = u.v4 + delta;
|
||||
return ret;
|
||||
}
|
||||
case V6:
|
||||
{
|
||||
Addr ret;
|
||||
ret.ver = V6;
|
||||
ret.u.v6 = u.v6 + delta;
|
||||
return ret;
|
||||
}
|
||||
default:
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
}
|
||||
|
||||
Addr operator-(const long delta) const {
|
||||
return operator+(-delta);
|
||||
}
|
||||
|
||||
#define OPENVPN_IP_OPERATOR_BINOP(OP) \
|
||||
Addr operator OP (const Addr& other) const { \
|
||||
if (ver != other.ver) \
|
||||
throw ip_exception("version inconsistency"); \
|
||||
switch (ver) \
|
||||
{ \
|
||||
case V4: \
|
||||
{ \
|
||||
Addr ret; \
|
||||
ret.ver = V4; \
|
||||
ret.u.v4 = u.v4 OP other.u.v4; \
|
||||
return ret; \
|
||||
} \
|
||||
case V6: \
|
||||
{ \
|
||||
Addr ret; \
|
||||
ret.ver = V6; \
|
||||
ret.u.v6 = u.v6 OP other.u.v6; \
|
||||
return ret; \
|
||||
} \
|
||||
default: \
|
||||
throw ip_exception("address unspecified"); \
|
||||
} \
|
||||
}
|
||||
|
||||
OPENVPN_IP_OPERATOR_BINOP(+)
|
||||
OPENVPN_IP_OPERATOR_BINOP(-)
|
||||
OPENVPN_IP_OPERATOR_BINOP(*)
|
||||
OPENVPN_IP_OPERATOR_BINOP(/)
|
||||
OPENVPN_IP_OPERATOR_BINOP(%)
|
||||
OPENVPN_IP_OPERATOR_BINOP(&)
|
||||
OPENVPN_IP_OPERATOR_BINOP(|)
|
||||
|
||||
#undef OPENVPN_IP_OPERATOR_BINOP
|
||||
|
||||
Addr operator<<(const unsigned int shift) const {
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
{
|
||||
Addr ret;
|
||||
ret.ver = V4;
|
||||
ret.u.v4 = u.v4 << shift;
|
||||
return ret;
|
||||
}
|
||||
case V6:
|
||||
{
|
||||
Addr ret;
|
||||
ret.ver = V6;
|
||||
ret.u.v6 = u.v6 << shift;
|
||||
return ret;
|
||||
}
|
||||
default:
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
}
|
||||
|
||||
Addr operator>>(const unsigned int shift) const {
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
{
|
||||
Addr ret;
|
||||
ret.ver = V4;
|
||||
ret.u.v4 = u.v4 >> shift;
|
||||
return ret;
|
||||
}
|
||||
case V6:
|
||||
{
|
||||
Addr ret;
|
||||
ret.ver = V6;
|
||||
ret.u.v6 = u.v6 >> shift;
|
||||
return ret;
|
||||
}
|
||||
default:
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
}
|
||||
|
||||
Addr operator~() const {
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
{
|
||||
Addr ret;
|
||||
ret.ver = V4;
|
||||
ret.u.v4 = ~u.v4;
|
||||
return ret;
|
||||
}
|
||||
case V6:
|
||||
{
|
||||
Addr ret;
|
||||
ret.ver = V6;
|
||||
ret.u.v6 = ~u.v6;
|
||||
return ret;
|
||||
}
|
||||
default:
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
}
|
||||
|
||||
Addr network_addr(const unsigned int prefix_len) const {
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
{
|
||||
Addr ret;
|
||||
ret.ver = V4;
|
||||
ret.u.v4 = u.v4.network_addr(prefix_len);
|
||||
return ret;
|
||||
}
|
||||
case V6:
|
||||
{
|
||||
Addr ret;
|
||||
ret.ver = V6;
|
||||
ret.u.v6 = u.v6.network_addr(prefix_len);
|
||||
return ret;
|
||||
}
|
||||
default:
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
}
|
||||
|
||||
bool operator==(const Addr& other) const
|
||||
{
|
||||
switch (ver)
|
||||
{
|
||||
case UNSPEC:
|
||||
return other.ver == UNSPEC;
|
||||
case V4:
|
||||
if (ver == other.ver)
|
||||
return u.v4 == other.u.v4;
|
||||
break;
|
||||
case V6:
|
||||
if (ver == other.ver)
|
||||
return u.v6 == other.u.v6;
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool operator!=(const Addr& other) const
|
||||
{
|
||||
return !operator==(other);
|
||||
}
|
||||
|
||||
#define OPENVPN_IP_OPERATOR_REL(OP) \
|
||||
bool operator OP(const Addr& other) const \
|
||||
{ \
|
||||
if (ver == other.ver) \
|
||||
{ \
|
||||
switch (ver) \
|
||||
{ \
|
||||
case V4: \
|
||||
return u.v4 OP other.u.v4; \
|
||||
case V6: \
|
||||
return u.v6 OP other.u.v6; \
|
||||
default: \
|
||||
return false; \
|
||||
} \
|
||||
} \
|
||||
else if (ver OP other.ver) \
|
||||
return true; \
|
||||
else \
|
||||
return false; \
|
||||
}
|
||||
|
||||
OPENVPN_IP_OPERATOR_REL(<)
|
||||
OPENVPN_IP_OPERATOR_REL(>)
|
||||
OPENVPN_IP_OPERATOR_REL(<=)
|
||||
OPENVPN_IP_OPERATOR_REL(>=)
|
||||
|
||||
#undef OPENVPN_IP_OPERATOR_REL
|
||||
|
||||
bool unspecified() const
|
||||
{
|
||||
return all_zeros();
|
||||
}
|
||||
|
||||
bool specified() const
|
||||
{
|
||||
return !unspecified();
|
||||
}
|
||||
|
||||
bool all_zeros() const
|
||||
{
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
return u.v4.all_zeros();
|
||||
case V6:
|
||||
return u.v6.all_zeros();
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool all_ones() const
|
||||
{
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
return u.v4.all_ones();
|
||||
case V6:
|
||||
return u.v6.all_ones();
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool is_loopback() const
|
||||
{
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
return u.v4.is_loopback();
|
||||
case V6:
|
||||
return u.v6.is_loopback();
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool defined() const
|
||||
{
|
||||
return ver != UNSPEC;
|
||||
}
|
||||
|
||||
const char *version_string() const
|
||||
{
|
||||
return version_string_static(ver);
|
||||
}
|
||||
|
||||
static const char *version_string_static(Version ver)
|
||||
{
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
return "v4";
|
||||
case V6:
|
||||
return "v6";
|
||||
default:
|
||||
return "v?";
|
||||
}
|
||||
}
|
||||
|
||||
Version version() const { return ver; }
|
||||
|
||||
static VersionMask version_mask(const Version ver)
|
||||
{
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
return V4_MASK;
|
||||
case V6:
|
||||
return V6_MASK;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
VersionMask version_mask() const
|
||||
{
|
||||
return version_mask(ver);
|
||||
}
|
||||
|
||||
int version_index() const
|
||||
{
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
return 0;
|
||||
case V6:
|
||||
return 1;
|
||||
default:
|
||||
throw ip_exception("version index undefined");
|
||||
}
|
||||
}
|
||||
|
||||
int family() const
|
||||
{
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
return AF_INET;
|
||||
case V6:
|
||||
return AF_INET6;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
bool is_compatible(const Addr& other) const
|
||||
{
|
||||
return ver == other.ver;
|
||||
}
|
||||
|
||||
bool is_ipv6() const
|
||||
{
|
||||
return ver == V6;
|
||||
}
|
||||
|
||||
void verify_version_consistency(const Addr& other) const
|
||||
{
|
||||
if (!is_compatible(other))
|
||||
throw ip_exception("version inconsistency");
|
||||
}
|
||||
|
||||
// throw exception if address is not a valid netmask
|
||||
void validate_netmask()
|
||||
{
|
||||
prefix_len();
|
||||
}
|
||||
|
||||
// number of network bits in netmask,
|
||||
// throws exception if addr is not a netmask
|
||||
unsigned int prefix_len() const
|
||||
{
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
return u.v4.prefix_len();
|
||||
case V6:
|
||||
return u.v6.prefix_len();
|
||||
default:
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
}
|
||||
|
||||
// IPv6 scope ID or -1 if not IPv6
|
||||
int scope_id() const
|
||||
{
|
||||
return ver == V6 ? u.v6.scope_id() : -1;
|
||||
}
|
||||
|
||||
// number of host bits in netmask
|
||||
unsigned int host_len() const
|
||||
{
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
return u.v4.host_len();
|
||||
case V6:
|
||||
return u.v6.host_len();
|
||||
default:
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
}
|
||||
|
||||
// return the number of host addresses contained within netmask
|
||||
Addr extent_from_netmask() const
|
||||
{
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
return from_ipv4(u.v4.extent_from_netmask());
|
||||
case V6:
|
||||
return from_ipv6(u.v6.extent_from_netmask());
|
||||
default:
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
}
|
||||
|
||||
// address size in bits
|
||||
unsigned int size() const
|
||||
{
|
||||
return version_size(ver);
|
||||
}
|
||||
|
||||
// address size in bytes
|
||||
unsigned int size_bytes() const
|
||||
{
|
||||
return size() / 8;
|
||||
}
|
||||
|
||||
// address size in bits of particular IP version
|
||||
static unsigned int version_size(Version v)
|
||||
{
|
||||
if (v == V4)
|
||||
return IPv4::Addr::SIZE;
|
||||
else if (v == V6)
|
||||
return IPv6::Addr::SIZE;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename HASH>
|
||||
void hash(HASH& h) const
|
||||
{
|
||||
switch (ver)
|
||||
{
|
||||
case Addr::V4:
|
||||
u.v4.hash(h);
|
||||
break;
|
||||
case Addr::V6:
|
||||
u.v6.hash(h);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HAVE_CITYHASH
|
||||
std::size_t hashval() const
|
||||
{
|
||||
HashSizeT h;
|
||||
hash(h);
|
||||
return h.value();
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef OPENVPN_IP_IMMUTABLE
|
||||
private:
|
||||
#endif
|
||||
|
||||
Addr()
|
||||
: ver(UNSPEC)
|
||||
{
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
ver = UNSPEC;
|
||||
}
|
||||
|
||||
Addr& operator=(const Addr& other)
|
||||
{
|
||||
switch (ver = other.ver)
|
||||
{
|
||||
case V4:
|
||||
u.v4 = other.u.v4;
|
||||
break;
|
||||
case V6:
|
||||
u.v6 = other.u.v6;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Addr& operator++()
|
||||
{
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
++u.v4;
|
||||
break;
|
||||
case V6:
|
||||
++u.v6;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Addr& operator+=(const long delta)
|
||||
{
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
u.v4 += delta;
|
||||
break;
|
||||
case V6:
|
||||
u.v6 += delta;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Addr& operator-=(const long delta)
|
||||
{
|
||||
switch (ver)
|
||||
{
|
||||
case V4:
|
||||
u.v4 -= delta;
|
||||
break;
|
||||
case V6:
|
||||
u.v6 -= delta;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void reset_ipv4_from_uint32(const IPv4::Addr::base_type addr)
|
||||
{
|
||||
ver = V4;
|
||||
u.v4 = IPv4::Addr::from_uint32(addr);
|
||||
}
|
||||
|
||||
private:
|
||||
union {
|
||||
IPv4::Addr v4;
|
||||
IPv6::Addr v6;
|
||||
} u;
|
||||
|
||||
Version ver;
|
||||
};
|
||||
|
||||
OPENVPN_OSTREAM(Addr, to_string)
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HAVE_CITYHASH
|
||||
OPENVPN_HASH_METHOD(openvpn::IP::Addr, hashval);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,75 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2017 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef OPENVPN_ADDR_IPERR_H
|
||||
#define OPENVPN_ADDR_IPERR_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <openvpn/io/io.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
namespace IP {
|
||||
namespace internal {
|
||||
// Called internally by IP, IPv4, and IPv6 classes
|
||||
|
||||
inline std::string format_error(const std::string& ipstr, const char *title, const char *ipver, const openvpn_io::error_code& ec)
|
||||
{
|
||||
std::string err = "error parsing";
|
||||
if (title)
|
||||
{
|
||||
err += ' ';
|
||||
err += title;
|
||||
}
|
||||
err += " IP";
|
||||
err += ipver;
|
||||
err += " address '";
|
||||
err += ipstr;
|
||||
err += "' : ";
|
||||
err += ec.message();
|
||||
return err;
|
||||
}
|
||||
|
||||
inline std::string format_error(const std::string& ipstr, const char *title, const char *ipver, const char *message)
|
||||
{
|
||||
std::string err = "error parsing";
|
||||
if (title)
|
||||
{
|
||||
err += ' ';
|
||||
err += title;
|
||||
}
|
||||
err += " IP";
|
||||
err += ipver;
|
||||
err += " address '";
|
||||
err += ipstr;
|
||||
err += '\'';
|
||||
if (message)
|
||||
{
|
||||
err += " : ";
|
||||
err += message;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,588 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2017 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef OPENVPN_ADDR_IPV4_H
|
||||
#define OPENVPN_ADDR_IPV4_H
|
||||
|
||||
#include <cstring> // for std::memcpy, std::memset
|
||||
#include <sstream>
|
||||
#include <cstdint> // for std::uint32_t
|
||||
|
||||
#include <openvpn/io/io.hpp>
|
||||
|
||||
#include <openvpn/common/size.hpp>
|
||||
#include <openvpn/common/exception.hpp>
|
||||
#include <openvpn/common/endian.hpp>
|
||||
#include <openvpn/common/ostream.hpp>
|
||||
#include <openvpn/common/socktypes.hpp>
|
||||
#include <openvpn/common/ffs.hpp>
|
||||
#include <openvpn/common/hexstr.hpp>
|
||||
#include <openvpn/common/hash.hpp>
|
||||
#include <openvpn/addr/iperr.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
namespace IP {
|
||||
class Addr;
|
||||
}
|
||||
|
||||
// Fundamental classes for representing an IPv4 IP address.
|
||||
|
||||
namespace IPv4 {
|
||||
|
||||
OPENVPN_EXCEPTION(ipv4_exception);
|
||||
|
||||
class Addr // NOTE: must be union-legal, so default constructor does not initialize
|
||||
{
|
||||
friend class IP::Addr;
|
||||
|
||||
public:
|
||||
enum { SIZE=32 };
|
||||
|
||||
typedef std::uint32_t base_type;
|
||||
typedef std::int32_t signed_base_type;
|
||||
|
||||
bool defined() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static Addr from_addr(const Addr& addr)
|
||||
{
|
||||
return addr;
|
||||
}
|
||||
|
||||
static Addr from_in_addr(const struct in_addr *in4)
|
||||
{
|
||||
Addr ret;
|
||||
ret.u.addr = ntohl(in4->s_addr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct in_addr to_in_addr() const
|
||||
{
|
||||
struct in_addr ret;
|
||||
ret.s_addr = htonl(u.addr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Addr from_sockaddr(const struct sockaddr_in *sa)
|
||||
{
|
||||
Addr ret;
|
||||
ret.u.addr = ntohl(sa->sin_addr.s_addr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct sockaddr_in to_sockaddr(const unsigned short port=0) const
|
||||
{
|
||||
struct sockaddr_in ret;
|
||||
std::memset(&ret, 0, sizeof(ret));
|
||||
ret.sin_family = AF_INET;
|
||||
ret.sin_port = htons(port);
|
||||
ret.sin_addr.s_addr = htonl(u.addr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Addr from_uint32(const base_type addr) // host byte order
|
||||
{
|
||||
Addr ret;
|
||||
ret.u.addr = addr;
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::uint32_t to_uint32() const // host byte order
|
||||
{
|
||||
return u.addr;
|
||||
}
|
||||
|
||||
static Addr from_uint32_net(const base_type addr) // addr in net byte order
|
||||
{
|
||||
Addr ret;
|
||||
ret.u.addr = ntohl(addr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void to_byte_string(unsigned char *bytestr) const
|
||||
{
|
||||
*(base_type*)bytestr = ntohl(u.addr);
|
||||
}
|
||||
|
||||
std::uint32_t to_uint32_net() const // return value in net byte order
|
||||
{
|
||||
return htonl(u.addr);
|
||||
}
|
||||
|
||||
static Addr from_ulong(unsigned long ul)
|
||||
{
|
||||
Addr ret;
|
||||
ret.u.addr = (base_type)ul;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// return *this as a unsigned long
|
||||
unsigned long to_ulong() const
|
||||
{
|
||||
return (unsigned long)u.addr;
|
||||
}
|
||||
|
||||
static Addr from_long(long ul)
|
||||
{
|
||||
Addr ret;
|
||||
ret.u.addr = (base_type)(signed_base_type)ul;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// return *this as a long
|
||||
long to_long() const
|
||||
{
|
||||
return (long)(signed_base_type)u.addr;
|
||||
}
|
||||
|
||||
static Addr from_bytes(const unsigned char *bytes) // host byte order
|
||||
{
|
||||
Addr ret;
|
||||
std::memcpy(ret.u.bytes, bytes, 4);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Addr from_bytes_net(const unsigned char *bytes) // network byte order
|
||||
{
|
||||
Addr ret;
|
||||
std::memcpy(ret.u.bytes, bytes, 4);
|
||||
ret.u.addr = ntohl(ret.u.addr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Addr from_zero()
|
||||
{
|
||||
Addr ret;
|
||||
ret.zero();
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Addr from_one()
|
||||
{
|
||||
Addr ret;
|
||||
ret.one();
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Addr from_zero_complement()
|
||||
{
|
||||
Addr ret;
|
||||
ret.zero_complement();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// build a netmask using given prefix_len
|
||||
static Addr netmask_from_prefix_len(const unsigned int prefix_len)
|
||||
{
|
||||
Addr ret;
|
||||
ret.u.addr = prefix_len_to_netmask(prefix_len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// build a netmask using given extent
|
||||
Addr netmask_from_extent() const
|
||||
{
|
||||
const int lb = find_last_set(u.addr - 1);
|
||||
return netmask_from_prefix_len(SIZE - lb);
|
||||
}
|
||||
|
||||
static Addr from_string(const std::string& ipstr, const char *title = nullptr)
|
||||
{
|
||||
openvpn_io::error_code ec;
|
||||
openvpn_io::ip::address_v4 a = openvpn_io::ip::make_address_v4(ipstr, ec);
|
||||
if (ec)
|
||||
throw ipv4_exception(IP::internal::format_error(ipstr, title, "v4", ec));
|
||||
return from_asio(a);
|
||||
}
|
||||
|
||||
std::string to_string() const
|
||||
{
|
||||
const openvpn_io::ip::address_v4 a = to_asio();
|
||||
std::string ret = a.to_string();
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Addr from_hex(const std::string& s)
|
||||
{
|
||||
Addr ret;
|
||||
ret.u.addr = 0;
|
||||
size_t len = s.length();
|
||||
size_t base = 0;
|
||||
if (len > 0 && s[len-1] == 'L')
|
||||
len -= 1;
|
||||
if (len >= 2 && s[0] == '0' && s[1] == 'x')
|
||||
{
|
||||
base = 2;
|
||||
len -= 2;
|
||||
}
|
||||
if (len < 1 || len > 8)
|
||||
throw ipv4_exception("parse hex error");
|
||||
size_t di = (len-1)>>1;
|
||||
for (int i = (len & 1) ? -1 : 0; i < int(len); i += 2)
|
||||
{
|
||||
const size_t idx = base + i;
|
||||
const int bh = (i >= 0) ? parse_hex_char(s[idx]) : 0;
|
||||
const int bl = parse_hex_char(s[idx+1]);
|
||||
if (bh == -1 || bl == -1)
|
||||
throw ipv4_exception("parse hex error");
|
||||
ret.u.bytes[Endian::e4(di--)] = (bh<<4) + bl;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string to_hex() const
|
||||
{
|
||||
std::string ret;
|
||||
ret.reserve(8);
|
||||
bool firstnonzero = false;
|
||||
for (size_t i = 0; i < 4; ++i)
|
||||
{
|
||||
const unsigned char b = u.bytes[Endian::e4rev(i)];
|
||||
if (b || firstnonzero || i == 3)
|
||||
{
|
||||
const char bh = b >> 4;
|
||||
if (bh || firstnonzero)
|
||||
ret += render_hex_char(bh);
|
||||
ret += render_hex_char(b & 0x0F);
|
||||
firstnonzero = true;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string arpa() const
|
||||
{
|
||||
std::ostringstream os;
|
||||
os << int(u.bytes[Endian::e4(0)]) << '.'
|
||||
<< int(u.bytes[Endian::e4(1)]) << '.'
|
||||
<< int(u.bytes[Endian::e4(2)]) << '.'
|
||||
<< int(u.bytes[Endian::e4(3)]) << ".in-addr.arpa";
|
||||
return os.str();
|
||||
}
|
||||
|
||||
static Addr from_asio(const openvpn_io::ip::address_v4& asio_addr)
|
||||
{
|
||||
Addr ret;
|
||||
ret.u.addr = (std::uint32_t)asio_addr.to_uint();
|
||||
return ret;
|
||||
}
|
||||
|
||||
openvpn_io::ip::address_v4 to_asio() const
|
||||
{
|
||||
return openvpn_io::ip::address_v4(u.addr);
|
||||
}
|
||||
|
||||
Addr operator&(const Addr& other) const {
|
||||
Addr ret;
|
||||
ret.u.addr = u.addr & other.u.addr;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator|(const Addr& other) const {
|
||||
Addr ret;
|
||||
ret.u.addr = u.addr | other.u.addr;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator+(const long delta) const {
|
||||
Addr ret;
|
||||
ret.u.addr = u.addr + (std::uint32_t)delta;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator+(const Addr& other) const {
|
||||
Addr ret;
|
||||
ret.u.addr = u.addr + other.u.addr;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator-(const long delta) const {
|
||||
return operator+(-delta);
|
||||
}
|
||||
|
||||
Addr operator-(const Addr& other) const {
|
||||
Addr ret;
|
||||
ret.u.addr = u.addr - other.u.addr;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator*(const Addr& other) const {
|
||||
Addr ret;
|
||||
ret.u.addr = u.addr * other.u.addr;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator/(const Addr& other) const {
|
||||
Addr ret;
|
||||
ret.u.addr = u.addr / other.u.addr;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator%(const Addr& other) const {
|
||||
Addr ret;
|
||||
ret.u.addr = u.addr % other.u.addr;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator<<(const unsigned int shift) const {
|
||||
Addr ret;
|
||||
ret.u.addr = u.addr << shift;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator>>(const unsigned int shift) const {
|
||||
Addr ret;
|
||||
ret.u.addr = u.addr >> shift;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator~() const {
|
||||
Addr ret;
|
||||
ret.u.addr = ~u.addr;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// return the network that contains the current address
|
||||
Addr network_addr(const unsigned int prefix_len) const
|
||||
{
|
||||
Addr ret;
|
||||
ret.u.addr = u.addr & prefix_len_to_netmask(prefix_len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool operator==(const Addr& other) const
|
||||
{
|
||||
return u.addr == other.u.addr;
|
||||
}
|
||||
|
||||
bool operator!=(const Addr& other) const
|
||||
{
|
||||
return u.addr != other.u.addr;
|
||||
}
|
||||
|
||||
bool operator<(const Addr& other) const
|
||||
{
|
||||
return u.addr < other.u.addr;
|
||||
}
|
||||
|
||||
bool operator>(const Addr& other) const
|
||||
{
|
||||
return u.addr > other.u.addr;
|
||||
}
|
||||
|
||||
bool operator<=(const Addr& other) const
|
||||
{
|
||||
return u.addr <= other.u.addr;
|
||||
}
|
||||
|
||||
bool operator>=(const Addr& other) const
|
||||
{
|
||||
return u.addr >= other.u.addr;
|
||||
}
|
||||
|
||||
bool unspecified() const
|
||||
{
|
||||
return all_zeros();
|
||||
}
|
||||
|
||||
bool specified() const
|
||||
{
|
||||
return !unspecified();
|
||||
}
|
||||
|
||||
bool all_zeros() const
|
||||
{
|
||||
return u.addr == 0;
|
||||
}
|
||||
|
||||
bool all_ones() const
|
||||
{
|
||||
return ~u.addr == 0;
|
||||
}
|
||||
|
||||
bool is_loopback() const
|
||||
{
|
||||
return (u.addr & 0x7F000000) == 0x7F000000;
|
||||
}
|
||||
|
||||
// number of network bits in netmask,
|
||||
// throws exception if addr is not a netmask
|
||||
unsigned int prefix_len() const
|
||||
{
|
||||
const int ret = prefix_len_32(u.addr);
|
||||
if (ret >= 0)
|
||||
return ret;
|
||||
else
|
||||
throw ipv4_exception("malformed netmask");
|
||||
}
|
||||
|
||||
int prefix_len_nothrow() const
|
||||
{
|
||||
return prefix_len_32(u.addr);
|
||||
}
|
||||
|
||||
// number of host bits in netmask
|
||||
unsigned int host_len() const
|
||||
{
|
||||
return SIZE - prefix_len();
|
||||
}
|
||||
|
||||
// return the number of host addresses contained within netmask
|
||||
Addr extent_from_netmask() const
|
||||
{
|
||||
Addr ret;
|
||||
ret.u.addr = extent_from_netmask_uint32();
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::uint32_t extent_from_netmask_uint32() const
|
||||
{
|
||||
const unsigned int hl = host_len();
|
||||
if (hl < SIZE)
|
||||
return 1 << hl;
|
||||
else if (hl == SIZE)
|
||||
return 0;
|
||||
else
|
||||
throw ipv4_exception("extent overflow");
|
||||
}
|
||||
|
||||
// convert netmask in addr to prefix_len, will return -1 on error
|
||||
static int prefix_len_32(const std::uint32_t addr)
|
||||
{
|
||||
if (addr == ~std::uint32_t(0))
|
||||
return 32;
|
||||
else if (addr == 0)
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
unsigned int high = 32;
|
||||
unsigned int low = 1;
|
||||
for (unsigned int i = 0; i < 5; ++i)
|
||||
{
|
||||
const unsigned int mid = (high + low) / 2;
|
||||
const IPv4::Addr::base_type test = prefix_len_to_netmask_unchecked(mid);
|
||||
if (addr == test)
|
||||
return mid;
|
||||
else if (addr > test)
|
||||
low = mid;
|
||||
else
|
||||
high = mid;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// address size in bits
|
||||
static unsigned int size()
|
||||
{
|
||||
return SIZE;
|
||||
}
|
||||
|
||||
template <typename HASH>
|
||||
void hash(HASH& h) const
|
||||
{
|
||||
h(u.addr);
|
||||
}
|
||||
|
||||
#ifdef HAVE_CITYHASH
|
||||
std::size_t hashval() const
|
||||
{
|
||||
HashSizeT h;
|
||||
hash(h);
|
||||
return h.value();
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef OPENVPN_IP_IMMUTABLE
|
||||
private:
|
||||
#endif
|
||||
|
||||
void negate()
|
||||
{
|
||||
u.addr = ~u.addr;
|
||||
}
|
||||
|
||||
void zero()
|
||||
{
|
||||
u.addr = 0;
|
||||
}
|
||||
|
||||
void zero_complement()
|
||||
{
|
||||
u.addr = ~0;
|
||||
}
|
||||
|
||||
void one()
|
||||
{
|
||||
u.addr = 1;
|
||||
}
|
||||
|
||||
Addr& operator++()
|
||||
{
|
||||
++u.addr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Addr& operator+=(const long delta)
|
||||
{
|
||||
u.addr += (std::uint32_t)delta;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Addr& operator-=(const long delta)
|
||||
{
|
||||
return operator+=(-delta);
|
||||
}
|
||||
|
||||
private:
|
||||
static base_type prefix_len_to_netmask_unchecked(const unsigned int prefix_len)
|
||||
{
|
||||
if (prefix_len)
|
||||
return ~((1 << (SIZE - prefix_len)) - 1);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
static base_type prefix_len_to_netmask(const unsigned int prefix_len)
|
||||
{
|
||||
if (prefix_len <= SIZE)
|
||||
return prefix_len_to_netmask_unchecked(prefix_len);
|
||||
else
|
||||
throw ipv4_exception("bad prefix len");
|
||||
}
|
||||
|
||||
union {
|
||||
base_type addr; // host byte order
|
||||
unsigned char bytes[4];
|
||||
} u;
|
||||
};
|
||||
|
||||
OPENVPN_OSTREAM(Addr, to_string)
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HAVE_CITYHASH
|
||||
OPENVPN_HASH_METHOD(openvpn::IPv4::Addr, hashval);
|
||||
#endif
|
||||
|
||||
#endif // OPENVPN_ADDR_IPV4_H
|
||||
@@ -0,0 +1,847 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2017 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef OPENVPN_ADDR_IPV6_H
|
||||
#define OPENVPN_ADDR_IPV6_H
|
||||
|
||||
#include <cstring> // for std::memcpy, std::memset
|
||||
#include <algorithm> // for std::min
|
||||
#include <cstdint> // for std::uint32_t
|
||||
|
||||
#include <openvpn/io/io.hpp>
|
||||
|
||||
#include <openvpn/common/size.hpp>
|
||||
#include <openvpn/common/exception.hpp>
|
||||
#include <openvpn/common/ostream.hpp>
|
||||
#include <openvpn/common/socktypes.hpp>
|
||||
#include <openvpn/common/ffs.hpp>
|
||||
#include <openvpn/common/hexstr.hpp>
|
||||
#include <openvpn/common/hash.hpp>
|
||||
#include <openvpn/addr/ipv4.hpp>
|
||||
#include <openvpn/addr/iperr.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
namespace IP {
|
||||
class Addr;
|
||||
}
|
||||
|
||||
// Fundamental classes for representing an IPv6 IP address.
|
||||
|
||||
namespace IPv6 {
|
||||
|
||||
OPENVPN_EXCEPTION(ipv6_exception);
|
||||
|
||||
class Addr // NOTE: must be union-legal, so default constructor does not initialize
|
||||
{
|
||||
friend class IP::Addr;
|
||||
|
||||
public:
|
||||
enum { SIZE=128 };
|
||||
|
||||
bool defined() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static Addr from_addr(const Addr& addr)
|
||||
{
|
||||
return addr;
|
||||
}
|
||||
|
||||
static Addr from_in6_addr(const struct in6_addr *in6)
|
||||
{
|
||||
Addr ret;
|
||||
network_to_host_order(&ret.u, (const union ipv6addr *)in6->s6_addr);
|
||||
ret.scope_id_ = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct in6_addr to_in6_addr() const
|
||||
{
|
||||
struct in6_addr ret;
|
||||
host_to_network_order((union ipv6addr *)&ret, &u);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Addr from_sockaddr(const struct sockaddr_in6 *sa)
|
||||
{
|
||||
Addr ret;
|
||||
network_to_host_order(&ret.u, (const union ipv6addr *)sa->sin6_addr.s6_addr);
|
||||
ret.scope_id_ = sa->sin6_scope_id;
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct sockaddr_in6 to_sockaddr(const unsigned short port=0) const
|
||||
{
|
||||
struct sockaddr_in6 ret;
|
||||
std::memset(&ret, 0, sizeof(ret));
|
||||
ret.sin6_family = AF_INET6;
|
||||
ret.sin6_port = htons(port);
|
||||
host_to_network_order((union ipv6addr *)&ret.sin6_addr.s6_addr, &u);
|
||||
ret.sin6_scope_id = scope_id_;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Addr from_string(const std::string& ipstr, const char *title = nullptr)
|
||||
{
|
||||
openvpn_io::error_code ec;
|
||||
openvpn_io::ip::address_v6 a = openvpn_io::ip::make_address_v6(ipstr, ec);
|
||||
if (ec)
|
||||
throw ipv6_exception(IP::internal::format_error(ipstr, title, "v6", ec));
|
||||
return from_asio(a);
|
||||
}
|
||||
|
||||
std::string to_string() const
|
||||
{
|
||||
const openvpn_io::ip::address_v6 a = to_asio();
|
||||
std::string ret = a.to_string();
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Addr from_hex(const std::string& s)
|
||||
{
|
||||
Addr ret;
|
||||
ret.scope_id_ = 0;
|
||||
ret.zero();
|
||||
size_t len = s.length();
|
||||
size_t base = 0;
|
||||
if (len > 0 && s[len-1] == 'L')
|
||||
len -= 1;
|
||||
if (len >= 2 && s[0] == '0' && s[1] == 'x')
|
||||
{
|
||||
base = 2;
|
||||
len -= 2;
|
||||
}
|
||||
if (len < 1 || len > 32)
|
||||
throw ipv6_exception("parse hex error");
|
||||
size_t di = (len-1)>>1;
|
||||
for (int i = (len & 1) ? -1 : 0; i < int(len); i += 2)
|
||||
{
|
||||
const size_t idx = base + i;
|
||||
const int bh = (i >= 0) ? parse_hex_char(s[idx]) : 0;
|
||||
const int bl = parse_hex_char(s[idx+1]);
|
||||
if (bh == -1 || bl == -1)
|
||||
throw ipv6_exception("parse hex error");
|
||||
ret.u.bytes[Endian::e16(di--)] = (bh<<4) + bl;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string to_hex() const
|
||||
{
|
||||
std::string ret;
|
||||
ret.reserve(32);
|
||||
bool firstnonzero = false;
|
||||
for (size_t i = 0; i < 16; ++i)
|
||||
{
|
||||
const unsigned char b = u.bytes[Endian::e16rev(i)];
|
||||
if (b || firstnonzero || i == 15)
|
||||
{
|
||||
const char bh = b >> 4;
|
||||
if (bh || firstnonzero)
|
||||
ret += render_hex_char(bh);
|
||||
ret += render_hex_char(b & 0x0F);
|
||||
firstnonzero = true;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Addr from_ulong(unsigned long ul)
|
||||
{
|
||||
Addr ret;
|
||||
ret.scope_id_ = 0;
|
||||
ret.u.u64[Endian::e2(0)] = std::uint64_t(ul);
|
||||
ret.u.u64[Endian::e2(1)] = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// return *this as a unsigned long
|
||||
unsigned long to_ulong() const
|
||||
{
|
||||
const unsigned long ret = (unsigned long)u.u64[Endian::e2(0)];
|
||||
const std::uint64_t cmp = std::uint64_t(ret);
|
||||
if (u.u64[Endian::e2(1)] || cmp != u.u64[Endian::e2(0)])
|
||||
throw ipv6_exception("overflow in conversion from IPv6.Addr to unsigned long");
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Addr from_long(long ul)
|
||||
{
|
||||
bool neg = false;
|
||||
Addr ret;
|
||||
ret.scope_id_ = 0;
|
||||
if (ul < 0)
|
||||
{
|
||||
ul = -(ul + 1);
|
||||
neg = true;
|
||||
}
|
||||
ret.u.u64[Endian::e2(0)] = std::uint64_t(ul);
|
||||
ret.u.u64[Endian::e2(1)] = 0;
|
||||
if (neg)
|
||||
ret.negate();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// return *this as a long
|
||||
long to_long() const
|
||||
{
|
||||
bool neg = false;
|
||||
Addr a = *this;
|
||||
if (a.u.u64[Endian::e2(1)])
|
||||
{
|
||||
a.negate();
|
||||
neg = true;
|
||||
}
|
||||
const long ret = (long)a.u.u64[Endian::e2(0)];
|
||||
const std::uint64_t cmp = std::uint64_t(ret);
|
||||
if (a.u.u64[Endian::e2(1)] || cmp != a.u.u64[Endian::e2(0)])
|
||||
throw ipv6_exception("overflow in conversion from IPv6.Addr to long");
|
||||
return neg ? -(ret + 1) : ret;
|
||||
}
|
||||
|
||||
std::string arpa() const
|
||||
{
|
||||
throw ipv6_exception("arpa() not implemented");
|
||||
}
|
||||
|
||||
static Addr from_asio(const openvpn_io::ip::address_v6& asio_addr)
|
||||
{
|
||||
Addr ret;
|
||||
union ipv6addr addr;
|
||||
addr.asio_bytes = asio_addr.to_bytes();
|
||||
network_to_host_order(&ret.u, &addr);
|
||||
ret.scope_id_ = (unsigned int)asio_addr.scope_id();
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Addr from_byte_string(const unsigned char *bytestr)
|
||||
{
|
||||
Addr ret;
|
||||
network_to_host_order(&ret.u, (const union ipv6addr *)bytestr);
|
||||
ret.scope_id_ = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void to_byte_string(unsigned char *bytestr) const
|
||||
{
|
||||
host_to_network_order((union ipv6addr *)bytestr, &u);
|
||||
}
|
||||
|
||||
static void v4_to_byte_string(unsigned char *bytestr,
|
||||
const std::uint32_t v4addr)
|
||||
{
|
||||
union ipv6addr *a = (union ipv6addr *)bytestr;
|
||||
a->u32[0] = a->u32[1] = a->u32[2] = 0;
|
||||
a->u32[3] = v4addr;
|
||||
}
|
||||
|
||||
static bool byte_string_is_v4(const unsigned char *bytestr)
|
||||
{
|
||||
const union ipv6addr *a = (const union ipv6addr *)bytestr;
|
||||
return a->u32[0] == 0 && a->u32[1] == 0 && a->u32[2] == 0;
|
||||
}
|
||||
|
||||
static std::uint32_t v4_from_byte_string(const unsigned char *bytestr)
|
||||
{
|
||||
const union ipv6addr *a = (const union ipv6addr *)bytestr;
|
||||
return a->u32[3];
|
||||
}
|
||||
|
||||
openvpn_io::ip::address_v6 to_asio() const
|
||||
{
|
||||
union ipv6addr addr;
|
||||
host_to_network_order(&addr, &u);
|
||||
return openvpn_io::ip::address_v6(addr.asio_bytes, scope_id_);
|
||||
}
|
||||
|
||||
static Addr from_zero()
|
||||
{
|
||||
Addr ret;
|
||||
ret.scope_id_ = 0;
|
||||
ret.zero();
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Addr from_one()
|
||||
{
|
||||
Addr ret;
|
||||
ret.scope_id_ = 0;
|
||||
ret.one();
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Addr from_zero_complement()
|
||||
{
|
||||
Addr ret;
|
||||
ret.scope_id_ = 0;
|
||||
ret.zero_complement();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// build a netmask using given prefix_len
|
||||
static Addr netmask_from_prefix_len(const unsigned int prefix_len)
|
||||
{
|
||||
Addr ret;
|
||||
ret.scope_id_ = 0;
|
||||
ret.prefix_len_to_netmask(prefix_len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// build a netmask using given extent
|
||||
Addr netmask_from_extent() const
|
||||
{
|
||||
const Addr lb = *this - 1;
|
||||
for (size_t i = 4; i --> 0 ;)
|
||||
{
|
||||
const std::uint32_t v = lb.u.u32[Endian::e4(i)];
|
||||
if (v)
|
||||
return netmask_from_prefix_len(SIZE - (((unsigned int)i<<5) + find_last_set(v)));
|
||||
}
|
||||
return from_zero_complement();
|
||||
}
|
||||
|
||||
Addr operator&(const Addr& other) const {
|
||||
Addr ret;
|
||||
ret.scope_id_ = scope_id_;
|
||||
ret.u.u64[0] = u.u64[0] & other.u.u64[0];
|
||||
ret.u.u64[1] = u.u64[1] & other.u.u64[1];
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator|(const Addr& other) const {
|
||||
Addr ret;
|
||||
ret.scope_id_ = scope_id_;
|
||||
ret.u.u64[0] = u.u64[0] | other.u.u64[0];
|
||||
ret.u.u64[1] = u.u64[1] | other.u.u64[1];
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator+(const long delta) const {
|
||||
Addr ret = *this;
|
||||
ret.u.u64[Endian::e2(0)] += delta;
|
||||
ret.u.u64[Endian::e2(1)] += (delta >= 0)
|
||||
? (ret.u.u64[Endian::e2(0)] < u.u64[Endian::e2(0)])
|
||||
: -(ret.u.u64[Endian::e2(0)] > u.u64[Endian::e2(0)]);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator+(const Addr& other) const {
|
||||
Addr ret = *this;
|
||||
add(ret.u, other.u);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator-(const long delta) const {
|
||||
return operator+(-delta);
|
||||
}
|
||||
|
||||
Addr operator-(const Addr& other) const {
|
||||
Addr ret = *this;
|
||||
sub(ret.u, other.u);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator*(const Addr& d) const {
|
||||
Addr m = d;
|
||||
Addr ret = from_zero();
|
||||
for (unsigned int i = 0; i < SIZE; ++i)
|
||||
{
|
||||
if (bit(i))
|
||||
ret += m;
|
||||
m <<= 1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator/(const Addr& d) const {
|
||||
Addr q, r;
|
||||
div(*this, d, q, r);
|
||||
return q;
|
||||
}
|
||||
|
||||
Addr operator%(const Addr& d) const {
|
||||
Addr q, r;
|
||||
div(*this, d, q, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
Addr operator<<(const unsigned int shift) const {
|
||||
Addr ret = *this;
|
||||
shiftl128(ret.u.u64[Endian::e2(0)],
|
||||
ret.u.u64[Endian::e2(1)],
|
||||
shift);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator>>(const unsigned int shift) const {
|
||||
Addr ret = *this;
|
||||
shiftr128(ret.u.u64[Endian::e2(0)],
|
||||
ret.u.u64[Endian::e2(1)],
|
||||
shift);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Addr operator~() const {
|
||||
Addr ret;
|
||||
ret.scope_id_ = scope_id_;
|
||||
ret.u.u64[0] = ~u.u64[0];
|
||||
ret.u.u64[1] = ~u.u64[1];
|
||||
return ret;
|
||||
}
|
||||
|
||||
// return the network that contains the current address
|
||||
Addr network_addr(const unsigned int prefix_len) const
|
||||
{
|
||||
return *this & netmask_from_prefix_len(prefix_len);
|
||||
}
|
||||
|
||||
bool operator==(const Addr& other) const
|
||||
{
|
||||
return u.u64[0] == other.u.u64[0] && u.u64[1] == other.u.u64[1] && scope_id_ == other.scope_id_;
|
||||
}
|
||||
|
||||
bool operator!=(const Addr& other) const
|
||||
{
|
||||
return !operator==(other);
|
||||
}
|
||||
|
||||
#define OPENVPN_IPV6_OPERATOR_REL(OP) \
|
||||
bool operator OP(const Addr& other) const \
|
||||
{ \
|
||||
if (u.u64[Endian::e2(1)] == other.u.u64[Endian::e2(1)]) \
|
||||
{ \
|
||||
if (u.u64[Endian::e2(0)] != other.u.u64[Endian::e2(0)]) \
|
||||
return u.u64[Endian::e2(0)] OP other.u.u64[Endian::e2(0)]; \
|
||||
else \
|
||||
return scope_id_ OP other.scope_id_; \
|
||||
} \
|
||||
else \
|
||||
return u.u64[Endian::e2(1)] OP other.u.u64[Endian::e2(1)]; \
|
||||
}
|
||||
|
||||
OPENVPN_IPV6_OPERATOR_REL(<)
|
||||
OPENVPN_IPV6_OPERATOR_REL(>)
|
||||
OPENVPN_IPV6_OPERATOR_REL(<=)
|
||||
OPENVPN_IPV6_OPERATOR_REL(>=)
|
||||
|
||||
#undef OPENVPN_IPV6_OPERATOR_REL
|
||||
|
||||
bool unspecified() const
|
||||
{
|
||||
return all_zeros();
|
||||
}
|
||||
|
||||
bool specified() const
|
||||
{
|
||||
return !unspecified();
|
||||
}
|
||||
|
||||
bool all_zeros() const
|
||||
{
|
||||
return u.u64[0] == 0 && u.u64[1] == 0;
|
||||
}
|
||||
|
||||
bool all_ones() const
|
||||
{
|
||||
return u.u64[0] == ~std::uint64_t(0) && u.u64[1] == ~std::uint64_t(0);
|
||||
}
|
||||
|
||||
bool is_loopback() const // ::1
|
||||
{
|
||||
return u.u64[Endian::e2(1)] == 0 && u.u64[Endian::e2(0)] == 1;
|
||||
}
|
||||
|
||||
bool bit(unsigned int pos) const
|
||||
{
|
||||
if (pos < 64)
|
||||
return (u.u64[Endian::e2(0)] & (std::uint64_t(1)<<pos)) != 0;
|
||||
else
|
||||
return (u.u64[Endian::e2(1)] & (std::uint64_t(1)<<(pos-64))) != 0;
|
||||
}
|
||||
|
||||
// number of network bits in netmask,
|
||||
// throws exception if addr is not a netmask
|
||||
unsigned int prefix_len() const
|
||||
{
|
||||
int idx = -1;
|
||||
|
||||
if (u.u32[Endian::e4(3)] != ~std::uint32_t(0))
|
||||
{
|
||||
if (!u.u32[Endian::e4(0)] && !u.u32[Endian::e4(1)] && !u.u32[Endian::e4(2)])
|
||||
idx = 0;
|
||||
}
|
||||
else if (u.u32[Endian::e4(2)] != ~std::uint32_t(0))
|
||||
{
|
||||
if (!u.u32[Endian::e4(0)] && !u.u32[Endian::e4(1)])
|
||||
idx = 1;
|
||||
}
|
||||
else if (u.u32[Endian::e4(1)] != ~std::uint32_t(0))
|
||||
{
|
||||
if (!u.u32[Endian::e4(0)])
|
||||
idx = 2;
|
||||
}
|
||||
else
|
||||
idx = 3;
|
||||
|
||||
if (idx >= 0)
|
||||
{
|
||||
const int ret = IPv4::Addr::prefix_len_32(u.u32[Endian::e4rev(idx)]);
|
||||
if (ret >= 0)
|
||||
return ret + (idx<<5);
|
||||
}
|
||||
throw ipv6_exception("malformed netmask");
|
||||
}
|
||||
|
||||
// number of host bits in netmask
|
||||
unsigned int host_len() const
|
||||
{
|
||||
return SIZE - prefix_len();
|
||||
}
|
||||
|
||||
// return the number of host addresses contained within netmask
|
||||
Addr extent_from_netmask() const
|
||||
{
|
||||
const unsigned int hl = host_len();
|
||||
if (hl < SIZE)
|
||||
{
|
||||
Addr a;
|
||||
a.scope_id_ = 0;
|
||||
a.one();
|
||||
return a << hl;
|
||||
}
|
||||
else if (hl == SIZE)
|
||||
return from_zero();
|
||||
else
|
||||
throw ipv6_exception("extent overflow");
|
||||
}
|
||||
|
||||
// address size in bits
|
||||
static unsigned int size()
|
||||
{
|
||||
return SIZE;
|
||||
}
|
||||
|
||||
template <typename HASH>
|
||||
void hash(HASH& h) const
|
||||
{
|
||||
h(u.bytes, sizeof(u.bytes));
|
||||
}
|
||||
|
||||
#ifdef HAVE_CITYHASH
|
||||
std::size_t hashval() const
|
||||
{
|
||||
HashSizeT h;
|
||||
hash(h);
|
||||
return h.value();
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef OPENVPN_IP_IMMUTABLE
|
||||
private:
|
||||
#endif
|
||||
|
||||
void negate()
|
||||
{
|
||||
u.u64[0] = ~u.u64[0];
|
||||
u.u64[1] = ~u.u64[1];
|
||||
}
|
||||
|
||||
void zero()
|
||||
{
|
||||
u.u64[0] = 0;
|
||||
u.u64[1] = 0;
|
||||
}
|
||||
|
||||
void zero_complement()
|
||||
{
|
||||
u.u64[0] = ~std::uint64_t(0);
|
||||
u.u64[1] = ~std::uint64_t(0);
|
||||
}
|
||||
|
||||
void one()
|
||||
{
|
||||
u.u64[0] = 1;
|
||||
u.u64[1] = 0;
|
||||
}
|
||||
|
||||
Addr& operator++()
|
||||
{
|
||||
if (++u.u64[Endian::e2(0)] == 0)
|
||||
++u.u64[Endian::e2(1)];
|
||||
return *this;
|
||||
}
|
||||
|
||||
Addr& operator+=(const long delta)
|
||||
{
|
||||
*this = *this + delta;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Addr& operator-=(const long delta)
|
||||
{
|
||||
return operator+=(-delta);
|
||||
}
|
||||
|
||||
Addr& operator+=(const Addr& other) {
|
||||
add(u, other.u);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Addr& operator-=(const Addr& other) {
|
||||
sub(u, other.u);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Addr& operator<<=(const unsigned int shift) {
|
||||
shiftl128(u.u64[Endian::e2(0)],
|
||||
u.u64[Endian::e2(1)],
|
||||
shift);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Addr& operator>>=(const unsigned int shift) {
|
||||
shiftr128(u.u64[Endian::e2(0)],
|
||||
u.u64[Endian::e2(1)],
|
||||
shift);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void set_clear_bit(unsigned int pos, bool value)
|
||||
{
|
||||
if (pos < 64)
|
||||
{
|
||||
if (value)
|
||||
u.u64[Endian::e2(0)] |= (std::uint64_t(1)<<pos);
|
||||
else
|
||||
u.u64[Endian::e2(0)] &= ~(std::uint64_t(1)<<pos);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (value)
|
||||
u.u64[Endian::e2(1)] |= (std::uint64_t(1)<<(pos-64));
|
||||
else
|
||||
u.u64[Endian::e2(1)] &= ~(std::uint64_t(1)<<(pos-64));
|
||||
}
|
||||
}
|
||||
|
||||
void set_bit(unsigned int pos, bool value)
|
||||
{
|
||||
if (value)
|
||||
{
|
||||
if (pos < 64)
|
||||
u.u64[Endian::e2(0)] |= (std::uint64_t(1)<<pos);
|
||||
else
|
||||
u.u64[Endian::e2(1)] |= (std::uint64_t(1)<<(pos-64));
|
||||
}
|
||||
}
|
||||
|
||||
static void div(const Addr& n, const Addr& d, Addr& q, Addr& r)
|
||||
{
|
||||
if (d.all_zeros())
|
||||
throw ipv6_exception("division by 0");
|
||||
q = from_zero(); // quotient
|
||||
r = n; // remainder (init to numerator)
|
||||
Addr ml = from_zero(); // mask low
|
||||
Addr mh = d; // mask high (init to denominator)
|
||||
for (unsigned int i = 0; i < SIZE; ++i)
|
||||
{
|
||||
ml >>= 1;
|
||||
ml.set_bit(SIZE-1, mh.bit(0));
|
||||
mh >>= 1;
|
||||
if (mh.all_zeros() && r >= ml)
|
||||
{
|
||||
r -= ml;
|
||||
q.set_bit((SIZE-1)-i, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int scope_id() const
|
||||
{
|
||||
return scope_id_;
|
||||
}
|
||||
|
||||
private:
|
||||
union ipv6addr {
|
||||
std::uint64_t u64[2];
|
||||
std::uint32_t u32[4]; // generally stored in host byte order
|
||||
unsigned char bytes[16];
|
||||
openvpn_io::ip::address_v6::bytes_type asio_bytes;
|
||||
};
|
||||
|
||||
void prefix_len_to_netmask_unchecked(const unsigned int prefix_len)
|
||||
{
|
||||
if (prefix_len > 0)
|
||||
{
|
||||
const unsigned int pl = prefix_len - 1;
|
||||
const std::uint32_t mask = ~((1 << (31 - (pl & 31))) - 1);
|
||||
switch (pl >> 5)
|
||||
{
|
||||
case 0:
|
||||
u.u32[Endian::e4(0)] = 0;
|
||||
u.u32[Endian::e4(1)] = 0;
|
||||
u.u32[Endian::e4(2)] = 0;
|
||||
u.u32[Endian::e4(3)] = mask;
|
||||
break;
|
||||
case 1:
|
||||
u.u32[Endian::e4(0)] = 0;
|
||||
u.u32[Endian::e4(1)] = 0;
|
||||
u.u32[Endian::e4(2)] = mask;
|
||||
u.u32[Endian::e4(3)] = ~0;
|
||||
break;
|
||||
case 2:
|
||||
u.u32[Endian::e4(0)] = 0;
|
||||
u.u32[Endian::e4(1)] = mask;
|
||||
u.u32[Endian::e4(2)] = ~0;
|
||||
u.u32[Endian::e4(3)] = ~0;
|
||||
break;
|
||||
case 3:
|
||||
u.u32[Endian::e4(0)] = mask;
|
||||
u.u32[Endian::e4(1)] = ~0;
|
||||
u.u32[Endian::e4(2)] = ~0;
|
||||
u.u32[Endian::e4(3)] = ~0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
zero();
|
||||
}
|
||||
|
||||
void prefix_len_to_netmask(const unsigned int prefix_len)
|
||||
{
|
||||
if (prefix_len <= SIZE)
|
||||
return prefix_len_to_netmask_unchecked(prefix_len);
|
||||
else
|
||||
throw ipv6_exception("bad prefix len");
|
||||
}
|
||||
|
||||
static void host_to_network_order(union ipv6addr *dest, const union ipv6addr *src)
|
||||
{
|
||||
dest->u32[0] = htonl(src->u32[Endian::e4rev(0)]);
|
||||
dest->u32[1] = htonl(src->u32[Endian::e4rev(1)]);
|
||||
dest->u32[2] = htonl(src->u32[Endian::e4rev(2)]);
|
||||
dest->u32[3] = htonl(src->u32[Endian::e4rev(3)]);
|
||||
}
|
||||
|
||||
static void network_to_host_order(union ipv6addr *dest, const union ipv6addr *src)
|
||||
{
|
||||
dest->u32[0] = ntohl(src->u32[Endian::e4rev(0)]);
|
||||
dest->u32[1] = ntohl(src->u32[Endian::e4rev(1)]);
|
||||
dest->u32[2] = ntohl(src->u32[Endian::e4rev(2)]);
|
||||
dest->u32[3] = ntohl(src->u32[Endian::e4rev(3)]);
|
||||
}
|
||||
|
||||
static void shiftl128(std::uint64_t& low,
|
||||
std::uint64_t& high,
|
||||
unsigned int shift)
|
||||
{
|
||||
if (shift == 1)
|
||||
{
|
||||
high <<= 1;
|
||||
if (low & (std::uint64_t(1) << 63))
|
||||
high |= 1;
|
||||
low <<= 1;
|
||||
}
|
||||
else if (shift == 0)
|
||||
;
|
||||
else if (shift <= 128)
|
||||
{
|
||||
if (shift >= 64)
|
||||
{
|
||||
high = low;
|
||||
low = 0;
|
||||
shift -= 64;
|
||||
}
|
||||
if (shift < 64)
|
||||
{
|
||||
high = (high << shift) | (low >> (64-shift));
|
||||
low <<= shift;
|
||||
}
|
||||
else // shift == 64
|
||||
high = 0;
|
||||
}
|
||||
else
|
||||
throw ipv6_exception("l-shift too large");
|
||||
}
|
||||
|
||||
static void shiftr128(std::uint64_t& low,
|
||||
std::uint64_t& high,
|
||||
unsigned int shift)
|
||||
{
|
||||
if (shift == 1)
|
||||
{
|
||||
low >>= 1;
|
||||
if (high & 1)
|
||||
low |= (std::uint64_t(1) << 63);
|
||||
high >>= 1;
|
||||
}
|
||||
else if (shift == 0)
|
||||
;
|
||||
else if (shift <= 128)
|
||||
{
|
||||
if (shift >= 64)
|
||||
{
|
||||
low = high;
|
||||
high = 0;
|
||||
shift -= 64;
|
||||
}
|
||||
if (shift < 64)
|
||||
{
|
||||
low = (low >> shift) | (high << (64-shift));
|
||||
high >>= shift;
|
||||
}
|
||||
else // shift == 64
|
||||
low = 0;
|
||||
}
|
||||
else
|
||||
throw ipv6_exception("r-shift too large");
|
||||
}
|
||||
|
||||
static void add(ipv6addr& dest, const ipv6addr& src) {
|
||||
const std::uint64_t dorigl = dest.u64[Endian::e2(0)];
|
||||
dest.u64[Endian::e2(0)] += src.u64[Endian::e2(0)];
|
||||
dest.u64[Endian::e2(1)] += src.u64[Endian::e2(1)];
|
||||
// check for overflow of low 64 bits, add carry to high
|
||||
if (dest.u64[Endian::e2(0)] < dorigl)
|
||||
++dest.u64[Endian::e2(1)];
|
||||
}
|
||||
|
||||
static void sub(ipv6addr& dest, const ipv6addr& src) {
|
||||
const std::uint64_t dorigl = dest.u64[Endian::e2(0)];
|
||||
dest.u64[Endian::e2(0)] -= src.u64[Endian::e2(0)];
|
||||
dest.u64[Endian::e2(1)] -= src.u64[Endian::e2(1)]
|
||||
+ (dorigl < dest.u64[Endian::e2(0)]);
|
||||
}
|
||||
|
||||
union ipv6addr u;
|
||||
unsigned int scope_id_;
|
||||
};
|
||||
|
||||
OPENVPN_OSTREAM(Addr, to_string)
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HAVE_CITYHASH
|
||||
OPENVPN_HASH_METHOD(openvpn::IPv6::Addr, hashval);
|
||||
#endif
|
||||
|
||||
#endif // OPENVPN_ADDR_IPV6_H
|
||||
@@ -0,0 +1,67 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2017 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef OPENVPN_ADDR_MACADDR_H
|
||||
#define OPENVPN_ADDR_MACADDR_H
|
||||
|
||||
#include <ostream>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
#include <openvpn/common/exception.hpp>
|
||||
#include <openvpn/common/ostream.hpp>
|
||||
#include <openvpn/common/hexstr.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
|
||||
// Fundamental class for representing an ethernet MAC address.
|
||||
|
||||
class MACAddr {
|
||||
public:
|
||||
MACAddr()
|
||||
{
|
||||
std::memset(addr_, 0, sizeof(addr_));
|
||||
}
|
||||
|
||||
MACAddr(const unsigned char *addr)
|
||||
{
|
||||
reset(addr);
|
||||
}
|
||||
|
||||
void reset(const unsigned char *addr)
|
||||
{
|
||||
std::memcpy(addr_, addr, sizeof(addr_));
|
||||
}
|
||||
|
||||
std::string to_string() const
|
||||
{
|
||||
return render_hex_sep(addr_, sizeof(addr_), ':');
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned char addr_[6];
|
||||
};
|
||||
|
||||
OPENVPN_OSTREAM(MACAddr, to_string)
|
||||
|
||||
} // namespace openvpn
|
||||
|
||||
#endif // OPENVPN_ADDR_MACADDR_H
|
||||
@@ -0,0 +1,164 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2017 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef OPENVPN_ADDR_POOL_H
|
||||
#define OPENVPN_ADDR_POOL_H
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <deque>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <openvpn/common/size.hpp>
|
||||
#include <openvpn/common/exception.hpp>
|
||||
|
||||
#include <openvpn/addr/ip.hpp>
|
||||
#include <openvpn/addr/range.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
namespace IP {
|
||||
|
||||
// Maintain a pool of IP addresses.
|
||||
// A should be IP::Addr, IPv4::Addr, or IPv6::Addr.
|
||||
template <typename ADDR>
|
||||
class PoolType
|
||||
{
|
||||
public:
|
||||
PoolType() {}
|
||||
|
||||
// Add range of addresses to pool (pool will own the addresses).
|
||||
void add_range(const RangeType<ADDR>& range)
|
||||
{
|
||||
auto iter = range.iterator();
|
||||
while (iter.more())
|
||||
{
|
||||
const ADDR& a = iter.addr();
|
||||
add_addr(a);
|
||||
iter.next();
|
||||
}
|
||||
}
|
||||
|
||||
// Add single address to pool (pool will own the address).
|
||||
void add_addr(const ADDR& addr)
|
||||
{
|
||||
auto e = map.find(addr);
|
||||
if (e == map.end())
|
||||
{
|
||||
freelist.push_back(addr);
|
||||
map[addr] = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Return number of pool addresses currently in use.
|
||||
size_t n_in_use() const
|
||||
{
|
||||
return map.size() - freelist.size();
|
||||
}
|
||||
|
||||
// Return number of pool addresses currently in use.
|
||||
size_t n_free() const
|
||||
{
|
||||
return freelist.size();
|
||||
}
|
||||
|
||||
// Acquire an address from pool. Returns true if successful,
|
||||
// with address placed in dest, or false if pool depleted.
|
||||
bool acquire_addr(ADDR& dest)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
freelist_fill();
|
||||
if (freelist.empty())
|
||||
return false;
|
||||
const ADDR& a = freelist.front();
|
||||
auto e = map.find(a);
|
||||
if (e == map.end()) // any address in freelist must exist in map
|
||||
throw Exception("PoolType: address in freelist doesn't exist in map");
|
||||
if (!e->second)
|
||||
{
|
||||
e->second = true;
|
||||
dest = a;
|
||||
freelist.pop_front();
|
||||
return true;
|
||||
}
|
||||
freelist.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
// Acquire a specific address from pool, returning true if
|
||||
// successful, or false if the address is not available.
|
||||
bool acquire_specific_addr(const ADDR& addr)
|
||||
{
|
||||
auto e = map.find(addr);
|
||||
if (e != map.end() && !e->second)
|
||||
{
|
||||
e->second = true;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
// Return a previously acquired address to the pool. Does nothing if
|
||||
// (a) the address is owned by the pool and marked as free, or
|
||||
// (b) the address is not owned by the pool.
|
||||
void release_addr(const ADDR& addr)
|
||||
{
|
||||
auto e = map.find(addr);
|
||||
if (e != map.end() && e->second)
|
||||
{
|
||||
freelist.push_back(addr);
|
||||
e->second = false;
|
||||
}
|
||||
}
|
||||
|
||||
// DEBUGGING -- get the map load factor
|
||||
float load_factor() const { return map.load_factor(); }
|
||||
|
||||
// Override to refill freelist on demand
|
||||
virtual void freelist_fill()
|
||||
{
|
||||
}
|
||||
|
||||
std::string to_string() const
|
||||
{
|
||||
std::string ret;
|
||||
for (const auto& e : map)
|
||||
{
|
||||
if (e.second)
|
||||
{
|
||||
ret += e.first.to_string();
|
||||
ret += '\n';
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private:
|
||||
std::deque<ADDR> freelist;
|
||||
std::unordered_map<ADDR, bool> map;
|
||||
};
|
||||
|
||||
typedef PoolType<IP::Addr> Pool;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,36 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2017 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace openvpn {
|
||||
|
||||
// return ip_addr in brackets if it is IPv6
|
||||
std::string quote_ip(const std::string& ip_addr)
|
||||
{
|
||||
if (ip_addr.find(':') != std::string::npos)
|
||||
return '[' + ip_addr + ']';
|
||||
else
|
||||
return ip_addr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2017 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <openvpn/addr/route.hpp>
|
||||
#include <openvpn/common/exception.hpp>
|
||||
#include <openvpn/random/randapi.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
namespace IP {
|
||||
|
||||
inline IPv4::Addr random_addr_v4(RandomAPI& prng)
|
||||
{
|
||||
return IPv4::Addr::from_uint32(prng.rand_get<std::uint32_t>());
|
||||
}
|
||||
|
||||
inline IPv6::Addr random_addr_v6(RandomAPI& prng)
|
||||
{
|
||||
unsigned char bytes[16];
|
||||
prng.rand_fill(bytes);
|
||||
return IPv6::Addr::from_byte_string(bytes);
|
||||
}
|
||||
|
||||
inline Addr random_addr(const Addr::Version v, RandomAPI& prng)
|
||||
{
|
||||
switch (v)
|
||||
{
|
||||
case Addr::V4:
|
||||
return Addr::from_ipv4(random_addr_v4(prng));
|
||||
case Addr::V6:
|
||||
return Addr::from_ipv6(random_addr_v6(prng));
|
||||
default:
|
||||
throw ip_exception("address unspecified");
|
||||
}
|
||||
}
|
||||
|
||||
// bit positions between templ.prefix_len and prefix_len are randomized
|
||||
inline Route random_subnet(const Route& templ,
|
||||
const unsigned int prefix_len,
|
||||
RandomAPI& prng)
|
||||
{
|
||||
if (!templ.is_canonical())
|
||||
throw Exception("IP::random_subnet: template route not canonical: " + templ.to_string());
|
||||
return Route(((random_addr(templ.addr.version(), prng) & ~templ.netmask()) | templ.addr)
|
||||
& Addr::netmask_from_prefix_len(templ.addr.version(), prefix_len),
|
||||
prefix_len);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2017 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef OPENVPN_ADDR_RANGE_H
|
||||
#define OPENVPN_ADDR_RANGE_H
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
#include <openvpn/common/size.hpp>
|
||||
#include <openvpn/common/exception.hpp>
|
||||
|
||||
#include <openvpn/addr/ip.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
namespace IP {
|
||||
|
||||
// Denote a range of IP addresses with a start and extent,
|
||||
// where A represents an address class.
|
||||
// A should be a network address class such as IP::Addr, IPv4::Addr, or IPv6::Addr.
|
||||
|
||||
template <typename ADDR>
|
||||
class RangeType
|
||||
{
|
||||
public:
|
||||
class Iterator
|
||||
{
|
||||
friend class RangeType;
|
||||
public:
|
||||
bool more() const { return remaining_ > 0; }
|
||||
|
||||
const ADDR& addr() const { return addr_; }
|
||||
|
||||
void next()
|
||||
{
|
||||
if (more())
|
||||
{
|
||||
++addr_;
|
||||
--remaining_;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
Iterator(const RangeType& range)
|
||||
: addr_(range.start_), remaining_(range.extent_) {}
|
||||
|
||||
ADDR addr_;
|
||||
size_t remaining_;
|
||||
};
|
||||
|
||||
RangeType() : extent_(0) {}
|
||||
|
||||
RangeType(const ADDR& start, const size_t extent)
|
||||
: start_(start), extent_(extent) {}
|
||||
|
||||
Iterator iterator() const { return Iterator(*this); }
|
||||
|
||||
const bool defined() const { return extent_ > 0; }
|
||||
const ADDR& start() const { return start_; }
|
||||
size_t extent() const { return extent_; }
|
||||
|
||||
RangeType pull_front(size_t extent)
|
||||
{
|
||||
if (extent > extent_)
|
||||
extent = extent_;
|
||||
RangeType ret(start_, extent);
|
||||
start_ += extent;
|
||||
extent_ -= extent;
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string to_string() const
|
||||
{
|
||||
std::ostringstream os;
|
||||
os << start_.to_string() << '[' << extent_ << ']';
|
||||
return os.str();
|
||||
}
|
||||
|
||||
private:
|
||||
ADDR start_;
|
||||
size_t extent_;
|
||||
};
|
||||
|
||||
template <typename ADDR>
|
||||
class RangePartitionType
|
||||
{
|
||||
public:
|
||||
RangePartitionType(const RangeType<ADDR>& src_range, const size_t n_partitions)
|
||||
: range(src_range),
|
||||
remaining(n_partitions)
|
||||
{
|
||||
}
|
||||
|
||||
bool next(RangeType<ADDR>& r)
|
||||
{
|
||||
if (remaining)
|
||||
{
|
||||
if (remaining > 1)
|
||||
r = range.pull_front(range.extent() / remaining);
|
||||
else
|
||||
r = range;
|
||||
--remaining;
|
||||
return r.defined();
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
RangeType<ADDR> range;
|
||||
size_t remaining;
|
||||
};
|
||||
|
||||
typedef RangeType<IP::Addr> Range;
|
||||
typedef RangePartitionType<IP::Addr> RangePartition;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,59 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2017 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// Regular expressions for IPv4/v6
|
||||
// Source: http://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses
|
||||
|
||||
#ifndef OPENVPN_ADDR_REGEX_H
|
||||
#define OPENVPN_ADDR_REGEX_H
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace openvpn {
|
||||
namespace IP {
|
||||
inline std::string v4_regex()
|
||||
{
|
||||
const std::string ipv4seg = "(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])";
|
||||
return "(?:" + ipv4seg + "\\.){3,3}" + ipv4seg;
|
||||
}
|
||||
|
||||
inline std::string v6_regex()
|
||||
{
|
||||
const std::string ipv6seg = "[0-9a-fA-F]{1,4}";
|
||||
return "(?:"
|
||||
"(?:" + ipv6seg + ":){7,7}" + ipv6seg + "|" // 1:2:3:4:5:6:7:8
|
||||
"(?:" + ipv6seg + ":){1,7}:|" // 1:: 1:2:3:4:5:6:7::
|
||||
"(?:" + ipv6seg + ":){1,6}:" + ipv6seg + "|" // 1::8 1:2:3:4:5:6::8 1:2:3:4:5:6::8
|
||||
"(?:" + ipv6seg + ":){1,5}(?::" + ipv6seg + "){1,2}|" // 1::7:8 1:2:3:4:5::7:8 1:2:3:4:5::8
|
||||
"(?:" + ipv6seg + ":){1,4}(?::" + ipv6seg + "){1,3}|" // 1::6:7:8 1:2:3:4::6:7:8 1:2:3:4::8
|
||||
"(?:" + ipv6seg + ":){1,3}(?::" + ipv6seg + "){1,4}|" // 1::5:6:7:8 1:2:3::5:6:7:8 1:2:3::8
|
||||
"(?:" + ipv6seg + ":){1,2}(?::" + ipv6seg + "){1,5}|" + // 1::4:5:6:7:8 1:2::4:5:6:7:8 1:2::8
|
||||
ipv6seg + ":(?:(?::" + ipv6seg + "){1,6})|" // 1::3:4:5:6:7:8 1::3:4:5:6:7:8 1::8
|
||||
":(?:(?::" + ipv6seg + "){1,7}|:)|" // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::
|
||||
"fe80:(?::" + ipv6seg + "){0,4}%[0-9a-zA-Z]{1,}|" // fe80::7:8%eth0 fe80::7:8%1 (link-local IPv6 addresses with zone index)
|
||||
"::(?:ffff(?::0{1,4}){0,1}:){0,1}" + v4_regex() + "|" // ::255.255.255.255 ::ffff:255.255.255.255 ::ffff:0:255.255.255.255 (IPv4-mapped IPv6 addresses and IPv4-translated addresses)
|
||||
"(?:" + ipv6seg + ":){1,4}:" + v4_regex() + // 2001:db8:3:4::192.0.2.33 64:ff9b::192.0.2.33 (IPv4-Embedded IPv6 Address)
|
||||
")";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,344 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2017 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef OPENVPN_ADDR_ROUTE_H
|
||||
#define OPENVPN_ADDR_ROUTE_H
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <cstdint> // for std::uint32_t
|
||||
#include <tuple>
|
||||
|
||||
#include <openvpn/common/size.hpp>
|
||||
#include <openvpn/common/exception.hpp>
|
||||
#include <openvpn/common/number.hpp>
|
||||
#include <openvpn/common/to_string.hpp>
|
||||
#include <openvpn/common/split.hpp>
|
||||
#include <openvpn/common/hash.hpp>
|
||||
#include <openvpn/addr/ip.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
namespace IP {
|
||||
// Basic route object
|
||||
template <typename ADDR>
|
||||
class RouteType
|
||||
{
|
||||
public:
|
||||
typedef ADDR Addr;
|
||||
|
||||
ADDR addr;
|
||||
unsigned int prefix_len;
|
||||
|
||||
OPENVPN_EXCEPTION(route_error);
|
||||
|
||||
RouteType()
|
||||
: prefix_len(0)
|
||||
{
|
||||
}
|
||||
|
||||
RouteType(const std::string& rtstr, const char *title = nullptr)
|
||||
: RouteType(RouteType::from_string(rtstr, title))
|
||||
{
|
||||
}
|
||||
|
||||
RouteType(const std::string& rtstr, const std::string& title)
|
||||
: RouteType(RouteType::from_string(rtstr, title.c_str()))
|
||||
{
|
||||
}
|
||||
|
||||
RouteType(const ADDR& addr_arg,
|
||||
const unsigned int prefix_len_arg)
|
||||
: addr(addr_arg),
|
||||
prefix_len(prefix_len_arg)
|
||||
{
|
||||
}
|
||||
|
||||
static RouteType from_string(const std::string& rtstr, const char *title = nullptr)
|
||||
{
|
||||
RouteType r;
|
||||
std::vector<std::string> pair;
|
||||
pair.reserve(2);
|
||||
Split::by_char_void<std::vector<std::string>, NullLex, Split::NullLimit>(pair, rtstr, '/', 0, 1);
|
||||
r.addr = ADDR::from_string(pair[0], title);
|
||||
if (pair.size() >= 2)
|
||||
{
|
||||
r.prefix_len = parse_number_throw<unsigned int>(pair[1], "prefix length");
|
||||
if (r.prefix_len > r.addr.size())
|
||||
OPENVPN_THROW(route_error, (title ? title : "route") << " : bad prefix length : " << rtstr);
|
||||
}
|
||||
else
|
||||
r.prefix_len = r.addr.size();
|
||||
return r;
|
||||
}
|
||||
|
||||
bool defined() const
|
||||
{
|
||||
return addr.defined();
|
||||
}
|
||||
|
||||
IP::Addr::Version version() const
|
||||
{
|
||||
return addr.version();
|
||||
}
|
||||
|
||||
IP::Addr::VersionMask version_mask() const
|
||||
{
|
||||
return addr.version_mask();
|
||||
}
|
||||
|
||||
RouteType<IPv4::Addr> to_ipv4() const
|
||||
{
|
||||
return RouteType<IPv4::Addr>(addr.to_ipv4(), prefix_len);
|
||||
}
|
||||
|
||||
RouteType<IPv6::Addr> to_ipv6() const
|
||||
{
|
||||
return RouteType<IPv6::Addr>(addr.to_ipv6(), prefix_len);
|
||||
}
|
||||
|
||||
ADDR netmask() const
|
||||
{
|
||||
return netmask_(addr, prefix_len);
|
||||
}
|
||||
|
||||
size_t extent() const
|
||||
{
|
||||
return netmask().extent_from_netmask().to_ulong();
|
||||
}
|
||||
|
||||
bool is_canonical() const
|
||||
{
|
||||
return (addr & netmask()) == addr;
|
||||
}
|
||||
|
||||
void force_canonical()
|
||||
{
|
||||
addr = addr & netmask();
|
||||
}
|
||||
|
||||
void verify_canonical() const
|
||||
{
|
||||
if (!is_canonical())
|
||||
throw route_error("route not canonical: " + to_string());
|
||||
}
|
||||
|
||||
bool is_host() const
|
||||
{
|
||||
return addr.defined() && prefix_len == addr.size();
|
||||
}
|
||||
|
||||
unsigned int host_bits() const
|
||||
{
|
||||
if (prefix_len < addr.size())
|
||||
return addr.size() - prefix_len;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool contains(const ADDR& a) const // assumes canonical address/routes
|
||||
{
|
||||
if (addr.defined() && version_eq(addr, a))
|
||||
return (a & netmask()) == addr;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool contains(const RouteType& r) const // assumes canonical routes
|
||||
{
|
||||
return contains(r.addr) && r.prefix_len >= prefix_len;
|
||||
}
|
||||
|
||||
bool split(RouteType& r1, RouteType& r2) const // assumes we are canonical
|
||||
{
|
||||
if (!is_host())
|
||||
{
|
||||
const unsigned int newpl = prefix_len + 1;
|
||||
r1.addr = addr;
|
||||
r1.prefix_len = newpl;
|
||||
|
||||
r2.addr = addr + netmask_(addr, newpl).extent_from_netmask();
|
||||
r2.prefix_len = newpl;
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string to_string() const
|
||||
{
|
||||
return addr.to_string() + '/' + openvpn::to_string(prefix_len);
|
||||
}
|
||||
|
||||
std::string to_string_by_netmask() const
|
||||
{
|
||||
return addr.to_string() + ' ' + netmask().to_string();
|
||||
}
|
||||
|
||||
bool operator==(const RouteType& other) const
|
||||
{
|
||||
return std::tie(prefix_len, addr) == std::tie(other.prefix_len, other.addr);
|
||||
}
|
||||
|
||||
bool operator!=(const RouteType& other) const
|
||||
{
|
||||
return std::tie(prefix_len, addr) != std::tie(other.prefix_len, other.addr);
|
||||
}
|
||||
|
||||
bool operator<(const RouteType& other) const
|
||||
{
|
||||
return std::tie(prefix_len, addr) < std::tie(other.prefix_len, other.addr);
|
||||
}
|
||||
|
||||
template <typename HASH>
|
||||
void hash(HASH& h) const
|
||||
{
|
||||
addr.hash(h);
|
||||
h(prefix_len);
|
||||
}
|
||||
|
||||
#ifdef HAVE_CITYHASH
|
||||
std::size_t hash_value() const
|
||||
{
|
||||
HashSizeT h;
|
||||
hash(h);
|
||||
return h.value();
|
||||
}
|
||||
#endif
|
||||
|
||||
private:
|
||||
static IPv4::Addr netmask_(const IPv4::Addr&, unsigned int prefix_len)
|
||||
{
|
||||
return IPv4::Addr::netmask_from_prefix_len(prefix_len);
|
||||
}
|
||||
|
||||
static IPv6::Addr netmask_(const IPv6::Addr&, unsigned int prefix_len)
|
||||
{
|
||||
return IPv6::Addr::netmask_from_prefix_len(prefix_len);
|
||||
}
|
||||
|
||||
static IP::Addr netmask_(const IP::Addr& addr, unsigned int prefix_len)
|
||||
{
|
||||
return IP::Addr::netmask_from_prefix_len(addr.version(), prefix_len);
|
||||
}
|
||||
|
||||
static bool version_eq(const IPv4::Addr&, const IPv4::Addr&)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool version_eq(const IPv6::Addr&, const IPv6::Addr&)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool version_eq(const IP::Addr& a1, const IP::Addr& a2)
|
||||
{
|
||||
return a1.version() == a2.version();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ADDR>
|
||||
struct RouteTypeList : public std::vector<RouteType<ADDR>>
|
||||
{
|
||||
typedef std::vector< RouteType<ADDR> > Base;
|
||||
|
||||
OPENVPN_EXCEPTION(route_list_error);
|
||||
|
||||
std::string to_string() const
|
||||
{
|
||||
std::ostringstream os;
|
||||
for (auto &r : *this)
|
||||
os << r.to_string() << std::endl;
|
||||
return os.str();
|
||||
}
|
||||
|
||||
IP::Addr::VersionMask version_mask() const
|
||||
{
|
||||
IP::Addr::VersionMask mask = 0;
|
||||
for (auto &r : *this)
|
||||
mask |= r.version_mask();
|
||||
return mask;
|
||||
}
|
||||
|
||||
void verify_canonical() const
|
||||
{
|
||||
for (auto &r : *this)
|
||||
r.verify_canonical();
|
||||
}
|
||||
|
||||
template <typename R>
|
||||
bool contains(const R& c) const
|
||||
{
|
||||
for (auto &r : *this)
|
||||
if (r.contains(c))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
typedef RouteType<IP::Addr> Route;
|
||||
typedef RouteType<IPv4::Addr> Route4;
|
||||
typedef RouteType<IPv6::Addr> Route6;
|
||||
|
||||
typedef RouteTypeList<IP::Addr> RouteList;
|
||||
typedef RouteTypeList<IPv4::Addr> Route4List;
|
||||
typedef RouteTypeList<IPv6::Addr> Route6List;
|
||||
|
||||
OPENVPN_OSTREAM(Route, to_string);
|
||||
OPENVPN_OSTREAM(Route4, to_string);
|
||||
OPENVPN_OSTREAM(Route6, to_string);
|
||||
|
||||
OPENVPN_OSTREAM(RouteList, to_string);
|
||||
OPENVPN_OSTREAM(Route4List, to_string);
|
||||
OPENVPN_OSTREAM(Route6List, to_string);
|
||||
|
||||
inline Route route_from_string_prefix(const std::string& addrstr,
|
||||
const unsigned int prefix_len,
|
||||
const std::string& title,
|
||||
const IP::Addr::Version required_version = IP::Addr::UNSPEC)
|
||||
{
|
||||
Route r;
|
||||
r.addr = IP::Addr(addrstr, title, required_version);
|
||||
r.prefix_len = prefix_len;
|
||||
if (r.prefix_len > r.addr.size())
|
||||
OPENVPN_THROW(Route::route_error, title << " : bad prefix length : " << addrstr);
|
||||
return r;
|
||||
}
|
||||
|
||||
inline Route route_from_string(const std::string& rtstr,
|
||||
const std::string& title,
|
||||
const IP::Addr::Version required_version = IP::Addr::UNSPEC)
|
||||
{
|
||||
Route r(rtstr, title);
|
||||
r.addr.validate_version(title, required_version);
|
||||
return r;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HAVE_CITYHASH
|
||||
OPENVPN_HASH_METHOD(openvpn::IP::Route, hash_value);
|
||||
OPENVPN_HASH_METHOD(openvpn::IP::Route4, hash_value);
|
||||
OPENVPN_HASH_METHOD(openvpn::IP::Route6, hash_value);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user