summaryrefslogtreecommitdiff
path: root/src/gnsrecord/gnsrecord_misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gnsrecord/gnsrecord_misc.c')
-rw-r--r--src/gnsrecord/gnsrecord_misc.c210
1 files changed, 105 insertions, 105 deletions
diff --git a/src/gnsrecord/gnsrecord_misc.c b/src/gnsrecord/gnsrecord_misc.c
index 50dd2086b..159f078ee 100644
--- a/src/gnsrecord/gnsrecord_misc.c
+++ b/src/gnsrecord/gnsrecord_misc.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file gnsrecord/gnsrecord_misc.c 22 * @file gnsrecord/gnsrecord_misc.c
@@ -35,7 +35,7 @@
35#include "gnunet_tun_lib.h" 35#include "gnunet_tun_lib.h"
36 36
37 37
38#define LOG(kind,...) GNUNET_log_from (kind, "gnsrecord",__VA_ARGS__) 38#define LOG(kind, ...) GNUNET_log_from(kind, "gnsrecord", __VA_ARGS__)
39 39
40/** 40/**
41 * Convert a UTF-8 string to UTF-8 lowercase 41 * Convert a UTF-8 string to UTF-8 lowercase
@@ -43,12 +43,12 @@
43 * @return converted result 43 * @return converted result
44 */ 44 */
45char * 45char *
46GNUNET_GNSRECORD_string_to_lowercase (const char *src) 46GNUNET_GNSRECORD_string_to_lowercase(const char *src)
47{ 47{
48 char *res; 48 char *res;
49 49
50 res = GNUNET_strdup (src); 50 res = GNUNET_strdup(src);
51 GNUNET_STRINGS_utf8_tolower (src, res); 51 GNUNET_STRINGS_utf8_tolower(src, res);
52 return res; 52 return res;
53} 53}
54 54
@@ -62,19 +62,19 @@ GNUNET_GNSRECORD_string_to_lowercase (const char *src)
62 * @return string form; will be overwritten by next call to #GNUNET_GNSRECORD_z2s 62 * @return string form; will be overwritten by next call to #GNUNET_GNSRECORD_z2s
63 */ 63 */
64const char * 64const char *
65GNUNET_GNSRECORD_z2s (const struct GNUNET_CRYPTO_EcdsaPublicKey *z) 65GNUNET_GNSRECORD_z2s(const struct GNUNET_CRYPTO_EcdsaPublicKey *z)
66{ 66{
67 static char buf[sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) * 8]; 67 static char buf[sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) * 8];
68 char *end; 68 char *end;
69 69
70 end = GNUNET_STRINGS_data_to_string ((const unsigned char *) z, 70 end = GNUNET_STRINGS_data_to_string((const unsigned char *)z,
71 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey), 71 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
72 buf, sizeof (buf)); 72 buf, sizeof(buf));
73 if (NULL == end) 73 if (NULL == end)
74 { 74 {
75 GNUNET_break (0); 75 GNUNET_break(0);
76 return NULL; 76 return NULL;
77 } 77 }
78 *end = '\0'; 78 *end = '\0';
79 return buf; 79 return buf;
80} 80}
@@ -90,51 +90,51 @@ GNUNET_GNSRECORD_z2s (const struct GNUNET_CRYPTO_EcdsaPublicKey *z)
90 * @return #GNUNET_YES if the records are equal or #GNUNET_NO if they are not 90 * @return #GNUNET_YES if the records are equal or #GNUNET_NO if they are not
91 */ 91 */
92int 92int
93GNUNET_GNSRECORD_records_cmp (const struct GNUNET_GNSRECORD_Data *a, 93GNUNET_GNSRECORD_records_cmp(const struct GNUNET_GNSRECORD_Data *a,
94 const struct GNUNET_GNSRECORD_Data *b) 94 const struct GNUNET_GNSRECORD_Data *b)
95{ 95{
96 LOG (GNUNET_ERROR_TYPE_DEBUG, 96 LOG(GNUNET_ERROR_TYPE_DEBUG,
97 "Comparing records\n"); 97 "Comparing records\n");
98 if (a->record_type != b->record_type) 98 if (a->record_type != b->record_type)
99 { 99 {
100 LOG (GNUNET_ERROR_TYPE_DEBUG, 100 LOG(GNUNET_ERROR_TYPE_DEBUG,
101 "Record type %lu != %lu\n", a->record_type, b->record_type); 101 "Record type %lu != %lu\n", a->record_type, b->record_type);
102 return GNUNET_NO; 102 return GNUNET_NO;
103 } 103 }
104 if ((a->expiration_time != b->expiration_time) && 104 if ((a->expiration_time != b->expiration_time) &&
105 ((a->expiration_time != 0) && (b->expiration_time != 0))) 105 ((a->expiration_time != 0) && (b->expiration_time != 0)))
106 { 106 {
107 LOG (GNUNET_ERROR_TYPE_DEBUG, 107 LOG(GNUNET_ERROR_TYPE_DEBUG,
108 "Expiration time %llu != %llu\n", 108 "Expiration time %llu != %llu\n",
109 a->expiration_time, 109 a->expiration_time,
110 b->expiration_time); 110 b->expiration_time);
111 return GNUNET_NO; 111 return GNUNET_NO;
112 } 112 }
113 if ((a->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS) 113 if ((a->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS)
114 != (b->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS)) 114 != (b->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS))
115 { 115 {
116 LOG (GNUNET_ERROR_TYPE_DEBUG, 116 LOG(GNUNET_ERROR_TYPE_DEBUG,
117 "Flags %lu (%lu) != %lu (%lu)\n", a->flags, 117 "Flags %lu (%lu) != %lu (%lu)\n", a->flags,
118 a->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS, b->flags, 118 a->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS, b->flags,
119 b->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS); 119 b->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS);
120 return GNUNET_NO; 120 return GNUNET_NO;
121 } 121 }
122 if (a->data_size != b->data_size) 122 if (a->data_size != b->data_size)
123 { 123 {
124 LOG (GNUNET_ERROR_TYPE_DEBUG, 124 LOG(GNUNET_ERROR_TYPE_DEBUG,
125 "Data size %lu != %lu\n", 125 "Data size %lu != %lu\n",
126 a->data_size, 126 a->data_size,
127 b->data_size); 127 b->data_size);
128 return GNUNET_NO; 128 return GNUNET_NO;
129 } 129 }
130 if (0 != memcmp (a->data, b->data, a->data_size)) 130 if (0 != memcmp(a->data, b->data, a->data_size))
131 { 131 {
132 LOG (GNUNET_ERROR_TYPE_DEBUG, 132 LOG(GNUNET_ERROR_TYPE_DEBUG,
133 "Data contents do not match\n"); 133 "Data contents do not match\n");
134 return GNUNET_NO; 134 return GNUNET_NO;
135 } 135 }
136 LOG (GNUNET_ERROR_TYPE_DEBUG, 136 LOG(GNUNET_ERROR_TYPE_DEBUG,
137 "Records are equal\n"); 137 "Records are equal\n");
138 return GNUNET_YES; 138 return GNUNET_YES;
139} 139}
140 140
@@ -149,8 +149,8 @@ GNUNET_GNSRECORD_records_cmp (const struct GNUNET_GNSRECORD_Data *a,
149 * @return absolute expiration time 149 * @return absolute expiration time
150 */ 150 */
151struct GNUNET_TIME_Absolute 151struct GNUNET_TIME_Absolute
152GNUNET_GNSRECORD_record_get_expiration_time (unsigned int rd_count, 152GNUNET_GNSRECORD_record_get_expiration_time(unsigned int rd_count,
153 const struct GNUNET_GNSRECORD_Data *rd) 153 const struct GNUNET_GNSRECORD_Data *rd)
154{ 154{
155 struct GNUNET_TIME_Absolute expire; 155 struct GNUNET_TIME_Absolute expire;
156 struct GNUNET_TIME_Absolute at; 156 struct GNUNET_TIME_Absolute at;
@@ -162,44 +162,44 @@ GNUNET_GNSRECORD_record_get_expiration_time (unsigned int rd_count,
162 return GNUNET_TIME_UNIT_ZERO_ABS; 162 return GNUNET_TIME_UNIT_ZERO_ABS;
163 expire = GNUNET_TIME_UNIT_FOREVER_ABS; 163 expire = GNUNET_TIME_UNIT_FOREVER_ABS;
164 for (unsigned int c = 0; c < rd_count; c++) 164 for (unsigned int c = 0; c < rd_count; c++)
165 {
166 if (0 != (rd[c].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
167 { 165 {
168 rt.rel_value_us = rd[c].expiration_time; 166 if (0 != (rd[c].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
169 at = GNUNET_TIME_relative_to_absolute (rt); 167 {
170 } 168 rt.rel_value_us = rd[c].expiration_time;
171 else 169 at = GNUNET_TIME_relative_to_absolute(rt);
172 { 170 }
173 at.abs_value_us = rd[c].expiration_time;
174 }
175
176 for (unsigned int c2 = 0; c2 < rd_count; c2++)
177 {
178 /* Check for shadow record */
179 if ( (c == c2) ||
180 (rd[c].record_type != rd[c2].record_type) ||
181 (0 == (rd[c2].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) )
182 continue;
183 /* We have a shadow record */
184 if (0 != (rd[c2].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
185 {
186 rt_shadow.rel_value_us = rd[c2].expiration_time;
187 at_shadow = GNUNET_TIME_relative_to_absolute (rt_shadow);
188 }
189 else 171 else
190 { 172 {
191 at_shadow.abs_value_us = rd[c2].expiration_time; 173 at.abs_value_us = rd[c].expiration_time;
192 } 174 }
193 at = GNUNET_TIME_absolute_max (at, 175
194 at_shadow); 176 for (unsigned int c2 = 0; c2 < rd_count; c2++)
177 {
178 /* Check for shadow record */
179 if ((c == c2) ||
180 (rd[c].record_type != rd[c2].record_type) ||
181 (0 == (rd[c2].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)))
182 continue;
183 /* We have a shadow record */
184 if (0 != (rd[c2].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
185 {
186 rt_shadow.rel_value_us = rd[c2].expiration_time;
187 at_shadow = GNUNET_TIME_relative_to_absolute(rt_shadow);
188 }
189 else
190 {
191 at_shadow.abs_value_us = rd[c2].expiration_time;
192 }
193 at = GNUNET_TIME_absolute_max(at,
194 at_shadow);
195 }
196 expire = GNUNET_TIME_absolute_min(at,
197 expire);
195 } 198 }
196 expire = GNUNET_TIME_absolute_min (at, 199 LOG(GNUNET_ERROR_TYPE_DEBUG,
197 expire); 200 "Determined expiration time for block with %u records to be %s\n",
198 } 201 rd_count,
199 LOG (GNUNET_ERROR_TYPE_DEBUG, 202 GNUNET_STRINGS_absolute_time_to_string(expire));
200 "Determined expiration time for block with %u records to be %s\n",
201 rd_count,
202 GNUNET_STRINGS_absolute_time_to_string (expire));
203 return expire; 203 return expire;
204} 204}
205 205
@@ -211,14 +211,14 @@ GNUNET_GNSRECORD_record_get_expiration_time (unsigned int rd_count,
211 * #GNUNET_NO if not 211 * #GNUNET_NO if not
212 */ 212 */
213int 213int
214GNUNET_GNSRECORD_is_expired (const struct GNUNET_GNSRECORD_Data *rd) 214GNUNET_GNSRECORD_is_expired(const struct GNUNET_GNSRECORD_Data *rd)
215{ 215{
216 struct GNUNET_TIME_Absolute at; 216 struct GNUNET_TIME_Absolute at;
217 217
218 if (0 != (rd->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) 218 if (0 != (rd->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
219 return GNUNET_NO; 219 return GNUNET_NO;
220 at.abs_value_us = rd->expiration_time; 220 at.abs_value_us = rd->expiration_time;
221 return (0 == GNUNET_TIME_absolute_get_remaining (at).rel_value_us) ? GNUNET_YES : GNUNET_NO; 221 return (0 == GNUNET_TIME_absolute_get_remaining(at).rel_value_us) ? GNUNET_YES : GNUNET_NO;
222} 222}
223 223
224 224
@@ -233,17 +233,17 @@ GNUNET_GNSRECORD_is_expired (const struct GNUNET_GNSRECORD_Data *rd)
233 * key in an encoding suitable for DNS labels. 233 * key in an encoding suitable for DNS labels.
234 */ 234 */
235const char * 235const char *
236GNUNET_GNSRECORD_pkey_to_zkey (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey) 236GNUNET_GNSRECORD_pkey_to_zkey(const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
237{ 237{
238 static char ret[128]; 238 static char ret[128];
239 char *pkeys; 239 char *pkeys;
240 240
241 pkeys = GNUNET_CRYPTO_ecdsa_public_key_to_string (pkey); 241 pkeys = GNUNET_CRYPTO_ecdsa_public_key_to_string(pkey);
242 GNUNET_snprintf (ret, 242 GNUNET_snprintf(ret,
243 sizeof (ret), 243 sizeof(ret),
244 "%s", 244 "%s",
245 pkeys); 245 pkeys);
246 GNUNET_free (pkeys); 246 GNUNET_free(pkeys);
247 return ret; 247 return ret;
248} 248}
249 249
@@ -258,13 +258,13 @@ GNUNET_GNSRECORD_pkey_to_zkey (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
258 * @return #GNUNET_SYSERR if @a zkey has the wrong syntax 258 * @return #GNUNET_SYSERR if @a zkey has the wrong syntax
259 */ 259 */
260int 260int
261GNUNET_GNSRECORD_zkey_to_pkey (const char *zkey, 261GNUNET_GNSRECORD_zkey_to_pkey(const char *zkey,
262 struct GNUNET_CRYPTO_EcdsaPublicKey *pkey) 262 struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
263{ 263{
264 if (GNUNET_OK != 264 if (GNUNET_OK !=
265 GNUNET_CRYPTO_ecdsa_public_key_from_string (zkey, 265 GNUNET_CRYPTO_ecdsa_public_key_from_string(zkey,
266 strlen (zkey), 266 strlen(zkey),
267 pkey)) 267 pkey))
268 return GNUNET_SYSERR; 268 return GNUNET_SYSERR;
269 return GNUNET_OK; 269 return GNUNET_OK;
270} 270}