GNUnet 0.21.1
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_MAINTENANCE   "is_maintenance"
 
#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_MAINTENANCE

#define GNUNET_JSON_GNSRECORD_FLAG_MAINTENANCE   "is_maintenance"

Definition at line 36 of file json_gnsrecord.c.

◆ GNUNET_JSON_GNSRECORD_FLAG_PRIVATE

#define GNUNET_JSON_GNSRECORD_FLAG_PRIVATE   "is_private"

Definition at line 37 of file json_gnsrecord.c.

◆ GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL

#define GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL   "is_supplemental"

Definition at line 38 of file json_gnsrecord.c.

◆ GNUNET_JSON_GNSRECORD_FLAG_RELATIVE

#define GNUNET_JSON_GNSRECORD_FLAG_RELATIVE   "is_relative_expiration"

Definition at line 39 of file json_gnsrecord.c.

◆ GNUNET_JSON_GNSRECORD_FLAG_SHADOW

#define GNUNET_JSON_GNSRECORD_FLAG_SHADOW   "is_shadow"

Definition at line 40 of file json_gnsrecord.c.

◆ GNUNET_JSON_GNSRECORD_RECORD_NAME

#define GNUNET_JSON_GNSRECORD_RECORD_NAME   "record_name"

Definition at line 41 of file json_gnsrecord.c.

Function Documentation

◆ cleanup_recordinfo()

static void cleanup_recordinfo ( struct GnsRecordInfo gnsrecord_info)
static

Definition at line 54 of file json_gnsrecord.c.

55{
56 char *tmp;
57
58 if (NULL != *(gnsrecord_info->rd))
59 {
60 for (int i = 0; i < *(gnsrecord_info->rd_count); i++)
61 {
62 tmp = (char*) (*(gnsrecord_info->rd))[i].data;
63 if (NULL != tmp)
64 GNUNET_free (tmp);
65 }
66 GNUNET_free (*(gnsrecord_info->rd));
67 *(gnsrecord_info->rd) = NULL;
68 }
69 if (NULL != *(gnsrecord_info->name))
70 GNUNET_free (*(gnsrecord_info->name));
71 *(gnsrecord_info->name) = NULL;
72}
static char * data
The data to insert into the dht.
#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 84 of file json_gnsrecord.c.

85{
86 struct GNUNET_TIME_Absolute abs_exp;
87 struct GNUNET_TIME_Relative rel_exp;
88 const char *value;
89 const char *record_type;
90 int maintenance;
91 int private;
92 int supplemental;
93 int is_rel_exp;
94 int shadow;
95 int unpack_state = 0;
96 json_error_t err;
97
98 // interpret single gns record
99 unpack_state = json_unpack_ex (data,
100 &err,
101 0,
102 "{s:s, s:s, s:I, s:b, s:b, s:b, s:b}",
104 &value,
106 &record_type,
108 &rel_exp.rel_value_us,
110 &maintenance,
112 &private,
114 &supplemental,
116 &is_rel_exp,
118 &shadow);
119 if (0 != unpack_state)
120 {
122 "Error gnsdata object has a wrong format: `%s'!\n",
123 err.text);
124 unpack_state = json_unpack_ex (data,
125 &err,
126 0,
127 "{s:s, s:s, s:I, s:b, s:b, s:b, s:b}",
129 &value,
131 &record_type,
133 &abs_exp.abs_value_us,
135 &maintenance,
137 &private,
139 &supplemental,
141 &is_rel_exp,
143 &shadow);
144 if ((0 != unpack_state) || (is_rel_exp))
145 {
147 "Error gnsdata object has a wrong format: `%s'!\n",
148 (is_rel_exp) ? "No relative expiration given" : err.text);
149 return GNUNET_SYSERR;
150 }
151 rd->expiration_time = abs_exp.abs_value_us;
152 }
153 else
154 {
155 rd->expiration_time = rel_exp.rel_value_us;
156 }
158 if (UINT32_MAX == rd->record_type)
159 {
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unsupported type\n");
161 return GNUNET_SYSERR;
162 }
164 value,
165 (void **) &rd->data,
166 &rd->data_size))
167 {
168 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Value invalid for record type\n");
169 return GNUNET_SYSERR;
170 }
171
172 if (is_rel_exp)
174 if (1 == maintenance)
176 if (1 == private)
178 if (1 == supplemental)
180 if (1 == shadow)
182 return GNUNET_OK;
183}
static struct GNUNET_GNSRECORD_Data rd[50]
The record data under a single label.
static char * value
Value of the record to add/remove.
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
@ 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_MAINTENANCE
Maintenance records.
@ 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.
#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_FLAG_MAINTENANCE
#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, data, 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_MAINTENANCE, 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_MAINTENANCE, 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 195 of file json_gnsrecord.c.

196{
197 GNUNET_assert (NULL != data);
198 if (! json_is_array (data))
199 {
201 "Error gns record data JSON is not an array!\n");
202 return GNUNET_SYSERR;
203 }
204 *(gnsrecord_info->rd_count) = json_array_size (data);
205 *(gnsrecord_info->rd) = GNUNET_malloc (sizeof(struct GNUNET_GNSRECORD_Data)
206 * json_array_size (data));
207 size_t index;
208 json_t *value;
209 json_array_foreach (data, index, value)
210 {
211 if (GNUNET_OK != parse_record (value, &(*(gnsrecord_info->rd))[index]))
212 return GNUNET_SYSERR;
213 }
214 return GNUNET_OK;
215}
#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 219 of file json_gnsrecord.c.

222{
223 struct GnsRecordInfo *gnsrecord_info;
224 int unpack_state = 0;
225 const char *name;
226 json_t *data;
227
228 GNUNET_assert (NULL != root);
229 if (! json_is_object (root))
230 {
232 "Error record JSON is not an object!\n");
233 return GNUNET_SYSERR;
234 }
235 // interpret single gns record
236 unpack_state = json_unpack (root,
237 "{s:s, s:o!}",
239 &name,
241 &data);
242 if (0 != unpack_state)
243 {
245 "Error namestore records object has a wrong format!\n");
246 return GNUNET_SYSERR;
247 }
248 gnsrecord_info = (struct GnsRecordInfo *) spec->ptr;
249 *(gnsrecord_info->name) = GNUNET_strdup (name);
250 if (GNUNET_OK != parse_record_data (gnsrecord_info, data))
251 {
252 cleanup_recordinfo (gnsrecord_info);
253 return GNUNET_SYSERR;
254 }
255 return GNUNET_OK;
256}
static char * name
Name (label) of the records to list.
#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
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.

Referenced by GNUNET_GNSRECORD_JSON_spec_gnsrecord().

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 266 of file json_gnsrecord.c.

267{
268 struct GnsRecordInfo *gnsrecord_info = (struct GnsRecordInfo *) spec->ptr;
269
270 GNUNET_free (gnsrecord_info);
271}

References GNUNET_free, and GNUNET_JSON_Specification::ptr.

Referenced by GNUNET_GNSRECORD_JSON_spec_gnsrecord().

Here is the caller graph for this function: