]>
code.delx.au - pulseaudio/blob - polyp/socket-client.c
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
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
37 #include "socket-client.h"
38 #include "socket-util.h"
43 struct pa_socket_client
{
45 struct pa_mainloop_api
*mainloop
;
47 struct pa_io_event
*io_event
;
48 struct pa_defer_event
*defer_event
;
49 void (*callback
)(struct pa_socket_client
*c
, struct pa_iochannel
*io
, void *userdata
);
54 static struct pa_socket_client
*pa_socket_client_new(struct pa_mainloop_api
*m
) {
55 struct pa_socket_client
*c
;
58 c
= pa_xmalloc(sizeof(struct pa_socket_client
));
63 c
->defer_event
= NULL
;
70 static void do_call(struct pa_socket_client
*c
) {
71 struct pa_iochannel
*io
= NULL
;
74 assert(c
&& c
->callback
);
76 pa_socket_client_ref(c
);
78 lerror
= sizeof(error
);
79 if (getsockopt(c
->fd
, SOL_SOCKET
, SO_ERROR
, &error
, &lerror
) < 0) {
80 pa_log(__FILE__
": getsockopt(): %s\n", strerror(errno
));
84 if (lerror
!= sizeof(error
)) {
85 pa_log(__FILE__
": getsockopt() returned invalid size.\n");
90 /* pa_log(__FILE__": connect(): %s\n", strerror(error)); */
95 io
= pa_iochannel_new(c
->mainloop
, c
->fd
, c
->fd
);
104 c
->callback(c
, io
, c
->userdata
);
106 pa_socket_client_unref(c
);
109 static void connect_fixed_cb(struct pa_mainloop_api
*m
, struct pa_defer_event
*e
, void *userdata
) {
110 struct pa_socket_client
*c
= userdata
;
111 assert(m
&& c
&& c
->defer_event
== e
);
112 m
->defer_free(c
->defer_event
);
113 c
->defer_event
= NULL
;
117 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
) {
118 struct pa_socket_client
*c
= userdata
;
119 assert(m
&& c
&& c
->io_event
== e
&& fd
>= 0);
120 m
->io_free(c
->io_event
);
125 static int do_connect(struct pa_socket_client
*c
, const struct sockaddr
*sa
, socklen_t len
) {
127 assert(c
&& sa
&& len
);
129 pa_make_nonblock_fd(c
->fd
);
131 if ((r
= connect(c
->fd
, sa
, len
)) < 0) {
132 if (errno
!= EINPROGRESS
) {
133 /*pa_log(__FILE__": connect(): %s\n", strerror(errno));*/
137 c
->io_event
= c
->mainloop
->io_new(c
->mainloop
, c
->fd
, PA_IO_EVENT_OUTPUT
, connect_io_cb
, c
);
140 c
->defer_event
= c
->mainloop
->defer_new(c
->mainloop
, connect_fixed_cb
, c
);
141 assert(c
->defer_event
);
147 struct pa_socket_client
* pa_socket_client_new_ipv4(struct pa_mainloop_api
*m
, uint32_t address
, uint16_t port
) {
148 struct sockaddr_in sa
;
149 assert(m
&& port
> 0);
151 memset(&sa
, 0, sizeof(sa
));
152 sa
.sin_family
= AF_INET
;
153 sa
.sin_port
= htons(port
);
154 sa
.sin_addr
.s_addr
= htonl(address
);
156 return pa_socket_client_new_sockaddr(m
, (struct sockaddr
*) &sa
, sizeof(sa
));
159 struct pa_socket_client
* pa_socket_client_new_unix(struct pa_mainloop_api
*m
, const char *filename
) {
160 struct sockaddr_un sa
;
161 assert(m
&& filename
);
163 memset(&sa
, 0, sizeof(sa
));
164 sa
.sun_family
= AF_LOCAL
;
165 strncpy(sa
.sun_path
, filename
, sizeof(sa
.sun_path
)-1);
166 sa
.sun_path
[sizeof(sa
.sun_path
) - 1] = 0;
168 return pa_socket_client_new_sockaddr(m
, (struct sockaddr
*) &sa
, sizeof(sa
));
171 struct pa_socket_client
* pa_socket_client_new_sockaddr(struct pa_mainloop_api
*m
, const struct sockaddr
*sa
, size_t salen
) {
172 struct pa_socket_client
*c
;
174 c
= pa_socket_client_new(m
);
177 switch (sa
->sa_family
) {
183 c
->local
= ((const struct sockaddr_in
*) sa
)->sin_addr
.s_addr
== INADDR_LOOPBACK
;
187 c
->local
= memcmp(&((const struct sockaddr_in6
*) sa
)->sin6_addr
, &in6addr_loopback
, sizeof(struct in6_addr
)) == 0;
194 if ((c
->fd
= socket(sa
->sa_family
, SOCK_STREAM
, 0)) < 0) {
195 pa_log(__FILE__
": socket(): %s\n", strerror(errno
));
199 pa_fd_set_cloexec(c
->fd
, 1);
200 if (sa
->sa_family
== AF_INET
|| sa
->sa_family
== AF_INET6
)
201 pa_socket_tcp_low_delay(c
->fd
);
203 pa_socket_low_delay(c
->fd
);
205 if (do_connect(c
, sa
, salen
) < 0)
211 pa_socket_client_unref(c
);
216 void socket_client_free(struct pa_socket_client
*c
) {
217 assert(c
&& c
->mainloop
);
219 c
->mainloop
->io_free(c
->io_event
);
221 c
->mainloop
->defer_free(c
->defer_event
);
227 void pa_socket_client_unref(struct pa_socket_client
*c
) {
228 assert(c
&& c
->ref
>= 1);
231 socket_client_free(c
);
234 struct pa_socket_client
* pa_socket_client_ref(struct pa_socket_client
*c
) {
235 assert(c
&& c
->ref
>= 1);
240 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
) {
242 c
->callback
= on_connection
;
243 c
->userdata
= userdata
;
246 struct pa_socket_client
* pa_socket_client_new_ipv6(struct pa_mainloop_api
*m
, uint8_t address
[16], uint16_t port
) {
247 struct sockaddr_in6 sa
;
249 memset(&sa
, 0, sizeof(sa
));
250 sa
.sin6_family
= AF_INET6
;
251 sa
.sin6_port
= htons(port
);
252 memcpy(&sa
.sin6_addr
, address
, sizeof(sa
.sin6_addr
));
254 return pa_socket_client_new_sockaddr(m
, (struct sockaddr
*) &sa
, sizeof(sa
));
257 /* Parse addresses in one of the following forms:
263 * Return a newly allocated string of the hostname and fill in *port if specified */
265 static char *parse_address(const char *s
, uint16_t *port
) {
269 if (!(e
= strchr(s
+1, ']')))
277 return pa_xstrndup(s
+1, e
-s
-1);
281 if (!(e
= strrchr(s
, ':')))
282 return pa_xstrdup(s
);
285 return pa_xstrndup(s
, e
-s
);
289 struct pa_socket_client
* pa_socket_client_new_string(struct pa_mainloop_api
*m
, const char*name
, uint16_t default_port
) {
291 struct pa_socket_client
*c
= NULL
;
292 enum { KIND_UNIX
, KIND_TCP_AUTO
, KIND_TCP4
, KIND_TCP6
} kind
= KIND_TCP_AUTO
;
297 /* The URL starts with a host specification for detecting local connections */
299 if (!pa_get_host_name(hn
, sizeof(hn
)))
302 pfx
= pa_sprintf_malloc("{%s}", hn
);
303 if (!pa_startswith(name
, pfx
))
307 p
= name
+ strlen(pfx
);
313 else if (pa_startswith(p
, "unix:")) {
315 p
+= sizeof("unix:")-1;
316 } else if (pa_startswith(p
, "tcp:") || pa_startswith(p
, "tcp4:")) {
318 p
+= sizeof("tcp:")-1;
319 } else if (pa_startswith(p
, "tcp6:")) {
321 p
+= sizeof("tcp6:")-1;
326 return pa_socket_client_new_unix(m
, p
);
328 case KIND_TCP_AUTO
: /* Fallthrough */
331 uint16_t port
= default_port
;
333 struct addrinfo hints
, *res
;
335 if (!(h
= parse_address(p
, &port
)))
338 memset(&hints
, 0, sizeof(hints
));
339 hints
.ai_family
= kind
== KIND_TCP4
? AF_INET
: (kind
== KIND_TCP6
? AF_INET6
: AF_UNSPEC
);
341 if (getaddrinfo(h
, NULL
, &hints
, &res
) < 0 || !res
|| !res
->ai_addr
)
344 if (res
->ai_family
== AF_INET
) {
345 if (res
->ai_addrlen
!= sizeof(struct sockaddr_in
))
347 assert(res
->ai_addr
->sa_family
== res
->ai_family
);
349 ((struct sockaddr_in
*) res
->ai_addr
)->sin_port
= htons(port
);
350 } else if (res
->ai_family
== AF_INET6
) {
351 if (res
->ai_addrlen
!= sizeof(struct sockaddr_in6
))
353 assert(res
->ai_addr
->sa_family
== res
->ai_family
);
355 ((struct sockaddr_in6
*) res
->ai_addr
)->sin6_port
= htons(port
);
359 c
= pa_socket_client_new_sockaddr(m
, res
->ai_addr
, res
->ai_addrlen
);
365 /* Should never be reached */
371 /* Return non-zero when the target sockaddr is considered
372 local. "local" means UNIX socket or TCP socket on localhost. Other
373 local IP addresses are not considered local. */
374 int pa_socket_client_is_local(struct pa_socket_client
*c
) {