GNUnet  0.10.x
Data Structures | Macros | Functions
json_gnsrecord.c File Reference

JSON handling of GNS record data. More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_json_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   "flag"
 
#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_JSON_spec_gnsrecord (struct GNUNET_GNSRECORD_Data **rd, unsigned int *rd_count, char **name)
 JSON Specification for GNS Records. More...
 

Detailed Description

JSON handling of GNS record data.

Author
Philippe Buschmann

Definition in file json_gnsrecord.c.

Macro Definition Documentation

◆ GNUNET_JSON_GNSRECORD_VALUE

#define GNUNET_JSON_GNSRECORD_VALUE   "value"

Definition at line 30 of file json_gnsrecord.c.

Referenced by parse_record().

◆ GNUNET_JSON_GNSRECORD_RECORD_DATA

#define GNUNET_JSON_GNSRECORD_RECORD_DATA   "data"

Definition at line 31 of file json_gnsrecord.c.

Referenced by parse_gnsrecordobject().

◆ GNUNET_JSON_GNSRECORD_TYPE

#define GNUNET_JSON_GNSRECORD_TYPE   "record_type"

Definition at line 32 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 33 of file json_gnsrecord.c.

Referenced by parse_record().

◆ GNUNET_JSON_GNSRECORD_FLAG

#define GNUNET_JSON_GNSRECORD_FLAG   "flag"

Definition at line 34 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 35 of file json_gnsrecord.c.

Referenced by parse_gnsrecordobject().

◆ GNUNET_JSON_GNSRECORD_NEVER

#define GNUNET_JSON_GNSRECORD_NEVER   "never"

Definition at line 36 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 48 of file json_gnsrecord.c.

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

Referenced by parse_gnsrecordobject().

49 {
50  if (NULL != *(gnsrecord_info->rd))
51  {
52  for (int i = 0; i < *(gnsrecord_info->rd_count); i++)
53  {
54  if (NULL != (*(gnsrecord_info->rd))[i].data)
55  GNUNET_free((char *)(*(gnsrecord_info->rd))[i].data);
56  }
57  GNUNET_free(*(gnsrecord_info->rd));
58  *(gnsrecord_info->rd) = NULL;
59  }
60  if (NULL != *(gnsrecord_info->name))
61  GNUNET_free(*(gnsrecord_info->name));
62  *(gnsrecord_info->name) = NULL;
63 }
unsigned int * rd_count
struct GNUNET_GNSRECORD_Data ** rd
#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 75 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_string_to_value(), GNUNET_GNSRECORD_typename_to_number(), GNUNET_JSON_GNSRECORD_EXPIRATION_TIME, GNUNET_JSON_GNSRECORD_FLAG, 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().

76 {
77  struct GNUNET_TIME_Absolute abs_expiration_time;
78  struct GNUNET_TIME_Relative rel_expiration_time;
79  const char *value;
80  const char *record_type;
81  const char *expiration_time;
82  int flag;
83  int unpack_state = 0;
84 
85  //interpret single gns record
86  unpack_state = json_unpack(data,
87  "{s:s, s:s, s:s, s?:i!}",
89  &value,
91  &record_type,
93  &expiration_time,
95  &flag);
96  if (0 != unpack_state)
97  {
99  "Error gnsdata object has a wrong format!\n");
100  return GNUNET_SYSERR;
101  }
103  if (UINT32_MAX == rd->record_type)
104  {
105  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Unsupported type\n");
106  return GNUNET_SYSERR;
107  }
109  value,
110  (void**)&rd->data,
111  &rd->data_size))
112  {
113  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Value invalid for record type\n");
114  return GNUNET_SYSERR;
115  }
116 
117  if (0 == strcmp(expiration_time, GNUNET_JSON_GNSRECORD_NEVER))
118  {
120  }
121  else if (GNUNET_OK ==
123  &abs_expiration_time))
124  {
125  rd->expiration_time = abs_expiration_time.abs_value_us;
126  }
127  else if (GNUNET_OK ==
129  &rel_expiration_time))
130  {
131  rd->expiration_time = rel_expiration_time.rel_value_us;
132  }
133  else
134  {
135  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Expiration time invalid\n");
136  return GNUNET_SYSERR;
137  }
138  rd->flags = (enum GNUNET_GNSRECORD_Flags)flag;
139  return GNUNET_OK;
140 }
#define GNUNET_JSON_GNSRECORD_VALUE
uint32_t GNUNET_GNSRECORD_typename_to_number(const char *dns_typename)
Convert a type name (i.e.
Definition: gnsrecord.c:203
#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:350
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:401
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
size_t data_size
Number of bytes in data.
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
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.
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:174
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
#define GNUNET_JSON_GNSRECORD_FLAG
#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.
GNUNET_GNSRECORD_Flags
Flags that can be set for a record.
#define GNUNET_JSON_GNSRECORD_NEVER
enum GNUNET_GNSRECORD_Flags flags
Flags for the record.
uint32_t data
The data value.
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:

◆ 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 152 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().

153 {
154  GNUNET_assert(NULL != data);
155  if (!json_is_array(data))
156  {
158  "Error gns record data JSON is not an array!\n");
159  return GNUNET_SYSERR;
160  }
161  *(gnsrecord_info->rd_count) = json_array_size(data);
162  *(gnsrecord_info->rd) = GNUNET_malloc(sizeof(struct GNUNET_GNSRECORD_Data) *
163  json_array_size(data));
164  size_t index;
165  json_t *value;
166  json_array_foreach(data, index, value)
167  {
168  if (GNUNET_OK != parse_record(value, &(*(gnsrecord_info->rd))[index]))
169  return GNUNET_SYSERR;
170  }
171  return GNUNET_OK;
172 }
#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:75
static char * value
Value of the record to add/remove.
unsigned int * rd_count
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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 176 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_JSON_spec_gnsrecord().

179 {
180  struct GnsRecordInfo *gnsrecord_info;
181  int unpack_state = 0;
182  const char *name;
183  json_t *data;
184 
185  GNUNET_assert(NULL != root);
186  if (!json_is_object(root))
187  {
189  "Error record JSON is not an object!\n");
190  return GNUNET_SYSERR;
191  }
192  //interpret single gns record
193  unpack_state = json_unpack(root,
194  "{s:s, s:o!}",
196  &name,
198  &data);
199  if (0 != unpack_state)
200  {
202  "Error namestore records object has a wrong format!\n");
203  return GNUNET_SYSERR;
204  }
205  gnsrecord_info = (struct GnsRecordInfo *)spec->ptr;
206  *(gnsrecord_info->name) = GNUNET_strdup(name);
207  if (GNUNET_OK != parse_record_data(gnsrecord_info, data))
208  {
209  cleanup_recordinfo(gnsrecord_info);
210  return GNUNET_SYSERR;
211  }
212  return GNUNET_OK;
213 }
#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_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
static void cleanup_recordinfo(struct GnsRecordInfo *gnsrecord_info)
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
const char * name
void * ptr
Pointer, details specific to the parser.
#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 223 of file json_gnsrecord.c.

References GNUNET_free, and GNUNET_JSON_Specification::ptr.

Referenced by GNUNET_JSON_spec_gnsrecord().

224 {
225  struct GnsRecordInfo *gnsrecord_info = (struct GnsRecordInfo *)spec->ptr;
226 
227  GNUNET_free(gnsrecord_info);
228 }
void * ptr
Pointer, details specific to the parser.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ GNUNET_JSON_spec_gnsrecord()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_gnsrecord ( struct GNUNET_GNSRECORD_Data **  rd,
unsigned int *  rd_count,
char **  name 
)

JSON Specification for GNS Records.

Parameters
gnsrecord_objectstruct of GNUNET_GNSRECORD_Data to fill
Returns
JSON Specification

Definition at line 238 of file json_gnsrecord.c.

References clean_gnsrecordobject(), GNUNET_new, GnsRecordInfo::name, parse_gnsrecordobject(), GNUNET_JSON_Specification::parser, GnsRecordInfo::rd, GnsRecordInfo::rd_count, and ret.

Referenced by namestore_add().

241 {
242  struct GnsRecordInfo *gnsrecord_info = GNUNET_new(struct GnsRecordInfo);
243 
244  gnsrecord_info->rd = rd;
245  gnsrecord_info->name = name;
246  gnsrecord_info->rd_count = rd_count;
248  .cleaner = &clean_gnsrecordobject,
249  .cls = NULL,
250  .field = NULL,
251  .ptr = (struct GnsRecordInfo *)
252  gnsrecord_info,
253  .ptr_size = 0,
254  .size_ptr = NULL };
255  return ret;
256 }
Entry in parser specification for GNUNET_JSON_parse().
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static int ret
Final status code.
Definition: gnunet-arm.c:89
static int parse_gnsrecordobject(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
unsigned int * rd_count
const char * name
static void clean_gnsrecordobject(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing the record.
struct GNUNET_GNSRECORD_Data ** rd
Here is the call graph for this function:
Here is the caller graph for this function: