2004-07-16 19:56:36 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
This file is part of polypaudio.
|
|
|
|
|
|
|
|
|
|
polypaudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
License, or (at your option) any later version.
|
2004-07-16 19:56:36 +00:00
|
|
|
|
|
|
|
|
polypaudio is distributed in the hope that it will be useful, but
|
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2004-11-14 14:58:54 +00:00
|
|
|
Lesser General Public License for more details.
|
2004-07-16 19:56:36 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
License along with polypaudio; if not, write to the Free Software
|
2004-07-16 19:56:36 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2005-01-08 01:15:11 +00:00
|
|
|
/* #undef HAVE_LIBASYNCNS */
|
|
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
2006-01-10 17:51:06 +00:00
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
2004-06-23 23:17:30 +00:00
|
|
|
#include <sys/un.h>
|
2006-01-10 17:51:06 +00:00
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
2004-06-23 23:17:30 +00:00
|
|
|
#include <arpa/inet.h>
|
2006-01-10 17:51:06 +00:00
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_NETDB_H
|
2004-11-11 21:18:33 +00:00
|
|
|
#include <netdb.h>
|
2006-01-10 17:51:06 +00:00
|
|
|
#endif
|
|
|
|
|
|
2005-01-08 01:15:11 +00:00
|
|
|
#ifdef HAVE_LIBASYNCNS
|
|
|
|
|
#include <asyncns.h>
|
|
|
|
|
#endif
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-01-10 17:51:06 +00:00
|
|
|
#include "winsock.h"
|
|
|
|
|
|
2006-05-17 16:34:18 +00:00
|
|
|
#include <polyp/xmalloc.h>
|
|
|
|
|
|
2006-02-17 12:10:58 +00:00
|
|
|
#include <polypcore/socket-util.h>
|
|
|
|
|
#include <polypcore/util.h>
|
|
|
|
|
#include <polypcore/log.h>
|
|
|
|
|
#include <polypcore/parseaddr.h>
|
|
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
#include "socket-client.h"
|
|
|
|
|
|
2005-01-11 20:47:10 +00:00
|
|
|
#define CONNECT_TIMEOUT 5
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
struct pa_socket_client {
|
2004-08-15 00:02:26 +00:00
|
|
|
int ref;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_mainloop_api *mainloop;
|
2004-06-23 23:17:30 +00:00
|
|
|
int fd;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_io_event *io_event;
|
|
|
|
|
pa_time_event *timeout_event;
|
|
|
|
|
pa_defer_event *defer_event;
|
|
|
|
|
void (*callback)(pa_socket_client*c, pa_iochannel *io, void *userdata);
|
2004-06-23 23:17:30 +00:00
|
|
|
void *userdata;
|
2004-11-11 21:18:33 +00:00
|
|
|
int local;
|
2005-01-08 01:15:11 +00:00
|
|
|
#ifdef HAVE_LIBASYNCNS
|
|
|
|
|
asyncns_t *asyncns;
|
|
|
|
|
asyncns_query_t * asyncns_query;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_io_event *asyncns_io_event;
|
2005-01-08 01:15:11 +00:00
|
|
|
#endif
|
2004-06-23 23:17:30 +00:00
|
|
|
};
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static pa_socket_client*pa_socket_client_new(pa_mainloop_api *m) {
|
|
|
|
|
pa_socket_client *c;
|
2004-06-23 23:17:30 +00:00
|
|
|
assert(m);
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
c = pa_xmalloc(sizeof(pa_socket_client));
|
2004-08-15 00:02:26 +00:00
|
|
|
c->ref = 1;
|
2004-06-23 23:17:30 +00:00
|
|
|
c->mainloop = m;
|
|
|
|
|
c->fd = -1;
|
2004-08-05 19:53:57 +00:00
|
|
|
c->io_event = NULL;
|
|
|
|
|
c->defer_event = NULL;
|
2005-01-11 20:47:10 +00:00
|
|
|
c->timeout_event = NULL;
|
2004-06-23 23:17:30 +00:00
|
|
|
c->callback = NULL;
|
|
|
|
|
c->userdata = NULL;
|
2004-11-11 21:18:33 +00:00
|
|
|
c->local = 0;
|
2005-01-08 01:15:11 +00:00
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBASYNCNS
|
|
|
|
|
c->asyncns = NULL;
|
|
|
|
|
c->asyncns_io_event = NULL;
|
|
|
|
|
c->asyncns_query = NULL;
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void free_events(pa_socket_client *c) {
|
2005-01-11 20:47:10 +00:00
|
|
|
assert(c);
|
|
|
|
|
|
|
|
|
|
if (c->io_event) {
|
|
|
|
|
c->mainloop->io_free(c->io_event);
|
|
|
|
|
c->io_event = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->defer_event) {
|
|
|
|
|
c->mainloop->defer_free(c->defer_event);
|
|
|
|
|
c->defer_event = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->timeout_event) {
|
|
|
|
|
c->mainloop->time_free(c->timeout_event);
|
|
|
|
|
c->timeout_event = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void do_call(pa_socket_client *c) {
|
|
|
|
|
pa_iochannel *io = NULL;
|
2004-09-01 00:46:56 +00:00
|
|
|
int error;
|
|
|
|
|
socklen_t lerror;
|
2004-06-23 23:17:30 +00:00
|
|
|
assert(c && c->callback);
|
|
|
|
|
|
2004-08-15 00:02:26 +00:00
|
|
|
pa_socket_client_ref(c);
|
2005-01-08 01:15:11 +00:00
|
|
|
|
|
|
|
|
if (c->fd < 0)
|
|
|
|
|
goto finish;
|
2004-08-15 00:02:26 +00:00
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
lerror = sizeof(error);
|
2006-01-10 17:51:06 +00:00
|
|
|
if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, (void*)&error, &lerror) < 0) {
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log(__FILE__": getsockopt(): %s", strerror(errno));
|
2004-08-15 00:02:26 +00:00
|
|
|
goto finish;
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lerror != sizeof(error)) {
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log(__FILE__": getsockopt() returned invalid size.");
|
2004-08-15 00:02:26 +00:00
|
|
|
goto finish;
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error != 0) {
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log_debug(__FILE__": connect(): %s", strerror(error));
|
2004-11-11 21:18:33 +00:00
|
|
|
errno = error;
|
2004-08-15 00:02:26 +00:00
|
|
|
goto finish;
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
2006-04-19 11:56:26 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
io = pa_iochannel_new(c->mainloop, c->fd, c->fd);
|
2004-06-23 23:17:30 +00:00
|
|
|
assert(io);
|
2004-08-15 00:02:26 +00:00
|
|
|
|
|
|
|
|
finish:
|
2005-01-08 01:15:11 +00:00
|
|
|
if (!io && c->fd >= 0)
|
2004-08-15 00:02:26 +00:00
|
|
|
close(c->fd);
|
2004-06-23 23:17:30 +00:00
|
|
|
c->fd = -1;
|
2005-01-11 20:47:10 +00:00
|
|
|
|
|
|
|
|
free_events(c);
|
2004-08-15 00:02:26 +00:00
|
|
|
|
|
|
|
|
assert(c->callback);
|
2004-06-23 23:17:30 +00:00
|
|
|
c->callback(c, io, c->userdata);
|
|
|
|
|
|
2004-08-15 00:02:26 +00:00
|
|
|
pa_socket_client_unref(c);
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void connect_fixed_cb(pa_mainloop_api *m, pa_defer_event *e, void *userdata) {
|
|
|
|
|
pa_socket_client *c = userdata;
|
2004-08-05 19:53:57 +00:00
|
|
|
assert(m && c && c->defer_event == e);
|
2004-06-23 23:17:30 +00:00
|
|
|
do_call(c);
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
static void connect_io_cb(pa_mainloop_api*m, pa_io_event *e, int fd, PA_GCC_UNUSED pa_io_event_flags_t f, void *userdata) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_socket_client *c = userdata;
|
2004-08-05 19:53:57 +00:00
|
|
|
assert(m && c && c->io_event == e && fd >= 0);
|
2004-06-23 23:17:30 +00:00
|
|
|
do_call(c);
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static int do_connect(pa_socket_client *c, const struct sockaddr *sa, socklen_t len) {
|
2004-06-23 23:17:30 +00:00
|
|
|
int r;
|
|
|
|
|
assert(c && sa && len);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_make_nonblock_fd(c->fd);
|
2004-06-23 23:17:30 +00:00
|
|
|
|
|
|
|
|
if ((r = connect(c->fd, sa, len)) < 0) {
|
2006-04-19 11:56:26 +00:00
|
|
|
#ifdef OS_IS_WIN32
|
|
|
|
|
if (WSAGetLastError() != EWOULDBLOCK) {
|
|
|
|
|
pa_log_debug(__FILE__": connect(): %d", WSAGetLastError());
|
|
|
|
|
#else
|
2004-07-15 00:16:27 +00:00
|
|
|
if (errno != EINPROGRESS) {
|
2006-04-19 11:56:26 +00:00
|
|
|
pa_log_debug(__FILE__": connect(): %s (%d)", strerror(errno), errno);
|
|
|
|
|
#endif
|
2004-06-23 23:17:30 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-05 19:53:57 +00:00
|
|
|
c->io_event = c->mainloop->io_new(c->mainloop, c->fd, PA_IO_EVENT_OUTPUT, connect_io_cb, c);
|
|
|
|
|
assert(c->io_event);
|
2004-06-23 23:17:30 +00:00
|
|
|
} else {
|
2004-08-05 19:53:57 +00:00
|
|
|
c->defer_event = c->mainloop->defer_new(c->mainloop, connect_fixed_cb, c);
|
|
|
|
|
assert(c->defer_event);
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_socket_client* pa_socket_client_new_ipv4(pa_mainloop_api *m, uint32_t address, uint16_t port) {
|
2004-06-23 23:17:30 +00:00
|
|
|
struct sockaddr_in sa;
|
2004-11-11 21:18:33 +00:00
|
|
|
assert(m && port > 0);
|
2004-06-30 00:00:52 +00:00
|
|
|
|
2004-11-11 21:18:33 +00:00
|
|
|
memset(&sa, 0, sizeof(sa));
|
2004-06-23 23:17:30 +00:00
|
|
|
sa.sin_family = AF_INET;
|
|
|
|
|
sa.sin_port = htons(port);
|
|
|
|
|
sa.sin_addr.s_addr = htonl(address);
|
|
|
|
|
|
2004-11-11 21:18:33 +00:00
|
|
|
return pa_socket_client_new_sockaddr(m, (struct sockaddr*) &sa, sizeof(sa));
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-10 17:51:06 +00:00
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_socket_client* pa_socket_client_new_unix(pa_mainloop_api *m, const char *filename) {
|
2004-06-23 23:17:30 +00:00
|
|
|
struct sockaddr_un sa;
|
2004-06-23 23:38:33 +00:00
|
|
|
assert(m && filename);
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2004-11-11 21:18:33 +00:00
|
|
|
memset(&sa, 0, sizeof(sa));
|
2006-01-10 17:51:06 +00:00
|
|
|
sa.sun_family = AF_UNIX;
|
2004-06-23 23:17:30 +00:00
|
|
|
strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
|
|
|
|
|
sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
|
|
|
|
|
|
2004-11-11 21:18:33 +00:00
|
|
|
return pa_socket_client_new_sockaddr(m, (struct sockaddr*) &sa, sizeof(sa));
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
2004-07-15 00:16:27 +00:00
|
|
|
|
2006-01-10 17:51:06 +00:00
|
|
|
#else /* HAVE_SYS_UN_H */
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_socket_client* pa_socket_client_new_unix(pa_mainloop_api *m, const char *filename) {
|
2006-01-10 17:51:06 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* HAVE_SYS_UN_H */
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static int sockaddr_prepare(pa_socket_client *c, const struct sockaddr *sa, size_t salen) {
|
2004-07-15 00:16:27 +00:00
|
|
|
assert(c);
|
2005-01-08 01:15:11 +00:00
|
|
|
assert(sa);
|
|
|
|
|
assert(salen);
|
|
|
|
|
|
2004-11-11 21:18:33 +00:00
|
|
|
switch (sa->sa_family) {
|
|
|
|
|
case AF_UNIX:
|
|
|
|
|
c->local = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AF_INET:
|
|
|
|
|
c->local = ((const struct sockaddr_in*) sa)->sin_addr.s_addr == INADDR_LOOPBACK;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AF_INET6:
|
|
|
|
|
c->local = memcmp(&((const struct sockaddr_in6*) sa)->sin6_addr, &in6addr_loopback, sizeof(struct in6_addr)) == 0;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
c->local = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-15 00:16:27 +00:00
|
|
|
if ((c->fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log(__FILE__": socket(): %s", strerror(errno));
|
2005-01-08 01:15:11 +00:00
|
|
|
return -1;
|
2004-07-15 00:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-01 22:36:49 +00:00
|
|
|
pa_fd_set_cloexec(c->fd, 1);
|
2004-11-11 21:18:33 +00:00
|
|
|
if (sa->sa_family == AF_INET || sa->sa_family == AF_INET6)
|
2004-07-15 00:16:27 +00:00
|
|
|
pa_socket_tcp_low_delay(c->fd);
|
|
|
|
|
else
|
|
|
|
|
pa_socket_low_delay(c->fd);
|
|
|
|
|
|
|
|
|
|
if (do_connect(c, sa, salen) < 0)
|
2005-01-08 01:15:11 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_socket_client* pa_socket_client_new_sockaddr(pa_mainloop_api *m, const struct sockaddr *sa, size_t salen) {
|
|
|
|
|
pa_socket_client *c;
|
2005-01-08 01:15:11 +00:00
|
|
|
assert(m && sa);
|
|
|
|
|
c = pa_socket_client_new(m);
|
|
|
|
|
assert(c);
|
|
|
|
|
|
|
|
|
|
if (sockaddr_prepare(c, sa, salen) < 0)
|
2004-07-15 00:16:27 +00:00
|
|
|
goto fail;
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2004-07-15 00:16:27 +00:00
|
|
|
return c;
|
|
|
|
|
|
|
|
|
|
fail:
|
2004-08-15 00:02:26 +00:00
|
|
|
pa_socket_client_unref(c);
|
2004-07-15 00:16:27 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void socket_client_free(pa_socket_client *c) {
|
2004-06-23 23:17:30 +00:00
|
|
|
assert(c && c->mainloop);
|
2005-01-11 20:47:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
free_events(c);
|
|
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
if (c->fd >= 0)
|
|
|
|
|
close(c->fd);
|
2005-01-08 01:15:11 +00:00
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBASYNCNS
|
|
|
|
|
if (c->asyncns_query)
|
|
|
|
|
asyncns_cancel(c->asyncns, c->asyncns_query);
|
|
|
|
|
if (c->asyncns)
|
|
|
|
|
asyncns_free(c->asyncns);
|
|
|
|
|
if (c->asyncns_io_event)
|
|
|
|
|
c->mainloop->io_free(c->asyncns_io_event);
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(c);
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_socket_client_unref(pa_socket_client *c) {
|
2004-08-15 00:02:26 +00:00
|
|
|
assert(c && c->ref >= 1);
|
|
|
|
|
|
|
|
|
|
if (!(--(c->ref)))
|
|
|
|
|
socket_client_free(c);
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_socket_client* pa_socket_client_ref(pa_socket_client *c) {
|
2004-08-15 00:02:26 +00:00
|
|
|
assert(c && c->ref >= 1);
|
|
|
|
|
c->ref++;
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_socket_client_set_callback(pa_socket_client *c, void (*on_connection)(pa_socket_client *c, pa_iochannel*io, void *userdata), void *userdata) {
|
2004-06-23 23:17:30 +00:00
|
|
|
assert(c);
|
|
|
|
|
c->callback = on_connection;
|
|
|
|
|
c->userdata = userdata;
|
|
|
|
|
}
|
2004-11-11 21:18:33 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_socket_client* pa_socket_client_new_ipv6(pa_mainloop_api *m, uint8_t address[16], uint16_t port) {
|
2004-11-11 21:18:33 +00:00
|
|
|
struct sockaddr_in6 sa;
|
|
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
|
sa.sin6_family = AF_INET6;
|
|
|
|
|
sa.sin6_port = htons(port);
|
|
|
|
|
memcpy(&sa.sin6_addr, address, sizeof(sa.sin6_addr));
|
|
|
|
|
|
|
|
|
|
return pa_socket_client_new_sockaddr(m, (struct sockaddr*) &sa, sizeof(sa));
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-08 01:15:11 +00:00
|
|
|
#ifdef HAVE_LIBASYNCNS
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
static void asyncns_cb(pa_mainloop_api*m, pa_io_event *e, int fd, PA_GCC_UNUSED pa_io_event_flags_t f, void *userdata) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_socket_client *c = userdata;
|
2005-01-08 01:15:11 +00:00
|
|
|
struct addrinfo *res = NULL;
|
|
|
|
|
int ret;
|
|
|
|
|
assert(m && c && c->asyncns_io_event == e && fd >= 0);
|
|
|
|
|
|
|
|
|
|
if (asyncns_wait(c->asyncns, 0) < 0)
|
2005-01-11 20:47:10 +00:00
|
|
|
goto fail;
|
2005-01-08 01:15:11 +00:00
|
|
|
|
|
|
|
|
if (!asyncns_isdone(c->asyncns, c->asyncns_query))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
ret = asyncns_getaddrinfo_done(c->asyncns, c->asyncns_query, &res);
|
|
|
|
|
c->asyncns_query = NULL;
|
|
|
|
|
|
|
|
|
|
if (ret != 0 || !res)
|
2005-01-11 20:47:10 +00:00
|
|
|
goto fail;
|
2005-01-08 01:15:11 +00:00
|
|
|
|
|
|
|
|
if (res->ai_addr)
|
|
|
|
|
sockaddr_prepare(c, res->ai_addr, res->ai_addrlen);
|
|
|
|
|
|
|
|
|
|
asyncns_freeaddrinfo(res);
|
|
|
|
|
|
2005-01-11 20:47:10 +00:00
|
|
|
goto finish;
|
|
|
|
|
|
|
|
|
|
fail:
|
2006-04-18 19:31:50 +00:00
|
|
|
errno = EHOSTUNREACH;
|
2005-01-11 20:47:10 +00:00
|
|
|
do_call(c);
|
|
|
|
|
|
2005-01-08 01:15:11 +00:00
|
|
|
finish:
|
|
|
|
|
|
|
|
|
|
m->io_free(c->asyncns_io_event);
|
|
|
|
|
c->asyncns_io_event = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void timeout_cb(pa_mainloop_api *m, pa_time_event *e, const struct timeval *tv, void *userdata) {
|
|
|
|
|
pa_socket_client *c = userdata;
|
2005-01-11 20:47:10 +00:00
|
|
|
assert(m);
|
|
|
|
|
assert(e);
|
|
|
|
|
assert(tv);
|
|
|
|
|
assert(c);
|
|
|
|
|
|
|
|
|
|
if (c->fd >= 0) {
|
|
|
|
|
close(c->fd);
|
|
|
|
|
c->fd = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
errno = ETIMEDOUT;
|
|
|
|
|
do_call(c);
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void start_timeout(pa_socket_client *c) {
|
2005-01-11 20:47:10 +00:00
|
|
|
struct timeval tv;
|
|
|
|
|
assert(c);
|
|
|
|
|
assert(!c->timeout_event);
|
|
|
|
|
|
2006-01-10 17:51:06 +00:00
|
|
|
pa_gettimeofday(&tv);
|
2005-01-11 20:47:10 +00:00
|
|
|
pa_timeval_add(&tv, CONNECT_TIMEOUT * 1000000);
|
|
|
|
|
c->timeout_event = c->mainloop->time_new(c->mainloop, &tv, timeout_cb, c);
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_socket_client* pa_socket_client_new_string(pa_mainloop_api *m, const char*name, uint16_t default_port) {
|
|
|
|
|
pa_socket_client *c = NULL;
|
|
|
|
|
pa_parsed_address a;
|
2004-11-11 21:18:33 +00:00
|
|
|
assert(m && name);
|
|
|
|
|
|
2004-12-15 01:02:50 +00:00
|
|
|
if (pa_parse_address(name, &a) < 0)
|
|
|
|
|
return NULL;
|
2004-11-11 21:18:33 +00:00
|
|
|
|
2005-01-06 01:07:43 +00:00
|
|
|
if (!a.port)
|
|
|
|
|
a.port = default_port;
|
|
|
|
|
|
2004-12-15 01:02:50 +00:00
|
|
|
switch (a.type) {
|
|
|
|
|
case PA_PARSED_ADDRESS_UNIX:
|
2005-01-12 17:37:31 +00:00
|
|
|
if ((c = pa_socket_client_new_unix(m, a.path_or_host)))
|
|
|
|
|
start_timeout(c);
|
2004-12-15 01:02:50 +00:00
|
|
|
break;
|
2004-11-11 21:18:33 +00:00
|
|
|
|
2004-12-15 01:02:50 +00:00
|
|
|
case PA_PARSED_ADDRESS_TCP4: /* Fallthrough */
|
|
|
|
|
case PA_PARSED_ADDRESS_TCP6: /* Fallthrough */
|
|
|
|
|
case PA_PARSED_ADDRESS_TCP_AUTO:{
|
2004-11-11 21:18:33 +00:00
|
|
|
|
2005-01-08 01:15:11 +00:00
|
|
|
struct addrinfo hints;
|
|
|
|
|
char port[12];
|
2004-11-18 20:50:44 +00:00
|
|
|
|
2005-01-08 01:15:11 +00:00
|
|
|
snprintf(port, sizeof(port), "%u", (unsigned) a.port);
|
2004-11-11 21:18:33 +00:00
|
|
|
|
2005-01-08 01:15:11 +00:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
|
hints.ai_family = a.type == PA_PARSED_ADDRESS_TCP4 ? PF_INET : (a.type == PA_PARSED_ADDRESS_TCP6 ? PF_INET6 : PF_UNSPEC);
|
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBASYNCNS
|
|
|
|
|
{
|
|
|
|
|
asyncns_t *asyncns;
|
2004-11-17 00:05:25 +00:00
|
|
|
|
2005-01-08 01:15:11 +00:00
|
|
|
if (!(asyncns = asyncns_new(1)))
|
2004-12-15 01:02:50 +00:00
|
|
|
goto finish;
|
2005-01-08 01:15:11 +00:00
|
|
|
|
|
|
|
|
c = pa_socket_client_new(m);
|
|
|
|
|
c->asyncns = asyncns;
|
|
|
|
|
c->asyncns_io_event = m->io_new(m, asyncns_fd(c->asyncns), PA_IO_EVENT_INPUT, asyncns_cb, c);
|
|
|
|
|
c->asyncns_query = asyncns_getaddrinfo(c->asyncns, a.path_or_host, port, &hints);
|
|
|
|
|
assert(c->asyncns_query);
|
2005-01-11 20:47:10 +00:00
|
|
|
start_timeout(c);
|
2005-01-08 01:15:11 +00:00
|
|
|
}
|
2006-01-10 17:51:06 +00:00
|
|
|
#else /* HAVE_LIBASYNCNS */
|
2005-01-08 01:15:11 +00:00
|
|
|
{
|
2006-01-10 17:51:06 +00:00
|
|
|
#ifdef HAVE_GETADDRINFO
|
2005-01-08 01:15:11 +00:00
|
|
|
int ret;
|
|
|
|
|
struct addrinfo *res = NULL;
|
|
|
|
|
|
|
|
|
|
ret = getaddrinfo(a.path_or_host, port, &hints, &res);
|
2004-11-17 00:05:25 +00:00
|
|
|
|
2005-01-08 01:15:11 +00:00
|
|
|
if (ret < 0 || !res)
|
|
|
|
|
goto finish;
|
2004-11-17 00:05:25 +00:00
|
|
|
|
2005-01-11 20:47:10 +00:00
|
|
|
if (res->ai_addr) {
|
2005-01-12 17:37:31 +00:00
|
|
|
if ((c = pa_socket_client_new_sockaddr(m, res->ai_addr, res->ai_addrlen)))
|
2006-01-10 17:51:06 +00:00
|
|
|
start_timeout(c);
|
2005-09-15 23:47:01 +00:00
|
|
|
}
|
2005-01-08 01:15:11 +00:00
|
|
|
|
|
|
|
|
freeaddrinfo(res);
|
2006-01-10 17:51:06 +00:00
|
|
|
#else /* HAVE_GETADDRINFO */
|
|
|
|
|
struct hostent *host = NULL;
|
|
|
|
|
struct sockaddr_in s;
|
|
|
|
|
|
2006-04-19 11:56:26 +00:00
|
|
|
/* FIXME: PF_INET6 support */
|
|
|
|
|
if (hints.ai_family == PF_INET6) {
|
|
|
|
|
pa_log_error(__FILE__": IPv6 is not supported on Windows");
|
2006-01-10 17:51:06 +00:00
|
|
|
goto finish;
|
2006-04-19 11:56:26 +00:00
|
|
|
}
|
2006-01-10 17:51:06 +00:00
|
|
|
|
|
|
|
|
host = gethostbyname(a.path_or_host);
|
|
|
|
|
if (!host) {
|
|
|
|
|
unsigned int addr = inet_addr(a.path_or_host);
|
|
|
|
|
if (addr != INADDR_NONE)
|
|
|
|
|
host = gethostbyaddr((char*)&addr, 4, AF_INET);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!host)
|
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
|
|
s.sin_family = AF_INET;
|
|
|
|
|
memcpy(&s.sin_addr, host->h_addr, sizeof(struct in_addr));
|
2006-02-24 16:28:02 +00:00
|
|
|
s.sin_port = htons(a.port);
|
2006-01-10 17:51:06 +00:00
|
|
|
|
2006-02-24 16:29:05 +00:00
|
|
|
if ((c = pa_socket_client_new_sockaddr(m, (struct sockaddr*)&s, sizeof(s))))
|
2006-01-10 17:51:06 +00:00
|
|
|
start_timeout(c);
|
|
|
|
|
#endif /* HAVE_GETADDRINFO */
|
2005-01-08 01:15:11 +00:00
|
|
|
}
|
2006-01-10 17:51:06 +00:00
|
|
|
#endif /* HAVE_LIBASYNCNS */
|
2004-11-11 21:18:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-15 01:02:50 +00:00
|
|
|
finish:
|
|
|
|
|
pa_xfree(a.path_or_host);
|
|
|
|
|
return c;
|
2004-11-11 21:18:33 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-17 00:05:25 +00:00
|
|
|
/* Return non-zero when the target sockaddr is considered
|
|
|
|
|
local. "local" means UNIX socket or TCP socket on localhost. Other
|
|
|
|
|
local IP addresses are not considered local. */
|
2006-01-11 01:17:39 +00:00
|
|
|
int pa_socket_client_is_local(pa_socket_client *c) {
|
2004-11-11 21:18:33 +00:00
|
|
|
assert(c);
|
|
|
|
|
return c->local;
|
|
|
|
|
}
|