4 This file is part of polypaudio.
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.
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.
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
26 /* #undef HAVE_LIBASYNCNS */
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
38 #ifdef HAVE_LIBASYNCNS
42 #include "socket-client.h"
43 #include "socket-util.h"
47 #include "parseaddr.h"
49 #define CONNECT_TIMEOUT 5
51 struct pa_socket_client
{
53 struct pa_mainloop_api
*mainloop
;
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
);
61 #ifdef HAVE_LIBASYNCNS
63 asyncns_query_t
* asyncns_query
;
64 struct pa_io_event
*asyncns_io_event
;
68 static struct pa_socket_client
*pa_socket_client_new(struct pa_mainloop_api
*m
) {
69 struct pa_socket_client
*c
;
72 c
= pa_xmalloc(sizeof(struct pa_socket_client
));
77 c
->defer_event
= NULL
;
78 c
->timeout_event
= NULL
;
83 #ifdef HAVE_LIBASYNCNS
85 c
->asyncns_io_event
= NULL
;
86 c
->asyncns_query
= NULL
;
92 static void free_events(struct pa_socket_client
*c
) {
96 c
->mainloop
->io_free(c
->io_event
);
100 if (c
->defer_event
) {
101 c
->mainloop
->defer_free(c
->defer_event
);
102 c
->defer_event
= NULL
;
105 if (c
->timeout_event
) {
106 c
->mainloop
->time_free(c
->timeout_event
);
107 c
->timeout_event
= NULL
;
111 static void do_call(struct pa_socket_client
*c
) {
112 struct pa_iochannel
*io
= NULL
;
115 assert(c
&& c
->callback
);
117 pa_socket_client_ref(c
);
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
));
128 if (lerror
!= sizeof(error
)) {
129 pa_log(__FILE__
": getsockopt() returned invalid size.\n");
134 pa_log_debug(__FILE__
": connect(): %s\n", strerror(error
));
139 io
= pa_iochannel_new(c
->mainloop
, c
->fd
, c
->fd
);
143 if (!io
&& c
->fd
>= 0)
150 c
->callback(c
, io
, c
->userdata
);
152 pa_socket_client_unref(c
);
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
);
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);
167 static int do_connect(struct pa_socket_client
*c
, const struct sockaddr
*sa
, socklen_t len
) {
169 assert(c
&& sa
&& len
);
171 pa_make_nonblock_fd(c
->fd
);
173 if ((r
= connect(c
->fd
, sa
, len
)) < 0) {
174 if (errno
!= EINPROGRESS
) {
175 /*pa_log(__FILE__": connect(): %s\n", strerror(errno));*/
179 c
->io_event
= c
->mainloop
->io_new(c
->mainloop
, c
->fd
, PA_IO_EVENT_OUTPUT
, connect_io_cb
, c
);
182 c
->defer_event
= c
->mainloop
->defer_new(c
->mainloop
, connect_fixed_cb
, c
);
183 assert(c
->defer_event
);
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);
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
);
198 return pa_socket_client_new_sockaddr(m
, (struct sockaddr
*) &sa
, sizeof(sa
));
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
);
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;
210 return pa_socket_client_new_sockaddr(m
, (struct sockaddr
*) &sa
, sizeof(sa
));
213 static int sockaddr_prepare(struct pa_socket_client
*c
, const struct sockaddr
*sa
, size_t salen
) {
218 switch (sa
->sa_family
) {
224 c
->local
= ((const struct sockaddr_in
*) sa
)->sin_addr
.s_addr
== INADDR_LOOPBACK
;
228 c
->local
= memcmp(&((const struct sockaddr_in6
*) sa
)->sin6_addr
, &in6addr_loopback
, sizeof(struct in6_addr
)) == 0;
235 if ((c
->fd
= socket(sa
->sa_family
, SOCK_STREAM
, 0)) < 0) {
236 pa_log(__FILE__
": socket(): %s\n", strerror(errno
));
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
);
244 pa_socket_low_delay(c
->fd
);
246 if (do_connect(c
, sa
, salen
) < 0)
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
;
255 c
= pa_socket_client_new(m
);
258 if (sockaddr_prepare(c
, sa
, salen
) < 0)
264 pa_socket_client_unref(c
);
269 void socket_client_free(struct pa_socket_client
*c
) {
270 assert(c
&& c
->mainloop
);
278 #ifdef HAVE_LIBASYNCNS
279 if (c
->asyncns_query
)
280 asyncns_cancel(c
->asyncns
, c
->asyncns_query
);
282 asyncns_free(c
->asyncns
);
283 if (c
->asyncns_io_event
)
284 c
->mainloop
->io_free(c
->asyncns_io_event
);
290 void pa_socket_client_unref(struct pa_socket_client
*c
) {
291 assert(c
&& c
->ref
>= 1);
294 socket_client_free(c
);
297 struct pa_socket_client
* pa_socket_client_ref(struct pa_socket_client
*c
) {
298 assert(c
&& c
->ref
>= 1);
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
) {
305 c
->callback
= on_connection
;
306 c
->userdata
= userdata
;
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
;
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
));
317 return pa_socket_client_new_sockaddr(m
, (struct sockaddr
*) &sa
, sizeof(sa
));
320 #ifdef HAVE_LIBASYNCNS
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
;
326 assert(m
&& c
&& c
->asyncns_io_event
== e
&& fd
>= 0);
328 if (asyncns_wait(c
->asyncns
, 0) < 0)
331 if (!asyncns_isdone(c
->asyncns
, c
->asyncns_query
))
334 ret
= asyncns_getaddrinfo_done(c
->asyncns
, c
->asyncns_query
, &res
);
335 c
->asyncns_query
= NULL
;
337 if (ret
!= 0 || !res
)
341 sockaddr_prepare(c
, res
->ai_addr
, res
->ai_addrlen
);
343 asyncns_freeaddrinfo(res
);
348 errno
== EHOSTUNREACH
;
353 m
->io_free(c
->asyncns_io_event
);
354 c
->asyncns_io_event
= NULL
;
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
;
375 static void start_timeout(struct pa_socket_client
*c
) {
378 assert(!c
->timeout_event
);
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
);
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
;
390 if (pa_parse_address(name
, &a
) < 0)
394 a
.port
= default_port
;
397 case PA_PARSED_ADDRESS_UNIX
:
398 if ((c
= pa_socket_client_new_unix(m
, a
.path_or_host
)))
402 case PA_PARSED_ADDRESS_TCP4
: /* Fallthrough */
403 case PA_PARSED_ADDRESS_TCP6
: /* Fallthrough */
404 case PA_PARSED_ADDRESS_TCP_AUTO
:{
406 struct addrinfo hints
;
409 snprintf(port
, sizeof(port
), "%u", (unsigned) a
.port
);
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
;
415 #ifdef HAVE_LIBASYNCNS
419 if (!(asyncns
= asyncns_new(1)))
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
);
432 struct addrinfo
*res
= NULL
;
434 ret
= getaddrinfo(a
.path_or_host
, port
, &hints
, &res
);
440 if ((c
= pa_socket_client_new_sockaddr(m
, res
->ai_addr
, res
->ai_addrlen
)))
451 pa_xfree(a
.path_or_host
);
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
) {