[systemd-commits] .gitignore Makefile.am src/libsystemd src/systemd
Tom Gundersen
tomegun at kemper.freedesktop.org
Tue Jan 14 09:46:16 PST 2014
.gitignore | 2
Makefile.am | 18
src/libsystemd/resolv-util.h | 31 -
src/libsystemd/resolve-util.h | 31 +
src/libsystemd/sd-resolv.c | 1141 ------------------------------------------
src/libsystemd/sd-resolve.c | 1141 ++++++++++++++++++++++++++++++++++++++++++
src/libsystemd/test-resolv.c | 160 -----
src/libsystemd/test-resolve.c | 160 +++++
src/systemd/sd-resolv.h | 158 -----
src/systemd/sd-resolve.h | 158 +++++
10 files changed, 1500 insertions(+), 1500 deletions(-)
New commits:
commit 3bedba4ae1237d0b7ff4543f9a5c6234d342d98f
Author: Tom Gundersen <teg at jklm.no>
Date: Tue Jan 14 18:18:43 2014 +0100
sd-resolv: rename to sd-resolve
Lennart pointed out that we were misspelling 'resolve'. Let's not repeat the mistakes of 'umount'
and 'resolv.conf'.
diff --git a/.gitignore b/.gitignore
index fb05094..b706ab9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -134,7 +134,7 @@
/test-device-nodes
/test-dhcp-client
/test-dhcp-option
-/test-resolv
+/test-resolve
/test-ellipsize
/test-engine
/test-env-replace
diff --git a/Makefile.am b/Makefile.am
index 9669541..4fc9097 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1982,7 +1982,7 @@ libsystemd_la_SOURCES = \
src/systemd/sd-event.h \
src/systemd/sd-dhcp-client.h \
src/systemd/sd-rtnl.h \
- src/systemd/sd-resolv.h \
+ src/systemd/sd-resolve.h \
src/libsystemd/sd-bus.c \
src/libsystemd/bus-control.c \
src/libsystemd/bus-control.h \
@@ -2033,8 +2033,8 @@ libsystemd_la_SOURCES = \
src/libsystemd/rtnl-message.c \
src/libsystemd/rtnl-util.h \
src/libsystemd/rtnl-util.c \
- src/libsystemd/sd-resolv.c \
- src/libsystemd/resolv-util.h
+ src/libsystemd/sd-resolve.c \
+ src/libsystemd/resolve-util.h
nodist_libsystemd_la_SOURCES = \
src/libsystemd/bus-error-mapping.c
@@ -2112,7 +2112,7 @@ tests += \
test-dhcp-option \
test-dhcp-client \
test-rtnl \
- test-resolv
+ test-resolve
bin_PROGRAMS += \
busctl
@@ -2337,15 +2337,15 @@ test_rtnl_LDADD = \
libsystemd-id128-internal.la \
libsystemd-shared.la
-test_resolv_SOURCES = \
- src/systemd/sd-resolv.h \
- src/libsystemd/test-resolv.c
+test_resolve_SOURCES = \
+ src/systemd/sd-resolve.h \
+ src/libsystemd/test-resolve.c
-test_resolv_LDADD = \
+test_resolve_LDADD = \
libsystemd-internal.la \
libsystemd-shared.la
-test_resolv_CFLAGS = \
+test_resolve_CFLAGS = \
$(AM_CFLAGS) \
-pthread
diff --git a/src/libsystemd/resolv-util.h b/src/libsystemd/resolv-util.h
deleted file mode 100644
index 7b6ec70..0000000
--- a/src/libsystemd/resolv-util.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Daniel Buch
-
- systemd is free software; you can redistribute it and/or modify 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.
-
- systemd 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
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include "util.h"
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(sd_resolv_t*, sd_resolv_free);
-DEFINE_TRIVIAL_CLEANUP_FUNC(unsigned char *, sd_resolv_freeanswer);
-DEFINE_TRIVIAL_CLEANUP_FUNC(struct addrinfo*, sd_resolv_freeaddrinfo);
-#define _cleanup_resolv_free_ _cleanup_(sd_resolv_freep)
-#define _cleanup_resolv_answer_free_ _cleanup_(sd_resolv_freeanswerp)
-#define _cleanup_resolv_addrinfo_free_ _cleanup_(sd_resolv_freeaddrinfop)
diff --git a/src/libsystemd/resolve-util.h b/src/libsystemd/resolve-util.h
new file mode 100644
index 0000000..0761c96
--- /dev/null
+++ b/src/libsystemd/resolve-util.h
@@ -0,0 +1,31 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+ This file is part of systemd.
+
+ Copyright 2014 Daniel Buch
+
+ systemd is free software; you can redistribute it and/or modify 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.
+
+ systemd 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "util.h"
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(sd_resolve_t*, sd_resolve_free);
+DEFINE_TRIVIAL_CLEANUP_FUNC(unsigned char *, sd_resolve_freeanswer);
+DEFINE_TRIVIAL_CLEANUP_FUNC(struct addrinfo*, sd_resolve_freeaddrinfo);
+#define _cleanup_resolve_free_ _cleanup_(sd_resolve_freep)
+#define _cleanup_resolve_answer_free_ _cleanup_(sd_resolve_freeanswerp)
+#define _cleanup_resolve_addrinfo_free_ _cleanup_(sd_resolve_freeaddrinfop)
diff --git a/src/libsystemd/sd-resolv.c b/src/libsystemd/sd-resolv.c
deleted file mode 100644
index b8288ba..0000000
--- a/src/libsystemd/sd-resolv.c
+++ /dev/null
@@ -1,1141 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2005-2008 Lennart Poettering
-
- systemd is free software; you can redistribute it and/or modify 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.
-
- systemd 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
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <assert.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <unistd.h>
-#include <sys/select.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <sys/wait.h>
-#include <sys/types.h>
-#include <pwd.h>
-#include <netinet/in.h>
-#include <arpa/nameser.h>
-#include <resolv.h>
-#include <dirent.h>
-#include <sys/time.h>
-#include <sys/resource.h>
-#include <stdint.h>
-#include <pthread.h>
-#include <sys/prctl.h>
-
-#include "sd-resolv.h"
-#include "util.h"
-
-#define MAX_WORKERS 16
-#define MAX_QUERIES 256
-#define BUFSIZE (10240)
-
-typedef enum {
- REQUEST_ADDRINFO,
- RESPONSE_ADDRINFO,
- REQUEST_NAMEINFO,
- RESPONSE_NAMEINFO,
- REQUEST_RES_QUERY,
- REQUEST_RES_SEARCH,
- RESPONSE_RES,
- REQUEST_TERMINATE,
- RESPONSE_DIED
-} query_type_t;
-
-enum {
- REQUEST_RECV_FD = 0,
- REQUEST_SEND_FD = 1,
- RESPONSE_RECV_FD = 2,
- RESPONSE_SEND_FD = 3,
- MESSAGE_FD_MAX = 4
-};
-
-struct sd_resolv {
- int fds[MESSAGE_FD_MAX];
-
- pthread_t workers[MAX_WORKERS];
- unsigned valid_workers;
-
- unsigned current_id, current_index;
- sd_resolv_query_t* queries[MAX_QUERIES];
-
- sd_resolv_query_t *done_head, *done_tail;
-
- int n_queries;
- int dead;
-};
-
-struct sd_resolv_query {
- sd_resolv_t *resolv;
- int done;
- unsigned id;
- query_type_t type;
- sd_resolv_query_t *done_next, *done_prev;
- int ret;
- int _errno;
- int _h_errno;
- struct addrinfo *addrinfo;
- char *serv, *host;
- void *userdata;
-};
-
-typedef struct rheader {
- query_type_t type;
- unsigned id;
- size_t length;
-} rheader_t;
-
-typedef struct addrinfo_request {
- struct rheader header;
- int hints_is_null;
- int ai_flags;
- int ai_family;
- int ai_socktype;
- int ai_protocol;
- size_t node_len, service_len;
-} addrinfo_request_t;
-
-typedef struct addrinfo_response {
- struct rheader header;
- int ret;
- int _errno;
- int _h_errno;
- /* followed by addrinfo_serialization[] */
-} addrinfo_response_t;
-
-typedef struct addrinfo_serialization {
- int ai_flags;
- int ai_family;
- int ai_socktype;
- int ai_protocol;
- size_t ai_addrlen;
- size_t canonname_len;
- /* Followed by ai_addr amd ai_canonname with variable lengths */
-} addrinfo_serialization_t;
-
-typedef struct nameinfo_request {
- struct rheader header;
- int flags;
- socklen_t sockaddr_len;
- int gethost, getserv;
-} nameinfo_request_t;
-
-typedef struct nameinfo_response {
- struct rheader header;
- size_t hostlen, servlen;
- int ret;
- int _errno;
- int _h_errno;
-} nameinfo_response_t;
-
-typedef struct res_request {
- struct rheader header;
- int class;
- int type;
- size_t dname_len;
-} res_request_t;
-
-typedef struct res_response {
- struct rheader header;
- int ret;
- int _errno;
- int _h_errno;
-} res_response_t;
-
-typedef union packet {
- rheader_t rheader;
- addrinfo_request_t addrinfo_request;
- addrinfo_response_t addrinfo_response;
- nameinfo_request_t nameinfo_request;
- nameinfo_response_t nameinfo_response;
- res_request_t res_request;
- res_response_t res_response;
-} packet_t;
-
-static int send_died(int out_fd) {
- rheader_t rh = {};
- assert(out_fd > 0);
-
- rh.type = RESPONSE_DIED;
- rh.id = 0;
- rh.length = sizeof(rh);
-
- return send(out_fd, &rh, rh.length, MSG_NOSIGNAL);
-}
-
-static void *serialize_addrinfo(void *p, const struct addrinfo *ai, size_t *length, size_t maxlength) {
- addrinfo_serialization_t s;
- size_t cnl, l;
- assert(p);
- assert(ai);
- assert(length);
- assert(*length <= maxlength);
-
- cnl = (ai->ai_canonname ? strlen(ai->ai_canonname)+1 : 0);
- l = sizeof(addrinfo_serialization_t) + ai->ai_addrlen + cnl;
-
- if (*length + l > maxlength)
- return NULL;
-
- s.ai_flags = ai->ai_flags;
- s.ai_family = ai->ai_family;
- s.ai_socktype = ai->ai_socktype;
- s.ai_protocol = ai->ai_protocol;
- s.ai_addrlen = ai->ai_addrlen;
- s.canonname_len = cnl;
-
- memcpy((uint8_t*) p, &s, sizeof(addrinfo_serialization_t));
- memcpy((uint8_t*) p + sizeof(addrinfo_serialization_t), ai->ai_addr, ai->ai_addrlen);
-
- if (ai->ai_canonname)
- strcpy((char*) p + sizeof(addrinfo_serialization_t) + ai->ai_addrlen, ai->ai_canonname);
-
- *length += l;
- return (uint8_t*) p + l;
-}
-
-static int send_addrinfo_reply(int out_fd, unsigned id, int ret, struct addrinfo *ai, int _errno, int _h_errno) {
- addrinfo_response_t data[BUFSIZE/sizeof(addrinfo_response_t) + 1] = {};
- addrinfo_response_t *resp = data;
- assert(out_fd >= 0);
-
- resp->header.type = RESPONSE_ADDRINFO;
- resp->header.id = id;
- resp->header.length = sizeof(addrinfo_response_t);
- resp->ret = ret;
- resp->_errno = _errno;
- resp->_h_errno = _h_errno;
-
- if (ret == 0 && ai) {
- void *p = data + 1;
- struct addrinfo *k;
-
- for (k = ai; k; k = k->ai_next) {
- p = serialize_addrinfo(p, k, &resp->header.length, (char*) data + BUFSIZE - (char*) p);
- if (!p) {
- resp->ret = EAI_MEMORY;
- break;
- }
- }
- }
-
- if (ai)
- freeaddrinfo(ai);
-
- return send(out_fd, resp, resp->header.length, MSG_NOSIGNAL);
-}
-
-static int send_nameinfo_reply(int out_fd, unsigned id, int ret, const char *host, const char *serv, int _errno, int _h_errno) {
- nameinfo_response_t data[BUFSIZE/sizeof(nameinfo_response_t) + 1] = {};
- size_t hl, sl;
- nameinfo_response_t *resp = data;
-
- assert(out_fd >= 0);
-
- sl = serv ? strlen(serv)+1 : 0;
- hl = host ? strlen(host)+1 : 0;
-
- resp->header.type = RESPONSE_NAMEINFO;
- resp->header.id = id;
- resp->header.length = sizeof(nameinfo_response_t) + hl + sl;
- resp->ret = ret;
- resp->_errno = _errno;
- resp->_h_errno = _h_errno;
- resp->hostlen = hl;
- resp->servlen = sl;
-
- assert(sizeof(data) >= resp->header.length);
-
- if (host)
- memcpy((uint8_t *)data + sizeof(nameinfo_response_t), host, hl);
-
- if (serv)
- memcpy((uint8_t *)data + sizeof(nameinfo_response_t) + hl, serv, sl);
-
- return send(out_fd, resp, resp->header.length, MSG_NOSIGNAL);
-}
-
-static int send_res_reply(int out_fd, unsigned id, const unsigned char *answer, int ret, int _errno, int _h_errno) {
- res_response_t data[BUFSIZE/sizeof(res_response_t) + 1] = {};
- res_response_t *resp = data;
-
- assert(out_fd >= 0);
-
- resp->header.type = RESPONSE_RES;
- resp->header.id = id;
- resp->header.length = sizeof(res_response_t) + (ret < 0 ? 0 : ret);
- resp->ret = ret;
- resp->_errno = _errno;
- resp->_h_errno = _h_errno;
-
- assert(sizeof(data) >= resp->header.length);
-
- if (ret > 0)
- memcpy((uint8_t *)data + sizeof(res_response_t), answer, ret);
-
- return send(out_fd, resp, resp->header.length, MSG_NOSIGNAL);
-}
-
-static int handle_request(int out_fd, const packet_t *packet, size_t length) {
- const rheader_t *req;
- assert(out_fd >= 0);
-
- req = &packet->rheader;
- assert(req);
- assert(length >= sizeof(rheader_t));
- assert(length == req->length);
-
- switch (req->type) {
- case REQUEST_ADDRINFO: {
- struct addrinfo ai = {}, *result = NULL;
- const addrinfo_request_t *ai_req = &packet->addrinfo_request;
- const char *node, *service;
- int ret;
-
- assert(length >= sizeof(addrinfo_request_t));
- assert(length == sizeof(addrinfo_request_t) + ai_req->node_len + ai_req->service_len);
-
- ai.ai_flags = ai_req->ai_flags;
- ai.ai_family = ai_req->ai_family;
- ai.ai_socktype = ai_req->ai_socktype;
- ai.ai_protocol = ai_req->ai_protocol;
-
- node = ai_req->node_len ? (const char*) ai_req + sizeof(addrinfo_request_t) : NULL;
- service = ai_req->service_len ? (const char*) ai_req + sizeof(addrinfo_request_t) + ai_req->node_len : NULL;
-
- ret = getaddrinfo(node, service,
- ai_req->hints_is_null ? NULL : &ai,
- &result);
-
- /* send_addrinfo_reply() frees result */
- return send_addrinfo_reply(out_fd, req->id, ret, result, errno, h_errno);
- }
-
- case REQUEST_NAMEINFO: {
- int ret;
- const nameinfo_request_t *ni_req = &packet->nameinfo_request;
- char hostbuf[NI_MAXHOST], servbuf[NI_MAXSERV];
- struct sockaddr_storage sa;
-
- assert(length >= sizeof(nameinfo_request_t));
- assert(length == sizeof(nameinfo_request_t) + ni_req->sockaddr_len);
-
- memcpy(&sa, (const uint8_t *) ni_req + sizeof(nameinfo_request_t), ni_req->sockaddr_len);
-
- ret = getnameinfo((struct sockaddr *)&sa, ni_req->sockaddr_len,
- ni_req->gethost ? hostbuf : NULL, ni_req->gethost ? sizeof(hostbuf) : 0,
- ni_req->getserv ? servbuf : NULL, ni_req->getserv ? sizeof(servbuf) : 0,
- ni_req->flags);
-
- return send_nameinfo_reply(out_fd, req->id, ret,
- ret == 0 && ni_req->gethost ? hostbuf : NULL,
- ret == 0 && ni_req->getserv ? servbuf : NULL,
- errno, h_errno);
- }
-
- case REQUEST_RES_QUERY:
- case REQUEST_RES_SEARCH: {
- int ret;
- HEADER answer[BUFSIZE/sizeof(HEADER) + 1];
- const res_request_t *res_req = &packet->res_request;
- const char *dname;
-
- assert(length >= sizeof(res_request_t));
- assert(length == sizeof(res_request_t) + res_req->dname_len);
-
- dname = (const char *) req + sizeof(res_request_t);
-
- if (req->type == REQUEST_RES_QUERY)
- ret = res_query(dname, res_req->class, res_req->type, (unsigned char *) answer, BUFSIZE);
- else
- ret = res_search(dname, res_req->class, res_req->type, (unsigned char *) answer, BUFSIZE);
-
- return send_res_reply(out_fd, req->id, (unsigned char *) answer, ret, errno, h_errno);
- }
-
- case REQUEST_TERMINATE:
- /* Quit */
- return -1;
-
- default:
- ;
- }
-
- return 0;
-}
-
-static void* thread_worker(void *p) {
- sd_resolv_t *resolv = p;
- sigset_t fullset;
-
- /* No signals in this thread please */
- sigfillset(&fullset);
- pthread_sigmask(SIG_BLOCK, &fullset, NULL);
-
- while (!resolv->dead) {
- packet_t buf[BUFSIZE/sizeof(packet_t) + 1];
- ssize_t length;
-
- length = recv(resolv->fds[REQUEST_RECV_FD], buf, sizeof(buf), 0);
-
- if (length <= 0) {
- if (length < 0 && (errno == EAGAIN || errno == EINTR))
- continue;
- break;
- }
-
- if (resolv->dead)
- break;
-
- if (handle_request(resolv->fds[RESPONSE_SEND_FD], buf, (size_t) length) < 0)
- break;
- }
-
- send_died(resolv->fds[RESPONSE_SEND_FD]);
-
- return NULL;
-}
-
-sd_resolv_t* sd_resolv_new(unsigned n_proc) {
- sd_resolv_t *resolv = NULL;
- int i, r;
-
- assert(n_proc >= 1);
-
- if (n_proc > MAX_WORKERS)
- n_proc = MAX_WORKERS;
-
- resolv = malloc(sizeof(sd_resolv_t));
- if (!resolv) {
- errno = ENOMEM;
- goto fail;
- }
-
- resolv->dead = 0;
- resolv->valid_workers = 0;
-
- for (i = 0; i < MESSAGE_FD_MAX; i++)
- resolv->fds[i] = -1;
-
- memset(resolv->queries, 0, sizeof(resolv->queries));
-
- r = socketpair(PF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, resolv->fds);
- if (r < 0)
- goto fail;
-
- r = socketpair(PF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, resolv->fds+2);
- if (r < 0)
- goto fail;
-
- for (resolv->valid_workers = 0; resolv->valid_workers < n_proc; resolv->valid_workers++) {
- r = pthread_create(&resolv->workers[resolv->valid_workers], NULL, thread_worker, resolv);
- if (r) {
- errno = r;
- goto fail;
- }
- }
-
- resolv->current_index = resolv->current_id = 0;
- resolv->done_head = resolv->done_tail = NULL;
- resolv->n_queries = 0;
-
- fd_nonblock(resolv->fds[RESPONSE_RECV_FD], true);
-
- return resolv;
-
-fail:
- if (resolv)
- sd_resolv_free(resolv);
-
- return NULL;
-}
-
-void sd_resolv_free(sd_resolv_t *resolv) {
- int i;
- int saved_errno = errno;
- unsigned p;
-
- assert(resolv);
-
- resolv->dead = 1;
-
- if (resolv->fds[REQUEST_SEND_FD] >= 0) {
- rheader_t req = {};
-
- req.type = REQUEST_TERMINATE;
- req.length = sizeof(req);
- req.id = 0;
-
- /* Send one termination packet for each worker */
- for (p = 0; p < resolv->valid_workers; p++)
- send(resolv->fds[REQUEST_SEND_FD], &req, req.length, MSG_NOSIGNAL);
- }
-
- /* Now terminate them and wait until they are gone. */
- for (p = 0; p < resolv->valid_workers; p++) {
- for (;;) {
- if (pthread_join(resolv->workers[p], NULL) != EINTR)
- break;
- }
- }
-
- /* Close all communication channels */
- for (i = 0; i < MESSAGE_FD_MAX; i++)
- if (resolv->fds[i] >= 0)
- close(resolv->fds[i]);
-
- for (p = 0; p < MAX_QUERIES; p++)
- if (resolv->queries[p])
- sd_resolv_cancel(resolv, resolv->queries[p]);
-
- free(resolv);
-
- errno = saved_errno;
-}
-
-int sd_resolv_fd(sd_resolv_t *resolv) {
- assert(resolv);
-
- return resolv->fds[RESPONSE_RECV_FD];
-}
-
-static sd_resolv_query_t *lookup_query(sd_resolv_t *resolv, unsigned id) {
- sd_resolv_query_t *q;
- assert(resolv);
-
- q = resolv->queries[id % MAX_QUERIES];
- if (q)
- if (q->id == id)
- return q;
-
- return NULL;
-}
-
-static void complete_query(sd_resolv_t *resolv, sd_resolv_query_t *q) {
- assert(resolv);
- assert(q);
- assert(!q->done);
-
- q->done = 1;
-
- if ((q->done_prev = resolv->done_tail))
- resolv->done_tail->done_next = q;
- else
- resolv->done_head = q;
-
- resolv->done_tail = q;
- q->done_next = NULL;
-}
-
-static const void *unserialize_addrinfo(const void *p, struct addrinfo **ret_ai, size_t *length) {
- addrinfo_serialization_t s;
- size_t l;
- struct addrinfo *ai;
- assert(p);
- assert(ret_ai);
- assert(length);
-
- if (*length < sizeof(addrinfo_serialization_t))
- return NULL;
-
- memcpy(&s, p, sizeof(s));
-
- l = sizeof(addrinfo_serialization_t) + s.ai_addrlen + s.canonname_len;
- if (*length < l)
- return NULL;
-
- ai = malloc(sizeof(struct addrinfo));
- if (!ai)
- goto fail;
-
- ai->ai_addr = NULL;
- ai->ai_canonname = NULL;
- ai->ai_next = NULL;
-
- if (s.ai_addrlen && !(ai->ai_addr = malloc(s.ai_addrlen)))
- goto fail;
-
- if (s.canonname_len && !(ai->ai_canonname = malloc(s.canonname_len)))
- goto fail;
-
- ai->ai_flags = s.ai_flags;
- ai->ai_family = s.ai_family;
- ai->ai_socktype = s.ai_socktype;
- ai->ai_protocol = s.ai_protocol;
- ai->ai_addrlen = s.ai_addrlen;
-
- if (ai->ai_addr)
- memcpy(ai->ai_addr, (const uint8_t*) p + sizeof(addrinfo_serialization_t), s.ai_addrlen);
-
- if (ai->ai_canonname)
- memcpy(ai->ai_canonname, (const uint8_t*) p + sizeof(addrinfo_serialization_t) + s.ai_addrlen, s.canonname_len);
-
- *length -= l;
- *ret_ai = ai;
-
- return (const uint8_t*) p + l;
-
-
-fail:
- if (ai)
- sd_resolv_freeaddrinfo(ai);
-
- return NULL;
-}
-
-static int handle_response(sd_resolv_t *resolv, const packet_t *packet, size_t length) {
- const rheader_t *resp;
- sd_resolv_query_t *q;
-
- assert(resolv);
-
- resp = &packet->rheader;
- assert(resp);
- assert(length >= sizeof(rheader_t));
- assert(length == resp->length);
-
- if (resp->type == RESPONSE_DIED) {
- resolv->dead = 1;
- return 0;
- }
-
- q = lookup_query(resolv, resp->id);
- if (!q)
- return 0;
-
- switch (resp->type) {
- case RESPONSE_ADDRINFO: {
- const addrinfo_response_t *ai_resp = &packet->addrinfo_response;
- const void *p;
- size_t l;
- struct addrinfo *prev = NULL;
-
- assert(length >= sizeof(addrinfo_response_t));
- assert(q->type == REQUEST_ADDRINFO);
-
- q->ret = ai_resp->ret;
- q->_errno = ai_resp->_errno;
- q->_h_errno = ai_resp->_h_errno;
- l = length - sizeof(addrinfo_response_t);
- p = (const uint8_t*) resp + sizeof(addrinfo_response_t);
-
- while (l > 0 && p) {
- struct addrinfo *ai = NULL;
- p = unserialize_addrinfo(p, &ai, &l);
-
- if (!p || !ai) {
- q->ret = EAI_MEMORY;
- break;
- }
-
- if (prev)
- prev->ai_next = ai;
- else
- q->addrinfo = ai;
-
- prev = ai;
- }
-
- complete_query(resolv, q);
- break;
- }
-
- case RESPONSE_NAMEINFO: {
- const nameinfo_response_t *ni_resp = &packet->nameinfo_response;
-
- assert(length >= sizeof(nameinfo_response_t));
- assert(q->type == REQUEST_NAMEINFO);
-
- q->ret = ni_resp->ret;
- q->_errno = ni_resp->_errno;
- q->_h_errno = ni_resp->_h_errno;
-
- if (ni_resp->hostlen)
- if (!(q->host = strndup((const char*) ni_resp + sizeof(nameinfo_response_t), ni_resp->hostlen-1)))
- q->ret = EAI_MEMORY;
-
- if (ni_resp->servlen)
- if (!(q->serv = strndup((const char*) ni_resp + sizeof(nameinfo_response_t) + ni_resp->hostlen, ni_resp->servlen-1)))
- q->ret = EAI_MEMORY;
-
- complete_query(resolv, q);
- break;
- }
-
- case RESPONSE_RES: {
- const res_response_t *res_resp = &packet->res_response;
-
- assert(length >= sizeof(res_response_t));
- assert(q->type == REQUEST_RES_QUERY || q->type == REQUEST_RES_SEARCH);
-
- q->ret = res_resp->ret;
- q->_errno = res_resp->_errno;
- q->_h_errno = res_resp->_h_errno;
-
- if (res_resp->ret >= 0) {
- if (!(q->serv = malloc(res_resp->ret))) {
- q->ret = -1;
- q->_errno = ENOMEM;
- } else
- memcpy(q->serv, (const char *)resp + sizeof(res_response_t), res_resp->ret);
- }
-
- complete_query(resolv, q);
- break;
- }
-
- default:
- ;
- }
-
- return 0;
-}
-
-int sd_resolv_wait(sd_resolv_t *resolv, int block) {
- int handled = 0;
- assert(resolv);
-
- for (;;) {
- packet_t buf[BUFSIZE/sizeof(packet_t) + 1];
- ssize_t l;
-
- if (resolv->dead) {
- errno = ECHILD;
- return -1;
- }
-
- l = recv(resolv->fds[RESPONSE_RECV_FD], buf, sizeof(buf), 0);
- if (l < 0) {
- fd_set fds;
-
- if (errno != EAGAIN)
- return -1;
-
- if (!block || handled)
- return 0;
-
- FD_ZERO(&fds);
- FD_SET(resolv->fds[RESPONSE_RECV_FD], &fds);
-
- if (select(resolv->fds[RESPONSE_RECV_FD]+1, &fds, NULL, NULL, NULL) < 0)
- return -1;
-
- continue;
- }
-
- if (handle_response(resolv, buf, (size_t) l) < 0)
- return -1;
-
- handled = 1;
- }
-}
-
-static sd_resolv_query_t *alloc_query(sd_resolv_t *resolv) {
- sd_resolv_query_t *q;
- assert(resolv);
-
- if (resolv->n_queries >= MAX_QUERIES) {
- errno = ENOMEM;
- return NULL;
- }
-
- while (resolv->queries[resolv->current_index]) {
- resolv->current_index++;
- resolv->current_id++;
-
- while (resolv->current_index >= MAX_QUERIES)
- resolv->current_index -= MAX_QUERIES;
- }
-
- q = resolv->queries[resolv->current_index] = malloc(sizeof(sd_resolv_query_t));
- if (!q) {
- errno = ENOMEM;
- return NULL;
- }
-
- resolv->n_queries++;
-
- q->resolv = resolv;
- q->done = 0;
- q->id = resolv->current_id;
- q->done_next = q->done_prev = NULL;
- q->ret = 0;
- q->_errno = 0;
- q->_h_errno = 0;
- q->addrinfo = NULL;
- q->userdata = NULL;
- q->host = q->serv = NULL;
-
- return q;
-}
-
-sd_resolv_query_t* sd_resolv_getaddrinfo(sd_resolv_t *resolv, const char *node, const char *service, const struct addrinfo *hints) {
- addrinfo_request_t data[BUFSIZE/sizeof(addrinfo_request_t) + 1] = {};
- addrinfo_request_t *req = data;
- sd_resolv_query_t *q;
- assert(resolv);
- assert(node || service);
-
- if (resolv->dead) {
- errno = ECHILD;
- return NULL;
- }
-
- q = alloc_query(resolv);
- if (!q)
- return NULL;
-
- req->node_len = node ? strlen(node)+1 : 0;
- req->service_len = service ? strlen(service)+1 : 0;
-
- req->header.id = q->id;
- req->header.type = q->type = REQUEST_ADDRINFO;
- req->header.length = sizeof(addrinfo_request_t) + req->node_len + req->service_len;
-
- if (req->header.length > BUFSIZE) {
- errno = ENOMEM;
- goto fail;
- }
-
- if (!(req->hints_is_null = !hints)) {
- req->ai_flags = hints->ai_flags;
- req->ai_family = hints->ai_family;
- req->ai_socktype = hints->ai_socktype;
- req->ai_protocol = hints->ai_protocol;
- }
-
- if (node)
- strcpy((char*) req + sizeof(addrinfo_request_t), node);
-
- if (service)
- strcpy((char*) req + sizeof(addrinfo_request_t) + req->node_len, service);
-
- if (send(resolv->fds[REQUEST_SEND_FD], req, req->header.length, MSG_NOSIGNAL) < 0)
- goto fail;
-
- return q;
-
-fail:
- if (q)
- sd_resolv_cancel(resolv, q);
-
- return NULL;
-}
-
-int sd_resolv_getaddrinfo_done(sd_resolv_t *resolv, sd_resolv_query_t* q, struct addrinfo **ret_res) {
- int ret;
- assert(resolv);
- assert(q);
- assert(q->resolv == resolv);
- assert(q->type == REQUEST_ADDRINFO);
-
- if (resolv->dead) {
- errno = ECHILD;
- return EAI_SYSTEM;
- }
-
- if (!q->done)
- return EAI_AGAIN;
-
- *ret_res = q->addrinfo;
- q->addrinfo = NULL;
-
- ret = q->ret;
-
- if (ret == EAI_SYSTEM)
- errno = q->_errno;
-
- if (ret != 0)
- h_errno = q->_h_errno;
-
- sd_resolv_cancel(resolv, q);
-
- return ret;
-}
-
-sd_resolv_query_t* sd_resolv_getnameinfo(sd_resolv_t *resolv, const struct sockaddr *sa, socklen_t salen, int flags, int gethost, int getserv) {
- nameinfo_request_t data[BUFSIZE/sizeof(nameinfo_request_t) + 1] = {};
- nameinfo_request_t *req = data;
- sd_resolv_query_t *q;
-
- assert(resolv);
- assert(sa);
- assert(salen > 0);
-
- if (resolv->dead) {
- errno = ECHILD;
- return NULL;
- }
-
- q = alloc_query(resolv);
- if (!q)
- return NULL;
-
- req->header.id = q->id;
- req->header.type = q->type = REQUEST_NAMEINFO;
- req->header.length = sizeof(nameinfo_request_t) + salen;
-
- if (req->header.length > BUFSIZE) {
- errno = ENOMEM;
- goto fail;
- }
-
- req->flags = flags;
- req->sockaddr_len = salen;
- req->gethost = gethost;
- req->getserv = getserv;
-
- memcpy((uint8_t*) req + sizeof(nameinfo_request_t), sa, salen);
-
- if (send(resolv->fds[REQUEST_SEND_FD], req, req->header.length, MSG_NOSIGNAL) < 0)
- goto fail;
-
- return q;
-
-fail:
- if (q)
- sd_resolv_cancel(resolv, q);
-
- return NULL;
-}
-
-int sd_resolv_getnameinfo_done(sd_resolv_t *resolv, sd_resolv_query_t* q, char *ret_host, size_t hostlen, char *ret_serv, size_t servlen) {
- int ret;
- assert(resolv);
- assert(q);
- assert(q->resolv == resolv);
- assert(q->type == REQUEST_NAMEINFO);
- assert(!ret_host || hostlen);
- assert(!ret_serv || servlen);
-
- if (resolv->dead) {
- errno = ECHILD;
- return EAI_SYSTEM;
- }
-
- if (!q->done)
- return EAI_AGAIN;
-
- if (ret_host && q->host) {
- strncpy(ret_host, q->host, hostlen);
- ret_host[hostlen-1] = 0;
- }
-
- if (ret_serv && q->serv) {
- strncpy(ret_serv, q->serv, servlen);
- ret_serv[servlen-1] = 0;
- }
-
- ret = q->ret;
-
- if (ret == EAI_SYSTEM)
- errno = q->_errno;
-
- if (ret != 0)
- h_errno = q->_h_errno;
-
- sd_resolv_cancel(resolv, q);
-
- return ret;
-}
-
-static sd_resolv_query_t * resolv_res(sd_resolv_t *resolv, query_type_t qtype, const char *dname, int class, int type) {
- res_request_t data[BUFSIZE/sizeof(res_request_t) + 1];
- res_request_t *req = data;
- sd_resolv_query_t *q;
-
- assert(resolv);
- assert(dname);
-
- if (resolv->dead) {
- errno = ECHILD;
- return NULL;
- }
-
- q = alloc_query(resolv);
- if (!q)
- return NULL;
-
- req->dname_len = strlen(dname) + 1;
-
- req->header.id = q->id;
- req->header.type = q->type = qtype;
- req->header.length = sizeof(res_request_t) + req->dname_len;
-
- if (req->header.length > BUFSIZE) {
- errno = ENOMEM;
- goto fail;
- }
-
- req->class = class;
- req->type = type;
-
- strcpy((char*) req + sizeof(res_request_t), dname);
-
- if (send(resolv->fds[REQUEST_SEND_FD], req, req->header.length, MSG_NOSIGNAL) < 0)
- goto fail;
-
- return q;
-
-fail:
- if (q)
- sd_resolv_cancel(resolv, q);
-
- return NULL;
-}
-
-sd_resolv_query_t* sd_resolv_res_query(sd_resolv_t *resolv, const char *dname, int class, int type) {
- return resolv_res(resolv, REQUEST_RES_QUERY, dname, class, type);
-}
-
-sd_resolv_query_t* sd_resolv_res_search(sd_resolv_t *resolv, const char *dname, int class, int type) {
- return resolv_res(resolv, REQUEST_RES_SEARCH, dname, class, type);
-}
-
-int sd_resolv_res_done(sd_resolv_t *resolv, sd_resolv_query_t* q, unsigned char **answer) {
- int ret;
- assert(resolv);
- assert(q);
- assert(q->resolv == resolv);
- assert(q->type == REQUEST_RES_QUERY || q->type == REQUEST_RES_SEARCH);
- assert(answer);
-
- if (resolv->dead) {
- errno = ECHILD;
- return -ECHILD;
- }
-
- if (!q->done) {
- errno = EAGAIN;
- return -EAGAIN;
- }
-
- *answer = (unsigned char *)q->serv;
- q->serv = NULL;
-
- ret = q->ret;
-
- if (ret < 0) {
- errno = q->_errno;
- h_errno = q->_h_errno;
- }
-
- sd_resolv_cancel(resolv, q);
-
- return ret < 0 ? -errno : ret;
-}
-
-sd_resolv_query_t* sd_resolv_getnext(sd_resolv_t *resolv) {
- assert(resolv);
- return resolv->done_head;
-}
-
-int sd_resolv_getnqueries(sd_resolv_t *resolv) {
- assert(resolv);
- return resolv->n_queries;
-}
-
-void sd_resolv_cancel(sd_resolv_t *resolv, sd_resolv_query_t* q) {
- int i;
- int saved_errno = errno;
-
- assert(resolv);
- assert(q);
- assert(q->resolv == resolv);
- assert(resolv->n_queries > 0);
-
- if (q->done) {
-
- if (q->done_prev)
- q->done_prev->done_next = q->done_next;
- else
- resolv->done_head = q->done_next;
-
- if (q->done_next)
- q->done_next->done_prev = q->done_prev;
- else
- resolv->done_tail = q->done_prev;
- }
-
- i = q->id % MAX_QUERIES;
- assert(resolv->queries[i] == q);
- resolv->queries[i] = NULL;
-
- sd_resolv_freeaddrinfo(q->addrinfo);
- free(q->host);
- free(q->serv);
-
- resolv->n_queries--;
- free(q);
-
- errno = saved_errno;
-}
-
-void sd_resolv_freeaddrinfo(struct addrinfo *ai) {
- int saved_errno = errno;
-
- while (ai) {
- struct addrinfo *next = ai->ai_next;
-
- free(ai->ai_addr);
- free(ai->ai_canonname);
- free(ai);
-
- ai = next;
- }
-
- errno = saved_errno;
-}
-
-void sd_resolv_freeanswer(unsigned char *answer) {
- int saved_errno = errno;
-
- if (!answer)
- return;
-
- free(answer);
-
- errno = saved_errno;
-}
-
-int sd_resolv_isdone(sd_resolv_t *resolv, sd_resolv_query_t*q) {
- assert(resolv);
- assert(q);
- assert(q->resolv == resolv);
-
- return q->done;
-}
-
-void sd_resolv_setuserdata(sd_resolv_t *resolv, sd_resolv_query_t *q, void *userdata) {
- assert(q);
- assert(resolv);
- assert(q->resolv = resolv);
-
- q->userdata = userdata;
-}
-
-void* sd_resolv_getuserdata(sd_resolv_t *resolv, sd_resolv_query_t *q) {
- assert(q);
- assert(resolv);
- assert(q->resolv = resolv);
-
- return q->userdata;
-}
diff --git a/src/libsystemd/sd-resolve.c b/src/libsystemd/sd-resolve.c
new file mode 100644
index 0000000..269f517
--- /dev/null
+++ b/src/libsystemd/sd-resolve.c
@@ -0,0 +1,1141 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2005-2008 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify 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.
+
+ systemd 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <assert.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <unistd.h>
+#include <sys/select.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/wait.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <netinet/in.h>
+#include <arpa/nameser.h>
+#include <resolv.h>
+#include <dirent.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <stdint.h>
+#include <pthread.h>
+#include <sys/prctl.h>
+
+#include "sd-resolve.h"
+#include "util.h"
+
+#define MAX_WORKERS 16
+#define MAX_QUERIES 256
+#define BUFSIZE (10240)
+
+typedef enum {
+ REQUEST_ADDRINFO,
+ RESPONSE_ADDRINFO,
+ REQUEST_NAMEINFO,
+ RESPONSE_NAMEINFO,
+ REQUEST_RES_QUERY,
+ REQUEST_RES_SEARCH,
+ RESPONSE_RES,
+ REQUEST_TERMINATE,
+ RESPONSE_DIED
+} query_type_t;
+
+enum {
+ REQUEST_RECV_FD = 0,
+ REQUEST_SEND_FD = 1,
+ RESPONSE_RECV_FD = 2,
+ RESPONSE_SEND_FD = 3,
+ MESSAGE_FD_MAX = 4
+};
+
+struct sd_resolve {
+ int fds[MESSAGE_FD_MAX];
+
+ pthread_t workers[MAX_WORKERS];
+ unsigned valid_workers;
+
+ unsigned current_id, current_index;
+ sd_resolve_query_t* queries[MAX_QUERIES];
+
+ sd_resolve_query_t *done_head, *done_tail;
+
+ int n_queries;
+ int dead;
+};
+
+struct sd_resolve_query {
+ sd_resolve_t *resolve;
+ int done;
+ unsigned id;
+ query_type_t type;
+ sd_resolve_query_t *done_next, *done_prev;
+ int ret;
+ int _errno;
+ int _h_errno;
+ struct addrinfo *addrinfo;
+ char *serv, *host;
+ void *userdata;
+};
+
+typedef struct rheader {
+ query_type_t type;
+ unsigned id;
+ size_t length;
+} rheader_t;
+
+typedef struct addrinfo_request {
+ struct rheader header;
+ int hints_is_null;
+ int ai_flags;
+ int ai_family;
+ int ai_socktype;
+ int ai_protocol;
+ size_t node_len, service_len;
+} addrinfo_request_t;
+
+typedef struct addrinfo_response {
+ struct rheader header;
+ int ret;
+ int _errno;
+ int _h_errno;
+ /* followed by addrinfo_serialization[] */
+} addrinfo_response_t;
+
+typedef struct addrinfo_serialization {
+ int ai_flags;
+ int ai_family;
+ int ai_socktype;
+ int ai_protocol;
+ size_t ai_addrlen;
+ size_t canonname_len;
+ /* Followed by ai_addr amd ai_canonname with variable lengths */
+} addrinfo_serialization_t;
+
+typedef struct nameinfo_request {
+ struct rheader header;
+ int flags;
+ socklen_t sockaddr_len;
+ int gethost, getserv;
+} nameinfo_request_t;
+
+typedef struct nameinfo_response {
+ struct rheader header;
+ size_t hostlen, servlen;
+ int ret;
+ int _errno;
+ int _h_errno;
+} nameinfo_response_t;
+
+typedef struct res_request {
+ struct rheader header;
+ int class;
+ int type;
+ size_t dname_len;
+} res_request_t;
+
+typedef struct res_response {
+ struct rheader header;
+ int ret;
+ int _errno;
+ int _h_errno;
+} res_response_t;
+
+typedef union packet {
+ rheader_t rheader;
+ addrinfo_request_t addrinfo_request;
+ addrinfo_response_t addrinfo_response;
+ nameinfo_request_t nameinfo_request;
+ nameinfo_response_t nameinfo_response;
+ res_request_t res_request;
+ res_response_t res_response;
+} packet_t;
+
+static int send_died(int out_fd) {
+ rheader_t rh = {};
+ assert(out_fd > 0);
+
+ rh.type = RESPONSE_DIED;
+ rh.id = 0;
+ rh.length = sizeof(rh);
+
+ return send(out_fd, &rh, rh.length, MSG_NOSIGNAL);
+}
+
+static void *serialize_addrinfo(void *p, const struct addrinfo *ai, size_t *length, size_t maxlength) {
+ addrinfo_serialization_t s;
+ size_t cnl, l;
+ assert(p);
+ assert(ai);
+ assert(length);
+ assert(*length <= maxlength);
+
+ cnl = (ai->ai_canonname ? strlen(ai->ai_canonname)+1 : 0);
+ l = sizeof(addrinfo_serialization_t) + ai->ai_addrlen + cnl;
+
+ if (*length + l > maxlength)
+ return NULL;
+
+ s.ai_flags = ai->ai_flags;
+ s.ai_family = ai->ai_family;
+ s.ai_socktype = ai->ai_socktype;
+ s.ai_protocol = ai->ai_protocol;
+ s.ai_addrlen = ai->ai_addrlen;
+ s.canonname_len = cnl;
+
+ memcpy((uint8_t*) p, &s, sizeof(addrinfo_serialization_t));
+ memcpy((uint8_t*) p + sizeof(addrinfo_serialization_t), ai->ai_addr, ai->ai_addrlen);
+
+ if (ai->ai_canonname)
+ strcpy((char*) p + sizeof(addrinfo_serialization_t) + ai->ai_addrlen, ai->ai_canonname);
+
+ *length += l;
+ return (uint8_t*) p + l;
+}
+
+static int send_addrinfo_reply(int out_fd, unsigned id, int ret, struct addrinfo *ai, int _errno, int _h_errno) {
+ addrinfo_response_t data[BUFSIZE/sizeof(addrinfo_response_t) + 1] = {};
+ addrinfo_response_t *resp = data;
+ assert(out_fd >= 0);
+
+ resp->header.type = RESPONSE_ADDRINFO;
+ resp->header.id = id;
+ resp->header.length = sizeof(addrinfo_response_t);
+ resp->ret = ret;
+ resp->_errno = _errno;
+ resp->_h_errno = _h_errno;
+
+ if (ret == 0 && ai) {
+ void *p = data + 1;
+ struct addrinfo *k;
+
+ for (k = ai; k; k = k->ai_next) {
+ p = serialize_addrinfo(p, k, &resp->header.length, (char*) data + BUFSIZE - (char*) p);
+ if (!p) {
+ resp->ret = EAI_MEMORY;
+ break;
+ }
+ }
+ }
+
+ if (ai)
+ freeaddrinfo(ai);
+
+ return send(out_fd, resp, resp->header.length, MSG_NOSIGNAL);
+}
+
+static int send_nameinfo_reply(int out_fd, unsigned id, int ret, const char *host, const char *serv, int _errno, int _h_errno) {
+ nameinfo_response_t data[BUFSIZE/sizeof(nameinfo_response_t) + 1] = {};
+ size_t hl, sl;
+ nameinfo_response_t *resp = data;
+
+ assert(out_fd >= 0);
+
+ sl = serv ? strlen(serv)+1 : 0;
+ hl = host ? strlen(host)+1 : 0;
+
+ resp->header.type = RESPONSE_NAMEINFO;
+ resp->header.id = id;
+ resp->header.length = sizeof(nameinfo_response_t) + hl + sl;
+ resp->ret = ret;
+ resp->_errno = _errno;
+ resp->_h_errno = _h_errno;
+ resp->hostlen = hl;
+ resp->servlen = sl;
+
+ assert(sizeof(data) >= resp->header.length);
+
+ if (host)
+ memcpy((uint8_t *)data + sizeof(nameinfo_response_t), host, hl);
+
+ if (serv)
+ memcpy((uint8_t *)data + sizeof(nameinfo_response_t) + hl, serv, sl);
+
+ return send(out_fd, resp, resp->header.length, MSG_NOSIGNAL);
+}
+
+static int send_res_reply(int out_fd, unsigned id, const unsigned char *answer, int ret, int _errno, int _h_errno) {
+ res_response_t data[BUFSIZE/sizeof(res_response_t) + 1] = {};
+ res_response_t *resp = data;
+
+ assert(out_fd >= 0);
+
+ resp->header.type = RESPONSE_RES;
+ resp->header.id = id;
+ resp->header.length = sizeof(res_response_t) + (ret < 0 ? 0 : ret);
+ resp->ret = ret;
+ resp->_errno = _errno;
+ resp->_h_errno = _h_errno;
+
+ assert(sizeof(data) >= resp->header.length);
+
+ if (ret > 0)
+ memcpy((uint8_t *)data + sizeof(res_response_t), answer, ret);
+
+ return send(out_fd, resp, resp->header.length, MSG_NOSIGNAL);
+}
+
+static int handle_request(int out_fd, const packet_t *packet, size_t length) {
+ const rheader_t *req;
+ assert(out_fd >= 0);
+
+ req = &packet->rheader;
+ assert(req);
+ assert(length >= sizeof(rheader_t));
+ assert(length == req->length);
+
+ switch (req->type) {
+ case REQUEST_ADDRINFO: {
+ struct addrinfo ai = {}, *result = NULL;
+ const addrinfo_request_t *ai_req = &packet->addrinfo_request;
+ const char *node, *service;
+ int ret;
+
+ assert(length >= sizeof(addrinfo_request_t));
+ assert(length == sizeof(addrinfo_request_t) + ai_req->node_len + ai_req->service_len);
+
+ ai.ai_flags = ai_req->ai_flags;
+ ai.ai_family = ai_req->ai_family;
+ ai.ai_socktype = ai_req->ai_socktype;
+ ai.ai_protocol = ai_req->ai_protocol;
+
+ node = ai_req->node_len ? (const char*) ai_req + sizeof(addrinfo_request_t) : NULL;
+ service = ai_req->service_len ? (const char*) ai_req + sizeof(addrinfo_request_t) + ai_req->node_len : NULL;
+
+ ret = getaddrinfo(node, service,
+ ai_req->hints_is_null ? NULL : &ai,
+ &result);
+
+ /* send_addrinfo_reply() frees result */
+ return send_addrinfo_reply(out_fd, req->id, ret, result, errno, h_errno);
+ }
+
+ case REQUEST_NAMEINFO: {
+ int ret;
+ const nameinfo_request_t *ni_req = &packet->nameinfo_request;
+ char hostbuf[NI_MAXHOST], servbuf[NI_MAXSERV];
+ struct sockaddr_storage sa;
+
+ assert(length >= sizeof(nameinfo_request_t));
+ assert(length == sizeof(nameinfo_request_t) + ni_req->sockaddr_len);
+
+ memcpy(&sa, (const uint8_t *) ni_req + sizeof(nameinfo_request_t), ni_req->sockaddr_len);
+
+ ret = getnameinfo((struct sockaddr *)&sa, ni_req->sockaddr_len,
+ ni_req->gethost ? hostbuf : NULL, ni_req->gethost ? sizeof(hostbuf) : 0,
+ ni_req->getserv ? servbuf : NULL, ni_req->getserv ? sizeof(servbuf) : 0,
+ ni_req->flags);
+
+ return send_nameinfo_reply(out_fd, req->id, ret,
+ ret == 0 && ni_req->gethost ? hostbuf : NULL,
+ ret == 0 && ni_req->getserv ? servbuf : NULL,
+ errno, h_errno);
+ }
+
+ case REQUEST_RES_QUERY:
+ case REQUEST_RES_SEARCH: {
+ int ret;
+ HEADER answer[BUFSIZE/sizeof(HEADER) + 1];
+ const res_request_t *res_req = &packet->res_request;
+ const char *dname;
+
+ assert(length >= sizeof(res_request_t));
+ assert(length == sizeof(res_request_t) + res_req->dname_len);
+
+ dname = (const char *) req + sizeof(res_request_t);
+
+ if (req->type == REQUEST_RES_QUERY)
+ ret = res_query(dname, res_req->class, res_req->type, (unsigned char *) answer, BUFSIZE);
+ else
+ ret = res_search(dname, res_req->class, res_req->type, (unsigned char *) answer, BUFSIZE);
+
+ return send_res_reply(out_fd, req->id, (unsigned char *) answer, ret, errno, h_errno);
+ }
+
+ case REQUEST_TERMINATE:
+ /* Quit */
+ return -1;
+
+ default:
+ ;
+ }
+
+ return 0;
+}
+
+static void* thread_worker(void *p) {
+ sd_resolve_t *resolve = p;
+ sigset_t fullset;
+
+ /* No signals in this thread please */
+ sigfillset(&fullset);
+ pthread_sigmask(SIG_BLOCK, &fullset, NULL);
+
+ while (!resolve->dead) {
+ packet_t buf[BUFSIZE/sizeof(packet_t) + 1];
+ ssize_t length;
+
+ length = recv(resolve->fds[REQUEST_RECV_FD], buf, sizeof(buf), 0);
+
+ if (length <= 0) {
+ if (length < 0 && (errno == EAGAIN || errno == EINTR))
+ continue;
+ break;
+ }
+
+ if (resolve->dead)
+ break;
+
+ if (handle_request(resolve->fds[RESPONSE_SEND_FD], buf, (size_t) length) < 0)
+ break;
+ }
+
+ send_died(resolve->fds[RESPONSE_SEND_FD]);
+
+ return NULL;
+}
+
+sd_resolve_t* sd_resolve_new(unsigned n_proc) {
+ sd_resolve_t *resolve = NULL;
+ int i, r;
+
+ assert(n_proc >= 1);
+
+ if (n_proc > MAX_WORKERS)
+ n_proc = MAX_WORKERS;
+
+ resolve = malloc(sizeof(sd_resolve_t));
+ if (!resolve) {
+ errno = ENOMEM;
+ goto fail;
+ }
+
+ resolve->dead = 0;
+ resolve->valid_workers = 0;
+
+ for (i = 0; i < MESSAGE_FD_MAX; i++)
+ resolve->fds[i] = -1;
+
+ memset(resolve->queries, 0, sizeof(resolve->queries));
+
+ r = socketpair(PF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, resolve->fds);
+ if (r < 0)
+ goto fail;
+
+ r = socketpair(PF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, resolve->fds+2);
+ if (r < 0)
+ goto fail;
+
+ for (resolve->valid_workers = 0; resolve->valid_workers < n_proc; resolve->valid_workers++) {
+ r = pthread_create(&resolve->workers[resolve->valid_workers], NULL, thread_worker, resolve);
+ if (r) {
+ errno = r;
+ goto fail;
+ }
+ }
+
+ resolve->current_index = resolve->current_id = 0;
+ resolve->done_head = resolve->done_tail = NULL;
+ resolve->n_queries = 0;
+
+ fd_nonblock(resolve->fds[RESPONSE_RECV_FD], true);
+
+ return resolve;
+
+fail:
+ if (resolve)
+ sd_resolve_free(resolve);
+
+ return NULL;
+}
+
+void sd_resolve_free(sd_resolve_t *resolve) {
+ int i;
+ int saved_errno = errno;
+ unsigned p;
+
+ assert(resolve);
+
+ resolve->dead = 1;
+
+ if (resolve->fds[REQUEST_SEND_FD] >= 0) {
+ rheader_t req = {};
+
+ req.type = REQUEST_TERMINATE;
+ req.length = sizeof(req);
+ req.id = 0;
+
+ /* Send one termination packet for each worker */
+ for (p = 0; p < resolve->valid_workers; p++)
+ send(resolve->fds[REQUEST_SEND_FD], &req, req.length, MSG_NOSIGNAL);
+ }
+
+ /* Now terminate them and wait until they are gone. */
+ for (p = 0; p < resolve->valid_workers; p++) {
+ for (;;) {
+ if (pthread_join(resolve->workers[p], NULL) != EINTR)
+ break;
+ }
+ }
+
+ /* Close all communication channels */
+ for (i = 0; i < MESSAGE_FD_MAX; i++)
+ if (resolve->fds[i] >= 0)
+ close(resolve->fds[i]);
+
+ for (p = 0; p < MAX_QUERIES; p++)
+ if (resolve->queries[p])
+ sd_resolve_cancel(resolve, resolve->queries[p]);
+
+ free(resolve);
+
+ errno = saved_errno;
+}
+
+int sd_resolve_fd(sd_resolve_t *resolve) {
+ assert(resolve);
+
+ return resolve->fds[RESPONSE_RECV_FD];
+}
+
+static sd_resolve_query_t *lookup_query(sd_resolve_t *resolve, unsigned id) {
+ sd_resolve_query_t *q;
+ assert(resolve);
+
+ q = resolve->queries[id % MAX_QUERIES];
+ if (q)
+ if (q->id == id)
+ return q;
+
+ return NULL;
+}
+
+static void complete_query(sd_resolve_t *resolve, sd_resolve_query_t *q) {
+ assert(resolve);
+ assert(q);
+ assert(!q->done);
+
+ q->done = 1;
+
+ if ((q->done_prev = resolve->done_tail))
+ resolve->done_tail->done_next = q;
+ else
+ resolve->done_head = q;
+
+ resolve->done_tail = q;
+ q->done_next = NULL;
+}
+
+static const void *unserialize_addrinfo(const void *p, struct addrinfo **ret_ai, size_t *length) {
+ addrinfo_serialization_t s;
+ size_t l;
+ struct addrinfo *ai;
+ assert(p);
+ assert(ret_ai);
+ assert(length);
+
+ if (*length < sizeof(addrinfo_serialization_t))
+ return NULL;
+
+ memcpy(&s, p, sizeof(s));
+
+ l = sizeof(addrinfo_serialization_t) + s.ai_addrlen + s.canonname_len;
+ if (*length < l)
+ return NULL;
+
+ ai = malloc(sizeof(struct addrinfo));
+ if (!ai)
+ goto fail;
+
+ ai->ai_addr = NULL;
+ ai->ai_canonname = NULL;
+ ai->ai_next = NULL;
+
+ if (s.ai_addrlen && !(ai->ai_addr = malloc(s.ai_addrlen)))
+ goto fail;
+
+ if (s.canonname_len && !(ai->ai_canonname = malloc(s.canonname_len)))
+ goto fail;
+
+ ai->ai_flags = s.ai_flags;
+ ai->ai_family = s.ai_family;
+ ai->ai_socktype = s.ai_socktype;
+ ai->ai_protocol = s.ai_protocol;
+ ai->ai_addrlen = s.ai_addrlen;
+
+ if (ai->ai_addr)
+ memcpy(ai->ai_addr, (const uint8_t*) p + sizeof(addrinfo_serialization_t), s.ai_addrlen);
+
+ if (ai->ai_canonname)
+ memcpy(ai->ai_canonname, (const uint8_t*) p + sizeof(addrinfo_serialization_t) + s.ai_addrlen, s.canonname_len);
+
+ *length -= l;
+ *ret_ai = ai;
+
+ return (const uint8_t*) p + l;
+
+
+fail:
+ if (ai)
+ sd_resolve_freeaddrinfo(ai);
+
+ return NULL;
+}
+
+static int handle_response(sd_resolve_t *resolve, const packet_t *packet, size_t length) {
+ const rheader_t *resp;
+ sd_resolve_query_t *q;
+
+ assert(resolve);
+
+ resp = &packet->rheader;
+ assert(resp);
+ assert(length >= sizeof(rheader_t));
+ assert(length == resp->length);
+
+ if (resp->type == RESPONSE_DIED) {
+ resolve->dead = 1;
+ return 0;
+ }
+
+ q = lookup_query(resolve, resp->id);
+ if (!q)
+ return 0;
+
+ switch (resp->type) {
+ case RESPONSE_ADDRINFO: {
+ const addrinfo_response_t *ai_resp = &packet->addrinfo_response;
+ const void *p;
+ size_t l;
+ struct addrinfo *prev = NULL;
+
+ assert(length >= sizeof(addrinfo_response_t));
+ assert(q->type == REQUEST_ADDRINFO);
+
+ q->ret = ai_resp->ret;
+ q->_errno = ai_resp->_errno;
+ q->_h_errno = ai_resp->_h_errno;
+ l = length - sizeof(addrinfo_response_t);
+ p = (const uint8_t*) resp + sizeof(addrinfo_response_t);
+
+ while (l > 0 && p) {
+ struct addrinfo *ai = NULL;
+ p = unserialize_addrinfo(p, &ai, &l);
+
+ if (!p || !ai) {
+ q->ret = EAI_MEMORY;
+ break;
+ }
+
+ if (prev)
+ prev->ai_next = ai;
+ else
+ q->addrinfo = ai;
+
+ prev = ai;
+ }
+
+ complete_query(resolve, q);
+ break;
+ }
+
+ case RESPONSE_NAMEINFO: {
+ const nameinfo_response_t *ni_resp = &packet->nameinfo_response;
+
+ assert(length >= sizeof(nameinfo_response_t));
+ assert(q->type == REQUEST_NAMEINFO);
+
+ q->ret = ni_resp->ret;
+ q->_errno = ni_resp->_errno;
+ q->_h_errno = ni_resp->_h_errno;
+
+ if (ni_resp->hostlen)
+ if (!(q->host = strndup((const char*) ni_resp + sizeof(nameinfo_response_t), ni_resp->hostlen-1)))
+ q->ret = EAI_MEMORY;
+
+ if (ni_resp->servlen)
+ if (!(q->serv = strndup((const char*) ni_resp + sizeof(nameinfo_response_t) + ni_resp->hostlen, ni_resp->servlen-1)))
+ q->ret = EAI_MEMORY;
+
+ complete_query(resolve, q);
+ break;
+ }
+
+ case RESPONSE_RES: {
+ const res_response_t *res_resp = &packet->res_response;
+
+ assert(length >= sizeof(res_response_t));
+ assert(q->type == REQUEST_RES_QUERY || q->type == REQUEST_RES_SEARCH);
+
+ q->ret = res_resp->ret;
+ q->_errno = res_resp->_errno;
+ q->_h_errno = res_resp->_h_errno;
+
+ if (res_resp->ret >= 0) {
+ if (!(q->serv = malloc(res_resp->ret))) {
+ q->ret = -1;
+ q->_errno = ENOMEM;
+ } else
+ memcpy(q->serv, (const char *)resp + sizeof(res_response_t), res_resp->ret);
+ }
+
+ complete_query(resolve, q);
+ break;
+ }
+
+ default:
+ ;
+ }
+
+ return 0;
+}
+
+int sd_resolve_wait(sd_resolve_t *resolve, int block) {
+ int handled = 0;
+ assert(resolve);
+
+ for (;;) {
+ packet_t buf[BUFSIZE/sizeof(packet_t) + 1];
+ ssize_t l;
+
+ if (resolve->dead) {
+ errno = ECHILD;
+ return -1;
+ }
+
+ l = recv(resolve->fds[RESPONSE_RECV_FD], buf, sizeof(buf), 0);
+ if (l < 0) {
+ fd_set fds;
+
+ if (errno != EAGAIN)
+ return -1;
+
+ if (!block || handled)
+ return 0;
+
+ FD_ZERO(&fds);
+ FD_SET(resolve->fds[RESPONSE_RECV_FD], &fds);
+
+ if (select(resolve->fds[RESPONSE_RECV_FD]+1, &fds, NULL, NULL, NULL) < 0)
+ return -1;
+
+ continue;
+ }
+
+ if (handle_response(resolve, buf, (size_t) l) < 0)
+ return -1;
+
+ handled = 1;
+ }
+}
+
+static sd_resolve_query_t *alloc_query(sd_resolve_t *resolve) {
+ sd_resolve_query_t *q;
+ assert(resolve);
+
+ if (resolve->n_queries >= MAX_QUERIES) {
+ errno = ENOMEM;
+ return NULL;
+ }
+
+ while (resolve->queries[resolve->current_index]) {
+ resolve->current_index++;
+ resolve->current_id++;
+
+ while (resolve->current_index >= MAX_QUERIES)
+ resolve->current_index -= MAX_QUERIES;
+ }
+
+ q = resolve->queries[resolve->current_index] = malloc(sizeof(sd_resolve_query_t));
+ if (!q) {
+ errno = ENOMEM;
+ return NULL;
+ }
+
+ resolve->n_queries++;
+
+ q->resolve = resolve;
+ q->done = 0;
+ q->id = resolve->current_id;
+ q->done_next = q->done_prev = NULL;
+ q->ret = 0;
+ q->_errno = 0;
+ q->_h_errno = 0;
+ q->addrinfo = NULL;
+ q->userdata = NULL;
+ q->host = q->serv = NULL;
+
+ return q;
+}
+
+sd_resolve_query_t* sd_resolve_getaddrinfo(sd_resolve_t *resolve, const char *node, const char *service, const struct addrinfo *hints) {
+ addrinfo_request_t data[BUFSIZE/sizeof(addrinfo_request_t) + 1] = {};
+ addrinfo_request_t *req = data;
+ sd_resolve_query_t *q;
+ assert(resolve);
+ assert(node || service);
+
+ if (resolve->dead) {
+ errno = ECHILD;
+ return NULL;
+ }
+
+ q = alloc_query(resolve);
+ if (!q)
+ return NULL;
+
+ req->node_len = node ? strlen(node)+1 : 0;
+ req->service_len = service ? strlen(service)+1 : 0;
+
+ req->header.id = q->id;
+ req->header.type = q->type = REQUEST_ADDRINFO;
+ req->header.length = sizeof(addrinfo_request_t) + req->node_len + req->service_len;
+
+ if (req->header.length > BUFSIZE) {
+ errno = ENOMEM;
+ goto fail;
+ }
+
+ if (!(req->hints_is_null = !hints)) {
+ req->ai_flags = hints->ai_flags;
+ req->ai_family = hints->ai_family;
+ req->ai_socktype = hints->ai_socktype;
+ req->ai_protocol = hints->ai_protocol;
+ }
+
+ if (node)
+ strcpy((char*) req + sizeof(addrinfo_request_t), node);
+
+ if (service)
+ strcpy((char*) req + sizeof(addrinfo_request_t) + req->node_len, service);
+
+ if (send(resolve->fds[REQUEST_SEND_FD], req, req->header.length, MSG_NOSIGNAL) < 0)
+ goto fail;
+
+ return q;
+
+fail:
+ if (q)
+ sd_resolve_cancel(resolve, q);
+
+ return NULL;
+}
+
+int sd_resolve_getaddrinfo_done(sd_resolve_t *resolve, sd_resolve_query_t* q, struct addrinfo **ret_res) {
+ int ret;
+ assert(resolve);
+ assert(q);
+ assert(q->resolve == resolve);
+ assert(q->type == REQUEST_ADDRINFO);
+
+ if (resolve->dead) {
+ errno = ECHILD;
+ return EAI_SYSTEM;
+ }
+
+ if (!q->done)
+ return EAI_AGAIN;
+
+ *ret_res = q->addrinfo;
+ q->addrinfo = NULL;
+
+ ret = q->ret;
+
+ if (ret == EAI_SYSTEM)
+ errno = q->_errno;
+
+ if (ret != 0)
+ h_errno = q->_h_errno;
+
+ sd_resolve_cancel(resolve, q);
+
+ return ret;
+}
+
+sd_resolve_query_t* sd_resolve_getnameinfo(sd_resolve_t *resolve, const struct sockaddr *sa, socklen_t salen, int flags, int gethost, int getserv) {
+ nameinfo_request_t data[BUFSIZE/sizeof(nameinfo_request_t) + 1] = {};
+ nameinfo_request_t *req = data;
+ sd_resolve_query_t *q;
+
+ assert(resolve);
+ assert(sa);
+ assert(salen > 0);
+
+ if (resolve->dead) {
+ errno = ECHILD;
+ return NULL;
+ }
+
+ q = alloc_query(resolve);
+ if (!q)
+ return NULL;
+
+ req->header.id = q->id;
+ req->header.type = q->type = REQUEST_NAMEINFO;
+ req->header.length = sizeof(nameinfo_request_t) + salen;
+
+ if (req->header.length > BUFSIZE) {
+ errno = ENOMEM;
+ goto fail;
+ }
+
+ req->flags = flags;
+ req->sockaddr_len = salen;
+ req->gethost = gethost;
+ req->getserv = getserv;
+
+ memcpy((uint8_t*) req + sizeof(nameinfo_request_t), sa, salen);
+
+ if (send(resolve->fds[REQUEST_SEND_FD], req, req->header.length, MSG_NOSIGNAL) < 0)
+ goto fail;
+
+ return q;
+
+fail:
+ if (q)
+ sd_resolve_cancel(resolve, q);
+
+ return NULL;
+}
+
+int sd_resolve_getnameinfo_done(sd_resolve_t *resolve, sd_resolve_query_t* q, char *ret_host, size_t hostlen, char *ret_serv, size_t servlen) {
+ int ret;
+ assert(resolve);
+ assert(q);
+ assert(q->resolve == resolve);
+ assert(q->type == REQUEST_NAMEINFO);
+ assert(!ret_host || hostlen);
+ assert(!ret_serv || servlen);
+
+ if (resolve->dead) {
+ errno = ECHILD;
+ return EAI_SYSTEM;
+ }
+
+ if (!q->done)
+ return EAI_AGAIN;
+
+ if (ret_host && q->host) {
+ strncpy(ret_host, q->host, hostlen);
+ ret_host[hostlen-1] = 0;
+ }
+
+ if (ret_serv && q->serv) {
+ strncpy(ret_serv, q->serv, servlen);
+ ret_serv[servlen-1] = 0;
+ }
+
+ ret = q->ret;
+
+ if (ret == EAI_SYSTEM)
+ errno = q->_errno;
+
+ if (ret != 0)
+ h_errno = q->_h_errno;
+
+ sd_resolve_cancel(resolve, q);
+
+ return ret;
+}
+
+static sd_resolve_query_t * resolve_res(sd_resolve_t *resolve, query_type_t qtype, const char *dname, int class, int type) {
+ res_request_t data[BUFSIZE/sizeof(res_request_t) + 1];
+ res_request_t *req = data;
+ sd_resolve_query_t *q;
+
+ assert(resolve);
+ assert(dname);
+
+ if (resolve->dead) {
+ errno = ECHILD;
+ return NULL;
+ }
+
+ q = alloc_query(resolve);
+ if (!q)
+ return NULL;
+
+ req->dname_len = strlen(dname) + 1;
+
+ req->header.id = q->id;
+ req->header.type = q->type = qtype;
+ req->header.length = sizeof(res_request_t) + req->dname_len;
+
+ if (req->header.length > BUFSIZE) {
+ errno = ENOMEM;
+ goto fail;
+ }
+
+ req->class = class;
+ req->type = type;
+
+ strcpy((char*) req + sizeof(res_request_t), dname);
+
+ if (send(resolve->fds[REQUEST_SEND_FD], req, req->header.length, MSG_NOSIGNAL) < 0)
+ goto fail;
+
+ return q;
+
+fail:
+ if (q)
+ sd_resolve_cancel(resolve, q);
+
+ return NULL;
+}
+
+sd_resolve_query_t* sd_resolve_res_query(sd_resolve_t *resolve, const char *dname, int class, int type) {
+ return resolve_res(resolve, REQUEST_RES_QUERY, dname, class, type);
+}
+
+sd_resolve_query_t* sd_resolve_res_search(sd_resolve_t *resolve, const char *dname, int class, int type) {
+ return resolve_res(resolve, REQUEST_RES_SEARCH, dname, class, type);
+}
+
+int sd_resolve_res_done(sd_resolve_t *resolve, sd_resolve_query_t* q, unsigned char **answer) {
+ int ret;
+ assert(resolve);
+ assert(q);
+ assert(q->resolve == resolve);
+ assert(q->type == REQUEST_RES_QUERY || q->type == REQUEST_RES_SEARCH);
+ assert(answer);
+
+ if (resolve->dead) {
+ errno = ECHILD;
+ return -ECHILD;
+ }
+
+ if (!q->done) {
+ errno = EAGAIN;
+ return -EAGAIN;
+ }
+
+ *answer = (unsigned char *)q->serv;
+ q->serv = NULL;
+
+ ret = q->ret;
+
+ if (ret < 0) {
+ errno = q->_errno;
+ h_errno = q->_h_errno;
+ }
+
+ sd_resolve_cancel(resolve, q);
+
+ return ret < 0 ? -errno : ret;
+}
+
+sd_resolve_query_t* sd_resolve_getnext(sd_resolve_t *resolve) {
+ assert(resolve);
+ return resolve->done_head;
+}
+
+int sd_resolve_getnqueries(sd_resolve_t *resolve) {
+ assert(resolve);
+ return resolve->n_queries;
+}
+
+void sd_resolve_cancel(sd_resolve_t *resolve, sd_resolve_query_t* q) {
+ int i;
+ int saved_errno = errno;
+
+ assert(resolve);
+ assert(q);
+ assert(q->resolve == resolve);
+ assert(resolve->n_queries > 0);
+
+ if (q->done) {
+
+ if (q->done_prev)
+ q->done_prev->done_next = q->done_next;
+ else
+ resolve->done_head = q->done_next;
+
+ if (q->done_next)
+ q->done_next->done_prev = q->done_prev;
+ else
+ resolve->done_tail = q->done_prev;
+ }
+
+ i = q->id % MAX_QUERIES;
+ assert(resolve->queries[i] == q);
+ resolve->queries[i] = NULL;
+
+ sd_resolve_freeaddrinfo(q->addrinfo);
+ free(q->host);
+ free(q->serv);
+
+ resolve->n_queries--;
+ free(q);
+
+ errno = saved_errno;
+}
+
+void sd_resolve_freeaddrinfo(struct addrinfo *ai) {
+ int saved_errno = errno;
+
+ while (ai) {
+ struct addrinfo *next = ai->ai_next;
+
+ free(ai->ai_addr);
+ free(ai->ai_canonname);
+ free(ai);
+
+ ai = next;
+ }
+
+ errno = saved_errno;
+}
+
+void sd_resolve_freeanswer(unsigned char *answer) {
+ int saved_errno = errno;
+
+ if (!answer)
+ return;
+
+ free(answer);
+
+ errno = saved_errno;
+}
+
+int sd_resolve_isdone(sd_resolve_t *resolve, sd_resolve_query_t*q) {
+ assert(resolve);
+ assert(q);
+ assert(q->resolve == resolve);
+
+ return q->done;
+}
+
+void sd_resolve_setuserdata(sd_resolve_t *resolve, sd_resolve_query_t *q, void *userdata) {
+ assert(q);
+ assert(resolve);
+ assert(q->resolve = resolve);
+
+ q->userdata = userdata;
+}
+
+void* sd_resolve_getuserdata(sd_resolve_t *resolve, sd_resolve_query_t *q) {
+ assert(q);
+ assert(resolve);
+ assert(q->resolve = resolve);
+
+ return q->userdata;
+}
diff --git a/src/libsystemd/test-resolv.c b/src/libsystemd/test-resolv.c
deleted file mode 100644
index 5baef96..0000000
--- a/src/libsystemd/test-resolv.c
+++ /dev/null
@@ -1,160 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2005-2008 Lennart Poettering
-
- systemd is free software; you can redistribute it and/or modify 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.
-
- systemd 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
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <arpa/inet.h>
-#include <stdio.h>
-#include <netinet/in.h>
-#include <arpa/nameser.h>
-#include <resolv.h>
-#include <assert.h>
-#include <signal.h>
-#include <errno.h>
-
-#include "sd-resolv.h"
-#include "resolv-util.h"
-#include "macro.h"
-
-int main(int argc, char *argv[]) {
- int r = 1, ret;
- _cleanup_resolv_free_ sd_resolv_t *resolv = NULL;
- _cleanup_resolv_addrinfo_free_ struct addrinfo *ai = NULL;
- _cleanup_resolv_answer_free_ unsigned char *srv = NULL;
- sd_resolv_query_t *q1, *q2, *q3;
- struct addrinfo hints = {};
- struct sockaddr_in sa = {};
- char host[NI_MAXHOST] = "", serv[NI_MAXSERV] = "";
-
- signal(SIGCHLD, SIG_IGN);
-
- resolv = sd_resolv_new(2);
- if (!resolv)
- log_oom();
-
- /* Make a name -> address query */
- hints.ai_family = PF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
-
- q1 = sd_resolv_getaddrinfo(resolv, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints);
- if (!q1)
- fprintf(stderr, "sd_resolv_getaddrinfo(): %s\n", strerror(errno));
-
- /* Make an address -> name query */
- sa.sin_family = AF_INET;
- sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71");
- sa.sin_port = htons(80);
-
- q2 = sd_resolv_getnameinfo(resolv, (struct sockaddr*) &sa, sizeof(sa), 0, 1, 1);
- if (!q2)
- fprintf(stderr, "sd_resolv_getnameinfo(): %s\n", strerror(errno));
-
- /* Make a res_query() call */
- q3 = sd_resolv_res_query(resolv, "_xmpp-client._tcp.gmail.com", C_IN, T_SRV);
- if (!q3)
- fprintf(stderr, "sd_resolv_res_query(): %s\n", strerror(errno));
-
- /* Wait until the three queries are completed */
- while (!sd_resolv_isdone(resolv, q1) ||
- !sd_resolv_isdone(resolv, q2) ||
- !sd_resolv_isdone(resolv, q3)) {
- if (sd_resolv_wait(resolv, 1) < 0)
- fprintf(stderr, "sd_resolv_wait(): %s\n", strerror(errno));
- }
-
- /* Interpret the result of the name -> addr query */
- ret = sd_resolv_getaddrinfo_done(resolv, q1, &ai);
- if (ret)
- fprintf(stderr, "error: %s %i\n", gai_strerror(ret), ret);
- else {
- struct addrinfo *i;
-
- for (i = ai; i; i = i->ai_next) {
- char t[256];
- const char *p = NULL;
-
- if (i->ai_family == PF_INET)
- p = inet_ntop(AF_INET, &((struct sockaddr_in*) i->ai_addr)->sin_addr, t, sizeof(t));
- else if (i->ai_family == PF_INET6)
- p = inet_ntop(AF_INET6, &((struct sockaddr_in6*) i->ai_addr)->sin6_addr, t, sizeof(t));
-
- printf("%s\n", p);
- }
- }
-
- /* Interpret the result of the addr -> name query */
- ret = sd_resolv_getnameinfo_done(resolv, q2, host, sizeof(host), serv, sizeof(serv));
- if (ret)
- fprintf(stderr, "error: %s %i\n", gai_strerror(ret), ret);
- else
- printf("%s -- %s\n", host, serv);
-
- /* Interpret the result of the SRV lookup */
- ret = sd_resolv_res_done(resolv, q3, &srv);
- if (ret < 0) {
- fprintf(stderr, "error: %s %i\n", strerror(errno), ret);
- } else if (ret == 0) {
- fprintf(stderr, "No reply for SRV lookup\n");
- } else {
- int qdcount;
- int ancount;
- int len;
- const unsigned char *pos = srv + sizeof(HEADER);
- unsigned char *end = srv + ret;
- HEADER *head = (HEADER *)srv;
- char name[256];
-
- qdcount = ntohs(head->qdcount);
- ancount = ntohs(head->ancount);
-
- printf("%d answers for srv lookup:\n", ancount);
-
- /* Ignore the questions */
- while (qdcount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
- assert(len >= 0);
- pos += len + QFIXEDSZ;
- }
-
- /* Parse the answers */
- while (ancount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
- /* Ignore the initial string */
- uint16_t pref, weight, port;
- assert(len >= 0);
- pos += len;
- /* Ignore type, ttl, class and dlen */
- pos += 10;
-
- GETSHORT(pref, pos);
- GETSHORT(weight, pos);
- GETSHORT(port, pos);
- len = dn_expand(srv, end, pos, name, 255);
- printf("\tpreference: %2d weight: %2d port: %d host: %s\n",
- pref, weight, port, name);
-
- pos += len;
- }
- }
-
- r = 0;
-
- return r;
-}
diff --git a/src/libsystemd/test-resolve.c b/src/libsystemd/test-resolve.c
new file mode 100644
index 0000000..c7feb1e
--- /dev/null
+++ b/src/libsystemd/test-resolve.c
@@ -0,0 +1,160 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2005-2008 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify 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.
+
+ systemd 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <stdio.h>
+#include <netinet/in.h>
+#include <arpa/nameser.h>
+#include <resolv.h>
+#include <assert.h>
+#include <signal.h>
+#include <errno.h>
+
+#include "sd-resolve.h"
+#include "resolve-util.h"
+#include "macro.h"
+
+int main(int argc, char *argv[]) {
+ int r = 1, ret;
+ _cleanup_resolve_free_ sd_resolve_t *resolve = NULL;
+ _cleanup_resolve_addrinfo_free_ struct addrinfo *ai = NULL;
+ _cleanup_resolve_answer_free_ unsigned char *srv = NULL;
+ sd_resolve_query_t *q1, *q2, *q3;
+ struct addrinfo hints = {};
+ struct sockaddr_in sa = {};
+ char host[NI_MAXHOST] = "", serv[NI_MAXSERV] = "";
+
+ signal(SIGCHLD, SIG_IGN);
+
+ resolve = sd_resolve_new(2);
+ if (!resolve)
+ log_oom();
+
+ /* Make a name -> address query */
+ hints.ai_family = PF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+
+ q1 = sd_resolve_getaddrinfo(resolve, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints);
+ if (!q1)
+ fprintf(stderr, "sd_resolve_getaddrinfo(): %s\n", strerror(errno));
+
+ /* Make an address -> name query */
+ sa.sin_family = AF_INET;
+ sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71");
+ sa.sin_port = htons(80);
+
+ q2 = sd_resolve_getnameinfo(resolve, (struct sockaddr*) &sa, sizeof(sa), 0, 1, 1);
+ if (!q2)
+ fprintf(stderr, "sd_resolve_getnameinfo(): %s\n", strerror(errno));
+
+ /* Make a res_query() call */
+ q3 = sd_resolve_res_query(resolve, "_xmpp-client._tcp.gmail.com", C_IN, T_SRV);
+ if (!q3)
+ fprintf(stderr, "sd_resolve_res_query(): %s\n", strerror(errno));
+
+ /* Wait until the three queries are completed */
+ while (!sd_resolve_isdone(resolve, q1) ||
+ !sd_resolve_isdone(resolve, q2) ||
+ !sd_resolve_isdone(resolve, q3)) {
+ if (sd_resolve_wait(resolve, 1) < 0)
+ fprintf(stderr, "sd_resolve_wait(): %s\n", strerror(errno));
+ }
+
+ /* Interpret the result of the name -> addr query */
+ ret = sd_resolve_getaddrinfo_done(resolve, q1, &ai);
+ if (ret)
+ fprintf(stderr, "error: %s %i\n", gai_strerror(ret), ret);
+ else {
+ struct addrinfo *i;
+
+ for (i = ai; i; i = i->ai_next) {
+ char t[256];
+ const char *p = NULL;
+
+ if (i->ai_family == PF_INET)
+ p = inet_ntop(AF_INET, &((struct sockaddr_in*) i->ai_addr)->sin_addr, t, sizeof(t));
+ else if (i->ai_family == PF_INET6)
+ p = inet_ntop(AF_INET6, &((struct sockaddr_in6*) i->ai_addr)->sin6_addr, t, sizeof(t));
+
+ printf("%s\n", p);
+ }
+ }
+
+ /* Interpret the result of the addr -> name query */
+ ret = sd_resolve_getnameinfo_done(resolve, q2, host, sizeof(host), serv, sizeof(serv));
+ if (ret)
+ fprintf(stderr, "error: %s %i\n", gai_strerror(ret), ret);
+ else
+ printf("%s -- %s\n", host, serv);
+
+ /* Interpret the result of the SRV lookup */
+ ret = sd_resolve_res_done(resolve, q3, &srv);
+ if (ret < 0) {
+ fprintf(stderr, "error: %s %i\n", strerror(errno), ret);
+ } else if (ret == 0) {
+ fprintf(stderr, "No reply for SRV lookup\n");
+ } else {
+ int qdcount;
+ int ancount;
+ int len;
+ const unsigned char *pos = srv + sizeof(HEADER);
+ unsigned char *end = srv + ret;
+ HEADER *head = (HEADER *)srv;
+ char name[256];
+
+ qdcount = ntohs(head->qdcount);
+ ancount = ntohs(head->ancount);
+
+ printf("%d answers for srv lookup:\n", ancount);
+
+ /* Ignore the questions */
+ while (qdcount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
+ assert(len >= 0);
+ pos += len + QFIXEDSZ;
+ }
+
+ /* Parse the answers */
+ while (ancount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
+ /* Ignore the initial string */
+ uint16_t pref, weight, port;
+ assert(len >= 0);
+ pos += len;
+ /* Ignore type, ttl, class and dlen */
+ pos += 10;
+
+ GETSHORT(pref, pos);
+ GETSHORT(weight, pos);
+ GETSHORT(port, pos);
+ len = dn_expand(srv, end, pos, name, 255);
+ printf("\tpreference: %2d weight: %2d port: %d host: %s\n",
+ pref, weight, port, name);
+
+ pos += len;
+ }
+ }
+
+ r = 0;
+
+ return r;
+}
diff --git a/src/systemd/sd-resolv.h b/src/systemd/sd-resolv.h
deleted file mode 100644
index 21e2413..0000000
--- a/src/systemd/sd-resolv.h
+++ /dev/null
@@ -1,158 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef foosdresolvhfoo
-#define foosdresolvhfoo
-
-/***
- This file is part of systemd.
-
- Copyright 2005-2008 Lennart Poettering
-
- systemd is free software; you can redistribute it and/or modify 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.
-
- systemd 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
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netdb.h>
-#include "_sd-common.h"
-
-_SD_BEGIN_DECLARATIONS;
-
-/** \mainpage
- *
- * \section moo Method of operation
- *
- * To use libresolv allocate an sd_resolv_t object with
- * sd_resolv_new(). This will spawn a number of worker threads (or processes, depending on what is available) which
- * are subsequently used to process the queries the controlling
- * program issues via sd_resolv_getaddrinfo() and
- * sd_resolv_getnameinfo(). Use sd_resolv_free() to shut down the worker
- * threads/processes.
- *
- * Since libresolv may fork off new processes you have to make sure that
- * your program is not irritated by spurious SIGCHLD signals.
- */
-
-/** An opaque libresolv session structure */
-typedef struct sd_resolv sd_resolv_t;
-
-/** An opaque libresolv query structure */
-typedef struct sd_resolv_query sd_resolv_query_t;
-
-/** Allocate a new libresolv session with n_proc worker processes/threads */
-sd_resolv_t* sd_resolv_new(unsigned n_proc);
-
-/** Free a libresolv session. This destroys all attached
- * sd_resolv_query_t objects automatically */
-void sd_resolv_free(sd_resolv_t *resolv);
-
-/** Return the UNIX file descriptor to select() for readability
- * on. Use this function to integrate libresolv with your custom main
- * loop. */
-int sd_resolv_fd(sd_resolv_t *resolv);
-
-/** Process pending responses. After this function is called you can
- * get the next completed query object(s) using sd_resolv_getnext(). If
- * block is non-zero wait until at least one response has been
- * processed. If block is zero, process all pending responses and
- * return. */
-int sd_resolv_wait(sd_resolv_t *resolv, int block);
-
-/** Issue a name to address query on the specified session. The
- * arguments are compatible with the ones of libc's
- * getaddrinfo(3). The function returns a new query object. When the
- * query is completed you may retrieve the results using
- * sd_resolv_getaddrinfo_done().*/
-sd_resolv_query_t* sd_resolv_getaddrinfo(sd_resolv_t *resolv, const char *node, const char *service, const struct addrinfo *hints);
-
-/** Retrieve the results of a preceding sd_resolv_getaddrinfo()
- * call. Returns a addrinfo structure and a return value compatible
- * with libc's getaddrinfo(3). The query object q is destroyed by this
- * call and may not be used any further. Make sure to free the
- * returned addrinfo structure with sd_resolv_freeaddrinfo() and not
- * libc's freeaddrinfo(3)! If the query is not completed yet EAI_AGAIN
- * is returned.*/
-int sd_resolv_getaddrinfo_done(sd_resolv_t *resolv, sd_resolv_query_t* q, struct addrinfo **ret_res);
-
-/** Issue an address to name query on the specified session. The
- * arguments are compatible with the ones of libc's
- * getnameinfo(3). The function returns a new query object. When the
- * query is completed you may retrieve the results using
- * sd_resolv_getnameinfo_done(). Set gethost (resp. getserv) to non-zero
- * if you want to query the hostname (resp. the service name). */
-sd_resolv_query_t* sd_resolv_getnameinfo(sd_resolv_t *resolv, const struct sockaddr *sa, socklen_t salen, int flags, int gethost, int getserv);
-
-/** Retrieve the results of a preceding sd_resolv_getnameinfo()
- * call. Returns the hostname and the service name in ret_host and
- * ret_serv. The query object q is destroyed by this call and may not
- * be used any further. If the query is not completed yet EAI_AGAIN is
- * returned. */
-int sd_resolv_getnameinfo_done(sd_resolv_t *resolv, sd_resolv_query_t* q, char *ret_host, size_t hostlen, char *ret_serv, size_t servlen);
-
-/** Issue a resolver query on the specified session. The arguments are
- * compatible with the ones of libc's res_query(3). The function returns a new
- * query object. When the query is completed you may retrieve the results using
- * sd_resolv_res_done(). */
-sd_resolv_query_t* sd_resolv_res_query(sd_resolv_t *resolv, const char *dname, int class, int type);
-
-/** Issue an resolver query on the specified session. The arguments are
- * compatible with the ones of libc's res_search(3). The function returns a new
- * query object. When the query is completed you may retrieve the results using
- * sd_resolv_res_done(). */
-sd_resolv_query_t* sd_resolv_res_search(sd_resolv_t *resolv, const char *dname, int class, int type);
-
-/** Retrieve the results of a preceding sd_resolv_res_query() or
- * resolv_res_search call. The query object q is destroyed by this
- * call and may not be used any further. Returns a pointer to the
- * answer of the res_query call. If the query is not completed yet
- * -EAGAIN is returned, on failure -errno is returned, otherwise the
- * length of answer is returned. Make sure to free the answer is a
- * call to sd_resolv_freeanswer(). */
-int sd_resolv_res_done(sd_resolv_t *resolv, sd_resolv_query_t* q, unsigned char **answer);
-
-/** Return the next completed query object. If no query has been
- * completed yet, return NULL. Please note that you need to run
- * sd_resolv_wait() before this function will return sensible data. */
-sd_resolv_query_t* sd_resolv_getnext(sd_resolv_t *resolv);
-
-/** Return the number of query objects (completed or not) attached to
- * this session */
-int sd_resolv_getnqueries(sd_resolv_t *resolv);
-
-/** Cancel a currently running query. q is is destroyed by this call
- * and may not be used any futher. */
-void sd_resolv_cancel(sd_resolv_t *resolv, sd_resolv_query_t* q);
-
-/** Free the addrinfo structure as returned by
- * sd_resolv_getaddrinfo_done(). Make sure to use this functions instead
- * of the libc's freeaddrinfo()! */
-void sd_resolv_freeaddrinfo(struct addrinfo *ai);
-
-/** Free the answer data as returned by sd_resolv_res_done().*/
-void sd_resolv_freeanswer(unsigned char *answer);
-
-/** Returns non-zero when the query operation specified by q has been completed */
-int sd_resolv_isdone(sd_resolv_t *resolv, sd_resolv_query_t*q);
-
-/** Assign some opaque userdata with a query object */
-void sd_resolv_setuserdata(sd_resolv_t *resolv, sd_resolv_query_t *q, void *userdata);
-
-/** Return userdata assigned to a query object. Use
- * sd_resolv_setuserdata() to set this data. If no data has been set
- * prior to this call it returns NULL. */
-void* sd_resolv_getuserdata(sd_resolv_t *resolv, sd_resolv_query_t *q);
-
-_SD_END_DECLARATIONS;
-
-#endif
diff --git a/src/systemd/sd-resolve.h b/src/systemd/sd-resolve.h
new file mode 100644
index 0000000..2023f11
--- /dev/null
+++ b/src/systemd/sd-resolve.h
@@ -0,0 +1,158 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#ifndef foosdresolvehfoo
+#define foosdresolvehfoo
+
+/***
+ This file is part of systemd.
+
+ Copyright 2005-2008 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify 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.
+
+ systemd 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include "_sd-common.h"
+
+_SD_BEGIN_DECLARATIONS;
+
+/** \mainpage
+ *
+ * \section moo Method of operation
+ *
+ * To use sd-resolve allocate an sd_resolve_t object with
+ * sd_resolve_new(). This will spawn a number of worker threads (or processes, depending on what is available) which
+ * are subsequently used to process the queries the controlling
+ * program issues via sd_resolve_getaddrinfo() and
+ * sd_resolve_getnameinfo(). Use sd_resolve_free() to shut down the worker
+ * threads/processes.
+ *
+ * Since sd-resolve may fork off new processes you have to make sure that
+ * your program is not irritated by spurious SIGCHLD signals.
+ */
+
+/** An opaque sd-resolve session structure */
+typedef struct sd_resolve sd_resolve_t;
+
+/** An opaque sd-resolve query structure */
+typedef struct sd_resolve_query sd_resolve_query_t;
+
+/** Allocate a new sd-resolve session with n_proc worker processes/threads */
+sd_resolve_t* sd_resolve_new(unsigned n_proc);
+
+/** Free a sd-resolve session. This destroys all attached
+ * sd_resolve_query_t objects automatically */
+void sd_resolve_free(sd_resolve_t *resolve);
+
+/** Return the UNIX file descriptor to select() for readability
+ * on. Use this function to integrate sd-resolve with your custom main
+ * loop. */
+int sd_resolve_fd(sd_resolve_t *resolve);
+
+/** Process pending responses. After this function is called you can
+ * get the next completed query object(s) using sd_resolve_getnext(). If
+ * block is non-zero wait until at least one response has been
+ * processed. If block is zero, process all pending responses and
+ * return. */
+int sd_resolve_wait(sd_resolve_t *resolve, int block);
+
+/** Issue a name to address query on the specified session. The
+ * arguments are compatible with the ones of libc's
+ * getaddrinfo(3). The function returns a new query object. When the
+ * query is completed you may retrieve the results using
+ * sd_resolve_getaddrinfo_done().*/
+sd_resolve_query_t* sd_resolve_getaddrinfo(sd_resolve_t *resolve, const char *node, const char *service, const struct addrinfo *hints);
+
+/** Retrieve the results of a preceding sd_resolve_getaddrinfo()
+ * call. Returns a addrinfo structure and a return value compatible
+ * with libc's getaddrinfo(3). The query object q is destroyed by this
+ * call and may not be used any further. Make sure to free the
+ * returned addrinfo structure with sd_resolve_freeaddrinfo() and not
+ * libc's freeaddrinfo(3)! If the query is not completed yet EAI_AGAIN
+ * is returned.*/
+int sd_resolve_getaddrinfo_done(sd_resolve_t *resolve, sd_resolve_query_t* q, struct addrinfo **ret_res);
+
+/** Issue an address to name query on the specified session. The
+ * arguments are compatible with the ones of libc's
+ * getnameinfo(3). The function returns a new query object. When the
+ * query is completed you may retrieve the results using
+ * sd_resolve_getnameinfo_done(). Set gethost (resp. getserv) to non-zero
+ * if you want to query the hostname (resp. the service name). */
+sd_resolve_query_t* sd_resolve_getnameinfo(sd_resolve_t *resolve, const struct sockaddr *sa, socklen_t salen, int flags, int gethost, int getserv);
+
+/** Retrieve the results of a preceding sd_resolve_getnameinfo()
+ * call. Returns the hostname and the service name in ret_host and
+ * ret_serv. The query object q is destroyed by this call and may not
+ * be used any further. If the query is not completed yet EAI_AGAIN is
+ * returned. */
+int sd_resolve_getnameinfo_done(sd_resolve_t *resolve, sd_resolve_query_t* q, char *ret_host, size_t hostlen, char *ret_serv, size_t servlen);
+
+/** Issue a resolveer query on the specified session. The arguments are
+ * compatible with the ones of libc's res_query(3). The function returns a new
+ * query object. When the query is completed you may retrieve the results using
+ * sd_resolve_res_done(). */
+sd_resolve_query_t* sd_resolve_res_query(sd_resolve_t *resolve, const char *dname, int class, int type);
+
+/** Issue an resolveer query on the specified session. The arguments are
+ * compatible with the ones of libc's res_search(3). The function returns a new
+ * query object. When the query is completed you may retrieve the results using
+ * sd_resolve_res_done(). */
+sd_resolve_query_t* sd_resolve_res_search(sd_resolve_t *resolve, const char *dname, int class, int type);
+
+/** Retrieve the results of a preceding sd_resolve_res_query() or
+ * resolve_res_search call. The query object q is destroyed by this
+ * call and may not be used any further. Returns a pointer to the
+ * answer of the res_query call. If the query is not completed yet
+ * -EAGAIN is returned, on failure -errno is returned, otherwise the
+ * length of answer is returned. Make sure to free the answer is a
+ * call to sd_resolve_freeanswer(). */
+int sd_resolve_res_done(sd_resolve_t *resolve, sd_resolve_query_t* q, unsigned char **answer);
+
+/** Return the next completed query object. If no query has been
+ * completed yet, return NULL. Please note that you need to run
+ * sd_resolve_wait() before this function will return sensible data. */
+sd_resolve_query_t* sd_resolve_getnext(sd_resolve_t *resolve);
+
+/** Return the number of query objects (completed or not) attached to
+ * this session */
+int sd_resolve_getnqueries(sd_resolve_t *resolve);
+
+/** Cancel a currently running query. q is is destroyed by this call
+ * and may not be used any futher. */
+void sd_resolve_cancel(sd_resolve_t *resolve, sd_resolve_query_t* q);
+
+/** Free the addrinfo structure as returned by
+ * sd_resolve_getaddrinfo_done(). Make sure to use this functions instead
+ * of the libc's freeaddrinfo()! */
+void sd_resolve_freeaddrinfo(struct addrinfo *ai);
+
+/** Free the answer data as returned by sd_resolve_res_done().*/
+void sd_resolve_freeanswer(unsigned char *answer);
+
+/** Returns non-zero when the query operation specified by q has been completed */
+int sd_resolve_isdone(sd_resolve_t *resolve, sd_resolve_query_t*q);
+
+/** Assign some opaque userdata with a query object */
+void sd_resolve_setuserdata(sd_resolve_t *resolve, sd_resolve_query_t *q, void *userdata);
+
+/** Return userdata assigned to a query object. Use
+ * sd_resolve_setuserdata() to set this data. If no data has been set
+ * prior to this call it returns NULL. */
+void* sd_resolve_getuserdata(sd_resolve_t *resolve, sd_resolve_query_t *q);
+
+_SD_END_DECLARATIONS;
+
+#endif
More information about the systemd-commits
mailing list