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
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
232
233
|
/*
* 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);
/* rdata from fqdn to qname */
static ssize_t record_rdata_qname(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_qname, /* NS */
&record_rdata_obsolete, /* MD */
&record_rdata_obsolete, /* MF */
&record_rdata_qname, /* 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_verbatim, /* TXT */
};
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_qname(char* _str, void** _rdata) {
size_t len;
size_t qlen;
if ( !_str || !_rdata )
return -1;
len = strlen(_str); /* Including \0 */
qlen = len + 1;
if (fqdn_check(_str, len) < 0) {
LOGPRINTF(_LOG_ERROR, "Not a valid FQDN");
return -1;
}
/*
* TODO Some funny business is going on around here
* Related to fqdn_to_qname()
*/
*_rdata = malloc(qlen+1);
if ( !*_rdata )
return -1;
/*strncpy(*_rdata, _str, len);*/
fqdn_to_qname(_str, len, *_rdata, qlen);
return (signed) qlen;
}
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);
if ( !str )
return -1;
strncpy(str, _str, len);
*_rdata = malloc(4);
if ( !*_rdata )
goto err;
tok = strtok(str, ".");
if( !tok )
goto err;
((uint8_t*)(*_rdata))[0] = (uint8_t) strtol(tok, &end, 10);
if( *end != '\0' )
goto err;
for( i=1; i<4; 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) {
uint16_t index;
if ( _rdtype > record_types_len )
return -1;
if ( !_rdata || !_str )
return -1;
index = _rdtype - 1;
return (*record_rdata_creator[index])(_str, _rdata);
}
|