]> code.delx.au - pulseaudio/blob - src/polypcore/socket-client.c
aa885759e3c1534fc7bc73f0bc41c02949b66fd1
[pulseaudio] / src / polypcore / socket-client.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
8 published by the Free Software Foundation; either version 2.1 of the
9 License, 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 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License 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 /* #undef HAVE_LIBASYNCNS */
27
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <stdlib.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 #endif
41 #ifdef HAVE_ARPA_INET_H
42 #include <arpa/inet.h>
43 #endif
44 #ifdef HAVE_NETINET_IN_H
45 #include <netinet/in.h>
46 #endif
47 #ifdef HAVE_NETDB_H
48 #include <netdb.h>
49 #endif
50
51 #ifdef HAVE_LIBASYNCNS
52 #include <asyncns.h>
53 #endif
54
55 #include "winsock.h"
56
57 #include <polyp/error.h>
58 #include <polyp/timeval.h>
59 #include <polyp/xmalloc.h>
60
61 #include <polypcore/socket-util.h>
62 #include <polypcore/core-util.h>
63 #include <polypcore/log.h>
64 #include <polypcore/parseaddr.h>
65
66 #include "socket-client.h"
67
68 #define CONNECT_TIMEOUT 5
69
70 struct pa_socket_client {
71 int ref;
72 pa_mainloop_api *mainloop;
73 int fd;
74 pa_io_event *io_event;
75 pa_time_event *timeout_event;
76 pa_defer_event *defer_event;
77 void (*callback)(pa_socket_client*c, pa_iochannel *io, void *userdata);
78 void *userdata;
79 int local;
80 #ifdef HAVE_LIBASYNCNS
81 asyncns_t *asyncns;
82 asyncns_query_t * asyncns_query;
83 pa_io_event *asyncns_io_event;
84 #endif
85 };
86
87 static pa_socket_client*pa_socket_client_new(pa_mainloop_api *m) {
88 pa_socket_client *c;
89 assert(m);
90
91 c = pa_xmalloc(sizeof(pa_socket_client));
92 c->ref = 1;
93 c->mainloop = m;
94 c->fd = -1;
95 c->io_event = NULL;
96 c->defer_event = NULL;
97 c->timeout_event = NULL;
98 c->callback = NULL;
99 c->userdata = NULL;
100 c->local = 0;
101
102 #ifdef HAVE_LIBASYNCNS
103 c->asyncns = NULL;
104 c->asyncns_io_event = NULL;
105 c->asyncns_query = NULL;
106 #endif
107
108 return c;
109 }
110
111 static void free_events(pa_socket_client *c) {
112 assert(c);
113
114 if (c->io_event) {
115 c->mainloop->io_free(c->io_event);
116 c->io_event = NULL;
117 }
118
119 if (c->defer_event) {
120 c->mainloop->defer_free(c->defer_event);
121 c->defer_event = NULL;
122 }
123
124 if (c->timeout_event) {
125 c->mainloop->time_free(c->timeout_event);
126 c->timeout_event = NULL;
127 }
128 }
129
130 static void do_call(pa_socket_client *c) {
131 pa_iochannel *io = NULL;
132 int error;
133 socklen_t lerror;
134 assert(c && c->callback);
135
136 pa_socket_client_ref(c);
137
138 if (c->fd < 0)
139 goto finish;
140
141 lerror = sizeof(error);
142 if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, (void*)&error, &lerror) < 0) {
143 pa_log(__FILE__": getsockopt(): %s", pa_cstrerror(errno));
144 goto finish;
145 }
146
147 if (lerror != sizeof(error)) {
148 pa_log(__FILE__": getsockopt() returned invalid size.");
149 goto finish;
150 }
151
152 if (error != 0) {
153 pa_log_debug(__FILE__": connect(): %s", pa_cstrerror(errno));
154 errno = error;
155 goto finish;
156 }
157
158 io = pa_iochannel_new(c->mainloop, c->fd, c->fd);
159 assert(io);
160
161 finish:
162 if (!io && c->fd >= 0)
163 close(c->fd);
164 c->fd = -1;
165
166 free_events(c);
167
168 assert(c->callback);
169 c->callback(c, io, c->userdata);
170
171 pa_socket_client_unref(c);
172 }
173
174 static void connect_fixed_cb(pa_mainloop_api *m, pa_defer_event *e, void *userdata) {
175 pa_socket_client *c = userdata;
176 assert(m && c && c->defer_event == e);
177 do_call(c);
178 }
179
180 static void connect_io_cb(pa_mainloop_api*m, pa_io_event *e, int fd, PA_GCC_UNUSED pa_io_event_flags_t f, void *userdata) {
181 pa_socket_client *c = userdata;
182 assert(m && c && c->io_event == e && fd >= 0);
183 do_call(c);
184 }
185
186 static int do_connect(pa_socket_client *c, const struct sockaddr *sa, socklen_t len) {
187 int r;
188 assert(c && sa && len);
189
190 pa_make_nonblock_fd(c->fd);
191
192 if ((r = connect(c->fd, sa, len)) < 0) {
193 #ifdef OS_IS_WIN32
194 if (WSAGetLastError() != EWOULDBLOCK) {
195 pa_log_debug(__FILE__": connect(): %d", WSAGetLastError());
196 #else
197 if (errno != EINPROGRESS) {
198 pa_log_debug(__FILE__": connect(): %s (%d)", pa_cstrerror(errno), errno);
199 #endif
200 return -1;
201 }
202
203 c->io_event = c->mainloop->io_new(c->mainloop, c->fd, PA_IO_EVENT_OUTPUT, connect_io_cb, c);
204 assert(c->io_event);
205 } else {
206 c->defer_event = c->mainloop->defer_new(c->mainloop, connect_fixed_cb, c);
207 assert(c->defer_event);
208 }
209
210 return 0;
211 }
212
213 pa_socket_client* pa_socket_client_new_ipv4(pa_mainloop_api *m, uint32_t address, uint16_t port) {
214 struct sockaddr_in sa;
215 assert(m && port > 0);
216
217 memset(&sa, 0, sizeof(sa));
218 sa.sin_family = AF_INET;
219 sa.sin_port = htons(port);
220 sa.sin_addr.s_addr = htonl(address);
221
222 return pa_socket_client_new_sockaddr(m, (struct sockaddr*) &sa, sizeof(sa));
223 }
224
225 #ifdef HAVE_SYS_UN_H
226
227 pa_socket_client* pa_socket_client_new_unix(pa_mainloop_api *m, const char *filename) {
228 struct sockaddr_un sa;
229 assert(m && filename);
230
231 memset(&sa, 0, sizeof(sa));
232 sa.sun_family = AF_UNIX;
233 strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
234 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
235
236 return pa_socket_client_new_sockaddr(m, (struct sockaddr*) &sa, sizeof(sa));
237 }
238
239 #else /* HAVE_SYS_UN_H */
240
241 pa_socket_client* pa_socket_client_new_unix(pa_mainloop_api *m, const char *filename) {
242 return NULL;
243 }
244
245 #endif /* HAVE_SYS_UN_H */
246
247 static int sockaddr_prepare(pa_socket_client *c, const struct sockaddr *sa, size_t salen) {
248 assert(c);
249 assert(sa);
250 assert(salen);
251
252 switch (sa->sa_family) {
253 case AF_UNIX:
254 c->local = 1;
255 break;
256
257 case AF_INET:
258 c->local = ((const struct sockaddr_in*) sa)->sin_addr.s_addr == INADDR_LOOPBACK;
259 break;
260
261 case AF_INET6:
262 c->local = memcmp(&((const struct sockaddr_in6*) sa)->sin6_addr, &in6addr_loopback, sizeof(struct in6_addr)) == 0;
263 break;
264
265 default:
266 c->local = 0;
267 }
268
269 if ((c->fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
270 pa_log(__FILE__": socket(): %s", pa_cstrerror(errno));
271 return -1;
272 }
273
274 pa_fd_set_cloexec(c->fd, 1);
275 if (sa->sa_family == AF_INET || sa->sa_family == AF_INET6)
276 pa_socket_tcp_low_delay(c->fd);
277 else
278 pa_socket_low_delay(c->fd);
279
280 if (do_connect(c, sa, salen) < 0)
281 return -1;
282
283 return 0;
284 }
285
286 pa_socket_client* pa_socket_client_new_sockaddr(pa_mainloop_api *m, const struct sockaddr *sa, size_t salen) {
287 pa_socket_client *c;
288 assert(m && sa);
289 c = pa_socket_client_new(m);
290 assert(c);
291
292 if (sockaddr_prepare(c, sa, salen) < 0)
293 goto fail;
294
295 return c;
296
297 fail:
298 pa_socket_client_unref(c);
299 return NULL;
300
301 }
302
303 static void socket_client_free(pa_socket_client *c) {
304 assert(c && c->mainloop);
305
306
307 free_events(c);
308
309 if (c->fd >= 0)
310 close(c->fd);
311
312 #ifdef HAVE_LIBASYNCNS
313 if (c->asyncns_query)
314 asyncns_cancel(c->asyncns, c->asyncns_query);
315 if (c->asyncns)
316 asyncns_free(c->asyncns);
317 if (c->asyncns_io_event)
318 c->mainloop->io_free(c->asyncns_io_event);
319 #endif
320
321 pa_xfree(c);
322 }
323
324 void pa_socket_client_unref(pa_socket_client *c) {
325 assert(c && c->ref >= 1);
326
327 if (!(--(c->ref)))
328 socket_client_free(c);
329 }
330
331 pa_socket_client* pa_socket_client_ref(pa_socket_client *c) {
332 assert(c && c->ref >= 1);
333 c->ref++;
334 return c;
335 }
336
337 void pa_socket_client_set_callback(pa_socket_client *c, void (*on_connection)(pa_socket_client *c, pa_iochannel*io, void *userdata), void *userdata) {
338 assert(c);
339 c->callback = on_connection;
340 c->userdata = userdata;
341 }
342
343 pa_socket_client* pa_socket_client_new_ipv6(pa_mainloop_api *m, uint8_t address[16], uint16_t port) {
344 struct sockaddr_in6 sa;
345
346 memset(&sa, 0, sizeof(sa));
347 sa.sin6_family = AF_INET6;
348 sa.sin6_port = htons(port);
349 memcpy(&sa.sin6_addr, address, sizeof(sa.sin6_addr));
350
351 return pa_socket_client_new_sockaddr(m, (struct sockaddr*) &sa, sizeof(sa));
352 }
353
354 #ifdef HAVE_LIBASYNCNS
355
356 static void asyncns_cb(pa_mainloop_api*m, pa_io_event *e, int fd, PA_GCC_UNUSED pa_io_event_flags_t f, void *userdata) {
357 pa_socket_client *c = userdata;
358 struct addrinfo *res = NULL;
359 int ret;
360 assert(m && c && c->asyncns_io_event == e && fd >= 0);
361
362 if (asyncns_wait(c->asyncns, 0) < 0)
363 goto fail;
364
365 if (!asyncns_isdone(c->asyncns, c->asyncns_query))
366 return;
367
368 ret = asyncns_getaddrinfo_done(c->asyncns, c->asyncns_query, &res);
369 c->asyncns_query = NULL;
370
371 if (ret != 0 || !res)
372 goto fail;
373
374 if (res->ai_addr)
375 sockaddr_prepare(c, res->ai_addr, res->ai_addrlen);
376
377 asyncns_freeaddrinfo(res);
378
379 goto finish;
380
381 fail:
382 errno = EHOSTUNREACH;
383 do_call(c);
384
385 finish:
386
387 m->io_free(c->asyncns_io_event);
388 c->asyncns_io_event = NULL;
389 }
390
391 #endif
392
393 static void timeout_cb(pa_mainloop_api *m, pa_time_event *e, const struct timeval *tv, void *userdata) {
394 pa_socket_client *c = userdata;
395 assert(m);
396 assert(e);
397 assert(tv);
398 assert(c);
399
400 if (c->fd >= 0) {
401 close(c->fd);
402 c->fd = -1;
403 }
404
405 errno = ETIMEDOUT;
406 do_call(c);
407 }
408
409 static void start_timeout(pa_socket_client *c) {
410 struct timeval tv;
411 assert(c);
412 assert(!c->timeout_event);
413
414 pa_gettimeofday(&tv);
415 pa_timeval_add(&tv, CONNECT_TIMEOUT * 1000000);
416 c->timeout_event = c->mainloop->time_new(c->mainloop, &tv, timeout_cb, c);
417 }
418
419 pa_socket_client* pa_socket_client_new_string(pa_mainloop_api *m, const char*name, uint16_t default_port) {
420 pa_socket_client *c = NULL;
421 pa_parsed_address a;
422 assert(m && name);
423
424 if (pa_parse_address(name, &a) < 0)
425 return NULL;
426
427 if (!a.port)
428 a.port = default_port;
429
430 switch (a.type) {
431 case PA_PARSED_ADDRESS_UNIX:
432 if ((c = pa_socket_client_new_unix(m, a.path_or_host)))
433 start_timeout(c);
434 break;
435
436 case PA_PARSED_ADDRESS_TCP4: /* Fallthrough */
437 case PA_PARSED_ADDRESS_TCP6: /* Fallthrough */
438 case PA_PARSED_ADDRESS_TCP_AUTO:{
439
440 struct addrinfo hints;
441 char port[12];
442
443 snprintf(port, sizeof(port), "%u", (unsigned) a.port);
444
445 memset(&hints, 0, sizeof(hints));
446 hints.ai_family = a.type == PA_PARSED_ADDRESS_TCP4 ? PF_INET : (a.type == PA_PARSED_ADDRESS_TCP6 ? PF_INET6 : PF_UNSPEC);
447 hints.ai_socktype = SOCK_STREAM;
448
449 #ifdef HAVE_LIBASYNCNS
450 {
451 asyncns_t *asyncns;
452
453 if (!(asyncns = asyncns_new(1)))
454 goto finish;
455
456 c = pa_socket_client_new(m);
457 c->asyncns = asyncns;
458 c->asyncns_io_event = m->io_new(m, asyncns_fd(c->asyncns), PA_IO_EVENT_INPUT, asyncns_cb, c);
459 c->asyncns_query = asyncns_getaddrinfo(c->asyncns, a.path_or_host, port, &hints);
460 assert(c->asyncns_query);
461 start_timeout(c);
462 }
463 #else /* HAVE_LIBASYNCNS */
464 {
465 #ifdef HAVE_GETADDRINFO
466 int ret;
467 struct addrinfo *res = NULL;
468
469 ret = getaddrinfo(a.path_or_host, port, &hints, &res);
470
471 if (ret < 0 || !res)
472 goto finish;
473
474 if (res->ai_addr) {
475 if ((c = pa_socket_client_new_sockaddr(m, res->ai_addr, res->ai_addrlen)))
476 start_timeout(c);
477 }
478
479 freeaddrinfo(res);
480 #else /* HAVE_GETADDRINFO */
481 struct hostent *host = NULL;
482 struct sockaddr_in s;
483
484 /* FIXME: PF_INET6 support */
485 if (hints.ai_family == PF_INET6) {
486 pa_log_error(__FILE__": IPv6 is not supported on Windows");
487 goto finish;
488 }
489
490 host = gethostbyname(a.path_or_host);
491 if (!host) {
492 unsigned int addr = inet_addr(a.path_or_host);
493 if (addr != INADDR_NONE)
494 host = gethostbyaddr((char*)&addr, 4, AF_INET);
495 }
496
497 if (!host)
498 goto finish;
499
500 s.sin_family = AF_INET;
501 memcpy(&s.sin_addr, host->h_addr, sizeof(struct in_addr));
502 s.sin_port = htons(a.port);
503
504 if ((c = pa_socket_client_new_sockaddr(m, (struct sockaddr*)&s, sizeof(s))))
505 start_timeout(c);
506 #endif /* HAVE_GETADDRINFO */
507 }
508 #endif /* HAVE_LIBASYNCNS */
509 }
510 }
511
512 finish:
513 pa_xfree(a.path_or_host);
514 return c;
515
516 }
517
518 /* Return non-zero when the target sockaddr is considered
519 local. "local" means UNIX socket or TCP socket on localhost. Other
520 local IP addresses are not considered local. */
521 int pa_socket_client_is_local(pa_socket_client *c) {
522 assert(c);
523 return c->local;
524 }