2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 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"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "listen.h"
|
2010-10-05 20:29:41 +02:00
|
|
|
#include "server_socket.h"
|
2008-08-28 20:02:43 +02:00
|
|
|
#include "client.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "conf.h"
|
2009-11-10 21:14:22 +01:00
|
|
|
#include "glib_compat.h"
|
2009-11-03 21:08:48 +01:00
|
|
|
#include "main.h"
|
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
|
|
|
|
2008-12-29 17:29:04 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "listen"
|
|
|
|
|
2005-03-06 20:00:58 +01:00
|
|
|
#define DEFAULT_PORT 6600
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2010-10-05 20:29:41 +02:00
|
|
|
static struct server_socket *listen_socket;
|
2009-02-24 17:42:36 +01:00
|
|
|
int listen_port;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2010-10-05 20:29:41 +02:00
|
|
|
static void
|
|
|
|
listen_callback(int fd, const struct sockaddr *address,
|
|
|
|
size_t address_length, int uid, G_GNUC_UNUSED void *ctx)
|
2009-02-24 18:49:09 +01:00
|
|
|
{
|
2009-11-03 21:08:48 +01:00
|
|
|
client_new(global_player_control, fd, address, address_length, uid);
|
2009-02-24 18:49:09 +01:00
|
|
|
}
|
2008-10-30 18:03:18 +01:00
|
|
|
|
2009-02-24 17:51:39 +01:00
|
|
|
static bool
|
|
|
|
listen_add_config_param(unsigned int port,
|
|
|
|
const struct config_param *param,
|
2010-10-05 20:29:41 +02:00
|
|
|
GError **error_r)
|
2009-02-24 17:51:39 +01:00
|
|
|
{
|
2009-02-24 18:36:31 +01:00
|
|
|
assert(param != NULL);
|
|
|
|
|
|
|
|
if (0 == strcmp(param->value, "any")) {
|
2010-10-05 20:29:41 +02:00
|
|
|
return server_socket_add_port(listen_socket, port, error_r);
|
2009-02-24 17:51:39 +01:00
|
|
|
} else if (param->value[0] == '/') {
|
2010-10-05 20:29:41 +02:00
|
|
|
return server_socket_add_path(listen_socket, param->value,
|
|
|
|
error_r);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2010-10-05 20:29:41 +02:00
|
|
|
return server_socket_add_host(listen_socket, param->value,
|
|
|
|
port, error_r);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-24 21:40:04 +02:00
|
|
|
bool
|
|
|
|
listen_global_init(GError **error_r)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-21 08:48:02 +01:00
|
|
|
int port = config_get_positive(CONF_PORT, DEFAULT_PORT);
|
2009-01-25 16:03:49 +01:00
|
|
|
const struct config_param *param =
|
2009-01-17 20:23:27 +01:00
|
|
|
config_get_next_param(CONF_BIND_TO_ADDRESS, NULL);
|
2009-02-24 17:43:10 +01:00
|
|
|
bool success;
|
|
|
|
GError *error = NULL;
|
2007-01-11 21:41:17 +01:00
|
|
|
|
2010-10-05 20:29:41 +02:00
|
|
|
listen_socket = server_socket_new(listen_callback, NULL);
|
|
|
|
|
2009-02-24 18:36:31 +01:00
|
|
|
if (param != NULL) {
|
|
|
|
/* "bind_to_address" is configured, create listeners
|
|
|
|
for all values */
|
|
|
|
|
|
|
|
do {
|
|
|
|
success = listen_add_config_param(port, param, &error);
|
2009-09-24 21:40:04 +02:00
|
|
|
if (!success) {
|
|
|
|
g_propagate_prefixed_error(error_r, error,
|
|
|
|
"Failed to listen on %s (line %i): ",
|
|
|
|
param->value, param->line);
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-24 18:36:31 +01:00
|
|
|
|
|
|
|
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 */
|
|
|
|
|
2010-10-05 20:29:41 +02:00
|
|
|
success = server_socket_add_port(listen_socket, port, error_r);
|
2009-09-24 21:40:04 +02:00
|
|
|
if (!success) {
|
|
|
|
g_propagate_prefixed_error(error_r, error,
|
|
|
|
"Failed to listen on *:%d: ",
|
|
|
|
port);
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-24 18:36:31 +01:00
|
|
|
}
|
|
|
|
|
2010-10-05 20:29:41 +02:00
|
|
|
if (!server_socket_open(listen_socket, error_r))
|
|
|
|
return false;
|
|
|
|
|
2009-02-24 17:42:36 +01:00
|
|
|
listen_port = port;
|
2009-09-24 21:40:04 +02:00
|
|
|
return true;
|
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
|
|
|
{
|
2009-02-24 17:42:36 +01:00
|
|
|
g_debug("listen_global_finish called");
|
2004-11-03 20:42:54 +01:00
|
|
|
|
2010-10-05 20:29:41 +02:00
|
|
|
assert(listen_socket != NULL);
|
2004-11-03 20:42:54 +01:00
|
|
|
|
2010-10-05 20:29:41 +02:00
|
|
|
server_socket_free(listen_socket);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|