2
0

gnsrecord_misc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009-2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file gnsrecord/gnsrecord_misc.c
  18. * @brief MISC functions related to GNS records
  19. * @author Martin Schanzenbach
  20. * @author Matthias Wachs
  21. * @author Christian Grothoff
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_constants.h"
  26. #include "gnunet_signatures.h"
  27. #include "gnunet_arm_service.h"
  28. #include "gnunet_gnsrecord_lib.h"
  29. #include "gnunet_dnsparser_lib.h"
  30. #include "gnunet_tun_lib.h"
  31. #define LOG(kind, ...) GNUNET_log_from (kind, "gnsrecord", __VA_ARGS__)
  32. /**
  33. * Convert a UTF-8 string to UTF-8 lowercase
  34. * @param src source string
  35. * @return converted result
  36. */
  37. char *
  38. GNUNET_GNSRECORD_string_to_lowercase (const char *src)
  39. {
  40. char *res;
  41. res = GNUNET_strdup (src);
  42. GNUNET_STRINGS_utf8_tolower (src, res);
  43. return res;
  44. }
  45. /**
  46. * Convert a zone key to a string (for printing debug messages).
  47. * This is one of the very few calls in the entire API that is
  48. * NOT reentrant!
  49. *
  50. * @param z the zone key
  51. * @return string form; will be overwritten by next call to #GNUNET_GNSRECORD_z2s
  52. */
  53. const char *
  54. GNUNET_GNSRECORD_z2s (const struct GNUNET_CRYPTO_EcdsaPublicKey *z)
  55. {
  56. static char buf[sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) * 8];
  57. char *end;
  58. end = GNUNET_STRINGS_data_to_string ((const unsigned char *) z,
  59. sizeof(struct
  60. GNUNET_CRYPTO_EcdsaPublicKey),
  61. buf, sizeof(buf));
  62. if (NULL == end)
  63. {
  64. GNUNET_break (0);
  65. return NULL;
  66. }
  67. *end = '\0';
  68. return buf;
  69. }
  70. /**
  71. * Compares if two records are equal (ignoring flags such
  72. * as authority, private and pending, but not relative vs.
  73. * absolute expiration time).
  74. *
  75. * @param a record
  76. * @param b record
  77. * @return #GNUNET_YES if the records are equal or #GNUNET_NO if they are not
  78. */
  79. int
  80. GNUNET_GNSRECORD_records_cmp (const struct GNUNET_GNSRECORD_Data *a,
  81. const struct GNUNET_GNSRECORD_Data *b)
  82. {
  83. LOG (GNUNET_ERROR_TYPE_DEBUG,
  84. "Comparing records\n");
  85. if (a->record_type != b->record_type)
  86. {
  87. LOG (GNUNET_ERROR_TYPE_DEBUG,
  88. "Record type %lu != %lu\n", a->record_type, b->record_type);
  89. return GNUNET_NO;
  90. }
  91. if ((a->expiration_time != b->expiration_time) &&
  92. ((a->expiration_time != 0) && (b->expiration_time != 0)))
  93. {
  94. LOG (GNUNET_ERROR_TYPE_DEBUG,
  95. "Expiration time %llu != %llu\n",
  96. a->expiration_time,
  97. b->expiration_time);
  98. return GNUNET_NO;
  99. }
  100. if ((a->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS)
  101. != (b->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS))
  102. {
  103. LOG (GNUNET_ERROR_TYPE_DEBUG,
  104. "Flags %lu (%lu) != %lu (%lu)\n", a->flags,
  105. a->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS, b->flags,
  106. b->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS);
  107. return GNUNET_NO;
  108. }
  109. if (a->data_size != b->data_size)
  110. {
  111. LOG (GNUNET_ERROR_TYPE_DEBUG,
  112. "Data size %lu != %lu\n",
  113. a->data_size,
  114. b->data_size);
  115. return GNUNET_NO;
  116. }
  117. if (0 != memcmp (a->data, b->data, a->data_size))
  118. {
  119. LOG (GNUNET_ERROR_TYPE_DEBUG,
  120. "Data contents do not match\n");
  121. return GNUNET_NO;
  122. }
  123. LOG (GNUNET_ERROR_TYPE_DEBUG,
  124. "Records are equal\n");
  125. return GNUNET_YES;
  126. }
  127. /**
  128. * Returns the expiration time of the given block of records. The block
  129. * expiration time is the expiration time of the record with smallest
  130. * expiration time.
  131. *
  132. * @param rd_count number of records given in @a rd
  133. * @param rd array of records
  134. * @return absolute expiration time
  135. */
  136. struct GNUNET_TIME_Absolute
  137. GNUNET_GNSRECORD_record_get_expiration_time (unsigned int rd_count,
  138. const struct
  139. GNUNET_GNSRECORD_Data *rd)
  140. {
  141. struct GNUNET_TIME_Absolute expire;
  142. struct GNUNET_TIME_Absolute at;
  143. struct GNUNET_TIME_Relative rt;
  144. struct GNUNET_TIME_Absolute at_shadow;
  145. struct GNUNET_TIME_Relative rt_shadow;
  146. if (NULL == rd)
  147. return GNUNET_TIME_UNIT_ZERO_ABS;
  148. expire = GNUNET_TIME_UNIT_FOREVER_ABS;
  149. for (unsigned int c = 0; c < rd_count; c++)
  150. {
  151. if (0 != (rd[c].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
  152. {
  153. rt.rel_value_us = rd[c].expiration_time;
  154. at = GNUNET_TIME_relative_to_absolute (rt);
  155. }
  156. else
  157. {
  158. at.abs_value_us = rd[c].expiration_time;
  159. }
  160. for (unsigned int c2 = 0; c2 < rd_count; c2++)
  161. {
  162. /* Check for shadow record */
  163. if ((c == c2) ||
  164. (rd[c].record_type != rd[c2].record_type) ||
  165. (0 == (rd[c2].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)))
  166. continue;
  167. /* We have a shadow record */
  168. if (0 != (rd[c2].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
  169. {
  170. rt_shadow.rel_value_us = rd[c2].expiration_time;
  171. at_shadow = GNUNET_TIME_relative_to_absolute (rt_shadow);
  172. }
  173. else
  174. {
  175. at_shadow.abs_value_us = rd[c2].expiration_time;
  176. }
  177. at = GNUNET_TIME_absolute_max (at,
  178. at_shadow);
  179. }
  180. expire = GNUNET_TIME_absolute_min (at,
  181. expire);
  182. }
  183. LOG (GNUNET_ERROR_TYPE_DEBUG,
  184. "Determined expiration time for block with %u records to be %s\n",
  185. rd_count,
  186. GNUNET_STRINGS_absolute_time_to_string (expire));
  187. return expire;
  188. }
  189. /**
  190. * Test if a given record is expired.
  191. *
  192. * @return #GNUNET_YES if the record is expired,
  193. * #GNUNET_NO if not
  194. */
  195. int
  196. GNUNET_GNSRECORD_is_expired (const struct GNUNET_GNSRECORD_Data *rd)
  197. {
  198. struct GNUNET_TIME_Absolute at;
  199. if (0 != (rd->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
  200. return GNUNET_NO;
  201. at.abs_value_us = rd->expiration_time;
  202. return (0 == GNUNET_TIME_absolute_get_remaining (at).rel_value_us) ?
  203. GNUNET_YES : GNUNET_NO;
  204. }
  205. /**
  206. * Convert public key to the respective absolute domain name in the
  207. * ".zkey" pTLD.
  208. * This is one of the very few calls in the entire API that is
  209. * NOT reentrant!
  210. *
  211. * @param pkey a public key with a point on the eliptic curve
  212. * @return string "X.zkey" where X is the public
  213. * key in an encoding suitable for DNS labels.
  214. */
  215. const char *
  216. GNUNET_GNSRECORD_pkey_to_zkey (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
  217. {
  218. static char ret[128];
  219. char *pkeys;
  220. pkeys = GNUNET_CRYPTO_ecdsa_public_key_to_string (pkey);
  221. GNUNET_snprintf (ret,
  222. sizeof(ret),
  223. "%s",
  224. pkeys);
  225. GNUNET_free (pkeys);
  226. return ret;
  227. }
  228. /**
  229. * Convert an absolute domain name to the
  230. * respective public key.
  231. *
  232. * @param zkey string encoding the coordinates of the public
  233. * key in an encoding suitable for DNS labels.
  234. * @param pkey set to a public key on the eliptic curve
  235. * @return #GNUNET_SYSERR if @a zkey has the wrong syntax
  236. */
  237. int
  238. GNUNET_GNSRECORD_zkey_to_pkey (const char *zkey,
  239. struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
  240. {
  241. if (GNUNET_OK !=
  242. GNUNET_CRYPTO_ecdsa_public_key_from_string (zkey,
  243. strlen (zkey),
  244. pkey))
  245. return GNUNET_SYSERR;
  246. return GNUNET_OK;
  247. }
  248. /* end of gnsrecord_misc.c */