]> code.delx.au - pulseaudio/blob - src/pulsecore/socket-server.c
* add new function pa_check_in_group()
[pulseaudio] / src / pulsecore / socket-server.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
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.
10
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.
15
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 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
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>
33 #include <sys/stat.h>
34
35 #ifdef HAVE_SYS_SOCKET_H
36 #include <sys/socket.h>
37 #endif
38 #ifdef HAVE_SYS_UN_H
39 #include <sys/un.h>
40 #ifndef SUN_LEN
41 #define SUN_LEN(ptr) \
42 ((size_t)(((struct sockaddr_un *) 0)->sun_path) + strlen((ptr)->sun_path))
43 #endif
44 #endif
45 #ifdef HAVE_ARPA_INET_H
46 #include <arpa/inet.h>
47 #endif
48 #ifdef HAVE_NETINET_IN_H
49 #include <netinet/in.h>
50 #endif
51
52 #ifdef HAVE_LIBWRAP
53 #include <tcpd.h>
54 #endif
55
56 #ifndef HAVE_INET_NTOP
57 #include "inet_ntop.h"
58 #endif
59
60 #ifndef HAVE_INET_PTON
61 #include "inet_pton.h"
62 #endif
63
64 #include "winsock.h"
65
66 #include <pulse/xmalloc.h>
67 #include <pulse/util.h>
68
69 #include <pulsecore/socket-util.h>
70 #include <pulsecore/core-util.h>
71 #include <pulsecore/log.h>
72 #include <pulsecore/core-error.h>
73
74 #include "socket-server.h"
75
76 struct pa_socket_server {
77 int ref;
78 int fd;
79 char *filename;
80 char *tcpwrap_service;
81
82 void (*on_connection)(pa_socket_server*s, pa_iochannel *io, void *userdata);
83 void *userdata;
84
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;
88 };
89
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;
92 pa_iochannel *io;
93 int nfd;
94 assert(s && s->mainloop == mainloop && s->io_event == e && e && fd >= 0 && fd == s->fd);
95
96 pa_socket_server_ref(s);
97
98 if ((nfd = accept(fd, NULL, NULL)) < 0) {
99 pa_log(__FILE__": accept(): %s", pa_cstrerror(errno));
100 goto finish;
101 }
102
103 pa_fd_set_cloexec(nfd, 1);
104
105 if (!s->on_connection) {
106 close(nfd);
107 goto finish;
108 }
109
110 #ifdef HAVE_LIBWRAP
111
112 if (s->tcpwrap_service) {
113 struct request_info req;
114
115 request_init(&req, RQ_DAEMON, s->tcpwrap_service, RQ_FILE, nfd, NULL);
116 fromhost(&req);
117 if (!hosts_access(&req)) {
118 pa_log_warn(__FILE__": TCP connection refused by tcpwrap.");
119 close(nfd);
120 goto finish;
121 }
122
123 pa_log_info(__FILE__": TCP connection accepted by tcpwrap.");
124 }
125 #endif
126
127 /* There should be a check for socket type here */
128 if (s->type == SOCKET_SERVER_IPV4)
129 pa_socket_tcp_low_delay(fd);
130 else
131 pa_socket_low_delay(fd);
132
133 io = pa_iochannel_new(s->mainloop, nfd, nfd);
134 assert(io);
135 s->on_connection(s, io, s->userdata);
136
137 finish:
138 pa_socket_server_unref(s);
139 }
140
141 pa_socket_server* pa_socket_server_new(pa_mainloop_api *m, int fd) {
142 pa_socket_server *s;
143 assert(m && fd >= 0);
144
145 s = pa_xmalloc(sizeof(pa_socket_server));
146 s->ref = 1;
147 s->fd = fd;
148 s->filename = NULL;
149 s->on_connection = NULL;
150 s->userdata = NULL;
151 s->tcpwrap_service = NULL;
152
153 s->mainloop = m;
154 s->io_event = m->io_new(m, fd, PA_IO_EVENT_INPUT, callback, s);
155 assert(s->io_event);
156
157 s->type = SOCKET_SERVER_GENERIC;
158
159 return s;
160 }
161
162 pa_socket_server* pa_socket_server_ref(pa_socket_server *s) {
163 assert(s && s->ref >= 1);
164 s->ref++;
165 return s;
166 }
167
168 #ifdef HAVE_SYS_UN_H
169
170 pa_socket_server* pa_socket_server_new_unix(pa_mainloop_api *m, const char *filename) {
171 int fd = -1;
172 struct sockaddr_un sa;
173 pa_socket_server *s;
174
175 assert(m && filename);
176
177 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
178 pa_log(__FILE__": socket(): %s", pa_cstrerror(errno));
179 goto fail;
180 }
181
182 pa_fd_set_cloexec(fd, 1);
183
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;
187
188 pa_socket_low_delay(fd);
189
190 if (bind(fd, (struct sockaddr*) &sa, SUN_LEN(&sa)) < 0) {
191 pa_log(__FILE__": bind(): %s", pa_cstrerror(errno));
192 goto fail;
193 }
194
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
198 * inodes. */
199 chmod(filename, 0777);
200
201 if (listen(fd, 5) < 0) {
202 pa_log(__FILE__": listen(): %s", pa_cstrerror(errno));
203 goto fail;
204 }
205
206 s = pa_socket_server_new(m, fd);
207 assert(s);
208
209 s->filename = pa_xstrdup(filename);
210 s->type = SOCKET_SERVER_UNIX;
211
212 return s;
213
214 fail:
215 if (fd >= 0)
216 close(fd);
217
218 return NULL;
219 }
220
221 #else /* HAVE_SYS_UN_H */
222
223 pa_socket_server* pa_socket_server_new_unix(pa_mainloop_api *m, const char *filename) {
224 return NULL;
225 }
226
227 #endif /* HAVE_SYS_UN_H */
228
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;
231 int fd = -1;
232 struct sockaddr_in sa;
233 int on = 1;
234
235 assert(m && port);
236
237 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
238 pa_log(__FILE__": socket(PF_INET): %s", pa_cstrerror(errno));
239 goto fail;
240 }
241
242 pa_fd_set_cloexec(fd, 1);
243
244 #ifdef SO_REUSEADDR
245 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
246 pa_log(__FILE__": setsockopt(): %s", pa_cstrerror(errno));
247 #endif
248
249 pa_socket_tcp_low_delay(fd);
250
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);
255
256 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
257 pa_log(__FILE__": bind(): %s", pa_cstrerror(errno));
258 goto fail;
259 }
260
261 if (listen(fd, 5) < 0) {
262 pa_log(__FILE__": listen(): %s", pa_cstrerror(errno));
263 goto fail;
264 }
265
266 if ((ss = pa_socket_server_new(m, fd))) {
267 ss->type = SOCKET_SERVER_IPV4;
268 ss->tcpwrap_service = pa_xstrdup(tcpwrap_service);
269 }
270
271 return ss;
272
273 fail:
274 if (fd >= 0)
275 close(fd);
276
277 return NULL;
278 }
279
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;
282 int fd = -1;
283 struct sockaddr_in6 sa;
284 int on = 1;
285
286 assert(m && port);
287
288 if ((fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0) {
289 pa_log(__FILE__": socket(PF_INET6): %s", pa_cstrerror(errno));
290 goto fail;
291 }
292
293 pa_fd_set_cloexec(fd, 1);
294
295 #ifdef IPV6_V6ONLY
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));
298 #endif
299
300 #ifdef SO_REUSEADDR
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));
303 #endif
304
305 pa_socket_tcp_low_delay(fd);
306
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);
311
312 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
313 pa_log(__FILE__": bind(): %s", pa_cstrerror(errno));
314 goto fail;
315 }
316
317 if (listen(fd, 5) < 0) {
318 pa_log(__FILE__": listen(): %s", pa_cstrerror(errno));
319 goto fail;
320 }
321
322 if ((ss = pa_socket_server_new(m, fd))) {
323 ss->type = SOCKET_SERVER_IPV6;
324 ss->tcpwrap_service = pa_xstrdup(tcpwrap_service);
325 }
326
327 return ss;
328
329 fail:
330 if (fd >= 0)
331 close(fd);
332
333 return NULL;
334 }
335
336 pa_socket_server* pa_socket_server_new_ipv4_loopback(pa_mainloop_api *m, uint16_t port, const char *tcpwrap_service) {
337 assert(m);
338 assert(port > 0);
339
340 return pa_socket_server_new_ipv4(m, INADDR_LOOPBACK, port, tcpwrap_service);
341 }
342
343 pa_socket_server* pa_socket_server_new_ipv6_loopback(pa_mainloop_api *m, uint16_t port, const char *tcpwrap_service) {
344 assert(m);
345 assert(port > 0);
346
347 return pa_socket_server_new_ipv6(m, in6addr_loopback.s6_addr, port, tcpwrap_service);
348 }
349
350 pa_socket_server* pa_socket_server_new_ipv4_any(pa_mainloop_api *m, uint16_t port, const char *tcpwrap_service) {
351 assert(m);
352 assert(port > 0);
353
354 return pa_socket_server_new_ipv4(m, INADDR_ANY, port, tcpwrap_service);
355 }
356
357 pa_socket_server* pa_socket_server_new_ipv6_any(pa_mainloop_api *m, uint16_t port, const char *tcpwrap_service) {
358 assert(m);
359 assert(port > 0);
360
361 return pa_socket_server_new_ipv6(m, in6addr_any.s6_addr, port, tcpwrap_service);
362 }
363
364 pa_socket_server* pa_socket_server_new_ipv4_string(pa_mainloop_api *m, const char *name, uint16_t port, const char *tcpwrap_service) {
365 struct in_addr ipv4;
366
367 assert(m);
368 assert(name);
369 assert(port > 0);
370
371 if (inet_pton(AF_INET, name, &ipv4) > 0)
372 return pa_socket_server_new_ipv4(m, ntohl(ipv4.s_addr), port, tcpwrap_service);
373
374 return NULL;
375 }
376
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;
379
380 assert(m);
381 assert(name);
382 assert(port > 0);
383
384 if (inet_pton(AF_INET6, name, &ipv6) > 0)
385 return pa_socket_server_new_ipv6(m, ipv6.s6_addr, port, tcpwrap_service);
386
387 return NULL;
388 }
389
390 static void socket_server_free(pa_socket_server*s) {
391 assert(s);
392
393 if (s->filename) {
394 unlink(s->filename);
395 pa_xfree(s->filename);
396 }
397
398 close(s->fd);
399
400 pa_xfree(s->tcpwrap_service);
401
402 s->mainloop->io_free(s->io_event);
403 pa_xfree(s);
404 }
405
406 void pa_socket_server_unref(pa_socket_server *s) {
407 assert(s && s->ref >= 1);
408
409 if (!(--(s->ref)))
410 socket_server_free(s);
411 }
412
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);
415
416 s->on_connection = on_connection;
417 s->userdata = userdata;
418 }
419
420 char *pa_socket_server_get_address(pa_socket_server *s, char *c, size_t l) {
421 assert(s && c && l > 0);
422
423 switch (s->type) {
424 case SOCKET_SERVER_IPV6: {
425 struct sockaddr_in6 sa;
426 socklen_t sa_len = sizeof(sa);
427
428 if (getsockname(s->fd, (struct sockaddr*) &sa, &sa_len) < 0) {
429 pa_log(__FILE__": getsockname(): %s", pa_cstrerror(errno));
430 return NULL;
431 }
432
433 if (memcmp(&in6addr_any, &sa.sin6_addr, sizeof(in6addr_any)) == 0) {
434 char fqdn[256];
435 if (!pa_get_fqdn(fqdn, sizeof(fqdn)))
436 return NULL;
437
438 snprintf(c, l, "tcp6:%s:%u", fqdn, (unsigned) ntohs(sa.sin6_port));
439
440 } else if (memcmp(&in6addr_loopback, &sa.sin6_addr, sizeof(in6addr_loopback)) == 0) {
441 char hn[256];
442 if (!pa_get_host_name(hn, sizeof(hn)))
443 return NULL;
444
445 snprintf(c, l, "{%s}tcp6:localhost:%u", hn, (unsigned) ntohs(sa.sin6_port));
446 } else {
447 char ip[INET6_ADDRSTRLEN];
448
449 if (!inet_ntop(AF_INET6, &sa.sin6_addr, ip, sizeof(ip))) {
450 pa_log(__FILE__": inet_ntop(): %s", pa_cstrerror(errno));
451 return NULL;
452 }
453
454 snprintf(c, l, "tcp6:[%s]:%u", ip, (unsigned) ntohs(sa.sin6_port));
455 }
456
457 return c;
458 }
459
460 case SOCKET_SERVER_IPV4: {
461 struct sockaddr_in sa;
462 socklen_t sa_len = sizeof(sa);
463
464 if (getsockname(s->fd, (struct sockaddr*) &sa, &sa_len) < 0) {
465 pa_log(__FILE__": getsockname(): %s", pa_cstrerror(errno));
466 return NULL;
467 }
468
469 if (sa.sin_addr.s_addr == INADDR_ANY) {
470 char fqdn[256];
471 if (!pa_get_fqdn(fqdn, sizeof(fqdn)))
472 return NULL;
473
474 snprintf(c, l, "tcp:%s:%u", fqdn, (unsigned) ntohs(sa.sin_port));
475 } else if (sa.sin_addr.s_addr == INADDR_LOOPBACK) {
476 char hn[256];
477 if (!pa_get_host_name(hn, sizeof(hn)))
478 return NULL;
479
480 snprintf(c, l, "{%s}tcp:localhost:%u", hn, (unsigned) ntohs(sa.sin_port));
481 } else {
482 char ip[INET_ADDRSTRLEN];
483
484 if (!inet_ntop(AF_INET, &sa.sin_addr, ip, sizeof(ip))) {
485 pa_log(__FILE__": inet_ntop(): %s", pa_cstrerror(errno));
486 return NULL;
487 }
488
489 snprintf(c, l, "tcp:[%s]:%u", ip, (unsigned) ntohs(sa.sin_port));
490
491 }
492
493 return c;
494 }
495
496 case SOCKET_SERVER_UNIX: {
497 char hn[256];
498
499 if (!s->filename)
500 return NULL;
501
502 if (!pa_get_host_name(hn, sizeof(hn)))
503 return NULL;
504
505 snprintf(c, l, "{%s}unix:%s", hn, s->filename);
506 return c;
507 }
508
509 default:
510 return NULL;
511 }
512 }