]> code.delx.au - pulseaudio/blob - src/pulsecore/socket-server.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / socket-server.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
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.
13
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.
18
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 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
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>
36
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
53
54 #ifdef HAVE_LIBWRAP
55 #include <tcpd.h>
56 #endif
57
58 #ifndef HAVE_INET_NTOP
59 #include "inet_ntop.h"
60 #endif
61
62 #ifndef HAVE_INET_PTON
63 #include "inet_pton.h"
64 #endif
65
66 #include "winsock.h"
67
68 #include <pulse/xmalloc.h>
69 #include <pulse/util.h>
70
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>
77
78 #include "socket-server.h"
79
80 struct pa_socket_server {
81 PA_REFCNT_DECLARE;
82 int fd;
83 char *filename;
84 char *tcpwrap_service;
85
86 void (*on_connection)(pa_socket_server*s, pa_iochannel *io, void *userdata);
87 void *userdata;
88
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;
92 };
93
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;
98
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);
106
107 pa_socket_server_ref(s);
108
109 if ((nfd = accept(fd, NULL, NULL)) < 0) {
110 pa_log("accept(): %s", pa_cstrerror(errno));
111 goto finish;
112 }
113
114 pa_make_fd_cloexec(nfd);
115
116 if (!s->on_connection) {
117 pa_close(nfd);
118 goto finish;
119 }
120
121 #ifdef HAVE_LIBWRAP
122
123 if (s->tcpwrap_service) {
124 struct request_info req;
125
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;
132 }
133
134 pa_log_info("TCP connection accepted by tcpwrap.");
135 }
136 #endif
137
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);
143
144 pa_assert_se(io = pa_iochannel_new(s->mainloop, nfd, nfd));
145 s->on_connection(s, io, s->userdata);
146
147 finish:
148 pa_socket_server_unref(s);
149 }
150
151 pa_socket_server* pa_socket_server_new(pa_mainloop_api *m, int fd) {
152 pa_socket_server *s;
153
154 pa_assert(m);
155 pa_assert(fd >= 0);
156
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;
164
165 s->mainloop = m;
166 pa_assert_se(s->io_event = m->io_new(m, fd, PA_IO_EVENT_INPUT, callback, s));
167
168 s->type = SOCKET_SERVER_GENERIC;
169
170 return s;
171 }
172
173 pa_socket_server* pa_socket_server_ref(pa_socket_server *s) {
174 pa_assert(s);
175 pa_assert(PA_REFCNT_VALUE(s) >= 1);
176
177 PA_REFCNT_INC(s);
178 return s;
179 }
180
181 #ifdef HAVE_SYS_UN_H
182
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;
187
188 pa_assert(m);
189 pa_assert(filename);
190
191 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
192 pa_log("socket(): %s", pa_cstrerror(errno));
193 goto fail;
194 }
195
196 pa_make_fd_cloexec(fd);
197
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;
201
202 pa_make_socket_low_delay(fd);
203
204 if (bind(fd, (struct sockaddr*) &sa, SUN_LEN(&sa)) < 0) {
205 pa_log("bind(): %s", pa_cstrerror(errno));
206 goto fail;
207 }
208
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);
214
215 if (listen(fd, 5) < 0) {
216 pa_log("listen(): %s", pa_cstrerror(errno));
217 goto fail;
218 }
219
220 pa_assert_se(s = pa_socket_server_new(m, fd));
221
222 s->filename = pa_xstrdup(filename);
223 s->type = SOCKET_SERVER_UNIX;
224
225 return s;
226
227 fail:
228 if (fd >= 0)
229 pa_close(fd);
230
231 return NULL;
232 }
233
234 #else /* HAVE_SYS_UN_H */
235
236 pa_socket_server* pa_socket_server_new_unix(pa_mainloop_api *m, const char *filename) {
237 return NULL;
238 }
239
240 #endif /* HAVE_SYS_UN_H */
241
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;
247
248 pa_assert(m);
249 pa_assert(port);
250
251 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
252 pa_log("socket(PF_INET): %s", pa_cstrerror(errno));
253 goto fail;
254 }
255
256 pa_make_fd_cloexec(fd);
257
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
262
263 pa_make_tcp_socket_low_delay(fd);
264
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);
269
270 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
271 pa_log("bind(): %s", pa_cstrerror(errno));
272 goto fail;
273 }
274
275 if (listen(fd, 5) < 0) {
276 pa_log("listen(): %s", pa_cstrerror(errno));
277 goto fail;
278 }
279
280 if ((ss = pa_socket_server_new(m, fd))) {
281 ss->type = SOCKET_SERVER_IPV4;
282 ss->tcpwrap_service = pa_xstrdup(tcpwrap_service);
283 }
284
285 return ss;
286
287 fail:
288 if (fd >= 0)
289 pa_close(fd);
290
291 return NULL;
292 }
293
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;
299
300 pa_assert(m);
301 pa_assert(port > 0);
302
303 if ((fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0) {
304 pa_log("socket(PF_INET6): %s", pa_cstrerror(errno));
305 goto fail;
306 }
307
308 pa_make_fd_cloexec(fd);
309
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
314
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
319
320 pa_make_tcp_socket_low_delay(fd);
321
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);
326
327 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
328 pa_log("bind(): %s", pa_cstrerror(errno));
329 goto fail;
330 }
331
332 if (listen(fd, 5) < 0) {
333 pa_log("listen(): %s", pa_cstrerror(errno));
334 goto fail;
335 }
336
337 if ((ss = pa_socket_server_new(m, fd))) {
338 ss->type = SOCKET_SERVER_IPV6;
339 ss->tcpwrap_service = pa_xstrdup(tcpwrap_service);
340 }
341
342 return ss;
343
344 fail:
345 if (fd >= 0)
346 pa_close(fd);
347
348 return NULL;
349 }
350
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);
354
355 return pa_socket_server_new_ipv4(m, INADDR_LOOPBACK, port, tcpwrap_service);
356 }
357
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);
361
362 return pa_socket_server_new_ipv6(m, in6addr_loopback.s6_addr, port, tcpwrap_service);
363 }
364
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);
368
369 return pa_socket_server_new_ipv4(m, INADDR_ANY, port, tcpwrap_service);
370 }
371
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);
375
376 return pa_socket_server_new_ipv6(m, in6addr_any.s6_addr, port, tcpwrap_service);
377 }
378
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;
381
382 pa_assert(m);
383 pa_assert(name);
384 pa_assert(port > 0);
385
386 if (inet_pton(AF_INET, name, &ipv4) > 0)
387 return pa_socket_server_new_ipv4(m, ntohl(ipv4.s_addr), port, tcpwrap_service);
388
389 return NULL;
390 }
391
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;
394
395 pa_assert(m);
396 pa_assert(name);
397 pa_assert(port > 0);
398
399 if (inet_pton(AF_INET6, name, &ipv6) > 0)
400 return pa_socket_server_new_ipv6(m, ipv6.s6_addr, port, tcpwrap_service);
401
402 return NULL;
403 }
404
405 static void socket_server_free(pa_socket_server*s) {
406 pa_assert(s);
407
408 if (s->filename) {
409 unlink(s->filename);
410 pa_xfree(s->filename);
411 }
412
413 pa_close(s->fd);
414
415 pa_xfree(s->tcpwrap_service);
416
417 s->mainloop->io_free(s->io_event);
418 pa_xfree(s);
419 }
420
421 void pa_socket_server_unref(pa_socket_server *s) {
422 pa_assert(s);
423 pa_assert(PA_REFCNT_VALUE(s) >= 1);
424
425 if (PA_REFCNT_DEC(s) <= 0)
426 socket_server_free(s);
427 }
428
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);
432
433 s->on_connection = on_connection;
434 s->userdata = userdata;
435 }
436
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);
442
443 switch (s->type) {
444 case SOCKET_SERVER_IPV6: {
445 struct sockaddr_in6 sa;
446 socklen_t sa_len = sizeof(sa);
447
448 if (getsockname(s->fd, (struct sockaddr*) &sa, &sa_len) < 0) {
449 pa_log("getsockname(): %s", pa_cstrerror(errno));
450 return NULL;
451 }
452
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;
457
458 pa_snprintf(c, l, "tcp6:%s:%u", fqdn, (unsigned) ntohs(sa.sin6_port));
459
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;
464
465 pa_snprintf(c, l, "{%s}tcp6:localhost:%u", hn, (unsigned) ntohs(sa.sin6_port));
466 } else {
467 char ip[INET6_ADDRSTRLEN];
468
469 if (!inet_ntop(AF_INET6, &sa.sin6_addr, ip, sizeof(ip))) {
470 pa_log("inet_ntop(): %s", pa_cstrerror(errno));
471 return NULL;
472 }
473
474 pa_snprintf(c, l, "tcp6:[%s]:%u", ip, (unsigned) ntohs(sa.sin6_port));
475 }
476
477 return c;
478 }
479
480 case SOCKET_SERVER_IPV4: {
481 struct sockaddr_in sa;
482 socklen_t sa_len = sizeof(sa);
483
484 if (getsockname(s->fd, (struct sockaddr*) &sa, &sa_len) < 0) {
485 pa_log("getsockname(): %s", pa_cstrerror(errno));
486 return NULL;
487 }
488
489 if (sa.sin_addr.s_addr == INADDR_ANY) {
490 char fqdn[256];
491 if (!pa_get_fqdn(fqdn, sizeof(fqdn)))
492 return NULL;
493
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;
499
500 pa_snprintf(c, l, "{%s}tcp:localhost:%u", hn, (unsigned) ntohs(sa.sin_port));
501 } else {
502 char ip[INET_ADDRSTRLEN];
503
504 if (!inet_ntop(AF_INET, &sa.sin_addr, ip, sizeof(ip))) {
505 pa_log("inet_ntop(): %s", pa_cstrerror(errno));
506 return NULL;
507 }
508
509 pa_snprintf(c, l, "tcp:[%s]:%u", ip, (unsigned) ntohs(sa.sin_port));
510
511 }
512
513 return c;
514 }
515
516 case SOCKET_SERVER_UNIX: {
517 char hn[256];
518
519 if (!s->filename)
520 return NULL;
521
522 if (!pa_get_host_name(hn, sizeof(hn)))
523 return NULL;
524
525 pa_snprintf(c, l, "{%s}unix:%s", hn, s->filename);
526 return c;
527 }
528
529 default:
530 return NULL;
531 }
532 }