chiark / gitweb /
resolved: properly process DNAME RRs
[elogind.git] / src / resolve / resolved-dns-rr.c
index 9131b4b..d282244 100644 (file)
@@ -19,6 +19,8 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <math.h>
+
 #include "strv.h"
 
 #include "resolved-dns-domain.h"
@@ -231,20 +233,35 @@ DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) {
         }
 
         if (rr->key) {
-                if (IN_SET(rr->key->type, DNS_TYPE_PTR, DNS_TYPE_NS, DNS_TYPE_CNAME))
+                switch(rr->key->type) {
+                case DNS_TYPE_PTR:
+                case DNS_TYPE_NS:
+                case DNS_TYPE_CNAME:
+                case DNS_TYPE_DNAME:
                         free(rr->ptr.name);
-                else if (rr->key->type == DNS_TYPE_HINFO) {
+                        break;
+                case DNS_TYPE_HINFO:
                         free(rr->hinfo.cpu);
                         free(rr->hinfo.os);
-                } else if (rr->key->type == DNS_TYPE_TXT) {
+                        break;
+                case DNS_TYPE_SPF:
+                case DNS_TYPE_TXT:
                         strv_free(rr->txt.strings);
-                } else if (rr->key->type == DNS_TYPE_SOA) {
+                        break;
+                case DNS_TYPE_SOA:
                         free(rr->soa.mname);
                         free(rr->soa.rname);
-                } else if (rr->key->type == DNS_TYPE_MX) {
+                        break;
+                case DNS_TYPE_MX:
                         free(rr->mx.exchange);
-                } else if (!IN_SET(rr->key->type, DNS_TYPE_A, DNS_TYPE_AAAA))
+                        break;
+                case DNS_TYPE_LOC:
+                case DNS_TYPE_A:
+                case DNS_TYPE_AAAA:
+                        break;
+                default:
                         free(rr->generic.data);
+                }
 
                 dns_resource_key_unref(rr->key);
         }
@@ -303,12 +320,14 @@ int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecor
         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;
 
@@ -343,12 +362,54 @@ int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecor
 
                 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;
@@ -360,11 +421,12 @@ int dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret) {
         if (r < 0)
                 return r;
 
-        switch (rr->key->type) {
+        switch (rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
 
         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;
@@ -377,6 +439,7 @@ int dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret) {
                         return -ENOMEM;
                 break;
 
+        case DNS_TYPE_SPF: /* exactly the same as TXT */
         case DNS_TYPE_TXT: {
                 _cleanup_free_ char *t;
 
@@ -440,6 +503,26 @@ int dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret) {
                         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;
 
@@ -498,8 +581,10 @@ static const struct {
         { 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"   },
@@ -509,7 +594,6 @@ static const struct {
         { DNS_TYPE_AXFR,  "AXFR"  },
 };
 
-
 const char *dns_type_to_string(uint16_t type) {
         unsigned i;