18 #ifdef HAVE_NETINET_IN_H
19 #include <netinet/in.h>
21 #ifdef HAVE_SYS_SOCKET_H
22 #include <sys/socket.h>
27 #ifdef HAVE_ARPA_INET_H
28 #include <arpa/inet.h>
64 struct sockaddr_storage *ns;
72 bool all_servers_rtt_inf;
75 uint8_t *reply_bytes =
NULL;
76 size_t reply_size = 0;
87 all_servers_rtt_inf =
true;
110 if ((ns->ss_family == AF_INET) &&
117 if ((ns->ss_family == AF_INET6) &&
125 all_servers_rtt_inf =
false;
127 gettimeofday(&tv_s,
NULL);
158 status = send_status;
181 gettimeofday(&tv_e,
NULL);
185 ((tv_e.tv_sec - tv_s.tv_sec) * 1000) +
186 (tv_e.tv_usec - tv_s.tv_usec) / 1000);
204 if (all_servers_rtt_inf) {
209 if (tsig_mac && reply && reply_bytes) {
232 ldns_sock_nonblock(
int sockfd)
236 if((flag = fcntl(sockfd, F_GETFL)) != -1) {
238 if(fcntl(sockfd, F_SETFL, flag) == -1) {
242 #elif defined(HAVE_IOCTLSOCKET)
243 unsigned long on = 1;
244 if(ioctlsocket(sockfd, FIONBIO, &on) != 0) {
252 ldns_sock_block(
int sockfd)
256 if((flag = fcntl(sockfd, F_GETFL)) != -1) {
258 if(fcntl(sockfd, F_SETFL, flag) == -1) {
262 #elif defined(HAVE_IOCTLSOCKET)
263 unsigned long off = 0;
264 if(ioctlsocket(sockfd, FIONBIO, &off) != 0) {
272 ldns_sock_wait(
int sockfd,
struct timeval timeout,
int write)
280 ret = select(sockfd+1,
NULL, &fds,
NULL, &timeout);
282 ret = select(sockfd+1, &fds,
NULL,
NULL, &timeout);
295 socklen_t tolen,
struct timeval timeout,
size_t *answer_size)
307 if(!ldns_sock_wait(sockfd, timeout, 0)) {
319 ldns_sock_nonblock(sockfd);
328 if (*answer_size == 0) {
339 struct timeval timeout)
366 if ((sockfd = socket((
int)((
struct sockaddr*)to)->sa_family, SOCK_DGRAM,
377 struct timeval timeout)
382 if ((sockfd = socket((
int)((
struct sockaddr*)to)->sa_family, SOCK_STREAM,
383 IPPROTO_TCP)) == -1) {
389 ldns_sock_nonblock(sockfd);
390 if (connect(sockfd, (
struct sockaddr*)to, tolen) == -1) {
393 if(errno != EINPROGRESS) {
401 if(WSAGetLastError() != WSAEINPROGRESS &&
402 WSAGetLastError() != WSAEWOULDBLOCK) {
413 socklen_t len = (socklen_t)
sizeof(error);
415 if(!ldns_sock_wait(sockfd, timeout, 1)) {
425 if(getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (
void*)&error,
430 error = WSAGetLastError();
434 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
435 if(error == EINPROGRESS || error == EWOULDBLOCK)
438 else if(error != 0) {
445 if(error == WSAEINPROGRESS)
447 else if(error == WSAEWOULDBLOCK)
449 else if(error != 0) {
460 ldns_sock_block(sockfd);
467 const struct sockaddr_storage *to, socklen_t tolen)
473 sendbuf =
LDNS_XMALLOC(uint8_t, ldns_buffer_position(qbin) + 2);
474 if(!sendbuf)
return 0;
475 ldns_write_uint16(sendbuf, ldns_buffer_position(qbin));
476 memcpy(sendbuf + 2, ldns_buffer_begin(qbin), ldns_buffer_position(qbin));
478 bytes = sendto(sockfd, (
void*)sendbuf,
479 ldns_buffer_position(qbin) + 2, 0, (
struct sockaddr *)to, tolen);
483 if (bytes == -1 || (
size_t) bytes != ldns_buffer_position(qbin) + 2 ) {
496 bytes = sendto(sockfd, (
void*)ldns_buffer_begin(qbin),
497 ldns_buffer_position(qbin), 0, (
struct sockaddr *)to, tolen);
499 if (bytes == -1 || (
size_t)bytes != ldns_buffer_position(qbin)) {
502 if ((
size_t) bytes != ldns_buffer_position(qbin)) {
512 uint8_t *wire, *wireout;
522 (
struct sockaddr *)from, fromlen);
525 if (wire_size == -1 || wire_size == 0) {
531 *size = (size_t)wire_size;
543 ssize_t bytes = 0, rc = 0;
552 if(!ldns_sock_wait(sockfd, timeout, 0)) {
557 rc = recv(sockfd, (
void*) (wire + bytes),
558 (
size_t) (2 - bytes), 0);
559 if (rc == -1 || rc == 0) {
567 wire_size = ldns_read_uint16(wire);
577 while (bytes < (ssize_t) wire_size) {
578 if(!ldns_sock_wait(sockfd, timeout, 0)) {
583 rc = recv(sockfd, (
void*) (wire + bytes),
584 (
size_t) (wire_size - bytes), 0);
585 if (rc == -1 || rc == 0) {
593 *size = (size_t) bytes;
602 ssize_t bytes = 0, rc = 0;
611 rc = recv(sockfd, (
void*) (wire + bytes),
612 (
size_t) (2 - bytes), 0);
613 if (rc == -1 || rc == 0) {
621 wire_size = ldns_read_uint16(wire);
631 while (bytes < (ssize_t) wire_size) {
632 rc = recv(sockfd, (
void*) (wire + bytes),
633 (
size_t) (wire_size - bytes), 0);
634 if (rc == -1 || rc == 0) {
642 *size = (size_t) bytes;
651 socklen_t tolen,
struct timeval timeout,
size_t *answer_size)
669 if (*answer_size == 0) {
675 *result =
LDNS_XREALLOC(answer, uint8_t, (
size_t)*answer_size);
685 struct timeval timeout)
708 struct sockaddr_storage *
711 struct sockaddr_storage *data;
712 struct sockaddr_in *data_in;
713 struct sockaddr_in6 *data_in6;
720 memset(data, 0,
sizeof(
struct sockaddr_storage));
728 data->ss_family = AF_INET;
730 data_in = (
struct sockaddr_in*) data;
731 data_in->sin_port = (in_port_t)htons(port);
733 *size =
sizeof(
struct sockaddr_in);
737 data->ss_family = AF_INET6;
739 data_in6 = (
struct sockaddr_in6*) data;
740 data_in6->sin6_port = (in_port_t)htons(port);
742 *size =
sizeof(
struct sockaddr_in6);
755 struct sockaddr_in *data_in;
756 struct sockaddr_in6 *data_in6;
758 switch(sock->ss_family) {
760 data_in = (
struct sockaddr_in*)sock;
762 *port = ntohs((uint16_t)data_in->sin_port);
768 data_in6 = (
struct sockaddr_in6*)sock;
770 *port = ntohs((uint16_t)data_in6->sin6_port);
792 struct sockaddr_storage *ns =
NULL;
842 closesocket(resolver->
_socket);
864 closesocket(resolver->
_socket);
881 closesocket(resolver->
_socket);
889 (socklen_t)ns_len) == 0) {
900 closesocket(resolver->
_socket);
ldns_rdf * ldns_rr_rdf(const ldns_rr *rr, size_t nr)
returns the rdata field member counter.
uint8_t ldns_resolver_ip6(const ldns_resolver *r)
Does the resolver use ip6 or ip4.
implementation of buffers to ease operations
char * ldns_resolver_tsig_algorithm(const ldns_resolver *r)
Return the tsig algorithm as used by the nameserver.
uint8_t ldns_resolver_retry(const ldns_resolver *r)
Get the number of retries.
DNS stub resolver structure.
enum ldns_enum_rr_class ldns_rr_class
void ldns_resolver_set_nameserver_rtt(ldns_resolver *r, size_t pos, size_t value)
Set round trip time for a specific nameserver.
#define LDNS_RESOLV_INET6
#define LDNS_XMALLOC(type, count)
#define LDNS_MIN_BUFLEN
number of initial bytes in buffer of which we cannot tell the size before hand
size_t ldns_rdf_size(const ldns_rdf *rd)
returns the size of the rdf.
void ldns_buffer_free(ldns_buffer *buffer)
frees the buffer.
ldns_rdf * ldns_rdf_clone(const ldns_rdf *rd)
clones a rdf structure.
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.
bool ldns_pkt_tsig_verify(ldns_pkt *pkt, uint8_t *wire, size_t wirelen, const char *key_name, const char *key_data, ldns_rdf *orig_mac_rdf)
verifies the tsig rr for the given packet and key.
#define LDNS_MAX_PACKETLEN
uint16_t ldns_resolver_port(const ldns_resolver *r)
Get the port the resolver should use.
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.
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.
#define LDNS_XREALLOC(ptr, type, count)
void ldns_pkt_set_size(ldns_pkt *packet, size_t s)
Set the packet's size.
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.
Including this file will include all ldns files, and define some lookup tables.
uint8_t * ldns_rdf_data(const ldns_rdf *rd)
returns the data of the rdf.
ldns_status ldns_pkt2buffer_wire(ldns_buffer *buffer, const ldns_pkt *packet)
Copies the packet data to the buffer in wire format.
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.
void ldns_resolver_nameservers_randomize(ldns_resolver *r)
Randomize the nameserver list in the resolver.
bool ldns_resolver_usevc(const ldns_resolver *r)
Does the resolver use tcp or udp.
size_t ldns_resolver_nameserver_count(const ldns_resolver *r)
How many nameserver are configured in the resolver.
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...
ldns_rdf * ldns_sockaddr_storage2rdf(struct sockaddr_storage *sock, uint16_t *port)
returns an rdf with the sockaddr info.
int ldns_udp_connect(const struct sockaddr_storage *to, struct timeval timeout)
Create a udp socket to the specified address.
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...
ldns_rdf ** _nameservers
Array of nameservers to query (IP addresses or dnames)
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.
void ldns_pkt_set_answerfrom(ldns_pkt *packet, ldns_rdf *answerfrom)
Set the packet's answering server.
ldns_rdf_type ldns_rdf_get_type(const ldns_rdf *rd)
returns the type of the rdf.
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.
char * ldns_resolver_tsig_keydata(const ldns_resolver *r)
Return the tsig keydata as used by the nameserver.
uint8_t * ldns_tcp_read_wire(int sockfd, size_t *size)
This routine may block.
bool ldns_resolver_random(const ldns_resolver *r)
Does the resolver randomize the nameserver before usage.
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.
enum ldns_enum_status ldns_status
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.
#define LDNS_MALLOC(type)
Memory management macros.
void ldns_pkt_set_querytime(ldns_pkt *packet, uint32_t time)
Set the packet's query time.
ldns_buffer * ldns_buffer_new(size_t capacity)
creates a new buffer with the specified capacity.
uint8_t ldns_resolver_retrans(const ldns_resolver *r)
Get the retransmit interval.
ldns_status ldns_pkt_tsig_sign(ldns_pkt *pkt, const char *key_name, const char *key_data, uint16_t fudge, const char *algorithm_name, ldns_rdf *query_mac)
creates a tsig rr for the given packet and key.
int ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout)
Create a tcp socket to the specified address.
size_t * ldns_resolver_rtt(const ldns_resolver *r)
Return the used round trip times for the nameservers.
bool ldns_resolver_fail(const ldns_resolver *r)
Does the resolver only try the first nameserver.
ldns_rdf ** ldns_resolver_nameservers(const ldns_resolver *r)
Return the configured nameserver ip address.
char * ldns_resolver_tsig_keyname(const ldns_resolver *r)
Return the tsig keyname as used by the nameserver.
Resource record data field.
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.
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.
ldns_rr * ldns_pkt_tsig(const ldns_pkt *pkt)
Return the packet's tsig pseudo rr's.
ldns_status ldns_axfr_start(ldns_resolver *resolver, 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_...
#define LDNS_RESOLV_RTT_INF
int _socket
Keep some things to make AXFR possible.
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.
void ldns_pkt_free(ldns_pkt *packet)
frees the packet structure and all data that it contains.
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.
struct timeval ldns_resolver_timeout(const ldns_resolver *r)
What is the timeout on socket connections.
void ldns_pkt_set_timestamp(ldns_pkt *packet, struct timeval timeval)
Set the packet's timestamp.