2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-03 11:05:44 +01:00
|
|
|
#include "Listen.hxx"
|
2014-01-24 00:26:53 +01:00
|
|
|
#include "client/Client.hxx"
|
2015-01-21 21:14:25 +01:00
|
|
|
#include "config/Param.hxx"
|
2014-01-24 00:20:01 +01:00
|
|
|
#include "config/ConfigGlobal.hxx"
|
|
|
|
#include "config/ConfigOption.hxx"
|
2015-02-10 20:30:10 +01:00
|
|
|
#include "net/SocketAddress.hxx"
|
2013-01-30 13:39:12 +01:00
|
|
|
#include "event/ServerSocket.hxx"
|
2016-10-28 10:36:05 +02:00
|
|
|
#include "system/Error.hxx"
|
|
|
|
#include "util/RuntimeError.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "util/Domain.hxx"
|
2013-10-17 21:59:35 +02:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2008-12-29 17:28:32 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
2009-02-24 18:36:31 +01:00
|
|
|
#include <assert.h>
|
2008-12-30 19:07:10 +01:00
|
|
|
|
2012-02-13 20:48:51 +01:00
|
|
|
#ifdef ENABLE_SYSTEMD_DAEMON
|
|
|
|
#include <systemd/sd-daemon.h>
|
|
|
|
#endif
|
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
static constexpr Domain listen_domain("listen");
|
2008-12-29 17:29:04 +01:00
|
|
|
|
2005-03-06 20:00:58 +01:00
|
|
|
#define DEFAULT_PORT 6600
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-01-30 13:20:27 +01:00
|
|
|
class ClientListener final : public ServerSocket {
|
2014-02-05 23:16:50 +01:00
|
|
|
Partition &partition;
|
|
|
|
|
2013-01-30 13:20:27 +01:00
|
|
|
public:
|
2014-02-05 23:16:50 +01:00
|
|
|
ClientListener(EventLoop &_loop, Partition &_partition)
|
|
|
|
:ServerSocket(_loop), partition(_partition) {}
|
2013-01-30 13:20:27 +01:00
|
|
|
|
|
|
|
private:
|
2015-02-10 20:30:10 +01:00
|
|
|
void OnAccept(int fd, SocketAddress address, int uid) override {
|
2014-02-05 23:16:50 +01:00
|
|
|
client_new(GetEventLoop(), partition,
|
2015-02-10 20:30:10 +01:00
|
|
|
fd, address, uid);
|
2013-01-30 13:20:27 +01:00
|
|
|
}
|
|
|
|
};
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-01-30 13:20:27 +01:00
|
|
|
static ClientListener *listen_socket;
|
|
|
|
int listen_port;
|
2008-10-30 18:03:18 +01:00
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
/**
|
|
|
|
* Throws #std::runtime_error on error.
|
|
|
|
*/
|
|
|
|
static void
|
2009-02-24 17:51:39 +01:00
|
|
|
listen_add_config_param(unsigned int port,
|
2016-10-28 10:36:05 +02:00
|
|
|
const ConfigParam *param)
|
2009-02-24 17:51:39 +01:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(param != nullptr);
|
2009-02-24 18:36:31 +01:00
|
|
|
|
2013-10-15 22:32:39 +02:00
|
|
|
if (0 == strcmp(param->value.c_str(), "any")) {
|
2016-10-28 10:36:05 +02:00
|
|
|
listen_socket->AddPort(port);
|
2013-08-25 15:51:41 +02:00
|
|
|
} else if (param->value[0] == '/' || param->value[0] == '~') {
|
2016-10-28 10:36:05 +02:00
|
|
|
listen_socket->AddPath(param->GetPath());
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2016-10-28 10:36:05 +02:00
|
|
|
listen_socket->AddHost(param->value.c_str(), port);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 23:49:34 +01:00
|
|
|
#ifdef ENABLE_SYSTEMD_DAEMON
|
|
|
|
|
2012-02-13 20:48:51 +01:00
|
|
|
static bool
|
2016-10-28 10:36:05 +02:00
|
|
|
listen_systemd_activation()
|
2012-02-13 20:48:51 +01:00
|
|
|
{
|
|
|
|
int n = sd_listen_fds(true);
|
|
|
|
if (n <= 0) {
|
|
|
|
if (n < 0)
|
2016-10-28 10:36:05 +02:00
|
|
|
throw MakeErrno(-n, "sd_listen_fds() failed");
|
2012-02-13 20:48:51 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = SD_LISTEN_FDS_START, end = SD_LISTEN_FDS_START + n;
|
|
|
|
i != end; ++i)
|
2016-10-28 10:36:05 +02:00
|
|
|
listen_socket->AddFD(i);
|
2012-02-13 20:48:51 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-19 23:49:34 +01:00
|
|
|
#endif
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
void
|
|
|
|
listen_global_init(EventLoop &loop, Partition &partition)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2015-01-21 22:36:13 +01:00
|
|
|
int port = config_get_positive(ConfigOption::PORT, DEFAULT_PORT);
|
2016-10-28 11:38:37 +02:00
|
|
|
const auto *param = config_get_param(ConfigOption::BIND_TO_ADDRESS);
|
2007-01-11 21:41:17 +01:00
|
|
|
|
2014-02-05 23:16:50 +01:00
|
|
|
listen_socket = new ClientListener(loop, partition);
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2014-02-19 23:49:34 +01:00
|
|
|
#ifdef ENABLE_SYSTEMD_DAEMON
|
2016-10-28 10:36:05 +02:00
|
|
|
if (listen_systemd_activation())
|
|
|
|
return;
|
2014-02-19 23:49:34 +01:00
|
|
|
#endif
|
2012-02-13 20:48:51 +01:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
if (param != nullptr) {
|
2009-02-24 18:36:31 +01:00
|
|
|
/* "bind_to_address" is configured, create listeners
|
|
|
|
for all values */
|
|
|
|
|
|
|
|
do {
|
2016-10-28 10:36:05 +02:00
|
|
|
try {
|
|
|
|
listen_add_config_param(port, param);
|
|
|
|
} catch (const std::runtime_error &e) {
|
2013-01-30 12:56:23 +01:00
|
|
|
delete listen_socket;
|
2016-10-28 10:36:05 +02:00
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to listen on %s (line %i)",
|
|
|
|
param->value.c_str(),
|
|
|
|
param->line));
|
2009-09-24 21:40:04 +02:00
|
|
|
}
|
2014-02-19 23:57:55 +01:00
|
|
|
} while ((param = param->next) != nullptr);
|
2009-02-24 18:36:31 +01:00
|
|
|
} else {
|
|
|
|
/* no "bind_to_address" configured, bind the
|
|
|
|
configured port on all interfaces */
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
try {
|
|
|
|
listen_socket->AddPort(port);
|
|
|
|
} catch (const std::runtime_error &e) {
|
2013-01-30 12:56:23 +01:00
|
|
|
delete listen_socket;
|
2016-10-28 10:36:05 +02:00
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to listen on *:%d: ", port));
|
2009-09-24 21:40:04 +02:00
|
|
|
}
|
2009-02-24 18:36:31 +01:00
|
|
|
}
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
try {
|
|
|
|
listen_socket->Open();
|
|
|
|
} catch (const std::runtime_error &e) {
|
2013-01-30 12:56:23 +01:00
|
|
|
delete listen_socket;
|
2016-10-28 10:36:05 +02:00
|
|
|
throw;
|
2013-01-30 12:56:23 +01:00
|
|
|
}
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2009-02-24 17:42:36 +01:00
|
|
|
listen_port = port;
|
2004-11-03 20:42:54 +01:00
|
|
|
}
|
|
|
|
|
2009-02-24 17:42:36 +01:00
|
|
|
void listen_global_finish(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-09-27 22:31:24 +02:00
|
|
|
LogDebug(listen_domain, "listen_global_finish called");
|
2004-11-03 20:42:54 +01:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(listen_socket != nullptr);
|
2004-11-03 20:42:54 +01:00
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
delete listen_socket;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|