GNUnet  0.10.x
json_generator.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2014, 2015, 2016 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_json_lib.h"
28 
29 
38 json_t *
40  size_t size)
41 {
42  char *buf;
43  json_t *json;
44 
45  buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
46  json = json_string (buf);
47  GNUNET_free (buf);
48  return json;
49 }
50 
51 
58 json_t *
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 * 1000LL)));
72  GNUNET_assert (ret > 0);
73  j = json_string (mystr);
74  GNUNET_free (mystr);
75  return j;
76 }
77 
78 
85 json_t *
87 {
89 }
90 
91 
98 json_t *
100 {
101  json_t *j;
102  char *mystr;
103  int ret;
104 
106  GNUNET_TIME_round_rel (&stamp));
107  if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
108  return json_string ("/forever/");
109  ret = GNUNET_asprintf (&mystr,
110  "/Delay(%llu)/",
111  (unsigned long long) (stamp.rel_value_us / (1000LL * 1000LL)));
112  GNUNET_assert (ret > 0);
113  j = json_string (mystr);
114  GNUNET_free (mystr);
115  return j;
116 }
117 
118 
125 json_t *
127 {
128  char *buf;
129  size_t buf_len;
130  json_t *ret;
131 
133  &buf);
134  ret = GNUNET_JSON_from_data (buf,
135  buf_len);
136  GNUNET_free (buf);
137  return ret;
138 }
139 
140 
147 json_t *
149 {
150  char *buf;
151  size_t buf_len;
152  json_t *ret;
153 
154  buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
155  &buf);
156  ret = GNUNET_JSON_from_data (buf,
157  buf_len);
158  GNUNET_free (buf);
159  return ret;
160 }
161 
169 json_t *
170 GNUNET_JSON_from_gnsrecord (const char* rname,
171  const struct GNUNET_GNSRECORD_Data *rd,
172  unsigned int rd_count)
173 {
174  struct GNUNET_TIME_Absolute expiration_time;
175  const char *expiration_time_str;
176  const char *record_type_str;
177  char *value_str;
178  json_t *data;
179  json_t *record;
180  json_t *records;
181 
182  data = json_object ();
183  json_object_set_new (data,
184  "record_name",
185  json_string (rname));
186  records = json_array ();
187  for (int i = 0; i < rd_count; i++)
188  {
189  value_str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
190  rd[i].data,
191  rd[i].data_size);
192  expiration_time = GNUNET_GNSRECORD_record_get_expiration_time(1, &rd[i]);
193  expiration_time_str = GNUNET_STRINGS_absolute_time_to_string (expiration_time);
194  record_type_str = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
196  "Packing %s %s %s %d\n",
197  value_str, record_type_str, expiration_time_str, rd[i].flags);
198  record = json_pack ("{s:s,s:s,s:s,s:i}",
199  "value",
200  value_str,
201  "record_type",
202  record_type_str,
203  "expiration_time",
204  expiration_time_str,
205  "flag",
206  rd[i].flags);
207  GNUNET_assert (NULL != record);
208  GNUNET_free (value_str);
209  json_array_append_new (records, record);
210  }
211  json_object_set_new (data, "data", records);
212  return data;
213 }
214 
215 
216 /* End of json/json_generator.c */
static struct GNUNET_CRYPTO_EddsaPrivateKey * pk
Private key of this peer.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:670
uint64_t rel_value_us
The actual value.
an RSA signature
Definition: crypto_rsa.c:63
functions to parse JSON objects into GNUnet objects
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:318
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:966
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static int ret
Final status code.
Definition: gnunet-arm.c:89
json_t * GNUNET_JSON_from_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *sig)
Convert RSA signature to JSON.
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
uint64_t abs_value_us
The actual value.
The public information of an RSA key pair.
Definition: crypto_rsa.c:51
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
static unsigned int records
Number of records we found.
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
static char buf[2048]
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
json_t * GNUNET_JSON_from_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *pk)
Convert RSA public key to JSON.
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.
static unsigned int size
Size of the "table".
Definition: peer.c:67
json_t * GNUNET_JSON_from_time_rel(struct GNUNET_TIME_Relative stamp)
Convert relative timestamp to a json string.
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
json_t * GNUNET_JSON_from_time_abs(struct GNUNET_TIME_Absolute stamp)
Convert absolute timestamp to a json string.
json_t * GNUNET_JSON_from_time_abs_nbo(struct GNUNET_TIME_AbsoluteNBO stamp)
Convert absolute timestamp to a json string.
#define GNUNET_log(kind,...)
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
json_t * GNUNET_JSON_from_gnsrecord(const char *rname, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count)
Convert GNS record to JSON.
Time for absolute times used by GNUnet, in microseconds.
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:988
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:792
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
static size_t data_size
Number of bytes in data.
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.
json_t * GNUNET_JSON_from_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.
Time for relative time used by GNUnet, in microseconds.