aboutsummaryrefslogtreecommitdiff
path: root/src/record.c
blob: 7ba40a3525fa9fae577a4111d6e173ccaa65039e (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
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
/*
 * src/record.c
 * (c) 2021 Jonas Gunz <himself@jonasgunz.de>
 * License: MIT
 */

/* https://datatracker.ietf.org/doc/html/rfc1035#section-3.3 */

#include "record.h"

/*
 * Prototypes for rdata from string functions
 * Arguments:
 *	_str:	String representing rdata
 *	_rdata:	A buffer containing the raw rdata will be alloced here
 * Return: Length of alloced _rdata buffer, <0 on error
 */

/* Obsolete record types. Will throw error. */
static ssize_t record_rdata_obsolete(char* _str, void** _rdata);

/* Unimplemented record types. Will throw error. */
static ssize_t record_rdata_not_implemented(char* _str, void** _rdata);

/* rdata that does not need conversion from string form */
static ssize_t record_rdata_verbatim(char* _str, void** _rdata);

/* IPv4 Addresses */
static ssize_t record_rdata_a(char* _str, void** _rdata);

/* Start of authority */
static ssize_t record_rdata_soa(char* _str, void** _rdata);

static const char* const record_types[] = {
	"A",
	"NS",
	"MD",
	"MF",
	"CNAME",
	"SOA",
	"MB",
	"MG",
	"MR",
	"NULL",
	"WKS",
	"PTR",
	"HINFO",
	"MINFO",
	"MX",
	"TXT"
};
static const uint16_t record_types_len = sizeof(record_types) / sizeof(char*);

static ssize_t (*record_rdata_creator[])(char*, void**) = {
	&record_rdata_a, /* A */
	&record_rdata_verbatim, /* NS */
	&record_rdata_obsolete, /* MD */
	&record_rdata_obsolete, /* MF */
	&record_rdata_verbatim, /* CNAME */
	&record_rdata_soa, /* SOA */

	&record_rdata_not_implemented,
	&record_rdata_not_implemented,
	&record_rdata_not_implemented,
	&record_rdata_not_implemented,
	&record_rdata_not_implemented,
	&record_rdata_not_implemented,
	&record_rdata_not_implemented,
	&record_rdata_not_implemented,
	&record_rdata_not_implemented,
	&record_rdata_not_implemented,
};

static const char* const record_classes[] = {
	"IN",
	"CS",
	"CH",
	"HS"
};
static const uint16_t record_classes_len = sizeof(record_classes) / sizeof(char*);

/* Implementation of RDATA cobverters */

static ssize_t record_rdata_obsolete(char* _str, void** _rdata) {
	LOGPRINTF(_LOG_ERROR, "Record type not obsolete");
	return -1;
}

static ssize_t record_rdata_not_implemented(char* _str, void** _rdata) {
	LOGPRINTF(_LOG_ERROR, "Record type not implemented");
	return -1;
}

static ssize_t record_rdata_verbatim(char* _str, void** _rdata) {
	size_t len;

	if ( !_str || !_rdata )
		return -1;

	len = strlen(_str) + 1; /* Including \0 */

	*_rdata = malloc(len);

	if ( !*_rdata )
		return -1;

	strncpy(*_rdata, _str, len);

	return (signed) len;
}

static ssize_t record_rdata_a(char* _str, void** _rdata) {
	char* tok;
	char* str;
	char* end;
	size_t len;
	int i;

	if ( !_str || !_rdata )
		return -1;

	len = strlen(_str) + 1;
	str = malloc(len);
	strncpy(str, _str, len);

	*_rdata = malloc(4);
	if ( !*_rdata )
		return -1;

	tok = strtok(str, ".");
	if( !tok )
		goto err;

	((uint8_t*)(*_rdata))[3] = (uint8_t) strtol(tok, &end, 10);

	if( *end != '\0' )
		goto err;

	for( i=2; i>=0; i-- ) {
		tok = strtok(NULL, ".");

		if( !tok )
			goto err;

		((uint8_t*)(*_rdata))[i] = (uint8_t) strtol(tok, &end, 10);

		if( *end != '\0' )
			goto err;
	}

	free(str);
	return 4;
err:
	free(str);
	return -1;
}

static ssize_t record_rdata_soa(char* _str, void** _rdata) {
	LOGPRINTF(_LOG_ERROR, "Record type not implemented");
	return -1;
}


/* Other methods */

static uint16_t record_match_from_array(char* _str, const char* const _arr[], uint16_t _len) {
	uint16_t i;

	for( i=0; i<_len; i++ ){
		if ( strcasecmp( _str, _arr[i] ) == 0 )
			return i+1; /* Indices start with 1 */
	}

	return 0;
}

uint16_t record_class_from_str(char* _str) {
	return record_match_from_array(_str, record_classes, record_classes_len);
}

uint16_t record_type_from_str(char* _str) {
	return record_match_from_array(_str, record_types, record_types_len);
}

ssize_t record_rdata_from_str(void** _rdata, char *_str, uint16_t _rdtype) {
	if ( _rdtype >= record_types_len )
		return -1;

	if ( !_rdata || !_str )
		return -1;

	return (*record_rdata_creator)(_str, _rdata);
}