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