Rename service_reply_a to dns_reply_a and move it to proper file
authorRafał Miłecki <rafal@milecki.pl>
Wed, 8 Feb 2017 15:47:52 +0000 (16:47 +0100)
committerRafał Miłecki <rafal@milecki.pl>
Wed, 8 Feb 2017 16:42:14 +0000 (17:42 +0100)
This function doesn't really do anything service specify, it just sends
an A(AAA) records. It could probably be used even without any services
registered.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
dns.c
dns.h
interface.c
service.c
service.h

diff --git a/dns.c b/dns.c
index 63788f73ebdb708ea66ac2e83e920e152ca68aed..7ca705ea416653099bde9a3329c81119d5bb9bae 100644 (file)
--- a/dns.c
+++ b/dns.c
@@ -15,6 +15,7 @@
 #include <sys/stat.h>
 
 #include <fcntl.h>
+#include <ifaddrs.h>
 #include <time.h>
 #include <stdio.h>
 #include <unistd.h>
@@ -180,6 +181,35 @@ dns_send_answer(struct interface *iface, const char *answer)
                fprintf(stderr, "failed to send question\n");
 }
 
+void
+dns_reply_a(struct interface *iface, 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) {
+                       uint8_t ll_prefix[] = {0xfe, 0x80 };
+                       sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
+                       if (!memcmp(&sa6->sin6_addr, &ll_prefix, 2))
+                               dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl);
+               }
+       }
+       dns_send_answer(iface, mdns_hostname_local);
+
+       freeifaddrs(ifap);
+}
+
 static int
 scan_name(const uint8_t *buffer, int len)
 {
@@ -345,7 +375,7 @@ parse_question(struct interface *iface, char *name, struct dns_question *q)
                if (host)
                        *host = '\0';
                if (!strcmp(mdns_hostname, name))
-                       service_reply_a(iface, announce_ttl);
+                       dns_reply_a(iface, announce_ttl);
                break;
        };
 }
diff --git a/dns.h b/dns.h
index 6210e0e52868d83892202714b1be2917762b3326..7f3cbe176e6b6d492fa7e7b4303b8c0d9a131c94 100644 (file)
--- a/dns.h
+++ b/dns.h
@@ -77,6 +77,7 @@ void dns_send_question(struct interface *iface, const char *question, int type,
 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);
 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);
 
index f09329ba17e586f17d5eb15805f4799d7c57bbbf..463335a89034692d2ca1048948dc11d0f528ab5b 100644 (file)
@@ -621,7 +621,7 @@ void interface_shutdown(void)
        vlist_for_each_element(&interfaces, iface, node)
                if (iface->fd.fd > 0 && iface->multicast) {
                        service_announce(iface, 0);
-                       service_reply_a(iface, 0);
+                       dns_reply_a(iface, 0);
                }
        vlist_for_each_element(&interfaces, iface, node)
                interface_close(iface);
index 8d514c244b9b759008d3fe280657a45b4463e8a0..a3a5bb49be452382d803b56d3a315f923f1faf85 100644 (file)
--- a/service.c
+++ b/service.c
@@ -15,7 +15,6 @@
 #include <arpa/nameser.h>
 #include <sys/socket.h>
 
-#include <ifaddrs.h>
 #include <resolv.h>
 #include <glob.h>
 #include <stdio.h>
@@ -118,35 +117,6 @@ service_timeout(struct service *s)
        return t;
 }
 
-void
-service_reply_a(struct interface *iface, 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) {
-                       uint8_t ll_prefix[] = {0xfe, 0x80 };
-                       sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
-                       if (!memcmp(&sa6->sin6_addr, &ll_prefix, 2))
-                               dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl);
-               }
-       }
-       dns_send_answer(iface, mdns_hostname_local);
-
-       freeifaddrs(ifap);
-}
-
 static void
 service_reply_single(struct interface *iface, struct service *s, const char *match, int ttl, int force)
 {
@@ -188,7 +158,7 @@ service_reply(struct interface *iface, const char *match, int ttl)
                return;
 
        if (ttl)
-               service_reply_a(iface, ttl);
+               dns_reply_a(iface, ttl);
 }
 
 void
index 49c2aedb5067fd6b3a85ecd8c8a338925c041c7b..c2f51f4b6a236b046f2735cc12de779262966265 100644 (file)
--- a/service.h
+++ b/service.h
@@ -19,6 +19,5 @@ extern void service_cleanup(void);
 extern void service_announce(struct interface *iface, int ttl);
 extern void service_announce_services(struct interface *iface, const char *service, int ttl);
 extern void service_reply(struct interface *iface, const char *match, int ttl);
-extern void service_reply_a(struct interface *iface, int ttl);
 
 #endif