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

#include <config.h>

#include <ldns/rdata.h>
#include <ldns/error.h>
#include <ldns/resolver.h>
#include <ldns/buffer.h>
#include <ldns/wire2host.h>
#include <ldns/host2wire.h>
#include <ldns/host2str.h>
#include <ldns/resolver.h>

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

#include "util.h"


extern int errno;

/* send a buffer using tcp */
ldns_pkt *
ldns_sendbuf_tcp(ldns_buffer *buf, int *sockfd, struct sockaddr *dest)
{
	return NULL;
}

/* axfr is a hack - handle it different */
ldns_pkt *
ldns_sendbuf_axfr(ldns_buffer *buf, int *sockfd, struct sockaddr *dest)
{
	return NULL;
}

/**
 * Send to ptk to the nameserver at ipnumber. Return the data
 * as a ldns_pkt
 * \param[in] resolver to use 
 * \param[in] query to send
 * \return the pkt received from the nameserver
 */
ldns_pkt *
ldns_send(ldns_resolver *r, ldns_pkt *query_pkt)
{
	uint8_t i;
	
	struct sockaddr_storage *ns;
	struct sockaddr_in *ns4;
	struct sockaddr_in6 *ns6;
	socklen_t ns_len;

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

	ns_array = ldns_resolver_nameservers(r);
	reply = NULL;
	
	printf("we are in ldns_send()\n");
	qb = ldns_buffer_new(MAX_PACKET_SIZE);

	if (ldns_pkt2buffer_wire(qb, query_pkt) != LDNS_STATUS_OK) {
		printf("could not convert to wire fmt\n");
		return NULL;
	}

	printf("nameservers %d\n",ldns_resolver_nameserver_count(r));
	
	/* loop through all defined nameservers */
	for (i = 0; i < ldns_resolver_nameserver_count(r); i++) {

		ns = ldns_rdf2native_sockaddr_storage(ns_array[i]);

		/* setup some family specific stuff */
		switch(ns->ss_family) {

			case AF_INET:
				ns4 = (struct sockaddr_in*) ns;
				ns4->sin_port = htons(ldns_resolver_port(r));
				ns_len = sizeof(struct sockaddr_in);
				printf("port %d\n", ntohs(ns4->sin_port));
				break;
			case AF_INET6:
				ns6 = (struct sockaddr_in6*) ns;
				ns6->sin6_port = htons(ldns_resolver_port(r));
				ns_len = sizeof(struct sockaddr_in6);
				printf("port %d\n", ntohs(ns6->sin6_port));
				break;
		}

		printf("ip address len %d\n", ns_len);

		/* query */
		reply = ldns_send_udp(qb, ns, ns_len);
		
		if (reply) {
			ldns_pkt_set_answerfrom(reply, ns_array[i]);
			printf("reply found\n");
			break;
		}
	}
	return reply;
}


/**
 */
ldns_pkt *
ldns_send_udp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen)
{
	int sockfd;
	ssize_t bytes;
	uint8_t *answer;
	ldns_pkt *answer_pkt;

	struct timeval tv_s;
        struct timeval tv_e;

	gettimeofday(&tv_s, NULL);

	if ((sockfd = socket(((struct sockaddr*)to)->sa_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
		printf("could not open socket\n");
		return NULL;
	}

	bytes = sendto(sockfd, ldns_buffer_begin(qbin),
			ldns_buffer_position(qbin), 0, (struct sockaddr *)to, tolen);

	gettimeofday(&tv_e, NULL);

	if (bytes == -1) {
		printf("error with sending: %s\n", strerror(errno));
		close(sockfd);
		return NULL;
	}

	if ((size_t) bytes != ldns_buffer_position(qbin)) {
		printf("amount mismatch\n");
		close(sockfd);
		return NULL;
	}
	
	/* wait for an response*/
	answer = XMALLOC(uint8_t, MAX_PACKET_SIZE);
	if (!answer) {
		printf("respons alloc error\n");
		return NULL;
	}

	bytes = recv(sockfd, answer, MAX_PACKET_SIZE, 0);

	close(sockfd);

	if (bytes == -1) {
		printf("received too little\n");
		FREE(answer);
		return NULL;
	}
	
	/* resize accordingly */
	XREALLOC(answer, uint8_t *, (size_t) bytes);

        if (ldns_wire2pkt(&answer_pkt, answer, (size_t) bytes) != 
			LDNS_STATUS_OK) {
		printf("could not create packet\n");
		return NULL;
	} else {
		/* set some extra values in the pkt */
		ldns_pkt_set_querytime(answer_pkt,
				((tv_e.tv_sec - tv_s.tv_sec)*1000) +
				((tv_e.tv_usec - tv_s.tv_usec)/1000));

		return answer_pkt;
	}
}
<commit_msg>removed errno debugging stuff. Made preparation for adding tcp support<commit_after>/*
 * net.c
 *
 * Network implementation
 * All network related functions are grouped here
 *
 * a Net::DNS like library for C
 *
 * (c) NLnet Labs, 2004
 *
 * See the file LICENSE for the license
 */

#include <config.h>

#include <ldns/rdata.h>
#include <ldns/error.h>
#include <ldns/resolver.h>
#include <ldns/buffer.h>
#include <ldns/wire2host.h>
#include <ldns/host2wire.h>
#include <ldns/host2str.h>
#include <ldns/resolver.h>

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

#include "util.h"



#if 0
/* axfr is a hack - handle it different */
ldns_pkt *
ldns_sendbuf_axfr(ldns_buffer *buf, int *sockfd, struct sockaddr *dest)
{
	return NULL;
}
#endif 

/**
 * Send to ptk to the nameserver at ipnumber. Return the data
 * as a ldns_pkt
 * \param[in] resolver to use 
 * \param[in] query to send
 * \return the pkt received from the nameserver
 */
ldns_pkt *
ldns_send(ldns_resolver *r, ldns_pkt *query_pkt)
{
	uint8_t i;
	
	struct sockaddr_storage *ns;
	struct sockaddr_in *ns4;
	struct sockaddr_in6 *ns6;
	socklen_t ns_len;

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

	ns_array = ldns_resolver_nameservers(r);
	reply = NULL;
	
	printf("we are in ldns_send()\n");
	qb = ldns_buffer_new(MAX_PACKET_SIZE);

	if (ldns_pkt2buffer_wire(qb, query_pkt) != LDNS_STATUS_OK) {
		printf("could not convert to wire fmt\n");
		return NULL;
	}

	printf("nameservers %d\n",ldns_resolver_nameserver_count(r));
	
	/* loop through all defined nameservers */
	for (i = 0; i < ldns_resolver_nameserver_count(r); i++) {

		ns = ldns_rdf2native_sockaddr_storage(ns_array[i]);

		/* setup some family specific stuff */
		switch(ns->ss_family) {
			case AF_INET:
				ns4 = (struct sockaddr_in*) ns;
				ns4->sin_port = htons(ldns_resolver_port(r));
				ns_len = sizeof(struct sockaddr_in);
				printf("port %d\n", ntohs(ns4->sin_port));
				break;
			case AF_INET6:
				ns6 = (struct sockaddr_in6*) ns;
				ns6->sin6_port = htons(ldns_resolver_port(r));
				ns_len = sizeof(struct sockaddr_in6);
				printf("port %d\n", ntohs(ns6->sin6_port));
				break;
		}

		printf("ip address len %d\n", ns_len);

		/* query */
		reply = ldns_send_udp(qb, ns, ns_len);
		
		if (reply) {
			ldns_pkt_set_answerfrom(reply, ns_array[i]);
			printf("reply found\n");
			break;
		}
	}
	return reply;
}


/**
 * Send a buffer to an ip using udp and return the respons as a ldns_pkt
 * \param[in] qbin the ldns_buffer to be send
 * \param[in] to the ip addr to send to
 * \param[in] tolen length of the ip addr
 * \return a packet with the answer
 */
ldns_pkt *
ldns_send_udp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen)
{
	int sockfd;
	ssize_t bytes;
	uint8_t *answer;
	ldns_pkt *answer_pkt;

	struct timeval tv_s;
        struct timeval tv_e;

	gettimeofday(&tv_s, NULL);

	if ((sockfd = socket(((struct sockaddr*)to)->sa_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
		printf("could not open socket\n");
		return NULL;
	}

	bytes = sendto(sockfd, ldns_buffer_begin(qbin),
			ldns_buffer_position(qbin), 0, (struct sockaddr *)to, tolen);

	gettimeofday(&tv_e, NULL);

	if (bytes == -1) {
		printf("error with sending: %s\n");
		close(sockfd);
		return NULL;
	}

	if ((size_t) bytes != ldns_buffer_position(qbin)) {
		printf("amount mismatch\n");
		close(sockfd);
		return NULL;
	}
	
	/* wait for an response*/
	answer = XMALLOC(uint8_t, MAX_PACKET_SIZE);
	if (!answer) {
		printf("respons alloc error\n");
		return NULL;
	}

	bytes = recv(sockfd, answer, MAX_PACKET_SIZE, 0);

	close(sockfd);

	if (bytes == -1) {
		printf("received too little\n");
		FREE(answer);
		return NULL;
	}
	
	/* resize accordingly */
	XREALLOC(answer, uint8_t *, (size_t) bytes);

        if (ldns_wire2pkt(&answer_pkt, answer, (size_t) bytes) != 
			LDNS_STATUS_OK) {
		printf("could not create packet\n");
		return NULL;
	} else {
		/* set some extra values in the pkt */
		ldns_pkt_set_querytime(answer_pkt,
				((tv_e.tv_sec - tv_s.tv_sec)*1000) +
				((tv_e.tv_usec - tv_s.tv_usec)/1000));

		return answer_pkt;
	}
}

/**
 * Send a buffer to an ip using tcp and return the respons as a ldns_pkt
 * \param[in] qbin the ldns_buffer to be send
 * \param[in] to the ip addr to send to
 * \param[in] tolen length of the ip addr
 * \return a packet with the answer
 */
ldns_pkt *
ldns_send_tcp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen)
{
	int sockfd;
	ssize_t bytes;
	uint8_t *answer;
	ldns_pkt *answer_pkt;

	struct timeval tv_s;
        struct timeval tv_e;

	gettimeofday(&tv_s, NULL);

	if ((sockfd = socket(((struct sockaddr*)to)->sa_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
		printf("could not open socket\n");
		return NULL;
	}

	bytes = sendto(sockfd, ldns_buffer_begin(qbin),
			ldns_buffer_position(qbin), 0, (struct sockaddr *)to, tolen);

	gettimeofday(&tv_e, NULL);

	if (bytes == -1) {
		printf("error with sending\n");
		close(sockfd);
		return NULL;
	}

	if ((size_t) bytes != ldns_buffer_position(qbin)) {
		printf("amount mismatch\n");
		close(sockfd);
		return NULL;
	}
	
	/* wait for an response*/
	answer = XMALLOC(uint8_t, MAX_PACKET_SIZE);
	if (!answer) {
		printf("respons alloc error\n");
		return NULL;
	}

	bytes = recv(sockfd, answer, MAX_PACKET_SIZE, 0);

	close(sockfd);

	if (bytes == -1) {
		printf("received too little\n");
		FREE(answer);
		return NULL;
	}
	
	/* resize accordingly */
	XREALLOC(answer, uint8_t *, (size_t) bytes);

        if (ldns_wire2pkt(&answer_pkt, answer, (size_t) bytes) != 
			LDNS_STATUS_OK) {
		printf("could not create packet\n");
		return NULL;
	} else {
		/* set some extra values in the pkt */
		ldns_pkt_set_querytime(answer_pkt,
				((tv_e.tv_sec - tv_s.tv_sec)*1000) +
				((tv_e.tv_usec - tv_s.tv_usec)/1000));

		return answer_pkt;
	}
}

{'fraction_non_alphanumeric': 0.13884681409673835, 'fraction_numerical': 0.010299909118448955, 'mean_word_length': 3.915136476426799, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 45, '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': '2022705', 'n_tokens_mistral': 4263, 'n_tokens_neox': 3948, 'n_words': 948}