You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1427 lines
33 KiB

  1. /***
  2. This file is part of libasyncns.
  3. Copyright 2005-2008 Lennart Poettering
  4. libasyncns is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation, either version 2.1 of the
  7. License, or (at your option) any later version.
  8. libasyncns is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with libasyncns. If not, see
  14. <http://www.gnu.org/licenses/>.
  15. ***/
  16. #ifdef HAVE_CONFIG_H
  17. #include <config.h>
  18. #endif
  19. /* #undef HAVE_PTHREAD */
  20. #include <assert.h>
  21. #include <fcntl.h>
  22. #include <signal.h>
  23. #include <unistd.h>
  24. #include <sys/select.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <errno.h>
  29. #include <sys/wait.h>
  30. #include <sys/types.h>
  31. #include <pwd.h>
  32. #include <netinet/in.h>
  33. #include <arpa/nameser.h>
  34. #include <resolv.h>
  35. #include <dirent.h>
  36. #include <sys/time.h>
  37. #include <sys/resource.h>
  38. #include <stdint.h>
  39. #ifdef HAVE_SYS_PRCTL_H
  40. #include <sys/prctl.h>
  41. #endif
  42. #if HAVE_PTHREAD
  43. #include <pthread.h>
  44. #endif
  45. #include "asyncns.h"
  46. #ifndef MSG_NOSIGNAL
  47. #define MSG_NOSIGNAL 0
  48. #endif
  49. #define MAX_WORKERS 16
  50. #define MAX_QUERIES 256
  51. #define BUFSIZE (10240)
  52. typedef enum {
  53. REQUEST_ADDRINFO,
  54. RESPONSE_ADDRINFO,
  55. REQUEST_NAMEINFO,
  56. RESPONSE_NAMEINFO,
  57. REQUEST_TERMINATE,
  58. RESPONSE_DIED
  59. } query_type_t;
  60. enum {
  61. REQUEST_RECV_FD = 0,
  62. REQUEST_SEND_FD = 1,
  63. RESPONSE_RECV_FD = 2,
  64. RESPONSE_SEND_FD = 3,
  65. MESSAGE_FD_MAX = 4
  66. };
  67. struct asyncns {
  68. int fds[4];
  69. #ifndef HAVE_PTHREAD
  70. pid_t workers[MAX_WORKERS];
  71. #else
  72. pthread_t workers[MAX_WORKERS];
  73. #endif
  74. unsigned valid_workers;
  75. unsigned current_id, current_index;
  76. asyncns_query_t* queries[MAX_QUERIES];
  77. asyncns_query_t *done_head, *done_tail;
  78. int n_queries;
  79. int dead;
  80. };
  81. struct asyncns_query {
  82. asyncns_t *asyncns;
  83. int done;
  84. unsigned id;
  85. query_type_t type;
  86. asyncns_query_t *done_next, *done_prev;
  87. int ret;
  88. int _errno;
  89. int _h_errno;
  90. struct addrinfo *addrinfo;
  91. char *serv, *host;
  92. void *userdata;
  93. };
  94. typedef struct rheader {
  95. query_type_t type;
  96. unsigned id;
  97. size_t length;
  98. } rheader_t;
  99. typedef struct addrinfo_request {
  100. struct rheader header;
  101. int hints_is_null;
  102. int ai_flags;
  103. int ai_family;
  104. int ai_socktype;
  105. int ai_protocol;
  106. size_t node_len, service_len;
  107. } addrinfo_request_t;
  108. typedef struct addrinfo_response {
  109. struct rheader header;
  110. int ret;
  111. int _errno;
  112. int _h_errno;
  113. /* followed by addrinfo_serialization[] */
  114. } addrinfo_response_t;
  115. typedef struct addrinfo_serialization {
  116. int ai_flags;
  117. int ai_family;
  118. int ai_socktype;
  119. int ai_protocol;
  120. size_t ai_addrlen;
  121. size_t canonname_len;
  122. /* Followed by ai_addr amd ai_canonname with variable lengths */
  123. } addrinfo_serialization_t;
  124. typedef struct nameinfo_request {
  125. struct rheader header;
  126. int flags;
  127. socklen_t sockaddr_len;
  128. int gethost, getserv;
  129. } nameinfo_request_t;
  130. typedef struct nameinfo_response {
  131. struct rheader header;
  132. size_t hostlen, servlen;
  133. int ret;
  134. int _errno;
  135. int _h_errno;
  136. } nameinfo_response_t;
  137. typedef struct res_request {
  138. struct rheader header;
  139. int class;
  140. int type;
  141. size_t dname_len;
  142. } res_request_t;
  143. typedef struct res_response {
  144. struct rheader header;
  145. int ret;
  146. int _errno;
  147. int _h_errno;
  148. } res_response_t;
  149. typedef union packet {
  150. rheader_t rheader;
  151. addrinfo_request_t addrinfo_request;
  152. addrinfo_response_t addrinfo_response;
  153. nameinfo_request_t nameinfo_request;
  154. nameinfo_response_t nameinfo_response;
  155. res_request_t res_request;
  156. res_response_t res_response;
  157. } packet_t;
  158. #ifndef HAVE_STRNDUP
  159. static char *strndup(const char *s, size_t l) {
  160. size_t a;
  161. char *n;
  162. a = strlen(s);
  163. if (a > l)
  164. a = l;
  165. if (!(n = malloc(a+1)))
  166. return NULL;
  167. memcpy(n, s, a);
  168. n[a] = 0;
  169. return n;
  170. }
  171. #endif
  172. #ifndef HAVE_PTHREAD
  173. static int close_allv(const int except_fds[]) {
  174. struct rlimit rl;
  175. int fd, maxfd;
  176. #ifdef __linux__
  177. DIR *d;
  178. assert(except_fds);
  179. if ((d = opendir("/proc/self/fd"))) {
  180. struct dirent *de;
  181. while ((de = readdir(d))) {
  182. int found;
  183. long l;
  184. char *e = NULL;
  185. int i;
  186. if (de->d_name[0] == '.')
  187. continue;
  188. errno = 0;
  189. l = strtol(de->d_name, &e, 10);
  190. if (errno != 0 || !e || *e) {
  191. closedir(d);
  192. errno = EINVAL;
  193. return -1;
  194. }
  195. fd = (int) l;
  196. if ((long) fd != l) {
  197. closedir(d);
  198. errno = EINVAL;
  199. return -1;
  200. }
  201. if (fd < 3)
  202. continue;
  203. if (fd == dirfd(d))
  204. continue;
  205. found = 0;
  206. for (i = 0; except_fds[i] >= 0; i++)
  207. if (except_fds[i] == fd) {
  208. found = 1;
  209. break;
  210. }
  211. if (found)
  212. continue;
  213. if (close(fd) < 0) {
  214. int saved_errno;
  215. saved_errno = errno;
  216. closedir(d);
  217. errno = saved_errno;
  218. return -1;
  219. }
  220. }
  221. closedir(d);
  222. return 0;
  223. }
  224. #endif
  225. if (getrlimit(RLIMIT_NOFILE, &rl) > 0)
  226. maxfd = (int) rl.rlim_max;
  227. else
  228. maxfd = sysconf(_SC_OPEN_MAX);
  229. for (fd = 3; fd < maxfd; fd++) {
  230. int i, found;
  231. found = 0;
  232. for (i = 0; except_fds[i] >= 0; i++)
  233. if (except_fds[i] == fd) {
  234. found = 1;
  235. continue;
  236. }
  237. if (found)
  238. continue;
  239. if (close(fd) < 0 && errno != EBADF)
  240. return -1;
  241. }
  242. return 0;
  243. }
  244. static int reset_sigsv(const int except[]) {
  245. int sig;
  246. assert(except);
  247. for (sig = 1; sig < NSIG; sig++) {
  248. int reset = 1;
  249. switch (sig) {
  250. case SIGKILL:
  251. case SIGSTOP:
  252. reset = 0;
  253. break;
  254. default: {
  255. int i;
  256. for (i = 0; except[i] > 0; i++) {
  257. if (sig == except[i]) {
  258. reset = 0;
  259. break;
  260. }
  261. }
  262. }
  263. }
  264. if (reset) {
  265. struct sigaction sa;
  266. memset(&sa, 0, sizeof(sa));
  267. sa.sa_handler = SIG_DFL;
  268. /* On Linux the first two RT signals are reserved by
  269. * glibc, and sigaction() will return EINVAL for them. */
  270. if ((sigaction(sig, &sa, NULL) < 0))
  271. if (errno != EINVAL)
  272. return -1;
  273. }
  274. }
  275. return 0;
  276. }
  277. static int ignore_sigsv(const int ignore[]) {
  278. int i;
  279. assert(ignore);
  280. for (i = 0; ignore[i] > 0; i++) {
  281. struct sigaction sa;
  282. memset(&sa, 0, sizeof(sa));
  283. sa.sa_handler = SIG_IGN;
  284. if ((sigaction(ignore[i], &sa, NULL) < 0))
  285. return -1;
  286. }
  287. return 0;
  288. }
  289. #endif
  290. static int fd_nonblock(int fd) {
  291. int i;
  292. assert(fd >= 0);
  293. if ((i = fcntl(fd, F_GETFL, 0)) < 0)
  294. return -1;
  295. if (i & O_NONBLOCK)
  296. return 0;
  297. return fcntl(fd, F_SETFL, i | O_NONBLOCK);
  298. }
  299. static int fd_cloexec(int fd) {
  300. int v;
  301. assert(fd >= 0);
  302. if ((v = fcntl(fd, F_GETFD, 0)) < 0)
  303. return -1;
  304. if (v & FD_CLOEXEC)
  305. return 0;
  306. return fcntl(fd, F_SETFD, v | FD_CLOEXEC);
  307. }
  308. static int send_died(int out_fd) {
  309. rheader_t rh;
  310. assert(out_fd > 0);
  311. memset(&rh, 0, sizeof(rh));
  312. rh.type = RESPONSE_DIED;
  313. rh.id = 0;
  314. rh.length = sizeof(rh);
  315. return send(out_fd, &rh, rh.length, MSG_NOSIGNAL);
  316. }
  317. static void *serialize_addrinfo(void *p, const struct addrinfo *ai, size_t *length, size_t maxlength) {
  318. addrinfo_serialization_t s;
  319. size_t cnl, l;
  320. assert(p);
  321. assert(ai);
  322. assert(length);
  323. assert(*length <= maxlength);
  324. cnl = (ai->ai_canonname ? strlen(ai->ai_canonname)+1 : 0);
  325. l = sizeof(addrinfo_serialization_t) + ai->ai_addrlen + cnl;
  326. if (*length + l > maxlength)
  327. return NULL;
  328. s.ai_flags = ai->ai_flags;
  329. s.ai_family = ai->ai_family;
  330. s.ai_socktype = ai->ai_socktype;
  331. s.ai_protocol = ai->ai_protocol;
  332. s.ai_addrlen = ai->ai_addrlen;
  333. s.canonname_len = cnl;
  334. memcpy((uint8_t*) p, &s, sizeof(addrinfo_serialization_t));
  335. memcpy((uint8_t*) p + sizeof(addrinfo_serialization_t), ai->ai_addr, ai->ai_addrlen);
  336. if (ai->ai_canonname)
  337. strcpy((char*) p + sizeof(addrinfo_serialization_t) + ai->ai_addrlen, ai->ai_canonname);
  338. *length += l;
  339. return (uint8_t*) p + l;
  340. }
  341. static int send_addrinfo_reply(int out_fd, unsigned id, int ret, struct addrinfo *ai, int _errno, int _h_errno) {
  342. addrinfo_response_t data[BUFSIZE/sizeof(addrinfo_response_t) + 1];
  343. addrinfo_response_t *resp = data;
  344. assert(out_fd >= 0);
  345. memset(data, 0, sizeof(data));
  346. resp->header.type = RESPONSE_ADDRINFO;
  347. resp->header.id = id;
  348. resp->header.length = sizeof(addrinfo_response_t);
  349. resp->ret = ret;
  350. resp->_errno = _errno;
  351. resp->_h_errno = _h_errno;
  352. if (ret == 0 && ai) {
  353. void *p = data + 1;
  354. struct addrinfo *k;
  355. for (k = ai; k; k = k->ai_next) {
  356. if (!(p = serialize_addrinfo(p, k, &resp->header.length, (char*) data + BUFSIZE - (char*) p))) {
  357. resp->ret = EAI_MEMORY;
  358. break;
  359. }
  360. }
  361. }
  362. if (ai)
  363. freeaddrinfo(ai);
  364. return send(out_fd, resp, resp->header.length, MSG_NOSIGNAL);
  365. }
  366. static int send_nameinfo_reply(int out_fd, unsigned id, int ret, const char *host, const char *serv, int _errno, int _h_errno) {
  367. nameinfo_response_t data[BUFSIZE/sizeof(nameinfo_response_t) + 1];
  368. size_t hl, sl;
  369. nameinfo_response_t *resp = data;
  370. assert(out_fd >= 0);
  371. sl = serv ? strlen(serv)+1 : 0;
  372. hl = host ? strlen(host)+1 : 0;
  373. memset(data, 0, sizeof(data));
  374. resp->header.type = RESPONSE_NAMEINFO;
  375. resp->header.id = id;
  376. resp->header.length = sizeof(nameinfo_response_t) + hl + sl;
  377. resp->ret = ret;
  378. resp->_errno = _errno;
  379. resp->_h_errno = _h_errno;
  380. resp->hostlen = hl;
  381. resp->servlen = sl;
  382. assert(sizeof(data) >= resp->header.length);
  383. if (host)
  384. memcpy((uint8_t *)data + sizeof(nameinfo_response_t), host, hl);
  385. if (serv)
  386. memcpy((uint8_t *)data + sizeof(nameinfo_response_t) + hl, serv, sl);
  387. return send(out_fd, resp, resp->header.length, MSG_NOSIGNAL);
  388. }
  389. static int handle_request(int out_fd, const packet_t *packet, size_t length) {
  390. const rheader_t *req;
  391. assert(out_fd >= 0);
  392. req = &packet->rheader;
  393. assert(req);
  394. assert(length >= sizeof(rheader_t));
  395. assert(length == req->length);
  396. switch (req->type) {
  397. case REQUEST_ADDRINFO: {
  398. struct addrinfo ai, *result = NULL;
  399. const addrinfo_request_t *ai_req = &packet->addrinfo_request;
  400. const char *node, *service;
  401. int ret;
  402. assert(length >= sizeof(addrinfo_request_t));
  403. assert(length == sizeof(addrinfo_request_t) + ai_req->node_len + ai_req->service_len);
  404. memset(&ai, 0, sizeof(ai));
  405. ai.ai_flags = ai_req->ai_flags;
  406. ai.ai_family = ai_req->ai_family;
  407. ai.ai_socktype = ai_req->ai_socktype;
  408. ai.ai_protocol = ai_req->ai_protocol;
  409. node = ai_req->node_len ? (const char*) ai_req + sizeof(addrinfo_request_t) : NULL;
  410. service = ai_req->service_len ? (const char*) ai_req + sizeof(addrinfo_request_t) + ai_req->node_len : NULL;
  411. ret = getaddrinfo(node, service,
  412. ai_req->hints_is_null ? NULL : &ai,
  413. &result);
  414. /* send_addrinfo_reply() frees result */
  415. return send_addrinfo_reply(out_fd, req->id, ret, result, errno, h_errno);
  416. }
  417. case REQUEST_NAMEINFO: {
  418. int ret;
  419. const nameinfo_request_t *ni_req = &packet->nameinfo_request;
  420. char hostbuf[NI_MAXHOST], servbuf[NI_MAXSERV];
  421. struct sockaddr_storage sa;
  422. assert(length >= sizeof(nameinfo_request_t));
  423. assert(length == sizeof(nameinfo_request_t) + ni_req->sockaddr_len);
  424. memcpy(&sa, (const uint8_t *) ni_req + sizeof(nameinfo_request_t), ni_req->sockaddr_len);
  425. ret = getnameinfo((struct sockaddr *)&sa, ni_req->sockaddr_len,
  426. ni_req->gethost ? hostbuf : NULL, ni_req->gethost ? sizeof(hostbuf) : 0,
  427. ni_req->getserv ? servbuf : NULL, ni_req->getserv ? sizeof(servbuf) : 0,
  428. ni_req->flags);
  429. return send_nameinfo_reply(out_fd, req->id, ret,
  430. ret == 0 && ni_req->gethost ? hostbuf : NULL,
  431. ret == 0 && ni_req->getserv ? servbuf : NULL,
  432. errno, h_errno);
  433. }
  434. case REQUEST_TERMINATE:
  435. /* Quit */
  436. return -1;
  437. default:
  438. ;
  439. }
  440. return 0;
  441. }
  442. #ifndef HAVE_PTHREAD
  443. static int process_worker(int in_fd, int out_fd) {
  444. int have_death_sig = 0;
  445. int good_fds[3];
  446. int ret = 1;
  447. const int ignore_sigs[] = {
  448. SIGINT,
  449. SIGHUP,
  450. SIGPIPE,
  451. SIGUSR1,
  452. SIGUSR2,
  453. -1
  454. };
  455. assert(in_fd > 2);
  456. assert(out_fd > 2);
  457. close(0);
  458. close(1);
  459. close(2);
  460. if (open("/dev/null", O_RDONLY) != 0)
  461. goto fail;
  462. if (open("/dev/null", O_WRONLY) != 1)
  463. goto fail;
  464. if (open("/dev/null", O_WRONLY) != 2)
  465. goto fail;
  466. if (chdir("/") < 0)
  467. goto fail;
  468. if (geteuid() == 0) {
  469. struct passwd *pw;
  470. int r;
  471. if ((pw = getpwnam("nobody"))) {
  472. #ifdef HAVE_SETRESUID
  473. r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
  474. #elif HAVE_SETREUID
  475. r = setreuid(pw->pw_uid, pw->pw_uid);
  476. #else
  477. if ((r = setuid(pw->pw_uid)) >= 0)
  478. r = seteuid(pw->pw_uid);
  479. #endif
  480. if (r < 0)
  481. goto fail;
  482. }
  483. }
  484. if (reset_sigsv(ignore_sigs) < 0)
  485. goto fail;
  486. if (ignore_sigsv(ignore_sigs) < 0)
  487. goto fail;
  488. good_fds[0] = in_fd; good_fds[1] = out_fd; good_fds[2] = -1;
  489. if (close_allv(good_fds) < 0)
  490. goto fail;
  491. #ifdef PR_SET_PDEATHSIG
  492. if (prctl(PR_SET_PDEATHSIG, SIGTERM) >= 0)
  493. have_death_sig = 1;
  494. #endif
  495. if (!have_death_sig)
  496. fd_nonblock(in_fd);
  497. while (getppid() > 1) { /* if the parent PID is 1 our parent process died. */
  498. packet_t buf[BUFSIZE/sizeof(packet_t) + 1];
  499. ssize_t length;
  500. if (!have_death_sig) {
  501. fd_set fds;
  502. struct timeval tv = { 0, 5000000 };
  503. FD_ZERO(&fds);
  504. FD_SET(in_fd, &fds);
  505. if (select(in_fd+1, &fds, NULL, NULL, &tv) < 0)
  506. break;
  507. if (getppid() == 1)
  508. break;
  509. }
  510. if ((length = recv(in_fd, buf, sizeof(buf), 0)) <= 0) {
  511. if (length < 0 &&
  512. (errno == EAGAIN || errno == EINTR))
  513. continue;
  514. break;
  515. }
  516. if (handle_request(out_fd, buf, (size_t) length) < 0)
  517. break;
  518. }
  519. ret = 0;
  520. fail:
  521. send_died(out_fd);
  522. return ret;
  523. }
  524. #else
  525. static void* thread_worker(void *p) {
  526. asyncns_t *asyncns = p;
  527. sigset_t fullset;
  528. int in_fd = asyncns->fds[REQUEST_RECV_FD];
  529. /* No signals in this thread please */
  530. sigfillset(&fullset);
  531. pthread_sigmask(SIG_BLOCK, &fullset, NULL);
  532. fd_nonblock(in_fd);
  533. while (!asyncns->dead) {
  534. packet_t buf[BUFSIZE/sizeof(packet_t) + 1];
  535. ssize_t length;
  536. if ((length = recv(in_fd, buf, sizeof(buf), 0)) <= 0) {
  537. if (!asyncns->dead) {
  538. fd_set fds;
  539. struct timeval tv = { 0, 5000000 };
  540. FD_ZERO(&fds);
  541. FD_SET(in_fd, &fds);
  542. if (select(in_fd+1, &fds, NULL, NULL, &tv) < 0)
  543. break;
  544. }
  545. if (length < 0 &&
  546. (errno == EAGAIN || errno == EINTR)) {
  547. continue;
  548. }
  549. break;
  550. }
  551. if (asyncns->dead)
  552. break;
  553. if (handle_request(asyncns->fds[RESPONSE_SEND_FD], buf, (size_t) length) < 0)
  554. break;
  555. }
  556. send_died(asyncns->fds[RESPONSE_SEND_FD]);
  557. return NULL;
  558. }
  559. #endif
  560. asyncns_t* asyncns_new(unsigned n_proc) {
  561. asyncns_t *asyncns = NULL;
  562. int i;
  563. assert(n_proc >= 1);
  564. if (n_proc > MAX_WORKERS)
  565. n_proc = MAX_WORKERS;
  566. if (!(asyncns = malloc(sizeof(asyncns_t)))) {
  567. errno = ENOMEM;
  568. goto fail;
  569. }
  570. asyncns->dead = 0;
  571. asyncns->valid_workers = 0;
  572. for (i = 0; i < MESSAGE_FD_MAX; i++)
  573. asyncns->fds[i] = -1;
  574. memset(asyncns->queries, 0, sizeof(asyncns->queries));
  575. if (socketpair(PF_UNIX, SOCK_DGRAM, 0, asyncns->fds) < 0 ||
  576. socketpair(PF_UNIX, SOCK_DGRAM, 0, asyncns->fds+2) < 0)
  577. goto fail;
  578. for (i = 0; i < MESSAGE_FD_MAX; i++)
  579. fd_cloexec(asyncns->fds[i]);
  580. for (asyncns->valid_workers = 0; asyncns->valid_workers < n_proc; asyncns->valid_workers++) {
  581. #ifndef HAVE_PTHREAD
  582. if ((asyncns->workers[asyncns->valid_workers] = fork()) < 0)
  583. goto fail;
  584. else if (asyncns->workers[asyncns->valid_workers] == 0) {
  585. int ret;
  586. close(asyncns->fds[REQUEST_SEND_FD]);
  587. close(asyncns->fds[RESPONSE_RECV_FD]);
  588. ret = process_worker(asyncns->fds[REQUEST_RECV_FD], asyncns->fds[RESPONSE_SEND_FD]);
  589. close(asyncns->fds[REQUEST_RECV_FD]);
  590. close(asyncns->fds[RESPONSE_SEND_FD]);
  591. _exit(ret);
  592. }
  593. #else
  594. int r;
  595. if ((r = pthread_create(&asyncns->workers[asyncns->valid_workers], NULL, thread_worker, asyncns)) != 0) {
  596. errno = r;
  597. goto fail;
  598. }
  599. #endif
  600. }
  601. #ifndef HAVE_PTHREAD
  602. close(asyncns->fds[REQUEST_RECV_FD]);
  603. close(asyncns->fds[RESPONSE_SEND_FD]);
  604. asyncns->fds[REQUEST_RECV_FD] = asyncns->fds[RESPONSE_SEND_FD] = -1;
  605. #endif
  606. asyncns->current_index = asyncns->current_id = 0;
  607. asyncns->done_head = asyncns->done_tail = NULL;
  608. asyncns->n_queries = 0;
  609. fd_nonblock(asyncns->fds[RESPONSE_RECV_FD]);
  610. return asyncns;
  611. fail:
  612. if (asyncns)
  613. asyncns_free(asyncns);
  614. return NULL;
  615. }
  616. void asyncns_free(asyncns_t *asyncns) {
  617. int i;
  618. int saved_errno = errno;
  619. unsigned p;
  620. assert(asyncns);
  621. asyncns->dead = 1;
  622. if (asyncns->fds[REQUEST_SEND_FD] >= 0) {
  623. rheader_t req;
  624. memset(&req, 0, sizeof(req));
  625. req.type = REQUEST_TERMINATE;
  626. req.length = sizeof(req);
  627. req.id = 0;
  628. /* Send one termination packet for each worker */
  629. for (p = 0; p < asyncns->valid_workers; p++)
  630. send(asyncns->fds[REQUEST_SEND_FD], &req, req.length, MSG_NOSIGNAL);
  631. }
  632. /* Now terminate them and wait until they are gone. */
  633. for (p = 0; p < asyncns->valid_workers; p++) {
  634. #ifndef HAVE_PTHREAD
  635. kill(asyncns->workers[p], SIGTERM);
  636. for (;;) {
  637. if (waitpid(asyncns->workers[p], NULL, 0) >= 0 || errno != EINTR)
  638. break;
  639. }
  640. #else
  641. for (;;) {
  642. if (pthread_join(asyncns->workers[p], NULL) != EINTR)
  643. break;
  644. }
  645. #endif
  646. }
  647. /* Close all communication channels */
  648. for (i = 0; i < MESSAGE_FD_MAX; i++)
  649. if (asyncns->fds[i] >= 0)
  650. close(asyncns->fds[i]);
  651. for (p = 0; p < MAX_QUERIES; p++)
  652. if (asyncns->queries[p])
  653. asyncns_cancel(asyncns, asyncns->queries[p]);
  654. free(asyncns);
  655. errno = saved_errno;
  656. }
  657. int asyncns_fd(asyncns_t *asyncns) {
  658. assert(asyncns);
  659. return asyncns->fds[RESPONSE_RECV_FD];
  660. }
  661. static asyncns_query_t *lookup_query(asyncns_t *asyncns, unsigned id) {
  662. asyncns_query_t *q;
  663. assert(asyncns);
  664. if ((q = asyncns->queries[id % MAX_QUERIES]))
  665. if (q->id == id)
  666. return q;
  667. return NULL;
  668. }
  669. static void complete_query(asyncns_t *asyncns, asyncns_query_t *q) {
  670. assert(asyncns);
  671. assert(q);
  672. assert(!q->done);
  673. q->done = 1;
  674. if ((q->done_prev = asyncns->done_tail))
  675. asyncns->done_tail->done_next = q;
  676. else
  677. asyncns->done_head = q;
  678. asyncns->done_tail = q;
  679. q->done_next = NULL;
  680. }
  681. static const void *unserialize_addrinfo(const void *p, struct addrinfo **ret_ai, size_t *length) {
  682. addrinfo_serialization_t s;
  683. size_t l;
  684. struct addrinfo *ai;
  685. assert(p);
  686. assert(ret_ai);
  687. assert(length);
  688. if (*length < sizeof(addrinfo_serialization_t))
  689. return NULL;
  690. memcpy(&s, p, sizeof(s));
  691. l = sizeof(addrinfo_serialization_t) + s.ai_addrlen + s.canonname_len;
  692. if (*length < l)
  693. return NULL;
  694. if (!(ai = malloc(sizeof(struct addrinfo))))
  695. goto fail;
  696. ai->ai_addr = NULL;
  697. ai->ai_canonname = NULL;
  698. ai->ai_next = NULL;
  699. if (s.ai_addrlen && !(ai->ai_addr = malloc(s.ai_addrlen)))
  700. goto fail;
  701. if (s.canonname_len && !(ai->ai_canonname = malloc(s.canonname_len)))
  702. goto fail;
  703. ai->ai_flags = s.ai_flags;
  704. ai->ai_family = s.ai_family;
  705. ai->ai_socktype = s.ai_socktype;
  706. ai->ai_protocol = s.ai_protocol;
  707. ai->ai_addrlen = s.ai_addrlen;
  708. if (ai->ai_addr)
  709. memcpy(ai->ai_addr, (const uint8_t*) p + sizeof(addrinfo_serialization_t), s.ai_addrlen);
  710. if (ai->ai_canonname)
  711. memcpy(ai->ai_canonname, (const uint8_t*) p + sizeof(addrinfo_serialization_t) + s.ai_addrlen, s.canonname_len);
  712. *length -= l;
  713. *ret_ai = ai;
  714. return (const uint8_t*) p + l;
  715. fail:
  716. if (ai)
  717. asyncns_freeaddrinfo(ai);
  718. return NULL;
  719. }
  720. static int handle_response(asyncns_t *asyncns, const packet_t *packet, size_t length) {
  721. const rheader_t *resp;
  722. asyncns_query_t *q;
  723. assert(asyncns);
  724. resp = &packet->rheader;
  725. assert(resp);
  726. assert(length >= sizeof(rheader_t));
  727. assert(length == resp->length);
  728. if (resp->type == RESPONSE_DIED) {
  729. asyncns->dead = 1;
  730. return 0;
  731. }
  732. if (!(q = lookup_query(asyncns, resp->id)))
  733. return 0;
  734. switch (resp->type) {
  735. case RESPONSE_ADDRINFO: {
  736. const addrinfo_response_t *ai_resp = &packet->addrinfo_response;
  737. const void *p;
  738. size_t l;
  739. struct addrinfo *prev = NULL;
  740. assert(length >= sizeof(addrinfo_response_t));
  741. assert(q->type == REQUEST_ADDRINFO);
  742. q->ret = ai_resp->ret;
  743. q->_errno = ai_resp->_errno;
  744. q->_h_errno = ai_resp->_h_errno;
  745. l = length - sizeof(addrinfo_response_t);
  746. p = (const uint8_t*) resp + sizeof(addrinfo_response_t);
  747. while (l > 0 && p) {
  748. struct addrinfo *ai = NULL;
  749. p = unserialize_addrinfo(p, &ai, &l);
  750. if (!p || !ai) {
  751. q->ret = EAI_MEMORY;
  752. break;
  753. }
  754. if (prev)
  755. prev->ai_next = ai;
  756. else
  757. q->addrinfo = ai;
  758. prev = ai;
  759. }
  760. complete_query(asyncns, q);
  761. break;
  762. }
  763. case RESPONSE_NAMEINFO: {
  764. const nameinfo_response_t *ni_resp = &packet->nameinfo_response;
  765. assert(length >= sizeof(nameinfo_response_t));
  766. assert(q->type == REQUEST_NAMEINFO);
  767. q->ret = ni_resp->ret;
  768. q->_errno = ni_resp->_errno;
  769. q->_h_errno = ni_resp->_h_errno;
  770. if (ni_resp->hostlen)
  771. if (!(q->host = strndup((const char*) ni_resp + sizeof(nameinfo_response_t), ni_resp->hostlen-1)))
  772. q->ret = EAI_MEMORY;
  773. if (ni_resp->servlen)
  774. if (!(q->serv = strndup((const char*) ni_resp + sizeof(nameinfo_response_t) + ni_resp->hostlen, ni_resp->servlen-1)))
  775. q->ret = EAI_MEMORY;
  776. complete_query(asyncns, q);
  777. break;
  778. }
  779. default:
  780. ;
  781. }
  782. return 0;
  783. }
  784. int asyncns_wait(asyncns_t *asyncns, int block) {
  785. int handled = 0;
  786. assert(asyncns);
  787. for (;;) {
  788. packet_t buf[BUFSIZE/sizeof(packet_t) + 1];
  789. ssize_t l;
  790. if (asyncns->dead) {
  791. errno = ECHILD;
  792. return -1;
  793. }
  794. if (((l = recv(asyncns->fds[RESPONSE_RECV_FD], buf, sizeof(buf), 0)) < 0)) {
  795. fd_set fds;
  796. if (errno != EAGAIN)
  797. return -1;
  798. if (!block || handled)
  799. return 0;
  800. FD_ZERO(&fds);
  801. FD_SET(asyncns->fds[RESPONSE_RECV_FD], &fds);
  802. if (select(asyncns->fds[RESPONSE_RECV_FD]+1, &fds, NULL, NULL, NULL) < 0)
  803. return -1;
  804. continue;
  805. }
  806. if (handle_response(asyncns, buf, (size_t) l) < 0)
  807. return -1;
  808. handled = 1;
  809. }
  810. }
  811. static asyncns_query_t *alloc_query(asyncns_t *asyncns) {
  812. asyncns_query_t *q;
  813. assert(asyncns);
  814. if (asyncns->n_queries >= MAX_QUERIES) {
  815. errno = ENOMEM;
  816. return NULL;
  817. }
  818. while (asyncns->queries[asyncns->current_index]) {
  819. asyncns->current_index++;
  820. asyncns->current_id++;
  821. while (asyncns->current_index >= MAX_QUERIES)
  822. asyncns->current_index -= MAX_QUERIES;
  823. }
  824. if (!(q = asyncns->queries[asyncns->current_index] = malloc(sizeof(asyncns_query_t)))) {
  825. errno = ENOMEM;
  826. return NULL;
  827. }
  828. asyncns->n_queries++;
  829. q->asyncns = asyncns;
  830. q->done = 0;
  831. q->id = asyncns->current_id;
  832. q->done_next = q->done_prev = NULL;
  833. q->ret = 0;
  834. q->_errno = 0;
  835. q->_h_errno = 0;
  836. q->addrinfo = NULL;
  837. q->userdata = NULL;
  838. q->host = q->serv = NULL;
  839. return q;
  840. }
  841. asyncns_query_t* asyncns_getaddrinfo(asyncns_t *asyncns, const char *node, const char *service, const struct addrinfo *hints) {
  842. addrinfo_request_t data[BUFSIZE/sizeof(addrinfo_request_t) + 1];
  843. addrinfo_request_t *req = data;
  844. asyncns_query_t *q;
  845. assert(asyncns);
  846. assert(node || service);
  847. if (asyncns->dead) {
  848. errno = ECHILD;
  849. return NULL;
  850. }
  851. if (!(q = alloc_query(asyncns)))
  852. return NULL;
  853. memset(req, 0, sizeof(addrinfo_request_t));
  854. req->node_len = node ? strlen(node)+1 : 0;
  855. req->service_len = service ? strlen(service)+1 : 0;
  856. req->header.id = q->id;
  857. req->header.type = q->type = REQUEST_ADDRINFO;
  858. req->header.length = sizeof(addrinfo_request_t) + req->node_len + req->service_len;
  859. if (req->header.length > BUFSIZE) {
  860. errno = ENOMEM;
  861. goto fail;
  862. }
  863. if (!(req->hints_is_null = !hints)) {
  864. req->ai_flags = hints->ai_flags;
  865. req->ai_family = hints->ai_family;
  866. req->ai_socktype = hints->ai_socktype;
  867. req->ai_protocol = hints->ai_protocol;
  868. }
  869. if (node)
  870. strcpy((char*) req + sizeof(addrinfo_request_t), node);
  871. if (service)
  872. strcpy((char*) req + sizeof(addrinfo_request_t) + req->node_len, service);
  873. if (send(asyncns->fds[REQUEST_SEND_FD], req, req->header.length, MSG_NOSIGNAL) < 0)
  874. goto fail;
  875. return q;
  876. fail:
  877. if (q)
  878. asyncns_cancel(asyncns, q);
  879. return NULL;
  880. }
  881. int asyncns_getaddrinfo_done(asyncns_t *asyncns, asyncns_query_t* q, struct addrinfo **ret_res) {
  882. int ret;
  883. assert(asyncns);
  884. assert(q);
  885. assert(q->asyncns == asyncns);
  886. assert(q->type == REQUEST_ADDRINFO);
  887. if (asyncns->dead) {
  888. errno = ECHILD;
  889. return EAI_SYSTEM;
  890. }
  891. if (!q->done)
  892. return EAI_AGAIN;
  893. *ret_res = q->addrinfo;
  894. q->addrinfo = NULL;
  895. ret = q->ret;
  896. if (ret == EAI_SYSTEM)
  897. errno = q->_errno;
  898. if (ret != 0)
  899. h_errno = q->_h_errno;
  900. asyncns_cancel(asyncns, q);
  901. return ret;
  902. }
  903. asyncns_query_t* asyncns_getnameinfo(asyncns_t *asyncns, const struct sockaddr *sa, socklen_t salen, int flags, int gethost, int getserv) {
  904. nameinfo_request_t data[BUFSIZE/sizeof(nameinfo_request_t) + 1];
  905. nameinfo_request_t *req = data;
  906. asyncns_query_t *q;
  907. assert(asyncns);
  908. assert(sa);
  909. assert(salen > 0);
  910. if (asyncns->dead) {
  911. errno = ECHILD;
  912. return NULL;
  913. }
  914. if (!(q = alloc_query(asyncns)))
  915. return NULL;
  916. memset(req, 0, sizeof(nameinfo_request_t));
  917. req->header.id = q->id;
  918. req->header.type = q->type = REQUEST_NAMEINFO;
  919. req->header.length = sizeof(nameinfo_request_t) + salen;
  920. if (req->header.length > BUFSIZE) {
  921. errno = ENOMEM;
  922. goto fail;
  923. }
  924. req->flags = flags;
  925. req->sockaddr_len = salen;
  926. req->gethost = gethost;
  927. req->getserv = getserv;
  928. memcpy((uint8_t*) req + sizeof(nameinfo_request_t), sa, salen);
  929. if (send(asyncns->fds[REQUEST_SEND_FD], req, req->header.length, MSG_NOSIGNAL) < 0)
  930. goto fail;
  931. return q;
  932. fail:
  933. if (q)
  934. asyncns_cancel(asyncns, q);
  935. return NULL;
  936. }
  937. int asyncns_getnameinfo_done(asyncns_t *asyncns, asyncns_query_t* q, char *ret_host, size_t hostlen, char *ret_serv, size_t servlen) {
  938. int ret;
  939. assert(asyncns);
  940. assert(q);
  941. assert(q->asyncns == asyncns);
  942. assert(q->type == REQUEST_NAMEINFO);
  943. assert(!ret_host || hostlen);
  944. assert(!ret_serv || servlen);
  945. if (asyncns->dead) {
  946. errno = ECHILD;
  947. return EAI_SYSTEM;
  948. }
  949. if (!q->done)
  950. return EAI_AGAIN;
  951. if (ret_host && q->host) {
  952. strncpy(ret_host, q->host, hostlen);
  953. ret_host[hostlen-1] = 0;
  954. }
  955. if (ret_serv && q->serv) {
  956. strncpy(ret_serv, q->serv, servlen);
  957. ret_serv[servlen-1] = 0;
  958. }
  959. ret = q->ret;
  960. if (ret == EAI_SYSTEM)
  961. errno = q->_errno;
  962. if (ret != 0)
  963. h_errno = q->_h_errno;
  964. asyncns_cancel(asyncns, q);
  965. return ret;
  966. }
  967. static asyncns_query_t * asyncns_res(asyncns_t *asyncns, query_type_t qtype, const char *dname, int class, int type) {
  968. res_request_t data[BUFSIZE/sizeof(res_request_t) + 1];
  969. res_request_t *req = data;
  970. asyncns_query_t *q;
  971. assert(asyncns);
  972. assert(dname);
  973. if (asyncns->dead) {
  974. errno = ECHILD;
  975. return NULL;
  976. }
  977. if (!(q = alloc_query(asyncns)))
  978. return NULL;
  979. memset(req, 0, sizeof(res_request_t));
  980. req->dname_len = strlen(dname) + 1;
  981. req->header.id = q->id;
  982. req->header.type = q->type = qtype;
  983. req->header.length = sizeof(res_request_t) + req->dname_len;
  984. if (req->header.length > BUFSIZE) {
  985. errno = ENOMEM;
  986. goto fail;
  987. }
  988. req->class = class;
  989. req->type = type;
  990. strcpy((char*) req + sizeof(res_request_t), dname);
  991. if (send(asyncns->fds[REQUEST_SEND_FD], req, req->header.length, MSG_NOSIGNAL) < 0)
  992. goto fail;
  993. return q;
  994. fail:
  995. if (q)
  996. asyncns_cancel(asyncns, q);
  997. return NULL;
  998. }
  999. asyncns_query_t* asyncns_getnext(asyncns_t *asyncns) {
  1000. assert(asyncns);
  1001. return asyncns->done_head;
  1002. }
  1003. int asyncns_getnqueries(asyncns_t *asyncns) {
  1004. assert(asyncns);
  1005. return asyncns->n_queries;
  1006. }
  1007. void asyncns_cancel(asyncns_t *asyncns, asyncns_query_t* q) {
  1008. int i;
  1009. int saved_errno = errno;
  1010. assert(asyncns);
  1011. assert(q);
  1012. assert(q->asyncns == asyncns);
  1013. assert(asyncns->n_queries > 0);
  1014. if (q->done) {
  1015. if (q->done_prev)
  1016. q->done_prev->done_next = q->done_next;
  1017. else
  1018. asyncns->done_head = q->done_next;
  1019. if (q->done_next)
  1020. q->done_next->done_prev = q->done_prev;
  1021. else
  1022. asyncns->done_tail = q->done_prev;
  1023. }
  1024. i = q->id % MAX_QUERIES;
  1025. assert(asyncns->queries[i] == q);
  1026. asyncns->queries[i] = NULL;
  1027. asyncns_freeaddrinfo(q->addrinfo);
  1028. free(q->host);
  1029. free(q->serv);
  1030. asyncns->n_queries--;
  1031. free(q);
  1032. errno = saved_errno;
  1033. }
  1034. void asyncns_freeaddrinfo(struct addrinfo *ai) {
  1035. int saved_errno = errno;
  1036. while (ai) {
  1037. struct addrinfo *next = ai->ai_next;
  1038. free(ai->ai_addr);
  1039. free(ai->ai_canonname);
  1040. free(ai);
  1041. ai = next;
  1042. }
  1043. errno = saved_errno;
  1044. }
  1045. void asyncns_freeanswer(unsigned char *answer) {
  1046. int saved_errno = errno;
  1047. if (!answer)
  1048. return;
  1049. /* Please note that this function is new in libasyncns 0.4. In
  1050. * older versions you were supposed to free the answer directly
  1051. * with free(). Hence, if this function is changed to do more than
  1052. * just a simple free() this must be considered ABI/API breakage! */
  1053. free(answer);
  1054. errno = saved_errno;
  1055. }
  1056. int asyncns_isdone(asyncns_t *asyncns, asyncns_query_t*q) {
  1057. assert(asyncns);
  1058. assert(q);
  1059. assert(q->asyncns == asyncns);
  1060. return q->done;
  1061. }
  1062. void asyncns_setuserdata(asyncns_t *asyncns, asyncns_query_t *q, void *userdata) {
  1063. assert(q);
  1064. assert(asyncns);
  1065. assert(q->asyncns = asyncns);
  1066. q->userdata = userdata;
  1067. }
  1068. void* asyncns_getuserdata(asyncns_t *asyncns, asyncns_query_t *q) {
  1069. assert(q);
  1070. assert(asyncns);
  1071. assert(q->asyncns = asyncns);
  1072. return q->userdata;
  1073. }