aboutsummaryrefslogtreecommitdiff
path: root/src/json/json_gnsrecord.c
diff options
context:
space:
mode:
authorPhil <phil.buschmann@tum.de>2018-07-26 02:31:30 +0200
committerPhil <phil.buschmann@tum.de>2018-07-26 02:31:30 +0200
commitf7ca27a73e69a8c224d65768be3416ff1388c1d7 (patch)
tree7530a6def74a55885780285162b18b4c2b353380 /src/json/json_gnsrecord.c
parentcc577a227d6a5ae8ef75e0fa91ef98ced2d2b743 (diff)
downloadgnunet-f7ca27a73e69a8c224d65768be3416ff1388c1d7.tar.gz
gnunet-f7ca27a73e69a8c224d65768be3416ff1388c1d7.zip
change namestore, json handling; fix identity, gns
Diffstat (limited to 'src/json/json_gnsrecord.c')
-rw-r--r--src/json/json_gnsrecord.c163
1 files changed, 163 insertions, 0 deletions
diff --git a/src/json/json_gnsrecord.c b/src/json/json_gnsrecord.c
new file mode 100644
index 000000000..48b78f38b
--- /dev/null
+++ b/src/json/json_gnsrecord.c
@@ -0,0 +1,163 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 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
19/**
20 * @file json/json_gnsrecord.c
21 * @brief JSON handling of GNS record data
22 * @author Philippe Buschmann
23 */
24#include "platform.h"
25#include "gnunet_util_lib.h"
26#include "gnunet_gnsrecord_lib.h"
27#include "gnunet_json_lib.h"
28
29#define GNUNET_JSON_GNSRECORD_VALUE "value"
30#define GNUNET_JSON_GNSRECORD_TYPE "type"
31#define GNUNET_JSON_GNSRECORD_EXPIRATION_TIME "expiration_time"
32#define GNUNET_JSON_GNSRECORD_FLAG "flag"
33#define GNUNET_JSON_GNSRECORD_LABEL "label"
34#define GNUNET_JSON_GNSRECORD_NEVER "never"
35
36
37/**
38 * Parse given JSON object to gns record
39 *
40 * @param cls closure, NULL
41 * @param root the json object representing data
42 * @param spec where to write the data
43 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
44 */
45static int
46parse_gnsrecordobject (void *cls,
47 json_t *root,
48 struct GNUNET_JSON_Specification *spec)
49{
50 struct GNUNET_GNSRECORD_Data *gnsrecord_object;
51 struct GNUNET_TIME_Absolute abs_expiration_time;
52 int unpack_state=0;
53 const char *value;
54 const char *expiration_time;
55 const char *record_type;
56 const char *label;
57 int flag;
58 void *rdata;
59 size_t rdata_size;
60
61 GNUNET_assert(NULL != root);
62 if(!json_is_object(root))
63 {
64 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
65 "Error json is not array nor object!\n");
66 return GNUNET_SYSERR;
67 }
68 //interpret single gns record
69 unpack_state = json_unpack(root,
70 "{s:s, s:s, s:s, s?:i, s:s!}",
71 GNUNET_JSON_GNSRECORD_VALUE, &value,
72 GNUNET_JSON_GNSRECORD_TYPE, &record_type,
73 GNUNET_JSON_GNSRECORD_EXPIRATION_TIME, &expiration_time,
74 GNUNET_JSON_GNSRECORD_FLAG, &flag,
75 GNUNET_JSON_GNSRECORD_LABEL, &label);
76 if (GNUNET_SYSERR == unpack_state)
77 {
78 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
79 "Error json object has a wrong format!\n");
80 return GNUNET_SYSERR;
81 }
82 gnsrecord_object = GNUNET_new (struct GNUNET_GNSRECORD_Data);
83 gnsrecord_object->record_type = GNUNET_GNSRECORD_typename_to_number(record_type);
84 if (UINT32_MAX == gnsrecord_object->record_type)
85 {
86 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Unsupported type");
87 return GNUNET_SYSERR;
88 }
89 if (GNUNET_OK
90 != GNUNET_GNSRECORD_string_to_value (gnsrecord_object->record_type,
91 value,
92 &rdata,
93 &rdata_size))
94 {
95 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Value invalid for record type");
96 return GNUNET_SYSERR;
97 }
98
99 gnsrecord_object->data = rdata;
100 gnsrecord_object->data_size = rdata_size;
101
102 if (0 == strcmp (expiration_time, GNUNET_JSON_GNSRECORD_NEVER))
103 {
104 gnsrecord_object->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
105 }
106 else if (GNUNET_OK
107 == GNUNET_STRINGS_fancy_time_to_absolute (expiration_time,
108 &abs_expiration_time))
109 {
110 gnsrecord_object->expiration_time = abs_expiration_time.abs_value_us;
111 }
112 else
113 {
114 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Value invalid for expiration time");
115 return GNUNET_SYSERR;
116 }
117 gnsrecord_object->flags = (enum GNUNET_GNSRECORD_Flags)flag;
118 *(struct GNUNET_GNSRECORD_Data **) spec->ptr = gnsrecord_object;
119 return GNUNET_OK;
120}
121
122/**
123 * Cleanup data left from parsing RSA public key.
124 *
125 * @param cls closure, NULL
126 * @param[out] spec where to free the data
127 */
128static void
129clean_gnsrecordobject (void *cls, struct GNUNET_JSON_Specification *spec)
130{
131 struct GNUNET_GNSRECORD_Data **gnsrecord_object;
132 gnsrecord_object = (struct GNUNET_GNSRECORD_Data **) spec->ptr;
133 if (NULL != *gnsrecord_object)
134 {
135 if (NULL != (*gnsrecord_object)->data)
136 GNUNET_free((char*)(*gnsrecord_object)->data);
137
138 GNUNET_free(*gnsrecord_object);
139 *gnsrecord_object = NULL;
140 }
141}
142
143/**
144 * JSON Specification for GNS Records.
145 *
146 * @param gnsrecord_object struct of GNUNET_GNSRECORD_Data to fill
147 * @return JSON Specification
148 */
149struct GNUNET_JSON_Specification
150GNUNET_JSON_spec_gnsrecord_data (struct GNUNET_GNSRECORD_Data **gnsrecord_object)
151{
152 struct GNUNET_JSON_Specification ret = {
153 .parser = &parse_gnsrecordobject,
154 .cleaner = &clean_gnsrecordobject,
155 .cls = NULL,
156 .field = NULL,
157 .ptr = gnsrecord_object,
158 .ptr_size = 0,
159 .size_ptr = NULL
160 };
161 *gnsrecord_object = NULL;
162 return ret;
163}