Listen.cxx 2.9 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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/Listener.hxx"
23
#include "config/Param.hxx"
24 25
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx"
26 27
#include "system/Error.hxx"
#include "util/RuntimeError.hxx"
28
#include "fs/AllocatedPath.hxx"
Max Kellermann's avatar
Max Kellermann committed
29 30

#include <string.h>
31
#include <assert.h>
32

33 34 35 36
#ifdef ENABLE_SYSTEMD_DAEMON
#include <systemd/sd-daemon.h>
#endif

37
#define DEFAULT_PORT	6600
Warren Dukes's avatar
Warren Dukes committed
38

39
int listen_port;
40

41 42 43 44
/**
 * Throws #std::runtime_error on error.
 */
static void
45 46
listen_add_config_param(ClientListener &listener,
			unsigned int port,
47
			const ConfigParam *param)
48
{
49
	assert(param != nullptr);
50

51
	if (0 == strcmp(param->value.c_str(), "any")) {
52
		listener.AddPort(port);
53
	} else if (param->value[0] == '/' || param->value[0] == '~') {
54
		listener.AddPath(param->GetPath());
Avuton Olrich's avatar
Avuton Olrich committed
55
	} else {
56
		listener.AddHost(param->value.c_str(), port);
Warren Dukes's avatar
Warren Dukes committed
57 58 59
	}
}

60 61
#ifdef ENABLE_SYSTEMD_DAEMON

62
static bool
63
listen_systemd_activation(ClientListener &listener)
64 65 66 67
{
	int n = sd_listen_fds(true);
	if (n <= 0) {
		if (n < 0)
68
			throw MakeErrno(-n, "sd_listen_fds() failed");
69 70 71 72 73
		return false;
	}

	for (int i = SD_LISTEN_FDS_START, end = SD_LISTEN_FDS_START + n;
	     i != end; ++i)
74
		listener.AddFD(i);
75 76 77 78

	return true;
}

79 80
#endif

81
void
82
listen_global_init(ClientListener &listener)
Avuton Olrich's avatar
Avuton Olrich committed
83
{
84
	int port = config_get_positive(ConfigOption::PORT, DEFAULT_PORT);
85
	const auto *param = config_get_param(ConfigOption::BIND_TO_ADDRESS);
86

87
#ifdef ENABLE_SYSTEMD_DAEMON
88
	if (listen_systemd_activation(listener))
89
		return;
90
#endif
91

92
	if (param != nullptr) {
93 94 95 96
		/* "bind_to_address" is configured, create listeners
		   for all values */

		do {
97
			try {
98
				listen_add_config_param(listener, port, param);
99
			} catch (...) {
100 101 102
				std::throw_with_nested(FormatRuntimeError("Failed to listen on %s (line %i)",
									  param->value.c_str(),
									  param->line));
103
			}
104
		} while ((param = param->next) != nullptr);
105 106 107 108
	} else {
		/* no "bind_to_address" configured, bind the
		   configured port on all interfaces */

109
		try {
110
			listener.AddPort(port);
111
		} catch (...) {
112
			std::throw_with_nested(FormatRuntimeError("Failed to listen on *:%d: ", port));
113
		}
114 115
	}

116
	listener.Open();
117

Max Kellermann's avatar
Max Kellermann committed
118
	listen_port = port;
119
}