GNUnet  0.11.x
gnunet_json_lib.h
Go to the documentation of this file.
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  SPDX-License-Identifier: AGPL3.0-or-later
19  */
27 #ifndef GNUNET_JSON_LIB_H
28 #define GNUNET_JSON_LIB_H
29 
30 #include "gnunet_util_lib.h"
31 #include "gnunet_gnsrecord_lib.h"
32 #include <jansson.h>
33 #include <microhttpd.h>
34 
35 /* ****************** Generic parser interface ******************* */
36 
41 
42 
52 typedef int
54  json_t *root,
55  struct GNUNET_JSON_Specification *spec);
56 
57 
64 typedef void
66  struct GNUNET_JSON_Specification *spec);
67 
68 
73 {
78 
83 
87  void *cls;
88 
93  const char *field;
94 
98  void *ptr;
99 
103  size_t ptr_size;
104 
108  size_t *size_ptr;
109 
114 };
115 
116 
130 int
131 GNUNET_JSON_parse (const json_t *root,
132  struct GNUNET_JSON_Specification *spec,
133  const char **error_json_name,
134  unsigned int *error_line);
135 
136 
143 void
145 
146 
147 /* ****************** Canonical parser specifications ******************* */
148 
149 
154 GNUNET_JSON_spec_end (void);
155 
156 
165 
166 
176 GNUNET_JSON_spec_fixed (const char *name,
177  void *obj,
178  size_t size);
179 
180 
188 #define GNUNET_JSON_spec_fixed_auto(name, obj) \
189  GNUNET_JSON_spec_fixed (name, obj, sizeof(*obj))
190 
191 
201 GNUNET_JSON_spec_varsize (const char *name,
202  void **obj,
203  size_t *size);
204 
205 
213 GNUNET_JSON_spec_string (const char *name,
214  const char **strptr);
215 
216 
224 GNUNET_JSON_spec_json (const char *name,
225  json_t **jsonp);
226 
227 
235 GNUNET_JSON_spec_bool (const char *name,
236  bool *b);
237 
238 
246 GNUNET_JSON_spec_uint8 (const char *name,
247  uint8_t *u8);
248 
249 
257 GNUNET_JSON_spec_uint16 (const char *name,
258  uint16_t *u16);
259 
260 
268 GNUNET_JSON_spec_uint32 (const char *name,
269  uint32_t *u32);
270 
271 
279 GNUNET_JSON_spec_uint64 (const char *name,
280  uint64_t *u64);
281 
282 
290 GNUNET_JSON_spec_int64 (const char *name,
291  int64_t *i64);
292 
293 
301 GNUNET_JSON_spec_boolean (const char *name,
302  int *boolean);
303 
304 
305 /* ************ GNUnet-specific parser specifications ******************* */
306 
315  struct GNUNET_TIME_Absolute *at);
316 
317 
326  struct GNUNET_TIME_AbsoluteNBO *at);
327 
328 
337  struct GNUNET_TIME_Relative *rt);
338 
339 
348  struct GNUNET_CRYPTO_RsaPublicKey **pk);
349 
350 
359  struct GNUNET_CRYPTO_RsaSignature **sig);
360 
361 
370  unsigned int *rd_count,
371  char **name);
372 
373 
374 /* ****************** Generic generator interface ******************* */
375 
376 
385 json_t *
386 GNUNET_JSON_from_data (const void *data, size_t size);
387 
388 
396 #define GNUNET_JSON_from_data_auto(ptr) \
397  GNUNET_JSON_from_data (ptr, sizeof(*ptr))
398 
399 
406 json_t *
408 
409 
416 json_t *
418 
419 
426 json_t *
428 
429 
436 json_t *
438 
439 
446 json_t *
448 
456 json_t *
457 GNUNET_JSON_from_gnsrecord (const char *rname,
458  const struct GNUNET_GNSRECORD_Data *rd,
459  unsigned int rd_count);
460 
461 /* ******************* Helpers for MHD upload handling ******************* */
462 
467 {
472 
477 
482 
487 
492 };
493 
494 
511 GNUNET_JSON_post_parser (size_t buffer_max,
512  struct MHD_Connection *connection,
513  void **con_cls,
514  const char *upload_data,
515  size_t *upload_data_size,
516  json_t **json);
517 
518 
526 void
527 GNUNET_JSON_post_parser_cleanup (void *con_cls);
528 
529 
530 /* ****************** GETOPT JSON helper ******************* */
531 
532 
544  const char *name,
545  const char *argumentHelp,
546  const char *description,
547  json_t **json);
548 
549 #endif
550 
551 /* end of gnunet_json_lib.h */
void * cls
Closure for parser and cleaner.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint8(const char *name, uint8_t *u8)
8-bit integer.
Definition: json_helper.c:422
json_t * GNUNET_JSON_from_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.
JSON parsing failed.
const char * argumentHelp
Name of the argument for the user in help text.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_rsa_signature(const char *name, struct GNUNET_CRYPTO_RsaSignature **sig)
Specification for parsing an RSA signature.
Definition: json_helper.c:1033
an RSA signature
Definition: crypto_rsa.c:63
const char * field
Name of the field to parse, use NULL to get the JSON of the main object instead of the JSON of an ind...
json_t * GNUNET_JSON_from_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *sig)
Convert RSA signature to JSON.
json_t * GNUNET_JSON_from_time_abs_nbo(struct GNUNET_TIME_AbsoluteNBO stamp)
Convert absolute timestamp to a json string.
json_t * GNUNET_JSON_from_time_rel(struct GNUNET_TIME_Relative stamp)
Convert relative timestamp to a json string.
struct GNUNET_CRYPTO_EcdsaPrivateKey pk
Private key from command line option, or NULL.
const char * description
Help text for the option (description)
struct GNUNET_JSON_Specification GNUNET_JSON_spec_fixed(const char *name, void *obj, size_t size)
Variable size object (in network byte order, encoded using Crockford Base32hex encoding).
Definition: json_helper.c:96
void GNUNET_JSON_parse_free(struct GNUNET_JSON_Specification *spec)
Frees all elements allocated during a GNUNET_JSON_parse() operation.
Definition: json.c:100
int is_optional
Set to GNUNET_YES if this component is optional.
Entry in parser specification for GNUNET_JSON_parse().
struct GNUNET_JSON_Specification GNUNET_JSON_spec_rsa_public_key(const char *name, struct GNUNET_CRYPTO_RsaPublicKey **pk)
Specification for parsing an RSA public key.
Definition: json_helper.c:939
Definition of a command line option.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_absolute_time_nbo(const char *name, struct GNUNET_TIME_AbsoluteNBO *at)
Absolute time in network byte order.
Definition: json_helper.c:769
const char shortName
Short name of the option.
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
The public information of an RSA key pair.
Definition: crypto_rsa.c:51
struct GNUNET_JSON_Specification GNUNET_JSON_spec_absolute_time(const char *name, struct GNUNET_TIME_Absolute *at)
Absolute time.
Definition: json_helper.c:715
struct GNUNET_JSON_Specification GNUNET_JSON_spec_json(const char *name, json_t **jsonp)
JSON object.
Definition: json_helper.c:311
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint16(const char *name, uint16_t *u16)
16-bit integer.
Definition: json_helper.c:478
Parsing successful, JSON result is in *json.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_string(const char *name, const char **strptr)
The expected field stores a string.
Definition: json_helper.c:242
json_t * GNUNET_JSON_from_time_abs(struct GNUNET_TIME_Absolute stamp)
Convert absolute timestamp to a json string.
int GNUNET_JSON_parse(const json_t *root, struct GNUNET_JSON_Specification *spec, const char **error_json_name, unsigned int *error_line)
Navigate and parse data in a JSON tree.
Definition: json.c:45
struct GNUNET_JSON_Specification GNUNET_JSON_spec_boolean(const char *name, int *boolean)
Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO).
Definition: json_helper.c:1083
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint32(const char *name, uint32_t *u32)
32-bit integer.
Definition: json_helper.c:534
void(* GNUNET_JSON_Cleaner)(void *cls, struct GNUNET_JSON_Specification *spec)
Function called to clean up data from earlier parsing.
Sorry, memory allocation (malloc()) failed.
GNUNET_JSON_Parser parser
Function for how to parse this type of entry.
json_t * GNUNET_JSON_from_gnsrecord(const char *rname, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count)
Convert Gns record to JSON.
Request size exceeded buffer_max argument.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_bool(const char *name, bool *b)
boolean.
Definition: json_helper.c:366
struct GNUNET_JSON_Specification GNUNET_JSON_spec_relative_time(const char *name, struct GNUNET_TIME_Relative *rt)
Relative time.
Definition: json_helper.c:846
struct GNUNET_GETOPT_CommandLineOption GNUNET_JSON_getopt(char shortName, const char *name, const char *argumentHelp, const char *description, json_t **json)
Allow user to specify a JSON input value.
Definition: json.c:153
static unsigned int size
Size of the "table".
Definition: peer.c:67
struct GNUNET_JSON_Specification GNUNET_JSON_spec_gnsrecord(struct GNUNET_GNSRECORD_Data **rd, unsigned int *rd_count, char **name)
JSON Specification for GNS Records.
void * ptr
Pointer, details specific to the parser.
int(* GNUNET_JSON_Parser)(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Function called to parse JSON argument.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_int64(const char *name, int64_t *i64)
64-bit signed integer.
Definition: json_helper.c:636
struct GNUNET_JSON_Specification GNUNET_JSON_spec_varsize(const char *name, void **obj, size_t *size)
Variable size object (in network byte order, encoded using Crockford Base32hex encoding).
Definition: json_helper.c:189
GNUNET_JSON_PostResult
Return codes from GNUNET_JSON_post_parser().
enum GNUNET_JSON_PostResult GNUNET_JSON_post_parser(size_t buffer_max, struct MHD_Connection *connection, void **con_cls, const char *upload_data, size_t *upload_data_size, json_t **json)
Process a POST request containing a JSON object.
Definition: json_mhd.c:265
GNUNET_JSON_Cleaner cleaner
Function for how to clean up this type of entry.
const char * name
void GNUNET_JSON_post_parser_cleanup(void *con_cls)
Function called whenever we are done with a request to clean up our state.
Definition: json_mhd.c:357
Parsing continues, call again soon!
size_t ptr_size
Number of bytes available in ptr.
size_t * size_ptr
Where should we store the final size of ptr.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint64(const char *name, uint64_t *u64)
64-bit integer.
Definition: json_helper.c:585
json_t * GNUNET_JSON_from_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *pk)
Convert RSA public key to JSON.
Time for absolute times used by GNUnet, in microseconds.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_end(void)
End of a parser specification.
Definition: json_helper.c:35
uint32_t data
The data value.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_mark_optional(struct GNUNET_JSON_Specification spec)
Set the "optional" flag for a parser specification entry.
Definition: json.c:84
Time for relative time used by GNUnet, in microseconds.