diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2015-03-11 12:54:22 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2015-03-11 12:54:22 +0000 |
commit | 86882d3e761fe639e833446ccfef8d31e0e42998 (patch) | |
tree | 0d7c054aed321dff8b3a0f469068e546bdfc7f2b /src/rest | |
parent | 4bbe44b460db015f8c784d4b699f6d381d2d96c3 (diff) | |
download | gnunet-86882d3e761fe639e833446ccfef8d31e0e42998.tar.gz gnunet-86882d3e761fe639e833446ccfef8d31e0e42998.zip |
REST Service
Diffstat (limited to 'src/rest')
-rw-r--r-- | src/rest/Makefile.am | 31 | ||||
-rw-r--r-- | src/rest/gnunet-rest-server.c | 746 | ||||
-rw-r--r-- | src/rest/rest.conf | 0 |
3 files changed, 777 insertions, 0 deletions
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 | ||
2 | AM_CPPFLAGS = -I$(top_srcdir)/src/include | ||
3 | |||
4 | plugindir = $(libdir)/gnunet | ||
5 | |||
6 | pkgcfgdir= $(pkgdatadir)/config.d/ | ||
7 | |||
8 | libexecdir= $(pkglibdir)/libexec/ | ||
9 | |||
10 | pkgcfg_DATA = \ | ||
11 | rest.conf | ||
12 | |||
13 | |||
14 | if MINGW | ||
15 | WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols | ||
16 | endif | ||
17 | |||
18 | if USE_COVERAGE | ||
19 | AM_CFLAGS = --coverage -O0 | ||
20 | XLIBS = -lgcov | ||
21 | endif | ||
22 | |||
23 | libexec_PROGRAMS = \ | ||
24 | gnunet-rest-server | ||
25 | |||
26 | gnunet_rest_server_SOURCES = \ | ||
27 | gnunet-rest-server.c | ||
28 | |||
29 | gnunet_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 | */ | ||
65 | struct GNUNET_SCHEDULER_Task * httpd_task; | ||
66 | |||
67 | /** | ||
68 | * is this an ssl daemon? //TODO | ||
69 | */ | ||
70 | int is_ssl; | ||
71 | |||
72 | /** | ||
73 | * The port the service is running on (default 7776) | ||
74 | */ | ||
75 | static unsigned long port = GNUNET_REST_SERVICE_PORT; | ||
76 | |||
77 | /** | ||
78 | * The listen socket of the service for IPv4 | ||
79 | */ | ||
80 | static struct GNUNET_NETWORK_Handle *lsock4; | ||
81 | |||
82 | /** | ||
83 | * The listen socket of the service for IPv6 | ||
84 | */ | ||
85 | static struct GNUNET_NETWORK_Handle *lsock6; | ||
86 | |||
87 | /** | ||
88 | * The listen task ID for IPv4 | ||
89 | */ | ||
90 | static struct GNUNET_SCHEDULER_Task * ltask4; | ||
91 | |||
92 | /** | ||
93 | * The listen task ID for IPv6 | ||
94 | */ | ||
95 | static struct GNUNET_SCHEDULER_Task * ltask6; | ||
96 | |||
97 | /** | ||
98 | * Daemon for HTTP | ||
99 | */ | ||
100 | static struct MHD_Daemon *httpd; | ||
101 | |||
102 | /** | ||
103 | * Response we return on failures. | ||
104 | */ | ||
105 | static struct MHD_Response *failure_response; | ||
106 | |||
107 | /** | ||
108 | * Our configuration. | ||
109 | */ | ||
110 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
111 | |||
112 | /** | ||
113 | * Map of loaded plugins. | ||
114 | */ | ||
115 | struct GNUNET_CONTAINER_MultiHashMap *plugin_map; | ||
116 | |||
117 | /** | ||
118 | * MHD Connection handle | ||
119 | */ | ||
120 | struct 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 | */ | ||
142 | static void | ||
143 | do_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 | */ | ||
152 | static void | ||
153 | run_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 | */ | ||
170 | void | ||
171 | plugin_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 | */ | ||
211 | static int | ||
212 | create_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 | |||
292 | GNUNET_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 | */ | ||
318 | static void | ||
319 | mhd_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 | */ | ||
335 | static void | ||
336 | kill_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 | */ | ||
352 | static void | ||
353 | kill_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 | */ | ||
366 | static void | ||
367 | schedule_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 | */ | ||
434 | static void | ||
435 | do_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 | */ | ||
449 | static void | ||
450 | do_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 | */ | ||
500 | static void | ||
501 | do_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 | */ | ||
514 | static struct GNUNET_NETWORK_Handle * | ||
515 | bind_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 | */ | ||
549 | static struct GNUNET_NETWORK_Handle * | ||
550 | bind_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 | */ | ||
586 | void | ||
587 | load_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 | */ | ||
626 | static void | ||
627 | run (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 | */ | ||
713 | int | ||
714 | main (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 | |||