-implementing new DNS client API
[oweals/gnunet.git] / src / include / gnunet_dns_service.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_dns_service.h
23  * @brief API to access the DNS service.  Not finished at all,
24  *        currently only contains the structs for the IPC, which
25  *        don't even belong here (legacy code in transition)
26  * @author Philipp Toelke
27  * 
28  * TODO:
29  * - replace (most?) structs with nice function (prototypes) that take
30  *   the appropriate arguments to pass the data
31  * - clean up API implementation itself (nicer reconnect, etc.)
32  */
33 #ifndef GNUNET_DNS_SERVICE_H
34 #define GNUNET_DNS_SERVICE_H
35
36 #include "gnunet_common.h"
37 #include "gnunet_util_lib.h"
38
39
40 /**
41  * Subtypes of DNS answers.
42  */
43 enum GNUNET_DNS_ANSWER_Subtype
44 {
45   /**
46    * Answers of this type contain a dns-packet that just has to be transmitted
47    */
48   GNUNET_DNS_ANSWER_TYPE_IP,
49
50   /**
51    * Answers of this type contain an incomplete dns-packet. The IP-Address
52    * is all 0s. The addroffset points to it.
53    */
54   GNUNET_DNS_ANSWER_TYPE_SERVICE,
55
56   /**
57    * Answers of this type contain an incomplete dns-packet as answer to a
58    * PTR-Query. The resolved name is not allocated. The addroffset points to it.
59    */
60   GNUNET_DNS_ANSWER_TYPE_REV,
61   
62   /**
63    * Answers of this type contains an IP6-Address but traffic to this IP should
64    * be routed through the GNUNet.
65    */
66   GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA,
67   
68   /**
69    * Answers of this type contains an IP4-Address but traffic to this IP should
70    * be routed through the GNUNet.
71    */
72   GNUNET_DNS_ANSWER_TYPE_REMOTE_A
73
74 };
75
76
77 GNUNET_NETWORK_STRUCT_BEGIN
78 struct GNUNET_vpn_service_descriptor
79 {
80   GNUNET_HashCode peer GNUNET_PACKED;
81   GNUNET_HashCode service_descriptor GNUNET_PACKED;
82   uint64_t ports GNUNET_PACKED;
83   uint32_t service_type GNUNET_PACKED;
84 };
85
86
87 struct answer_packet
88 {
89   /* General data */
90   struct GNUNET_MessageHeader hdr;
91   enum GNUNET_DNS_ANSWER_Subtype subtype GNUNET_PACKED;
92
93   char from[16];
94   char to[16];
95   char addrlen;
96   unsigned dst_port:16 GNUNET_PACKED;
97   /* -- */
98
99   /* Data for GNUNET_DNS_ANSWER_TYPE_SERVICE */
100   struct GNUNET_vpn_service_descriptor service_descr;
101   /* -- */
102
103   /* Data for GNUNET_DNS_ANSWER_TYPE_REV */
104   /* The offsett in octets from the beginning of the struct to the field
105    * in data where the IP-Address has to go. */
106   uint16_t addroffset GNUNET_PACKED;
107   /* -- */
108
109   /* Data for GNUNET_DNS_ANSWER_TYPE_REMOTE */
110   /* either 4 or 16 */
111   char addrsize;
112   unsigned char addr[16];
113   /* -- */
114
115   unsigned char data[1];
116 };
117 GNUNET_NETWORK_STRUCT_END
118
119
120
121 /**
122  * Type of a function to be called by the DNS API whenever
123  * a DNS reply is obtained.
124  *
125  * @param cls closure
126  * @param pkt reply that we got
127  */
128 typedef void (*GNUNET_DNS_ResponseCallback)(void *cls,
129                                             const struct answer_packet *pkt);
130
131
132 /**
133  * Opaque DNS handle
134  */
135 struct GNUNET_DNS_Handle;
136
137
138 /**
139  * Connect to the service-dns
140  *
141  * @param cfg configuration to use
142  * @param cb function to call with DNS replies
143  * @param cb_cls closure to pass to cb
144  * @return DNS handle 
145  */
146 struct GNUNET_DNS_Handle *
147 GNUNET_DNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
148                     GNUNET_DNS_ResponseCallback cb,
149                     void *cb_cls);
150
151
152 /**
153  * Signal the DNS service that it needs to re-initialize the DNS
154  * hijacking (the network setup has changed significantly).
155  *
156  * @param h DNS handle
157  */
158 void
159 GNUNET_DNS_restart_hijack (struct GNUNET_DNS_Handle *h);
160
161
162 /**
163  * Process a DNS request sent to an IPv4 resolver.  Pass it
164  * to the DNS service for resolution.
165  *
166  * @param h DNS handle
167  * @param dst_ip destination IPv4 address
168  * @param src_ip source IPv4 address (usually local machine)
169  * @param src_port source port (to be used for reply)
170  * @param udp_packet_len length of the UDP payload in bytes
171  * @param udp_packet UDP payload
172  */
173 void
174 GNUNET_DNS_queue_request_v4 (struct GNUNET_DNS_Handle *h,
175                              const struct in_addr *dst_ip,
176                              const struct in_addr *src_ip,
177                              uint16_t src_port,
178                              size_t udp_packet_len,
179                              const char *udp_packet);
180
181 /**
182  * Process a DNS request sent to an IPv6 resolver.  Pass it
183  * to the DNS service for resolution.
184  *
185  * @param h DNS handle
186  * @param dst_ip destination IPv6 address
187  * @param src_ip source IPv6 address (usually local machine)
188  * @param src_port source port (to be used for reply)
189  * @param udp_packet_len length of the UDP payload in bytes
190  * @param udp_packet UDP payload
191  */
192 void
193 GNUNET_DNS_queue_request_v6 (struct GNUNET_DNS_Handle *h,
194                              const struct in6_addr *dst_ip,
195                              const struct in6_addr *src_ip,
196                              uint16_t src_port,
197                              size_t udp_packet_len,
198                              const char *udp_packet);
199
200 /**
201  * Disconnect from the DNS service.
202  *
203  * @param h DNS handle
204  */
205 void
206 GNUNET_DNS_disconnect (struct GNUNET_DNS_Handle *h);
207
208 #endif