X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=dns.c;h=3e902d019df417fd5443d436c956992cf99fdc5a;hb=HEAD;hp=fc93f4a4fd88eb686c17cf3e74814972b56f9e3c;hpb=30d33913cb2d8e895ebf1c79595a0ebf901d925b;p=oweals%2Fmdnsd.git diff --git a/dns.c b/dns.c index fc93f4a..3e902d0 100644 --- a/dns.c +++ b/dns.c @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -22,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -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,18 @@ dns_type_string(uint16_t type) } void -dns_send_question(struct interface *iface, const char *question, int type) +dns_send_question(struct interface *iface, struct sockaddr *to, + 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 +88,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; - 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); + DBG(1, "Q <- %s %s\n", dns_type_string(type), question); + if (interface_send_packet(iface, to, iov, ARRAY_SIZE(iov)) < 0) + perror("failed to send question"); } @@ -123,7 +122,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 +131,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); @@ -140,7 +139,7 @@ dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength) } void -dns_send_answer(struct interface *iface, const char *answer) +dns_send_answer(struct interface *iface, struct sockaddr *to, const char *answer) { uint8_t buffer[256]; struct blob_attr *attr; @@ -152,8 +151,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)); @@ -179,8 +178,35 @@ dns_send_answer(struct interface *iface, const char *answer) DBG(1, "A <- %s %s\n", dns_type_string(be16_to_cpu(a->type)), answer); } - if (interface_send_packet(iface, iov, n_iov) < 0) - fprintf(stderr, "failed to send question\n"); + if (interface_send_packet(iface, to, iov, n_iov) < 0) + perror("failed to send answer"); +} + +void +dns_reply_a(struct interface *iface, struct sockaddr *to, 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) { + sa6 = (struct sockaddr_in6 *) ifa->ifa_addr; + dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl); + } + } + dns_send_answer(iface, to, mdns_hostname_local); + + freeifaddrs(ifap); } static int @@ -194,6 +220,7 @@ scan_name(const uint8_t *buffer, int len) if (IS_COMPRESSED(l)) return offset + 2; + if (l + 1 > len) return -1; len -= l + 1; offset += l + 1; buffer += l + 1; @@ -209,16 +236,16 @@ static struct dns_header* dns_consume_header(uint8_t **data, int *len) { struct dns_header *h = (struct dns_header *) *data; - uint16_t *swap = (uint16_t *) h; - int endianess = 6; if (*len < sizeof(struct dns_header)) return NULL; - while (endianess--) { - *swap = __be16_to_cpu(*swap); - swap++; - } + h->id = be16_to_cpu(h->id); + h->flags = be16_to_cpu(h->flags); + h->questions = be16_to_cpu(h->questions); + h->answers = be16_to_cpu(h->answers); + h->authority = be16_to_cpu(h->authority); + h->additional = be16_to_cpu(h->additional); *len -= sizeof(struct dns_header); *data += sizeof(struct dns_header); @@ -230,16 +257,12 @@ static struct dns_question* dns_consume_question(uint8_t **data, int *len) { struct dns_question *q = (struct dns_question *) *data; - uint16_t *swap = (uint16_t *) q; - int endianess = 2; if (*len < sizeof(struct dns_question)) return NULL; - while (endianess--) { - *swap = __be16_to_cpu(*swap); - swap++; - } + q->type = be16_to_cpu(q->type); + q->class = be16_to_cpu(q->class); *len -= sizeof(struct dns_question); *data += sizeof(struct dns_question); @@ -255,10 +278,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); @@ -285,14 +308,15 @@ dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len) return name_buffer; } -static int -parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache) +static int parse_answer(struct interface *iface, struct sockaddr *from, + uint8_t *buffer, int len, uint8_t **b, int *rlen, + int cache) { char *name = dns_consume_name(buffer, len, b, rlen); struct dns_answer *a; uint8_t *rdata; - if (!name) { + if (!name || *rlen < 0) { fprintf(stderr, "dropping: bad question\n"); return -1; } @@ -303,6 +327,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,27 +340,52 @@ 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, from, buffer, len, name, a, rdata, a->class & CLASS_FLUSH); return 0; } static void -parse_question(struct interface *iface, char *name, struct dns_question *q) +parse_question(struct interface *iface, struct sockaddr *from, char *name, struct dns_question *q) { + struct sockaddr *to = NULL; char *host; + /* TODO: Multicast if more than one quarter of TTL has passed */ + if (q->class & CLASS_UNICAST) { + to = from; + if (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)) { + dns_reply_a(iface, to, announce_ttl); + service_reply(iface, to, NULL, NULL, announce_ttl); + } break; case TYPE_PTR: - service_announce_services(iface, name); - service_reply(iface, name); + if (!strcmp(name, C_DNS_SD)) { + dns_reply_a(iface, to, announce_ttl); + service_announce_services(iface, to, announce_ttl); + } else { + if (name[0] == '_') { + service_reply(iface, to, NULL, name, announce_ttl); + } else { + /* First dot separates instance name from the rest */ + char *dot = strchr(name, '.'); + + if (dot) { + *dot = '\0'; + service_reply(iface, to, name, dot + 1, announce_ttl); + *dot = '.'; + } + } + } break; case TYPE_AAAA: @@ -341,14 +393,14 @@ parse_question(struct interface *iface, char *name, struct dns_question *q) host = strstr(name, ".local"); if (host) *host = '\0'; - if (!strcmp(mdns_hostname, name)) - service_reply_a(iface, q->type); + if (!strcmp(umdns_host_label, name)) + dns_reply_a(iface, to, announce_ttl); break; }; } void -dns_handle_packet(struct interface *iface, uint8_t *buffer, int len) +dns_handle_packet(struct interface *iface, struct sockaddr *from, uint16_t port, uint8_t *buffer, int len) { struct dns_header *h; uint8_t *b = buffer; @@ -360,11 +412,15 @@ dns_handle_packet(struct interface *iface, uint8_t *buffer, int len) return; } + if (h->questions && !iface->multicast && port != MCAST_PORT) + /* 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; - if (!name) { + if (!name || rlen < 0) { fprintf(stderr, "dropping: bad name\n"); return; } @@ -376,18 +432,22 @@ dns_handle_packet(struct interface *iface, uint8_t *buffer, int len) } if (!(h->flags & FLAG_RESPONSE)) - parse_question(iface, name, q); + parse_question(iface, from, name, q); } if (!(h->flags & FLAG_RESPONSE)) return; while (h->answers-- > 0) - parse_answer(iface, buffer, len, &b, &rlen, 1); + if (parse_answer(iface, from, buffer, len, &b, &rlen, 1)) + return; while (h->authority-- > 0) - parse_answer(iface, buffer, len, &b, &rlen, 0); + if (parse_answer(iface, from, buffer, len, &b, &rlen, 1)) + return; while (h->additional-- > 0) - parse_answer(iface, buffer, len, &b, &rlen, 1); + if (parse_answer(iface, from, buffer, len, &b, &rlen, 1)) + return; + }