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