Update to ldns-1.6.7
[dragonfly.git] / contrib / ldns / net.c
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
34 ldns_status
35 ldns_send(ldns_pkt **result_packet, ldns_resolver *r, const ldns_pkt *query_pkt)
36 {
37         ldns_buffer *qb;
38         ldns_status result;
39         ldns_rdf *tsig_mac = NULL;
40
41         qb = ldns_buffer_new(LDNS_MIN_BUFLEN);
42
43         if (query_pkt && ldns_pkt_tsig(query_pkt)) {
44                 tsig_mac = ldns_rr_rdf(ldns_pkt_tsig(query_pkt), 3);
45         }
46
47         if (!query_pkt ||
48             ldns_pkt2buffer_wire(qb, query_pkt) != LDNS_STATUS_OK) {
49                 result = LDNS_STATUS_ERR;
50         } else {
51                 result = ldns_send_buffer(result_packet, r, qb, tsig_mac);
52         }
53
54         ldns_buffer_free(qb);
55
56         return result;
57 }
58
59 ldns_status
60 ldns_send_buffer(ldns_pkt **result, ldns_resolver *r, ldns_buffer *qb, ldns_rdf *tsig_mac)
61 {
62         uint8_t i;
63         
64         struct sockaddr_storage *ns;
65         size_t ns_len;
66         struct timeval tv_s;
67         struct timeval tv_e;
68
69         ldns_rdf **ns_array;
70         size_t *rtt;
71         ldns_pkt *reply;
72         bool all_servers_rtt_inf;
73         uint8_t retries;
74
75         uint8_t *reply_bytes = NULL;
76         size_t reply_size = 0;
77         ldns_status status, send_status;
78
79         assert(r != NULL);
80
81         status = LDNS_STATUS_OK;
82         rtt = ldns_resolver_rtt(r);
83         ns_array = ldns_resolver_nameservers(r);
84         reply = NULL; 
85         ns_len = 0;
86
87         all_servers_rtt_inf = true;
88
89         if (ldns_resolver_random(r)) {
90                 ldns_resolver_nameservers_randomize(r);
91         }
92
93         /* loop through all defined nameservers */
94         for (i = 0; i < ldns_resolver_nameserver_count(r); i++) {
95                 if (rtt[i] == LDNS_RESOLV_RTT_INF) {
96                         /* not reachable nameserver! */
97                         continue;
98                 }
99
100                 /* maybe verbosity setting?
101                 printf("Sending to ");
102                 ldns_rdf_print(stdout, ns_array[i]);
103                 printf("\n");
104                 */
105                 ns = ldns_rdf2native_sockaddr_storage(ns_array[i],
106                                 ldns_resolver_port(r), &ns_len);
107
108                 if ((ns->ss_family == AF_INET) &&
109                                 (ldns_resolver_ip6(r) == LDNS_RESOLV_INET6)) {
110                         /* not reachable */
111                         continue;
112                 }
113
114                 if ((ns->ss_family == AF_INET6) &&
115                                  (ldns_resolver_ip6(r) == LDNS_RESOLV_INET)) {
116                         /* not reachable */
117                         continue;
118                 }
119
120                 all_servers_rtt_inf = false;
121
122                 gettimeofday(&tv_s, NULL);
123
124                 send_status = LDNS_STATUS_ERR;
125
126                 /* reply_bytes implicitly handles our error */
127                 if (1 == ldns_resolver_usevc(r)) {
128                         for (retries = ldns_resolver_retry(r); retries > 0; retries--) {
129                                 send_status = 
130                                         ldns_tcp_send(&reply_bytes, qb, ns, 
131                                         (socklen_t)ns_len, ldns_resolver_timeout(r), 
132                                         &reply_size);
133                                 if (send_status == LDNS_STATUS_OK) {
134                                         break;
135                                 }
136                         }
137                 } else {
138                         for (retries = ldns_resolver_retry(r); retries > 0; retries--) {
139                                 /* ldns_rdf_print(stdout, ns_array[i]); */
140                                 send_status = 
141                                         ldns_udp_send(&reply_bytes, qb, ns, 
142                                                         (socklen_t)ns_len, ldns_resolver_timeout(r), 
143                                                         &reply_size);
144                                 
145                                 if (send_status == LDNS_STATUS_OK) {
146                                         break;
147                                 }
148                         }
149                 }
150
151                 if (send_status != LDNS_STATUS_OK) {
152                         ldns_resolver_set_nameserver_rtt(r, i, LDNS_RESOLV_RTT_INF);
153                         status = send_status;
154                 }
155                 
156                 /* obey the fail directive */
157                 if (!reply_bytes) {
158                         /* the current nameserver seems to have a problem, blacklist it */
159                         if (ldns_resolver_fail(r)) {
160                                 LDNS_FREE(ns);
161                                 return LDNS_STATUS_ERR;
162                         } else {
163                                 LDNS_FREE(ns);
164                                 continue;
165                         }
166                 } 
167                 
168                 status = ldns_wire2pkt(&reply, reply_bytes, reply_size);
169                 if (status != LDNS_STATUS_OK) {
170                         LDNS_FREE(reply_bytes);
171                         LDNS_FREE(ns);
172                         return status;
173                 }
174                 
175                 LDNS_FREE(ns);
176                 gettimeofday(&tv_e, NULL);
177
178                 if (reply) {
179                         ldns_pkt_set_querytime(reply, (uint32_t)
180                                 ((tv_e.tv_sec - tv_s.tv_sec) * 1000) +
181                                 (tv_e.tv_usec - tv_s.tv_usec) / 1000);
182                         ldns_pkt_set_answerfrom(reply, ns_array[i]);
183                         ldns_pkt_set_timestamp(reply, tv_s);
184                         ldns_pkt_set_size(reply, reply_size);
185                         break;
186                 } else {
187                         if (ldns_resolver_fail(r)) {
188                                 /* if fail is set bail out, after the first
189                                  * one */
190                                 break;
191                         }
192                 }
193
194                 /* wait retrans seconds... */
195                 sleep((unsigned int) ldns_resolver_retrans(r));
196         }
197
198         if (all_servers_rtt_inf) {
199                 LDNS_FREE(reply_bytes);
200                 return LDNS_STATUS_RES_NO_NS;
201         }
202 #ifdef HAVE_SSL
203         if (tsig_mac && reply_bytes) {
204                 if (!ldns_pkt_tsig_verify(reply,
205                                           reply_bytes,
206                                           reply_size,
207                                           ldns_resolver_tsig_keyname(r),
208                                           ldns_resolver_tsig_keydata(r), tsig_mac)) {
209                         status = LDNS_STATUS_CRYPTO_TSIG_BOGUS;
210                 }
211         }
212 #else
213         (void)tsig_mac;
214 #endif /* HAVE_SSL */
215
216         LDNS_FREE(reply_bytes);
217         if (result) {
218                 *result = reply;
219         }
220
221         return status;
222 }
223
224 /** best effort to set nonblocking */
225 static void
226 ldns_sock_nonblock(int sockfd)
227 {
228 #ifdef HAVE_FCNTL
229         int flag;
230         if((flag = fcntl(sockfd, F_GETFL)) != -1) {
231                 flag |= O_NONBLOCK;
232                 if(fcntl(sockfd, F_SETFL, flag) == -1) {
233                         /* ignore error, continue blockingly */
234                 }
235         }
236 #elif defined(HAVE_IOCTLSOCKET)
237         unsigned long on = 1;
238         if(ioctlsocket(sockfd, FIONBIO, &on) != 0) {
239                 /* ignore error, continue blockingly */
240         }
241 #endif
242 }
243
244 /** best effort to set blocking */
245 static void
246 ldns_sock_block(int sockfd)
247 {
248 #ifdef HAVE_FCNTL
249         int flag;
250         if((flag = fcntl(sockfd, F_GETFL)) != -1) {
251                 flag &= ~O_NONBLOCK;
252                 if(fcntl(sockfd, F_SETFL, flag) == -1) {
253                         /* ignore error, continue */
254                 }
255         }
256 #elif defined(HAVE_IOCTLSOCKET)
257         unsigned long off = 0;
258         if(ioctlsocket(sockfd, FIONBIO, &off) != 0) {
259                 /* ignore error, continue */
260         }
261 #endif
262 }
263
264 /** wait for a socket to become ready */
265 static int
266 ldns_sock_wait(int sockfd, struct timeval timeout, int write)
267 {
268         fd_set fds;
269         int ret;
270         FD_ZERO(&fds);
271         FD_SET(FD_SET_T sockfd, &fds);
272         if(write)
273                 ret = select(sockfd+1, NULL, &fds, NULL, &timeout);
274         else
275                 ret = select(sockfd+1, &fds, NULL, NULL, &timeout);
276         if(ret == 0)
277                 /* timeout expired */
278                 return 0;
279         else if(ret == -1)
280                 /* error */
281                 return 0;
282         return 1;
283 }
284
285 ldns_status
286 ldns_udp_send(uint8_t **result, ldns_buffer *qbin, const struct sockaddr_storage *to,
287                 socklen_t tolen, struct timeval timeout, size_t *answer_size)
288 {
289         int sockfd;
290         uint8_t *answer;
291
292         sockfd = ldns_udp_bgsend(qbin, to, tolen, timeout);
293
294         if (sockfd == 0) {
295                 return LDNS_STATUS_SOCKET_ERROR;
296         }
297
298         /* wait for an response*/
299         if(!ldns_sock_wait(sockfd, timeout, 0)) {
300 #ifndef USE_WINSOCK
301                 close(sockfd);
302 #else
303                 closesocket(sockfd);
304 #endif
305                 return LDNS_STATUS_NETWORK_ERR;
306         }
307
308         answer = ldns_udp_read_wire(sockfd, answer_size, NULL, NULL);
309 #ifndef USE_WINSOCK
310         close(sockfd);
311 #else
312         closesocket(sockfd);
313 #endif
314
315         if (*answer_size == 0) {
316                 /* oops */
317                 return LDNS_STATUS_NETWORK_ERR;
318         }
319
320         *result = answer;
321         return LDNS_STATUS_OK;
322 }
323
324 int
325 ldns_udp_bgsend(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, 
326                 struct timeval timeout)
327 {
328         int sockfd;
329
330         sockfd = ldns_udp_connect(to, timeout);
331
332         if (sockfd == 0) {
333                 return 0;
334         }
335
336         if (ldns_udp_send_query(qbin, sockfd, to, tolen) == 0) {
337                 return 0;
338         }
339         return sockfd;
340 }
341
342 int
343 ldns_udp_connect(const struct sockaddr_storage *to, struct timeval ATTR_UNUSED(timeout))
344 {
345         int sockfd;
346
347         if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_DGRAM, 
348                                         IPPROTO_UDP)) 
349                         == -1) {
350                 return 0;
351         }
352         return sockfd;
353 }
354
355 int
356 ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen, 
357                 struct timeval timeout)
358 {
359         int sockfd;
360
361         if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_STREAM, 
362                                         IPPROTO_TCP)) == -1) {
363                 return 0;
364         }
365
366         /* perform nonblocking connect, to be able to wait with select() */
367         ldns_sock_nonblock(sockfd);
368         if (connect(sockfd, (struct sockaddr*)to, tolen) == -1) {
369 #ifndef USE_WINSOCK
370 #ifdef EINPROGRESS
371                 if(errno != EINPROGRESS) {
372 #else
373                 if(1) {
374 #endif
375                         close(sockfd);
376                         return 0;
377                 }
378 #else /* USE_WINSOCK */
379                 if(WSAGetLastError() != WSAEINPROGRESS &&
380                         WSAGetLastError() != WSAEWOULDBLOCK) {
381                         closesocket(sockfd);
382                         return 0;
383                 }
384 #endif
385                 /* error was only telling us that it would block */
386         }
387
388         /* wait(write) until connected or error */
389         while(1) {
390                 int error = 0;
391                 socklen_t len = (socklen_t)sizeof(error);
392
393                 if(!ldns_sock_wait(sockfd, timeout, 1)) {
394 #ifndef USE_WINSOCK
395                         close(sockfd);
396 #else
397                         closesocket(sockfd);
398 #endif
399                         return 0;
400                 }
401
402                 /* check if there is a pending error for nonblocking connect */
403                 if(getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void*)&error,
404                         &len) < 0) {
405 #ifndef USE_WINSOCK
406                         error = errno; /* on solaris errno is error */
407 #else
408                         error = WSAGetLastError();
409 #endif
410                 }
411 #ifndef USE_WINSOCK
412 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
413                 if(error == EINPROGRESS || error == EWOULDBLOCK)
414                         continue; /* try again */
415 #endif
416                 else if(error != 0) {
417                         close(sockfd);
418                         /* error in errno for our user */
419                         errno = error;
420                         return 0;
421                 }
422 #else /* USE_WINSOCK */
423                 if(error == WSAEINPROGRESS)
424                         continue;
425                 else if(error == WSAEWOULDBLOCK)
426                         continue;
427                 else if(error != 0) {
428                         closesocket(sockfd);
429                         errno = error;
430                         return 0;
431                 }
432 #endif /* USE_WINSOCK */
433                 /* connected */
434                 break;
435         }
436
437         /* set the socket blocking again */
438         ldns_sock_block(sockfd);
439
440         return sockfd;
441 }
442
443 ssize_t
444 ldns_tcp_send_query(ldns_buffer *qbin, int sockfd, 
445                     const struct sockaddr_storage *to, socklen_t tolen)
446 {
447         uint8_t *sendbuf;
448         ssize_t bytes;
449
450         /* add length of packet */
451         sendbuf = LDNS_XMALLOC(uint8_t, ldns_buffer_position(qbin) + 2);
452         if(!sendbuf) return 0;
453         ldns_write_uint16(sendbuf, ldns_buffer_position(qbin));
454         memcpy(sendbuf + 2, ldns_buffer_export(qbin), ldns_buffer_position(qbin));
455
456         bytes = sendto(sockfd, (void*)sendbuf,
457                         ldns_buffer_position(qbin) + 2, 0, (struct sockaddr *)to, tolen);
458
459         LDNS_FREE(sendbuf);
460
461         if (bytes == -1 || (size_t) bytes != ldns_buffer_position(qbin) + 2 ) {
462                 return 0;
463         }
464         return bytes;
465 }
466
467 /* don't wait for an answer */
468 ssize_t
469 ldns_udp_send_query(ldns_buffer *qbin, int sockfd, const struct sockaddr_storage *to, 
470                 socklen_t tolen)
471 {
472         ssize_t bytes;
473
474         bytes = sendto(sockfd, (void*)ldns_buffer_begin(qbin),
475                         ldns_buffer_position(qbin), 0, (struct sockaddr *)to, tolen);
476
477         if (bytes == -1 || (size_t)bytes != ldns_buffer_position(qbin)) {
478                 return 0;
479         }
480         if ((size_t) bytes != ldns_buffer_position(qbin)) {
481                 return 0;
482         }
483         return bytes;
484 }
485
486 uint8_t *
487 ldns_udp_read_wire(int sockfd, size_t *size, struct sockaddr_storage *from,
488                 socklen_t *fromlen)
489 {
490         uint8_t *wire, *wireout;
491         ssize_t wire_size;
492
493         wire = LDNS_XMALLOC(uint8_t, LDNS_MAX_PACKETLEN);
494         if (!wire) {
495                 *size = 0;
496                 return NULL;
497         }
498
499         wire_size = recvfrom(sockfd, (void*)wire, LDNS_MAX_PACKETLEN, 0, 
500                         (struct sockaddr *)from, fromlen);
501
502         /* recvfrom can also return 0 */
503         if (wire_size == -1 || wire_size == 0) {
504                 *size = 0;
505                 LDNS_FREE(wire);
506                 return NULL;
507         }
508
509         *size = (size_t)wire_size;
510         wireout = LDNS_XREALLOC(wire, uint8_t, (size_t)wire_size);
511         if(!wireout) LDNS_FREE(wire);
512
513         return wireout;
514 }
515
516 uint8_t *
517 ldns_tcp_read_wire_timeout(int sockfd, size_t *size, struct timeval timeout)
518 {
519         uint8_t *wire;
520         uint16_t wire_size;
521         ssize_t bytes = 0, rc = 0;
522
523         wire = LDNS_XMALLOC(uint8_t, 2);
524         if (!wire) {
525                 *size = 0;
526                 return NULL;
527         }
528         
529         while (bytes < 2) {
530                 if(!ldns_sock_wait(sockfd, timeout, 0)) {
531                         *size = 0;
532                         LDNS_FREE(wire);
533                         return NULL;
534                 }
535                 rc = recv(sockfd, (void*) (wire + bytes), 
536                                 (size_t) (2 - bytes), 0);
537                 if (rc == -1 || rc == 0) {
538                         *size = 0;
539                         LDNS_FREE(wire);
540                         return NULL;
541                 }
542                 bytes += rc;
543         }
544
545         wire_size = ldns_read_uint16(wire);
546         
547         LDNS_FREE(wire);
548         wire = LDNS_XMALLOC(uint8_t, wire_size);
549         bytes = 0;
550
551         while (bytes < (ssize_t) wire_size) {
552                 if(!ldns_sock_wait(sockfd, timeout, 0)) {
553                         *size = 0;
554                         LDNS_FREE(wire);
555                         return NULL;
556                 }
557                 rc = recv(sockfd, (void*) (wire + bytes), 
558                                 (size_t) (wire_size - bytes), 0);
559                 if (rc == -1 || rc == 0) {
560                         LDNS_FREE(wire);
561                         *size = 0;
562                         return NULL;
563                 }
564                 bytes += rc;
565         }
566         
567         *size = (size_t) bytes;
568         return wire;
569 }
570
571 uint8_t *
572 ldns_tcp_read_wire(int sockfd, size_t *size)
573 {
574         uint8_t *wire;
575         uint16_t wire_size;
576         ssize_t bytes = 0, rc = 0;
577
578         wire = LDNS_XMALLOC(uint8_t, 2);
579         if (!wire) {
580                 *size = 0;
581                 return NULL;
582         }
583         
584         while (bytes < 2) {
585                 rc = recv(sockfd, (void*) (wire + bytes), 
586                                 (size_t) (2 - bytes), 0);
587                 if (rc == -1 || rc == 0) {
588                         *size = 0;
589                         LDNS_FREE(wire);
590                         return NULL;
591                 }
592                 bytes += rc;
593         }
594
595         wire_size = ldns_read_uint16(wire);
596         
597         LDNS_FREE(wire);
598         wire = LDNS_XMALLOC(uint8_t, wire_size);
599         bytes = 0;
600
601         while (bytes < (ssize_t) wire_size) {
602                 rc = recv(sockfd, (void*) (wire + bytes), 
603                                 (size_t) (wire_size - bytes), 0);
604                 if (rc == -1 || rc == 0) {
605                         LDNS_FREE(wire);
606                         *size = 0;
607                         return NULL;
608                 }
609                 bytes += rc;
610         }
611         
612         *size = (size_t) bytes;
613         return wire;
614 }
615
616 /* keep in mind that in DNS tcp messages the first 2 bytes signal the
617  * amount data to expect
618  */
619 ldns_status
620 ldns_tcp_send(uint8_t **result,  ldns_buffer *qbin, const struct sockaddr_storage *to, 
621                 socklen_t tolen, struct timeval timeout, size_t *answer_size)
622 {
623         int sockfd;
624         uint8_t *answer;
625         
626         sockfd = ldns_tcp_bgsend(qbin, to, tolen, timeout);
627         
628         if (sockfd == 0) {
629                 return LDNS_STATUS_ERR;
630         }
631
632         answer = ldns_tcp_read_wire_timeout(sockfd, answer_size, timeout);
633 #ifndef USE_WINSOCK
634         close(sockfd);
635 #else
636         closesocket(sockfd);
637 #endif
638
639         if (*answer_size == 0) {
640                 /* oops */
641                 return LDNS_STATUS_NETWORK_ERR;
642         }
643
644         /* resize accordingly */
645         answer = (uint8_t*)LDNS_XREALLOC(answer, uint8_t *, (size_t)*answer_size);
646         *result = answer;
647         return LDNS_STATUS_OK;
648 }
649
650 int
651 ldns_tcp_bgsend(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, 
652                 struct timeval timeout)
653 {
654         int sockfd;
655         
656         sockfd = ldns_tcp_connect(to, tolen, timeout);
657         
658         if (sockfd == 0) {
659                 return 0;
660         }
661         
662         if (ldns_tcp_send_query(qbin, sockfd, to, tolen) == 0) {
663                 return 0;
664         }
665         
666         return sockfd;
667 }
668
669 /* code from rdata.c */
670 struct sockaddr_storage *
671 ldns_rdf2native_sockaddr_storage(const ldns_rdf *rd, uint16_t port, size_t *size)
672 {
673         struct sockaddr_storage *data;
674         struct sockaddr_in  *data_in;
675         struct sockaddr_in6 *data_in6;
676
677         data = LDNS_MALLOC(struct sockaddr_storage);
678         if (!data) {
679                 return NULL;
680         }
681                 /* zero the structure for portability */
682                 memset(data, 0, sizeof(struct sockaddr_storage));
683         if (port == 0) {
684                 port =  LDNS_PORT;
685         }
686
687         switch(ldns_rdf_get_type(rd)) {
688                 case LDNS_RDF_TYPE_A:
689                         data->ss_family = AF_INET;
690                         data_in = (struct sockaddr_in*) data;
691                         data_in->sin_port = (in_port_t)htons(port);
692                         memcpy(&(data_in->sin_addr), ldns_rdf_data(rd), ldns_rdf_size(rd));
693                         *size = sizeof(struct sockaddr_in);
694                         return data;
695                 case LDNS_RDF_TYPE_AAAA:
696                         data->ss_family = AF_INET6;
697                         data_in6 = (struct sockaddr_in6*) data;
698                         data_in6->sin6_port = (in_port_t)htons(port);
699                         memcpy(&data_in6->sin6_addr, ldns_rdf_data(rd), ldns_rdf_size(rd));
700                         *size = sizeof(struct sockaddr_in6);
701                         return data;
702                 default:
703                         LDNS_FREE(data);
704                         return NULL;
705         }
706 }
707
708 ldns_rdf *
709 ldns_sockaddr_storage2rdf(struct sockaddr_storage *sock, uint16_t *port)
710 {
711         ldns_rdf *addr;
712         struct sockaddr_in *data_in;
713         struct sockaddr_in6 *data_in6;
714
715         switch(sock->ss_family) {
716                 case AF_INET:
717                         data_in = (struct sockaddr_in*)sock;
718                         if (port) {
719                                 *port = ntohs((uint16_t)data_in->sin_port);
720                         }
721                         addr = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_A,
722                                         LDNS_IP4ADDRLEN, &data_in->sin_addr);
723                         break;
724                 case AF_INET6:
725                         data_in6 = (struct sockaddr_in6*)sock;
726                         if (port) {
727                                 *port = ntohs((uint16_t)data_in6->sin6_port);
728                         }
729                         addr = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_AAAA,
730                                         LDNS_IP6ADDRLEN, &data_in6->sin6_addr);
731                         break;
732                 default:
733                         if (port) {
734                                 *port = 0;
735                         }
736                         return NULL;
737         }
738         return addr;
739 }
740
741 /* code from resolver.c */
742 ldns_status
743 ldns_axfr_start(ldns_resolver *resolver, ldns_rdf *domain, ldns_rr_class class) 
744 {
745         ldns_pkt *query;
746         ldns_buffer *query_wire;
747
748         struct sockaddr_storage *ns = NULL;
749         size_t ns_len = 0;
750         size_t ns_i;
751         ldns_status status;
752
753         if (!resolver || ldns_resolver_nameserver_count(resolver) < 1) {
754                 return LDNS_STATUS_ERR;
755         }
756
757         query = ldns_pkt_query_new(ldns_rdf_clone(domain), LDNS_RR_TYPE_AXFR, class, 0);
758
759         if (!query) {
760                 return LDNS_STATUS_ADDRESS_ERR;
761         }
762         /* For AXFR, we have to make the connection ourselves */
763         /* try all nameservers (which usually would mean v4 fallback if
764          * @hostname is used */
765         for (ns_i = 0;
766              ns_i < ldns_resolver_nameserver_count(resolver) &&
767              resolver->_socket == 0;
768              ns_i++) {
769                 ns = ldns_rdf2native_sockaddr_storage(
770                         resolver->_nameservers[ns_i],
771                         ldns_resolver_port(resolver), &ns_len);
772
773                 resolver->_socket = ldns_tcp_connect(ns, (socklen_t)ns_len,
774                                 ldns_resolver_timeout(resolver));
775         }
776
777         if (resolver->_socket == 0) {
778                 ldns_pkt_free(query);
779                 LDNS_FREE(ns);
780                 return LDNS_STATUS_NETWORK_ERR;
781         }
782
783 #ifdef HAVE_SSL
784         if (ldns_resolver_tsig_keyname(resolver) && ldns_resolver_tsig_keydata(resolver)) {
785                 status = ldns_pkt_tsig_sign(query,
786                                             ldns_resolver_tsig_keyname(resolver),
787                                             ldns_resolver_tsig_keydata(resolver),
788                                             300, ldns_resolver_tsig_algorithm(resolver), NULL);
789                 if (status != LDNS_STATUS_OK) {
790                         /* RoRi: to prevent problems on subsequent calls to ldns_axfr_start
791                            we have to close the socket here! */
792 #ifndef USE_WINSOCK
793                         close(resolver->_socket);
794 #else
795                         closesocket(resolver->_socket);
796 #endif
797                         resolver->_socket = 0;
798
799                         return LDNS_STATUS_CRYPTO_TSIG_ERR;
800                 }
801         }
802 #endif /* HAVE_SSL */
803
804         /* Convert the query to a buffer
805          * Is this necessary?
806          */
807         query_wire = ldns_buffer_new(LDNS_MAX_PACKETLEN);
808         status = ldns_pkt2buffer_wire(query_wire, query);
809         if (status != LDNS_STATUS_OK) {
810                 ldns_pkt_free(query);
811                 ldns_buffer_free(query_wire);
812                 LDNS_FREE(ns);
813
814                 /* RoRi: to prevent problems on subsequent calls to ldns_axfr_start
815                     we have to close the socket here! */
816 #ifndef USE_WINSOCK
817                 close(resolver->_socket);
818 #else
819                 closesocket(resolver->_socket);
820 #endif
821                 resolver->_socket = 0;
822
823                 return status;
824         }
825         /* Send the query */
826         if (ldns_tcp_send_query(query_wire, resolver->_socket, ns,
827                                 (socklen_t)ns_len) == 0) {
828                 ldns_pkt_free(query);
829                 ldns_buffer_free(query_wire);
830                 LDNS_FREE(ns);
831
832                 /* RoRi: to prevent problems on subsequent calls to ldns_axfr_start
833                          we have to close the socket here! */
834
835 #ifndef USE_WINSOCK
836                 close(resolver->_socket);
837 #else
838                 closesocket(resolver->_socket);
839 #endif
840                 resolver->_socket = 0;
841
842                 return LDNS_STATUS_NETWORK_ERR;
843         }
844
845         ldns_pkt_free(query);
846         ldns_buffer_free(query_wire);
847         LDNS_FREE(ns);
848
849         /*
850          * The AXFR is done once the second SOA record is sent
851          */
852         resolver->_axfr_soa_count = 0;
853         return LDNS_STATUS_OK;
854 }