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.

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