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