[LEDE-DEV] [PATCH mdns 1/3] Prepare support for sending unicast DNS answers

Rafał Miłecki zajec5 at gmail.com
Tue Feb 14 03:18:04 PST 2017


From: Rafał Miłecki <rafal at milecki.pl>

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 <rafal at milecki.pl>
---
 dns.c       |  8 ++++----
 dns.h       |  2 +-
 interface.c | 26 +++++++++++++++++++-------
 interface.h |  2 +-
 service.c   |  6 +++---
 5 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/dns.c b/dns.c
index dac6f2c..707d54c 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,7 +177,7 @@ 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");
 }
 
@@ -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, NULL, mdns_hostname_local);
 
 	freeifaddrs(ifap);
 }
diff --git a/dns.h b/dns.h
index 7f3cbe1..b257837 100644
--- a/dns.h
+++ b/dns.h
@@ -76,7 +76,7 @@ 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_send_answer(struct interface *iface, struct sockaddr *to, const char *answer);
 void dns_reply_a(struct interface *iface, 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..55706bf 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)
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 15f05f9..ca84da2 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);
 	}
-- 
2.11.0




More information about the Lede-dev mailing list