don't send SCM_CREDENTIALS on every sendmsg(), instead do it only on handshake
[pulseaudio.git] / src / pulsecore / socket-server.c
blob77ea13e70ef8383e0f22985c06b13cfe5588861a
1 /* $Id$ */
3 /***
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
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <stdio.h>
32 #include <unistd.h>
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
36 #endif
37 #ifdef HAVE_SYS_UN_H
38 #include <sys/un.h>
39 #ifndef SUN_LEN
40 #define SUN_LEN(ptr) \
41 ((size_t)(((struct sockaddr_un *) 0)->sun_path) + strlen((ptr)->sun_path))
42 #endif
43 #endif
44 #ifdef HAVE_ARPA_INET_H
45 #include <arpa/inet.h>
46 #endif
47 #ifdef HAVE_NETINET_IN_H
48 #include <netinet/in.h>
49 #endif
51 #ifdef HAVE_LIBWRAP
52 #include <tcpd.h>
53 #endif
55 #ifndef HAVE_INET_NTOP
56 #include "inet_ntop.h"
57 #endif
59 #ifndef HAVE_INET_PTON
60 #include "inet_pton.h"
61 #endif
63 #include "winsock.h"
65 #include <pulse/xmalloc.h>
66 #include <pulse/util.h>
68 #include <pulsecore/socket-util.h>
69 #include <pulsecore/core-util.h>
70 #include <pulsecore/log.h>
71 #include <pulsecore/core-error.h>
73 #include "socket-server.h"
75 struct pa_socket_server {
76 int ref;
77 int fd;
78 char *filename;
79 char *tcpwrap_service;
81 void (*on_connection)(pa_socket_server*s, pa_iochannel *io, void *userdata);
82 void *userdata;
84 pa_io_event *io_event;
85 pa_mainloop_api *mainloop;
86 enum { SOCKET_SERVER_GENERIC, SOCKET_SERVER_IPV4, SOCKET_SERVER_UNIX, SOCKET_SERVER_IPV6 } type;
89 static void callback(pa_mainloop_api *mainloop, pa_io_event *e, int fd, PA_GCC_UNUSED pa_io_event_flags_t f, void *userdata) {
90 pa_socket_server *s = userdata;
91 pa_iochannel *io;
92 int nfd;
93 assert(s && s->mainloop == mainloop && s->io_event == e && e && fd >= 0 && fd == s->fd);
95 pa_socket_server_ref(s);
97 if ((nfd = accept(fd, NULL, NULL)) < 0) {
98 pa_log(__FILE__": accept(): %s", pa_cstrerror(errno));
99 goto finish;
102 pa_fd_set_cloexec(nfd, 1);
104 if (!s->on_connection) {
105 close(nfd);
106 goto finish;
109 #ifdef HAVE_LIBWRAP
111 if (s->tcpwrap_service) {
112 struct request_info req;
114 request_init(&req, RQ_DAEMON, s->tcpwrap_service, RQ_FILE, nfd, NULL);
115 fromhost(&req);
116 if (!hosts_access(&req)) {
117 pa_log_warn(__FILE__": TCP connection refused by tcpwrap.");
118 close(nfd);
119 goto finish;
122 pa_log_info(__FILE__": TCP connection accepted by tcpwrap.");
124 #endif
126 /* There should be a check for socket type here */
127 if (s->type == SOCKET_SERVER_IPV4)
128 pa_socket_tcp_low_delay(fd);
129 else
130 pa_socket_low_delay(fd);
132 io = pa_iochannel_new(s->mainloop, nfd, nfd);
133 assert(io);
134 s->on_connection(s, io, s->userdata);
136 finish:
137 pa_socket_server_unref(s);
140 pa_socket_server* pa_socket_server_new(pa_mainloop_api *m, int fd) {
141 pa_socket_server *s;
142 assert(m && fd >= 0);
144 s = pa_xmalloc(sizeof(pa_socket_server));
145 s->ref = 1;
146 s->fd = fd;
147 s->filename = NULL;
148 s->on_connection = NULL;
149 s->userdata = NULL;
150 s->tcpwrap_service = NULL;
152 s->mainloop = m;
153 s->io_event = m->io_new(m, fd, PA_IO_EVENT_INPUT, callback, s);
154 assert(s->io_event);
156 s->type = SOCKET_SERVER_GENERIC;
158 return s;
161 pa_socket_server* pa_socket_server_ref(pa_socket_server *s) {
162 assert(s && s->ref >= 1);
163 s->ref++;
164 return s;
167 #ifdef HAVE_SYS_UN_H
169 pa_socket_server* pa_socket_server_new_unix(pa_mainloop_api *m, const char *filename) {
170 int fd = -1;
171 struct sockaddr_un sa;
172 pa_socket_server *s;
174 assert(m && filename);
176 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
177 pa_log(__FILE__": socket(): %s", pa_cstrerror(errno));
178 goto fail;
181 pa_fd_set_cloexec(fd, 1);
183 sa.sun_family = AF_UNIX;
184 strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
185 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
187 pa_socket_low_delay(fd);
189 if (bind(fd, (struct sockaddr*) &sa, SUN_LEN(&sa)) < 0) {
190 pa_log(__FILE__": bind(): %s", pa_cstrerror(errno));
191 goto fail;
194 if (listen(fd, 5) < 0) {
195 pa_log(__FILE__": listen(): %s", pa_cstrerror(errno));
196 goto fail;
199 s = pa_socket_server_new(m, fd);
200 assert(s);
202 s->filename = pa_xstrdup(filename);
203 s->type = SOCKET_SERVER_UNIX;
205 return s;
207 fail:
208 if (fd >= 0)
209 close(fd);
211 return NULL;
214 #else /* HAVE_SYS_UN_H */
216 pa_socket_server* pa_socket_server_new_unix(pa_mainloop_api *m, const char *filename) {
217 return NULL;
220 #endif /* HAVE_SYS_UN_H */
222 pa_socket_server* pa_socket_server_new_ipv4(pa_mainloop_api *m, uint32_t address, uint16_t port, const char *tcpwrap_service) {
223 pa_socket_server *ss;
224 int fd = -1;
225 struct sockaddr_in sa;
226 int on = 1;
228 assert(m && port);
230 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
231 pa_log(__FILE__": socket(PF_INET): %s", pa_cstrerror(errno));
232 goto fail;
235 pa_fd_set_cloexec(fd, 1);
237 #ifdef SO_REUSEADDR
238 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
239 pa_log(__FILE__": setsockopt(): %s", pa_cstrerror(errno));
240 #endif
242 pa_socket_tcp_low_delay(fd);
244 memset(&sa, 0, sizeof(sa));
245 sa.sin_family = AF_INET;
246 sa.sin_port = htons(port);
247 sa.sin_addr.s_addr = htonl(address);
249 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
250 pa_log(__FILE__": bind(): %s", pa_cstrerror(errno));
251 goto fail;
254 if (listen(fd, 5) < 0) {
255 pa_log(__FILE__": listen(): %s", pa_cstrerror(errno));
256 goto fail;
259 if ((ss = pa_socket_server_new(m, fd))) {
260 ss->type = SOCKET_SERVER_IPV4;
261 ss->tcpwrap_service = pa_xstrdup(tcpwrap_service);
264 return ss;
266 fail:
267 if (fd >= 0)
268 close(fd);
270 return NULL;
273 pa_socket_server* pa_socket_server_new_ipv6(pa_mainloop_api *m, const uint8_t address[16], uint16_t port, const char *tcpwrap_service) {
274 pa_socket_server *ss;
275 int fd = -1;
276 struct sockaddr_in6 sa;
277 int on = 1;
279 assert(m && port);
281 if ((fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0) {
282 pa_log(__FILE__": socket(PF_INET6): %s", pa_cstrerror(errno));
283 goto fail;
286 pa_fd_set_cloexec(fd, 1);
288 #ifdef IPV6_V6ONLY
289 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
290 pa_log(__FILE__": setsockopt(IPPROTO_IPV6, IPV6_V6ONLY): %s", pa_cstrerror(errno));
291 #endif
293 #ifdef SO_REUSEADDR
294 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
295 pa_log(__FILE__": setsockopt(SOL_SOCKET, SO_REUSEADDR, 1): %s", pa_cstrerror(errno));
296 #endif
298 pa_socket_tcp_low_delay(fd);
300 memset(&sa, 0, sizeof(sa));
301 sa.sin6_family = AF_INET6;
302 sa.sin6_port = htons(port);
303 memcpy(sa.sin6_addr.s6_addr, address, 16);
305 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
306 pa_log(__FILE__": bind(): %s", pa_cstrerror(errno));
307 goto fail;
310 if (listen(fd, 5) < 0) {
311 pa_log(__FILE__": listen(): %s", pa_cstrerror(errno));
312 goto fail;
315 if ((ss = pa_socket_server_new(m, fd))) {
316 ss->type = SOCKET_SERVER_IPV6;
317 ss->tcpwrap_service = pa_xstrdup(tcpwrap_service);
320 return ss;
322 fail:
323 if (fd >= 0)
324 close(fd);
326 return NULL;
329 pa_socket_server* pa_socket_server_new_ipv4_loopback(pa_mainloop_api *m, uint16_t port, const char *tcpwrap_service) {
330 assert(m);
331 assert(port > 0);
333 return pa_socket_server_new_ipv4(m, INADDR_LOOPBACK, port, tcpwrap_service);
336 pa_socket_server* pa_socket_server_new_ipv6_loopback(pa_mainloop_api *m, uint16_t port, const char *tcpwrap_service) {
337 assert(m);
338 assert(port > 0);
340 return pa_socket_server_new_ipv6(m, in6addr_loopback.s6_addr, port, tcpwrap_service);
343 pa_socket_server* pa_socket_server_new_ipv4_any(pa_mainloop_api *m, uint16_t port, const char *tcpwrap_service) {
344 assert(m);
345 assert(port > 0);
347 return pa_socket_server_new_ipv4(m, INADDR_ANY, port, tcpwrap_service);
350 pa_socket_server* pa_socket_server_new_ipv6_any(pa_mainloop_api *m, uint16_t port, const char *tcpwrap_service) {
351 assert(m);
352 assert(port > 0);
354 return pa_socket_server_new_ipv6(m, in6addr_any.s6_addr, port, tcpwrap_service);
357 pa_socket_server* pa_socket_server_new_ipv4_string(pa_mainloop_api *m, const char *name, uint16_t port, const char *tcpwrap_service) {
358 struct in_addr ipv4;
360 assert(m);
361 assert(name);
362 assert(port > 0);
364 if (inet_pton(AF_INET, name, &ipv4) > 0)
365 return pa_socket_server_new_ipv4(m, ntohl(ipv4.s_addr), port, tcpwrap_service);
367 return NULL;
370 pa_socket_server* pa_socket_server_new_ipv6_string(pa_mainloop_api *m, const char *name, uint16_t port, const char *tcpwrap_service) {
371 struct in6_addr ipv6;
373 assert(m);
374 assert(name);
375 assert(port > 0);
377 if (inet_pton(AF_INET6, name, &ipv6) > 0)
378 return pa_socket_server_new_ipv6(m, ipv6.s6_addr, port, tcpwrap_service);
380 return NULL;
383 static void socket_server_free(pa_socket_server*s) {
384 assert(s);
386 if (s->filename) {
387 unlink(s->filename);
388 pa_xfree(s->filename);
391 close(s->fd);
393 pa_xfree(s->tcpwrap_service);
395 s->mainloop->io_free(s->io_event);
396 pa_xfree(s);
399 void pa_socket_server_unref(pa_socket_server *s) {
400 assert(s && s->ref >= 1);
402 if (!(--(s->ref)))
403 socket_server_free(s);
406 void pa_socket_server_set_callback(pa_socket_server*s, void (*on_connection)(pa_socket_server*s, pa_iochannel *io, void *userdata), void *userdata) {
407 assert(s && s->ref >= 1);
409 s->on_connection = on_connection;
410 s->userdata = userdata;
413 char *pa_socket_server_get_address(pa_socket_server *s, char *c, size_t l) {
414 assert(s && c && l > 0);
416 switch (s->type) {
417 case SOCKET_SERVER_IPV6: {
418 struct sockaddr_in6 sa;
419 socklen_t sa_len = sizeof(sa);
421 if (getsockname(s->fd, (struct sockaddr*) &sa, &sa_len) < 0) {
422 pa_log(__FILE__": getsockname(): %s", pa_cstrerror(errno));
423 return NULL;
426 if (memcmp(&in6addr_any, &sa.sin6_addr, sizeof(in6addr_any)) == 0) {
427 char fqdn[256];
428 if (!pa_get_fqdn(fqdn, sizeof(fqdn)))
429 return NULL;
431 snprintf(c, l, "tcp6:%s:%u", fqdn, (unsigned) ntohs(sa.sin6_port));
433 } else if (memcmp(&in6addr_loopback, &sa.sin6_addr, sizeof(in6addr_loopback)) == 0) {
434 char hn[256];
435 if (!pa_get_host_name(hn, sizeof(hn)))
436 return NULL;
438 snprintf(c, l, "{%s}tcp6:localhost:%u", hn, (unsigned) ntohs(sa.sin6_port));
439 } else {
440 char ip[INET6_ADDRSTRLEN];
442 if (!inet_ntop(AF_INET6, &sa.sin6_addr, ip, sizeof(ip))) {
443 pa_log(__FILE__": inet_ntop(): %s", pa_cstrerror(errno));
444 return NULL;
447 snprintf(c, l, "tcp6:[%s]:%u", ip, (unsigned) ntohs(sa.sin6_port));
450 return c;
453 case SOCKET_SERVER_IPV4: {
454 struct sockaddr_in sa;
455 socklen_t sa_len = sizeof(sa);
457 if (getsockname(s->fd, (struct sockaddr*) &sa, &sa_len) < 0) {
458 pa_log(__FILE__": getsockname(): %s", pa_cstrerror(errno));
459 return NULL;
462 if (sa.sin_addr.s_addr == INADDR_ANY) {
463 char fqdn[256];
464 if (!pa_get_fqdn(fqdn, sizeof(fqdn)))
465 return NULL;
467 snprintf(c, l, "tcp:%s:%u", fqdn, (unsigned) ntohs(sa.sin_port));
468 } else if (sa.sin_addr.s_addr == INADDR_LOOPBACK) {
469 char hn[256];
470 if (!pa_get_host_name(hn, sizeof(hn)))
471 return NULL;
473 snprintf(c, l, "{%s}tcp:localhost:%u", hn, (unsigned) ntohs(sa.sin_port));
474 } else {
475 char ip[INET_ADDRSTRLEN];
477 if (!inet_ntop(AF_INET, &sa.sin_addr, ip, sizeof(ip))) {
478 pa_log(__FILE__": inet_ntop(): %s", pa_cstrerror(errno));
479 return NULL;
482 snprintf(c, l, "tcp:[%s]:%u", ip, (unsigned) ntohs(sa.sin_port));
486 return c;
489 case SOCKET_SERVER_UNIX: {
490 char hn[256];
492 if (!s->filename)
493 return NULL;
495 if (!pa_get_host_name(hn, sizeof(hn)))
496 return NULL;
498 snprintf(c, l, "{%s}unix:%s", hn, s->filename);
499 return c;
502 default:
503 return NULL;