#include <config.h>
#endif
+/* #undef HAVE_LIBASYNCNS */
+
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
+
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
-#include <netinet/in.h>
+#endif
+#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
+#endif
+#ifdef HAVE_NETINET_IN_H
+#include <netinet/in.h>
+#endif
+#ifdef HAVE_NETDB_H
#include <netdb.h>
+#endif
+
+#ifdef HAVE_LIBASYNCNS
+#include <asyncns.h>
+#endif
+
+#include "winsock.h"
#include "socket-client.h"
#include "socket-util.h"
#include "util.h"
#include "xmalloc.h"
#include "log.h"
+#include "parseaddr.h"
+
+#define CONNECT_TIMEOUT 5
struct pa_socket_client {
int ref;
struct pa_mainloop_api *mainloop;
int fd;
struct pa_io_event *io_event;
+ struct pa_time_event *timeout_event;
struct pa_defer_event *defer_event;
void (*callback)(struct pa_socket_client*c, struct pa_iochannel *io, void *userdata);
void *userdata;
int local;
+#ifdef HAVE_LIBASYNCNS
+ asyncns_t *asyncns;
+ asyncns_query_t * asyncns_query;
+ struct pa_io_event *asyncns_io_event;
+#endif
};
static struct pa_socket_client*pa_socket_client_new(struct pa_mainloop_api *m) {
c->fd = -1;
c->io_event = NULL;
c->defer_event = NULL;
+ c->timeout_event = NULL;
c->callback = NULL;
c->userdata = NULL;
c->local = 0;
+
+#ifdef HAVE_LIBASYNCNS
+ c->asyncns = NULL;
+ c->asyncns_io_event = NULL;
+ c->asyncns_query = NULL;
+#endif
+
return c;
}
+static void free_events(struct pa_socket_client *c) {
+ 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;
+ }
+}
+
static void do_call(struct pa_socket_client *c) {
struct pa_iochannel *io = NULL;
int error;
assert(c && c->callback);
pa_socket_client_ref(c);
+
+ if (c->fd < 0)
+ goto finish;
lerror = sizeof(error);
- if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &error, &lerror) < 0) {
+ if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, (void*)&error, &lerror) < 0) {
pa_log(__FILE__": getsockopt(): %s\n", strerror(errno));
goto finish;
}
}
if (error != 0) {
-/* pa_log(__FILE__": connect(): %s\n", strerror(error)); */
+ pa_log_debug(__FILE__": connect(): %s\n", strerror(error));
errno = error;
goto finish;
}
assert(io);
finish:
- if (!io)
+ if (!io && c->fd >= 0)
close(c->fd);
c->fd = -1;
+
+ free_events(c);
assert(c->callback);
c->callback(c, io, c->userdata);
static void connect_fixed_cb(struct pa_mainloop_api *m, struct pa_defer_event *e, void *userdata) {
struct pa_socket_client *c = userdata;
assert(m && c && c->defer_event == e);
- m->defer_free(c->defer_event);
- c->defer_event = NULL;
do_call(c);
}
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) {
struct pa_socket_client *c = userdata;
assert(m && c && c->io_event == e && fd >= 0);
- m->io_free(c->io_event);
- c->io_event = NULL;
do_call(c);
}
return pa_socket_client_new_sockaddr(m, (struct sockaddr*) &sa, sizeof(sa));
}
+#ifdef HAVE_SYS_UN_H
+
struct pa_socket_client* pa_socket_client_new_unix(struct pa_mainloop_api *m, const char *filename) {
struct sockaddr_un sa;
assert(m && filename);
memset(&sa, 0, sizeof(sa));
- sa.sun_family = AF_LOCAL;
+ sa.sun_family = AF_UNIX;
strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
return pa_socket_client_new_sockaddr(m, (struct sockaddr*) &sa, sizeof(sa));
}
-struct pa_socket_client* pa_socket_client_new_sockaddr(struct pa_mainloop_api *m, const struct sockaddr *sa, size_t salen) {
- struct pa_socket_client *c;
- assert(m && sa);
- c = pa_socket_client_new(m);
- assert(c);
+#else /* HAVE_SYS_UN_H */
+struct pa_socket_client* pa_socket_client_new_unix(struct pa_mainloop_api *m, const char *filename) {
+ return NULL;
+}
+
+#endif /* HAVE_SYS_UN_H */
+
+static int sockaddr_prepare(struct pa_socket_client *c, const struct sockaddr *sa, size_t salen) {
+ assert(c);
+ assert(sa);
+ assert(salen);
+
switch (sa->sa_family) {
case AF_UNIX:
c->local = 1;
if ((c->fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
pa_log(__FILE__": socket(): %s\n", strerror(errno));
- goto fail;
+ return -1;
}
pa_fd_set_cloexec(c->fd, 1);
pa_socket_low_delay(c->fd);
if (do_connect(c, sa, salen) < 0)
+ return -1;
+
+ return 0;
+}
+
+struct pa_socket_client* pa_socket_client_new_sockaddr(struct pa_mainloop_api *m, const struct sockaddr *sa, size_t salen) {
+ struct pa_socket_client *c;
+ assert(m && sa);
+ c = pa_socket_client_new(m);
+ assert(c);
+
+ if (sockaddr_prepare(c, sa, salen) < 0)
goto fail;
return c;
void socket_client_free(struct pa_socket_client *c) {
assert(c && c->mainloop);
- if (c->io_event)
- c->mainloop->io_free(c->io_event);
- if (c->defer_event)
- c->mainloop->defer_free(c->defer_event);
+
+
+ free_events(c);
+
if (c->fd >= 0)
close(c->fd);
+
+#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
+
pa_xfree(c);
}
return pa_socket_client_new_sockaddr(m, (struct sockaddr*) &sa, sizeof(sa));
}
-/* Parse addresses in one of the following forms:
- * HOSTNAME
- * HOSTNAME:PORT
- * [HOSTNAME]
- * [HOSTNAME]:PORT
- *
- * Return a newly allocated string of the hostname and fill in *port if specified */
-
-static char *parse_address(const char *s, uint16_t *port) {
- assert(s && port);
- if (*s == '[') {
- char *e;
- if (!(e = strchr(s+1, ']')))
- return NULL;
-
- if (e[1] == ':')
- *port = atoi(e+2);
- else if (e[1] != 0)
- return NULL;
-
- return pa_xstrndup(s+1, e-s-1);
- } else {
- char *e;
-
- if (!(e = strrchr(s, ':')))
- return pa_xstrdup(s);
+#ifdef HAVE_LIBASYNCNS
+
+static void asyncns_cb(struct pa_mainloop_api*m, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
+ struct pa_socket_client *c = userdata;
+ struct addrinfo *res = NULL;
+ int ret;
+ assert(m && c && c->asyncns_io_event == e && fd >= 0);
+
+ if (asyncns_wait(c->asyncns, 0) < 0)
+ goto fail;
+
+ 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)
+ goto fail;
+
+ if (res->ai_addr)
+ sockaddr_prepare(c, res->ai_addr, res->ai_addrlen);
+
+ asyncns_freeaddrinfo(res);
- *port = atoi(e+1);
- return pa_xstrndup(s, e-s);
+ goto finish;
+
+fail:
+ errno == EHOSTUNREACH;
+ do_call(c);
+
+finish:
+
+ m->io_free(c->asyncns_io_event);
+ c->asyncns_io_event = NULL;
+}
+
+#endif
+
+static void timeout_cb(struct pa_mainloop_api *m, struct pa_time_event *e, const struct timeval *tv, void *userdata) {
+ struct pa_socket_client *c = userdata;
+ assert(m);
+ assert(e);
+ assert(tv);
+ assert(c);
+
+ if (c->fd >= 0) {
+ close(c->fd);
+ c->fd = -1;
}
+
+ errno = ETIMEDOUT;
+ do_call(c);
+}
+
+static void start_timeout(struct pa_socket_client *c) {
+ struct timeval tv;
+ assert(c);
+ assert(!c->timeout_event);
+
+ pa_gettimeofday(&tv);
+ pa_timeval_add(&tv, CONNECT_TIMEOUT * 1000000);
+ c->timeout_event = c->mainloop->time_new(c->mainloop, &tv, timeout_cb, c);
}
struct pa_socket_client* pa_socket_client_new_string(struct pa_mainloop_api *m, const char*name, uint16_t default_port) {
- const char *p;
struct pa_socket_client *c = NULL;
- enum { KIND_UNIX, KIND_TCP_AUTO, KIND_TCP4, KIND_TCP6 } kind = KIND_TCP_AUTO;
+ struct pa_parsed_address a;
assert(m && name);
- if (*name == '{') {
- char hn[256], *pfx;
- /* The URL starts with a host specification for detecting local connections */
-
- if (!pa_get_host_name(hn, sizeof(hn)))
- return NULL;
-
- pfx = pa_sprintf_malloc("{%s}", hn);
- if (!pa_startswith(name, pfx))
- /* Not local */
- return NULL;
-
- p = name + strlen(pfx);
- } else
- p = name;
+ if (pa_parse_address(name, &a) < 0)
+ return NULL;
+
+ if (!a.port)
+ a.port = default_port;
- if (*p == '/')
- kind = KIND_UNIX;
- else if (pa_startswith(p, "unix:")) {
- kind = KIND_UNIX;
- p += sizeof("unix:")-1;
- } else if (pa_startswith(p, "tcp:") || pa_startswith(p, "tcp4:")) {
- kind = KIND_TCP4;
- p += sizeof("tcp:")-1;
- } else if (pa_startswith(p, "tcp6:")) {
- kind = KIND_TCP6;
- p += sizeof("tcp6:")-1;
- }
+ switch (a.type) {
+ case PA_PARSED_ADDRESS_UNIX:
+ if ((c = pa_socket_client_new_unix(m, a.path_or_host)))
+ start_timeout(c);
+ break;
- switch (kind) {
- case KIND_UNIX:
- return pa_socket_client_new_unix(m, p);
+ case PA_PARSED_ADDRESS_TCP4: /* Fallthrough */
+ case PA_PARSED_ADDRESS_TCP6: /* Fallthrough */
+ case PA_PARSED_ADDRESS_TCP_AUTO:{
- case KIND_TCP_AUTO: /* Fallthrough */
- case KIND_TCP4:
- case KIND_TCP6: {
- uint16_t port = default_port;
- char *h;
- struct addrinfo hints, *res;
+ struct addrinfo hints;
+ char port[12];
- if (!(h = parse_address(p, &port)))
- return NULL;
+ snprintf(port, sizeof(port), "%u", (unsigned) a.port);
memset(&hints, 0, sizeof(hints));
- hints.ai_family = kind == KIND_TCP4 ? AF_INET : (kind == KIND_TCP6 ? AF_INET6 : AF_UNSPEC);
+ 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;
- if (getaddrinfo(h, NULL, &hints, &res) < 0 || !res || !res->ai_addr)
- return NULL;
-
- if (res->ai_family == AF_INET) {
- if (res->ai_addrlen != sizeof(struct sockaddr_in))
- return NULL;
- assert(res->ai_addr->sa_family == res->ai_family);
+#ifdef HAVE_LIBASYNCNS
+ {
+ asyncns_t *asyncns;
- ((struct sockaddr_in*) res->ai_addr)->sin_port = htons(port);
- } else if (res->ai_family == AF_INET6) {
- if (res->ai_addrlen != sizeof(struct sockaddr_in6))
- return NULL;
- assert(res->ai_addr->sa_family == res->ai_family);
+ if (!(asyncns = asyncns_new(1)))
+ goto finish;
+
+ 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);
+ start_timeout(c);
+ }
+#else /* HAVE_LIBASYNCNS */
+ {
+#ifdef HAVE_GETADDRINFO
+ int ret;
+ struct addrinfo *res = NULL;
+
+ ret = getaddrinfo(a.path_or_host, port, &hints, &res);
- ((struct sockaddr_in6*) res->ai_addr)->sin6_port = htons(port);
- } else
- return NULL;
+ if (ret < 0 || !res)
+ goto finish;
- c = pa_socket_client_new_sockaddr(m, res->ai_addr, res->ai_addrlen);
- freeaddrinfo(res);
- return c;
+ if (res->ai_addr) {
+ if ((c = pa_socket_client_new_sockaddr(m, res->ai_addr, res->ai_addrlen)))
+ start_timeout(c);
+ }
+
+ freeaddrinfo(res);
+#else /* HAVE_GETADDRINFO */
+ struct hostent *host = NULL;
+ struct sockaddr_in s;
+
+ /* FIXME: PF_INET6 support */
+ if (hints.ai_family != PF_INET)
+ goto finish;
+
+ 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));
+ s.sin_port = port;
+
+ if ((c = pa_socket_client_new_sockaddr(m, &s, sizeof(s))))
+ start_timeout(c);
+#endif /* HAVE_GETADDRINFO */
+ }
+#endif /* HAVE_LIBASYNCNS */
}
}
- /* Should never be reached */
- assert(0);
- return NULL;
+finish:
+ pa_xfree(a.path_or_host);
+ return c;
}