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