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"
|
|
|
|
#include "interface.h"
|
|
|
|
#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
|
|
|
|
|
|
|
#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) {
|
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
|
|
|
|
case AF_UNIX:
|
|
|
|
pf = PF_UNIX;
|
|
|
|
break;
|
|
|
|
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-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;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!param || 0 == strcmp(param->value, "any")) {
|
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: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");
|
|
|
|
|
|
|
|
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)
|
|
|
|
BINDERROR();
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
struct hostent *he;
|
2004-11-03 20:42:54 +01:00
|
|
|
DEBUG("binding to address for %s\n", param->value);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!(he = gethostbyname(param->value))) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("can't lookup host \"%s\" at line %i\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
param->value, param->line);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (he->h_addrtype) {
|
2004-02-24 00:41:20 +01:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
case AF_INET6:
|
2006-07-22 19:19:59 +02:00
|
|
|
if (!useIpv6) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("no IPv6 support, but a IPv6 address "
|
2006-07-20 18:02:40 +02:00
|
|
|
"found for \"%s\" at line %i\n",
|
|
|
|
param->value, param->line);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-08-01 06:18:37 +02:00
|
|
|
memcpy((char *)&sin6.sin6_addr.s6_addr,
|
2008-04-12 06:07:11 +02:00
|
|
|
(const char *)he->h_addr, he->h_length);
|
|
|
|
addrp = (const struct sockaddr *)&sin6;
|
2004-02-24 00:41:20 +01:00
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case AF_INET:
|
2008-02-05 11:20:46 +01:00
|
|
|
memcpy((char *)&sin4.sin_addr.s_addr,
|
2008-04-12 06:07:11 +02:00
|
|
|
(const char *)he->h_addr, he->h_length);
|
2008-02-05 11:20:46 +01:00
|
|
|
addrp = (struct sockaddr *)&sin4;
|
2004-02-24 00:41:20 +01:00
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
|
|
|
break;
|
|
|
|
default:
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("address type for \"%s\" is not IPv4 or IPv6 "
|
2006-07-20 18:02:40 +02:00
|
|
|
"at line %i\n", param->value, param->line);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-04-12 06:07:06 +02:00
|
|
|
if (establishListen(addrp, addrlen) < 0)
|
2006-07-22 15:15:20 +02:00
|
|
|
BINDERROR();
|
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) {
|
|
|
|
openAInterface(fd, &sockAddr);
|
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|