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_HOSTPORT_H
|
|
|
|
|
|
|
|
namespace ircd::net
|
|
|
|
{
|
|
|
|
struct hostport;
|
|
|
|
|
2018-05-20 03:25:11 +02:00
|
|
|
// abstraction bleed
|
|
|
|
extern const uint16_t canon_port;
|
|
|
|
extern const string_view canon_service;
|
|
|
|
|
2018-02-03 20:38:46 +01:00
|
|
|
const uint16_t &port(const hostport &);
|
2018-01-22 11:06:43 +01:00
|
|
|
const string_view &host(const hostport &);
|
2018-02-03 20:38:46 +01:00
|
|
|
const string_view &service(const hostport &);
|
|
|
|
|
|
|
|
uint16_t &port(hostport &);
|
2018-01-22 11:06:43 +01:00
|
|
|
string_view &host(hostport &);
|
2018-01-07 22:48:03 +01:00
|
|
|
string_view string(const mutable_buffer &out, const hostport &);
|
2018-05-20 03:25:11 +02:00
|
|
|
|
|
|
|
string_view canonize(const mutable_buffer &out, const hostport &, const uint16_t &port = canon_port);
|
|
|
|
std::string canonize(const hostport &, const uint16_t &port = canon_port);
|
2019-02-16 01:47:00 +01:00
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &, const hostport &);
|
2018-01-07 22:48:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
using net::hostport;
|
|
|
|
using net::host;
|
|
|
|
}
|
|
|
|
|
2018-02-03 20:38:46 +01:00
|
|
|
/// This structure holds a hostname and service or port usually fresh from
|
|
|
|
/// user input intended for resolution.
|
|
|
|
///
|
|
|
|
/// The host can be passed to the constructor as a hostname or hostname:port
|
|
|
|
/// string.
|
2018-01-07 22:48:03 +01:00
|
|
|
///
|
2018-02-03 20:38:46 +01:00
|
|
|
/// The service field is a string which will trigger an SRV query during
|
|
|
|
/// resolution and/or be used to figure out the port number. If it is omitted
|
|
|
|
/// then the numerical port should be specified directly. If it exists then
|
|
|
|
/// the numerical port will only be used as a fallback, and an SRV response
|
|
|
|
/// with an alternative port may even override both the host and given
|
|
|
|
/// numerical port here.
|
2018-01-07 22:48:03 +01:00
|
|
|
///
|
|
|
|
struct ircd::net::hostport
|
|
|
|
{
|
2019-03-19 01:40:16 +01:00
|
|
|
IRCD_OVERLOAD(verbatim)
|
|
|
|
|
2018-01-07 22:48:03 +01:00
|
|
|
string_view host {"0.0.0.0"};
|
2018-05-20 03:25:11 +02:00
|
|
|
string_view service {canon_service};
|
|
|
|
uint16_t port {canon_port};
|
2018-01-07 22:48:03 +01:00
|
|
|
|
2019-03-06 05:49:23 +01:00
|
|
|
explicit operator bool() const;
|
2018-01-22 11:06:43 +01:00
|
|
|
bool operator!() const;
|
|
|
|
|
2018-05-20 03:25:11 +02:00
|
|
|
hostport(const string_view &host, const string_view &service, const uint16_t &port = canon_port);
|
2018-02-03 20:38:46 +01:00
|
|
|
hostport(const string_view &host, const uint16_t &port);
|
|
|
|
hostport(const string_view &amalgam);
|
2019-03-19 01:40:16 +01:00
|
|
|
hostport(const string_view &amalgam, verbatim_t);
|
2018-01-07 22:48:03 +01:00
|
|
|
hostport() = default;
|
|
|
|
};
|
|
|
|
|
2018-02-06 23:00:52 +01:00
|
|
|
/// Creates a host:service pair from a hostname and a service name string.
|
|
|
|
/// When passed to net::dns() this will indicate SRV resolution. If no
|
|
|
|
/// SRV record is found
|
|
|
|
/// TODO: todo: the servce is translated into its proper port.
|
|
|
|
/// TODO: now: the port 8448 is used with the hostname.
|
2018-02-03 20:38:46 +01:00
|
|
|
inline
|
|
|
|
ircd::net::hostport::hostport(const string_view &host,
|
2018-04-23 09:07:55 +02:00
|
|
|
const string_view &service,
|
|
|
|
const uint16_t &port)
|
2019-03-19 01:40:16 +01:00
|
|
|
:host{rfc3986::host(host)}
|
2018-02-03 20:38:46 +01:00
|
|
|
,service{service}
|
2018-04-23 09:07:55 +02:00
|
|
|
,port{port}
|
2019-03-19 01:40:16 +01:00
|
|
|
{}
|
2018-02-03 20:38:46 +01:00
|
|
|
|
2018-02-06 23:00:52 +01:00
|
|
|
/// Creates a host:port pair from a hostname and a port number. When
|
|
|
|
/// passed to net::dns() no SRV resolution will be done because no
|
|
|
|
/// service name has been given. (unless manually optioned when using
|
|
|
|
/// net::dns).
|
2018-02-03 20:38:46 +01:00
|
|
|
inline
|
|
|
|
ircd::net::hostport::hostport(const string_view &host,
|
|
|
|
const uint16_t &port)
|
2018-10-03 03:36:19 +02:00
|
|
|
:hostport
|
|
|
|
{
|
|
|
|
host, string_view{}, port
|
|
|
|
}
|
2018-02-03 20:38:46 +01:00
|
|
|
{}
|
|
|
|
|
2018-02-06 23:00:52 +01:00
|
|
|
/// Creates a host:service or host:port pair from the single string literally
|
|
|
|
/// containing the colon deliminated values. If the suffix is a port number
|
|
|
|
/// then the behavior for the port number constructor applies; if a service
|
|
|
|
/// string then the service constructor applies; if empty (just a hostname)
|
|
|
|
/// then service "matrix" is assumed with port 8448 fallback.
|
2018-02-03 20:38:46 +01:00
|
|
|
inline
|
|
|
|
ircd::net::hostport::hostport(const string_view &amalgam)
|
|
|
|
:host
|
|
|
|
{
|
2019-03-13 18:50:49 +01:00
|
|
|
rfc3986::host(amalgam)
|
2018-02-03 20:38:46 +01:00
|
|
|
}
|
2019-03-13 18:50:49 +01:00
|
|
|
,port
|
2018-02-03 20:38:46 +01:00
|
|
|
{
|
2019-03-13 18:50:49 +01:00
|
|
|
rfc3986::port(amalgam)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// When the amalgam has no port
|
|
|
|
if(amalgam == host)
|
2018-10-03 03:36:19 +02:00
|
|
|
{
|
2019-03-13 18:50:49 +01:00
|
|
|
// set the port to the canon_port; port=0 is bad
|
|
|
|
port = port?: canon_port;
|
2018-02-06 23:00:52 +01:00
|
|
|
return;
|
2018-10-03 03:36:19 +02:00
|
|
|
}
|
2018-02-06 23:00:52 +01:00
|
|
|
|
2019-03-13 18:50:49 +01:00
|
|
|
// or a valid integer port
|
|
|
|
if(port)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// When the port is actually a service string
|
|
|
|
const auto service
|
2018-02-06 23:00:52 +01:00
|
|
|
{
|
2019-03-13 18:50:49 +01:00
|
|
|
rsplit(amalgam, ':').second
|
|
|
|
};
|
|
|
|
|
|
|
|
if(service)
|
|
|
|
this->service = service;
|
|
|
|
else
|
|
|
|
this->port = canon_port;
|
2018-02-03 20:38:46 +01:00
|
|
|
}
|
|
|
|
|
2019-03-19 01:40:16 +01:00
|
|
|
inline
|
|
|
|
ircd::net::hostport::hostport(const string_view &amalgam,
|
|
|
|
verbatim_t)
|
|
|
|
:host
|
|
|
|
{
|
|
|
|
rfc3986::host(amalgam)
|
|
|
|
}
|
|
|
|
,service
|
|
|
|
{
|
|
|
|
amalgam != host && !rfc3986::port(amalgam)?
|
|
|
|
rsplit(amalgam, ':').second:
|
|
|
|
string_view{}
|
|
|
|
}
|
|
|
|
,port
|
|
|
|
{
|
|
|
|
rfc3986::port(amalgam)
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
2019-03-06 05:49:23 +01:00
|
|
|
inline
|
|
|
|
ircd::net::hostport::operator
|
|
|
|
bool()
|
|
|
|
const
|
|
|
|
{
|
2019-07-15 02:54:35 +02:00
|
|
|
return net::host(*this) != net::host(hostport{});
|
2019-03-06 05:49:23 +01:00
|
|
|
}
|
|
|
|
|
2018-01-22 11:06:43 +01:00
|
|
|
inline bool
|
|
|
|
ircd::net::hostport::operator!()
|
|
|
|
const
|
|
|
|
{
|
2019-03-06 05:49:23 +01:00
|
|
|
return !bool(*this);
|
2018-01-22 11:06:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::string_view &
|
2018-01-07 22:48:03 +01:00
|
|
|
ircd::net::host(hostport &hp)
|
|
|
|
{
|
|
|
|
return hp.host;
|
|
|
|
}
|
|
|
|
|
2018-02-03 20:38:46 +01:00
|
|
|
inline uint16_t &
|
|
|
|
ircd::net::port(hostport &hp)
|
|
|
|
{
|
|
|
|
return hp.port;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const ircd::string_view &
|
|
|
|
ircd::net::service(const hostport &hp)
|
|
|
|
{
|
|
|
|
return hp.service;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-02-03 20:38:46 +01:00
|
|
|
inline const uint16_t &
|
2018-01-10 10:20:16 +01:00
|
|
|
ircd::net::port(const hostport &hp)
|
|
|
|
{
|
2018-02-03 20:38:46 +01:00
|
|
|
return hp.port;
|
2018-01-10 10:20:16 +01:00
|
|
|
}
|