aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Schanzenbach <mschanzenbach@posteo.de>2015-03-11 12:54:22 +0000
committerMartin Schanzenbach <mschanzenbach@posteo.de>2015-03-11 12:54:22 +0000
commit86882d3e761fe639e833446ccfef8d31e0e42998 (patch)
tree0d7c054aed321dff8b3a0f469068e546bdfc7f2b
parent4bbe44b460db015f8c784d4b699f6d381d2d96c3 (diff)
downloadgnunet-86882d3e761fe639e833446ccfef8d31e0e42998.tar.gz
gnunet-86882d3e761fe639e833446ccfef8d31e0e42998.zip
REST Service
-rw-r--r--configure.ac10
-rw-r--r--src/Makefile.am5
-rw-r--r--src/gns/Makefile.am16
-rw-r--r--src/gns/plugin_rest_gns.c658
-rw-r--r--src/include/gnunet_rest_plugin.h100
-rw-r--r--src/rest/Makefile.am31
-rw-r--r--src/rest/gnunet-rest-server.c746
-rw-r--r--src/rest/rest.conf0
8 files changed, 1565 insertions, 1 deletions
diff --git a/configure.ac b/configure.ac
index bb4f74366..c0bf33421 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1216,6 +1216,15 @@ AC_ARG_ENABLE([wachs],
1216AC_MSG_RESULT($enable_wachs) 1216AC_MSG_RESULT($enable_wachs)
1217AM_CONDITIONAL([HAVE_WACHS], [test "x$enable_wachs" = "xyes"]) 1217AM_CONDITIONAL([HAVE_WACHS], [test "x$enable_wachs" = "xyes"])
1218 1218
1219# REST API
1220AC_MSG_CHECKING(whether to compile REST API)
1221AC_ARG_ENABLE([rest],
1222 [AS_HELP_STRING([--enable-rest], [enable REST])],
1223 [enable_rest=${enableval}],
1224 [enable_rest=no])
1225AC_MSG_RESULT($enable_rest)
1226AM_CONDITIONAL([HAVE_REST], [test "x$enable_rest" = "xyes"])
1227
1219# should malicious code be compiled (should only be used for testing)? 1228# should malicious code be compiled (should only be used for testing)?
1220AC_MSG_CHECKING(whether to compile malicious code) 1229AC_MSG_CHECKING(whether to compile malicious code)
1221AC_ARG_ENABLE([malicious], 1230AC_ARG_ENABLE([malicious],
@@ -1520,6 +1529,7 @@ src/util/Makefile
1520src/util/resolver.conf 1529src/util/resolver.conf
1521src/vpn/Makefile 1530src/vpn/Makefile
1522src/vpn/vpn.conf 1531src/vpn/vpn.conf
1532src/rest/Makefile
1523pkgconfig/Makefile 1533pkgconfig/Makefile
1524pkgconfig/gnunetarm.pc 1534pkgconfig/gnunetarm.pc
1525pkgconfig/gnunetats.pc 1535pkgconfig/gnunetats.pc
diff --git a/src/Makefile.am b/src/Makefile.am
index 0b4d24371..79730b0bb 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -48,6 +48,10 @@ if HAVE_POSTGRESQL
48 POSTGRES_DIR = postgres 48 POSTGRES_DIR = postgres
49endif 49endif
50 50
51if HAVE_REST
52 REST_DIR = rest
53endif
54
51SUBDIRS = \ 55SUBDIRS = \
52 include $(INTLEMU_SUBDIRS) \ 56 include $(INTLEMU_SUBDIRS) \
53 util \ 57 util \
@@ -60,6 +64,7 @@ SUBDIRS = \
60 peerinfo \ 64 peerinfo \
61 $(MYSQL_DIR) \ 65 $(MYSQL_DIR) \
62 $(POSTGRES_DIR) \ 66 $(POSTGRES_DIR) \
67 $(REST_DIR) \
63 datacache \ 68 datacache \
64 datastore \ 69 datastore \
65 template \ 70 template \
diff --git a/src/gns/Makefile.am b/src/gns/Makefile.am
index 687c5f43f..0d9ef5de5 100644
--- a/src/gns/Makefile.am
+++ b/src/gns/Makefile.am
@@ -85,12 +85,26 @@ bin_PROGRAMS += gnunet-bcd
85endif 85endif
86endif 86endif
87 87
88if HAVE_REST
89REST_PLUGIN = libgnunet_plugin_rest_gns.la
90endif
91
88bin_SCRIPTS = gnunet-gns-proxy-setup-ca 92bin_SCRIPTS = gnunet-gns-proxy-setup-ca
89 93
90plugin_LTLIBRARIES = \ 94plugin_LTLIBRARIES = \
91 libgnunet_plugin_block_gns.la \ 95 libgnunet_plugin_block_gns.la \
92 libgnunet_plugin_gnsrecord_gns.la 96 libgnunet_plugin_gnsrecord_gns.la \
97 $(REST_PLUGIN)
93 98
99libgnunet_plugin_rest_gns_la_SOURCES = \
100 plugin_rest_gns.c
101libgnunet_plugin_rest_gns_la_LIBADD = \
102 $(top_builddir)/src/gns/libgnunetgns.la \
103 $(top_builddir)/src/identity/libgnunetidentity.la \
104 $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \
105 $(LTLIBINTL) -ljansson
106libgnunet_plugin_rest_gns_la_LDFLAGS = \
107 $(GN_PLUGIN_LDFLAGS)
94 108
95libgnunet_plugin_gnsrecord_gns_la_SOURCES = \ 109libgnunet_plugin_gnsrecord_gns_la_SOURCES = \
96 plugin_gnsrecord_gns.c 110 plugin_gnsrecord_gns.c
diff --git a/src/gns/plugin_rest_gns.c b/src/gns/plugin_rest_gns.c
new file mode 100644
index 000000000..bd28c23f3
--- /dev/null
+++ b/src/gns/plugin_rest_gns.c
@@ -0,0 +1,658 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 Christian Grothoff (and other contributing authors)
4
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
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
14
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
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20/**
21 * @author Martin Schanzenbach
22 * @file gns/plugin_rest_gns.c
23 * @brief GNUnet GNS REST plugin
24 *
25 */
26
27#include "platform.h"
28#include "gnunet_rest_plugin.h"
29#include <gnunet_dnsparser_lib.h>
30#include <gnunet_identity_service.h>
31#include <gnunet_gnsrecord_lib.h>
32#include <gnunet_namestore_service.h>
33#include <gnunet_gns_service.h>
34#include <jansson.h>
35
36#define API_NAMESPACE "gns"
37
38/**
39 * @brief struct returned by the initialization function of the plugin
40 */
41struct Plugin
42{
43 const struct GNUNET_CONFIGURATION_Handle *cfg;
44};
45
46const struct GNUNET_CONFIGURATION_Handle *cfg;
47
48struct LookupHandle
49{
50 /**
51 * Handle to GNS service.
52 */
53 struct GNUNET_GNS_Handle *gns;
54
55 /**
56 * Desired timeout for the lookup (default is no timeout).
57 */
58 struct GNUNET_TIME_Relative timeout;
59
60 /**
61 * Handle to lookup request
62 */
63 struct GNUNET_GNS_LookupRequest *lookup_request;
64
65 /**
66 * Lookup an ego with the identity service.
67 */
68 struct GNUNET_IDENTITY_EgoLookup *el;
69
70 /**
71 * Handle for identity service.
72 */
73 struct GNUNET_IDENTITY_Handle *identity;
74
75 /**
76 * Active operation on identity service.
77 */
78 struct GNUNET_IDENTITY_Operation *id_op;
79 /**
80 * ID of a task associated with the resolution process.
81 */
82 struct GNUNET_SCHEDULER_Task * timeout_task;
83
84 json_t *json_root;
85
86 GNUNET_REST_ResultProcessor proc;
87
88 char *name;
89
90 const char *ego_str;
91
92 const char *pkey_str;
93
94 int type;
95
96 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
97
98 struct GNUNET_CRYPTO_EcdsaPublicKey pkeym;
99
100 enum GNUNET_GNS_LocalOptions options;
101
102 struct GNUNET_CRYPTO_EcdsaPrivateKey shorten_key;
103 void *proc_cls;
104};
105
106void
107cleanup_handle (struct LookupHandle *handle)
108{
109 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
110 "Cleaning up\n");
111 if (NULL != handle->json_root)
112 json_decref (handle->json_root);
113
114 if (NULL != handle->name)
115 GNUNET_free (handle->name);
116 if (NULL != handle->el)
117 {
118 GNUNET_IDENTITY_ego_lookup_cancel (handle->el);
119 handle->el = NULL;
120 }
121 if (NULL != handle->id_op)
122 {
123 GNUNET_IDENTITY_cancel (handle->id_op);
124 handle->id_op = NULL;
125 }
126 if (NULL != handle->lookup_request)
127 {
128 GNUNET_GNS_lookup_cancel (handle->lookup_request);
129 handle->lookup_request = NULL;
130 }
131 if (NULL != handle->identity)
132 {
133 GNUNET_IDENTITY_disconnect (handle->identity);
134 handle->identity = NULL;
135 }
136 if (NULL != handle->gns)
137 {
138 GNUNET_GNS_disconnect (handle->gns);
139 handle->gns = NULL;
140 }
141
142 if (NULL != handle->timeout_task)
143 {
144 GNUNET_SCHEDULER_cancel (handle->timeout_task);
145 }
146 GNUNET_free (handle);
147}
148
149
150/**
151 * Task run on shutdown. Cleans up everything.
152 *
153 * @param cls unused
154 * @param tc scheduler context
155 */
156static void
157do_error (void *cls,
158 const struct GNUNET_SCHEDULER_TaskContext *tc)
159{
160 struct LookupHandle *handle = cls;
161
162 cleanup_handle (handle);
163 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
164}
165
166/**
167 * Function called with the result of a GNS lookup.
168 *
169 * @param cls the 'const char *' name that was resolved
170 * @param rd_count number of records returned
171 * @param rd array of @a rd_count records with the results
172 */
173static void
174process_lookup_result (void *cls, uint32_t rd_count,
175 const struct GNUNET_GNSRECORD_Data *rd)
176{
177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
178 "result \n");
179 struct LookupHandle *handle = cls;
180 uint32_t i;
181 const char *typename;
182 char *string_val;
183 char *result;
184 json_t *result_root;
185 json_t *result_name;
186 json_t *result_array;
187 json_t *record_obj;
188
189
190 result_root = json_object();
191 result_name = json_string (handle->name);
192 result_array = json_array();
193 json_object_set (result_root, "name", result_name);
194 json_decref (result_name);
195
196 handle->lookup_request = NULL;
197
198 for (i=0; i<rd_count; i++)
199 {
200 if ( (rd[i].record_type != handle->type) &&
201 (GNUNET_GNSRECORD_TYPE_ANY != handle->type) )
202 continue;
203 typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
204 string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
205 rd[i].data,
206 rd[i].data_size);
207 if (NULL == string_val)
208 {
209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
210 "Record %u of type %d malformed, skipping\n",
211 (unsigned int) i,
212 (int) rd[i].record_type);
213 continue;
214 }
215 else
216 {
217 record_obj = json_object();
218 json_object_set_new (record_obj, "type", json_string (typename));
219 json_object_set_new (record_obj, "value", json_string (string_val));
220 json_array_append (result_array, record_obj);
221 json_decref (record_obj);
222 }
223 GNUNET_free (string_val);
224 }
225 json_object_set (result_root, "query_result", result_array);
226 json_decref (result_array);
227 result = json_dumps (result_root, JSON_COMPACT);
228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Result %s\n", result);
229 json_decref (result_root);
230 handle->proc (handle->proc_cls, result, strlen (result), GNUNET_OK);
231 GNUNET_free (result);
232 cleanup_handle (handle);
233}
234
235/**
236 * Perform the actual resolution, starting with the zone
237 * identified by the given public key and the shorten zone.
238 *
239 * @param pkey public key to use for the zone, can be NULL
240 * @param shorten_key private key used for shortening, can be NULL
241 */
242static void
243lookup_with_keys (struct LookupHandle *handle, const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key)
244{
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
246 "Lookup w/ keys \n");
247 if (UINT32_MAX == handle->type)
248 {
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250 _("Invalid typename specified, assuming `ANY'\n"));
251 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
252 }
253
254 if (NULL != handle->name)
255 {
256 handle->lookup_request = GNUNET_GNS_lookup (handle->gns,
257 handle->name,
258 &handle->pkey,
259 handle->type,
260 handle->options,
261 shorten_key,
262 &process_lookup_result,
263 handle);
264 }
265 else
266 {
267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
268 _("Please specify name to lookup!\n"));
269 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
270 cleanup_handle (handle);
271 return;
272 }
273}
274
275/**
276 * Method called to with the ego we are to use for shortening
277 * during the lookup.
278 *
279 * @param cls closure contains the public key to use
280 * @param ego ego handle, NULL if not found
281 * @param ctx context for application to store data for this ego
282 * (during the lifetime of this process, initially NULL)
283 * @param name name assigned by the user for this ego,
284 * NULL if the user just deleted the ego and it
285 * must thus no longer be used
286 */
287static void
288identity_shorten_cb (void *cls,
289 struct GNUNET_IDENTITY_Ego *ego,
290 void **ctx,
291 const char *name)
292{
293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
294 "shorten \n");
295 struct LookupHandle *handle = cls;
296
297 handle->id_op = NULL;
298 if (NULL == ego)
299 lookup_with_keys (handle, NULL);
300 else
301 lookup_with_keys (handle,
302 GNUNET_IDENTITY_ego_get_private_key (ego));
303}
304
305/**
306 * Perform the actual resolution, starting with the zone
307 * identified by the given public key.
308 *
309 * @param pkey public key to use for the zone
310 */
311static void
312lookup_with_public_key (struct LookupHandle *handle)
313{
314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
315 "Lookup w/ pkey \n");
316 handle->pkeym = handle->pkey;
317 GNUNET_break (NULL == handle->id_op);
318 handle->id_op = GNUNET_IDENTITY_get (handle->identity,
319 "gns-short",
320 &identity_shorten_cb,
321 handle);
322 if (NULL == handle->id_op)
323 {
324 GNUNET_break (0);
325 lookup_with_keys (handle, NULL);
326 }
327}
328
329/**
330 * Method called to with the ego we are to use for the lookup,
331 * when the ego is determined by a name.
332 *
333 * @param cls closure (NULL, unused)
334 * @param ego ego handle, NULL if not found
335 */
336static void
337identity_zone_cb (void *cls,
338 const struct GNUNET_IDENTITY_Ego *ego)
339{
340 struct LookupHandle *handle = cls;
341
342 handle->el = NULL;
343 if (NULL == ego)
344 {
345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
346 _("Ego for not found, cannot perform lookup.\n"));
347 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
348 cleanup_handle (handle);
349 return;
350 }
351 else
352 {
353 GNUNET_IDENTITY_ego_get_public_key (ego, &handle->pkey);
354 lookup_with_public_key (handle);
355 }
356 json_decref(handle->json_root);
357}
358
359/**
360 * Method called to with the ego we are to use for the lookup,
361 * when the ego is the one for the default master zone.
362 *
363 * @param cls closure (NULL, unused)
364 * @param ego ego handle, NULL if not found
365 * @param ctx context for application to store data for this ego
366 * (during the lifetime of this process, initially NULL)
367 * @param name name assigned by the user for this ego,
368 * NULL if the user just deleted the ego and it
369 * must thus no longer be used
370 */
371static void
372identity_master_cb (void *cls,
373 struct GNUNET_IDENTITY_Ego *ego,
374 void **ctx,
375 const char *name)
376{
377 const char *dot;
378 struct LookupHandle *handle = cls;
379
380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
381 "Master cb \n");
382 handle->id_op = NULL;
383 if (NULL == ego)
384 {
385 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
386 _("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);
388 cleanup_handle (handle);
389 return;
390 }
391 GNUNET_IDENTITY_ego_get_public_key (ego, &handle->pkey);
392 /* main name is our own master zone, do no look for that in the DHT */
393 handle->options = GNUNET_GNS_LO_LOCAL_MASTER;
394 /* if the name is of the form 'label.gnu', never go to the DHT */
395 dot = NULL;
396 if (NULL != handle->name)
397 dot = strchr (handle->name, '.');
398 if ( (NULL != dot) &&
399 (0 == strcasecmp (dot, ".gnu")) )
400 handle->options = GNUNET_GNS_LO_NO_DHT;
401 lookup_with_public_key (handle);
402}
403
404int
405parse_url (const char *url, struct LookupHandle *handle)
406{
407 char *name;
408 char *type;
409 char tmp_url[strlen(url)+1];
410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Parsing %s\n", url);
411 strcpy (tmp_url, url);
412
413 char *tok = strtok ((char*)tmp_url, "/");
414
415
416 if (NULL == tok)
417 return GNUNET_SYSERR;
418
419 name = strtok (NULL, "/");
420
421 if (NULL == name)
422 return GNUNET_SYSERR;
423
424 GNUNET_asprintf (&handle->name,
425 "%s",
426 name);
427
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;
444}
445
446int
447parse_json (const char *data, size_t data_size, struct LookupHandle *handle)
448{
449 json_error_t error;
450 json_t *pkey_json;
451 json_t *ego_json;
452 json_t *options_json;
453
454 char term_data[data_size+1];
455 term_data[data_size] = '\0';
456
457 memcpy (term_data, data, data_size);
458
459 handle->json_root = json_loads (term_data, 0, &error);
460
461 if (NULL == handle->json_root)
462 {
463 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, error.text);
464 return GNUNET_SYSERR;
465 }
466
467 if(!json_is_object(handle->json_root))
468 {
469 return GNUNET_SYSERR;
470 }
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
479 pkey_json = json_object_get (handle->json_root, "pkey");
480 if(json_is_string(pkey_json))
481 {
482 handle->pkey_str = json_string_value (pkey_json);
483 }
484
485 options_json = json_object_get (handle->json_root, "options");
486 if(json_is_integer (options_json))
487 {
488 handle->options = json_integer_value (options_json);
489 }
490 return GNUNET_OK;
491}
492
493
494/**
495 * Function processing the REST call
496 *
497 * @param method HTTP method
498 * @param url URL of the HTTP request
499 * @param data body of the HTTP request (optional)
500 * @param data_size length of the body
501 * @param proc callback function for the result
502 * @param proc_cls closure for callback function
503 * @return GNUNET_OK if request accepted
504 */
505void
506rest_gns_process_request(const char *method,
507 const char *url,
508 const char *data,
509 size_t data_size,
510 GNUNET_REST_ResultProcessor proc,
511 void *proc_cls)
512{
513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
514 "In plugin\n");
515 struct LookupHandle *handle = GNUNET_new (struct LookupHandle);
516
517 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL; //TODO read from json
518
519 //parse name and type from url
520 if (GNUNET_OK != parse_url (url, handle))
521 {
522 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing url...\n");
523 proc (proc_cls, NULL, 0, GNUNET_SYSERR);
524 cleanup_handle (handle);
525 return;
526 }
527
528 handle->proc_cls = proc_cls;
529 handle->proc = proc;
530
531 if (0 < data_size)
532 {
533 if (GNUNET_OK != parse_json (data, data_size, handle))
534 {
535 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing json...\n");
536 proc (proc_cls, NULL, 0, GNUNET_SYSERR);
537 cleanup_handle (handle);
538 return;
539 }
540 }
541 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
542 "Connecting...\n");
543 handle->gns = GNUNET_GNS_connect (cfg);
544 handle->identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
545
546 handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout,
547 &do_error, handle);
548 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
549 "Connected\n");
550
551 if (NULL == handle->gns)
552 {
553 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
554 "Connecting to GNS failed\n");
555 proc (proc_cls, NULL, 0, GNUNET_SYSERR);
556 cleanup_handle (handle);
557 return;
558 }
559
560 if (NULL != handle->pkey_str)
561 {
562 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
563 "pkey_str\n");
564 if (GNUNET_OK !=
565 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->pkey_str,
566 strlen(handle->pkey_str),
567 &(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 {
601 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
602 "gns_master\n");
603 GNUNET_break (NULL == handle->id_op);
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 }
610}
611
612/**
613 * Entry point for the plugin.
614 *
615 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
616 * @return NULL on error, otherwise the plugin context
617 */
618void *
619libgnunet_plugin_rest_gns_init (void *cls)
620{
621 static struct Plugin plugin;
622 cfg = cls;
623 struct GNUNET_REST_Plugin *api;
624
625 if (NULL != plugin.cfg)
626 return NULL; /* can only initialize once! */
627 memset (&plugin, 0, sizeof (struct Plugin));
628 plugin.cfg = cfg;
629 api = GNUNET_new (struct GNUNET_REST_Plugin);
630 api->cls = &plugin;
631 api->name = API_NAMESPACE;
632 api->process_request = &rest_gns_process_request;
633 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
634 _("GNS REST API initialized\n"));
635 return api;
636}
637
638
639/**
640 * Exit point from the plugin.
641 *
642 * @param cls the plugin context (as returned by "init")
643 * @return always NULL
644 */
645void *
646libgnunet_plugin_namestore_sqlite_done (void *cls)
647{
648 struct GNUNET_REST_Plugin *api = cls;
649 struct Plugin *plugin = api->cls;
650
651 plugin->cfg = NULL;
652 GNUNET_free (api);
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "GNS REST plugin is finished\n");
655 return NULL;
656}
657
658/* end of plugin_rest_gns.c */
diff --git a/src/include/gnunet_rest_plugin.h b/src/include/gnunet_rest_plugin.h
new file mode 100644
index 000000000..d690343ba
--- /dev/null
+++ b/src/include/gnunet_rest_plugin.h
@@ -0,0 +1,100 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 Christian Grothoff (and other contributing authors)
4
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
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
14
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
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20/**
21 * @author Martin Schanzenbach
22 * @file include/gnunet_rest_plugin.h
23 * @brief GNUnet service REST plugin header
24 *
25 */
26#ifndef GNUNET_REST_PLUGIN_H
27#define GNUNET_REST_PLUGIN_H
28
29#include "gnunet_util_lib.h"
30
31#ifdef __cplusplus
32extern "C"
33{
34#if 0 /* keep Emacsens' auto-indent happy */
35}
36#endif
37#endif
38
39/**
40 * Iterator called on obtained result for a REST result.
41 *
42 * @param cls closure
43 * @param data REST result
44 * @param data_len length of result
45 * @param status status code (HTTP)
46 */
47typedef void (*GNUNET_REST_ResultProcessor) (void *cls,
48 const char *data,
49 size_t data_len,
50 int status);
51
52/**
53 * @brief struct returned by the initialization function of the plugin
54 */
55struct GNUNET_REST_Plugin
56{
57
58 /**
59 *
60 * The closure of the plugin
61 *
62 */
63 void *cls;
64
65 /**
66 * Plugin name. Used as the namespace for the API.
67 * e.g. http://hostname:port/<name>
68 */
69 char *name;
70
71 /**
72 * Function to process a REST call
73 *
74 * @param method the HTTP method called
75 * @param url the relative url accessed
76 * @param data the REST data (can be NULL)
77 * @param data_size the length of the data
78 * @param proc the callback for result
79 * @param proc_cls closure for callback
80 */
81 void (*process_request) (const char *method,
82 const char *url,
83 const char *data,
84 size_t data_size,
85 GNUNET_REST_ResultProcessor proc,
86 void *proc_cls);
87
88};
89
90
91#if 0 /* keep Emacsens' auto-indent happy */
92{
93#endif
94#ifdef __cplusplus
95}
96#endif
97
98/* end of gnunet_rest_plugin.h */
99#endif
100
diff --git a/src/rest/Makefile.am b/src/rest/Makefile.am
new file mode 100644
index 000000000..00021cce2
--- /dev/null
+++ b/src/rest/Makefile.am
@@ -0,0 +1,31 @@
1# This Makefile.am is in the public domain
2AM_CPPFLAGS = -I$(top_srcdir)/src/include
3
4plugindir = $(libdir)/gnunet
5
6pkgcfgdir= $(pkgdatadir)/config.d/
7
8libexecdir= $(pkglibdir)/libexec/
9
10pkgcfg_DATA = \
11 rest.conf
12
13
14if MINGW
15 WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols
16endif
17
18if USE_COVERAGE
19 AM_CFLAGS = --coverage -O0
20 XLIBS = -lgcov
21endif
22
23libexec_PROGRAMS = \
24 gnunet-rest-server
25
26gnunet_rest_server_SOURCES = \
27 gnunet-rest-server.c
28
29gnunet_rest_server_LDADD = \
30 $(top_builddir)/src/util/libgnunetutil.la \
31 $(GN_LIBINTL) -lmicrohttpd
diff --git a/src/rest/gnunet-rest-server.c b/src/rest/gnunet-rest-server.c
new file mode 100644
index 000000000..fa1e87a3a
--- /dev/null
+++ b/src/rest/gnunet-rest-server.c
@@ -0,0 +1,746 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 Christian Grothoff (and other contributing authors)
4
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
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
14
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
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20/**
21 * @author Martin Schanzenbach
22 * @file src/rest/gnunet-rest-server.c
23 * @brief REST service for GNUnet services
24 *
25 */
26#include "platform.h"
27#include <microhttpd.h>
28#include "gnunet_util_lib.h"
29#include "gnunet_rest_plugin.h"
30
31
32/**
33 * Default Socks5 listen port.
34 */
35#define GNUNET_REST_SERVICE_PORT 7776
36
37/**
38 * Maximum supported length for a URI.
39 * Should die. @deprecated
40 */
41#define MAX_HTTP_URI_LENGTH 2048
42
43/**
44 * Port for plaintext HTTP.
45 */
46#define HTTP_PORT 80
47
48/**
49 * Port for HTTPS.
50 */
51#define HTTPS_PORT 443
52
53/**
54 * After how long do we clean up unused MHD SSL/TLS instances?
55 */
56#define MHD_CACHE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
57
58#define GN_REST_STATE_INIT 0
59#define GN_REST_STATE_UPLOAD 1
60#define GN_REST_STATE_RECV 2
61
62/**
63 * The task ID
64 */
65struct GNUNET_SCHEDULER_Task * httpd_task;
66
67/**
68 * is this an ssl daemon? //TODO
69 */
70int is_ssl;
71
72/**
73 * The port the service is running on (default 7776)
74 */
75static unsigned long port = GNUNET_REST_SERVICE_PORT;
76
77/**
78 * The listen socket of the service for IPv4
79 */
80static struct GNUNET_NETWORK_Handle *lsock4;
81
82/**
83 * The listen socket of the service for IPv6
84 */
85static struct GNUNET_NETWORK_Handle *lsock6;
86
87/**
88 * The listen task ID for IPv4
89 */
90static struct GNUNET_SCHEDULER_Task * ltask4;
91
92/**
93 * The listen task ID for IPv6
94 */
95static struct GNUNET_SCHEDULER_Task * ltask6;
96
97/**
98 * Daemon for HTTP
99 */
100static struct MHD_Daemon *httpd;
101
102/**
103 * Response we return on failures.
104 */
105static struct MHD_Response *failure_response;
106
107/**
108 * Our configuration.
109 */
110static const struct GNUNET_CONFIGURATION_Handle *cfg;
111
112/**
113 * Map of loaded plugins.
114 */
115struct GNUNET_CONTAINER_MultiHashMap *plugin_map;
116
117/**
118 * MHD Connection handle
119 */
120struct MhdConnectionHandle
121{
122 struct MHD_Connection *con;
123
124 struct MHD_Response *response;
125
126 struct GNUNET_REST_Plugin *plugin;
127
128 int status;
129
130 int state;
131};
132
133/* ************************* Global helpers ********************* */
134
135
136/**
137 * Task run whenever HTTP server operations are pending.
138 *
139 * @param cls NULL
140 * @param tc sched context
141 */
142static void
143do_httpd (void *cls,
144 const struct GNUNET_SCHEDULER_TaskContext *tc);
145
146
147/**
148 * Run MHD now, we have extra data ready for the callback.
149 *
150 * @param hd the daemon to run now.
151 */
152static void
153run_mhd_now ()
154{
155 if (NULL !=
156 httpd_task)
157 GNUNET_SCHEDULER_cancel (httpd_task);
158 httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd,
159 NULL);
160}
161
162/**
163 * Plugin result callback
164 *
165 * @param cls closure (MHD connection handle)
166 * @param data the data to return to the caller
167 * @param len length of the data
168 * @param status GNUNET_OK if successful
169 */
170void
171plugin_callback (void *cls,
172 const char *data,
173 size_t len,
174 int status)
175{
176 struct MhdConnectionHandle *handle = cls;
177 struct MHD_Response *resp = MHD_create_response_from_buffer (len,
178 (void*)data,
179 MHD_RESPMEM_MUST_COPY);
180 (void) MHD_add_response_header (resp,MHD_HTTP_HEADER_CONTENT_TYPE,"application/json");
181 handle->status = status;
182 handle->response = resp;
183 run_mhd_now();
184}
185
186/* ********************************* MHD response generation ******************* */
187
188/**
189 * Main MHD callback for handling requests.
190 *
191 * @param cls unused
192 * @param con MHD connection handle
193 * @param url the url in the request
194 * @param meth the HTTP method used ("GET", "PUT", etc.)
195 * @param ver the HTTP version string (i.e. "HTTP/1.1")
196 * @param upload_data the data being uploaded (excluding HEADERS,
197 * for a POST that fits into memory and that is encoded
198 * with a supported encoding, the POST data will NOT be
199 * given in upload_data and is instead available as
200 * part of MHD_get_connection_values; very large POST
201 * data *will* be made available incrementally in
202 * upload_data)
203 * @param upload_data_size set initially to the size of the
204 * @a upload_data provided; the method must update this
205 * value to the number of bytes NOT processed;
206 * @param con_cls pointer to location where we store the 'struct Request'
207 * @return MHD_YES if the connection was handled successfully,
208 * MHD_NO if the socket must be closed due to a serious
209 * error while handling the request
210 */
211static int
212create_response (void *cls,
213 struct MHD_Connection *con,
214 const char *url,
215 const char *meth,
216 const char *ver,
217 const char *upload_data,
218 size_t *upload_data_size,
219 void **con_cls)
220{
221 char *plugin_name;
222 struct GNUNET_HashCode key;
223 struct MhdConnectionHandle *con_handle;
224
225 con_handle = *con_cls;
226
227 if (NULL == *con_cls)
228 {
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230 "New connection %s\n", url);
231 char tmp_url[strlen(url)+1];
232 strcpy (tmp_url, url);
233 con_handle = GNUNET_new (struct MhdConnectionHandle);
234 con_handle->con = con;
235 con_handle->state = GN_REST_STATE_INIT;
236 *con_cls = con_handle;
237
238 plugin_name = strtok(tmp_url, "/");
239
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");
254 GNUNET_free (con_handle);
255 MHD_queue_response (con,
256 MHD_HTTP_INTERNAL_SERVER_ERROR,
257 failure_response);
258 }
259 return MHD_YES;
260 }
261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
262 "Size %d\n", *upload_data_size);
263 if (GN_REST_STATE_INIT == con_handle->state)
264 {
265 if (0 != *upload_data_size)
266 {
267 con_handle->state = GN_REST_STATE_UPLOAD;
268
269 con_handle->plugin->process_request (meth,
270 url,
271 upload_data,
272 *upload_data_size,
273 &plugin_callback,
274 con_handle);
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 }
289 if (NULL != con_handle->response)
290 {
291
292GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
293 "Queueing response from plugin with MHD\n");
294 if (GNUNET_OK == con_handle->status) {
295 return MHD_queue_response (con,
296 MHD_HTTP_OK,
297 con_handle->response);
298 } else {
299 return MHD_queue_response (con,
300 MHD_HTTP_BAD_REQUEST,
301 con_handle->response);
302 }
303 }
304 return MHD_YES;
305}
306
307/* ******************** MHD HTTP setup and event loop ******************** */
308
309/**
310 * Function called when MHD decides that we are done with a connection.
311 *
312 * @param cls NULL
313 * @param connection connection handle
314 * @param con_cls value as set by the last call to
315 * the MHD_AccessHandlerCallback, should be our handle
316 * @param toe reason for request termination (ignored)
317 */
318static void
319mhd_completed_cb (void *cls,
320 struct MHD_Connection *connection,
321 void **con_cls,
322 enum MHD_RequestTerminationCode toe)
323{
324
325 if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe)
326 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
327 "MHD encountered error handling request: %d\n",
328 toe);
329 *con_cls = NULL;
330}
331
332/**
333 * Kill the MHD daemon.
334 */
335static void
336kill_httpd ()
337{
338 MHD_stop_daemon (httpd);
339 if (NULL != httpd_task)
340 {
341 GNUNET_SCHEDULER_cancel (httpd_task);
342 httpd_task = NULL;
343 }
344}
345
346/**
347 * Task run whenever HTTP server is idle for too long. Kill it.
348 *
349 * @param cls NULL
350 * @param tc sched context
351 */
352static void
353kill_httpd_task (void *cls,
354 const struct GNUNET_SCHEDULER_TaskContext *tc)
355{
356 httpd_task = NULL;
357 kill_httpd ();
358}
359/**
360 * Schedule MHD. This function should be called initially when an
361 * MHD is first getting its client socket, and will then automatically
362 * always be called later whenever there is work to be done.
363 *
364 * @param hd the daemon to schedule
365 */
366static void
367schedule_httpd ()
368{
369 fd_set rs;
370 fd_set ws;
371 fd_set es;
372 struct GNUNET_NETWORK_FDSet *wrs;
373 struct GNUNET_NETWORK_FDSet *wws;
374 int max;
375 int haveto;
376 MHD_UNSIGNED_LONG_LONG timeout;
377 struct GNUNET_TIME_Relative tv;
378
379 FD_ZERO (&rs);
380 FD_ZERO (&ws);
381 FD_ZERO (&es);
382 max = -1;
383 if (MHD_YES != MHD_get_fdset (httpd, &rs, &ws, &es, &max))
384 {
385 kill_httpd ();
386 return;
387 }
388 haveto = MHD_get_timeout (httpd, &timeout);
389 if (MHD_YES == haveto)
390 tv.rel_value_us = (uint64_t) timeout * 1000LL;
391 else
392 tv = GNUNET_TIME_UNIT_FOREVER_REL;
393 if (-1 != max)
394 {
395 wrs = GNUNET_NETWORK_fdset_create ();
396 wws = GNUNET_NETWORK_fdset_create ();
397 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
398 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
399 }
400 else
401 {
402 wrs = NULL;
403 wws = NULL;
404 }
405 if (NULL != httpd_task)
406 GNUNET_SCHEDULER_cancel (httpd_task);
407 if ( (MHD_YES != haveto) &&
408 (-1 == max))
409 {
410 /* daemon is idle, kill after timeout */
411 httpd_task = GNUNET_SCHEDULER_add_delayed (MHD_CACHE_TIMEOUT,
412 &kill_httpd_task,
413 NULL);
414 }
415 else
416 {
417 httpd_task =
418 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
419 tv, wrs, wws,
420 &do_httpd, NULL);
421 }
422 if (NULL != wrs)
423 GNUNET_NETWORK_fdset_destroy (wrs);
424 if (NULL != wws)
425 GNUNET_NETWORK_fdset_destroy (wws);
426}
427
428/**
429 * Task run whenever HTTP server operations are pending.
430 *
431 * @param cls NULL
432 * @param tc scheduler context
433 */
434static void
435do_httpd (void *cls,
436 const struct GNUNET_SCHEDULER_TaskContext *tc)
437{
438 httpd_task = NULL;
439 MHD_run (httpd);
440 schedule_httpd ();
441}
442
443/**
444 * Accept new incoming connections
445 *
446 * @param cls the closure with the lsock4 or lsock6
447 * @param tc the scheduler context
448 */
449static void
450do_accept (void *cls,
451 const struct GNUNET_SCHEDULER_TaskContext *tc)
452{
453 struct GNUNET_NETWORK_Handle *lsock = cls;
454 struct GNUNET_NETWORK_Handle *s;
455 int fd;
456 const struct sockaddr *addr;
457 socklen_t len;
458
459 if (lsock == lsock4)
460 ltask4 = NULL;
461 else
462 ltask6 = NULL;
463 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
464 return;
465 if (lsock == lsock4)
466 ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
467 lsock,
468 &do_accept, lsock);
469 else
470 ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
471 lsock,
472 &do_accept, lsock);
473 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
474 if (NULL == s)
475 {
476 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
477 return;
478 }
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "Got an inbound connection, waiting for data\n");
481 fd = GNUNET_NETWORK_get_fd (s);
482 addr = GNUNET_NETWORK_get_addr (s);
483 len = GNUNET_NETWORK_get_addrlen (s);
484 if (MHD_YES != MHD_add_connection (httpd, fd, addr, len))
485 {
486 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
487 _("Failed to pass client to MHD\n"));
488 return;
489 }
490
491 schedule_httpd ();
492}
493
494/**
495 * Task run on shutdown
496 *
497 * @param cls closure
498 * @param tc task context
499 */
500static void
501do_shutdown (void *cls,
502 const struct GNUNET_SCHEDULER_TaskContext *tc)
503{
504 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
505 "Shutting down...\n");
506 kill_httpd ();
507}
508
509/**
510 * Create an IPv4 listen socket bound to our port.
511 *
512 * @return NULL on error
513 */
514static struct GNUNET_NETWORK_Handle *
515bind_v4 ()
516{
517 struct GNUNET_NETWORK_Handle *ls;
518 struct sockaddr_in sa4;
519 int eno;
520
521 memset (&sa4, 0, sizeof (sa4));
522 sa4.sin_family = AF_INET;
523 sa4.sin_port = htons (port);
524#if HAVE_SOCKADDR_IN_SIN_LEN
525 sa4.sin_len = sizeof (sa4);
526#endif
527 ls = GNUNET_NETWORK_socket_create (AF_INET,
528 SOCK_STREAM,
529 0);
530 if (NULL == ls)
531 return NULL;
532 if (GNUNET_OK !=
533 GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4,
534 sizeof (sa4)))
535 {
536 eno = errno;
537 GNUNET_NETWORK_socket_close (ls);
538 errno = eno;
539 return NULL;
540 }
541 return ls;
542}
543
544/**
545 * Create an IPv6 listen socket bound to our port.
546 *
547 * @return NULL on error
548 */
549static struct GNUNET_NETWORK_Handle *
550bind_v6 ()
551{
552 struct GNUNET_NETWORK_Handle *ls;
553 struct sockaddr_in6 sa6;
554 int eno;
555
556 memset (&sa6, 0, sizeof (sa6));
557 sa6.sin6_family = AF_INET6;
558 sa6.sin6_port = htons (port);
559#if HAVE_SOCKADDR_IN_SIN_LEN
560 sa6.sin6_len = sizeof (sa6);
561#endif
562 ls = GNUNET_NETWORK_socket_create (AF_INET6,
563 SOCK_STREAM,
564 0);
565 if (NULL == ls)
566 return NULL;
567 if (GNUNET_OK !=
568 GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa6,
569 sizeof (sa6)))
570 {
571 eno = errno;
572 GNUNET_NETWORK_socket_close (ls);
573 errno = eno;
574 return NULL;
575 }
576 return ls;
577}
578
579/**
580 * Callback for plugin load
581 *
582 * @param cls NULL
583 * @param libname the name of the library loaded
584 * @param lib_ret the object returned by the plugin initializer
585 */
586void
587load_plugin (void *cls,
588 const char *libname,
589 void *lib_ret)
590{
591 struct GNUNET_REST_Plugin *plugin = lib_ret;
592 struct GNUNET_HashCode key;
593 if (NULL == lib_ret)
594 {
595 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
596 "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);
611 return;
612 }
613 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
614 "Loaded plugin `%s'\n",
615 libname);
616}
617
618/**
619 * Main function that will be run
620 *
621 * @param cls closure
622 * @param args remaining command-line arguments
623 * @param cfgfile name of the configuration file used (for saving, can be NULL)
624 * @param c configuration
625 */
626static void
627run (void *cls, char *const *args, const char *cfgfile,
628 const struct GNUNET_CONFIGURATION_Handle *c)
629{
630 cfg = c;
631
632 plugin_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
633
634 /* Load plugins */
635 GNUNET_PLUGIN_load_all ("libgnunet_plugin_rest",
636 (void *) cfg,
637 &load_plugin,
638 NULL);
639
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 {
647 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock6, 5))
648 {
649 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
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 }
659 lsock4 = bind_v4 ();
660 if (NULL == lsock4)
661 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
662 else
663 {
664 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock4, 5))
665 {
666 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
667 GNUNET_NETWORK_socket_close (lsock4);
668 lsock4 = NULL;
669 }
670 else
671 {
672 ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
673 lsock4, &do_accept, lsock4);
674 }
675 }
676 if ( (NULL == lsock4) &&
677 (NULL == lsock6) )
678 {
679 GNUNET_SCHEDULER_shutdown ();
680 return;
681 }
682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
683 "Service listens on port %u\n",
684 port);
685 httpd = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET,
686 0,
687 NULL, NULL,
688 &create_response, NULL,
689 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
690 MHD_OPTION_NOTIFY_COMPLETED, &mhd_completed_cb, NULL,
691 MHD_OPTION_END);
692 if (NULL == httpd)
693 {
694 GNUNET_SCHEDULER_shutdown ();
695 return;
696 }
697
698
699
700 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
701 &do_shutdown, NULL);
702}
703
704/**
705 *
706 * The main function for gnunet-rest-service
707 *
708 * @param argc number of arguments from the cli
709 * @param argv command line arguments
710 * @return 0 ok, 1 on error
711 *
712 */
713int
714main (int argc, char *const *argv)
715{
716 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
717 {'p', "port", NULL,
718 gettext_noop ("listen on specified port (default: 7776)"), 1,
719 &GNUNET_GETOPT_set_ulong, &port},
720 GNUNET_GETOPT_OPTION_END
721 };
722 static const char* err_page =
723 "<html><head><title>gnunet-rest-service</title>"
724 "</head><body>ERROR</body></html>";
725 int ret;
726
727 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
728 return 2;
729 GNUNET_log_setup ("gnunet-rest-service", "WARNING", NULL);
730 failure_response = MHD_create_response_from_buffer (strlen(err_page),
731 (void*)err_page,
732 MHD_RESPMEM_PERSISTENT);
733 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
734 "Start\n");
735 ret =
736 (GNUNET_OK ==
737 GNUNET_PROGRAM_run (argc, argv, "gnunet-rest-service",
738 _("GNUnet REST service"),
739 options,
740 &run, NULL)) ? 0: 1;
741 MHD_destroy_response (failure_response);
742 GNUNET_free_non_null ((char *) argv);
743 return ret;
744}
745
746/* end of gnunet-rest-service.c */
diff --git a/src/rest/rest.conf b/src/rest/rest.conf
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/src/rest/rest.conf