GNUnet  0.11.x
gnsrecord_misc.c
Go to the documentation of this file.
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  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_signatures.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_gnsrecord_lib.h"
34 #include "gnunet_dnsparser_lib.h"
35 #include "gnunet_tun_lib.h"
36 
37 
38 #define LOG(kind, ...) GNUNET_log_from (kind, "gnsrecord", __VA_ARGS__)
39 
45 char *
47 {
48  char *res;
49 
50  res = GNUNET_strdup (src);
51  GNUNET_STRINGS_utf8_tolower (src, res);
52  return res;
53 }
54 
55 
64 const char *
66 {
67  static char buf[sizeof(struct GNUNET_IDENTITY_PublicKey) * 8];
68  char *end;
69 
70  end = GNUNET_STRINGS_data_to_string ((const unsigned char *) z,
71  sizeof(struct
73  buf, sizeof(buf));
74  if (NULL == end)
75  {
76  GNUNET_break (0);
77  return NULL;
78  }
79  *end = '\0';
80  return buf;
81 }
82 
83 
93 int
95  const struct GNUNET_GNSRECORD_Data *b)
96 {
98  "Comparing records\n");
99  if (a->record_type != b->record_type)
100  {
102  "Record type %u != %u\n", a->record_type, b->record_type);
103  return GNUNET_NO;
104  }
105  if ((a->expiration_time != b->expiration_time) &&
106  ((a->expiration_time != 0) && (b->expiration_time != 0)))
107  {
109  "Expiration time %llu != %llu\n",
110  (unsigned long long) a->expiration_time,
111  (unsigned long long) b->expiration_time);
112  return GNUNET_NO;
113  }
116  {
118  "Flags %u (%u) != %u (%u)\n", a->flags,
121  return GNUNET_NO;
122  }
123  if (a->data_size != b->data_size)
124  {
126  "Data size %lu != %lu\n",
127  a->data_size,
128  b->data_size);
129  return GNUNET_NO;
130  }
131  if (0 != memcmp (a->data, b->data, a->data_size))
132  {
134  "Data contents do not match\n");
135  return GNUNET_NO;
136  }
138  "Records are equal\n");
139  return GNUNET_YES;
140 }
141 
142 
154  const struct
156 {
157  struct GNUNET_TIME_Absolute expire;
158  struct GNUNET_TIME_Absolute at;
159  struct GNUNET_TIME_Relative rt;
160  struct GNUNET_TIME_Absolute at_shadow;
161  struct GNUNET_TIME_Relative rt_shadow;
162 
163  if (NULL == rd)
166  for (unsigned int c = 0; c < rd_count; c++)
167  {
168  if (0 != (rd[c].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
169  {
170  rt.rel_value_us = rd[c].expiration_time;
172  }
173  else
174  {
175  at.abs_value_us = rd[c].expiration_time;
176  }
177 
178  for (unsigned int c2 = 0; c2 < rd_count; c2++)
179  {
180  /* Check for shadow record */
181  if ((c == c2) ||
182  (rd[c].record_type != rd[c2].record_type) ||
183  (0 == (rd[c2].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)))
184  continue;
185  /* We have a shadow record */
186  if (0 != (rd[c2].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
187  {
188  rt_shadow.rel_value_us = rd[c2].expiration_time;
189  at_shadow = GNUNET_TIME_relative_to_absolute (rt_shadow);
190  }
191  else
192  {
193  at_shadow.abs_value_us = rd[c2].expiration_time;
194  }
195  at = GNUNET_TIME_absolute_max (at,
196  at_shadow);
197  }
198  expire = GNUNET_TIME_absolute_min (at,
199  expire);
200  }
202  "Determined expiration time for block with %u records to be %s\n",
203  rd_count,
205  return expire;
206 }
207 
208 
215 int
217 {
218  struct GNUNET_TIME_Absolute at;
219 
221  return GNUNET_NO;
222  at.abs_value_us = rd->expiration_time;
223  return (0 == GNUNET_TIME_absolute_get_remaining (at).rel_value_us) ?
225 }
226 
227 
238 const char *
240 {
241  static char ret[128];
242  char *pkeys;
243 
245  GNUNET_snprintf (ret,
246  sizeof(ret),
247  "%s",
248  pkeys);
249  GNUNET_free (pkeys);
250  return ret;
251 }
252 
253 
263 int
266 {
267  if (GNUNET_OK !=
269  pkey))
270  return GNUNET_SYSERR;
271  return GNUNET_OK;
272 }
273 
274 
277  size_t data_size,
278  uint32_t type,
280 {
282  return GNUNET_SYSERR;
283  if (data_size > sizeof (struct GNUNET_IDENTITY_PublicKey))
284  return GNUNET_SYSERR;
285  return (GNUNET_IDENTITY_read_key_from_buffer (key, data, data_size) ==
286  data_size?
287  GNUNET_OK :
288  GNUNET_SYSERR);
289 }
290 
291 
295  char **data,
296  size_t *data_size,
297  uint32_t *type)
298 {
299  *type = ntohl (key->type);
300  *data_size = GNUNET_IDENTITY_key_get_length (key);
301  if (0 == *data_size)
302  return GNUNET_SYSERR;
303  *data = GNUNET_malloc (*data_size);
304  return (GNUNET_IDENTITY_write_key_to_buffer (key, *data, *data_size) ==
305  *data_size?
306  GNUNET_OK :
307  GNUNET_SYSERR);
308 }
309 
310 
313 {
314  switch (type)
315  {
318  return GNUNET_YES;
319  default:
320  return GNUNET_NO;
321  }
322 }
323 
324 
325 size_t
327 {
328  switch (ntohl (block->type))
329  {
331  return sizeof (uint32_t) /* zone type */
332  + sizeof (block->ecdsa_block) /* EcdsaBlock */
333  + ntohl (block->ecdsa_block.purpose.size) /* Length of signed data */
334  - sizeof (block->ecdsa_block.purpose); /* Purpose already in EcdsaBlock */
335  break;
336  default:
337  return 0;
338  }
339  return 0;
340 }
341 
342 
345  GNUNET_GNSRECORD_Block *block)
346 {
347 
348  switch (ntohl (block->type))
349  {
351  return GNUNET_TIME_absolute_ntoh (block->ecdsa_block.expiration_time);
352  default:
354  }
356 
357 }
358 
359 
362  struct GNUNET_HashCode *query)
363 {
364  switch (ntohl (block->type))
365  {
368  sizeof (block->ecdsa_block.derived_key),
369  query);
370  return GNUNET_OK;
371  default:
372  return GNUNET_SYSERR;
373  }
374  return GNUNET_SYSERR;
375 
376 }
377 
378 
382 {
384  "Got record of type %u\n",
385  rd->record_type);
386  switch (rd->record_type)
387  {
389  key->type = htonl (rd->record_type);
390  memcpy (&key->ecdsa_key, rd->data, sizeof (key->ecdsa_key));
391  return GNUNET_OK;
392  default:
393  return GNUNET_SYSERR;
394  }
395  return GNUNET_SYSERR;
396 
397 
398 }
399 
400 
401 /* end of gnsrecord_misc.c */
This record should not be used unless all (other) records with an absolute expiration time have expir...
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:673
#define GNUNET_TIME_UNIT_ZERO_ABS
Absolute time zero.
char * GNUNET_GNSRECORD_string_to_lowercase(const char *src)
Convert a UTF-8 string to UTF-8 lowercase.
uint64_t rel_value_us
The actual value.
static char * pkey
Public key of the zone to look in, in ASCII.
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
uint32_t type
Type of public key.
struct GNUNET_GNSRECORD_EcdsaBlock ecdsa_block
#define GNUNET_GNSRECORD_TYPE_EDKEY
Record type for EDKEY delegations.
int GNUNET_GNSRECORD_zkey_to_pkey(const char *zkey, struct GNUNET_IDENTITY_PublicKey *pkey)
Convert an absolute domain name to the respective public key.
static void expire(void *cls)
Expire a PooledConnection object.
char * GNUNET_IDENTITY_public_key_to_string(const struct GNUNET_IDENTITY_PublicKey *key)
Creates a (Base32) string representation of the public key.
struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
Convert relative time to an absolute time in the future.
Definition: time.c:246
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
ssize_t GNUNET_IDENTITY_read_key_from_buffer(struct GNUNET_IDENTITY_PublicKey *key, const void *buffer, size_t len)
Reads a GNUNET_IDENTITY_PublicKey from a compact buffer.
Definition: identity_api.c:994
ssize_t GNUNET_IDENTITY_key_get_length(const struct GNUNET_IDENTITY_PublicKey *key)
Get the compacted length of a GNUNET_IDENTITY_PublicKey.
Definition: identity_api.c:976
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
struct GNUNET_CRYPTO_EcdsaPublicKey derived_key
Derived key used for signing; hash of this is the query.
GNUNET_GenericReturnValue
Named constants for return values.
Definition: gnunet_common.h:83
ssize_t GNUNET_IDENTITY_write_key_to_buffer(const struct GNUNET_IDENTITY_PublicKey *key, void *buffer, size_t len)
Writes a GNUNET_IDENTITY_PublicKey to a compact buffer.
size_t data_size
Number of bytes in data.
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_record_to_identity_key(const struct GNUNET_GNSRECORD_Data *rd, struct GNUNET_IDENTITY_PublicKey *key)
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
uint64_t abs_value_us
The actual value.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Number of bytes signed; also specifies the number of bytes of encrypted data that follow...
const void * data
Binary value stored in the DNS record.
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_is_zonekey_type(uint32_t type)
Check if this type is one of the supported GNS zone types.
size_t GNUNET_GNSRECORD_block_get_size(const struct GNUNET_GNSRECORD_Block *block)
Returns the length of this block in bytes.
#define GNUNET_GNSRECORD_TYPE_PKEY
Record type for GNS zone transfer ("PKEY").
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:48
#define LOG(kind,...)
struct GNUNET_CRYPTO_EcdsaPublicKey ecdsa_key
An ECDSA identity key.
uint64_t expiration_time
Expiration time for the DNS record.
static char buf[2048]
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_max(struct GNUNET_TIME_Absolute t1, struct GNUNET_TIME_Absolute t2)
Return the maximum of two absolute time values.
Definition: time.c:317
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_zero_(void)
Return absolute time of 0ms.
Definition: time.c:147
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!)...
A 512-bit hashcode.
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_identity_from_data(const char *data, size_t data_size, uint32_t type, struct GNUNET_IDENTITY_PublicKey *key)
Build a #GNUNET_GNSRECORD_PublicKey from zone delegation resource record data.
static int res
struct GNUNET_TIME_Absolute GNUNET_GNSRECORD_record_get_expiration_time(unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Returns the expiration time of the given block of records.
enum GNUNET_GenericReturnValue GNUNET_IDENTITY_public_key_from_string(const char *str, struct GNUNET_IDENTITY_PublicKey *key)
Parses a (Base32) string representation of the public key.
struct GNUNET_HashCode key
The key used in the DHT.
#define GNUNET_GNSRECORD_RF_RCMP_FLAGS
int GNUNET_GNSRECORD_records_cmp(const struct GNUNET_GNSRECORD_Data *a, const struct GNUNET_GNSRECORD_Data *b)
Compares if two records are equal (ignoring flags such as authority, private and pending, but not relative vs.
const char * GNUNET_GNSRECORD_pkey_to_zkey(const struct GNUNET_IDENTITY_PublicKey *pkey)
Convert public key to the respective absolute domain name in the ".zkey" pTLD.
int GNUNET_GNSRECORD_is_expired(const struct GNUNET_GNSRECORD_Data *rd)
Test if a given record is expired.
An identity key as per LSD0001.
uint32_t record_type
Type of the GNS/DNS record.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_min(struct GNUNET_TIME_Absolute t1, struct GNUNET_TIME_Absolute t2)
Return the minimum of two absolute time values.
Definition: time.c:302
#define GNUNET_log(kind,...)
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_query_from_block(const struct GNUNET_GNSRECORD_Block *block, struct GNUNET_HashCode *query)
Builds the query hash from a block.
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition: time.c:331
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Time for absolute times used by GNUnet, in microseconds.
enum GNUNET_GNSRECORD_Flags flags
Flags for the record.
This expiration time of the record is a relative time (not an absolute time).
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:758
struct GNUNET_TIME_Absolute GNUNET_GNSRECORD_block_get_expiration(const struct GNUNET_GNSRECORD_Block *block)
Returns the expiration of a block.
uint32_t data
The data value.
const char * GNUNET_GNSRECORD_z2s(const struct GNUNET_IDENTITY_PublicKey *z)
Convert a zone key to a string (for printing debug messages).
#define GNUNET_malloc(size)
Wrapper around malloc.
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using CrockfordBase32.
Definition: strings.c:872
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
void GNUNET_STRINGS_utf8_tolower(const char *input, char *output)
Convert the utf-8 input string to lower case.
Definition: strings.c:568
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_data_from_identity(const struct GNUNET_IDENTITY_PublicKey *key, char **data, size_t *data_size, uint32_t *type)
Create record data and size from an identity key.