GNUnet  0.11.x
Data Structures | Macros | Functions
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_EXPIRATION_TIME   "expiration_time"
 
#define GNUNET_JSON_GNSRECORD_FLAG_PRIVATE   "private"
 
#define GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL   "supplemental"
 
#define GNUNET_JSON_GNSRECORD_FLAG_RELATIVE   "relative_expiration"
 
#define GNUNET_JSON_GNSRECORD_FLAG_SHADOW   "shadow"
 
#define GNUNET_JSON_GNSRECORD_RECORD_NAME   "record_name"
 
#define GNUNET_JSON_GNSRECORD_NEVER   "never"
 

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.

Referenced by parse_record().

◆ GNUNET_JSON_GNSRECORD_RECORD_DATA

#define GNUNET_JSON_GNSRECORD_RECORD_DATA   "data"

Definition at line 32 of file json_gnsrecord.c.

Referenced by parse_gnsrecordobject().

◆ GNUNET_JSON_GNSRECORD_TYPE

#define GNUNET_JSON_GNSRECORD_TYPE   "record_type"

Definition at line 33 of file json_gnsrecord.c.

Referenced by parse_record().

◆ GNUNET_JSON_GNSRECORD_EXPIRATION_TIME

#define GNUNET_JSON_GNSRECORD_EXPIRATION_TIME   "expiration_time"

Definition at line 34 of file json_gnsrecord.c.

Referenced by parse_record().

◆ GNUNET_JSON_GNSRECORD_FLAG_PRIVATE

#define GNUNET_JSON_GNSRECORD_FLAG_PRIVATE   "private"

Definition at line 35 of file json_gnsrecord.c.

Referenced by parse_record().

◆ GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL

#define GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL   "supplemental"

Definition at line 36 of file json_gnsrecord.c.

Referenced by parse_record().

◆ GNUNET_JSON_GNSRECORD_FLAG_RELATIVE

#define GNUNET_JSON_GNSRECORD_FLAG_RELATIVE   "relative_expiration"

Definition at line 37 of file json_gnsrecord.c.

Referenced by parse_record().

◆ GNUNET_JSON_GNSRECORD_FLAG_SHADOW

#define GNUNET_JSON_GNSRECORD_FLAG_SHADOW   "shadow"

Definition at line 38 of file json_gnsrecord.c.

Referenced by parse_record().

◆ GNUNET_JSON_GNSRECORD_RECORD_NAME

#define GNUNET_JSON_GNSRECORD_RECORD_NAME   "record_name"

Definition at line 39 of file json_gnsrecord.c.

Referenced by parse_gnsrecordobject().

◆ GNUNET_JSON_GNSRECORD_NEVER

#define GNUNET_JSON_GNSRECORD_NEVER   "never"

Definition at line 40 of file json_gnsrecord.c.

Referenced by parse_record().

Function Documentation

◆ cleanup_recordinfo()

static void cleanup_recordinfo ( struct GnsRecordInfo gnsrecord_info)
static

Definition at line 53 of file json_gnsrecord.c.

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

Referenced by parse_gnsrecordobject().

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 }
unsigned int * rd_count
struct GNUNET_GNSRECORD_Data ** rd
uint32_t data
The data value.
#define GNUNET_free(ptr)
Wrapper around free.
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.

References GNUNET_TIME_Absolute::abs_value_us, GNUNET_GNSRECORD_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_RECORD, GNUNET_GNSRECORD_RF_SUPPLEMENTAL, GNUNET_GNSRECORD_string_to_value(), GNUNET_GNSRECORD_typename_to_number(), GNUNET_JSON_GNSRECORD_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_NEVER, GNUNET_JSON_GNSRECORD_TYPE, GNUNET_JSON_GNSRECORD_VALUE, GNUNET_log, GNUNET_OK, GNUNET_STRINGS_fancy_time_to_absolute(), GNUNET_STRINGS_fancy_time_to_relative(), GNUNET_SYSERR, GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_GNSRECORD_Data::record_type, GNUNET_TIME_Relative::rel_value_us, and value.

Referenced by parse_record_data().

84 {
85  struct GNUNET_TIME_Absolute abs_expiration_time;
86  struct GNUNET_TIME_Relative rel_expiration_time;
87  const char *value;
88  const char *record_type;
89  const char *expiration_time;
90  int private;
91  int supplemental;
92  int rel_exp;
93  int shadow;
94  int unpack_state = 0;
95 
96  // interpret single gns record
97  unpack_state = json_unpack (data,
98  "{s:s, s:s, s:s, s:b, s:b, s:b, s:b}",
100  &value,
102  &record_type,
104  &expiration_time,
106  &private,
108  &supplemental,
110  &rel_exp,
112  &shadow);
113  if (0 != unpack_state)
114  {
116  "Error gnsdata object has a wrong format!\n");
117  return GNUNET_SYSERR;
118  }
120  if (UINT32_MAX == rd->record_type)
121  {
122  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unsupported type\n");
123  return GNUNET_SYSERR;
124  }
126  value,
127  (void **) &rd->data,
128  &rd->data_size))
129  {
130  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Value invalid for record type\n");
131  return GNUNET_SYSERR;
132  }
133 
134  if (0 == strcmp (expiration_time, GNUNET_JSON_GNSRECORD_NEVER))
135  {
137  }
138  else if ((1 != rel_exp) &&
139  (GNUNET_OK ==
140  GNUNET_STRINGS_fancy_time_to_absolute (expiration_time,
141  &abs_expiration_time)))
142  {
143  rd->expiration_time = abs_expiration_time.abs_value_us;
144  }
145  else if (GNUNET_OK ==
146  GNUNET_STRINGS_fancy_time_to_relative (expiration_time,
147  &rel_expiration_time))
148  {
150  rd->expiration_time = rel_expiration_time.rel_value_us;
151  }
152  else
153  {
154  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expiration time invalid\n");
155  return GNUNET_SYSERR;
156  }
157  if (1 == private)
159  if (1 == supplemental)
161  if (1 == shadow)
163  return GNUNET_OK;
164 }
#define GNUNET_JSON_GNSRECORD_VALUE
This record should not be used unless all (other) records with an absolute expiration time have expir...
#define GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL
uint32_t GNUNET_GNSRECORD_typename_to_number(const char *dns_typename)
Convert a type name (i.e.
Definition: gnsrecord.c:220
#define GNUNET_JSON_GNSRECORD_EXPIRATION_TIME
int GNUNET_STRINGS_fancy_time_to_relative(const char *fancy_time, struct GNUNET_TIME_Relative *rtime)
Convert a given fancy human-readable time to our internal representation.
Definition: strings.c:356
int GNUNET_STRINGS_fancy_time_to_absolute(const char *fancy_time, struct GNUNET_TIME_Absolute *atime)
Convert a given fancy human-readable time to our internal representation.
Definition: strings.c:407
This is a supplemental record.
size_t data_size
Number of bytes in data.
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
#define GNUNET_JSON_GNSRECORD_FLAG_PRIVATE
const void * data
Binary value stored in the DNS record.
static char * value
Value of the record to add/remove.
uint64_t expiration_time
Expiration time for the DNS record.
This is a private record of this peer and it should thus not be handed out to other peers...
int GNUNET_GNSRECORD_string_to_value(uint32_t type, const char *s, void **data, size_t *data_size)
Convert human-readable version of a &#39;value&#39; of a record to the binary representation.
Definition: gnsrecord.c:191
#define GNUNET_JSON_GNSRECORD_TYPE
uint32_t record_type
Type of the GNS/DNS record.
#define GNUNET_log(kind,...)
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_JSON_GNSRECORD_NEVER
enum GNUNET_GNSRECORD_Flags flags
Flags for the record.
This expiration time of the record is a relative time (not an absolute time).
uint32_t data
The data value.
#define GNUNET_JSON_GNSRECORD_FLAG_RELATIVE
Time for relative time used by GNUnet, in microseconds.
#define GNUNET_JSON_GNSRECORD_FLAG_SHADOW
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 176 of file json_gnsrecord.c.

References 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().

177 {
178  GNUNET_assert (NULL != data);
179  if (! json_is_array (data))
180  {
182  "Error gns record data JSON is not an array!\n");
183  return GNUNET_SYSERR;
184  }
185  *(gnsrecord_info->rd_count) = json_array_size (data);
186  *(gnsrecord_info->rd) = GNUNET_malloc (sizeof(struct GNUNET_GNSRECORD_Data)
187  * json_array_size (data));
188  size_t index;
189  json_t *value;
190  json_array_foreach (data, index, value)
191  {
192  if (GNUNET_OK != parse_record (value, &(*(gnsrecord_info->rd))[index]))
193  return GNUNET_SYSERR;
194  }
195  return GNUNET_OK;
196 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static char * value
Value of the record to add/remove.
unsigned int * rd_count
static int parse_record(json_t *data, struct GNUNET_GNSRECORD_Data *rd)
Parse given JSON object to gns record.
#define GNUNET_log(kind,...)
struct GNUNET_GNSRECORD_Data ** rd
uint32_t data
The data value.
#define GNUNET_malloc(size)
Wrapper around malloc.
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 200 of file json_gnsrecord.c.

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, GnsRecordInfo::name, parse_record_data(), and GNUNET_JSON_Specification::ptr.

Referenced by GNUNET_GNSRECORD_JSON_spec_gnsrecord().

203 {
204  struct GnsRecordInfo *gnsrecord_info;
205  int unpack_state = 0;
206  const char *name;
207  json_t *data;
208 
209  GNUNET_assert (NULL != root);
210  if (! json_is_object (root))
211  {
213  "Error record JSON is not an object!\n");
214  return GNUNET_SYSERR;
215  }
216  // interpret single gns record
217  unpack_state = json_unpack (root,
218  "{s:s, s:o!}",
220  &name,
222  &data);
223  if (0 != unpack_state)
224  {
226  "Error namestore records object has a wrong format!\n");
227  return GNUNET_SYSERR;
228  }
229  gnsrecord_info = (struct GnsRecordInfo *) spec->ptr;
230  *(gnsrecord_info->name) = GNUNET_strdup (name);
231  if (GNUNET_OK != parse_record_data (gnsrecord_info, data))
232  {
233  cleanup_recordinfo (gnsrecord_info);
234  return GNUNET_SYSERR;
235  }
236  return GNUNET_OK;
237 }
#define GNUNET_JSON_GNSRECORD_RECORD_NAME
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int parse_record_data(struct GnsRecordInfo *gnsrecord_info, json_t *data)
Parse given JSON object to gns record.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
static void cleanup_recordinfo(struct GnsRecordInfo *gnsrecord_info)
void * ptr
Pointer, details specific to the parser.
const char * name
#define GNUNET_log(kind,...)
#define GNUNET_JSON_GNSRECORD_RECORD_DATA
uint32_t data
The data value.
Here is the call graph for this function:
Here is the caller 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 247 of file json_gnsrecord.c.

References GNUNET_free, and GNUNET_JSON_Specification::ptr.

Referenced by GNUNET_GNSRECORD_JSON_spec_gnsrecord().

248 {
249  struct GnsRecordInfo *gnsrecord_info = (struct GnsRecordInfo *) spec->ptr;
250 
251  GNUNET_free (gnsrecord_info);
252 }
void * ptr
Pointer, details specific to the parser.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function: