RECLAIM: Start move to GNS encryption
[oweals/gnunet.git] / src / reclaim / plugin_gnsrecord_reclaim.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2013, 2014 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 reclaim/plugin_gnsrecord_reclaim.c
23  * @brief gnsrecord plugin to provide the API for identity records
24  * @author Martin Schanzenbach
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_gnsrecord_lib.h"
29 #include "gnunet_gnsrecord_plugin.h"
30
31
32 /**
33  * Convert the 'value' of a record to a string.
34  *
35  * @param cls closure, unused
36  * @param type type of the record
37  * @param data value in binary encoding
38  * @param data_size number of bytes in @a data
39  * @return NULL on error, otherwise human-readable representation of the value
40  */
41 static char *
42 value_to_string (void *cls,
43                  uint32_t type,
44                  const void *data,
45                  size_t data_size)
46 {
47   switch (type)
48   {
49     case GNUNET_GNSRECORD_TYPE_RECLAIM_ATTR:
50       return GNUNET_STRINGS_data_to_string_alloc (data, data_size);
51     case GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT:
52     case GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_CLIENT:
53       return GNUNET_strndup (data, data_size);
54     case GNUNET_GNSRECORD_TYPE_RECLAIM_AUTHZ:
55     case GNUNET_GNSRECORD_TYPE_RECLAIM_MASTER:
56       return GNUNET_STRINGS_data_to_string_alloc (data, data_size);
57     default:
58       return NULL;
59   }
60 }
61
62
63 /**
64  * Convert human-readable version of a 'value' of a record to the binary
65  * representation.
66  *
67  * @param cls closure, unused
68  * @param type type of the record
69  * @param s human-readable string
70  * @param data set to value in binary encoding (will be allocated)
71  * @param data_size set to number of bytes in @a data
72  * @return #GNUNET_OK on success
73  */
74 static int
75 string_to_value (void *cls,
76                  uint32_t type,
77                  const char *s,
78                  void **data,
79                  size_t *data_size)
80 {
81   if (NULL == s)
82     return GNUNET_SYSERR;
83   switch (type)
84   {
85     case GNUNET_GNSRECORD_TYPE_RECLAIM_ATTR:
86       return GNUNET_STRINGS_string_to_data (s,
87                                             strlen (s),
88                                             *data,
89                                             *data_size);
90     case GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT:
91     case GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_CLIENT:
92       *data = GNUNET_strdup (s);
93       *data_size = strlen (s);
94       return GNUNET_OK;
95     case GNUNET_GNSRECORD_TYPE_RECLAIM_AUTHZ:
96     case GNUNET_GNSRECORD_TYPE_RECLAIM_MASTER:
97       return GNUNET_STRINGS_string_to_data (s,
98                                             strlen (s),
99                                             *data,
100                                             *data_size);
101     default:
102       return GNUNET_SYSERR;
103   }
104 }
105
106
107 /**
108  * Mapping of record type numbers to human-readable
109  * record type names.
110  */
111 static struct {
112   const char *name;
113   uint32_t number;
114 } name_map[] = {
115   { "RECLAIM_ATTR", GNUNET_GNSRECORD_TYPE_RECLAIM_ATTR },
116   { "RECLAIM_AUTHZ", GNUNET_GNSRECORD_TYPE_RECLAIM_AUTHZ },
117   { "RECLAIM_MASTER", GNUNET_GNSRECORD_TYPE_RECLAIM_MASTER },
118   { "RECLAIM_OIDC_CLIENT", GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_CLIENT },
119   { "RECLAIM_OIDC_REDIRECT", GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT },
120   { NULL, UINT32_MAX }
121 };
122
123
124 /**
125  * Convert a type name (i.e. "AAAA") to the corresponding number.
126  *
127  * @param cls closure, unused
128  * @param dns_typename name to convert
129  * @return corresponding number, UINT32_MAX on error
130  */
131 static uint32_t
132 typename_to_number (void *cls,
133                     const char *dns_typename)
134 {
135   unsigned int i;
136
137   i=0;
138   while ( (NULL != name_map[i].name) &&
139           (0 != strcasecmp (dns_typename, name_map[i].name)) )
140     i++;
141   return name_map[i].number;
142 }
143
144
145 /**
146  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
147  *
148  * @param cls closure, unused
149  * @param type number of a type to convert
150  * @return corresponding typestring, NULL on error
151  */
152 static const char *
153 number_to_typename (void *cls,
154                     uint32_t type)
155 {
156   unsigned int i;
157
158   i=0;
159   while ( (NULL != name_map[i].name) &&
160           (type != name_map[i].number) )
161     i++;
162   return name_map[i].name;
163 }
164
165
166 /**
167  * Entry point for the plugin.
168  *
169  * @param cls NULL
170  * @return the exported block API
171  */
172 void *
173 libgnunet_plugin_gnsrecord_reclaim_init (void *cls)
174 {
175   struct GNUNET_GNSRECORD_PluginFunctions *api;
176
177   api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
178   api->value_to_string = &value_to_string;
179   api->string_to_value = &string_to_value;
180   api->typename_to_number = &typename_to_number;
181   api->number_to_typename = &number_to_typename;
182   return api;
183 }
184
185
186 /**
187  * Exit point from the plugin.
188  *
189  * @param cls the return value from #libgnunet_plugin_block_test_init
190  * @return NULL
191  */
192 void *
193 libgnunet_plugin_gnsrecord_reclaim_done (void *cls)
194 {
195   struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
196
197   GNUNET_free (api);
198   return NULL;
199 }
200
201 /* end of plugin_gnsrecord_dns.c */