Add a missing pa_xfree.
[pulseaudio.git] / src / pulsecore / socket-server.c
blob162a1aac2f8f9e7cf802d9e846ffb800550b1a70
1 /* $Id$ */
3 /***
4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <sys/stat.h>
37 #ifdef HAVE_SYS_SOCKET_H
38 #include <sys/socket.h>
39 #endif
40 #ifdef HAVE_SYS_UN_H
41 #include <sys/un.h>
42 #ifndef SUN_LEN
43 #define SUN_LEN(ptr) \
44 ((size_t)(((struct sockaddr_un *) 0)->sun_path) + strlen((ptr)->sun_path))
45 #endif
46 #endif
47 #ifdef HAVE_ARPA_INET_H
48 #include <arpa/inet.h>
49 #endif
50 #ifdef HAVE_NETINET_IN_H
51 #include <netinet/in.h>
52 #endif
54 #ifdef HAVE_LIBWRAP
55 #include <tcpd.h>
56 #endif
58 #ifndef HAVE_INET_NTOP
59 #include "inet_ntop.h"
60 #endif
62 #ifndef HAVE_INET_PTON
63 #include "inet_pton.h"
64 #endif
66 #include "winsock.h"
68 #include <pulse/xmalloc.h>
69 #include <pulse/util.h>
71 #include <pulsecore/socket-util.h>
72 #include <pulsecore/core-util.h>
73 #include <pulsecore/log.h>
74 #include <pulsecore/macro.h>
75 #include <pulsecore/core-error.h>
76 #include <pulsecore/refcnt.h>
78 #include "socket-server.h"
80 struct pa_socket_server {
81 PA_REFCNT_DECLARE;
82 int fd;
83 char *filename;
84 char *tcpwrap_service;
86 void (*on_connection)(pa_socket_server*s, pa_iochannel *io, void *userdata);
87 void *userdata;
89 pa_io_event *io_event;
90 pa_mainloop_api *mainloop;
91 enum { SOCKET_SERVER_GENERIC, SOCKET_SERVER_IPV4, SOCKET_SERVER_UNIX, SOCKET_SERVER_IPV6 } type;
94 static void callback(pa_mainloop_api *mainloop, pa_io_event *e, int fd, PA_GCC_UNUSED pa_io_event_flags_t f, void *userdata) {
95 pa_socket_server *s = userdata;
96 pa_iochannel *io;
97 int nfd;
99 pa_assert(s);
100 pa_assert(PA_REFCNT_VALUE(s) >= 1);
101 pa_assert(s->mainloop == mainloop);
102 pa_assert(s->io_event == e);
103 pa_assert(e);
104 pa_assert(fd >= 0);
105 pa_assert(fd == s->fd);
107 pa_socket_server_ref(s);
109 if ((nfd = accept(fd, NULL, NULL)) < 0) {
110 pa_log("accept(): %s", pa_cstrerror(errno));
111 goto finish;
114 pa_make_fd_cloexec(nfd);
116 if (!s->on_connection) {
117 pa_close(nfd);
118 goto finish;
121 #ifdef HAVE_LIBWRAP
123 if (s->tcpwrap_service) {
124 struct request_info req;
126 request_init(&req, RQ_DAEMON, s->tcpwrap_service, RQ_FILE, nfd, NULL);
127 fromhost(&req);
128 if (!hosts_access(&req)) {
129 pa_log_warn("TCP connection refused by tcpwrap.");
130 pa_close(nfd);
131 goto finish;
134 pa_log_info("TCP connection accepted by tcpwrap.");
136 #endif
138 /* There should be a check for socket type here */
139 if (s->type == SOCKET_SERVER_IPV4)
140 pa_make_tcp_socket_low_delay(fd);
141 else
142 pa_make_socket_low_delay(fd);
144 pa_assert_se(io = pa_iochannel_new(s->mainloop, nfd, nfd));
145 s->on_connection(s, io, s->userdata);
147 finish:
148 pa_socket_server_unref(s);
151 pa_socket_server* pa_socket_server_new(pa_mainloop_api *m, int fd) {
152 pa_socket_server *s;
154 pa_assert(m);
155 pa_assert(fd >= 0);
157 s = pa_xnew(pa_socket_server, 1);
158 PA_REFCNT_INIT(s);
159 s->fd = fd;
160 s->filename = NULL;
161 s->on_connection = NULL;
162 s->userdata = NULL;
163 s->tcpwrap_service = NULL;
165 s->mainloop = m;
166 pa_assert_se(s->io_event = m->io_new(m, fd, PA_IO_EVENT_INPUT, callback, s));
168 s->type = SOCKET_SERVER_GENERIC;
170 return s;
173 pa_socket_server* pa_socket_server_ref(pa_socket_server *s) {
174 pa_assert(s);
175 pa_assert(PA_REFCNT_VALUE(s) >= 1);
177 PA_REFCNT_INC(s);
178 return s;
181 #ifdef HAVE_SYS_UN_H
183 pa_socket_server* pa_socket_server_new_unix(pa_mainloop_api *m, const char *filename) {
184 int fd = -1;
185 struct sockaddr_un sa;
186 pa_socket_server *s;
188 pa_assert(m);
189 pa_assert(filename);
191 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
192 pa_log("socket(): %s", pa_cstrerror(errno));
193 goto fail;
196 pa_make_fd_cloexec(fd);
198 sa.sun_family = AF_UNIX;
199 strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
200 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
202 pa_make_socket_low_delay(fd);
204 if (bind(fd, (struct sockaddr*) &sa, SUN_LEN(&sa)) < 0) {
205 pa_log("bind(): %s", pa_cstrerror(errno));
206 goto fail;
209 /* Allow access from all clients. Sockets like this one should
210 * always be put inside a directory with proper access rights,
211 * because not all OS check the access rights on the socket
212 * inodes. */
213 chmod(filename, 0777);
215 if (listen(fd, 5) < 0) {
216 pa_log("listen(): %s", pa_cstrerror(errno));
217 goto fail;
220 pa_assert_se(s = pa_socket_server_new(m, fd));
222 s->filename = pa_xstrdup(filename);
223 s->type = SOCKET_SERVER_UNIX;
225 return s;
227 fail:
228 if (fd >= 0)
229 pa_close(fd);
231 return NULL;
234 #else /* HAVE_SYS_UN_H */
236 pa_socket_server* pa_socket_server_new_unix(pa_mainloop_api *m, const char *filename) {
237 return NULL;
240 #endif /* HAVE_SYS_UN_H */
242 pa_socket_server* pa_socket_server_new_ipv4(pa_mainloop_api *m, uint32_t address, uint16_t port, const char *tcpwrap_service) {
243 pa_socket_server *ss;
244 int fd = -1;
245 struct sockaddr_in sa;
246 int on = 1;
248 pa_assert(m);
249 pa_assert(port);
251 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
252 pa_log("socket(PF_INET): %s", pa_cstrerror(errno));
253 goto fail;
256 pa_make_fd_cloexec(fd);
258 #ifdef SO_REUSEADDR
259 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
260 pa_log("setsockopt(): %s", pa_cstrerror(errno));
261 #endif
263 pa_make_tcp_socket_low_delay(fd);
265 memset(&sa, 0, sizeof(sa));
266 sa.sin_family = AF_INET;
267 sa.sin_port = htons(port);
268 sa.sin_addr.s_addr = htonl(address);
270 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
271 pa_log("bind(): %s", pa_cstrerror(errno));
272 goto fail;
275 if (listen(fd, 5) < 0) {
276 pa_log("listen(): %s", pa_cstrerror(errno));
277 goto fail;
280 if ((ss = pa_socket_server_new(m, fd))) {
281 ss->type = SOCKET_SERVER_IPV4;
282 ss->tcpwrap_service = pa_xstrdup(tcpwrap_service);
285 return ss;
287 fail:
288 if (fd >= 0)
289 pa_close(fd);
291 return NULL;
294 pa_socket_server* pa_socket_server_new_ipv6(pa_mainloop_api *m, const uint8_t address[16], uint16_t port, const char *tcpwrap_service) {
295 pa_socket_server *ss;
296 int fd = -1;
297 struct sockaddr_in6 sa;
298 int on = 1;
300 pa_assert(m);
301 pa_assert(port > 0);
303 if ((fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0) {
304 pa_log("socket(PF_INET6): %s", pa_cstrerror(errno));
305 goto fail;
308 pa_make_fd_cloexec(fd);
310 #ifdef IPV6_V6ONLY
311 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
312 pa_log("setsockopt(IPPROTO_IPV6, IPV6_V6ONLY): %s", pa_cstrerror(errno));
313 #endif
315 #ifdef SO_REUSEADDR
316 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
317 pa_log("setsockopt(SOL_SOCKET, SO_REUSEADDR, 1): %s", pa_cstrerror(errno));
318 #endif
320 pa_make_tcp_socket_low_delay(fd);
322 memset(&sa, 0, sizeof(sa));
323 sa.sin6_family = AF_INET6;
324 sa.sin6_port = htons(port);
325 memcpy(sa.sin6_addr.s6_addr, address, 16);
327 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
328 pa_log("bind(): %s", pa_cstrerror(errno));
329 goto fail;
332 if (listen(fd, 5) < 0) {
333 pa_log("listen(): %s", pa_cstrerror(errno));
334 goto fail;
337 if ((ss = pa_socket_server_new(m, fd))) {
338 ss->type = SOCKET_SERVER_IPV6;
339 ss->tcpwrap_service = pa_xstrdup(tcpwrap_service);
342 return ss;
344 fail:
345 if (fd >= 0)
346 pa_close(fd);
348 return NULL;
351 pa_socket_server* pa_socket_server_new_ipv4_loopback(pa_mainloop_api *m, uint16_t port, const char *tcpwrap_service) {
352 pa_assert(m);
353 pa_assert(port > 0);
355 return pa_socket_server_new_ipv4(m, INADDR_LOOPBACK, port, tcpwrap_service);
358 pa_socket_server* pa_socket_server_new_ipv6_loopback(pa_mainloop_api *m, uint16_t port, const char *tcpwrap_service) {
359 pa_assert(m);
360 pa_assert(port > 0);
362 return pa_socket_server_new_ipv6(m, in6addr_loopback.s6_addr, port, tcpwrap_service);
365 pa_socket_server* pa_socket_server_new_ipv4_any(pa_mainloop_api *m, uint16_t port, const char *tcpwrap_service) {
366 pa_assert(m);
367 pa_assert(port > 0);
369 return pa_socket_server_new_ipv4(m, INADDR_ANY, port, tcpwrap_service);
372 pa_socket_server* pa_socket_server_new_ipv6_any(pa_mainloop_api *m, uint16_t port, const char *tcpwrap_service) {
373 pa_assert(m);
374 pa_assert(port > 0);
376 return pa_socket_server_new_ipv6(m, in6addr_any.s6_addr, port, tcpwrap_service);
379 pa_socket_server* pa_socket_server_new_ipv4_string(pa_mainloop_api *m, const char *name, uint16_t port, const char *tcpwrap_service) {
380 struct in_addr ipv4;
382 pa_assert(m);
383 pa_assert(name);
384 pa_assert(port > 0);
386 if (inet_pton(AF_INET, name, &ipv4) > 0)
387 return pa_socket_server_new_ipv4(m, ntohl(ipv4.s_addr), port, tcpwrap_service);
389 return NULL;
392 pa_socket_server* pa_socket_server_new_ipv6_string(pa_mainloop_api *m, const char *name, uint16_t port, const char *tcpwrap_service) {
393 struct in6_addr ipv6;
395 pa_assert(m);
396 pa_assert(name);
397 pa_assert(port > 0);
399 if (inet_pton(AF_INET6, name, &ipv6) > 0)
400 return pa_socket_server_new_ipv6(m, ipv6.s6_addr, port, tcpwrap_service);
402 return NULL;
405 static void socket_server_free(pa_socket_server*s) {
406 pa_assert(s);
408 if (s->filename) {
409 unlink(s->filename);
410 pa_xfree(s->filename);
413 pa_close(s->fd);
415 pa_xfree(s->tcpwrap_service);
417 s->mainloop->io_free(s->io_event);
418 pa_xfree(s);
421 void pa_socket_server_unref(pa_socket_server *s) {
422 pa_assert(s);
423 pa_assert(PA_REFCNT_VALUE(s) >= 1);
425 if (PA_REFCNT_DEC(s) <= 0)
426 socket_server_free(s);
429 void pa_socket_server_set_callback(pa_socket_server*s, void (*on_connection)(pa_socket_server*s, pa_iochannel *io, void *userdata), void *userdata) {
430 pa_assert(s);
431 pa_assert(PA_REFCNT_VALUE(s) >= 1);
433 s->on_connection = on_connection;
434 s->userdata = userdata;
437 char *pa_socket_server_get_address(pa_socket_server *s, char *c, size_t l) {
438 pa_assert(s);
439 pa_assert(PA_REFCNT_VALUE(s) >= 1);
440 pa_assert(c);
441 pa_assert(l > 0);
443 switch (s->type) {
444 case SOCKET_SERVER_IPV6: {
445 struct sockaddr_in6 sa;
446 socklen_t sa_len = sizeof(sa);
448 if (getsockname(s->fd, (struct sockaddr*) &sa, &sa_len) < 0) {
449 pa_log("getsockname(): %s", pa_cstrerror(errno));
450 return NULL;
453 if (memcmp(&in6addr_any, &sa.sin6_addr, sizeof(in6addr_any)) == 0) {
454 char fqdn[256];
455 if (!pa_get_fqdn(fqdn, sizeof(fqdn)))
456 return NULL;
458 pa_snprintf(c, l, "tcp6:%s:%u", fqdn, (unsigned) ntohs(sa.sin6_port));
460 } else if (memcmp(&in6addr_loopback, &sa.sin6_addr, sizeof(in6addr_loopback)) == 0) {
461 char hn[256];
462 if (!pa_get_host_name(hn, sizeof(hn)))
463 return NULL;
465 pa_snprintf(c, l, "{%s}tcp6:localhost:%u", hn, (unsigned) ntohs(sa.sin6_port));
466 } else {
467 char ip[INET6_ADDRSTRLEN];
469 if (!inet_ntop(AF_INET6, &sa.sin6_addr, ip, sizeof(ip))) {
470 pa_log("inet_ntop(): %s", pa_cstrerror(errno));
471 return NULL;
474 pa_snprintf(c, l, "tcp6:[%s]:%u", ip, (unsigned) ntohs(sa.sin6_port));
477 return c;
480 case SOCKET_SERVER_IPV4: {
481 struct sockaddr_in sa;
482 socklen_t sa_len = sizeof(sa);
484 if (getsockname(s->fd, (struct sockaddr*) &sa, &sa_len) < 0) {
485 pa_log("getsockname(): %s", pa_cstrerror(errno));
486 return NULL;
489 if (sa.sin_addr.s_addr == INADDR_ANY) {
490 char fqdn[256];
491 if (!pa_get_fqdn(fqdn, sizeof(fqdn)))
492 return NULL;
494 pa_snprintf(c, l, "tcp:%s:%u", fqdn, (unsigned) ntohs(sa.sin_port));
495 } else if (sa.sin_addr.s_addr == INADDR_LOOPBACK) {
496 char hn[256];
497 if (!pa_get_host_name(hn, sizeof(hn)))
498 return NULL;
500 pa_snprintf(c, l, "{%s}tcp:localhost:%u", hn, (unsigned) ntohs(sa.sin_port));
501 } else {
502 char ip[INET_ADDRSTRLEN];
504 if (!inet_ntop(AF_INET, &sa.sin_addr, ip, sizeof(ip))) {
505 pa_log("inet_ntop(): %s", pa_cstrerror(errno));
506 return NULL;
509 pa_snprintf(c, l, "tcp:[%s]:%u", ip, (unsigned) ntohs(sa.sin_port));
513 return c;
516 case SOCKET_SERVER_UNIX: {
517 char hn[256];
519 if (!s->filename)
520 return NULL;
522 if (!pa_get_host_name(hn, sizeof(hn)))
523 return NULL;
525 pa_snprintf(c, l, "{%s}unix:%s", hn, s->filename);
526 return c;
529 default:
530 return NULL;