GNUnet  0.19.2
json_gnsrecord.c File Reference
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_json_lib.h"
#include "gnunet_gnsrecord_lib.h"
Include dependency graph for json_gnsrecord.c:

Go to the source code of this file.

Data Structures

struct  GnsRecordInfo
 

Macros

#define GNUNET_JSON_GNSRECORD_VALUE   "value"
 
#define GNUNET_JSON_GNSRECORD_RECORD_DATA   "data"
 
#define GNUNET_JSON_GNSRECORD_TYPE   "record_type"
 
#define GNUNET_JSON_GNSRECORD_RELATIVE_EXPIRATION_TIME   "relative_expiration"
 
#define GNUNET_JSON_GNSRECORD_ABSOLUTE_EXPIRATION_TIME   "absolute_expiration"
 
#define GNUNET_JSON_GNSRECORD_FLAG_PRIVATE   "is_private"
 
#define GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL   "is_supplemental"
 
#define GNUNET_JSON_GNSRECORD_FLAG_RELATIVE   "is_relative_expiration"
 
#define GNUNET_JSON_GNSRECORD_FLAG_SHADOW   "is_shadow"
 
#define GNUNET_JSON_GNSRECORD_RECORD_NAME   "record_name"
 

Functions

static void cleanup_recordinfo (struct GnsRecordInfo *gnsrecord_info)
 
static int parse_record (json_t *data, struct GNUNET_GNSRECORD_Data *rd)
 Parse given JSON object to gns record. More...
 
static int parse_record_data (struct GnsRecordInfo *gnsrecord_info, json_t *data)
 Parse given JSON object to gns record. More...
 
static int parse_gnsrecordobject (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
 
static void clean_gnsrecordobject (void *cls, struct GNUNET_JSON_Specification *spec)
 Cleanup data left from parsing the record. More...
 
struct GNUNET_JSON_Specification GNUNET_GNSRECORD_JSON_spec_gnsrecord (struct GNUNET_GNSRECORD_Data **rd, unsigned int *rd_count, char **name)
 JSON Specification for GNS Records. More...
 
json_t * GNUNET_GNSRECORD_JSON_from_gnsrecord (const char *rname, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count)
 Convert GNS record to JSON. More...
 

Macro Definition Documentation

◆ GNUNET_JSON_GNSRECORD_VALUE

#define GNUNET_JSON_GNSRECORD_VALUE   "value"

Definition at line 31 of file json_gnsrecord.c.

◆ GNUNET_JSON_GNSRECORD_RECORD_DATA

#define GNUNET_JSON_GNSRECORD_RECORD_DATA   "data"

Definition at line 32 of file json_gnsrecord.c.

◆ GNUNET_JSON_GNSRECORD_TYPE

#define GNUNET_JSON_GNSRECORD_TYPE   "record_type"

Definition at line 33 of file json_gnsrecord.c.

◆ GNUNET_JSON_GNSRECORD_RELATIVE_EXPIRATION_TIME

#define GNUNET_JSON_GNSRECORD_RELATIVE_EXPIRATION_TIME   "relative_expiration"

Definition at line 34 of file json_gnsrecord.c.

◆ GNUNET_JSON_GNSRECORD_ABSOLUTE_EXPIRATION_TIME

#define GNUNET_JSON_GNSRECORD_ABSOLUTE_EXPIRATION_TIME   "absolute_expiration"

Definition at line 35 of file json_gnsrecord.c.

◆ GNUNET_JSON_GNSRECORD_FLAG_PRIVATE

#define GNUNET_JSON_GNSRECORD_FLAG_PRIVATE   "is_private"

Definition at line 36 of file json_gnsrecord.c.

◆ GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL

#define GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL   "is_supplemental"

Definition at line 37 of file json_gnsrecord.c.

◆ GNUNET_JSON_GNSRECORD_FLAG_RELATIVE

#define GNUNET_JSON_GNSRECORD_FLAG_RELATIVE   "is_relative_expiration"

Definition at line 38 of file json_gnsrecord.c.

◆ GNUNET_JSON_GNSRECORD_FLAG_SHADOW

#define GNUNET_JSON_GNSRECORD_FLAG_SHADOW   "is_shadow"

Definition at line 39 of file json_gnsrecord.c.

◆ GNUNET_JSON_GNSRECORD_RECORD_NAME

#define GNUNET_JSON_GNSRECORD_RECORD_NAME   "record_name"

Definition at line 40 of file json_gnsrecord.c.

Function Documentation

◆ cleanup_recordinfo()

static void cleanup_recordinfo ( struct GnsRecordInfo gnsrecord_info)
static

Definition at line 53 of file json_gnsrecord.c.

54 {
55  char *tmp;
56 
57  if (NULL != *(gnsrecord_info->rd))
58  {
59  for (int i = 0; i < *(gnsrecord_info->rd_count); i++)
60  {
61  tmp = (char*) (*(gnsrecord_info->rd))[i].data;
62  if (NULL != tmp)
63  GNUNET_free (tmp);
64  }
65  GNUNET_free (*(gnsrecord_info->rd));
66  *(gnsrecord_info->rd) = NULL;
67  }
68  if (NULL != *(gnsrecord_info->name))
69  GNUNET_free (*(gnsrecord_info->name));
70  *(gnsrecord_info->name) = NULL;
71 }
uint32_t data
The data value.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_GNSRECORD_Data ** rd
unsigned int * rd_count

References data, GNUNET_free, GnsRecordInfo::name, GnsRecordInfo::rd, and GnsRecordInfo::rd_count.

Referenced by parse_gnsrecordobject().

Here is the caller graph for this function:

◆ parse_record()

static int parse_record ( json_t *  data,
struct GNUNET_GNSRECORD_Data rd 
)
static

Parse given JSON object to gns record.

Parameters
clsclosure, NULL
rootthe json object representing data
specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 83 of file json_gnsrecord.c.

84 {
85  struct GNUNET_TIME_Absolute abs_exp;
86  struct GNUNET_TIME_Relative rel_exp;
87  const char *value;
88  const char *record_type;
89  int private;
90  int supplemental;
91  int is_rel_exp;
92  int shadow;
93  int unpack_state = 0;
94  json_error_t err;
95 
96  // interpret single gns record
97  unpack_state = json_unpack_ex (data,
98  &err,
99  0,
100  "{s:s, s:s, s:I, s:b, s:b, s:b, s:b}",
102  &value,
104  &record_type,
106  &rel_exp.rel_value_us,
108  &private,
110  &supplemental,
112  &is_rel_exp,
114  &shadow);
115  if (0 != unpack_state)
116  {
118  "Error gnsdata object has a wrong format: `%s'!\n",
119  err.text);
120  unpack_state = json_unpack_ex (data,
121  &err,
122  0,
123  "{s:s, s:s, s:I, s:b, s:b, s:b, s:b}",
125  &value,
127  &record_type,
129  &abs_exp.abs_value_us,
131  &private,
133  &supplemental,
135  &is_rel_exp,
137  &shadow);
138  if ((0 != unpack_state) || (is_rel_exp))
139  {
141  "Error gnsdata object has a wrong format: `%s'!\n",
142  (is_rel_exp) ? "No relative expiration given" : err.text);
143  return GNUNET_SYSERR;
144  }
145  rd->expiration_time = abs_exp.abs_value_us;
146  } else {
147  rd->expiration_time = rel_exp.rel_value_us;
148  }
150  if (UINT32_MAX == rd->record_type)
151  {
152  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unsupported type\n");
153  return GNUNET_SYSERR;
154  }
156  value,
157  (void **) &rd->data,
158  &rd->data_size))
159  {
160  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Value invalid for record type\n");
161  return GNUNET_SYSERR;
162  }
163 
164  if (is_rel_exp)
166  if (1 == private)
168  if (1 == supplemental)
170  if (1 == shadow)
172  return GNUNET_OK;
173 }
static struct GNUNET_GNSRECORD_Data rd[50]
The record data under a single label.
static char * value
Value of the record to add/remove.
@ GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION
This expiration time of the record is a relative time (not an absolute time).
@ GNUNET_GNSRECORD_RF_SUPPLEMENTAL
This is a supplemental record.
@ GNUNET_GNSRECORD_RF_SHADOW
This record should not be used unless all (other) records in the set with an absolute expiration time...
@ GNUNET_GNSRECORD_RF_PRIVATE
This is a private record of this peer and it should thus not be published.
uint32_t GNUNET_GNSRECORD_typename_to_number(const char *dns_typename)
Convert a type name (e.g.
Definition: gnsrecord.c:200
int GNUNET_GNSRECORD_string_to_value(uint32_t type, const char *s, void **data, size_t *data_size)
Convert human-readable version of the value s of a record of type type to the respective binary repre...
Definition: gnsrecord.c:177
#define GNUNET_log(kind,...)
@ GNUNET_OK
@ GNUNET_SYSERR
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_JSON_GNSRECORD_FLAG_SHADOW
#define GNUNET_JSON_GNSRECORD_VALUE
#define GNUNET_JSON_GNSRECORD_FLAG_PRIVATE
#define GNUNET_JSON_GNSRECORD_ABSOLUTE_EXPIRATION_TIME
#define GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL
#define GNUNET_JSON_GNSRECORD_RELATIVE_EXPIRATION_TIME
#define GNUNET_JSON_GNSRECORD_FLAG_RELATIVE
#define GNUNET_JSON_GNSRECORD_TYPE
uint32_t record_type
Type of the GNS/DNS record.
const void * data
Binary value stored in the DNS record.
size_t data_size
Number of bytes in data.
enum GNUNET_GNSRECORD_Flags flags
Flags for the record.
uint64_t expiration_time
Expiration time for the DNS record.
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.

References GNUNET_TIME_Absolute::abs_value_us, GNUNET_GNSRECORD_Data::data, data, GNUNET_GNSRECORD_Data::data_size, GNUNET_GNSRECORD_Data::expiration_time, GNUNET_GNSRECORD_Data::flags, GNUNET_ERROR_TYPE_ERROR, GNUNET_GNSRECORD_RF_PRIVATE, GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION, GNUNET_GNSRECORD_RF_SHADOW, GNUNET_GNSRECORD_RF_SUPPLEMENTAL, GNUNET_GNSRECORD_string_to_value(), GNUNET_GNSRECORD_typename_to_number(), GNUNET_JSON_GNSRECORD_ABSOLUTE_EXPIRATION_TIME, GNUNET_JSON_GNSRECORD_FLAG_PRIVATE, GNUNET_JSON_GNSRECORD_FLAG_RELATIVE, GNUNET_JSON_GNSRECORD_FLAG_SHADOW, GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL, GNUNET_JSON_GNSRECORD_RELATIVE_EXPIRATION_TIME, GNUNET_JSON_GNSRECORD_TYPE, GNUNET_JSON_GNSRECORD_VALUE, GNUNET_log, GNUNET_OK, GNUNET_SYSERR, rd, GNUNET_GNSRECORD_Data::record_type, GNUNET_TIME_Relative::rel_value_us, and value.

Referenced by parse_record_data().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_record_data()

static int parse_record_data ( struct GnsRecordInfo gnsrecord_info,
json_t *  data 
)
static

Parse given JSON object to gns record.

Parameters
clsclosure, NULL
rootthe json object representing data
specwhere to write the data
Returns
GNUNET_OK upon successful parsing; GNUNET_SYSERR upon error

Definition at line 185 of file json_gnsrecord.c.

186 {
187  GNUNET_assert (NULL != data);
188  if (! json_is_array (data))
189  {
191  "Error gns record data JSON is not an array!\n");
192  return GNUNET_SYSERR;
193  }
194  *(gnsrecord_info->rd_count) = json_array_size (data);
195  *(gnsrecord_info->rd) = GNUNET_malloc (sizeof(struct GNUNET_GNSRECORD_Data)
196  * json_array_size (data));
197  size_t index;
198  json_t *value;
199  json_array_foreach (data, index, value)
200  {
201  if (GNUNET_OK != parse_record (value, &(*(gnsrecord_info->rd))[index]))
202  return GNUNET_SYSERR;
203  }
204  return GNUNET_OK;
205 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_malloc(size)
Wrapper around malloc.
static int parse_record(json_t *data, struct GNUNET_GNSRECORD_Data *rd)
Parse given JSON object to gns record.

References data, GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_malloc, GNUNET_OK, GNUNET_SYSERR, parse_record(), GnsRecordInfo::rd, GnsRecordInfo::rd_count, and value.

Referenced by parse_gnsrecordobject().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_gnsrecordobject()

static int parse_gnsrecordobject ( void *  cls,
json_t *  root,
struct GNUNET_JSON_Specification spec 
)
static

Definition at line 209 of file json_gnsrecord.c.

212 {
213  struct GnsRecordInfo *gnsrecord_info;
214  int unpack_state = 0;
215  const char *name;
216  json_t *data;
217 
218  GNUNET_assert (NULL != root);
219  if (! json_is_object (root))
220  {
222  "Error record JSON is not an object!\n");
223  return GNUNET_SYSERR;
224  }
225  // interpret single gns record
226  unpack_state = json_unpack (root,
227  "{s:s, s:o!}",
229  &name,
231  &data);
232  if (0 != unpack_state)
233  {
235  "Error namestore records object has a wrong format!\n");
236  return GNUNET_SYSERR;
237  }
238  gnsrecord_info = (struct GnsRecordInfo *) spec->ptr;
239  *(gnsrecord_info->name) = GNUNET_strdup (name);
240  if (GNUNET_OK != parse_record_data (gnsrecord_info, data))
241  {
242  cleanup_recordinfo (gnsrecord_info);
243  return GNUNET_SYSERR;
244  }
245  return GNUNET_OK;
246 }
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
static int parse_record_data(struct GnsRecordInfo *gnsrecord_info, json_t *data)
Parse given JSON object to gns record.
#define GNUNET_JSON_GNSRECORD_RECORD_NAME
static void cleanup_recordinfo(struct GnsRecordInfo *gnsrecord_info)
#define GNUNET_JSON_GNSRECORD_RECORD_DATA
const char * name
void * ptr
Pointer, details specific to the parser.

References cleanup_recordinfo(), data, GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_JSON_GNSRECORD_RECORD_DATA, GNUNET_JSON_GNSRECORD_RECORD_NAME, GNUNET_log, GNUNET_OK, GNUNET_strdup, GNUNET_SYSERR, name, GnsRecordInfo::name, parse_record_data(), and GNUNET_JSON_Specification::ptr.

Here is the call graph for this function:

◆ clean_gnsrecordobject()

static void clean_gnsrecordobject ( void *  cls,
struct GNUNET_JSON_Specification spec 
)
static

Cleanup data left from parsing the record.

Parameters
clsclosure, NULL
[out]specwhere to free the data

Definition at line 256 of file json_gnsrecord.c.

257 {
258  struct GnsRecordInfo *gnsrecord_info = (struct GnsRecordInfo *) spec->ptr;
259 
260  GNUNET_free (gnsrecord_info);
261 }

References GNUNET_free, and GNUNET_JSON_Specification::ptr.