sd-resolve.c revision 151b9b9662a90455262ce575a8a8ae74bf4ff336
/*-*- 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 <assert.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <pwd.h>
#include <resolv.h>
#include <dirent.h>
#include <sys/resource.h>
#include <stdint.h>
#include <pthread.h>
#include "util.h"
#include "list.h"
#include "socket-util.h"
#include "sd-resolve.h"
#define WORKERS_MIN 1
#define WORKERS_MAX 16
#define QUERIES_MAX 256
#define BUFSIZE 10240
typedef enum {
} QueryType;
enum {
};
struct sd_resolve {
bool dead:1;
unsigned n_valid_workers;
unsigned current_id, current_index;
unsigned n_queries;
unsigned n_done;
};
struct sd_resolve_query {
bool done:1;
unsigned id;
int ret;
int _errno;
int _h_errno;
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 struct ResRequest {
int class;
int type;
} ResRequest;
typedef struct ResResponse {
int ret;
int _errno;
int _h_errno;
} ResResponse;
typedef union Packet {
} Packet;
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;
}
static int send_res_reply(int out_fd, unsigned id, const unsigned char *answer, int ret, int _errno, int _h_errno) {
ResResponse resp = {};
size_t l;
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_RES_QUERY:
case REQUEST_RES_SEARCH: {
union {
} answer;
const char *dname;
int ret;
else
}
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;
if (n < WORKERS_MIN)
n = WORKERS_MIN;
if (n > WORKERS_MAX)
n = WORKERS_MAX;
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:
if (resolve)
return r;
}
unsigned i;
};
/* 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 NULL;
}
}
}
return 0;
}
sd_resolve_query *q;
if (q)
return q;
return NULL;
}
static void complete_query(sd_resolve_query *q) {
assert(q);
q->done = true;
}
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;
}
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
}
complete_query(q);
break;
}
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;
}
}
complete_query(q);
break;
}
case RESPONSE_RES: {
if (!q->serv) {
q->ret = -1;
q->_h_errno = 0;
}
}
complete_query(q);
break;
}
default:
;
}
return 0;
}
int n_processed = 0, r;
for (;;) {
ssize_t l;
union {
} buf;
if (l < 0) {
return n_processed;
return -errno;
}
if (l == 0)
return -ECONNREFUSED;
if (r < 0)
return r;
n_processed++;
}
}
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_index++;
resolve->current_id++;
}
if (!q)
return -ENOMEM;
*_q = q;
return 0;
}
const char *node,
const char *service,
AddrInfoRequest req = {};
sd_resolve_query *q;
int r;
r = alloc_query(resolve, &q);
if (r < 0)
return r;
if (hints) {
req.hints_valid = true;
}
if (node)
if (service)
return -errno;
}
*_q = q;
return 0;
}
int ret;
if (!q) {
return EAI_SYSTEM;
}
if (q->type != REQUEST_ADDRINFO) {
return EAI_SYSTEM;
}
if (resolve_pid_changed(q->resolve)) {
return EAI_SYSTEM;
}
if (!q->done)
return EAI_AGAIN;
if (ret_res) {
}
if (ret != 0) {
}
return ret;
}
int flags,
NameInfoRequest req = {};
sd_resolve_query *q;
int r;
r = alloc_query(resolve, &q);
if (r < 0)
return r;
return -errno;
}
*_q = q;
return 0;
}
int ret;
if (!q) {
return EAI_SYSTEM;
}
if (q->type != REQUEST_NAMEINFO) {
return EAI_SYSTEM;
}
if (resolve_pid_changed(q->resolve)) {
return EAI_SYSTEM;
}
if (!q->done)
return EAI_AGAIN;
if (ret_host) {
}
if (ret_serv) {
}
if (ret != 0) {
}
return ret;
}
static int resolve_res(
const char *dname,
ResRequest req = {};
sd_resolve_query *q;
int r;
r = alloc_query(resolve, &q);
if (r < 0)
return r;
return -errno;
}
*_q = q;
return 0;
}
_public_ int sd_resolve_res_query(sd_resolve *resolve, sd_resolve_query** q, const char *dname, int class, int type) {
}
_public_ int sd_resolve_res_search(sd_resolve *resolve, sd_resolve_query** q, const char *dname, int class, int type) {
}
int ret;
assert_return(q, -EINVAL);
if (!q->done)
return -EAGAIN;
if (ret != 0) {
}
}
assert_return(q, -EINVAL);
}
}
int i;
if (!q)
return 0;
if (q->done) {
}
i = q->id % QUERIES_MAX;
free(q);
return 0;
}
while (ai) {
}
}
assert_return(q, -EINVAL);
return q->done;
}
void *ret;
assert_return(q, NULL);
return ret;
}
assert_return(q, NULL);
return q->userdata;
}