GNUnet  0.19.3
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...
 
struct  GNUNET_JSON_PackSpec
 Element in the array to give to the packer. 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_spec_fixed64_auto(name, obj)    GNUNET_JSON_spec_fixed (name, obj, sizeof(*obj))
 Fixed size object (in network byte order, encoded using base64 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...
 
#define GNUNET_JSON_from_data64_auto(ptr)    GNUNET_JSON_from_data64 (ptr, sizeof(*ptr))
 Convert binary data to a JSON string with base64 encoding. More...
 
#define GNUNET_JSON_PACK(...)
 Pack a JSON object from a spec. More...
 
#define GNUNET_JSON_pack_data_auto(name, blob)    GNUNET_JSON_pack_data_varsize (name, blob, sizeof (*blob))
 Generate packer instruction for a JSON field where the size is automatically determined from the argument. More...
 
#define GNUNET_JSON_pack_data64_auto(name, blob)    GNUNET_JSON_pack_data64_varsize (name, blob, sizeof (*blob))
 Generate packer instruction for a JSON field where the size is automatically determined from the argument. More...
 

Typedefs

typedef enum GNUNET_GenericReturnValue(* 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...
 
typedef json_t *(* GNUNET_JSON_PackCallback) (const struct GNUNET_JSON_PackSpec *se)
 Function called to pack an element into the JSON object as part of GNUNET_JSON_pack_(). 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, bool *missing)
 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_fixed64 (const char *name, void *obj, size_t size)
 Variable size object (in network byte order, encoded using base64 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_timestamp (const char *name, struct GNUNET_TIME_Timestamp *t)
 Timestamp. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_timestamp_nbo (const char *name, struct GNUNET_TIME_TimestampNBO *tn)
 Timestamp 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_data64 (const void *data, size_t size)
 Convert binary data to a JSON string with base64 encoding. More...
 
json_t * GNUNET_JSON_from_timestamp (struct GNUNET_TIME_Timestamp stamp)
 Convert timestamp to a json string. More...
 
json_t * GNUNET_JSON_from_timestamp_nbo (struct GNUNET_TIME_TimestampNBO stamp)
 Convert 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...
 
json_t * GNUNET_JSON_pack_ (struct GNUNET_JSON_PackSpec spec[])
 Pack a JSON object from a spec. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_end_ (void)
 Do not use directly. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_allow_null (struct GNUNET_JSON_PackSpec in)
 Modify packer instruction to allow NULL as a value. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_bool (const char *name, bool b)
 Generate packer instruction for a JSON field of type bool. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_string (const char *name, const char *s)
 Generate packer instruction for a JSON field of type string. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_uint64 (const char *name, uint64_t num)
 Generate packer instruction for a JSON field of type unsigned integer. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_int64 (const char *name, int64_t num)
 Generate packer instruction for a JSON field of type signed integer. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_object_steal (const char *name, json_t *o)
 Generate packer instruction for a JSON field of type JSON object where the reference is taken over by the packer. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_object_incref (const char *name, json_t *o)
 Generate packer instruction for a JSON field of type JSON object where the reference counter is incremented by the packer. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_array_steal (const char *name, json_t *a)
 Generate packer instruction for a JSON field of type JSON array where the reference is taken over by the packer. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_array_incref (const char *name, json_t *a)
 Generate packer instruction for a JSON field of type JSON array where the reference counter is incremented by the packer. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_data_varsize (const char *name, const void *blob, size_t blob_size)
 Generate packer instruction for a JSON field of type variable size binary blob. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_data64_varsize (const char *name, const void *blob, size_t blob_size)
 Generate packer instruction for a JSON field of type variable size binary blob. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_timestamp (const char *name, struct GNUNET_TIME_Timestamp at)
 Generate packer instruction for a JSON field of type timestamp. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_timestamp_nbo (const char *name, struct GNUNET_TIME_TimestampNBO at)
 Generate packer instruction for a JSON field of type timestamp in network byte order. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_rel (const char *name, struct GNUNET_TIME_Relative rt)
 Generate packer instruction for a JSON field of type relative time. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_rel_nbo (const char *name, struct GNUNET_TIME_RelativeNBO rt)
 Generate packer instruction for a JSON field of type relative time in network byte order. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_rsa_public_key (const char *name, const struct GNUNET_CRYPTO_RsaPublicKey *pk)
 Generate packer instruction for a JSON field of type RSA public key. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_rsa_signature (const char *name, const struct GNUNET_CRYPTO_RsaSignature *sig)
 Generate packer instruction for a JSON field of type RSA signature. 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 196 of file gnunet_json_lib.h.

◆ GNUNET_JSON_spec_fixed64_auto

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

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

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

Definition at line 219 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 429 of file gnunet_json_lib.h.

◆ GNUNET_JSON_from_data64_auto

#define GNUNET_JSON_from_data64_auto (   ptr)     GNUNET_JSON_from_data64 (ptr, sizeof(*ptr))

Convert binary data to a JSON string with base64 encoding.

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

Definition at line 440 of file gnunet_json_lib.h.

◆ GNUNET_JSON_PACK

#define GNUNET_JSON_PACK (   ...)
Value:
GNUNET_JSON_pack_ ((struct GNUNET_JSON_PackSpec[]) {__VA_ARGS__, \
GNUNET_JSON_pack_end_ ()})
json_t * GNUNET_JSON_pack_(struct GNUNET_JSON_PackSpec spec[])
Pack a JSON object from a spec.
Definition: json_pack.c:30
Element in the array to give to the packer.

Pack a JSON object from a spec.

Aborts if packing fails.

Parameters
...list of specification objects
Returns
JSON object

Definition at line 645 of file gnunet_json_lib.h.

◆ GNUNET_JSON_pack_data_auto

#define GNUNET_JSON_pack_data_auto (   name,
  blob 
)     GNUNET_JSON_pack_data_varsize (name, blob, sizeof (*blob))

Generate packer instruction for a JSON field where the size is automatically determined from the argument.

Parameters
namename of the field to add to the object
blobdata to pack, must not be an array
Returns
json pack specification

Definition at line 801 of file gnunet_json_lib.h.

◆ GNUNET_JSON_pack_data64_auto

#define GNUNET_JSON_pack_data64_auto (   name,
  blob 
)     GNUNET_JSON_pack_data64_varsize (name, blob, sizeof (*blob))

Generate packer instruction for a JSON field where the size is automatically determined from the argument.

Use base64-encoding, instead of the more common Crockford base32-encoding.

Parameters
namename of the field to add to the object
blobdata to pack, must not be an array
Returns
json pack specification

Definition at line 832 of file gnunet_json_lib.h.

Typedef Documentation

◆ GNUNET_JSON_Parser

typedef enum GNUNET_GenericReturnValue(* 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 1 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.

◆ GNUNET_JSON_PackCallback

typedef json_t*(* GNUNET_JSON_PackCallback) (const struct GNUNET_JSON_PackSpec *se)

Function called to pack an element into the JSON object as part of GNUNET_JSON_pack_().

Parameters
sepack specification to execute
Returns
json object to pack, NULL to pack nothing

Definition at line 598 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 498 of file gnunet_json_lib.h.

499 {
504 
509 
514 
519 
524 };
@ GNUNET_JSON_PR_CONTINUE
Parsing continues, call again soon!
@ GNUNET_JSON_PR_OUT_OF_MEMORY
Sorry, memory allocation (malloc()) failed.
@ GNUNET_JSON_PR_JSON_INVALID
JSON parsing failed.
@ GNUNET_JSON_PR_REQUEST_TOO_LARGE
Request size exceeded buffer_max argument.
@ GNUNET_JSON_PR_SUCCESS
Parsing successful, JSON result is in *json.

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]error_linewhich index into spec did we encounter an error
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 1 of file json.c.

36 {
37  if (NULL == root)
38  return GNUNET_SYSERR;
39  for (unsigned int i = 0; NULL != spec[i].parser; i++)
40  {
41  json_t *pos;
42 
43  if (NULL == spec[i].field)
44  pos = (json_t *) root;
45  else
46  pos = json_object_get (root,
47  spec[i].field);
48  if ( ( (NULL == pos) ||
49  (json_is_null (pos) ) ) &&
50  (spec[i].is_optional) )
51  {
52  if (NULL != spec[i].missing)
53  *spec[i].missing = true;
54  continue;
55  }
56  if ( (NULL == pos) ||
57  (GNUNET_OK !=
58  spec[i].parser (spec[i].cls,
59  pos,
60  &spec[i])) )
61  {
62  if (NULL != error_json_name)
63  *error_json_name = spec[i].field;
64  else
66  "Parsing failed for field `%s:%u`\n",
67  spec[i].field,
68  i);
69  if (NULL != error_line)
70  *error_line = i;
72  return GNUNET_SYSERR;
73  }
74  if (NULL != spec[i].missing)
75  *spec[i].missing = false;
76  }
77  return GNUNET_OK; /* all OK! */
78 }
#define GNUNET_log(kind,...)
@ GNUNET_OK
@ GNUNET_SYSERR
@ GNUNET_ERROR_TYPE_WARNING
void GNUNET_JSON_parse_free(struct GNUNET_JSON_Specification *spec)
Frees all elements allocated during a GNUNET_JSON_parse() operation.
Definition: json.c:94
bool * missing
Pointer to set to true if this argument is indeed missing.
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...
GNUNET_JSON_Parser parser
Function for how to parse this type of entry.

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

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

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 94 of file json.c.

95 {
96  for (unsigned int i = 0; NULL != spec[i].parser; i++)
97  if (NULL != spec[i].cleaner)
98  spec[i].cleaner (spec[i].cls,
99  &spec[i]);
100 }
GNUNET_JSON_Cleaner cleaner
Function for how to clean up this type of entry.

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

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 1 of file json_helper.c.

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

References ret.

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

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,
bool *  missing 
)

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

Parameters
specspecification to modify
[out]missingset to true if the argument is missing, NULL is allowed.
Returns
spec copy of spec with optional bit set

Definition at line 1 of file json.c.

84 {
85  struct GNUNET_JSON_Specification ret = spec;
86 
87  ret.is_optional = true;
88  ret.missing = missing;
89  return ret;
90 }

◆ 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 1 of file json_helper.c.

91 {
93  .parser = &parse_fixed_data,
94  .cleaner = NULL,
95  .cls = NULL,
96  .field = name,
97  .ptr = obj,
98  .ptr_size = size,
99  .size_ptr = NULL
100  };
101 
102  return ret;
103 }
static enum GNUNET_GenericReturnValue 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
static unsigned int size
Size of the "table".
Definition: peer.c:68
const char * name

◆ GNUNET_JSON_spec_fixed64()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_fixed64 ( const char *  name,
void *  obj,
size_t  size 
)

Variable size object (in network byte order, encoded using base64 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 1 of file json_helper.c.

155 {
156  struct GNUNET_JSON_Specification ret = {
157  .parser = &parse_fixed64_data,
158  .cleaner = NULL,
159  .cls = NULL,
160  .field = name,
161  .ptr = obj,
162  .ptr_size = size,
163  .size_ptr = NULL
164  };
165 
166  return ret;
167 }
static enum GNUNET_GenericReturnValue parse_fixed64_data(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to fixed size data.
Definition: json_helper.c:115

◆ 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 215 of file json_helper.c.

232 {
233  struct GNUNET_JSON_Specification ret = {
234  .parser = &parse_variable_data,
235  .cleaner = &clean_variable_data,
236  .cls = NULL,
237  .field = name,
238  .ptr = obj,
239  .ptr_size = 0,
240  .size_ptr = size
241  };
242 
243  *obj = NULL;
244  *size = 0;
245  return ret;
246 }
static void clean_variable_data(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing variable size data.
Definition: json_helper.c:215
static enum GNUNET_GenericReturnValue 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:179

◆ 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 215 of file json_helper.c.

279 {
280  struct GNUNET_JSON_Specification ret = {
281  .parser = &parse_string,
282  .cleaner = NULL,
283  .cls = NULL,
284  .field = name,
285  .ptr = strptr,
286  .ptr_size = 0,
287  .size_ptr = NULL
288  };
289 
290  *strptr = NULL;
291  return ret;
292 }
static enum GNUNET_GenericReturnValue parse_string(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to string.
Definition: json_helper.c:258

◆ 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 326 of file json_helper.c.

342 {
343  struct GNUNET_JSON_Specification ret = {
344  .parser = &parse_object,
345  .cleaner = &clean_object,
346  .cls = NULL,
347  .field = name,
348  .ptr = jsonp,
349  .ptr_size = 0,
350  .size_ptr = NULL
351  };
352 
353  *jsonp = NULL;
354  return ret;
355 }
static void clean_object(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing JSON object.
Definition: json_helper.c:326
static enum GNUNET_GenericReturnValue parse_object(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a JSON object.
Definition: json_helper.c:304

◆ 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 326 of file json_helper.c.

391 {
392  struct GNUNET_JSON_Specification ret = {
393  .parser = &parse_bool,
394  .cleaner = NULL,
395  .cls = NULL,
396  .field = name,
397  .ptr = b,
398  .ptr_size = sizeof(bool),
399  .size_ptr = NULL
400  };
401 
402  return ret;
403 }
static enum GNUNET_GenericReturnValue parse_bool(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a bool.
Definition: json_helper.c:367
size_t * size_ptr
Where should we store the final size of ptr.

◆ 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 326 of file json_helper.c.

441 {
442  struct GNUNET_JSON_Specification ret = {
443  .parser = &parse_u8,
444  .cleaner = NULL,
445  .cls = NULL,
446  .field = name,
447  .ptr = u8,
448  .ptr_size = sizeof(uint8_t),
449  .size_ptr = NULL
450  };
451 
452  return ret;
453 }
static enum GNUNET_GenericReturnValue parse_u8(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a uint8_t.
Definition: json_helper.c:415

◆ 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 326 of file json_helper.c.

491 {
492  struct GNUNET_JSON_Specification ret = {
493  .parser = &parse_u16,
494  .cleaner = NULL,
495  .cls = NULL,
496  .field = name,
497  .ptr = u16,
498  .ptr_size = sizeof(uint16_t),
499  .size_ptr = NULL
500  };
501 
502  return ret;
503 }
static enum GNUNET_GenericReturnValue parse_u16(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a uint16_t.
Definition: json_helper.c:465

◆ 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 326 of file json_helper.c.

541 {
542  struct GNUNET_JSON_Specification ret = {
543  .parser = &parse_u32,
544  .cleaner = NULL,
545  .cls = NULL,
546  .field = name,
547  .ptr = u32,
548  .ptr_size = sizeof(uint32_t),
549  .size_ptr = NULL
550  };
551 
552  return ret;
553 }
static enum GNUNET_GenericReturnValue parse_u32(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a uint32_t.
Definition: json_helper.c:515

◆ 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 326 of file json_helper.c.

586 {
587  struct GNUNET_JSON_Specification ret = {
588  .parser = &parse_u64,
589  .cleaner = NULL,
590  .cls = NULL,
591  .field = name,
592  .ptr = u64,
593  .ptr_size = sizeof(uint64_t),
594  .size_ptr = NULL
595  };
596 
597  return ret;
598 }
static enum GNUNET_GenericReturnValue parse_u64(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a uint64_t.
Definition: json_helper.c:565

◆ 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 326 of file json_helper.c.

631 {
632  struct GNUNET_JSON_Specification ret = {
633  .parser = &parse_i64,
634  .cleaner = NULL,
635  .cls = NULL,
636  .field = name,
637  .ptr = i64,
638  .ptr_size = sizeof(int64_t),
639  .size_ptr = NULL
640  };
641 
642  return ret;
643 }
static enum GNUNET_GenericReturnValue parse_i64(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a int64_t.
Definition: json_helper.c:610

◆ 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 978 of file json_helper.c.

1038 {
1039  struct GNUNET_JSON_Specification ret = {
1040  .parser = &parse_boolean,
1041  .cleaner = NULL,
1042  .cls = NULL,
1043  .field = name,
1044  .ptr = boolean,
1045  .ptr_size = sizeof(int),
1046  .size_ptr = NULL
1047  };
1048 
1049  return ret;
1050 }
static enum GNUNET_GenericReturnValue 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:1019

◆ GNUNET_JSON_spec_timestamp()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_timestamp ( const char *  name,
struct GNUNET_TIME_Timestamp t 
)

Timestamp.

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

Definition at line 326 of file json_helper.c.

710 {
711  struct GNUNET_JSON_Specification ret = {
712  .parser = &parse_timestamp,
713  .field = name,
714  .ptr = t,
715  .ptr_size = sizeof(struct GNUNET_TIME_Timestamp)
716  };
717 
718  return ret;
719 }
static struct GNUNET_SCHEDULER_Task * t
Main task.
static enum GNUNET_GenericReturnValue parse_timestamp(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a timestamp.
Definition: json_helper.c:657
Rounded time for timestamps used by GNUnet, in seconds.

◆ GNUNET_JSON_spec_timestamp_nbo()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_timestamp_nbo ( const char *  name,
struct GNUNET_TIME_TimestampNBO tn 
)

Timestamp in network byte order.

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

Definition at line 326 of file json_helper.c.

755 {
756  struct GNUNET_JSON_Specification ret = {
757  .parser = &parse_timestamp_nbo,
758  .field = name,
759  .ptr = at,
760  .ptr_size = sizeof(struct GNUNET_TIME_TimestampNBO)
761  };
762 
763  return ret;
764 }
static enum GNUNET_GenericReturnValue parse_timestamp_nbo(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to absolute time.
Definition: json_helper.c:731
Time for timestamps used by GNUnet, in seconds and in network byte order.

◆ 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 326 of file json_helper.c.

825 {
826  struct GNUNET_JSON_Specification ret = {
827  .parser = &parse_rel_time,
828  .field = name,
829  .ptr = rt,
830  .ptr_size = sizeof(struct GNUNET_TIME_Relative)
831  };
832 
833  return ret;
834 }
static enum GNUNET_GenericReturnValue parse_rel_time(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to relative time.
Definition: json_helper.c:776
Time for relative time used by GNUnet, in microseconds.

◆ 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 893 of file json_helper.c.

909 {
910  struct GNUNET_JSON_Specification ret = {
911  .parser = &parse_rsa_public_key,
912  .cleaner = &clean_rsa_public_key,
913  .field = name,
914  .ptr = pk
915  };
916 
917  *pk = NULL;
918  return ret;
919 }
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
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:893
static enum GNUNET_GenericReturnValue 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:846

◆ 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 978 of file json_helper.c.

994 {
995  struct GNUNET_JSON_Specification ret = {
996  .parser = &parse_rsa_signature,
997  .cleaner = &clean_rsa_signature,
998  .cls = NULL,
999  .field = name,
1000  .ptr = sig,
1001  .ptr_size = 0,
1002  .size_ptr = NULL
1003  };
1004 
1005  *sig = NULL;
1006  return ret;
1007 }
static enum GNUNET_GenericReturnValue parse_rsa_signature(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to RSA signature.
Definition: json_helper.c:931
static void clean_rsa_signature(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA signature.
Definition: json_helper.c:978

◆ 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 31 of file json_generator.c.

33 {
34  char *buf;
35  json_t *json;
36 
37  if ((size * 8 + 4) / 5 + 1 >=
39  {
40  GNUNET_break (0);
41  return NULL;
42  }
44  size);
45  json = json_string (buf);
46  GNUNET_free (buf);
47  GNUNET_break (NULL != json);
48  return json;
49 }
uint32_t data
The data value.
static char buf[2048]
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
#define GNUNET_free(ptr)
Wrapper around free.
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:763

References buf, data, GNUNET_break, GNUNET_free, GNUNET_MAX_MALLOC_CHECKED, GNUNET_STRINGS_data_to_string_alloc(), and size.

Referenced by GNUNET_JSON_from_rsa_public_key(), and GNUNET_JSON_from_rsa_signature().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_JSON_from_data64()

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

Convert binary data to a JSON string with base64 encoding.

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

Definition at line 53 of file json_generator.c.

55 {
56  char *buf = NULL;
57  json_t *json;
58  size_t len;
59 
60  if ((size * 8 + 5) / 6 + 1 >=
62  {
63  GNUNET_break (0);
64  return NULL;
65  }
67  size,
68  &buf);
69  if (NULL == buf)
70  {
71  GNUNET_break (0);
72  return NULL;
73  }
74  json = json_stringn (buf,
75  len);
76  GNUNET_free (buf);
77  GNUNET_break (NULL != json);
78  return json;
79 }
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:1607

References buf, data, GNUNET_break, GNUNET_free, GNUNET_MAX_MALLOC_CHECKED, GNUNET_STRINGS_base64_encode(), len, and size.

Here is the call graph for this function:

◆ GNUNET_JSON_from_timestamp()

json_t* GNUNET_JSON_from_timestamp ( struct GNUNET_TIME_Timestamp  stamp)

Convert timestamp to a json string.

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

Definition at line 83 of file json_generator.c.

84 {
85  json_t *j;
86 
87  j = json_object ();
88  if (NULL == j)
89  {
90  GNUNET_break (0);
91  return NULL;
92  }
94  {
95  if (0 !=
96  json_object_set_new (j,
97  "t_s",
98  json_string ("never")))
99  {
100  GNUNET_break (0);
101  json_decref (j);
102  return NULL;
103  }
104  return j;
105  }
106  GNUNET_assert (
107  0 ==
108  (stamp.abs_time.abs_value_us
109  % GNUNET_TIME_UNIT_SECONDS.rel_value_us));
110  if (0 !=
111  json_object_set_new (
112  j,
113  "t_s",
114  json_integer (
115  (json_int_t) (stamp.abs_time.abs_value_us
116  / GNUNET_TIME_UNIT_SECONDS.rel_value_us))))
117  {
118  GNUNET_break (0);
119  json_decref (j);
120  return NULL;
121  }
122  return j;
123 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_TIME_UNIT_SECONDS
One second.
bool GNUNET_TIME_absolute_is_never(struct GNUNET_TIME_Absolute abs)
Test if abs is never.
Definition: time.c:648
uint64_t abs_value_us
The actual value.
struct GNUNET_TIME_Absolute abs_time
The actual value.

References GNUNET_TIME_Timestamp::abs_time, GNUNET_TIME_Absolute::abs_value_us, GNUNET_assert, GNUNET_break, GNUNET_TIME_absolute_is_never(), and GNUNET_TIME_UNIT_SECONDS.

Referenced by GNUNET_JSON_from_timestamp_nbo().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_JSON_from_timestamp_nbo()

json_t* GNUNET_JSON_from_timestamp_nbo ( struct GNUNET_TIME_TimestampNBO  stamp)

Convert timestamp to a json string.

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

Definition at line 127 of file json_generator.c.

128 {
130 }
struct GNUNET_TIME_Timestamp GNUNET_TIME_timestamp_ntoh(struct GNUNET_TIME_TimestampNBO tn)
Convert timestamp from network byte order.
Definition: time.c:101
json_t * GNUNET_JSON_from_timestamp(struct GNUNET_TIME_Timestamp stamp)
Convert timestamp to a json string.

References GNUNET_JSON_from_timestamp(), and GNUNET_TIME_timestamp_ntoh().

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 134 of file json_generator.c.

135 {
136  json_t *j;
137 
138  j = json_object ();
139  if (NULL == j)
140  {
141  GNUNET_break (0);
142  return NULL;
143  }
145  {
146  if (0 !=
147  json_object_set_new (j,
148  "d_us",
149  json_string ("forever")))
150  {
151  GNUNET_break (0);
152  json_decref (j);
153  return NULL;
154  }
155  return j;
156  }
157  if (stamp.rel_value_us >= (1LLU << 53))
158  {
159  /* value is larger than allowed */
160  GNUNET_break (0);
161  return NULL;
162  }
163  if (0 !=
164  json_object_set_new (
165  j,
166  "d_us",
167  json_integer ((json_int_t) stamp.rel_value_us)))
168  {
169  GNUNET_break (0);
170  json_decref (j);
171  return NULL;
172  }
173  return j;
174 }
bool GNUNET_TIME_relative_is_forever(struct GNUNET_TIME_Relative rel)
Test if rel is forever.
Definition: time.c:655
uint64_t rel_value_us
The actual value.

References GNUNET_break, GNUNET_TIME_relative_is_forever(), and GNUNET_TIME_Relative::rel_value_us.

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 178 of file json_generator.c.

179 {
180  void *buf;
181  size_t buf_len;
182  json_t *ret;
183 
185  &buf);
187  buf_len);
188  GNUNET_free (buf);
189  return ret;
190 }
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:325
json_t * GNUNET_JSON_from_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.

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

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 194 of file json_generator.c.

195 {
196  void *buf;
197  size_t buf_len;
198  json_t *ret;
199 
200  buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
201  &buf);
203  buf_len);
204  GNUNET_free (buf);
205  return ret;
206 }
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:999

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

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 122 of file json_mhd.c.

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);
323  {
324  buffer_deinit (r);
325  GNUNET_free (r);
326  *con_cls = NULL;
327  return ret;
328  }
329  }
330 
331  {
332  json_error_t err;
333 
334  *json = json_loadb (r->data,
335  r->fill,
336  0,
337  &err);
338  if (NULL == *json)
339  {
341  "Failed to parse JSON request body of %u byte at offset %d: %s\n",
342  (unsigned int) r->fill,
343  err.position,
344  err.text);
345  buffer_deinit (r);
346  GNUNET_free (r);
347  *con_cls = NULL;
349  }
350  }
351  buffer_deinit (r);
352  GNUNET_free (r);
353  *con_cls = NULL;
354 
355  return GNUNET_JSON_PR_SUCCESS;
356 }
#define GNUNET_new(type)
Allocate a struct or union of the given type.
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
#define REQUEST_BUFFER_INITIAL
Initial size for POST request buffers.
Definition: json_mhd.c:37
static enum GNUNET_JSON_PostResult inflate_data(struct Buffer *buf)
Decompress data in buf.
Definition: json_mhd.c:156
static void buffer_deinit(struct Buffer *buf)
Free the data in a buffer.
Definition: json_mhd.c:104
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
Buffer for POST requests.
Definition: json_mhd.c:44
size_t fill
Number of valid bytes in buffer.
Definition: json_mhd.c:53
char * data
Allocated memory.
Definition: json_mhd.c:48

Referenced by create_response().

Here is the caller 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 367 of file json_mhd.c.

368 {
369  struct Buffer *r = con_cls;
370 
371  if (NULL != r)
372  {
373  buffer_deinit (r);
374  GNUNET_free (r);
375  }
376 }

References buffer_deinit(), and GNUNET_free.

Referenced by create_response().

Here is the call graph for this function:
Here is the caller 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 94 of file json.c.

146 {
147  struct GNUNET_GETOPT_CommandLineOption clo = {
148  .shortName = shortName,
149  .name = name,
150  .argumentHelp = argumentHelp,
151  .description = description,
152  .require_argument = 1,
153  .processor = &set_json,
154  .scls = (void *) json
155  };
156 
157  return clo;
158 }
static enum GNUNET_GenericReturnValue 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:116
Definition of a command line option.
const char * description
Help text for the option (description)
const char * argumentHelp
Name of the argument for the user in help text.
const char shortName
Short name of the option.

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

◆ GNUNET_JSON_pack_()

json_t* GNUNET_JSON_pack_ ( struct GNUNET_JSON_PackSpec  spec[])

Pack a JSON object from a spec.

Aborts if packing fails.

Parameters
specspecification object
Returns
JSON object

Definition at line 30 of file json_pack.c.

31 {
32  json_t *ret;
33 
34  if (NULL == spec[0].field_name)
35  {
36  ret = spec[0].object;
37  spec[0].object = NULL;
38  return ret;
39  }
40  ret = json_object ();
41  GNUNET_assert (NULL != ret);
42  for (unsigned int i = 0;
43  NULL != spec[i].field_name;
44  i++)
45  {
46  if (NULL == spec[i].object)
47  {
48  GNUNET_assert (spec[i].allow_null);
49  }
50  else
51  {
52  GNUNET_assert (0 ==
53  json_object_set_new (ret,
54  spec[i].field_name,
55  spec[i].object));
56  spec[i].object = NULL;
57  }
58  }
59  return ret;
60 }
json_t * object
Object to pack.
const char * field_name
Name of the field to pack.

References GNUNET_JSON_PackSpec::field_name, GNUNET_assert, GNUNET_JSON_PackSpec::object, and ret.

◆ GNUNET_JSON_pack_end_()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_end_ ( void  )

Do not use directly.

Use GNUNET_JSON_PACK.

Returns
array terminator

Definition at line 30 of file json_pack.c.

65 {
66  struct GNUNET_JSON_PackSpec ps = {
67  .field_name = NULL
68  };
69 
70  return ps;
71 }

◆ GNUNET_JSON_pack_allow_null()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_allow_null ( struct GNUNET_JSON_PackSpec  in)

Modify packer instruction to allow NULL as a value.

Parameters
injson pack specification to modify
Returns
json pack specification

Definition at line 30 of file json_pack.c.

76 {
77  in.allow_null = true;
78  return in;
79 }
bool allow_null
True if a NULL (or 0) argument is allowed.

◆ GNUNET_JSON_pack_bool()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_bool ( const char *  name,
bool  b 
)

Generate packer instruction for a JSON field of type bool.

Parameters
namename of the field to add to the object
bboolean value
Returns
json pack specification

Definition at line 30 of file json_pack.c.

85 {
86  struct GNUNET_JSON_PackSpec ps = {
87  .field_name = name,
88  .object = json_boolean (b)
89  };
90 
91  return ps;
92 }

◆ GNUNET_JSON_pack_string()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_string ( const char *  name,
const char *  s 
)

Generate packer instruction for a JSON field of type string.

Parameters
namename of the field to add to the object
sstring value
Returns
json pack specification

Definition at line 30 of file json_pack.c.

98 {
99  struct GNUNET_JSON_PackSpec ps = {
100  .field_name = name,
101  .object = json_string (s)
102  };
103 
104  return ps;
105 }

◆ GNUNET_JSON_pack_uint64()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_uint64 ( const char *  name,
uint64_t  num 
)

Generate packer instruction for a JSON field of type unsigned integer.

Note that the maximum allowed value is still limited by JSON and not UINT64_MAX.

Parameters
namename of the field to add to the object
numnumeric value
Returns
json pack specification

Definition at line 30 of file json_pack.c.

111 {
112  struct GNUNET_JSON_PackSpec ps = {
113  .field_name = name,
114  .object = json_integer ((json_int_t) num)
115  };
116 
117 #if JSON_INTEGER_IS_LONG_LONG
118  GNUNET_assert (num <= LLONG_MAX);
119 #else
120  GNUNET_assert (num <= LONG_MAX);
121 #endif
122  return ps;
123 }

◆ GNUNET_JSON_pack_int64()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_int64 ( const char *  name,
int64_t  num 
)

Generate packer instruction for a JSON field of type signed integer.

Parameters
namename of the field to add to the object
numnumeric value
Returns
json pack specification

Definition at line 30 of file json_pack.c.

129 {
130  struct GNUNET_JSON_PackSpec ps = {
131  .field_name = name,
132  .object = json_integer ((json_int_t) num)
133  };
134 
135 #if JSON_INTEGER_IS_LONG_LONG
136  GNUNET_assert (num <= LLONG_MAX);
137  GNUNET_assert (num >= LLONG_MIN);
138 #else
139  GNUNET_assert (num <= LONG_MAX);
140  GNUNET_assert (num >= LONG_MIN);
141 #endif
142  return ps;
143 }

◆ GNUNET_JSON_pack_object_steal()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_object_steal ( const char *  name,
json_t *  o 
)

Generate packer instruction for a JSON field of type JSON object where the reference is taken over by the packer.

Parameters
namename of the field to add to the object
oobject to steal
Returns
json pack specification

Definition at line 30 of file json_pack.c.

149 {
150  struct GNUNET_JSON_PackSpec ps = {
151  .field_name = name,
152  .object = o
153  };
154 
155  if (NULL == o)
156  return ps;
157  if (! json_is_object (o))
158  {
160  "Expected JSON object for field `%s'\n",
161  name);
162  GNUNET_assert (0);
163  }
164  return ps;
165 }
@ GNUNET_ERROR_TYPE_ERROR

◆ GNUNET_JSON_pack_object_incref()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_object_incref ( const char *  name,
json_t *  o 
)

Generate packer instruction for a JSON field of type JSON object where the reference counter is incremented by the packer.

Note that a deep copy is not performed.

Parameters
namename of the field to add to the object
oobject to increment reference counter of
Returns
json pack specification

Definition at line 30 of file json_pack.c.

171 {
172  struct GNUNET_JSON_PackSpec ps = {
173  .field_name = name,
174  .object = o
175  };
176 
177  if (NULL == o)
178  return ps;
179  (void) json_incref (o);
180  if (! json_is_object (o))
181  {
183  "Expected JSON object for field `%s'\n",
184  name);
185  GNUNET_assert (0);
186  }
187  return ps;
188 }

◆ GNUNET_JSON_pack_array_steal()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_array_steal ( const char *  name,
json_t *  a 
)

Generate packer instruction for a JSON field of type JSON array where the reference is taken over by the packer.

Parameters
namename of the field to add to the object
aarray to steal
Returns
json pack specification

Definition at line 30 of file json_pack.c.

194 {
195  struct GNUNET_JSON_PackSpec ps = {
196  .field_name = name,
197  .object = a
198  };
199 
200  if (NULL == a)
201  return ps;
202  if (! json_is_array (a))
203  {
205  "Expected JSON array for field `%s'\n",
206  name);
207  GNUNET_assert (0);
208  }
209  return ps;
210 }

◆ GNUNET_JSON_pack_array_incref()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_array_incref ( const char *  name,
json_t *  a 
)

Generate packer instruction for a JSON field of type JSON array where the reference counter is incremented by the packer.

Note that a deep copy is not performed.

Parameters
namename of the field to add to the object
aarray to increment reference counter of
Returns
json pack specification

Definition at line 30 of file json_pack.c.

216 {
217  struct GNUNET_JSON_PackSpec ps = {
218  .field_name = name,
219  .object = a
220  };
221 
222  if (NULL == a)
223  return ps;
224  (void) json_incref (a);
225  if (! json_is_array (a))
226  {
228  "Expected JSON array for field `%s'\n",
229  name);
230  GNUNET_assert (0);
231  }
232  return ps;
233 }

◆ GNUNET_JSON_pack_data_varsize()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_data_varsize ( const char *  name,
const void *  blob,
size_t  blob_size 
)

Generate packer instruction for a JSON field of type variable size binary blob.

Parameters
namename of the field to add to the object
blobbinary data to pack
blob_sizenumber of bytes in blob
Returns
json pack specification

Definition at line 30 of file json_pack.c.

240 {
241  struct GNUNET_JSON_PackSpec ps = {
242  .field_name = name,
243  .object = (NULL != blob)
244  ? GNUNET_JSON_from_data (blob,
245  blob_size)
246  : NULL
247  };
248 
249  return ps;
250 }
json_t * GNUNET_JSON_from_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.

◆ GNUNET_JSON_pack_data64_varsize()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_data64_varsize ( const char *  name,
const void *  blob,
size_t  blob_size 
)

Generate packer instruction for a JSON field of type variable size binary blob.

Use base64-encoding, instead of the more common Crockford base32-encoding.

Parameters
namename of the field to add to the object
blobbinary data to pack
blob_sizenumber of bytes in blob
Returns
json pack specification

Definition at line 30 of file json_pack.c.

257 {
258  struct GNUNET_JSON_PackSpec ps = {
259  .field_name = name,
260  .object = (NULL != blob)
261  ? GNUNET_JSON_from_data64 (blob,
262  blob_size)
263  : NULL
264  };
265 
266  return ps;
267 }
json_t * GNUNET_JSON_from_data64(const void *data, size_t size)
Convert binary data to a JSON string with base64 encoding.

◆ GNUNET_JSON_pack_timestamp()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_timestamp ( const char *  name,
struct GNUNET_TIME_Timestamp  at 
)

Generate packer instruction for a JSON field of type timestamp.

Parameters
namename of the field to add to the object
attimestamp pack, a value of 0 is only allowed with GNUNET_JSON_pack_allow_null()!
Returns
json pack specification

Definition at line 30 of file json_pack.c.

273 {
274  struct GNUNET_JSON_PackSpec ps = {
275  .field_name = name
276  };
277 
278  if (! GNUNET_TIME_absolute_is_zero (t.abs_time))
279  {
281  GNUNET_assert (NULL != ps.object);
282  }
283  else
284  {
285  ps.object = NULL;
286  }
287  return ps;
288 }
json_t * GNUNET_JSON_from_timestamp(struct GNUNET_TIME_Timestamp stamp)
Convert timestamp to a json string.
bool GNUNET_TIME_absolute_is_zero(struct GNUNET_TIME_Absolute abs)
Test if abs is truly zero.
Definition: time.c:844

◆ GNUNET_JSON_pack_timestamp_nbo()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_timestamp_nbo ( const char *  name,
struct GNUNET_TIME_TimestampNBO  at 
)

Generate packer instruction for a JSON field of type timestamp in network byte order.

Parameters
namename of the field to add to the object
attimestamp to pack, a value of 0 is only allowed with GNUNET_JSON_pack_allow_null()!
Returns
json pack specification

Definition at line 30 of file json_pack.c.

294 {
297 }
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_timestamp(const char *name, struct GNUNET_TIME_Timestamp t)
Generate packer instruction for a JSON field of type timestamp.
Definition: json_pack.c:271

◆ GNUNET_JSON_pack_time_rel()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_rel ( const char *  name,
struct GNUNET_TIME_Relative  rt 
)

Generate packer instruction for a JSON field of type relative time.

Parameters
namename of the field to add to the object
rtrelative time to pack
Returns
json pack specification

Definition at line 30 of file json_pack.c.

303 {
304  json_t *json;
305 
306  json = GNUNET_JSON_from_time_rel (rt);
307  GNUNET_assert (NULL != json);
309  json);
310 }
json_t * GNUNET_JSON_from_time_rel(struct GNUNET_TIME_Relative stamp)
Convert relative timestamp to a json string.
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_object_steal(const char *name, json_t *o)
Generate packer instruction for a JSON field of type JSON object where the reference is taken over by...
Definition: json_pack.c:147

◆ GNUNET_JSON_pack_time_rel_nbo()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_rel_nbo ( const char *  name,
struct GNUNET_TIME_RelativeNBO  rt 
)

Generate packer instruction for a JSON field of type relative time in network byte order.

Parameters
namename of the field to add to the object
rtrelative time to pack
Returns
json pack specification

Definition at line 30 of file json_pack.c.

316 {
319 }
struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh(struct GNUNET_TIME_RelativeNBO a)
Convert relative time from network byte order.
Definition: time.c:628
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_rel(const char *name, struct GNUNET_TIME_Relative rt)
Generate packer instruction for a JSON field of type relative time.
Definition: json_pack.c:301

◆ GNUNET_JSON_pack_rsa_public_key()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_rsa_public_key ( const char *  name,
const struct GNUNET_CRYPTO_RsaPublicKey pk 
)

Generate packer instruction for a JSON field of type RSA public key.

Parameters
namename of the field to add to the object
pkRSA public key
Returns
json pack specification

Definition at line 30 of file json_pack.c.

325 {
326  struct GNUNET_JSON_PackSpec ps = {
327  .field_name = name,
329  };
330 
331  return ps;
332 }
json_t * GNUNET_JSON_from_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *pk)
Convert RSA public key to JSON.

◆ GNUNET_JSON_pack_rsa_signature()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_rsa_signature ( const char *  name,
const struct GNUNET_CRYPTO_RsaSignature sig 
)

Generate packer instruction for a JSON field of type RSA signature.

Parameters
namename of the field to add to the object
sigRSA signature
Returns
json pack specification

Definition at line 30 of file json_pack.c.

338 {
339  struct GNUNET_JSON_PackSpec ps = {
340  .field_name = name,
341  .object = GNUNET_JSON_from_rsa_signature (sig)
342  };
343 
344  return ps;
345 }
json_t * GNUNET_JSON_from_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *sig)
Convert RSA signature to JSON.