]> code.delx.au - pulseaudio/blob - src/pulsecore/socket-server.c
77ea13e70ef8383e0f22985c06b13cfe5588861a
[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
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
50
51 #ifdef HAVE_LIBWRAP
52 #include <tcpd.h>
53 #endif
54
55 #ifndef HAVE_INET_NTOP
56 #include "inet_ntop.h"
57 #endif
58
59 #ifndef HAVE_INET_PTON
60 #include "inet_pton.h"
61 #endif
62
63 #include "winsock.h"
64
65 #include <pulse/xmalloc.h>
66 #include <pulse/util.h>
67
68 #include <pulsecore/socket-util.h>
69 #include <pulsecore/core-util.h>
70 #include <pulsecore/log.h>
71 #include <pulsecore/core-error.h>
72
73 #include "socket-server.h"
74
75 struct pa_socket_server {
76 int ref;
77 int fd;
78 char *filename;
79 char *tcpwrap_service;
80
81 void (*on_connection)(pa_socket_server*s, pa_iochannel *io, void *userdata);
82 void *userdata;
83
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;
87 };
88
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);
94
95 pa_socket_server_ref(s);
96
97 if ((nfd = accept(fd, NULL, NULL)) < 0) {
98 pa_log(__FILE__": accept(): %s", pa_cstrerror(errno));
99 goto finish;
100 }
101
102 pa_fd_set_cloexec(nfd, 1);
103
104 if (!s->on_connection) {
105 close(nfd);
106 goto finish;
107 }
108
109 #ifdef HAVE_LIBWRAP
110
111 if (s->tcpwrap_service) {
112 struct request_info req;
113
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;
120 }
121
122 pa_log_info(__FILE__": TCP connection accepted by tcpwrap.");
123 }
124 #endif
125
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);
131
132 io = pa_iochannel_new(s->mainloop, nfd, nfd);
133 assert(io);
134 s->on_connection(s, io, s->userdata);
135
136 finish:
137 pa_socket_server_unref(s);
138 }
139
140 pa_socket_server* pa_socket_server_new(pa_mainloop_api *m, int fd) {
141 pa_socket_server *s;
142 assert(m && fd >= 0);
143
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;
151
152 s->mainloop = m;
153 s->io_event = m->io_new(m, fd, PA_IO_EVENT_INPUT, callback, s);
154 assert(s->io_event);
155
156 s->type = SOCKET_SERVER_GENERIC;
157
158 return s;
159 }
160
161 pa_socket_server* pa_socket_server_ref(pa_socket_server *s) {
162 assert(s && s->ref >= 1);
163 s->ref++;
164 return s;
165 }
166
167 #ifdef HAVE_SYS_UN_H
168
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;
173
174 assert(m && filename);
175
176 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
177 pa_log(__FILE__": socket(): %s", pa_cstrerror(errno));
178 goto fail;
179 }
180
181 pa_fd_set_cloexec(fd, 1);
182
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;
186
187 pa_socket_low_delay(fd);
188
189 if (bind(fd, (struct sockaddr*) &sa, SUN_LEN(&sa)) < 0) {
190 pa_log(__FILE__": bind(): %s", pa_cstrerror(errno));
191 goto fail;
192 }
193
194 if (listen(fd, 5) < 0) {
195 pa_log(__FILE__": listen(): %s", pa_cstrerror(errno));
196 goto fail;
197 }
198
199 s = pa_socket_server_new(m, fd);
200 assert(s);
201
202 s->filename = pa_xstrdup(filename);
203 s->type = SOCKET_SERVER_UNIX;
204
205 return s;
206
207 fail:
208 if (fd >= 0)
209 close(fd);
210
211 return NULL;
212 }
213
214 #else /* HAVE_SYS_UN_H */
215
216 pa_socket_server* pa_socket_server_new_unix(pa_mainloop_api *m, const char *filename) {
217 return NULL;
218 }
219
220 #endif /* HAVE_SYS_UN_H */
221
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;
227
228 assert(m && port);
229
230 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
231 pa_log(__FILE__": socket(PF_INET): %s", pa_cstrerror(errno));
232 goto fail;
233 }
234
235 pa_fd_set_cloexec(fd, 1);
236
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
241
242 pa_socket_tcp_low_delay(fd);
243
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);
248
249 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
250 pa_log(__FILE__": bind(): %s", pa_cstrerror(errno));
251 goto fail;
252 }
253
254 if (listen(fd, 5) < 0) {
255 pa_log(__FILE__": listen(): %s", pa_cstrerror(errno));
256 goto fail;
257 }
258
259 if ((ss = pa_socket_server_new(m, fd))) {
260 ss->type = SOCKET_SERVER_IPV4;
261 ss->tcpwrap_service = pa_xstrdup(tcpwrap_service);
262 }
263
264 return ss;
265
266 fail:
267 if (fd >= 0)
268 close(fd);
269
270 return NULL;
271 }
272
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;
278
279 assert(m && port);
280
281 if ((fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0) {
282 pa_log(__FILE__": socket(PF_INET6): %s", pa_cstrerror(errno));
283 goto fail;
284 }
285
286 pa_fd_set_cloexec(fd, 1);
287
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
292
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
297
298 pa_socket_tcp_low_delay(fd);
299
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);
304
305 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
306 pa_log(__FILE__": bind(): %s", pa_cstrerror(errno));
307 goto fail;
308 }
309
310 if (listen(fd, 5) < 0) {
311 pa_log(__FILE__": listen(): %s", pa_cstrerror(errno));
312 goto fail;
313 }
314
315 if ((ss = pa_socket_server_new(m, fd))) {
316 ss->type = SOCKET_SERVER_IPV6;
317 ss->tcpwrap_service = pa_xstrdup(tcpwrap_service);
318 }
319
320 return ss;
321
322 fail:
323 if (fd >= 0)
324 close(fd);
325
326 return NULL;
327 }
328
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);
332
333 return pa_socket_server_new_ipv4(m, INADDR_LOOPBACK, port, tcpwrap_service);
334 }
335
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);
339
340 return pa_socket_server_new_ipv6(m, in6addr_loopback.s6_addr, port, tcpwrap_service);
341 }
342
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);
346
347 return pa_socket_server_new_ipv4(m, INADDR_ANY, port, tcpwrap_service);
348 }
349
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);
353
354 return pa_socket_server_new_ipv6(m, in6addr_any.s6_addr, port, tcpwrap_service);
355 }
356
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;
359
360 assert(m);
361 assert(name);
362 assert(port > 0);
363
364 if (inet_pton(AF_INET, name, &ipv4) > 0)
365 return pa_socket_server_new_ipv4(m, ntohl(ipv4.s_addr), port, tcpwrap_service);
366
367 return NULL;
368 }
369
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;
372
373 assert(m);
374 assert(name);
375 assert(port > 0);
376
377 if (inet_pton(AF_INET6, name, &ipv6) > 0)
378 return pa_socket_server_new_ipv6(m, ipv6.s6_addr, port, tcpwrap_service);
379
380 return NULL;
381 }
382
383 static void socket_server_free(pa_socket_server*s) {
384 assert(s);
385
386 if (s->filename) {
387 unlink(s->filename);
388 pa_xfree(s->filename);
389 }
390
391 close(s->fd);
392
393 pa_xfree(s->tcpwrap_service);
394
395 s->mainloop->io_free(s->io_event);
396 pa_xfree(s);
397 }
398
399 void pa_socket_server_unref(pa_socket_server *s) {
400 assert(s && s->ref >= 1);
401
402 if (!(--(s->ref)))
403 socket_server_free(s);
404 }
405
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);
408
409 s->on_connection = on_connection;
410 s->userdata = userdata;
411 }
412
413 char *pa_socket_server_get_address(pa_socket_server *s, char *c, size_t l) {
414 assert(s && c && l > 0);
415
416 switch (s->type) {
417 case SOCKET_SERVER_IPV6: {
418 struct sockaddr_in6 sa;
419 socklen_t sa_len = sizeof(sa);
420
421 if (getsockname(s->fd, (struct sockaddr*) &sa, &sa_len) < 0) {
422 pa_log(__FILE__": getsockname(): %s", pa_cstrerror(errno));
423 return NULL;
424 }
425
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;
430
431 snprintf(c, l, "tcp6:%s:%u", fqdn, (unsigned) ntohs(sa.sin6_port));
432
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;
437
438 snprintf(c, l, "{%s}tcp6:localhost:%u", hn, (unsigned) ntohs(sa.sin6_port));
439 } else {
440 char ip[INET6_ADDRSTRLEN];
441
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;
445 }
446
447 snprintf(c, l, "tcp6:[%s]:%u", ip, (unsigned) ntohs(sa.sin6_port));
448 }
449
450 return c;
451 }
452
453 case SOCKET_SERVER_IPV4: {
454 struct sockaddr_in sa;
455 socklen_t sa_len = sizeof(sa);
456
457 if (getsockname(s->fd, (struct sockaddr*) &sa, &sa_len) < 0) {
458 pa_log(__FILE__": getsockname(): %s", pa_cstrerror(errno));
459 return NULL;
460 }
461
462 if (sa.sin_addr.s_addr == INADDR_ANY) {
463 char fqdn[256];
464 if (!pa_get_fqdn(fqdn, sizeof(fqdn)))
465 return NULL;
466
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;
472
473 snprintf(c, l, "{%s}tcp:localhost:%u", hn, (unsigned) ntohs(sa.sin_port));
474 } else {
475 char ip[INET_ADDRSTRLEN];
476
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;
480 }
481
482 snprintf(c, l, "tcp:[%s]:%u", ip, (unsigned) ntohs(sa.sin_port));
483
484 }
485
486 return c;
487 }
488
489 case SOCKET_SERVER_UNIX: {
490 char hn[256];
491
492 if (!s->filename)
493 return NULL;
494
495 if (!pa_get_host_name(hn, sizeof(hn)))
496 return NULL;
497
498 snprintf(c, l, "{%s}unix:%s", hn, s->filename);
499 return c;
500 }
501
502 default:
503 return NULL;
504 }
505 }