diff options
Diffstat (limited to 'src/dhtu/plugin_dhtu_gnunet.c')
-rw-r--r-- | src/dhtu/plugin_dhtu_gnunet.c | 285 |
1 files changed, 285 insertions, 0 deletions
diff --git a/src/dhtu/plugin_dhtu_gnunet.c b/src/dhtu/plugin_dhtu_gnunet.c new file mode 100644 index 000000000..d6cd75242 --- /dev/null +++ b/src/dhtu/plugin_dhtu_gnunet.c | |||
@@ -0,0 +1,285 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2021 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 | * @author Christian Grothoff | ||
23 | * | ||
24 | * @file plugin_dhtu_ip.c | ||
25 | * @brief plain IP based DHT network underlay | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #incluce "gnunet_dhtu_plugin.h" | ||
29 | |||
30 | /** | ||
31 | * Opaque handle that the underlay offers for our address to be used when | ||
32 | * sending messages to another peer. | ||
33 | */ | ||
34 | struct GNUNET_DHTU_Source | ||
35 | { | ||
36 | |||
37 | /** | ||
38 | * Application context for this source. | ||
39 | */ | ||
40 | void *app_ctx; | ||
41 | }; | ||
42 | |||
43 | |||
44 | /** | ||
45 | * Opaque handle that the underlay offers for the target peer when sending | ||
46 | * messages to another peer. | ||
47 | */ | ||
48 | struct GNUNET_DHTU_Target | ||
49 | { | ||
50 | |||
51 | /** | ||
52 | * Application context for this target. | ||
53 | */ | ||
54 | void *app_ctx; | ||
55 | |||
56 | /** | ||
57 | * Head of preferences expressed for this target. | ||
58 | */ | ||
59 | struct GNUNET_DHTU_PreferenceHandle *ph_head; | ||
60 | |||
61 | /** | ||
62 | * Tail of preferences expressed for this target. | ||
63 | */ | ||
64 | struct GNUNET_DHTU_PreferenceHandle *ph_tail; | ||
65 | |||
66 | /** | ||
67 | * Preference counter, length of the @a ph_head DLL. | ||
68 | */ | ||
69 | unsigned int ph_count; | ||
70 | |||
71 | }; | ||
72 | |||
73 | /** | ||
74 | * Opaque handle expressing a preference of the DHT to | ||
75 | * keep a particular target connected. | ||
76 | */ | ||
77 | struct GNUNET_DHTU_PreferenceHandle | ||
78 | { | ||
79 | /** | ||
80 | * Kept in a DLL. | ||
81 | */ | ||
82 | struct GNUNET_DHTU_PreferenceHandle *next; | ||
83 | |||
84 | /** | ||
85 | * Kept in a DLL. | ||
86 | */ | ||
87 | struct GNUNET_DHTU_PreferenceHandle *prev; | ||
88 | |||
89 | /** | ||
90 | * Target a preference was expressed for. | ||
91 | */ | ||
92 | struct GNUNET_DHTU_Target *target; | ||
93 | }; | ||
94 | |||
95 | |||
96 | /** | ||
97 | * Opaque handle for a private key used by this underlay. | ||
98 | */ | ||
99 | struct GNUNET_DHTU_PrivateKey | ||
100 | { | ||
101 | /* we are IP, we do not do crypto */ | ||
102 | }; | ||
103 | |||
104 | |||
105 | /** | ||
106 | * Closure for all plugin functions. | ||
107 | */ | ||
108 | struct Plugin | ||
109 | { | ||
110 | /** | ||
111 | * Callbacks into the DHT. | ||
112 | */ | ||
113 | struct GNUNET_DHTU_PluginEnvironment *env; | ||
114 | }; | ||
115 | |||
116 | |||
117 | /** | ||
118 | * Use our private key to sign a message. | ||
119 | * | ||
120 | * @param cls closure | ||
121 | * @param pk our private key to sign with | ||
122 | * @param purpose what to sign | ||
123 | * @param[out] signature, allocated on heap and returned | ||
124 | * @return -1 on error, otherwise number of bytes in @a sig | ||
125 | */ | ||
126 | static ssize_t | ||
127 | ip_sign (void *cls, | ||
128 | const struct GNUNET_DHTU_PrivateKey *pk, | ||
129 | const struct GNUNET_DHTU_SignaturePurpose *purpose, | ||
130 | void **sig) | ||
131 | { | ||
132 | return 0; | ||
133 | } | ||
134 | |||
135 | |||
136 | /** | ||
137 | * Verify signature in @a sig over @a purpose. | ||
138 | * | ||
139 | * @param cls closure | ||
140 | * @param pk public key to verify signature of | ||
141 | * @param purpose what was being signed | ||
142 | * @param sig signature data | ||
143 | * @param sig_size number of bytes in @a sig | ||
144 | * @return #GNUNET_OK if signature is valid | ||
145 | * #GNUNET_NO if signatures are not supported | ||
146 | * #GNUNET_SYSERR if signature is invalid | ||
147 | */ | ||
148 | static enum GNUNET_GenericReturnValue | ||
149 | ip_verify (void *cls, | ||
150 | const struct GNUNET_DHTU_PublicKey *pk, | ||
151 | const struct GNUNET_DHTU_SignaturePurpose *purpose, | ||
152 | const void *sig, | ||
153 | size_t sig_size) | ||
154 | { | ||
155 | return GNUNET_NO; | ||
156 | } | ||
157 | |||
158 | |||
159 | /** | ||
160 | * Request creation of a session with a peer at the given @a address. | ||
161 | * | ||
162 | * @param cls closure (internal context for the plugin) | ||
163 | * @param address target address to connect to | ||
164 | */ | ||
165 | static void | ||
166 | ip_try_connect (void *cls, | ||
167 | const char *address) | ||
168 | { | ||
169 | GNUNET_break (0); | ||
170 | } | ||
171 | |||
172 | |||
173 | /** | ||
174 | * Request underlay to keep the connection to @a target alive if possible. | ||
175 | * Hold may be called multiple times to express a strong preference to | ||
176 | * keep a connection, say because a @a target is in multiple tables. | ||
177 | * | ||
178 | * @param cls closure | ||
179 | * @param target connection to keep alive | ||
180 | */ | ||
181 | static struct GNUNET_DHTU_PreferenceHandle * | ||
182 | ip_hold (void *cls, | ||
183 | struct GNUNET_DHTU_Target *target) | ||
184 | { | ||
185 | struct GNUNET_DHTU_PreferenceHandle *ph; | ||
186 | |||
187 | ph = GNUNET_new (struct GNUNET_DHTU_PreferenceHandle); | ||
188 | ph->target = target; | ||
189 | GNUNET_CONTAINER_DLL_insert (target->ph_head, | ||
190 | target->ph_tail, | ||
191 | ph); | ||
192 | target->ph_count++; | ||
193 | return ph; | ||
194 | } | ||
195 | |||
196 | |||
197 | /** | ||
198 | * Do no long request underlay to keep the connection alive. | ||
199 | * | ||
200 | * @param cls closure | ||
201 | * @param target connection to keep alive | ||
202 | */ | ||
203 | static void | ||
204 | ip_drop (struct GNUNET_DHTU_PreferenceHandle *ph) | ||
205 | { | ||
206 | struct GNUNET_DHTU_Target *target = ph->target; | ||
207 | |||
208 | GNUNET_CONTAINER_DLL_remove (target->ph_head, | ||
209 | target->ph_tail, | ||
210 | ph); | ||
211 | target->ph_count--; | ||
212 | GNUNET_free (ph); | ||
213 | } | ||
214 | |||
215 | |||
216 | /** | ||
217 | * Send message to some other participant over the network. Note that | ||
218 | * sending is not guaranteeing that the other peer actually received the | ||
219 | * message. For any given @a target, the DHT must wait for the @a | ||
220 | * finished_cb to be called before calling send() again. | ||
221 | * | ||
222 | * @param cls closure (internal context for the plugin) | ||
223 | * @param target receiver identification | ||
224 | * @param msg message | ||
225 | * @param msg_size number of bytes in @a msg | ||
226 | * @param finished_cb function called once transmission is done | ||
227 | * (not called if @a target disconnects, then only the | ||
228 | * disconnect_cb is called). | ||
229 | * @param finished_cb_cls closure for @a finished_cb | ||
230 | */ | ||
231 | static void | ||
232 | ip_send (void *cls, | ||
233 | struct GNUNET_DHTU_Target *target, | ||
234 | const void *msg, | ||
235 | size_t msg_size, | ||
236 | GNUNET_SCHEDULER_TaskCallback finished_cb, | ||
237 | void *finished_cb_cls) | ||
238 | { | ||
239 | GNUNET_break (0); | ||
240 | } | ||
241 | |||
242 | |||
243 | /** | ||
244 | * Entry point for the plugin. | ||
245 | * | ||
246 | * @param cls closure (the `struct GNUNET_DHTU_PluginEnvironment`) | ||
247 | * @return the plugin's API | ||
248 | */ | ||
249 | void * | ||
250 | libgnunet_plugin_dhtu_ip_init (void *cls) | ||
251 | { | ||
252 | struct GNUNET_DHTU_PluginEnvironment *env = cls; | ||
253 | struct GNUNET_DHTU_PluginFunctions *api; | ||
254 | struct Plugin *plugin; | ||
255 | |||
256 | plugin = GNUNET_new (struct Plugin); | ||
257 | plugin->env = env; | ||
258 | api = GNUNET_new (struct GNUNET_DHTU_PluginFunctions); | ||
259 | api->cls = plugin; | ||
260 | api->sign = &ip_sign; | ||
261 | api->verify = &ip_verify; | ||
262 | api->try_connect = &ip_try_connect; | ||
263 | api->hold = &ip_hold; | ||
264 | api->drop = &ip_drop; | ||
265 | api->send = &ip_send; | ||
266 | return api; | ||
267 | } | ||
268 | |||
269 | |||
270 | /** | ||
271 | * Exit point from the plugin. | ||
272 | * | ||
273 | * @param cls closure (our `struct Plugin`) | ||
274 | * @return NULL | ||
275 | */ | ||
276 | void * | ||
277 | libgnunet_plugin_dhtu_gnunet_done (void *cls) | ||
278 | { | ||
279 | struct GNUNET_DHTU_PluginFunctions *api = cls; | ||
280 | struct Plugin *plugin = api->cls; | ||
281 | |||
282 | GNUNET_free (plugin); | ||
283 | GNUNET_free (api); | ||
284 | return NULL; | ||
285 | } | ||