Browse Source

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 <rafal@milecki.pl>
Rafał Miłecki 7 years ago
parent
commit
e78deecf29
6 changed files with 34 additions and 22 deletions
  1. 1 1
      announce.c
  2. 7 7
      dns.c
  3. 2 2
      dns.h
  4. 20 8
      interface.c
  5. 1 1
      interface.h
  6. 3 3
      service.c

+ 1 - 1
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;

+ 7 - 7
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;
 	};
 }

+ 2 - 2
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);
 

+ 20 - 8
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)

+ 1 - 1
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

+ 3 - 3
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);
 	}