1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-26 03:22:00 +03:00

sd-resolve: drop res_query() API

Querying low-level DNS RRs should be done via resolved now, not via
glibc's awful res_query() API anymore. Let's not introduce an async
wrapper for it hence.
This commit is contained in:
Lennart Poettering 2015-08-27 20:47:13 +02:00
parent e1439a1472
commit 1fac0c31ab
3 changed files with 3 additions and 251 deletions

View File

@ -48,9 +48,6 @@ typedef enum {
RESPONSE_ADDRINFO, RESPONSE_ADDRINFO,
REQUEST_NAMEINFO, REQUEST_NAMEINFO,
RESPONSE_NAMEINFO, RESPONSE_NAMEINFO,
REQUEST_RES_QUERY,
REQUEST_RES_SEARCH,
RESPONSE_RES,
REQUEST_TERMINATE, REQUEST_TERMINATE,
RESPONSE_DIED RESPONSE_DIED
} QueryType; } QueryType;
@ -104,12 +101,10 @@ struct sd_resolve_query {
int _h_errno; int _h_errno;
struct addrinfo *addrinfo; struct addrinfo *addrinfo;
char *serv, *host; char *serv, *host;
unsigned char *answer;
union { union {
sd_resolve_getaddrinfo_handler_t getaddrinfo_handler; sd_resolve_getaddrinfo_handler_t getaddrinfo_handler;
sd_resolve_getnameinfo_handler_t getnameinfo_handler; sd_resolve_getnameinfo_handler_t getnameinfo_handler;
sd_resolve_res_handler_t res_handler;
}; };
void *userdata; void *userdata;
@ -166,33 +161,16 @@ typedef struct NameInfoResponse {
int _h_errno; int _h_errno;
} NameInfoResponse; } NameInfoResponse;
typedef struct ResRequest {
struct RHeader header;
int class;
int type;
size_t dname_len;
} ResRequest;
typedef struct ResResponse {
struct RHeader header;
int ret;
int _errno;
int _h_errno;
} ResResponse;
typedef union Packet { typedef union Packet {
RHeader rheader; RHeader rheader;
AddrInfoRequest addrinfo_request; AddrInfoRequest addrinfo_request;
AddrInfoResponse addrinfo_response; AddrInfoResponse addrinfo_response;
NameInfoRequest nameinfo_request; NameInfoRequest nameinfo_request;
NameInfoResponse nameinfo_response; NameInfoResponse nameinfo_response;
ResRequest res_request;
ResResponse res_response;
} Packet; } Packet;
static int getaddrinfo_done(sd_resolve_query* q); static int getaddrinfo_done(sd_resolve_query* q);
static int getnameinfo_done(sd_resolve_query *q); static int getnameinfo_done(sd_resolve_query *q);
static int res_query_done(sd_resolve_query* q);
static void resolve_query_disconnect(sd_resolve_query *q); static void resolve_query_disconnect(sd_resolve_query *q);
@ -343,38 +321,6 @@ static int send_nameinfo_reply(
return 0; 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 = {
.header.type = RESPONSE_RES,
.header.id = id,
.ret = ret,
._errno = _errno,
._h_errno = _h_errno,
};
struct msghdr mh = {};
struct iovec iov[2];
size_t l;
assert(out_fd >= 0);
l = ret > 0 ? (size_t) ret : 0;
resp.header.length = sizeof(ResResponse) + l;
iov[0] = (struct iovec) { .iov_base = &resp, .iov_len = sizeof(ResResponse) };
iov[1] = (struct iovec) { .iov_base = (void*) answer, .iov_len = l };
mh.msg_iov = iov;
mh.msg_iovlen = ELEMENTSOF(iov);
if (sendmsg(out_fd, &mh, MSG_NOSIGNAL) < 0)
return -errno;
return 0;
}
static int handle_request(int out_fd, const Packet *packet, size_t length) { static int handle_request(int out_fd, const Packet *packet, size_t length) {
const RHeader *req; const RHeader *req;
@ -437,29 +383,6 @@ static int handle_request(int out_fd, const Packet *packet, size_t length) {
errno, h_errno); errno, h_errno);
} }
case REQUEST_RES_QUERY:
case REQUEST_RES_SEARCH: {
const ResRequest *res_req = &packet->res_request;
union {
HEADER header;
uint8_t space[BUFSIZE];
} answer;
const char *dname;
int ret;
assert(length >= sizeof(ResRequest));
assert(length == sizeof(ResRequest) + res_req->dname_len);
dname = (const char *) res_req + sizeof(ResRequest);
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: case REQUEST_TERMINATE:
/* Quit */ /* Quit */
return -ECONNRESET; return -ECONNRESET;
@ -752,11 +675,6 @@ static int complete_query(sd_resolve *resolve, sd_resolve_query *q) {
r = getnameinfo_done(q); r = getnameinfo_done(q);
break; break;
case REQUEST_RES_QUERY:
case REQUEST_RES_SEARCH:
r = res_query_done(q);
break;
default: default:
assert_not_reached("Cannot complete unknown query type"); assert_not_reached("Cannot complete unknown query type");
} }
@ -923,28 +841,6 @@ static int handle_response(sd_resolve *resolve, const Packet *packet, size_t len
return complete_query(resolve, q); return complete_query(resolve, q);
} }
case RESPONSE_RES: {
const ResResponse *res_resp = &packet->res_response;
assert(length >= sizeof(ResResponse));
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) {
q->answer = memdup((const char *)resp + sizeof(ResResponse), res_resp->ret);
if (!q->answer) {
q->ret = -1;
q->_errno = ENOMEM;
q->_h_errno = 0;
}
}
return complete_query(resolve, q);
}
default: default:
return 0; return 0;
} }
@ -1181,79 +1077,6 @@ static int getnameinfo_done(sd_resolve_query *q) {
return q->getnameinfo_handler(q, q->ret, q->host, q->serv, q->userdata); return q->getnameinfo_handler(q, q->ret, q->host, q->serv, q->userdata);
} }
static int resolve_res(
sd_resolve *resolve,
sd_resolve_query **_q,
QueryType qtype,
const char *dname,
int class, int type,
sd_resolve_res_handler_t callback, void *userdata) {
struct msghdr mh = {};
struct iovec iov[2];
ResRequest req = {};
sd_resolve_query *q;
int r;
assert_return(resolve, -EINVAL);
assert_return(dname, -EINVAL);
assert_return(callback, -EINVAL);
assert_return(!resolve_pid_changed(resolve), -ECHILD);
r = alloc_query(resolve, !_q, &q);
if (r < 0)
return r;
q->type = qtype;
q->res_handler = callback;
q->userdata = userdata;
req.dname_len = strlen(dname) + 1;
req.class = class;
req.type = type;
req.header.id = q->id;
req.header.type = qtype;
req.header.length = sizeof(ResRequest) + req.dname_len;
iov[0] = (struct iovec) { .iov_base = &req, .iov_len = sizeof(ResRequest) };
iov[1] = (struct iovec) { .iov_base = (void*) dname, .iov_len = req.dname_len };
mh.msg_iov = iov;
mh.msg_iovlen = 2;
if (sendmsg(resolve->fds[REQUEST_SEND_FD], &mh, MSG_NOSIGNAL) < 0) {
sd_resolve_query_unref(q);
return -errno;
}
resolve->n_outstanding++;
if (_q)
*_q = q;
return 0;
}
_public_ int sd_resolve_res_query(sd_resolve *resolve, sd_resolve_query** q, const char *dname, int class, int type, sd_resolve_res_handler_t callback, void *userdata) {
return resolve_res(resolve, q, REQUEST_RES_QUERY, dname, class, type, callback, userdata);
}
_public_ int sd_resolve_res_search(sd_resolve *resolve, sd_resolve_query** q, const char *dname, int class, int type, sd_resolve_res_handler_t callback, void *userdata) {
return resolve_res(resolve, q, REQUEST_RES_SEARCH, dname, class, type, callback, userdata);
}
static int res_query_done(sd_resolve_query* q) {
assert(q);
assert(q->done);
assert(q->res_handler);
errno = q->_errno;
h_errno = q->_h_errno;
return q->res_handler(q, q->ret, q->answer, q->userdata);
}
_public_ sd_resolve_query* sd_resolve_query_ref(sd_resolve_query *q) { _public_ sd_resolve_query* sd_resolve_query_ref(sd_resolve_query *q) {
assert_return(q, NULL); assert_return(q, NULL);
@ -1310,7 +1133,6 @@ static void resolve_query_free(sd_resolve_query *q) {
resolve_freeaddrinfo(q->addrinfo); resolve_freeaddrinfo(q->addrinfo);
free(q->host); free(q->host);
free(q->serv); free(q->serv);
free(q->answer);
free(q); free(q);
} }

View File

@ -67,59 +67,8 @@ static int getnameinfo_handler(sd_resolve_query *q, int ret, const char *host, c
return 0; return 0;
} }
static int res_handler(sd_resolve_query *q, int ret, unsigned char *answer, void *userdata) {
int qdcount, ancount, len;
const unsigned char *pos = answer + sizeof(HEADER);
unsigned char *end = answer + ret;
HEADER *head = (HEADER *) answer;
char name[256];
assert_se(q);
if (ret < 0) {
log_error("res_query() error: %s %i", strerror(errno), errno);
return 0;
}
if (ret == 0) {
log_error("No reply for SRV lookup");
return 0;
}
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(answer, end, pos, name, 255)) >= 0) {
assert_se(len >= 0);
pos += len + QFIXEDSZ;
}
/* Parse the answers */
while (ancount-- > 0 && (len = dn_expand(answer, end, pos, name, 255)) >= 0) {
/* Ignore the initial string */
uint16_t pref, weight, port;
assert_se(len >= 0);
pos += len;
/* Ignore type, ttl, class and dlen */
pos += 10;
GETSHORT(pref, pos);
GETSHORT(weight, pos);
GETSHORT(port, pos);
len = dn_expand(answer, end, pos, name, 255);
printf("\tpreference: %2d weight: %2d port: %d host: %s\n",
pref, weight, port, name);
pos += len;
}
return 0;
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
_cleanup_resolve_query_unref_ sd_resolve_query *q1 = NULL, *q2 = NULL, *q3 = NULL; _cleanup_resolve_query_unref_ sd_resolve_query *q1 = NULL, *q2 = NULL;
_cleanup_resolve_unref_ sd_resolve *resolve = NULL; _cleanup_resolve_unref_ sd_resolve *resolve = NULL;
int r = 0; int r = 0;
@ -150,15 +99,9 @@ int main(int argc, char *argv[]) {
if (r < 0) if (r < 0)
log_error_errno(r, "sd_resolve_getnameinfo(): %m"); log_error_errno(r, "sd_resolve_getnameinfo(): %m");
/* Make a res_query() call */ /* Wait until the two queries are completed */
r = sd_resolve_res_query(resolve, &q3, "_xmpp-client._tcp.gmail.com", C_IN, T_SRV, res_handler, NULL);
if (r < 0)
log_error_errno(r, "sd_resolve_res_query(): %m");
/* Wait until the three queries are completed */
while (sd_resolve_query_is_done(q1) == 0 || while (sd_resolve_query_is_done(q1) == 0 ||
sd_resolve_query_is_done(q2) == 0 || sd_resolve_query_is_done(q2) == 0) {
sd_resolve_query_is_done(q3) == 0) {
r = sd_resolve_wait(resolve, (uint64_t) -1); r = sd_resolve_wait(resolve, (uint64_t) -1);
if (r < 0) { if (r < 0) {

View File

@ -40,7 +40,6 @@ typedef struct sd_resolve_query sd_resolve_query;
/* A callback on completion */ /* A callback on completion */
typedef int (*sd_resolve_getaddrinfo_handler_t)(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata); typedef int (*sd_resolve_getaddrinfo_handler_t)(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata);
typedef int (*sd_resolve_getnameinfo_handler_t)(sd_resolve_query *q, int ret, const char *host, const char *serv, void *userdata); typedef int (*sd_resolve_getnameinfo_handler_t)(sd_resolve_query *q, int ret, const char *host, const char *serv, void *userdata);
typedef int (*sd_resolve_res_handler_t)(sd_resolve_query* q, int ret, unsigned char *answer, void *userdata);
enum { enum {
SD_RESOLVE_GET_HOST = 1ULL, SD_RESOLVE_GET_HOST = 1ULL,
@ -99,18 +98,6 @@ int sd_resolve_getaddrinfo(sd_resolve *resolve, sd_resolve_query **q, const char
* if you want to query the hostname (resp. the service name). */ * if you want to query the hostname (resp. the service name). */
int sd_resolve_getnameinfo(sd_resolve *resolve, sd_resolve_query **q, const struct sockaddr *sa, socklen_t salen, int flags, uint64_t get, sd_resolve_getnameinfo_handler_t callback, void *userdata); int sd_resolve_getnameinfo(sd_resolve *resolve, sd_resolve_query **q, const struct sockaddr *sa, socklen_t salen, int flags, uint64_t get, sd_resolve_getnameinfo_handler_t callback, void *userdata);
/* Issue a resolver query on the specified session. The arguments are
* compatible with those 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(). */
int sd_resolve_res_query(sd_resolve *resolve, sd_resolve_query **q, const char *dname, int clazz, int type, sd_resolve_res_handler_t callback, void *userdata);
/* Issue a resolver query on the specified session. The arguments are
* compatible with those 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(). */
int sd_resolve_res_search(sd_resolve *resolve, sd_resolve_query **q, const char *dname, int clazz, int type, sd_resolve_res_handler_t callback, void *userdata);
sd_resolve_query *sd_resolve_query_ref(sd_resolve_query* q); sd_resolve_query *sd_resolve_query_ref(sd_resolve_query* q);
sd_resolve_query *sd_resolve_query_unref(sd_resolve_query* q); sd_resolve_query *sd_resolve_query_unref(sd_resolve_query* q);