Listen.cxx 3.89 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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
#include "util/Error.hxx"
29
#include "util/Domain.hxx"
30
#include "fs/AllocatedPath.hxx"
31
#include "Log.hxx"
Max Kellermann's avatar
Max Kellermann committed
32 33

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

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

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

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

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

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

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

58 59
static ClientListener *listen_socket;
int listen_port;
60

61 62 63
static bool
listen_add_config_param(unsigned int port,
			const struct config_param *param,
64
			Error &error_r)
65
{
66
	assert(param != nullptr);
67

68
	if (0 == strcmp(param->value.c_str(), "any")) {
69
		return listen_socket->AddPort(port, error_r);
70
	} else if (param->value[0] == '/' || param->value[0] == '~') {
71 72 73
		auto path = config_parse_path(param, error_r);
		return !path.IsNull() &&
			listen_socket->AddPath(std::move(path), error_r);
Avuton Olrich's avatar
Avuton Olrich committed
74
	} else {
75 76
		return listen_socket->AddHost(param->value.c_str(), port,
					      error_r);
Warren Dukes's avatar
Warren Dukes committed
77 78 79
	}
}

80 81
#ifdef ENABLE_SYSTEMD_DAEMON

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

	for (int i = SD_LISTEN_FDS_START, end = SD_LISTEN_FDS_START + n;
	     i != end; ++i)
95
		if (!listen_socket->AddFD(i, error_r))
96 97 98 99 100
			return false;

	return true;
}

101 102
#endif

103
bool
104
listen_global_init(EventLoop &loop, Partition &partition, Error &error)
Avuton Olrich's avatar
Avuton Olrich committed
105
{
106
	int port = config_get_positive(ConfigOption::PORT, DEFAULT_PORT);
107
	const struct config_param *param =
108
		config_get_param(ConfigOption::BIND_TO_ADDRESS);
109

110
	listen_socket = new ClientListener(loop, partition);
111

112
#ifdef ENABLE_SYSTEMD_DAEMON
113
	if (listen_systemd_activation(error))
114 115
		return true;

116
	if (error.IsDefined())
117
		return false;
118
#endif
119

120
	if (param != nullptr) {
121 122 123 124
		/* "bind_to_address" is configured, create listeners
		   for all values */

		do {
125
			if (!listen_add_config_param(port, param, error)) {
126
				delete listen_socket;
127
				error.FormatPrefix("Failed to listen on %s (line %i): ",
128 129
						   param->value.c_str(),
						   param->line);
130 131
				return false;
			}
132
		} while ((param = param->next) != nullptr);
133 134 135 136
	} else {
		/* no "bind_to_address" configured, bind the
		   configured port on all interfaces */

137
		if (!listen_socket->AddPort(port, error)) {
138
			delete listen_socket;
139
			error.FormatPrefix("Failed to listen on *:%d: ", port);
140 141
			return false;
		}
142 143
	}

144
	if (!listen_socket->Open(error)) {
145
		delete listen_socket;
146
		return false;
147
	}
148

Max Kellermann's avatar
Max Kellermann committed
149
	listen_port = port;
150
	return true;
151 152
}

Max Kellermann's avatar
Max Kellermann committed
153
void listen_global_finish(void)
Avuton Olrich's avatar
Avuton Olrich committed
154
{
155
	LogDebug(listen_domain, "listen_global_finish called");
156

157
	assert(listen_socket != nullptr);
158

159
	delete listen_socket;
Warren Dukes's avatar
Warren Dukes committed
160
}