Listen.cxx 3.78 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "Listen.hxx"
22
#include "client/Client.hxx"
23
#include "config/Param.hxx"
24 25
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx"
26
#include "net/SocketAddress.hxx"
27
#include "event/ServerSocket.hxx"
28 29
#include "system/Error.hxx"
#include "util/RuntimeError.hxx"
30
#include "util/Domain.hxx"
31
#include "fs/AllocatedPath.hxx"
32
#include "Log.hxx"
Max Kellermann's avatar
Max Kellermann committed
33 34

#include <string.h>
35
#include <assert.h>
36

37 38 39 40
#ifdef ENABLE_SYSTEMD_DAEMON
#include <systemd/sd-daemon.h>
#endif

41
static constexpr Domain listen_domain("listen");
42

43
#define DEFAULT_PORT	6600
Warren Dukes's avatar
Warren Dukes committed
44

45
class ClientListener final : public ServerSocket {
46 47
	Partition &partition;

48
public:
49 50
	ClientListener(EventLoop &_loop, Partition &_partition)
		:ServerSocket(_loop), partition(_partition) {}
51 52

private:
53
	void OnAccept(int fd, SocketAddress address, int uid) override {
54
		client_new(GetEventLoop(), partition,
55
			   fd, address, uid);
56 57
	}
};
Warren Dukes's avatar
Warren Dukes committed
58

59 60
static ClientListener *listen_socket;
int listen_port;
61

62 63 64 65
/**
 * Throws #std::runtime_error on error.
 */
static void
66
listen_add_config_param(unsigned int port,
67
			const ConfigParam *param)
68
{
69
	assert(param != nullptr);
70

71
	if (0 == strcmp(param->value.c_str(), "any")) {
72
		listen_socket->AddPort(port);
73
	} else if (param->value[0] == '/' || param->value[0] == '~') {
74
		listen_socket->AddPath(param->GetPath());
Avuton Olrich's avatar
Avuton Olrich committed
75
	} else {
76
		listen_socket->AddHost(param->value.c_str(), port);
Warren Dukes's avatar
Warren Dukes committed
77 78 79
	}
}

80 81
#ifdef ENABLE_SYSTEMD_DAEMON

82
static bool
83
listen_systemd_activation()
84 85 86 87
{
	int n = sd_listen_fds(true);
	if (n <= 0) {
		if (n < 0)
88
			throw MakeErrno(-n, "sd_listen_fds() failed");
89 90 91 92 93
		return false;
	}

	for (int i = SD_LISTEN_FDS_START, end = SD_LISTEN_FDS_START + n;
	     i != end; ++i)
94
		listen_socket->AddFD(i);
95 96 97 98

	return true;
}

99 100
#endif

101 102
void
listen_global_init(EventLoop &loop, Partition &partition)
Avuton Olrich's avatar
Avuton Olrich committed
103
{
104
	int port = config_get_positive(ConfigOption::PORT, DEFAULT_PORT);
105
	const auto *param = config_get_param(ConfigOption::BIND_TO_ADDRESS);
106

107
	listen_socket = new ClientListener(loop, partition);
108

109
#ifdef ENABLE_SYSTEMD_DAEMON
110 111
	if (listen_systemd_activation())
		return;
112
#endif
113

114
	if (param != nullptr) {
115 116 117 118
		/* "bind_to_address" is configured, create listeners
		   for all values */

		do {
119 120 121
			try {
				listen_add_config_param(port, param);
			} catch (const std::runtime_error &e) {
122
				delete listen_socket;
123 124 125
				std::throw_with_nested(FormatRuntimeError("Failed to listen on %s (line %i)",
									  param->value.c_str(),
									  param->line));
126
			}
127
		} while ((param = param->next) != nullptr);
128 129 130 131
	} else {
		/* no "bind_to_address" configured, bind the
		   configured port on all interfaces */

132 133 134
		try {
			listen_socket->AddPort(port);
		} catch (const std::runtime_error &e) {
135
			delete listen_socket;
136
			std::throw_with_nested(FormatRuntimeError("Failed to listen on *:%d: ", port));
137
		}
138 139
	}

140 141 142
	try {
		listen_socket->Open();
	} catch (const std::runtime_error &e) {
143
		delete listen_socket;
144
		throw;
145
	}
146

Max Kellermann's avatar
Max Kellermann committed
147
	listen_port = port;
148 149
}

Max Kellermann's avatar
Max Kellermann committed
150
void listen_global_finish(void)
Avuton Olrich's avatar
Avuton Olrich committed
151
{
152
	LogDebug(listen_domain, "listen_global_finish called");
153

154
	assert(listen_socket != nullptr);
155

156
	delete listen_socket;
Warren Dukes's avatar
Warren Dukes committed
157
}