chiark / gitweb /
dns-domain: introduce macros for accessing all DNS header fields
authorLennart Poettering <lennart@poettering.net>
Wed, 16 Jul 2014 16:03:46 +0000 (18:03 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 16 Jul 2014 16:03:46 +0000 (18:03 +0200)
src/resolve/resolved-bus.c
src/resolve/resolved-dns-packet.c
src/resolve/resolved-dns-packet.h
src/resolve/resolved-manager.c

index e73bdac..02e971d 100644 (file)
@@ -86,10 +86,9 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
                 if (r < 0)
                         goto finish;
 
-                n = be16toh(DNS_PACKET_HEADER(q->packet)->ancount) +
-                    be16toh(DNS_PACKET_HEADER(q->packet)->nscount) +
-                    be16toh(DNS_PACKET_HEADER(q->packet)->arcount);
-
+                n = DNS_PACKET_ANCOUNT(q->packet) +
+                    DNS_PACKET_NSCOUNT(q->packet) +
+                    DNS_PACKET_ARCOUNT(q->packet);
                 for (i = 0; i < n; i++) {
                         _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
 
@@ -292,10 +291,9 @@ static void bus_method_resolve_address_complete(DnsQuery *q) {
                 if (r < 0)
                         goto finish;
 
-                n = be16toh(DNS_PACKET_HEADER(q->packet)->ancount) +
-                    be16toh(DNS_PACKET_HEADER(q->packet)->nscount) +
-                    be16toh(DNS_PACKET_HEADER(q->packet)->arcount);
-
+                n = DNS_PACKET_ANCOUNT(q->packet) +
+                    DNS_PACKET_NSCOUNT(q->packet) +
+                    DNS_PACKET_ARCOUNT(q->packet);
                 for (i = 0; i < n; i++) {
                         _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
 
index a503b01..9aa0734 100644 (file)
@@ -116,7 +116,6 @@ int dns_packet_validate(DnsPacket *p) {
 }
 
 int dns_packet_validate_reply(DnsPacket *p) {
-        DnsPacketHeader *h;
         int r;
 
         assert(p);
@@ -125,14 +124,10 @@ int dns_packet_validate_reply(DnsPacket *p) {
         if (r < 0)
                 return r;
 
-        h = DNS_PACKET_HEADER(p);
-
-        /* Check QR field */
-        if ((be16toh(h->flags) & 1) == 0)
+        if (DNS_PACKET_QR(p) == 0)
                 return -EBADMSG;
 
-        /* Check opcode field */
-        if (((be16toh(h->flags) >> 1) & 15) != 0)
+        if (DNS_PACKET_OPCODE(p) != 0)
                 return -EBADMSG;
 
         return 0;
@@ -699,7 +694,7 @@ int dns_packet_skip_question(DnsPacket *p) {
         unsigned i, n;
         assert(p);
 
-        n = be16toh(DNS_PACKET_HEADER(p)->qdcount);
+        n = DNS_PACKET_QDCOUNT(p);
         for (i = 0; i < n; i++) {
                 _cleanup_(dns_resource_key_free) DnsResourceKey key = {};
 
index 18ed4ba..77edc05 100644 (file)
@@ -62,6 +62,26 @@ static inline uint8_t* DNS_PACKET_DATA(DnsPacket *p) {
 }
 
 #define DNS_PACKET_HEADER(p) ((DnsPacketHeader*) DNS_PACKET_DATA(p))
+#define DNS_PACKET_ID(p) DNS_PACKET_HEADER(p)->id
+#define DNS_PACKET_QR(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 15) & 1)
+#define DNS_PACKET_OPCODE(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 11) & 15)
+#define DNS_PACKET_RCODE(p) (be16toh(DNS_PACKET_HEADER(p)->flags) & 15)
+#define DNS_PACKET_QDCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->qdcount)
+#define DNS_PACKET_ANCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->ancount)
+#define DNS_PACKET_NSCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->nscount)
+#define DNS_PACKET_ARCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->arcount)
+
+#define DNS_PACKET_MAKE_FLAGS(qr, opcode, aa, tc, rd, ra, ad, cd, rcode) \
+        (((uint16_t) !!qr << 15) |  \
+         ((uint16_t) (opcode & 15) << 11) | \
+         ((uint16_t) !!aa << 10) | \
+         ((uint16_t) !!tc << 9) | \
+         ((uint16_t) !!rd << 8) | \
+         ((uint16_t) !!ra << 7) | \
+         ((uint16_t) !!ad << 5) | \
+         ((uint16_t) !!cd << 4) | \
+         ((uint16_t) (rcode & 15)))
+
 
 int dns_packet_new(DnsPacket **p, size_t mtu);
 int dns_packet_new_query(DnsPacket **p, size_t mtu);
@@ -92,19 +112,6 @@ int dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, size_t *start);
 
 int dns_packet_skip_question(DnsPacket *p);
 
-#define DNS_PACKET_MAKE_FLAGS(qr, opcode, aa, tc, rd, ra, ad, cd, rcode) \
-        (((uint16_t) !!qr << 15) |  \
-         ((uint16_t) (opcode & 15) << 11) | \
-         ((uint16_t) !!aa << 10) | \
-         ((uint16_t) !!tc << 9) | \
-         ((uint16_t) !!rd << 8) | \
-         ((uint16_t) !!ra << 7) | \
-         ((uint16_t) !!ad << 5) | \
-         ((uint16_t) !!cd << 4) | \
-         ((uint16_t) (rcode & 15)))
-
-#define DNS_PACKET_RCODE(p) (be16toh(DNS_PACKET_HEADER(p)->flags) & 15)
-
 enum {
         DNS_RCODE_SUCCESS = 0,
         DNS_RCODE_FORMERR = 1,
index 8718ea4..09b758f 100644 (file)
@@ -653,7 +653,7 @@ static int on_dns_ipv4_packet(sd_event_source *s, int fd, uint32_t revents, void
         if (r <= 0)
                 return r;
 
-        t = hashmap_get(m->dns_query_transactions, UINT_TO_PTR(DNS_PACKET_HEADER(p)->id));
+        t = hashmap_get(m->dns_query_transactions, UINT_TO_PTR(DNS_PACKET_ID(p)));
         if (!t)
                 return 0;
 
@@ -670,7 +670,7 @@ static int on_dns_ipv6_packet(sd_event_source *s, int fd, uint32_t revents, void
         if (r <= 0)
                 return r;
 
-        t = hashmap_get(m->dns_query_transactions, UINT_TO_PTR(DNS_PACKET_HEADER(p)->id));
+        t = hashmap_get(m->dns_query_transactions, UINT_TO_PTR(DNS_PACKET_ID(p)));
         if (!t)
                 return 0;