ldns  1.7.0
net.c
Go to the documentation of this file.
1 /*
2  * net.c
3  *
4  * Network implementation
5  * All network related functions are grouped here
6  *
7  * a Net::DNS like library for C
8  *
9  * (c) NLnet Labs, 2004-2006
10  *
11  * See the file LICENSE for the license
12  */
13 
14 #include <ldns/config.h>
15 
16 #include <ldns/ldns.h>
17 
18 #ifdef HAVE_NETINET_IN_H
19 #include <netinet/in.h>
20 #endif
21 #ifdef HAVE_SYS_SOCKET_H
22 #include <sys/socket.h>
23 #endif
24 #ifdef HAVE_NETDB_H
25 #include <netdb.h>
26 #endif
27 #ifdef HAVE_ARPA_INET_H
28 #include <arpa/inet.h>
29 #endif
30 #include <sys/time.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #ifdef HAVE_POLL
34 #include <poll.h>
35 #endif
36 
38 ldns_send(ldns_pkt **result_packet, ldns_resolver *r, const ldns_pkt *query_pkt)
39 {
40  ldns_buffer *qb;
41  ldns_status result;
42  ldns_rdf *tsig_mac = NULL;
43 
45 
46  if (query_pkt && ldns_pkt_tsig(query_pkt)) {
47  tsig_mac = ldns_rr_rdf(ldns_pkt_tsig(query_pkt), 3);
48  }
49 
50  if (!query_pkt ||
51  ldns_pkt2buffer_wire(qb, query_pkt) != LDNS_STATUS_OK) {
52  result = LDNS_STATUS_ERR;
53  } else {
54  result = ldns_send_buffer(result_packet, r, qb, tsig_mac);
55  }
56 
57  ldns_buffer_free(qb);
58 
59  return result;
60 }
61 
62 /* code from rdata.c */
63 static struct sockaddr_storage *
64 ldns_rdf2native_sockaddr_storage_port(
65  const ldns_rdf *rd, uint16_t port, size_t *size)
66 {
67  struct sockaddr_storage *data;
68  struct sockaddr_in *data_in;
69  struct sockaddr_in6 *data_in6;
70 
71  data = LDNS_MALLOC(struct sockaddr_storage);
72  if (!data) {
73  return NULL;
74  }
75  /* zero the structure for portability */
76  memset(data, 0, sizeof(struct sockaddr_storage));
77 
78  switch(ldns_rdf_get_type(rd)) {
79  case LDNS_RDF_TYPE_A:
80 #ifndef S_SPLINT_S
81  data->ss_family = AF_INET;
82 #endif
83  data_in = (struct sockaddr_in*) data;
84  data_in->sin_port = (in_port_t)htons(port);
85  memcpy(&(data_in->sin_addr), ldns_rdf_data(rd), ldns_rdf_size(rd));
86  *size = sizeof(struct sockaddr_in);
87  return data;
88  case LDNS_RDF_TYPE_AAAA:
89 #ifndef S_SPLINT_S
90  data->ss_family = AF_INET6;
91 #endif
92  data_in6 = (struct sockaddr_in6*) data;
93  data_in6->sin6_port = (in_port_t)htons(port);
94  memcpy(&data_in6->sin6_addr, ldns_rdf_data(rd), ldns_rdf_size(rd));
95  *size = sizeof(struct sockaddr_in6);
96  return data;
97  default:
98  LDNS_FREE(data);
99  return NULL;
100  }
101 }
102 
103 struct sockaddr_storage *
105  const ldns_rdf *rd, uint16_t port, size_t *size)
106 {
107  return ldns_rdf2native_sockaddr_storage_port(
108  rd, (port == 0 ? (uint16_t)LDNS_PORT : port), size);
109 }
110 
112 static void
113 ldns_sock_nonblock(int sockfd)
114 {
115 #ifdef HAVE_FCNTL
116  int flag;
117  if((flag = fcntl(sockfd, F_GETFL)) != -1) {
118  flag |= O_NONBLOCK;
119  if(fcntl(sockfd, F_SETFL, flag) == -1) {
120  /* ignore error, continue blockingly */
121  }
122  }
123 #elif defined(HAVE_IOCTLSOCKET)
124  unsigned long on = 1;
125  if(ioctlsocket(sockfd, FIONBIO, &on) != 0) {
126  /* ignore error, continue blockingly */
127  }
128 #endif
129 }
130 
132 static void
133 ldns_sock_block(int sockfd)
134 {
135 #ifdef HAVE_FCNTL
136  int flag;
137  if((flag = fcntl(sockfd, F_GETFL)) != -1) {
138  flag &= ~O_NONBLOCK;
139  if(fcntl(sockfd, F_SETFL, flag) == -1) {
140  /* ignore error, continue */
141  }
142  }
143 #elif defined(HAVE_IOCTLSOCKET)
144  unsigned long off = 0;
145  if(ioctlsocket(sockfd, FIONBIO, &off) != 0) {
146  /* ignore error, continue */
147  }
148 #endif
149 }
150 
152 static int
153 ldns_sock_wait(int sockfd, struct timeval timeout, int write)
154 {
155  int ret;
156 #ifndef HAVE_POLL
157 #ifndef S_SPLINT_S
158  fd_set fds;
159  FD_ZERO(&fds);
160  FD_SET(FD_SET_T sockfd, &fds);
161  if(write)
162  ret = select(sockfd+1, NULL, &fds, NULL, &timeout);
163  else
164  ret = select(sockfd+1, &fds, NULL, NULL, &timeout);
165 #endif
166 #else
167  struct pollfd pfds[2];
168 
169  memset(&pfds[0], 0, sizeof(pfds[0]) * 2);
170 
171  pfds[0].fd = sockfd;
172  pfds[0].events = POLLIN|POLLERR;
173 
174  if (write) {
175  pfds[0].events |= POLLOUT;
176  }
177 
178  ret = poll(pfds, 1, (int)(timeout.tv_sec * 1000
179  + timeout.tv_usec / 1000));
180 #endif
181  if(ret == 0)
182  /* timeout expired */
183  return 0;
184  else if(ret == -1)
185  /* error */
186  return 0;
187  return 1;
188 }
189 
190 
191 static int
192 ldns_tcp_connect_from(const struct sockaddr_storage *to, socklen_t tolen,
193  const struct sockaddr_storage *from, socklen_t fromlen,
194  struct timeval timeout)
195 {
196  int sockfd;
197 
198 #ifndef S_SPLINT_S
199  if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_STREAM,
200  IPPROTO_TCP)) == SOCK_INVALID) {
201  return 0;
202  }
203 #endif
204  if (from && bind(sockfd, (const struct sockaddr*)from, fromlen) == SOCK_INVALID){
205  return 0;
206  }
207 
208  /* perform nonblocking connect, to be able to wait with select() */
209  ldns_sock_nonblock(sockfd);
210  if (connect(sockfd, (struct sockaddr*)to, tolen) == SOCK_INVALID) {
211 #ifndef USE_WINSOCK
212 #ifdef EINPROGRESS
213  if(errno != EINPROGRESS) {
214 #else
215  if(1) {
216 #endif
217  close_socket(sockfd);
218  return 0;
219  }
220 #else /* USE_WINSOCK */
221  if(WSAGetLastError() != WSAEINPROGRESS &&
222  WSAGetLastError() != WSAEWOULDBLOCK) {
223  close_socket(sockfd);
224  return 0;
225  }
226 #endif
227  /* error was only telling us that it would block */
228  }
229 
230  /* wait(write) until connected or error */
231  while(1) {
232  int error = 0;
233  socklen_t len = (socklen_t)sizeof(error);
234 
235  if(!ldns_sock_wait(sockfd, timeout, 1)) {
236  close_socket(sockfd);
237  return 0;
238  }
239 
240  /* check if there is a pending error for nonblocking connect */
241  if(getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void*)&error,
242  &len) < 0) {
243 #ifndef USE_WINSOCK
244  error = errno; /* on solaris errno is error */
245 #else
246  error = WSAGetLastError();
247 #endif
248  }
249 #ifndef USE_WINSOCK
250 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
251  if(error == EINPROGRESS || error == EWOULDBLOCK)
252  continue; /* try again */
253 #endif
254  else if(error != 0) {
255  close_socket(sockfd);
256  /* error in errno for our user */
257  errno = error;
258  return 0;
259  }
260 #else /* USE_WINSOCK */
261  if(error == WSAEINPROGRESS)
262  continue;
263  else if(error == WSAEWOULDBLOCK)
264  continue;
265  else if(error != 0) {
266  close_socket(sockfd);
267  errno = error;
268  return 0;
269  }
270 #endif /* USE_WINSOCK */
271  /* connected */
272  break;
273  }
274 
275  /* set the socket blocking again */
276  ldns_sock_block(sockfd);
277 
278  return sockfd;
279 }
280 
281 int
282 ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen,
283  struct timeval timeout)
284 {
285  return ldns_tcp_connect_from(to, tolen, NULL, 0, timeout);
286 }
287 
288 static int
289 ldns_tcp_bgsend_from(ldns_buffer *qbin,
290  const struct sockaddr_storage *to, socklen_t tolen,
291  const struct sockaddr_storage *from, socklen_t fromlen,
292  struct timeval timeout)
293 {
294  int sockfd;
295 
296  sockfd = ldns_tcp_connect_from(to, tolen, from, fromlen, timeout);
297 
298  if (sockfd == 0) {
299  return 0;
300  }
301 
302  if (ldns_tcp_send_query(qbin, sockfd, to, tolen) == 0) {
303  close_socket(sockfd);
304  return 0;
305  }
306 
307  return sockfd;
308 }
309 
310 int
312  const struct sockaddr_storage *to, socklen_t tolen,
313  struct timeval timeout)
314 {
315  return ldns_tcp_bgsend_from(qbin, to, tolen, NULL, 0, timeout);
316 }
317 
318 
319 /* keep in mind that in DNS tcp messages the first 2 bytes signal the
320  * amount data to expect
321  */
322 static ldns_status
323 ldns_tcp_send_from(uint8_t **result, ldns_buffer *qbin,
324  const struct sockaddr_storage *to, socklen_t tolen,
325  const struct sockaddr_storage *from, socklen_t fromlen,
326  struct timeval timeout, size_t *answer_size)
327 {
328  int sockfd;
329  uint8_t *answer;
330 
331  sockfd = ldns_tcp_bgsend_from(qbin, to, tolen, from, fromlen, timeout);
332 
333  if (sockfd == 0) {
334  return LDNS_STATUS_ERR;
335  }
336 
337  answer = ldns_tcp_read_wire_timeout(sockfd, answer_size, timeout);
338  close_socket(sockfd);
339 
340  if (*answer_size == 0) {
341  /* oops */
343  }
344 
345  /* resize accordingly */
346  *result = LDNS_XREALLOC(answer, uint8_t, (size_t)*answer_size);
347  if(!*result) {
348  LDNS_FREE(answer);
349  return LDNS_STATUS_MEM_ERR;
350  }
351  return LDNS_STATUS_OK;
352 }
353 
355 ldns_tcp_send(uint8_t **result, ldns_buffer *qbin,
356  const struct sockaddr_storage *to, socklen_t tolen,
357  struct timeval timeout, size_t *answer_size)
358 {
359  return ldns_tcp_send_from(result, qbin,
360  to, tolen, NULL, 0, timeout, answer_size);
361 }
362 
363 int
364 ldns_udp_connect(const struct sockaddr_storage *to, struct timeval ATTR_UNUSED(timeout))
365 {
366  int sockfd;
367 
368 #ifndef S_SPLINT_S
369  if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_DGRAM,
370  IPPROTO_UDP))
371  == -1) {
372  return 0;
373  }
374 #endif
375  return sockfd;
376 }
377 
378 static int
379 ldns_udp_bgsend_from(ldns_buffer *qbin,
380  const struct sockaddr_storage *to , socklen_t tolen,
381  const struct sockaddr_storage *from, socklen_t fromlen,
382  struct timeval timeout)
383 {
384  int sockfd;
385 
386  sockfd = ldns_udp_connect(to, timeout);
387 
388  if (sockfd == 0) {
389  return 0;
390  }
391 
392  if (from && bind(sockfd, (const struct sockaddr*)from, fromlen) == -1){
393  return 0;
394  }
395 
396  if (ldns_udp_send_query(qbin, sockfd, to, tolen) == 0) {
397  close_socket(sockfd);
398  return 0;
399  }
400  return sockfd;
401 }
402 
403 int
405  const struct sockaddr_storage *to , socklen_t tolen,
406  struct timeval timeout)
407 {
408  return ldns_udp_bgsend_from(qbin, to, tolen, NULL, 0, timeout);
409 }
410 
411 static ldns_status
412 ldns_udp_send_from(uint8_t **result, ldns_buffer *qbin,
413  const struct sockaddr_storage *to , socklen_t tolen,
414  const struct sockaddr_storage *from, socklen_t fromlen,
415  struct timeval timeout, size_t *answer_size)
416 {
417  int sockfd;
418  uint8_t *answer;
419 
420  sockfd = ldns_udp_bgsend_from(qbin, to, tolen, from, fromlen, timeout);
421 
422  if (sockfd == 0) {
424  }
425 
426  /* wait for an response*/
427  if(!ldns_sock_wait(sockfd, timeout, 0)) {
428  close_socket(sockfd);
430  }
431 
432  /* set to nonblocking, so if the checksum is bad, it becomes
433  * an EGAIN error and the ldns_udp_send function does not block,
434  * but returns a 'NETWORK_ERROR' much like a timeout. */
435  ldns_sock_nonblock(sockfd);
436 
437  answer = ldns_udp_read_wire(sockfd, answer_size, NULL, NULL);
438  close_socket(sockfd);
439 
440  if (*answer_size == 0) {
441  /* oops */
443  }
444 
445  *result = answer;
446  return LDNS_STATUS_OK;
447 }
448 
450 ldns_udp_send(uint8_t **result, ldns_buffer *qbin,
451  const struct sockaddr_storage *to , socklen_t tolen,
452  struct timeval timeout, size_t *answer_size)
453 {
454  return ldns_udp_send_from(result, qbin, to, tolen, NULL, 0,
455  timeout, answer_size);
456 }
457 
460 {
461  uint8_t i;
462 
463  struct sockaddr_storage *src = NULL;
464  size_t src_len;
465  struct sockaddr_storage *ns;
466  size_t ns_len;
467  struct timeval tv_s;
468  struct timeval tv_e;
469 
470  ldns_rdf **ns_array;
471  size_t *rtt;
472  ldns_pkt *reply;
473  bool all_servers_rtt_inf;
474  uint8_t retries;
475 
476  uint8_t *reply_bytes = NULL;
477  size_t reply_size = 0;
478  ldns_status status, send_status;
479 
480  assert(r != NULL);
481 
482  status = LDNS_STATUS_OK;
483  rtt = ldns_resolver_rtt(r);
484  ns_array = ldns_resolver_nameservers(r);
485  reply = NULL;
486  ns_len = 0;
487 
488  all_servers_rtt_inf = true;
489 
490  if (ldns_resolver_random(r)) {
492  }
493 
494  if(ldns_resolver_source(r)) {
495  src = ldns_rdf2native_sockaddr_storage_port(
496  ldns_resolver_source(r), 0, &src_len);
497  }
498 
499  /* loop through all defined nameservers */
500  for (i = 0; i < ldns_resolver_nameserver_count(r); i++) {
501  if (rtt[i] == LDNS_RESOLV_RTT_INF) {
502  /* not reachable nameserver! */
503  continue;
504  }
505 
506  /* maybe verbosity setting?
507  printf("Sending to ");
508  ldns_rdf_print(stdout, ns_array[i]);
509  printf("\n");
510  */
511  ns = ldns_rdf2native_sockaddr_storage(ns_array[i],
512  ldns_resolver_port(r), &ns_len);
513 
514 
515 #ifndef S_SPLINT_S
516  if ((ns->ss_family == AF_INET) &&
518  /* not reachable */
519  LDNS_FREE(ns);
520  continue;
521  }
522 
523  if ((ns->ss_family == AF_INET6) &&
525  /* not reachable */
526  LDNS_FREE(ns);
527  continue;
528  }
529 #endif
530 
531  all_servers_rtt_inf = false;
532 
533  gettimeofday(&tv_s, NULL);
534 
535  send_status = LDNS_STATUS_ERR;
536 
537  /* reply_bytes implicitly handles our error */
538  if (ldns_resolver_usevc(r)) {
539  for (retries = ldns_resolver_retry(r); retries > 0; retries--) {
540  send_status =
541  ldns_tcp_send_from(&reply_bytes, qb,
542  ns, (socklen_t)ns_len,
543  src, (socklen_t)src_len,
545  &reply_size);
546  if (send_status == LDNS_STATUS_OK) {
547  break;
548  }
549  }
550  } else {
551  for (retries = ldns_resolver_retry(r); retries > 0; retries--) {
552  /* ldns_rdf_print(stdout, ns_array[i]); */
553  send_status =
554  ldns_udp_send_from(&reply_bytes, qb,
555  ns, (socklen_t)ns_len,
556  src, (socklen_t)src_len,
558  &reply_size);
559  if (send_status == LDNS_STATUS_OK) {
560  break;
561  }
562  }
563  }
564 
565  if (send_status != LDNS_STATUS_OK) {
567  status = send_status;
568  }
569 
570  /* obey the fail directive */
571  if (!reply_bytes) {
572  /* the current nameserver seems to have a problem, blacklist it */
573  if (ldns_resolver_fail(r)) {
574  LDNS_FREE(ns);
575  return LDNS_STATUS_ERR;
576  } else {
577  LDNS_FREE(ns);
578  continue;
579  }
580  }
581 
582  status = ldns_wire2pkt(&reply, reply_bytes, reply_size);
583  if (status != LDNS_STATUS_OK) {
584  LDNS_FREE(reply_bytes);
585  LDNS_FREE(ns);
586  return status;
587  }
588 
589  LDNS_FREE(ns);
590  gettimeofday(&tv_e, NULL);
591 
592  if (reply) {
593  ldns_pkt_set_querytime(reply, (uint32_t)
594  ((tv_e.tv_sec - tv_s.tv_sec) * 1000) +
595  (tv_e.tv_usec - tv_s.tv_usec) / 1000);
597  ldns_rdf_clone(ns_array[i]));
598  ldns_pkt_set_timestamp(reply, tv_s);
599  ldns_pkt_set_size(reply, reply_size);
600  break;
601  } else {
602  if (ldns_resolver_fail(r)) {
603  /* if fail is set bail out, after the first
604  * one */
605  break;
606  }
607  }
608 
609  /* wait retrans seconds... */
610  sleep((unsigned int) ldns_resolver_retrans(r));
611  }
612 
613  if(src) {
614  LDNS_FREE(src);
615  }
616  if (all_servers_rtt_inf) {
617  LDNS_FREE(reply_bytes);
618  return LDNS_STATUS_RES_NO_NS;
619  }
620 #ifdef HAVE_SSL
621  if (tsig_mac && reply && reply_bytes) {
622  if (!ldns_pkt_tsig_verify(reply,
623  reply_bytes,
624  reply_size,
626  ldns_resolver_tsig_keydata(r), tsig_mac)) {
628  }
629  }
630 #else
631  (void)tsig_mac;
632 #endif /* HAVE_SSL */
633 
634  LDNS_FREE(reply_bytes);
635  if (result) {
636  *result = reply;
637  }
638 
639  return status;
640 }
641 
642 ssize_t
643 ldns_tcp_send_query(ldns_buffer *qbin, int sockfd,
644  const struct sockaddr_storage *to, socklen_t tolen)
645 {
646  uint8_t *sendbuf;
647  ssize_t bytes;
648 
649  /* add length of packet */
650  sendbuf = LDNS_XMALLOC(uint8_t, ldns_buffer_position(qbin) + 2);
651  if(!sendbuf) return 0;
652  ldns_write_uint16(sendbuf, ldns_buffer_position(qbin));
653  memcpy(sendbuf + 2, ldns_buffer_begin(qbin), ldns_buffer_position(qbin));
654 
655  bytes = sendto(sockfd, (void*)sendbuf,
656  ldns_buffer_position(qbin) + 2, 0, (struct sockaddr *)to, tolen);
657 
658  LDNS_FREE(sendbuf);
659 
660  if (bytes == -1 || (size_t) bytes != ldns_buffer_position(qbin) + 2 ) {
661  return 0;
662  }
663  return bytes;
664 }
665 
666 /* don't wait for an answer */
667 ssize_t
668 ldns_udp_send_query(ldns_buffer *qbin, int sockfd, const struct sockaddr_storage *to,
669  socklen_t tolen)
670 {
671  ssize_t bytes;
672 
673  bytes = sendto(sockfd, (void*)ldns_buffer_begin(qbin),
674  ldns_buffer_position(qbin), 0, (struct sockaddr *)to, tolen);
675 
676  if (bytes == -1 || (size_t)bytes != ldns_buffer_position(qbin)) {
677  return 0;
678  }
679  if ((size_t) bytes != ldns_buffer_position(qbin)) {
680  return 0;
681  }
682  return bytes;
683 }
684 
685 uint8_t *
686 ldns_udp_read_wire(int sockfd, size_t *size, struct sockaddr_storage *from,
687  socklen_t *fromlen)
688 {
689  uint8_t *wire, *wireout;
690  ssize_t wire_size;
691 
692  wire = LDNS_XMALLOC(uint8_t, LDNS_MAX_PACKETLEN);
693  if (!wire) {
694  *size = 0;
695  return NULL;
696  }
697 
698  wire_size = recvfrom(sockfd, (void*)wire, LDNS_MAX_PACKETLEN, 0,
699  (struct sockaddr *)from, fromlen);
700 
701  /* recvfrom can also return 0 */
702  if (wire_size == -1 || wire_size == 0) {
703  *size = 0;
704  LDNS_FREE(wire);
705  return NULL;
706  }
707 
708  *size = (size_t)wire_size;
709  wireout = LDNS_XREALLOC(wire, uint8_t, (size_t)wire_size);
710  if(!wireout) LDNS_FREE(wire);
711 
712  return wireout;
713 }
714 
715 uint8_t *
716 ldns_tcp_read_wire_timeout(int sockfd, size_t *size, struct timeval timeout)
717 {
718  uint8_t *wire;
719  uint16_t wire_size;
720  ssize_t bytes = 0, rc = 0;
721 
722  wire = LDNS_XMALLOC(uint8_t, 2);
723  if (!wire) {
724  *size = 0;
725  return NULL;
726  }
727 
728  while (bytes < 2) {
729  if(!ldns_sock_wait(sockfd, timeout, 0)) {
730  *size = 0;
731  LDNS_FREE(wire);
732  return NULL;
733  }
734  rc = recv(sockfd, (void*) (wire + bytes),
735  (size_t) (2 - bytes), 0);
736  if (rc == -1 || rc == 0) {
737  *size = 0;
738  LDNS_FREE(wire);
739  return NULL;
740  }
741  bytes += rc;
742  }
743 
744  wire_size = ldns_read_uint16(wire);
745 
746  LDNS_FREE(wire);
747  wire = LDNS_XMALLOC(uint8_t, wire_size);
748  if (!wire) {
749  *size = 0;
750  return NULL;
751  }
752  bytes = 0;
753 
754  while (bytes < (ssize_t) wire_size) {
755  if(!ldns_sock_wait(sockfd, timeout, 0)) {
756  *size = 0;
757  LDNS_FREE(wire);
758  return NULL;
759  }
760  rc = recv(sockfd, (void*) (wire + bytes),
761  (size_t) (wire_size - bytes), 0);
762  if (rc == -1 || rc == 0) {
763  LDNS_FREE(wire);
764  *size = 0;
765  return NULL;
766  }
767  bytes += rc;
768  }
769 
770  *size = (size_t) bytes;
771  return wire;
772 }
773 
774 uint8_t *
775 ldns_tcp_read_wire(int sockfd, size_t *size)
776 {
777  uint8_t *wire;
778  uint16_t wire_size;
779  ssize_t bytes = 0, rc = 0;
780 
781  wire = LDNS_XMALLOC(uint8_t, 2);
782  if (!wire) {
783  *size = 0;
784  return NULL;
785  }
786 
787  while (bytes < 2) {
788  rc = recv(sockfd, (void*) (wire + bytes),
789  (size_t) (2 - bytes), 0);
790  if (rc == -1 || rc == 0) {
791  *size = 0;
792  LDNS_FREE(wire);
793  return NULL;
794  }
795  bytes += rc;
796  }
797 
798  wire_size = ldns_read_uint16(wire);
799 
800  LDNS_FREE(wire);
801  wire = LDNS_XMALLOC(uint8_t, wire_size);
802  if (!wire) {
803  *size = 0;
804  return NULL;
805  }
806  bytes = 0;
807 
808  while (bytes < (ssize_t) wire_size) {
809  rc = recv(sockfd, (void*) (wire + bytes),
810  (size_t) (wire_size - bytes), 0);
811  if (rc == -1 || rc == 0) {
812  LDNS_FREE(wire);
813  *size = 0;
814  return NULL;
815  }
816  bytes += rc;
817  }
818 
819  *size = (size_t) bytes;
820  return wire;
821 }
822 
823 #ifndef S_SPLINT_S
824 ldns_rdf *
825 ldns_sockaddr_storage2rdf(const struct sockaddr_storage *sock, uint16_t *port)
826 {
827  ldns_rdf *addr;
828  struct sockaddr_in *data_in;
829  struct sockaddr_in6 *data_in6;
830 
831  switch(sock->ss_family) {
832  case AF_INET:
833  data_in = (struct sockaddr_in*)sock;
834  if (port) {
835  *port = ntohs((uint16_t)data_in->sin_port);
836  }
838  LDNS_IP4ADDRLEN, &data_in->sin_addr);
839  break;
840  case AF_INET6:
841  data_in6 = (struct sockaddr_in6*)sock;
842  if (port) {
843  *port = ntohs((uint16_t)data_in6->sin6_port);
844  }
846  LDNS_IP6ADDRLEN, &data_in6->sin6_addr);
847  break;
848  default:
849  if (port) {
850  *port = 0;
851  }
852  return NULL;
853  }
854  return addr;
855 }
856 #endif
857 
858 /* code from resolver.c */
860 ldns_axfr_start(ldns_resolver *resolver, const ldns_rdf *domain, ldns_rr_class class)
861 {
862  ldns_pkt *query;
863  ldns_buffer *query_wire;
864 
865  struct sockaddr_storage *src = NULL;
866  size_t src_len = 0;
867  struct sockaddr_storage *ns = NULL;
868  size_t ns_len = 0;
869  size_t ns_i;
870  ldns_status status;
871 
872  if (!resolver || ldns_resolver_nameserver_count(resolver) < 1) {
873  return LDNS_STATUS_ERR;
874  }
875 
876  query = ldns_pkt_query_new(ldns_rdf_clone(domain), LDNS_RR_TYPE_AXFR, class, 0);
877 
878  if (!query) {
880  }
881  if(ldns_resolver_source(resolver)) {
882  src = ldns_rdf2native_sockaddr_storage_port(
883  ldns_resolver_source(resolver), 0, &src_len);
884  }
885  /* For AXFR, we have to make the connection ourselves */
886  /* try all nameservers (which usually would mean v4 fallback if
887  * @hostname is used */
888  for (ns_i = 0;
889  ns_i < ldns_resolver_nameserver_count(resolver) &&
890  resolver->_socket == SOCK_INVALID;
891  ns_i++) {
892  if (ns != NULL) {
893  LDNS_FREE(ns);
894  }
896  resolver->_nameservers[ns_i],
897  ldns_resolver_port(resolver), &ns_len);
898 #ifndef S_SPLINT_S
899  if ((ns->ss_family == AF_INET) &&
900  (ldns_resolver_ip6(resolver) == LDNS_RESOLV_INET6)) {
901  /* not reachable */
902  LDNS_FREE(ns);
903  ns = NULL;
904  continue;
905  }
906 
907  if ((ns->ss_family == AF_INET6) &&
908  (ldns_resolver_ip6(resolver) == LDNS_RESOLV_INET)) {
909  /* not reachable */
910  LDNS_FREE(ns);
911  ns = NULL;
912  continue;
913  }
914 #endif
915 
916  resolver->_socket = ldns_tcp_connect_from(
917  ns, (socklen_t)ns_len,
918  src, (socklen_t)src_len,
919  ldns_resolver_timeout(resolver));
920  }
921 
922  if (resolver->_socket == SOCK_INVALID) {
923  ldns_pkt_free(query);
924  LDNS_FREE(ns);
926  }
927 
928 #ifdef HAVE_SSL
929  if (ldns_resolver_tsig_keyname(resolver) && ldns_resolver_tsig_keydata(resolver)) {
930  status = ldns_pkt_tsig_sign(query,
931  ldns_resolver_tsig_keyname(resolver),
932  ldns_resolver_tsig_keydata(resolver),
933  300, ldns_resolver_tsig_algorithm(resolver), NULL);
934  if (status != LDNS_STATUS_OK) {
935  /* to prevent problems on subsequent calls to
936  * ldns_axfr_start we have to close the socket here! */
937  close_socket(resolver->_socket);
938  resolver->_socket = 0;
939 
940  ldns_pkt_free(query);
941  LDNS_FREE(ns);
942 
944  }
945  }
946 #endif /* HAVE_SSL */
947 
948  /* Convert the query to a buffer
949  * Is this necessary?
950  */
951  query_wire = ldns_buffer_new(LDNS_MAX_PACKETLEN);
952  if(!query_wire) {
953  ldns_pkt_free(query);
954  LDNS_FREE(ns);
955 
956  close_socket(resolver->_socket);
957 
958  return LDNS_STATUS_MEM_ERR;
959  }
960  status = ldns_pkt2buffer_wire(query_wire, query);
961  if (status != LDNS_STATUS_OK) {
962  ldns_pkt_free(query);
963  ldns_buffer_free(query_wire);
964  LDNS_FREE(ns);
965 
966  /* to prevent problems on subsequent calls to ldns_axfr_start
967  * we have to close the socket here! */
968  close_socket(resolver->_socket);
969  resolver->_socket = 0;
970 
971  return status;
972  }
973  /* Send the query */
974  if (ldns_tcp_send_query(query_wire, resolver->_socket, ns,
975  (socklen_t)ns_len) == 0) {
976  ldns_pkt_free(query);
977  ldns_buffer_free(query_wire);
978  LDNS_FREE(ns);
979 
980  /* to prevent problems on subsequent calls to ldns_axfr_start
981  * we have to close the socket here! */
982 
983 
984  close_socket(resolver->_socket);
985 
987  }
988 
989  ldns_pkt_free(query);
990  ldns_buffer_free(query_wire);
991  LDNS_FREE(ns);
992 
993  /*
994  * The AXFR is done once the second SOA record is sent
995  */
996  resolver->_axfr_soa_count = 0;
997  return LDNS_STATUS_OK;
998 }
ldns_rdf * ldns_rr_rdf(const ldns_rr *rr, size_t nr)
returns the rdata field member counter.
Definition: rr.c:895
uint8_t ldns_resolver_ip6(const ldns_resolver *r)
Does the resolver use ip6 or ip4.
Definition: resolver.c:60
implementation of buffers to ease operations
Definition: buffer.h:50
ldns_status ldns_pkt_tsig_sign(ldns_pkt *pkt, const char *key_name, const char *key_data, uint16_t fudge, const char *algorithm_name, const ldns_rdf *query_mac)
creates a tsig rr for the given packet and key.
Definition: tsig.c:364
uint8_t ldns_resolver_retry(const ldns_resolver *r)
Get the number of retries.
Definition: resolver.c:42
DNS stub resolver structure.
Definition: resolver.h:59
enum ldns_enum_rr_class ldns_rr_class
Definition: rr.h:64
ldns_status ldns_axfr_start(ldns_resolver *resolver, const ldns_rdf *domain, ldns_rr_class class)
Prepares the resolver for an axfr query The query is sent and the answers can be read with ldns_axfr_...
Definition: net.c:860
void ldns_resolver_set_nameserver_rtt(ldns_resolver *r, size_t pos, size_t value)
Set round trip time for a specific nameserver.
Definition: resolver.c:506
#define LDNS_RESOLV_INET6
Definition: resolver.h:51
#define LDNS_XMALLOC(type, count)
Definition: util.h:51
#define LDNS_MIN_BUFLEN
number of initial bytes in buffer of which we cannot tell the size before hand
Definition: buffer.h:33
size_t ldns_rdf_size(const ldns_rdf *rd)
returns the size of the rdf.
Definition: rdata.c:24
ldns_rdf * ldns_sockaddr_storage2rdf(const struct sockaddr_storage *sock, uint16_t *port)
returns an rdf with the sockaddr info.
Definition: net.c:825
void ldns_buffer_free(ldns_buffer *buffer)
frees the buffer.
Definition: buffer.c:137
ldns_rdf * ldns_rdf_clone(const ldns_rdf *rd)
clones a rdf structure.
Definition: rdata.c:222
int ldns_tcp_bgsend(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout)
Send an tcp query and don&#39;t wait for an answer but return the socket.
Definition: net.c:311
#define LDNS_MAX_PACKETLEN
Definition: packet.h:24
uint16_t ldns_resolver_port(const ldns_resolver *r)
Get the port the resolver should use.
Definition: resolver.c:24
const char * ldns_resolver_tsig_keydata(const ldns_resolver *r)
Return the tsig keydata as used by the nameserver.
Definition: resolver.c:219
ssize_t ldns_udp_send_query(ldns_buffer *qbin, int sockfd, const struct sockaddr_storage *to, socklen_t tolen)
send a query via udp to a server.
Definition: net.c:668
ldns_pkt * ldns_pkt_query_new(ldns_rdf *rr_name, ldns_rr_type rr_type, ldns_rr_class rr_class, uint16_t flags)
creates a packet with a query in it for the given name, type and class.
Definition: packet.c:1042
bool ldns_pkt_tsig_verify(ldns_pkt *pkt, const uint8_t *wire, size_t wirelen, const char *key_name, const char *key_data, const ldns_rdf *orig_mac_rdf)
verifies the tsig rr for the given packet and key.
Definition: tsig.c:288
#define LDNS_XREALLOC(ptr, type, count)
Definition: util.h:57
#define FD_SET_T
Definition: config.h:563
void ldns_pkt_set_size(ldns_pkt *packet, size_t s)
Set the packet&#39;s size.
Definition: packet.c:591
ldns_status ldns_send(ldns_pkt **result_packet, ldns_resolver *r, const ldns_pkt *query_pkt)
Sends ptk to the nameserver at the resolver object.
Definition: net.c:38
Including this file will include all ldns files, and define some lookup tables.
int ldns_udp_connect(const struct sockaddr_storage *to, struct timeval timeout __attribute__((unused)))
Definition: net.c:364
uint8_t * ldns_rdf_data(const ldns_rdf *rd)
returns the data of the rdf.
Definition: rdata.c:38
#define LDNS_IP4ADDRLEN
Definition: ldns.h:131
ldns_status ldns_pkt2buffer_wire(ldns_buffer *buffer, const ldns_pkt *packet)
Copies the packet data to the buffer in wire format.
Definition: host2wire.c:366
ldns_status ldns_wire2pkt(ldns_pkt **packet_p, const uint8_t *wire, size_t max)
converts the data on the uint8_t bytearray (in wire format) to a DNS packet.
Definition: wire2host.c:404
void ldns_resolver_nameservers_randomize(ldns_resolver *r)
Randomize the nameserver list in the resolver.
Definition: resolver.c:1570
#define ATTR_UNUSED(x)
Definition: common.h:69
bool ldns_resolver_usevc(const ldns_resolver *r)
Does the resolver use tcp or udp.
Definition: resolver.c:171
size_t ldns_resolver_nameserver_count(const ldns_resolver *r)
How many nameserver are configured in the resolver.
Definition: resolver.c:114
ldns_status ldns_send_buffer(ldns_pkt **result, ldns_resolver *r, ldns_buffer *qb, ldns_rdf *tsig_mac)
Sends and ldns_buffer (presumably containing a packet to the nameserver at the resolver object...
Definition: net.c:459
int _axfr_soa_count
Count the number of LDNS_RR_TYPE_SOA RRs we have seen so far (the second one signifies the end of the...
Definition: resolver.h:121
A record.
Definition: rdata.h:58
#define LDNS_IP6ADDRLEN
Definition: ldns.h:132
AAAA record.
Definition: rdata.h:60
ldns_rdf ** _nameservers
Array of nameservers to query (IP addresses or dnames)
Definition: resolver.h:65
uint8_t * ldns_tcp_read_wire_timeout(int sockfd, size_t *size, struct timeval timeout)
Gives back a raw packet from the wire and reads the header data from the given socket.
Definition: net.c:716
void ldns_pkt_set_answerfrom(ldns_pkt *packet, ldns_rdf *answerfrom)
Set the packet&#39;s answering server.
Definition: packet.c:578
#define LDNS_PORT
Definition: ldns.h:133
ldns_rdf_type ldns_rdf_get_type(const ldns_rdf *rd)
returns the type of the rdf.
Definition: rdata.c:31
DNS packet.
Definition: packet.h:233
int ldns_udp_bgsend(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout)
Send an udp query and don&#39;t wait for an answer but return the socket.
Definition: net.c:404
uint8_t * ldns_tcp_read_wire(int sockfd, size_t *size)
This routine may block.
Definition: net.c:775
bool ldns_resolver_random(const ldns_resolver *r)
Does the resolver randomize the nameserver before usage.
Definition: resolver.c:225
const char * ldns_resolver_tsig_algorithm(const ldns_resolver *r)
Return the tsig algorithm as used by the nameserver.
Definition: resolver.c:213
ssize_t ldns_tcp_send_query(ldns_buffer *qbin, int sockfd, const struct sockaddr_storage *to, socklen_t tolen)
send a query via tcp to a server.
Definition: net.c:643
enum ldns_enum_status ldns_status
Definition: error.h:134
ldns_rdf * ldns_rdf_new_frm_data(ldns_rdf_type type, size_t size, const void *data)
allocates a new rdf structure and fills it.
Definition: rdata.c:193
#define LDNS_MALLOC(type)
Memory management macros.
Definition: util.h:49
void ldns_pkt_set_querytime(ldns_pkt *packet, uint32_t time)
Set the packet&#39;s query time.
Definition: packet.c:572
ldns_buffer * ldns_buffer_new(size_t capacity)
creates a new buffer with the specified capacity.
Definition: buffer.c:16
uint8_t ldns_resolver_retrans(const ldns_resolver *r)
Get the retransmit interval.
Definition: resolver.c:48
int ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout)
Create a tcp socket to the specified address.
Definition: net.c:282
size_t * ldns_resolver_rtt(const ldns_resolver *r)
Return the used round trip times for the nameservers.
Definition: resolver.c:177
bool ldns_resolver_fail(const ldns_resolver *r)
Does the resolver only try the first nameserver.
Definition: resolver.c:84
ldns_rdf * ldns_resolver_source(const ldns_resolver *r)
Get the source address the resolver should use.
Definition: resolver.c:30
ldns_rdf ** ldns_resolver_nameservers(const ldns_resolver *r)
Return the configured nameserver ip address.
Definition: resolver.c:108
#define close_socket(_s)
Definition: config.h:651
Resource record data field.
Definition: rdata.h:174
struct sockaddr_storage * ldns_rdf2native_sockaddr_storage(const ldns_rdf *rd, uint16_t port, size_t *size)
returns the native sockaddr representation from the rdf.
Definition: net.c:104
ldns_status ldns_tcp_send(uint8_t **result, ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout, size_t *answer_size)
Sends a buffer to an ip using tcp and return the respons as a ldns_pkt.
Definition: net.c:355
ldns_rr * ldns_pkt_tsig(const ldns_pkt *pkt)
Return the packet&#39;s tsig pseudo rr&#39;s.
Definition: packet.c:450
#define LDNS_FREE(ptr)
Definition: util.h:60
#define SOCK_INVALID
Definition: config.h:650
#define LDNS_RESOLV_RTT_INF
Definition: resolver.h:53
int _socket
Keep some things to make AXFR possible.
Definition: resolver.h:117
ldns_status ldns_udp_send(uint8_t **result, ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout, size_t *answer_size)
Sends a buffer to an ip using udp and return the respons as a ldns_pkt.
Definition: net.c:450
const char * ldns_resolver_tsig_keyname(const ldns_resolver *r)
Return the tsig keyname as used by the nameserver.
Definition: resolver.c:207
void ldns_pkt_free(ldns_pkt *packet)
frees the packet structure and all data that it contains.
Definition: packet.c:792
#define LDNS_RESOLV_INET
Definition: resolver.h:50
uint8_t * ldns_udp_read_wire(int sockfd, size_t *size, struct sockaddr_storage *from, socklen_t *fromlen)
Gives back a raw packet from the wire and reads the header data from the given socket.
Definition: net.c:686
struct timeval ldns_resolver_timeout(const ldns_resolver *r)
What is the timeout on socket connections.
Definition: resolver.c:201
void ldns_pkt_set_timestamp(ldns_pkt *packet, struct timeval timeval)
Set the packet&#39;s timestamp.
Definition: packet.c:584