⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 t_names.c

📁 package of develop dns
💻 C
📖 第 1 页 / 共 4 页
字号:
/* * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC") * Copyright (C) 1998-2003  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 ISC DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS.  IN NO EVENT SHALL ISC 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. *//* $Id: t_names.c,v 1.32.2.2.8.3 2004/03/08 21:06:23 marka Exp $ */#include <config.h>#include <ctype.h>#include <stdlib.h>#include <isc/buffer.h>#include <isc/mem.h>#include <isc/string.h>#include <dns/compress.h>#include <dns/name.h>#include <dns/result.h>#include <tests/t_api.h>#define	MAXTOKS		16#define	BUFLEN		256#define	BIGBUFLEN	4096static char	*Tokens[MAXTOKS + 1];/* * get a hex formatted dns message from a data * file into an isc_buffer_t * caller supplies data storage and the isc_buffer * we read the file, convert, setup the buffer * and return the data length */#ifdef	NEED_PBUFstatic char *ctoh(unsigned char c) {	int		val;	static char	buf[3];	val = (c >> 4) & 0x0f;	if ((0 <= val) && (val <= 9))		buf[0] = '0' + val;	else if ((10 <= val) && (val <= 16))		buf[0] = 'a' + val - 10;	val = c & 0x0f;	if ((0 <= val) && (val <= 9))		buf[1] = '0' + val;	else if ((10 <= val) && (val <= 16))		buf[1] = 'a' + val - 10;	buf[2] = '\0';	return (buf);}static voidpbuf(isc_buffer_t *pbuf) {	size_t		len;	unsigned char	*p;	len = 0;	p = pbuf->base;	while (len < pbuf->length) {		printf("%s", ctoh(*p));		++p;		++len;		if ((len % 40) == 0)			printf("\n");	}}#endif	/* NEED_PBUF *//* * Compare data at buf with data in hex representation at exp_data, * of length exp_data_len, for equality. * Return 0 if equal, else non-zero. */static intchkdata(unsigned char *buf, size_t buflen, char *exp_data,	size_t exp_data_len){	int		result;	unsigned char	*p;	unsigned char	*v;	char		*q;	unsigned char	*data;	size_t		cnt;	if (buflen == exp_data_len) {		data = (unsigned char *)malloc(exp_data_len *					       sizeof(unsigned char));		if (data == NULL) {			t_info("malloc failed unexpectedly\n");			return (-1);		}		/*		 * First convert exp_data from hex format.		 */		p = data;		q = exp_data;		cnt = 0;		while (cnt < exp_data_len) {			if (('0' <= *q) && (*q <= '9'))				*p = *q - '0';			else if (('a' <= *q) && (*q <= 'z'))				*p = *q - 'a' + 10;			else if (('A' <= *q) && (*q <= 'Z'))				*p = *q - 'A' + 10;			++q;			*p <<= 4;			if (('0' <= *q) && (*q <= '9'))				*p |= ((*q - '0') & 0x0f);			else if (('a' <= *q) && (*q <= 'z'))				*p |= ((*q - 'a' + 10) & 0x0f);			else if (('A' <= *q) && (*q <= 'Z'))				*p |= ((*q - 'A' + 10) & 0x0f);			++p;			++q;			++cnt;		}		/*		 * Now compare data.		 */		p = buf;		v = data;		for (cnt = 0; cnt < exp_data_len; ++cnt) {			if (*p != *v)				break;			++p;			++v;		}		if (cnt == exp_data_len)			result = 0;		else {			t_info("bad data at position %lu, "			       "got 0x%.2x, expected 0x%.2x\n",			       (unsigned long)cnt, *p, *q);			result = cnt + 1;		}		(void)free(data);	} else {		t_info("data length error, expected %lu, got %lu\n",			(unsigned long)exp_data_len, (unsigned long)buflen);		result = exp_data_len - buflen;	}	return (result);}/* * Get a hex formatted dns message from a data file into an isc_buffer_t. * Caller supplies data storage and the isc_buffer.  We read the file, convert, * setup the buffer and return the data length. */static intgetmsg(char *datafile_name, unsigned char *buf, int buflen, isc_buffer_t *pbuf){	int			c;	int			len;	int			cnt;	unsigned int		val;	unsigned char		*p;	FILE			*fp;	fp = fopen(datafile_name, "r");	if (fp == NULL) {		t_info("No such file %s\n", datafile_name);		return (0);	}	p = buf;	cnt = 0;	len = 0;	val = 0;	while ((c = getc(fp)) != EOF) {		if (	(c == ' ') || (c == '\t') ||			(c == '\r') || (c == '\n'))				continue;		if (c == '#') {			while ((c = getc(fp)) != '\n')				;			continue;		}		if (('0' <= c) && (c <= '9'))			val = c - '0';		else if (('a' <= c) && (c <= 'z'))			val = c - 'a' + 10;		else if (('A' <= c) && (c <= 'Z'))			val = c - 'A'+ 10;		else {			t_info("Bad format in datafile\n");			return (0);		}		if ((len % 2) == 0) {			*p = (val << 4);		} else {			*p += val;			++p;			++cnt;			if (cnt >= buflen) {				/*				 * Buffer too small.				 */				t_info("Buffer overflow error\n");				return (0);			}		}		++len;	}	(void)fclose(fp);	if (len % 2) {		t_info("Bad format in %s\n", datafile_name);		return (0);	}	*p = '\0';	isc_buffer_init(pbuf, buf, cnt);	isc_buffer_add(pbuf, cnt);	return (cnt);}static intbustline(char *line, char **toks) {	int	cnt;	char	*p;	cnt = 0;	if (line && *line) {		while ((p = strtok(line, "\t")) && (cnt < MAXTOKS)) {			*toks++ = p;			line = NULL;			++cnt;		}	}	return (cnt);}/* * convert a name from hex representation to text form * format of hex notation is: * %xXXXXXXXX */#ifdef	NEED_HNAME_TO_TNAMEstatic inthname_to_tname(char *src, char *target, size_t len) {	int		i;	int		c;	unsigned int	val;	size_t		srclen;	char		*p;	char		*q;	p = src;	srclen = strlen(p);	if ((srclen >= 2) && ((*p != '%') || (*(p+1) != 'x'))) {		/*		 * No conversion needed.		 */		if (srclen >= len)			return (1);		memcpy(target, src, srclen + 1);		return (0);	}	i = 0;	p += 2;	q = target;	while (*p) {		c = *p;		if (('0' < c) && (c <= '9'))			val = c - '0';		else if (('a' <= c) && (c <= 'z'))			val = c + 10 - 'a';		else if (('A' <= c) && (c <= 'Z'))			val = c + 10 - 'A';		else {			return (1);		}		if (i % 2) {			*q |= val;			++q;		} else			*q = (val << 4);		++i;		++p;	}	if (i % 2) {		return (1);	} else {		*q = '\0';		return (0);	}}#endif	/* NEED_HNAME_TO_TNAME *//* * initialize a dns_name_t from a text name, hiding all * buffer and other object initialization from the caller * */static isc_result_tdname_from_tname(char *name, dns_name_t *dns_name) {	int		len;	isc_buffer_t	txtbuf;	isc_buffer_t	*binbuf;	unsigned char	*junk;	isc_result_t	result;	len = strlen(name);	isc_buffer_init(&txtbuf, name, len);	isc_buffer_add(&txtbuf, len);	junk = (unsigned char *)malloc(sizeof(unsigned char) * BUFLEN);	binbuf = (isc_buffer_t *)malloc(sizeof(isc_buffer_t));	if ((junk != NULL) && (binbuf != NULL)) {		isc_buffer_init(binbuf, junk, BUFLEN);		dns_name_init(dns_name, NULL);		dns_name_setbuffer(dns_name, binbuf);		result = dns_name_fromtext(dns_name,  &txtbuf,						NULL, ISC_FALSE, NULL);	} else {		result = ISC_R_NOSPACE;		if (junk != NULL)			(void)free(junk);		if (binbuf != NULL)			(void)free(binbuf);	}	return (result);}static const char *a3 =	"dns_name_init initializes 'name' to the empty name";static voidt_dns_name_init(void) {	int		rval;	int		result;	dns_name_t	name;	unsigned char	offsets[1];	rval = 0;	t_assert("dns_name_init", 1, T_REQUIRED, a3);	dns_name_init(&name, offsets);	/* magic is hidden in name.c ...	if (name.magic != NAME_MAGIC) {		t_info("name.magic is not set to NAME_MAGIC\n");		++rval;	}	*/	if (name.ndata != NULL) {		t_info("name.ndata is not NULL\n");		++rval;	}	if (name.length != 0) {		t_info("name.length is not 0\n");		++rval;	}	if (name.labels != 0) {		t_info("name.labels is not 0\n");		++rval;	}	if (name.attributes != 0) {		t_info("name.attributes is not 0\n");		++rval;	}	if (name.offsets != offsets) {		t_info("name.offsets is incorrect\n");		++rval;	}	if (name.buffer != NULL) {		t_info("name.buffer is not NULL\n");		++rval;	}	if (rval == 0)		result = T_PASS;	else		result = T_FAIL;	t_result(result);}static const char *a4 =	"dns_name_invalidate invalidates 'name'";static voidt_dns_name_invalidate(void) {	int		rval;	int		result;	dns_name_t	name;	unsigned char	offsets[1];	t_assert("dns_name_invalidate", 1, T_REQUIRED, a4);	rval = 0;	dns_name_init(&name, offsets);	dns_name_invalidate(&name);	/* magic is hidden in name.c ...	if (name.magic != 0) {		t_info("name.magic is not set to NAME_MAGIC\n");		++rval;	}	*/	if (name.ndata != NULL) {		t_info("name.ndata is not NULL\n");		++rval;	}	if (name.length != 0) {		t_info("name.length is not 0\n");		++rval;	}	if (name.labels != 0) {		t_info("name.labels is not 0\n");		++rval;	}	if (name.attributes != 0) {		t_info("name.attributes is not 0\n");		++rval;	}	if (name.offsets != NULL) {		t_info("name.offsets is not NULL\n");		++rval;	}	if (name.buffer != NULL) {		t_info("name.buffer is not NULL\n");		++rval;	}	if (rval == 0)		result = T_PASS;	else		result = T_FAIL;	t_result(result);}static const char *a5 =	"dns_name_setbuffer dedicates a buffer for use "			"with 'name'";static voidt_dns_name_setbuffer(void) {	int		result;	unsigned char	junk[BUFLEN];	dns_name_t	name;	isc_buffer_t	buffer;	t_assert("dns_name_setbuffer", 1, T_REQUIRED, a5);	isc_buffer_init(&buffer, junk, BUFLEN);	dns_name_init(&name, NULL);	dns_name_setbuffer(&name, &buffer);	if (name.buffer == &buffer)		result = T_PASS;	else		result = T_FAIL;	t_result(result);}static const char *a6 =	"dns_name_hasbuffer returns ISC_TRUE if 'name' has a "			"dedicated buffer, otherwise it returns ISC_FALSE";static voidt_dns_name_hasbuffer(void) {	int		result;	int		rval;	unsigned char	junk[BUFLEN];	dns_name_t	name;	isc_buffer_t	buffer;	t_assert("dns_name_hasbuffer", 1, T_REQUIRED, a6);	rval = 0;	isc_buffer_init(&buffer, junk, BUFLEN);	dns_name_init(&name, NULL);	if (dns_name_hasbuffer(&name) != ISC_FALSE)		++rval;	dns_name_setbuffer(&name, &buffer);	if (dns_name_hasbuffer(&name) != ISC_TRUE)		++rval;	if (rval == 0)		result = T_PASS;	else		result = T_FAIL;	t_result(result);}static const char *a7 =	"dns_name_isabsolute returns ISC_TRUE if 'name' ends "			"in the root label";static inttest_dns_name_isabsolute(char *test_name, isc_boolean_t expected) {	dns_name_t	name;	isc_buffer_t	buf;	isc_buffer_t	binbuf;	unsigned char	junk[BUFLEN];	int		len;	int		rval;	isc_boolean_t	isabs_p;	isc_result_t	result;	rval = T_UNRESOLVED;	t_info("testing name %s\n", test_name);	len = strlen(test_name);	isc_buffer_init(&buf, test_name, len);	isc_buffer_add(&buf, len);	isc_buffer_init(&binbuf, &junk[0], BUFLEN);	dns_name_init(&name, NULL);	dns_name_setbuffer(&name, &binbuf);	result = dns_name_fromtext(&name,  &buf, NULL, ISC_FALSE, NULL);	if (result == ISC_R_SUCCESS) {		isabs_p = dns_name_isabsolute(&name);		if (isabs_p == expected)			rval = T_PASS;		else			rval = T_FAIL;	} else {		t_info("dns_name_fromtext %s failed, result = %s\n",				test_name, dns_result_totext(result));	}	return (rval);}static voidt_dns_name_isabsolute(void) {	int	line;	int	cnt;	int	result;	char	*p;	FILE	*fp;	t_assert("dns_name_isabsolute", 1, T_REQUIRED, a7);	result = T_UNRESOLVED;	fp = fopen("dns_name_isabsolute_data", "r");	if (fp != NULL) {		line = 0;		while ((p = t_fgetbs(fp)) != NULL) {			++line;			/*			 * Skip comment lines.			 */			if ((isspace((unsigned char)*p)) || (*p == '#'))				continue;			cnt = bustline(p, Tokens);			if (cnt == 2) {				/*				 * label, bitpos, expected value.				 */				result = test_dns_name_isabsolute(Tokens[0],							        atoi(Tokens[1])								  == 0 ?								  ISC_FALSE :

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -