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 "gnunet_gnsrecord_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...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_gnsrecord (struct GNUNET_GNSRECORD_Data **rd, unsigned int *rd_count, char **name)
 JSON Specification for GNS Records. 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...
 
json_t * GNUNET_JSON_from_gnsrecord (const char *rname, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count)
 Convert Gns record 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 188 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 396 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 53 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 65 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 466 of file gnunet_json_lib.h.

467 {
472 
477 
482 
487 
492 };
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, and GNUNET_JSON_Specification::parser.

Referenced by add_attestation_cont(), add_attribute_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, spec[i].field);
60  if ((NULL == pos) && (spec[i].is_optional))
61  continue;
62  if ((NULL == pos) ||
63  (GNUNET_OK != spec[i].parser (spec[i].cls, pos, &spec[i])))
64  {
65  if (NULL != error_json_name)
66  *error_json_name = spec[i].field;
67  if (NULL != error_line)
68  *error_line = i;
70  return GNUNET_SYSERR;
71  }
72  }
73  return GNUNET_OK; /* all OK! */
74 }
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:100
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 100 of file json.c.

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

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

101 {
102  for (unsigned int i = 0; NULL != spec[i].parser; i++)
103  if (NULL != spec[i].cleaner)
104  spec[i].cleaner (spec[i].cls, &spec[i]);
105 }
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 35 of file json_helper.c.

References GNUNET_JSON_Specification::parser, and ret.

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

36 {
38  .parser = NULL,
39  .cleaner = NULL,
40  .cls = NULL
41  };
42 
43  return ret;
44 }
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 84 of file json.c.

References GNUNET_YES, GNUNET_JSON_Specification::is_optional, and ret.

85 {
86  struct GNUNET_JSON_Specification ret = spec;
87 
88  ret.is_optional = GNUNET_YES;
89  return ret;
90 }
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 96 of file json_helper.c.

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

99 {
100  struct GNUNET_JSON_Specification ret = {
101  .parser = &parse_fixed_data,
102  .cleaner = NULL,
103  .cls = NULL,
104  .field = name,
105  .ptr = obj,
106  .ptr_size = size,
107  .size_ptr = NULL
108  };
109 
110  return ret;
111 }
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:56
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 189 of file json_helper.c.

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

192 {
193  struct GNUNET_JSON_Specification ret = {
194  .parser = &parse_variable_data,
195  .cleaner = &clean_variable_data,
196  .cls = NULL,
197  .field = name,
198  .ptr = obj,
199  .ptr_size = 0,
200  .size_ptr = size
201  };
202 
203  *obj = NULL;
204  *size = 0;
205  return ret;
206 }
static void clean_variable_data(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing variable size data.
Definition: json_helper.c:168
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:123
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 242 of file json_helper.c.

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

244 {
245  struct GNUNET_JSON_Specification ret = {
246  .parser = &parse_string,
247  .cleaner = NULL,
248  .cls = NULL,
249  .field = name,
250  .ptr = strptr,
251  .ptr_size = 0,
252  .size_ptr = NULL
253  };
254 
255  *strptr = NULL;
256  return ret;
257 }
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:218
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 311 of file json_helper.c.

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

313 {
314  struct GNUNET_JSON_Specification ret = {
315  .parser = &parse_object,
316  .cleaner = &clean_object,
317  .cls = NULL,
318  .field = name,
319  .ptr = jsonp,
320  .ptr_size = 0,
321  .size_ptr = NULL
322  };
323 
324  *jsonp = NULL;
325  return ret;
326 }
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:269
static void clean_object(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing JSON object.
Definition: json_helper.c:291
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 366 of file json_helper.c.

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

368 {
369  struct GNUNET_JSON_Specification ret = {
370  .parser = &parse_bool,
371  .cleaner = NULL,
372  .cls = NULL,
373  .field = name,
374  .ptr = b,
375  .ptr_size = sizeof(bool),
376  .size_ptr = NULL
377  };
378 
379  return ret;
380 }
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:338
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 422 of file json_helper.c.

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

424 {
425  struct GNUNET_JSON_Specification ret = {
426  .parser = &parse_u8,
427  .cleaner = NULL,
428  .cls = NULL,
429  .field = name,
430  .ptr = u8,
431  .ptr_size = sizeof(uint8_t),
432  .size_ptr = NULL
433  };
434 
435  return ret;
436 }
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:392
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 478 of file json_helper.c.

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

480 {
481  struct GNUNET_JSON_Specification ret = {
482  .parser = &parse_u16,
483  .cleaner = NULL,
484  .cls = NULL,
485  .field = name,
486  .ptr = u16,
487  .ptr_size = sizeof(uint16_t),
488  .size_ptr = NULL
489  };
490 
491  return ret;
492 }
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:448
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 534 of file json_helper.c.

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

536 {
537  struct GNUNET_JSON_Specification ret = {
538  .parser = &parse_u32,
539  .cleaner = NULL,
540  .cls = NULL,
541  .field = name,
542  .ptr = u32,
543  .ptr_size = sizeof(uint32_t),
544  .size_ptr = NULL
545  };
546 
547  return ret;
548 }
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:504
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 585 of file json_helper.c.

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

587 {
588  struct GNUNET_JSON_Specification ret = {
589  .parser = &parse_u64,
590  .cleaner = NULL,
591  .cls = NULL,
592  .field = name,
593  .ptr = u64,
594  .ptr_size = sizeof(uint64_t),
595  .size_ptr = NULL
596  };
597 
598  return ret;
599 }
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:560
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 636 of file json_helper.c.

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

638 {
639  struct GNUNET_JSON_Specification ret = {
640  .parser = &parse_i64,
641  .cleaner = NULL,
642  .cls = NULL,
643  .field = name,
644  .ptr = i64,
645  .ptr_size = sizeof(int64_t),
646  .size_ptr = NULL
647  };
648 
649  return ret;
650 }
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:611
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

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

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

1085 {
1086  struct GNUNET_JSON_Specification ret = {
1087  .parser = &parse_boolean,
1088  .cleaner = NULL,
1089  .cls = NULL,
1090  .field = name,
1091  .ptr = boolean,
1092  .ptr_size = sizeof(int),
1093  .size_ptr = NULL
1094  };
1095 
1096  return ret;
1097 }
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:1060
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 715 of file json_helper.c.

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

717 {
718  struct GNUNET_JSON_Specification ret = {
719  .parser = &parse_abs_time,
720  .cleaner = NULL,
721  .cls = NULL,
722  .field = name,
723  .ptr = at,
724  .ptr_size = sizeof(struct GNUNET_TIME_Absolute),
725  .size_ptr = NULL
726  };
727 
728  return ret;
729 }
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:664
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 769 of file json_helper.c.

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

771 {
772  struct GNUNET_JSON_Specification ret = {
773  .parser = &parse_abs_time_nbo,
774  .cleaner = NULL,
775  .cls = NULL,
776  .field = name,
777  .ptr = at,
778  .ptr_size = sizeof(struct GNUNET_TIME_AbsoluteNBO),
779  .size_ptr = NULL
780  };
781 
782  return ret;
783 }
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:741
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 846 of file json_helper.c.

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

848 {
849  struct GNUNET_JSON_Specification ret = {
850  .parser = &parse_rel_time,
851  .cleaner = NULL,
852  .cls = NULL,
853  .field = name,
854  .ptr = rt,
855  .ptr_size = sizeof(struct GNUNET_TIME_Relative),
856  .size_ptr = NULL
857  };
858 
859  return ret;
860 }
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:795
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 939 of file json_helper.c.

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

941 {
942  struct GNUNET_JSON_Specification ret = {
943  .parser = &parse_rsa_public_key,
944  .cleaner = &clean_rsa_public_key,
945  .cls = NULL,
946  .field = name,
947  .ptr = pk,
948  .ptr_size = 0,
949  .size_ptr = NULL
950  };
951 
952  *pk = NULL;
953  return ret;
954 }
struct GNUNET_CRYPTO_EcdsaPrivateKey pk
Private key from command line option, or NULL.
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:872
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:919
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 1033 of file json_helper.c.

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

1035 {
1036  struct GNUNET_JSON_Specification ret = {
1037  .parser = &parse_rsa_signature,
1038  .cleaner = &clean_rsa_signature,
1039  .cls = NULL,
1040  .field = name,
1041  .ptr = sig,
1042  .ptr_size = 0,
1043  .size_ptr = NULL
1044  };
1045 
1046  *sig = NULL;
1047  return ret;
1048 }
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:966
static void clean_rsa_signature(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA signature.
Definition: json_helper.c:1013
const char * name
Here is the call graph for this function:

◆ GNUNET_JSON_spec_gnsrecord()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_gnsrecord ( struct GNUNET_GNSRECORD_Data **  rd,
unsigned int *  rd_count,
char **  name 
)

JSON Specification for GNS Records.

Parameters
gnsrecord_objectstruct of GNUNET_GNSRECORD_Data to fill
Returns
JSON Specification

Definition at line 261 of file json_gnsrecord.c.

References clean_gnsrecordobject(), GNUNET_new, GnsRecordInfo::name, parse_gnsrecordobject(), GNUNET_JSON_Specification::parser, GnsRecordInfo::rd, GnsRecordInfo::rd_count, and ret.

Referenced by namestore_add_or_update().

264 {
265  struct GnsRecordInfo *gnsrecord_info = GNUNET_new (struct GnsRecordInfo);
266 
267  gnsrecord_info->rd = rd;
268  gnsrecord_info->name = name;
269  gnsrecord_info->rd_count = rd_count;
271  .cleaner = &clean_gnsrecordobject,
272  .cls = NULL,
273  .field = NULL,
274  .ptr = (struct GnsRecordInfo *)
275  gnsrecord_info,
276  .ptr_size = 0,
277  .size_ptr = NULL };
278  return ret;
279 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static int parse_gnsrecordobject(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
unsigned int * rd_count
const char * name
static void clean_gnsrecordobject(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing the record.
struct GNUNET_GNSRECORD_Data ** rd
Here is the call graph for this function:
Here is the caller 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:936
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:80
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:673
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:100
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:346
#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:1076
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_from_gnsrecord()

json_t* GNUNET_JSON_from_gnsrecord ( const char *  rname,
const struct GNUNET_GNSRECORD_Data rd,
unsigned int  rd_count 
)

Convert Gns record to JSON.

Parameters
rnamename of record
rdrecord data
Returns
corresponding JSON encoding

Convert Gns record to JSON.

Parameters
rnamename of record
rdrecord data
Returns
corresponding JSON encoding

Definition at line 212 of file json_generator.c.

References GNUNET_TIME_Absolute::abs_value_us, data, data_size, GNUNET_GNSRECORD_Data::expiration_time, GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_GNSRECORD_number_to_typename(), GNUNET_GNSRECORD_RF_PRIVATE, GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION, GNUNET_GNSRECORD_RF_SHADOW_RECORD, GNUNET_GNSRECORD_RF_SUPPLEMENTAL, GNUNET_GNSRECORD_value_to_string(), GNUNET_log, GNUNET_NO, GNUNET_STRINGS_absolute_time_to_string(), GNUNET_STRINGS_relative_time_to_string(), record(), records, and GNUNET_TIME_Relative::rel_value_us.

Referenced by handle_gns_response(), namestore_list_iteration(), and ns_get_lookup_cb().

215 {
216  struct GNUNET_TIME_Absolute abs_exp;
217  struct GNUNET_TIME_Relative rel_exp;
218  const char *expiration_time_str;
219  const char *record_type_str;
220  char *value_str;
221  json_t *data;
222  json_t *record;
223  json_t *records;
224 
225  data = json_object ();
226  if (NULL == data)
227  {
228  GNUNET_break (0);
229  return NULL;
230  }
231  if (0 !=
232  json_object_set_new (data,
233  "record_name",
234  json_string (rname)))
235  {
236  GNUNET_break (0);
237  json_decref (data);
238  return NULL;
239  }
240  records = json_array ();
241  if (NULL == records)
242  {
243  GNUNET_break (0);
244  json_decref (data);
245  return NULL;
246  }
247  for (int i = 0; i < rd_count; i++)
248  {
249  value_str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
250  rd[i].data,
251  rd[i].data_size);
252  if (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION & rd[i].flags)
253  {
254  rel_exp.rel_value_us = rd[i].expiration_time;
255  expiration_time_str = GNUNET_STRINGS_relative_time_to_string (rel_exp,
256  GNUNET_NO);
257  }
258  else
259  {
260  abs_exp.abs_value_us = rd[i].expiration_time;
261  expiration_time_str = GNUNET_STRINGS_absolute_time_to_string (abs_exp);
262  }
263  record_type_str = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
265  "Packing %s %s %s %d\n",
266  value_str, record_type_str, expiration_time_str, rd[i].flags);
267  record = json_pack ("{s:s,s:s,s:s,s:b,s:b,s:b,s:b}",
268  "value",
269  value_str,
270  "record_type",
271  record_type_str,
272  "expiration_time",
273  expiration_time_str,
274  "private",
275  rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE,
276  "relative_expiration",
278  "supplemental",
279  rd[i].flags & GNUNET_GNSRECORD_RF_SUPPLEMENTAL,
280  "shadow",
281  rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD);
282  GNUNET_free (value_str);
283  if (NULL == record)
284  {
285  GNUNET_break (0);
286  json_decref (records);
287  json_decref (data);
288  return NULL;
289  }
290  if (0 !=
291  json_array_append_new (records,
292  record))
293  {
294  GNUNET_break (0);
295  json_decref (records);
296  json_decref (data);
297  return NULL;
298  }
299  }
300  if (0 !=
301  json_object_set_new (data,
302  "data",
303  records))
304  {
305  GNUNET_break (0);
306  json_decref (data);
307  return NULL;
308  }
309  return data;
310 }
This record should not be used unless all (other) records with an absolute expiration time have expir...
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
This is a supplemental record.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static unsigned int records
Number of records we found.
uint64_t expiration_time
Expiration time for the DNS record.
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:702
This is a private record of this peer and it should thus not be handed out to other peers...
#define GNUNET_log(kind,...)
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
Time for absolute times used by GNUnet, in microseconds.
This expiration time of the record is a relative time (not an absolute time).
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:757
uint32_t data
The data value.
char * GNUNET_GNSRECORD_value_to_string(uint32_t type, const void *data, size_t data_size)
Convert the &#39;value&#39; of a record to a string.
Definition: gnsrecord.c:159
const char * GNUNET_GNSRECORD_number_to_typename(uint32_t type)
Convert a type number (i.e.
Definition: gnsrecord.c:247
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
Here is the call graph for this function:
Here is the caller 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 153 of file json.c.

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

158 {
160  .name = name,
161  .argumentHelp = argumentHelp,
162  .description = description,
163  .require_argument = 1,
164  .processor = &set_json,
165  .scls = (void *) json };
166 
167  return clo;
168 }
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:121
const char * name
Here is the call graph for this function: