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
33 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
44 #define SUN_LEN(ptr) \
45 ((size_t)(((struct sockaddr_un *) 0)->sun_path) + strlen((ptr)->sun_path))
48 #ifdef HAVE_ARPA_INET_H
49 #include <arpa/inet.h>
51 #ifdef HAVE_NETINET_IN_H
52 #include <netinet/in.h>
59 #ifndef HAVE_INET_NTOP
60 #include "inet_ntop.h"
63 #ifndef HAVE_INET_PTON
64 #include "inet_pton.h"
69 #include <pulse/xmalloc.h>
70 #include <pulse/util.h>
72 #include <pulsecore/socket-util.h>
73 #include <pulsecore/core-util.h>
74 #include <pulsecore/log.h>
75 #include <pulsecore/core-error.h>
77 #include "socket-server.h"
79 struct pa_socket_server
{
83 char *tcpwrap_service
;
85 void (*on_connection
)(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
);
88 pa_io_event
*io_event
;
89 pa_mainloop_api
*mainloop
;
90 enum { SOCKET_SERVER_GENERIC
, SOCKET_SERVER_IPV4
, SOCKET_SERVER_UNIX
, SOCKET_SERVER_IPV6
} type
;
93 static void callback(pa_mainloop_api
*mainloop
, pa_io_event
*e
, int fd
, PA_GCC_UNUSED pa_io_event_flags_t f
, void *userdata
) {
94 pa_socket_server
*s
= userdata
;
97 assert(s
&& s
->mainloop
== mainloop
&& s
->io_event
== e
&& e
&& fd
>= 0 && fd
== s
->fd
);
99 pa_socket_server_ref(s
);
101 if ((nfd
= accept(fd
, NULL
, NULL
)) < 0) {
102 pa_log("accept(): %s", pa_cstrerror(errno
));
106 pa_fd_set_cloexec(nfd
, 1);
108 if (!s
->on_connection
) {
115 if (s
->tcpwrap_service
) {
116 struct request_info req
;
118 request_init(&req
, RQ_DAEMON
, s
->tcpwrap_service
, RQ_FILE
, nfd
, NULL
);
120 if (!hosts_access(&req
)) {
121 pa_log_warn("TCP connection refused by tcpwrap.");
126 pa_log_info("TCP connection accepted by tcpwrap.");
130 /* There should be a check for socket type here */
131 if (s
->type
== SOCKET_SERVER_IPV4
)
132 pa_socket_tcp_low_delay(fd
);
134 pa_socket_low_delay(fd
);
136 io
= pa_iochannel_new(s
->mainloop
, nfd
, nfd
);
138 s
->on_connection(s
, io
, s
->userdata
);
141 pa_socket_server_unref(s
);
144 pa_socket_server
* pa_socket_server_new(pa_mainloop_api
*m
, int fd
) {
146 assert(m
&& fd
>= 0);
148 s
= pa_xmalloc(sizeof(pa_socket_server
));
152 s
->on_connection
= NULL
;
154 s
->tcpwrap_service
= NULL
;
157 s
->io_event
= m
->io_new(m
, fd
, PA_IO_EVENT_INPUT
, callback
, s
);
160 s
->type
= SOCKET_SERVER_GENERIC
;
165 pa_socket_server
* pa_socket_server_ref(pa_socket_server
*s
) {
166 assert(s
&& s
->ref
>= 1);
173 pa_socket_server
* pa_socket_server_new_unix(pa_mainloop_api
*m
, const char *filename
) {
175 struct sockaddr_un sa
;
178 assert(m
&& filename
);
180 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
181 pa_log("socket(): %s", pa_cstrerror(errno
));
185 pa_fd_set_cloexec(fd
, 1);
187 sa
.sun_family
= AF_UNIX
;
188 strncpy(sa
.sun_path
, filename
, sizeof(sa
.sun_path
)-1);
189 sa
.sun_path
[sizeof(sa
.sun_path
) - 1] = 0;
191 pa_socket_low_delay(fd
);
193 if (bind(fd
, (struct sockaddr
*) &sa
, SUN_LEN(&sa
)) < 0) {
194 pa_log("bind(): %s", pa_cstrerror(errno
));
198 /* Allow access from all clients. Sockets like this one should
199 * always be put inside a directory with proper access rights,
200 * because not all OS check the access rights on the socket
202 chmod(filename
, 0777);
204 if (listen(fd
, 5) < 0) {
205 pa_log("listen(): %s", pa_cstrerror(errno
));
209 s
= pa_socket_server_new(m
, fd
);
212 s
->filename
= pa_xstrdup(filename
);
213 s
->type
= SOCKET_SERVER_UNIX
;
224 #else /* HAVE_SYS_UN_H */
226 pa_socket_server
* pa_socket_server_new_unix(pa_mainloop_api
*m
, const char *filename
) {
230 #endif /* HAVE_SYS_UN_H */
232 pa_socket_server
* pa_socket_server_new_ipv4(pa_mainloop_api
*m
, uint32_t address
, uint16_t port
, const char *tcpwrap_service
) {
233 pa_socket_server
*ss
;
235 struct sockaddr_in sa
;
240 if ((fd
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
241 pa_log("socket(PF_INET): %s", pa_cstrerror(errno
));
245 pa_fd_set_cloexec(fd
, 1);
248 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
)) < 0)
249 pa_log("setsockopt(): %s", pa_cstrerror(errno
));
252 pa_socket_tcp_low_delay(fd
);
254 memset(&sa
, 0, sizeof(sa
));
255 sa
.sin_family
= AF_INET
;
256 sa
.sin_port
= htons(port
);
257 sa
.sin_addr
.s_addr
= htonl(address
);
259 if (bind(fd
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0) {
260 pa_log("bind(): %s", pa_cstrerror(errno
));
264 if (listen(fd
, 5) < 0) {
265 pa_log("listen(): %s", pa_cstrerror(errno
));
269 if ((ss
= pa_socket_server_new(m
, fd
))) {
270 ss
->type
= SOCKET_SERVER_IPV4
;
271 ss
->tcpwrap_service
= pa_xstrdup(tcpwrap_service
);
283 pa_socket_server
* pa_socket_server_new_ipv6(pa_mainloop_api
*m
, const uint8_t address
[16], uint16_t port
, const char *tcpwrap_service
) {
284 pa_socket_server
*ss
;
286 struct sockaddr_in6 sa
;
291 if ((fd
= socket(PF_INET6
, SOCK_STREAM
, 0)) < 0) {
292 pa_log("socket(PF_INET6): %s", pa_cstrerror(errno
));
296 pa_fd_set_cloexec(fd
, 1);
299 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &on
, sizeof(on
)) < 0)
300 pa_log("setsockopt(IPPROTO_IPV6, IPV6_V6ONLY): %s", pa_cstrerror(errno
));
304 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
)) < 0)
305 pa_log("setsockopt(SOL_SOCKET, SO_REUSEADDR, 1): %s", pa_cstrerror(errno
));
308 pa_socket_tcp_low_delay(fd
);
310 memset(&sa
, 0, sizeof(sa
));
311 sa
.sin6_family
= AF_INET6
;
312 sa
.sin6_port
= htons(port
);
313 memcpy(sa
.sin6_addr
.s6_addr
, address
, 16);
315 if (bind(fd
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0) {
316 pa_log("bind(): %s", pa_cstrerror(errno
));
320 if (listen(fd
, 5) < 0) {
321 pa_log("listen(): %s", pa_cstrerror(errno
));
325 if ((ss
= pa_socket_server_new(m
, fd
))) {
326 ss
->type
= SOCKET_SERVER_IPV6
;
327 ss
->tcpwrap_service
= pa_xstrdup(tcpwrap_service
);
339 pa_socket_server
* pa_socket_server_new_ipv4_loopback(pa_mainloop_api
*m
, uint16_t port
, const char *tcpwrap_service
) {
343 return pa_socket_server_new_ipv4(m
, INADDR_LOOPBACK
, port
, tcpwrap_service
);
346 pa_socket_server
* pa_socket_server_new_ipv6_loopback(pa_mainloop_api
*m
, uint16_t port
, const char *tcpwrap_service
) {
350 return pa_socket_server_new_ipv6(m
, in6addr_loopback
.s6_addr
, port
, tcpwrap_service
);
353 pa_socket_server
* pa_socket_server_new_ipv4_any(pa_mainloop_api
*m
, uint16_t port
, const char *tcpwrap_service
) {
357 return pa_socket_server_new_ipv4(m
, INADDR_ANY
, port
, tcpwrap_service
);
360 pa_socket_server
* pa_socket_server_new_ipv6_any(pa_mainloop_api
*m
, uint16_t port
, const char *tcpwrap_service
) {
364 return pa_socket_server_new_ipv6(m
, in6addr_any
.s6_addr
, port
, tcpwrap_service
);
367 pa_socket_server
* pa_socket_server_new_ipv4_string(pa_mainloop_api
*m
, const char *name
, uint16_t port
, const char *tcpwrap_service
) {
374 if (inet_pton(AF_INET
, name
, &ipv4
) > 0)
375 return pa_socket_server_new_ipv4(m
, ntohl(ipv4
.s_addr
), port
, tcpwrap_service
);
380 pa_socket_server
* pa_socket_server_new_ipv6_string(pa_mainloop_api
*m
, const char *name
, uint16_t port
, const char *tcpwrap_service
) {
381 struct in6_addr ipv6
;
387 if (inet_pton(AF_INET6
, name
, &ipv6
) > 0)
388 return pa_socket_server_new_ipv6(m
, ipv6
.s6_addr
, port
, tcpwrap_service
);
393 static void socket_server_free(pa_socket_server
*s
) {
398 pa_xfree(s
->filename
);
403 pa_xfree(s
->tcpwrap_service
);
405 s
->mainloop
->io_free(s
->io_event
);
409 void pa_socket_server_unref(pa_socket_server
*s
) {
410 assert(s
&& s
->ref
>= 1);
413 socket_server_free(s
);
416 void pa_socket_server_set_callback(pa_socket_server
*s
, void (*on_connection
)(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
), void *userdata
) {
417 assert(s
&& s
->ref
>= 1);
419 s
->on_connection
= on_connection
;
420 s
->userdata
= userdata
;
423 char *pa_socket_server_get_address(pa_socket_server
*s
, char *c
, size_t l
) {
424 assert(s
&& c
&& l
> 0);
427 case SOCKET_SERVER_IPV6
: {
428 struct sockaddr_in6 sa
;
429 socklen_t sa_len
= sizeof(sa
);
431 if (getsockname(s
->fd
, (struct sockaddr
*) &sa
, &sa_len
) < 0) {
432 pa_log("getsockname(): %s", pa_cstrerror(errno
));
436 if (memcmp(&in6addr_any
, &sa
.sin6_addr
, sizeof(in6addr_any
)) == 0) {
438 if (!pa_get_fqdn(fqdn
, sizeof(fqdn
)))
441 snprintf(c
, l
, "tcp6:%s:%u", fqdn
, (unsigned) ntohs(sa
.sin6_port
));
443 } else if (memcmp(&in6addr_loopback
, &sa
.sin6_addr
, sizeof(in6addr_loopback
)) == 0) {
445 if (!pa_get_host_name(hn
, sizeof(hn
)))
448 snprintf(c
, l
, "{%s}tcp6:localhost:%u", hn
, (unsigned) ntohs(sa
.sin6_port
));
450 char ip
[INET6_ADDRSTRLEN
];
452 if (!inet_ntop(AF_INET6
, &sa
.sin6_addr
, ip
, sizeof(ip
))) {
453 pa_log("inet_ntop(): %s", pa_cstrerror(errno
));
457 snprintf(c
, l
, "tcp6:[%s]:%u", ip
, (unsigned) ntohs(sa
.sin6_port
));
463 case SOCKET_SERVER_IPV4
: {
464 struct sockaddr_in sa
;
465 socklen_t sa_len
= sizeof(sa
);
467 if (getsockname(s
->fd
, (struct sockaddr
*) &sa
, &sa_len
) < 0) {
468 pa_log("getsockname(): %s", pa_cstrerror(errno
));
472 if (sa
.sin_addr
.s_addr
== INADDR_ANY
) {
474 if (!pa_get_fqdn(fqdn
, sizeof(fqdn
)))
477 snprintf(c
, l
, "tcp:%s:%u", fqdn
, (unsigned) ntohs(sa
.sin_port
));
478 } else if (sa
.sin_addr
.s_addr
== INADDR_LOOPBACK
) {
480 if (!pa_get_host_name(hn
, sizeof(hn
)))
483 snprintf(c
, l
, "{%s}tcp:localhost:%u", hn
, (unsigned) ntohs(sa
.sin_port
));
485 char ip
[INET_ADDRSTRLEN
];
487 if (!inet_ntop(AF_INET
, &sa
.sin_addr
, ip
, sizeof(ip
))) {
488 pa_log("inet_ntop(): %s", pa_cstrerror(errno
));
492 snprintf(c
, l
, "tcp:[%s]:%u", ip
, (unsigned) ntohs(sa
.sin_port
));
499 case SOCKET_SERVER_UNIX
: {
505 if (!pa_get_host_name(hn
, sizeof(hn
)))
508 snprintf(c
, l
, "{%s}unix:%s", hn
, s
->filename
);