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 struct pa_socket_client
{
51 struct pa_mainloop_api
*mainloop
;
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
);
58 #ifdef HAVE_LIBASYNCNS
60 asyncns_query_t
* asyncns_query
;
61 struct pa_io_event
*asyncns_io_event
;
65 static struct pa_socket_client
*pa_socket_client_new(struct pa_mainloop_api
*m
) {
66 struct pa_socket_client
*c
;
69 c
= pa_xmalloc(sizeof(struct pa_socket_client
));
74 c
->defer_event
= NULL
;
79 #ifdef HAVE_LIBASYNCNS
81 c
->asyncns_io_event
= NULL
;
82 c
->asyncns_query
= NULL
;
88 static void do_call(struct pa_socket_client
*c
) {
89 struct pa_iochannel
*io
= NULL
;
92 assert(c
&& c
->callback
);
94 pa_socket_client_ref(c
);
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
));
105 if (lerror
!= sizeof(error
)) {
106 pa_log(__FILE__
": getsockopt() returned invalid size.\n");
111 /* pa_log(__FILE__": connect(): %s\n", strerror(error)); */
116 io
= pa_iochannel_new(c
->mainloop
, c
->fd
, c
->fd
);
120 if (!io
&& c
->fd
>= 0)
125 c
->callback(c
, io
, c
->userdata
);
127 pa_socket_client_unref(c
);
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
;
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
);
146 static int do_connect(struct pa_socket_client
*c
, const struct sockaddr
*sa
, socklen_t len
) {
148 assert(c
&& sa
&& len
);
150 pa_make_nonblock_fd(c
->fd
);
152 if ((r
= connect(c
->fd
, sa
, len
)) < 0) {
153 if (errno
!= EINPROGRESS
) {
154 /*pa_log(__FILE__": connect(): %s\n", strerror(errno));*/
158 c
->io_event
= c
->mainloop
->io_new(c
->mainloop
, c
->fd
, PA_IO_EVENT_OUTPUT
, connect_io_cb
, c
);
161 c
->defer_event
= c
->mainloop
->defer_new(c
->mainloop
, connect_fixed_cb
, c
);
162 assert(c
->defer_event
);
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);
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
);
177 return pa_socket_client_new_sockaddr(m
, (struct sockaddr
*) &sa
, sizeof(sa
));
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
);
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;
189 return pa_socket_client_new_sockaddr(m
, (struct sockaddr
*) &sa
, sizeof(sa
));
192 static int sockaddr_prepare(struct pa_socket_client
*c
, const struct sockaddr
*sa
, size_t salen
) {
197 switch (sa
->sa_family
) {
203 c
->local
= ((const struct sockaddr_in
*) sa
)->sin_addr
.s_addr
== INADDR_LOOPBACK
;
207 c
->local
= memcmp(&((const struct sockaddr_in6
*) sa
)->sin6_addr
, &in6addr_loopback
, sizeof(struct in6_addr
)) == 0;
214 if ((c
->fd
= socket(sa
->sa_family
, SOCK_STREAM
, 0)) < 0) {
215 pa_log(__FILE__
": socket(): %s\n", strerror(errno
));
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
);
223 pa_socket_low_delay(c
->fd
);
225 if (do_connect(c
, sa
, salen
) < 0)
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
;
234 c
= pa_socket_client_new(m
);
237 if (sockaddr_prepare(c
, sa
, salen
) < 0)
243 pa_socket_client_unref(c
);
248 void socket_client_free(struct pa_socket_client
*c
) {
249 assert(c
&& c
->mainloop
);
251 c
->mainloop
->io_free(c
->io_event
);
253 c
->mainloop
->defer_free(c
->defer_event
);
257 #ifdef HAVE_LIBASYNCNS
258 if (c
->asyncns_query
)
259 asyncns_cancel(c
->asyncns
, c
->asyncns_query
);
261 asyncns_free(c
->asyncns
);
262 if (c
->asyncns_io_event
)
263 c
->mainloop
->io_free(c
->asyncns_io_event
);
269 void pa_socket_client_unref(struct pa_socket_client
*c
) {
270 assert(c
&& c
->ref
>= 1);
273 socket_client_free(c
);
276 struct pa_socket_client
* pa_socket_client_ref(struct pa_socket_client
*c
) {
277 assert(c
&& c
->ref
>= 1);
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
) {
284 c
->callback
= on_connection
;
285 c
->userdata
= userdata
;
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
;
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
));
296 return pa_socket_client_new_sockaddr(m
, (struct sockaddr
*) &sa
, sizeof(sa
));
299 #ifdef HAVE_LIBASYNCNS
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
;
305 assert(m
&& c
&& c
->asyncns_io_event
== e
&& fd
>= 0);
307 if (asyncns_wait(c
->asyncns
, 0) < 0)
310 if (!asyncns_isdone(c
->asyncns
, c
->asyncns_query
))
313 ret
= asyncns_getaddrinfo_done(c
->asyncns
, c
->asyncns_query
, &res
);
314 c
->asyncns_query
= NULL
;
316 if (ret
!= 0 || !res
)
320 sockaddr_prepare(c
, res
->ai_addr
, res
->ai_addrlen
);
322 asyncns_freeaddrinfo(res
);
326 m
->io_free(c
->asyncns_io_event
);
327 c
->asyncns_io_event
= NULL
;
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
;
338 if (pa_parse_address(name
, &a
) < 0)
342 a
.port
= default_port
;
345 case PA_PARSED_ADDRESS_UNIX
:
346 c
= pa_socket_client_new_unix(m
, a
.path_or_host
);
349 case PA_PARSED_ADDRESS_TCP4
: /* Fallthrough */
350 case PA_PARSED_ADDRESS_TCP6
: /* Fallthrough */
351 case PA_PARSED_ADDRESS_TCP_AUTO
:{
353 struct addrinfo hints
;
356 snprintf(port
, sizeof(port
), "%u", (unsigned) a
.port
);
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
;
362 #ifdef HAVE_LIBASYNCNS
366 if (!(asyncns
= asyncns_new(1)))
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
);
378 struct addrinfo
*res
= NULL
;
380 ret
= getaddrinfo(a
.path_or_host
, port
, &hints
, &res
);
386 c
= pa_socket_client_new_sockaddr(m
, res
->ai_addr
, res
->ai_addrlen
);
395 pa_xfree(a
.path_or_host
);
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
) {