2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file gnsrecord/gnsrecord_misc.c
23 * @brief MISC functions related to GNS records
24 * @author Martin Schanzenbach
25 * @author Matthias Wachs
26 * @author Christian Grothoff
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"
38 #define LOG(kind, ...) GNUNET_log_from (kind, "gnsrecord", __VA_ARGS__)
41 * Convert a UTF-8 string to UTF-8 lowercase
42 * @param src source string
43 * @return converted result
46 GNUNET_GNSRECORD_string_to_lowercase (const char *src)
50 res = GNUNET_strdup (src);
51 GNUNET_STRINGS_utf8_tolower (src, res);
57 * Convert a zone key to a string (for printing debug messages).
58 * This is one of the very few calls in the entire API that is
61 * @param z the zone key
62 * @return string form; will be overwritten by next call to #GNUNET_GNSRECORD_z2s
65 GNUNET_GNSRECORD_z2s (const struct GNUNET_CRYPTO_EcdsaPublicKey *z)
67 static char buf[sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) * 8];
70 end = GNUNET_STRINGS_data_to_string ((const unsigned char *) z,
72 GNUNET_CRYPTO_EcdsaPublicKey),
85 * Compares if two records are equal (ignoring flags such
86 * as authority, private and pending, but not relative vs.
87 * absolute expiration time).
91 * @return #GNUNET_YES if the records are equal or #GNUNET_NO if they are not
94 GNUNET_GNSRECORD_records_cmp (const struct GNUNET_GNSRECORD_Data *a,
95 const struct GNUNET_GNSRECORD_Data *b)
97 LOG (GNUNET_ERROR_TYPE_DEBUG,
98 "Comparing records\n");
99 if (a->record_type != b->record_type)
101 LOG (GNUNET_ERROR_TYPE_DEBUG,
102 "Record type %lu != %lu\n", a->record_type, b->record_type);
105 if ((a->expiration_time != b->expiration_time) &&
106 ((a->expiration_time != 0) && (b->expiration_time != 0)))
108 LOG (GNUNET_ERROR_TYPE_DEBUG,
109 "Expiration time %llu != %llu\n",
114 if ((a->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS)
115 != (b->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS))
117 LOG (GNUNET_ERROR_TYPE_DEBUG,
118 "Flags %lu (%lu) != %lu (%lu)\n", a->flags,
119 a->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS, b->flags,
120 b->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS);
123 if (a->data_size != b->data_size)
125 LOG (GNUNET_ERROR_TYPE_DEBUG,
126 "Data size %lu != %lu\n",
131 if (0 != memcmp (a->data, b->data, a->data_size))
133 LOG (GNUNET_ERROR_TYPE_DEBUG,
134 "Data contents do not match\n");
137 LOG (GNUNET_ERROR_TYPE_DEBUG,
138 "Records are equal\n");
144 * Returns the expiration time of the given block of records. The block
145 * expiration time is the expiration time of the record with smallest
148 * @param rd_count number of records given in @a rd
149 * @param rd array of records
150 * @return absolute expiration time
152 struct GNUNET_TIME_Absolute
153 GNUNET_GNSRECORD_record_get_expiration_time (unsigned int rd_count,
155 GNUNET_GNSRECORD_Data *rd)
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;
164 return GNUNET_TIME_UNIT_ZERO_ABS;
165 expire = GNUNET_TIME_UNIT_FOREVER_ABS;
166 for (unsigned int c = 0; c < rd_count; c++)
168 if (0 != (rd[c].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
170 rt.rel_value_us = rd[c].expiration_time;
171 at = GNUNET_TIME_relative_to_absolute (rt);
175 at.abs_value_us = rd[c].expiration_time;
178 for (unsigned int c2 = 0; c2 < rd_count; c2++)
180 /* Check for shadow record */
182 (rd[c].record_type != rd[c2].record_type) ||
183 (0 == (rd[c2].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)))
185 /* We have a shadow record */
186 if (0 != (rd[c2].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
188 rt_shadow.rel_value_us = rd[c2].expiration_time;
189 at_shadow = GNUNET_TIME_relative_to_absolute (rt_shadow);
193 at_shadow.abs_value_us = rd[c2].expiration_time;
195 at = GNUNET_TIME_absolute_max (at,
198 expire = GNUNET_TIME_absolute_min (at,
201 LOG (GNUNET_ERROR_TYPE_DEBUG,
202 "Determined expiration time for block with %u records to be %s\n",
204 GNUNET_STRINGS_absolute_time_to_string (expire));
210 * Test if a given record is expired.
212 * @return #GNUNET_YES if the record is expired,
216 GNUNET_GNSRECORD_is_expired (const struct GNUNET_GNSRECORD_Data *rd)
218 struct GNUNET_TIME_Absolute at;
220 if (0 != (rd->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
222 at.abs_value_us = rd->expiration_time;
223 return (0 == GNUNET_TIME_absolute_get_remaining (at).rel_value_us) ?
224 GNUNET_YES : GNUNET_NO;
229 * Convert public key to the respective absolute domain name in the
231 * This is one of the very few calls in the entire API that is
234 * @param pkey a public key with a point on the eliptic curve
235 * @return string "X.zkey" where X is the public
236 * key in an encoding suitable for DNS labels.
239 GNUNET_GNSRECORD_pkey_to_zkey (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
241 static char ret[128];
244 pkeys = GNUNET_CRYPTO_ecdsa_public_key_to_string (pkey);
245 GNUNET_snprintf (ret,
255 * Convert an absolute domain name to the
256 * respective public key.
258 * @param zkey string encoding the coordinates of the public
259 * key in an encoding suitable for DNS labels.
260 * @param pkey set to a public key on the eliptic curve
261 * @return #GNUNET_SYSERR if @a zkey has the wrong syntax
264 GNUNET_GNSRECORD_zkey_to_pkey (const char *zkey,
265 struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
268 GNUNET_CRYPTO_ecdsa_public_key_from_string (zkey,
271 return GNUNET_SYSERR;
276 /* end of gnsrecord_misc.c */