From 405ad9e58bbb1be771d777f00b253e03061f6b2e Mon Sep 17 00:00:00 2001 From: Warren Dukes Date: Wed, 3 Nov 2004 19:42:54 +0000 Subject: now one can specify multiple bind_to_addresses git-svn-id: https://svn.musicpd.org/mpd/trunk@2501 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- src/directory.c | 2 +- src/interface.c | 5 ++- src/listen.c | 107 +++++++++++++++++++++++++++++++++++++++++--------------- src/listen.h | 16 +++++++-- src/main.c | 9 ++--- src/player.c | 2 +- 6 files changed, 97 insertions(+), 44 deletions(-) (limited to 'src') diff --git a/src/directory.c b/src/directory.c index 5caeb311d..38043276f 100644 --- a/src/directory.c +++ b/src/directory.c @@ -193,7 +193,7 @@ int updateInit(FILE * fp, List * pathList) { unblockSignals(); finishSigHandlers(); - close(listenSocket); + closeAllListenSockets(); freeAllInterfaces(); finishPlaylist(); finishVolume(); diff --git a/src/interface.c b/src/interface.c index 72c71f911..d2ae43a8d 100644 --- a/src/interface.c +++ b/src/interface.c @@ -337,8 +337,7 @@ void addInterfacesReadyToReadAndListenSocketToFdSet(fd_set * fds, int * fdmax) { int i; FD_ZERO(fds); - FD_SET(listenSocket,fds); - if(*fdmaxvalue, "any")==0) { + if(!param || 0==strcmp(param->value, "any")) { + DEBUG("binding to any address\n"); #ifdef HAVE_IPV6 if(ipv6Supported()) { sin6.sin6_addr = in6addr_any; @@ -81,6 +80,7 @@ int establish(unsigned short port) { } else { struct hostent * he; + DEBUG("binding to address for %s\n", param->value); if(!(he = gethostbyname(param->value))) { ERROR("can't lookup host \"%s\" at line %i\n", param->value, param->line); @@ -129,53 +129,102 @@ int establish(unsigned short port) { break; default: ERROR("unknown address family: %i\n",addrp->sa_family); - return -1; + exit(EXIT_FAILURE); } if((sock = socket(pf,SOCK_STREAM,0)) < 0) { ERROR("socket < 0\n"); - return -1; + exit(EXIT_FAILURE); + } + + if(fcntl(sock, F_SETFL ,fcntl(sock, F_GETFL) | O_NONBLOCK) < 0) { + ERROR("problems setting nonblocking on listen socket: %s\n", + strerror(errno)); + exit(EXIT_FAILURE); } if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&allowReuse, sizeof(allowReuse))<0) { - close(sock); ERROR("problems setsockopt'ing\n"); - return -1; + exit(EXIT_FAILURE); } if(bind(sock,addrp,addrlen)<0) { - ERROR("unable to bind port %i, maybe MPD is still running?\n", - port); - close(sock); - return -1; + ERROR("unable to bind port %i: %s\n", port, strerror(errno)); + ERROR("maybe MPD is still running?\n"); + exit(EXIT_FAILURE); } if(listen(sock,5)<0) { - close(sock); - ERROR("problems listen'ing\n"); - return -1; + ERROR("problems listen'ing: %s\n", strerror(errno)); + exit(EXIT_FAILURE); } return sock; } -void getConnections(int sock) { - fd_set fdsr; +void establish(unsigned int port) { + ConfigParam * param = getNextConfigParam(CONF_BIND_TO_ADDRESS, NULL); + + do { + numberOfListenSockets++; + listenSockets = realloc(listenSockets, + sizeof(int)*numberOfListenSockets); + + listenSockets[numberOfListenSockets-1] = + establishListen(port, param); + } while ((param = getNextConfigParam(CONF_BIND_TO_ADDRESS, param))); +} + +void addListenSocketsToFdSet(fd_set * fds, int * fdmax) { + int i; + + for(i=0; i *fdmax) *fdmax = listenSockets[i]; + } +} + +void closeAllListenSockets() { + int i; + + DEBUG("closeAllListenSockets called\n"); + + for(i=0; i= 0)) { - openAInterface(fd,&sockAddr); + for(i=0; i= 0) + { + openAInterface(fd,&sockAddr); + } + else if(fd<0 && (errno!=EAGAIN && errno!=EINTR)) { + ERROR("Problems accept()'ing\n"); + } + } } - else if(fd<0) ERROR("Problems accept()'ing\n"); } diff --git a/src/listen.h b/src/listen.h index dd5e32d8c..1030d9b19 100644 --- a/src/listen.h +++ b/src/listen.h @@ -21,10 +21,20 @@ #include "../config.h" -extern int listenSocket; +#include +#include +#include +#include -int establish(unsigned short port); +void establish(unsigned int port); -void getConnections(int sock); +void getConnections(fd_set * fds); + +int isAListenSocket(int sock); + +void closeAllListenSockets(); + +/* fdmax should be initialized to something */ +void addListenSocketsToFdSet(fd_set * fds, int * fdmax); #endif diff --git a/src/main.c b/src/main.c index 2160a1ccd..bb1146cb0 100644 --- a/src/main.c +++ b/src/main.c @@ -226,12 +226,7 @@ void establishListen(Options * options) { exit(EXIT_FAILURE); } - if(options->createDB <= 0 && !options->updateDB && - (listenSocket = establish(port))<0) - { - ERROR("error binding port\n"); - exit(EXIT_FAILURE); - } + if(options->createDB <= 0 && !options->updateDB) establish(port); } void changeToUser(Options * options) { @@ -453,7 +448,7 @@ int main(int argc, char * argv[]) { playerKill(); freeAllInterfaces(); - close(listenSocket); + closeAllListenSockets(); closeMp3Directory(); closeTables(); finishPlaylist(); diff --git a/src/player.c b/src/player.c index cb9b5ed1c..c16cad22e 100644 --- a/src/player.c +++ b/src/player.c @@ -112,7 +112,7 @@ int playerInit() { setSigHandlersForDecoder(); - while(close(listenSocket)<0 && errno==EINTR); + closeAllListenSockets(); freeAllInterfaces(); closeMp3Directory(); finishPlaylist(); -- cgit v1.2.3