diff options
author | Phil <phil.buschmann@tum.de> | 2018-07-25 21:57:49 +0200 |
---|---|---|
committer | Phil <phil.buschmann@tum.de> | 2018-07-25 21:57:49 +0200 |
commit | cc577a227d6a5ae8ef75e0fa91ef98ced2d2b743 (patch) | |
tree | 07084e1aa8a37dc0dc4d15fe0724801d7867157a /src | |
parent | 4992eacc10bedaa0edfa03b401253408c6267798 (diff) | |
download | gnunet-cc577a227d6a5ae8ef75e0fa91ef98ced2d2b743.tar.gz gnunet-cc577a227d6a5ae8ef75e0fa91ef98ced2d2b743.zip |
-wip namestore api, changed adding gnsrecord
Diffstat (limited to 'src')
-rw-r--r-- | src/gnsrecord/Makefile.am | 1 | ||||
-rw-r--r-- | src/gnsrecord/gnsrecord.c | 127 | ||||
-rw-r--r-- | src/include/gnunet_gnsrecord_lib.h | 8 | ||||
-rw-r--r-- | src/include/gnunet_json_lib.h | 65 | ||||
-rw-r--r-- | src/json/Makefile.am | 3 | ||||
-rw-r--r-- | src/json/json_parser.c | 169 | ||||
-rw-r--r-- | src/namestore/plugin_rest_namestore.c | 124 |
7 files changed, 190 insertions, 307 deletions
diff --git a/src/gnsrecord/Makefile.am b/src/gnsrecord/Makefile.am index f840a31a4..47d83169f 100644 --- a/src/gnsrecord/Makefile.am +++ b/src/gnsrecord/Makefile.am | |||
@@ -39,6 +39,7 @@ libgnunetgnsrecord_la_SOURCES = \ | |||
39 | gnsrecord_misc.c | 39 | gnsrecord_misc.c |
40 | libgnunetgnsrecord_la_LIBADD = \ | 40 | libgnunetgnsrecord_la_LIBADD = \ |
41 | $(top_builddir)/src/util/libgnunetutil.la \ | 41 | $(top_builddir)/src/util/libgnunetutil.la \ |
42 | -ljansson \ | ||
42 | $(GN_LIBINTL) | 43 | $(GN_LIBINTL) |
43 | libgnunetgnsrecord_la_LDFLAGS = \ | 44 | libgnunetgnsrecord_la_LDFLAGS = \ |
44 | $(GN_LIB_LDFLAGS) $(WINFLAGS) \ | 45 | $(GN_LIB_LDFLAGS) $(WINFLAGS) \ |
diff --git a/src/gnsrecord/gnsrecord.c b/src/gnsrecord/gnsrecord.c index da34846e7..30a0ffa83 100644 --- a/src/gnsrecord/gnsrecord.c +++ b/src/gnsrecord/gnsrecord.c | |||
@@ -28,7 +28,9 @@ | |||
28 | #include "gnunet_constants.h" | 28 | #include "gnunet_constants.h" |
29 | #include "gnunet_gnsrecord_lib.h" | 29 | #include "gnunet_gnsrecord_lib.h" |
30 | #include "gnunet_gnsrecord_plugin.h" | 30 | #include "gnunet_gnsrecord_plugin.h" |
31 | #include "gnunet_json_lib.h" | ||
31 | #include "gnunet_tun_lib.h" | 32 | #include "gnunet_tun_lib.h" |
33 | #include <jansson.h> | ||
32 | 34 | ||
33 | 35 | ||
34 | #define LOG(kind,...) GNUNET_log_from (kind, "gnsrecord",__VA_ARGS__) | 36 | #define LOG(kind,...) GNUNET_log_from (kind, "gnsrecord",__VA_ARGS__) |
@@ -245,5 +247,130 @@ GNUNET_GNSRECORD_number_to_typename (uint32_t type) | |||
245 | return NULL; | 247 | return NULL; |
246 | } | 248 | } |
247 | 249 | ||
250 | /** | ||
251 | * Parse given JSON object to gns record | ||
252 | * | ||
253 | * @param cls closure, NULL | ||
254 | * @param root the json object representing data | ||
255 | * @param spec where to write the data | ||
256 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | ||
257 | */ | ||
258 | static int | ||
259 | parse_gnsrecordobject (void *cls, | ||
260 | json_t *root, | ||
261 | struct GNUNET_JSON_Specification *spec) | ||
262 | { | ||
263 | struct GNUNET_GNSRECORD_Data *gnsrecord_object; | ||
264 | struct GNUNET_TIME_Absolute abs_expiration_time; | ||
265 | int unpack_state=0; | ||
266 | const char *data; | ||
267 | const char *expiration_date; | ||
268 | const char *record_type; | ||
269 | const char *dummy_value; | ||
270 | int flag; | ||
271 | void *rdata; | ||
272 | size_t rdata_size; | ||
273 | |||
274 | if(!json_is_object(root)) | ||
275 | { | ||
276 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
277 | "Error json is not array nor object!\n"); | ||
278 | return GNUNET_SYSERR; | ||
279 | } | ||
280 | //interpret single gns record | ||
281 | unpack_state = json_unpack(root, | ||
282 | "{s:s, s:s, s:s, s?:i, s:s!}", | ||
283 | "value", &data, | ||
284 | "type", &record_type, | ||
285 | "expiration_time", &expiration_date, | ||
286 | "flag", &flag, | ||
287 | "label", &dummy_value); | ||
288 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, | ||
289 | "{value:%s, type:%s, expire:%s, flag:%i}", | ||
290 | data, | ||
291 | record_type, | ||
292 | expiration_date, | ||
293 | flag); | ||
294 | if (GNUNET_SYSERR == unpack_state) | ||
295 | { | ||
296 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, | ||
297 | "Error json object has a wrong format!\n"); | ||
298 | return GNUNET_SYSERR; | ||
299 | } | ||
300 | //TODO test | ||
301 | gnsrecord_object = GNUNET_new (struct GNUNET_GNSRECORD_Data); | ||
302 | gnsrecord_object->record_type = GNUNET_GNSRECORD_typename_to_number(record_type); | ||
303 | if (GNUNET_OK | ||
304 | != GNUNET_GNSRECORD_string_to_value (gnsrecord_object->record_type, | ||
305 | data, &rdata, | ||
306 | &rdata_size)) | ||
307 | { | ||
308 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR,"Value invalid for record type"); | ||
309 | return GNUNET_SYSERR; | ||
310 | } | ||
311 | gnsrecord_object->data = rdata; | ||
312 | gnsrecord_object->data_size = rdata_size; | ||
313 | |||
314 | if (0 == strcmp (expiration_date, "never")) | ||
315 | { | ||
316 | gnsrecord_object->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us; | ||
317 | } | ||
318 | else if (GNUNET_OK | ||
319 | == GNUNET_STRINGS_fancy_time_to_absolute (expiration_date, | ||
320 | &abs_expiration_time)) | ||
321 | { | ||
322 | gnsrecord_object->expiration_time = abs_expiration_time.abs_value_us; | ||
323 | } | ||
324 | else | ||
325 | { | ||
326 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Value invalid for record type"); | ||
327 | return GNUNET_SYSERR; | ||
328 | } | ||
329 | gnsrecord_object->flags = (enum GNUNET_GNSRECORD_Flags)flag; | ||
330 | *(struct GNUNET_GNSRECORD_Data **) spec->ptr = gnsrecord_object; | ||
331 | return GNUNET_OK; | ||
332 | } | ||
333 | |||
334 | /** | ||
335 | * Cleanup data left from parsing RSA public key. | ||
336 | * | ||
337 | * @param cls closure, NULL | ||
338 | * @param[out] spec where to free the data | ||
339 | */ | ||
340 | static void | ||
341 | clean_gnsrecordobject (void *cls, struct GNUNET_JSON_Specification *spec) | ||
342 | { | ||
343 | struct GNUNET_GNSRECORD_Data **gnsrecord_object; | ||
344 | gnsrecord_object = (struct GNUNET_GNSRECORD_Data **) spec->ptr; | ||
345 | if (NULL != *gnsrecord_object) | ||
346 | { | ||
347 | if (NULL != (*gnsrecord_object)->data) | ||
348 | GNUNET_free((char*)(*gnsrecord_object)->data); | ||
349 | GNUNET_free(*gnsrecord_object); | ||
350 | *gnsrecord_object = NULL; | ||
351 | } | ||
352 | } | ||
353 | |||
354 | /** | ||
355 | * JSON Specification for GNS Records. | ||
356 | * | ||
357 | * @param gnsrecord_object struct of GNUNET_GNSRECORD_Data to fill | ||
358 | * @return JSON Specification | ||
359 | */ | ||
360 | struct GNUNET_JSON_Specification | ||
361 | GNUNET_JSON_spec_gnsrecord_data (struct GNUNET_GNSRECORD_Data **gnsrecord_object) | ||
362 | { | ||
363 | struct GNUNET_JSON_Specification ret = { | ||
364 | .parser = &parse_gnsrecordobject, | ||
365 | .cleaner = &clean_gnsrecordobject, | ||
366 | .cls = NULL, | ||
367 | .field = NULL, | ||
368 | .ptr = gnsrecord_object, | ||
369 | .ptr_size = 0, | ||
370 | .size_ptr = NULL | ||
371 | }; | ||
372 | *gnsrecord_object = NULL; | ||
373 | return ret; | ||
374 | } | ||
248 | 375 | ||
249 | /* end of gnsrecord.c */ | 376 | /* end of gnsrecord.c */ |
diff --git a/src/include/gnunet_gnsrecord_lib.h b/src/include/gnunet_gnsrecord_lib.h index 20846238b..2eaa304de 100644 --- a/src/include/gnunet_gnsrecord_lib.h +++ b/src/include/gnunet_gnsrecord_lib.h | |||
@@ -447,6 +447,14 @@ GNUNET_GNSRECORD_records_deserialize (size_t len, | |||
447 | unsigned int rd_count, | 447 | unsigned int rd_count, |
448 | struct GNUNET_GNSRECORD_Data *dest); | 448 | struct GNUNET_GNSRECORD_Data *dest); |
449 | 449 | ||
450 | /** | ||
451 | * JSON Specification for GNS Records. | ||
452 | * | ||
453 | * @param gnsrecord_object struct of GNUNET_GNSRECORD_Data to fill | ||
454 | * @return JSON Specification | ||
455 | */ | ||
456 | struct GNUNET_JSON_Specification | ||
457 | GNUNET_JSON_spec_gnsrecord_data (struct GNUNET_GNSRECORD_Data **gnsrecord_object); | ||
450 | 458 | ||
451 | /* ******* general APIs relating to blocks, records and labels ******** */ | 459 | /* ******* general APIs relating to blocks, records and labels ******** */ |
452 | 460 | ||
diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h index 6340d1f41..4855f21b4 100644 --- a/src/include/gnunet_json_lib.h +++ b/src/include/gnunet_json_lib.h | |||
@@ -479,71 +479,6 @@ GNUNET_JSON_getopt (char shortName, | |||
479 | const char *description, | 479 | const char *description, |
480 | json_t **json); | 480 | json_t **json); |
481 | 481 | ||
482 | /* ****************** GETOPT JSON parser ******************* */ | ||
483 | |||
484 | struct GNUNET_REST_JSON_Data | ||
485 | { | ||
486 | /** | ||
487 | * Public key of an identity | ||
488 | */ | ||
489 | char *pubkey; | ||
490 | |||
491 | /** | ||
492 | * Name | ||
493 | */ | ||
494 | char *name; | ||
495 | |||
496 | /** | ||
497 | * Nickname | ||
498 | */ | ||
499 | char *nickname; | ||
500 | |||
501 | /** | ||
502 | * New name | ||
503 | */ | ||
504 | char *new_name; | ||
505 | |||
506 | /** | ||
507 | * Name of subsystem | ||
508 | */ | ||
509 | char *subsystem; | ||
510 | |||
511 | /** | ||
512 | * Should data be handled as public (GNUNET_YES or GNUNET_NO) | ||
513 | */ | ||
514 | int is_public; | ||
515 | |||
516 | /** | ||
517 | * Expiration date of data | ||
518 | */ | ||
519 | char *expiration_time; | ||
520 | |||
521 | /** | ||
522 | * Type of data | ||
523 | */ | ||
524 | char *type; | ||
525 | |||
526 | /** | ||
527 | * Value of data | ||
528 | */ | ||
529 | char *value; | ||
530 | |||
531 | /** | ||
532 | * Zone | ||
533 | */ | ||
534 | char *zone; | ||
535 | }; | ||
536 | /* | ||
537 | * Test | ||
538 | */ | ||
539 | int | ||
540 | GNUNET_REST_JSON_parse (struct GNUNET_REST_JSON_Data** rest_json_data, | ||
541 | json_t *json_data); | ||
542 | |||
543 | int | ||
544 | GNUNET_REST_JSON_free (struct GNUNET_REST_JSON_Data* rest_json_data); | ||
545 | |||
546 | |||
547 | #endif | 482 | #endif |
548 | 483 | ||
549 | /* end of gnunet_json_lib.h */ | 484 | /* end of gnunet_json_lib.h */ |
diff --git a/src/json/Makefile.am b/src/json/Makefile.am index 5aac23654..16c0450d9 100644 --- a/src/json/Makefile.am +++ b/src/json/Makefile.am | |||
@@ -16,8 +16,7 @@ libgnunetjson_la_SOURCES = \ | |||
16 | json.c \ | 16 | json.c \ |
17 | json_mhd.c \ | 17 | json_mhd.c \ |
18 | json_generator.c \ | 18 | json_generator.c \ |
19 | json_helper.c \ | 19 | json_helper.c |
20 | json_parser.c | ||
21 | libgnunetjson_la_LIBADD = \ | 20 | libgnunetjson_la_LIBADD = \ |
22 | $(top_builddir)/src/util/libgnunetutil.la \ | 21 | $(top_builddir)/src/util/libgnunetutil.la \ |
23 | -ljansson \ | 22 | -ljansson \ |
diff --git a/src/json/json_parser.c b/src/json/json_parser.c deleted file mode 100644 index cfe553b34..000000000 --- a/src/json/json_parser.c +++ /dev/null | |||
@@ -1,169 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2014, 2015, 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | /** | ||
19 | * @file json/json_helper.c | ||
20 | * @brief functions for REST JSON parsing | ||
21 | * @author Philippe Buschmann | ||
22 | */ | ||
23 | |||
24 | #include "platform.h" | ||
25 | #include "gnunet_util_lib.h" | ||
26 | #include "gnunet_json_lib.h" | ||
27 | |||
28 | #define GNUNET_REST_JSON_PUBKEY_ENTRY "pubkey" | ||
29 | #define GNUNET_REST_JSON_NAME_ENTRY "name" | ||
30 | #define GNUNET_REST_JSON_NICKNAME_ENTRY "nickname" | ||
31 | #define GNUNET_REST_JSON_NEWNAME_ENTRY "newname" | ||
32 | #define GNUNET_REST_JSON_SUBSYSTEM_ENTRY "subsystem" | ||
33 | #define GNUNET_REST_JSON_IS_PUBLIC_ENTRY "is_public" | ||
34 | #define GNUNET_REST_JSON_EXPIRATION_DATE_ENTRY "expiration_time" | ||
35 | #define GNUNET_REST_JSON_TYPE_ENTRY "type" | ||
36 | #define GNUNET_REST_JSON_VALUE_ENTRY "value" | ||
37 | #define GNUNET_REST_JSON_ZONE_ENTRY "zone" | ||
38 | |||
39 | |||
40 | int | ||
41 | GNUNET_REST_JSON_parse (struct GNUNET_REST_JSON_Data** output_data ,json_t *json_data) | ||
42 | { | ||
43 | struct GNUNET_REST_JSON_Data *rest_json_data; | ||
44 | json_t *cache; | ||
45 | |||
46 | rest_json_data = GNUNET_malloc(sizeof(struct GNUNET_REST_JSON_Data)); | ||
47 | |||
48 | cache = json_object_get (json_data, GNUNET_REST_JSON_EXPIRATION_DATE_ENTRY); | ||
49 | rest_json_data->expiration_time = NULL; | ||
50 | if (NULL != cache) | ||
51 | { | ||
52 | if (json_is_string(cache)) | ||
53 | { | ||
54 | rest_json_data->expiration_time = GNUNET_strdup(json_string_value(cache)); | ||
55 | } | ||
56 | } | ||
57 | cache = json_object_get (json_data, GNUNET_REST_JSON_NAME_ENTRY); | ||
58 | rest_json_data->name = NULL; | ||
59 | if (NULL != cache) | ||
60 | { | ||
61 | if (json_is_string(cache)) | ||
62 | { | ||
63 | rest_json_data->name = GNUNET_strdup(json_string_value(cache)); | ||
64 | } | ||
65 | } | ||
66 | cache = json_object_get (json_data, GNUNET_REST_JSON_NEWNAME_ENTRY); | ||
67 | rest_json_data->new_name = NULL; | ||
68 | if (NULL != cache) | ||
69 | { | ||
70 | if (json_is_string(cache)) | ||
71 | { | ||
72 | rest_json_data->new_name = GNUNET_strdup(json_string_value(cache)); | ||
73 | } | ||
74 | } | ||
75 | cache = json_object_get (json_data, GNUNET_REST_JSON_NICKNAME_ENTRY); | ||
76 | rest_json_data->nickname = NULL; | ||
77 | if (NULL != cache) | ||
78 | { | ||
79 | if (json_is_string(cache)) | ||
80 | { | ||
81 | rest_json_data->nickname = GNUNET_strdup(json_string_value(cache)); | ||
82 | } | ||
83 | } | ||
84 | cache = json_object_get (json_data, GNUNET_REST_JSON_PUBKEY_ENTRY); | ||
85 | rest_json_data->pubkey = NULL; | ||
86 | if (NULL != cache) | ||
87 | { | ||
88 | if (json_is_string(cache)) | ||
89 | { | ||
90 | rest_json_data->pubkey = GNUNET_strdup(json_string_value(cache)); | ||
91 | } | ||
92 | } | ||
93 | cache = json_object_get (json_data, GNUNET_REST_JSON_SUBSYSTEM_ENTRY); | ||
94 | rest_json_data->subsystem = NULL; | ||
95 | if (NULL != cache) | ||
96 | { | ||
97 | if (json_is_string(cache)) | ||
98 | { | ||
99 | rest_json_data->subsystem = GNUNET_strdup(json_string_value(cache)); | ||
100 | } | ||
101 | } | ||
102 | cache = json_object_get (json_data, GNUNET_REST_JSON_TYPE_ENTRY); | ||
103 | rest_json_data->type = NULL; | ||
104 | if (NULL != cache) | ||
105 | { | ||
106 | if (json_is_string(cache)) | ||
107 | { | ||
108 | rest_json_data->type = GNUNET_strdup(json_string_value(cache)); | ||
109 | } | ||
110 | } | ||
111 | cache = json_object_get (json_data, GNUNET_REST_JSON_VALUE_ENTRY); | ||
112 | rest_json_data->value = NULL; | ||
113 | if (NULL != cache) | ||
114 | { | ||
115 | if (json_is_string(cache)) | ||
116 | { | ||
117 | rest_json_data->value = GNUNET_strdup(json_string_value(cache)); | ||
118 | } | ||
119 | } | ||
120 | cache = json_object_get (json_data, GNUNET_REST_JSON_ZONE_ENTRY); | ||
121 | rest_json_data->zone = NULL; | ||
122 | if (NULL != cache) | ||
123 | { | ||
124 | if (json_is_string(cache)) | ||
125 | { | ||
126 | rest_json_data->zone = GNUNET_strdup(json_string_value(cache)); | ||
127 | } | ||
128 | } | ||
129 | cache = json_object_get (json_data, GNUNET_REST_JSON_IS_PUBLIC_ENTRY); | ||
130 | if (NULL != cache) | ||
131 | { | ||
132 | if (json_is_integer(cache)) | ||
133 | { | ||
134 | rest_json_data->is_public = json_integer_value(cache); | ||
135 | } | ||
136 | } | ||
137 | *output_data = rest_json_data; | ||
138 | return GNUNET_OK; | ||
139 | } | ||
140 | |||
141 | |||
142 | |||
143 | int | ||
144 | GNUNET_REST_JSON_free (struct GNUNET_REST_JSON_Data* rest_json_data) | ||
145 | { | ||
146 | if (rest_json_data != NULL) | ||
147 | { | ||
148 | GNUNET_free_non_null(rest_json_data->expiration_time); | ||
149 | GNUNET_free_non_null(rest_json_data->name); | ||
150 | GNUNET_free_non_null(rest_json_data->new_name); | ||
151 | GNUNET_free_non_null(rest_json_data->nickname); | ||
152 | GNUNET_free_non_null(rest_json_data->pubkey); | ||
153 | GNUNET_free_non_null(rest_json_data->subsystem); | ||
154 | GNUNET_free_non_null(rest_json_data->type); | ||
155 | GNUNET_free_non_null(rest_json_data->value); | ||
156 | GNUNET_free_non_null(rest_json_data->zone); | ||
157 | } | ||
158 | GNUNET_free_non_null(rest_json_data); | ||
159 | return GNUNET_OK; | ||
160 | } | ||
161 | |||
162 | |||
163 | |||
164 | |||
165 | |||
166 | |||
167 | |||
168 | |||
169 | |||
diff --git a/src/namestore/plugin_rest_namestore.c b/src/namestore/plugin_rest_namestore.c index ab490c04f..afe010b79 100644 --- a/src/namestore/plugin_rest_namestore.c +++ b/src/namestore/plugin_rest_namestore.c | |||
@@ -2,20 +2,18 @@ | |||
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2012-2015 GNUnet e.V. | 3 | Copyright (C) 2012-2015 GNUnet e.V. |
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 it |
6 | it under the terms of the GNU General Public License as published | 6 | under the terms of the GNU Affero 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 of the License, |
8 | option) any later version. | 8 | or (at your 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 | Affero 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 Affero General Public License |
16 | along with GNUnet; see the file COPYING. If not, write to the | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | 17 | */ |
20 | /** | 18 | /** |
21 | * @author Martin Schanzenbach | 19 | * @author Martin Schanzenbach |
@@ -42,6 +40,7 @@ | |||
42 | #define GNUNET_REST_JSON_NAMESTORE_VALUE "value" | 40 | #define GNUNET_REST_JSON_NAMESTORE_VALUE "value" |
43 | #define GNUNET_REST_JSON_NAMESTORE_EXPIRATION "expiration" | 41 | #define GNUNET_REST_JSON_NAMESTORE_EXPIRATION "expiration" |
44 | #define GNUNET_REST_JSON_NAMESTORE_EXPIRED "expired" | 42 | #define GNUNET_REST_JSON_NAMESTORE_EXPIRED "expired" |
43 | #define GNUNET_REST_ERROR_UNKNOWN "Unknown Error" | ||
45 | 44 | ||
46 | #define GNUNET_REST_NAMESTORE_RD_COUNT 1 | 45 | #define GNUNET_REST_NAMESTORE_RD_COUNT 1 |
47 | 46 | ||
@@ -96,17 +95,17 @@ struct RequestHandle | |||
96 | /** | 95 | /** |
97 | * Records to store | 96 | * Records to store |
98 | */ | 97 | */ |
99 | struct GNUNET_GNSRECORD_Data *rd; | 98 | char *label_name; |
100 | 99 | ||
101 | /** | 100 | /** |
102 | * NAMESTORE Operation | 101 | * Records to store |
103 | */ | 102 | */ |
104 | struct GNUNET_NAMESTORE_QueueEntry *add_qe; | 103 | struct GNUNET_GNSRECORD_Data *rd; |
105 | 104 | ||
106 | /** | 105 | /** |
107 | * JSON data parser | 106 | * NAMESTORE Operation |
108 | */ | 107 | */ |
109 | struct GNUNET_REST_JSON_Data *json_data; | 108 | struct GNUNET_NAMESTORE_QueueEntry *add_qe; |
110 | 109 | ||
111 | /** | 110 | /** |
112 | * Response object | 111 | * Response object |
@@ -192,8 +191,9 @@ struct RequestHandle | |||
192 | * @param handle Handle to clean up | 191 | * @param handle Handle to clean up |
193 | */ | 192 | */ |
194 | static void | 193 | static void |
195 | cleanup_handle (struct RequestHandle *handle) | 194 | cleanup_handle (void *cls) |
196 | { | 195 | { |
196 | struct RequestHandle *handle = cls; | ||
197 | size_t index; | 197 | size_t index; |
198 | json_t *json_ego; | 198 | json_t *json_ego; |
199 | 199 | ||
@@ -204,6 +204,8 @@ cleanup_handle (struct RequestHandle *handle) | |||
204 | GNUNET_SCHEDULER_cancel (handle->timeout_task); | 204 | GNUNET_SCHEDULER_cancel (handle->timeout_task); |
205 | handle->timeout_task = NULL; | 205 | handle->timeout_task = NULL; |
206 | } | 206 | } |
207 | if (NULL != handle->label_name) | ||
208 | GNUNET_free(handle->label_name); | ||
207 | if (NULL != handle->url) | 209 | if (NULL != handle->url) |
208 | GNUNET_free(handle->url); | 210 | GNUNET_free(handle->url); |
209 | if (NULL != handle->emsg) | 211 | if (NULL != handle->emsg) |
@@ -243,9 +245,6 @@ cleanup_handle (struct RequestHandle *handle) | |||
243 | } | 245 | } |
244 | json_decref(handle->resp_object); | 246 | json_decref(handle->resp_object); |
245 | } | 247 | } |
246 | |||
247 | if (NULL != handle->json_data) | ||
248 | GNUNET_REST_JSON_free(handle->json_data); | ||
249 | 248 | ||
250 | GNUNET_free (handle); | 249 | GNUNET_free (handle); |
251 | } | 250 | } |
@@ -261,20 +260,22 @@ do_error (void *cls) | |||
261 | { | 260 | { |
262 | struct RequestHandle *handle = cls; | 261 | struct RequestHandle *handle = cls; |
263 | struct MHD_Response *resp; | 262 | struct MHD_Response *resp; |
264 | char *json_error; | 263 | json_t *json_error = json_object(); |
264 | char *response; | ||
265 | 265 | ||
266 | if (NULL == handle->emsg) | 266 | if (NULL == handle->emsg) |
267 | handle->emsg = GNUNET_strdup("Unknown Error"); | 267 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_UNKNOWN); |
268 | |||
269 | json_object_set_new(json_error,"error", json_string(handle->emsg)); | ||
268 | 270 | ||
269 | GNUNET_asprintf (&json_error, "{\"error\": \"%s\"}", handle->emsg); | ||
270 | |||
271 | if (0 == handle->response_code) | 271 | if (0 == handle->response_code) |
272 | handle->response_code = MHD_HTTP_OK; | 272 | handle->response_code = MHD_HTTP_OK; |
273 | 273 | response = json_dumps (json_error, 0); | |
274 | resp = GNUNET_REST_create_response (json_error); | 274 | resp = GNUNET_REST_create_response (response); |
275 | handle->proc (handle->proc_cls, resp, handle->response_code); | 275 | handle->proc (handle->proc_cls, resp, handle->response_code); |
276 | cleanup_handle (handle); | 276 | json_decref(json_error); |
277 | GNUNET_free(json_error); | 277 | GNUNET_free(response); |
278 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
278 | } | 279 | } |
279 | 280 | ||
280 | /** | 281 | /** |
@@ -287,7 +288,7 @@ namestore_iteration_error (void *cls) | |||
287 | struct MHD_Response *resp = GNUNET_REST_create_response (NULL); | 288 | struct MHD_Response *resp = GNUNET_REST_create_response (NULL); |
288 | handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; | 289 | handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; |
289 | handle->proc (handle->proc_cls, resp, handle->response_code); | 290 | handle->proc (handle->proc_cls, resp, handle->response_code); |
290 | cleanup_handle (handle); | 291 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); |
291 | } | 292 | } |
292 | 293 | ||
293 | /** | 294 | /** |
@@ -360,7 +361,7 @@ create_finished (void *cls, int32_t success, const char *emsg) | |||
360 | } | 361 | } |
361 | resp = GNUNET_REST_create_response (NULL); | 362 | resp = GNUNET_REST_create_response (NULL); |
362 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT); | 363 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT); |
363 | cleanup_handle(handle); | 364 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); |
364 | } | 365 | } |
365 | 366 | ||
366 | /** | 367 | /** |
@@ -391,7 +392,7 @@ namestore_list_finished (void *cls) | |||
391 | resp = GNUNET_REST_create_response (result_str); | 392 | resp = GNUNET_REST_create_response (result_str); |
392 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 393 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
393 | GNUNET_free_non_null (result_str); | 394 | GNUNET_free_non_null (result_str); |
394 | cleanup_handle(handle); | 395 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); |
395 | } | 396 | } |
396 | 397 | ||
397 | 398 | ||
@@ -476,34 +477,7 @@ namestore_get (struct GNUNET_REST_RequestHandle *con_handle, | |||
476 | &namestore_list_finished, | 477 | &namestore_list_finished, |
477 | handle); | 478 | handle); |
478 | } | 479 | } |
479 | 480 | /* | |
480 | int | ||
481 | check_needed_data(struct RequestHandle *handle){ | ||
482 | if(NULL == handle->json_data->name) | ||
483 | { | ||
484 | handle->emsg = GNUNET_strdup("Missing JSON parameter: name"); | ||
485 | return GNUNET_SYSERR; | ||
486 | } | ||
487 | |||
488 | if(NULL == handle->json_data->type) | ||
489 | { | ||
490 | handle->emsg = GNUNET_strdup("Missing JSON parameter: type"); | ||
491 | return GNUNET_SYSERR; | ||
492 | } | ||
493 | |||
494 | if(NULL == handle->json_data->value) | ||
495 | { | ||
496 | handle->emsg = GNUNET_strdup("Missing JSON parameter: value"); | ||
497 | return GNUNET_SYSERR; | ||
498 | } | ||
499 | |||
500 | if(NULL == handle->json_data->expiration_time) | ||
501 | { | ||
502 | handle->emsg = GNUNET_strdup("Missing JSON parameter: expiration time"); | ||
503 | return GNUNET_SYSERR; | ||
504 | } | ||
505 | return GNUNET_OK; | ||
506 | } | ||
507 | 481 | ||
508 | //TODO filter input | 482 | //TODO filter input |
509 | static int | 483 | static int |
@@ -544,7 +518,7 @@ json_to_gnsrecord (struct RequestHandle *handle) | |||
544 | rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD; | 518 | rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD; |
545 | if (1 != handle->is_public) | 519 | if (1 != handle->is_public) |
546 | rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE; | 520 | rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE; |
547 | */ | 521 | * |
548 | if (0 == strcmp (handle->json_data->expiration_time, "never")) | 522 | if (0 == strcmp (handle->json_data->expiration_time, "never")) |
549 | { | 523 | { |
550 | (*handle->rd).expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us; | 524 | (*handle->rd).expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us; |
@@ -569,7 +543,7 @@ json_to_gnsrecord (struct RequestHandle *handle) | |||
569 | } | 543 | } |
570 | return GNUNET_OK; | 544 | return GNUNET_OK; |
571 | } | 545 | } |
572 | 546 | */ | |
573 | 547 | ||
574 | /** | 548 | /** |
575 | * We're storing a new record; this requires | 549 | * We're storing a new record; this requires |
@@ -590,8 +564,9 @@ create_new_record_cont (void *cls, | |||
590 | { | 564 | { |
591 | struct RequestHandle *handle = cls; | 565 | struct RequestHandle *handle = cls; |
592 | 566 | ||
567 | |||
593 | handle->add_qe = NULL; | 568 | handle->add_qe = NULL; |
594 | if (0 != strcmp (rec_name, handle->json_data->name)) | 569 | if (0 != strcmp (rec_name, handle->label_name)) |
595 | { | 570 | { |
596 | GNUNET_break (0); | 571 | GNUNET_break (0); |
597 | do_error (handle); | 572 | do_error (handle); |
@@ -603,19 +578,18 @@ create_new_record_cont (void *cls, | |||
603 | handle->proc (handle->proc_cls, | 578 | handle->proc (handle->proc_cls, |
604 | GNUNET_REST_create_response (NULL), | 579 | GNUNET_REST_create_response (NULL), |
605 | MHD_HTTP_CONFLICT); | 580 | MHD_HTTP_CONFLICT); |
606 | cleanup_handle(handle); | 581 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); |
607 | return; | 582 | return; |
608 | } | 583 | } |
609 | handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, | 584 | handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, |
610 | &handle->zone_pkey, | 585 | &handle->zone_pkey, |
611 | handle->json_data->name, | 586 | handle->label_name, |
612 | GNUNET_REST_NAMESTORE_RD_COUNT, | 587 | GNUNET_REST_NAMESTORE_RD_COUNT, |
613 | handle->rd, | 588 | handle->rd, |
614 | &create_finished, | 589 | &create_finished, |
615 | handle); | 590 | handle); |
616 | } | 591 | } |
617 | 592 | ||
618 | |||
619 | /** | 593 | /** |
620 | * Handle namestore POST request | 594 | * Handle namestore POST request |
621 | * | 595 | * |
@@ -629,9 +603,15 @@ namestore_add (struct GNUNET_REST_RequestHandle *con_handle, | |||
629 | void *cls) | 603 | void *cls) |
630 | { | 604 | { |
631 | struct RequestHandle *handle = cls; | 605 | struct RequestHandle *handle = cls; |
606 | struct GNUNET_GNSRECORD_Data *gns_record; | ||
632 | json_t *data_js; | 607 | json_t *data_js; |
608 | json_t *name_json; | ||
633 | json_error_t err; | 609 | json_error_t err; |
634 | char term_data[handle->rest_handle->data_size + 1]; | 610 | char term_data[handle->rest_handle->data_size + 1]; |
611 | struct GNUNET_JSON_Specification gnsspec[] = { | ||
612 | GNUNET_JSON_spec_gnsrecord_data(&gns_record), | ||
613 | GNUNET_JSON_spec_end () | ||
614 | }; | ||
635 | 615 | ||
636 | if (strlen (GNUNET_REST_API_NS_NAMESTORE) != strlen (handle->url)) | 616 | if (strlen (GNUNET_REST_API_NS_NAMESTORE) != strlen (handle->url)) |
637 | { | 617 | { |
@@ -649,24 +629,26 @@ namestore_add (struct GNUNET_REST_RequestHandle *con_handle, | |||
649 | GNUNET_memcpy(term_data, handle->rest_handle->data, | 629 | GNUNET_memcpy(term_data, handle->rest_handle->data, |
650 | handle->rest_handle->data_size); | 630 | handle->rest_handle->data_size); |
651 | data_js = json_loads (term_data, JSON_DECODE_ANY, &err); | 631 | data_js = json_loads (term_data, JSON_DECODE_ANY, &err); |
652 | GNUNET_REST_JSON_parse(&handle->json_data, data_js); | 632 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (data_js, gnsspec, NULL, NULL)); |
653 | if(NULL == handle->json_data) | 633 | name_json = json_object_get(data_js, "label"); |
634 | if (!json_is_string(name_json)) | ||
654 | { | 635 | { |
655 | handle->emsg = GNUNET_strdup("Wrong data"); | 636 | handle->emsg = GNUNET_strdup("Missing name"); |
656 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 637 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
657 | return; | 638 | return; |
658 | } | 639 | } |
659 | if(GNUNET_SYSERR == check_needed_data(handle)) | 640 | handle->label_name = GNUNET_strdup(json_string_value(name_json)); |
641 | if(NULL == handle->label_name) | ||
660 | { | 642 | { |
661 | json_decref (data_js); | 643 | handle->emsg = GNUNET_strdup("Missing name"); |
662 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 644 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
663 | return; | 645 | return; |
664 | } | 646 | } |
665 | json_decref (data_js); | 647 | json_decref (data_js); |
666 | json_to_gnsrecord (handle); | 648 | handle->rd = gns_record; |
667 | handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, | 649 | handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, |
668 | &handle->zone_pkey, | 650 | &handle->zone_pkey, |
669 | handle->json_data->name, | 651 | handle->label_name, |
670 | &do_error, | 652 | &do_error, |
671 | handle, | 653 | handle, |
672 | &create_new_record_cont, | 654 | &create_new_record_cont, |
@@ -718,7 +700,7 @@ options_cont (struct GNUNET_REST_RequestHandle *con_handle, | |||
718 | "Access-Control-Allow-Methods", | 700 | "Access-Control-Allow-Methods", |
719 | allow_methods); | 701 | allow_methods); |
720 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 702 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
721 | cleanup_handle (handle); | 703 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); |
722 | return; | 704 | return; |
723 | } | 705 | } |
724 | 706 | ||