- sendto(io->fd, buf, size, MSG_DONTWAIT | MSG_NOSIGNAL, &sa.sa, sl);
+
+ int index = -1;
+ int family = AF_UNSPEC;
+
+ for(struct cmsghdr *cmptr = CMSG_FIRSTHDR(&msg); cmptr != NULL; cmptr = CMSG_NXTHDR(&msg, cmptr)) {
+#ifdef IP_PKTINFO
+
+ if(cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO) {
+ struct in_pktinfo *pktinfo = (struct in_pktinfo *) CMSG_DATA(cmptr);
+ index = pktinfo->ipi_ifindex;
+ family = AF_INET;
+ break;
+ } else
+#endif
+#ifdef IPV6_PKTINFO
+ if(cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == IPV6_PKTINFO) {
+ struct in6_pktinfo *pktinfo = (struct in6_pktinfo *) CMSG_DATA(cmptr);
+ index = pktinfo->ipi6_ifindex;
+ family = AF_INET6;
+ break;
+ }
+
+#endif
+ }
+
+ if(index) {
+ // Send a multicast response back using all addresses matching the interface
+ for(int i = 0; i < mesh->discovery.address_count; i++) {
+ discovery_address_t *p = &mesh->discovery.addresses[i];
+
+ if(p->index == index && p->address.sa.sa_family == family) {
+ send_mdns_packet(mesh, p, true);
+ }
+ }
+ } else {
+ // Send a unicast response back
+ sendto(io->fd, buf, size, MSG_DONTWAIT | MSG_NOSIGNAL, &sa.sa, sl);
+ }
+