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 -1;
202  }
203 #endif
204  if (from && bind(sockfd, (const struct sockaddr*)from, fromlen) == SOCK_INVALID){
205  close_socket(sockfd);
206  return -1;
207  }
208 
209  /* perform nonblocking connect, to be able to wait with select() */
210  ldns_sock_nonblock(sockfd);
211  if (connect(sockfd, (struct sockaddr*)to, tolen) == SOCK_INVALID) {
212 #ifndef USE_WINSOCK
213 #ifdef EINPROGRESS
214  if(errno != EINPROGRESS) {
215 #else
216  if(1) {
217 #endif
218  close_socket(sockfd);
219  return -1;
220  }
221 #else /* USE_WINSOCK */
222  if(WSAGetLastError() != WSAEINPROGRESS &&
223  WSAGetLastError() != WSAEWOULDBLOCK) {
224  close_socket(sockfd);
225  return -1;
226  }
227 #endif
228  /* error was only telling us that it would block */
229  }
230 
231  /* wait(write) until connected or error */
232  while(1) {
233  int error = 0;
234  socklen_t len = (socklen_t)sizeof(error);
235 
236  if(!ldns_sock_wait(sockfd, timeout, 1)) {
237  close_socket(sockfd);
238  return -1;
239  }
240 
241  /* check if there is a pending error for nonblocking connect */
242  if(getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void*)&error,
243  &len) < 0) {
244 #ifndef USE_WINSOCK
245  error = errno; /* on solaris errno is error */
246 #else
247  error = WSAGetLastError();
248 #endif
249  }
250 #ifndef USE_WINSOCK
251 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
252  if(error == EINPROGRESS || error == EWOULDBLOCK)
253  continue; /* try again */
254 #endif
255  else if(error != 0) {
256  close_socket(sockfd);
257  /* error in errno for our user */
258  errno = error;
259  return -1;
260  }
261 #else /* USE_WINSOCK */
262  if(error == WSAEINPROGRESS)
263  continue;
264  else if(error == WSAEWOULDBLOCK)
265  continue;
266  else if(error != 0) {
267  close_socket(sockfd);
268  errno = error;
269  return -1;
270  }
271 #endif /* USE_WINSOCK */
272  /* connected */
273  break;
274  }
275 
276  /* set the socket blocking again */
277  ldns_sock_block(sockfd);
278 
279  return sockfd;
280 }
281 
282 int
283 ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen,
284  struct timeval timeout)
285 {
286  int s = ldns_tcp_connect_from(to, tolen, NULL, 0, timeout);
287  return s > 0 ? s : 0;
288 }
289 
290 int
291 ldns_tcp_connect2(const struct sockaddr_storage *to, socklen_t tolen,
292  struct timeval timeout)
293 {
294  return ldns_tcp_connect_from(to, tolen, NULL, 0, timeout);
295 }
296 
297 static int
298 ldns_tcp_bgsend_from(ldns_buffer *qbin,
299  const struct sockaddr_storage *to, socklen_t tolen,
300  const struct sockaddr_storage *from, socklen_t fromlen,
301  struct timeval timeout)
302 {
303  int sockfd;
304 
305  sockfd = ldns_tcp_connect_from(to, tolen, from, fromlen, timeout);
306 
307  if (sockfd >= 0 && ldns_tcp_send_query(qbin, sockfd, to, tolen) == 0) {
308  close_socket(sockfd);
309  return -1;
310  }
311 
312  return sockfd;
313 }
314 
315 int
317  const struct sockaddr_storage *to, socklen_t tolen,
318  struct timeval timeout)
319 {
320  int s = ldns_tcp_bgsend_from(qbin, to, tolen, NULL, 0, timeout);
321  return s > 0 ? s : 0;
322 }
323 
324 int
326  const struct sockaddr_storage *to, socklen_t tolen,
327  struct timeval timeout)
328 {
329  return ldns_tcp_bgsend_from(qbin, to, tolen, NULL, 0, timeout);
330 }
331 
332 /* keep in mind that in DNS tcp messages the first 2 bytes signal the
333  * amount data to expect
334  */
335 static ldns_status
336 ldns_tcp_send_from(uint8_t **result, ldns_buffer *qbin,
337  const struct sockaddr_storage *to, socklen_t tolen,
338  const struct sockaddr_storage *from, socklen_t fromlen,
339  struct timeval timeout, size_t *answer_size)
340 {
341  int sockfd;
342  uint8_t *answer;
343 
344  sockfd = ldns_tcp_bgsend_from(qbin, to, tolen, from, fromlen, timeout);
345 
346  if (sockfd == -1) {
347  return LDNS_STATUS_ERR;
348  }
349 
350  answer = ldns_tcp_read_wire_timeout(sockfd, answer_size, timeout);
351  close_socket(sockfd);
352 
353  if (!answer) {
354  /* oops */
356  }
357 
358  *result = answer;
359  return LDNS_STATUS_OK;
360 }
361 
363 ldns_tcp_send(uint8_t **result, ldns_buffer *qbin,
364  const struct sockaddr_storage *to, socklen_t tolen,
365  struct timeval timeout, size_t *answer_size)
366 {
367  return ldns_tcp_send_from(result, qbin,
368  to, tolen, NULL, 0, timeout, answer_size);
369 }
370 
371 int
372 ldns_udp_connect(const struct sockaddr_storage *to, struct timeval ATTR_UNUSED(timeout))
373 {
374  int sockfd;
375 
376 #ifndef S_SPLINT_S
377  if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_DGRAM,
378  IPPROTO_UDP))
379  == SOCK_INVALID) {
380  return 0;
381  }
382 #endif
383  return sockfd;
384 }
385 
386 int
387 ldns_udp_connect2(const struct sockaddr_storage *to, struct timeval ATTR_UNUSED(timeout))
388 {
389  int sockfd;
390 
391 #ifndef S_SPLINT_S
392  if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_DGRAM,
393  IPPROTO_UDP))
394  == SOCK_INVALID) {
395  return -1;
396  }
397 #endif
398  return sockfd;
399 }
400 
401 static int
402 ldns_udp_bgsend_from(ldns_buffer *qbin,
403  const struct sockaddr_storage *to , socklen_t tolen,
404  const struct sockaddr_storage *from, socklen_t fromlen,
405  struct timeval timeout)
406 {
407  int sockfd;
408 
409  sockfd = ldns_udp_connect2(to, timeout);
410 
411  if (sockfd == -1) {
412  return -1;
413  }
414 
415  if (from && bind(sockfd, (const struct sockaddr*)from, fromlen) == -1){
416  close_socket(sockfd);
417  return -1;
418  }
419 
420  if (ldns_udp_send_query(qbin, sockfd, to, tolen) == 0) {
421  close_socket(sockfd);
422  return -1;
423  }
424  return sockfd;
425 }
426 
427 int
429  const struct sockaddr_storage *to , socklen_t tolen,
430  struct timeval timeout)
431 {
432  int s = ldns_udp_bgsend_from(qbin, to, tolen, NULL, 0, timeout);
433  return s > 0 ? s : 0;
434 }
435 
436 int
438  const struct sockaddr_storage *to , socklen_t tolen,
439  struct timeval timeout)
440 {
441  return ldns_udp_bgsend_from(qbin, to, tolen, NULL, 0, timeout);
442 }
443 
444 static ldns_status
445 ldns_udp_send_from(uint8_t **result, ldns_buffer *qbin,
446  const struct sockaddr_storage *to , socklen_t tolen,
447  const struct sockaddr_storage *from, socklen_t fromlen,
448  struct timeval timeout, size_t *answer_size)
449 {
450  int sockfd;
451  uint8_t *answer;
452 
453  sockfd = ldns_udp_bgsend_from(qbin, to, tolen, from, fromlen, timeout);
454 
455  if (sockfd == -1) {
457  }
458 
459  /* wait for an response*/
460  if(!ldns_sock_wait(sockfd, timeout, 0)) {
461  close_socket(sockfd);
463  }
464 
465  /* set to nonblocking, so if the checksum is bad, it becomes
466  * an EGAIN error and the ldns_udp_send function does not block,
467  * but returns a 'NETWORK_ERROR' much like a timeout. */
468  ldns_sock_nonblock(sockfd);
469 
470  answer = ldns_udp_read_wire(sockfd, answer_size, NULL, NULL);
471  close_socket(sockfd);
472 
473  if (!answer) {
474  /* oops */
476  }
477 
478  *result = answer;
479  return LDNS_STATUS_OK;
480 }
481 
483 ldns_udp_send(uint8_t **result, ldns_buffer *qbin,
484  const struct sockaddr_storage *to , socklen_t tolen,
485  struct timeval timeout, size_t *answer_size)
486 {
487  return ldns_udp_send_from(result, qbin, to, tolen, NULL, 0,
488  timeout, answer_size);
489 }
490 
493 {
494  uint8_t i;
495 
496  struct sockaddr_storage *src = NULL;
497  size_t src_len = 0;
498  struct sockaddr_storage *ns;
499  size_t ns_len;
500  struct timeval tv_s;
501  struct timeval tv_e;
502 
503  ldns_rdf **ns_array;
504  size_t *rtt;
505  ldns_pkt *reply;
506  bool all_servers_rtt_inf;
507  uint8_t retries;
508 
509  uint8_t *reply_bytes = NULL;
510  size_t reply_size = 0;
511  ldns_status status, send_status;
512 
513  assert(r != NULL);
514 
515  /* The query should at least have one question */
516  if(ldns_buffer_limit(qb) < 6 || ldns_buffer_read_u16_at(qb, 4) != 1)
518 
519  status = LDNS_STATUS_OK;
520  rtt = ldns_resolver_rtt(r);
521  ns_array = ldns_resolver_nameservers(r);
522  reply = NULL;
523  ns_len = 0;
524 
525  all_servers_rtt_inf = true;
526 
527  if (ldns_resolver_random(r)) {
529  }
530 
531  if(ldns_resolver_source(r)) {
532  src = ldns_rdf2native_sockaddr_storage_port(
533  ldns_resolver_source(r), 0, &src_len);
534  }
535 
536  /* loop through all defined nameservers */
537  for (i = 0; i < ldns_resolver_nameserver_count(r); i++) {
538  if (rtt[i] == LDNS_RESOLV_RTT_INF) {
539  /* not reachable nameserver! */
540  continue;
541  }
542 
543  /* maybe verbosity setting?
544  printf("Sending to ");
545  ldns_rdf_print(stdout, ns_array[i]);
546  printf("\n");
547  */
548  ns = ldns_rdf2native_sockaddr_storage(ns_array[i],
549  ldns_resolver_port(r), &ns_len);
550 
551 
552 #ifndef S_SPLINT_S
553  if ((ns->ss_family == AF_INET) &&
555  /* not reachable */
556  LDNS_FREE(ns);
557  continue;
558  }
559 
560  if ((ns->ss_family == AF_INET6) &&
562  /* not reachable */
563  LDNS_FREE(ns);
564  continue;
565  }
566 #endif
567 
568  all_servers_rtt_inf = false;
569 
570  gettimeofday(&tv_s, NULL);
571 
572  send_status = LDNS_STATUS_ERR;
573 
574  /* reply_bytes implicitly handles our error */
575  if (ldns_resolver_usevc(r)) {
576  for (retries = ldns_resolver_retry(r); retries > 0; retries--) {
577  send_status =
578  ldns_tcp_send_from(&reply_bytes, qb,
579  ns, (socklen_t)ns_len,
580  src, (socklen_t)src_len,
582  &reply_size);
583  if (send_status == LDNS_STATUS_OK) {
584  break;
585  }
586  }
587  } else {
588  for (retries = ldns_resolver_retry(r); retries > 0; retries--) {
589  /* ldns_rdf_print(stdout, ns_array[i]); */
590  send_status =
591  ldns_udp_send_from(&reply_bytes, qb,
592  ns, (socklen_t)ns_len,
593  src, (socklen_t)src_len,
595  &reply_size);
596  if (send_status == LDNS_STATUS_OK) {
597  break;
598  }
599  }
600  }
601 
602  if (send_status != LDNS_STATUS_OK) {
604  status = send_status;
605  }
606 
607  /* obey the fail directive */
608  if (!reply_bytes) {
609  /* the current nameserver seems to have a problem, blacklist it */
610  if (ldns_resolver_fail(r)) {
611  if(src) {
612  LDNS_FREE(src);
613  }
614  LDNS_FREE(ns);
615  return status ? status : LDNS_STATUS_ERR;
616  } else {
617  LDNS_FREE(ns);
618  continue;
619  }
620  }
621 
622  status = ldns_wire2pkt(&reply, reply_bytes, reply_size);
623  if (status != LDNS_STATUS_OK) {
624  if(src) LDNS_FREE(src);
625  LDNS_FREE(reply_bytes);
626  LDNS_FREE(ns);
627  return status;
628  }
629  assert(reply);
630 
631  LDNS_FREE(ns);
632  gettimeofday(&tv_e, NULL);
633 
634  if (reply) {
635  ldns_pkt_set_querytime(reply, (uint32_t)
636  ((tv_e.tv_sec - tv_s.tv_sec) * 1000) +
637  (tv_e.tv_usec - tv_s.tv_usec) / 1000);
639  ldns_rdf_clone(ns_array[i]));
640  ldns_pkt_set_timestamp(reply, tv_s);
641  ldns_pkt_set_size(reply, reply_size);
642  break;
643  } else {
644  if (ldns_resolver_fail(r)) {
645  /* if fail is set bail out, after the first
646  * one */
647  break;
648  }
649  }
650 
651  /* wait retrans seconds... */
652  sleep((unsigned int) ldns_resolver_retrans(r));
653  }
654 
655  if(src) {
656  LDNS_FREE(src);
657  }
658  if (all_servers_rtt_inf) {
659  LDNS_FREE(reply_bytes);
660  return LDNS_STATUS_RES_NO_NS;
661  }
662 #ifdef HAVE_SSL
663  if (tsig_mac && reply && reply_bytes) {
664  if (!ldns_pkt_tsig_verify(reply,
665  reply_bytes,
666  reply_size,
668  ldns_resolver_tsig_keydata(r), tsig_mac)) {
670  }
671  }
672 #else
673  (void)tsig_mac;
674 #endif /* HAVE_SSL */
675 
676  LDNS_FREE(reply_bytes);
677  if (reply) {
678  ldns_pkt *query = NULL;
679 
680  if(ldns_pkt_qdcount(reply) != 1) {
682  ldns_pkt_free(reply);
683  reply = NULL;
684 
685  } else if(ldns_wire2pkt(&query
686  , ldns_buffer_begin(qb)
687  , ldns_buffer_position(qb)) != LDNS_STATUS_OK
688  || ldns_pkt_qdcount(query) != 1
690  ,ldns_rr_list_rr(ldns_pkt_question(reply),0))){
692  ldns_pkt_free(reply);
693  reply = NULL;
694  }
695  ldns_pkt_free(query);
696  }
697  if (result) {
698  *result = reply;
699  }
700 
701  return status;
702 }
703 
704 ssize_t
705 ldns_tcp_send_query(ldns_buffer *qbin, int sockfd,
706  const struct sockaddr_storage *to, socklen_t tolen)
707 {
708  uint8_t *sendbuf;
709  ssize_t bytes;
710 
711  /* add length of packet */
712  sendbuf = LDNS_XMALLOC(uint8_t, ldns_buffer_position(qbin) + 2);
713  if(!sendbuf) return 0;
714  ldns_write_uint16(sendbuf, ldns_buffer_position(qbin));
715  memcpy(sendbuf + 2, ldns_buffer_begin(qbin), ldns_buffer_position(qbin));
716 
717  bytes = sendto(sockfd, (void*)sendbuf,
718  ldns_buffer_position(qbin) + 2, 0, (struct sockaddr *)to, tolen);
719 
720  LDNS_FREE(sendbuf);
721 
722  if (bytes == -1 || (size_t) bytes != ldns_buffer_position(qbin) + 2 ) {
723  return 0;
724  }
725  return bytes;
726 }
727 
728 /* don't wait for an answer */
729 ssize_t
730 ldns_udp_send_query(ldns_buffer *qbin, int sockfd, const struct sockaddr_storage *to,
731  socklen_t tolen)
732 {
733  ssize_t bytes;
734 
735  bytes = sendto(sockfd, (void*)ldns_buffer_begin(qbin),
736  ldns_buffer_position(qbin), 0, (struct sockaddr *)to, tolen);
737 
738  if (bytes == -1 || (size_t)bytes != ldns_buffer_position(qbin)) {
739  return 0;
740  }
741  return bytes;
742 }
743 
744 uint8_t *
745 ldns_udp_read_wire(int sockfd, size_t *size, struct sockaddr_storage *from,
746  socklen_t *fromlen)
747 {
748  uint8_t *wire, *wireout;
749  ssize_t wire_size;
750 
751  wire = LDNS_XMALLOC(uint8_t, LDNS_MAX_PACKETLEN);
752  if (!wire) {
753  *size = 0;
754  return NULL;
755  }
756 
757  wire_size = recvfrom(sockfd, (void*)wire, LDNS_MAX_PACKETLEN, 0,
758  (struct sockaddr *)from, fromlen);
759 
760  /* recvfrom can also return 0 */
761  if (wire_size == -1 || wire_size == 0) {
762  *size = 0;
763  LDNS_FREE(wire);
764  return NULL;
765  }
766 
767  *size = (size_t)wire_size;
768  wireout = LDNS_XREALLOC(wire, uint8_t, (size_t)wire_size);
769  if(!wireout) LDNS_FREE(wire);
770 
771  return wireout;
772 }
773 
774 uint8_t *
775 ldns_tcp_read_wire_timeout(int sockfd, size_t *size, struct timeval timeout)
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  if(!ldns_sock_wait(sockfd, timeout, 0)) {
789  *size = 0;
790  LDNS_FREE(wire);
791  return NULL;
792  }
793  rc = recv(sockfd, (void*) (wire + bytes),
794  (size_t) (2 - bytes), 0);
795  if (rc == -1 || rc == 0) {
796  *size = 0;
797  LDNS_FREE(wire);
798  return NULL;
799  }
800  bytes += rc;
801  }
802 
803  wire_size = ldns_read_uint16(wire);
804 
805  LDNS_FREE(wire);
806  wire = LDNS_XMALLOC(uint8_t, wire_size);
807  if (!wire) {
808  *size = 0;
809  return NULL;
810  }
811  bytes = 0;
812 
813  while (bytes < (ssize_t) wire_size) {
814  if(!ldns_sock_wait(sockfd, timeout, 0)) {
815  *size = 0;
816  LDNS_FREE(wire);
817  return NULL;
818  }
819  rc = recv(sockfd, (void*) (wire + bytes),
820  (size_t) (wire_size - bytes), 0);
821  if (rc == -1 || rc == 0) {
822  LDNS_FREE(wire);
823  *size = 0;
824  return NULL;
825  }
826  bytes += rc;
827  }
828 
829  *size = (size_t) bytes;
830  return wire;
831 }
832 
833 uint8_t *
834 ldns_tcp_read_wire(int sockfd, size_t *size)
835 {
836  uint8_t *wire;
837  uint16_t wire_size;
838  ssize_t bytes = 0, rc = 0;
839 
840  wire = LDNS_XMALLOC(uint8_t, 2);
841  if (!wire) {
842  *size = 0;
843  return NULL;
844  }
845 
846  while (bytes < 2) {
847  rc = recv(sockfd, (void*) (wire + bytes),
848  (size_t) (2 - bytes), 0);
849  if (rc == -1 || rc == 0) {
850  *size = 0;
851  LDNS_FREE(wire);
852  return NULL;
853  }
854  bytes += rc;
855  }
856 
857  wire_size = ldns_read_uint16(wire);
858 
859  LDNS_FREE(wire);
860  wire = LDNS_XMALLOC(uint8_t, wire_size);
861  if (!wire) {
862  *size = 0;
863  return NULL;
864  }
865  bytes = 0;
866 
867  while (bytes < (ssize_t) wire_size) {
868  rc = recv(sockfd, (void*) (wire + bytes),
869  (size_t) (wire_size - bytes), 0);
870  if (rc == -1 || rc == 0) {
871  LDNS_FREE(wire);
872  *size = 0;
873  return NULL;
874  }
875  bytes += rc;
876  }
877 
878  *size = (size_t) bytes;
879  return wire;
880 }
881 
882 #ifndef S_SPLINT_S
883 ldns_rdf *
884 ldns_sockaddr_storage2rdf(const struct sockaddr_storage *sock, uint16_t *port)
885 {
886  ldns_rdf *addr;
887  struct sockaddr_in *data_in;
888  struct sockaddr_in6 *data_in6;
889 
890  switch(sock->ss_family) {
891  case AF_INET:
892  data_in = (struct sockaddr_in*)sock;
893  if (port) {
894  *port = ntohs((uint16_t)data_in->sin_port);
895  }
897  LDNS_IP4ADDRLEN, &data_in->sin_addr);
898  break;
899  case AF_INET6:
900  data_in6 = (struct sockaddr_in6*)sock;
901  if (port) {
902  *port = ntohs((uint16_t)data_in6->sin6_port);
903  }
905  LDNS_IP6ADDRLEN, &data_in6->sin6_addr);
906  break;
907  default:
908  if (port) {
909  *port = 0;
910  }
911  return NULL;
912  }
913  return addr;
914 }
915 #endif
916 
917 /* code from resolver.c */
919 ldns_axfr_start(ldns_resolver *resolver, const ldns_rdf *domain, ldns_rr_class class)
920 {
921  ldns_pkt *query;
922  ldns_buffer *query_wire;
923 
924  struct sockaddr_storage *src = NULL;
925  size_t src_len = 0;
926  struct sockaddr_storage *ns = NULL;
927  size_t ns_len = 0;
928  size_t ns_i;
929  ldns_status status;
930 
931  if (!resolver || ldns_resolver_nameserver_count(resolver) < 1) {
932  return LDNS_STATUS_ERR;
933  }
934 
935  query = ldns_pkt_query_new(ldns_rdf_clone(domain), LDNS_RR_TYPE_AXFR, class, 0);
936 
937  if (!query) {
939  }
940  if(ldns_resolver_source(resolver)) {
941  src = ldns_rdf2native_sockaddr_storage_port(
942  ldns_resolver_source(resolver), 0, &src_len);
943  }
944  /* For AXFR, we have to make the connection ourselves */
945  /* try all nameservers (which usually would mean v4 fallback if
946  * @hostname is used */
947  for (ns_i = 0;
948  ns_i < ldns_resolver_nameserver_count(resolver) &&
949  resolver->_socket == SOCK_INVALID;
950  ns_i++) {
951  if (ns != NULL) {
952  LDNS_FREE(ns);
953  }
955  resolver->_nameservers[ns_i],
956  ldns_resolver_port(resolver), &ns_len);
957 #ifndef S_SPLINT_S
958  if ((ns->ss_family == AF_INET) &&
959  (ldns_resolver_ip6(resolver) == LDNS_RESOLV_INET6)) {
960  /* not reachable */
961  LDNS_FREE(ns);
962  ns = NULL;
963  continue;
964  }
965 
966  if ((ns->ss_family == AF_INET6) &&
967  (ldns_resolver_ip6(resolver) == LDNS_RESOLV_INET)) {
968  /* not reachable */
969  LDNS_FREE(ns);
970  ns = NULL;
971  continue;
972  }
973 #endif
974 
975  resolver->_socket = ldns_tcp_connect_from(
976  ns, (socklen_t)ns_len,
977  src, (socklen_t)src_len,
978  ldns_resolver_timeout(resolver));
979  }
980  if (src) {
981  LDNS_FREE(src);
982  }
983 
984  if (resolver->_socket == SOCK_INVALID) {
985  ldns_pkt_free(query);
986  LDNS_FREE(ns);
988  }
989 
990 #ifdef HAVE_SSL
991  if (ldns_resolver_tsig_keyname(resolver) && ldns_resolver_tsig_keydata(resolver)) {
992  status = ldns_pkt_tsig_sign(query,
993  ldns_resolver_tsig_keyname(resolver),
994  ldns_resolver_tsig_keydata(resolver),
995  300, ldns_resolver_tsig_algorithm(resolver), NULL);
996  if (status != LDNS_STATUS_OK) {
997  /* to prevent problems on subsequent calls to
998  * ldns_axfr_start we have to close the socket here! */
999  close_socket(resolver->_socket);
1000  resolver->_socket = 0;
1001 
1002  ldns_pkt_free(query);
1003  LDNS_FREE(ns);
1004 
1006  }
1007  }
1008 #endif /* HAVE_SSL */
1009 
1010  /* Convert the query to a buffer
1011  * Is this necessary?
1012  */
1013  query_wire = ldns_buffer_new(LDNS_MAX_PACKETLEN);
1014  if(!query_wire) {
1015  ldns_pkt_free(query);
1016  LDNS_FREE(ns);
1017 
1018  close_socket(resolver->_socket);
1019 
1020  return LDNS_STATUS_MEM_ERR;
1021  }
1022  status = ldns_pkt2buffer_wire(query_wire, query);
1023  if (status != LDNS_STATUS_OK) {
1024  ldns_pkt_free(query);
1025  ldns_buffer_free(query_wire);
1026  LDNS_FREE(ns);
1027 
1028  /* to prevent problems on subsequent calls to ldns_axfr_start
1029  * we have to close the socket here! */
1030  close_socket(resolver->_socket);
1031  resolver->_socket = 0;
1032 
1033  return status;
1034  }
1035  /* Send the query */
1036  if (ldns_tcp_send_query(query_wire, resolver->_socket, ns,
1037  (socklen_t)ns_len) == 0) {
1038  ldns_pkt_free(query);
1039  ldns_buffer_free(query_wire);
1040  LDNS_FREE(ns);
1041 
1042  /* to prevent problems on subsequent calls to ldns_axfr_start
1043  * we have to close the socket here! */
1044 
1045 
1046  close_socket(resolver->_socket);
1047 
1048  return LDNS_STATUS_NETWORK_ERR;
1049  }
1050 
1051  ldns_pkt_free(query);
1052  ldns_buffer_free(query_wire);
1053  LDNS_FREE(ns);
1054 
1055  /*
1056  * The AXFR is done once the second SOA record is sent
1057  */
1058  resolver->_axfr_soa_count = 0;
1059  return LDNS_STATUS_OK;
1060 }
void ldns_buffer_free(ldns_buffer *buffer)
frees the buffer.
Definition: buffer.c:137
ldns_buffer * ldns_buffer_new(size_t capacity)
creates a new buffer with the specified capacity.
Definition: buffer.c:16
#define LDNS_MIN_BUFLEN
number of initial bytes in buffer of which we cannot tell the size before hand
Definition: buffer.h:33
#define ATTR_UNUSED(x)
Definition: common.h:69
#define close_socket(_s)
Definition: config.h:730
#define SOCK_INVALID
Definition: config.h:729
#define FD_SET_T
Definition: config.h:642
@ LDNS_STATUS_NETWORK_ERR
Definition: error.h:46
@ LDNS_STATUS_SOCKET_ERROR
Definition: error.h:96
@ LDNS_STATUS_ERR
Definition: error.h:37
@ LDNS_STATUS_MEM_ERR
Definition: error.h:34
@ LDNS_STATUS_QDCOUNT_MUST_BE_ONE
Definition: error.h:135
@ LDNS_STATUS_CRYPTO_TSIG_BOGUS
Definition: error.h:64
@ LDNS_STATUS_QUERY_DID_NOT_MATCH
Definition: error.h:136
@ LDNS_STATUS_ADDRESS_ERR
Definition: error.h:47
@ LDNS_STATUS_OK
Definition: error.h:26
@ LDNS_STATUS_CRYPTO_TSIG_ERR
Definition: error.h:65
@ LDNS_STATUS_RES_NO_NS
Definition: error.h:70
enum ldns_enum_status ldns_status
Definition: error.h:138
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:376
Including this file will include all ldns files, and define some lookup tables.
#define LDNS_IP4ADDRLEN
Definition: ldns.h:131
#define LDNS_IP6ADDRLEN
Definition: ldns.h:132
#define LDNS_PORT
Definition: ldns.h:133
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:775
int ldns_udp_connect(const struct sockaddr_storage *to, struct timeval timeout __attribute__((unused)))
Definition: net.c:372
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:919
uint8_t * ldns_tcp_read_wire(int sockfd, size_t *size)
This routine may block.
Definition: net.c:834
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
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:745
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:705
int ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout)
Create a tcp socket to the specified address This function has the flaw that it returns 0 on failure,...
Definition: net.c:283
int ldns_tcp_connect2(const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout)
Create a tcp socket to the specified address.
Definition: net.c:291
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:492
int ldns_udp_bgsend(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout)
Send an udp query and don't wait for an answer but return the socket This function has the flaw that ...
Definition: net.c:428
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
int ldns_tcp_bgsend(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout)
Send an tcp query and don't wait for an answer but return the socket This function has the flaw that ...
Definition: net.c:316
int ldns_tcp_bgsend2(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout)
Send an tcp query and don't wait for an answer but return the socket.
Definition: net.c:325
int ldns_udp_bgsend2(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout)
Send an udp query and don't wait for an answer but return the socket.
Definition: net.c:437
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:483
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:363
ldns_rdf * ldns_sockaddr_storage2rdf(const struct sockaddr_storage *sock, uint16_t *port)
returns an rdf with the sockaddr info.
Definition: net.c:884
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:730
int ldns_udp_connect2(const struct sockaddr_storage *to, struct timeval timeout __attribute__((unused)))
Definition: net.c:387
void ldns_pkt_free(ldns_pkt *packet)
frees the packet structure and all data that it contains.
Definition: packet.c:807
void ldns_pkt_set_querytime(ldns_pkt *packet, uint32_t time)
Set the packet's query time.
Definition: packet.c:587
ldns_rr_list * ldns_pkt_question(const ldns_pkt *packet)
Return the packet's question section.
Definition: packet.c:124
void ldns_pkt_set_timestamp(ldns_pkt *packet, struct timeval timeval)
Set the packet's timestamp.
Definition: packet.c:599
void ldns_pkt_set_size(ldns_pkt *packet, size_t s)
Set the packet's size.
Definition: packet.c:606
uint16_t ldns_pkt_qdcount(const ldns_pkt *packet)
Return the packet's qd count.
Definition: packet.c:100
ldns_rr * ldns_pkt_tsig(const ldns_pkt *pkt)
Return the packet's tsig pseudo rr's.
Definition: packet.c:465
void ldns_pkt_set_answerfrom(ldns_pkt *packet, ldns_rdf *answerfrom)
Set the packet's answering server.
Definition: packet.c:593
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:1059
#define LDNS_MAX_PACKETLEN
Definition: packet.h:24
ldns_rdf_type ldns_rdf_get_type(const ldns_rdf *rd)
returns the type of the rdf.
Definition: rdata.c:31
size_t ldns_rdf_size(const ldns_rdf *rd)
returns the size of the rdf.
Definition: rdata.c:24
uint8_t * ldns_rdf_data(const ldns_rdf *rd)
returns the data of the rdf.
Definition: rdata.c:38
ldns_rdf * ldns_rdf_clone(const ldns_rdf *rd)
clones a rdf structure.
Definition: rdata.c:222
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
@ LDNS_RDF_TYPE_AAAA
AAAA record.
Definition: rdata.h:60
@ LDNS_RDF_TYPE_A
A record.
Definition: rdata.h:58
const char * ldns_resolver_tsig_algorithm(const ldns_resolver *r)
Return the tsig algorithm as used by the nameserver.
Definition: resolver.c:213
const char * ldns_resolver_tsig_keydata(const ldns_resolver *r)
Return the tsig keydata as used by the nameserver.
Definition: resolver.c:219
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
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
uint8_t ldns_resolver_retrans(const ldns_resolver *r)
Get the retransmit interval.
Definition: resolver.c:48
void ldns_resolver_nameservers_randomize(ldns_resolver *r)
Randomize the nameserver list in the resolver.
Definition: resolver.c:1576
bool ldns_resolver_random(const ldns_resolver *r)
Does the resolver randomize the nameserver before usage.
Definition: resolver.c:225
struct timeval ldns_resolver_timeout(const ldns_resolver *r)
What is the timeout on socket connections.
Definition: resolver.c:201
size_t * ldns_resolver_rtt(const ldns_resolver *r)
Return the used round trip times for the nameservers.
Definition: resolver.c:177
uint8_t ldns_resolver_ip6(const ldns_resolver *r)
Does the resolver use ip6 or ip4.
Definition: resolver.c:60
uint8_t ldns_resolver_retry(const ldns_resolver *r)
Get the number of retries.
Definition: resolver.c:42
bool ldns_resolver_fail(const ldns_resolver *r)
Does the resolver only try the first nameserver.
Definition: resolver.c:84
const char * ldns_resolver_tsig_keyname(const ldns_resolver *r)
Return the tsig keyname as used by the nameserver.
Definition: resolver.c:207
ldns_rdf ** ldns_resolver_nameservers(const ldns_resolver *r)
Return the configured nameserver ip address.
Definition: resolver.c:108
ldns_rdf * ldns_resolver_source(const ldns_resolver *r)
Get the source address the resolver should use.
Definition: resolver.c:30
uint16_t ldns_resolver_port(const ldns_resolver *r)
Get the port the resolver should use.
Definition: resolver.c:24
#define LDNS_RESOLV_RTT_INF
Definition: resolver.h:53
#define LDNS_RESOLV_INET
Definition: resolver.h:50
#define LDNS_RESOLV_INET6
Definition: resolver.h:51
ldns_rr * ldns_rr_list_rr(const ldns_rr_list *rr_list, size_t nr)
returns a specific rr of an rrlist.
Definition: rr.c:985
int ldns_rr_compare(const ldns_rr *rr1, const ldns_rr *rr2)
compares two rrs.
Definition: rr.c:1632
ldns_rdf * ldns_rr_rdf(const ldns_rr *rr, size_t nr)
returns the rdata field member counter.
Definition: rr.c:904
@ LDNS_RR_TYPE_AXFR
Definition: rr.h:214
enum ldns_enum_rr_class ldns_rr_class
Definition: rr.h:61
implementation of buffers to ease operations
Definition: buffer.h:51
DNS packet.
Definition: packet.h:234
Resource record data field.
Definition: rdata.h:178
DNS stub resolver structure.
Definition: resolver.h:60
int _socket
Keep some things to make AXFR possible.
Definition: resolver.h:117
ldns_rdf ** _nameservers
Array of nameservers to query (IP addresses or dnames)
Definition: resolver.h:65
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
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
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:370
#define LDNS_FREE(ptr)
Definition: util.h:60
#define LDNS_MALLOC(type)
Memory management macros.
Definition: util.h:49
#define LDNS_XMALLOC(type, count)
Definition: util.h:51
#define LDNS_XREALLOC(ptr, type, count)
Definition: util.h:57
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:405