4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <sys/types.h>
35 #ifdef HAVE_SYS_SOCKET_H
36 #include <sys/socket.h>
41 #define SUN_LEN(ptr) \
42 ((size_t)(((struct sockaddr_un *) 0)->sun_path) + strlen((ptr)->sun_path))
45 #ifdef HAVE_ARPA_INET_H
46 #include <arpa/inet.h>
48 #ifdef HAVE_NETINET_IN_H
49 #include <netinet/in.h>
56 #ifndef HAVE_INET_NTOP
57 #include "inet_ntop.h"
60 #ifndef HAVE_INET_PTON
61 #include "inet_pton.h"
66 #include <pulse/xmalloc.h>
67 #include <pulse/util.h>
69 #include <pulsecore/socket-util.h>
70 #include <pulsecore/core-util.h>
71 #include <pulsecore/log.h>
72 #include <pulsecore/core-error.h>
74 #include "socket-server.h"
76 struct pa_socket_server {
80 char *tcpwrap_service;
82 void (*on_connection)(pa_socket_server*s, pa_iochannel *io, void *userdata);
85 pa_io_event *io_event;
86 pa_mainloop_api *mainloop;
87 enum { SOCKET_SERVER_GENERIC, SOCKET_SERVER_IPV4, SOCKET_SERVER_UNIX, SOCKET_SERVER_IPV6 } type;
90 static void callback(pa_mainloop_api *mainloop, pa_io_event *e, int fd, PA_GCC_UNUSED pa_io_event_flags_t f, void *userdata) {
91 pa_socket_server *s = userdata;
94 assert(s && s->mainloop == mainloop && s->io_event == e && e && fd >= 0 && fd == s->fd);
96 pa_socket_server_ref(s);
98 if ((nfd = accept(fd, NULL, NULL)) < 0) {
99 pa_log(__FILE__": accept(): %s", pa_cstrerror(errno));
103 pa_fd_set_cloexec(nfd, 1);
105 if (!s->on_connection) {
112 if (s->tcpwrap_service) {
113 struct request_info req;
115 request_init(&req, RQ_DAEMON, s->tcpwrap_service, RQ_FILE, nfd, NULL);
117 if (!hosts_access(&req)) {
118 pa_log_warn(__FILE__": TCP connection refused by tcpwrap.");
123 pa_log_info(__FILE__": TCP connection accepted by tcpwrap.");
127 /* There should be a check for socket type here */
128 if (s->type == SOCKET_SERVER_IPV4)
129 pa_socket_tcp_low_delay(fd);
131 pa_socket_low_delay(fd);
133 io = pa_iochannel_new(s->mainloop, nfd, nfd);
135 s->on_connection(s, io, s->userdata);
138 pa_socket_server_unref(s);
141 pa_socket_server* pa_socket_server_new(pa_mainloop_api *m, int fd) {
143 assert(m && fd >= 0);
145 s = pa_xmalloc(sizeof(pa_socket_server));
149 s->on_connection = NULL;
151 s->tcpwrap_service = NULL;
154 s->io_event = m->io_new(m, fd, PA_IO_EVENT_INPUT, callback, s);
157 s->type = SOCKET_SERVER_GENERIC;
162 pa_socket_server* pa_socket_server_ref(pa_socket_server *s) {
163 assert(s && s->ref >= 1);
170 pa_socket_server* pa_socket_server_new_unix(pa_mainloop_api *m, const char *filename) {
172 struct sockaddr_un sa;
175 assert(m && filename);
177 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
178 pa_log(__FILE__": socket(): %s", pa_cstrerror(errno));
182 pa_fd_set_cloexec(fd, 1);
184 sa.sun_family = AF_UNIX;
185 strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
186 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
188 pa_socket_low_delay(fd);
190 if (bind(fd, (struct sockaddr*) &sa, SUN_LEN(&sa)) < 0) {
191 pa_log(__FILE__": bind(): %s", pa_cstrerror(errno));
195 /* Allow access from all clients. Sockets like this one should
196 * always be put inside a directory with proper access rights,
197 * because not all OS check the access rights on the socket
199 chmod(filename, 0777);
201 if (listen(fd, 5) < 0) {
202 pa_log(__FILE__": listen(): %s", pa_cstrerror(errno));
206 s = pa_socket_server_new(m, fd);
209 s->filename = pa_xstrdup(filename);
210 s->type = SOCKET_SERVER_UNIX;
221 #else /* HAVE_SYS_UN_H */
223 pa_socket_server* pa_socket_server_new_unix(pa_mainloop_api *m, const char *filename) {
227 #endif /* HAVE_SYS_UN_H */
229 pa_socket_server* pa_socket_server_new_ipv4(pa_mainloop_api *m, uint32_t address, uint16_t port, const char *tcpwrap_service) {
230 pa_socket_server *ss;
232 struct sockaddr_in sa;
237 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
238 pa_log(__FILE__": socket(PF_INET): %s", pa_cstrerror(errno));
242 pa_fd_set_cloexec(fd, 1);
245 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
246 pa_log(__FILE__": setsockopt(): %s", pa_cstrerror(errno));
249 pa_socket_tcp_low_delay(fd);
251 memset(&sa, 0, sizeof(sa));
252 sa.sin_family = AF_INET;
253 sa.sin_port = htons(port);
254 sa.sin_addr.s_addr = htonl(address);
256 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
257 pa_log(__FILE__": bind(): %s", pa_cstrerror(errno));
261 if (listen(fd, 5) < 0) {
262 pa_log(__FILE__": listen(): %s", pa_cstrerror(errno));
266 if ((ss = pa_socket_server_new(m, fd))) {
267 ss->type = SOCKET_SERVER_IPV4;
268 ss->tcpwrap_service = pa_xstrdup(tcpwrap_service);
280 pa_socket_server* pa_socket_server_new_ipv6(pa_mainloop_api *m, const uint8_t address[16], uint16_t port, const char *tcpwrap_service) {
281 pa_socket_server *ss;
283 struct sockaddr_in6 sa;
288 if ((fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0) {
289 pa_log(__FILE__": socket(PF_INET6): %s", pa_cstrerror(errno));
293 pa_fd_set_cloexec(fd, 1);
296 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
297 pa_log(__FILE__": setsockopt(IPPROTO_IPV6, IPV6_V6ONLY): %s", pa_cstrerror(errno));
301 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
302 pa_log(__FILE__": setsockopt(SOL_SOCKET, SO_REUSEADDR, 1): %s", pa_cstrerror(errno));
305 pa_socket_tcp_low_delay(fd);
307 memset(&sa, 0, sizeof(sa));
308 sa.sin6_family = AF_INET6;
309 sa.sin6_port = htons(port);
310 memcpy(sa.sin6_addr.s6_addr, address, 16);
312 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
313 pa_log(__FILE__": bind(): %s", pa_cstrerror(errno));
317 if (listen(fd, 5) < 0) {
318 pa_log(__FILE__": listen(): %s", pa_cstrerror(errno));
322 if ((ss = pa_socket_server_new(m, fd))) {
323 ss->type = SOCKET_SERVER_IPV6;
324 ss->tcpwrap_service = pa_xstrdup(tcpwrap_service);
336 pa_socket_server* pa_socket_server_new_ipv4_loopback(pa_mainloop_api *m, uint16_t port, const char *tcpwrap_service) {
340 return pa_socket_server_new_ipv4(m, INADDR_LOOPBACK, port, tcpwrap_service);
343 pa_socket_server* pa_socket_server_new_ipv6_loopback(pa_mainloop_api *m, uint16_t port, const char *tcpwrap_service) {
347 return pa_socket_server_new_ipv6(m, in6addr_loopback.s6_addr, port, tcpwrap_service);
350 pa_socket_server* pa_socket_server_new_ipv4_any(pa_mainloop_api *m, uint16_t port, const char *tcpwrap_service) {
354 return pa_socket_server_new_ipv4(m, INADDR_ANY, port, tcpwrap_service);
357 pa_socket_server* pa_socket_server_new_ipv6_any(pa_mainloop_api *m, uint16_t port, const char *tcpwrap_service) {
361 return pa_socket_server_new_ipv6(m, in6addr_any.s6_addr, port, tcpwrap_service);
364 pa_socket_server* pa_socket_server_new_ipv4_string(pa_mainloop_api *m, const char *name, uint16_t port, const char *tcpwrap_service) {
371 if (inet_pton(AF_INET, name, &ipv4) > 0)
372 return pa_socket_server_new_ipv4(m, ntohl(ipv4.s_addr), port, tcpwrap_service);
377 pa_socket_server* pa_socket_server_new_ipv6_string(pa_mainloop_api *m, const char *name, uint16_t port, const char *tcpwrap_service) {
378 struct in6_addr ipv6;
384 if (inet_pton(AF_INET6, name, &ipv6) > 0)
385 return pa_socket_server_new_ipv6(m, ipv6.s6_addr, port, tcpwrap_service);
390 static void socket_server_free(pa_socket_server*s) {
395 pa_xfree(s->filename);
400 pa_xfree(s->tcpwrap_service);
402 s->mainloop->io_free(s->io_event);
406 void pa_socket_server_unref(pa_socket_server *s) {
407 assert(s && s->ref >= 1);
410 socket_server_free(s);
413 void pa_socket_server_set_callback(pa_socket_server*s, void (*on_connection)(pa_socket_server*s, pa_iochannel *io, void *userdata), void *userdata) {
414 assert(s && s->ref >= 1);
416 s->on_connection = on_connection;
417 s->userdata = userdata;
420 char *pa_socket_server_get_address(pa_socket_server *s, char *c, size_t l) {
421 assert(s && c && l > 0);
424 case SOCKET_SERVER_IPV6: {
425 struct sockaddr_in6 sa;
426 socklen_t sa_len = sizeof(sa);
428 if (getsockname(s->fd, (struct sockaddr*) &sa, &sa_len) < 0) {
429 pa_log(__FILE__": getsockname(): %s", pa_cstrerror(errno));
433 if (memcmp(&in6addr_any, &sa.sin6_addr, sizeof(in6addr_any)) == 0) {
435 if (!pa_get_fqdn(fqdn, sizeof(fqdn)))
438 snprintf(c, l, "tcp6:%s:%u", fqdn, (unsigned) ntohs(sa.sin6_port));
440 } else if (memcmp(&in6addr_loopback, &sa.sin6_addr, sizeof(in6addr_loopback)) == 0) {
442 if (!pa_get_host_name(hn, sizeof(hn)))
445 snprintf(c, l, "{%s}tcp6:localhost:%u", hn, (unsigned) ntohs(sa.sin6_port));
447 char ip[INET6_ADDRSTRLEN];
449 if (!inet_ntop(AF_INET6, &sa.sin6_addr, ip, sizeof(ip))) {
450 pa_log(__FILE__": inet_ntop(): %s", pa_cstrerror(errno));
454 snprintf(c, l, "tcp6:[%s]:%u", ip, (unsigned) ntohs(sa.sin6_port));
460 case SOCKET_SERVER_IPV4: {
461 struct sockaddr_in sa;
462 socklen_t sa_len = sizeof(sa);
464 if (getsockname(s->fd, (struct sockaddr*) &sa, &sa_len) < 0) {
465 pa_log(__FILE__": getsockname(): %s", pa_cstrerror(errno));
469 if (sa.sin_addr.s_addr == INADDR_ANY) {
471 if (!pa_get_fqdn(fqdn, sizeof(fqdn)))
474 snprintf(c, l, "tcp:%s:%u", fqdn, (unsigned) ntohs(sa.sin_port));
475 } else if (sa.sin_addr.s_addr == INADDR_LOOPBACK) {
477 if (!pa_get_host_name(hn, sizeof(hn)))
480 snprintf(c, l, "{%s}tcp:localhost:%u", hn, (unsigned) ntohs(sa.sin_port));
482 char ip[INET_ADDRSTRLEN];
484 if (!inet_ntop(AF_INET, &sa.sin_addr, ip, sizeof(ip))) {
485 pa_log(__FILE__": inet_ntop(): %s", pa_cstrerror(errno));
489 snprintf(c, l, "tcp:[%s]:%u", ip, (unsigned) ntohs(sa.sin_port));
496 case SOCKET_SERVER_UNIX: {
502 if (!pa_get_host_name(hn, sizeof(hn)))
505 snprintf(c, l, "{%s}unix:%s", hn, s->filename);