Rename project to umdns
[project/mdnsd.git] / dns.c
diff --git a/dns.c b/dns.c
index fc93f4a..91434f2 100644 (file)
--- a/dns.c
+++ b/dns.c
@@ -15,6 +15,7 @@
 #include <sys/stat.h>
 
 #include <fcntl.h>
+#include <ifaddrs.h>
 #include <time.h>
 #include <stdio.h>
 #include <unistd.h>
@@ -22,7 +23,6 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
-#include <asm/byteorder.h>
 #include <arpa/nameser.h>
 #include <resolv.h>
 #include <stdlib.h>
@@ -40,6 +40,7 @@
 #include "interface.h"
 
 static char name_buffer[MAX_NAME_LEN + 1];
+static char dns_buffer[MAX_NAME_LEN];
 static struct blob_buf ans_buf;
 
 const char*
@@ -67,21 +68,17 @@ dns_type_string(uint16_t type)
 }
 
 void
-dns_send_question(struct interface *iface, const char *question, int type)
+dns_send_question(struct interface *iface, const char *question, int type, int multicast)
 {
-       static struct dns_header h = {
-               .questions = cpu_to_be16(1),
-       };
-       static struct dns_question q = {
-               .class = cpu_to_be16(1),
-       };
+       static struct dns_header h;
+       static struct dns_question q;
        static struct iovec iov[] = {
                {
                        .iov_base = &h,
                        .iov_len = sizeof(h),
                },
                {
-                       .iov_base = name_buffer,
+                       .iov_base = dns_buffer,
                },
                {
                        .iov_base = &q,
@@ -90,18 +87,19 @@ dns_send_question(struct interface *iface, const char *question, int type)
        };
        int len;
 
-       q.type = __cpu_to_be16(type);
+       h.questions = cpu_to_be16(1);
+       q.class = cpu_to_be16((multicast ? 0 : CLASS_UNICAST) | 1);
+       q.type = cpu_to_be16(type);
 
-       len = dn_comp(question, (void *) name_buffer, sizeof(name_buffer), NULL, NULL);
+       len = dn_comp(question, (void *) dns_buffer, sizeof(dns_buffer), NULL, NULL);
        if (len < 1)
                return;
 
        iov[1].iov_len = len;
 
+       DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
        if (interface_send_packet(iface, iov, ARRAY_SIZE(iov)) < 0)
-               fprintf(stderr, "failed to send question\n");
-       else
-               DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
+               perror("failed to send question :");
 }
 
 
@@ -123,7 +121,7 @@ dns_init_answer(void)
 }
 
 void
-dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength)
+dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength, int ttl)
 {
        struct blob_attr *attr;
        struct dns_answer *a;
@@ -132,7 +130,7 @@ dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength)
        a = blob_data(attr);
        a->type = cpu_to_be16(type);
        a->class = cpu_to_be16(1);
-       a->ttl = cpu_to_be32(announce_ttl);
+       a->ttl = cpu_to_be32(ttl);
        a->rdlength = cpu_to_be16(rdlength);
        memcpy(a + 1, rdata, rdlength);
 
@@ -152,8 +150,8 @@ dns_send_answer(struct interface *iface, const char *answer)
        if (!dns_answer_cnt)
                return;
 
-       h.answers = __cpu_to_be16(dns_answer_cnt);
-       h.flags = __cpu_to_be16(0x8400);
+       h.answers = cpu_to_be16(dns_answer_cnt);
+       h.flags = cpu_to_be16(0x8400);
 
        iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 2) + 1));
 
@@ -183,6 +181,35 @@ dns_send_answer(struct interface *iface, const char *answer)
                fprintf(stderr, "failed to send question\n");
 }
 
+void
+dns_reply_a(struct interface *iface, int ttl)
+{
+       struct ifaddrs *ifap, *ifa;
+       struct sockaddr_in *sa;
+       struct sockaddr_in6 *sa6;
+
+       getifaddrs(&ifap);
+
+       dns_init_answer();
+       for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+               if (strcmp(ifa->ifa_name, iface->name))
+                       continue;
+               if (ifa->ifa_addr->sa_family == AF_INET) {
+                       sa = (struct sockaddr_in *) ifa->ifa_addr;
+                       dns_add_answer(TYPE_A, (uint8_t *) &sa->sin_addr, 4, ttl);
+               }
+               if (ifa->ifa_addr->sa_family == AF_INET6) {
+                       uint8_t ll_prefix[] = {0xfe, 0x80 };
+                       sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
+                       if (!memcmp(&sa6->sin6_addr, &ll_prefix, 2))
+                               dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl);
+               }
+       }
+       dns_send_answer(iface, mdns_hostname_local);
+
+       freeifaddrs(ifap);
+}
+
 static int
 scan_name(const uint8_t *buffer, int len)
 {
@@ -216,7 +243,7 @@ dns_consume_header(uint8_t **data, int *len)
                return NULL;
 
        while (endianess--) {
-               *swap = __be16_to_cpu(*swap);
+               *swap = be16_to_cpu(*swap);
                swap++;
        }
 
@@ -237,7 +264,7 @@ dns_consume_question(uint8_t **data, int *len)
                return NULL;
 
        while (endianess--) {
-               *swap = __be16_to_cpu(*swap);
+               *swap = be16_to_cpu(*swap);
                swap++;
        }
 
@@ -255,10 +282,10 @@ dns_consume_answer(uint8_t **data, int *len)
        if (*len < sizeof(struct dns_answer))
                return NULL;
 
-       a->type = __be16_to_cpu(a->type);
-       a->class = __be16_to_cpu(a->class);
-       a->ttl = __be32_to_cpu(a->ttl);
-       a->rdlength = __be16_to_cpu(a->rdlength);
+       a->type = be16_to_cpu(a->type);
+       a->class = be16_to_cpu(a->class);
+       a->ttl = be32_to_cpu(a->ttl);
+       a->rdlength = be16_to_cpu(a->rdlength);
 
        *len -= sizeof(struct dns_answer);
        *data += sizeof(struct dns_answer);
@@ -303,6 +330,9 @@ parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int
                return -1;
        }
 
+       if ((a->class & ~CLASS_FLUSH) != CLASS_IN)
+               return -1;
+
        rdata = *b;
        if (a->rdlength > *rlen) {
                fprintf(stderr, "dropping: bad question\n");
@@ -313,7 +343,7 @@ parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int
        *b += a->rdlength;
 
        if (cache)
-               cache_answer(iface, buffer, len, name, a, rdata);
+               cache_answer(iface, buffer, len, name, a, rdata, a->class & CLASS_FLUSH);
 
        return 0;
 }
@@ -323,17 +353,23 @@ parse_question(struct interface *iface, char *name, struct dns_question *q)
 {
        char *host;
 
+       if ((q->class & CLASS_UNICAST) && iface->multicast)
+               iface = iface->peer;
+
        DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
 
        switch (q->type) {
        case TYPE_ANY:
-               if (!strcmp(name, mdns_hostname_local))
-                       service_reply(iface, NULL);
+               if (!strcmp(name, mdns_hostname_local)) {
+                       service_reply(iface, NULL, announce_ttl);
+                       dns_reply_a(iface, announce_ttl);
+               }
                break;
 
        case TYPE_PTR:
-               service_announce_services(iface, name);
-               service_reply(iface, name);
+               if (!strcmp(name, sdudp))
+                       service_announce_services(iface, announce_ttl);
+               service_reply(iface, name, announce_ttl);
                break;
 
        case TYPE_AAAA:
@@ -342,13 +378,13 @@ parse_question(struct interface *iface, char *name, struct dns_question *q)
                if (host)
                        *host = '\0';
                if (!strcmp(mdns_hostname, name))
-                       service_reply_a(iface, q->type);
+                       dns_reply_a(iface, announce_ttl);
                break;
        };
 }
 
 void
-dns_handle_packet(struct interface *iface, uint8_t *buffer, int len)
+dns_handle_packet(struct interface *iface, struct sockaddr *s, uint16_t port, uint8_t *buffer, int len)
 {
        struct dns_header *h;
        uint8_t *b = buffer;
@@ -360,6 +396,10 @@ dns_handle_packet(struct interface *iface, uint8_t *buffer, int len)
                return;
        }
 
+       if (h->questions && !iface->multicast && port != 5353)
+               /* silently drop unicast questions that dont originate from port 5353 */
+               return;
+
        while (h->questions-- > 0) {
                char *name = dns_consume_name(buffer, len, &b, &rlen);
                struct dns_question *q;
@@ -383,11 +423,15 @@ dns_handle_packet(struct interface *iface, uint8_t *buffer, int len)
                return;
 
        while (h->answers-- > 0)
-               parse_answer(iface, buffer, len, &b, &rlen, 1);
+               if (parse_answer(iface, buffer, len, &b, &rlen, 1))
+                       return;
 
        while (h->authority-- > 0)
-               parse_answer(iface, buffer, len, &b, &rlen, 0);
+               if (parse_answer(iface, buffer, len, &b, &rlen, 1))
+                       return;
 
        while (h->additional-- > 0)
-               parse_answer(iface, buffer, len, &b, &rlen, 1);
+               if (parse_answer(iface, buffer, len, &b, &rlen, 1))
+                       return;
+
 }