GNUnet  0.11.x
Functions
json_generator.c File Reference

helper functions for generating JSON from GNUnet data structures More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_json_lib.h"
Include dependency graph for json_generator.c:

Go to the source code of this file.

Functions

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

Detailed Description

helper functions for generating JSON from GNUnet data structures

Author
Sree Harsha Totakura sreeh.nosp@m.arsh.nosp@m.a@tot.nosp@m.akur.nosp@m.a.in

Definition in file json_generator.c.

Function Documentation

◆ 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_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  return json;
49 }
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:920
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 59 of file json_generator.c.

References GNUNET_TIME_Absolute::abs_value_us, GNUNET_asprintf(), GNUNET_assert, GNUNET_free, GNUNET_OK, GNUNET_TIME_round_abs(), GNUNET_TIME_UNIT_FOREVER_ABS, and ret.

Referenced by GNUNET_JSON_from_time_abs_nbo().

60 {
61  json_t *j;
62  char *mystr;
63  int ret;
64 
66  GNUNET_TIME_round_abs (&stamp));
67  if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
68  return json_string ("/never/");
69  ret = GNUNET_asprintf (&mystr,
70  "/Date(%llu)/",
71  (unsigned long long) (stamp.abs_value_us / (1000LL
72  * 1000LL)));
73  GNUNET_assert (ret > 0);
74  j = json_string (mystr);
75  GNUNET_free (mystr);
76  return j;
77 }
#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
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
uint64_t abs_value_us
The actual value.
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
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
#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_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 87 of file json_generator.c.

References GNUNET_JSON_from_time_abs(), and GNUNET_TIME_absolute_ntoh().

88 {
90 }
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 100 of file json_generator.c.

References GNUNET_asprintf(), GNUNET_assert, GNUNET_free, GNUNET_OK, GNUNET_TIME_round_rel(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TIME_Relative::rel_value_us, and ret.

101 {
102  json_t *j;
103  char *mystr;
104  int ret;
105 
107  GNUNET_TIME_round_rel (&stamp));
108  if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
109  return json_string ("/forever/");
110  ret = GNUNET_asprintf (&mystr,
111  "/Delay(%llu)/",
112  (unsigned long long) (stamp.rel_value_us / (1000LL
113  * 1000LL)));
114  GNUNET_assert (ret > 0);
115  j = json_string (mystr);
116  GNUNET_free (mystr);
117  return j;
118 }
uint64_t rel_value_us
The actual value.
#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
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
#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
#define GNUNET_free(ptr)
Wrapper around free.
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 128 of file json_generator.c.

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

129 {
130  char *buf;
131  size_t buf_len;
132  json_t *ret;
133 
135  &buf);
136  ret = GNUNET_JSON_from_data (buf,
137  buf_len);
138  GNUNET_free (buf);
139  return ret;
140 }
size_t GNUNET_CRYPTO_rsa_public_key_encode(const struct GNUNET_CRYPTO_RsaPublicKey *key, char **buffer)
Encode the public key in a format suitable for storing it into a file.
Definition: crypto_rsa.c:321
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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_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 150 of file json_generator.c.

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

151 {
152  char *buf;
153  size_t buf_len;
154  json_t *ret;
155 
156  buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
157  &buf);
158  ret = GNUNET_JSON_from_data (buf,
159  buf_len);
160  GNUNET_free (buf);
161  return ret;
162 }
size_t GNUNET_CRYPTO_rsa_signature_encode(const struct GNUNET_CRYPTO_RsaSignature *sig, char **buffer)
Encode the given signature in a format suitable for storing it into a file.
Definition: crypto_rsa.c:973
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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.

Convert Gns record to JSON.

Parameters
rnamename of record
rdrecord data
Returns
corresponding JSON encoding

Definition at line 173 of file json_generator.c.

References data, data_size, GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_GNSRECORD_number_to_typename(), GNUNET_GNSRECORD_record_get_expiration_time(), GNUNET_GNSRECORD_value_to_string(), GNUNET_log, GNUNET_STRINGS_absolute_time_to_string(), record(), and records.

Referenced by handle_gns_response(), and namestore_list_iteration().

176 {
177  struct GNUNET_TIME_Absolute expiration_time;
178  const char *expiration_time_str;
179  const char *record_type_str;
180  char *value_str;
181  json_t *data;
182  json_t *record;
183  json_t *records;
184 
185  data = json_object ();
186  json_object_set_new (data,
187  "record_name",
188  json_string (rname));
189  records = json_array ();
190  for (int i = 0; i < rd_count; i++)
191  {
192  value_str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
193  rd[i].data,
194  rd[i].data_size);
195  expiration_time = GNUNET_GNSRECORD_record_get_expiration_time (1, &rd[i]);
196  expiration_time_str = GNUNET_STRINGS_absolute_time_to_string (
197  expiration_time);
198  record_type_str = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
200  "Packing %s %s %s %d\n",
201  value_str, record_type_str, expiration_time_str, rd[i].flags);
202  record = json_pack ("{s:s,s:s,s:s,s:i}",
203  "value",
204  value_str,
205  "record_type",
206  record_type_str,
207  "expiration_time",
208  expiration_time_str,
209  "flag",
210  rd[i].flags);
211  GNUNET_assert (NULL != record);
212  GNUNET_free (value_str);
213  json_array_append_new (records, record);
214  }
215  json_object_set_new (data, "data", records);
216  return data;
217 }
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static unsigned int records
Number of records we found.
struct GNUNET_TIME_Absolute GNUNET_GNSRECORD_record_get_expiration_time(unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Returns the expiration time of the given block of records.
#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.
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:742
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:143
const char * GNUNET_GNSRECORD_number_to_typename(uint32_t type)
Convert a type number (i.e.
Definition: gnsrecord.c:231
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function: