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_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:935
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.

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  } else {
258  abs_exp.abs_value_us = rd[i].expiration_time;
259  expiration_time_str = GNUNET_STRINGS_absolute_time_to_string (abs_exp);
260  }
261  record_type_str = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
263  "Packing %s %s %s %d\n",
264  value_str, record_type_str, expiration_time_str, rd[i].flags);
265  record = json_pack ("{s:s,s:s,s:s,s:b,s:b,s:b,s:b}",
266  "value",
267  value_str,
268  "record_type",
269  record_type_str,
270  "expiration_time",
271  expiration_time_str,
272  "private",
273  rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE,
274  "relative_expiration",
276  "supplemental",
277  rd[i].flags & GNUNET_GNSRECORD_RF_SUPPLEMENTAL,
278  "shadow",
279  rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD);
280  GNUNET_free (value_str);
281  if (NULL == record)
282  {
283  GNUNET_break (0);
284  json_decref (records);
285  json_decref (data);
286  return NULL;
287  }
288  if (0 !=
289  json_array_append_new (records,
290  record))
291  {
292  GNUNET_break (0);
293  json_decref (records);
294  json_decref (data);
295  return NULL;
296  }
297  }
298  if (0 !=
299  json_object_set_new (data,
300  "data",
301  records))
302  {
303  GNUNET_break (0);
304  json_decref (data);
305  return NULL;
306  }
307  return data;
308 }
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:701
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:756
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.
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: