2 * str2wire.h - read txt presentation of RRs
4 * (c) NLnet Labs, 2005-2006
6 * See the file LICENSE for the license
12 * Parses text to wireformat.
15 #ifndef LDNS_STR2WIRE_H
16 #define LDNS_STR2WIRE_H
18 /* include rrdef for MAX_DOMAINLEN constant */
19 #include <sldns/rrdef.h>
24 struct sldns_struct_lookup_table;
26 /** buffer to read an RR, cannot be larger than 64K because of packet size */
27 #define LDNS_RR_BUF_SIZE 65535 /* bytes */
28 #define LDNS_DEFAULT_TTL 3600
31 * To convert class and type to string see
32 * sldns_get_rr_class_by_name(str)
33 * sldns_get_rr_type_by_name(str)
38 * Convert text string into dname wireformat, mallocless, with user buffer.
39 * @param str: the text string with the domain name.
40 * @param buf: the result buffer, suggested size LDNS_MAX_DOMAINLEN+1
41 * @param len: length of the buffer on input, length of the result on output.
42 * @return 0 on success, otherwise an error.
44 int sldns_str2wire_dname_buf(const char* str, uint8_t* buf, size_t* len);
47 * Same as sldns_str2wire_dname_buf, but concatenates origin if the domain
48 * name is relative (does not end in '.').
49 * @param str: the text string with the domain name.
50 * @param buf: the result buffer, suggested size LDNS_MAX_DOMAINLEN+1
51 * @param len: length of the buffer on input, length of the result on output.
52 * @param origin: the origin to append or NULL (nothing is appended).
53 * @param origin_len: length of origin.
54 * @return 0 on success, otherwise an error.
56 int sldns_str2wire_dname_buf_origin(const char* str, uint8_t* buf, size_t* len,
57 uint8_t* origin, size_t origin_len);
60 * Convert text string into dname wireformat
61 * @param str: the text string with the domain name.
62 * @param len: returned length of wireformat.
63 * @return wireformat dname (malloced) or NULL on failure.
65 uint8_t* sldns_str2wire_dname(const char* str, size_t* len);
68 * Convert text RR to wireformat, with user buffer.
69 * @param str: the RR data in text presentation format.
70 * @param rr: the buffer where the result is stored into. This buffer has
71 * the wire-dname(uncompressed), type, class, ttl, rdatalen, rdata.
72 * These values are probably not aligned, and in network format.
73 * Use the sldns_wirerr_get_xxx functions to access them safely.
74 * buffer size LDNS_RR_BUF_SIZE is suggested.
75 * @param len: on input the length of the buffer, on output the amount of
76 * the buffer used for the rr.
77 * @param dname_len: if non-NULL, filled with the dname length as result.
78 * Because after the dname you find the type, class, ttl, rdatalen, rdata.
79 * @param default_ttl: TTL used if no TTL available.
80 * @param origin: used for origin dname (if not NULL)
81 * @param origin_len: length of origin.
82 * @param prev: used for prev_rr dname (if not NULL)
83 * @param prev_len: length of prev.
84 * @return 0 on success, an error on failure.
86 int sldns_str2wire_rr_buf(const char* str, uint8_t* rr, size_t* len,
87 size_t* dname_len, uint32_t default_ttl, uint8_t* origin,
88 size_t origin_len, uint8_t* prev, size_t prev_len);
91 * Same as sldns_str2wire_rr_buf, but there is no rdata, it returns an RR
92 * with zero rdata and no ttl. It has name, type, class.
93 * You can access those with the sldns_wirerr_get_type and class functions.
94 * @param str: the RR data in text presentation format.
95 * @param rr: the buffer where the result is stored into.
96 * @param len: on input the length of the buffer, on output the amount of
97 * the buffer used for the rr.
98 * @param dname_len: if non-NULL, filled with the dname length as result.
99 * Because after the dname you find the type, class, ttl, rdatalen, rdata.
100 * @param origin: used for origin dname (if not NULL)
101 * @param origin_len: length of origin.
102 * @param prev: used for prev_rr dname (if not NULL)
103 * @param prev_len: length of prev.
104 * @return 0 on success, an error on failure.
106 int sldns_str2wire_rr_question_buf(const char* str, uint8_t* rr, size_t* len,
107 size_t* dname_len, uint8_t* origin, size_t origin_len, uint8_t* prev,
111 * Get the type of the RR.
112 * @param rr: the RR in wire format.
113 * @param len: rr length.
114 * @param dname_len: dname length to skip.
115 * @return type in host byteorder
117 uint16_t sldns_wirerr_get_type(uint8_t* rr, size_t len, size_t dname_len);
120 * Get the class of the RR.
121 * @param rr: the RR in wire format.
122 * @param len: rr length.
123 * @param dname_len: dname length to skip.
124 * @return class in host byteorder
126 uint16_t sldns_wirerr_get_class(uint8_t* rr, size_t len, size_t dname_len);
129 * Get the ttl of the RR.
130 * @param rr: the RR in wire format.
131 * @param len: rr length.
132 * @param dname_len: dname length to skip.
133 * @return ttl in host byteorder
135 uint32_t sldns_wirerr_get_ttl(uint8_t* rr, size_t len, size_t dname_len);
138 * Get the rdata length of the RR.
139 * @param rr: the RR in wire format.
140 * @param len: rr length.
141 * @param dname_len: dname length to skip.
142 * @return rdata length in host byteorder
143 * If the rdata length is larger than the rr-len allows, it is truncated.
144 * So, that it is safe to read the data length returned
145 * from this function from the rdata pointer of sldns_wirerr_get_rdata.
147 uint16_t sldns_wirerr_get_rdatalen(uint8_t* rr, size_t len, size_t dname_len);
150 * Get the rdata pointer of the RR.
151 * @param rr: the RR in wire format.
152 * @param len: rr length.
153 * @param dname_len: dname length to skip.
154 * @return rdata pointer
156 uint8_t* sldns_wirerr_get_rdata(uint8_t* rr, size_t len, size_t dname_len);
159 * Get the rdata pointer of the RR. prefixed with rdata length.
160 * @param rr: the RR in wire format.
161 * @param len: rr length.
162 * @param dname_len: dname length to skip.
163 * @return pointer to rdatalength, followed by the rdata.
165 uint8_t* sldns_wirerr_get_rdatawl(uint8_t* rr, size_t len, size_t dname_len);
170 #define LDNS_WIREPARSE_MASK 0x0fff
171 #define LDNS_WIREPARSE_SHIFT 12
172 #define LDNS_WIREPARSE_ERROR(e) ((e)&LDNS_WIREPARSE_MASK)
173 #define LDNS_WIREPARSE_OFFSET(e) (((e)&~LDNS_WIREPARSE_MASK)>>LDNS_WIREPARSE_SHIFT)
174 /* use lookuptable to get error string, sldns_wireparse_errors */
175 #define LDNS_WIREPARSE_ERR_OK 0
176 #define LDNS_WIREPARSE_ERR_GENERAL 342
177 #define LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW 343
178 #define LDNS_WIREPARSE_ERR_DOMAINNAME_UNDERFLOW 344
179 #define LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL 345
180 #define LDNS_WIREPARSE_ERR_LABEL_OVERFLOW 346
181 #define LDNS_WIREPARSE_ERR_EMPTY_LABEL 347
182 #define LDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE 348
183 #define LDNS_WIREPARSE_ERR_SYNTAX 349
184 #define LDNS_WIREPARSE_ERR_SYNTAX_TTL 350
185 #define LDNS_WIREPARSE_ERR_SYNTAX_TYPE 351
186 #define LDNS_WIREPARSE_ERR_SYNTAX_CLASS 352
187 #define LDNS_WIREPARSE_ERR_SYNTAX_RDATA 353
188 #define LDNS_WIREPARSE_ERR_SYNTAX_MISSING_VALUE 354
189 #define LDNS_WIREPARSE_ERR_INVALID_STR 355
190 #define LDNS_WIREPARSE_ERR_SYNTAX_B64 356
191 #define LDNS_WIREPARSE_ERR_SYNTAX_B32_EXT 357
192 #define LDNS_WIREPARSE_ERR_SYNTAX_HEX 358
193 #define LDNS_WIREPARSE_ERR_CERT_BAD_ALGORITHM 359
194 #define LDNS_WIREPARSE_ERR_SYNTAX_TIME 360
195 #define LDNS_WIREPARSE_ERR_SYNTAX_PERIOD 361
196 #define LDNS_WIREPARSE_ERR_SYNTAX_ILNP64 362
197 #define LDNS_WIREPARSE_ERR_SYNTAX_EUI48 363
198 #define LDNS_WIREPARSE_ERR_SYNTAX_EUI64 364
199 #define LDNS_WIREPARSE_ERR_SYNTAX_TAG 365
200 #define LDNS_WIREPARSE_ERR_NOT_IMPL 366
201 #define LDNS_WIREPARSE_ERR_SYNTAX_INT 367
202 #define LDNS_WIREPARSE_ERR_SYNTAX_IP4 368
203 #define LDNS_WIREPARSE_ERR_SYNTAX_IP6 369
204 #define LDNS_WIREPARSE_ERR_SYNTAX_INTEGER_OVERFLOW 370
205 #define LDNS_WIREPARSE_ERR_INCLUDE 371
206 #define LDNS_WIREPARSE_ERR_PARENTHESIS 372
209 * Get reference to a constant string for the (parse) error.
210 * @param e: error return value
213 const char* sldns_get_errorstr_parse(int e);
216 * wire parse state for parsing files
218 struct sldns_file_parse_state {
219 /** the origin domain name, if len!=0. uncompressed wireformat */
220 uint8_t origin[LDNS_MAX_DOMAINLEN+1];
221 /** length of origin domain name, in bytes. 0 if not set. */
223 /** the previous domain name, if len!=0. uncompressed wireformat*/
224 uint8_t prev_rr[LDNS_MAX_DOMAINLEN+1];
225 /** length of the previous domain name, in bytes. 0 if not set. */
227 /** default TTL, this is used if the text does not specify a TTL,
229 uint32_t default_ttl;
230 /** line number information */
235 * Read one RR from zonefile with buffer for the data.
236 * @param in: file that is read from (one RR, multiple lines if it spans them).
237 * @param rr: this is malloced by the user and the result is stored here,
238 * if an RR is read. If no RR is read this is signalled with the
239 * return len set to 0 (for ORIGIN, TTL directives).
240 * @param len: on input, the length of the rr buffer. on output the rr len.
241 * Buffer size of 64k should be enough.
242 * @param dname_len: returns the length of the dname initial part of the rr.
243 * @param parse_state: pass a pointer to user-allocated struct.
244 * Contents are maintained by this function.
245 * If you pass NULL then ORIGIN and TTL directives are not honored.
246 * You can start out with a particular origin by pre-filling it.
247 * otherwise, zero the structure before passing it.
248 * lineno is incremented when a newline is passed by the parser,
249 * you should initialize it at 1 at the start of the file.
250 * @return 0 on success, error on failure.
252 int sldns_fp2wire_rr_buf(FILE* in, uint8_t* rr, size_t* len, size_t* dname_len,
253 struct sldns_file_parse_state* parse_state);
256 * Convert one rdf in rdata to wireformat and parse from string.
257 * @param str: the text to convert for this rdata element.
258 * @param rd: rdata buffer for the wireformat.
259 * @param len: length of rd buffer on input, used length on output.
260 * @param rdftype: the type of the rdf.
261 * @return 0 on success, error on failure.
263 int sldns_str2wire_rdf_buf(const char* str, uint8_t* rd, size_t* len,
264 sldns_rdf_type rdftype);
267 * Convert rdf of type LDNS_RDF_TYPE_INT8 from string to wireformat.
268 * @param str: the text to convert for this rdata element.
269 * @param rd: rdata buffer for the wireformat.
270 * @param len: length of rd buffer on input, used length on output.
271 * @return 0 on success, error on failure.
273 int sldns_str2wire_int8_buf(const char* str, uint8_t* rd, size_t* len);
276 * Convert rdf of type LDNS_RDF_TYPE_INT16 from string to wireformat.
277 * @param str: the text to convert for this rdata element.
278 * @param rd: rdata buffer for the wireformat.
279 * @param len: length of rd buffer on input, used length on output.
280 * @return 0 on success, error on failure.
282 int sldns_str2wire_int16_buf(const char* str, uint8_t* rd, size_t* len);
285 * Convert rdf of type LDNS_RDF_TYPE_INT32 from string to wireformat.
286 * @param str: the text to convert for this rdata element.
287 * @param rd: rdata buffer for the wireformat.
288 * @param len: length of rd buffer on input, used length on output.
289 * @return 0 on success, error on failure.
291 int sldns_str2wire_int32_buf(const char* str, uint8_t* rd, size_t* len);
294 * Convert rdf of type LDNS_RDF_TYPE_A from string to wireformat.
295 * @param str: the text to convert for this rdata element.
296 * @param rd: rdata buffer for the wireformat.
297 * @param len: length of rd buffer on input, used length on output.
298 * @return 0 on success, error on failure.
300 int sldns_str2wire_a_buf(const char* str, uint8_t* rd, size_t* len);
303 * Convert rdf of type LDNS_RDF_TYPE_AAAA from string to wireformat.
304 * @param str: the text to convert for this rdata element.
305 * @param rd: rdata buffer for the wireformat.
306 * @param len: length of rd buffer on input, used length on output.
307 * @return 0 on success, error on failure.
309 int sldns_str2wire_aaaa_buf(const char* str, uint8_t* rd, size_t* len);
312 * Convert rdf of type LDNS_RDF_TYPE_STR from string to wireformat.
313 * @param str: the text to convert for this rdata element.
314 * @param rd: rdata buffer for the wireformat.
315 * @param len: length of rd buffer on input, used length on output.
316 * @return 0 on success, error on failure.
318 int sldns_str2wire_str_buf(const char* str, uint8_t* rd, size_t* len);
321 * Convert rdf of type LDNS_RDF_TYPE_APL from string to wireformat.
322 * @param str: the text to convert for this rdata element.
323 * @param rd: rdata buffer for the wireformat.
324 * @param len: length of rd buffer on input, used length on output.
325 * @return 0 on success, error on failure.
327 int sldns_str2wire_apl_buf(const char* str, uint8_t* rd, size_t* len);
330 * Convert rdf of type LDNS_RDF_TYPE_B64 from string to wireformat.
331 * @param str: the text to convert for this rdata element.
332 * @param rd: rdata buffer for the wireformat.
333 * @param len: length of rd buffer on input, used length on output.
334 * @return 0 on success, error on failure.
336 int sldns_str2wire_b64_buf(const char* str, uint8_t* rd, size_t* len);
339 * Convert rdf of type LDNS_RDF_TYPE_B32_EXT from string to wireformat.
340 * And also LDNS_RDF_TYPE_NSEC3_NEXT_OWNER.
341 * @param str: the text to convert for this rdata element.
342 * @param rd: rdata buffer for the wireformat.
343 * @param len: length of rd buffer on input, used length on output.
344 * @return 0 on success, error on failure.
346 int sldns_str2wire_b32_ext_buf(const char* str, uint8_t* rd, size_t* len);
349 * Convert rdf of type LDNS_RDF_TYPE_HEX from string to wireformat.
350 * @param str: the text to convert for this rdata element.
351 * @param rd: rdata buffer for the wireformat.
352 * @param len: length of rd buffer on input, used length on output.
353 * @return 0 on success, error on failure.
355 int sldns_str2wire_hex_buf(const char* str, uint8_t* rd, size_t* len);
358 * Convert rdf of type LDNS_RDF_TYPE_NSEC from string to wireformat.
359 * @param str: the text to convert for this rdata element.
360 * @param rd: rdata buffer for the wireformat.
361 * @param len: length of rd buffer on input, used length on output.
362 * @return 0 on success, error on failure.
364 int sldns_str2wire_nsec_buf(const char* str, uint8_t* rd, size_t* len);
367 * Convert rdf of type LDNS_RDF_TYPE_TYPE from string to wireformat.
368 * @param str: the text to convert for this rdata element.
369 * @param rd: rdata buffer for the wireformat.
370 * @param len: length of rd buffer on input, used length on output.
371 * @return 0 on success, error on failure.
373 int sldns_str2wire_type_buf(const char* str, uint8_t* rd, size_t* len);
376 * Convert rdf of type LDNS_RDF_TYPE_CLASS from string to wireformat.
377 * @param str: the text to convert for this rdata element.
378 * @param rd: rdata buffer for the wireformat.
379 * @param len: length of rd buffer on input, used length on output.
380 * @return 0 on success, error on failure.
382 int sldns_str2wire_class_buf(const char* str, uint8_t* rd, size_t* len);
385 * Convert rdf of type LDNS_RDF_TYPE_CERT_ALG from string to wireformat.
386 * @param str: the text to convert for this rdata element.
387 * @param rd: rdata buffer for the wireformat.
388 * @param len: length of rd buffer on input, used length on output.
389 * @return 0 on success, error on failure.
391 int sldns_str2wire_cert_alg_buf(const char* str, uint8_t* rd, size_t* len);
394 * Convert rdf of type LDNS_RDF_TYPE_ALG from string to wireformat.
395 * @param str: the text to convert for this rdata element.
396 * @param rd: rdata buffer for the wireformat.
397 * @param len: length of rd buffer on input, used length on output.
398 * @return 0 on success, error on failure.
400 int sldns_str2wire_alg_buf(const char* str, uint8_t* rd, size_t* len);
403 * Convert rdf of type LDNS_RDF_TYPE_TIME from string to wireformat.
404 * @param str: the text to convert for this rdata element.
405 * @param rd: rdata buffer for the wireformat.
406 * @param len: length of rd buffer on input, used length on output.
407 * @return 0 on success, error on failure.
409 int sldns_str2wire_time_buf(const char* str, uint8_t* rd, size_t* len);
412 * Convert rdf of type LDNS_RDF_TYPE_PERIOD from string to wireformat.
413 * @param str: the text to convert for this rdata element.
414 * @param rd: rdata buffer for the wireformat.
415 * @param len: length of rd buffer on input, used length on output.
416 * @return 0 on success, error on failure.
418 int sldns_str2wire_period_buf(const char* str, uint8_t* rd, size_t* len);
421 * Convert rdf of type LDNS_RDF_TYPE_LOC from string to wireformat.
422 * @param str: the text to convert for this rdata element.
423 * @param rd: rdata buffer for the wireformat.
424 * @param len: length of rd buffer on input, used length on output.
425 * @return 0 on success, error on failure.
427 int sldns_str2wire_loc_buf(const char* str, uint8_t* rd, size_t* len);
430 * Convert rdf of type LDNS_RDF_TYPE_WKS from string to wireformat.
431 * @param str: the text to convert for this rdata element.
432 * @param rd: rdata buffer for the wireformat.
433 * @param len: length of rd buffer on input, used length on output.
434 * @return 0 on success, error on failure.
436 int sldns_str2wire_wks_buf(const char* str, uint8_t* rd, size_t* len);
439 * Convert rdf of type LDNS_RDF_TYPE_NSAP from string to wireformat.
440 * @param str: the text to convert for this rdata element.
441 * @param rd: rdata buffer for the wireformat.
442 * @param len: length of rd buffer on input, used length on output.
443 * @return 0 on success, error on failure.
445 int sldns_str2wire_nsap_buf(const char* str, uint8_t* rd, size_t* len);
448 * Convert rdf of type LDNS_RDF_TYPE_ATMA from string to wireformat.
449 * @param str: the text to convert for this rdata element.
450 * @param rd: rdata buffer for the wireformat.
451 * @param len: length of rd buffer on input, used length on output.
452 * @return 0 on success, error on failure.
454 int sldns_str2wire_atma_buf(const char* str, uint8_t* rd, size_t* len);
457 * Convert rdf of type LDNS_RDF_TYPE_IPSECKEY from string to wireformat.
458 * @param str: the text to convert for this rdata element.
459 * @param rd: rdata buffer for the wireformat.
460 * @param len: length of rd buffer on input, used length on output.
461 * @return 0 on success, error on failure.
463 int sldns_str2wire_ipseckey_buf(const char* str, uint8_t* rd, size_t* len);
466 * Convert rdf of type LDNS_RDF_TYPE_NSEC3_SALT from string to wireformat.
467 * @param str: the text to convert for this rdata element.
468 * @param rd: rdata buffer for the wireformat.
469 * @param len: length of rd buffer on input, used length on output.
470 * @return 0 on success, error on failure.
472 int sldns_str2wire_nsec3_salt_buf(const char* str, uint8_t* rd, size_t* len);
475 * Convert rdf of type LDNS_RDF_TYPE_ILNP64 from string to wireformat.
476 * @param str: the text to convert for this rdata element.
477 * @param rd: rdata buffer for the wireformat.
478 * @param len: length of rd buffer on input, used length on output.
479 * @return 0 on success, error on failure.
481 int sldns_str2wire_ilnp64_buf(const char* str, uint8_t* rd, size_t* len);
484 * Convert rdf of type LDNS_RDF_TYPE_EUI48 from string to wireformat.
485 * @param str: the text to convert for this rdata element.
486 * @param rd: rdata buffer for the wireformat.
487 * @param len: length of rd buffer on input, used length on output.
488 * @return 0 on success, error on failure.
490 int sldns_str2wire_eui48_buf(const char* str, uint8_t* rd, size_t* len);
493 * Convert rdf of type LDNS_RDF_TYPE_EUI64 from string to wireformat.
494 * @param str: the text to convert for this rdata element.
495 * @param rd: rdata buffer for the wireformat.
496 * @param len: length of rd buffer on input, used length on output.
497 * @return 0 on success, error on failure.
499 int sldns_str2wire_eui64_buf(const char* str, uint8_t* rd, size_t* len);
502 * Convert rdf of type LDNS_RDF_TYPE_TAG from string to wireformat.
503 * @param str: the text to convert for this rdata element.
504 * @param rd: rdata buffer for the wireformat.
505 * @param len: length of rd buffer on input, used length on output.
506 * @return 0 on success, error on failure.
508 int sldns_str2wire_tag_buf(const char* str, uint8_t* rd, size_t* len);
511 * Convert rdf of type LDNS_RDF_TYPE_LONG_STR from string to wireformat.
512 * @param str: the text to convert for this rdata element.
513 * @param rd: rdata buffer for the wireformat.
514 * @param len: length of rd buffer on input, used length on output.
515 * @return 0 on success, error on failure.
517 int sldns_str2wire_long_str_buf(const char* str, uint8_t* rd, size_t* len);
520 * Convert rdf of type LDNS_RDF_TYPE_HIP from string to wireformat.
521 * @param str: the text to convert for this rdata element.
522 * @param rd: rdata buffer for the wireformat.
523 * @param len: length of rd buffer on input, used length on output.
524 * @return 0 on success, error on failure.
526 int sldns_str2wire_hip_buf(const char* str, uint8_t* rd, size_t* len);
529 * Convert rdf of type LDNS_RDF_TYPE_INT16_DATA from string to wireformat.
530 * @param str: the text to convert for this rdata element.
531 * @param rd: rdata buffer for the wireformat.
532 * @param len: length of rd buffer on input, used length on output.
533 * @return 0 on success, error on failure.
535 int sldns_str2wire_int16_data_buf(const char* str, uint8_t* rd, size_t* len);
541 #endif /* LDNS_STR2WIRE_H */