zone-dump-text.c 27.5 KB
Newer Older
1 2 3 4 5 6 7
/*!
 * \file zone-dump-text.c
 *
 * \author Jan Kadlec <jan.kadlec@nic.cz>, conversion functions by NLabs,
 *         see LICENSE. b64ntop by ISC.
 */

8
#include <ctype.h>
9 10
#include <assert.h>
#include <arpa/inet.h>
11 12

#include "dnslib.h"
Lubos Slovak's avatar
Lubos Slovak committed
13
#include "common/skip-list.h"
14

15 16
/* TODO max length of alg */

17 18 19 20 21
enum uint_max_length {
	U8_MAX_STR_LEN = 4, U16_MAX_STR_LEN = 6,
	U32_MAX_STR_LEN = 11, MAX_RR_TYPE_LEN = 20,
	MAX_NSEC_BIT_STR_LEN = 4096,
	};
22

23 24 25
#define APL_NEGATION_MASK      0x80U
#define APL_LENGTH_MASK	       (~APL_NEGATION_MASK)

26
/* TODO to be moved elsewhere */
27 28
int b32_ntop(uint8_t const *src, size_t srclength, char *target,
	     size_t targsize)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
{
	static char b32[]="0123456789abcdefghijklmnopqrstuv";
	char buf[9];
	ssize_t len=0;

	while(srclength > 0)
	{
		int t;
		memset(buf,'\0',sizeof buf);

		/* xxxxx000 00000000 00000000 00000000 00000000 */
		buf[0]=b32[src[0] >> 3];

		/* 00000xxx xx000000 00000000 00000000 00000000 */
		t=(src[0]&7) << 2;
		if(srclength > 1)
			t+=src[1] >> 6;
		buf[1]=b32[t];
		if(srclength == 1)
			break;

		/* 00000000 00xxxxx0 00000000 00000000 00000000 */
		buf[2]=b32[(src[1] >> 1)&0x1f];

		/* 00000000 0000000x xxxx0000 00000000 00000000 */
		t=(src[1]&1) << 4;
		if(srclength > 2)
			t+=src[2] >> 4;
		buf[3]=b32[t];
		if(srclength == 2)
			break;

		/* 00000000 00000000 0000xxxx x0000000 00000000 */
		t=(src[2]&0xf) << 1;
		if(srclength > 3)
			t+=src[3] >> 7;
		buf[4]=b32[t];
		if(srclength == 3)
			break;

		/* 00000000 00000000 00000000 0xxxxx00 00000000 */
		buf[5]=b32[(src[3] >> 2)&0x1f];

		/* 00000000 00000000 00000000 000000xx xxx00000 */
		t=(src[3]&3) << 3;
		if(srclength > 4)
			t+=src[4] >> 5;
		buf[6]=b32[t];
		if(srclength == 4)
			break;

		/* 00000000 00000000 00000000 00000000 000xxxxx */
		buf[7]=b32[src[4]&0x1f];

		if(targsize < 8)
			return -1;

		src += 5;
		srclength -= 5;

		memcpy(target,buf,8);
		target += 8;
		targsize -= 8;
		len += 8;
	}
	if(srclength)
	{
		if(targsize < strlen(buf)+1)
			return -1;
		dnslib_strlcpy(target, buf, targsize);
		len += strlen(buf);
	}
	else if(targsize < 1)
		return -1;
	else
		*target='\0';
	return len;
}

108 109
/* Following copyrights are only valid for b64_ntop function */

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
/*
 * Copyright (c) 1996, 1998 by 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.
 */

/*
 * Portions Copyright (c) 1995 by International Business Machines, Inc.
 *
 * International Business Machines, Inc. (hereinafter called IBM) grants
 * permission under its copyrights to use, copy, modify, and distribute this
 * Software with or without fee, provided that the above copyright notice and
 * all paragraphs of this notice appear in all copies, and that the name of IBM
 * not be used in connection with the marketing of any product incorporating
 * the Software or modifications thereof, without specific, written prior
 * permission.
 *
 * To the extent it has a right to do so, IBM grants an immunity from suit
 * under its patents, if any, for the use, sale or manufacture of products to
 * the extent that such products are used for performing Domain Name System
 * dynamic updates in TCP/IP networks by means of the Software.  No immunity is
 * granted for any product per se or for any other function of any product.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
 */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define Assert(Cond) if (!(Cond)) abort()

static const char Base64[] =
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char Pad64 = '=';

/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
   The following encoding technique is taken from RFC 1521 by Borenstein
   and Freed.  It is reproduced here in a slightly edited form for
   convenience.

   A 65-character subset of US-ASCII is used, enabling 6 bits to be
   represented per printable character. (The extra 65th character, "=",
   is used to signify a special processing function.)

   The encoding process represents 24-bit groups of input bits as output
   strings of 4 encoded characters. Proceeding from left to right, a
   24-bit input group is formed by concatenating 3 8-bit input groups.
   These 24 bits are then treated as 4 concatenated 6-bit groups, each
   of which is translated into a single digit in the base64 alphabet.

   Each 6-bit group is used as an index into an array of 64 printable
   characters. The character referenced by the index is placed in the
   output string.

                         Table 1: The Base64 Alphabet

      Value Encoding  Value Encoding  Value Encoding  Value Encoding
          0 A            17 R            34 i            51 z
          1 B            18 S            35 j            52 0
          2 C            19 T            36 k            53 1
          3 D            20 U            37 l            54 2
          4 E            21 V            38 m            55 3
          5 F            22 W            39 n            56 4
          6 G            23 X            40 o            57 5
          7 H            24 Y            41 p            58 6
          8 I            25 Z            42 q            59 7
          9 J            26 a            43 r            60 8
         10 K            27 b            44 s            61 9
         11 L            28 c            45 t            62 +
         12 M            29 d            46 u            63 /
         13 N            30 e            47 v
         14 O            31 f            48 w         (pad) =
         15 P            32 g            49 x
         16 Q            33 h            50 y

   Special processing is performed if fewer than 24 bits are available
   at the end of the data being encoded.  A full encoding quantum is
   always completed at the end of a quantity.  When fewer than 24 input
   bits are available in an input group, zero bits are added (on the
   right) to form an integral number of 6-bit groups.  Padding at the
   end of the data is performed using the '=' character.

   Since all base64 input is an integral number of octets, only the
         -------------------------------------------------                       
   following cases can arise:
   
       (1) the final quantum of encoding input is an integral
           multiple of 24 bits; here, the final unit of encoded
	   output will be an integral multiple of 4 characters
	   with no "=" padding,
       (2) the final quantum of encoding input is exactly 8 bits;
           here, the final unit of encoded output will be two
	   characters followed by two "=" padding characters, or
       (3) the final quantum of encoding input is exactly 16 bits;
           here, the final unit of encoded output will be three
	   characters followed by one "=" padding character.
   */

232 233
int b64_ntop(uint8_t const *src, size_t srclength, char *target,
	     size_t targsize) {
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	size_t datalength = 0;
	uint8_t input[3];
	uint8_t output[4];
	size_t i;

	while (2 < srclength) {
		input[0] = *src++;
		input[1] = *src++;
		input[2] = *src++;
		srclength -= 3;

		output[0] = input[0] >> 2;
		output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
		output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
		output[3] = input[2] & 0x3f;
		Assert(output[0] < 64);
		Assert(output[1] < 64);
		Assert(output[2] < 64);
		Assert(output[3] < 64);

		if (datalength + 4 > targsize)
			return (-1);
		target[datalength++] = Base64[output[0]];
		target[datalength++] = Base64[output[1]];
		target[datalength++] = Base64[output[2]];
		target[datalength++] = Base64[output[3]];
	}
    
	/* Now we worry about padding. */
	if (0 != srclength) {
		/* Get what's left. */
		input[0] = input[1] = input[2] = '\0';
		for (i = 0; i < srclength; i++)
			input[i] = *src++;
	
		output[0] = input[0] >> 2;
		output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
		output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
		Assert(output[0] < 64);
		Assert(output[1] < 64);
		Assert(output[2] < 64);

		if (datalength + 4 > targsize)
			return (-1);
		target[datalength++] = Base64[output[0]];
		target[datalength++] = Base64[output[1]];
		if (srclength == 1)
			target[datalength++] = Pad64;
		else
			target[datalength++] = Base64[output[2]];
		target[datalength++] = Pad64;
	}
	if (datalength >= targsize)
		return (-1);
	target[datalength] = '\0';	/* Returned value doesn't count \0. */
	return (datalength);
}

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
/* Taken from RFC 4398, section 2.1.  */
dnslib_lookup_table_t dnslib_dns_certificate_types[] = {
/*	0		Reserved */
	{ 1, "PKIX" },	/* X.509 as per PKIX */
	{ 2, "SPKI" },	/* SPKI cert */
	{ 3, "PGP" },	/* OpenPGP packet */
	{ 4, "IPKIX" },	/* The URL of an X.509 data object */
	{ 5, "ISPKI" },	/* The URL of an SPKI certificate */
	{ 6, "IPGP" },	/* The fingerprint and URL of an OpenPGP packet */
	{ 7, "ACPKIX" },	/* Attribute Certificate */
	{ 8, "IACPKIX" },	/* The URL of an Attribute Certificate */
	{ 253, "URI" },	/* URI private */
	{ 254, "OID" },	/* OID private */
/*	255 		Reserved */
/* 	256-65279	Available for IANA assignment */
/*	65280-65534	Experimental */
/*	65535		Reserved */
	{ 0, NULL }
};

/* Taken from RFC 2535, section 7.  */
dnslib_lookup_table_t dnslib_dns_algorithms[] = {
	{ 1, "RSAMD5" },	/* RFC 2537 */
	{ 2, "DH" },		/* RFC 2539 */
	{ 3, "DSA" },		/* RFC 2536 */
	{ 4, "ECC" },
	{ 5, "RSASHA1" },	/* RFC 3110 */
	{ 252, "INDIRECT" },
	{ 253, "PRIVATEDNS" },
	{ 254, "PRIVATEOID" },
	{ 0, NULL }
};

325 326 327 328 329 330 331 332 333
int get_bit(uint8_t bits[], size_t index)
{
	/*
	 * The bits are counted from left to right, so bit #0 is the
	 * left most bit.
	 */
	return bits[index / 8] & (1 << (7 - index % 8));
}

334 335 336 337 338
static inline uint8_t * rdata_item_data(dnslib_rdata_item_t item)
{
	return (uint8_t *)(item.raw_data + 1);
}

339 340 341 342 343
static inline uint8_t rdata_item_size(dnslib_rdata_item_t item)
{
	return item.raw_data[0];
}

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
char *rdata_dname_to_string(dnslib_rdata_item_t item)
{
	return dnslib_dname_to_str(item.dname);
}

char *rdata_dns_name_to_string(dnslib_rdata_item_t item)
{
	return dnslib_dname_to_str(item.dname);
}

char *rdata_text_to_string(dnslib_rdata_item_t item)
{
	const uint8_t *data = (const uint8_t *) item.raw_data + 1;
	uint8_t length = data[0];
	size_t i;

360
	/* XXX */
361

362 363 364 365 366 367 368 369
	/* 3 because: opening '"', closing '"', and \0 at the end */
	char *ret = malloc(sizeof(char) * (length + 3));

	memset(ret, 0, sizeof(char) * (length + 3));

	size_t current_length = sizeof(char) * (length + 3);

	strcat(ret, "\\");
370 371 372 373 374 375 376

	for (i = 1; i <= length; i++) {
		char ch = (char) data[i];
		if (isprint((int)ch)) {
			if (ch == '"' || ch == '\\') {
				strcat(ret, "\\");
			}
377 378
				/* XXX for the love of god, how to this better,
				   but w/o obscure self-made functions */
379
				/* MANUALLY */
380 381 382 383 384 385 386
				char tmp_str[2];
				tmp_str[0] = ch;
				tmp_str[1] = 0;
				strcat(ret, tmp_str);
		} else {
			strcat(ret, "\\");
			char tmp_str[2];
387
			/* TODO convert to unsigned*/
388 389 390
			tmp_str[0] = ch;
			tmp_str[1] = 0;

391 392 393 394 395 396 397
			current_length += sizeof(char);

			if (realloc(ret, current_length) == NULL) {
				ERR_ALLOC_FAILED;
				return NULL;
			}

398 399 400 401 402 403 404 405 406 407 408 409
			strcat(ret, tmp_str);
			// XXX
//			buffer_printf(output, "\\%03u", (unsigned) ch);
		}
	}
	strcat(ret, "\"");

	return ret;
}

char *rdata_byte_to_string(dnslib_rdata_item_t item)
{
410
	assert(item.raw_data[0] == 1);
411
	uint8_t data = item.raw_data[1];
412 413
	char *ret = malloc(sizeof(char) * U8_MAX_STR_LEN);
	snprintf(ret, U8_MAX_STR_LEN, "%d", (char) data);
414 415 416
	return ret;
}

417
char *rdata_short_to_string(dnslib_rdata_item_t item)
418
{
419
	uint16_t data = dnslib_wire_read_u16(rdata_item_data(item));
420 421
	char *ret = malloc(sizeof(char) * U16_MAX_STR_LEN);
	snprintf(ret, U16_MAX_STR_LEN, "%u", data);
422 423 424
	/* XXX Use proper macros - see response tests*/
	/* XXX check return value, return NULL on failure */
	return ret;
425 426
}

427
char *rdata_long_to_string(dnslib_rdata_item_t item)
428
{
429
	uint32_t data = dnslib_wire_read_u32(rdata_item_data(item));
430
	char *ret = malloc(sizeof(char) * U32_MAX_STR_LEN);
431
	/* u should be enough */
432
	snprintf(ret, U32_MAX_STR_LEN, "%u", data);
433
	return ret;
434 435
}

436
char *rdata_a_to_string(dnslib_rdata_item_t item)
437
{
438 439 440 441 442 443
	/* 200 seems like a little too much */
	char *ret = malloc(sizeof(char) * 200);
	if (inet_ntop(AF_INET, rdata_item_data(item), ret, 200)) {
		return ret;
	} else {
		return NULL;
444 445 446
	}
}

447
char *rdata_aaaa_to_string(dnslib_rdata_item_t item)
448
{
449 450 451 452 453
	char *ret = malloc(sizeof(char) * 200);
	if (inet_ntop(AF_INET6, rdata_item_data(item), ret, 200)) {
		return ret;
	} else {
		return NULL;
454 455 456
	}
}

457
char *rdata_rrtype_to_string(dnslib_rdata_item_t item)
458
{
459 460
	uint16_t type = dnslib_wire_read_u16(rdata_item_data(item));
	const char *tmp = dnslib_rrtype_to_string(type);
461 462
	char *ret = malloc(sizeof(char) * MAX_RR_TYPE_LEN);
	strncpy(ret, tmp, MAX_RR_TYPE_LEN);
463
	return ret;
464 465
}

466
char *rdata_algorithm_to_string(dnslib_rdata_item_t item)
467
{
468
	uint8_t id = *rdata_item_data(item);
469
	char *ret = malloc(sizeof(char) * MAX_RR_TYPE_LEN);
470 471
	dnslib_lookup_table_t *alg
		= dnslib_lookup_by_id(dnslib_dns_algorithms, id);
472
	if (alg) {
473
		strncpy(ret, alg->name, MAX_RR_TYPE_LEN);
474
	} else {
475
		snprintf(ret, U8_MAX_STR_LEN, "%u", id);
476
	}
477 478

	return ret;
479 480
}

481
char *rdata_certificate_type_to_string(dnslib_rdata_item_t item)
482
{
483
	uint16_t id = dnslib_wire_read_u16(rdata_item_data(item));
484
	char *ret = malloc(sizeof(char) * MAX_RR_TYPE_LEN);
485 486
	dnslib_lookup_table_t *type
		= dnslib_lookup_by_id(dnslib_dns_certificate_types, id);
487
	if (type) {
488
		strncpy(ret, type->name, MAX_RR_TYPE_LEN);
489
	} else {
490
		snprintf(ret, U16_MAX_STR_LEN, "%u", id);
491
	}
492 493

	return ret;
494 495
}

496
char *rdata_period_to_string(dnslib_rdata_item_t item)
497
{
498
	/* uint32 but read 16 XXX */
499
	uint32_t period = dnslib_wire_read_u32(rdata_item_data(item));
500 501 502
	char *ret = malloc(sizeof(char) * U32_MAX_STR_LEN);
	snprintf(ret, U32_MAX_STR_LEN, "%u", period);
	return ret;
503 504
}

505
char *rdata_time_to_string(dnslib_rdata_item_t item)
506
{
507
	time_t time = (time_t) dnslib_wire_read_u32(rdata_item_data(item));
508
	struct tm *tm = gmtime(&time);
509 510 511 512 513
	char *ret = malloc(sizeof(char) * 15);
	if (strftime(ret, 15, "%Y%m%d%H%M%S", tm)) {
		return ret;
	} else {
		return NULL;
514 515 516
	}
}

517
char *rdata_base32_to_string(dnslib_rdata_item_t item)
518 519
{
	int length;
520 521 522 523 524 525
	size_t size = rdata_item_size(item);
	if (size == 0) {
		char *ret = malloc(sizeof(char) * 2);
		ret[0] = '-';
		ret[1] = '\0';
		return ret;
526 527
	}
	size -= 1; // remove length byte from count
528 529 530
	char *ret = malloc(sizeof(char) * (size * 2 + 1));
	length = b32_ntop(rdata_item_data(item)+1, size,
			  ret, size * 2);
531
	if (length > 0) {
532 533 534 535
		return ret;
	} else {
		free(ret);
		return NULL;
536 537 538
	}
}

539
char *rdata_base64_to_string(dnslib_rdata_item_t item)
540 541
{
	int length;
542 543
	size_t size = rdata_item_size(item);
	/* XXX check with the originals !!! */
544
	char *ret = malloc((sizeof(char) * 2 * size) + 1 * sizeof(char));
545 546
	length = b64_ntop(rdata_item_data(item), size,
			  ret, size * 2);
547
	if (length > 0) {
548 549 550 551
		return ret;
	} else {
		free(ret);
		return NULL;
552 553 554
	}
}

555 556
char *hex_to_string(const uint8_t *data, size_t size)
{
557 558 559 560 561 562
	static const char hexdigits[] = {
		'0', '1', '2', '3', '4', '5', '6', '7',
		'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
	};
	size_t i;

563
	char *ret = malloc(sizeof(char) * (size * 2 + 1));
564 565

	for (i = 0; i < size * 2; i += 2) {
566
		uint8_t octet = *data++;
567 568
		ret[i] = hexdigits [octet >> 4];
		ret[i + 1] = hexdigits [octet & 0x0f];
569
	}
570 571 572

	/* TODO triple check */

573
	ret[--i] = '\0';
574 575

	return ret;
576 577
}

578
char *rdata_hex_to_string(dnslib_rdata_item_t item)
579
{
580
	return hex_to_string(rdata_item_data(item), rdata_item_size(item));
581 582
}

583
char *rdata_hexlen_to_string(dnslib_rdata_item_t item)
584
{
585
	if(rdata_item_size(item) <= 1) {
586
		// NSEC3 salt hex can be empty
587 588 589 590 591 592 593
		char *ret = malloc(sizeof(char) * 2);
		ret[0] = '-';
		ret[1] = '\0';
		return ret;
	} else {
		return hex_to_string(rdata_item_data(item)+1,
		                     rdata_item_size(item)-1);
594 595 596
	}
}

597
char *rdata_nsap_to_string(dnslib_rdata_item_t item)
598
{
599 600 601
	char *ret = malloc(sizeof(char) * (rdata_item_size(item) + 3));
	strcat(ret, "0x");
	/* TODO maybe assert? */
602 603
	strcat(ret, hex_to_string(rdata_item_data(item),
				  rdata_item_size(item)));
604
	return ret;
605 606
}

607
char *rdata_apl_to_string(dnslib_rdata_item_t item)
608
{
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
	uint8_t *data = rdata_item_data(item);
	uint16_t address_family = dnslib_wire_read_u16(data);
	uint8_t prefix = data[2];
	uint8_t length = data[3];
	int negated = length & APL_NEGATION_MASK;
	int af = -1;

	char *ret = malloc(sizeof(char) * MAX_NSEC_BIT_STR_LEN);

	memset(ret, 0, MAX_NSEC_BIT_STR_LEN);

	length &= APL_LENGTH_MASK;
	switch (address_family) {
		case 1: af = AF_INET; break;
		case 2: af = AF_INET6; break;
	}

	if (af != -1) {
		char text_address[1000];
		uint8_t address[128];
		memset(address, 0, sizeof(address));
		memcpy(address, data + 4, length);
		if (inet_ntop(af, address,
			      text_address,
			      sizeof(text_address))) {
			snprintf(ret, sizeof(text_address) +
				 U32_MAX_STR_LEN * 2,
				 "%s%d:%s/%d",
				 negated ? "!" : "",
				 (int) address_family,
				 text_address,
				 (int) prefix);
		}
	}

	return ret;

		/*
647 648 649 650
	int result = 0;
	buffer_type packet;

	buffer_create_from(
651
		&packet, rdata_item_data(rdata), rdata_atom_size(rdata));
652 653 654 655 656 657 658 659 660 661 662

	if (buffer_available(&packet, 4)) {
		uint16_t address_family = buffer_read_u16(&packet);
		uint8_t prefix = buffer_read_u8(&packet);
		uint8_t length = buffer_read_u8(&packet);
		int negated = length & APL_NEGATION_MASK;
		int af = -1;

		length &= APL_LENGTH_MASK;
		switch (address_family) {
		case 1: af = AF_INET; break;
663
		*case 2: af = AF_INET6; break;
664 665 666 667 668 669
		}
		if (af != -1 && buffer_available(&packet, length)) {
			char text_address[1000];
			uint8_t address[128];
			memset(address, 0, sizeof(address));
			buffer_read(&packet, address, length);
670 671
			if (inet_ntop(af, address, text_address,
			    sizeof(text_address))) {
672 673 674 675 676 677 678 679 680 681
				buffer_printf(output, "%s%d:%s/%d",
					      negated ? "!" : "",
					      (int) address_family,
					      text_address,
					      (int) prefix);
				result = 1;
			}
		}
	}
	return result;
682
	*/
683 684


685 686
}

687
char *rdata_services_to_string(dnslib_rdata_item_t item)
688
{
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
	uint8_t *data = rdata_item_data(item);
	uint8_t protocol_number = data[0];
	ssize_t bitmap_size = rdata_item_size(item) - 1;
	uint8_t *bitmap = data + 1;
	struct protoent *proto = getprotobynumber(protocol_number);

	char *ret = malloc(sizeof(char) * MAX_NSEC_BIT_STR_LEN);

	memset(ret, 0, MAX_NSEC_BIT_STR_LEN);

	if (proto) {
		int i;

		strcpy(ret, proto->p_name);

		strcat(ret, " ");

		for (i = 0; i < bitmap_size * 8; ++i) {
			if (get_bit(bitmap, i)) {
				struct servent *service =
					getservbyport((int)htons(i),
						      proto->p_name);
				if (service) {
					strcat(ret, service->s_name);
					strcat(ret, " ");
				} else {
					char tmp[U32_MAX_STR_LEN];
					snprintf(tmp, U32_MAX_STR_LEN,
						 "%d ", i);
					strcat(ret, tmp);
				}
			}
		}
	}

	return ret;
Jan Kadlec's avatar
Jan Kadlec committed
725

Jan Kadlec's avatar
Jan Kadlec committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
	/*
	int result = 0;
	uint8_t protocol_number = buffer_read_u8(&packet);
	ssize_t bitmap_size = buffer_remaining(&packet);
	uint8_t *bitmap = buffer_current(&packet);
	struct protoent *proto = getprotobynumber(protocol_number);


	if (proto) {
		int i;

		strcpy(ret, proto->p_name);

		for (i = 0; i < bitmap_size * 8; ++i) {
			if (get_bit(bitmap, i)) {
				struct servent *service =
					getservbyport((int)htons(i),
						      proto->p_name);
				if (service) {
745 746
					buffer_printf(output, " %s",
						      service->s_name);
Jan Kadlec's avatar
Jan Kadlec committed
747 748
				} else {
					buffer_printf(output, " %d", i);
749 750 751
				}
			}
		}
Jan Kadlec's avatar
Jan Kadlec committed
752
		result = 1;
753
	}
Jan Kadlec's avatar
Jan Kadlec committed
754
	return ret;
755
	*/
756 757
}

758
char *rdata_ipsecgateway_to_string(dnslib_rdata_item_t item)
759
{
760
	return NULL;
761 762
	/*
	int gateway_type = rdata_item_data(rr->rdatas[1])[0];
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
	switch(gateway_type) {
	case IPSECKEY_NOGATEWAY:
		buffer_printf(output, ".");
		break;
	case IPSECKEY_IP4:
		rdata_a_to_string(output, rdata, rr);
		break;
	case IPSECKEY_IP6:
		rdata_aaaa_to_string(output, rdata, rr);
		break;
	case IPSECKEY_DNAME:
		rdata_dname_to_string(output, rdata, rr);
		break;
	default:
		return 0;
	}
779 780
	return 1;
	*/
781 782
}

783
char *rdata_nxt_to_string(dnslib_rdata_item_t item)
784
{
785 786 787 788 789 790 791
	size_t i;
	uint8_t *bitmap = rdata_item_data(item);
	size_t bitmap_size = rdata_item_size(item);

	char *ret = malloc(sizeof(char) * MAX_NSEC_BIT_STR_LEN);

	memset(ret, 0, MAX_NSEC_BIT_STR_LEN);
792 793 794

	for (i = 0; i < bitmap_size * 8; ++i) {
		if (get_bit(bitmap, i)) {
795 796
			strcat(ret, dnslib_rrtype_to_string(i));
				strcat(ret, " ");
797 798 799
		}
	}

800
	return ret;
801 802 803
}


804 805 806 807 808 809 810 811 812 813 814 815 816 817
char *rdata_nsec_to_string(dnslib_rdata_item_t item)
{
//	int insert_space = 0;

	char *ret = malloc(sizeof(char) * MAX_NSEC_BIT_STR_LEN);

	memset(ret, 0, MAX_NSEC_BIT_STR_LEN);

	uint8_t *data = rdata_item_data(item);

	int increment = 1;

	for (int i = 1; i < rdata_item_size(item); i += increment) {
		uint8_t window = data[i];
818 819 820
		/* TODO probably wrong set in parser, should
		 *be 0 in most of the cases.
		 */
821 822 823 824 825 826 827
		window = 0;
		uint8_t bitmap_size = data[i+1];
		uint8_t *bitmap =
			malloc(sizeof(uint8_t) * (bitmap_size >
			                          rdata_item_size(item) ?
						  bitmap_size :
						  rdata_item_size(item)));
828

829 830 831 832 833 834 835 836 837 838 839
		memset(bitmap, 0,
		       sizeof(uint8_t) *  bitmap_size > rdata_item_size(item) ?
		       bitmap_size :
		       rdata_item_size(item));

		memcpy(bitmap, data + i + 1, rdata_item_size(item) - (i + 1));

		increment += bitmap_size + 3;

		for (int j = 0; j < bitmap_size * 8; j++) {
			if (get_bit(bitmap, j)) {
840 841 842
				strcat(ret,
				       dnslib_rrtype_to_string(j +
							       window * 256));
843 844 845 846 847 848 849 850 851 852
				strcat(ret, " ");
			}
		}

		free(bitmap);
	}

	return ret;

/*	while (buffer_available(&packet, 2)) {
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
		uint8_t window = buffer_read_u8(&packet);
		uint8_t bitmap_size = buffer_read_u8(&packet);
		uint8_t *bitmap = buffer_current(&packet);
		int i;

		if (!buffer_available(&packet, bitmap_size)) {
			buffer_set_position(output, saved_position);
			return 0;
		}

		for (i = 0; i < bitmap_size * 8; ++i) {
			if (get_bit(bitmap, i)) {
				buffer_printf(output,
					      "%s%s",
					      insert_space ? " " : "",
					      rrtype_to_string(
						      window * 256 + i));
				insert_space = 1;
			}
		}
		buffer_skip(&packet, bitmap_size);
	}

876
	return 1; */
877 878
}

879
char *rdata_unknown_to_string(dnslib_rdata_item_t item)
880
{
881
 	uint16_t size = rdata_item_size(item);
882 883 884 885 886
	char *ret =
		malloc(sizeof(char) * (rdata_item_size(item) +
				       strlen("\\# ") + U16_MAX_STR_LEN));
	snprintf(ret, strlen("\\# ") + U16_MAX_STR_LEN, "%lu",
		 (unsigned long) size);
887 888
	strcat(ret, hex_to_string(rdata_item_data(item), size));
	return ret;
889 890
}

891
char *rdata_loc_to_string(dnslib_rdata_item_t item)
892
{
893
	return rdata_unknown_to_string(item);
894 895 896 897 898 899 900 901
}

typedef char * (*item_to_string_t)(dnslib_rdata_item_t);

static item_to_string_t item_to_string_table[DNSLIB_RDATA_ZF_UNKNOWN + 1] = {
	rdata_dname_to_string,
	rdata_dns_name_to_string,
	rdata_text_to_string,
902 903
	rdata_byte_to_string,
	rdata_short_to_string,
904
	rdata_long_to_string,
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
	rdata_a_to_string,
	rdata_aaaa_to_string,
	rdata_rrtype_to_string,
	rdata_algorithm_to_string,
	rdata_certificate_type_to_string,
	rdata_period_to_string,
	rdata_time_to_string,
	rdata_base64_to_string,
	rdata_base32_to_string,
	rdata_hex_to_string,
	rdata_hexlen_to_string,
	rdata_nsap_to_string,
	rdata_apl_to_string,
	rdata_ipsecgateway_to_string,
	rdata_services_to_string,
	rdata_nxt_to_string,
	rdata_nsec_to_string,
	rdata_loc_to_string,
923
	rdata_unknown_to_string
924 925
};

926 927
char *rdata_item_to_string(dnslib_rdata_zoneformat_t type,
                           dnslib_rdata_item_t item)
928 929 930 931
{
	return item_to_string_table[type](item);
}

932 933 934 935 936 937
/*void dnslib_zone_tree_apply_inorder(dnslib_zone_t *zone,
                              void (*function)(dnslib_node_t *node, void *data),
                              void *data); */

void rdata_dump_text(dnslib_rdata_t *rdata, uint16_t type, FILE *f)
{
938 939 940
	dnslib_rrtype_descriptor_t *desc =
		dnslib_rrtype_descriptor_by_type(type);
	char *item_str = NULL;
941
	for (int i = 0; i < desc->length; i++) {
942 943
		item_str = rdata_item_to_string(desc->zoneformat[i],
						rdata->items[i]);
944
		if (item_str == NULL) {
945 946 947
			item_str =
				rdata_item_to_string(DNSLIB_RDATA_ZF_UNKNOWN,
						     rdata->items[i]);
948 949 950 951 952 953 954
		}
		if (i != desc->length - 1) {
			fprintf(f, "%s ", item_str);
		} else {
			fprintf(f, "%s", item_str);
		}
		free(item_str);
955
	}
956
	fprintf(f, "\n");
957 958
}

959
void dump_rrset_header(dnslib_rrset_t *rrset, FILE *f)
960 961
{
	char *name = dnslib_dname_to_str(rrset->owner);
962
	fprintf(f, "%-20s ",  name);
963
	free(name);
964 965 966
	fprintf(f, "%-5u ", rrset->ttl);
	fprintf(f, "%-2s ", dnslib_rrclass_to_string(rrset->rclass));
	fprintf(f, "%-5s ",  dnslib_rrtype_to_string(rrset->type));
967 968
}

969
void rrsig_set_dump_text(dnslib_rrset_t *rrsig, FILE *f)
970
{
971
	dump_rrset_header(rrsig, f);
972 973 974
	dnslib_rdata_t *tmp = rrsig->rdata;

	while (tmp->next != rrsig->rdata) {
975
		rdata_dump_text(tmp, DNSLIB_RRTYPE_RRSIG, f);
976
		dump_rrset_header(rrsig, f);
977 978 979
		tmp = tmp->next;
	}

980
	rdata_dump_text(tmp, DNSLIB_RRTYPE_RRSIG, f);
981 982
}

983

984 985 986
void rrset_dump_text(dnslib_rrset_t *rrset, FILE *f)
{
	dump_rrset_header(rrset, f);
987
	dnslib_rdata_t *tmp = rrset->rdata;
988

989
	while (tmp->next != rrset->rdata) {
990
		rdata_dump_text(tmp, rrset->type, f);
991
		dump_rrset_header(rrset, f);
992 993 994
		tmp = tmp->next;
	}

995
	rdata_dump_text(tmp, rrset->type, f);
996
	dnslib_rrset_t *rrsig_set = rrset->rrsigs;
997 998 999
	if (rrsig_set != NULL) {
		rrsig_set_dump_text(rrsig_set, f);
	}
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
}

struct dump_param {
	FILE *f;
	dnslib_dname_t *origin;
};

void apex_node_dump_text(dnslib_node_t *node, FILE *f)
{
	int tmp = DNSLIB_RRTYPE_SOA;
	dnslib_rrset_t *tmp_rrset =
		(dnslib_rrset_t *)skip_find(node->rrsets,
		                            &tmp);
	rrset_dump_text(tmp_rrset, f);

	const skip_node_t *skip_node =
		skip_first(node->rrsets);

	tmp_rrset = (dnslib_rrset_t *)skip_node->value;

	if (tmp_rrset->type != DNSLIB_RRTYPE_SOA) {
		rrset_dump_text(tmp_rrset, f);
	}

	while ((skip_node = skip_next(skip_node)) != NULL) {
		tmp_rrset = (dnslib_rrset_t *)skip_node->value;
		if (tmp_rrset->type != DNSLIB_RRTYPE_SOA) {
			rrset_dump_text(tmp_rrset, f);
		}
	}
}

void node_dump_text(dnslib_node_t *node, void *data)
{
	struct dump_param *param;
	param = (struct dump_param *)data;
	FILE *f = param->f;
	dnslib_dname_t *origin = param->origin;

	/* pointers should do in this case */
	if (node->owner == origin) {
		apex_node_dump_text(node, f);
		return;
	}

	const skip_node_t *skip_node =
		skip_first(node->rrsets);

	/* empty nodes should not be dumped */
	if (skip_node == NULL) {
		return;
	}

	dnslib_rrset_t *tmp = (dnslib_rrset_t *)skip_node->value;

	rrset_dump_text(tmp, f);

	while ((skip_node = skip_next(skip_node)) != NULL) {
		tmp = (dnslib_rrset_t *)skip_node->value;
		rrset_dump_text(tmp, f);
	}
}

int zone_dump_text(dnslib_zone_t *zone, const char *filename)
{
	FILE *f = fopen(filename, "w");
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
	if (f == NULL) {
		//TODO log message
		return -1;
	}

	fprintf(f, ";Dumped using %s v. %d.%d.%d\n", PROJECT_NAME,
	        PROJECT_VER / 10000,
		(PROJECT_VER / 100) % 100,
		PROJECT_VER % 100);

1076 1077 1078 1079 1080
	struct dump_param param;
	param.f = f;
	param.origin = zone->apex->owner;
	dnslib_zone_tree_apply_inorder(zone, node_dump_text, &param);
	fclose(f);
1081 1082

	return 0;
1083
}