2018-01-07 22:48:03 +01:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2018-01-10 10:20:16 +01:00
|
|
|
uint16_t port(const hostport &);
|
2018-01-22 11:06:43 +01:00
|
|
|
const string_view &host(const hostport &);
|
|
|
|
string_view &host(hostport &);
|
2018-01-07 22:48:03 +01:00
|
|
|
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};
|
|
|
|
|
2018-01-22 11:06:43 +01:00
|
|
|
bool operator!() const;
|
|
|
|
|
2018-01-07 22:48:03 +01:00
|
|
|
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}
|
2018-01-08 02:42:16 +01:00
|
|
|
,port{amalgam != host? rsplit(amalgam, ':').second : "8448"}
|
2018-01-07 22:48:03 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
hostport() = default;
|
|
|
|
|
|
|
|
friend std::ostream &operator<<(std::ostream &, const hostport &);
|
|
|
|
};
|
|
|
|
|
2018-01-22 11:06:43 +01:00
|
|
|
inline bool
|
|
|
|
ircd::net::hostport::operator!()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
static const hostport defaults{};
|
|
|
|
return net::host(*this) == net::host(defaults);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::string_view &
|
2018-01-07 22:48:03 +01:00
|
|
|
ircd::net::host(hostport &hp)
|
|
|
|
{
|
|
|
|
return hp.host;
|
|
|
|
}
|
|
|
|
|
2018-01-22 11:06:43 +01:00
|
|
|
inline const ircd::string_view &
|
2018-01-07 22:48:03 +01:00
|
|
|
ircd::net::host(const hostport &hp)
|
|
|
|
{
|
|
|
|
return hp.host;
|
|
|
|
}
|
2018-01-10 10:20:16 +01:00
|
|
|
|
|
|
|
inline uint16_t
|
|
|
|
ircd::net::port(const hostport &hp)
|
|
|
|
{
|
|
|
|
return hp.portnum? hp.portnum:
|
|
|
|
hp.port? lex_cast<uint16_t>(hp.port):
|
|
|
|
0;
|
|
|
|
}
|