res_msend.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <netdb.h>
  4. #include <arpa/inet.h>
  5. #include <stdint.h>
  6. #include <string.h>
  7. #include <poll.h>
  8. #include <time.h>
  9. #include <ctype.h>
  10. #include <unistd.h>
  11. #include <errno.h>
  12. #include <pthread.h>
  13. #include "stdio_impl.h"
  14. #include "syscall.h"
  15. #include "lookup.h"
  16. static void cleanup(void *p)
  17. {
  18. __syscall(SYS_close, (intptr_t)p);
  19. }
  20. static unsigned long mtime()
  21. {
  22. struct timespec ts;
  23. clock_gettime(CLOCK_REALTIME, &ts);
  24. return (unsigned long)ts.tv_sec * 1000
  25. + ts.tv_nsec / 1000000;
  26. }
  27. int __res_msend_rc(int nqueries, const unsigned char *const *queries,
  28. const int *qlens, unsigned char *const *answers, int *alens, int asize,
  29. const struct resolvconf *conf)
  30. {
  31. int fd;
  32. int timeout, attempts, retry_interval, servfail_retry;
  33. union {
  34. struct sockaddr_in sin;
  35. struct sockaddr_in6 sin6;
  36. } sa = {0}, ns[MAXNS] = {{0}};
  37. socklen_t sl = sizeof sa.sin;
  38. int nns = 0;
  39. int family = AF_INET;
  40. int rlen;
  41. int next;
  42. int i, j;
  43. int cs;
  44. struct pollfd pfd;
  45. unsigned long t0, t1, t2;
  46. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
  47. timeout = 1000*conf->timeout;
  48. attempts = conf->attempts;
  49. for (nns=0; nns<conf->nns; nns++) {
  50. const struct address *iplit = &conf->ns[nns];
  51. if (iplit->family == AF_INET) {
  52. memcpy(&ns[nns].sin.sin_addr, iplit->addr, 4);
  53. ns[nns].sin.sin_port = htons(53);
  54. ns[nns].sin.sin_family = AF_INET;
  55. } else {
  56. sl = sizeof sa.sin6;
  57. memcpy(&ns[nns].sin6.sin6_addr, iplit->addr, 16);
  58. ns[nns].sin6.sin6_port = htons(53);
  59. ns[nns].sin6.sin6_scope_id = iplit->scopeid;
  60. ns[nns].sin6.sin6_family = family = AF_INET6;
  61. }
  62. }
  63. /* Get local address and open/bind a socket */
  64. sa.sin.sin_family = family;
  65. fd = socket(family, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
  66. /* Handle case where system lacks IPv6 support */
  67. if (fd < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
  68. fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
  69. family = AF_INET;
  70. }
  71. if (fd < 0 || bind(fd, (void *)&sa, sl) < 0) {
  72. if (fd >= 0) close(fd);
  73. pthread_setcancelstate(cs, 0);
  74. return -1;
  75. }
  76. /* Past this point, there are no errors. Each individual query will
  77. * yield either no reply (indicated by zero length) or an answer
  78. * packet which is up to the caller to interpret. */
  79. pthread_cleanup_push(cleanup, (void *)(intptr_t)fd);
  80. pthread_setcancelstate(cs, 0);
  81. /* Convert any IPv4 addresses in a mixed environment to v4-mapped */
  82. if (family == AF_INET6) {
  83. setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &(int){0}, sizeof 0);
  84. for (i=0; i<nns; i++) {
  85. if (ns[i].sin.sin_family != AF_INET) continue;
  86. memcpy(ns[i].sin6.sin6_addr.s6_addr+12,
  87. &ns[i].sin.sin_addr, 4);
  88. memcpy(ns[i].sin6.sin6_addr.s6_addr,
  89. "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
  90. ns[i].sin6.sin6_family = AF_INET6;
  91. ns[i].sin6.sin6_flowinfo = 0;
  92. ns[i].sin6.sin6_scope_id = 0;
  93. }
  94. }
  95. memset(alens, 0, sizeof *alens * nqueries);
  96. pfd.fd = fd;
  97. pfd.events = POLLIN;
  98. retry_interval = timeout / attempts;
  99. next = 0;
  100. t0 = t2 = mtime();
  101. t1 = t2 - retry_interval;
  102. for (; t2-t0 < timeout; t2=mtime()) {
  103. if (t2-t1 >= retry_interval) {
  104. /* Query all configured namservers in parallel */
  105. for (i=0; i<nqueries; i++)
  106. if (!alens[i])
  107. for (j=0; j<nns; j++)
  108. sendto(fd, queries[i],
  109. qlens[i], MSG_NOSIGNAL,
  110. (void *)&ns[j], sl);
  111. t1 = t2;
  112. servfail_retry = 2 * nqueries;
  113. }
  114. /* Wait for a response, or until time to retry */
  115. if (poll(&pfd, 1, t1+retry_interval-t2) <= 0) continue;
  116. while ((rlen = recvfrom(fd, answers[next], asize, 0,
  117. (void *)&sa, (socklen_t[1]){sl})) >= 0) {
  118. /* Ignore non-identifiable packets */
  119. if (rlen < 4) continue;
  120. /* Ignore replies from addresses we didn't send to */
  121. for (j=0; j<nns && memcmp(ns+j, &sa, sl); j++);
  122. if (j==nns) continue;
  123. /* Find which query this answer goes with, if any */
  124. for (i=next; i<nqueries && (
  125. answers[next][0] != queries[i][0] ||
  126. answers[next][1] != queries[i][1] ); i++);
  127. if (i==nqueries) continue;
  128. if (alens[i]) continue;
  129. /* Only accept positive or negative responses;
  130. * retry immediately on server failure, and ignore
  131. * all other codes such as refusal. */
  132. switch (answers[next][3] & 15) {
  133. case 0:
  134. case 3:
  135. break;
  136. case 2:
  137. if (servfail_retry && servfail_retry--)
  138. sendto(fd, queries[i],
  139. qlens[i], MSG_NOSIGNAL,
  140. (void *)&ns[j], sl);
  141. default:
  142. continue;
  143. }
  144. /* Store answer in the right slot, or update next
  145. * available temp slot if it's already in place. */
  146. alens[i] = rlen;
  147. if (i == next)
  148. for (; next<nqueries && alens[next]; next++);
  149. else
  150. memcpy(answers[i], answers[next], rlen);
  151. if (next == nqueries) goto out;
  152. }
  153. }
  154. out:
  155. pthread_cleanup_pop(1);
  156. return 0;
  157. }
  158. int __res_msend(int nqueries, const unsigned char *const *queries,
  159. const int *qlens, unsigned char *const *answers, int *alens, int asize)
  160. {
  161. struct resolvconf conf;
  162. if (__get_resolv_conf(&conf, 0, 0) < 0) return -1;
  163. return __res_msend_rc(nqueries, queries, qlens, answers, alens, asize, &conf);
  164. }