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"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <resolv.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#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;
|
|
|
|
static int boundPort;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-22 15:15:20 +02:00
|
|
|
static int establishListen(unsigned int port,
|
|
|
|
struct sockaddr *addrp, socklen_t addrlen)
|
2006-07-19 20:56:54 +02:00
|
|
|
{
|
2007-05-26 20:20:53 +02:00
|
|
|
int pf = 0;
|
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
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK) < 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)
|
|
|
|
{
|
2007-05-26 20:20:53 +02:00
|
|
|
struct sockaddr *addrp = NULL;
|
|
|
|
socklen_t addrlen = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
struct sockaddr_in sin;
|
|
|
|
#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
|
|
|
|
memset(&sin, 0, sizeof(struct sockaddr_in));
|
|
|
|
sin.sin_port = htons(port);
|
|
|
|
sin.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;
|
|
|
|
addrp = (struct sockaddr *)&sin6;
|
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
2007-05-26 20:15:54 +02:00
|
|
|
if (establishListen(port, addrp, addrlen) < 0)
|
2006-07-22 19:19:59 +02:00
|
|
|
BINDERROR();
|
|
|
|
}
|
|
|
|
#endif
|
2006-07-22 15:15:20 +02:00
|
|
|
sin.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addrp = (struct sockaddr *)&sin;
|
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
2006-07-22 19:19:59 +02:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
if ((establishListen(port, addrp, addrlen) < 0) && !useIpv6) {
|
|
|
|
#else
|
2006-07-22 15:15:20 +02:00
|
|
|
if (establishListen(port, addrp, addrlen) < 0) {
|
2006-07-22 19:19:59 +02:00
|
|
|
#endif
|
2006-07-22 15:15:20 +02:00
|
|
|
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,
|
|
|
|
(char *)he->h_addr, he->h_length);
|
2006-07-20 18:02:40 +02:00
|
|
|
addrp = (struct sockaddr *)&sin6;
|
2004-02-24 00:41:20 +01:00
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case AF_INET:
|
2006-08-01 06:18:37 +02:00
|
|
|
memcpy((char *)&sin.sin_addr.s_addr,
|
|
|
|
(char *)he->h_addr, he->h_length);
|
2006-07-20 18:02:40 +02:00
|
|
|
addrp = (struct sockaddr *)&sin;
|
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
|
|
|
}
|
|
|
|
|
2007-05-26 20:15:54 +02:00
|
|
|
if (establishListen(port, 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;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2007-01-11 21:41:17 +01:00
|
|
|
|
2007-01-14 03:08:20 +01:00
|
|
|
int getBoundPort(void)
|
2007-01-11 21:41:17 +01:00
|
|
|
{
|
|
|
|
return boundPort;
|
|
|
|
}
|