aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMartin Schanzenbach <mschanzenbach@posteo.de>2015-03-11 17:39:32 +0000
committerMartin Schanzenbach <mschanzenbach@posteo.de>2015-03-11 17:39:32 +0000
commitb5cb8ce9b34d0f8cf2eb19cf82680aca365d157f (patch)
tree7db300f694ca75a5fcd5fc81bc59db30f67fb641 /src
parent86882d3e761fe639e833446ccfef8d31e0e42998 (diff)
downloadgnunet-b5cb8ce9b34d0f8cf2eb19cf82680aca365d157f.tar.gz
gnunet-b5cb8ce9b34d0f8cf2eb19cf82680aca365d157f.zip
- fixes, intendation
Diffstat (limited to 'src')
-rw-r--r--src/gns/plugin_rest_gns.c648
-rw-r--r--src/rest/gnunet-rest-server.c451
2 files changed, 554 insertions, 545 deletions
diff --git a/src/gns/plugin_rest_gns.c b/src/gns/plugin_rest_gns.c
index bd28c23f3..d5d6788c8 100644
--- a/src/gns/plugin_rest_gns.c
+++ b/src/gns/plugin_rest_gns.c
@@ -1,22 +1,22 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2012-2015 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 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 7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version. 8 option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details. 13 General Public License for more details.
14 14
15 You should have received a copy of the GNU General Public License 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 16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19 */
20/** 20/**
21 * @author Martin Schanzenbach 21 * @author Martin Schanzenbach
22 * @file gns/plugin_rest_gns.c 22 * @file gns/plugin_rest_gns.c
@@ -47,103 +47,145 @@ const struct GNUNET_CONFIGURATION_Handle *cfg;
47 47
48struct LookupHandle 48struct LookupHandle
49{ 49{
50 /** 50 /**
51 * Handle to GNS service. 51 * Handle to GNS service.
52 */ 52 */
53 struct GNUNET_GNS_Handle *gns; 53 struct GNUNET_GNS_Handle *gns;
54 54
55 /** 55 /**
56 * Desired timeout for the lookup (default is no timeout). 56 * Desired timeout for the lookup (default is no timeout).
57 */ 57 */
58 struct GNUNET_TIME_Relative timeout; 58 struct GNUNET_TIME_Relative timeout;
59 59
60 /** 60 /**
61 * Handle to lookup request 61 * Handle to lookup request
62 */ 62 */
63 struct GNUNET_GNS_LookupRequest *lookup_request; 63 struct GNUNET_GNS_LookupRequest *lookup_request;
64 64
65 /** 65 /**
66 * Lookup an ego with the identity service. 66 * Lookup an ego with the identity service.
67 */ 67 */
68 struct GNUNET_IDENTITY_EgoLookup *el; 68 struct GNUNET_IDENTITY_EgoLookup *el;
69 69
70 /** 70 /**
71 * Handle for identity service. 71 * Handle for identity service.
72 */ 72 */
73 struct GNUNET_IDENTITY_Handle *identity; 73 struct GNUNET_IDENTITY_Handle *identity;
74 74
75 /** 75 /**
76 * Active operation on identity service. 76 * Active operation on identity service.
77 */ 77 */
78 struct GNUNET_IDENTITY_Operation *id_op; 78 struct GNUNET_IDENTITY_Operation *id_op;
79 /** 79
80 * ID of a task associated with the resolution process. 80 /**
81 */ 81 * ID of a task associated with the resolution process.
82 struct GNUNET_SCHEDULER_Task * timeout_task; 82 */
83 83 struct GNUNET_SCHEDULER_Task * timeout_task;
84 json_t *json_root; 84
85 85 /**
86 GNUNET_REST_ResultProcessor proc; 86 * The root of the received JSON or NULL
87 87 */
88 char *name; 88 json_t *json_root;
89 89
90 const char *ego_str; 90 /**
91 91 * The plugin result processor
92 const char *pkey_str; 92 */
93 93 GNUNET_REST_ResultProcessor proc;
94 int type; 94
95 95 /**
96 struct GNUNET_CRYPTO_EcdsaPublicKey pkey; 96 * The closure of the result processor
97 97 */
98 struct GNUNET_CRYPTO_EcdsaPublicKey pkeym; 98 void *proc_cls;
99 99
100 enum GNUNET_GNS_LocalOptions options; 100 /**
101 101 * The name to look up
102 struct GNUNET_CRYPTO_EcdsaPrivateKey shorten_key; 102 */
103 void *proc_cls; 103 char *name;
104
105 /**
106 * The ego to use
107 * In string representation from JSON
108 */
109 const char *ego_str;
110
111 /**
112 * The Pkey to use
113 * In stirng representation from JSON
114 */
115 const char *pkey_str;
116
117 /**
118 * The record type to look up
119 */
120 int type;
121
122 /**
123 * The public key of to use for lookup
124 */
125 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
126
127 /**
128 * The public key to use for lookup
129 */
130 struct GNUNET_CRYPTO_EcdsaPublicKey pkeym;
131
132 /**
133 * The resolver options
134 */
135 enum GNUNET_GNS_LocalOptions options;
136
137 /**
138 * the shorten key
139 */
140 struct GNUNET_CRYPTO_EcdsaPrivateKey shorten_key;
141
104}; 142};
105 143
144/**
145 * Cleanup lookup handle
146 * @praram handle Handle to clean up
147 */
106void 148void
107cleanup_handle (struct LookupHandle *handle) 149cleanup_handle (struct LookupHandle *handle)
108{ 150{
109 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
110 "Cleaning up\n"); 152 "Cleaning up\n");
111 if (NULL != handle->json_root) 153 if (NULL != handle->json_root)
112 json_decref (handle->json_root); 154 json_decref (handle->json_root);
113 155
114 if (NULL != handle->name) 156 if (NULL != handle->name)
115 GNUNET_free (handle->name); 157 GNUNET_free (handle->name);
116 if (NULL != handle->el) 158 if (NULL != handle->el)
117 { 159 {
118 GNUNET_IDENTITY_ego_lookup_cancel (handle->el); 160 GNUNET_IDENTITY_ego_lookup_cancel (handle->el);
119 handle->el = NULL; 161 handle->el = NULL;
120 } 162 }
121 if (NULL != handle->id_op) 163 if (NULL != handle->id_op)
122 { 164 {
123 GNUNET_IDENTITY_cancel (handle->id_op); 165 GNUNET_IDENTITY_cancel (handle->id_op);
124 handle->id_op = NULL; 166 handle->id_op = NULL;
125 } 167 }
126 if (NULL != handle->lookup_request) 168 if (NULL != handle->lookup_request)
127 { 169 {
128 GNUNET_GNS_lookup_cancel (handle->lookup_request); 170 GNUNET_GNS_lookup_cancel (handle->lookup_request);
129 handle->lookup_request = NULL; 171 handle->lookup_request = NULL;
130 } 172 }
131 if (NULL != handle->identity) 173 if (NULL != handle->identity)
132 { 174 {
133 GNUNET_IDENTITY_disconnect (handle->identity); 175 GNUNET_IDENTITY_disconnect (handle->identity);
134 handle->identity = NULL; 176 handle->identity = NULL;
135 } 177 }
136 if (NULL != handle->gns) 178 if (NULL != handle->gns)
137 { 179 {
138 GNUNET_GNS_disconnect (handle->gns); 180 GNUNET_GNS_disconnect (handle->gns);
139 handle->gns = NULL; 181 handle->gns = NULL;
140 } 182 }
141 183
142 if (NULL != handle->timeout_task) 184 if (NULL != handle->timeout_task)
143 { 185 {
144 GNUNET_SCHEDULER_cancel (handle->timeout_task); 186 GNUNET_SCHEDULER_cancel (handle->timeout_task);
145 } 187 }
146 GNUNET_free (handle); 188 GNUNET_free (handle);
147} 189}
148 190
149 191
@@ -155,12 +197,11 @@ cleanup_handle (struct LookupHandle *handle)
155 */ 197 */
156static void 198static void
157do_error (void *cls, 199do_error (void *cls,
158 const struct GNUNET_SCHEDULER_TaskContext *tc) 200 const struct GNUNET_SCHEDULER_TaskContext *tc)
159{ 201{
160 struct LookupHandle *handle = cls; 202 struct LookupHandle *handle = cls;
161
162 cleanup_handle (handle);
163 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 203 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
204 cleanup_handle (handle);
164} 205}
165 206
166/** 207/**
@@ -172,10 +213,8 @@ do_error (void *cls,
172 */ 213 */
173static void 214static void
174process_lookup_result (void *cls, uint32_t rd_count, 215process_lookup_result (void *cls, uint32_t rd_count,
175 const struct GNUNET_GNSRECORD_Data *rd) 216 const struct GNUNET_GNSRECORD_Data *rd)
176{ 217{
177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
178 "result \n");
179 struct LookupHandle *handle = cls; 218 struct LookupHandle *handle = cls;
180 uint32_t i; 219 uint32_t i;
181 const char *typename; 220 const char *typename;
@@ -186,30 +225,28 @@ process_lookup_result (void *cls, uint32_t rd_count,
186 json_t *result_array; 225 json_t *result_array;
187 json_t *record_obj; 226 json_t *record_obj;
188 227
189
190 result_root = json_object(); 228 result_root = json_object();
191 result_name = json_string (handle->name); 229 result_name = json_string (handle->name);
192 result_array = json_array(); 230 result_array = json_array();
193 json_object_set (result_root, "name", result_name); 231 json_object_set (result_root, "name", result_name);
194 json_decref (result_name); 232 json_decref (result_name);
195
196 handle->lookup_request = NULL; 233 handle->lookup_request = NULL;
197 234
198 for (i=0; i<rd_count; i++) 235 for (i=0; i<rd_count; i++)
199 { 236 {
200 if ( (rd[i].record_type != handle->type) && 237 if ( (rd[i].record_type != handle->type) &&
201 (GNUNET_GNSRECORD_TYPE_ANY != handle->type) ) 238 (GNUNET_GNSRECORD_TYPE_ANY != handle->type) )
202 continue; 239 continue;
203 typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type); 240 typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
204 string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, 241 string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
205 rd[i].data, 242 rd[i].data,
206 rd[i].data_size); 243 rd[i].data_size);
207 if (NULL == string_val) 244 if (NULL == string_val)
208 { 245 {
209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
210 "Record %u of type %d malformed, skipping\n", 247 "Record %u of type %d malformed, skipping\n",
211 (unsigned int) i, 248 (unsigned int) i,
212 (int) rd[i].record_type); 249 (int) rd[i].record_type);
213 continue; 250 continue;
214 } 251 }
215 else 252 else
@@ -242,30 +279,27 @@ process_lookup_result (void *cls, uint32_t rd_count,
242static void 279static void
243lookup_with_keys (struct LookupHandle *handle, const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key) 280lookup_with_keys (struct LookupHandle *handle, const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key)
244{ 281{
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
246 "Lookup w/ keys \n");
247 if (UINT32_MAX == handle->type) 282 if (UINT32_MAX == handle->type)
248 { 283 {
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250 _("Invalid typename specified, assuming `ANY'\n")); 285 _("Invalid typename specified, assuming `ANY'\n"));
251 handle->type = GNUNET_GNSRECORD_TYPE_ANY; 286 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
252 } 287 }
253
254 if (NULL != handle->name) 288 if (NULL != handle->name)
255 { 289 {
256 handle->lookup_request = GNUNET_GNS_lookup (handle->gns, 290 handle->lookup_request = GNUNET_GNS_lookup (handle->gns,
257 handle->name, 291 handle->name,
258 &handle->pkey, 292 &handle->pkey,
259 handle->type, 293 handle->type,
260 handle->options, 294 handle->options,
261 shorten_key, 295 shorten_key,
262 &process_lookup_result, 296 &process_lookup_result,
263 handle); 297 handle);
264 } 298 }
265 else 299 else
266 { 300 {
267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 301 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
268 _("Please specify name to lookup!\n")); 302 _("Please specify name to lookup!\n"));
269 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 303 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
270 cleanup_handle (handle); 304 cleanup_handle (handle);
271 return; 305 return;
@@ -286,12 +320,10 @@ lookup_with_keys (struct LookupHandle *handle, const struct GNUNET_CRYPTO_EcdsaP
286 */ 320 */
287static void 321static void
288identity_shorten_cb (void *cls, 322identity_shorten_cb (void *cls,
289 struct GNUNET_IDENTITY_Ego *ego, 323 struct GNUNET_IDENTITY_Ego *ego,
290 void **ctx, 324 void **ctx,
291 const char *name) 325 const char *name)
292{ 326{
293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
294 "shorten \n");
295 struct LookupHandle *handle = cls; 327 struct LookupHandle *handle = cls;
296 328
297 handle->id_op = NULL; 329 handle->id_op = NULL;
@@ -299,7 +331,7 @@ identity_shorten_cb (void *cls,
299 lookup_with_keys (handle, NULL); 331 lookup_with_keys (handle, NULL);
300 else 332 else
301 lookup_with_keys (handle, 333 lookup_with_keys (handle,
302 GNUNET_IDENTITY_ego_get_private_key (ego)); 334 GNUNET_IDENTITY_ego_get_private_key (ego));
303} 335}
304 336
305/** 337/**
@@ -311,14 +343,12 @@ identity_shorten_cb (void *cls,
311static void 343static void
312lookup_with_public_key (struct LookupHandle *handle) 344lookup_with_public_key (struct LookupHandle *handle)
313{ 345{
314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
315 "Lookup w/ pkey \n");
316 handle->pkeym = handle->pkey; 346 handle->pkeym = handle->pkey;
317 GNUNET_break (NULL == handle->id_op); 347 GNUNET_break (NULL == handle->id_op);
318 handle->id_op = GNUNET_IDENTITY_get (handle->identity, 348 handle->id_op = GNUNET_IDENTITY_get (handle->identity,
319 "gns-short", 349 "gns-short",
320 &identity_shorten_cb, 350 &identity_shorten_cb,
321 handle); 351 handle);
322 if (NULL == handle->id_op) 352 if (NULL == handle->id_op)
323 { 353 {
324 GNUNET_break (0); 354 GNUNET_break (0);
@@ -335,7 +365,7 @@ lookup_with_public_key (struct LookupHandle *handle)
335 */ 365 */
336static void 366static void
337identity_zone_cb (void *cls, 367identity_zone_cb (void *cls,
338 const struct GNUNET_IDENTITY_Ego *ego) 368 const struct GNUNET_IDENTITY_Ego *ego)
339{ 369{
340 struct LookupHandle *handle = cls; 370 struct LookupHandle *handle = cls;
341 371
@@ -343,7 +373,7 @@ identity_zone_cb (void *cls,
343 if (NULL == ego) 373 if (NULL == ego)
344 { 374 {
345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 375 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
346 _("Ego for not found, cannot perform lookup.\n")); 376 _("Ego for not found, cannot perform lookup.\n"));
347 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 377 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
348 cleanup_handle (handle); 378 cleanup_handle (handle);
349 return; 379 return;
@@ -370,20 +400,18 @@ identity_zone_cb (void *cls,
370 */ 400 */
371static void 401static void
372identity_master_cb (void *cls, 402identity_master_cb (void *cls,
373 struct GNUNET_IDENTITY_Ego *ego, 403 struct GNUNET_IDENTITY_Ego *ego,
374 void **ctx, 404 void **ctx,
375 const char *name) 405 const char *name)
376{ 406{
377 const char *dot; 407 const char *dot;
378 struct LookupHandle *handle = cls; 408 struct LookupHandle *handle = cls;
379 409
380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
381 "Master cb \n");
382 handle->id_op = NULL; 410 handle->id_op = NULL;
383 if (NULL == ego) 411 if (NULL == ego)
384 { 412 {
385 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 413 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
386 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n")); 414 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
387 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 415 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
388 cleanup_handle (handle); 416 cleanup_handle (handle);
389 return; 417 return;
@@ -401,93 +429,98 @@ identity_master_cb (void *cls,
401 lookup_with_public_key (handle); 429 lookup_with_public_key (handle);
402} 430}
403 431
432/**
433 * Parse REST uri for name and record type
434 *
435 * @param url Url to parse
436 * @param handle lookup handle to populate
437 * @return GNUNET_SYSERR on error
438 */
404int 439int
405parse_url (const char *url, struct LookupHandle *handle) 440parse_url (const char *url, struct LookupHandle *handle)
406{ 441{
407 char *name; 442 char *name;
408 char *type; 443 char *type;
409 char tmp_url[strlen(url)+1]; 444 char tmp_url[strlen(url)+1];
410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Parsing %s\n", url); 445 char *tok;
411 strcpy (tmp_url, url); 446
412 447 strcpy (tmp_url, url);
413 char *tok = strtok ((char*)tmp_url, "/"); 448 tok = strtok ((char*)tmp_url, "/");
414 449 if (NULL == tok)
415 450 return GNUNET_SYSERR;
416 if (NULL == tok) 451 name = strtok (NULL, "/");
417 return GNUNET_SYSERR; 452 if (NULL == name)
418 453 return GNUNET_SYSERR;
419 name = strtok (NULL, "/"); 454 GNUNET_asprintf (&handle->name,
420 455 "%s",
421 if (NULL == name) 456 name);
422 return GNUNET_SYSERR; 457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 458 "Got name: %s\n", handle->name);
424 GNUNET_asprintf (&handle->name, 459 type = strtok (NULL, "/");
425 "%s", 460 if (NULL == type)
426 name); 461 {
427 462 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
428 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
429 "Got name: %s\n", handle->name);
430
431 type = strtok (NULL, "/");
432
433 if (NULL == type)
434 {
435 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
436 return GNUNET_OK;
437 }
438
439 handle->type = GNUNET_GNSRECORD_typename_to_number (type);
440
441 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
442 "Got type: %s\n", type);
443 return GNUNET_OK; 463 return GNUNET_OK;
464 }
465 handle->type = GNUNET_GNSRECORD_typename_to_number (type);
466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
467 "Got type: %s\n", type);
468 return GNUNET_OK;
444} 469}
445 470
471/**
472 * Parse json from REST request
473 *
474 * @param data REST data
475 * @param data_size data size
476 * @param handle Handle to populate
477 * @return GNUNET_SYSERR on error
478 */
446int 479int
447parse_json (const char *data, size_t data_size, struct LookupHandle *handle) 480parse_json (const char *data, size_t data_size, struct LookupHandle *handle)
448{ 481{
449 json_error_t error; 482 json_error_t error;
450 json_t *pkey_json; 483 json_t *pkey_json;
451 json_t *ego_json; 484 json_t *ego_json;
452 json_t *options_json; 485 json_t *options_json;
453 486
454 char term_data[data_size+1]; 487 char term_data[data_size+1];
455 term_data[data_size] = '\0'; 488 term_data[data_size] = '\0';
456 489
457 memcpy (term_data, data, data_size); 490 memcpy (term_data, data, data_size);
458 491
459 handle->json_root = json_loads (term_data, 0, &error); 492 handle->json_root = json_loads (term_data, 0, &error);
460 493
461 if (NULL == handle->json_root) 494 if (NULL == handle->json_root)
462 { 495 {
463 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, error.text); 496 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, error.text);
464 return GNUNET_SYSERR; 497 return GNUNET_SYSERR;
465 } 498 }
466 499
467 if(!json_is_object(handle->json_root)) 500 if(!json_is_object(handle->json_root))
468 { 501 {
469 return GNUNET_SYSERR; 502 return GNUNET_SYSERR;
470 } 503 }
471
472 ego_json = json_object_get (handle->json_root, "ego");
473
474 if(json_is_string(ego_json))
475 {
476 handle->ego_str = json_string_value (ego_json);
477 }
478 504
479 pkey_json = json_object_get (handle->json_root, "pkey"); 505 ego_json = json_object_get (handle->json_root, "ego");
480 if(json_is_string(pkey_json))
481 {
482 handle->pkey_str = json_string_value (pkey_json);
483 }
484 506
485 options_json = json_object_get (handle->json_root, "options"); 507 if(json_is_string(ego_json))
486 if(json_is_integer (options_json)) 508 {
487 { 509 handle->ego_str = json_string_value (ego_json);
488 handle->options = json_integer_value (options_json); 510 }
489 } 511
490 return GNUNET_OK; 512 pkey_json = json_object_get (handle->json_root, "pkey");
513 if(json_is_string(pkey_json))
514 {
515 handle->pkey_str = json_string_value (pkey_json);
516 }
517
518 options_json = json_object_get (handle->json_root, "options");
519 if(json_is_integer (options_json))
520 {
521 handle->options = json_integer_value (options_json);
522 }
523 return GNUNET_OK;
491} 524}
492 525
493 526
@@ -510,103 +543,90 @@ rest_gns_process_request(const char *method,
510 GNUNET_REST_ResultProcessor proc, 543 GNUNET_REST_ResultProcessor proc,
511 void *proc_cls) 544 void *proc_cls)
512{ 545{
513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 546 struct LookupHandle *handle = GNUNET_new (struct LookupHandle);
514 "In plugin\n"); 547 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
515 struct LookupHandle *handle = GNUNET_new (struct LookupHandle);
516
517 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL; //TODO read from json
518 548
519 //parse name and type from url 549 //parse name and type from url
520 if (GNUNET_OK != parse_url (url, handle)) 550 if (GNUNET_OK != parse_url (url, handle))
521 { 551 {
522 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing url...\n"); 552 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing url...\n");
523 proc (proc_cls, NULL, 0, GNUNET_SYSERR); 553 proc (proc_cls, NULL, 0, GNUNET_SYSERR);
524 cleanup_handle (handle); 554 cleanup_handle (handle);
525 return; 555 return;
526 } 556 }
527 557
528 handle->proc_cls = proc_cls; 558 handle->proc_cls = proc_cls;
529 handle->proc = proc; 559 handle->proc = proc;
530 560 if (0 < data_size)
531 if (0 < data_size) 561 {
562 if (GNUNET_OK != parse_json (data, data_size, handle))
532 { 563 {
533 if (GNUNET_OK != parse_json (data, data_size, handle)) 564 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing json...\n");
534 { 565 proc (proc_cls, NULL, 0, GNUNET_SYSERR);
535 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing json...\n"); 566 cleanup_handle (handle);
536 proc (proc_cls, NULL, 0, GNUNET_SYSERR); 567 return;
537 cleanup_handle (handle);
538 return;
539 }
540 } 568 }
541 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 569 }
542 "Connecting...\n"); 570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
543 handle->gns = GNUNET_GNS_connect (cfg); 571 "Connecting...\n");
544 handle->identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL); 572 handle->gns = GNUNET_GNS_connect (cfg);
573 handle->identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
574 handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout,
575 &do_error, handle);
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
577 "Connected\n");
545 578
546 handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout, 579 if (NULL == handle->gns)
547 &do_error, handle); 580 {
548 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 581 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
549 "Connected\n"); 582 "Connecting to GNS failed\n");
550 583 proc (proc_cls, NULL, 0, GNUNET_SYSERR);
551 if (NULL == handle->gns) 584 cleanup_handle (handle);
552 { 585 return;
553 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 586 }
554 "Connecting to GNS failed\n");
555 proc (proc_cls, NULL, 0, GNUNET_SYSERR);
556 cleanup_handle (handle);
557 return;
558 }
559 587
560 if (NULL != handle->pkey_str) 588 if (NULL != handle->pkey_str)
561 { 589 {
562 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 590 if (GNUNET_OK !=
563 "pkey_str\n");
564 if (GNUNET_OK !=
565 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->pkey_str, 591 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->pkey_str,
566 strlen(handle->pkey_str), 592 strlen(handle->pkey_str),
567 &(handle->pkey))) 593 &(handle->pkey)))
568 {
569 proc (proc_cls, NULL, 0, GNUNET_SYSERR);
570 cleanup_handle (handle);
571 return;
572 }
573 lookup_with_public_key (handle);
574
575 }
576 if (NULL != handle->ego_str)
577 {
578 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
579 "ego_str\n");
580 handle->el = GNUNET_IDENTITY_ego_lookup (cfg,
581 handle->ego_str,
582 &identity_zone_cb,
583 handle);
584 return;
585 }
586 if ( (NULL != handle->name) &&
587 (strlen (handle->name) > 4) &&
588 (0 == strcmp (".zkey",
589 &handle->name[strlen (handle->name) - 4])) )
590 {
591 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
592 "zkey\n");
593 /* no zone required, use 'anonymous' zone */
594 GNUNET_CRYPTO_ecdsa_key_get_public
595 (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
596 &(handle->pkey));
597 lookup_with_public_key (handle);
598 }
599 else
600 { 594 {
601 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 595 proc (proc_cls, NULL, 0, GNUNET_SYSERR);
602 "gns_master\n"); 596 cleanup_handle (handle);
603 GNUNET_break (NULL == handle->id_op); 597 return;
604 handle->id_op = GNUNET_IDENTITY_get (handle->identity,
605 "gns-master",
606 &identity_master_cb,
607 handle);
608 GNUNET_assert (NULL != handle->id_op);
609 } 598 }
599 lookup_with_public_key (handle);
600
601 }
602 if (NULL != handle->ego_str)
603 {
604 handle->el = GNUNET_IDENTITY_ego_lookup (cfg,
605 handle->ego_str,
606 &identity_zone_cb,
607 handle);
608 return;
609 }
610 if ( (NULL != handle->name) &&
611 (strlen (handle->name) > 4) &&
612 (0 == strcmp (".zkey",
613 &handle->name[strlen (handle->name) - 4])) )
614 {
615 /* no zone required, use 'anonymous' zone */
616 GNUNET_CRYPTO_ecdsa_key_get_public
617 (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
618 &(handle->pkey));
619 lookup_with_public_key (handle);
620 }
621 else
622 {
623 GNUNET_break (NULL == handle->id_op);
624 handle->id_op = GNUNET_IDENTITY_get (handle->identity,
625 "gns-master",
626 &identity_master_cb,
627 handle);
628 GNUNET_assert (NULL != handle->id_op);
629 }
610} 630}
611 631
612/** 632/**
@@ -631,7 +651,7 @@ libgnunet_plugin_rest_gns_init (void *cls)
631 api->name = API_NAMESPACE; 651 api->name = API_NAMESPACE;
632 api->process_request = &rest_gns_process_request; 652 api->process_request = &rest_gns_process_request;
633 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 653 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
634 _("GNS REST API initialized\n")); 654 _("GNS REST API initialized\n"));
635 return api; 655 return api;
636} 656}
637 657
@@ -651,7 +671,7 @@ libgnunet_plugin_namestore_sqlite_done (void *cls)
651 plugin->cfg = NULL; 671 plugin->cfg = NULL;
652 GNUNET_free (api); 672 GNUNET_free (api);
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "GNS REST plugin is finished\n"); 674 "GNS REST plugin is finished\n");
655 return NULL; 675 return NULL;
656} 676}
657 677
diff --git a/src/rest/gnunet-rest-server.c b/src/rest/gnunet-rest-server.c
index fa1e87a3a..26ca8f5cd 100644
--- a/src/rest/gnunet-rest-server.c
+++ b/src/rest/gnunet-rest-server.c
@@ -1,22 +1,22 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2012-2015 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 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 7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version. 8 option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details. 13 General Public License for more details.
14 14
15 You should have received a copy of the GNU General Public License 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 16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19 */
20/** 20/**
21 * @author Martin Schanzenbach 21 * @author Martin Schanzenbach
22 * @file src/rest/gnunet-rest-server.c 22 * @file src/rest/gnunet-rest-server.c
@@ -119,15 +119,15 @@ struct GNUNET_CONTAINER_MultiHashMap *plugin_map;
119 */ 119 */
120struct MhdConnectionHandle 120struct MhdConnectionHandle
121{ 121{
122 struct MHD_Connection *con; 122 struct MHD_Connection *con;
123
124 struct MHD_Response *response;
125 123
126 struct GNUNET_REST_Plugin *plugin; 124 struct MHD_Response *response;
127 125
128 int status; 126 struct GNUNET_REST_Plugin *plugin;
129 127
130 int state; 128 int status;
129
130 int state;
131}; 131};
132 132
133/* ************************* Global helpers ********************* */ 133/* ************************* Global helpers ********************* */
@@ -146,8 +146,6 @@ do_httpd (void *cls,
146 146
147/** 147/**
148 * Run MHD now, we have extra data ready for the callback. 148 * Run MHD now, we have extra data ready for the callback.
149 *
150 * @param hd the daemon to run now.
151 */ 149 */
152static void 150static void
153run_mhd_now () 151run_mhd_now ()
@@ -156,7 +154,7 @@ run_mhd_now ()
156 httpd_task) 154 httpd_task)
157 GNUNET_SCHEDULER_cancel (httpd_task); 155 GNUNET_SCHEDULER_cancel (httpd_task);
158 httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, 156 httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd,
159 NULL); 157 NULL);
160} 158}
161 159
162/** 160/**
@@ -173,14 +171,14 @@ plugin_callback (void *cls,
173 size_t len, 171 size_t len,
174 int status) 172 int status)
175{ 173{
176 struct MhdConnectionHandle *handle = cls; 174 struct MhdConnectionHandle *handle = cls;
177 struct MHD_Response *resp = MHD_create_response_from_buffer (len, 175 struct MHD_Response *resp = MHD_create_response_from_buffer (len,
178 (void*)data, 176 (void*)data,
179 MHD_RESPMEM_MUST_COPY); 177 MHD_RESPMEM_MUST_COPY);
180 (void) MHD_add_response_header (resp,MHD_HTTP_HEADER_CONTENT_TYPE,"application/json"); 178 (void) MHD_add_response_header (resp,MHD_HTTP_HEADER_CONTENT_TYPE,"application/json");
181 handle->status = status; 179 handle->status = status;
182 handle->response = resp; 180 handle->response = resp;
183 run_mhd_now(); 181 run_mhd_now();
184} 182}
185 183
186/* ********************************* MHD response generation ******************* */ 184/* ********************************* MHD response generation ******************* */
@@ -218,90 +216,90 @@ create_response (void *cls,
218 size_t *upload_data_size, 216 size_t *upload_data_size,
219 void **con_cls) 217 void **con_cls)
220{ 218{
221 char *plugin_name; 219 char *plugin_name;
222 struct GNUNET_HashCode key; 220 struct GNUNET_HashCode key;
223 struct MhdConnectionHandle *con_handle; 221 struct MhdConnectionHandle *con_handle;
224 222
225 con_handle = *con_cls; 223 con_handle = *con_cls;
226 224
227 if (NULL == *con_cls) 225 if (NULL == *con_cls)
226 {
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "New connection %s\n", url);
229 char tmp_url[strlen(url)+1];
230 strcpy (tmp_url, url);
231 con_handle = GNUNET_new (struct MhdConnectionHandle);
232 con_handle->con = con;
233 con_handle->state = GN_REST_STATE_INIT;
234 *con_cls = con_handle;
235
236 plugin_name = strtok(tmp_url, "/");
237
238 if (NULL != plugin_name)
228 { 239 {
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 240 GNUNET_CRYPTO_hash (plugin_name, strlen (plugin_name), &key);
230 "New connection %s\n", url); 241
231 char tmp_url[strlen(url)+1]; 242 con_handle->plugin = GNUNET_CONTAINER_multihashmap_get (plugin_map,
232 strcpy (tmp_url, url); 243 &key);
233 con_handle = GNUNET_new (struct MhdConnectionHandle); 244 }
234 con_handle->con = con; 245 else
235 con_handle->state = GN_REST_STATE_INIT; 246 con_handle->plugin = NULL;
236 *con_cls = con_handle; 247
237 248 if (NULL == con_handle->plugin)
238 plugin_name = strtok(tmp_url, "/"); 249 {
239 250 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
240 if (NULL != plugin_name)
241 {
242 GNUNET_CRYPTO_hash (plugin_name, strlen (plugin_name), &key);
243
244 con_handle->plugin = GNUNET_CONTAINER_multihashmap_get (plugin_map,
245 &key);
246 }
247 else
248 con_handle->plugin = NULL;
249
250 if (NULL == con_handle->plugin)
251 {
252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
253 "Queueing response with MHD\n"); 251 "Queueing response with MHD\n");
254 GNUNET_free (con_handle); 252 GNUNET_free (con_handle);
255 MHD_queue_response (con, 253 MHD_queue_response (con,
256 MHD_HTTP_INTERNAL_SERVER_ERROR, 254 MHD_HTTP_INTERNAL_SERVER_ERROR,
257 failure_response); 255 failure_response);
258 }
259 return MHD_YES;
260 } 256 }
261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 257 return MHD_YES;
262 "Size %d\n", *upload_data_size); 258 }
263 if (GN_REST_STATE_INIT == con_handle->state) 259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260 "Size %d\n", *upload_data_size);
261 if (GN_REST_STATE_INIT == con_handle->state)
262 {
263 if (0 != *upload_data_size)
264 { 264 {
265 if (0 != *upload_data_size) 265 con_handle->state = GN_REST_STATE_UPLOAD;
266 { 266
267 con_handle->state = GN_REST_STATE_UPLOAD; 267 con_handle->plugin->process_request (meth,
268 268 url,
269 con_handle->plugin->process_request (meth, 269 upload_data,
270 url, 270 *upload_data_size,
271 upload_data, 271 &plugin_callback,
272 *upload_data_size, 272 con_handle);
273 &plugin_callback, 273 *upload_data_size = 0;
274 con_handle); 274
275 *upload_data_size = 0;
276
277 }
278 else
279 {
280 con_handle->state = GN_REST_STATE_RECV;
281 con_handle->plugin->process_request (meth,
282 url,
283 NULL,
284 0,
285 &plugin_callback,
286 con_handle);
287 }
288 } 275 }
289 if (NULL != con_handle->response) 276 else
290 { 277 {
278 con_handle->state = GN_REST_STATE_RECV;
279 con_handle->plugin->process_request (meth,
280 url,
281 NULL,
282 0,
283 &plugin_callback,
284 con_handle);
285 }
286 }
287 if (NULL != con_handle->response)
288 {
291 289
292GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Queueing response from plugin with MHD\n"); 291 "Queueing response from plugin with MHD\n");
294 if (GNUNET_OK == con_handle->status) { 292 if (GNUNET_OK == con_handle->status) {
295 return MHD_queue_response (con, 293 return MHD_queue_response (con,
296 MHD_HTTP_OK, 294 MHD_HTTP_OK,
297 con_handle->response); 295 con_handle->response);
298 } else { 296 } else {
299 return MHD_queue_response (con, 297 return MHD_queue_response (con,
300 MHD_HTTP_BAD_REQUEST, 298 MHD_HTTP_BAD_REQUEST,
301 con_handle->response); 299 con_handle->response);
302 }
303 } 300 }
304 return MHD_YES; 301 }
302 return MHD_YES;
305} 303}
306 304
307/* ******************** MHD HTTP setup and event loop ******************** */ 305/* ******************** MHD HTTP setup and event loop ******************** */
@@ -405,19 +403,19 @@ schedule_httpd ()
405 if (NULL != httpd_task) 403 if (NULL != httpd_task)
406 GNUNET_SCHEDULER_cancel (httpd_task); 404 GNUNET_SCHEDULER_cancel (httpd_task);
407 if ( (MHD_YES != haveto) && 405 if ( (MHD_YES != haveto) &&
408 (-1 == max)) 406 (-1 == max))
409 { 407 {
410 /* daemon is idle, kill after timeout */ 408 /* daemon is idle, kill after timeout */
411 httpd_task = GNUNET_SCHEDULER_add_delayed (MHD_CACHE_TIMEOUT, 409 httpd_task = GNUNET_SCHEDULER_add_delayed (MHD_CACHE_TIMEOUT,
412 &kill_httpd_task, 410 &kill_httpd_task,
413 NULL); 411 NULL);
414 } 412 }
415 else 413 else
416 { 414 {
417 httpd_task = 415 httpd_task =
418 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, 416 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
419 tv, wrs, wws, 417 tv, wrs, wws,
420 &do_httpd, NULL); 418 &do_httpd, NULL);
421 } 419 }
422 if (NULL != wrs) 420 if (NULL != wrs)
423 GNUNET_NETWORK_fdset_destroy (wrs); 421 GNUNET_NETWORK_fdset_destroy (wrs);
@@ -484,10 +482,10 @@ do_accept (void *cls,
484 if (MHD_YES != MHD_add_connection (httpd, fd, addr, len)) 482 if (MHD_YES != MHD_add_connection (httpd, fd, addr, len))
485 { 483 {
486 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 484 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
487 _("Failed to pass client to MHD\n")); 485 _("Failed to pass client to MHD\n"));
488 return; 486 return;
489 } 487 }
490 488
491 schedule_httpd (); 489 schedule_httpd ();
492} 490}
493 491
@@ -588,31 +586,29 @@ load_plugin (void *cls,
588 const char *libname, 586 const char *libname,
589 void *lib_ret) 587 void *lib_ret)
590{ 588{
591 struct GNUNET_REST_Plugin *plugin = lib_ret; 589 struct GNUNET_REST_Plugin *plugin = lib_ret;
592 struct GNUNET_HashCode key; 590 struct GNUNET_HashCode key;
593 if (NULL == lib_ret) 591 if (NULL == lib_ret)
594 { 592 {
595 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 593 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
596 "Could not load plugin `%s'\n", 594 "Could not load plugin `%s'\n",
597 libname);
598 return;
599 }
600
601 GNUNET_CRYPTO_hash (plugin->name, strlen (plugin->name), &key);
602
603 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (plugin_map,
604 &key,
605 plugin,
606 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
607 {
608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
609 "Could not load add plugin `%s'\n",
610 libname); 595 libname);
611 return; 596 return;
612 } 597 }
613 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 598 GNUNET_CRYPTO_hash (plugin->name, strlen (plugin->name), &key);
614 "Loaded plugin `%s'\n", 599 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (plugin_map,
600 &key,
601 plugin,
602 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
603 {
604 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
605 "Could not load add plugin `%s'\n",
615 libname); 606 libname);
607 return;
608 }
609 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
610 "Loaded plugin `%s'\n",
611 libname);
616} 612}
617 613
618/** 614/**
@@ -627,78 +623,72 @@ static void
627run (void *cls, char *const *args, const char *cfgfile, 623run (void *cls, char *const *args, const char *cfgfile,
628 const struct GNUNET_CONFIGURATION_Handle *c) 624 const struct GNUNET_CONFIGURATION_Handle *c)
629{ 625{
630 cfg = c; 626 cfg = c;
631 627 plugin_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
632 plugin_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO); 628
633 629 /* Open listen socket proxy */
634 /* Load plugins */ 630 lsock6 = bind_v6 ();
635 GNUNET_PLUGIN_load_all ("libgnunet_plugin_rest", 631 if (NULL == lsock6)
636 (void *) cfg, 632 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
637 &load_plugin, 633 else
638 NULL); 634 {
639 635 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock6, 5))
640
641 /* Open listen socket proxy */
642 lsock6 = bind_v6 ();
643 if (NULL == lsock6)
644 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
645 else
646 { 636 {
647 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock6, 5)) 637 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
648 { 638 GNUNET_NETWORK_socket_close (lsock6);
649 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen"); 639 lsock6 = NULL;
650 GNUNET_NETWORK_socket_close (lsock6);
651 lsock6 = NULL;
652 }
653 else
654 {
655 ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
656 lsock6, &do_accept, lsock6);
657 }
658 } 640 }
659 lsock4 = bind_v4 ();
660 if (NULL == lsock4)
661 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
662 else 641 else
663 { 642 {
664 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock4, 5)) 643 ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
665 { 644 lsock6, &do_accept, lsock6);
666 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen"); 645 }
667 GNUNET_NETWORK_socket_close (lsock4); 646 }
668 lsock4 = NULL; 647 lsock4 = bind_v4 ();
669 } 648 if (NULL == lsock4)
670 else 649 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
671 { 650 else
672 ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 651 {
673 lsock4, &do_accept, lsock4); 652 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock4, 5))
674 } 653 {
654 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
655 GNUNET_NETWORK_socket_close (lsock4);
656 lsock4 = NULL;
675 } 657 }
676 if ( (NULL == lsock4) && 658 else
677 (NULL == lsock6) )
678 { 659 {
679 GNUNET_SCHEDULER_shutdown (); 660 ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
680 return; 661 lsock4, &do_accept, lsock4);
681 } 662 }
682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 663 }
664 if ( (NULL == lsock4) &&
665 (NULL == lsock6) )
666 {
667 GNUNET_SCHEDULER_shutdown ();
668 return;
669 }
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
683 "Service listens on port %u\n", 671 "Service listens on port %u\n",
684 port); 672 port);
685 httpd = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET, 673 httpd = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET,
686 0, 674 0,
687 NULL, NULL, 675 NULL, NULL,
688 &create_response, NULL, 676 &create_response, NULL,
689 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16, 677 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
690 MHD_OPTION_NOTIFY_COMPLETED, &mhd_completed_cb, NULL, 678 MHD_OPTION_NOTIFY_COMPLETED, &mhd_completed_cb, NULL,
691 MHD_OPTION_END); 679 MHD_OPTION_END);
692 if (NULL == httpd) 680 if (NULL == httpd)
693 { 681 {
694 GNUNET_SCHEDULER_shutdown (); 682 GNUNET_SCHEDULER_shutdown ();
695 return; 683 return;
696 } 684 }
697 685 /* Load plugins */
698 686 GNUNET_PLUGIN_load_all ("libgnunet_plugin_rest",
699 687 (void *) cfg,
700 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 688 &load_plugin,
701 &do_shutdown, NULL); 689 NULL);
690 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
691 &do_shutdown, NULL);
702} 692}
703 693
704/** 694/**
@@ -713,34 +703,33 @@ run (void *cls, char *const *args, const char *cfgfile,
713int 703int
714main (int argc, char *const *argv) 704main (int argc, char *const *argv)
715{ 705{
716 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 706 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
717 {'p', "port", NULL, 707 {'p', "port", NULL,
718 gettext_noop ("listen on specified port (default: 7776)"), 1, 708 gettext_noop ("listen on specified port (default: 7776)"), 1,
719 &GNUNET_GETOPT_set_ulong, &port}, 709 &GNUNET_GETOPT_set_ulong, &port},
720 GNUNET_GETOPT_OPTION_END 710 GNUNET_GETOPT_OPTION_END
721 }; 711 };
722 static const char* err_page = 712 static const char* err_page =
723 "<html><head><title>gnunet-rest-service</title>" 713 "{}";
724 "</head><body>ERROR</body></html>"; 714 int ret;
725 int ret; 715
726 716 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
727 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 717 return 2;
728 return 2; 718 GNUNET_log_setup ("gnunet-rest-service", "WARNING", NULL);
729 GNUNET_log_setup ("gnunet-rest-service", "WARNING", NULL); 719 failure_response = MHD_create_response_from_buffer (strlen(err_page),
730 failure_response = MHD_create_response_from_buffer (strlen(err_page), 720 (void*)err_page,
731 (void*)err_page, 721 MHD_RESPMEM_PERSISTENT);
732 MHD_RESPMEM_PERSISTENT); 722 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
733 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 723 "Start\n");
734 "Start\n"); 724 ret =
735 ret = 725 (GNUNET_OK ==
736 (GNUNET_OK == 726 GNUNET_PROGRAM_run (argc, argv, "gnunet-rest-server",
737 GNUNET_PROGRAM_run (argc, argv, "gnunet-rest-service", 727 _("GNUnet REST server"),
738 _("GNUnet REST service"), 728 options,
739 options, 729 &run, NULL)) ? 0: 1;
740 &run, NULL)) ? 0: 1; 730 MHD_destroy_response (failure_response);
741 MHD_destroy_response (failure_response); 731 GNUNET_free_non_null ((char *) argv);
742 GNUNET_free_non_null ((char *) argv); 732 return ret;
743 return ret;
744} 733}
745 734
746/* end of gnunet-rest-service.c */ 735/* end of gnunet-rest-server.c */