aboutsummaryrefslogtreecommitdiff
path: root/src/json/json_gnsrecord.c
blob: 7bdf97f06dab80141c63010289dc477f3076afed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
     This file is part of GNUnet.
     Copyright (C) 2009-2013 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file json/json_gnsrecord.c
 * @brief JSON handling of GNS record data
 * @author Philippe Buschmann
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_json_lib.h"

#define GNUNET_JSON_GNSRECORD_VALUE "value"
#define GNUNET_JSON_GNSRECORD_TYPE "type"
#define GNUNET_JSON_GNSRECORD_EXPIRATION_TIME "expiration_time"
#define GNUNET_JSON_GNSRECORD_FLAG "flag"
#define GNUNET_JSON_GNSRECORD_LABEL "label"
#define GNUNET_JSON_GNSRECORD_NEVER "never"


/**
 * Parse given JSON object to gns record
 *
 * @param cls closure, NULL
 * @param root the json object representing data
 * @param spec where to write the data
 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
 */
static int
parse_gnsrecordobject (void *cls,
		       json_t *root,
		       struct GNUNET_JSON_Specification *spec)
{
  struct GNUNET_GNSRECORD_Data *gnsrecord_object;
  struct GNUNET_TIME_Absolute abs_expiration_time;
  int unpack_state=0;
  const char *value;
  const char *expiration_time;
  const char *record_type;
  const char *label;
  int flag;
  void *rdata = NULL;
  size_t rdata_size;

  GNUNET_assert(NULL != root);
  if(!json_is_object(root))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		"Error json is not array nor object!\n");
    return GNUNET_SYSERR;
  }
  //interpret single gns record
  unpack_state = json_unpack(root,
			     "{s:s, s:s, s:s, s?:i, s:s!}",
			     GNUNET_JSON_GNSRECORD_VALUE, &value,
			     GNUNET_JSON_GNSRECORD_TYPE, &record_type,
			     GNUNET_JSON_GNSRECORD_EXPIRATION_TIME, &expiration_time,
			     GNUNET_JSON_GNSRECORD_FLAG, &flag,
			     GNUNET_JSON_GNSRECORD_LABEL, &label);
  if (0 != unpack_state)
  {
    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
	       "Error json object has a wrong format!\n");
    return GNUNET_SYSERR;
  }
  gnsrecord_object = GNUNET_new (struct GNUNET_GNSRECORD_Data);
  gnsrecord_object->record_type = GNUNET_GNSRECORD_typename_to_number(record_type);
  if (UINT32_MAX == gnsrecord_object->record_type)
  {
    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Unsupported type\n");
    GNUNET_free(gnsrecord_object);
    return GNUNET_SYSERR;
  }
  if (GNUNET_OK
      != GNUNET_GNSRECORD_string_to_value (gnsrecord_object->record_type,
					   value,
					   &rdata,
					   &rdata_size))
  {
    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Value invalid for record type\n");
    GNUNET_free(gnsrecord_object);
    return GNUNET_SYSERR;
  }

  gnsrecord_object->data = rdata;
  gnsrecord_object->data_size = rdata_size;

  if (0 == strcmp (expiration_time, GNUNET_JSON_GNSRECORD_NEVER))
  {
    gnsrecord_object->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
  }
  else if (GNUNET_OK
      == GNUNET_STRINGS_fancy_time_to_absolute (expiration_time,
						&abs_expiration_time))
  {
    gnsrecord_object->expiration_time = abs_expiration_time.abs_value_us;
  }
  else
  {
    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Expiration time invalid\n");
    GNUNET_free_non_null(rdata);
    GNUNET_free(gnsrecord_object);
    return GNUNET_SYSERR;
  }
  // check if flag is a valid enum value
  if ((GNUNET_GNSRECORD_RF_NONE != flag)
      && (GNUNET_GNSRECORD_RF_PRIVATE != flag)
      && (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION != flag)
      && (GNUNET_GNSRECORD_RF_SHADOW_RECORD) != flag)
  {
    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Flag invalid\n");
    GNUNET_free_non_null(rdata);
    GNUNET_free(gnsrecord_object);
    return GNUNET_SYSERR;
  }
  gnsrecord_object->flags = (enum GNUNET_GNSRECORD_Flags)flag;
  *(struct GNUNET_GNSRECORD_Data **) spec->ptr = gnsrecord_object;
  return GNUNET_OK;
}

/**
 * Cleanup data left from parsing RSA public key.
 *
 * @param cls closure, NULL
 * @param[out] spec where to free the data
 */
static void
clean_gnsrecordobject (void *cls, struct GNUNET_JSON_Specification *spec)
{
  struct GNUNET_GNSRECORD_Data **gnsrecord_object;
  gnsrecord_object = (struct GNUNET_GNSRECORD_Data **) spec->ptr;
  if (NULL != *gnsrecord_object)
  {
    if (NULL != (*gnsrecord_object)->data)
      GNUNET_free((char*)(*gnsrecord_object)->data);

    GNUNET_free(*gnsrecord_object);
    *gnsrecord_object = NULL;
  }
}

/**
 * JSON Specification for GNS Records.
 *
 * @param gnsrecord_object struct of GNUNET_GNSRECORD_Data to fill
 * @return JSON Specification
 */
struct GNUNET_JSON_Specification
GNUNET_JSON_spec_gnsrecord_data (struct GNUNET_GNSRECORD_Data **gnsrecord_object)
{
  struct GNUNET_JSON_Specification ret = {
    .parser = &parse_gnsrecordobject,
    .cleaner = &clean_gnsrecordobject,
    .cls = NULL,
    .field = NULL,
    .ptr = gnsrecord_object,
    .ptr_size = 0,
    .size_ptr = NULL
  };
  *gnsrecord_object = NULL;
  return ret;
}