struct timeval tv_s;
struct timeval tv_e;
-
+ struct timeval timeout;
+
+ timeout.tv_sec = LDNS_DEFAULT_TIMEOUT_SEC;
+ timeout.tv_usec = LDNS_DEFAULT_TIMEOUT_USEC;
+
gettimeofday(&tv_s, NULL);
if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
return NULL;
}
+ if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout,
+ (socklen_t) sizeof(timeout))) {
+ perror("setsockopt");
+ close(sockfd);
+ return NULL;
+ }
+
bytes = sendto(sockfd, ldns_buffer_begin(qbin),
ldns_buffer_position(qbin), 0, (struct sockaddr *)to, tolen);
close(sockfd);
if (bytes == -1) {
+ if (errno == EAGAIN) {
+ fprintf(stderr, "socket timeout\n");
+ }
printf("received too little\n");
FREE(answer);
return NULL;
struct timeval tv_s;
struct timeval tv_e;
+ struct timeval timeout;
+
+ timeout.tv_sec = LDNS_DEFAULT_TIMEOUT_SEC;
+ timeout.tv_usec = LDNS_DEFAULT_TIMEOUT_USEC;
+
gettimeofday(&tv_s, NULL);
if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
return NULL;
}
+ if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout,
+ (socklen_t) sizeof(timeout))) {
+ perror("setsockopt");
+ close(sockfd);
+ return NULL;
+ }
+
if (connect(sockfd, (struct sockaddr*)to, tolen) == -1) {
close(sockfd);
perror("could not bind socket");
while (total_bytes < 2) {
bytes = recv(sockfd, answer, MAX_PACKET_SIZE, 0);
if (bytes == -1) {
+ if (errno == EAGAIN) {
+ fprintf(stderr, "socket timeout\n");
+ }
perror("error receiving tcp packet");
FREE(answer);
return NULL;
/* if we did not receive the whole packet in one tcp packet,
we must recv() on */
- while (total_bytes < answer_size + 2) {
- bytes = recv(sockfd, answer+total_bytes, MAX_PACKET_SIZE-total_bytes, 0);
+ while (total_bytes < (ssize_t) (answer_size + 2)) {
+ bytes = recv(sockfd, answer+total_bytes, (size_t) (MAX_PACKET_SIZE-total_bytes), 0);
if (bytes == -1) {
+ if (errno == EAGAIN) {
+ fprintf(stderr, "socket timeout\n");
+ }
perror("error receiving tcp packet");
FREE(answer);
return NULL;
}
}
-/*
- * Read SIZE bytes from the socket into BUF. Keep reading unless an
- * error occurs (except for EINTR) or EOF is reached.
- */
-static bool
-read_socket(int s, void *buf, size_t size)
-{
- char *data = buf;
- size_t total_count = 0;
-
- while (total_count < size) {
- ssize_t count = read(s, data + total_count, size -
- total_count);
- if (count == -1) {
- if (errno != EINTR) {
- return false;
- } else {
- continue;
- }
- }
- total_count += count;
- }
- return true;
-}
-
-/*
- * Write the complete buffer to the socket, irrespective of short
- * writes or interrupts.
- */
-static bool
-write_socket(int s, const void *buf, size_t size)
-{
- const char *data = buf;
- size_t total_count = 0;
-
- while (total_count < size) {
- ssize_t count = write(s, data + total_count, size -
- total_count);
- if (count == -1) {
- if (errno != EINTR) {
- return false;
- } else {
- continue;
- }
- }
- total_count += count;
- }
- return true;
-}
exit(0);
}
-void
+int
main(int argc, char **argv)
{
ldns_resolver *res;
/* init */
res = ldns_resolver_new();
if (!res)
- return;
+ return -1;
nameserver = ldns_rdf_new_frm_str(server_ip, LDNS_RDF_TYPE_A);
if (!nameserver) {
printf("Bad server ip\n");
- return;
+ return -1;
}
if (ldns_resolver_push_nameserver(res, nameserver) != LDNS_STATUS_OK) {
printf("error push nameserver\n");
- return;
+ return -1;
}
/* HACK */
ldns_resolver_set_configured(res, 1);
qname = ldns_rdf_new_frm_str(name, LDNS_RDF_TYPE_DNAME);
if (!qname) {
printf("error making qname\n");
- return;
+ return -1;
}
pkt = ldns_resolver_send(res, qname, ldns_rr_get_type_by_name(type), 0, LDNS_RD);
if (!pkt) {
printf("error pkt sending\n");
- return;
+ return -1;
}
ldns_pkt_print(stdout, pkt);
+
+ return 0;
}