2010-10-05 20:29:41 +02:00
|
|
|
/*
|
2022-07-14 17:58:12 +02:00
|
|
|
* Copyright 2003-2022 The Music Player Daemon Project
|
2010-10-05 20:29:41 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2013-01-15 21:42:55 +01:00
|
|
|
#include "ServerSocket.hxx"
|
2021-06-24 20:22:48 +02:00
|
|
|
#include "lib/fmt/ExceptionFormatter.hxx"
|
2017-08-11 08:41:58 +02:00
|
|
|
#include "net/IPv4Address.hxx"
|
2018-10-30 20:19:04 +01:00
|
|
|
#include "net/IPv6Address.hxx"
|
2015-02-13 21:16:43 +01:00
|
|
|
#include "net/StaticSocketAddress.hxx"
|
2015-07-22 11:05:08 +02:00
|
|
|
#include "net/AllocatedSocketAddress.hxx"
|
2015-02-10 21:46:23 +01:00
|
|
|
#include "net/SocketUtil.hxx"
|
|
|
|
#include "net/SocketError.hxx"
|
2017-08-10 09:53:09 +02:00
|
|
|
#include "net/UniqueSocketDescriptor.hxx"
|
2015-02-10 21:46:23 +01:00
|
|
|
#include "net/Resolver.hxx"
|
2018-08-21 08:26:12 +02:00
|
|
|
#include "net/AddressInfo.hxx"
|
2015-07-22 10:03:36 +02:00
|
|
|
#include "net/ToString.hxx"
|
2020-10-14 14:24:16 +02:00
|
|
|
#include "event/SocketEvent.hxx"
|
2013-10-19 17:40:56 +02:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
2016-10-28 10:36:05 +02:00
|
|
|
#include "util/RuntimeError.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Domain.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2013-10-19 17:40:56 +02:00
|
|
|
#include <string>
|
2019-07-05 09:59:00 +02:00
|
|
|
#include <utility>
|
2013-10-19 17:40:56 +02:00
|
|
|
|
2018-10-30 20:17:24 +01:00
|
|
|
#ifdef HAVE_UN
|
|
|
|
#include <sys/stat.h>
|
2010-10-05 20:29:41 +02:00
|
|
|
#endif
|
|
|
|
|
2020-10-14 14:24:16 +02:00
|
|
|
class ServerSocket::OneServerSocket final {
|
2013-01-30 13:20:27 +01:00
|
|
|
ServerSocket &parent;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2020-10-14 14:24:16 +02:00
|
|
|
SocketEvent event;
|
|
|
|
|
2013-01-15 21:42:55 +01:00
|
|
|
const unsigned serial;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2015-03-05 08:14:23 +01:00
|
|
|
#ifdef HAVE_UN
|
2013-10-19 17:40:56 +02:00
|
|
|
AllocatedPath path;
|
2015-03-05 08:14:23 +01:00
|
|
|
#endif
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2015-07-22 11:05:08 +02:00
|
|
|
const AllocatedSocketAddress address;
|
2013-01-15 21:42:55 +01:00
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
public:
|
2015-07-22 11:05:08 +02:00
|
|
|
template<typename A>
|
2013-01-30 13:20:27 +01:00
|
|
|
OneServerSocket(EventLoop &_loop, ServerSocket &_parent,
|
2013-01-15 22:50:49 +01:00
|
|
|
unsigned _serial,
|
2018-10-30 19:57:39 +01:00
|
|
|
A &&_address) noexcept
|
2020-10-14 14:24:16 +02:00
|
|
|
:parent(_parent),
|
|
|
|
event(_loop, BIND_THIS_METHOD(OnSocketReady)),
|
|
|
|
serial(_serial),
|
2015-03-05 08:14:23 +01:00
|
|
|
#ifdef HAVE_UN
|
2018-01-17 12:17:41 +01:00
|
|
|
path(nullptr),
|
2015-03-05 08:14:23 +01:00
|
|
|
#endif
|
2015-07-22 11:05:08 +02:00
|
|
|
address(std::forward<A>(_address))
|
2013-01-15 21:42:55 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
OneServerSocket(const OneServerSocket &other) = delete;
|
|
|
|
OneServerSocket &operator=(const OneServerSocket &other) = delete;
|
|
|
|
|
2018-10-30 19:57:39 +01:00
|
|
|
~OneServerSocket() noexcept {
|
2020-10-14 16:23:13 +02:00
|
|
|
Close();
|
2013-01-15 21:42:55 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 07:43:06 +01:00
|
|
|
[[nodiscard]] unsigned GetSerial() const noexcept {
|
2013-01-30 12:56:23 +01:00
|
|
|
return serial;
|
|
|
|
}
|
|
|
|
|
2015-03-05 08:14:23 +01:00
|
|
|
#ifdef HAVE_UN
|
2018-10-30 19:57:39 +01:00
|
|
|
void SetPath(AllocatedPath &&_path) noexcept {
|
2013-10-19 17:40:56 +02:00
|
|
|
assert(path.IsNull());
|
2013-01-30 12:56:23 +01:00
|
|
|
|
2013-10-19 17:40:56 +02:00
|
|
|
path = std::move(_path);
|
2013-01-30 12:56:23 +01:00
|
|
|
}
|
2015-03-05 08:14:23 +01:00
|
|
|
#endif
|
2013-01-30 12:56:23 +01:00
|
|
|
|
2021-11-11 11:27:17 +01:00
|
|
|
[[nodiscard]] bool IsDefined() const noexcept {
|
2020-10-14 14:24:16 +02:00
|
|
|
return event.IsDefined();
|
|
|
|
}
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
void Open();
|
2013-01-15 21:42:55 +01:00
|
|
|
|
2020-10-14 14:24:16 +02:00
|
|
|
void Close() noexcept {
|
|
|
|
event.Close();
|
|
|
|
}
|
2013-01-15 21:42:55 +01:00
|
|
|
|
2021-02-04 21:26:48 +01:00
|
|
|
[[nodiscard]] [[gnu::pure]]
|
2017-05-08 14:44:49 +02:00
|
|
|
std::string ToString() const noexcept {
|
2015-07-22 10:20:57 +02:00
|
|
|
return ::ToString(address);
|
2013-12-04 08:43:55 +01:00
|
|
|
}
|
2013-01-15 21:42:55 +01:00
|
|
|
|
2018-10-30 20:05:57 +01:00
|
|
|
void SetFD(UniqueSocketDescriptor _fd) noexcept {
|
2020-10-14 14:24:16 +02:00
|
|
|
event.Open(_fd.Release());
|
|
|
|
event.ScheduleRead();
|
2013-01-15 22:50:49 +01:00
|
|
|
}
|
2013-01-15 22:53:14 +01:00
|
|
|
|
2017-05-08 14:44:49 +02:00
|
|
|
void Accept() noexcept;
|
2013-01-15 22:50:49 +01:00
|
|
|
|
|
|
|
private:
|
2020-10-14 14:24:16 +02:00
|
|
|
void OnSocketReady(unsigned flags) noexcept;
|
2010-10-05 20:29:41 +02:00
|
|
|
};
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain server_socket_domain("server_socket");
|
2010-10-05 20:29:41 +02:00
|
|
|
|
|
|
|
static int
|
|
|
|
get_remote_uid(int fd)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_STRUCT_UCRED
|
|
|
|
struct ucred cred;
|
|
|
|
socklen_t len = sizeof (cred);
|
|
|
|
|
|
|
|
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len) < 0)
|
2014-11-18 20:53:59 +01:00
|
|
|
return -1;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
|
|
|
return cred.uid;
|
|
|
|
#else
|
|
|
|
#ifdef HAVE_GETPEEREID
|
|
|
|
uid_t euid;
|
|
|
|
gid_t egid;
|
|
|
|
|
|
|
|
if (getpeereid(fd, &euid, &egid) == 0)
|
|
|
|
return euid;
|
|
|
|
#else
|
|
|
|
(void)fd;
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-01-15 22:53:14 +01:00
|
|
|
inline void
|
2018-10-30 20:16:26 +01:00
|
|
|
ServerSocket::OneServerSocket::Accept() noexcept
|
2013-01-15 22:53:14 +01:00
|
|
|
{
|
2015-02-13 21:16:43 +01:00
|
|
|
StaticSocketAddress peer_address;
|
2020-10-14 14:24:16 +02:00
|
|
|
UniqueSocketDescriptor peer_fd(event.GetSocket().AcceptNonBlock(peer_address));
|
2017-08-10 18:25:22 +02:00
|
|
|
if (!peer_fd.IsDefined()) {
|
2013-01-15 22:53:14 +01:00
|
|
|
const SocketErrorMessage msg;
|
2021-06-24 20:22:48 +02:00
|
|
|
FmtError(server_socket_domain,
|
|
|
|
"accept() failed: {}", (const char *)msg);
|
2013-01-15 22:53:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-10 19:25:02 +02:00
|
|
|
if (!peer_fd.SetKeepAlive()) {
|
2013-01-15 22:53:14 +01:00
|
|
|
const SocketErrorMessage msg;
|
2021-06-24 20:22:48 +02:00
|
|
|
FmtError(server_socket_domain,
|
|
|
|
"Could not set TCP keepalive option: {}",
|
|
|
|
(const char *)msg);
|
2013-01-15 22:53:14 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 13:22:25 +02:00
|
|
|
const auto uid = get_remote_uid(peer_fd.Get());
|
|
|
|
|
|
|
|
parent.OnAccept(std::move(peer_fd), peer_address, uid);
|
2013-01-15 22:53:14 +01:00
|
|
|
}
|
|
|
|
|
2020-10-14 14:24:16 +02:00
|
|
|
void
|
2020-03-12 20:56:11 +01:00
|
|
|
ServerSocket::OneServerSocket::OnSocketReady([[maybe_unused]] unsigned flags) noexcept
|
2012-02-13 21:10:36 +01:00
|
|
|
{
|
2013-01-15 22:50:49 +01:00
|
|
|
Accept();
|
2012-02-13 21:10:36 +01:00
|
|
|
}
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
inline void
|
2018-10-30 20:16:26 +01:00
|
|
|
ServerSocket::OneServerSocket::Open()
|
2013-01-15 21:42:55 +01:00
|
|
|
{
|
2013-01-15 22:50:49 +01:00
|
|
|
assert(!IsDefined());
|
2013-01-15 21:42:55 +01:00
|
|
|
|
2017-08-10 09:53:09 +02:00
|
|
|
auto _fd = socket_bind_listen(address.GetFamily(),
|
|
|
|
SOCK_STREAM, 0,
|
|
|
|
address, 5);
|
2013-01-15 21:42:55 +01:00
|
|
|
|
2022-01-10 17:59:38 +01:00
|
|
|
#ifdef HAVE_TCP
|
2022-01-11 20:10:42 +01:00
|
|
|
if (parent.dscp_class >= 0) {
|
|
|
|
const int family = address.GetFamily();
|
|
|
|
if ((family == AF_INET &&
|
|
|
|
!_fd.SetIntOption(IPPROTO_IP, IP_TOS, parent.dscp_class)) ||
|
|
|
|
(family == AF_INET6 &&
|
|
|
|
!_fd.SetIntOption(IPPROTO_IPV6, IPV6_TCLASS,
|
|
|
|
parent.dscp_class))) {
|
|
|
|
const SocketErrorMessage msg;
|
|
|
|
FmtError(server_socket_domain,
|
|
|
|
"Could not set DSCP class: {}",
|
|
|
|
(const char *)msg);
|
|
|
|
}
|
2022-01-10 17:59:38 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-08-14 22:53:19 +02:00
|
|
|
#ifdef HAVE_UN
|
|
|
|
/* allow everybody to connect */
|
|
|
|
|
|
|
|
if (!path.IsNull())
|
|
|
|
chmod(path.c_str(), 0666);
|
|
|
|
#endif
|
|
|
|
|
2018-08-07 21:30:25 +02:00
|
|
|
/* register in the EventLoop */
|
2013-01-15 21:42:55 +01:00
|
|
|
|
2018-10-30 20:05:57 +01:00
|
|
|
SetFD(std::move(_fd));
|
2013-01-15 21:42:55 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 19:57:39 +01:00
|
|
|
ServerSocket::ServerSocket(EventLoop &_loop) noexcept
|
2018-10-30 20:11:58 +01:00
|
|
|
:loop(_loop) {}
|
2013-01-30 12:56:23 +01:00
|
|
|
|
|
|
|
/* this is just here to allow the OneServerSocket forward
|
|
|
|
declaration */
|
2018-10-30 19:57:39 +01:00
|
|
|
ServerSocket::~ServerSocket() noexcept = default;
|
2013-01-30 12:56:23 +01:00
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
void
|
|
|
|
ServerSocket::Open()
|
2010-10-05 20:29:41 +02:00
|
|
|
{
|
2013-01-30 12:56:23 +01:00
|
|
|
OneServerSocket *good = nullptr, *bad = nullptr;
|
2016-10-28 10:36:05 +02:00
|
|
|
std::exception_ptr last_error;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
for (auto &i : sockets) {
|
|
|
|
assert(i.GetSerial() > 0);
|
2013-11-04 20:16:28 +01:00
|
|
|
assert(good == nullptr || i.GetSerial() >= good->GetSerial());
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2018-10-30 20:36:42 +01:00
|
|
|
if (i.IsDefined())
|
|
|
|
/* already open - was probably added by
|
|
|
|
AddFD() */
|
|
|
|
continue;
|
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
if (bad != nullptr && i.GetSerial() != bad->GetSerial()) {
|
|
|
|
Close();
|
2016-10-28 10:36:05 +02:00
|
|
|
std::rethrow_exception(last_error);
|
2010-10-05 20:29:41 +02:00
|
|
|
}
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
try {
|
|
|
|
i.Open();
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
2013-01-30 12:56:23 +01:00
|
|
|
if (good != nullptr && good->GetSerial() == i.GetSerial()) {
|
2013-12-04 08:43:55 +01:00
|
|
|
const auto address_string = i.ToString();
|
|
|
|
const auto good_string = good->ToString();
|
2021-06-24 20:22:48 +02:00
|
|
|
FmtError(server_socket_domain,
|
|
|
|
"bind to '{}' failed "
|
|
|
|
"(continuing anyway, because "
|
|
|
|
"binding to '{}' succeeded): {}",
|
|
|
|
address_string,
|
|
|
|
good_string,
|
|
|
|
std::current_exception());
|
2013-01-15 21:42:55 +01:00
|
|
|
} else if (bad == nullptr) {
|
|
|
|
bad = &i;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2013-12-04 08:43:55 +01:00
|
|
|
const auto address_string = i.ToString();
|
2013-08-10 18:02:44 +02:00
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
try {
|
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to bind to '%s'",
|
|
|
|
address_string.c_str()));
|
|
|
|
} catch (...) {
|
|
|
|
last_error = std::current_exception();
|
|
|
|
}
|
2013-08-10 18:02:44 +02:00
|
|
|
}
|
|
|
|
|
2010-10-05 20:29:41 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mark this socket as "good", and clear previous
|
|
|
|
errors */
|
|
|
|
|
2013-01-15 21:42:55 +01:00
|
|
|
good = &i;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2013-01-15 21:42:55 +01:00
|
|
|
if (bad != nullptr) {
|
|
|
|
bad = nullptr;
|
2016-10-28 10:36:05 +02:00
|
|
|
last_error = nullptr;
|
2010-10-05 20:29:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 21:42:55 +01:00
|
|
|
if (bad != nullptr) {
|
2013-01-30 12:56:23 +01:00
|
|
|
Close();
|
2016-10-28 10:36:05 +02:00
|
|
|
std::rethrow_exception(last_error);
|
2010-10-05 20:29:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 21:42:55 +01:00
|
|
|
void
|
2018-10-30 19:57:39 +01:00
|
|
|
ServerSocket::Close() noexcept
|
2013-01-15 21:42:55 +01:00
|
|
|
{
|
|
|
|
for (auto &i : sockets)
|
2013-02-04 11:44:36 +01:00
|
|
|
if (i.IsDefined())
|
|
|
|
i.Close();
|
2013-01-15 21:42:55 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 20:43:52 +01:00
|
|
|
template<typename A>
|
2018-10-30 20:16:26 +01:00
|
|
|
ServerSocket::OneServerSocket &
|
2018-10-30 20:43:52 +01:00
|
|
|
ServerSocket::AddAddress(A &&address) noexcept
|
2013-01-15 21:42:55 +01:00
|
|
|
{
|
2013-11-04 20:10:46 +01:00
|
|
|
sockets.emplace_back(loop, *this, next_serial,
|
2018-10-30 20:43:52 +01:00
|
|
|
std::forward<A>(address));
|
2015-07-22 11:05:08 +02:00
|
|
|
|
|
|
|
return sockets.back();
|
|
|
|
}
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
void
|
2018-10-30 20:05:57 +01:00
|
|
|
ServerSocket::AddFD(UniqueSocketDescriptor fd)
|
2012-02-13 20:58:57 +01:00
|
|
|
{
|
2018-10-30 20:05:57 +01:00
|
|
|
assert(fd.IsDefined());
|
2012-02-13 20:58:57 +01:00
|
|
|
|
2017-08-10 18:25:22 +02:00
|
|
|
StaticSocketAddress address = fd.GetLocalAddress();
|
|
|
|
if (!address.IsDefined())
|
|
|
|
throw MakeSocketError("Failed to get socket address");
|
2015-02-13 21:16:43 +01:00
|
|
|
|
|
|
|
OneServerSocket &s = AddAddress(address);
|
2018-10-30 20:05:57 +01:00
|
|
|
s.SetFD(std::move(fd));
|
2012-02-13 20:58:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 20:44:56 +01:00
|
|
|
void
|
|
|
|
ServerSocket::AddFD(UniqueSocketDescriptor fd,
|
|
|
|
AllocatedSocketAddress &&address) noexcept
|
|
|
|
{
|
|
|
|
assert(fd.IsDefined());
|
|
|
|
assert(!address.IsNull());
|
|
|
|
assert(address.IsDefined());
|
|
|
|
|
|
|
|
OneServerSocket &s = AddAddress(std::move(address));
|
|
|
|
s.SetFD(std::move(fd));
|
|
|
|
}
|
|
|
|
|
2010-10-05 20:29:41 +02:00
|
|
|
#ifdef HAVE_TCP
|
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
inline void
|
2018-10-30 19:57:39 +01:00
|
|
|
ServerSocket::AddPortIPv4(unsigned port) noexcept
|
2010-10-05 20:29:41 +02:00
|
|
|
{
|
2017-08-11 08:41:58 +02:00
|
|
|
AddAddress(IPv4Address(port));
|
2010-10-05 20:29:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_IPV6
|
2013-11-04 23:36:00 +01:00
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
inline void
|
2018-10-30 19:57:39 +01:00
|
|
|
ServerSocket::AddPortIPv6(unsigned port) noexcept
|
2010-10-05 20:29:41 +02:00
|
|
|
{
|
2018-10-30 20:19:04 +01:00
|
|
|
AddAddress(IPv6Address(port));
|
2010-10-05 20:29:41 +02:00
|
|
|
}
|
2013-11-04 23:36:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is IPv6 supported by the kernel?
|
|
|
|
*/
|
2021-02-04 21:26:48 +01:00
|
|
|
[[gnu::pure]]
|
2013-11-04 23:36:00 +01:00
|
|
|
static bool
|
2017-05-08 14:44:49 +02:00
|
|
|
SupportsIPv6() noexcept
|
2013-11-04 23:36:00 +01:00
|
|
|
{
|
|
|
|
int fd = socket(AF_INET6, SOCK_STREAM, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-05 20:29:41 +02:00
|
|
|
#endif /* HAVE_IPV6 */
|
|
|
|
|
|
|
|
#endif /* HAVE_TCP */
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
void
|
|
|
|
ServerSocket::AddPort(unsigned port)
|
2010-10-05 20:29:41 +02:00
|
|
|
{
|
|
|
|
#ifdef HAVE_TCP
|
2016-10-28 10:36:05 +02:00
|
|
|
if (port == 0 || port > 0xffff)
|
|
|
|
throw std::runtime_error("Invalid TCP port");
|
2010-10-05 20:29:41 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_IPV6
|
2013-11-04 23:36:00 +01:00
|
|
|
if (SupportsIPv6())
|
|
|
|
AddPortIPv6(port);
|
2010-10-05 20:29:41 +02:00
|
|
|
#endif
|
2013-01-30 12:56:23 +01:00
|
|
|
AddPortIPv4(port);
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
++next_serial;
|
2010-10-05 20:29:41 +02:00
|
|
|
#else /* HAVE_TCP */
|
|
|
|
(void)port;
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
throw std::runtime_error("TCP support is disabled");
|
2010-10-05 20:29:41 +02:00
|
|
|
#endif /* HAVE_TCP */
|
|
|
|
}
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
void
|
|
|
|
ServerSocket::AddHost(const char *hostname, unsigned port)
|
2010-10-05 20:29:41 +02:00
|
|
|
{
|
|
|
|
#ifdef HAVE_TCP
|
2018-08-21 08:26:12 +02:00
|
|
|
for (const auto &i : Resolve(hostname, port,
|
|
|
|
AI_PASSIVE, SOCK_STREAM))
|
|
|
|
AddAddress(i);
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
++next_serial;
|
2010-10-05 20:29:41 +02:00
|
|
|
#else /* HAVE_TCP */
|
|
|
|
(void)hostname;
|
|
|
|
(void)port;
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
throw std::runtime_error("TCP support is disabled");
|
2010-10-05 20:29:41 +02:00
|
|
|
#endif /* HAVE_TCP */
|
|
|
|
}
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
void
|
|
|
|
ServerSocket::AddPath(AllocatedPath &&path)
|
2010-10-05 20:29:41 +02:00
|
|
|
{
|
|
|
|
#ifdef HAVE_UN
|
2016-08-15 22:25:15 +02:00
|
|
|
unlink(path.c_str());
|
2015-09-29 19:39:06 +02:00
|
|
|
|
2015-07-22 11:01:44 +02:00
|
|
|
AllocatedSocketAddress address;
|
|
|
|
address.SetLocal(path.c_str());
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2015-07-22 11:01:44 +02:00
|
|
|
OneServerSocket &s = AddAddress(std::move(address));
|
2013-10-19 17:40:56 +02:00
|
|
|
s.SetPath(std::move(path));
|
2010-10-05 20:29:41 +02:00
|
|
|
#else /* !HAVE_UN */
|
|
|
|
(void)path;
|
|
|
|
|
2019-03-20 12:57:26 +01:00
|
|
|
throw std::runtime_error("Local socket support is disabled");
|
2010-10-05 20:29:41 +02:00
|
|
|
#endif /* !HAVE_UN */
|
|
|
|
}
|
|
|
|
|
2019-02-25 13:01:42 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
ServerSocket::AddAbstract(const char *name)
|
|
|
|
{
|
2019-03-20 13:09:16 +01:00
|
|
|
#if !defined(__linux__)
|
|
|
|
(void)name;
|
|
|
|
|
|
|
|
throw std::runtime_error("Abstract sockets are only available on Linux");
|
|
|
|
#elif !defined(HAVE_UN)
|
2019-03-20 13:06:09 +01:00
|
|
|
(void)name;
|
|
|
|
|
|
|
|
throw std::runtime_error("Local socket support is disabled");
|
|
|
|
#else
|
2019-02-25 13:01:42 +01:00
|
|
|
assert(name != nullptr);
|
|
|
|
assert(*name == '@');
|
|
|
|
|
|
|
|
AllocatedSocketAddress address;
|
|
|
|
address.SetLocal(name);
|
|
|
|
|
|
|
|
AddAddress(std::move(address));
|
2019-03-20 13:06:09 +01:00
|
|
|
#endif
|
2019-02-25 13:01:42 +01:00
|
|
|
}
|