2 This file is part of GNUnet
3 Copyright (C) 2010-2014 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @author Philipp Toelke
21 * @author Christian Grothoff
24 * API for helper library to parse DNS packets.
26 * @defgroup dns-parser DNS parser library
27 * Helper library to parse DNS packets.
30 #ifndef GNUNET_DNSPARSER_LIB_H
31 #define GNUNET_DNSPARSER_LIB_H
33 #include "gnunet_util_lib.h"
36 * Maximum length of a label in DNS.
38 #define GNUNET_DNSPARSER_MAX_LABEL_LENGTH 63
41 * Maximum length of a name in DNS.
43 #define GNUNET_DNSPARSER_MAX_NAME_LENGTH 253
47 * A few common DNS types.
49 #define GNUNET_DNSPARSER_TYPE_ANY 0
50 #define GNUNET_DNSPARSER_TYPE_A 1
51 #define GNUNET_DNSPARSER_TYPE_NS 2
52 #define GNUNET_DNSPARSER_TYPE_CNAME 5
53 #define GNUNET_DNSPARSER_TYPE_SOA 6
54 #define GNUNET_DNSPARSER_TYPE_PTR 12
55 #define GNUNET_DNSPARSER_TYPE_MX 15
56 #define GNUNET_DNSPARSER_TYPE_TXT 16
57 #define GNUNET_DNSPARSER_TYPE_RP 17
58 #define GNUNET_DNSPARSER_TYPE_AFSDB 18
59 #define GNUNET_DNSPARSER_TYPE_SIG 24
60 #define GNUNET_DNSPARSER_TYPE_KEY 25
61 #define GNUNET_DNSPARSER_TYPE_AAAA 28
62 #define GNUNET_DNSPARSER_TYPE_LOC 29
63 #define GNUNET_DNSPARSER_TYPE_SRV 33
64 #define GNUNET_DNSPARSER_TYPE_NAPTR 35
65 #define GNUNET_DNSPARSER_TYPE_KX 36
66 #define GNUNET_DNSPARSER_TYPE_CERT 37
67 #define GNUNET_DNSPARSER_TYPE_DNAME 39
68 #define GNUNET_DNSPARSER_TYPE_APL 42
69 #define GNUNET_DNSPARSER_TYPE_DS 43
70 #define GNUNET_DNSPARSER_TYPE_SSHFP 44
71 #define GNUNET_DNSPARSER_TYPE_IPSECKEY 45
72 #define GNUNET_DNSPARSER_TYPE_RRSIG 46
73 #define GNUNET_DNSPARSER_TYPE_NSEC 47
74 #define GNUNET_DNSPARSER_TYPE_DNSKEY 48
75 #define GNUNET_DNSPARSER_TYPE_DHCID 49
76 #define GNUNET_DNSPARSER_TYPE_NSEC3 50
77 #define GNUNET_DNSPARSER_TYPE_NSEC3PARAM 51
78 #define GNUNET_DNSPARSER_TYPE_TLSA 52
79 #define GNUNET_DNSPARSER_TYPE_HIP 55
80 #define GNUNET_DNSPARSER_TYPE_CDS 59
81 #define GNUNET_DNSPARSER_TYPE_CDNSKEY 60
82 #define GNUNET_DNSPARSER_TYPE_OPENPGPKEY 61
83 #define GNUNET_DNSPARSER_TYPE_TKEY 249
84 #define GNUNET_DNSPARSER_TYPE_TSIG 250
85 #define GNUNET_DNSPARSER_TYPE_URI 256
86 #define GNUNET_DNSPARSER_TYPE_TA 32768
91 struct GNUNET_DNSPARSER_Query
95 * Name of the record that the query is for (0-terminated).
96 * In UTF-8 format. The library will convert from and to DNS-IDNA
97 * as necessary. Use #GNUNET_DNSPARSER_check_label() to test if an
98 * individual label is well-formed. If a given name is not well-formed,
99 * creating the DNS packet will fail.
104 * See GNUNET_DNSPARSER_TYPE_*.
109 * See GNUNET_TUN_DNS_CLASS_*.
111 uint16_t dns_traffic_class;
117 * Information from MX records (RFC 1035).
119 struct GNUNET_DNSPARSER_MxRecord
123 * Preference for this entry (lower value is higher preference).
128 * Name of the mail server.
129 * In UTF-8 format. The library will convert from and to DNS-IDNA
130 * as necessary. Use #GNUNET_DNSPARSER_check_label() to test if an
131 * individual label is well-formed. If a given name is not well-formed,
132 * creating the DNS packet will fail.
140 * Information from SRV records (RFC 2782).
142 struct GNUNET_DNSPARSER_SrvRecord
146 * Hostname offering the service.
147 * In UTF-8 format. The library will convert from and to DNS-IDNA
148 * as necessary. Use #GNUNET_DNSPARSER_check_label() to test if an
149 * individual label is well-formed. If a given name is not well-formed,
150 * creating the DNS packet will fail.
155 * Preference for this entry (lower value is higher preference). Clients
156 * will contact hosts from the lowest-priority group first and fall back
157 * to higher priorities if the low-priority entries are unavailable.
162 * Relative weight for records with the same priority. Clients will use
163 * the hosts of the same (lowest) priority with a probability proportional
164 * to the weight given.
169 * TCP or UDP port of the service.
177 * DNS CERT types as defined in RFC 4398.
179 enum GNUNET_DNSPARSER_CertType
184 GNUNET_DNSPARSER_CERTTYPE_RESERVED = 0,
187 * An x509 PKIX certificate
189 GNUNET_DNSPARSER_CERTTYPE_PKIX = 1,
194 GNUNET_DNSPARSER_CERTTYPE_SKPI = 2,
199 GNUNET_DNSPARSER_CERTTYPE_PGP = 3,
202 * An x509 PKIX cert URL
204 GNUNET_DNSPARSER_CERTTYPE_IPKIX = 4,
209 GNUNET_DNSPARSER_CERTTYPE_ISKPI = 5,
212 * A PGP cert fingerprint and URL
214 GNUNET_DNSPARSER_CERTTYPE_IPGP = 6,
217 * An attribute Certificate
219 GNUNET_DNSPARSER_CERTTYPE_ACPKIX = 7,
222 * An attribute cert URL
224 GNUNET_DNSPARSER_CERTTYPE_IACKPIX = 8
229 * DNSCERT algorithms as defined in http://www.iana.org/assignments/
230 * dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml, under dns-sec-alg-numbers-1
232 enum GNUNET_DNSPARSER_CertAlgorithm
237 GNUNET_DNSPARSER_CERTALGO_UNDEFINED = 0,
242 GNUNET_DNSPARSER_CERTALGO_RSAMD5 = 1,
247 GNUNET_DNSPARSER_CERTALGO_DH = 2,
252 GNUNET_DNSPARSER_CERTALGO_DSASHA = 3,
257 GNUNET_DNSPARSER_CERTALGO_RSRVD4 = 4,
262 GNUNET_DNSPARSER_CERTALGO_RSASHA = 5,
267 GNUNET_DNSPARSER_CERTALGO_DSANSEC3 = 6,
272 GNUNET_DNSPARSER_CERTALGO_RSANSEC3 = 7,
277 GNUNET_DNSPARSER_CERTALGO_RSASHA256 = 8,
282 GNUNET_DNSPARSER_CERTALGO_RSRVD9 = 9,
287 GNUNET_DNSPARSER_CERTALGO_RSASHA512 = 10,
292 GNUNET_DNSPARSER_CERTALGO_GOST_R34 = 12,
295 * ECDSA Curve P-256/SHA256
297 GNUNET_DNSPARSER_CERTALGO_ECDSA_P256SHA256 = 13,
300 * ECDSA Curve P-384/SHA384
302 GNUNET_DNSPARSER_CERTALGO_ECDSA_P384SHA384 = 14
308 * Information from CERT records (RFC 4034).
310 struct GNUNET_DNSPARSER_CertRecord
316 enum GNUNET_DNSPARSER_CertType cert_type;
326 enum GNUNET_DNSPARSER_CertAlgorithm algorithm;
329 * Number of bytes in @e certificate_data
331 size_t certificate_size;
334 * Data of the certificate.
336 char *certificate_data;
342 * Information from SOA records (RFC 1035).
344 struct GNUNET_DNSPARSER_SoaRecord
348 * The domainname of the name server that was the
349 * original or primary source of data for this zone.
350 * In UTF-8 format. The library will convert from and to DNS-IDNA
351 * as necessary. Use #GNUNET_DNSPARSER_check_label() to test if an
352 * individual label is well-formed. If a given name is not well-formed,
353 * creating the DNS packet will fail.
358 * A domainname which specifies the mailbox of the
359 * person responsible for this zone.
360 * In UTF-8 format. The library will convert from and to DNS-IDNA
361 * as necessary. Use #GNUNET_DNSPARSER_check_label() to test if an
362 * individual label is well-formed. If a given name is not well-formed,
363 * creating the DNS packet will fail.
368 * The version number of the original copy of the zone.
373 * Time interval before the zone should be refreshed.
378 * Time interval that should elapse before a failed refresh should
384 * Time value that specifies the upper limit on the time interval
385 * that can elapse before the zone is no longer authoritative.
390 * The bit minimum TTL field that should be exported with any RR
393 uint32_t minimum_ttl;
399 * Binary record information (unparsed).
401 struct GNUNET_DNSPARSER_RawRecord
405 * Binary record data.
410 * Number of bytes in data.
417 * A DNS response record.
419 struct GNUNET_DNSPARSER_Record
423 * Name of the record that the query is for (0-terminated).
424 * In UTF-8 format. The library will convert from and to DNS-IDNA
425 * as necessary. Use #GNUNET_DNSPARSER_check_label() to test if an
426 * individual label is well-formed. If a given name is not well-formed,
427 * creating the DNS packet will fail.
432 * Payload of the record (which one of these is valid depends on the 'type').
438 * For NS, CNAME and PTR records, this is the uncompressed 0-terminated hostname.
439 * In UTF-8 format. The library will convert from and to DNS-IDNA
440 * as necessary. Use #GNUNET_DNSPARSER_check_label() to test if an
441 * individual label is well-formed. If a given name is not well-formed,
442 * creating the DNS packet will fail.
447 * SOA data for SOA records.
449 struct GNUNET_DNSPARSER_SoaRecord *soa;
452 * CERT data for CERT records.
454 struct GNUNET_DNSPARSER_CertRecord *cert;
457 * MX data for MX records.
459 struct GNUNET_DNSPARSER_MxRecord *mx;
462 * SRV data for SRV records.
464 struct GNUNET_DNSPARSER_SrvRecord *srv;
467 * Raw data for all other types.
469 struct GNUNET_DNSPARSER_RawRecord raw;
475 * When does the record expire?
477 struct GNUNET_TIME_Absolute expiration_time;
480 * See GNUNET_DNSPARSER_TYPE_*.
485 * See GNUNET_TUN_DNS_CLASS_*.
487 uint16_t dns_traffic_class;
493 * Easy-to-process, parsed version of a DNS packet.
495 struct GNUNET_DNSPARSER_Packet
498 * Array of all queries in the packet, must contain "num_queries" entries.
500 struct GNUNET_DNSPARSER_Query *queries;
503 * Array of all answers in the packet, must contain "num_answers" entries.
505 struct GNUNET_DNSPARSER_Record *answers;
508 * Array of all authority records in the packet, must contain "num_authority_records" entries.
510 struct GNUNET_DNSPARSER_Record *authority_records;
513 * Array of all additional answers in the packet, must contain "num_additional_records" entries.
515 struct GNUNET_DNSPARSER_Record *additional_records;
518 * Number of queries in the packet.
520 unsigned int num_queries;
523 * Number of answers in the packet, should be 0 for queries.
525 unsigned int num_answers;
528 * Number of authoritative answers in the packet, should be 0 for queries.
530 unsigned int num_authority_records;
533 * Number of additional records in the packet, should be 0 for queries.
535 unsigned int num_additional_records;
538 * Bitfield of DNS flags.
540 struct GNUNET_TUN_DnsFlags flags;
543 * DNS ID (to match replies to requests).
551 * Check if a label in UTF-8 format can be coded into valid IDNA.
552 * This can fail if the ASCII-conversion becomes longer than 63 characters.
554 * @param label label to check (UTF-8 string)
555 * @return #GNUNET_OK if the label can be converted to IDNA,
556 * #GNUNET_SYSERR if the label is not valid for DNS names
559 GNUNET_DNSPARSER_check_label (const char *label);
563 * Check if a hostname in UTF-8 format can be coded into valid IDNA.
564 * This can fail if a label becomes longer than 63 characters or if
565 * the entire name exceeds 253 characters.
567 * @param name name to check (UTF-8 string)
568 * @return #GNUNET_OK if the label can be converted to IDNA,
569 * #GNUNET_SYSERR if the label is not valid for DNS names
572 GNUNET_DNSPARSER_check_name (const char *name);
576 * Parse a UDP payload of a DNS packet in to a nice struct for further
577 * processing and manipulation.
579 * @param udp_payload wire-format of the DNS packet
580 * @param udp_payload_length number of bytes in @a udp_payload
581 * @return NULL on error, otherwise the parsed packet
583 struct GNUNET_DNSPARSER_Packet *
584 GNUNET_DNSPARSER_parse (const char *udp_payload,
585 size_t udp_payload_length);
589 * Free memory taken by a packet.
591 * @param p packet to free
594 GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p);
598 * Given a DNS packet @a p, generate the corresponding UDP payload.
599 * Note that we do not attempt to pack the strings with pointers
600 * as this would complicate the code and this is about being
601 * simple and secure, not fast, fancy and broken like bind.
603 * @param p packet to pack
604 * @param max maximum allowed size for the resulting UDP payload
605 * @param buf set to a buffer with the packed message
606 * @param buf_length set to the length of @a buf
607 * @return #GNUNET_SYSERR if @a p is invalid
608 * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
609 * #GNUNET_OK if @a p was packed completely into @a buf
612 GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
617 /* ***************** low-level packing API ******************** */
620 * Add a DNS name to the UDP packet at the given location, converting
621 * the name to IDNA notation as necessary.
623 * @param dst where to write the name (UDP packet)
624 * @param dst_len number of bytes in @a dst
625 * @param off pointer to offset where to write the name (increment by bytes used)
626 * must not be changed if there is an error
627 * @param name name to write
628 * @return #GNUNET_SYSERR if @a name is invalid
629 * #GNUNET_NO if @a name did not fit
630 * #GNUNET_OK if @a name was added to @a dst
633 GNUNET_DNSPARSER_builder_add_name (char *dst,
640 * Add a DNS query to the UDP packet at the given location.
642 * @param dst where to write the query
643 * @param dst_len number of bytes in @a dst
644 * @param off pointer to offset where to write the query (increment by bytes used)
645 * must not be changed if there is an error
646 * @param query query to write
647 * @return #GNUNET_SYSERR if @a query is invalid
648 * #GNUNET_NO if @a query did not fit
649 * #GNUNET_OK if @a query was added to @a dst
652 GNUNET_DNSPARSER_builder_add_query (char *dst,
655 const struct GNUNET_DNSPARSER_Query *query);
659 * Add an MX record to the UDP packet at the given location.
661 * @param dst where to write the mx record
662 * @param dst_len number of bytes in @a dst
663 * @param off pointer to offset where to write the mx information (increment by bytes used);
664 * can also change if there was an error
665 * @param mx mx information to write
666 * @return #GNUNET_SYSERR if @a mx is invalid
667 * #GNUNET_NO if @a mx did not fit
668 * #GNUNET_OK if @a mx was added to @a dst
671 GNUNET_DNSPARSER_builder_add_mx (char *dst,
674 const struct GNUNET_DNSPARSER_MxRecord *mx);
678 * Add an SOA record to the UDP packet at the given location.
680 * @param dst where to write the SOA record
681 * @param dst_len number of bytes in @a dst
682 * @param off pointer to offset where to write the SOA information (increment by bytes used)
683 * can also change if there was an error
684 * @param soa SOA information to write
685 * @return #GNUNET_SYSERR if @a soa is invalid
686 * #GNUNET_NO if @a soa did not fit
687 * #GNUNET_OK if @a soa was added to @a dst
690 GNUNET_DNSPARSER_builder_add_soa (char *dst,
693 const struct GNUNET_DNSPARSER_SoaRecord *soa);
697 * Add CERT record to the UDP packet at the given location.
699 * @param dst where to write the CERT record
700 * @param dst_len number of bytes in @a dst
701 * @param off pointer to offset where to write the CERT information (increment by bytes used)
702 * can also change if there was an error
703 * @param cert CERT information to write
704 * @return #GNUNET_SYSERR if @a soa is invalid
705 * #GNUNET_NO if @a soa did not fit
706 * #GNUNET_OK if @a soa was added to @a dst
709 GNUNET_DNSPARSER_builder_add_cert (char *dst,
712 const struct GNUNET_DNSPARSER_CertRecord *cert);
716 * Add an SRV record to the UDP packet at the given location.
718 * @param dst where to write the SRV record
719 * @param dst_len number of bytes in @a dst
720 * @param off pointer to offset where to write the SRV information (increment by bytes used)
721 * can also change if there was an error
722 * @param srv SRV information to write
723 * @return #GNUNET_SYSERR if @a srv is invalid
724 * #GNUNET_NO if @a srv did not fit
725 * #GNUNET_OK if @a srv was added to @a dst
728 GNUNET_DNSPARSER_builder_add_srv (char *dst,
731 const struct GNUNET_DNSPARSER_SrvRecord *srv);
733 /* ***************** low-level parsing API ******************** */
736 * Parse a DNS record entry.
738 * @param udp_payload entire UDP payload
739 * @param udp_payload_length length of @a udp_payload
740 * @param off pointer to the offset of the record to parse in the udp_payload (to be
741 * incremented by the size of the record)
742 * @param r where to write the record information
743 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
746 GNUNET_DNSPARSER_parse_record (const char *udp_payload,
747 size_t udp_payload_length,
749 struct GNUNET_DNSPARSER_Record *r);
753 * Parse name inside of a DNS query or record.
755 * @param udp_payload entire UDP payload
756 * @param udp_payload_length length of @a udp_payload
757 * @param off pointer to the offset of the name to parse in the udp_payload (to be
758 * incremented by the size of the name)
759 * @return name as 0-terminated C string on success, NULL if the payload is malformed
762 GNUNET_DNSPARSER_parse_name (const char *udp_payload,
763 size_t udp_payload_length,
768 * Parse a DNS query entry.
770 * @param udp_payload entire UDP payload
771 * @param udp_payload_length length of @a udp_payload
772 * @param off pointer to the offset of the query to parse in the udp_payload (to be
773 * incremented by the size of the query)
774 * @param q where to write the query information
775 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
778 GNUNET_DNSPARSER_parse_query (const char *udp_payload,
779 size_t udp_payload_length,
781 struct GNUNET_DNSPARSER_Query *q);
785 * Parse a DNS SOA record.
787 * @param udp_payload reference to UDP packet
788 * @param udp_payload_length length of @a udp_payload
789 * @param off pointer to the offset of the query to parse in the SOA record (to be
790 * incremented by the size of the record), unchanged on error
791 * @return the parsed SOA record, NULL on error
793 struct GNUNET_DNSPARSER_SoaRecord *
794 GNUNET_DNSPARSER_parse_soa (const char *udp_payload,
795 size_t udp_payload_length,
800 * Parse a DNS CERT record.
802 * @param udp_payload reference to UDP packet
803 * @param udp_payload_length length of @a udp_payload
804 * @param off pointer to the offset of the query to parse in the CERT record (to be
805 * incremented by the size of the record), unchanged on error
806 * @return the parsed CERT record, NULL on error
808 struct GNUNET_DNSPARSER_CertRecord *
809 GNUNET_DNSPARSER_parse_cert (const char *udp_payload,
810 size_t udp_payload_length,
815 * Parse a DNS MX record.
817 * @param udp_payload reference to UDP packet
818 * @param udp_payload_length length of @a udp_payload
819 * @param off pointer to the offset of the query to parse in the MX record (to be
820 * incremented by the size of the record), unchanged on error
821 * @return the parsed MX record, NULL on error
823 struct GNUNET_DNSPARSER_MxRecord *
824 GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
825 size_t udp_payload_length,
830 * Parse a DNS SRV record.
832 * @param udp_payload reference to UDP packet
833 * @param udp_payload_length length of @a udp_payload
834 * @param off pointer to the offset of the query to parse in the SRV record (to be
835 * incremented by the size of the record), unchanged on error
836 * @return the parsed SRV record, NULL on error
838 struct GNUNET_DNSPARSER_SrvRecord *
839 GNUNET_DNSPARSER_parse_srv (const char *udp_payload,
840 size_t udp_payload_length,
843 /* ***************** low-level deallocation API ******************** */
846 * Free the given DNS record.
848 * @param r record to free
851 GNUNET_DNSPARSER_free_record (struct GNUNET_DNSPARSER_Record *r);
855 * Free MX information record.
857 * @param mx record to free
860 GNUNET_DNSPARSER_free_mx (struct GNUNET_DNSPARSER_MxRecord *mx);
864 * Free SRV information record.
866 * @param srv record to free
869 GNUNET_DNSPARSER_free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv);
873 * Free SOA information record.
875 * @param soa record to free
878 GNUNET_DNSPARSER_free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa);
882 * Free CERT information record.
884 * @param cert record to free
887 GNUNET_DNSPARSER_free_cert (struct GNUNET_DNSPARSER_CertRecord *cert);
891 * Convert a block of binary data to HEX.
893 * @param data binary data to convert
894 * @param data_size number of bytes in @a data
895 * @return HEX string (lower case)
898 GNUNET_DNSPARSER_bin_to_hex (const void *data,
903 * Convert a HEX string to block of binary data.
905 * @param hex HEX string to convert (may contain mixed case)
906 * @param data where to write result, must be
907 * at least `strlen(hex)/2` bytes long
908 * @return number of bytes written to data
911 GNUNET_DNSPARSER_hex_to_bin (const char *hex,
917 /** @} */ /* end of group */