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 * The read line is available in the rr_buf (zero terminated), for
241 * $DIRECTIVE style elements.
242 * @param len: on input, the length of the rr buffer. on output the rr len.
243 * Buffer size of 64k should be enough.
244 * @param dname_len: returns the length of the dname initial part of the rr.
245 * @param parse_state: pass a pointer to user-allocated struct.
246 * Contents are maintained by this function.
247 * If you pass NULL then ORIGIN and TTL directives are not honored.
248 * You can start out with a particular origin by pre-filling it.
249 * otherwise, zero the structure before passing it.
250 * lineno is incremented when a newline is passed by the parser,
251 * you should initialize it at 1 at the start of the file.
252 * @return 0 on success, error on failure.
254 int sldns_fp2wire_rr_buf(FILE* in, uint8_t* rr, size_t* len, size_t* dname_len,
255 struct sldns_file_parse_state* parse_state);
258 * Convert one rdf in rdata to wireformat and parse from string.
259 * @param str: the text to convert for this rdata element.
260 * @param rd: rdata buffer for the wireformat.
261 * @param len: length of rd buffer on input, used length on output.
262 * @param rdftype: the type of the rdf.
263 * @return 0 on success, error on failure.
265 int sldns_str2wire_rdf_buf(const char* str, uint8_t* rd, size_t* len,
266 sldns_rdf_type rdftype);
269 * Convert rdf of type LDNS_RDF_TYPE_INT8 from string to wireformat.
270 * @param str: the text to convert for this rdata element.
271 * @param rd: rdata buffer for the wireformat.
272 * @param len: length of rd buffer on input, used length on output.
273 * @return 0 on success, error on failure.
275 int sldns_str2wire_int8_buf(const char* str, uint8_t* rd, size_t* len);
278 * Convert rdf of type LDNS_RDF_TYPE_INT16 from string to wireformat.
279 * @param str: the text to convert for this rdata element.
280 * @param rd: rdata buffer for the wireformat.
281 * @param len: length of rd buffer on input, used length on output.
282 * @return 0 on success, error on failure.
284 int sldns_str2wire_int16_buf(const char* str, uint8_t* rd, size_t* len);
287 * Convert rdf of type LDNS_RDF_TYPE_INT32 from string to wireformat.
288 * @param str: the text to convert for this rdata element.
289 * @param rd: rdata buffer for the wireformat.
290 * @param len: length of rd buffer on input, used length on output.
291 * @return 0 on success, error on failure.
293 int sldns_str2wire_int32_buf(const char* str, uint8_t* rd, size_t* len);
296 * Convert rdf of type LDNS_RDF_TYPE_A from string to wireformat.
297 * @param str: the text to convert for this rdata element.
298 * @param rd: rdata buffer for the wireformat.
299 * @param len: length of rd buffer on input, used length on output.
300 * @return 0 on success, error on failure.
302 int sldns_str2wire_a_buf(const char* str, uint8_t* rd, size_t* len);
305 * Convert rdf of type LDNS_RDF_TYPE_AAAA from string to wireformat.
306 * @param str: the text to convert for this rdata element.
307 * @param rd: rdata buffer for the wireformat.
308 * @param len: length of rd buffer on input, used length on output.
309 * @return 0 on success, error on failure.
311 int sldns_str2wire_aaaa_buf(const char* str, uint8_t* rd, size_t* len);
314 * Convert rdf of type LDNS_RDF_TYPE_STR from string to wireformat.
315 * @param str: the text to convert for this rdata element.
316 * @param rd: rdata buffer for the wireformat.
317 * @param len: length of rd buffer on input, used length on output.
318 * @return 0 on success, error on failure.
320 int sldns_str2wire_str_buf(const char* str, uint8_t* rd, size_t* len);
323 * Convert rdf of type LDNS_RDF_TYPE_APL from string to wireformat.
324 * @param str: the text to convert for this rdata element.
325 * @param rd: rdata buffer for the wireformat.
326 * @param len: length of rd buffer on input, used length on output.
327 * @return 0 on success, error on failure.
329 int sldns_str2wire_apl_buf(const char* str, uint8_t* rd, size_t* len);
332 * Convert rdf of type LDNS_RDF_TYPE_B64 from string to wireformat.
333 * @param str: the text to convert for this rdata element.
334 * @param rd: rdata buffer for the wireformat.
335 * @param len: length of rd buffer on input, used length on output.
336 * @return 0 on success, error on failure.
338 int sldns_str2wire_b64_buf(const char* str, uint8_t* rd, size_t* len);
341 * Convert rdf of type LDNS_RDF_TYPE_B32_EXT from string to wireformat.
342 * And also LDNS_RDF_TYPE_NSEC3_NEXT_OWNER.
343 * @param str: the text to convert for this rdata element.
344 * @param rd: rdata buffer for the wireformat.
345 * @param len: length of rd buffer on input, used length on output.
346 * @return 0 on success, error on failure.
348 int sldns_str2wire_b32_ext_buf(const char* str, uint8_t* rd, size_t* len);
351 * Convert rdf of type LDNS_RDF_TYPE_HEX from string to wireformat.
352 * @param str: the text to convert for this rdata element.
353 * @param rd: rdata buffer for the wireformat.
354 * @param len: length of rd buffer on input, used length on output.
355 * @return 0 on success, error on failure.
357 int sldns_str2wire_hex_buf(const char* str, uint8_t* rd, size_t* len);
360 * Convert rdf of type LDNS_RDF_TYPE_NSEC from string to wireformat.
361 * @param str: the text to convert for this rdata element.
362 * @param rd: rdata buffer for the wireformat.
363 * @param len: length of rd buffer on input, used length on output.
364 * @return 0 on success, error on failure.
366 int sldns_str2wire_nsec_buf(const char* str, uint8_t* rd, size_t* len);
369 * Convert rdf of type LDNS_RDF_TYPE_TYPE from string to wireformat.
370 * @param str: the text to convert for this rdata element.
371 * @param rd: rdata buffer for the wireformat.
372 * @param len: length of rd buffer on input, used length on output.
373 * @return 0 on success, error on failure.
375 int sldns_str2wire_type_buf(const char* str, uint8_t* rd, size_t* len);
378 * Convert rdf of type LDNS_RDF_TYPE_CLASS from string to wireformat.
379 * @param str: the text to convert for this rdata element.
380 * @param rd: rdata buffer for the wireformat.
381 * @param len: length of rd buffer on input, used length on output.
382 * @return 0 on success, error on failure.
384 int sldns_str2wire_class_buf(const char* str, uint8_t* rd, size_t* len);
387 * Convert rdf of type LDNS_RDF_TYPE_CERT_ALG from string to wireformat.
388 * @param str: the text to convert for this rdata element.
389 * @param rd: rdata buffer for the wireformat.
390 * @param len: length of rd buffer on input, used length on output.
391 * @return 0 on success, error on failure.
393 int sldns_str2wire_cert_alg_buf(const char* str, uint8_t* rd, size_t* len);
396 * Convert rdf of type LDNS_RDF_TYPE_ALG from string to wireformat.
397 * @param str: the text to convert for this rdata element.
398 * @param rd: rdata buffer for the wireformat.
399 * @param len: length of rd buffer on input, used length on output.
400 * @return 0 on success, error on failure.
402 int sldns_str2wire_alg_buf(const char* str, uint8_t* rd, size_t* len);
405 * Convert rdf of type LDNS_RDF_TYPE_TIME from string to wireformat.
406 * @param str: the text to convert for this rdata element.
407 * @param rd: rdata buffer for the wireformat.
408 * @param len: length of rd buffer on input, used length on output.
409 * @return 0 on success, error on failure.
411 int sldns_str2wire_time_buf(const char* str, uint8_t* rd, size_t* len);
414 * Convert rdf of type LDNS_RDF_TYPE_PERIOD from string to wireformat.
415 * @param str: the text to convert for this rdata element.
416 * @param rd: rdata buffer for the wireformat.
417 * @param len: length of rd buffer on input, used length on output.
418 * @return 0 on success, error on failure.
420 int sldns_str2wire_period_buf(const char* str, uint8_t* rd, size_t* len);
423 * Convert rdf of type LDNS_RDF_TYPE_TSIGTIME from string to wireformat.
424 * @param str: the text to convert for this rdata element.
425 * @param rd: rdata buffer for the wireformat.
426 * @param len: length of rd buffer on input, used length on output.
427 * @return 0 on success, error on failure.
429 int sldns_str2wire_tsigtime_buf(const char* str, uint8_t* rd, size_t* len);
432 * Convert rdf of type LDNS_RDF_TYPE_TSIGERROR from string to wireformat.
433 * @param str: the text to convert for this rdata element.
434 * @param rd: rdata buffer for the wireformat.
435 * @param len: length of rd buffer on input, used length on output.
436 * @return 0 on success, error on failure.
438 int sldns_str2wire_tsigerror_buf(const char* str, uint8_t* rd, size_t* len);
441 * Convert rdf of type LDNS_RDF_TYPE_LOC from string to wireformat.
442 * @param str: the text to convert for this rdata element.
443 * @param rd: rdata buffer for the wireformat.
444 * @param len: length of rd buffer on input, used length on output.
445 * @return 0 on success, error on failure.
447 int sldns_str2wire_loc_buf(const char* str, uint8_t* rd, size_t* len);
450 * Convert rdf of type LDNS_RDF_TYPE_WKS from string to wireformat.
451 * @param str: the text to convert for this rdata element.
452 * @param rd: rdata buffer for the wireformat.
453 * @param len: length of rd buffer on input, used length on output.
454 * @return 0 on success, error on failure.
456 int sldns_str2wire_wks_buf(const char* str, uint8_t* rd, size_t* len);
459 * Convert rdf of type LDNS_RDF_TYPE_NSAP from string to wireformat.
460 * @param str: the text to convert for this rdata element.
461 * @param rd: rdata buffer for the wireformat.
462 * @param len: length of rd buffer on input, used length on output.
463 * @return 0 on success, error on failure.
465 int sldns_str2wire_nsap_buf(const char* str, uint8_t* rd, size_t* len);
468 * Convert rdf of type LDNS_RDF_TYPE_ATMA from string to wireformat.
469 * @param str: the text to convert for this rdata element.
470 * @param rd: rdata buffer for the wireformat.
471 * @param len: length of rd buffer on input, used length on output.
472 * @return 0 on success, error on failure.
474 int sldns_str2wire_atma_buf(const char* str, uint8_t* rd, size_t* len);
477 * Convert rdf of type LDNS_RDF_TYPE_IPSECKEY from string to wireformat.
478 * @param str: the text to convert for this rdata element.
479 * @param rd: rdata buffer for the wireformat.
480 * @param len: length of rd buffer on input, used length on output.
481 * @return 0 on success, error on failure.
483 int sldns_str2wire_ipseckey_buf(const char* str, uint8_t* rd, size_t* len);
486 * Convert rdf of type LDNS_RDF_TYPE_NSEC3_SALT from string to wireformat.
487 * @param str: the text to convert for this rdata element.
488 * @param rd: rdata buffer for the wireformat.
489 * @param len: length of rd buffer on input, used length on output.
490 * @return 0 on success, error on failure.
492 int sldns_str2wire_nsec3_salt_buf(const char* str, uint8_t* rd, size_t* len);
495 * Convert rdf of type LDNS_RDF_TYPE_ILNP64 from string to wireformat.
496 * @param str: the text to convert for this rdata element.
497 * @param rd: rdata buffer for the wireformat.
498 * @param len: length of rd buffer on input, used length on output.
499 * @return 0 on success, error on failure.
501 int sldns_str2wire_ilnp64_buf(const char* str, uint8_t* rd, size_t* len);
504 * Convert rdf of type LDNS_RDF_TYPE_EUI48 from string to wireformat.
505 * @param str: the text to convert for this rdata element.
506 * @param rd: rdata buffer for the wireformat.
507 * @param len: length of rd buffer on input, used length on output.
508 * @return 0 on success, error on failure.
510 int sldns_str2wire_eui48_buf(const char* str, uint8_t* rd, size_t* len);
513 * Convert rdf of type LDNS_RDF_TYPE_EUI64 from string to wireformat.
514 * @param str: the text to convert for this rdata element.
515 * @param rd: rdata buffer for the wireformat.
516 * @param len: length of rd buffer on input, used length on output.
517 * @return 0 on success, error on failure.
519 int sldns_str2wire_eui64_buf(const char* str, uint8_t* rd, size_t* len);
522 * Convert rdf of type LDNS_RDF_TYPE_TAG from string to wireformat.
523 * @param str: the text to convert for this rdata element.
524 * @param rd: rdata buffer for the wireformat.
525 * @param len: length of rd buffer on input, used length on output.
526 * @return 0 on success, error on failure.
528 int sldns_str2wire_tag_buf(const char* str, uint8_t* rd, size_t* len);
531 * Convert rdf of type LDNS_RDF_TYPE_LONG_STR from string to wireformat.
532 * @param str: the text to convert for this rdata element.
533 * @param rd: rdata buffer for the wireformat.
534 * @param len: length of rd buffer on input, used length on output.
535 * @return 0 on success, error on failure.
537 int sldns_str2wire_long_str_buf(const char* str, uint8_t* rd, size_t* len);
540 * Convert rdf of type LDNS_RDF_TYPE_HIP from string to wireformat.
541 * @param str: the text to convert for this rdata element.
542 * @param rd: rdata buffer for the wireformat.
543 * @param len: length of rd buffer on input, used length on output.
544 * @return 0 on success, error on failure.
546 int sldns_str2wire_hip_buf(const char* str, uint8_t* rd, size_t* len);
549 * Convert rdf of type LDNS_RDF_TYPE_INT16_DATA from string to wireformat.
550 * @param str: the text to convert for this rdata element.
551 * @param rd: rdata buffer for the wireformat.
552 * @param len: length of rd buffer on input, used length on output.
553 * @return 0 on success, error on failure.
555 int sldns_str2wire_int16_data_buf(const char* str, uint8_t* rd, size_t* len);
558 * Strip whitespace from the start and the end of line.
559 * @param line: modified with 0 to shorten it.
560 * @return new start with spaces skipped.
562 char * sldns_strip_ws(char *line);
567 #endif /* LDNS_STR2WIRE_H */