Listen.cxx 3.69 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2013 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 22
#include "Listen.hxx"
#include "Main.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "Client.hxx"
24
#include "conf.h"
Max Kellermann's avatar
Max Kellermann committed
25 26

extern "C" {
27
#include "server_socket.h"
Max Kellermann's avatar
Max Kellermann committed
28
}
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 38 39
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "listen"

40
#define DEFAULT_PORT	6600
Warren Dukes's avatar
Warren Dukes committed
41

42
static struct server_socket *listen_socket;
Max Kellermann's avatar
Max Kellermann committed
43
int listen_port;
Warren Dukes's avatar
Warren Dukes committed
44

45 46 47
static void
listen_callback(int fd, const struct sockaddr *address,
		size_t address_length, int uid, G_GNUC_UNUSED void *ctx)
48
{
49
	client_new(*main_loop, *global_partition,
50
		   fd, address, address_length, uid);
51
}
52

53 54 55
static bool
listen_add_config_param(unsigned int port,
			const struct config_param *param,
56
			GError **error_r)
57
{
58 59 60
	assert(param != NULL);

	if (0 == strcmp(param->value, "any")) {
61
		return server_socket_add_port(listen_socket, port, error_r);
62
	} else if (param->value[0] == '/') {
63 64
		return server_socket_add_path(listen_socket, param->value,
					      error_r);
Avuton Olrich's avatar
Avuton Olrich committed
65
	} else {
66 67
		return server_socket_add_host(listen_socket, param->value,
					      port, error_r);
Warren Dukes's avatar
Warren Dukes committed
68 69 70
	}
}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
static bool
listen_systemd_activation(GError **error_r)
{
#ifdef ENABLE_SYSTEMD_DAEMON
	int n = sd_listen_fds(true);
	if (n <= 0) {
		if (n < 0)
			g_warning("sd_listen_fds() failed: %s",
				  g_strerror(-n));
		return false;
	}

	for (int i = SD_LISTEN_FDS_START, end = SD_LISTEN_FDS_START + n;
	     i != end; ++i)
		if (!server_socket_add_fd(listen_socket, i, error_r))
			return false;

	return true;
#else
	(void)error_r;
	return false;
#endif
}

95 96
bool
listen_global_init(GError **error_r)
Avuton Olrich's avatar
Avuton Olrich committed
97
{
98
	int port = config_get_positive(CONF_PORT, DEFAULT_PORT);
99
	const struct config_param *param =
100
		config_get_next_param(CONF_BIND_TO_ADDRESS, NULL);
101 102
	bool success;
	GError *error = NULL;
103

104 105
	listen_socket = server_socket_new(listen_callback, NULL);

106 107 108 109 110 111 112 113
	if (listen_systemd_activation(&error))
		return true;

	if (error != NULL) {
		g_propagate_error(error_r, error);
		return false;
	}

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

		do {
			success = listen_add_config_param(port, param, &error);
120 121 122 123 124 125
			if (!success) {
				g_propagate_prefixed_error(error_r, error,
							   "Failed to listen on %s (line %i): ",
							   param->value, param->line);
				return false;
			}
126 127 128 129 130 131 132 133

			param = config_get_next_param(CONF_BIND_TO_ADDRESS,
						      param);
		} while (param != NULL);
	} else {
		/* no "bind_to_address" configured, bind the
		   configured port on all interfaces */

134
		success = server_socket_add_port(listen_socket, port, error_r);
135 136 137 138 139 140
		if (!success) {
			g_propagate_prefixed_error(error_r, error,
						   "Failed to listen on *:%d: ",
						   port);
			return false;
		}
141 142
	}

143 144 145
	if (!server_socket_open(listen_socket, error_r))
		return false;

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

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

154
	assert(listen_socket != NULL);
155

156
	server_socket_free(listen_socket);
Warren Dukes's avatar
Warren Dukes committed
157
}