+
+int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *hostname) {
+ _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
+ _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
+ _cleanup_free_ char *ptr = NULL;
+ int r;
+
+ assert(ret);
+ assert(address);
+ assert(hostname);
+
+ r = dns_name_reverse(family, address, &ptr);
+ if (r < 0)
+ return r;
+
+ key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, ptr);
+ if (!key)
+ return -ENOMEM;
+
+ ptr = NULL;
+
+ rr = dns_resource_record_new(key);
+ if (!rr)
+ return -ENOMEM;
+
+ rr->ptr.name = strdup(hostname);
+ if (!rr->ptr.name)
+ return -ENOMEM;
+
+ *ret = rr;
+ rr = NULL;
+
+ return 0;
+}
+
+int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
+ int r;
+
+ assert(a);
+ assert(b);
+
+ r = dns_resource_key_equal(a->key, b->key);
+ if (r <= 0)
+ return r;
+
+ switch (a->key->type) {
+
+ case DNS_TYPE_SRV:
+ r = dns_name_equal(a->srv.name, b->srv.name);
+ if (r <= 0)
+ return r;
+
+ return a->srv.priority == b->srv.priority &&
+ a->srv.weight == b->srv.weight &&
+ a->srv.port == b->srv.port;
+
+ case DNS_TYPE_PTR:
+ case DNS_TYPE_NS:
+ case DNS_TYPE_CNAME:
+ case DNS_TYPE_DNAME:
+ return dns_name_equal(a->ptr.name, b->ptr.name);
+
+ case DNS_TYPE_HINFO:
+ return strcaseeq(a->hinfo.cpu, b->hinfo.cpu) &&
+ strcaseeq(a->hinfo.os, b->hinfo.os);
+
+ case DNS_TYPE_SPF: /* exactly the same as TXT */
+ case DNS_TYPE_TXT: {
+ int i;
+
+ for (i = 0; a->txt.strings[i] || b->txt.strings[i]; i++)
+ if (!streq_ptr(a->txt.strings[i], b->txt.strings[i]))
+ return false;
+ return true;
+ }
+
+ case DNS_TYPE_A:
+ return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
+
+ case DNS_TYPE_AAAA:
+ return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
+
+ case DNS_TYPE_SOA:
+ r = dns_name_equal(a->soa.mname, b->soa.mname);
+ if (r <= 0)
+ return r;
+ r = dns_name_equal(a->soa.rname, b->soa.rname);
+ if (r <= 0)
+ return r;
+
+ return a->soa.serial == b->soa.serial &&
+ a->soa.refresh == b->soa.refresh &&
+ a->soa.retry == b->soa.retry &&
+ a->soa.expire == b->soa.expire &&
+ a->soa.minimum == b->soa.minimum;
+
+ case DNS_TYPE_MX:
+ if (a->mx.priority != b->mx.priority)
+ return 0;
+
+ return dns_name_equal(a->mx.exchange, b->mx.exchange);
+
+ case DNS_TYPE_LOC:
+ assert(a->loc.version == b->loc.version);
+
+ return a->loc.size == b->loc.size &&
+ a->loc.horiz_pre == b->loc.horiz_pre &&
+ a->loc.vert_pre == b->loc.vert_pre &&
+ a->loc.latitude == b->loc.latitude &&
+ a->loc.longitude == b->loc.longitude &&
+ a->loc.altitude == b->loc.altitude;
+
+ default:
+ return a->generic.size == b->generic.size &&
+ memcmp(a->generic.data, b->generic.data, a->generic.size) == 0;
+ }
+}
+
+static char* format_location(uint32_t latitude, uint32_t longitude, uint32_t altitude,
+ uint8_t size, uint8_t horiz_pre, uint8_t vert_pre) {
+ char *s;
+ char NS = latitude >= 1U<<31 ? 'N' : 'S';
+ char EW = longitude >= 1U<<31 ? 'E' : 'W';
+
+ int lat = latitude >= 1U<<31 ? (int) (latitude - (1U<<31)) : (int) ((1U<<31) - latitude);
+ int lon = longitude >= 1U<<31 ? (int) (longitude - (1U<<31)) : (int) ((1U<<31) - longitude);
+ double alt = altitude >= 10000000u ? altitude - 10000000u : -(double)(10000000u - altitude);
+ double siz = (size >> 4) * exp10((double) (size & 0xF));
+ double hor = (horiz_pre >> 4) * exp10((double) (horiz_pre & 0xF));
+ double ver = (vert_pre >> 4) * exp10((double) (vert_pre & 0xF));
+
+ if (asprintf(&s, "%d %d %.3f %c %d %d %.3f %c %.2fm %.2fm %.2fm %.2fm",
+ (lat / 60000 / 60),
+ (lat / 60000) % 60,
+ (lat % 60000) / 1000.,
+ NS,
+ (lon / 60000 / 60),
+ (lon / 60000) % 60,
+ (lon % 60000) / 1000.,
+ EW,
+ alt / 100.,
+ siz / 100.,
+ hor / 100.,
+ ver / 100.) < 0)
+ return NULL;
+
+ return s;
+}
+
+
+int dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret) {
+ _cleanup_free_ char *k = NULL;
+ char *s;
+ int r;
+
+ assert(rr);
+
+ r = dns_resource_key_to_string(rr->key, &k);
+ if (r < 0)
+ return r;
+
+ switch (rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
+
+ case DNS_TYPE_SRV:
+ r = asprintf(&s, "%s %u %u %u %s",
+ k,
+ rr->srv.priority,
+ rr->srv.weight,
+ rr->srv.port,
+ strna(rr->srv.name));
+ if (r < 0)
+ return -ENOMEM;
+ break;
+
+ case DNS_TYPE_PTR:
+ case DNS_TYPE_NS:
+ case DNS_TYPE_CNAME:
+ case DNS_TYPE_DNAME:
+ s = strjoin(k, " ", rr->ptr.name, NULL);
+ if (!s)
+ return -ENOMEM;
+
+ break;
+
+ case DNS_TYPE_HINFO:
+ s = strjoin(k, " ", rr->hinfo.cpu, " ", rr->hinfo.os, NULL);
+ if (!s)
+ return -ENOMEM;
+ break;
+
+ case DNS_TYPE_SPF: /* exactly the same as TXT */
+ case DNS_TYPE_TXT: {
+ _cleanup_free_ char *t;
+
+ t = strv_join_quoted(rr->txt.strings);
+ if (!t)
+ return -ENOMEM;
+
+ s = strjoin(k, " ", t, NULL);
+ if (!s)
+ return -ENOMEM;
+
+ break;
+ }
+
+ case DNS_TYPE_A: {
+ _cleanup_free_ char *x = NULL;
+
+ r = in_addr_to_string(AF_INET, (const union in_addr_union*) &rr->a.in_addr, &x);
+ if (r < 0)
+ return r;
+
+ s = strjoin(k, " ", x, NULL);
+ if (!s)
+ return -ENOMEM;
+ break;
+ }
+
+ case DNS_TYPE_AAAA: {
+ _cleanup_free_ char *x = NULL;
+
+ r = in_addr_to_string(AF_INET6, (const union in_addr_union*) &rr->aaaa.in6_addr, &x);
+ if (r < 0)
+ return r;
+
+ s = strjoin(k, " ", x, NULL);
+ if (!s)
+ return -ENOMEM;
+ break;
+ }
+
+ case DNS_TYPE_SOA:
+ r = asprintf(&s, "%s %s %s %u %u %u %u %u",
+ k,
+ strna(rr->soa.mname),
+ strna(rr->soa.rname),
+ rr->soa.serial,
+ rr->soa.refresh,
+ rr->soa.retry,
+ rr->soa.expire,
+ rr->soa.minimum);
+ if (r < 0)
+ return -ENOMEM;
+ break;
+
+ case DNS_TYPE_MX:
+ r = asprintf(&s, "%s %u %s",
+ k,
+ rr->mx.priority,
+ rr->mx.exchange);
+ if (r < 0)
+ return -ENOMEM;
+ break;
+
+ case DNS_TYPE_LOC: {
+ _cleanup_free_ char *loc;
+ assert(rr->loc.version == 0);
+
+ loc = format_location(rr->loc.latitude,
+ rr->loc.longitude,
+ rr->loc.altitude,
+ rr->loc.size,
+ rr->loc.horiz_pre,
+ rr->loc.vert_pre);
+ if (!loc)
+ return -ENOMEM;
+
+ s = strjoin(k, " ", loc, NULL);
+ if (!s)
+ return -ENOMEM;
+
+ break;
+ }
+
+ default: {
+ _cleanup_free_ char *x = NULL;
+
+ x = hexmem(rr->generic.data, rr->generic.size);
+ if (!x)
+ return -ENOMEM;
+
+ s = strjoin(k, " ", x, NULL);
+ if (!s)
+ return -ENOMEM;
+ break;
+ }}
+
+ *ret = s;
+ return 0;
+}
+
+const char *dns_class_to_string(uint16_t class) {
+
+ switch (class) {
+
+ case DNS_CLASS_IN:
+ return "IN";
+
+ case DNS_CLASS_ANY:
+ return "ANY";
+ }
+
+ return NULL;
+}
+
+int dns_class_from_string(const char *s, uint16_t *class) {
+ assert(s);
+ assert(class);
+
+ if (strcaseeq(s, "IN"))
+ *class = DNS_CLASS_IN;
+ else if (strcaseeq(s, "ANY"))
+ *class = DNS_TYPE_ANY;
+ else
+ return -EINVAL;
+
+ return 0;
+}
+
+static const struct {
+ uint16_t type;
+ const char *name;
+} dns_types[] = {
+ { DNS_TYPE_A, "A" },
+ { DNS_TYPE_NS, "NS" },
+ { DNS_TYPE_CNAME, "CNAME" },
+ { DNS_TYPE_SOA, "SOA" },
+ { DNS_TYPE_PTR, "PTR" },
+ { DNS_TYPE_HINFO, "HINFO" },
+ { DNS_TYPE_MX, "MX" },
+ { DNS_TYPE_TXT, "TXT" },
+ { DNS_TYPE_AAAA, "AAAA" },
+ { DNS_TYPE_LOC, "LOC" },
+ { DNS_TYPE_SRV, "SRV" },
+ { DNS_TYPE_SSHFP, "SSHFP" },
+ { DNS_TYPE_SPF, "SPF" },
+ { DNS_TYPE_DNAME, "DNAME" },
+ { DNS_TYPE_ANY, "ANY" },
+ { DNS_TYPE_OPT, "OPT" },
+ { DNS_TYPE_TKEY, "TKEY" },
+ { DNS_TYPE_TSIG, "TSIG" },
+ { DNS_TYPE_IXFR, "IXFR" },
+ { DNS_TYPE_AXFR, "AXFR" },
+};
+
+const char *dns_type_to_string(uint16_t type) {
+ unsigned i;
+
+ for (i = 0; i < ELEMENTSOF(dns_types); i++)
+ if (dns_types[i].type == type)
+ return dns_types[i].name;
+
+ return NULL;
+}
+
+int dns_type_from_string(const char *s, uint16_t *type) {
+ unsigned i;
+
+ assert(s);
+ assert(type);
+
+ for (i = 0; i < ELEMENTSOF(dns_types); i++)
+ if (strcaseeq(dns_types[i].name, s)) {
+ *type = dns_types[i].type;
+ return 0;
+ }
+
+ return -EINVAL;
+}