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