fix GNS2DNS records for LSD001 UTF-8 change
[oweals/gnunet.git] / src / gns / plugin_gnsrecord_gns.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2013, 2014, 2016 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_gnsrecord_lib.h"
32 #include "gnunet_dnsparser_lib.h"
33 #include "gnunet_gnsrecord_plugin.h"
34 #include <inttypes.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
61   case GNUNET_GNSRECORD_TYPE_NICK:
62     return GNUNET_strndup (data, data_size);
63
64   case GNUNET_GNSRECORD_TYPE_LEHO:
65     return GNUNET_strndup (data, data_size);
66
67   case GNUNET_GNSRECORD_TYPE_GNS2DNS: {
68       char *ns;
69       char *ip;
70       size_t off;
71       char *nstr;
72
73       off = 0;
74       ns = GNUNET_DNSPARSER_parse_name (data, data_size, &off);
75       if (NULL == ns)
76       {
77         GNUNET_break_op (0);
78         GNUNET_free_non_null (ns);
79         return NULL;
80       }
81 #ifndef LSD001 //DNS server IP/name must be UTF-8
82       ip = GNUNET_strdup((char*) &data[off]);
83 #else
84       // Must be IP or DNS name
85       ip = GNUNET_DNSPARSER_parse_name (data, data_size, &off);
86       if ((NULL == ip) || (off != data_size))
87       {
88         GNUNET_break_op (0);
89         GNUNET_free_non_null (ns);
90         GNUNET_free_non_null (ip);
91         return NULL;
92       }
93 #endif
94       GNUNET_asprintf (&nstr, "%s@%s", ns, ip);
95       GNUNET_free_non_null (ns);
96       GNUNET_free_non_null (ip);
97       return nstr;
98     }
99
100   case GNUNET_GNSRECORD_TYPE_VPN: {
101       struct GNUNET_TUN_GnsVpnRecord vpn;
102       char *vpn_str;
103
104       cdata = data;
105       if ((data_size <= sizeof(vpn)) || ('\0' != cdata[data_size - 1]))
106         return NULL; /* malformed */
107       /* need to memcpy for alignment */
108       GNUNET_memcpy (&vpn, data, sizeof(vpn));
109       GNUNET_asprintf (&vpn_str,
110                        "%u %s %s",
111                        (unsigned int) ntohs (vpn.proto),
112                        (const char *) GNUNET_i2s_full (&vpn.peer),
113                        (const char *) &cdata[sizeof(vpn)]);
114       return vpn_str;
115     }
116
117   case GNUNET_GNSRECORD_TYPE_BOX: {
118       struct GNUNET_GNSRECORD_BoxRecord box;
119       uint32_t rt;
120       char *box_str;
121       char *ival;
122
123       cdata = data;
124       if (data_size < sizeof(struct GNUNET_GNSRECORD_BoxRecord))
125         return NULL; /* malformed */
126       GNUNET_memcpy (&box, data, sizeof(box));
127       rt = ntohl (box.record_type);
128       ival = GNUNET_GNSRECORD_value_to_string (rt,
129                                                &cdata[sizeof(box)],
130                                                data_size - sizeof(box));
131       if (NULL == ival)
132         return NULL; /* malformed */
133       GNUNET_asprintf (&box_str,
134                        "%u %u %u %s",
135                        (unsigned int) ntohs (box.protocol),
136                        (unsigned int) ntohs (box.service),
137                        (unsigned int) rt,
138                        ival);
139       GNUNET_free (ival);
140       return box_str;
141     }
142
143   default:
144     return NULL;
145   }
146 }
147
148
149 /**
150  * Convert human-readable version of a 'value' of a record to the binary
151  * representation.
152  *
153  * @param cls closure, unused
154  * @param type type of the record
155  * @param s human-readable string
156  * @param data set to value in binary encoding (will be allocated)
157  * @param data_size set to number of bytes in @a data
158  * @return #GNUNET_OK on success
159  */
160 static int
161 gns_string_to_value (void *cls,
162                      uint32_t type,
163                      const char *s,
164                      void **data,
165                      size_t *data_size)
166 {
167   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
168
169   if (NULL == s)
170     return GNUNET_SYSERR;
171   switch (type)
172   {
173   case GNUNET_GNSRECORD_TYPE_PKEY:
174     if (GNUNET_OK !=
175         GNUNET_CRYPTO_ecdsa_public_key_from_string (s, strlen (s), &pkey))
176     {
177       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
178                   _ ("Unable to parse PKEY record `%s'\n"),
179                   s);
180       return GNUNET_SYSERR;
181     }
182     *data = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
183     GNUNET_memcpy (*data, &pkey, sizeof(pkey));
184     *data_size = sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey);
185     return GNUNET_OK;
186
187   case GNUNET_GNSRECORD_TYPE_NICK:
188     *data = GNUNET_strdup (s);
189     *data_size = strlen (s);
190     return GNUNET_OK;
191
192   case GNUNET_GNSRECORD_TYPE_LEHO:
193     *data = GNUNET_strdup (s);
194     *data_size = strlen (s);
195     return GNUNET_OK;
196
197   case GNUNET_GNSRECORD_TYPE_GNS2DNS: {
198       char nsbuf[514];
199       char *cpy;
200       char *at;
201       size_t off;
202
203       cpy = GNUNET_strdup (s);
204       at = strchr (cpy, '@');
205       if (NULL == at)
206       {
207         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
208                     _ ("Unable to parse GNS2DNS record `%s'\n"),
209                     s);
210         GNUNET_free (cpy);
211         return GNUNET_SYSERR;
212       }
213       *at = '\0';
214       at++;
215
216       off = 0;
217       if (GNUNET_OK != GNUNET_DNSPARSER_builder_add_name (nsbuf,
218                                                            sizeof(nsbuf),
219                                                            &off,
220                                                            cpy))
221       {
222         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
223                     _ (
224                       "Failed to serialize GNS2DNS record with value `%s': Not a DNS name.\n"),
225                     s);
226         GNUNET_free (cpy);
227         return GNUNET_SYSERR;
228       }
229 #ifndef LSD001 //The DNS server location/name is in UTF-8
230       GNUNET_memcpy (&nsbuf[off], at, strlen (at) + 1);
231       off += strlen (at) + 1;
232 #else
233       if (GNUNET_OK !=
234           GNUNET_DNSPARSER_builder_add_name (nsbuf, sizeof(nsbuf), &off, at))
235       {
236         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237                     _ (
238                       "Failed to serialize GNS2DNS record with value `%s': Not a DNS name\n"),
239                     s);
240         GNUNET_free (cpy);
241         return GNUNET_SYSERR;
242       }
243 #endif
244       GNUNET_free (cpy);
245       *data_size = off;
246       *data = GNUNET_malloc (off);
247       GNUNET_memcpy (*data, nsbuf, off);
248       return GNUNET_OK;
249     }
250
251   case GNUNET_GNSRECORD_TYPE_VPN: {
252       struct GNUNET_TUN_GnsVpnRecord *vpn;
253       char s_peer[103 + 1];
254       char s_serv[253 + 1];
255       unsigned int proto;
256
257       if (3 != sscanf (s, "%u %103s %253s", &proto, s_peer, s_serv))
258       {
259         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260                     _ ("Unable to parse VPN record string `%s'\n"),
261                     s);
262         return GNUNET_SYSERR;
263       }
264       *data_size = sizeof(struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1;
265       *data = vpn = GNUNET_malloc (*data_size);
266       if (GNUNET_OK !=
267           GNUNET_CRYPTO_eddsa_public_key_from_string ((char *) s_peer,
268                                                       strlen (s_peer),
269                                                       &vpn->peer.public_key))
270       {
271         GNUNET_free (vpn);
272         *data_size = 0;
273         return GNUNET_SYSERR;
274       }
275       vpn->proto = htons ((uint16_t) proto);
276       strcpy ((char *) &vpn[1], s_serv);
277       return GNUNET_OK;
278     }
279
280   case GNUNET_GNSRECORD_TYPE_BOX: {
281       struct GNUNET_GNSRECORD_BoxRecord *box;
282       size_t rest;
283       unsigned int protocol;
284       unsigned int service;
285       unsigned int record_type;
286       void *bval;
287       size_t bval_size;
288
289       if (3 != sscanf (s, "%u %u %u ", &protocol, &service, &record_type))
290       {
291         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
292                     _ ("Unable to parse BOX record string `%s'\n"),
293                     s);
294         return GNUNET_SYSERR;
295       }
296       rest = snprintf (NULL, 0, "%u %u %u ", protocol, service, record_type);
297       if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value (record_type,
298                                                          &s[rest],
299                                                          &bval,
300                                                          &bval_size))
301         return GNUNET_SYSERR;
302       *data_size = sizeof(struct GNUNET_GNSRECORD_BoxRecord) + bval_size;
303       *data = box = GNUNET_malloc (*data_size);
304       box->protocol = htons (protocol);
305       box->service = htons (service);
306       box->record_type = htonl (record_type);
307       GNUNET_memcpy (&box[1], bval, bval_size);
308       GNUNET_free (bval);
309       return GNUNET_OK;
310     }
311
312   default:
313     return GNUNET_SYSERR;
314   }
315 }
316
317
318 /**
319  * Mapping of record type numbers to human-readable
320  * record type names.
321  */
322 static struct
323 {
324   const char *name;
325   uint32_t number;
326 } gns_name_map[] = { { "PKEY", GNUNET_GNSRECORD_TYPE_PKEY },
327                      { "NICK", GNUNET_GNSRECORD_TYPE_NICK },
328                      { "LEHO", GNUNET_GNSRECORD_TYPE_LEHO },
329                      { "VPN", GNUNET_GNSRECORD_TYPE_VPN },
330                      { "GNS2DNS", GNUNET_GNSRECORD_TYPE_GNS2DNS },
331                      { "BOX", GNUNET_GNSRECORD_TYPE_BOX },
332                      { NULL, UINT32_MAX } };
333
334
335 /**
336  * Convert a type name (i.e. "AAAA") to the corresponding number.
337  *
338  * @param cls closure, unused
339  * @param gns_typename name to convert
340  * @return corresponding number, UINT32_MAX on error
341  */
342 static uint32_t
343 gns_typename_to_number (void *cls, const char *gns_typename)
344 {
345   unsigned int i;
346
347   i = 0;
348   while ((NULL != gns_name_map[i].name) &&
349          (0 != strcasecmp (gns_typename, gns_name_map[i].name)))
350     i++;
351   return gns_name_map[i].number;
352 }
353
354
355 /**
356  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
357  *
358  * @param cls closure, unused
359  * @param type number of a type to convert
360  * @return corresponding typestring, NULL on error
361  */
362 static const char *
363 gns_number_to_typename (void *cls, uint32_t type)
364 {
365   unsigned int i;
366
367   i = 0;
368   while ((NULL != gns_name_map[i].name) && (type != gns_name_map[i].number))
369     i++;
370   return gns_name_map[i].name;
371 }
372
373
374 /**
375  * Entry point for the plugin.
376  *
377  * @param cls NULL
378  * @return the exported block API
379  */
380 void *
381 libgnunet_plugin_gnsrecord_gns_init (void *cls)
382 {
383   struct GNUNET_GNSRECORD_PluginFunctions *api;
384
385   api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
386   api->value_to_string = &gns_value_to_string;
387   api->string_to_value = &gns_string_to_value;
388   api->typename_to_number = &gns_typename_to_number;
389   api->number_to_typename = &gns_number_to_typename;
390   return api;
391 }
392
393
394 /**
395  * Exit point from the plugin.
396  *
397  * @param cls the return value from #libgnunet_plugin_block_test_init()
398  * @return NULL
399  */
400 void *
401 libgnunet_plugin_gnsrecord_gns_done (void *cls)
402 {
403   struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
404
405   GNUNET_free (api);
406   return NULL;
407 }
408
409
410 /* end of plugin_gnsrecord_gns.c */