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