diff options
author | Phil <phil.buschmann@tum.de> | 2018-07-26 02:31:30 +0200 |
---|---|---|
committer | Phil <phil.buschmann@tum.de> | 2018-07-26 02:31:30 +0200 |
commit | f7ca27a73e69a8c224d65768be3416ff1388c1d7 (patch) | |
tree | 7530a6def74a55885780285162b18b4c2b353380 /src/json | |
parent | cc577a227d6a5ae8ef75e0fa91ef98ced2d2b743 (diff) | |
download | gnunet-f7ca27a73e69a8c224d65768be3416ff1388c1d7.tar.gz gnunet-f7ca27a73e69a8c224d65768be3416ff1388c1d7.zip |
change namestore, json handling; fix identity, gns
Diffstat (limited to 'src/json')
-rw-r--r-- | src/json/Makefile.am | 3 | ||||
-rw-r--r-- | src/json/json_generator.c | 35 | ||||
-rw-r--r-- | src/json/json_gnsrecord.c | 163 |
3 files changed, 200 insertions, 1 deletions
diff --git a/src/json/Makefile.am b/src/json/Makefile.am index 16c0450d9..04f27fec7 100644 --- a/src/json/Makefile.am +++ b/src/json/Makefile.am | |||
@@ -16,7 +16,8 @@ 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_gnsrecord.c | ||
20 | libgnunetjson_la_LIBADD = \ | 21 | libgnunetjson_la_LIBADD = \ |
21 | $(top_builddir)/src/util/libgnunetutil.la \ | 22 | $(top_builddir)/src/util/libgnunetutil.la \ |
22 | -ljansson \ | 23 | -ljansson \ |
diff --git a/src/json/json_generator.c b/src/json/json_generator.c index dd6df4f74..7b24a3c12 100644 --- a/src/json/json_generator.c +++ b/src/json/json_generator.c | |||
@@ -157,5 +157,40 @@ GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig) | |||
157 | return ret; | 157 | return ret; |
158 | } | 158 | } |
159 | 159 | ||
160 | /** | ||
161 | * Convert Gns record to JSON. | ||
162 | * | ||
163 | * @param rname name of record | ||
164 | * @param rd record data | ||
165 | * @return corresponding JSON encoding | ||
166 | */ | ||
167 | json_t * | ||
168 | GNUNET_JSON_from_gns_record (const char* rname, | ||
169 | const struct GNUNET_GNSRECORD_Data *rd) | ||
170 | { | ||
171 | struct GNUNET_TIME_Absolute expiration_time; | ||
172 | const char *expiration_time_str; | ||
173 | const char *record_type_str; | ||
174 | char *value_str; | ||
175 | json_t *ret; | ||
176 | int flags; | ||
177 | |||
178 | value_str = GNUNET_GNSRECORD_value_to_string(rd->record_type,rd->data,rd->data_size); | ||
179 | expiration_time = GNUNET_GNSRECORD_record_get_expiration_time(1, rd); | ||
180 | expiration_time_str = GNUNET_STRINGS_absolute_time_to_string(expiration_time); | ||
181 | flags = (int)rd->flags; //maybe necessary | ||
182 | record_type_str = GNUNET_GNSRECORD_number_to_typename(rd->record_type); | ||
183 | |||
184 | // ? for possible NULL values | ||
185 | ret = json_pack("{s:s?,s:s?,s:s?,s:i,s:s?}", | ||
186 | "value", value_str, | ||
187 | "type", record_type_str, | ||
188 | "expiration_time", expiration_time_str, | ||
189 | "flag", flags, | ||
190 | "label", rname); | ||
191 | GNUNET_free_non_null(value_str); | ||
192 | return ret; | ||
193 | } | ||
194 | |||
160 | 195 | ||
161 | /* End of json/json_generator.c */ | 196 | /* End of json/json_generator.c */ |
diff --git a/src/json/json_gnsrecord.c b/src/json/json_gnsrecord.c new file mode 100644 index 000000000..48b78f38b --- /dev/null +++ b/src/json/json_gnsrecord.c | |||
@@ -0,0 +1,163 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2009-2013 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 | /** | ||
20 | * @file json/json_gnsrecord.c | ||
21 | * @brief JSON handling of GNS record data | ||
22 | * @author Philippe Buschmann | ||
23 | */ | ||
24 | #include "platform.h" | ||
25 | #include "gnunet_util_lib.h" | ||
26 | #include "gnunet_gnsrecord_lib.h" | ||
27 | #include "gnunet_json_lib.h" | ||
28 | |||
29 | #define GNUNET_JSON_GNSRECORD_VALUE "value" | ||
30 | #define GNUNET_JSON_GNSRECORD_TYPE "type" | ||
31 | #define GNUNET_JSON_GNSRECORD_EXPIRATION_TIME "expiration_time" | ||
32 | #define GNUNET_JSON_GNSRECORD_FLAG "flag" | ||
33 | #define GNUNET_JSON_GNSRECORD_LABEL "label" | ||
34 | #define GNUNET_JSON_GNSRECORD_NEVER "never" | ||
35 | |||
36 | |||
37 | /** | ||
38 | * Parse given JSON object to gns record | ||
39 | * | ||
40 | * @param cls closure, NULL | ||
41 | * @param root the json object representing data | ||
42 | * @param spec where to write the data | ||
43 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | ||
44 | */ | ||
45 | static int | ||
46 | parse_gnsrecordobject (void *cls, | ||
47 | json_t *root, | ||
48 | struct GNUNET_JSON_Specification *spec) | ||
49 | { | ||
50 | struct GNUNET_GNSRECORD_Data *gnsrecord_object; | ||
51 | struct GNUNET_TIME_Absolute abs_expiration_time; | ||
52 | int unpack_state=0; | ||
53 | const char *value; | ||
54 | const char *expiration_time; | ||
55 | const char *record_type; | ||
56 | const char *label; | ||
57 | int flag; | ||
58 | void *rdata; | ||
59 | size_t rdata_size; | ||
60 | |||
61 | GNUNET_assert(NULL != root); | ||
62 | if(!json_is_object(root)) | ||
63 | { | ||
64 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
65 | "Error json is not array nor object!\n"); | ||
66 | return GNUNET_SYSERR; | ||
67 | } | ||
68 | //interpret single gns record | ||
69 | unpack_state = json_unpack(root, | ||
70 | "{s:s, s:s, s:s, s?:i, s:s!}", | ||
71 | GNUNET_JSON_GNSRECORD_VALUE, &value, | ||
72 | GNUNET_JSON_GNSRECORD_TYPE, &record_type, | ||
73 | GNUNET_JSON_GNSRECORD_EXPIRATION_TIME, &expiration_time, | ||
74 | GNUNET_JSON_GNSRECORD_FLAG, &flag, | ||
75 | GNUNET_JSON_GNSRECORD_LABEL, &label); | ||
76 | if (GNUNET_SYSERR == unpack_state) | ||
77 | { | ||
78 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
79 | "Error json object has a wrong format!\n"); | ||
80 | return GNUNET_SYSERR; | ||
81 | } | ||
82 | gnsrecord_object = GNUNET_new (struct GNUNET_GNSRECORD_Data); | ||
83 | gnsrecord_object->record_type = GNUNET_GNSRECORD_typename_to_number(record_type); | ||
84 | if (UINT32_MAX == gnsrecord_object->record_type) | ||
85 | { | ||
86 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Unsupported type"); | ||
87 | return GNUNET_SYSERR; | ||
88 | } | ||
89 | if (GNUNET_OK | ||
90 | != GNUNET_GNSRECORD_string_to_value (gnsrecord_object->record_type, | ||
91 | value, | ||
92 | &rdata, | ||
93 | &rdata_size)) | ||
94 | { | ||
95 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Value invalid for record type"); | ||
96 | return GNUNET_SYSERR; | ||
97 | } | ||
98 | |||
99 | gnsrecord_object->data = rdata; | ||
100 | gnsrecord_object->data_size = rdata_size; | ||
101 | |||
102 | if (0 == strcmp (expiration_time, GNUNET_JSON_GNSRECORD_NEVER)) | ||
103 | { | ||
104 | gnsrecord_object->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us; | ||
105 | } | ||
106 | else if (GNUNET_OK | ||
107 | == GNUNET_STRINGS_fancy_time_to_absolute (expiration_time, | ||
108 | &abs_expiration_time)) | ||
109 | { | ||
110 | gnsrecord_object->expiration_time = abs_expiration_time.abs_value_us; | ||
111 | } | ||
112 | else | ||
113 | { | ||
114 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Value invalid for expiration time"); | ||
115 | return GNUNET_SYSERR; | ||
116 | } | ||
117 | gnsrecord_object->flags = (enum GNUNET_GNSRECORD_Flags)flag; | ||
118 | *(struct GNUNET_GNSRECORD_Data **) spec->ptr = gnsrecord_object; | ||
119 | return GNUNET_OK; | ||
120 | } | ||
121 | |||
122 | /** | ||
123 | * Cleanup data left from parsing RSA public key. | ||
124 | * | ||
125 | * @param cls closure, NULL | ||
126 | * @param[out] spec where to free the data | ||
127 | */ | ||
128 | static void | ||
129 | clean_gnsrecordobject (void *cls, struct GNUNET_JSON_Specification *spec) | ||
130 | { | ||
131 | struct GNUNET_GNSRECORD_Data **gnsrecord_object; | ||
132 | gnsrecord_object = (struct GNUNET_GNSRECORD_Data **) spec->ptr; | ||
133 | if (NULL != *gnsrecord_object) | ||
134 | { | ||
135 | if (NULL != (*gnsrecord_object)->data) | ||
136 | GNUNET_free((char*)(*gnsrecord_object)->data); | ||
137 | |||
138 | GNUNET_free(*gnsrecord_object); | ||
139 | *gnsrecord_object = NULL; | ||
140 | } | ||
141 | } | ||
142 | |||
143 | /** | ||
144 | * JSON Specification for GNS Records. | ||
145 | * | ||
146 | * @param gnsrecord_object struct of GNUNET_GNSRECORD_Data to fill | ||
147 | * @return JSON Specification | ||
148 | */ | ||
149 | struct GNUNET_JSON_Specification | ||
150 | GNUNET_JSON_spec_gnsrecord_data (struct GNUNET_GNSRECORD_Data **gnsrecord_object) | ||
151 | { | ||
152 | struct GNUNET_JSON_Specification ret = { | ||
153 | .parser = &parse_gnsrecordobject, | ||
154 | .cleaner = &clean_gnsrecordobject, | ||
155 | .cls = NULL, | ||
156 | .field = NULL, | ||
157 | .ptr = gnsrecord_object, | ||
158 | .ptr_size = 0, | ||
159 | .size_ptr = NULL | ||
160 | }; | ||
161 | *gnsrecord_object = NULL; | ||
162 | return ret; | ||
163 | } | ||