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"
|
|
|
|
#include "log.h"
|
2004-05-15 23:19:43 +02:00
|
|
|
#include "utils.h"
|
2008-01-03 08:29:49 +01:00
|
|
|
#include "os_compat.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-04-12 06:19:26 +02:00
|
|
|
#include "../config.h"
|
|
|
|
|
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 { \
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("unable to bind port %u: %s\n" \
|
|
|
|
"maybe MPD is still running?\n", \
|
|
|
|
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
|
|
|
|
2007-09-05 03:19:37 +02:00
|
|
|
/*
|
|
|
|
* redirect stdin to /dev/null to work around a libao bug
|
|
|
|
* there are likely other bugs in other libraries (and even our code!)
|
|
|
|
* that check for fd > 0, so it's easiest to just keep
|
|
|
|
* fd = 0 == /dev/null for now...
|
|
|
|
*/
|
|
|
|
static void redirect_stdin(void)
|
|
|
|
{
|
|
|
|
int fd, st;
|
|
|
|
struct stat ss;
|
|
|
|
|
|
|
|
if ((st = fstat(STDIN_FILENO, &ss)) < 0) {
|
|
|
|
if ((fd = open("/dev/null", O_RDONLY) > 0)) {
|
|
|
|
DEBUG("stdin closed, and could not open /dev/null "
|
|
|
|
"as fd=0, some external library bugs "
|
|
|
|
"may be exposed...\n");
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!isatty(STDIN_FILENO))
|
|
|
|
return;
|
|
|
|
if ((fd = open("/dev/null", O_RDONLY)) < 0)
|
|
|
|
FATAL("failed to open /dev/null %s\n", strerror(errno));
|
|
|
|
if (dup2(fd, STDIN_FILENO) < 0)
|
|
|
|
FATAL("dup2 stdin: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
|
2008-04-12 06:07:11 +02:00
|
|
|
static int establishListen(const struct sockaddr *addrp, socklen_t addrlen)
|
2006-07-19 20:56:54 +02:00
|
|
|
{
|
2007-08-27 10:05:55 +02:00
|
|
|
int pf;
|
2004-02-24 00:41:20 +01:00
|
|
|
int sock;
|
2006-07-19 20:56:54 +02:00
|
|
|
int allowReuse = ALLOW_REUSE;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (addrp->sa_family) {
|
2008-04-12 06:07:24 +02:00
|
|
|
#ifdef HAVE_TCP
|
2006-07-19 20:56:54 +02:00
|
|
|
case AF_INET:
|
|
|
|
pf = PF_INET;
|
|
|
|
break;
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
pf = PF_INET6;
|
|
|
|
break;
|
|
|
|
#endif
|
2008-04-12 06:07:24 +02:00
|
|
|
#endif /* HAVE_TCP */
|
|
|
|
#ifdef HAVE_UN
|
2006-07-19 20:56:54 +02:00
|
|
|
case AF_UNIX:
|
|
|
|
pf = PF_UNIX;
|
|
|
|
break;
|
2008-04-12 06:07:24 +02:00
|
|
|
#endif /* HAVE_UN */
|
2006-07-19 20:56:54 +02:00
|
|
|
default:
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("unknown address family: %i\n", addrp->sa_family);
|
2006-07-19 20:56:54 +02:00
|
|
|
}
|
|
|
|
|
2007-05-26 20:15:54 +02:00
|
|
|
if ((sock = socket(pf, SOCK_STREAM, 0)) < 0)
|
|
|
|
FATAL("socket < 0\n");
|
2006-07-19 20:56:54 +02:00
|
|
|
|
2008-03-26 11:39:03 +01:00
|
|
|
if (set_nonblocking(sock) < 0) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("problems setting nonblocking on listen socket: %s\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
strerror(errno));
|
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) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("problems setsockopt'ing: %s\n", 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)
|
|
|
|
FATAL("problems listen'ing: %s\n", strerror(errno));
|
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
|
|
|
|
|
|
|
return 0;
|
2006-07-19 20:56:54 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void parseListenConfigParam(unsigned int port, ConfigParam * param)
|
|
|
|
{
|
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
|
2004-11-03 20:42:54 +01:00
|
|
|
DEBUG("binding to any address\n");
|
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-04-12 06:07:06 +02:00
|
|
|
if (establishListen(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-04-12 06:07:06 +02:00
|
|
|
if ((establishListen(addrp, addrlen) < 0) && !useIpv6) {
|
2006-07-22 19:19:59 +02:00
|
|
|
#else
|
2008-04-12 06:07:06 +02:00
|
|
|
if (establishListen(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 */
|
|
|
|
FATAL("TCP support is disabled\n");
|
|
|
|
#endif /* HAVE_TCP */
|
|
|
|
#ifdef HAVE_UN
|
2008-04-12 06:07:18 +02:00
|
|
|
} else if (param->value[0] == '/') {
|
|
|
|
size_t path_length;
|
|
|
|
struct sockaddr_un sun;
|
|
|
|
|
|
|
|
path_length = strlen(param->value);
|
|
|
|
if (path_length >= sizeof(sun.sun_path))
|
|
|
|
FATAL("unix socket path is too long\n");
|
|
|
|
|
2008-04-12 06:20:56 +02:00
|
|
|
unlink(param->value);
|
|
|
|
|
2008-04-12 06:07:18 +02:00
|
|
|
sun.sun_family = AF_UNIX;
|
|
|
|
memcpy(sun.sun_path, param->value, path_length + 1);
|
|
|
|
|
|
|
|
addrp = (const struct sockaddr *)&sun;
|
|
|
|
addrlen = sizeof(sun);
|
|
|
|
|
|
|
|
if (establishListen(addrp, addrlen) < 0)
|
2008-04-12 06:07:37 +02:00
|
|
|
FATAL("unable to bind to %s: %s\n",
|
|
|
|
param->value, strerror(errno));
|
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;
|
|
|
|
|
2004-11-03 20:42:54 +01:00
|
|
|
DEBUG("binding to address for %s\n", param->value);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-15 07:20:53 +02:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_flags = AI_ADDRCONFIG;
|
|
|
|
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)
|
|
|
|
FATAL("can't lookup host \"%s\" at line %i: %s\n",
|
|
|
|
param->value, param->line, gai_strerror(ret));
|
|
|
|
|
|
|
|
for (i = ai; i != NULL; i = i->ai_next)
|
|
|
|
if (establishListen(i->ai_addr, i->ai_addrlen) < 0)
|
|
|
|
BINDERROR();
|
|
|
|
|
|
|
|
freeaddrinfo(ai);
|
2008-04-12 06:07:24 +02:00
|
|
|
#else /* HAVE_TCP */
|
|
|
|
FATAL("TCP support is disabled\n");
|
|
|
|
#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') {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("%s \"%s\" specified at line %i is not a "
|
2007-02-28 13:34:36 +01:00
|
|
|
"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;
|
|
|
|
|
2007-09-05 03:19:37 +02:00
|
|
|
redirect_stdin();
|
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 addListenSocketsToFdSet(fd_set * fds, int *fdmax)
|
|
|
|
{
|
2004-11-03 20:42:54 +01:00
|
|
|
int i;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < numberOfListenSockets; i++) {
|
2004-11-03 20:42:54 +01:00
|
|
|
FD_SET(listenSockets[i], fds);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (listenSockets[i] > *fdmax)
|
|
|
|
*fdmax = listenSockets[i];
|
2004-11-03 20:42:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void closeAllListenSockets(void)
|
|
|
|
{
|
2004-11-03 20:42:54 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
DEBUG("closeAllListenSockets called\n");
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < numberOfListenSockets; i++) {
|
2005-03-19 13:39:50 +01:00
|
|
|
DEBUG("closing listen socket %i\n", i);
|
2006-07-20 18:02:40 +02:00
|
|
|
while (close(listenSockets[i]) < 0 && errno == EINTR) ;
|
2004-11-03 20:42:54 +01:00
|
|
|
}
|
2005-11-16 15:43:04 +01:00
|
|
|
freeAllListenSockets();
|
|
|
|
}
|
2004-11-03 20:42:54 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void freeAllListenSockets(void)
|
|
|
|
{
|
2004-11-03 20:42:54 +01:00
|
|
|
numberOfListenSockets = 0;
|
|
|
|
free(listenSockets);
|
|
|
|
listenSockets = NULL;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void getConnections(fd_set * fds)
|
|
|
|
{
|
2004-11-03 20:42:54 +01:00
|
|
|
int i;
|
2004-02-24 00:41:20 +01:00
|
|
|
int fd = 0;
|
|
|
|
struct sockaddr sockAddr;
|
|
|
|
socklen_t socklen = sizeof(sockAddr);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < numberOfListenSockets; i++) {
|
|
|
|
if (FD_ISSET(listenSockets[i], fds)) {
|
|
|
|
if ((fd = accept(listenSockets[i], &sockAddr, &socklen))
|
|
|
|
>= 0) {
|
2008-08-28 20:03:02 +02:00
|
|
|
client_new(fd, &sockAddr);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (fd < 0
|
|
|
|
&& (errno != EAGAIN && errno != EINTR)) {
|
|
|
|
ERROR("Problems accept()'ing\n");
|
2004-11-03 20:42:54 +01:00
|
|
|
}
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|