processing.c 4.47 KB
Newer Older
1
/*  Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 3 4 5 6 7 8 9 10 11 12 13

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
14
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
15 16
 */

17
#include <inttypes.h>
18
#include <stdlib.h>
19
#include <stdio.h>
20
#include <time.h>
21

22 23 24
#include "libzscanner/scanner.h"
#include "libzscanner/functions.c"
#include "libzscanner/processing.h"
25
#include "libknot/descriptor.c"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

const char *separator = "------\n";

static void print_wire_dname(const uint8_t *dname, uint32_t dname_length)
{
	uint32_t label_length = 0, i = 0;

	for (i = 0; i < dname_length; i++) {
		if (label_length == 0) {
			label_length = dname[i];
			printf("(%u)", label_length);
			continue;
		}
		printf("%c", (char)dname[i]);
		label_length--;
	}
}

44
void debug_process_error(zs_scanner_t *s)
45
{
46
	if (s->error.fatal) {
47 48
		printf("LINE(%03"PRIu64") ERROR(%s) FILE(%s) NEAR(%s)\n",
		       s->line_counter,
49
		       zs_strerror(s->error.code),
50
		       s->file.name,
51 52 53 54
		       s->buffer);
	} else {
		printf("LINE(%03"PRIu64") WARNING(%s) FILE(%s) NEAR(%s)\n",
		       s->line_counter,
55
		       zs_strerror(s->error.code),
56
		       s->file.name,
57 58 59 60 61
		       s->buffer);
	}
	fflush(stdout);
}

62
void debug_process_record(zs_scanner_t *s)
63
{
64
	uint32_t i;
65

66 67 68 69 70
	char rclass[32];
	char rtype[32];

	if (knot_rrclass_to_string(s->r_class, rclass, sizeof(rclass)) > 0 &&
	    knot_rrtype_to_string(s->r_type, rtype, sizeof(rtype)) > 0) {
71
		printf("LINE(%03"PRIu64") %s %6u %*s ",
72 73
		       s->line_counter, rclass, s->r_ttl, 5, rtype);
	} else {
74
		printf("LINE(%03"PRIu64") %u %6u %*u ",
75 76
		       s->line_counter, s->r_class, s->r_ttl, 5, s->r_type);
	}
77 78 79

	print_wire_dname(s->r_owner, s->r_owner_length);

80
	printf(" \\# %u ", s->r_data_length);
81

82 83
	for (i = 0; i < s->r_data_length; i++) {
		printf("%02X", (s->r_data)[i]);
84 85
	}
	printf("\n");
86 87 88 89 90 91 92
	fflush(stdout);
}

void debug_process_comment(zs_scanner_t *s)
{
	printf("LINE(%03"PRIu64") COMMENT(%.*s)\n", s->line_counter,
	       (int)s->buffer_length, s->buffer);
93 94 95
	fflush(stdout);
}

96
void test_process_error(zs_scanner_t *s)
97
{
98 99
	if (s->error.fatal) {
		printf("ERROR=%s\n%s", zs_errorname(s->error.code), separator);
100
	} else {
101
		printf("WARNG=%s\n%s", zs_errorname(s->error.code), separator);
102 103 104 105
	}
	fflush(stdout);
}

106
void test_process_record(zs_scanner_t *s)
107
{
108
	uint32_t i;
109 110 111

	printf("OWNER=");
	for (i = 0; i < s->r_owner_length; i++) {
112
		printf("%02X", s->r_owner[i]);
113 114 115 116 117 118
	}
	printf("\n");
	printf("CLASS=%04X\n", s->r_class);
	printf("RRTTL=%08X\n", s->r_ttl);
	printf("RTYPE=%04X\n", s->r_type);
	printf("RDATA=");
119 120
	for (i = 0; i < s->r_data_length; i++) {
		printf("%02X", (s->r_data)[i]);
121 122 123 124
	}
	printf("\n%s", separator);
	fflush(stdout);
}
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

int test_date_to_timestamp(void)
{
	time_t    ref_timestamp, max_timestamp;
	uint32_t  test_timestamp;
	uint8_t   buffer[16];
	uint64_t  val1, val2; // For time_t type unification.
	struct tm tm;

	// Set UTC for strftime.
	putenv("TZ=UTC");
	tzset();

	// Get maximal allowed timestamp.
	strptime("21051231235959", "%Y%m%d%H%M%S", &tm);
	max_timestamp = mktime(&tm);

	// Testing loop over whole input interval.
	for (ref_timestamp = 0;
	     ref_timestamp < max_timestamp;
	     ref_timestamp += 1) {
		struct tm result;
		// Get reference (correct) timestamp.
		strftime((char*)buffer, sizeof(buffer), "%Y%m%d%H%M%S",
			 gmtime_r(&ref_timestamp, &result));

		// Get testing timestamp.
		test_timestamp = 0U; // prevents Wunitialized
		date_to_timestamp(buffer, &test_timestamp);

		// Some continuous loging.
		if (ref_timestamp % 10000000 == 0) {
			val1 = ref_timestamp;
			printf("%s = %"PRIu64"\n", buffer, val1);
		}

		// Comparing results.
		if (ref_timestamp != test_timestamp) {
			val1 = ref_timestamp;

			if (ref_timestamp > test_timestamp) {
				val2 = ref_timestamp - test_timestamp;
				printf("%s = %"PRIu64", in - out = %"PRIu64"\n",
				       buffer, val1, val2);
			} else {
				val2 = test_timestamp - ref_timestamp;
				printf("%s = %"PRIu64", out - in = %"PRIu64"\n",
				       buffer, val1, val2);
			}

			return -1;
		}
	}

	return 0;
}