2f460cadf6fc1c7f2f59619f16fd1565ba8d108f
[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,
106                        "%u %s %s",
107                        (unsigned int) ntohs (vpn->proto),
108                        (const char*) GNUNET_i2s_full (&vpn->peer),
109                        (const char*) &vpn[1]);
110       return vpn_str;
111     }
112   case GNUNET_GNSRECORD_TYPE_BOX:
113     {
114       const struct GNUNET_GNSRECORD_BoxRecord *box;
115       uint32_t rt;
116       char *box_str;
117       char *ival;
118
119       if (data_size < sizeof (struct GNUNET_GNSRECORD_BoxRecord))
120         return NULL; /* malformed */
121       box = data;
122       rt = ntohl (box->record_type);
123       ival = GNUNET_GNSRECORD_value_to_string (rt,
124                                                &box[1],
125                                                data_size - sizeof (struct GNUNET_GNSRECORD_BoxRecord));
126       if (NULL == ival)
127         return NULL; /* malformed */
128       GNUNET_asprintf (&box_str,
129                        "%u %u %u %s",
130                        (unsigned int) ntohs (box->protocol),
131                        (unsigned int) ntohs (box->service),
132                        (unsigned int) rt,
133                        ival);
134       GNUNET_free (ival);
135       return box_str;
136     }
137   default:
138     return NULL;
139   }
140 }
141
142
143 /**
144  * Convert human-readable version of a 'value' of a record to the binary
145  * representation.
146  *
147  * @param cls closure, unused
148  * @param type type of the record
149  * @param s human-readable string
150  * @param data set to value in binary encoding (will be allocated)
151  * @param data_size set to number of bytes in @a data
152  * @return #GNUNET_OK on success
153  */
154 static int
155 gns_string_to_value (void *cls,
156                      uint32_t type,
157                      const char *s,
158                      void **data,
159                      size_t *data_size)
160 {
161   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
162
163   if (NULL == s)
164     return GNUNET_SYSERR;
165   switch (type)
166   {
167
168   case GNUNET_GNSRECORD_TYPE_PKEY:
169     if (GNUNET_OK !=
170         GNUNET_CRYPTO_ecdsa_public_key_from_string (s, strlen (s), &pkey))
171     {
172       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
173            _("Unable to parse PKEY record `%s'\n"),
174            s);
175       return GNUNET_SYSERR;
176     }
177     *data = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
178     memcpy (*data, &pkey, sizeof (pkey));
179     *data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
180     return GNUNET_OK;
181
182   case GNUNET_GNSRECORD_TYPE_NICK:
183     *data = GNUNET_strdup (s);
184     *data_size = strlen (s);
185     return GNUNET_OK;
186   case GNUNET_GNSRECORD_TYPE_LEHO:
187     *data = GNUNET_strdup (s);
188     *data_size = strlen (s);
189     return GNUNET_OK;
190   case GNUNET_GNSRECORD_TYPE_GNS2DNS:
191     {
192       char nsbuf[514];
193       char *cpy;
194       char *at;
195       size_t off;
196
197       cpy = GNUNET_strdup (s);
198       at = strchr (cpy, '@');
199       if (NULL == at)
200       {
201         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202                     _("Unable to parse GNS2DNS record `%s'\n"),
203                     s);
204         GNUNET_free (cpy);
205         return GNUNET_SYSERR;
206       }
207       *at = '\0';
208       at++;
209
210       off = 0;
211       if ( (GNUNET_OK !=
212             GNUNET_DNSPARSER_builder_add_name (nsbuf,
213                                                sizeof (nsbuf),
214                                                &off,
215                                                cpy)) ||
216            (GNUNET_OK !=
217             GNUNET_DNSPARSER_builder_add_name (nsbuf,
218                                                sizeof (nsbuf),
219                                                &off,
220                                                at)) )
221       {
222         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
223                     _("Failed to serialize GNS2DNS record with value `%s'\n"),
224                     s);
225         GNUNET_free (cpy);
226         return GNUNET_SYSERR;
227       }
228       GNUNET_free (cpy);
229       *data_size = off;
230       *data = GNUNET_malloc (off);
231       memcpy (*data, nsbuf, off);
232       return GNUNET_OK;
233     }
234   case GNUNET_GNSRECORD_TYPE_VPN:
235     {
236       struct GNUNET_TUN_GnsVpnRecord *vpn;
237       char s_peer[103 + 1];
238       char s_serv[253 + 1];
239       unsigned int proto;
240
241       if (3 != SSCANF (s,
242                        "%u %103s %253s",
243                        &proto, s_peer, s_serv))
244       {
245         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
246                     _("Unable to parse VPN record string `%s'\n"),
247                     s);
248         return GNUNET_SYSERR;
249       }
250       *data_size = sizeof (struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1;
251       *data = vpn = GNUNET_malloc (*data_size);
252       if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string ((char*) s_peer,
253                                                                    strlen (s_peer),
254                                                                    &vpn->peer.public_key))
255       {
256         GNUNET_free (vpn);
257         *data_size = 0;
258         return GNUNET_SYSERR;
259       }
260       vpn->proto = htons ((uint16_t) proto);
261       strcpy ((char*)&vpn[1], s_serv);
262       return GNUNET_OK;
263     }
264   case GNUNET_GNSRECORD_TYPE_BOX:
265     {
266       struct GNUNET_GNSRECORD_BoxRecord *box;
267       size_t slen = strlen (s) + 1;
268       char rest[slen];
269       unsigned int protocol;
270       unsigned int service;
271       unsigned int record_type;
272       void *bval;
273       size_t bval_size;
274
275       if (4 != SSCANF (s,
276                        "%u %u %u %s",
277                        &protocol,
278                        &service,
279                        &record_type,
280                        rest))
281       {
282         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
283                     _("Unable to parse BOX record string `%s'\n"),
284                     s);
285         return GNUNET_SYSERR;
286       }
287       if (GNUNET_OK !=
288           GNUNET_GNSRECORD_string_to_value (record_type,
289                                             rest,
290                                             &bval,
291                                             &bval_size))
292         return GNUNET_SYSERR;
293       *data_size = sizeof (struct GNUNET_GNSRECORD_BoxRecord) + bval_size;
294       *data = box = GNUNET_malloc (*data_size);
295       box->protocol = htons (protocol);
296       box->service = htons (service);
297       box->record_type = htonl (record_type);
298       memcpy (&box[1],
299               bval,
300               bval_size);
301       GNUNET_free (bval);
302       return GNUNET_OK;
303     }
304   default:
305     return GNUNET_SYSERR;
306   }
307 }
308
309
310 /**
311  * Mapping of record type numbers to human-readable
312  * record type names.
313  */
314 static struct {
315   const char *name;
316   uint32_t number;
317 } gns_name_map[] = {
318   { "PKEY",  GNUNET_GNSRECORD_TYPE_PKEY },
319   { "NICK",  GNUNET_GNSRECORD_TYPE_NICK },
320   { "LEHO",  GNUNET_GNSRECORD_TYPE_LEHO },
321   { "VPN", GNUNET_GNSRECORD_TYPE_VPN },
322   { "GNS2DNS", GNUNET_GNSRECORD_TYPE_GNS2DNS },
323   { "BOX", GNUNET_GNSRECORD_TYPE_BOX },
324   { NULL, UINT32_MAX }
325 };
326
327
328 /**
329  * Convert a type name (i.e. "AAAA") to the corresponding number.
330  *
331  * @param cls closure, unused
332  * @param gns_typename name to convert
333  * @return corresponding number, UINT32_MAX on error
334  */
335 static uint32_t
336 gns_typename_to_number (void *cls,
337                         const char *gns_typename)
338 {
339   unsigned int i;
340
341   i=0;
342   while ( (gns_name_map[i].name != NULL) &&
343           (0 != strcasecmp (gns_typename, gns_name_map[i].name)) )
344     i++;
345   return gns_name_map[i].number;
346 }
347
348
349 /**
350  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
351  *
352  * @param cls closure, unused
353  * @param type number of a type to convert
354  * @return corresponding typestring, NULL on error
355  */
356 static const char *
357 gns_number_to_typename (void *cls,
358                         uint32_t type)
359 {
360   unsigned int i;
361
362   i=0;
363   while ( (gns_name_map[i].name != NULL) &&
364           (type != gns_name_map[i].number) )
365     i++;
366   return gns_name_map[i].name;
367 }
368
369
370 /**
371  * Entry point for the plugin.
372  *
373  * @param cls NULL
374  * @return the exported block API
375  */
376 void *
377 libgnunet_plugin_gnsrecord_gns_init (void *cls)
378 {
379   struct GNUNET_GNSRECORD_PluginFunctions *api;
380
381   api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
382   api->value_to_string = &gns_value_to_string;
383   api->string_to_value = &gns_string_to_value;
384   api->typename_to_number = &gns_typename_to_number;
385   api->number_to_typename = &gns_number_to_typename;
386   return api;
387 }
388
389
390 /**
391  * Exit point from the plugin.
392  *
393  * @param cls the return value from #libgnunet_plugin_block_test_init
394  * @return NULL
395  */
396 void *
397 libgnunet_plugin_gnsrecord_gns_done (void *cls)
398 {
399   struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
400
401   GNUNET_free (api);
402   return NULL;
403 }
404
405 /* end of plugin_gnsrecord_gns.c */