1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
/* $Id$ */
/***
This file is part of polypaudio.
polypaudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
polypaudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with polypaudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
#ifndef SUN_LEN
#define SUN_LEN(ptr) \
((size_t)(((struct sockaddr_un *) 0)->sun_path) + strlen((ptr)->sun_path))
#endif
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_LIBWRAP
#include <tcpd.h>
#endif
#ifndef HAVE_INET_NTOP
#include "inet_ntop.h"
#endif
#include "winsock.h"
#include "socket-server.h"
#include "socket-util.h"
#include "xmalloc.h"
#include "util.h"
#include "log.h"
struct pa_socket_server {
int ref;
int fd;
char *filename;
char *tcpwrap_service;
void (*on_connection)(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata);
void *userdata;
struct pa_io_event *io_event;
struct pa_mainloop_api *mainloop;
enum { SOCKET_SERVER_GENERIC, SOCKET_SERVER_IPV4, SOCKET_SERVER_UNIX, SOCKET_SERVER_IPV6 } type;
};
static void callback(struct pa_mainloop_api *mainloop, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
struct pa_socket_server *s = userdata;
struct pa_iochannel *io;
int nfd;
assert(s && s->mainloop == mainloop && s->io_event == e && e && fd >= 0 && fd == s->fd);
pa_socket_server_ref(s);
if ((nfd = accept(fd, NULL, NULL)) < 0) {
pa_log(__FILE__": accept(): %s\n", strerror(errno));
goto finish;
}
pa_fd_set_cloexec(nfd, 1);
if (!s->on_connection) {
close(nfd);
goto finish;
}
#ifdef HAVE_LIBWRAP
if (s->type == SOCKET_SERVER_IPV4 && s->tcpwrap_service) {
struct request_info req;
request_init(&req, RQ_DAEMON, s->tcpwrap_service, RQ_FILE, nfd, NULL);
fromhost(&req);
if (!hosts_access(&req)) {
pa_log(__FILE__": TCP connection refused by tcpwrap.\n");
close(nfd);
goto finish;
}
pa_log(__FILE__": TCP connection accepted by tcpwrap.\n");
}
#endif
/* There should be a check for socket type here */
if (s->type == SOCKET_SERVER_IPV4)
pa_socket_tcp_low_delay(fd);
else
pa_socket_low_delay(fd);
io = pa_iochannel_new(s->mainloop, nfd, nfd);
assert(io);
s->on_connection(s, io, s->userdata);
finish:
pa_socket_server_unref(s);
}
struct pa_socket_server* pa_socket_server_new(struct pa_mainloop_api *m, int fd) {
struct pa_socket_server *s;
assert(m && fd >= 0);
s = pa_xmalloc(sizeof(struct pa_socket_server));
s->ref = 1;
s->fd = fd;
s->filename = NULL;
s->on_connection = NULL;
s->userdata = NULL;
s->tcpwrap_service = NULL;
s->mainloop = m;
s->io_event = m->io_new(m, fd, PA_IO_EVENT_INPUT, callback, s);
assert(s->io_event);
s->type = SOCKET_SERVER_GENERIC;
return s;
}
struct pa_socket_server* pa_socket_server_ref(struct pa_socket_server *s) {
assert(s && s->ref >= 1);
s->ref++;
return s;
}
#ifdef HAVE_SYS_UN_H
struct pa_socket_server* pa_socket_server_new_unix(struct pa_mainloop_api *m, const char *filename) {
int fd = -1;
struct sockaddr_un sa;
struct pa_socket_server *s;
assert(m && filename);
if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
pa_log(__FILE__": socket(): %s\n", strerror(errno));
goto fail;
}
pa_fd_set_cloexec(fd, 1);
sa.sun_family = AF_UNIX;
strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
pa_socket_low_delay(fd);
if (bind(fd, (struct sockaddr*) &sa, SUN_LEN(&sa)) < 0) {
pa_log(__FILE__": bind(): %s\n", strerror(errno));
goto fail;
}
if (listen(fd, 5) < 0) {
pa_log(__FILE__": listen(): %s\n", strerror(errno));
goto fail;
}
s = pa_socket_server_new(m, fd);
assert(s);
s->filename = pa_xstrdup(filename);
s->type = SOCKET_SERVER_UNIX;
return s;
fail:
if (fd >= 0)
close(fd);
return NULL;
}
#else /* HAVE_SYS_UN_H */
struct pa_socket_server* pa_socket_server_new_unix(struct pa_mainloop_api *m, const char *filename) {
return NULL;
}
#endif /* HAVE_SYS_UN_H */
struct pa_socket_server* pa_socket_server_new_ipv4(struct pa_mainloop_api *m, uint32_t address, uint16_t port, const char *tcpwrap_service) {
struct pa_socket_server *ss;
int fd = -1;
struct sockaddr_in sa;
int on = 1;
assert(m && port);
if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
pa_log(__FILE__": socket(): %s\n", strerror(errno));
goto fail;
}
pa_fd_set_cloexec(fd, 1);
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on)) < 0)
pa_log(__FILE__": setsockopt(): %s\n", strerror(errno));
pa_socket_tcp_low_delay(fd);
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
sa.sin_addr.s_addr = htonl(address);
if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
pa_log(__FILE__": bind(): %s\n", strerror(errno));
goto fail;
}
if (listen(fd, 5) < 0) {
pa_log(__FILE__": listen(): %s\n", strerror(errno));
goto fail;
}
if ((ss = pa_socket_server_new(m, fd))) {
ss->type = SOCKET_SERVER_IPV4;
ss->tcpwrap_service = pa_xstrdup(tcpwrap_service);
}
return ss;
fail:
if (fd >= 0)
close(fd);
return NULL;
}
struct pa_socket_server* pa_socket_server_new_ipv6(struct pa_mainloop_api *m, uint8_t address[16], uint16_t port) {
struct pa_socket_server *ss;
int fd = -1;
struct sockaddr_in6 sa;
int on = 1;
assert(m && port);
if ((fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0) {
pa_log(__FILE__": socket(): %s\n", strerror(errno));
goto fail;
}
pa_fd_set_cloexec(fd, 1);
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on)) < 0)
pa_log(__FILE__": setsockopt(): %s\n", strerror(errno));
pa_socket_tcp_low_delay(fd);
memset(&sa, 0, sizeof(sa));
sa.sin6_family = AF_INET6;
sa.sin6_port = htons(port);
memcpy(sa.sin6_addr.s6_addr, address, 16);
if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
pa_log(__FILE__": bind(): %s\n", strerror(errno));
goto fail;
}
if (listen(fd, 5) < 0) {
pa_log(__FILE__": listen(): %s\n", strerror(errno));
goto fail;
}
if ((ss = pa_socket_server_new(m, fd)))
ss->type = SOCKET_SERVER_IPV6;
return ss;
fail:
if (fd >= 0)
close(fd);
return NULL;
}
static void socket_server_free(struct pa_socket_server*s) {
assert(s);
close(s->fd);
if (s->filename) {
unlink(s->filename);
pa_xfree(s->filename);
}
pa_xfree(s->tcpwrap_service);
s->mainloop->io_free(s->io_event);
pa_xfree(s);
}
void pa_socket_server_unref(struct pa_socket_server *s) {
assert(s && s->ref >= 1);
if (!(--(s->ref)))
socket_server_free(s);
}
void pa_socket_server_set_callback(struct pa_socket_server*s, void (*on_connection)(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata), void *userdata) {
assert(s && s->ref >= 1);
s->on_connection = on_connection;
s->userdata = userdata;
}
char *pa_socket_server_get_address(struct pa_socket_server *s, char *c, size_t l) {
assert(s && c && l > 0);
switch (s->type) {
case SOCKET_SERVER_IPV6: {
struct sockaddr_in6 sa;
socklen_t sa_len = sizeof(sa);
if (getsockname(s->fd, (struct sockaddr*) &sa, &sa_len) < 0) {
pa_log(__FILE__": getsockname() failed: %s\n", strerror(errno));
return NULL;
}
if (memcmp(&in6addr_any, &sa.sin6_addr, sizeof(in6addr_any)) == 0) {
char fqdn[256];
if (!pa_get_fqdn(fqdn, sizeof(fqdn)))
return NULL;
snprintf(c, l, "tcp6:%s:%u", fqdn, (unsigned) ntohs(sa.sin6_port));
} else if (memcmp(&in6addr_loopback, &sa.sin6_addr, sizeof(in6addr_loopback)) == 0) {
char hn[256];
if (!pa_get_host_name(hn, sizeof(hn)))
return NULL;
snprintf(c, l, "{%s}tcp6:localhost:%u", hn, (unsigned) ntohs(sa.sin6_port));
} else {
char ip[INET6_ADDRSTRLEN];
if (!inet_ntop(AF_INET6, &sa.sin6_addr, ip, sizeof(ip))) {
pa_log(__FILE__": inet_ntop() failed: %s\n", strerror(errno));
return NULL;
}
snprintf(c, l, "tcp6:[%s]:%u", ip, (unsigned) ntohs(sa.sin6_port));
}
return c;
}
case SOCKET_SERVER_IPV4: {
struct sockaddr_in sa;
socklen_t sa_len = sizeof(sa);
if (getsockname(s->fd, (struct sockaddr*) &sa, &sa_len) < 0) {
pa_log(__FILE__": getsockname() failed: %s\n", strerror(errno));
return NULL;
}
if (sa.sin_addr.s_addr == INADDR_ANY) {
char fqdn[256];
if (!pa_get_fqdn(fqdn, sizeof(fqdn)))
return NULL;
snprintf(c, l, "tcp:%s:%u", fqdn, (unsigned) ntohs(sa.sin_port));
} else if (sa.sin_addr.s_addr == INADDR_LOOPBACK) {
char hn[256];
if (!pa_get_host_name(hn, sizeof(hn)))
return NULL;
snprintf(c, l, "{%s}tcp:localhost:%u", hn, (unsigned) ntohs(sa.sin_port));
} else {
char ip[INET_ADDRSTRLEN];
if (!inet_ntop(AF_INET, &sa.sin_addr, ip, sizeof(ip))) {
pa_log(__FILE__": inet_ntop() failed: %s\n", strerror(errno));
return NULL;
}
snprintf(c, l, "tcp:[%s]:%u", ip, (unsigned) ntohs(sa.sin_port));
}
return c;
}
case SOCKET_SERVER_UNIX: {
char hn[256];
if (!s->filename)
return NULL;
if (!pa_get_host_name(hn, sizeof(hn)))
return NULL;
snprintf(c, l, "{%s}unix:%s", hn, s->filename);
return c;
}
default:
return NULL;
}
}
|