listen.c 7.27 KB
Newer Older
Warren Dukes's avatar
Warren Dukes committed
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
Warren Dukes's avatar
Warren Dukes committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * This project's homepage is: http://www.musicpd.org
 *
 * 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.
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "listen.h"
20
#include "client.h"
Warren Dukes's avatar
Warren Dukes committed
21
#include "conf.h"
22
#include "utils.h"
Max Kellermann's avatar
Max Kellermann committed
23 24 25 26 27 28
#include "config.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
29 30 31
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
32 33 34 35 36

#ifdef WIN32
#include <ws2tcpip.h>
#include <winsock.h>
#else
Max Kellermann's avatar
Max Kellermann committed
37
#include <netinet/in.h>
38
#include <sys/socket.h>
Max Kellermann's avatar
Max Kellermann committed
39 40
#include <sys/un.h>
#include <netdb.h>
41
#endif
Max Kellermann's avatar
Max Kellermann committed
42

43 44 45
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "listen"

Warren Dukes's avatar
Warren Dukes committed
46 47
#define MAXHOSTNAME 	1024

48 49 50
#define ALLOW_REUSE	1

#define DEFAULT_PORT	6600
Warren Dukes's avatar
Warren Dukes committed
51

52
#define BINDERROR() do { \
53 54 55
	g_error("unable to bind port %u: %s; " \
		"maybe MPD is still running?", \
		port, strerror(errno)); \
56 57
} while (0);

58 59 60 61
struct listen_socket {
	struct listen_socket *next;

	int fd;
62 63

	guint source_id;
64 65 66
};

static struct listen_socket *listen_sockets;
67
int boundPort;
Warren Dukes's avatar
Warren Dukes committed
68

69 70 71
static gboolean
listen_in_event(GIOChannel *source, GIOCondition condition, gpointer data);

72 73
static int establishListen(int pf, const struct sockaddr *addrp,
			   socklen_t addrlen)
74
{
Warren Dukes's avatar
Warren Dukes committed
75
	int sock;
76
	int allowReuse = ALLOW_REUSE;
77
#ifdef HAVE_STRUCT_UCRED
78
	int passcred = 1;
79
#endif
80
	struct listen_socket *ls;
81
	GIOChannel *channel;
82

83
	if ((sock = socket(pf, SOCK_STREAM, 0)) < 0)
84
		g_error("socket < 0");
85

Avuton Olrich's avatar
Avuton Olrich committed
86 87
	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&allowReuse,
		       sizeof(allowReuse)) < 0) {
88
		g_error("problems setsockopt'ing: %s", strerror(errno));
89 90
	}

Avuton Olrich's avatar
Avuton Olrich committed
91
	if (bind(sock, addrp, addrlen) < 0) {
92 93
		close(sock);
		return -1;
94
	}
Avuton Olrich's avatar
Avuton Olrich committed
95

96
	if (listen(sock, 5) < 0)
97
		g_error("problems listen'ing: %s", strerror(errno));
98

99
#ifdef HAVE_STRUCT_UCRED
100 101 102
	setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &passcred, sizeof(passcred));
#endif

103 104
	ls = g_new(struct listen_socket, 1);
	ls->fd = sock;
105

106
	channel = g_io_channel_unix_new(sock);
107 108
	ls->source_id = g_io_add_watch(channel, G_IO_IN,
				       listen_in_event, GINT_TO_POINTER(sock));
109 110
	g_io_channel_unref(channel);

111 112 113
	ls->next = listen_sockets;
	listen_sockets = ls;

114
	return 0;
115 116
}

117
#ifdef HAVE_IPV6
118 119 120 121 122 123 124 125 126
static bool ipv6Supported(void)
{
	int s;
	s = socket(AF_INET6, SOCK_STREAM, 0);
	if (s == -1)
		return false;
	close(s);
	return true;
}
127
#endif
128

129
static void
130
parseListenConfigParam(G_GNUC_UNUSED unsigned int port,
131
		       const struct config_param *param)
Avuton Olrich's avatar
Avuton Olrich committed
132
{
133
	const struct sockaddr *addrp;
134
	socklen_t addrlen;
135
#ifdef HAVE_TCP
136
	struct sockaddr_in sin4;
Warren Dukes's avatar
Warren Dukes committed
137 138
#ifdef HAVE_IPV6
	struct sockaddr_in6 sin6;
139
	int useIpv6 = ipv6Supported();
Warren Dukes's avatar
Warren Dukes committed
140 141 142 143 144

	memset(&sin6, 0, sizeof(struct sockaddr_in6));
	sin6.sin6_port = htons(port);
	sin6.sin6_family = AF_INET6;
#endif
145 146 147
	memset(&sin4, 0, sizeof(struct sockaddr_in));
	sin4.sin_port = htons(port);
	sin4.sin_family = AF_INET;
148
#endif /* HAVE_TCP */
149

Avuton Olrich's avatar
Avuton Olrich committed
150
	if (!param || 0 == strcmp(param->value, "any")) {
151
#ifdef HAVE_TCP
152
		g_debug("binding to any address");
153 154 155
#ifdef HAVE_IPV6
		if (useIpv6) {
			sin6.sin6_addr = in6addr_any;
156
			addrp = (const struct sockaddr *)&sin6;
157
			addrlen = sizeof(struct sockaddr_in6);
158
			if (establishListen(PF_INET6, addrp, addrlen) < 0)
159 160 161
				BINDERROR();
		}
#endif
162
		sin4.sin_addr.s_addr = INADDR_ANY;
163
		addrp = (const struct sockaddr *)&sin4;
164
		addrlen = sizeof(struct sockaddr_in);
165
#ifdef HAVE_IPV6
166
		if ((establishListen(PF_INET, addrp, addrlen) < 0) && !useIpv6) {
167
#else
168
		if (establishListen(PF_INET, addrp, addrlen) < 0) {
169
#endif
170 171
			BINDERROR();
		}
172
#else /* HAVE_TCP */
173
		g_error("TCP support is disabled");
174 175
#endif /* HAVE_TCP */
#ifdef HAVE_UN
176 177
	} else if (param->value[0] == '/') {
		size_t path_length;
178
		struct sockaddr_un s_un;
179 180

		path_length = strlen(param->value);
181
		if (path_length >= sizeof(s_un.sun_path))
182
			g_error("unix socket path is too long");
183

184 185
		unlink(param->value);

186 187
		s_un.sun_family = AF_UNIX;
		memcpy(s_un.sun_path, param->value, path_length + 1);
188

189 190
		addrp = (const struct sockaddr *)&s_un;
		addrlen = sizeof(s_un);
191

192
		if (establishListen(PF_UNIX, addrp, addrlen) < 0)
193 194
			g_error("unable to bind to %s: %s",
				param->value, strerror(errno));
195 196 197 198

		/* allow everybody to connect */
		chmod(param->value, 0666);

199
#endif /* HAVE_UN */
Avuton Olrich's avatar
Avuton Olrich committed
200
	} else {
201
#ifdef HAVE_TCP
202
#ifndef WIN32
203 204 205 206
		struct addrinfo hints, *ai, *i;
		char service[20];
		int ret;

207
		g_debug("binding to address for %s", param->value);
Warren Dukes's avatar
Warren Dukes committed
208

209
		memset(&hints, 0, sizeof(hints));
210 211 212 213
		hints.ai_flags = AI_PASSIVE;
#ifdef AI_ADDRCONFIG
		hints.ai_flags |= AI_ADDRCONFIG;
#endif
214 215 216 217
		hints.ai_family = PF_UNSPEC;
		hints.ai_socktype = SOCK_STREAM;
		hints.ai_protocol = IPPROTO_TCP;

218
		g_snprintf(service, sizeof(service), "%u", port);
219 220 221

		ret = getaddrinfo(param->value, service, &hints, &ai);
		if (ret != 0)
222 223
			g_error("can't lookup host \"%s\" at line %i: %s",
				param->value, param->line, gai_strerror(ret));
224 225

		for (i = ai; i != NULL; i = i->ai_next)
226 227
			if (establishListen(i->ai_family, i->ai_addr,
					    i->ai_addrlen) < 0)
228 229 230
				BINDERROR();

		freeaddrinfo(ai);
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
#else /* WIN32 */
		const struct hostent *he;

		g_debug("binding to address for %s", param->value);

		he = gethostbyname(param->value);
		if (he == NULL)
			g_error("can't lookup host \"%s\" at line %i",
				param->value, param->line);

		if (he->h_addrtype != AF_INET)
			g_error("IPv4 address expected for host \"%s\" at line %i",
				param->value, param->line);

		if (establishListen(AF_INET, he->h_addr, he->h_length) < 0)
			BINDERROR();
#endif /* !WIN32 */
248
#else /* HAVE_TCP */
249
		g_error("TCP support is disabled");
250
#endif /* HAVE_TCP */
Warren Dukes's avatar
Warren Dukes committed
251 252 253
	}
}

Avuton Olrich's avatar
Avuton Olrich committed
254 255
void listenOnPort(void)
{
256
	int port = config_get_positive(CONF_PORT, DEFAULT_PORT);
257
	const struct config_param *param =
258
		config_get_next_param(CONF_BIND_TO_ADDRESS, NULL);
259

260
	do {
Avuton Olrich's avatar
Avuton Olrich committed
261
		parseListenConfigParam(port, param);
262
	} while ((param = config_get_next_param(CONF_BIND_TO_ADDRESS, param)));
263 264
}

Avuton Olrich's avatar
Avuton Olrich committed
265 266
void closeAllListenSockets(void)
{
267
	g_debug("closeAllListenSockets called");
268

269 270 271
	while (listen_sockets != NULL) {
		struct listen_socket *ls = listen_sockets;
		listen_sockets = ls->next;
272

273
		g_source_remove(ls->source_id);
274 275 276
		close(ls->fd);
		g_free(ls);
	}
277 278
}

279 280
static int get_remote_uid(int fd)
{
281
#ifdef HAVE_STRUCT_UCRED
282 283 284 285 286 287 288 289 290 291 292 293 294
	struct ucred cred;
	socklen_t len = sizeof (cred);

	if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len) < 0)
		return 0;

	return cred.uid;
#else
	(void)fd;
	return -1;
#endif
}

295 296 297 298
static gboolean
listen_in_event(G_GNUC_UNUSED GIOChannel *source,
		G_GNUC_UNUSED GIOCondition condition,
		gpointer data)
Avuton Olrich's avatar
Avuton Olrich committed
299
{
300
	int listen_fd = GPOINTER_TO_INT(data), fd;
Warren Dukes's avatar
Warren Dukes committed
301 302 303
	struct sockaddr sockAddr;
	socklen_t socklen = sizeof(sockAddr);

304 305
	fd = accept(listen_fd, &sockAddr, &socklen);
	if (fd >= 0) {
306 307
		set_nonblocking(fd);

308 309 310
		client_new(fd, &sockAddr, get_remote_uid(fd));
	} else if (fd < 0 && errno != EINTR) {
		g_warning("Problems accept()'ing");
Warren Dukes's avatar
Warren Dukes committed
311
	}
312 313

	return true;
Warren Dukes's avatar
Warren Dukes committed
314
}