2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* 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"
|
2008-08-28 20:02:43 +02:00
|
|
|
#include "client.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "conf.h"
|
2004-05-15 23:19:43 +02:00
|
|
|
#include "utils.h"
|
2008-12-29 17:28:32 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2008-12-30 19:07:10 +01:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <winsock.h>
|
|
|
|
#else
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <netdb.h>
|
2008-12-30 19:07:10 +01:00
|
|
|
#endif
|
2008-04-12 06:19:26 +02:00
|
|
|
|
2008-12-29 17:29:04 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "listen"
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
#define MAXHOSTNAME 1024
|
|
|
|
|
2005-03-06 20:00:58 +01:00
|
|
|
#define ALLOW_REUSE 1
|
|
|
|
|
|
|
|
#define DEFAULT_PORT 6600
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-22 15:15:20 +02:00
|
|
|
#define BINDERROR() do { \
|
2008-12-29 17:29:04 +01:00
|
|
|
g_error("unable to bind port %u: %s; " \
|
|
|
|
"maybe MPD is still running?", \
|
|
|
|
port, strerror(errno)); \
|
2006-07-22 15:15:20 +02:00
|
|
|
} while (0);
|
|
|
|
|
2007-01-14 04:07:53 +01:00
|
|
|
static int *listenSockets;
|
|
|
|
static int numberOfListenSockets;
|
2007-06-01 19:44:03 +02:00
|
|
|
int boundPort;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-12-30 19:24:39 +01:00
|
|
|
static gboolean
|
|
|
|
listen_in_event(GIOChannel *source, GIOCondition condition, gpointer data);
|
|
|
|
|
2008-10-15 07:30:24 +02:00
|
|
|
static int establishListen(int pf, const struct sockaddr *addrp,
|
|
|
|
socklen_t addrlen)
|
2006-07-19 20:56:54 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
int sock;
|
2006-07-19 20:56:54 +02:00
|
|
|
int allowReuse = ALLOW_REUSE;
|
2008-10-18 18:21:49 +02:00
|
|
|
#ifdef HAVE_STRUCT_UCRED
|
2008-10-15 22:34:21 +02:00
|
|
|
int passcred = 1;
|
2008-10-17 17:40:28 +02:00
|
|
|
#endif
|
2008-12-30 19:24:39 +01:00
|
|
|
GIOChannel *channel;
|
2006-07-19 20:56:54 +02:00
|
|
|
|
2007-05-26 20:15:54 +02:00
|
|
|
if ((sock = socket(pf, SOCK_STREAM, 0)) < 0)
|
2008-12-29 17:29:04 +01:00
|
|
|
g_error("socket < 0");
|
2006-07-19 20:56:54 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&allowReuse,
|
|
|
|
sizeof(allowReuse)) < 0) {
|
2008-12-29 17:29:04 +01:00
|
|
|
g_error("problems setsockopt'ing: %s", strerror(errno));
|
2006-07-19 20:56:54 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (bind(sock, addrp, addrlen) < 0) {
|
2006-07-22 15:15:20 +02:00
|
|
|
close(sock);
|
|
|
|
return -1;
|
2006-07-19 20:56:54 +02:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2007-05-26 20:15:54 +02:00
|
|
|
if (listen(sock, 5) < 0)
|
2008-12-29 17:29:04 +01:00
|
|
|
g_error("problems listen'ing: %s", strerror(errno));
|
2006-07-19 20:56:54 +02:00
|
|
|
|
2008-10-18 18:21:49 +02:00
|
|
|
#ifdef HAVE_STRUCT_UCRED
|
2008-10-15 22:34:21 +02:00
|
|
|
setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &passcred, sizeof(passcred));
|
|
|
|
#endif
|
|
|
|
|
2006-07-19 20:56:54 +02:00
|
|
|
numberOfListenSockets++;
|
2006-07-20 18:02:40 +02:00
|
|
|
listenSockets =
|
2006-08-26 08:25:57 +02:00
|
|
|
xrealloc(listenSockets, sizeof(int) * numberOfListenSockets);
|
2006-07-19 20:56:54 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
listenSockets[numberOfListenSockets - 1] = sock;
|
2006-07-22 15:15:20 +02:00
|
|
|
|
2008-12-30 19:24:39 +01:00
|
|
|
channel = g_io_channel_unix_new(sock);
|
|
|
|
g_io_add_watch(channel, G_IO_IN,
|
|
|
|
listen_in_event, GINT_TO_POINTER(sock));
|
|
|
|
g_io_channel_unref(channel);
|
|
|
|
|
2006-07-22 15:15:20 +02:00
|
|
|
return 0;
|
2006-07-19 20:56:54 +02:00
|
|
|
}
|
|
|
|
|
2008-12-24 11:07:58 +01:00
|
|
|
static void
|
|
|
|
parseListenConfigParam(G_GNUC_UNUSED unsigned int port, ConfigParam * param)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-04-12 06:07:11 +02:00
|
|
|
const struct sockaddr *addrp;
|
2007-08-27 10:05:55 +02:00
|
|
|
socklen_t addrlen;
|
2008-04-12 06:07:24 +02:00
|
|
|
#ifdef HAVE_TCP
|
2008-02-05 11:20:46 +01:00
|
|
|
struct sockaddr_in sin4;
|
2004-02-24 00:41:20 +01:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
struct sockaddr_in6 sin6;
|
2006-07-22 19:19:59 +02:00
|
|
|
int useIpv6 = ipv6Supported();
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
memset(&sin6, 0, sizeof(struct sockaddr_in6));
|
|
|
|
sin6.sin6_port = htons(port);
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
#endif
|
2008-02-05 11:20:46 +01:00
|
|
|
memset(&sin4, 0, sizeof(struct sockaddr_in));
|
|
|
|
sin4.sin_port = htons(port);
|
|
|
|
sin4.sin_family = AF_INET;
|
2008-04-12 06:07:24 +02:00
|
|
|
#endif /* HAVE_TCP */
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!param || 0 == strcmp(param->value, "any")) {
|
2008-04-12 06:07:24 +02:00
|
|
|
#ifdef HAVE_TCP
|
2008-12-29 17:29:04 +01:00
|
|
|
g_debug("binding to any address");
|
2006-07-22 19:19:59 +02:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
if (useIpv6) {
|
|
|
|
sin6.sin6_addr = in6addr_any;
|
2008-04-12 06:07:11 +02:00
|
|
|
addrp = (const struct sockaddr *)&sin6;
|
2006-07-22 19:19:59 +02:00
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
2008-10-15 07:30:24 +02:00
|
|
|
if (establishListen(PF_INET6, addrp, addrlen) < 0)
|
2006-07-22 19:19:59 +02:00
|
|
|
BINDERROR();
|
|
|
|
}
|
|
|
|
#endif
|
2008-02-05 11:20:46 +01:00
|
|
|
sin4.sin_addr.s_addr = INADDR_ANY;
|
2008-04-12 06:07:11 +02:00
|
|
|
addrp = (const struct sockaddr *)&sin4;
|
2006-07-22 15:15:20 +02:00
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
2006-07-22 19:19:59 +02:00
|
|
|
#ifdef HAVE_IPV6
|
2008-10-15 07:30:24 +02:00
|
|
|
if ((establishListen(PF_INET, addrp, addrlen) < 0) && !useIpv6) {
|
2006-07-22 19:19:59 +02:00
|
|
|
#else
|
2008-10-15 07:30:24 +02:00
|
|
|
if (establishListen(PF_INET, addrp, addrlen) < 0) {
|
2006-07-22 19:19:59 +02:00
|
|
|
#endif
|
2006-07-22 15:15:20 +02:00
|
|
|
BINDERROR();
|
|
|
|
}
|
2008-04-12 06:07:24 +02:00
|
|
|
#else /* HAVE_TCP */
|
2008-12-29 17:29:04 +01:00
|
|
|
g_error("TCP support is disabled");
|
2008-04-12 06:07:24 +02:00
|
|
|
#endif /* HAVE_TCP */
|
|
|
|
#ifdef HAVE_UN
|
2008-04-12 06:07:18 +02:00
|
|
|
} else if (param->value[0] == '/') {
|
|
|
|
size_t path_length;
|
2008-11-07 10:33:48 +01:00
|
|
|
struct sockaddr_un s_un;
|
2008-04-12 06:07:18 +02:00
|
|
|
|
|
|
|
path_length = strlen(param->value);
|
2008-11-07 10:33:48 +01:00
|
|
|
if (path_length >= sizeof(s_un.sun_path))
|
2008-12-29 17:29:04 +01:00
|
|
|
g_error("unix socket path is too long");
|
2008-04-12 06:07:18 +02:00
|
|
|
|
2008-04-12 06:20:56 +02:00
|
|
|
unlink(param->value);
|
|
|
|
|
2008-11-07 10:33:48 +01:00
|
|
|
s_un.sun_family = AF_UNIX;
|
|
|
|
memcpy(s_un.sun_path, param->value, path_length + 1);
|
2008-04-12 06:07:18 +02:00
|
|
|
|
2008-11-07 10:33:48 +01:00
|
|
|
addrp = (const struct sockaddr *)&s_un;
|
|
|
|
addrlen = sizeof(s_un);
|
2008-04-12 06:07:18 +02:00
|
|
|
|
2008-10-15 07:30:24 +02:00
|
|
|
if (establishListen(PF_UNIX, addrp, addrlen) < 0)
|
2008-12-29 17:29:04 +01:00
|
|
|
g_error("unable to bind to %s: %s",
|
|
|
|
param->value, strerror(errno));
|
2008-10-30 18:03:18 +01:00
|
|
|
|
|
|
|
/* allow everybody to connect */
|
|
|
|
chmod(param->value, 0666);
|
|
|
|
|
2008-04-12 06:07:24 +02:00
|
|
|
#endif /* HAVE_UN */
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2008-04-12 06:07:24 +02:00
|
|
|
#ifdef HAVE_TCP
|
2008-10-15 07:20:53 +02:00
|
|
|
struct addrinfo hints, *ai, *i;
|
|
|
|
char service[20];
|
|
|
|
int ret;
|
|
|
|
|
2008-12-29 17:29:04 +01:00
|
|
|
g_debug("binding to address for %s", param->value);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-15 07:20:53 +02:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2008-12-02 10:23:36 +01:00
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
#ifdef AI_ADDRCONFIG
|
|
|
|
hints.ai_flags |= AI_ADDRCONFIG;
|
|
|
|
#endif
|
2008-10-15 07:20:53 +02:00
|
|
|
hints.ai_family = PF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
|
|
|
|
snprintf(service, sizeof(service), "%u", port);
|
|
|
|
|
|
|
|
ret = getaddrinfo(param->value, service, &hints, &ai);
|
|
|
|
if (ret != 0)
|
2008-12-29 17:29:04 +01:00
|
|
|
g_error("can't lookup host \"%s\" at line %i: %s",
|
|
|
|
param->value, param->line, gai_strerror(ret));
|
2008-10-15 07:20:53 +02:00
|
|
|
|
|
|
|
for (i = ai; i != NULL; i = i->ai_next)
|
2008-10-15 07:30:24 +02:00
|
|
|
if (establishListen(i->ai_family, i->ai_addr,
|
|
|
|
i->ai_addrlen) < 0)
|
2008-10-15 07:20:53 +02:00
|
|
|
BINDERROR();
|
|
|
|
|
|
|
|
freeaddrinfo(ai);
|
2008-04-12 06:07:24 +02:00
|
|
|
#else /* HAVE_TCP */
|
2008-12-29 17:29:04 +01:00
|
|
|
g_error("TCP support is disabled");
|
2008-04-12 06:07:24 +02:00
|
|
|
#endif /* HAVE_TCP */
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void listenOnPort(void)
|
|
|
|
{
|
2005-03-06 20:00:58 +01:00
|
|
|
int port = DEFAULT_PORT;
|
2006-07-20 18:02:40 +02:00
|
|
|
ConfigParam *param = getNextConfigParam(CONF_BIND_TO_ADDRESS, NULL);
|
2007-02-28 13:34:36 +01:00
|
|
|
ConfigParam *portParam = getConfigParam(CONF_PORT);
|
|
|
|
|
|
|
|
if (portParam) {
|
|
|
|
char *test;
|
|
|
|
port = strtol(portParam->value, &test, 10);
|
|
|
|
if (port <= 0 || *test != '\0') {
|
2008-12-29 17:29:04 +01:00
|
|
|
g_error("%s \"%s\" specified at line %i is not a "
|
|
|
|
"positive integer",
|
|
|
|
CONF_PORT,
|
|
|
|
portParam->value, portParam->line);
|
2005-03-06 20:00:58 +01:00
|
|
|
}
|
|
|
|
}
|
2004-11-03 20:42:54 +01:00
|
|
|
|
2007-01-11 21:41:17 +01:00
|
|
|
boundPort = port;
|
|
|
|
|
2004-11-03 20:42:54 +01:00
|
|
|
do {
|
2006-07-20 18:02:40 +02:00
|
|
|
parseListenConfigParam(port, param);
|
2004-11-03 20:42:54 +01:00
|
|
|
} while ((param = getNextConfigParam(CONF_BIND_TO_ADDRESS, param)));
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void closeAllListenSockets(void)
|
|
|
|
{
|
2004-11-03 20:42:54 +01:00
|
|
|
int i;
|
|
|
|
|
2008-12-29 17:29:04 +01:00
|
|
|
g_debug("closeAllListenSockets called");
|
2004-11-03 20:42:54 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < numberOfListenSockets; i++) {
|
2008-12-29 17:29:04 +01:00
|
|
|
g_debug("closing listen socket %i", i);
|
2006-07-20 18:02:40 +02:00
|
|
|
while (close(listenSockets[i]) < 0 && errno == EINTR) ;
|
2004-11-03 20:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
numberOfListenSockets = 0;
|
|
|
|
free(listenSockets);
|
|
|
|
listenSockets = NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-15 22:34:21 +02:00
|
|
|
static int get_remote_uid(int fd)
|
|
|
|
{
|
2008-10-18 18:21:49 +02:00
|
|
|
#ifdef HAVE_STRUCT_UCRED
|
2008-10-15 22:34:21 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2008-12-30 19:24:39 +01:00
|
|
|
static gboolean
|
|
|
|
listen_in_event(G_GNUC_UNUSED GIOChannel *source,
|
|
|
|
G_GNUC_UNUSED GIOCondition condition,
|
|
|
|
gpointer data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-12-30 19:24:39 +01:00
|
|
|
int listen_fd = GPOINTER_TO_INT(data), fd;
|
2004-02-24 00:41:20 +01:00
|
|
|
struct sockaddr sockAddr;
|
|
|
|
socklen_t socklen = sizeof(sockAddr);
|
|
|
|
|
2008-12-30 19:24:39 +01:00
|
|
|
fd = accept(listen_fd, &sockAddr, &socklen);
|
|
|
|
if (fd >= 0) {
|
|
|
|
client_new(fd, &sockAddr, get_remote_uid(fd));
|
|
|
|
} else if (fd < 0 && errno != EINTR) {
|
|
|
|
g_warning("Problems accept()'ing");
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2008-12-30 19:24:39 +01:00
|
|
|
|
|
|
|
return true;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|