GNUnet  0.11.x
Data Structures | Macros | Typedefs | Enumerations | Functions
gnunet_json_lib.h File Reference

functions to parse JSON objects into GNUnet objects More...

#include "gnunet_util_lib.h"
#include <jansson.h>
#include <microhttpd.h>
Include dependency graph for gnunet_json_lib.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  GNUNET_JSON_Specification
 Entry in parser specification for GNUNET_JSON_parse(). More...
 

Macros

#define GNUNET_JSON_spec_fixed_auto(name, obj)   GNUNET_JSON_spec_fixed (name, obj, sizeof(*obj))
 Fixed size object (in network byte order, encoded using Crockford Base32hex encoding). More...
 
#define GNUNET_JSON_from_data_auto(ptr)   GNUNET_JSON_from_data (ptr, sizeof(*ptr))
 Convert binary data to a JSON string with the base32crockford encoding. More...
 

Typedefs

typedef int(* GNUNET_JSON_Parser) (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 Function called to parse JSON argument. More...
 
typedef void(* GNUNET_JSON_Cleaner) (void *cls, struct GNUNET_JSON_Specification *spec)
 Function called to clean up data from earlier parsing. More...
 

Enumerations

enum  GNUNET_JSON_PostResult {
  GNUNET_JSON_PR_SUCCESS, GNUNET_JSON_PR_CONTINUE, GNUNET_JSON_PR_OUT_OF_MEMORY, GNUNET_JSON_PR_REQUEST_TOO_LARGE,
  GNUNET_JSON_PR_JSON_INVALID
}
 Return codes from GNUNET_JSON_post_parser(). More...
 

Functions

enum GNUNET_GenericReturnValue 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. More...
 
void GNUNET_JSON_parse_free (struct GNUNET_JSON_Specification *spec)
 Frees all elements allocated during a GNUNET_JSON_parse() operation. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_end (void)
 End of a parser specification. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_mark_optional (struct GNUNET_JSON_Specification spec)
 Set the "optional" flag for a parser specification entry. More...
 
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). More...
 
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). More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_string (const char *name, const char **strptr)
 The expected field stores a string. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_json (const char *name, json_t **jsonp)
 JSON object. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_bool (const char *name, bool *b)
 boolean. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint8 (const char *name, uint8_t *u8)
 8-bit integer. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint16 (const char *name, uint16_t *u16)
 16-bit integer. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint32 (const char *name, uint32_t *u32)
 32-bit integer. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint64 (const char *name, uint64_t *u64)
 64-bit integer. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_int64 (const char *name, int64_t *i64)
 64-bit signed integer. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_boolean (const char *name, int *boolean)
 Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO). More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_absolute_time (const char *name, struct GNUNET_TIME_Absolute *at)
 Absolute time. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_absolute_time_nbo (const char *name, struct GNUNET_TIME_AbsoluteNBO *at)
 Absolute time in network byte order. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_relative_time (const char *name, struct GNUNET_TIME_Relative *rt)
 Relative time. More...
 
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. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_rsa_signature (const char *name, struct GNUNET_CRYPTO_RsaSignature **sig)
 Specification for parsing an RSA signature. More...
 
json_t * GNUNET_JSON_from_data (const void *data, size_t size)
 Convert binary data to a JSON string with the base32crockford encoding. More...
 
json_t * GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
 Convert absolute timestamp to a json string. More...
 
json_t * GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp)
 Convert absolute timestamp to a json string. More...
 
json_t * GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
 Convert relative timestamp to a json string. More...
 
json_t * GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
 Convert RSA public key to JSON. More...
 
json_t * GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
 Convert RSA signature to JSON. More...
 
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. More...
 
void GNUNET_JSON_post_parser_cleanup (void *con_cls)
 Function called whenever we are done with a request to clean up our state. More...
 
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. More...
 

Detailed Description

functions to parse JSON objects into GNUnet objects

Author
Florian Dold
Benedikt Mueller
Christian Grothoff

Definition in file gnunet_json_lib.h.

Macro Definition Documentation

◆ GNUNET_JSON_spec_fixed_auto

#define GNUNET_JSON_spec_fixed_auto (   name,
  obj 
)    GNUNET_JSON_spec_fixed (name, obj, sizeof(*obj))

Fixed size object (in network byte order, encoded using Crockford Base32hex encoding).

Parameters
namename of the JSON field
objpointer where to write the data (type of *obj will determine size)

Definition at line 187 of file gnunet_json_lib.h.

◆ GNUNET_JSON_from_data_auto

#define GNUNET_JSON_from_data_auto (   ptr)    GNUNET_JSON_from_data (ptr, sizeof(*ptr))

Convert binary data to a JSON string with the base32crockford encoding.

Parameters
ptrbinary data, sizeof (*ptr) must yield correct size
Returns
json string that encodes data

Definition at line 383 of file gnunet_json_lib.h.

Typedef Documentation

◆ GNUNET_JSON_Parser

typedef int(* GNUNET_JSON_Parser) (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)

Function called to parse JSON argument.

Parameters
clsclosure
rootJSON to parse
specour specification entry with further details
Returns
GNUNET_SYSERR on error, GNUNET_OK on success

Definition at line 52 of file gnunet_json_lib.h.

◆ GNUNET_JSON_Cleaner

typedef void(* GNUNET_JSON_Cleaner) (void *cls, struct GNUNET_JSON_Specification *spec)

Function called to clean up data from earlier parsing.

Parameters
clsclosure
specour specification entry with data to clean.

Definition at line 64 of file gnunet_json_lib.h.

Enumeration Type Documentation

◆ GNUNET_JSON_PostResult

Return codes from GNUNET_JSON_post_parser().

Enumerator
GNUNET_JSON_PR_SUCCESS 

Parsing successful, JSON result is in *json.

GNUNET_JSON_PR_CONTINUE 

Parsing continues, call again soon!

GNUNET_JSON_PR_OUT_OF_MEMORY 

Sorry, memory allocation (malloc()) failed.

GNUNET_JSON_PR_REQUEST_TOO_LARGE 

Request size exceeded buffer_max argument.

GNUNET_JSON_PR_JSON_INVALID 

JSON parsing failed.

This was not a JSON upload.

Definition at line 441 of file gnunet_json_lib.h.

442 {
447 
452 
457 
462 
467 };
JSON parsing failed.
Parsing successful, JSON result is in *json.
Sorry, memory allocation (malloc()) failed.
Request size exceeded buffer_max argument.
Parsing continues, call again soon!

Function Documentation

◆ GNUNET_JSON_parse()

enum GNUNET_GenericReturnValue 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.

Tries to parse the root to find all of the values given in the spec. If one of the entries in spec cannot be found or parsed, the name of the JSON field is returned in error_json_name, and the offset of the entry in spec is returned in error_line.

Parameters
rootthe JSON node to start the navigation at.
specparse specification array
[out]error_json_namewhich JSON field was problematic
[out]whichindex into spec did we encounter an error
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 45 of file json.c.

References GNUNET_JSON_Specification::field, GNUNET_JSON_parse_free(), GNUNET_OK, GNUNET_SYSERR, GNUNET_JSON_Specification::parser, and consensus-simulation::parser.

Referenced by add_attribute_cont(), add_credential_cont(), consume_ticket_cont(), namestore_add_or_update(), and revoke_ticket_cont().

49 {
50  if (NULL == root)
51  return GNUNET_SYSERR;
52  for (unsigned int i = 0; NULL != spec[i].parser; i++)
53  {
54  json_t *pos;
55 
56  if (NULL == spec[i].field)
57  pos = (json_t *) root;
58  else
59  pos = json_object_get (root,
60  spec[i].field);
61  if ( ( (NULL == pos) ||
62  (json_is_null (pos) ) ) &&
63  (spec[i].is_optional) )
64  continue;
65  if ( (NULL == pos) ||
66  (GNUNET_OK !=
67  spec[i].parser (spec[i].cls,
68  pos,
69  &spec[i])) )
70  {
71  if (NULL != error_json_name)
72  *error_json_name = spec[i].field;
73  if (NULL != error_line)
74  *error_line = i;
76  return GNUNET_SYSERR;
77  }
78  }
79  return GNUNET_OK; /* all OK! */
80 }
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...
void GNUNET_JSON_parse_free(struct GNUNET_JSON_Specification *spec)
Frees all elements allocated during a GNUNET_JSON_parse() operation.
Definition: json.c:106
GNUNET_JSON_Parser parser
Function for how to parse this type of entry.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_JSON_parse_free()

void GNUNET_JSON_parse_free ( struct GNUNET_JSON_Specification spec)

Frees all elements allocated during a GNUNET_JSON_parse() operation.

Parameters
specspecification of the parse operation

Definition at line 106 of file json.c.

References GNUNET_JSON_Specification::cleaner, GNUNET_JSON_Specification::cls, and GNUNET_JSON_Specification::parser.

Referenced by add_attribute_cont(), add_credential_cont(), consume_ticket_cont(), GNUNET_JSON_parse(), namestore_add_or_update(), and revoke_ticket_cont().

107 {
108  for (unsigned int i = 0; NULL != spec[i].parser; i++)
109  if (NULL != spec[i].cleaner)
110  spec[i].cleaner (spec[i].cls, &spec[i]);
111 }
GNUNET_JSON_Parser parser
Function for how to parse this type of entry.
GNUNET_JSON_Cleaner cleaner
Function for how to clean up this type of entry.
Here is the caller graph for this function:

◆ GNUNET_JSON_spec_end()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_end ( void  )

End of a parser specification.

Definition at line 32 of file json_helper.c.

References GNUNET_JSON_Specification::parser, and ret.

Referenced by add_attribute_cont(), add_credential_cont(), consume_ticket_cont(), namestore_add_or_update(), and revoke_ticket_cont().

33 {
35  .parser = NULL,
36  .cleaner = NULL,
37  .cls = NULL
38  };
39 
40  return ret;
41 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
Here is the caller graph for this function:

◆ GNUNET_JSON_spec_mark_optional()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_mark_optional ( struct GNUNET_JSON_Specification  spec)

Set the "optional" flag for a parser specification entry.

Parameters
specspecification to modify
Returns
spec copy of spec with optional bit set

Definition at line 90 of file json.c.

References GNUNET_YES, GNUNET_JSON_Specification::is_optional, and ret.

91 {
92  struct GNUNET_JSON_Specification ret = spec;
93 
94  ret.is_optional = GNUNET_YES;
95  return ret;
96 }
int is_optional
Set to GNUNET_YES if this component is optional.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().

◆ GNUNET_JSON_spec_fixed()

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).

Parameters
namename of the JSON field
[out]objpointer where to write the data, must have size bytes
sizenumber of bytes expected in obj

Definition at line 85 of file json_helper.c.

References name, parse_fixed_data(), GNUNET_JSON_Specification::parser, ret, and size.

88 {
90  .parser = &parse_fixed_data,
91  .cleaner = NULL,
92  .cls = NULL,
93  .field = name,
94  .ptr = obj,
95  .ptr_size = size,
96  .size_ptr = NULL
97  };
98 
99  return ret;
100 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
static unsigned int size
Size of the "table".
Definition: peer.c:67
const char * name
static int parse_fixed_data(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to fixed size data.
Definition: json_helper.c:53
Here is the call graph for this function:

◆ GNUNET_JSON_spec_varsize()

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).

Parameters
namename of the JSON field
[out]objpointer where to write the data, will be allocated
[out]sizewhere to store the number of bytes allocated for obj

Definition at line 171 of file json_helper.c.

References clean_variable_data(), name, parse_variable_data(), GNUNET_JSON_Specification::parser, ret, and size.

174 {
175  struct GNUNET_JSON_Specification ret = {
176  .parser = &parse_variable_data,
177  .cleaner = &clean_variable_data,
178  .cls = NULL,
179  .field = name,
180  .ptr = obj,
181  .ptr_size = 0,
182  .size_ptr = size
183  };
184 
185  *obj = NULL;
186  *size = 0;
187  return ret;
188 }
static void clean_variable_data(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing variable size data.
Definition: json_helper.c:157
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
static unsigned int size
Size of the "table".
Definition: peer.c:67
static int parse_variable_data(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to variable size data.
Definition: json_helper.c:112
const char * name
Here is the call graph for this function:

◆ GNUNET_JSON_spec_string()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_string ( const char *  name,
const char **  strptr 
)

The expected field stores a string.

Parameters
namename of the JSON field
strptrwhere to store a pointer to the field

Definition at line 219 of file json_helper.c.

References name, parse_string(), GNUNET_JSON_Specification::parser, and ret.

221 {
222  struct GNUNET_JSON_Specification ret = {
223  .parser = &parse_string,
224  .cleaner = NULL,
225  .cls = NULL,
226  .field = name,
227  .ptr = strptr,
228  .ptr_size = 0,
229  .size_ptr = NULL
230  };
231 
232  *strptr = NULL;
233  return ret;
234 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
static int parse_string(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to string.
Definition: json_helper.c:200
const char * name
Here is the call graph for this function:

◆ GNUNET_JSON_spec_json()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_json ( const char *  name,
json_t **  jsonp 
)

JSON object.

Parameters
namename of the JSON field
[out]jsonpwhere to store the JSON found under name

Definition at line 282 of file json_helper.c.

References clean_object(), name, parse_object(), GNUNET_JSON_Specification::parser, and ret.

284 {
285  struct GNUNET_JSON_Specification ret = {
286  .parser = &parse_object,
287  .cleaner = &clean_object,
288  .cls = NULL,
289  .field = name,
290  .ptr = jsonp,
291  .ptr_size = 0,
292  .size_ptr = NULL
293  };
294 
295  *jsonp = NULL;
296  return ret;
297 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
static int parse_object(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a JSON object.
Definition: json_helper.c:246
static void clean_object(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing JSON object.
Definition: json_helper.c:268
const char * name
Here is the call graph for this function:

◆ GNUNET_JSON_spec_bool()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_bool ( const char *  name,
bool *  b 
)

boolean.

Parameters
namename of the JSON field
[out]bwhere to store the boolean found under name

Definition at line 331 of file json_helper.c.

References testconfigure::b, name, parse_bool(), GNUNET_JSON_Specification::parser, ret, and GNUNET_JSON_Specification::size_ptr.

333 {
334  struct GNUNET_JSON_Specification ret = {
335  .parser = &parse_bool,
336  .cleaner = NULL,
337  .cls = NULL,
338  .field = name,
339  .ptr = b,
340  .ptr_size = sizeof(bool),
341  .size_ptr = NULL
342  };
343 
344  return ret;
345 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
static int parse_bool(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a bool.
Definition: json_helper.c:309
const char * name
size_t * size_ptr
Where should we store the final size of ptr.
Here is the call graph for this function:

◆ GNUNET_JSON_spec_uint8()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint8 ( const char *  name,
uint8_t *  u8 
)

8-bit integer.

Parameters
namename of the JSON field
[out]u8where to store the integer found under name

Definition at line 381 of file json_helper.c.

References name, parse_u8(), GNUNET_JSON_Specification::parser, ret, and GNUNET_JSON_Specification::size_ptr.

383 {
384  struct GNUNET_JSON_Specification ret = {
385  .parser = &parse_u8,
386  .cleaner = NULL,
387  .cls = NULL,
388  .field = name,
389  .ptr = u8,
390  .ptr_size = sizeof(uint8_t),
391  .size_ptr = NULL
392  };
393 
394  return ret;
395 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
const char * name
size_t * size_ptr
Where should we store the final size of ptr.
static int parse_u8(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a uint8_t.
Definition: json_helper.c:357
Here is the call graph for this function:

◆ GNUNET_JSON_spec_uint16()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint16 ( const char *  name,
uint16_t *  u16 
)

16-bit integer.

Parameters
namename of the JSON field
[out]u16where to store the integer found under name

Definition at line 431 of file json_helper.c.

References name, parse_u16(), GNUNET_JSON_Specification::parser, ret, and GNUNET_JSON_Specification::size_ptr.

433 {
434  struct GNUNET_JSON_Specification ret = {
435  .parser = &parse_u16,
436  .cleaner = NULL,
437  .cls = NULL,
438  .field = name,
439  .ptr = u16,
440  .ptr_size = sizeof(uint16_t),
441  .size_ptr = NULL
442  };
443 
444  return ret;
445 }
static int parse_u16(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a uint16_t.
Definition: json_helper.c:407
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
const char * name
size_t * size_ptr
Where should we store the final size of ptr.
Here is the call graph for this function:

◆ GNUNET_JSON_spec_uint32()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint32 ( const char *  name,
uint32_t *  u32 
)

32-bit integer.

Parameters
namename of the JSON field
[out]u32where to store the integer found under name

Definition at line 481 of file json_helper.c.

References name, parse_u32(), GNUNET_JSON_Specification::parser, ret, and GNUNET_JSON_Specification::size_ptr.

483 {
484  struct GNUNET_JSON_Specification ret = {
485  .parser = &parse_u32,
486  .cleaner = NULL,
487  .cls = NULL,
488  .field = name,
489  .ptr = u32,
490  .ptr_size = sizeof(uint32_t),
491  .size_ptr = NULL
492  };
493 
494  return ret;
495 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
static int parse_u32(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a uint32_t.
Definition: json_helper.c:457
const char * name
size_t * size_ptr
Where should we store the final size of ptr.
Here is the call graph for this function:

◆ GNUNET_JSON_spec_uint64()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint64 ( const char *  name,
uint64_t *  u64 
)

64-bit integer.

Parameters
namename of the JSON field
[out]u64where to store the integer found under name

Definition at line 526 of file json_helper.c.

References name, parse_u64(), GNUNET_JSON_Specification::parser, ret, and GNUNET_JSON_Specification::size_ptr.

528 {
529  struct GNUNET_JSON_Specification ret = {
530  .parser = &parse_u64,
531  .cleaner = NULL,
532  .cls = NULL,
533  .field = name,
534  .ptr = u64,
535  .ptr_size = sizeof(uint64_t),
536  .size_ptr = NULL
537  };
538 
539  return ret;
540 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
static int parse_u64(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a uint64_t.
Definition: json_helper.c:507
const char * name
size_t * size_ptr
Where should we store the final size of ptr.
Here is the call graph for this function:

◆ GNUNET_JSON_spec_int64()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_int64 ( const char *  name,
int64_t *  i64 
)

64-bit signed integer.

Parameters
namename of the JSON field
[out]i64where to store the integer found under name

Definition at line 571 of file json_helper.c.

References name, parse_i64(), GNUNET_JSON_Specification::parser, ret, and GNUNET_JSON_Specification::size_ptr.

573 {
574  struct GNUNET_JSON_Specification ret = {
575  .parser = &parse_i64,
576  .cleaner = NULL,
577  .cls = NULL,
578  .field = name,
579  .ptr = i64,
580  .ptr_size = sizeof(int64_t),
581  .size_ptr = NULL
582  };
583 
584  return ret;
585 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
static int parse_i64(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a int64_t.
Definition: json_helper.c:552
const char * name
size_t * size_ptr
Where should we store the final size of ptr.
Here is the call graph for this function:

◆ GNUNET_JSON_spec_boolean()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_boolean ( const char *  name,
int *  boolean 
)

Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO).

Parameters
namename of the JSON field
[out]booleanwhere to store the boolean found under name

Definition at line 982 of file json_helper.c.

References consensus-simulation::int, name, parse_boolean(), GNUNET_JSON_Specification::parser, ret, and GNUNET_JSON_Specification::size_ptr.

984 {
985  struct GNUNET_JSON_Specification ret = {
986  .parser = &parse_boolean,
987  .cleaner = NULL,
988  .cls = NULL,
989  .field = name,
990  .ptr = boolean,
991  .ptr_size = sizeof(int),
992  .size_ptr = NULL
993  };
994 
995  return ret;
996 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
static int parse_boolean(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to an int as a boolean.
Definition: json_helper.c:965
const char * name
size_t * size_ptr
Where should we store the final size of ptr.
Here is the call graph for this function:

◆ GNUNET_JSON_spec_absolute_time()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_absolute_time ( const char *  name,
struct GNUNET_TIME_Absolute at 
)

Absolute time.

Parameters
namename of the JSON field
[out]atwhere to store the absolute time found under name

Definition at line 644 of file json_helper.c.

References name, parse_abs_time(), GNUNET_JSON_Specification::parser, and ret.

646 {
647  struct GNUNET_JSON_Specification ret = {
648  .parser = &parse_abs_time,
649  .cleaner = NULL,
650  .cls = NULL,
651  .field = name,
652  .ptr = at,
653  .ptr_size = sizeof(struct GNUNET_TIME_Absolute),
654  .size_ptr = NULL
655  };
656 
657  return ret;
658 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
const char * name
Time for absolute times used by GNUnet, in microseconds.
static int parse_abs_time(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to absolute time.
Definition: json_helper.c:599
Here is the call graph for this function:

◆ GNUNET_JSON_spec_absolute_time_nbo()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_absolute_time_nbo ( const char *  name,
struct GNUNET_TIME_AbsoluteNBO at 
)

Absolute time in network byte order.

Parameters
namename of the JSON field
[out]atwhere to store the absolute time found under name

Definition at line 692 of file json_helper.c.

References name, parse_abs_time_nbo(), GNUNET_JSON_Specification::parser, and ret.

694 {
695  struct GNUNET_JSON_Specification ret = {
696  .parser = &parse_abs_time_nbo,
697  .cleaner = NULL,
698  .cls = NULL,
699  .field = name,
700  .ptr = at,
701  .ptr_size = sizeof(struct GNUNET_TIME_AbsoluteNBO),
702  .size_ptr = NULL
703  };
704 
705  return ret;
706 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
const char * name
static int parse_abs_time_nbo(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to absolute time.
Definition: json_helper.c:670
Here is the call graph for this function:

◆ GNUNET_JSON_spec_relative_time()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_relative_time ( const char *  name,
struct GNUNET_TIME_Relative rt 
)

Relative time.

Parameters
namename of the JSON field
[out]rtwhere to store the relative time found under name

Definition at line 763 of file json_helper.c.

References name, parse_rel_time(), GNUNET_JSON_Specification::parser, and ret.

765 {
766  struct GNUNET_JSON_Specification ret = {
767  .parser = &parse_rel_time,
768  .cleaner = NULL,
769  .cls = NULL,
770  .field = name,
771  .ptr = rt,
772  .ptr_size = sizeof(struct GNUNET_TIME_Relative),
773  .size_ptr = NULL
774  };
775 
776  return ret;
777 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
static int parse_rel_time(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to relative time.
Definition: json_helper.c:718
const char * name
Time for relative time used by GNUnet, in microseconds.
Here is the call graph for this function:

◆ GNUNET_JSON_spec_rsa_public_key()

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.

Parameters
namename of the JSON field
pkwhere to store the RSA key found under name

Definition at line 850 of file json_helper.c.

References clean_rsa_public_key(), name, parse_rsa_public_key(), GNUNET_JSON_Specification::parser, pk, and ret.

852 {
853  struct GNUNET_JSON_Specification ret = {
854  .parser = &parse_rsa_public_key,
855  .cleaner = &clean_rsa_public_key,
856  .cls = NULL,
857  .field = name,
858  .ptr = pk,
859  .ptr_size = 0,
860  .size_ptr = NULL
861  };
862 
863  *pk = NULL;
864  return ret;
865 }
static int parse_rsa_public_key(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to RSA public key.
Definition: json_helper.c:789
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
static void clean_rsa_public_key(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA public key.
Definition: json_helper.c:836
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
const char * name
Here is the call graph for this function:

◆ GNUNET_JSON_spec_rsa_signature()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_rsa_signature ( const char *  name,
struct GNUNET_CRYPTO_RsaSignature **  sig 
)

Specification for parsing an RSA signature.

Parameters
namename of the JSON field
sigwhere to store the RSA signature found under name

Definition at line 938 of file json_helper.c.

References clean_rsa_signature(), name, parse_rsa_signature(), GNUNET_JSON_Specification::parser, and ret.

940 {
941  struct GNUNET_JSON_Specification ret = {
942  .parser = &parse_rsa_signature,
943  .cleaner = &clean_rsa_signature,
944  .cls = NULL,
945  .field = name,
946  .ptr = sig,
947  .ptr_size = 0,
948  .size_ptr = NULL
949  };
950 
951  *sig = NULL;
952  return ret;
953 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
static int parse_rsa_signature(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to RSA signature.
Definition: json_helper.c:877
static void clean_rsa_signature(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA signature.
Definition: json_helper.c:924
const char * name
Here is the call graph for this function:

◆ GNUNET_JSON_from_data()

json_t* GNUNET_JSON_from_data ( const void *  data,
size_t  size 
)

Convert binary data to a JSON string with the base32crockford encoding.

Parameters
databinary data
sizesize of data in bytes
Returns
json string that encodes data

Definition at line 39 of file json_generator.c.

References buf, GNUNET_break, GNUNET_free, and GNUNET_STRINGS_data_to_string_alloc().

Referenced by GNUNET_JSON_from_rsa_public_key(), and GNUNET_JSON_from_rsa_signature().

41 {
42  char *buf;
43  json_t *json;
44 
46  json = json_string (buf);
47  GNUNET_free (buf);
48  GNUNET_break (NULL != json);
49  return json;
50 }
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static char buf[2048]
static unsigned int size
Size of the "table".
Definition: peer.c:67
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:937
uint32_t data
The data value.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_JSON_from_time_abs()

json_t* GNUNET_JSON_from_time_abs ( struct GNUNET_TIME_Absolute  stamp)

Convert absolute timestamp to a json string.

Parameters
stampthe time stamp
Returns
a json string with the timestamp in stamp

Definition at line 60 of file json_generator.c.

References GNUNET_TIME_Absolute::abs_value_us, GNUNET_assert, GNUNET_break, GNUNET_OK, GNUNET_TIME_round_abs(), and GNUNET_TIME_UNIT_FOREVER_ABS.

Referenced by GNUNET_JSON_from_time_abs_nbo().

61 {
62  json_t *j;
63 
65  GNUNET_TIME_round_abs (&stamp));
66 
67  j = json_object ();
68  if (NULL == j)
69  {
70  GNUNET_break (0);
71  return NULL;
72  }
73  if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
74  {
75  if (0 !=
76  json_object_set_new (j,
77  "t_ms",
78  json_string ("never")))
79  {
80  GNUNET_break (0);
81  json_decref (j);
82  return NULL;
83  }
84  return j;
85  }
86  if (0 !=
87  json_object_set_new (j,
88  "t_ms",
89  json_integer ((json_int_t) (stamp.abs_value_us
90  / 1000LL))))
91  {
92  GNUNET_break (0);
93  json_decref (j);
94  return NULL;
95  }
96  return j;
97 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
uint64_t abs_value_us
The actual value.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
int GNUNET_TIME_round_abs(struct GNUNET_TIME_Absolute *at)
Round a time value so that it is suitable for transmission via JSON encodings.
Definition: time.c:62
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_JSON_from_time_abs_nbo()

json_t* GNUNET_JSON_from_time_abs_nbo ( struct GNUNET_TIME_AbsoluteNBO  stamp)

Convert absolute timestamp to a json string.

Parameters
stampthe time stamp
Returns
a json string with the timestamp in stamp

Definition at line 107 of file json_generator.c.

References GNUNET_JSON_from_time_abs(), and GNUNET_TIME_absolute_ntoh().

108 {
110 }
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:474
json_t * GNUNET_JSON_from_time_abs(struct GNUNET_TIME_Absolute stamp)
Convert absolute timestamp to a json string.
Here is the call graph for this function:

◆ GNUNET_JSON_from_time_rel()

json_t* GNUNET_JSON_from_time_rel ( struct GNUNET_TIME_Relative  stamp)

Convert relative timestamp to a json string.

Parameters
stampthe time stamp
Returns
a json string with the timestamp in stamp

Definition at line 120 of file json_generator.c.

References GNUNET_assert, GNUNET_break, GNUNET_OK, GNUNET_TIME_round_rel(), GNUNET_TIME_UNIT_FOREVER_REL, and GNUNET_TIME_Relative::rel_value_us.

121 {
122  json_t *j;
123 
125  GNUNET_TIME_round_rel (&stamp));
126 
127  j = json_object ();
128  if (NULL == j)
129  {
130  GNUNET_break (0);
131  return NULL;
132  }
133  if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
134  {
135  if (0 !=
136  json_object_set_new (j,
137  "d_ms",
138  json_string ("forever")))
139  {
140  GNUNET_break (0);
141  json_decref (j);
142  return NULL;
143  }
144  return j;
145  }
146  if (0 !=
147  json_object_set_new (j,
148  "d_ms",
149  json_integer ((json_int_t) (stamp.rel_value_us
150  / 1000LL))))
151  {
152  GNUNET_break (0);
153  json_decref (j);
154  return NULL;
155  }
156  return j;
157 }
uint64_t rel_value_us
The actual value.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
int GNUNET_TIME_round_rel(struct GNUNET_TIME_Relative *rt)
Round a time value so that it is suitable for transmission via JSON encodings.
Definition: time.c:74
Here is the call graph for this function:

◆ GNUNET_JSON_from_rsa_public_key()

json_t* GNUNET_JSON_from_rsa_public_key ( const struct GNUNET_CRYPTO_RsaPublicKey pk)

Convert RSA public key to JSON.

Parameters
pkpublic key to convert
Returns
corresponding JSON encoding

Definition at line 167 of file json_generator.c.

References buf, GNUNET_CRYPTO_rsa_public_key_encode(), GNUNET_free, GNUNET_JSON_from_data(), and ret.

168 {
169  void *buf;
170  size_t buf_len;
171  json_t *ret;
172 
174  &buf);
175  ret = GNUNET_JSON_from_data (buf,
176  buf_len);
177  GNUNET_free (buf);
178  return ret;
179 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static char buf[2048]
size_t GNUNET_CRYPTO_rsa_public_key_encode(const struct GNUNET_CRYPTO_RsaPublicKey *key, void **buffer)
Encode the public key in a format suitable for storing it into a file.
Definition: crypto_rsa.c:300
#define GNUNET_free(ptr)
Wrapper around free.
json_t * GNUNET_JSON_from_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.
Here is the call graph for this function:

◆ GNUNET_JSON_from_rsa_signature()

json_t* GNUNET_JSON_from_rsa_signature ( const struct GNUNET_CRYPTO_RsaSignature sig)

Convert RSA signature to JSON.

Parameters
sigsignature to convert
Returns
corresponding JSON encoding

Definition at line 189 of file json_generator.c.

References buf, GNUNET_CRYPTO_rsa_signature_encode(), GNUNET_free, GNUNET_JSON_from_data(), and ret.

190 {
191  void *buf;
192  size_t buf_len;
193  json_t *ret;
194 
195  buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
196  &buf);
197  ret = GNUNET_JSON_from_data (buf,
198  buf_len);
199  GNUNET_free (buf);
200  return ret;
201 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
size_t GNUNET_CRYPTO_rsa_signature_encode(const struct GNUNET_CRYPTO_RsaSignature *sig, void **buffer)
Encode the given signature in a format suitable for storing it into a file.
Definition: crypto_rsa.c:953
static char buf[2048]
#define GNUNET_free(ptr)
Wrapper around free.
json_t * GNUNET_JSON_from_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.
Here is the call graph for this function:

◆ 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.

This function realizes an MHD POST processor that will (incrementally) process JSON data uploaded to the HTTP server. It will store the required state in the con_cls, which must be cleaned up using #GNUNET_JSON_post_parser_callback().

Parameters
buffer_maxmaximum allowed size for the buffer
connectionMHD connection handle (for meta data about the upload)
con_clsthe closure (will point to a struct Buffer *)
upload_datathe POST data
upload_data_sizenumber of bytes in upload_data
jsonthe JSON object for a completed request
Returns
result code indicating the status of the operation

Definition at line 265 of file json_mhd.c.

References buffer_append(), buffer_deinit(), buffer_init(), Buffer::data, Buffer::fill, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_JSON_PR_CONTINUE, GNUNET_JSON_PR_JSON_INVALID, GNUNET_JSON_PR_OUT_OF_MEMORY, GNUNET_JSON_PR_REQUEST_TOO_LARGE, GNUNET_JSON_PR_SUCCESS, GNUNET_log, GNUNET_new, GNUNET_OK, inflate_data(), REQUEST_BUFFER_INITIAL, and ret.

271 {
272  struct Buffer *r = *con_cls;
273  const char *ce;
274  int ret;
275 
276  *json = NULL;
277  if (NULL == *con_cls)
278  {
279  /* We are seeing a fresh POST request. */
280  r = GNUNET_new (struct Buffer);
281  if (GNUNET_OK != buffer_init (r,
282  upload_data,
283  *upload_data_size,
285  buffer_max))
286  {
287  *con_cls = NULL;
288  buffer_deinit (r);
289  GNUNET_free (r);
291  }
292  /* everything OK, wait for more POST data */
293  *upload_data_size = 0;
294  *con_cls = r;
296  }
297  if (0 != *upload_data_size)
298  {
299  /* We are seeing an old request with more data available. */
300 
301  if (GNUNET_OK !=
302  buffer_append (r, upload_data, *upload_data_size, buffer_max))
303  {
304  /* Request too long */
305  *con_cls = NULL;
306  buffer_deinit (r);
307  GNUNET_free (r);
309  }
310  /* everything OK, wait for more POST data */
311  *upload_data_size = 0;
313  }
314 
315  /* We have seen the whole request. */
316  ce = MHD_lookup_connection_value (connection,
317  MHD_HEADER_KIND,
318  MHD_HTTP_HEADER_CONTENT_ENCODING);
319  if ((NULL != ce) && (0 == strcasecmp ("deflate", ce)))
320  {
321  ret = inflate_data (r);
322  if (GNUNET_JSON_PR_SUCCESS != ret)
323  {
324  buffer_deinit (r);
325  GNUNET_free (r);
326  *con_cls = NULL;
327  return ret;
328  }
329  }
330 
331  *json = json_loadb (r->data, r->fill, 0, NULL);
332  if (NULL == *json)
333  {
335  "Failed to parse JSON request body\n");
336  buffer_deinit (r);
337  GNUNET_free (r);
338  *con_cls = NULL;
340  }
341  buffer_deinit (r);
342  GNUNET_free (r);
343  *con_cls = NULL;
344 
345  return GNUNET_JSON_PR_SUCCESS;
346 }
JSON parsing failed.
static enum GNUNET_JSON_PostResult inflate_data(struct Buffer *buf)
Decompress data in buf.
Definition: json_mhd.c:156
Buffer for POST requests.
Definition: json_mhd.c:43
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_new(type)
Allocate a struct or union of the given type.
size_t fill
Number of valid bytes in buffer.
Definition: json_mhd.c:53
Parsing successful, JSON result is in *json.
static int buffer_init(struct Buffer *buf, const void *data, size_t data_size, size_t alloc_size, size_t max_size)
Initialize a buffer.
Definition: json_mhd.c:78
Sorry, memory allocation (malloc()) failed.
Request size exceeded buffer_max argument.
static int buffer_append(struct Buffer *buf, const void *data, size_t data_size, size_t max_size)
Append data to a buffer, growing the buffer if necessary.
Definition: json_mhd.c:122
char * data
Allocated memory.
Definition: json_mhd.c:48
#define REQUEST_BUFFER_INITIAL
Initial size for POST request buffers.
Definition: json_mhd.c:37
Parsing continues, call again soon!
#define GNUNET_log(kind,...)
static void buffer_deinit(struct Buffer *buf)
Free the data in a buffer.
Definition: json_mhd.c:104
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:

◆ GNUNET_JSON_post_parser_cleanup()

void GNUNET_JSON_post_parser_cleanup ( void *  con_cls)

Function called whenever we are done with a request to clean up our state.

Parameters
con_clsvalue as it was left by GNUNET_JSON_post_parser(), to be cleaned up

Definition at line 357 of file json_mhd.c.

References buffer_deinit(), and GNUNET_free.

358 {
359  struct Buffer *r = con_cls;
360 
361  if (NULL != r)
362  {
363  buffer_deinit (r);
364  GNUNET_free (r);
365  }
366 }
Buffer for POST requests.
Definition: json_mhd.c:43
static void buffer_deinit(struct Buffer *buf)
Free the data in a buffer.
Definition: json_mhd.c:104
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:

◆ GNUNET_JSON_getopt()

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.

Parameters
shortNameshort name of the option
namelong name of the option
argumentHelphelp text for the option argument
descriptionlong help text for the option
[out]valset to the JSON specified at the command line

Definition at line 159 of file json.c.

References GNUNET_GETOPT_CommandLineOption::argumentHelp, GNUNET_GETOPT_CommandLineOption::description, name, set_json(), and GNUNET_GETOPT_CommandLineOption::shortName.

164 {
166  .name = name,
167  .argumentHelp = argumentHelp,
168  .description = description,
169  .require_argument = 1,
170  .processor = &set_json,
171  .scls = (void *) json };
172 
173  return clo;
174 }
const char * argumentHelp
Name of the argument for the user in help text.
const char * description
Help text for the option (description)
Definition of a command line option.
const char shortName
Short name of the option.
static int set_json(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Set an option with a JSON value from the command line.
Definition: json.c:127
const char * name
Here is the call graph for this function: