<commit_before>/*
 * net.c
 *
 * Network implementation
 * All network related functions are grouped here
 *
 * a Net::DNS like library for C
 *
 * (c) NLnet Labs, 2004, 2005
 *
 * See the file LICENSE for the license
 */

#include <ldns/config.h>

#include <ldns/dns.h>

#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <errno.h>

ldns_status
ldns_send(ldns_pkt **result, ldns_resolver *r, ldns_pkt *query_pkt)
{
	uint8_t i,j;
	ldns_rdf *temp;
	
	struct sockaddr_storage *ns;
	size_t ns_len;
	struct timeval tv_s;
        struct timeval tv_e;

	ldns_rdf **ns_array;
	ldns_rdf **ns_rand_array;
	ldns_pkt *reply;
	ldns_buffer *qb;

	uint8_t *reply_bytes = NULL;
	size_t reply_size = 0;
	ldns_rdf *tsig_mac = NULL;
	ldns_status status;

	status = LDNS_STATUS_OK;

	ns_rand_array = LDNS_XMALLOC(ldns_rdf*, ldns_resolver_nameserver_count(r));

	if (!query_pkt || !ns_rand_array) {
		/* nothing to do? */
		return LDNS_STATUS_ERR;
	}
	
	ns_array = ldns_resolver_nameservers(r);
	reply = NULL; ns_len = 0;
	for (i = 0; i < ldns_resolver_nameserver_count(r); i++) {
		ns_rand_array[i] = ns_array[i];
	}
	
	qb = ldns_buffer_new(LDNS_MIN_BUFLEN);

	if (ldns_pkt_tsig(query_pkt)) {
		tsig_mac = ldns_rr_rdf(ldns_pkt_tsig(query_pkt), 3);
	}

	if (ldns_pkt2buffer_wire(qb, query_pkt) != LDNS_STATUS_OK) {
		ldns_buffer_free(qb);
		return LDNS_STATUS_ERR;
	}
	/* random should already be setup - isn't so bad
	 * if this isn't "good" random. Note that this
	 * changes the order in the resolver as well!
	 */
	if (ldns_resolver_random(r)) {
		for (i = 0; i < ldns_resolver_nameserver_count(r); i++) {
			j = random() % ldns_resolver_nameserver_count(r);
			/* printf("r = %d, switch i = %d, j = %d\n", 
			 ldns_resolver_nameserver_count(r), i, j);
			 */
			temp = ns_rand_array[i];
			ns_rand_array[i] = ns_rand_array[j];
			ns_rand_array[j] = temp;
		}
	}

	/* loop through all defined nameservers */
	for (i = 0; i < ldns_resolver_nameserver_count(r); i++) {

		ns = ldns_rdf2native_sockaddr_storage(ns_rand_array[i],
				ldns_resolver_port(r), &ns_len);
		
		if ((ns->ss_family == AF_INET) && 
				(ldns_resolver_ip6(r) == LDNS_RESOLV_INET6)) {
			printf("mismatch 4 - 6!!!\n");
			continue;
		}
		if ((ns->ss_family == AF_INET6) &&
				 (ldns_resolver_ip6(r) == LDNS_RESOLV_INET)) {
			printf("mismatch!!! 6 - 4\n");
			continue;
		}

		gettimeofday(&tv_s, NULL);
		/* query */

		if (1 == ldns_resolver_usevc(r)) {
			/* do err handling here ? */
			(void)ldns_tcp_send(&reply_bytes, qb, ns, (socklen_t)ns_len, ldns_resolver_timeout(r), &reply_size);
		} else {
			/* udp here, please */
			(void)ldns_udp_send(&reply_bytes, qb, ns, (socklen_t)ns_len, ldns_resolver_timeout(r), &reply_size);
		}
		<commit_msg>skip the continues for now<commit_after>/*
 * net.c
 *
 * Network implementation
 * All network related functions are grouped here
 *
 * a Net::DNS like library for C
 *
 * (c) NLnet Labs, 2004, 2005
 *
 * See the file LICENSE for the license
 */

#include <ldns/config.h>

#include <ldns/dns.h>

#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <errno.h>

ldns_status
ldns_send(ldns_pkt **result, ldns_resolver *r, ldns_pkt *query_pkt)
{
	uint8_t i,j;
	ldns_rdf *temp;
	
	struct sockaddr_storage *ns;
	size_t ns_len;
	struct timeval tv_s;
        struct timeval tv_e;

	ldns_rdf **ns_array;
	ldns_rdf **ns_rand_array;
	ldns_pkt *reply;
	ldns_buffer *qb;

	uint8_t *reply_bytes = NULL;
	size_t reply_size = 0;
	ldns_rdf *tsig_mac = NULL;
	ldns_status status;

	status = LDNS_STATUS_OK;

	ns_rand_array = LDNS_XMALLOC(ldns_rdf*, ldns_resolver_nameserver_count(r));

	if (!query_pkt || !ns_rand_array) {
		/* nothing to do? */
		return LDNS_STATUS_ERR;
	}
	
	ns_array = ldns_resolver_nameservers(r);
	reply = NULL; ns_len = 0;
	for (i = 0; i < ldns_resolver_nameserver_count(r); i++) {
		ns_rand_array[i] = ns_array[i];
	}
	
	qb = ldns_buffer_new(LDNS_MIN_BUFLEN);

	if (ldns_pkt_tsig(query_pkt)) {
		tsig_mac = ldns_rr_rdf(ldns_pkt_tsig(query_pkt), 3);
	}

	if (ldns_pkt2buffer_wire(qb, query_pkt) != LDNS_STATUS_OK) {
		ldns_buffer_free(qb);
		return LDNS_STATUS_ERR;
	}
	/* random should already be setup - isn't so bad
	 * if this isn't "good" random. Note that this
	 * changes the order in the resolver as well!
	 */
	if (ldns_resolver_random(r)) {
		for (i = 0; i < ldns_resolver_nameserver_count(r); i++) {
			j = random() % ldns_resolver_nameserver_count(r);
			/* printf("r = %d, switch i = %d, j = %d\n", 
			 ldns_resolver_nameserver_count(r), i, j);
			 */
			temp = ns_rand_array[i];
			ns_rand_array[i] = ns_rand_array[j];
			ns_rand_array[j] = temp;
		}
	}

	/* loop through all defined nameservers */
	for (i = 0; i < ldns_resolver_nameserver_count(r); i++) {

		ns = ldns_rdf2native_sockaddr_storage(ns_rand_array[i],
				ldns_resolver_port(r), &ns_len);
		
		if ((ns->ss_family == AF_INET) && 
				(ldns_resolver_ip6(r) == LDNS_RESOLV_INET6)) {
			printf("mismatch 4 - 6!!!\n");
			/* FREE OF NS */
			/* continue; */
		}
		if ((ns->ss_family == AF_INET6) &&
				 (ldns_resolver_ip6(r) == LDNS_RESOLV_INET)) {
			printf("mismatch!!! 6 - 4\n");
			/* continue; */
		}

		gettimeofday(&tv_s, NULL);
		/* query */

		if (1 == ldns_resolver_usevc(r)) {
			/* do err handling here ? */
			(void)ldns_tcp_send(&reply_bytes, qb, ns, (socklen_t)ns_len, ldns_resolver_timeout(r), &reply_size);
		} else {
			/* udp here, please */
			(void)ldns_udp_send(&reply_bytes, qb, ns, (socklen_t)ns_len, ldns_resolver_timeout(r), &reply_size);
		}
		
{'fraction_non_alphanumeric': 0.14472038592102912, 'fraction_numerical': 0.009648025728068608, 'mean_word_length': 4.0296495956873315, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2685441', 'n_tokens_mistral': 2652, 'n_tokens_neox': 2402, 'n_words': 481}