2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 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. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2018-01-07 22:48:03 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_NET_IPPORT_H
|
|
|
|
|
|
|
|
namespace ircd::net
|
|
|
|
{
|
|
|
|
struct ipport;
|
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
const uint128_t &host6(const ipport &);
|
|
|
|
const uint32_t &host4(const ipport &);
|
|
|
|
uint128_t &host6(ipport &);
|
|
|
|
uint32_t &host4(ipport &);
|
|
|
|
|
2018-01-07 22:48:03 +01:00
|
|
|
const uint16_t &port(const ipport &);
|
|
|
|
uint16_t &port(ipport &);
|
|
|
|
|
2019-08-05 01:41:08 +02:00
|
|
|
bool is_null(const ipport &);
|
2019-03-25 02:27:49 +01:00
|
|
|
bool is_loop(const ipport &);
|
2018-01-07 22:48:03 +01:00
|
|
|
bool is_v6(const ipport &);
|
|
|
|
bool is_v4(const ipport &);
|
|
|
|
|
|
|
|
string_view string(const mutable_buffer &out, const ipport &);
|
2019-02-16 22:25:18 +01:00
|
|
|
std::ostream &operator<<(std::ostream &, const ipport &);
|
2018-01-07 22:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
///
|
|
|
|
struct ircd::net::ipport
|
2019-03-25 02:27:49 +01:00
|
|
|
:std::pair<ipaddr, uint16_t>
|
2018-01-07 22:48:03 +01:00
|
|
|
{
|
2019-03-25 02:27:49 +01:00
|
|
|
enum { IP, PORT };
|
2018-01-07 22:48:03 +01:00
|
|
|
|
2018-09-30 01:46:02 +02:00
|
|
|
struct cmp;
|
|
|
|
struct cmp_ip;
|
|
|
|
struct cmp_port;
|
|
|
|
|
2018-01-07 22:48:03 +01:00
|
|
|
explicit operator bool() const;
|
|
|
|
bool operator!() const { return !static_cast<bool>(*this); }
|
|
|
|
|
2019-06-02 00:01:19 +02:00
|
|
|
operator const ipaddr &() const;
|
|
|
|
operator ipaddr &();
|
2019-06-01 23:54:38 +02:00
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
template<class iparg> ipport(iparg&&, const uint16_t &port);
|
|
|
|
template<class iparg> ipport(iparg&&, const string_view &port);
|
2019-03-25 03:17:05 +01:00
|
|
|
ipport(const string_view &amalgam);
|
2018-01-07 22:48:03 +01:00
|
|
|
ipport();
|
|
|
|
};
|
|
|
|
|
2018-09-30 01:46:02 +02:00
|
|
|
struct ircd::net::ipport::cmp_port
|
|
|
|
{
|
|
|
|
bool operator()(const ipport &a, const ipport &b) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ircd::net::ipport::cmp_ip
|
|
|
|
{
|
|
|
|
bool operator()(const ipport &a, const ipport &b) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ircd::net::ipport::cmp
|
|
|
|
:std::less<ipport>
|
|
|
|
{
|
|
|
|
using std::less<ipport>::less;
|
|
|
|
};
|
|
|
|
|
2018-01-07 22:48:03 +01:00
|
|
|
inline
|
|
|
|
ircd::net::ipport::ipport()
|
2019-03-25 02:27:49 +01:00
|
|
|
:std::pair<ipaddr, uint16_t>
|
2018-01-07 22:48:03 +01:00
|
|
|
{
|
2019-03-25 02:27:49 +01:00
|
|
|
{}, 0
|
2018-01-07 22:48:03 +01:00
|
|
|
}
|
2019-03-25 02:27:49 +01:00
|
|
|
{}
|
|
|
|
|
2019-03-25 03:17:05 +01:00
|
|
|
inline
|
|
|
|
ircd::net::ipport::ipport(const string_view &amalgam)
|
|
|
|
:std::pair<ipaddr, uint16_t>
|
|
|
|
{
|
|
|
|
rfc3986::host(amalgam), rfc3986::port(amalgam)
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
2019-03-25 02:27:49 +01:00
|
|
|
template<class iparg>
|
|
|
|
ircd::net::ipport::ipport(iparg&& arg,
|
|
|
|
const string_view &port)
|
|
|
|
:ipport
|
|
|
|
{
|
|
|
|
std::forward<iparg>(arg), lex_cast<uint16_t>(port)
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<class iparg>
|
|
|
|
ircd::net::ipport::ipport(iparg&& arg,
|
|
|
|
const uint16_t &port)
|
|
|
|
:std::pair<ipaddr, uint16_t>
|
|
|
|
{
|
|
|
|
std::forward<iparg>(arg), port
|
|
|
|
}
|
|
|
|
{}
|
2018-01-07 22:48:03 +01:00
|
|
|
|
2019-06-01 23:54:38 +02:00
|
|
|
inline ircd::net::ipport::operator
|
|
|
|
ipaddr &()
|
|
|
|
{
|
|
|
|
return this->first;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::net::ipport::operator
|
|
|
|
const ipaddr &()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return this->first;
|
|
|
|
}
|
|
|
|
|
2018-01-07 22:48:03 +01:00
|
|
|
inline ircd::net::ipport::operator
|
|
|
|
bool()
|
|
|
|
const
|
|
|
|
{
|
2018-10-03 23:17:27 +02:00
|
|
|
return host6(*this) != uint128_t(0);
|
2018-01-07 22:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::uint128_t &
|
|
|
|
ircd::net::host6(ipport &ipp)
|
|
|
|
{
|
2019-06-23 08:28:24 +02:00
|
|
|
return host6(std::get<ipport::IP>(ipp));
|
2018-01-07 22:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline const ircd::uint128_t &
|
|
|
|
ircd::net::host6(const ipport &ipp)
|
|
|
|
{
|
2019-06-23 08:28:24 +02:00
|
|
|
return host6(std::get<ipport::IP>(ipp));
|
2018-01-07 22:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::net::is_v6(const ipport &ipp)
|
|
|
|
{
|
2019-06-23 08:28:24 +02:00
|
|
|
return is_v6(std::get<ipport::IP>(ipp));
|
2018-01-07 22:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t &
|
|
|
|
ircd::net::host4(ipport &ipp)
|
|
|
|
{
|
2019-06-23 08:28:24 +02:00
|
|
|
return host4(std::get<ipport::IP>(ipp));
|
2018-01-07 22:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline const uint32_t &
|
|
|
|
ircd::net::host4(const ipport &ipp)
|
|
|
|
{
|
2019-06-23 08:28:24 +02:00
|
|
|
return host4(std::get<ipport::IP>(ipp));
|
2018-01-07 22:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::net::is_v4(const ipport &ipp)
|
|
|
|
{
|
2019-06-23 08:28:24 +02:00
|
|
|
return is_v4(std::get<ipport::IP>(ipp));
|
2018-01-07 22:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline uint16_t &
|
|
|
|
ircd::net::port(ipport &ipp)
|
|
|
|
{
|
2019-06-23 08:28:24 +02:00
|
|
|
return std::get<ipport::PORT>(ipp);
|
2018-01-07 22:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline const uint16_t &
|
|
|
|
ircd::net::port(const ipport &ipp)
|
|
|
|
{
|
2019-06-23 08:28:24 +02:00
|
|
|
return std::get<ipport::PORT>(ipp);
|
2018-01-07 22:48:03 +01:00
|
|
|
}
|
2019-03-25 02:27:49 +01:00
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::net::is_loop(const ipport &ipp)
|
|
|
|
{
|
2019-06-23 08:28:24 +02:00
|
|
|
return is_loop(std::get<ipport::IP>(ipp));
|
2019-03-25 02:27:49 +01:00
|
|
|
}
|
2019-08-05 01:41:08 +02:00
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::net::is_null(const ipport &ipp)
|
|
|
|
{
|
|
|
|
return is_null(std::get<ipport::IP>(ipp));
|
|
|
|
}
|