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

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

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

Go to the source code of this file.

Data Structures

struct  GNUNET_JSON_Specification
 Entry in parser specification for GNUNET_JSON_parse(). More...
 
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_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_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...
 

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...
 
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)
 Set the "optional" flag for a parser specification entry. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_fixed (const char *name, void *obj, size_t size)
 Variable size object (in network byte order, encoded using Crockford Base32hex encoding). More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_varsize (const char *name, void **obj, size_t *size)
 Variable size object (in network byte order, encoded using Crockford Base32hex encoding). More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_string (const char *name, const char **strptr)
 The expected field stores a string. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_json (const char *name, json_t **jsonp)
 JSON object. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_bool (const char *name, bool *b)
 boolean. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint8 (const char *name, uint8_t *u8)
 8-bit integer. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint16 (const char *name, uint16_t *u16)
 16-bit integer. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint32 (const char *name, uint32_t *u32)
 32-bit integer. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint64 (const char *name, uint64_t *u64)
 64-bit integer. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_int64 (const char *name, int64_t *i64)
 64-bit signed integer. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_boolean (const char *name, int *boolean)
 Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO). More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_absolute_time (const char *name, struct GNUNET_TIME_Absolute *at)
 Absolute time. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_absolute_time_nbo (const char *name, struct GNUNET_TIME_AbsoluteNBO *at)
 Absolute time in network byte order. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_relative_time (const char *name, struct GNUNET_TIME_Relative *rt)
 Relative time. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_rsa_public_key (const char *name, struct GNUNET_CRYPTO_RsaPublicKey **pk)
 Specification for parsing an RSA public key. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_rsa_signature (const char *name, struct GNUNET_CRYPTO_RsaSignature **sig)
 Specification for parsing an RSA signature. More...
 
json_t * GNUNET_JSON_from_data (const void *data, size_t size)
 Convert binary data to a JSON string with the base32crockford encoding. More...
 
json_t * GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
 Convert absolute timestamp to a json string. More...
 
json_t * GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp)
 Convert absolute timestamp to a json string. More...
 
json_t * GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
 Convert relative timestamp to a json string. More...
 
json_t * GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
 Convert RSA public key to JSON. More...
 
json_t * GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
 Convert RSA signature to JSON. More...
 
enum GNUNET_JSON_PostResult GNUNET_JSON_post_parser (size_t buffer_max, struct MHD_Connection *connection, void **con_cls, const char *upload_data, size_t *upload_data_size, json_t **json)
 Process a POST request containing a JSON object. More...
 
void GNUNET_JSON_post_parser_cleanup (void *con_cls)
 Function called whenever we are done with a request to clean up our state. More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_JSON_getopt (char shortName, const char *name, const char *argumentHelp, const char *description, json_t **json)
 Allow user to specify a JSON input value. More...
 
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_time_abs (const char *name, struct GNUNET_TIME_Absolute at)
 Generate packer instruction for a JSON field of type absolute time. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_abs_nbo (const char *name, struct GNUNET_TIME_AbsoluteNBO at)
 Generate packer instruction for a JSON field of type absolute time 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 187 of file gnunet_json_lib.h.

◆ GNUNET_JSON_from_data_auto

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

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

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

Definition at line 383 of file gnunet_json_lib.h.

◆ 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 588 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 744 of file gnunet_json_lib.h.

Typedef Documentation

◆ GNUNET_JSON_Parser

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

Function called to parse JSON argument.

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

Definition at line 52 of file gnunet_json_lib.h.

◆ GNUNET_JSON_Cleaner

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

Function called to clean up data from earlier parsing.

Parameters
clsclosure
specour specification entry with data to clean.

Definition at line 64 of file gnunet_json_lib.h.

◆ 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 542 of file gnunet_json_lib.h.

Enumeration Type Documentation

◆ GNUNET_JSON_PostResult

Return codes from GNUNET_JSON_post_parser().

Enumerator
GNUNET_JSON_PR_SUCCESS 

Parsing successful, JSON result is in *json.

GNUNET_JSON_PR_CONTINUE 

Parsing continues, call again soon!

GNUNET_JSON_PR_OUT_OF_MEMORY 

Sorry, memory allocation (malloc()) failed.

GNUNET_JSON_PR_REQUEST_TOO_LARGE 

Request size exceeded buffer_max argument.

GNUNET_JSON_PR_JSON_INVALID 

JSON parsing failed.

This was not a JSON upload.

Definition at line 441 of file gnunet_json_lib.h.

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

Function Documentation

◆ GNUNET_JSON_parse()

enum GNUNET_GenericReturnValue GNUNET_JSON_parse ( const json_t *  root,
struct GNUNET_JSON_Specification spec,
const char **  error_json_name,
unsigned int *  error_line 
)

Navigate and parse data in a JSON tree.

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

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

Definition at line 32 of file json.c.

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

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

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  continue;
52  if ( (NULL == pos) ||
53  (GNUNET_OK !=
54  spec[i].parser (spec[i].cls,
55  pos,
56  &spec[i])) )
57  {
58  if (NULL != error_json_name)
59  *error_json_name = spec[i].field;
60  else
62  "Parsing failed for field `%s:%u`\n",
63  spec[i].field,
64  i);
65  if (NULL != error_line)
66  *error_line = i;
68  return GNUNET_SYSERR;
69  }
70  }
71  return GNUNET_OK; /* all OK! */
72 }
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:86
GNUNET_JSON_Parser parser
Function for how to parse this type of entry.
#define GNUNET_log(kind,...)
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 86 of file json.c.

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

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

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

◆ GNUNET_JSON_spec_end()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_end ( void  )

End of a parser specification.

Definition at line 32 of file json_helper.c.

References GNUNET_JSON_Specification::parser, and ret.

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

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

◆ GNUNET_JSON_spec_mark_optional()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_mark_optional ( struct GNUNET_JSON_Specification  spec)

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

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

Definition at line 76 of file json.c.

References GNUNET_YES, GNUNET_JSON_Specification::is_optional, and ret.

77 {
78  struct GNUNET_JSON_Specification ret = spec;
79 
80  ret.is_optional = GNUNET_YES;
81  return ret;
82 }
int is_optional
Set to GNUNET_YES if this component is optional.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().

◆ GNUNET_JSON_spec_fixed()

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

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

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

Definition at line 85 of file json_helper.c.

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

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

◆ GNUNET_JSON_spec_varsize()

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

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

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

Definition at line 162 of file json_helper.c.

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

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

◆ GNUNET_JSON_spec_string()

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

The expected field stores a string.

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

Definition at line 210 of file json_helper.c.

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

212 {
213  struct GNUNET_JSON_Specification ret = {
214  .parser = &parse_string,
215  .cleaner = NULL,
216  .cls = NULL,
217  .field = name,
218  .ptr = strptr,
219  .ptr_size = 0,
220  .size_ptr = NULL
221  };
222 
223  *strptr = NULL;
224  return ret;
225 }
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:191
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 273 of file json_helper.c.

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

275 {
276  struct GNUNET_JSON_Specification ret = {
277  .parser = &parse_object,
278  .cleaner = &clean_object,
279  .cls = NULL,
280  .field = name,
281  .ptr = jsonp,
282  .ptr_size = 0,
283  .size_ptr = NULL
284  };
285 
286  *jsonp = NULL;
287  return ret;
288 }
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:237
static void clean_object(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing JSON object.
Definition: json_helper.c:259
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 322 of file json_helper.c.

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

324 {
325  struct GNUNET_JSON_Specification ret = {
326  .parser = &parse_bool,
327  .cleaner = NULL,
328  .cls = NULL,
329  .field = name,
330  .ptr = b,
331  .ptr_size = sizeof(bool),
332  .size_ptr = NULL
333  };
334 
335  return ret;
336 }
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:300
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 372 of file json_helper.c.

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

374 {
375  struct GNUNET_JSON_Specification ret = {
376  .parser = &parse_u8,
377  .cleaner = NULL,
378  .cls = NULL,
379  .field = name,
380  .ptr = u8,
381  .ptr_size = sizeof(uint8_t),
382  .size_ptr = NULL
383  };
384 
385  return ret;
386 }
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:348
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 422 of file json_helper.c.

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

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

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

474 {
475  struct GNUNET_JSON_Specification ret = {
476  .parser = &parse_u32,
477  .cleaner = NULL,
478  .cls = NULL,
479  .field = name,
480  .ptr = u32,
481  .ptr_size = sizeof(uint32_t),
482  .size_ptr = NULL
483  };
484 
485  return ret;
486 }
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:448
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 517 of file json_helper.c.

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

519 {
520  struct GNUNET_JSON_Specification ret = {
521  .parser = &parse_u64,
522  .cleaner = NULL,
523  .cls = NULL,
524  .field = name,
525  .ptr = u64,
526  .ptr_size = sizeof(uint64_t),
527  .size_ptr = NULL
528  };
529 
530  return ret;
531 }
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:498
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 562 of file json_helper.c.

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

564 {
565  struct GNUNET_JSON_Specification ret = {
566  .parser = &parse_i64,
567  .cleaner = NULL,
568  .cls = NULL,
569  .field = name,
570  .ptr = i64,
571  .ptr_size = sizeof(int64_t),
572  .size_ptr = NULL
573  };
574 
575  return ret;
576 }
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:543
const char * name
size_t * size_ptr
Where should we store the final size of ptr.
Here is the call graph for this function:

◆ GNUNET_JSON_spec_boolean()

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

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

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

Definition at line 976 of file json_helper.c.

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

978 {
979  struct GNUNET_JSON_Specification ret = {
980  .parser = &parse_boolean,
981  .cleaner = NULL,
982  .cls = NULL,
983  .field = name,
984  .ptr = boolean,
985  .ptr_size = sizeof(int),
986  .size_ptr = NULL
987  };
988 
989  return ret;
990 }
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:959
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 638 of file json_helper.c.

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

640 {
641  struct GNUNET_JSON_Specification ret = {
642  .parser = &parse_abs_time,
643  .cleaner = NULL,
644  .cls = NULL,
645  .field = name,
646  .ptr = at,
647  .ptr_size = sizeof(struct GNUNET_TIME_Absolute),
648  .size_ptr = NULL
649  };
650 
651  return ret;
652 }
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:590
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 686 of file json_helper.c.

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

688 {
689  struct GNUNET_JSON_Specification ret = {
690  .parser = &parse_abs_time_nbo,
691  .cleaner = NULL,
692  .cls = NULL,
693  .field = name,
694  .ptr = at,
695  .ptr_size = sizeof(struct GNUNET_TIME_AbsoluteNBO),
696  .size_ptr = NULL
697  };
698 
699  return ret;
700 }
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:664
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 757 of file json_helper.c.

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

759 {
760  struct GNUNET_JSON_Specification ret = {
761  .parser = &parse_rel_time,
762  .cleaner = NULL,
763  .cls = NULL,
764  .field = name,
765  .ptr = rt,
766  .ptr_size = sizeof(struct GNUNET_TIME_Relative),
767  .size_ptr = NULL
768  };
769 
770  return ret;
771 }
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:712
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 844 of file json_helper.c.

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

846 {
847  struct GNUNET_JSON_Specification ret = {
848  .parser = &parse_rsa_public_key,
849  .cleaner = &clean_rsa_public_key,
850  .cls = NULL,
851  .field = name,
852  .ptr = pk,
853  .ptr_size = 0,
854  .size_ptr = NULL
855  };
856 
857  *pk = NULL;
858  return ret;
859 }
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:783
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:830
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
const char * name
Here is the call graph for this function:

◆ GNUNET_JSON_spec_rsa_signature()

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

Specification for parsing an RSA signature.

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

Definition at line 932 of file json_helper.c.

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

934 {
935  struct GNUNET_JSON_Specification ret = {
936  .parser = &parse_rsa_signature,
937  .cleaner = &clean_rsa_signature,
938  .cls = NULL,
939  .field = name,
940  .ptr = sig,
941  .ptr_size = 0,
942  .size_ptr = NULL
943  };
944 
945  *sig = NULL;
946  return ret;
947 }
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:871
static void clean_rsa_signature(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA signature.
Definition: json_helper.c:918
const char * name
Here is the call graph for this function:

◆ GNUNET_JSON_from_data()

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

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

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

Definition at line 39 of file json_generator.c.

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

Referenced by GNUNET_JSON_from_rsa_public_key(), GNUNET_JSON_from_rsa_signature(), and GNUNET_JSON_pack_data_varsize().

41 {
42  char *buf;
43  json_t *json;
44 
45  if ((size * 8 + 4) / 5 + 1 >=
47  {
48  GNUNET_break (0);
49  return NULL;
50  }
52  size);
53  json = json_string (buf);
54  GNUNET_free (buf);
55  GNUNET_break (NULL != json);
56  return json;
57 }
#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
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:750
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 67 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(), and GNUNET_JSON_pack_time_abs().

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

◆ GNUNET_JSON_from_time_abs_nbo()

json_t* GNUNET_JSON_from_time_abs_nbo ( struct GNUNET_TIME_AbsoluteNBO  stamp)

Convert absolute timestamp to a json string.

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

Definition at line 114 of file json_generator.c.

References GNUNET_JSON_from_time_abs(), and GNUNET_TIME_absolute_ntoh().

115 {
117 }
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:542
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 127 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.

Referenced by GNUNET_JSON_pack_time_rel().

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

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

Referenced by GNUNET_JSON_pack_rsa_public_key().

175 {
176  void *buf;
177  size_t buf_len;
178  json_t *ret;
179 
181  &buf);
182  ret = GNUNET_JSON_from_data (buf,
183  buf_len);
184  GNUNET_free (buf);
185  return ret;
186 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static char buf[2048]
size_t GNUNET_CRYPTO_rsa_public_key_encode(const struct GNUNET_CRYPTO_RsaPublicKey *key, void **buffer)
Encode the public key in a format suitable for storing it into a file.
Definition: crypto_rsa.c:300
#define GNUNET_free(ptr)
Wrapper around free.
json_t * GNUNET_JSON_from_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.
Here is the call graph for this function:
Here is the caller 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 196 of file json_generator.c.

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

Referenced by GNUNET_JSON_pack_rsa_signature().

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

Referenced by create_response().

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  {
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 }
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:
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.

References buffer_deinit(), and GNUNET_free.

Referenced by create_response().

368 {
369  struct Buffer *r = con_cls;
370 
371  if (NULL != r)
372  {
373  buffer_deinit (r);
374  GNUNET_free (r);
375  }
376 }
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:
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 130 of file json.c.

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

135 {
137  .name = name,
138  .argumentHelp = argumentHelp,
139  .description = description,
140  .require_argument = 1,
141  .processor = &set_json,
142  .scls = (void *) json };
143 
144  return clo;
145 }
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:107
const char * name
Here is the call graph for this function:

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

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

31 {
32  json_t *ret;
33 
34  ret = json_object ();
35  GNUNET_assert (NULL != ret);
36  for (unsigned int i = 0;
37  NULL != spec[i].field_name;
38  i++)
39  {
40  if (NULL == spec[i].object)
41  {
42  GNUNET_assert (spec[i].allow_null);
43  }
44  else
45  {
46  GNUNET_assert (0 ==
47  json_object_set_new (ret,
48  spec[i].field_name,
49  spec[i].object));
50  spec[i].object = NULL;
51  }
52  }
53  return ret;
54 }
const char * field_name
Name of the field to pack.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
json_t * object
Object to pack.

◆ 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 58 of file json_pack.c.

References GNUNET_JSON_PackSpec::field_name.

59 {
60  struct GNUNET_JSON_PackSpec ps = {
61  .field_name = NULL
62  };
63 
64  return ps;
65 }
const char * field_name
Name of the field to pack.
Element in the array to give to the packer.

◆ 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 69 of file json_pack.c.

References GNUNET_JSON_PackSpec::allow_null.

70 {
71  in.allow_null = true;
72  return in;
73 }
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 77 of file json_pack.c.

References testconfigure::b, GNUNET_JSON_PackSpec::field_name, and name.

79 {
80  struct GNUNET_JSON_PackSpec ps = {
81  .field_name = name,
82  .object = json_boolean (b)
83  };
84 
85  return ps;
86 }
const char * field_name
Name of the field to pack.
Element in the array to give to the packer.
const char * name

◆ 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 90 of file json_pack.c.

References GNUNET_JSON_PackSpec::field_name, and name.

92 {
93  struct GNUNET_JSON_PackSpec ps = {
94  .field_name = name,
95  .object = json_string (s)
96  };
97 
98  return ps;
99 }
const char * field_name
Name of the field to pack.
Element in the array to give to the packer.
const char * name

◆ 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 103 of file json_pack.c.

References GNUNET_JSON_PackSpec::field_name, GNUNET_assert, and name.

105 {
106  struct GNUNET_JSON_PackSpec ps = {
107  .field_name = name,
108  .object = json_integer ((json_int_t) num)
109  };
110 
111 #if JSON_INTEGER_IS_LONG_LONG
112  GNUNET_assert (num <= LLONG_MAX);
113 #else
114  GNUNET_assert (num <= LONG_MAX);
115 #endif
116  return ps;
117 }
const char * field_name
Name of the field to pack.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Element in the array to give to the packer.
const char * name

◆ 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 121 of file json_pack.c.

References GNUNET_JSON_PackSpec::field_name, GNUNET_assert, and name.

123 {
124  struct GNUNET_JSON_PackSpec ps = {
125  .field_name = name,
126  .object = json_integer ((json_int_t) num)
127  };
128 
129 #if JSON_INTEGER_IS_LONG_LONG
130  GNUNET_assert (num <= LLONG_MAX);
131  GNUNET_assert (num >= LLONG_MIN);
132 #else
133  GNUNET_assert (num <= LONG_MAX);
134  GNUNET_assert (num >= LONG_MIN);
135 #endif
136  return ps;
137 }
const char * field_name
Name of the field to pack.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Element in the array to give to the packer.
const char * name

◆ 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 141 of file json_pack.c.

References GNUNET_JSON_PackSpec::field_name, GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, and name.

Referenced by GNUNET_JSON_pack_time_rel().

143 {
144  struct GNUNET_JSON_PackSpec ps = {
145  .field_name = name,
146  .object = o
147  };
148 
149  if (NULL == o)
150  return ps;
151  if (! json_is_object (o))
152  {
154  "Expected JSON object for field `%s'\n",
155  name);
156  GNUNET_assert (0);
157  }
158  return ps;
159 }
const char * field_name
Name of the field to pack.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Element in the array to give to the packer.
const char * name
#define GNUNET_log(kind,...)
Here is the caller graph for this function:

◆ 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 163 of file json_pack.c.

References GNUNET_JSON_PackSpec::field_name, GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, and name.

165 {
166  struct GNUNET_JSON_PackSpec ps = {
167  .field_name = name,
168  .object = o
169  };
170 
171  if (NULL == o)
172  return ps;
173  (void) json_incref (o);
174  if (! json_is_object (o))
175  {
177  "Expected JSON object for field `%s'\n",
178  name);
179  GNUNET_assert (0);
180  }
181  return ps;
182 }
const char * field_name
Name of the field to pack.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Element in the array to give to the packer.
const char * name
#define GNUNET_log(kind,...)

◆ 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 186 of file json_pack.c.

References GNUNET_JSON_PackSpec::field_name, GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, and name.

188 {
189  struct GNUNET_JSON_PackSpec ps = {
190  .field_name = name,
191  .object = a
192  };
193 
194  if (NULL == a)
195  return ps;
196  if (! json_is_array (a))
197  {
199  "Expected JSON array for field `%s'\n",
200  name);
201  GNUNET_assert (0);
202  }
203  return ps;
204 }
const char * field_name
Name of the field to pack.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Element in the array to give to the packer.
const char * name
#define GNUNET_log(kind,...)

◆ 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 208 of file json_pack.c.

References GNUNET_JSON_PackSpec::field_name, GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, and name.

210 {
211  struct GNUNET_JSON_PackSpec ps = {
212  .field_name = name,
213  .object = a
214  };
215 
216  if (NULL == a)
217  return ps;
218  (void) json_incref (a);
219  if (! json_is_array (a))
220  {
222  "Expected JSON array for field `%s'\n",
223  name);
224  GNUNET_assert (0);
225  }
226  return ps;
227 }
const char * field_name
Name of the field to pack.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Element in the array to give to the packer.
const char * name
#define GNUNET_log(kind,...)

◆ 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 231 of file json_pack.c.

References GNUNET_JSON_PackSpec::field_name, GNUNET_JSON_from_data(), and name.

234 {
235  struct GNUNET_JSON_PackSpec ps = {
236  .field_name = name,
237  .object = (NULL != blob)
238  ? GNUNET_JSON_from_data (blob,
239  blob_size)
240  : NULL
241  };
242 
243  return ps;
244 }
json_t * GNUNET_JSON_from_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.
const char * field_name
Name of the field to pack.
Element in the array to give to the packer.
const char * name
Here is the call graph for this function:

◆ GNUNET_JSON_pack_time_abs()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_abs ( const char *  name,
struct GNUNET_TIME_Absolute  at 
)

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

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

Definition at line 248 of file json_pack.c.

References GNUNET_JSON_PackSpec::field_name, GNUNET_assert, GNUNET_JSON_from_time_abs(), and GNUNET_JSON_PackSpec::object.

Referenced by GNUNET_JSON_pack_time_abs_nbo().

250 {
251  struct GNUNET_JSON_PackSpec ps = {
252  .field_name = name
253  };
254 
255  if (0 != at.abs_value_us)
256  {
258  GNUNET_assert (NULL != ps.object);
259  }
260  else
261  {
262  ps.object = NULL;
263  }
264  return ps;
265 }
const char * field_name
Name of the field to pack.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
uint64_t abs_value_us
The actual value.
json_t * GNUNET_JSON_from_time_abs(struct GNUNET_TIME_Absolute stamp)
Convert absolute timestamp to a json string.
json_t * object
Object to pack.
Element in the array to give to the packer.
const char * name
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_JSON_pack_time_abs_nbo()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_abs_nbo ( const char *  name,
struct GNUNET_TIME_AbsoluteNBO  at 
)

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

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

Definition at line 269 of file json_pack.c.

References GNUNET_JSON_pack_time_abs(), and GNUNET_TIME_absolute_ntoh().

271 {
274 }
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:542
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_abs(const char *name, struct GNUNET_TIME_Absolute at)
Generate packer instruction for a JSON field of type absolute time.
Definition: json_pack.c:248
const char * name
Here is the call graph for this function:

◆ 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 278 of file json_pack.c.

References GNUNET_assert, GNUNET_JSON_from_time_rel(), and GNUNET_JSON_pack_object_steal().

Referenced by GNUNET_JSON_pack_time_rel_nbo().

280 {
281  json_t *json;
282 
283  json = GNUNET_JSON_from_time_rel (rt);
284  GNUNET_assert (NULL != json);
286  json);
287 }
json_t * GNUNET_JSON_from_time_rel(struct GNUNET_TIME_Relative stamp)
Convert relative timestamp to a json string.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
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:141
const char * name
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 291 of file json_pack.c.

References GNUNET_JSON_pack_time_rel(), and GNUNET_TIME_relative_ntoh().

293 {
296 }
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:278
struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh(struct GNUNET_TIME_RelativeNBO a)
Convert relative time from network byte order.
Definition: time.c:454
const char * name
Here is the call graph for this function:

◆ 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 300 of file json_pack.c.

References GNUNET_JSON_PackSpec::field_name, GNUNET_JSON_from_rsa_public_key(), name, and pk.

302 {
303  struct GNUNET_JSON_PackSpec ps = {
304  .field_name = name,
305  .object = GNUNET_JSON_from_rsa_public_key (pk)
306  };
307 
308  return ps;
309 }
const char * field_name
Name of the field to pack.
Element in the array to give to the packer.
const char * name
json_t * GNUNET_JSON_from_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *pk)
Convert RSA public key to JSON.
Here is the call graph for this function:

◆ 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 313 of file json_pack.c.

References GNUNET_JSON_PackSpec::field_name, GNUNET_JSON_from_rsa_signature(), and name.

315 {
316  struct GNUNET_JSON_PackSpec ps = {
317  .field_name = name,
318  .object = GNUNET_JSON_from_rsa_signature (sig)
319  };
320 
321  return ps;
322 }
json_t * GNUNET_JSON_from_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *sig)
Convert RSA signature to JSON.
const char * field_name
Name of the field to pack.
Element in the array to give to the packer.
const char * name
Here is the call graph for this function: