Initial revision 6759
[qball-mpd.git] / src / .svn / text-base / listen.c.svn-base
blob9d105e815b51d8f73d9a93f2b69623ae47f8b123
1 /* the Music Player Daemon (MPD)
2  * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3  * This project's homepage is: http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
19 #include "listen.h"
20 #include "interface.h"
21 #include "conf.h"
22 #include "log.h"
23 #include "utils.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/socket.h>
28 #include <sys/stat.h>
29 #include <sys/param.h>
30 #include <netinet/in.h>
31 #include <netdb.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <resolv.h>
35 #include <fcntl.h>
37 #define MAXHOSTNAME     1024
39 #define ALLOW_REUSE     1
41 #define DEFAULT_PORT    6600
43 #define BINDERROR() do { \
44         FATAL("unable to bind port %u: %s\n" \
45               "maybe MPD is still running?\n", \
46               port, strerror(errno)); \
47 } while (0);
49 static int *listenSockets;
50 static int numberOfListenSockets;
51 int boundPort;
53 static int establishListen(unsigned int port,
54                            struct sockaddr *addrp, socklen_t addrlen)
56         int pf = 0;
57         int sock;
58         int allowReuse = ALLOW_REUSE;
60         switch (addrp->sa_family) {
61         case AF_INET:
62                 pf = PF_INET;
63                 break;
64 #ifdef HAVE_IPV6
65         case AF_INET6:
66                 pf = PF_INET6;
67                 break;
68 #endif
69         case AF_UNIX:
70                 pf = PF_UNIX;
71                 break;
72         default:
73                 FATAL("unknown address family: %i\n", addrp->sa_family);
74         }
76         if ((sock = socket(pf, SOCK_STREAM, 0)) < 0)
77                 FATAL("socket < 0\n");
79         if (fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK) < 0) {
80                 FATAL("problems setting nonblocking on listen socket: %s\n",
81                       strerror(errno));
82         }
84         if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&allowReuse,
85                        sizeof(allowReuse)) < 0) {
86                 FATAL("problems setsockopt'ing: %s\n", strerror(errno));
87         }
89         if (bind(sock, addrp, addrlen) < 0) {
90                 close(sock);
91                 return -1;
92         }
94         if (listen(sock, 5) < 0)
95                 FATAL("problems listen'ing: %s\n", strerror(errno));
97         numberOfListenSockets++;
98         listenSockets =
99             xrealloc(listenSockets, sizeof(int) * numberOfListenSockets);
101         listenSockets[numberOfListenSockets - 1] = sock;
103         return 0;
106 static void parseListenConfigParam(unsigned int port, ConfigParam * param)
108         struct sockaddr *addrp = NULL;
109         socklen_t addrlen = 0;
110         struct sockaddr_in sin;
111 #ifdef HAVE_IPV6
112         struct sockaddr_in6 sin6;
113         int useIpv6 = ipv6Supported();
115         memset(&sin6, 0, sizeof(struct sockaddr_in6));
116         sin6.sin6_port = htons(port);
117         sin6.sin6_family = AF_INET6;
118 #endif
119         memset(&sin, 0, sizeof(struct sockaddr_in));
120         sin.sin_port = htons(port);
121         sin.sin_family = AF_INET;
123         if (!param || 0 == strcmp(param->value, "any")) {
124                 DEBUG("binding to any address\n");
125 #ifdef HAVE_IPV6
126                 if (useIpv6) {
127                         sin6.sin6_addr = in6addr_any;
128                         addrp = (struct sockaddr *)&sin6;
129                         addrlen = sizeof(struct sockaddr_in6);
130                         if (establishListen(port, addrp, addrlen) < 0)
131                                 BINDERROR();
132                 }
133 #endif
134                 sin.sin_addr.s_addr = INADDR_ANY;
135                 addrp = (struct sockaddr *)&sin;
136                 addrlen = sizeof(struct sockaddr_in);
137 #ifdef HAVE_IPV6
138                 if ((establishListen(port, addrp, addrlen) < 0) && !useIpv6) {
139 #else
140                 if (establishListen(port, addrp, addrlen) < 0) {
141 #endif
142                         BINDERROR();
143                 }
144         } else {
145                 struct hostent *he;
146                 DEBUG("binding to address for %s\n", param->value);
147                 if (!(he = gethostbyname(param->value))) {
148                         FATAL("can't lookup host \"%s\" at line %i\n",
149                               param->value, param->line);
150                 }
151                 switch (he->h_addrtype) {
152 #ifdef HAVE_IPV6
153                 case AF_INET6:
154                         if (!useIpv6) {
155                                 FATAL("no IPv6 support, but a IPv6 address "
156                                       "found for \"%s\" at line %i\n",
157                                       param->value, param->line);
158                         }
159                         memcpy((char *)&sin6.sin6_addr.s6_addr,
160                                (char *)he->h_addr, he->h_length);
161                         addrp = (struct sockaddr *)&sin6;
162                         addrlen = sizeof(struct sockaddr_in6);
163                         break;
164 #endif
165                 case AF_INET:
166                         memcpy((char *)&sin.sin_addr.s_addr,
167                                (char *)he->h_addr, he->h_length);
168                         addrp = (struct sockaddr *)&sin;
169                         addrlen = sizeof(struct sockaddr_in);
170                         break;
171                 default:
172                         FATAL("address type for \"%s\" is not IPv4 or IPv6 "
173                               "at line %i\n", param->value, param->line);
174                 }
176                 if (establishListen(port, addrp, addrlen) < 0)
177                         BINDERROR();
178         }
181 void listenOnPort(void)
183         int port = DEFAULT_PORT;
184         ConfigParam *param = getNextConfigParam(CONF_BIND_TO_ADDRESS, NULL);
185         ConfigParam *portParam = getConfigParam(CONF_PORT);
187         if (portParam) {
188                 char *test;
189                 port = strtol(portParam->value, &test, 10);
190                 if (port <= 0 || *test != '\0') {
191                         FATAL("%s \"%s\" specified at line %i is not a "
192                               "positive integer", CONF_PORT,
193                               portParam->value, portParam->line);
194                 }
195         }
197         boundPort = port;
199         do {
200                 parseListenConfigParam(port, param);
201         } while ((param = getNextConfigParam(CONF_BIND_TO_ADDRESS, param)));
204 void addListenSocketsToFdSet(fd_set * fds, int *fdmax)
206         int i;
208         for (i = 0; i < numberOfListenSockets; i++) {
209                 FD_SET(listenSockets[i], fds);
210                 if (listenSockets[i] > *fdmax)
211                         *fdmax = listenSockets[i];
212         }
215 void closeAllListenSockets(void)
217         int i;
219         DEBUG("closeAllListenSockets called\n");
221         for (i = 0; i < numberOfListenSockets; i++) {
222                 DEBUG("closing listen socket %i\n", i);
223                 while (close(listenSockets[i]) < 0 && errno == EINTR) ;
224         }
225         freeAllListenSockets();
228 void freeAllListenSockets(void)
230         numberOfListenSockets = 0;
231         free(listenSockets);
232         listenSockets = NULL;
235 void getConnections(fd_set * fds)
237         int i;
238         int fd = 0;
239         struct sockaddr sockAddr;
240         socklen_t socklen = sizeof(sockAddr);
242         for (i = 0; i < numberOfListenSockets; i++) {
243                 if (FD_ISSET(listenSockets[i], fds)) {
244                         if ((fd = accept(listenSockets[i], &sockAddr, &socklen))
245                             >= 0) {
246                                 openAInterface(fd, &sockAddr);
247                         } else if (fd < 0
248                                    && (errno != EAGAIN && errno != EINTR)) {
249                                 ERROR("Problems accept()'ing\n");
250                         }
251                 }
252         }