sd-resolve.c revision 07630cea1f3a845c09309f197ac7c4f11edd3b62
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2005-2008 Lennart Poettering
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 <errno.h>
#include <poll.h>
#include <pthread.h>
#include <resolv.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "sd-resolve.h"
#include "list.h"
#include "missing.h"
#include "resolve-util.h"
#include "socket-util.h"
#include "util.h"
#define WORKERS_MIN 1U
#define WORKERS_MAX 16U
#define QUERIES_MAX 256U
#define BUFSIZE 10240U
typedef enum {
} QueryType;
enum {
};
struct sd_resolve {
unsigned n_ref;
bool dead:1;
unsigned n_valid_workers;
unsigned current_id;
};
struct sd_resolve_query {
unsigned n_ref;
bool done:1;
bool floating:1;
unsigned id;
int ret;
int _errno;
int _h_errno;
union {
};
void *userdata;
};
typedef struct RHeader {
unsigned id;
} RHeader;
typedef struct AddrInfoRequest {
bool hints_valid;
int ai_flags;
int ai_family;
int ai_socktype;
int ai_protocol;
typedef struct AddrInfoResponse {
int ret;
int _errno;
int _h_errno;
/* followed by addrinfo_serialization[] */
typedef struct AddrInfoSerialization {
int ai_flags;
int ai_family;
int ai_socktype;
int ai_protocol;
/* Followed by ai_addr amd ai_canonname with variable lengths */
typedef struct NameInfoRequest {
int flags;
typedef struct NameInfoResponse {
int ret;
int _errno;
int _h_errno;
typedef union Packet {
} Packet;
static int getaddrinfo_done(sd_resolve_query* q);
static int getnameinfo_done(sd_resolve_query *q);
static void resolve_query_disconnect(sd_resolve_query *q);
#define RESOLVE_DONT_DESTROY(resolve) \
.type = RESPONSE_DIED,
};
return -errno;
return 0;
}
static void *serialize_addrinfo(void *p, const struct addrinfo *ai, size_t *length, size_t maxlength) {
assert(p);
return NULL;
s.canonname_len = cnl;
if (ai->ai_canonname)
*length += l;
return (uint8_t*) p + l;
}
static int send_addrinfo_reply(
int out_fd,
unsigned id,
int ret,
int _errno,
int _h_errno) {
AddrInfoResponse resp = {
};
union {
} buffer;
void *p = &buffer;
struct addrinfo *k;
if (!p) {
return -ENOBUFS;
}
}
}
if (ai)
iov[1] = (struct iovec) { .iov_base = &buffer, .iov_len = resp.header.length - sizeof(AddrInfoResponse) };
return -errno;
return 0;
}
static int send_nameinfo_reply(
int out_fd,
unsigned id,
int ret,
const char *host,
const char *serv,
int _errno,
int _h_errno) {
NameInfoResponse resp = {
};
return -errno;
return 0;
}
case REQUEST_ADDRINFO: {
int ret;
service = ai_req->service_len ? (const char*) ai_req + sizeof(AddrInfoRequest) + ai_req->node_len : NULL;
ret = getaddrinfo(
&result);
/* send_addrinfo_reply() frees result */
}
case REQUEST_NAMEINFO: {
union sockaddr_union sa;
int ret;
}
case REQUEST_TERMINATE:
/* Quit */
return -ECONNRESET;
default:
assert_not_reached("Unknown request");
}
return 0;
}
static void* thread_worker(void *p) {
sd_resolve *resolve = p;
/* No signals in this thread please */
/* Assign a pretty name to this thread */
union {
} buf;
if (length < 0) {
continue;
break;
}
if (length == 0)
break;
break;
break;
}
return NULL;
}
unsigned n;
int r;
while (resolve->n_valid_workers < n) {
if (r != 0)
return -r;
resolve->n_valid_workers ++;
}
return 0;
}
static bool resolve_pid_changed(sd_resolve *r) {
assert(r);
/* We don't support people creating a resolver and keeping it
* around after fork(). Let's complain. */
return r->original_pid != getpid();
}
int i, r;
if (!resolve)
return -ENOMEM;
for (i = 0; i < _FD_MAX; i++)
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
return 0;
fail:
return r;
}
sd_resolve *e = NULL;
int r;
if (!ret)
return !!default_resolve;
if (default_resolve) {
return 0;
}
r = sd_resolve_new(&e);
if (r < 0)
return r;
default_resolve = e;
*ret = e;
return 1;
}
return 0;
}
return -ENXIO;
}
sd_resolve_query *q;
unsigned i;
}
if (resolve->default_resolve_ptr)
};
/* Send one termination packet for each worker */
for (i = 0; i < resolve->n_valid_workers; i++)
}
/* Now terminate them and wait until they are gone. */
for (i = 0; i < resolve->n_valid_workers; i++) {
for (;;) {
break;
}
}
/* Close all communication channels */
for (i = 0; i < _FD_MAX; i++)
}
return resolve;
}
if (!resolve)
return NULL;
return NULL;
}
}
}
return 0;
}
sd_resolve_query *q;
if (q)
return q;
return NULL;
}
int r;
assert(q);
q->done = true;
switch (q->type) {
case REQUEST_ADDRINFO:
r = getaddrinfo_done(q);
break;
case REQUEST_NAMEINFO:
r = getnameinfo_done(q);
break;
default:
assert_not_reached("Cannot complete unknown query type");
}
if (q->floating) {
}
return r;
}
size_t l;
assert(p);
assert(*p);
if (*length < sizeof(AddrInfoSerialization))
return -EBADMSG;
memcpy(&s, *p, sizeof(s));
if (*length < l)
return -EBADMSG;
if (!ai)
return -ENOMEM;
if (s.ai_addrlen > 0) {
return -ENOMEM;
}
}
if (s.canonname_len > 0) {
ai->ai_canonname = memdup((const uint8_t*) *p + sizeof(AddrInfoSerialization) + s.ai_addrlen, s.canonname_len);
if (!ai->ai_canonname) {
return -ENOMEM;
}
}
*length -= l;
*p = ((const uint8_t*) *p) + l;
return 0;
}
sd_resolve_query *q;
int r;
return 0;
}
resolve->n_outstanding--;
if (!q)
return 0;
case RESPONSE_ADDRINFO: {
const void *p;
size_t l;
l = length - sizeof(AddrInfoResponse);
while (l > 0 && p) {
r = unserialize_addrinfo(&p, &l, &ai);
if (r < 0) {
q->ret = EAI_SYSTEM;
q->_errno = -r;
q->_h_errno = 0;
freeaddrinfo(q->addrinfo);
break;
}
if (prev)
else
}
return complete_query(resolve, q);
}
case RESPONSE_NAMEINFO: {
if (!q->host) {
q->ret = EAI_MEMORY;
q->_h_errno = 0;
}
}
q->serv = strndup((const char*) ni_resp + sizeof(NameInfoResponse) + ni_resp->hostlen, ni_resp->servlen-1);
if (!q->serv) {
q->ret = EAI_MEMORY;
q->_h_errno = 0;
}
}
return complete_query(resolve, q);
}
default:
return 0;
}
}
union {
} buf;
ssize_t l;
int r;
/* We don't allow recursively invoking sd_resolve_process(). */
if (l < 0) {
return 0;
return -errno;
}
if (l == 0)
return -ECONNREFUSED;
if (r < 0)
return r;
return 1;
}
int r;
return 0;
do {
} while (r == -EINTR);
if (r < 0)
return r;
return sd_resolve_process(resolve);
}
sd_resolve_query *q;
int r;
return -ENOBUFS;
if (r < 0)
return r;
resolve->current_id++;
if (!q)
return -ENOMEM;
q->n_ref = 1;
if (!floating)
*_q = q;
return 0;
}
AddrInfoRequest req = {};
sd_resolve_query *q;
int r;
if (r < 0)
return r;
q->type = REQUEST_ADDRINFO;
q->getaddrinfo_handler = callback;
if (hints) {
req.hints_valid = true;
}
if (node)
if (service)
return -errno;
}
resolve->n_outstanding++;
if (_q)
*_q = q;
return 0;
}
static int getaddrinfo_done(sd_resolve_query* q) {
assert(q);
}
int flags,
void *userdata) {
NameInfoRequest req = {};
sd_resolve_query *q;
int r;
if (r < 0)
return r;
q->type = REQUEST_NAMEINFO;
q->getnameinfo_handler = callback;
return -errno;
}
resolve->n_outstanding++;
if (_q)
*_q = q;
return 0;
}
static int getnameinfo_done(sd_resolve_query *q) {
assert(q);
}
assert_return(q, NULL);
q->n_ref++;
return q;
}
while (ai) {
}
}
static void resolve_query_disconnect(sd_resolve_query *q) {
unsigned i;
assert(q);
if (!q->resolve)
return;
if (q->done) {
}
i = q->id % QUERIES_MAX;
if (!q->floating)
}
static void resolve_query_free(sd_resolve_query *q) {
assert(q);
free(q);
}
if (!q)
return NULL;
q->n_ref--;
if (q->n_ref <= 0)
return NULL;
}
assert_return(q, -EINVAL);
return q->done;
}
void *ret;
assert_return(q, NULL);
return ret;
}
assert_return(q, NULL);
return q->userdata;
}
assert_return(q, NULL);
return q->resolve;
}
int r;
r = sd_resolve_process(resolve);
if (r < 0)
return r;
return 1;
}
int r;
if (event)
else {
if (r < 0)
return r;
}
r = sd_event_add_io(resolve->event, &resolve->event_source, resolve->fds[RESPONSE_RECV_FD], POLLIN, io_callback, resolve);
if (r < 0)
goto fail;
if (r < 0)
goto fail;
return 0;
fail:
return r;
}
return 0;
if (resolve->event_source) {
}
return 1;
}
}