adding extended proxy support for http(s) client
[oweals/gnunet.git] / src / gns / plugin_gnsrecord_gns.c
1 /*
2      This file is part of GNUnet
3      (C) 2013 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 3, 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 gns/plugin_gnsrecord_gns.c
23  * @brief gnsrecord plugin to provide the API for fundamental GNS records
24  *                  This includes the VPN record because GNS resolution
25  *                  is expected to understand VPN records and (if needed)
26  *                  map the result to A/AAAA.
27  * @author Christian Grothoff
28  */
29
30 #include "platform.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_gnsrecord_lib.h"
33 #include "gnunet_dnsparser_lib.h"
34 #include "gnunet_gnsrecord_plugin.h"
35
36
37 /**
38  * Convert the 'value' of a record to a string.
39  *
40  * @param cls closure, unused
41  * @param type type of the record
42  * @param data value in binary encoding
43  * @param data_size number of bytes in @a data
44  * @return NULL on error, otherwise human-readable representation of the value
45  */
46 static char *
47 gns_value_to_string (void *cls,
48                      uint32_t type,
49                      const void *data,
50                      size_t data_size)
51 {
52   const char *cdata;
53
54   switch (type)
55   {
56   case GNUNET_GNSRECORD_TYPE_PKEY:
57     if (data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
58       return NULL;
59     return GNUNET_CRYPTO_ecdsa_public_key_to_string (data);
60   case GNUNET_GNSRECORD_TYPE_NICK:
61     return GNUNET_strndup (data, data_size);
62   case GNUNET_GNSRECORD_TYPE_LEHO:
63     return GNUNET_strndup (data, data_size);
64   case GNUNET_GNSRECORD_TYPE_GNS2DNS:
65     {
66       char *ns;
67       char *ip;
68       size_t off;
69       char *nstr;
70
71       off = 0;
72       ns = GNUNET_DNSPARSER_parse_name (data,
73                                         data_size,
74                                         &off);
75       ip = GNUNET_DNSPARSER_parse_name (data,
76                                         data_size,
77                                         &off);
78       if ( (NULL == ns) ||
79            (NULL == ip) ||
80            (off != data_size) )
81       {
82         GNUNET_break_op (0);
83         GNUNET_free_non_null (ns);
84         GNUNET_free_non_null (ip);
85         return NULL;
86       }
87       GNUNET_asprintf (&nstr,
88                        "%s@%s",
89                        ns,
90                        ip);
91       GNUNET_free_non_null (ns);
92       GNUNET_free_non_null (ip);
93       return nstr;
94     }
95   case GNUNET_GNSRECORD_TYPE_VPN:
96     {
97       const struct GNUNET_TUN_GnsVpnRecord *vpn;
98       char* vpn_str;
99
100       cdata = data;
101       if ( (data_size <= sizeof (struct GNUNET_TUN_GnsVpnRecord)) ||
102            ('\0' != cdata[data_size - 1]) )
103         return NULL; /* malformed */
104       vpn = data;
105       GNUNET_asprintf (&vpn_str, "%u %s %s",
106                        (unsigned int) ntohs (vpn->proto),
107                        (const char*) GNUNET_i2s_full (&vpn->peer),
108                        (const char*) &vpn[1]);
109       return vpn_str;
110     }
111   default:
112     return NULL;
113   }
114 }
115
116
117 /**
118  * Convert human-readable version of a 'value' of a record to the binary
119  * representation.
120  *
121  * @param cls closure, unused
122  * @param type type of the record
123  * @param s human-readable string
124  * @param data set to value in binary encoding (will be allocated)
125  * @param data_size set to number of bytes in @a data
126  * @return #GNUNET_OK on success
127  */
128 static int
129 gns_string_to_value (void *cls,
130                      uint32_t type,
131                      const char *s,
132                      void **data,
133                      size_t *data_size)
134 {
135   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
136   struct GNUNET_TUN_GnsVpnRecord *vpn;
137   char s_peer[103 + 1];
138   char s_serv[253 + 1];
139   unsigned int proto;
140
141   if (NULL == s)
142     return GNUNET_SYSERR;
143   switch (type)
144   {
145
146   case GNUNET_GNSRECORD_TYPE_PKEY:
147     if (GNUNET_OK !=
148         GNUNET_CRYPTO_ecdsa_public_key_from_string (s, strlen (s), &pkey))
149     {
150       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
151            _("Unable to parse PKEY record `%s'\n"),
152            s);
153       return GNUNET_SYSERR;
154     }
155     *data = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
156     memcpy (*data, &pkey, sizeof (pkey));
157     *data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
158     return GNUNET_OK;
159
160   case GNUNET_GNSRECORD_TYPE_NICK:
161     *data = GNUNET_strdup (s);
162     *data_size = strlen (s);
163     return GNUNET_OK;
164   case GNUNET_GNSRECORD_TYPE_LEHO:
165     *data = GNUNET_strdup (s);
166     *data_size = strlen (s);
167     return GNUNET_OK;
168   case GNUNET_GNSRECORD_TYPE_GNS2DNS:
169     {
170       char nsbuf[514];
171       char *cpy;
172       char *at;
173       size_t off;
174
175       cpy = GNUNET_strdup (s);
176       at = strchr (cpy, '@');
177       if (NULL == at)
178       {
179         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
180                     _("Unable to parse GNS2DNS record `%s'\n"),
181                     s);
182         GNUNET_free (cpy);
183         return GNUNET_SYSERR;
184       }
185       *at = '\0';
186       at++;
187
188       off = 0;
189       if ( (GNUNET_OK !=
190             GNUNET_DNSPARSER_builder_add_name (nsbuf,
191                                                sizeof (nsbuf),
192                                                &off,
193                                                cpy)) ||
194            (GNUNET_OK !=
195             GNUNET_DNSPARSER_builder_add_name (nsbuf,
196                                                sizeof (nsbuf),
197                                                &off,
198                                                at)) )
199       {
200         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
201                     _("Failed to serialize GNS2DNS record with value `%s'\n"),
202                     s);
203         GNUNET_free (cpy);
204         return GNUNET_SYSERR;
205       }
206       GNUNET_free (cpy);
207       *data_size = off;
208       *data = GNUNET_malloc (off);
209       memcpy (*data, nsbuf, off);
210       return GNUNET_OK;
211     }
212   case GNUNET_GNSRECORD_TYPE_VPN:
213     if (3 != SSCANF (s,"%u %103s %253s",
214                      &proto, s_peer, s_serv))
215     {
216       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
217            _("Unable to parse VPN record string `%s'\n"),
218            s);
219       return GNUNET_SYSERR;
220     }
221     *data_size = sizeof (struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1;
222     *data = vpn = GNUNET_malloc (*data_size);
223     if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string ((char*) s_peer,
224                                                                     strlen (s_peer),
225                                                                     &vpn->peer.public_key))
226     {
227       GNUNET_free (vpn);
228       *data_size = 0;
229       return GNUNET_SYSERR;
230     }
231     vpn->proto = htons ((uint16_t) proto);
232     strcpy ((char*)&vpn[1], s_serv);
233     return GNUNET_OK;
234   default:
235     return GNUNET_SYSERR;
236   }
237 }
238
239
240 /**
241  * Mapping of record type numbers to human-readable
242  * record type names.
243  */
244 static struct {
245   const char *name;
246   uint32_t number;
247 } gns_name_map[] = {
248   { "PKEY",  GNUNET_GNSRECORD_TYPE_PKEY },
249   { "NICK",  GNUNET_GNSRECORD_TYPE_NICK },
250   { "LEHO",  GNUNET_GNSRECORD_TYPE_LEHO },
251   { "VPN", GNUNET_GNSRECORD_TYPE_VPN },
252   { "GNS2DNS", GNUNET_GNSRECORD_TYPE_GNS2DNS },
253   { NULL, UINT32_MAX }
254 };
255
256
257 /**
258  * Convert a type name (i.e. "AAAA") to the corresponding number.
259  *
260  * @param cls closure, unused
261  * @param gns_typename name to convert
262  * @return corresponding number, UINT32_MAX on error
263  */
264 static uint32_t
265 gns_typename_to_number (void *cls,
266                         const char *gns_typename)
267 {
268   unsigned int i;
269
270   i=0;
271   while ( (gns_name_map[i].name != NULL) &&
272           (0 != strcasecmp (gns_typename, gns_name_map[i].name)) )
273     i++;
274   return gns_name_map[i].number;
275 }
276
277
278 /**
279  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
280  *
281  * @param cls closure, unused
282  * @param type number of a type to convert
283  * @return corresponding typestring, NULL on error
284  */
285 static const char *
286 gns_number_to_typename (void *cls,
287                         uint32_t type)
288 {
289   unsigned int i;
290
291   i=0;
292   while ( (gns_name_map[i].name != NULL) &&
293           (type != gns_name_map[i].number) )
294     i++;
295   return gns_name_map[i].name;
296 }
297
298
299 /**
300  * Entry point for the plugin.
301  *
302  * @param cls NULL
303  * @return the exported block API
304  */
305 void *
306 libgnunet_plugin_gnsrecord_gns_init (void *cls)
307 {
308   struct GNUNET_GNSRECORD_PluginFunctions *api;
309
310   api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
311   api->value_to_string = &gns_value_to_string;
312   api->string_to_value = &gns_string_to_value;
313   api->typename_to_number = &gns_typename_to_number;
314   api->number_to_typename = &gns_number_to_typename;
315   return api;
316 }
317
318
319 /**
320  * Exit point from the plugin.
321  *
322  * @param cls the return value from #libgnunet_plugin_block_test_init
323  * @return NULL
324  */
325 void *
326 libgnunet_plugin_gnsrecord_gns_done (void *cls)
327 {
328   struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
329
330   GNUNET_free (api);
331   return NULL;
332 }
333
334 /* end of plugin_gnsrecord_gns.c */