fix indentation, typo, improve logging
[oweals/gnunet.git] / src / conversation / plugin_gnsrecord_conversation.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file conversation/plugin_gnsrecord_conversation.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_conversation_service.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 conversation_value_to_string (void *cls,
48                               uint32_t type,
49                               const void *data,
50                               size_t data_size)
51 {
52   char *s;
53
54   switch (type)
55   {
56   case GNUNET_GNSRECORD_TYPE_PHONE:
57     {
58       const struct GNUNET_CONVERSATION_PhoneRecord *pr;
59       char *ret;
60       char *pkey;
61
62       if (data_size != sizeof (struct GNUNET_CONVERSATION_PhoneRecord))
63       {
64         GNUNET_break_op (0);
65         return NULL;
66       }
67       pr = data;
68       if (1 != ntohl (pr->version))
69       {
70         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
71                     _("PHONE version %u not supported\n"),
72                     ntohl (pr->version));
73         return NULL;
74       }
75       pkey = GNUNET_CRYPTO_eddsa_public_key_to_string (&pr->peer.public_key);
76       s = GNUNET_STRINGS_data_to_string_alloc (&pr->line_port,
77                                                sizeof (struct GNUNET_HashCode));
78
79       GNUNET_asprintf (&ret,
80                        "%s-%s",
81                        s,
82                        pkey);
83       GNUNET_free (s);
84       GNUNET_free (pkey);
85       return ret;
86     }
87   default:
88     return NULL;
89   }
90 }
91
92
93 /**
94  * Convert human-readable version of a 'value' of a record to the binary
95  * representation.
96  *
97  * @param cls closure, unused
98  * @param type type of the record
99  * @param s human-readable string
100  * @param data set to value in binary encoding (will be allocated)
101  * @param data_size set to number of bytes in @a data
102  * @return #GNUNET_OK on success
103  */
104 static int
105 conversation_string_to_value (void *cls,
106                               uint32_t type,
107                               const char *s,
108                               void **data,
109                               size_t *data_size)
110 {
111   if (NULL == s)
112   {
113     GNUNET_break (0);
114     return GNUNET_SYSERR;
115   }
116   switch (type)
117   {
118   case GNUNET_GNSRECORD_TYPE_PHONE:
119     {
120       struct GNUNET_CONVERSATION_PhoneRecord *pr;
121       char line_port[103];
122       const char *dash;
123       struct GNUNET_PeerIdentity peer;
124
125       if ( (NULL == (dash = strchr (s, '-'))) ||
126            (1 != sscanf (s, "%103s-", line_port)) ||
127            (GNUNET_OK !=
128             GNUNET_CRYPTO_eddsa_public_key_from_string (dash + 1,
129                                                         strlen (dash + 1),
130                                                         &peer.public_key)) )
131       {
132         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
133                     _("Unable to parse PHONE record `%s'\n"),
134                     s);
135         return GNUNET_SYSERR;
136       }
137       pr = GNUNET_new (struct GNUNET_CONVERSATION_PhoneRecord);
138       pr->version = htonl (1);
139       pr->reserved = htonl (0);
140       if (GNUNET_OK !=
141           GNUNET_STRINGS_string_to_data (line_port,
142                                          strlen (line_port),
143                                          &pr->line_port,
144                                          sizeof (struct GNUNET_HashCode)))
145       {
146         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
147                     _("Unable to parse PHONE record `%s'\n"),
148                     s);
149         GNUNET_free (pr);
150         return GNUNET_SYSERR;
151       }
152       pr->peer = peer;
153       *data = pr;
154       *data_size = sizeof (struct GNUNET_CONVERSATION_PhoneRecord);
155       return GNUNET_OK;
156     }
157   default:
158     return GNUNET_SYSERR;
159   }
160 }
161
162
163 /**
164  * Mapping of record type numbers to human-readable
165  * record type names.
166  */
167 static struct {
168   const char *name;
169   uint32_t number;
170 } name_map[] = {
171   { "PHONE",  GNUNET_GNSRECORD_TYPE_PHONE },
172   { NULL, UINT32_MAX }
173 };
174
175
176 /**
177  * Convert a type name (i.e. "AAAA") to the corresponding number.
178  *
179  * @param cls closure, unused
180  * @param gns_typename name to convert
181  * @return corresponding number, UINT32_MAX on error
182  */
183 static uint32_t
184 conversation_typename_to_number (void *cls,
185                                  const char *gns_typename)
186 {
187   unsigned int i;
188
189   i=0;
190   while ( (name_map[i].name != NULL) &&
191           (0 != strcasecmp (gns_typename, name_map[i].name)) )
192     i++;
193   return name_map[i].number;
194 }
195
196
197 /**
198  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
199  *
200  * @param cls closure, unused
201  * @param type number of a type to convert
202  * @return corresponding typestring, NULL on error
203  */
204 static const char *
205 conversation_number_to_typename (void *cls,
206                                  uint32_t type)
207 {
208   unsigned int i;
209
210   i=0;
211   while ( (name_map[i].name != NULL) &&
212           (type != name_map[i].number) )
213     i++;
214   return name_map[i].name;
215 }
216
217
218 /**
219  * Entry point for the plugin.
220  *
221  * @param cls NULL
222  * @return the exported block API
223  */
224 void *
225 libgnunet_plugin_gnsrecord_conversation_init (void *cls)
226 {
227   struct GNUNET_GNSRECORD_PluginFunctions *api;
228
229   api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
230   api->value_to_string = &conversation_value_to_string;
231   api->string_to_value = &conversation_string_to_value;
232   api->typename_to_number = &conversation_typename_to_number;
233   api->number_to_typename = &conversation_number_to_typename;
234   return api;
235 }
236
237
238 /**
239  * Exit point from the plugin.
240  *
241  * @param cls the return value from #libgnunet_plugin_block_test_init
242  * @return NULL
243  */
244 void *
245 libgnunet_plugin_gnsrecord_conversation_done (void *cls)
246 {
247   struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
248
249   GNUNET_free (api);
250   return NULL;
251 }
252
253 /* end of plugin_gnsrecord_conversation.c */