2 This file is part of libasyncns.
4 Copyright 2005-2008 Lennart Poettering
6 libasyncns is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation, either version 2.1 of the
9 License, or (at your option) any later version.
11 libasyncns is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with libasyncns. If not, see
18 <http://www.gnu.org/licenses/>.
25 /* #undef HAVE_PTHREAD */
31 #include <sys/select.h>
37 #include <sys/types.h>
39 #include <netinet/in.h>
40 #include <arpa/nameser.h>
44 #include <sys/resource.h>
47 #ifdef HAVE_SYS_PRCTL_H
48 #include <sys/prctl.h>
58 #define MSG_NOSIGNAL 0
61 #define MAX_WORKERS 16
62 #define MAX_QUERIES 256
63 #define BUFSIZE (10240)
89 pid_t workers[MAX_WORKERS];
91 pthread_t workers[MAX_WORKERS];
93 unsigned valid_workers;
95 unsigned current_id, current_index;
96 asyncns_query_t* queries[MAX_QUERIES];
98 asyncns_query_t *done_head, *done_tail;
104 struct asyncns_query {
109 asyncns_query_t *done_next, *done_prev;
113 struct addrinfo *addrinfo;
118 typedef struct rheader {
124 typedef struct addrinfo_request {
125 struct rheader header;
131 size_t node_len, service_len;
132 } addrinfo_request_t;
134 typedef struct addrinfo_response {
135 struct rheader header;
139 /* followed by addrinfo_serialization[] */
140 } addrinfo_response_t;
142 typedef struct addrinfo_serialization {
148 size_t canonname_len;
149 /* Followed by ai_addr amd ai_canonname with variable lengths */
150 } addrinfo_serialization_t;
152 typedef struct nameinfo_request {
153 struct rheader header;
155 socklen_t sockaddr_len;
156 int gethost, getserv;
157 } nameinfo_request_t;
159 typedef struct nameinfo_response {
160 struct rheader header;
161 size_t hostlen, servlen;
165 } nameinfo_response_t;
167 typedef struct res_request {
168 struct rheader header;
174 typedef struct res_response {
175 struct rheader header;
181 typedef union packet {
183 addrinfo_request_t addrinfo_request;
184 addrinfo_response_t addrinfo_response;
185 nameinfo_request_t nameinfo_request;
186 nameinfo_response_t nameinfo_response;
187 res_request_t res_request;
188 res_response_t res_response;
193 static char *strndup(const char *s, size_t l) {
201 if (!(n = malloc(a+1)))
214 static int close_allv(const int except_fds[]) {
224 if ((d = opendir("/proc/self/fd"))) {
228 while ((de = readdir(d))) {
234 if (de->d_name[0] == '.')
238 l = strtol(de->d_name, &e, 10);
239 if (errno != 0 || !e || *e) {
247 if ((long) fd != l) {
260 for (i = 0; except_fds[i] >= 0; i++)
261 if (except_fds[i] == fd) {
286 if (getrlimit(RLIMIT_NOFILE, &rl) > 0)
287 maxfd = (int) rl.rlim_max;
289 maxfd = sysconf(_SC_OPEN_MAX);
291 for (fd = 3; fd < maxfd; fd++) {
295 for (i = 0; except_fds[i] >= 0; i++)
296 if (except_fds[i] == fd) {
304 if (close(fd) < 0 && errno != EBADF)
311 static int reset_sigsv(const int except[]) {
315 for (sig = 1; sig < NSIG; sig++) {
327 for (i = 0; except[i] > 0; i++) {
328 if (sig == except[i]) {
339 memset(&sa, 0, sizeof(sa));
340 sa.sa_handler = SIG_DFL;
342 /* On Linux the first two RT signals are reserved by
343 * glibc, and sigaction() will return EINVAL for them. */
344 if ((sigaction(sig, &sa, NULL) < 0))
353 static int ignore_sigsv(const int ignore[]) {
357 for (i = 0; ignore[i] > 0; i++) {
360 memset(&sa, 0, sizeof(sa));
361 sa.sa_handler = SIG_IGN;
363 if ((sigaction(ignore[i], &sa, NULL) < 0))
372 static int fd_nonblock(int fd) {
376 if ((i = fcntl(fd, F_GETFL, 0)) < 0)
382 return fcntl(fd, F_SETFL, i | O_NONBLOCK);
385 static int fd_cloexec(int fd) {
389 if ((v = fcntl(fd, F_GETFD, 0)) < 0)
395 return fcntl(fd, F_SETFD, v | FD_CLOEXEC);
398 static int send_died(int out_fd) {
402 memset(&rh, 0, sizeof(rh));
403 rh.type = RESPONSE_DIED;
405 rh.length = sizeof(rh);
407 return send(out_fd, &rh, rh.length, MSG_NOSIGNAL);
410 static void *serialize_addrinfo(void *p, const struct addrinfo *ai, size_t *length, size_t maxlength) {
411 addrinfo_serialization_t s;
416 assert(*length <= maxlength);
418 cnl = (ai->ai_canonname ? strlen(ai->ai_canonname)+1 : 0);
419 l = sizeof(addrinfo_serialization_t) + ai->ai_addrlen + cnl;
421 if (*length + l > maxlength)
424 s.ai_flags = ai->ai_flags;
425 s.ai_family = ai->ai_family;
426 s.ai_socktype = ai->ai_socktype;
427 s.ai_protocol = ai->ai_protocol;
428 s.ai_addrlen = ai->ai_addrlen;
429 s.canonname_len = cnl;
431 memcpy((uint8_t*) p, &s, sizeof(addrinfo_serialization_t));
432 memcpy((uint8_t*) p + sizeof(addrinfo_serialization_t), ai->ai_addr, ai->ai_addrlen);
434 if (ai->ai_canonname)
435 strcpy((char*) p + sizeof(addrinfo_serialization_t) + ai->ai_addrlen, ai->ai_canonname);
438 return (uint8_t*) p + l;
441 static int send_addrinfo_reply(int out_fd, unsigned id, int ret, struct addrinfo *ai, int _errno, int _h_errno) {
442 addrinfo_response_t data[BUFSIZE/sizeof(addrinfo_response_t) + 1];
443 addrinfo_response_t *resp = data;
446 memset(data, 0, sizeof(data));
447 resp->header.type = RESPONSE_ADDRINFO;
448 resp->header.id = id;
449 resp->header.length = sizeof(addrinfo_response_t);
451 resp->_errno = _errno;
452 resp->_h_errno = _h_errno;
454 if (ret == 0 && ai) {
458 for (k = ai; k; k = k->ai_next) {
460 if (!(p = serialize_addrinfo(p, k, &resp->header.length, (char*) data + BUFSIZE - (char*) p))) {
461 resp->ret = EAI_MEMORY;
470 return send(out_fd, resp, resp->header.length, MSG_NOSIGNAL);
473 static int send_nameinfo_reply(int out_fd, unsigned id, int ret, const char *host, const char *serv, int _errno, int _h_errno) {
474 nameinfo_response_t data[BUFSIZE/sizeof(nameinfo_response_t) + 1];
476 nameinfo_response_t *resp = data;
480 sl = serv ? strlen(serv)+1 : 0;
481 hl = host ? strlen(host)+1 : 0;
483 memset(data, 0, sizeof(data));
484 resp->header.type = RESPONSE_NAMEINFO;
485 resp->header.id = id;
486 resp->header.length = sizeof(nameinfo_response_t) + hl + sl;
488 resp->_errno = _errno;
489 resp->_h_errno = _h_errno;
493 assert(sizeof(data) >= resp->header.length);
496 memcpy((uint8_t *)data + sizeof(nameinfo_response_t), host, hl);
499 memcpy((uint8_t *)data + sizeof(nameinfo_response_t) + hl, serv, sl);
501 return send(out_fd, resp, resp->header.length, MSG_NOSIGNAL);
504 static int send_res_reply(int out_fd, unsigned id, const unsigned char *answer, int ret, int _errno, int _h_errno) {
505 res_response_t data[BUFSIZE/sizeof(res_response_t) + 1];
506 res_response_t *resp = data;
510 memset(data, 0, sizeof(data));
511 resp->header.type = RESPONSE_RES;
512 resp->header.id = id;
513 resp->header.length = sizeof(res_response_t) + (ret < 0 ? 0 : ret);
515 resp->_errno = _errno;
516 resp->_h_errno = _h_errno;
518 assert(sizeof(data) >= resp->header.length);
521 memcpy((uint8_t *)data + sizeof(res_response_t), answer, ret);
523 return send(out_fd, resp, resp->header.length, MSG_NOSIGNAL);
526 static int handle_request(int out_fd, const packet_t *packet, size_t length) {
527 const rheader_t *req;
530 req = &packet->rheader;
532 assert(length >= sizeof(rheader_t));
533 assert(length == req->length);
537 case REQUEST_ADDRINFO: {
538 struct addrinfo ai, *result = NULL;
539 const addrinfo_request_t *ai_req = &packet->addrinfo_request;
540 const char *node, *service;
543 assert(length >= sizeof(addrinfo_request_t));
544 assert(length == sizeof(addrinfo_request_t) + ai_req->node_len + ai_req->service_len);
546 memset(&ai, 0, sizeof(ai));
547 ai.ai_flags = ai_req->ai_flags;
548 ai.ai_family = ai_req->ai_family;
549 ai.ai_socktype = ai_req->ai_socktype;
550 ai.ai_protocol = ai_req->ai_protocol;
552 node = ai_req->node_len ? (const char*) ai_req + sizeof(addrinfo_request_t) : NULL;
553 service = ai_req->service_len ? (const char*) ai_req + sizeof(addrinfo_request_t) + ai_req->node_len : NULL;
555 ret = getaddrinfo(node, service,
556 ai_req->hints_is_null ? NULL : &ai,
559 /* send_addrinfo_reply() frees result */
560 return send_addrinfo_reply(out_fd, req->id, ret, result, errno, h_errno);
563 case REQUEST_NAMEINFO: {
565 const nameinfo_request_t *ni_req = &packet->nameinfo_request;
566 char hostbuf[NI_MAXHOST], servbuf[NI_MAXSERV];
567 struct sockaddr_storage sa;
569 assert(length >= sizeof(nameinfo_request_t));
570 assert(length == sizeof(nameinfo_request_t) + ni_req->sockaddr_len);
572 memcpy(&sa, (const uint8_t *) ni_req + sizeof(nameinfo_request_t), ni_req->sockaddr_len);
574 ret = getnameinfo((struct sockaddr *)&sa, ni_req->sockaddr_len,
575 ni_req->gethost ? hostbuf : NULL, ni_req->gethost ? sizeof(hostbuf) : 0,
576 ni_req->getserv ? servbuf : NULL, ni_req->getserv ? sizeof(servbuf) : 0,
579 return send_nameinfo_reply(out_fd, req->id, ret,
580 ret == 0 && ni_req->gethost ? hostbuf : NULL,
581 ret == 0 && ni_req->getserv ? servbuf : NULL,
585 case REQUEST_RES_QUERY:
586 case REQUEST_RES_SEARCH: {
588 HEADER answer[BUFSIZE/sizeof(HEADER) + 1];
589 const res_request_t *res_req = &packet->res_request;
592 assert(length >= sizeof(res_request_t));
593 assert(length == sizeof(res_request_t) + res_req->dname_len);
595 dname = (const char *) req + sizeof(res_request_t);
597 if (req->type == REQUEST_RES_QUERY)
598 ret = res_query(dname, res_req->class, res_req->type, (unsigned char *) answer, BUFSIZE);
600 ret = res_search(dname, res_req->class, res_req->type, (unsigned char *) answer, BUFSIZE);
602 return send_res_reply(out_fd, req->id, (unsigned char *) answer, ret, errno, h_errno);
605 case REQUEST_TERMINATE:
618 static int process_worker(int in_fd, int out_fd) {
619 int have_death_sig = 0;
623 const int ignore_sigs[] = {
639 if (open("/dev/null", O_RDONLY) != 0)
642 if (open("/dev/null", O_WRONLY) != 1)
645 if (open("/dev/null", O_WRONLY) != 2)
651 if (geteuid() == 0) {
655 if ((pw = getpwnam("nobody"))) {
656 #ifdef HAVE_SETRESUID
657 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
659 r = setreuid(pw->pw_uid, pw->pw_uid);
661 if ((r = setuid(pw->pw_uid)) >= 0)
662 r = seteuid(pw->pw_uid);
669 if (reset_sigsv(ignore_sigs) < 0)
672 if (ignore_sigsv(ignore_sigs) < 0)
675 good_fds[0] = in_fd; good_fds[1] = out_fd; good_fds[2] = -1;
676 if (close_allv(good_fds) < 0)
679 #ifdef PR_SET_PDEATHSIG
680 if (prctl(PR_SET_PDEATHSIG, SIGTERM) >= 0)
687 while (getppid() > 1) { /* if the parent PID is 1 our parent process died. */
688 packet_t buf[BUFSIZE/sizeof(packet_t) + 1];
691 if (!have_death_sig) {
693 struct timeval tv = { 0, 500000 };
698 if (select(in_fd+1, &fds, NULL, NULL, &tv) < 0)
705 if ((length = recv(in_fd, buf, sizeof(buf), 0)) <= 0) {
708 (errno == EAGAIN || errno == EINTR))
714 if (handle_request(out_fd, buf, (size_t) length) < 0)
728 static void* thread_worker(void *p) {
729 asyncns_t *asyncns = p;
732 /* No signals in this thread please */
733 sigfillset(&fullset);
734 pthread_sigmask(SIG_BLOCK, &fullset, NULL);
736 while (!asyncns->dead) {
737 packet_t buf[BUFSIZE/sizeof(packet_t) + 1];
740 if ((length = recv(asyncns->fds[REQUEST_RECV_FD], buf, sizeof(buf), 0)) <= 0) {
743 (errno == EAGAIN || errno == EINTR))
752 if (handle_request(asyncns->fds[RESPONSE_SEND_FD], buf, (size_t) length) < 0)
756 send_died(asyncns->fds[RESPONSE_SEND_FD]);
763 asyncns_t* asyncns_new(unsigned n_proc) {
764 asyncns_t *asyncns = NULL;
768 if (n_proc > MAX_WORKERS)
769 n_proc = MAX_WORKERS;
771 if (!(asyncns = malloc(sizeof(asyncns_t)))) {
777 asyncns->valid_workers = 0;
779 for (i = 0; i < MESSAGE_FD_MAX; i++)
780 asyncns->fds[i] = -1;
782 memset(asyncns->queries, 0, sizeof(asyncns->queries));
784 if (socketpair(PF_UNIX, SOCK_DGRAM, 0, asyncns->fds) < 0 ||
785 socketpair(PF_UNIX, SOCK_DGRAM, 0, asyncns->fds+2) < 0)
788 for (i = 0; i < MESSAGE_FD_MAX; i++)
789 fd_cloexec(asyncns->fds[i]);
791 for (asyncns->valid_workers = 0; asyncns->valid_workers < n_proc; asyncns->valid_workers++) {
794 if ((asyncns->workers[asyncns->valid_workers] = fork()) < 0)
796 else if (asyncns->workers[asyncns->valid_workers] == 0) {
799 close(asyncns->fds[REQUEST_SEND_FD]);
800 close(asyncns->fds[RESPONSE_RECV_FD]);
801 ret = process_worker(asyncns->fds[REQUEST_RECV_FD], asyncns->fds[RESPONSE_SEND_FD]);
802 close(asyncns->fds[REQUEST_RECV_FD]);
803 close(asyncns->fds[RESPONSE_SEND_FD]);
809 if ((r = pthread_create(&asyncns->workers[asyncns->valid_workers], NULL, thread_worker, asyncns)) != 0) {
817 close(asyncns->fds[REQUEST_RECV_FD]);
818 close(asyncns->fds[RESPONSE_SEND_FD]);
819 asyncns->fds[REQUEST_RECV_FD] = asyncns->fds[RESPONSE_SEND_FD] = -1;
822 asyncns->current_index = asyncns->current_id = 0;
823 asyncns->done_head = asyncns->done_tail = NULL;
824 asyncns->n_queries = 0;
826 fd_nonblock(asyncns->fds[RESPONSE_RECV_FD]);
832 asyncns_free(asyncns);
837 void asyncns_free(asyncns_t *asyncns) {
839 int saved_errno = errno;
846 if (asyncns->fds[REQUEST_SEND_FD] >= 0) {
849 memset(&req, 0, sizeof(req));
850 req.type = REQUEST_TERMINATE;
851 req.length = sizeof(req);
854 /* Send one termination packet for each worker */
855 for (p = 0; p < asyncns->valid_workers; p++)
856 send(asyncns->fds[REQUEST_SEND_FD], &req, req.length, MSG_NOSIGNAL);
859 /* Now terminate them and wait until they are gone. */
860 for (p = 0; p < asyncns->valid_workers; p++) {
862 kill(asyncns->workers[p], SIGTERM);
864 if (waitpid(asyncns->workers[p], NULL, 0) >= 0 || errno != EINTR)
869 if (pthread_join(asyncns->workers[p], NULL) != EINTR)
875 /* Close all communication channels */
876 for (i = 0; i < MESSAGE_FD_MAX; i++)
877 if (asyncns->fds[i] >= 0)
878 close(asyncns->fds[i]);
880 for (p = 0; p < MAX_QUERIES; p++)
881 if (asyncns->queries[p])
882 asyncns_cancel(asyncns, asyncns->queries[p]);
889 int asyncns_fd(asyncns_t *asyncns) {
892 return asyncns->fds[RESPONSE_RECV_FD];
895 static asyncns_query_t *lookup_query(asyncns_t *asyncns, unsigned id) {
899 if ((q = asyncns->queries[id % MAX_QUERIES]))
906 static void complete_query(asyncns_t *asyncns, asyncns_query_t *q) {
913 if ((q->done_prev = asyncns->done_tail))
914 asyncns->done_tail->done_next = q;
916 asyncns->done_head = q;
918 asyncns->done_tail = q;
922 static const void *unserialize_addrinfo(const void *p, struct addrinfo **ret_ai, size_t *length) {
923 addrinfo_serialization_t s;
930 if (*length < sizeof(addrinfo_serialization_t))
933 memcpy(&s, p, sizeof(s));
935 l = sizeof(addrinfo_serialization_t) + s.ai_addrlen + s.canonname_len;
939 if (!(ai = malloc(sizeof(struct addrinfo))))
943 ai->ai_canonname = NULL;
946 if (s.ai_addrlen && !(ai->ai_addr = malloc(s.ai_addrlen)))
949 if (s.canonname_len && !(ai->ai_canonname = malloc(s.canonname_len)))
952 ai->ai_flags = s.ai_flags;
953 ai->ai_family = s.ai_family;
954 ai->ai_socktype = s.ai_socktype;
955 ai->ai_protocol = s.ai_protocol;
956 ai->ai_addrlen = s.ai_addrlen;
959 memcpy(ai->ai_addr, (const uint8_t*) p + sizeof(addrinfo_serialization_t), s.ai_addrlen);
961 if (ai->ai_canonname)
962 memcpy(ai->ai_canonname, (const uint8_t*) p + sizeof(addrinfo_serialization_t) + s.ai_addrlen, s.canonname_len);
967 return (const uint8_t*) p + l;
972 asyncns_freeaddrinfo(ai);
977 static int handle_response(asyncns_t *asyncns, const packet_t *packet, size_t length) {
978 const rheader_t *resp;
983 resp = &packet->rheader;
985 assert(length >= sizeof(rheader_t));
986 assert(length == resp->length);
988 if (resp->type == RESPONSE_DIED) {
993 if (!(q = lookup_query(asyncns, resp->id)))
996 switch (resp->type) {
997 case RESPONSE_ADDRINFO: {
998 const addrinfo_response_t *ai_resp = &packet->addrinfo_response;
1001 struct addrinfo *prev = NULL;
1003 assert(length >= sizeof(addrinfo_response_t));
1004 assert(q->type == REQUEST_ADDRINFO);
1006 q->ret = ai_resp->ret;
1007 q->_errno = ai_resp->_errno;
1008 q->_h_errno = ai_resp->_h_errno;
1009 l = length - sizeof(addrinfo_response_t);
1010 p = (const uint8_t*) resp + sizeof(addrinfo_response_t);
1012 while (l > 0 && p) {
1013 struct addrinfo *ai = NULL;
1014 p = unserialize_addrinfo(p, &ai, &l);
1017 q->ret = EAI_MEMORY;
1029 complete_query(asyncns, q);
1033 case RESPONSE_NAMEINFO: {
1034 const nameinfo_response_t *ni_resp = &packet->nameinfo_response;
1036 assert(length >= sizeof(nameinfo_response_t));
1037 assert(q->type == REQUEST_NAMEINFO);
1039 q->ret = ni_resp->ret;
1040 q->_errno = ni_resp->_errno;
1041 q->_h_errno = ni_resp->_h_errno;
1043 if (ni_resp->hostlen)
1044 if (!(q->host = strndup((const char*) ni_resp + sizeof(nameinfo_response_t), ni_resp->hostlen-1)))
1045 q->ret = EAI_MEMORY;
1047 if (ni_resp->servlen)
1048 if (!(q->serv = strndup((const char*) ni_resp + sizeof(nameinfo_response_t) + ni_resp->hostlen, ni_resp->servlen-1)))
1049 q->ret = EAI_MEMORY;
1051 complete_query(asyncns, q);
1055 case RESPONSE_RES: {
1056 const res_response_t *res_resp = &packet->res_response;
1058 assert(length >= sizeof(res_response_t));
1059 assert(q->type == REQUEST_RES_QUERY || q->type == REQUEST_RES_SEARCH);
1061 q->ret = res_resp->ret;
1062 q->_errno = res_resp->_errno;
1063 q->_h_errno = res_resp->_h_errno;
1065 if (res_resp->ret >= 0) {
1066 if (!(q->serv = malloc(res_resp->ret))) {
1070 memcpy(q->serv, (const char *)resp + sizeof(res_response_t), res_resp->ret);
1073 complete_query(asyncns, q);
1084 int asyncns_wait(asyncns_t *asyncns, int block) {
1089 packet_t buf[BUFSIZE/sizeof(packet_t) + 1];
1092 if (asyncns->dead) {
1097 if (((l = recv(asyncns->fds[RESPONSE_RECV_FD], buf, sizeof(buf), 0)) < 0)) {
1100 if (errno != EAGAIN)
1103 if (!block || handled)
1107 FD_SET(asyncns->fds[RESPONSE_RECV_FD], &fds);
1109 if (select(asyncns->fds[RESPONSE_RECV_FD]+1, &fds, NULL, NULL, NULL) < 0)
1115 if (handle_response(asyncns, buf, (size_t) l) < 0)
1122 static asyncns_query_t *alloc_query(asyncns_t *asyncns) {
1126 if (asyncns->n_queries >= MAX_QUERIES) {
1131 while (asyncns->queries[asyncns->current_index]) {
1133 asyncns->current_index++;
1134 asyncns->current_id++;
1136 while (asyncns->current_index >= MAX_QUERIES)
1137 asyncns->current_index -= MAX_QUERIES;
1140 if (!(q = asyncns->queries[asyncns->current_index] = malloc(sizeof(asyncns_query_t)))) {
1145 asyncns->n_queries++;
1147 q->asyncns = asyncns;
1149 q->id = asyncns->current_id;
1150 q->done_next = q->done_prev = NULL;
1156 q->host = q->serv = NULL;
1161 asyncns_query_t* asyncns_getaddrinfo(asyncns_t *asyncns, const char *node, const char *service, const struct addrinfo *hints) {
1162 addrinfo_request_t data[BUFSIZE/sizeof(addrinfo_request_t) + 1];
1163 addrinfo_request_t *req = data;
1166 assert(node || service);
1168 if (asyncns->dead) {
1173 if (!(q = alloc_query(asyncns)))
1176 memset(req, 0, sizeof(addrinfo_request_t));
1178 req->node_len = node ? strlen(node)+1 : 0;
1179 req->service_len = service ? strlen(service)+1 : 0;
1181 req->header.id = q->id;
1182 req->header.type = q->type = REQUEST_ADDRINFO;
1183 req->header.length = sizeof(addrinfo_request_t) + req->node_len + req->service_len;
1185 if (req->header.length > BUFSIZE) {
1190 if (!(req->hints_is_null = !hints)) {
1191 req->ai_flags = hints->ai_flags;
1192 req->ai_family = hints->ai_family;
1193 req->ai_socktype = hints->ai_socktype;
1194 req->ai_protocol = hints->ai_protocol;
1198 strcpy((char*) req + sizeof(addrinfo_request_t), node);
1201 strcpy((char*) req + sizeof(addrinfo_request_t) + req->node_len, service);
1203 if (send(asyncns->fds[REQUEST_SEND_FD], req, req->header.length, MSG_NOSIGNAL) < 0)
1210 asyncns_cancel(asyncns, q);
1215 int asyncns_getaddrinfo_done(asyncns_t *asyncns, asyncns_query_t* q, struct addrinfo **ret_res) {
1219 assert(q->asyncns == asyncns);
1220 assert(q->type == REQUEST_ADDRINFO);
1222 if (asyncns->dead) {
1230 *ret_res = q->addrinfo;
1235 if (ret == EAI_SYSTEM)
1239 h_errno = q->_h_errno;
1241 asyncns_cancel(asyncns, q);
1246 asyncns_query_t* asyncns_getnameinfo(asyncns_t *asyncns, const struct sockaddr *sa, socklen_t salen, int flags, int gethost, int getserv) {
1247 nameinfo_request_t data[BUFSIZE/sizeof(nameinfo_request_t) + 1];
1248 nameinfo_request_t *req = data;
1255 if (asyncns->dead) {
1260 if (!(q = alloc_query(asyncns)))
1263 memset(req, 0, sizeof(nameinfo_request_t));
1265 req->header.id = q->id;
1266 req->header.type = q->type = REQUEST_NAMEINFO;
1267 req->header.length = sizeof(nameinfo_request_t) + salen;
1269 if (req->header.length > BUFSIZE) {
1275 req->sockaddr_len = salen;
1276 req->gethost = gethost;
1277 req->getserv = getserv;
1279 memcpy((uint8_t*) req + sizeof(nameinfo_request_t), sa, salen);
1281 if (send(asyncns->fds[REQUEST_SEND_FD], req, req->header.length, MSG_NOSIGNAL) < 0)
1288 asyncns_cancel(asyncns, q);
1293 int asyncns_getnameinfo_done(asyncns_t *asyncns, asyncns_query_t* q, char *ret_host, size_t hostlen, char *ret_serv, size_t servlen) {
1297 assert(q->asyncns == asyncns);
1298 assert(q->type == REQUEST_NAMEINFO);
1299 assert(!ret_host || hostlen);
1300 assert(!ret_serv || servlen);
1302 if (asyncns->dead) {
1310 if (ret_host && q->host) {
1311 strncpy(ret_host, q->host, hostlen);
1312 ret_host[hostlen-1] = 0;
1315 if (ret_serv && q->serv) {
1316 strncpy(ret_serv, q->serv, servlen);
1317 ret_serv[servlen-1] = 0;
1322 if (ret == EAI_SYSTEM)
1326 h_errno = q->_h_errno;
1328 asyncns_cancel(asyncns, q);
1333 static asyncns_query_t * asyncns_res(asyncns_t *asyncns, query_type_t qtype, const char *dname, int class, int type) {
1334 res_request_t data[BUFSIZE/sizeof(res_request_t) + 1];
1335 res_request_t *req = data;
1341 if (asyncns->dead) {
1346 if (!(q = alloc_query(asyncns)))
1349 memset(req, 0, sizeof(res_request_t));
1351 req->dname_len = strlen(dname) + 1;
1353 req->header.id = q->id;
1354 req->header.type = q->type = qtype;
1355 req->header.length = sizeof(res_request_t) + req->dname_len;
1357 if (req->header.length > BUFSIZE) {
1365 strcpy((char*) req + sizeof(res_request_t), dname);
1367 if (send(asyncns->fds[REQUEST_SEND_FD], req, req->header.length, MSG_NOSIGNAL) < 0)
1374 asyncns_cancel(asyncns, q);
1379 asyncns_query_t* asyncns_res_query(asyncns_t *asyncns, const char *dname, int class, int type) {
1380 return asyncns_res(asyncns, REQUEST_RES_QUERY, dname, class, type);
1383 asyncns_query_t* asyncns_res_search(asyncns_t *asyncns, const char *dname, int class, int type) {
1384 return asyncns_res(asyncns, REQUEST_RES_SEARCH, dname, class, type);
1387 int asyncns_res_done(asyncns_t *asyncns, asyncns_query_t* q, unsigned char **answer) {
1391 assert(q->asyncns == asyncns);
1392 assert(q->type == REQUEST_RES_QUERY || q->type == REQUEST_RES_SEARCH);
1395 if (asyncns->dead) {
1405 *answer = (unsigned char *)q->serv;
1412 h_errno = q->_h_errno;
1415 asyncns_cancel(asyncns, q);
1417 return ret < 0 ? -errno : ret;
1420 asyncns_query_t* asyncns_getnext(asyncns_t *asyncns) {
1422 return asyncns->done_head;
1425 int asyncns_getnqueries(asyncns_t *asyncns) {
1427 return asyncns->n_queries;
1430 void asyncns_cancel(asyncns_t *asyncns, asyncns_query_t* q) {
1432 int saved_errno = errno;
1436 assert(q->asyncns == asyncns);
1437 assert(asyncns->n_queries > 0);
1442 q->done_prev->done_next = q->done_next;
1444 asyncns->done_head = q->done_next;
1447 q->done_next->done_prev = q->done_prev;
1449 asyncns->done_tail = q->done_prev;
1452 i = q->id % MAX_QUERIES;
1453 assert(asyncns->queries[i] == q);
1454 asyncns->queries[i] = NULL;
1456 asyncns_freeaddrinfo(q->addrinfo);
1460 asyncns->n_queries--;
1463 errno = saved_errno;
1466 void asyncns_freeaddrinfo(struct addrinfo *ai) {
1467 int saved_errno = errno;
1470 struct addrinfo *next = ai->ai_next;
1473 free(ai->ai_canonname);
1479 errno = saved_errno;
1482 void asyncns_freeanswer(unsigned char *answer) {
1483 int saved_errno = errno;
1488 /* Please note that this function is new in libasyncns 0.4. In
1489 * older versions you were supposed to free the answer directly
1490 * with free(). Hence, if this function is changed to do more than
1491 * just a simple free() this must be considered ABI/API breakage! */
1495 errno = saved_errno;
1498 int asyncns_isdone(asyncns_t *asyncns, asyncns_query_t*q) {
1501 assert(q->asyncns == asyncns);
1506 void asyncns_setuserdata(asyncns_t *asyncns, asyncns_query_t *q, void *userdata) {
1509 assert(q->asyncns = asyncns);
1511 q->userdata = userdata;
1514 void* asyncns_getuserdata(asyncns_t *asyncns, asyncns_query_t *q) {
1517 assert(q->asyncns = asyncns);