summaryrefslogtreecommitdiff
path: root/samples/net/dns_client/src/dns_utils.c
blob: 7ebccdcaa224e3e639c0cfc7ab7b67a7261332f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
108
109
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
/*
 * Copyright (c) 2016 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dns_pack.h"

#include <stdio.h>
#include <errno.h>

int dns_print_msg_header(uint8_t *header, int size)
{
	if (size < DNS_MSG_HEADER_SIZE) {
		return -ENOMEM;
	}

	printf("\nHeader\n");
	printf("\tFlags\n");
	printf("\t\tTransaction ID:\t\t%d\n", dns_header_id(header));
	printf("\t\tMessage type:\t\t%d (%s)\n", dns_header_qr(header),
	       (dns_header_qr(header) == DNS_QUERY ? "query" : "response"));
	printf("\t\tOperation code:\t\t%d (%s)\n", dns_header_opcode(header),
	       (dns_header_opcode(header) == DNS_QUERY ? "query" : "other"));
	printf("\t\tAuthoritative:\t\t%d (%s)\n", dns_header_aa(header),
	       (dns_header_aa(header) ? "true" : "false"));
	printf("\t\tTruncated:\t\t%d (%s)\n", dns_header_tc(header),
	       (dns_header_tc(header) ? "true" : "false"));
	printf("\t\tRecursion desired:\t%d (%s)\n", dns_header_rd(header),
	       (dns_header_rd(header) ? "true" : "false"));
	printf("\t\tRecursion avaialable:\t%d (%s)\n", dns_header_ra(header),
	       (dns_header_ra(header) ? "true" : "false"));
	printf("\t\tZ:\t\t\t%d\n", dns_header_z(header));
	printf("\t\tResponse code:\t\t%d (%s)\n", dns_header_rcode(header),
	       (dns_header_rcode(header) == 0 ? "no error" : "error"));
	printf("\tQuestion counter:\t%d\n", dns_header_qdcount(header));
	printf("\tAnswer counter:\t\t%d\n", dns_header_ancount(header));
	printf("\tNServer counter:\t%d\n", dns_header_nscount(header));
	printf("\tAdditional counter:\t%d\n", dns_header_arcount(header));

	return 0;
}

int dns_print_label(uint8_t *label, int size)
{
	int n;
	int i;

	i = 0;
	while (i < size) {
		n = label[i];
		if (n == 0) {
			break;
		} else if (n > 63) {
			break;
		} else if (i + n <= size) {

			if (i) {
				printf(".");
			}
			int j = i + 1; /* next char			*/

			while (j < i + 1 + n) {
				printf("%c", label[j]);
				j++;
			}

			i += n + 1;	/* content + octect size	*/
		} else {
			i = 0;		/* no memory!			*/
			break;
		}
	}

	if (i == 0) {
		return -ENOMEM;
	}

	return 0;
}

int dns_print_msg_query(uint8_t *qname, int qname_size, int qtype, int qclass)
{
	printf("\nQuery\n");
	printf("\tQuery name\n\t\tLabel size:\t%d\n\t\tDomain name:\t",
	       qname_size);
	dns_print_label(qname, qname_size);
	printf("\n");
	printf("\tQuery type:\t\t%d\n", qtype);
	printf("\tQuery class:\t\t%d\n", qclass);

	return 0;
}

int dns_print_readable_msg_label(int offset, uint8_t *buf, int size)
{
	int next;
	int i;
	int j;

	for (i = offset; i < size;) {
		if (buf[i] <= 63) {
			/* +1 because the null label or a pointer */
			if (i + buf[i] + 1 >= size) {
				return -ENOMEM;
			}

			for (j = 1; j <= buf[i]; j++) {
				printf("%c", buf[i + j]);
			}
			i += buf[i] + 1;
			if (buf[i] == 0) {
				break;
			}
			printf(".");

		} else {
			if (i + 1 >= size) {
				return -ENOMEM;
			}

			next = ((buf[i] & 0x3F) << 8) + buf[i + 1];
			if (next >= size) {
				return -ENOMEM;
			}
			i = next;
			offset = next;
		}
	}
	printf("\n");

	return 0;
}

int print_buf(uint8_t *buf, size_t size)
{
	size_t i;

	for (i = 0; i < size; i++) {
		printf("%d ", buf[i]);
	}
	printf("\n");

	return 0;
}


int print_app_buf(struct app_buf_t *buf)
{
	print_buf(buf->buf, buf->length);
	return 0;
}