resolved: rr - print formated timestamps in RRSIG
[thirdparty/systemd.git] / src / resolve / resolved-dns-rr.c
CommitLineData
74b2466e
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2014 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
0dae31d4
ZJS
22#include <math.h>
23
2e276efc
ZJS
24#include "strv.h"
25
4ad7f276 26#include "dns-domain.h"
74b2466e 27#include "resolved-dns-rr.h"
8db0d2f5 28#include "resolved-dns-packet.h"
7263f724 29#include "dns-type.h"
74b2466e 30
faa133f3
LP
31DnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name) {
32 DnsResourceKey *k;
33 size_t l;
74b2466e 34
faa133f3
LP
35 assert(name);
36
37 l = strlen(name);
38 k = malloc0(sizeof(DnsResourceKey) + l + 1);
39 if (!k)
40 return NULL;
41
42 k->n_ref = 1;
43 k->class = class;
44 k->type = type;
45
46 strcpy((char*) k + sizeof(DnsResourceKey), name);
47
48 return k;
49}
50
51DnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name) {
52 DnsResourceKey *k;
53
54 assert(name);
55
56 k = new0(DnsResourceKey, 1);
57 if (!k)
58 return NULL;
59
60 k->n_ref = 1;
61 k->class = class;
62 k->type = type;
63 k->_name = name;
64
65 return k;
66}
67
68DnsResourceKey* dns_resource_key_ref(DnsResourceKey *k) {
69
70 if (!k)
71 return NULL;
72
73 assert(k->n_ref > 0);
74 k->n_ref++;
75
76 return k;
77}
78
79DnsResourceKey* dns_resource_key_unref(DnsResourceKey *k) {
80 if (!k)
81 return NULL;
82
83 assert(k->n_ref > 0);
84
85 if (k->n_ref == 1) {
86 free(k->_name);
87 free(k);
88 } else
89 k->n_ref--;
90
91 return NULL;
92}
93
94int dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b) {
95 int r;
96
97 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(a), DNS_RESOURCE_KEY_NAME(b));
98 if (r <= 0)
99 return r;
100
101 if (a->class != b->class)
102 return 0;
103
104 if (a->type != b->type)
105 return 0;
106
107 return 1;
108}
109
110int dns_resource_key_match_rr(const DnsResourceKey *key, const DnsResourceRecord *rr) {
111 assert(key);
112 assert(rr);
113
114 if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
115 return 0;
116
117 if (rr->key->type != key->type && key->type != DNS_TYPE_ANY)
118 return 0;
119
120 return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
121}
122
123int dns_resource_key_match_cname(const DnsResourceKey *key, const DnsResourceRecord *rr) {
124 assert(key);
125 assert(rr);
126
127 if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
128 return 0;
129
130 if (rr->key->type != DNS_TYPE_CNAME)
131 return 0;
132
133 return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
74b2466e
LP
134}
135
d5099efc 136static unsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]) {
322345fd
LP
137 const DnsResourceKey *k = i;
138 unsigned long ul;
139
faa133f3 140 ul = dns_name_hash_func(DNS_RESOURCE_KEY_NAME(k), hash_key);
322345fd
LP
141 ul = ul * hash_key[0] + ul + k->class;
142 ul = ul * hash_key[1] + ul + k->type;
143
144 return ul;
145}
146
d5099efc 147static int dns_resource_key_compare_func(const void *a, const void *b) {
322345fd
LP
148 const DnsResourceKey *x = a, *y = b;
149 int ret;
150
faa133f3 151 ret = dns_name_compare_func(DNS_RESOURCE_KEY_NAME(x), DNS_RESOURCE_KEY_NAME(y));
322345fd
LP
152 if (ret != 0)
153 return ret;
154
155 if (x->type < y->type)
156 return -1;
157 if (x->type > y->type)
158 return 1;
159
160 if (x->class < y->class)
161 return -1;
162 if (x->class > y->class)
163 return 1;
164
165 return 0;
166}
167
d5099efc
MS
168const struct hash_ops dns_resource_key_hash_ops = {
169 .hash = dns_resource_key_hash_func,
170 .compare = dns_resource_key_compare_func
171};
172
2d4c5cbc
LP
173int dns_resource_key_to_string(const DnsResourceKey *key, char **ret) {
174 char cbuf[DECIMAL_STR_MAX(uint16_t)], tbuf[DECIMAL_STR_MAX(uint16_t)];
175 const char *c, *t;
176 char *s;
177
178 c = dns_class_to_string(key->class);
179 if (!c) {
180 sprintf(cbuf, "%i", key->class);
181 c = cbuf;
182 }
183
184 t = dns_type_to_string(key->type);
185 if (!t){
186 sprintf(tbuf, "%i", key->type);
187 t = tbuf;
188 }
189
23432a1c 190 if (asprintf(&s, "%s %s %-5s", DNS_RESOURCE_KEY_NAME(key), c, t) < 0)
2d4c5cbc
LP
191 return -ENOMEM;
192
193 *ret = s;
194 return 0;
195}
196
faa133f3 197DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
74b2466e
LP
198 DnsResourceRecord *rr;
199
200 rr = new0(DnsResourceRecord, 1);
201 if (!rr)
202 return NULL;
203
204 rr->n_ref = 1;
faa133f3
LP
205 rr->key = dns_resource_key_ref(key);
206
74b2466e
LP
207 return rr;
208}
209
8bf52d3d
LP
210DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name) {
211 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
212
213 key = dns_resource_key_new(class, type, name);
214 if (!key)
215 return NULL;
216
217 return dns_resource_record_new(key);
218}
219
74b2466e
LP
220DnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr) {
221 if (!rr)
222 return NULL;
223
224 assert(rr->n_ref > 0);
225 rr->n_ref++;
226
227 return rr;
228}
229
230DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) {
231 if (!rr)
232 return NULL;
233
234 assert(rr->n_ref > 0);
235
236 if (rr->n_ref > 1) {
237 rr->n_ref--;
238 return NULL;
239 }
240
faa133f3 241 if (rr->key) {
9de3e329 242 switch(rr->key->type) {
9c92ce6d
LP
243
244 case DNS_TYPE_SRV:
245 free(rr->srv.name);
246 break;
247
9de3e329
ZJS
248 case DNS_TYPE_PTR:
249 case DNS_TYPE_NS:
250 case DNS_TYPE_CNAME:
8ac4e9e1 251 case DNS_TYPE_DNAME:
faa133f3 252 free(rr->ptr.name);
9de3e329 253 break;
9c92ce6d 254
9de3e329 255 case DNS_TYPE_HINFO:
faa133f3
LP
256 free(rr->hinfo.cpu);
257 free(rr->hinfo.os);
9de3e329 258 break;
9c92ce6d 259
9de3e329 260 case DNS_TYPE_TXT:
9c92ce6d 261 case DNS_TYPE_SPF:
2e276efc 262 strv_free(rr->txt.strings);
9de3e329 263 break;
9c92ce6d 264
9de3e329 265 case DNS_TYPE_SOA:
7e8e0422
LP
266 free(rr->soa.mname);
267 free(rr->soa.rname);
9de3e329 268 break;
9c92ce6d 269
9de3e329 270 case DNS_TYPE_MX:
946c7094 271 free(rr->mx.exchange);
9de3e329 272 break;
9c92ce6d 273
42cc2eeb
LP
274 case DNS_TYPE_SSHFP:
275 free(rr->sshfp.key);
276 break;
277
8db0d2f5
ZJS
278 case DNS_TYPE_DNSKEY:
279 free(rr->dnskey.key);
280 break;
281
151226ab
ZJS
282 case DNS_TYPE_RRSIG:
283 free(rr->rrsig.signer);
284 free(rr->rrsig.signature);
285 break;
286
0dae31d4 287 case DNS_TYPE_LOC:
9de3e329
ZJS
288 case DNS_TYPE_A:
289 case DNS_TYPE_AAAA:
290 break;
9c92ce6d 291
9de3e329 292 default:
faa133f3 293 free(rr->generic.data);
9de3e329 294 }
322345fd 295
faa133f3
LP
296 dns_resource_key_unref(rr->key);
297 }
322345fd 298
faa133f3 299 free(rr);
322345fd 300
322345fd
LP
301 return NULL;
302}
303
623a4c97
LP
304int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *hostname) {
305 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
306 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
307 _cleanup_free_ char *ptr = NULL;
308 int r;
309
310 assert(ret);
311 assert(address);
312 assert(hostname);
313
314 r = dns_name_reverse(family, address, &ptr);
315 if (r < 0)
316 return r;
317
318 key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, ptr);
319 if (!key)
320 return -ENOMEM;
321
322 ptr = NULL;
323
324 rr = dns_resource_record_new(key);
325 if (!rr)
326 return -ENOMEM;
327
328 rr->ptr.name = strdup(hostname);
329 if (!rr->ptr.name)
330 return -ENOMEM;
331
332 *ret = rr;
333 rr = NULL;
334
335 return 0;
336}
337
322345fd
LP
338int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
339 int r;
340
341 assert(a);
342 assert(b);
343
faa133f3 344 r = dns_resource_key_equal(a->key, b->key);
322345fd
LP
345 if (r <= 0)
346 return r;
347
fd0b4602
LP
348 if (a->unparseable != b->unparseable)
349 return 0;
350
351 switch (a->unparseable ? _DNS_TYPE_INVALID : a->key->type) {
2d4c5cbc 352
9c92ce6d
LP
353 case DNS_TYPE_SRV:
354 r = dns_name_equal(a->srv.name, b->srv.name);
355 if (r <= 0)
356 return r;
357
358 return a->srv.priority == b->srv.priority &&
359 a->srv.weight == b->srv.weight &&
360 a->srv.port == b->srv.port;
361
2d4c5cbc
LP
362 case DNS_TYPE_PTR:
363 case DNS_TYPE_NS:
364 case DNS_TYPE_CNAME:
8ac4e9e1 365 case DNS_TYPE_DNAME:
322345fd 366 return dns_name_equal(a->ptr.name, b->ptr.name);
2d4c5cbc
LP
367
368 case DNS_TYPE_HINFO:
369 return strcaseeq(a->hinfo.cpu, b->hinfo.cpu) &&
370 strcaseeq(a->hinfo.os, b->hinfo.os);
371
9de3e329 372 case DNS_TYPE_SPF: /* exactly the same as TXT */
0f84a72e
DH
373 case DNS_TYPE_TXT:
374 return strv_equal(a->txt.strings, b->txt.strings);
2e276efc 375
2d4c5cbc 376 case DNS_TYPE_A:
322345fd 377 return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
2d4c5cbc
LP
378
379 case DNS_TYPE_AAAA:
322345fd 380 return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
2d4c5cbc
LP
381
382 case DNS_TYPE_SOA:
7e8e0422
LP
383 r = dns_name_equal(a->soa.mname, b->soa.mname);
384 if (r <= 0)
385 return r;
386 r = dns_name_equal(a->soa.rname, b->soa.rname);
387 if (r <= 0)
388 return r;
389
390 return a->soa.serial == b->soa.serial &&
391 a->soa.refresh == b->soa.refresh &&
392 a->soa.retry == b->soa.retry &&
393 a->soa.expire == b->soa.expire &&
394 a->soa.minimum == b->soa.minimum;
9c92ce6d 395
946c7094
ZJS
396 case DNS_TYPE_MX:
397 if (a->mx.priority != b->mx.priority)
398 return 0;
399
400 return dns_name_equal(a->mx.exchange, b->mx.exchange);
401
0dae31d4
ZJS
402 case DNS_TYPE_LOC:
403 assert(a->loc.version == b->loc.version);
404
405 return a->loc.size == b->loc.size &&
406 a->loc.horiz_pre == b->loc.horiz_pre &&
407 a->loc.vert_pre == b->loc.vert_pre &&
408 a->loc.latitude == b->loc.latitude &&
409 a->loc.longitude == b->loc.longitude &&
410 a->loc.altitude == b->loc.altitude;
411
42cc2eeb
LP
412 case DNS_TYPE_SSHFP:
413 return a->sshfp.algorithm == b->sshfp.algorithm &&
414 a->sshfp.fptype == b->sshfp.fptype &&
415 a->sshfp.key_size == b->sshfp.key_size &&
416 memcmp(a->sshfp.key, b->sshfp.key, a->sshfp.key_size) == 0;
417
8db0d2f5
ZJS
418 case DNS_TYPE_DNSKEY:
419 return a->dnskey.zone_key_flag == b->dnskey.zone_key_flag &&
420 a->dnskey.sep_flag == b->dnskey.sep_flag &&
421 a->dnskey.algorithm == b->dnskey.algorithm &&
422 a->dnskey.key_size == b->dnskey.key_size &&
423 memcmp(a->dnskey.key, b->dnskey.key, a->dnskey.key_size) == 0;
424
151226ab
ZJS
425 case DNS_TYPE_RRSIG:
426 /* do the fast comparisons first */
03664a62
LN
427 if (a->rrsig.type_covered != b->rrsig.type_covered ||
428 a->rrsig.algorithm != b->rrsig.algorithm ||
429 a->rrsig.labels != b->rrsig.labels ||
430 a->rrsig.original_ttl != b->rrsig.original_ttl ||
431 a->rrsig.expiration != b->rrsig.expiration ||
432 a->rrsig.inception != b->rrsig.inception ||
433 a->rrsig.key_tag != b->rrsig.key_tag ||
151226ab
ZJS
434 a->rrsig.signature_size != b->rrsig.signature_size ||
435 memcmp(a->rrsig.signature, b->rrsig.signature, a->rrsig.signature_size) != 0)
436 return false;
437
438 return dns_name_equal(a->rrsig.signer, b->rrsig.signer);
439
2d4c5cbc 440 default:
322345fd
LP
441 return a->generic.size == b->generic.size &&
442 memcmp(a->generic.data, b->generic.data, a->generic.size) == 0;
2d4c5cbc 443 }
322345fd
LP
444}
445
0dae31d4
ZJS
446static char* format_location(uint32_t latitude, uint32_t longitude, uint32_t altitude,
447 uint8_t size, uint8_t horiz_pre, uint8_t vert_pre) {
448 char *s;
449 char NS = latitude >= 1U<<31 ? 'N' : 'S';
450 char EW = longitude >= 1U<<31 ? 'E' : 'W';
451
452 int lat = latitude >= 1U<<31 ? (int) (latitude - (1U<<31)) : (int) ((1U<<31) - latitude);
453 int lon = longitude >= 1U<<31 ? (int) (longitude - (1U<<31)) : (int) ((1U<<31) - longitude);
454 double alt = altitude >= 10000000u ? altitude - 10000000u : -(double)(10000000u - altitude);
455 double siz = (size >> 4) * exp10((double) (size & 0xF));
456 double hor = (horiz_pre >> 4) * exp10((double) (horiz_pre & 0xF));
457 double ver = (vert_pre >> 4) * exp10((double) (vert_pre & 0xF));
458
459 if (asprintf(&s, "%d %d %.3f %c %d %d %.3f %c %.2fm %.2fm %.2fm %.2fm",
460 (lat / 60000 / 60),
461 (lat / 60000) % 60,
462 (lat % 60000) / 1000.,
463 NS,
464 (lon / 60000 / 60),
465 (lon / 60000) % 60,
466 (lon % 60000) / 1000.,
467 EW,
468 alt / 100.,
469 siz / 100.,
470 hor / 100.,
471 ver / 100.) < 0)
472 return NULL;
473
474 return s;
475}
476
7c6423e1
TG
477static int format_timestamp_dns(char *buf, size_t l, time_t sec) {
478 struct tm tm;
479
480 assert(buf);
481 assert(l > strlen("YYYYMMDDHHmmSS"));
482
483 if (!gmtime_r(&sec, &tm))
484 return -EINVAL;
485
486 if (strftime(buf, l, "%Y%m%d%H%M%S", &tm) <= 0)
487 return -EINVAL;
488
489 return 0;
490}
491
2d4c5cbc 492int dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret) {
8db0d2f5 493 _cleanup_free_ char *k = NULL, *t = NULL;
2d4c5cbc
LP
494 char *s;
495 int r;
322345fd 496
2d4c5cbc 497 assert(rr);
322345fd 498
2d4c5cbc
LP
499 r = dns_resource_key_to_string(rr->key, &k);
500 if (r < 0)
501 return r;
322345fd 502
0dae31d4 503 switch (rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
322345fd 504
9c92ce6d
LP
505 case DNS_TYPE_SRV:
506 r = asprintf(&s, "%s %u %u %u %s",
507 k,
508 rr->srv.priority,
509 rr->srv.weight,
510 rr->srv.port,
511 strna(rr->srv.name));
512 if (r < 0)
513 return -ENOMEM;
514 break;
515
2d4c5cbc
LP
516 case DNS_TYPE_PTR:
517 case DNS_TYPE_NS:
518 case DNS_TYPE_CNAME:
8ac4e9e1 519 case DNS_TYPE_DNAME:
2d4c5cbc
LP
520 s = strjoin(k, " ", rr->ptr.name, NULL);
521 if (!s)
522 return -ENOMEM;
322345fd 523
2d4c5cbc 524 break;
322345fd 525
2d4c5cbc
LP
526 case DNS_TYPE_HINFO:
527 s = strjoin(k, " ", rr->hinfo.cpu, " ", rr->hinfo.os, NULL);
528 if (!s)
529 return -ENOMEM;
530 break;
322345fd 531
9de3e329 532 case DNS_TYPE_SPF: /* exactly the same as TXT */
8db0d2f5 533 case DNS_TYPE_TXT:
2e276efc
ZJS
534 t = strv_join_quoted(rr->txt.strings);
535 if (!t)
536 return -ENOMEM;
537
538 s = strjoin(k, " ", t, NULL);
539 if (!s)
540 return -ENOMEM;
541
542 break;
2e276efc 543
2d4c5cbc
LP
544 case DNS_TYPE_A: {
545 _cleanup_free_ char *x = NULL;
322345fd 546
2d4c5cbc
LP
547 r = in_addr_to_string(AF_INET, (const union in_addr_union*) &rr->a.in_addr, &x);
548 if (r < 0)
549 return r;
322345fd 550
2d4c5cbc
LP
551 s = strjoin(k, " ", x, NULL);
552 if (!s)
553 return -ENOMEM;
554 break;
555 }
322345fd 556
8db0d2f5
ZJS
557 case DNS_TYPE_AAAA:
558 r = in_addr_to_string(AF_INET6, (const union in_addr_union*) &rr->aaaa.in6_addr, &t);
2d4c5cbc
LP
559 if (r < 0)
560 return r;
322345fd 561
8db0d2f5 562 s = strjoin(k, " ", t, NULL);
2d4c5cbc
LP
563 if (!s)
564 return -ENOMEM;
565 break;
322345fd 566
2d4c5cbc
LP
567 case DNS_TYPE_SOA:
568 r = asprintf(&s, "%s %s %s %u %u %u %u %u",
569 k,
570 strna(rr->soa.mname),
571 strna(rr->soa.rname),
572 rr->soa.serial,
573 rr->soa.refresh,
574 rr->soa.retry,
575 rr->soa.expire,
576 rr->soa.minimum);
577 if (r < 0)
578 return -ENOMEM;
579 break;
580
946c7094
ZJS
581 case DNS_TYPE_MX:
582 r = asprintf(&s, "%s %u %s",
583 k,
584 rr->mx.priority,
585 rr->mx.exchange);
586 if (r < 0)
587 return -ENOMEM;
588 break;
589
8db0d2f5 590 case DNS_TYPE_LOC:
0dae31d4
ZJS
591 assert(rr->loc.version == 0);
592
8db0d2f5
ZJS
593 t = format_location(rr->loc.latitude,
594 rr->loc.longitude,
595 rr->loc.altitude,
596 rr->loc.size,
597 rr->loc.horiz_pre,
598 rr->loc.vert_pre);
599 if (!t)
0dae31d4
ZJS
600 return -ENOMEM;
601
8db0d2f5 602 s = strjoin(k, " ", t, NULL);
0dae31d4
ZJS
603 if (!s)
604 return -ENOMEM;
0dae31d4 605 break;
0dae31d4 606
8db0d2f5
ZJS
607 case DNS_TYPE_SSHFP:
608 t = hexmem(rr->sshfp.key, rr->sshfp.key_size);
609 if (!t)
42cc2eeb
LP
610 return -ENOMEM;
611
612 r = asprintf(&s, "%s %u %u %s",
613 k,
614 rr->sshfp.algorithm,
615 rr->sshfp.fptype,
8db0d2f5 616 t);
42cc2eeb
LP
617 if (r < 0)
618 return -ENOMEM;
619 break;
42cc2eeb 620
ff3d6560
ZJS
621 case DNS_TYPE_DNSKEY: {
622 const char *alg;
623
624 alg = dnssec_algorithm_to_string(rr->dnskey.algorithm);
625
1bf968f3 626 t = base64mem(rr->dnskey.key, rr->dnskey.key_size);
8db0d2f5
ZJS
627 if (!t)
628 return -ENOMEM;
2d4c5cbc 629
ff3d6560 630 r = asprintf(&s, "%s %u 3 %.*s%.*u %s",
8db0d2f5
ZJS
631 k,
632 dnskey_to_flags(rr),
ff3d6560
ZJS
633 alg ? -1 : 0, alg,
634 alg ? 0 : 1, alg ? 0u : (unsigned) rr->dnskey.algorithm,
8db0d2f5
ZJS
635 t);
636 if (r < 0)
2d4c5cbc 637 return -ENOMEM;
8db0d2f5 638 break;
ff3d6560 639 }
2d4c5cbc 640
151226ab
ZJS
641 case DNS_TYPE_RRSIG: {
642 const char *type, *alg;
7c6423e1 643 char expiration[strlen("YYYYMMDDHHmmSS") + 1], inception[strlen("YYYYMMDDHHmmSS") + 1];
151226ab
ZJS
644
645 type = dns_type_to_string(rr->rrsig.type_covered);
646 alg = dnssec_algorithm_to_string(rr->rrsig.algorithm);
647
1bf968f3 648 t = base64mem(rr->rrsig.signature, rr->rrsig.signature_size);
151226ab
ZJS
649 if (!t)
650 return -ENOMEM;
651
7c6423e1
TG
652 r = format_timestamp_dns(expiration, sizeof(expiration), rr->rrsig.expiration);
653 if (r < 0)
654 return r;
655
656 r = format_timestamp_dns(inception, sizeof(inception), rr->rrsig.inception);
657 if (r < 0)
658 return r;
659
151226ab
ZJS
660 /* TYPE?? follows
661 * http://tools.ietf.org/html/rfc3597#section-5 */
662
7c6423e1 663 r = asprintf(&s, "%s %s%.*u %.*s%.*u %u %u %s %s %u %s %s",
151226ab
ZJS
664 k,
665 type ?: "TYPE",
666 type ? 0 : 1, type ? 0u : (unsigned) rr->rrsig.type_covered,
667 alg ? -1 : 0, alg,
668 alg ? 0 : 1, alg ? 0u : (unsigned) rr->rrsig.algorithm,
669 rr->rrsig.labels,
670 rr->rrsig.original_ttl,
7c6423e1
TG
671 expiration,
672 inception,
151226ab
ZJS
673 rr->rrsig.key_tag,
674 rr->rrsig.signer,
675 t);
676 if (r < 0)
677 return -ENOMEM;
678 break;
679 }
680
8db0d2f5
ZJS
681 default:
682 t = hexmem(rr->generic.data, rr->generic.size);
683 if (!t)
684 return -ENOMEM;
685
686 s = strjoin(k, " ", t, NULL);
2d4c5cbc
LP
687 if (!s)
688 return -ENOMEM;
689 break;
8db0d2f5 690 }
2d4c5cbc
LP
691
692 *ret = s;
693 return 0;
694}
322345fd 695
2d4c5cbc 696const char *dns_class_to_string(uint16_t class) {
322345fd 697
2d4c5cbc 698 switch (class) {
322345fd 699
2d4c5cbc
LP
700 case DNS_CLASS_IN:
701 return "IN";
322345fd 702
2d4c5cbc
LP
703 case DNS_CLASS_ANY:
704 return "ANY";
705 }
322345fd 706
2d4c5cbc
LP
707 return NULL;
708}
322345fd 709
2d4c5cbc
LP
710int dns_class_from_string(const char *s, uint16_t *class) {
711 assert(s);
712 assert(class);
713
714 if (strcaseeq(s, "IN"))
715 *class = DNS_CLASS_IN;
716 else if (strcaseeq(s, "ANY"))
717 *class = DNS_TYPE_ANY;
718 else
719 return -EINVAL;
322345fd 720
2d4c5cbc
LP
721 return 0;
722}