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.

1469 lines
34 KiB

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