aboutsummaryrefslogtreecommitdiff
path: root/src/regex/gnunet-service-regex.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regex/gnunet-service-regex.c')
-rw-r--r--src/regex/gnunet-service-regex.c303
1 files changed, 150 insertions, 153 deletions
diff --git a/src/regex/gnunet-service-regex.c b/src/regex/gnunet-service-regex.c
index a866288a3..90ea91780 100644
--- a/src/regex/gnunet-service-regex.c
+++ b/src/regex/gnunet-service-regex.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file regex/gnunet-service-regex.c 22 * @file regex/gnunet-service-regex.c
@@ -33,9 +33,7 @@
33/** 33/**
34 * Information about one of our clients. 34 * Information about one of our clients.
35 */ 35 */
36struct ClientEntry 36struct ClientEntry {
37{
38
39 /** 37 /**
40 * Queue for transmissions to @e client. 38 * Queue for transmissions to @e client.
41 */ 39 */
@@ -65,7 +63,6 @@ struct ClientEntry
65 * Task for re-announcing. 63 * Task for re-announcing.
66 */ 64 */
67 struct GNUNET_SCHEDULER_Task *refresh_task; 65 struct GNUNET_SCHEDULER_Task *refresh_task;
68
69}; 66};
70 67
71 68
@@ -91,14 +88,14 @@ static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
91 * @param cls unused 88 * @param cls unused
92 */ 89 */
93static void 90static void
94cleanup_task (void *cls) 91cleanup_task(void *cls)
95{ 92{
96 GNUNET_DHT_disconnect (dht); 93 GNUNET_DHT_disconnect(dht);
97 dht = NULL; 94 dht = NULL;
98 GNUNET_STATISTICS_destroy (stats, 95 GNUNET_STATISTICS_destroy(stats,
99 GNUNET_NO); 96 GNUNET_NO);
100 stats = NULL; 97 stats = NULL;
101 GNUNET_free (my_private_key); 98 GNUNET_free(my_private_key);
102 my_private_key = NULL; 99 my_private_key = NULL;
103} 100}
104 101
@@ -110,14 +107,14 @@ cleanup_task (void *cls)
110 * announcement 107 * announcement
111 */ 108 */
112static void 109static void
113reannounce (void *cls) 110reannounce(void *cls)
114{ 111{
115 struct ClientEntry *ce = cls; 112 struct ClientEntry *ce = cls;
116 113
117 REGEX_INTERNAL_reannounce (ce->ah); 114 REGEX_INTERNAL_reannounce(ce->ah);
118 ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency, 115 ce->refresh_task = GNUNET_SCHEDULER_add_delayed(ce->frequency,
119 &reannounce, 116 &reannounce,
120 ce); 117 ce);
121} 118}
122 119
123 120
@@ -129,18 +126,18 @@ reannounce (void *cls)
129 * @return #GNUNET_OK if @am is well-formed 126 * @return #GNUNET_OK if @am is well-formed
130 */ 127 */
131static int 128static int
132check_announce (void *cls, 129check_announce(void *cls,
133 const struct AnnounceMessage *am) 130 const struct AnnounceMessage *am)
134{ 131{
135 struct ClientEntry *ce = cls; 132 struct ClientEntry *ce = cls;
136 133
137 GNUNET_MQ_check_zero_termination (am); 134 GNUNET_MQ_check_zero_termination(am);
138 if (NULL != ce->ah) 135 if (NULL != ce->ah)
139 { 136 {
140 /* only one announcement per client allowed */ 137 /* only one announcement per client allowed */
141 GNUNET_break (0); 138 GNUNET_break(0);
142 return GNUNET_SYSERR; 139 return GNUNET_SYSERR;
143 } 140 }
144 return GNUNET_OK; 141 return GNUNET_OK;
145} 142}
146 143
@@ -152,36 +149,36 @@ check_announce (void *cls,
152 * @param am the actual message 149 * @param am the actual message
153 */ 150 */
154static void 151static void
155handle_announce (void *cls, 152handle_announce(void *cls,
156 const struct AnnounceMessage *am) 153 const struct AnnounceMessage *am)
157{ 154{
158 struct ClientEntry *ce = cls; 155 struct ClientEntry *ce = cls;
159 const char *regex; 156 const char *regex;
160 157
161 regex = (const char *) &am[1]; 158 regex = (const char *)&am[1];
162 ce->frequency = GNUNET_TIME_relative_ntoh (am->refresh_delay); 159 ce->frequency = GNUNET_TIME_relative_ntoh(am->refresh_delay);
163 ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency, 160 ce->refresh_task = GNUNET_SCHEDULER_add_delayed(ce->frequency,
164 &reannounce, 161 &reannounce,
165 ce); 162 ce);
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 163 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
167 "Starting to announce regex `%s' every %s\n", 164 "Starting to announce regex `%s' every %s\n",
168 regex, 165 regex,
169 GNUNET_STRINGS_relative_time_to_string (ce->frequency, 166 GNUNET_STRINGS_relative_time_to_string(ce->frequency,
170 GNUNET_NO)); 167 GNUNET_NO));
171 ce->ah = REGEX_INTERNAL_announce (dht, 168 ce->ah = REGEX_INTERNAL_announce(dht,
172 my_private_key, 169 my_private_key,
173 regex, 170 regex,
174 ntohs (am->compression), 171 ntohs(am->compression),
175 stats); 172 stats);
176 if (NULL == ce->ah) 173 if (NULL == ce->ah)
177 { 174 {
178 GNUNET_break (0); 175 GNUNET_break(0);
179 GNUNET_SCHEDULER_cancel (ce->refresh_task); 176 GNUNET_SCHEDULER_cancel(ce->refresh_task);
180 ce->refresh_task = NULL; 177 ce->refresh_task = NULL;
181 GNUNET_SERVICE_client_drop (ce->client); 178 GNUNET_SERVICE_client_drop(ce->client);
182 return; 179 return;
183 } 180 }
184 GNUNET_SERVICE_client_continue (ce->client); 181 GNUNET_SERVICE_client_continue(ce->client);
185} 182}
186 183
187 184
@@ -196,12 +193,12 @@ handle_announce (void *cls,
196 * @param put_path_length Length of the @a put_path. 193 * @param put_path_length Length of the @a put_path.
197 */ 194 */
198static void 195static void
199handle_search_result (void *cls, 196handle_search_result(void *cls,
200 const struct GNUNET_PeerIdentity *id, 197 const struct GNUNET_PeerIdentity *id,
201 const struct GNUNET_PeerIdentity *get_path, 198 const struct GNUNET_PeerIdentity *get_path,
202 unsigned int get_path_length, 199 unsigned int get_path_length,
203 const struct GNUNET_PeerIdentity *put_path, 200 const struct GNUNET_PeerIdentity *put_path,
204 unsigned int put_path_length) 201 unsigned int put_path_length)
205{ 202{
206 struct ClientEntry *ce = cls; 203 struct ClientEntry *ce = cls;
207 struct GNUNET_MQ_Envelope *env; 204 struct GNUNET_MQ_Envelope *env;
@@ -209,30 +206,30 @@ handle_search_result (void *cls,
209 struct GNUNET_PeerIdentity *gp; 206 struct GNUNET_PeerIdentity *gp;
210 uint16_t size; 207 uint16_t size;
211 208
212 if ( (get_path_length >= 65536) || 209 if ((get_path_length >= 65536) ||
213 (put_path_length >= 65536) || 210 (put_path_length >= 65536) ||
214 ( (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity)) 211 ((get_path_length + put_path_length) * sizeof(struct GNUNET_PeerIdentity))
215 + sizeof (struct ResultMessage) >= GNUNET_MAX_MESSAGE_SIZE) 212 + sizeof(struct ResultMessage) >= GNUNET_MAX_MESSAGE_SIZE)
216 { 213 {
217 GNUNET_break (0); 214 GNUNET_break(0);
218 return; 215 return;
219 } 216 }
220 size = (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity); 217 size = (get_path_length + put_path_length) * sizeof(struct GNUNET_PeerIdentity);
221 env = GNUNET_MQ_msg_extra (result, 218 env = GNUNET_MQ_msg_extra(result,
222 size, 219 size,
223 GNUNET_MESSAGE_TYPE_REGEX_RESULT); 220 GNUNET_MESSAGE_TYPE_REGEX_RESULT);
224 result->get_path_length = htons ((uint16_t) get_path_length); 221 result->get_path_length = htons((uint16_t)get_path_length);
225 result->put_path_length = htons ((uint16_t) put_path_length); 222 result->put_path_length = htons((uint16_t)put_path_length);
226 result->id = *id; 223 result->id = *id;
227 gp = &result->id; 224 gp = &result->id;
228 GNUNET_memcpy (&gp[1], 225 GNUNET_memcpy(&gp[1],
229 get_path, 226 get_path,
230 get_path_length * sizeof (struct GNUNET_PeerIdentity)); 227 get_path_length * sizeof(struct GNUNET_PeerIdentity));
231 GNUNET_memcpy (&gp[1 + get_path_length], 228 GNUNET_memcpy(&gp[1 + get_path_length],
232 put_path, 229 put_path,
233 put_path_length * sizeof (struct GNUNET_PeerIdentity)); 230 put_path_length * sizeof(struct GNUNET_PeerIdentity));
234 GNUNET_MQ_send (ce->mq, 231 GNUNET_MQ_send(ce->mq,
235 env); 232 env);
236} 233}
237 234
238 235
@@ -243,26 +240,26 @@ handle_search_result (void *cls,
243 * @param message the actual message 240 * @param message the actual message
244 */ 241 */
245static int 242static int
246check_search (void *cls, 243check_search(void *cls,
247 const struct RegexSearchMessage *sm) 244 const struct RegexSearchMessage *sm)
248{ 245{
249 struct ClientEntry *ce = cls; 246 struct ClientEntry *ce = cls;
250 const char *string; 247 const char *string;
251 uint16_t size; 248 uint16_t size;
252 249
253 size = ntohs (sm->header.size) - sizeof (*sm); 250 size = ntohs(sm->header.size) - sizeof(*sm);
254 string = (const char *) &sm[1]; 251 string = (const char *)&sm[1];
255 if ('\0' != string[size - 1]) 252 if ('\0' != string[size - 1])
256 { 253 {
257 GNUNET_break (0); 254 GNUNET_break(0);
258 return GNUNET_SYSERR; 255 return GNUNET_SYSERR;
259 } 256 }
260 if (NULL != ce->sh) 257 if (NULL != ce->sh)
261 { 258 {
262 /* only one search allowed per client */ 259 /* only one search allowed per client */
263 GNUNET_break (0); 260 GNUNET_break(0);
264 return GNUNET_SYSERR; 261 return GNUNET_SYSERR;
265 } 262 }
266 return GNUNET_OK; 263 return GNUNET_OK;
267} 264}
268 265
@@ -274,28 +271,28 @@ check_search (void *cls,
274 * @param message the actual message 271 * @param message the actual message
275 */ 272 */
276static void 273static void
277handle_search (void *cls, 274handle_search(void *cls,
278 const struct RegexSearchMessage *sm) 275 const struct RegexSearchMessage *sm)
279{ 276{
280 struct ClientEntry *ce = cls; 277 struct ClientEntry *ce = cls;
281 const char *string; 278 const char *string;
282 279
283 string = (const char *) &sm[1]; 280 string = (const char *)&sm[1];
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 281 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
285 "Starting to search for `%s'\n", 282 "Starting to search for `%s'\n",
286 string); 283 string);
287 ce->sh = REGEX_INTERNAL_search (dht, 284 ce->sh = REGEX_INTERNAL_search(dht,
288 string, 285 string,
289 &handle_search_result, 286 &handle_search_result,
290 ce, 287 ce,
291 stats); 288 stats);
292 if (NULL == ce->sh) 289 if (NULL == ce->sh)
293 { 290 {
294 GNUNET_break (0); 291 GNUNET_break(0);
295 GNUNET_SERVICE_client_drop (ce->client); 292 GNUNET_SERVICE_client_drop(ce->client);
296 return; 293 return;
297 } 294 }
298 GNUNET_SERVICE_client_continue (ce->client); 295 GNUNET_SERVICE_client_continue(ce->client);
299} 296}
300 297
301 298
@@ -307,27 +304,27 @@ handle_search (void *cls,
307 * @param service the initialized service 304 * @param service the initialized service
308 */ 305 */
309static void 306static void
310run (void *cls, 307run(void *cls,
311 const struct GNUNET_CONFIGURATION_Handle *cfg, 308 const struct GNUNET_CONFIGURATION_Handle *cfg,
312 struct GNUNET_SERVICE_Handle *service) 309 struct GNUNET_SERVICE_Handle *service)
313{ 310{
314 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg); 311 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration(cfg);
315 if (NULL == my_private_key) 312 if (NULL == my_private_key)
316 { 313 {
317 GNUNET_SCHEDULER_shutdown (); 314 GNUNET_SCHEDULER_shutdown();
318 return; 315 return;
319 } 316 }
320 dht = GNUNET_DHT_connect (cfg, 1024); 317 dht = GNUNET_DHT_connect(cfg, 1024);
321 if (NULL == dht) 318 if (NULL == dht)
322 { 319 {
323 GNUNET_free (my_private_key); 320 GNUNET_free(my_private_key);
324 my_private_key = NULL; 321 my_private_key = NULL;
325 GNUNET_SCHEDULER_shutdown (); 322 GNUNET_SCHEDULER_shutdown();
326 return; 323 return;
327 } 324 }
328 GNUNET_SCHEDULER_add_shutdown (&cleanup_task, 325 GNUNET_SCHEDULER_add_shutdown(&cleanup_task,
329 NULL); 326 NULL);
330 stats = GNUNET_STATISTICS_create ("regex", cfg); 327 stats = GNUNET_STATISTICS_create("regex", cfg);
331} 328}
332 329
333 330
@@ -340,13 +337,13 @@ run (void *cls,
340 * @return @a c 337 * @return @a c
341 */ 338 */
342static void * 339static void *
343client_connect_cb (void *cls, 340client_connect_cb(void *cls,
344 struct GNUNET_SERVICE_Client *c, 341 struct GNUNET_SERVICE_Client *c,
345 struct GNUNET_MQ_Handle *mq) 342 struct GNUNET_MQ_Handle *mq)
346{ 343{
347 struct ClientEntry *ce; 344 struct ClientEntry *ce;
348 345
349 ce = GNUNET_new (struct ClientEntry); 346 ce = GNUNET_new(struct ClientEntry);
350 ce->client = c; 347 ce->client = c;
351 ce->mq = mq; 348 ce->mq = mq;
352 return ce; 349 return ce;
@@ -361,28 +358,28 @@ client_connect_cb (void *cls,
361 * @param internal_cls should be equal to @a c 358 * @param internal_cls should be equal to @a c
362 */ 359 */
363static void 360static void
364client_disconnect_cb (void *cls, 361client_disconnect_cb(void *cls,
365 struct GNUNET_SERVICE_Client *c, 362 struct GNUNET_SERVICE_Client *c,
366 void *internal_cls) 363 void *internal_cls)
367{ 364{
368 struct ClientEntry *ce = internal_cls; 365 struct ClientEntry *ce = internal_cls;
369 366
370 if (NULL != ce->refresh_task) 367 if (NULL != ce->refresh_task)
371 { 368 {
372 GNUNET_SCHEDULER_cancel (ce->refresh_task); 369 GNUNET_SCHEDULER_cancel(ce->refresh_task);
373 ce->refresh_task = NULL; 370 ce->refresh_task = NULL;
374 } 371 }
375 if (NULL != ce->ah) 372 if (NULL != ce->ah)
376 { 373 {
377 REGEX_INTERNAL_announce_cancel (ce->ah); 374 REGEX_INTERNAL_announce_cancel(ce->ah);
378 ce->ah = NULL; 375 ce->ah = NULL;
379 } 376 }
380 if (NULL != ce->sh) 377 if (NULL != ce->sh)
381 { 378 {
382 REGEX_INTERNAL_search_cancel (ce->sh); 379 REGEX_INTERNAL_search_cancel(ce->sh);
383 ce->sh = NULL; 380 ce->sh = NULL;
384 } 381 }
385 GNUNET_free (ce); 382 GNUNET_free(ce);
386} 383}
387 384
388 385
@@ -390,21 +387,21 @@ client_disconnect_cb (void *cls,
390 * Define "main" method using service macro. 387 * Define "main" method using service macro.
391 */ 388 */
392GNUNET_SERVICE_MAIN 389GNUNET_SERVICE_MAIN
393("regex", 390 ("regex",
394 GNUNET_SERVICE_OPTION_NONE, 391 GNUNET_SERVICE_OPTION_NONE,
395 &run, 392 &run,
396 &client_connect_cb, 393 &client_connect_cb,
397 &client_disconnect_cb, 394 &client_disconnect_cb,
398 NULL, 395 NULL,
399 GNUNET_MQ_hd_var_size (announce, 396 GNUNET_MQ_hd_var_size(announce,
400 GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE, 397 GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE,
401 struct AnnounceMessage, 398 struct AnnounceMessage,
402 NULL), 399 NULL),
403 GNUNET_MQ_hd_var_size (search, 400 GNUNET_MQ_hd_var_size(search,
404 GNUNET_MESSAGE_TYPE_REGEX_SEARCH, 401 GNUNET_MESSAGE_TYPE_REGEX_SEARCH,
405 struct RegexSearchMessage, 402 struct RegexSearchMessage,
406 NULL), 403 NULL),
407 GNUNET_MQ_handler_end ()); 404 GNUNET_MQ_handler_end());
408 405
409 406
410/* end of gnunet-service-regex.c */ 407/* end of gnunet-service-regex.c */