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