2 This file is part of GNUnet
3 (C) 2010-2014 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 3, 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
30 #include "gnunet_util_lib.h"
31 #include "gnunet_tun_lib.h"
34 * Maximum length of a label in DNS.
36 #define GNUNET_DNSPARSER_MAX_LABEL_LENGTH 63
39 * Maximum length of a name in DNS.
41 #define GNUNET_DNSPARSER_MAX_NAME_LENGTH 253
45 * A few common DNS types.
47 #define GNUNET_DNSPARSER_TYPE_A 1
48 #define GNUNET_DNSPARSER_TYPE_NS 2
49 #define GNUNET_DNSPARSER_TYPE_CNAME 5
50 #define GNUNET_DNSPARSER_TYPE_SOA 6
51 #define GNUNET_DNSPARSER_TYPE_PTR 12
52 #define GNUNET_DNSPARSER_TYPE_MX 15
53 #define GNUNET_DNSPARSER_TYPE_TXT 16
54 #define GNUNET_DNSPARSER_TYPE_AAAA 28
55 #define GNUNET_DNSPARSER_TYPE_SRV 33
56 #define GNUNET_DNSPARSER_TYPE_CERT 37
57 #define GNUNET_DNSPARSER_TYPE_TLSA 52
63 struct GNUNET_DNSPARSER_Query
67 * Name of the record that the query is for (0-terminated).
68 * In UTF-8 format. The library will convert from and to DNS-IDNA
69 * as necessary. Use #GNUNET_DNSPARSER_check_label() to test if an
70 * individual label is well-formed. If a given name is not well-formed,
71 * creating the DNS packet will fail.
76 * See GNUNET_DNSPARSER_TYPE_*.
81 * See GNUNET_TUN_DNS_CLASS_*.
83 uint16_t dns_traffic_class;
89 * Information from MX records (RFC 1035).
91 struct GNUNET_DNSPARSER_MxRecord
95 * Preference for this entry (lower value is higher preference).
100 * Name of the mail server.
101 * In UTF-8 format. The library will convert from and to DNS-IDNA
102 * as necessary. Use #GNUNET_DNSPARSER_check_label() to test if an
103 * individual label is well-formed. If a given name is not well-formed,
104 * creating the DNS packet will fail.
112 * Information from SRV records (RFC 2782).
114 struct GNUNET_DNSPARSER_SrvRecord
118 * Hostname offering the service.
119 * In UTF-8 format. The library will convert from and to DNS-IDNA
120 * as necessary. Use #GNUNET_DNSPARSER_check_label() to test if an
121 * individual label is well-formed. If a given name is not well-formed,
122 * creating the DNS packet will fail.
127 * Preference for this entry (lower value is higher preference). Clients
128 * will contact hosts from the lowest-priority group first and fall back
129 * to higher priorities if the low-priority entries are unavailable.
134 * Relative weight for records with the same priority. Clients will use
135 * the hosts of the same (lowest) priority with a probability proportional
136 * to the weight given.
141 * TCP or UDP port of the service.
149 * DNS CERT types as defined in RFC 4398.
151 enum GNUNET_DNSPARSER_CertType
156 GNUNET_DNSPARSER_CERTTYPE_RESERVED = 0,
159 * An x509 PKIX certificate
161 GNUNET_DNSPARSER_CERTTYPE_PKIX = 1,
166 GNUNET_DNSPARSER_CERTTYPE_SKPI = 2,
171 GNUNET_DNSPARSER_CERTTYPE_PGP = 3,
174 * An x509 PKIX cert URL
176 GNUNET_DNSPARSER_CERTTYPE_IPKIX = 4,
181 GNUNET_DNSPARSER_CERTTYPE_ISKPI = 5,
184 * A PGP cert fingerprint and URL
186 GNUNET_DNSPARSER_CERTTYPE_IPGP = 6,
189 * An attribute Certificate
191 GNUNET_DNSPARSER_CERTTYPE_ACPKIX = 7,
194 * An attribute cert URL
196 GNUNET_DNSPARSER_CERTTYPE_IACKPIX = 8
201 * DNSCERT algorithms as defined in http://www.iana.org/assignments/
202 * dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml, under dns-sec-alg-numbers-1
204 enum GNUNET_DNSPARSER_CertAlgorithm
209 GNUNET_DNSPARSER_CERTALGO_UNDEFINED = 0,
214 GNUNET_DNSPARSER_CERTALGO_RSAMD5 = 1,
219 GNUNET_DNSPARSER_CERTALGO_DH = 2,
224 GNUNET_DNSPARSER_CERTALGO_DSASHA = 3,
229 GNUNET_DNSPARSER_CERTALGO_RSRVD4 = 4,
234 GNUNET_DNSPARSER_CERTALGO_RSASHA = 5,
239 GNUNET_DNSPARSER_CERTALGO_DSANSEC3 = 6,
244 GNUNET_DNSPARSER_CERTALGO_RSANSEC3 = 7,
249 GNUNET_DNSPARSER_CERTALGO_RSASHA256 = 8,
254 GNUNET_DNSPARSER_CERTALGO_RSRVD9 = 9,
259 GNUNET_DNSPARSER_CERTALGO_RSASHA512 = 10,
264 GNUNET_DNSPARSER_CERTALGO_GOST_R34 = 12,
267 * ECDSA Curve P-256/SHA256
269 GNUNET_DNSPARSER_CERTALGO_ECDSA_P256SHA256 = 13,
272 * ECDSA Curve P-384/SHA384
274 GNUNET_DNSPARSER_CERTALGO_ECDSA_P384SHA384 = 14
280 * Information from CERT records (RFC 4034).
282 struct GNUNET_DNSPARSER_CertRecord
288 enum GNUNET_DNSPARSER_CertType cert_type;
298 enum GNUNET_DNSPARSER_CertAlgorithm algorithm;
301 * Number of bytes in @e certificate_data
303 size_t certificate_size;
306 * Data of the certificate.
308 char *certificate_data;
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 * CERT data for CERT records.
426 struct GNUNET_DNSPARSER_CertRecord *cert;
429 * MX data for MX records.
431 struct GNUNET_DNSPARSER_MxRecord *mx;
434 * SRV data for SRV records.
436 struct GNUNET_DNSPARSER_SrvRecord *srv;
439 * Raw data for all other types.
441 struct GNUNET_DNSPARSER_RawRecord raw;
447 * When does the record expire?
449 struct GNUNET_TIME_Absolute expiration_time;
452 * See GNUNET_DNSPARSER_TYPE_*.
457 * See GNUNET_TUN_DNS_CLASS_*.
459 uint16_t dns_traffic_class;
465 * Easy-to-process, parsed version of a DNS packet.
467 struct GNUNET_DNSPARSER_Packet
470 * Array of all queries in the packet, must contain "num_queries" entries.
472 struct GNUNET_DNSPARSER_Query *queries;
475 * Array of all answers in the packet, must contain "num_answers" entries.
477 struct GNUNET_DNSPARSER_Record *answers;
480 * Array of all authority records in the packet, must contain "num_authority_records" entries.
482 struct GNUNET_DNSPARSER_Record *authority_records;
485 * Array of all additional answers in the packet, must contain "num_additional_records" entries.
487 struct GNUNET_DNSPARSER_Record *additional_records;
490 * Number of queries in the packet.
492 unsigned int num_queries;
495 * Number of answers in the packet, should be 0 for queries.
497 unsigned int num_answers;
500 * Number of authoritative answers in the packet, should be 0 for queries.
502 unsigned int num_authority_records;
505 * Number of additional records in the packet, should be 0 for queries.
507 unsigned int num_additional_records;
510 * Bitfield of DNS flags.
512 struct GNUNET_TUN_DnsFlags flags;
515 * DNS ID (to match replies to requests).
523 * Check if a label in UTF-8 format can be coded into valid IDNA.
524 * This can fail if the ASCII-conversion becomes longer than 63 characters.
526 * @param label label to check (UTF-8 string)
527 * @return #GNUNET_OK if the label can be converted to IDNA,
528 * #GNUNET_SYSERR if the label is not valid for DNS names
531 GNUNET_DNSPARSER_check_label (const char *label);
535 * Check if a hostname in UTF-8 format can be coded into valid IDNA.
536 * This can fail if a label becomes longer than 63 characters or if
537 * the entire name exceeds 253 characters.
539 * @param name name to check (UTF-8 string)
540 * @return #GNUNET_OK if the label can be converted to IDNA,
541 * #GNUNET_SYSERR if the label is not valid for DNS names
544 GNUNET_DNSPARSER_check_name (const char *name);
548 * Parse a UDP payload of a DNS packet in to a nice struct for further
549 * processing and manipulation.
551 * @param udp_payload wire-format of the DNS packet
552 * @param udp_payload_length number of bytes in @a udp_payload
553 * @return NULL on error, otherwise the parsed packet
555 struct GNUNET_DNSPARSER_Packet *
556 GNUNET_DNSPARSER_parse (const char *udp_payload,
557 size_t udp_payload_length);
561 * Free memory taken by a packet.
563 * @param p packet to free
566 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p);
570 * Given a DNS packet @a p, generate the corresponding UDP payload.
571 * Note that we do not attempt to pack the strings with pointers
572 * as this would complicate the code and this is about being
573 * simple and secure, not fast, fancy and broken like bind.
575 * @param p packet to pack
576 * @param max maximum allowed size for the resulting UDP payload
577 * @param buf set to a buffer with the packed message
578 * @param buf_length set to the length of @a buf
579 * @return #GNUNET_SYSERR if @a p is invalid
580 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
581 * #GNUNET_OK if @a p was packed completely into @a buf
584 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
589 /* ***************** low-level packing API ******************** */
592 * Add a DNS name to the UDP packet at the given location, converting
593 * the name to IDNA notation as necessary.
595 * @param dst where to write the name (UDP packet)
596 * @param dst_len number of bytes in @a dst
597 * @param off pointer to offset where to write the name (increment by bytes used)
598 * must not be changed if there is an error
599 * @param name name to write
600 * @return #GNUNET_SYSERR if @a name is invalid
601 * #GNUNET_NO if @a name did not fit
602 * #GNUNET_OK if @a name was added to @a dst
605 GNUNET_DNSPARSER_builder_add_name (char *dst,
612 * Add a DNS query to the UDP packet at the given location.
614 * @param dst where to write the query
615 * @param dst_len number of bytes in @a dst
616 * @param off pointer to offset where to write the query (increment by bytes used)
617 * must not be changed if there is an error
618 * @param query query to write
619 * @return #GNUNET_SYSERR if @a query is invalid
620 * #GNUNET_NO if @a query did not fit
621 * #GNUNET_OK if @a query was added to @a dst
624 GNUNET_DNSPARSER_builder_add_query (char *dst,
627 const struct GNUNET_DNSPARSER_Query *query);
631 * Add an MX record to the UDP packet at the given location.
633 * @param dst where to write the mx record
634 * @param dst_len number of bytes in @a dst
635 * @param off pointer to offset where to write the mx information (increment by bytes used);
636 * can also change if there was an error
637 * @param mx mx information to write
638 * @return #GNUNET_SYSERR if @a mx is invalid
639 * #GNUNET_NO if @a mx did not fit
640 * #GNUNET_OK if @a mx was added to @a dst
643 GNUNET_DNSPARSER_builder_add_mx (char *dst,
646 const struct GNUNET_DNSPARSER_MxRecord *mx);
650 * Add an SOA record to the UDP packet at the given location.
652 * @param dst where to write the SOA record
653 * @param dst_len number of bytes in @a dst
654 * @param off pointer to offset where to write the SOA information (increment by bytes used)
655 * can also change if there was an error
656 * @param soa SOA information to write
657 * @return #GNUNET_SYSERR if @a soa is invalid
658 * #GNUNET_NO if @a soa did not fit
659 * #GNUNET_OK if @a soa was added to @a dst
662 GNUNET_DNSPARSER_builder_add_soa (char *dst,
665 const struct GNUNET_DNSPARSER_SoaRecord *soa);
669 * Add CERT record to the UDP packet at the given location.
671 * @param dst where to write the CERT record
672 * @param dst_len number of bytes in @a dst
673 * @param off pointer to offset where to write the CERT information (increment by bytes used)
674 * can also change if there was an error
675 * @param cert CERT information to write
676 * @return #GNUNET_SYSERR if @a soa is invalid
677 * #GNUNET_NO if @a soa did not fit
678 * #GNUNET_OK if @a soa was added to @a dst
681 GNUNET_DNSPARSER_builder_add_cert (char *dst,
684 const struct GNUNET_DNSPARSER_CertRecord *cert);
688 * Add an SRV record to the UDP packet at the given location.
690 * @param dst where to write the SRV record
691 * @param dst_len number of bytes in @a dst
692 * @param off pointer to offset where to write the SRV information (increment by bytes used)
693 * can also change if there was an error
694 * @param srv SRV information to write
695 * @return #GNUNET_SYSERR if @a srv is invalid
696 * #GNUNET_NO if @a srv did not fit
697 * #GNUNET_OK if @a srv was added to @a dst
700 GNUNET_DNSPARSER_builder_add_srv (char *dst,
703 const struct GNUNET_DNSPARSER_SrvRecord *srv);
705 /* ***************** low-level parsing API ******************** */
708 * Parse a DNS record entry.
710 * @param udp_payload entire UDP payload
711 * @param udp_payload_length length of @a udp_payload
712 * @param off pointer to the offset of the record to parse in the udp_payload (to be
713 * incremented by the size of the record)
714 * @param r where to write the record information
715 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
718 GNUNET_DNSPARSER_parse_record (const char *udp_payload,
719 size_t udp_payload_length,
721 struct GNUNET_DNSPARSER_Record *r);
725 * Parse name inside of a DNS query or record.
727 * @param udp_payload entire UDP payload
728 * @param udp_payload_length length of @a udp_payload
729 * @param off pointer to the offset of the name to parse in the udp_payload (to be
730 * incremented by the size of the name)
731 * @return name as 0-terminated C string on success, NULL if the payload is malformed
734 GNUNET_DNSPARSER_parse_name (const char *udp_payload,
735 size_t udp_payload_length,
740 * Parse a DNS query entry.
742 * @param udp_payload entire UDP payload
743 * @param udp_payload_length length of @a udp_payload
744 * @param off pointer to the offset of the query to parse in the udp_payload (to be
745 * incremented by the size of the query)
746 * @param q where to write the query information
747 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
750 GNUNET_DNSPARSER_parse_query (const char *udp_payload,
751 size_t udp_payload_length,
753 struct GNUNET_DNSPARSER_Query *q);
757 * Parse a DNS SOA record.
759 * @param udp_payload reference to UDP packet
760 * @param udp_payload_length length of @a udp_payload
761 * @param off pointer to the offset of the query to parse in the SOA record (to be
762 * incremented by the size of the record), unchanged on error
763 * @return the parsed SOA record, NULL on error
765 struct GNUNET_DNSPARSER_SoaRecord *
766 GNUNET_DNSPARSER_parse_soa (const char *udp_payload,
767 size_t udp_payload_length,
772 * Parse a DNS CERT record.
774 * @param udp_payload reference to UDP packet
775 * @param udp_payload_length length of @a udp_payload
776 * @param off pointer to the offset of the query to parse in the CERT record (to be
777 * incremented by the size of the record), unchanged on error
778 * @return the parsed CERT record, NULL on error
780 struct GNUNET_DNSPARSER_CertRecord *
781 GNUNET_DNSPARSER_parse_cert (const char *udp_payload,
782 size_t udp_payload_length,
787 * Parse a DNS MX record.
789 * @param udp_payload reference to UDP packet
790 * @param udp_payload_length length of @a udp_payload
791 * @param off pointer to the offset of the query to parse in the MX record (to be
792 * incremented by the size of the record), unchanged on error
793 * @return the parsed MX record, NULL on error
795 struct GNUNET_DNSPARSER_MxRecord *
796 GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
797 size_t udp_payload_length,
802 * Parse a DNS SRV record.
804 * @param udp_payload reference to UDP packet
805 * @param udp_payload_length length of @a udp_payload
806 * @param off pointer to the offset of the query to parse in the SRV record (to be
807 * incremented by the size of the record), unchanged on error
808 * @return the parsed SRV record, NULL on error
810 struct GNUNET_DNSPARSER_SrvRecord *
811 GNUNET_DNSPARSER_parse_srv (const char *udp_payload,
812 size_t udp_payload_length,
815 /* ***************** low-level deallocation API ******************** */
818 * Free the given DNS record.
820 * @param r record to free
823 GNUNET_DNSPARSER_free_record (struct GNUNET_DNSPARSER_Record *r);
827 * Free MX information record.
829 * @param mx record to free
832 GNUNET_DNSPARSER_free_mx (struct GNUNET_DNSPARSER_MxRecord *mx);
836 * Free SRV information record.
838 * @param srv record to free
841 GNUNET_DNSPARSER_free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv);
845 * Free SOA information record.
847 * @param soa record to free
850 GNUNET_DNSPARSER_free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa);
854 * Free CERT information record.
856 * @param cert record to free
859 GNUNET_DNSPARSER_free_cert (struct GNUNET_DNSPARSER_CertRecord *cert);
863 * Convert a block of binary data to HEX.
865 * @param data binary data to convert
866 * @param data_size number of bytes in @a data
867 * @return HEX string (lower case)
870 GNUNET_DNSPARSER_bin_to_hex (const void *data,
875 * Convert a HEX string to block of binary data.
877 * @param hex HEX string to convert (may contain mixed case)
878 * @param data where to write result, must be
879 * at least `strlen(hex)/2` bytes long
880 * @return number of bytes written to data
883 GNUNET_DNSPARSER_hex_to_bin (const char *hex,