diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2014-07-31 04:19:43 -0400 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2014-07-31 08:56:03 -0400 |
commit | 0dae31d468b1a0e22d98921f7b0dbd92fd217167 (patch) | |
tree | 0b1b2c51eab8bc4dc474b5fd1b3777abf64ed85c /src/resolve | |
parent | 9de3e3294065e8697ff10130b53f274319cdcf6f (diff) |
resolved: LOC records
LOC records have a version field. So far only version 0 has been
published, but if a record with a different version was encountered,
our only recourse is to treat it as an unknown type. This is
implemented with the 'unparseable' flag, which causes the
serialization/deserialization and printing function to cause the
record as a blob. The flag can be used if other packet types cannot be
parsed for whatever reason.
Diffstat (limited to 'src/resolve')
-rw-r--r-- | src/resolve/resolved-dns-packet.c | 75 | ||||
-rw-r--r-- | src/resolve/resolved-dns-rr.c | 68 | ||||
-rw-r--r-- | src/resolve/resolved-dns-rr.h | 12 |
3 files changed, 153 insertions, 2 deletions
diff --git a/src/resolve/resolved-dns-packet.c b/src/resolve/resolved-dns-packet.c index f22582971e..fae105d0da 100644 --- a/src/resolve/resolved-dns-packet.c +++ b/src/resolve/resolved-dns-packet.c @@ -497,7 +497,7 @@ int dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, size_t *star if (r < 0) goto fail; - switch (rr->key->type) { + switch (rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) { case DNS_TYPE_PTR: case DNS_TYPE_NS: @@ -570,10 +570,40 @@ int dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, size_t *star r = dns_packet_append_name(p, rr->mx.exchange, NULL); break; + case DNS_TYPE_LOC: + r = dns_packet_append_uint8(p, rr->loc.version, NULL); + if (r < 0) + goto fail; + + r = dns_packet_append_uint8(p, rr->loc.size, NULL); + if (r < 0) + goto fail; + + r = dns_packet_append_uint8(p, rr->loc.horiz_pre, NULL); + if (r < 0) + goto fail; + + r = dns_packet_append_uint8(p, rr->loc.vert_pre, NULL); + if (r < 0) + goto fail; + + r = dns_packet_append_uint16(p, rr->loc.latitude, NULL); + if (r < 0) + goto fail; + + r = dns_packet_append_uint16(p, rr->loc.longitude, NULL); + if (r < 0) + goto fail; + + r = dns_packet_append_uint16(p, rr->loc.altitude, NULL); + break; + case DNS_TYPE_SRV: case DNS_TYPE_DNAME: case DNS_TYPE_SSHFP: + case _DNS_TYPE_INVALID: /* unparseable */ default: + r = dns_packet_append_blob(p, rr->generic.data, rr->generic.size, NULL); break; } @@ -994,6 +1024,49 @@ int dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, size_t *start) { r = dns_packet_read_name(p, &rr->mx.exchange, NULL); break; + case DNS_TYPE_LOC: { + uint8_t t; + size_t pos; + + r = dns_packet_read_uint8(p, &t, &pos); + if (r < 0) + goto fail; + + if (t == 0) { + rr->loc.version = t; + + r = dns_packet_read_uint8(p, &rr->loc.size, NULL); + if (r < 0) + goto fail; + + r = dns_packet_read_uint8(p, &rr->loc.horiz_pre, NULL); + if (r < 0) + goto fail; + + r = dns_packet_read_uint8(p, &rr->loc.vert_pre, NULL); + if (r < 0) + goto fail; + + r = dns_packet_read_uint32(p, &rr->loc.latitude, NULL); + if (r < 0) + goto fail; + + r = dns_packet_read_uint32(p, &rr->loc.longitude, NULL); + if (r < 0) + goto fail; + + r = dns_packet_read_uint32(p, &rr->loc.altitude, NULL); + if (r < 0) + goto fail; + + break; + } else { + dns_packet_rewind(p, pos); + rr->unparseable = true; + /* fall through */ + } + } + case DNS_TYPE_SRV: case DNS_TYPE_DNAME: case DNS_TYPE_SSHFP: diff --git a/src/resolve/resolved-dns-rr.c b/src/resolve/resolved-dns-rr.c index 1ee93fe8c5..b6a2c04cb5 100644 --- a/src/resolve/resolved-dns-rr.c +++ b/src/resolve/resolved-dns-rr.c @@ -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" @@ -252,6 +254,7 @@ DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) { case DNS_TYPE_MX: free(rr->mx.exchange); break; + case DNS_TYPE_LOC: case DNS_TYPE_A: case DNS_TYPE_AAAA: break; @@ -357,12 +360,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; @@ -374,7 +419,7 @@ 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: @@ -455,6 +500,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; @@ -513,6 +578,7 @@ 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" }, diff --git a/src/resolve/resolved-dns-rr.h b/src/resolve/resolved-dns-rr.h index afd623a382..8fb463946b 100644 --- a/src/resolve/resolved-dns-rr.h +++ b/src/resolve/resolved-dns-rr.h @@ -51,6 +51,7 @@ enum { DNS_TYPE_MX = 0x0F, DNS_TYPE_TXT = 0x10, DNS_TYPE_AAAA = 0x1C, + DNS_TYPE_LOC = 0x1D, DNS_TYPE_SRV = 0x21, DNS_TYPE_DNAME = 0x27, DNS_TYPE_SSHFP = 0x2C, @@ -78,6 +79,7 @@ struct DnsResourceRecord { unsigned n_ref; DnsResourceKey *key; uint32_t ttl; + bool unparseable; union { struct { void *data; @@ -126,6 +128,16 @@ struct DnsResourceRecord { uint16_t priority; char *exchange; } mx; + + struct { + uint8_t version; + uint8_t size; + uint8_t horiz_pre; + uint8_t vert_pre; + uint32_t latitude; + uint32_t longitude; + uint32_t altitude; + } loc; }; }; |