<commit_before>/*
 * Copyright (C) 2000  Internet Software Consortium.
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

#include <config.h>

#include <isc/mem.h>
#include <isc/netaddr.h>
#include <isc/string.h>		/* Required for HP/UX (and others?) */
#include <isc/task.h>
#include <isc/util.h>

#include <dns/byaddr.h>
#include <dns/db.h>
#include <dns/events.h>
#include <dns/rdata.h>
#include <dns/rdataset.h>
#include <dns/resolver.h>
#include <dns/result.h>
#include <dns/view.h>

/*
 * XXXRTH  We could use a static event...
 */

struct dns_byaddr {
	/* Unlocked. */
	unsigned int		magic;
	isc_mem_t *		mctx;
	isc_mutex_t		lock;
	dns_fixedname_t		name;
	/* Locked by lock. */
	unsigned int		options;
	isc_task_t *		task;
	dns_view_t *		view;
	dns_byaddrevent_t *	event;
	dns_fetch_t *		fetch;
	unsigned int		restarts;
	isc_boolean_t		canceled;
	dns_rdataset_t		rdataset;
};

#define BYADDR_MAGIC			0x42794164U	/* ByAd. */
#define VALID_BYADDR(b)			((b) != NULL && \
					 (b)->magic == BYADDR_MAGIC)

#define MAX_RESTARTS 16

static void byaddr_find(dns_byaddr_t *byaddr, dns_fetchevent_t *event);

static char hex_digits[] = {
	'0', '1', '2', '3', '4', '5', '6', '7', 
	'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};

static inline isc_result_t
address_to_ptr_name(dns_byaddr_t *byaddr, isc_netaddr_t *address) {
	char textname[128];
	unsigned char *bytes;
	int i;
	char *cp;
	isc_buffer_t buffer;
	unsigned int len;

	/*
	 * The caller must be holding the byaddr's lock.
	 */

	/*
	 * We create the text representation and then convert to a
	 * dns_name_t.  This is not maximally efficient, but it keeps all
	 * of the knowledge of wire format in the dns_name_ routines.
	 */

	dns_fixedname_init(&byaddr->name);
	bytes = (unsigned char *)(&address->type);
	if (address->family == AF_INET) {
		(void)sprintf(textname, "%u.%u.%u.%u.in-addr.arpa.",
			      (bytes[3] & 0xff),
			      (bytes[2] & 0xff),
			      (bytes[1] & 0xff),
			      (bytes[0] & 0xff));
	} else if (address->family == AF_INET6) {
		if ((byaddr->options & DNS_BYADDROPT_IPV6NIBBLE) != 0) {
			cp = textname;
			for (i = 15; i >= 0; i--) {
				*cp++ = hex_digits[bytes[i] & 0x0f];
				*cp++ = '.';
				*cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
				*cp++ = '.';
			}
			strcpy(cp, "ip6.int.");
		} else {
			cp = textname;
			*cp++ = '\\';
			*cp++ = '[';
			*cp++ = 'x';
			for (i = 0; i < 16; i += 2) {
				*cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
				*cp++ = hex_digits[bytes[i] & 0x0f];
				*cp++ = hex_digits[(bytes[i+1] >> 4) & 0x0f];
				*cp++ = hex_digits[bytes[i+1] & 0x0f];
			}
			strcpy(cp, "].ip6.int.");
		}
	} else
		return (ISC_R_NOTIMPLEMENTED);

	len = (unsigned int)strlen(textname);
	isc_buffer_init(&buffer, textname, len);
	isc_buffer_add(&buffer, len);
	return (dns_name_fromtext(dns_fixedname_name(&byaddr->name),
				  &buffer, dns_rootname, ISC_FALSE, NULL));
}

static inline isc_result_t
copy_ptr_targets(dns_byaddr_t *byaddr) {
	isc_result_t result;
	dns_name_t *name;
	dns_name_t target;
	dns_rdata_t rdata;
	isc_region_t r;

	/*
	 * The caller must be holding the byaddr's lock.<commit_msg>Use ip6.arpa for bitstring labels<commit_after>/*
 * Copyright (C) 2000  Internet Software Consortium.
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

#include <config.h>

#include <isc/mem.h>
#include <isc/netaddr.h>
#include <isc/string.h>		/* Required for HP/UX (and others?) */
#include <isc/task.h>
#include <isc/util.h>

#include <dns/byaddr.h>
#include <dns/db.h>
#include <dns/events.h>
#include <dns/rdata.h>
#include <dns/rdataset.h>
#include <dns/resolver.h>
#include <dns/result.h>
#include <dns/view.h>

/*
 * XXXRTH  We could use a static event...
 */

struct dns_byaddr {
	/* Unlocked. */
	unsigned int		magic;
	isc_mem_t *		mctx;
	isc_mutex_t		lock;
	dns_fixedname_t		name;
	/* Locked by lock. */
	unsigned int		options;
	isc_task_t *		task;
	dns_view_t *		view;
	dns_byaddrevent_t *	event;
	dns_fetch_t *		fetch;
	unsigned int		restarts;
	isc_boolean_t		canceled;
	dns_rdataset_t		rdataset;
};

#define BYADDR_MAGIC			0x42794164U	/* ByAd. */
#define VALID_BYADDR(b)			((b) != NULL && \
					 (b)->magic == BYADDR_MAGIC)

#define MAX_RESTARTS 16

static void byaddr_find(dns_byaddr_t *byaddr, dns_fetchevent_t *event);

static char hex_digits[] = {
	'0', '1', '2', '3', '4', '5', '6', '7', 
	'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};

static inline isc_result_t
address_to_ptr_name(dns_byaddr_t *byaddr, isc_netaddr_t *address) {
	char textname[128];
	unsigned char *bytes;
	int i;
	char *cp;
	isc_buffer_t buffer;
	unsigned int len;

	/*
	 * The caller must be holding the byaddr's lock.
	 */

	/*
	 * We create the text representation and then convert to a
	 * dns_name_t.  This is not maximally efficient, but it keeps all
	 * of the knowledge of wire format in the dns_name_ routines.
	 */

	dns_fixedname_init(&byaddr->name);
	bytes = (unsigned char *)(&address->type);
	if (address->family == AF_INET) {
		(void)sprintf(textname, "%u.%u.%u.%u.in-addr.arpa.",
			      (bytes[3] & 0xff),
			      (bytes[2] & 0xff),
			      (bytes[1] & 0xff),
			      (bytes[0] & 0xff));
	} else if (address->family == AF_INET6) {
		if ((byaddr->options & DNS_BYADDROPT_IPV6NIBBLE) != 0) {
			cp = textname;
			for (i = 15; i >= 0; i--) {
				*cp++ = hex_digits[bytes[i] & 0x0f];
				*cp++ = '.';
				*cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
				*cp++ = '.';
			}
			strcpy(cp, "ip6.int.");
		} else {
			cp = textname;
			*cp++ = '\\';
			*cp++ = '[';
			*cp++ = 'x';
			for (i = 0; i < 16; i += 2) {
				*cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
				*cp++ = hex_digits[bytes[i] & 0x0f];
				*cp++ = hex_digits[(bytes[i+1] >> 4) & 0x0f];
				*cp++ = hex_digits[bytes[i+1] & 0x0f];
			}
			strcpy(cp, "].ip6.arpa.");
		}
	} else
		return (ISC_R_NOTIMPLEMENTED);

	len = (unsigned int)strlen(textname);
	isc_buffer_init(&buffer, textname, len);
	isc_buffer_add(&buffer, len);
	return (dns_name_fromtext(dns_fixedname_name(&byaddr->name),
				  &buffer, dns_rootname, ISC_FALSE, NULL));
}

static inline isc_result_t
copy_ptr_targets(dns_byaddr_t *byaddr) {
	isc_result_t result;
	dns_name_t *name;
	dns_name_t target;
	dns_rdata_t rdata;
	isc_region_t r;

	/*
	 * The caller must be holding the byaddr's lock.
{'fraction_non_alphanumeric': 0.1482205709816191, 'fraction_numerical': 0.017077304132446878, 'mean_word_length': 3.627261761158022, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 59, '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': '5279016', 'n_tokens_mistral': 3314, 'n_tokens_neox': 3011, 'n_words': 813}