3aa289e7065f93baf02762e9498ff02d34e9056a
[oweals/gnunet.git] / src / include / gnunet_dnsparser_lib.h
1 /*
2       This file is part of GNUnet
3       (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
22  * @file include/gnunet_dnsparse_lib.h
23  * @brief API for helper library to parse DNS packets. 
24  * @author Philipp Toelke
25  */
26 #ifndef GNUNET_DNSPARSER_LIB_H
27 #define GNUNET_DNSPARSER_LIB_H
28
29 #include "platform.h"
30 #include "gnunet_common.h"
31
32 /**
33  * A few common DNS types.
34  */
35 #define GNUNET_DNS_TYPE_A 1
36 #define GNUNET_DNS_TYPE_NS 2
37 #define GNUNET_DNS_TYPE_CNAME 5
38 #define GNUNET_DNS_TYPE_SOA 6
39 #define GNUNET_DNS_TYPE_PTR 12
40 #define GNUNET_DNS_TYPE_MX 15
41 #define GNUNET_DNS_TYPE_TXT 16
42 #define GNUNET_DNS_TYPE_AAAA 28
43 #define GNUNET_DNS_TYPE_IXFR 251
44 #define GNUNET_DNS_TYPE_AXFR 252
45
46 /**
47  * A few common DNS classes (ok, only one is common, but I list a
48  * couple more to make it clear what we're talking about here).
49  */
50 #define GNUNET_DNS_CLASS_INTERNET 1
51 #define GNUNET_DNS_CLASS_CHAOS 3
52 #define GNUNET_DNS_CLASS_HESIOD 4
53
54
55 // DNS-Stuff
56 GNUNET_NETWORK_STRUCT_BEGIN
57
58 struct dns_static
59 {
60   uint16_t id GNUNET_PACKED;
61
62   unsigned rd:1 GNUNET_PACKED;  // recursion desired (client -> server)
63   unsigned tc:1 GNUNET_PACKED;  // message is truncated
64   unsigned aa:1 GNUNET_PACKED;  // authoritative answer
65   unsigned op:4 GNUNET_PACKED;  // query:0, inverse q.:1, status: 2
66   unsigned qr:1 GNUNET_PACKED;  // query:0, response:1
67
68   unsigned rcode:4 GNUNET_PACKED;       // 0 No error
69   // 1 Format error
70   // 2 Server failure
71   // 3 Name Error
72   // 4 Not Implemented
73   // 5 Refused
74   unsigned z:3 GNUNET_PACKED;   // reserved
75   unsigned ra:1 GNUNET_PACKED;  // recursion available (server -> client)
76
77   uint16_t qdcount GNUNET_PACKED;       // number of questions
78   uint16_t ancount GNUNET_PACKED;       // number of answers
79   uint16_t nscount GNUNET_PACKED;       // number of authority-records
80   uint16_t arcount GNUNET_PACKED;       // number of additional records
81 };
82 GNUNET_NETWORK_STRUCT_END
83
84 struct dns_pkt
85 {
86   struct dns_static s;
87   unsigned char data[1];
88 };
89
90 struct dns_pkt_parsed
91 {
92   struct dns_static s;
93   struct dns_query **queries;
94   struct dns_record **answers;
95   struct dns_record **nameservers;
96   struct dns_record **additional;
97 };
98
99 struct dns_query_line
100 {
101   uint16_t type;
102   uint16_t class;
103 };
104
105 struct dns_query
106 {
107   char *name;
108   unsigned char namelen;
109   uint16_t qtype;
110   uint16_t qclass;
111 };
112
113 struct dns_record_line
114 {
115   uint16_t type;
116   uint16_t class;
117   uint32_t ttl;
118   uint16_t data_len;
119   unsigned char data;
120 };
121
122 struct dns_record
123 {
124   char *name;
125   unsigned char namelen;
126   uint16_t type;
127   uint16_t class;
128   uint32_t ttl;
129   uint16_t data_len;
130   unsigned char *data;
131 };
132
133
134 struct dns_pkt_parsed *
135 parse_dns_packet (struct dns_pkt *pkt);
136
137 struct dns_pkt *
138 unparse_dns_packet (struct dns_pkt_parsed *pkt);
139
140 void
141 free_parsed_dns_packet (struct dns_pkt_parsed *ppkt);
142
143 #endif