2010-10-05 20:29:41 +02:00
|
|
|
/*
|
2015-01-01 19:48:13 +01:00
|
|
|
* Copyright (C) 2003-2015 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2013-01-15 21:42:55 +01:00
|
|
|
#include "ServerSocket.hxx"
|
2015-02-13 21:16:43 +01:00
|
|
|
#include "net/StaticSocketAddress.hxx"
|
2015-02-10 20:30:10 +01:00
|
|
|
#include "net/SocketAddress.hxx"
|
2015-02-10 21:46:23 +01:00
|
|
|
#include "net/SocketUtil.hxx"
|
|
|
|
#include "net/SocketError.hxx"
|
|
|
|
#include "net/Resolver.hxx"
|
2015-07-22 10:03:36 +02:00
|
|
|
#include "net/ToString.hxx"
|
2013-01-15 22:50:49 +01:00
|
|
|
#include "event/SocketMonitor.hxx"
|
2013-08-07 10:31:31 +02:00
|
|
|
#include "system/fd_util.h"
|
2013-10-19 17:40:56 +02:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
|
|
|
#include "fs/FileSystem.hxx"
|
2014-01-07 23:33:46 +01:00
|
|
|
#include "util/Alloc.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#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>
|
2013-11-28 11:50:54 +01:00
|
|
|
#include <algorithm>
|
2013-10-19 17:40:56 +02:00
|
|
|
|
2010-10-05 20:29:41 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <winsock.h>
|
|
|
|
#else
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
class OneServerSocket final : private SocketMonitor {
|
2013-01-30 13:20:27 +01:00
|
|
|
ServerSocket &parent;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
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-02-10 20:30:10 +01:00
|
|
|
SocketAddress address;
|
2013-01-15 21:42:55 +01:00
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
public:
|
2013-01-30 13:20:27 +01:00
|
|
|
OneServerSocket(EventLoop &_loop, ServerSocket &_parent,
|
2013-01-15 22:50:49 +01:00
|
|
|
unsigned _serial,
|
2015-02-10 20:30:10 +01:00
|
|
|
SocketAddress _address)
|
2013-01-15 22:50:49 +01:00
|
|
|
:SocketMonitor(_loop),
|
|
|
|
parent(_parent), serial(_serial),
|
2015-03-05 08:14:23 +01:00
|
|
|
#ifdef HAVE_UN
|
2013-10-19 17:40:56 +02:00
|
|
|
path(AllocatedPath::Null()),
|
2015-03-05 08:14:23 +01:00
|
|
|
#endif
|
2015-02-10 20:30:10 +01:00
|
|
|
address((sockaddr *)xmemdup(_address.GetAddress(),
|
|
|
|
_address.GetSize()),
|
|
|
|
_address.GetSize())
|
2013-01-15 21:42:55 +01:00
|
|
|
{
|
2015-02-10 20:30:10 +01:00
|
|
|
assert(!_address.IsNull());
|
|
|
|
assert(_address.GetSize() > 0);
|
2013-01-15 21:42:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
OneServerSocket(const OneServerSocket &other) = delete;
|
|
|
|
OneServerSocket &operator=(const OneServerSocket &other) = delete;
|
|
|
|
|
|
|
|
~OneServerSocket() {
|
2015-02-10 20:30:10 +01:00
|
|
|
free(const_cast<struct sockaddr *>(address.GetAddress()));
|
2014-01-06 18:02:57 +01:00
|
|
|
|
|
|
|
if (IsDefined())
|
|
|
|
Close();
|
2013-01-15 21:42:55 +01:00
|
|
|
}
|
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
unsigned GetSerial() const {
|
|
|
|
return serial;
|
|
|
|
}
|
|
|
|
|
2015-03-05 08:14:23 +01:00
|
|
|
#ifdef HAVE_UN
|
2013-10-19 17:40:56 +02:00
|
|
|
void SetPath(AllocatedPath &&_path) {
|
|
|
|
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
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
bool Open(Error &error);
|
2013-01-15 21:42:55 +01:00
|
|
|
|
2013-02-04 11:44:36 +01:00
|
|
|
using SocketMonitor::IsDefined;
|
2013-01-15 22:50:49 +01:00
|
|
|
using SocketMonitor::Close;
|
2013-01-15 21:42:55 +01:00
|
|
|
|
2013-12-04 08:43:55 +01:00
|
|
|
gcc_pure
|
|
|
|
std::string ToString() const {
|
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
|
|
|
|
2013-01-15 22:50:49 +01:00
|
|
|
void SetFD(int _fd) {
|
|
|
|
SocketMonitor::Open(_fd);
|
|
|
|
SocketMonitor::ScheduleRead();
|
|
|
|
}
|
2013-01-15 22:53:14 +01:00
|
|
|
|
|
|
|
void Accept();
|
2013-01-15 22:50:49 +01:00
|
|
|
|
|
|
|
private:
|
2013-01-30 10:53:32 +01:00
|
|
|
virtual bool OnSocketReady(unsigned flags) override;
|
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
|
|
|
|
OneServerSocket::Accept()
|
|
|
|
{
|
2015-02-13 21:16:43 +01:00
|
|
|
StaticSocketAddress peer_address;
|
2013-01-15 22:53:14 +01:00
|
|
|
size_t peer_address_length = sizeof(peer_address);
|
|
|
|
int peer_fd =
|
2015-02-13 21:16:43 +01:00
|
|
|
accept_cloexec_nonblock(Get(), peer_address,
|
2013-01-15 22:53:14 +01:00
|
|
|
&peer_address_length);
|
|
|
|
if (peer_fd < 0) {
|
|
|
|
const SocketErrorMessage msg;
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(server_socket_domain,
|
|
|
|
"accept() failed: %s", (const char *)msg);
|
2013-01-15 22:53:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-13 21:16:43 +01:00
|
|
|
peer_address.SetSize(peer_address_length);
|
|
|
|
|
2013-01-15 22:53:14 +01:00
|
|
|
if (socket_keepalive(peer_fd)) {
|
|
|
|
const SocketErrorMessage msg;
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(server_socket_domain,
|
|
|
|
"Could not set TCP keepalive option: %s",
|
|
|
|
(const char *)msg);
|
2013-01-15 22:53:14 +01:00
|
|
|
}
|
|
|
|
|
2015-02-13 21:16:43 +01:00
|
|
|
parent.OnAccept(peer_fd, peer_address,
|
2015-02-10 20:30:10 +01:00
|
|
|
get_remote_uid(peer_fd));
|
2013-01-15 22:53:14 +01:00
|
|
|
}
|
|
|
|
|
2013-01-30 10:53:32 +01:00
|
|
|
bool
|
2013-01-15 22:50:49 +01:00
|
|
|
OneServerSocket::OnSocketReady(gcc_unused unsigned flags)
|
2012-02-13 21:10:36 +01:00
|
|
|
{
|
2013-01-15 22:50:49 +01:00
|
|
|
Accept();
|
2013-01-30 10:53:32 +01:00
|
|
|
return true;
|
2012-02-13 21:10:36 +01:00
|
|
|
}
|
|
|
|
|
2013-01-15 21:42:55 +01:00
|
|
|
inline bool
|
2013-08-10 18:02:44 +02:00
|
|
|
OneServerSocket::Open(Error &error)
|
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
|
|
|
|
2015-02-10 20:30:10 +01:00
|
|
|
int _fd = socket_bind_listen(address.GetFamily(),
|
2013-01-15 21:42:55 +01:00
|
|
|
SOCK_STREAM, 0,
|
2015-02-10 20:30:10 +01:00
|
|
|
address, 5,
|
2013-08-10 18:02:44 +02:00
|
|
|
error);
|
2013-01-15 21:42:55 +01:00
|
|
|
if (_fd < 0)
|
|
|
|
return false;
|
|
|
|
|
2015-03-05 08:14:23 +01:00
|
|
|
#ifdef HAVE_UN
|
2013-01-15 21:42:55 +01:00
|
|
|
/* allow everybody to connect */
|
|
|
|
|
2013-10-19 17:40:56 +02:00
|
|
|
if (!path.IsNull())
|
|
|
|
chmod(path.c_str(), 0666);
|
2015-03-05 08:14:23 +01:00
|
|
|
#endif
|
2013-01-15 21:42:55 +01:00
|
|
|
|
2014-12-04 23:33:08 +01:00
|
|
|
/* register in the EventLoop */
|
2013-01-15 21:42:55 +01:00
|
|
|
|
|
|
|
SetFD(_fd);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-30 13:20:27 +01:00
|
|
|
ServerSocket::ServerSocket(EventLoop &_loop)
|
|
|
|
:loop(_loop), next_serial(1) {}
|
2013-01-30 12:56:23 +01:00
|
|
|
|
|
|
|
/* this is just here to allow the OneServerSocket forward
|
|
|
|
declaration */
|
|
|
|
ServerSocket::~ServerSocket() {}
|
|
|
|
|
2010-10-05 20:29:41 +02:00
|
|
|
bool
|
2013-08-10 18:02:44 +02:00
|
|
|
ServerSocket::Open(Error &error)
|
2010-10-05 20:29:41 +02:00
|
|
|
{
|
2013-01-30 12:56:23 +01:00
|
|
|
OneServerSocket *good = nullptr, *bad = nullptr;
|
2013-08-10 18:02:44 +02:00
|
|
|
Error 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
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
if (bad != nullptr && i.GetSerial() != bad->GetSerial()) {
|
|
|
|
Close();
|
2013-08-10 18:02:44 +02:00
|
|
|
error = std::move(last_error);
|
2010-10-05 20:29:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error2;
|
|
|
|
if (!i.Open(error2)) {
|
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();
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(server_socket_domain,
|
|
|
|
"bind to '%s' failed: %s "
|
|
|
|
"(continuing anyway, because "
|
|
|
|
"binding to '%s' succeeded)",
|
2013-12-04 08:43:55 +01:00
|
|
|
address_string.c_str(),
|
|
|
|
error2.GetMessage(),
|
|
|
|
good_string.c_str());
|
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
|
|
|
error2.FormatPrefix("Failed to bind to '%s': ",
|
2013-12-04 08:43:55 +01:00
|
|
|
address_string.c_str());
|
2013-08-10 18:02:44 +02:00
|
|
|
|
|
|
|
last_error = std::move(error2);
|
|
|
|
}
|
|
|
|
|
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;
|
2013-08-10 18:02:44 +02:00
|
|
|
last_error.Clear();
|
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();
|
2013-08-10 18:02:44 +02:00
|
|
|
error = std::move(last_error);
|
2010-10-05 20:29:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-15 21:42:55 +01:00
|
|
|
void
|
2013-01-30 12:56:23 +01:00
|
|
|
ServerSocket::Close()
|
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
|
|
|
}
|
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
OneServerSocket &
|
2015-02-10 20:30:10 +01:00
|
|
|
ServerSocket::AddAddress(SocketAddress address)
|
2013-01-15 21:42:55 +01:00
|
|
|
{
|
2013-11-04 20:10:46 +01:00
|
|
|
sockets.emplace_back(loop, *this, next_serial,
|
2015-02-10 20:30:10 +01:00
|
|
|
address);
|
2013-01-15 21:42:55 +01:00
|
|
|
|
2013-11-04 20:10:46 +01:00
|
|
|
return sockets.back();
|
2010-10-05 20:29:41 +02:00
|
|
|
}
|
|
|
|
|
2012-02-13 20:58:57 +01:00
|
|
|
bool
|
2013-08-10 18:02:44 +02:00
|
|
|
ServerSocket::AddFD(int fd, Error &error)
|
2012-02-13 20:58:57 +01:00
|
|
|
{
|
|
|
|
assert(fd >= 0);
|
|
|
|
|
2015-02-13 21:16:43 +01:00
|
|
|
StaticSocketAddress address;
|
2013-01-29 15:05:44 +01:00
|
|
|
socklen_t address_length = sizeof(address);
|
2015-02-13 21:16:43 +01:00
|
|
|
if (getsockname(fd, address,
|
2012-02-13 20:58:57 +01:00
|
|
|
&address_length) < 0) {
|
2013-08-10 18:02:44 +02:00
|
|
|
SetSocketError(error);
|
|
|
|
error.AddPrefix("Failed to get socket address: ");
|
2012-02-13 20:58:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-13 21:16:43 +01:00
|
|
|
address.SetSize(address_length);
|
|
|
|
|
|
|
|
OneServerSocket &s = AddAddress(address);
|
2013-01-15 21:42:55 +01:00
|
|
|
s.SetFD(fd);
|
2012-02-13 20:58:57 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-05 20:29:41 +02:00
|
|
|
#ifdef HAVE_TCP
|
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
inline void
|
|
|
|
ServerSocket::AddPortIPv4(unsigned port)
|
2010-10-05 20:29:41 +02:00
|
|
|
{
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
sin.sin_port = htons(port);
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
2015-02-10 20:30:10 +01:00
|
|
|
AddAddress({(const sockaddr *)&sin, sizeof(sin)});
|
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
|
|
|
|
ServerSocket::AddPortIPv6(unsigned port)
|
2010-10-05 20:29:41 +02:00
|
|
|
{
|
|
|
|
struct sockaddr_in6 sin;
|
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
sin.sin6_port = htons(port);
|
|
|
|
sin.sin6_family = AF_INET6;
|
|
|
|
|
2015-02-10 20:30:10 +01:00
|
|
|
AddAddress({(const sockaddr *)&sin, sizeof(sin)});
|
2010-10-05 20:29:41 +02:00
|
|
|
}
|
2013-11-04 23:36:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is IPv6 supported by the kernel?
|
|
|
|
*/
|
|
|
|
gcc_pure
|
|
|
|
static bool
|
|
|
|
SupportsIPv6()
|
|
|
|
{
|
|
|
|
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 */
|
|
|
|
|
|
|
|
bool
|
2013-08-10 18:02:44 +02:00
|
|
|
ServerSocket::AddPort(unsigned port, Error &error)
|
2010-10-05 20:29:41 +02:00
|
|
|
{
|
|
|
|
#ifdef HAVE_TCP
|
|
|
|
if (port == 0 || port > 0xffff) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(server_socket_domain, "Invalid TCP port");
|
2010-10-05 20:29:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
return true;
|
|
|
|
#else /* HAVE_TCP */
|
|
|
|
(void)port;
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(server_socket_domain, "TCP support is disabled");
|
2010-10-05 20:29:41 +02:00
|
|
|
return false;
|
|
|
|
#endif /* HAVE_TCP */
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-08-10 18:02:44 +02:00
|
|
|
ServerSocket::AddHost(const char *hostname, unsigned port, Error &error)
|
2010-10-05 20:29:41 +02:00
|
|
|
{
|
|
|
|
#ifdef HAVE_TCP
|
2011-09-20 20:52:49 +02:00
|
|
|
struct addrinfo *ai = resolve_host_port(hostname, port,
|
|
|
|
AI_PASSIVE, SOCK_STREAM,
|
2013-08-10 18:02:44 +02:00
|
|
|
error);
|
2013-01-15 21:42:55 +01:00
|
|
|
if (ai == nullptr)
|
2010-10-05 20:29:41 +02:00
|
|
|
return false;
|
|
|
|
|
2013-01-15 21:42:55 +01:00
|
|
|
for (const struct addrinfo *i = ai; i != nullptr; i = i->ai_next)
|
2015-02-10 20:30:10 +01:00
|
|
|
AddAddress(SocketAddress(i->ai_addr, i->ai_addrlen));
|
2010-10-05 20:29:41 +02:00
|
|
|
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
++next_serial;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
#else /* HAVE_TCP */
|
|
|
|
(void)hostname;
|
|
|
|
(void)port;
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(server_socket_domain, "TCP support is disabled");
|
2010-10-05 20:29:41 +02:00
|
|
|
return false;
|
|
|
|
#endif /* HAVE_TCP */
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-10-19 17:40:56 +02:00
|
|
|
ServerSocket::AddPath(AllocatedPath &&path, Error &error)
|
2010-10-05 20:29:41 +02:00
|
|
|
{
|
|
|
|
#ifdef HAVE_UN
|
|
|
|
struct sockaddr_un s_un;
|
|
|
|
|
2013-10-19 17:40:56 +02:00
|
|
|
const size_t path_length = path.length();
|
2010-10-05 20:29:41 +02:00
|
|
|
if (path_length >= sizeof(s_un.sun_path)) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(server_socket_domain,
|
|
|
|
"UNIX socket path is too long");
|
2010-10-05 20:29:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-19 17:40:56 +02:00
|
|
|
RemoveFile(path);
|
2010-10-05 20:29:41 +02:00
|
|
|
|
|
|
|
s_un.sun_family = AF_UNIX;
|
2013-10-19 17:40:56 +02:00
|
|
|
memcpy(s_un.sun_path, path.c_str(), path_length + 1);
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2015-02-10 20:30:10 +01:00
|
|
|
OneServerSocket &s = AddAddress({(const sockaddr *)&s_un, sizeof(s_un)});
|
2013-10-19 17:40:56 +02:00
|
|
|
s.SetPath(std::move(path));
|
2010-10-05 20:29:41 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
#else /* !HAVE_UN */
|
|
|
|
(void)path;
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(server_socket_domain,
|
|
|
|
"UNIX domain socket support is disabled");
|
2010-10-05 20:29:41 +02:00
|
|
|
return false;
|
|
|
|
#endif /* !HAVE_UN */
|
|
|
|
}
|
|
|
|
|