0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-25 08:12:37 +01:00

ircd::net: Split up remote.h

This commit is contained in:
Jason Volk 2018-01-07 13:48:03 -08:00
parent 9dbae8b27a
commit 5615ef2470
5 changed files with 340 additions and 274 deletions

View file

@ -0,0 +1,85 @@
/*
* Copyright (C) 2017 Charybdis Development Team
* Copyright (C) 2017 Jason Volk <jason@zemos.net>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#define HAVE_IRCD_NET_HOSTPORT_H
namespace ircd::net
{
struct hostport;
const auto &host(const hostport &);
auto &host(hostport &);
string_view string(const mutable_buffer &out, const hostport &);
}
namespace ircd
{
using net::hostport;
using net::host;
}
/// This structure holds a hostname and port usually fresh from user input
/// intended for resolution.
///
/// The host can be specified as a hostname or hostname:port string. If no
/// port argument is specified and no port is present in the host string
/// then 8448 is assumed.
///
struct ircd::net::hostport
{
string_view host {"0.0.0.0"};
string_view port {"8448"};
uint16_t portnum {0};
hostport(const string_view &host, const string_view &port)
:host{host}
,port{port}
{}
hostport(const string_view &host, const uint16_t &portnum)
:host{host}
,port{}
,portnum{portnum}
{}
hostport(const string_view &amalgam)
:host{rsplit(amalgam, ':').first}
,port{rsplit(amalgam, ':').second}
{}
hostport() = default;
friend std::ostream &operator<<(std::ostream &, const hostport &);
};
inline auto &
ircd::net::host(hostport &hp)
{
return hp.host;
}
inline const auto &
ircd::net::host(const hostport &hp)
{
return hp.host;
}

167
include/ircd/net/ipport.h Normal file
View file

@ -0,0 +1,167 @@
/*
* Copyright (C) 2017 Charybdis Development Team
* Copyright (C) 2017 Jason Volk <jason@zemos.net>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#define HAVE_IRCD_NET_IPPORT_H
// Forward declarations for boost because it is not included here.
namespace boost::asio::ip
{
struct address;
};
namespace ircd::net
{
struct ipport;
const uint16_t &port(const ipport &);
uint16_t &port(ipport &);
bool is_v6(const ipport &);
bool is_v4(const ipport &);
const uint128_t &host6(const ipport &);
const uint32_t &host4(const ipport &);
uint128_t &host6(ipport &);
uint32_t &host4(ipport &);
string_view string(const mutable_buffer &out, const uint32_t &);
string_view string(const mutable_buffer &out, const uint128_t &);
string_view string(const mutable_buffer &out, const ipport &);
}
namespace ircd
{
using net::ipport;
using net::host;
using net::port;
}
/// This lightweight structure holds an IP address and port in native byte
/// form. It is usually returned from the result of a resolution but may also
/// serve as input to a reverse resolution. Either way, this allocation-free
/// structure is useful for storing raw IP/port data, even in large sets.
///
/// The TYPE field is only a boolean for now indicating IPv4 or IPv6; the
/// official way to query this is to use net::is_v6(ipport) etc and not
/// std::get the type directly.
///
struct ircd::net::ipport
:std::tuple<std::array<uint8_t, 16>, uint16_t, bool>
{
enum { IP, PORT, TYPE };
explicit operator bool() const;
bool operator!() const { return !static_cast<bool>(*this); }
ipport(const uint32_t &ip, const uint16_t &port);
ipport(const uint128_t &ip, const uint16_t &port);
ipport(const boost::asio::ip::address &, const uint16_t &port);
ipport();
friend std::ostream &operator<<(std::ostream &, const ipport &);
};
inline
ircd::net::ipport::ipport()
{
std::get<PORT>(*this) = 0;
std::get<TYPE>(*this) = 0;
auto &ip(std::get<IP>(*this));
std::fill(begin(ip), end(ip), 0x0);
}
inline
ircd::net::ipport::ipport(const uint32_t &ip,
const uint16_t &p)
{
std::get<TYPE>(*this) = false;
host4(*this) = ip;
port(*this) = p;
}
inline
ircd::net::ipport::ipport(const uint128_t &ip,
const uint16_t &p)
{
std::get<TYPE>(*this) = true;
host6(*this) = ip;
port(*this) = p;
}
inline ircd::net::ipport::operator
bool()
const
{
return std::get<PORT>(*this) != 0;
}
inline ircd::uint128_t &
ircd::net::host6(ipport &ipp)
{
auto &bytes{std::get<ipp.IP>(ipp)};
return *reinterpret_cast<uint128_t *>(bytes.data());
}
inline const ircd::uint128_t &
ircd::net::host6(const ipport &ipp)
{
const auto &bytes{std::get<ipp.IP>(ipp)};
return *reinterpret_cast<const uint128_t *>(bytes.data());
}
inline bool
ircd::net::is_v6(const ipport &ipp)
{
return std::get<ipp.TYPE>(ipp) == true;
}
inline uint32_t &
ircd::net::host4(ipport &ipp)
{
auto &bytes{std::get<ipp.IP>(ipp)};
return *reinterpret_cast<uint32_t *>(bytes.data());
}
inline const uint32_t &
ircd::net::host4(const ipport &ipp)
{
const auto &bytes{std::get<ipp.IP>(ipp)};
return *reinterpret_cast<const uint32_t *>(bytes.data());
}
inline bool
ircd::net::is_v4(const ipport &ipp)
{
return std::get<ipp.TYPE>(ipp) == false;
}
inline uint16_t &
ircd::net::port(ipport &ipp)
{
return std::get<ipp.PORT>(ipp);
}
inline const uint16_t &
ircd::net::port(const ipport &ipp)
{
return std::get<ipp.PORT>(ipp);
}

View file

@ -46,6 +46,8 @@ namespace ircd::net
extern struct log::log log;
}
#include "hostport.h"
#include "ipport.h"
#include "remote.h"
#include "resolve.h"
#include "listener.h"

View file

@ -22,110 +22,18 @@
#pragma once
#define HAVE_IRCD_NET_REMOTE_H
// Forward declarations for boost because it is not included here.
namespace boost::asio::ip
{
struct address;
};
namespace ircd::net
{
struct hostport;
struct ipport;
struct remote;
const uint16_t &port(const ipport &);
uint16_t &port(ipport &);
bool is_v6(const ipport &);
bool is_v4(const ipport &);
const uint128_t &host6(const ipport &);
const uint32_t &host4(const ipport &);
const auto &host(const hostport &);
uint128_t &host6(ipport &);
uint32_t &host4(ipport &);
auto &host(hostport &);
string_view string(const mutable_buffer &out, const uint32_t &);
string_view string(const mutable_buffer &out, const uint128_t &);
string_view string(const mutable_buffer &out, const hostport &);
string_view string(const mutable_buffer &out, const ipport &);
string_view string(const mutable_buffer &out, const remote &);
}
namespace ircd
{
using net::hostport;
using net::ipport;
using net::remote;
using net::host;
using net::port;
}
/// This structure holds a hostname and port usually fresh from user input
/// intended for resolution.
///
/// The host can be specified as a hostname or hostname:port string. If no
/// port argument is specified and no port is present in the host string
/// then 8448 is assumed.
///
struct ircd::net::hostport
{
string_view host {"0.0.0.0"};
string_view port {"8448"};
uint16_t portnum {0};
hostport(const string_view &host, const string_view &port)
:host{host}
,port{port}
{}
hostport(const string_view &host, const uint16_t &portnum)
:host{host}
,port{}
,portnum{portnum}
{}
hostport(const string_view &amalgam)
:host{rsplit(amalgam, ':').first}
,port{rsplit(amalgam, ':').second}
{}
hostport() = default;
friend std::ostream &operator<<(std::ostream &, const hostport &);
};
/// This lightweight structure holds an IP address and port in native byte
/// form. It is usually returned from the result of a resolution but may also
/// serve as input to a reverse resolution. Either way, this allocation-free
/// structure is useful for storing raw IP/port data, even in large sets.
///
/// Constructing this object with a hostname will yield an IP address
/// resolution, the result being saved in the structure. The hostname argument
/// is not saved in the structure; consider using net::remote instead.
///
/// The TYPE field is only a boolean for now indicating IPv4 or IPv6; the
/// official way to query this is to use net::is_v6(ipport) etc and not
/// std::get the type directly.
///
struct ircd::net::ipport
:std::tuple<std::array<uint8_t, 16>, uint16_t, bool>
{
enum { IP, PORT, TYPE };
explicit operator bool() const;
bool operator!() const { return !static_cast<bool>(*this); }
ipport(const uint32_t &ip, const uint16_t &port);
ipport(const uint128_t &ip, const uint16_t &port);
ipport(const boost::asio::ip::address &, const uint16_t &port);
ipport();
friend std::ostream &operator<<(std::ostream &, const ipport &);
};
/// This structure combines features of hostport and ipport to hold a remote's
/// resolved IP in bytes, a port number, and an optional hostname string which
/// may have been used to resolve the IP, or may have been resolved from the
@ -164,101 +72,3 @@ const
{
return resolved() || !hostname.empty();
}
inline
ircd::net::ipport::ipport()
{
std::get<PORT>(*this) = 0;
std::get<TYPE>(*this) = 0;
auto &ip(std::get<IP>(*this));
std::fill(begin(ip), end(ip), 0x0);
}
inline
ircd::net::ipport::ipport(const uint32_t &ip,
const uint16_t &p)
{
std::get<TYPE>(*this) = false;
host4(*this) = ip;
port(*this) = p;
}
inline
ircd::net::ipport::ipport(const uint128_t &ip,
const uint16_t &p)
{
std::get<TYPE>(*this) = true;
host6(*this) = ip;
port(*this) = p;
}
inline ircd::net::ipport::operator
bool()
const
{
return std::get<PORT>(*this) != 0;
}
inline ircd::uint128_t &
ircd::net::host6(ipport &ipp)
{
auto &bytes{std::get<ipp.IP>(ipp)};
return *reinterpret_cast<uint128_t *>(bytes.data());
}
inline const ircd::uint128_t &
ircd::net::host6(const ipport &ipp)
{
const auto &bytes{std::get<ipp.IP>(ipp)};
return *reinterpret_cast<const uint128_t *>(bytes.data());
}
inline bool
ircd::net::is_v6(const ipport &ipp)
{
return std::get<ipp.TYPE>(ipp) == true;
}
inline uint32_t &
ircd::net::host4(ipport &ipp)
{
auto &bytes{std::get<ipp.IP>(ipp)};
return *reinterpret_cast<uint32_t *>(bytes.data());
}
inline const uint32_t &
ircd::net::host4(const ipport &ipp)
{
const auto &bytes{std::get<ipp.IP>(ipp)};
return *reinterpret_cast<const uint32_t *>(bytes.data());
}
inline bool
ircd::net::is_v4(const ipport &ipp)
{
return std::get<ipp.TYPE>(ipp) == false;
}
inline auto &
ircd::net::host(hostport &hp)
{
return hp.host;
}
inline const auto &
ircd::net::host(const hostport &hp)
{
return hp.host;
}
inline uint16_t &
ircd::net::port(ipport &ipp)
{
return std::get<ipp.PORT>(ipp);
}
inline const uint16_t &
ircd::net::port(const ipport &ipp)
{
return std::get<ipp.PORT>(ipp);
}

View file

@ -2075,81 +2075,12 @@ ircd::net::_resolve(const ipport &ipport,
// net/remote.h
//
//
// host / port utils
//
ircd::string_view
ircd::net::string(const mutable_buffer &buf,
const uint32_t &ip)
std::ostream &
ircd::net::operator<<(std::ostream &s, const remote &t)
{
const auto len
{
ip::address_v4{ip}.to_string().copy(data(buf), size(buf))
};
return { data(buf), size_t(len) };
}
ircd::string_view
ircd::net::string(const mutable_buffer &buf,
const uint128_t &ip)
{
const auto &pun
{
reinterpret_cast<const uint8_t (&)[16]>(ip)
};
const auto &punpun
{
reinterpret_cast<const std::array<uint8_t, 16> &>(pun)
};
const auto len
{
ip::address_v6{punpun}.to_string().copy(data(buf), size(buf))
};
return { data(buf), size_t(len) };
}
ircd::string_view
ircd::net::string(const mutable_buffer &buf,
const hostport &hp)
{
const auto len
{
fmt::sprintf
{
buf, "%s:%s",
hp.host,
hp.portnum? lex_cast(hp.portnum) : hp.port
}
};
return { data(buf), size_t(len) };
}
ircd::string_view
ircd::net::string(const mutable_buffer &buf,
const ipport &ipp)
{
const auto len
{
is_v4(ipp)?
fmt::sprintf(buf, "%s:%u",
ip::address_v4{host4(ipp)}.to_string(),
port(ipp)):
is_v6(ipp)?
fmt::sprintf(buf, "%s:%u",
ip::address_v6{std::get<ipp.IP>(ipp)}.to_string(),
port(ipp)):
0
};
return { data(buf), size_t(len) };
char buf[256];
s << string(buf, t);
return s;
}
ircd::string_view
@ -2190,16 +2121,9 @@ ircd::net::remote::remote(const hostport &hostport)
{
}
std::ostream &
ircd::net::operator<<(std::ostream &s, const remote &t)
{
char buf[256];
s << string(buf, t);
return s;
}
///////////////////////////////////////////////////////////////////////////////
//
// ipport
// net/ipport.h
//
std::ostream &
@ -2210,6 +2134,62 @@ ircd::net::operator<<(std::ostream &s, const ipport &t)
return s;
}
ircd::string_view
ircd::net::string(const mutable_buffer &buf,
const uint32_t &ip)
{
const auto len
{
ip::address_v4{ip}.to_string().copy(data(buf), size(buf))
};
return { data(buf), size_t(len) };
}
ircd::string_view
ircd::net::string(const mutable_buffer &buf,
const uint128_t &ip)
{
const auto &pun
{
reinterpret_cast<const uint8_t (&)[16]>(ip)
};
const auto &punpun
{
reinterpret_cast<const std::array<uint8_t, 16> &>(pun)
};
const auto len
{
ip::address_v6{punpun}.to_string().copy(data(buf), size(buf))
};
return { data(buf), size_t(len) };
}
ircd::string_view
ircd::net::string(const mutable_buffer &buf,
const ipport &ipp)
{
const auto len
{
is_v4(ipp)?
fmt::sprintf(buf, "%s:%u",
ip::address_v4{host4(ipp)}.to_string(),
port(ipp)):
is_v6(ipp)?
fmt::sprintf(buf, "%s:%u",
ip::address_v6{std::get<ipp.IP>(ipp)}.to_string(),
port(ipp)):
0
};
return { data(buf), size_t(len) };
}
ircd::net::ipport
ircd::net::make_ipport(const boost::asio::ip::tcp::endpoint &ep)
{
@ -2235,6 +2215,10 @@ ircd::net::make_endpoint(const ipport &ipport)
};
}
//
// ipport
//
ircd::net::ipport::ipport(const boost::asio::ip::address &address,
const uint16_t &port)
{
@ -2250,8 +2234,9 @@ ircd::net::ipport::ipport(const boost::asio::ip::address &address,
net::port(*this) = port;
}
///////////////////////////////////////////////////////////////////////////////
//
// hostport
// net/hostport.h
//
std::ostream &
@ -2262,6 +2247,23 @@ ircd::net::operator<<(std::ostream &s, const hostport &t)
return s;
}
ircd::string_view
ircd::net::string(const mutable_buffer &buf,
const hostport &hp)
{
const auto len
{
fmt::sprintf
{
buf, "%s:%s",
hp.host,
hp.portnum? lex_cast(hp.portnum) : hp.port
}
};
return { data(buf), size_t(len) };
}
///////////////////////////////////////////////////////////////////////////////
//
// net/asio.h