2 This file is part of GNUnet
3 (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file include/gnunet_dnsparser_lib.h
23 * @brief API for helper library to parse DNS packets.
24 * @author Philipp Toelke
25 * @author Christian Grothoff
27 #ifndef GNUNET_DNSPARSER_LIB_H
28 #define GNUNET_DNSPARSER_LIB_H
31 #include "gnunet_common.h"
34 * A few common DNS types.
36 #define GNUNET_DNSPARSER_TYPE_A 1
37 #define GNUNET_DNSPARSER_TYPE_NS 2
38 #define GNUNET_DNSPARSER_TYPE_CNAME 5
39 #define GNUNET_DNSPARSER_TYPE_SOA 6
40 #define GNUNET_DNSPARSER_TYPE_PTR 12
41 #define GNUNET_DNSPARSER_TYPE_MX 15
42 #define GNUNET_DNSPARSER_TYPE_TXT 16
43 #define GNUNET_DNSPARSER_TYPE_AAAA 28
44 #define GNUNET_DNSPARSER_TYPE_SRV 33
45 #define GNUNET_DNSPARSER_TYPE_TLSA 52
48 * A few common DNS classes (ok, only one is common, but I list a
49 * couple more to make it clear what we're talking about here).
51 #define GNUNET_DNSPARSER_CLASS_INTERNET 1
52 #define GNUNET_DNSPARSER_CLASS_CHAOS 3
53 #define GNUNET_DNSPARSER_CLASS_HESIOD 4
55 #define GNUNET_DNSPARSER_OPCODE_QUERY 0
56 #define GNUNET_DNSPARSER_OPCODE_INVERSE_QUERY 1
57 #define GNUNET_DNSPARSER_OPCODE_STATUS 2
62 #define GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR 0
63 #define GNUNET_DNSPARSER_RETURN_CODE_FORMAT_ERROR 1
64 #define GNUNET_DNSPARSER_RETURN_CODE_SERVER_FAILURE 2
65 #define GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR 3
66 #define GNUNET_DNSPARSER_RETURN_CODE_NOT_IMPLEMENTED 4
67 #define GNUNET_DNSPARSER_RETURN_CODE_REFUSED 5
72 #define GNUNET_DNSPARSER_RETURN_CODE_YXDOMAIN 6
73 #define GNUNET_DNSPARSER_RETURN_CODE_YXRRSET 7
74 #define GNUNET_DNSPARSER_RETURN_CODE_NXRRSET 8
75 #define GNUNET_DNSPARSER_RETURN_CODE_NOT_AUTH 9
76 #define GNUNET_DNSPARSER_RETURN_CODE_NOT_ZONE 10
79 * DNS flags (largely RFC 1035 / RFC 2136).
81 struct GNUNET_DNSPARSER_Flags
83 #if __BYTE_ORDER == __LITTLE_ENDIAN
85 * Set to 1 if recursion is desired (client -> server)
87 unsigned int recursion_desired : 1 GNUNET_PACKED;
90 * Set to 1 if message is truncated
92 unsigned int message_truncated : 1 GNUNET_PACKED;
95 * Set to 1 if this is an authoritative answer
97 unsigned int authoritative_answer : 1 GNUNET_PACKED;
100 * See GNUNET_DNSPARSER_OPCODE_ defines.
102 unsigned int opcode : 4 GNUNET_PACKED;
105 * query:0, response:1
107 unsigned int query_or_response : 1 GNUNET_PACKED;
110 * See GNUNET_DNSPARSER_RETURN_CODE_ defines.
112 unsigned int return_code : 4 GNUNET_PACKED;
117 unsigned int checking_disabled : 1 GNUNET_PACKED;
120 * Response has been cryptographically verified, RFC 4035.
122 unsigned int authenticated_data : 1 GNUNET_PACKED;
127 unsigned int zero : 1 GNUNET_PACKED;
130 * Set to 1 if recursion is available (server -> client)
132 unsigned int recursion_available : 1 GNUNET_PACKED;
133 #elif __BYTE_ORDER == __BIG_ENDIAN
136 * query:0, response:1
138 unsigned int query_or_response : 1 GNUNET_PACKED;
141 * See GNUNET_DNSPARSER_OPCODE_ defines.
143 unsigned int opcode : 4 GNUNET_PACKED;
146 * Set to 1 if this is an authoritative answer
148 unsigned int authoritative_answer : 1 GNUNET_PACKED;
151 * Set to 1 if message is truncated
153 unsigned int message_truncated : 1 GNUNET_PACKED;
156 * Set to 1 if recursion is desired (client -> server)
158 unsigned int recursion_desired : 1 GNUNET_PACKED;
162 * Set to 1 if recursion is available (server -> client)
164 unsigned int recursion_available : 1 GNUNET_PACKED;
169 unsigned int zero : 1 GNUNET_PACKED;
172 * Response has been cryptographically verified, RFC 4035.
174 unsigned int authenticated_data : 1 GNUNET_PACKED;
179 unsigned int checking_disabled : 1 GNUNET_PACKED;
182 * See GNUNET_DNSPARSER_RETURN_CODE_ defines.
184 unsigned int return_code : 4 GNUNET_PACKED;
186 #error byteorder undefined
189 } GNUNET_GCC_STRUCT_LAYOUT;
195 struct GNUNET_DNSPARSER_Query
199 * Name of the record that the query is for (0-terminated).
200 * In UTF-8 format. The library will convert from and to DNS-IDNA
201 * as necessary. Use 'GNUNET_DNSPARSER_check_label' to test if an
202 * individual label is well-formed. If a given name is not well-formed,
203 * creating the DNS packet will fail.
208 * See GNUNET_DNSPARSER_TYPE_*.
213 * See GNUNET_DNSPARSER_CLASS_*.
221 * Information from MX records (RFC 1035).
223 struct GNUNET_DNSPARSER_MxRecord
227 * Preference for this entry (lower value is higher preference).
232 * Name of the mail server.
233 * In UTF-8 format. The library will convert from and to DNS-IDNA
234 * as necessary. Use 'GNUNET_DNSPARSER_check_label' to test if an
235 * individual label is well-formed. If a given name is not well-formed,
236 * creating the DNS packet will fail.
244 * Information from SRV records (RFC 2782). The 'service', 'proto'
245 * and 'domain_name' fields together give the DNS-name which for SRV
246 * records is of the form "_$SERVICE._$PROTO.$DOMAIN_NAME". The DNS
247 * parser provides the full name in 'struct DNSPARSER_Record' and the
248 * individual components in the respective fields of this struct.
249 * When serializing, you CAN set the 'name' field of 'struct
250 * GNUNET_DNSPARSER_Record' to NULL, in which case the DNSPARSER code
251 * will populate 'name' from the 'service', 'proto' and 'domain_name'
252 * fields in this struct.
254 struct GNUNET_DNSPARSER_SrvRecord
258 * Service name without the underscore (!). Note that RFC 6335 clarifies the
259 * set of legal characters for service names.
260 * In UTF-8 format. The library will convert from and to DNS-IDNA
261 * as necessary. Use 'GNUNET_DNSPARSER_check_label' to test if an
262 * individual label is well-formed. If a given name is not well-formed,
263 * creating the DNS packet will fail.
268 * Transport protocol (typcially "tcp" or "udp", but others might be allowed).
269 * Without the underscore (!).
274 * Domain name for which the record is valid
275 * In UTF-8 format. The library will convert from and to DNS-IDNA
276 * as necessary. Use 'GNUNET_DNSPARSER_check_label' to test if an
277 * individual label is well-formed. If a given name is not well-formed,
278 * creating the DNS packet will fail.
283 * Hostname offering the service.
284 * In UTF-8 format. The library will convert from and to DNS-IDNA
285 * as necessary. Use 'GNUNET_DNSPARSER_check_label' to test if an
286 * individual label is well-formed. If a given name is not well-formed,
287 * creating the DNS packet will fail.
292 * Preference for this entry (lower value is higher preference). Clients
293 * will contact hosts from the lowest-priority group first and fall back
294 * to higher priorities if the low-priority entries are unavailable.
299 * Relative weight for records with the same priority. Clients will use
300 * the hosts of the same (lowest) priority with a probability proportional
301 * to the weight given.
306 * TCP or UDP port of the service.
314 * Information from SOA records (RFC 1035).
316 struct GNUNET_DNSPARSER_SoaRecord
320 *The domainname of the name server that was the
321 * original or primary source of data for this zone.
322 * In UTF-8 format. The library will convert from and to DNS-IDNA
323 * as necessary. Use 'GNUNET_DNSPARSER_check_label' to test if an
324 * individual label is well-formed. If a given name is not well-formed,
325 * creating the DNS packet will fail.
330 * A domainname which specifies the mailbox of the
331 * person responsible for this zone.
332 * In UTF-8 format. The library will convert from and to DNS-IDNA
333 * as necessary. Use 'GNUNET_DNSPARSER_check_label' to test if an
334 * individual label is well-formed. If a given name is not well-formed,
335 * creating the DNS packet will fail.
340 * The version number of the original copy of the zone.
345 * Time interval before the zone should be refreshed.
350 * Time interval that should elapse before a failed refresh should
356 * Time value that specifies the upper limit on the time interval
357 * that can elapse before the zone is no longer authoritative.
362 * The bit minimum TTL field that should be exported with any RR
365 uint32_t minimum_ttl;
371 * Binary record information (unparsed).
373 struct GNUNET_DNSPARSER_RawRecord
377 * Binary record data.
382 * Number of bytes in data.
389 * A DNS response record.
391 struct GNUNET_DNSPARSER_Record
395 * Name of the record that the query is for (0-terminated).
396 * In UTF-8 format. The library will convert from and to DNS-IDNA
397 * as necessary. Use 'GNUNET_DNSPARSER_check_label' to test if an
398 * individual label is well-formed. If a given name is not well-formed,
399 * creating the DNS packet will fail.
404 * Payload of the record (which one of these is valid depends on the 'type').
410 * For NS, CNAME and PTR records, this is the uncompressed 0-terminated hostname.
411 * In UTF-8 format. The library will convert from and to DNS-IDNA
412 * as necessary. Use 'GNUNET_DNSPARSER_check_label' to test if an
413 * individual label is well-formed. If a given name is not well-formed,
414 * creating the DNS packet will fail.
419 * SOA data for SOA records.
421 struct GNUNET_DNSPARSER_SoaRecord *soa;
424 * MX data for MX records.
426 struct GNUNET_DNSPARSER_MxRecord *mx;
429 * SRV data for SRV records.
431 struct GNUNET_DNSPARSER_SrvRecord *srv;
434 * Raw data for all other types.
436 struct GNUNET_DNSPARSER_RawRecord raw;
442 * When does the record expire?
444 struct GNUNET_TIME_Absolute expiration_time;
447 * See GNUNET_DNSPARSER_TYPE_*.
452 * See GNUNET_DNSPARSER_CLASS_*.
460 * Easy-to-process, parsed version of a DNS packet.
462 struct GNUNET_DNSPARSER_Packet
465 * Array of all queries in the packet, must contain "num_queries" entries.
467 struct GNUNET_DNSPARSER_Query *queries;
470 * Array of all answers in the packet, must contain "num_answers" entries.
472 struct GNUNET_DNSPARSER_Record *answers;
475 * Array of all authority records in the packet, must contain "num_authority_records" entries.
477 struct GNUNET_DNSPARSER_Record *authority_records;
480 * Array of all additional answers in the packet, must contain "num_additional_records" entries.
482 struct GNUNET_DNSPARSER_Record *additional_records;
485 * Number of queries in the packet.
487 unsigned int num_queries;
490 * Number of answers in the packet, should be 0 for queries.
492 unsigned int num_answers;
495 * Number of authoritative answers in the packet, should be 0 for queries.
497 unsigned int num_authority_records;
500 * Number of additional records in the packet, should be 0 for queries.
502 unsigned int num_additional_records;
505 * Bitfield of DNS flags.
507 struct GNUNET_DNSPARSER_Flags flags;
510 * DNS ID (to match replies to requests).
518 * Check if a label in UTF-8 format can be coded into valid IDNA.
519 * This can fail if the ASCII-conversion becomes longer than 63 characters.
521 * @param label label to check (UTF-8 string)
522 * @return GNUNET_OK if the label can be converted to IDNA,
523 * GNUNET_SYSERR if the label is not valid for DNS names
526 GNUNET_DNSPARSER_check_label (const char *label);
530 * Parse a UDP payload of a DNS packet in to a nice struct for further
531 * processing and manipulation.
533 * @param udp_payload wire-format of the DNS packet
534 * @param udp_payload_length number of bytes in udp_payload
535 * @return NULL on error, otherwise the parsed packet
537 struct GNUNET_DNSPARSER_Packet *
538 GNUNET_DNSPARSER_parse (const char *udp_payload,
539 size_t udp_payload_length);
543 * Free memory taken by a packet.
545 * @param p packet to free
548 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p);
552 * Given a DNS packet, generate the corresponding UDP payload.
554 * @param p packet to pack
555 * @param max maximum allowed size for the resulting UDP payload
556 * @param buf set to a buffer with the packed message
557 * @param buf_length set to the length of buf
558 * @return GNUNET_SYSERR if 'p' is invalid
559 * GNUNET_NO if 'p' was truncated (but there is still a result in 'buf')
560 * GNUNET_OK if 'p' was packed completely into '*buf'
563 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,