37e31d24f9485a42abc91b011d036e6f8f57cf61
[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 // DNS-Stuff
33 GNUNET_NETWORK_STRUCT_BEGIN
34
35 struct dns_static
36 {
37   uint16_t id GNUNET_PACKED;
38
39   unsigned rd:1 GNUNET_PACKED;  // recursion desired (client -> server)
40   unsigned tc:1 GNUNET_PACKED;  // message is truncated
41   unsigned aa:1 GNUNET_PACKED;  // authoritative answer
42   unsigned op:4 GNUNET_PACKED;  // query:0, inverse q.:1, status: 2
43   unsigned qr:1 GNUNET_PACKED;  // query:0, response:1
44
45   unsigned rcode:4 GNUNET_PACKED;       // 0 No error
46   // 1 Format error
47   // 2 Server failure
48   // 3 Name Error
49   // 4 Not Implemented
50   // 5 Refused
51   unsigned z:3 GNUNET_PACKED;   // reserved
52   unsigned ra:1 GNUNET_PACKED;  // recursion available (server -> client)
53
54   uint16_t qdcount GNUNET_PACKED;       // number of questions
55   uint16_t ancount GNUNET_PACKED;       // number of answers
56   uint16_t nscount GNUNET_PACKED;       // number of authority-records
57   uint16_t arcount GNUNET_PACKED;       // number of additional records
58 };
59 GNUNET_NETWORK_STRUCT_END
60
61 struct dns_pkt
62 {
63   struct dns_static s;
64   unsigned char data[1];
65 };
66
67 struct dns_pkt_parsed
68 {
69   struct dns_static s;
70   struct dns_query **queries;
71   struct dns_record **answers;
72   struct dns_record **nameservers;
73   struct dns_record **additional;
74 };
75
76 struct dns_query_line
77 {
78   uint16_t type;
79   uint16_t class;
80 };
81
82 struct dns_query
83 {
84   char *name;
85   unsigned char namelen;
86   uint16_t qtype;
87   uint16_t qclass;
88 };
89
90 struct dns_record_line
91 {
92   uint16_t type;
93   uint16_t class;
94   uint32_t ttl;
95   uint16_t data_len;
96   unsigned char data;
97 };
98
99 struct dns_record
100 {
101   char *name;
102   unsigned char namelen;
103   uint16_t type;
104   uint16_t class;
105   uint32_t ttl;
106   uint16_t data_len;
107   unsigned char *data;
108 };
109
110
111 struct dns_pkt_parsed *
112 parse_dns_packet (struct dns_pkt *pkt);
113
114 struct dns_pkt *
115 unparse_dns_packet (struct dns_pkt_parsed *pkt);
116
117 void
118 free_parsed_dns_packet (struct dns_pkt_parsed *ppkt);
119
120 #endif