diff options
Diffstat (limited to 'src/json/json_gnsrecord.c')
-rw-r--r-- | src/json/json_gnsrecord.c | 233 |
1 files changed, 117 insertions, 116 deletions
diff --git a/src/json/json_gnsrecord.c b/src/json/json_gnsrecord.c index 2ba4da591..5991683bb 100644 --- a/src/json/json_gnsrecord.c +++ b/src/json/json_gnsrecord.c | |||
@@ -16,7 +16,7 @@ | |||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file json/json_gnsrecord.c | 22 | * @file json/json_gnsrecord.c |
@@ -35,8 +35,7 @@ | |||
35 | #define GNUNET_JSON_GNSRECORD_RECORD_NAME "record_name" | 35 | #define GNUNET_JSON_GNSRECORD_RECORD_NAME "record_name" |
36 | #define GNUNET_JSON_GNSRECORD_NEVER "never" | 36 | #define GNUNET_JSON_GNSRECORD_NEVER "never" |
37 | 37 | ||
38 | struct GnsRecordInfo | 38 | struct GnsRecordInfo { |
39 | { | ||
40 | char **name; | 39 | char **name; |
41 | 40 | ||
42 | unsigned int *rd_count; | 41 | unsigned int *rd_count; |
@@ -46,20 +45,20 @@ struct GnsRecordInfo | |||
46 | 45 | ||
47 | 46 | ||
48 | static void | 47 | static void |
49 | cleanup_recordinfo (struct GnsRecordInfo *gnsrecord_info) | 48 | cleanup_recordinfo(struct GnsRecordInfo *gnsrecord_info) |
50 | { | 49 | { |
51 | if (NULL != *(gnsrecord_info->rd)) | 50 | if (NULL != *(gnsrecord_info->rd)) |
52 | { | ||
53 | for (int i = 0; i < *(gnsrecord_info->rd_count); i++) | ||
54 | { | 51 | { |
55 | if (NULL != (*(gnsrecord_info->rd))[i].data) | 52 | for (int i = 0; i < *(gnsrecord_info->rd_count); i++) |
56 | GNUNET_free ((char *) (*(gnsrecord_info->rd))[i].data); | 53 | { |
54 | if (NULL != (*(gnsrecord_info->rd))[i].data) | ||
55 | GNUNET_free((char *)(*(gnsrecord_info->rd))[i].data); | ||
56 | } | ||
57 | GNUNET_free(*(gnsrecord_info->rd)); | ||
58 | *(gnsrecord_info->rd) = NULL; | ||
57 | } | 59 | } |
58 | GNUNET_free (*(gnsrecord_info->rd)); | ||
59 | *(gnsrecord_info->rd) = NULL; | ||
60 | } | ||
61 | if (NULL != *(gnsrecord_info->name)) | 60 | if (NULL != *(gnsrecord_info->name)) |
62 | GNUNET_free (*(gnsrecord_info->name)); | 61 | GNUNET_free(*(gnsrecord_info->name)); |
63 | *(gnsrecord_info->name) = NULL; | 62 | *(gnsrecord_info->name) = NULL; |
64 | } | 63 | } |
65 | 64 | ||
@@ -73,7 +72,7 @@ cleanup_recordinfo (struct GnsRecordInfo *gnsrecord_info) | |||
73 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | 72 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error |
74 | */ | 73 | */ |
75 | static int | 74 | static int |
76 | parse_record (json_t *data, struct GNUNET_GNSRECORD_Data *rd) | 75 | parse_record(json_t *data, struct GNUNET_GNSRECORD_Data *rd) |
77 | { | 76 | { |
78 | struct GNUNET_TIME_Absolute abs_expiration_time; | 77 | struct GNUNET_TIME_Absolute abs_expiration_time; |
79 | struct GNUNET_TIME_Relative rel_expiration_time; | 78 | struct GNUNET_TIME_Relative rel_expiration_time; |
@@ -84,59 +83,59 @@ parse_record (json_t *data, struct GNUNET_GNSRECORD_Data *rd) | |||
84 | int unpack_state = 0; | 83 | int unpack_state = 0; |
85 | 84 | ||
86 | //interpret single gns record | 85 | //interpret single gns record |
87 | unpack_state = json_unpack (data, | 86 | unpack_state = json_unpack(data, |
88 | "{s:s, s:s, s:s, s?:i!}", | 87 | "{s:s, s:s, s:s, s?:i!}", |
89 | GNUNET_JSON_GNSRECORD_VALUE, | 88 | GNUNET_JSON_GNSRECORD_VALUE, |
90 | &value, | 89 | &value, |
91 | GNUNET_JSON_GNSRECORD_TYPE, | 90 | GNUNET_JSON_GNSRECORD_TYPE, |
92 | &record_type, | 91 | &record_type, |
93 | GNUNET_JSON_GNSRECORD_EXPIRATION_TIME, | 92 | GNUNET_JSON_GNSRECORD_EXPIRATION_TIME, |
94 | &expiration_time, | 93 | &expiration_time, |
95 | GNUNET_JSON_GNSRECORD_FLAG, | 94 | GNUNET_JSON_GNSRECORD_FLAG, |
96 | &flag); | 95 | &flag); |
97 | if (0 != unpack_state) | 96 | if (0 != unpack_state) |
98 | { | 97 | { |
99 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 98 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
100 | "Error gnsdata object has a wrong format!\n"); | 99 | "Error gnsdata object has a wrong format!\n"); |
101 | return GNUNET_SYSERR; | 100 | return GNUNET_SYSERR; |
102 | } | 101 | } |
103 | rd->record_type = GNUNET_GNSRECORD_typename_to_number (record_type); | 102 | rd->record_type = GNUNET_GNSRECORD_typename_to_number(record_type); |
104 | if (UINT32_MAX == rd->record_type) | 103 | if (UINT32_MAX == rd->record_type) |
105 | { | 104 | { |
106 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unsupported type\n"); | 105 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Unsupported type\n"); |
107 | return GNUNET_SYSERR; | 106 | return GNUNET_SYSERR; |
108 | } | 107 | } |
109 | if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value (rd->record_type, | 108 | if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value(rd->record_type, |
110 | value, | 109 | value, |
111 | (void**)&rd->data, | 110 | (void**)&rd->data, |
112 | &rd->data_size)) | 111 | &rd->data_size)) |
113 | { | 112 | { |
114 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Value invalid for record type\n"); | 113 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Value invalid for record type\n"); |
115 | return GNUNET_SYSERR; | 114 | return GNUNET_SYSERR; |
116 | } | 115 | } |
117 | 116 | ||
118 | if (0 == strcmp (expiration_time, GNUNET_JSON_GNSRECORD_NEVER)) | 117 | if (0 == strcmp(expiration_time, GNUNET_JSON_GNSRECORD_NEVER)) |
119 | { | 118 | { |
120 | rd->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us; | 119 | rd->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us; |
121 | } | 120 | } |
122 | else if (GNUNET_OK == | 121 | else if (GNUNET_OK == |
123 | GNUNET_STRINGS_fancy_time_to_absolute (expiration_time, | 122 | GNUNET_STRINGS_fancy_time_to_absolute(expiration_time, |
124 | &abs_expiration_time)) | 123 | &abs_expiration_time)) |
125 | { | 124 | { |
126 | rd->expiration_time = abs_expiration_time.abs_value_us; | 125 | rd->expiration_time = abs_expiration_time.abs_value_us; |
127 | } | 126 | } |
128 | else if (GNUNET_OK == | 127 | else if (GNUNET_OK == |
129 | GNUNET_STRINGS_fancy_time_to_relative (expiration_time, | 128 | GNUNET_STRINGS_fancy_time_to_relative(expiration_time, |
130 | &rel_expiration_time)) | 129 | &rel_expiration_time)) |
131 | { | 130 | { |
132 | rd->expiration_time = rel_expiration_time.rel_value_us; | 131 | rd->expiration_time = rel_expiration_time.rel_value_us; |
133 | } | 132 | } |
134 | else | 133 | else |
135 | { | 134 | { |
136 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expiration time invalid\n"); | 135 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Expiration time invalid\n"); |
137 | return GNUNET_SYSERR; | 136 | return GNUNET_SYSERR; |
138 | } | 137 | } |
139 | rd->flags = (enum GNUNET_GNSRECORD_Flags) flag; | 138 | rd->flags = (enum GNUNET_GNSRECORD_Flags)flag; |
140 | return GNUNET_OK; | 139 | return GNUNET_OK; |
141 | } | 140 | } |
142 | 141 | ||
@@ -150,23 +149,23 @@ parse_record (json_t *data, struct GNUNET_GNSRECORD_Data *rd) | |||
150 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | 149 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error |
151 | */ | 150 | */ |
152 | static int | 151 | static int |
153 | parse_record_data (struct GnsRecordInfo *gnsrecord_info, json_t *data) | 152 | parse_record_data(struct GnsRecordInfo *gnsrecord_info, json_t *data) |
154 | { | 153 | { |
155 | GNUNET_assert (NULL != data); | 154 | GNUNET_assert(NULL != data); |
156 | if (! json_is_array (data)) | 155 | if (!json_is_array(data)) |
157 | { | 156 | { |
158 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 157 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
159 | "Error gns record data JSON is not an array!\n"); | 158 | "Error gns record data JSON is not an array!\n"); |
160 | return GNUNET_SYSERR; | 159 | return GNUNET_SYSERR; |
161 | } | 160 | } |
162 | *(gnsrecord_info->rd_count) = json_array_size (data); | 161 | *(gnsrecord_info->rd_count) = json_array_size(data); |
163 | *(gnsrecord_info->rd) = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * | 162 | *(gnsrecord_info->rd) = GNUNET_malloc(sizeof(struct GNUNET_GNSRECORD_Data) * |
164 | json_array_size (data)); | 163 | json_array_size(data)); |
165 | size_t index; | 164 | size_t index; |
166 | json_t *value; | 165 | json_t *value; |
167 | json_array_foreach (data, index, value) | 166 | json_array_foreach(data, index, value) |
168 | { | 167 | { |
169 | if (GNUNET_OK != parse_record (value, &(*(gnsrecord_info->rd))[index])) | 168 | if (GNUNET_OK != parse_record(value, &(*(gnsrecord_info->rd))[index])) |
170 | return GNUNET_SYSERR; | 169 | return GNUNET_SYSERR; |
171 | } | 170 | } |
172 | return GNUNET_OK; | 171 | return GNUNET_OK; |
@@ -174,42 +173,42 @@ parse_record_data (struct GnsRecordInfo *gnsrecord_info, json_t *data) | |||
174 | 173 | ||
175 | 174 | ||
176 | static int | 175 | static int |
177 | parse_gnsrecordobject (void *cls, | 176 | parse_gnsrecordobject(void *cls, |
178 | json_t *root, | 177 | json_t *root, |
179 | struct GNUNET_JSON_Specification *spec) | 178 | struct GNUNET_JSON_Specification *spec) |
180 | { | 179 | { |
181 | struct GnsRecordInfo *gnsrecord_info; | 180 | struct GnsRecordInfo *gnsrecord_info; |
182 | int unpack_state = 0; | 181 | int unpack_state = 0; |
183 | const char *name; | 182 | const char *name; |
184 | json_t *data; | 183 | json_t *data; |
185 | 184 | ||
186 | GNUNET_assert (NULL != root); | 185 | GNUNET_assert(NULL != root); |
187 | if (! json_is_object (root)) | 186 | if (!json_is_object(root)) |
188 | { | 187 | { |
189 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 188 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
190 | "Error record JSON is not an object!\n"); | 189 | "Error record JSON is not an object!\n"); |
191 | return GNUNET_SYSERR; | 190 | return GNUNET_SYSERR; |
192 | } | 191 | } |
193 | //interpret single gns record | 192 | //interpret single gns record |
194 | unpack_state = json_unpack (root, | 193 | unpack_state = json_unpack(root, |
195 | "{s:s, s:o!}", | 194 | "{s:s, s:o!}", |
196 | GNUNET_JSON_GNSRECORD_RECORD_NAME, | 195 | GNUNET_JSON_GNSRECORD_RECORD_NAME, |
197 | &name, | 196 | &name, |
198 | GNUNET_JSON_GNSRECORD_RECORD_DATA, | 197 | GNUNET_JSON_GNSRECORD_RECORD_DATA, |
199 | &data); | 198 | &data); |
200 | if (0 != unpack_state) | 199 | if (0 != unpack_state) |
201 | { | 200 | { |
202 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 201 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
203 | "Error namestore records object has a wrong format!\n"); | 202 | "Error namestore records object has a wrong format!\n"); |
204 | return GNUNET_SYSERR; | 203 | return GNUNET_SYSERR; |
205 | } | 204 | } |
206 | gnsrecord_info = (struct GnsRecordInfo *) spec->ptr; | 205 | gnsrecord_info = (struct GnsRecordInfo *)spec->ptr; |
207 | *(gnsrecord_info->name) = GNUNET_strdup (name); | 206 | *(gnsrecord_info->name) = GNUNET_strdup(name); |
208 | if (GNUNET_OK != parse_record_data (gnsrecord_info, data)) | 207 | if (GNUNET_OK != parse_record_data(gnsrecord_info, data)) |
209 | { | 208 | { |
210 | cleanup_recordinfo (gnsrecord_info); | 209 | cleanup_recordinfo(gnsrecord_info); |
211 | return GNUNET_SYSERR; | 210 | return GNUNET_SYSERR; |
212 | } | 211 | } |
213 | return GNUNET_OK; | 212 | return GNUNET_OK; |
214 | } | 213 | } |
215 | 214 | ||
@@ -221,10 +220,11 @@ parse_gnsrecordobject (void *cls, | |||
221 | * @param[out] spec where to free the data | 220 | * @param[out] spec where to free the data |
222 | */ | 221 | */ |
223 | static void | 222 | static void |
224 | clean_gnsrecordobject (void *cls, struct GNUNET_JSON_Specification *spec) | 223 | clean_gnsrecordobject(void *cls, struct GNUNET_JSON_Specification *spec) |
225 | { | 224 | { |
226 | struct GnsRecordInfo *gnsrecord_info = (struct GnsRecordInfo *) spec->ptr; | 225 | struct GnsRecordInfo *gnsrecord_info = (struct GnsRecordInfo *)spec->ptr; |
227 | GNUNET_free (gnsrecord_info); | 226 | |
227 | GNUNET_free(gnsrecord_info); | ||
228 | } | 228 | } |
229 | 229 | ||
230 | 230 | ||
@@ -235,21 +235,22 @@ clean_gnsrecordobject (void *cls, struct GNUNET_JSON_Specification *spec) | |||
235 | * @return JSON Specification | 235 | * @return JSON Specification |
236 | */ | 236 | */ |
237 | struct GNUNET_JSON_Specification | 237 | struct GNUNET_JSON_Specification |
238 | GNUNET_JSON_spec_gnsrecord (struct GNUNET_GNSRECORD_Data **rd, | 238 | GNUNET_JSON_spec_gnsrecord(struct GNUNET_GNSRECORD_Data **rd, |
239 | unsigned int *rd_count, | 239 | unsigned int *rd_count, |
240 | char **name) | 240 | char **name) |
241 | { | 241 | { |
242 | struct GnsRecordInfo *gnsrecord_info = GNUNET_new (struct GnsRecordInfo); | 242 | struct GnsRecordInfo *gnsrecord_info = GNUNET_new(struct GnsRecordInfo); |
243 | |||
243 | gnsrecord_info->rd = rd; | 244 | gnsrecord_info->rd = rd; |
244 | gnsrecord_info->name = name; | 245 | gnsrecord_info->name = name; |
245 | gnsrecord_info->rd_count = rd_count; | 246 | gnsrecord_info->rd_count = rd_count; |
246 | struct GNUNET_JSON_Specification ret = {.parser = &parse_gnsrecordobject, | 247 | struct GNUNET_JSON_Specification ret = { .parser = &parse_gnsrecordobject, |
247 | .cleaner = &clean_gnsrecordobject, | 248 | .cleaner = &clean_gnsrecordobject, |
248 | .cls = NULL, | 249 | .cls = NULL, |
249 | .field = NULL, | 250 | .field = NULL, |
250 | .ptr = (struct GnsRecordInfo *) | 251 | .ptr = (struct GnsRecordInfo *) |
251 | gnsrecord_info, | 252 | gnsrecord_info, |
252 | .ptr_size = 0, | 253 | .ptr_size = 0, |
253 | .size_ptr = NULL}; | 254 | .size_ptr = NULL }; |
254 | return ret; | 255 | return ret; |
255 | } | 256 | } |