From: Rafał Miłecki Date: Tue, 14 Feb 2017 11:18:04 +0000 (+0100) Subject: Prepare support for sending unicast DNS answers X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=e78deecf29d4c7298743aba09ee22c51a547c70d;p=oweals%2Fmdnsd.git Prepare support for sending unicast DNS answers This adds extra argument to dns_send_answer & interface_send_packet functions. For now we pass NULL-s only. Signed-off-by: Rafał Miłecki --- diff --git a/announce.c b/announce.c index f04a600..695c66e 100644 --- a/announce.c +++ b/announce.c @@ -65,7 +65,7 @@ announce_timer(struct uloop_timeout *timeout) /* Fall through */ case STATE_ANNOUNCE: - dns_reply_a(iface, announce_ttl); + dns_reply_a(iface, NULL, announce_ttl); service_announce_services(iface, announce_ttl); uloop_timeout_set(timeout, announce_ttl * 800); break; diff --git a/dns.c b/dns.c index dac6f2c..a09b0ec 100644 --- a/dns.c +++ b/dns.c @@ -98,7 +98,7 @@ dns_send_question(struct interface *iface, const char *question, int type, int m 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) + if (interface_send_packet(iface, NULL, iov, ARRAY_SIZE(iov)) < 0) perror("failed to send question :"); } @@ -138,7 +138,7 @@ dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength, int ttl) } 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; @@ -177,12 +177,12 @@ 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) + if (interface_send_packet(iface, to, iov, n_iov) < 0) fprintf(stderr, "failed to send question\n"); } void -dns_reply_a(struct interface *iface, int ttl) +dns_reply_a(struct interface *iface, struct sockaddr *to, int ttl) { struct ifaddrs *ifap, *ifa; struct sockaddr_in *sa; @@ -205,7 +205,7 @@ dns_reply_a(struct interface *iface, int ttl) dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl); } } - dns_send_answer(iface, mdns_hostname_local); + dns_send_answer(iface, to, mdns_hostname_local); freeifaddrs(ifap); } @@ -362,7 +362,7 @@ parse_question(struct interface *iface, char *name, struct dns_question *q) case TYPE_ANY: if (!strcmp(name, mdns_hostname_local)) { service_reply(iface, NULL, announce_ttl); - dns_reply_a(iface, announce_ttl); + dns_reply_a(iface, NULL, announce_ttl); } break; @@ -388,7 +388,7 @@ parse_question(struct interface *iface, char *name, struct dns_question *q) if (host) *host = '\0'; if (!strcmp(mdns_hostname, name)) - dns_reply_a(iface, announce_ttl); + dns_reply_a(iface, NULL, announce_ttl); break; }; } diff --git a/dns.h b/dns.h index 7f3cbe1..0f26f60 100644 --- a/dns.h +++ b/dns.h @@ -76,8 +76,8 @@ extern int cfg_no_subnet; void dns_send_question(struct interface *iface, const char *question, int type, int multicast); void dns_init_answer(void); void dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength, int ttl); -void dns_send_answer(struct interface *iface, const char *answer); -void dns_reply_a(struct interface *iface, int ttl); +void dns_send_answer(struct interface *iface, struct sockaddr *to, const char *answer); +void dns_reply_a(struct interface *iface, struct sockaddr *to, int ttl); const char* dns_type_string(uint16_t type); void dns_handle_packet(struct interface *iface, struct sockaddr *s, uint16_t port, uint8_t *buf, int len); diff --git a/interface.c b/interface.c index 76820c7..69de071 100644 --- a/interface.c +++ b/interface.c @@ -41,7 +41,7 @@ #include "service.h" static int -interface_send_packet4(struct interface *iface, struct iovec *iov, int iov_len) +interface_send_packet4(struct interface *iface, struct sockaddr_in *to, struct iovec *iov, int iov_len) { static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1]; static struct sockaddr_in a; @@ -69,13 +69,19 @@ interface_send_packet4(struct interface *iface, struct iovec *iov, int iov_len) pkti = (struct in_pktinfo*) CMSG_DATA(cmsg); pkti->ipi_ifindex = iface->ifindex; - a.sin_addr.s_addr = inet_addr(MCAST_ADDR); + if (iface->multicast || !to) { + a.sin_addr.s_addr = inet_addr(MCAST_ADDR); + if (to) + fprintf(stderr, "Ignoring IPv4 address for multicast interface\n"); + } else { + a.sin_addr.s_addr = to->sin_addr.s_addr; + } return sendmsg(fd, &m, 0); } static int -interface_send_packet6(struct interface *iface, struct iovec *iov, int iov_len) +interface_send_packet6(struct interface *iface, struct sockaddr_in6 *to, struct iovec *iov, int iov_len) { static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in6_pktinfo)) / sizeof(size_t)) + 1]; static struct sockaddr_in6 a; @@ -103,13 +109,19 @@ interface_send_packet6(struct interface *iface, struct iovec *iov, int iov_len) pkti = (struct in6_pktinfo*) CMSG_DATA(cmsg); pkti->ipi6_ifindex = iface->ifindex; - inet_pton(AF_INET6, MCAST_ADDR6, &a.sin6_addr); + if (iface->multicast || !to) { + inet_pton(AF_INET6, MCAST_ADDR6, &a.sin6_addr); + if (to) + fprintf(stderr, "Ignoring IPv6 address for multicast interface\n"); + } else { + a.sin6_addr = to->sin6_addr; + } return sendmsg(fd, &m, 0); } int -interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len) +interface_send_packet(struct interface *iface, struct sockaddr *to, struct iovec *iov, int iov_len) { if (debug > 1) { fprintf(stderr, "TX ipv%d: %s\n", iface->v6 * 2 + 4, iface->name); @@ -117,9 +129,9 @@ interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len) } if (iface->v6) - return interface_send_packet6(iface, iov, iov_len); + return interface_send_packet6(iface, (struct sockaddr_in6 *)to, iov, iov_len); - return interface_send_packet4(iface, iov, iov_len); + return interface_send_packet4(iface, (struct sockaddr_in *)to, iov, iov_len); } static void interface_close(struct interface *iface) @@ -624,7 +636,7 @@ void interface_shutdown(void) vlist_for_each_element(&interfaces, iface, node) if (iface->fd.fd > 0 && iface->multicast) { - dns_reply_a(iface, 0); + dns_reply_a(iface, NULL, 0); service_announce_services(iface, 0); } vlist_for_each_element(&interfaces, iface, node) diff --git a/interface.h b/interface.h index 50d7071..4ba6eee 100644 --- a/interface.h +++ b/interface.h @@ -53,7 +53,7 @@ struct interface { int interface_add(const char *name); void interface_shutdown(void); -int interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len); +int interface_send_packet(struct interface *iface, struct sockaddr *to, struct iovec *iov, int iov_len); struct interface* interface_get(const char *name, int v6, int multicast); #endif diff --git a/service.c b/service.c index f21578d..9ee01e6 100644 --- a/service.c +++ b/service.c @@ -133,13 +133,13 @@ service_reply_single(struct interface *iface, struct service *s, int ttl, int fo dns_init_answer(); service_add_ptr(service_name(s->service), ttl); - dns_send_answer(iface, service); + dns_send_answer(iface, NULL, service); dns_init_answer(); service_add_srv(s, ttl); if (s->txt && s->txt_len) dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len, ttl); - dns_send_answer(iface, host); + dns_send_answer(iface, NULL, host); } void @@ -163,7 +163,7 @@ service_announce_services(struct interface *iface, int ttl) if (ttl) { dns_init_answer(); service_add_ptr(s->service, ttl); - dns_send_answer(iface, sdudp); + dns_send_answer(iface, NULL, sdudp); } service_reply_single(iface, s, ttl, 0); }