service: simplify and remove more code duplication
[project/mdnsd.git] / service.c
index 4a9ccc8..1cf50e3 100644 (file)
--- a/service.c
+++ b/service.c
  */
 
 #include <sys/types.h>
  */
 
 #include <sys/types.h>
-#include <netinet/in.h>
 #include <arpa/nameser.h>
 #include <sys/socket.h>
 #include <arpa/nameser.h>
 #include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 
 #include <resolv.h>
 #include <glob.h>
 
 #include <resolv.h>
 #include <glob.h>
@@ -86,10 +83,9 @@ service_name(const char *domain)
 }
 
 static void
 }
 
 static void
-service_add_ptr(struct service *s)
+service_add_ptr(const char *host)
 {
        unsigned char buffer[MAX_NAME_LEN];
 {
        unsigned char buffer[MAX_NAME_LEN];
-       const char *host = service_name(s->service);
        int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
 
        if (len < 1)
        int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
 
        if (len < 1)
@@ -99,37 +95,6 @@ service_add_ptr(struct service *s)
 }
 
 static void
 }
 
 static void
-service_add_ptr_c(const char *host)
-{
-       unsigned char buffer[MAX_NAME_LEN];
-       int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
-
-       if (len < 1)
-               return;
-
-       dns_add_answer(TYPE_PTR, buffer, len);
-}
-
-static void
-service_send_a(struct interface *iface)
-{
-       unsigned char buffer[MAX_NAME_LEN];
-       char *host = service_name("local");
-       int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
-       struct in_addr in;
-
-       if (!inet_aton(iface->ip, &in)) {
-               fprintf(stderr, "%s is not valid\n", iface->ip);
-               return;
-       }
-
-       if (len < 1)
-               return;
-
-       dns_add_answer(TYPE_A, (uint8_t *) &in.s_addr, 4);
-}
-
-static void
 service_add_srv(struct service *s)
 {
        unsigned char buffer[MAX_NAME_LEN];
 service_add_srv(struct service *s)
 {
        unsigned char buffer[MAX_NAME_LEN];
@@ -173,7 +138,7 @@ service_reply_a(struct interface *iface, int type)
                return;
 
        dns_init_answer();
                return;
 
        dns_init_answer();
-       service_send_a(iface);
+       dns_add_answer(TYPE_A, (uint8_t *) &iface->v4_addr.s_addr, 4);
        dns_send_answer(iface, service_name("local"));
 }
 
        dns_send_answer(iface, service_name("local"));
 }
 
@@ -195,7 +160,7 @@ service_reply(struct interface *iface, const char *match)
                        continue;
 
                dns_init_answer();
                        continue;
 
                dns_init_answer();
-               service_add_ptr(s);
+               service_add_ptr(service_name(s->service));
                dns_send_answer(iface, service);
 
                dns_init_answer();
                dns_send_answer(iface, service);
 
                dns_init_answer();
@@ -208,9 +173,7 @@ service_reply(struct interface *iface, const char *match)
        if (match)
                return;
 
        if (match)
                return;
 
-       dns_init_answer();
-       service_send_a(iface);
-       dns_send_answer(iface, service_name("local"));
+       service_reply_a(iface, TYPE_A);
 }
 
 void
 }
 
 void
@@ -231,7 +194,7 @@ service_announce_services(struct interface *iface, const char *service)
                        continue;
                s->t = 0;
                dns_init_answer();
                        continue;
                s->t = 0;
                dns_init_answer();
-               service_add_ptr_c(s->service);
+               service_add_ptr(s->service);
                if (tcp)
                        dns_send_answer(iface, sdtcp);
                else
                if (tcp)
                        dns_send_answer(iface, sdtcp);
                else
@@ -337,4 +300,5 @@ void
 service_cleanup(void)
 {
        vlist_flush(&services);
 service_cleanup(void)
 {
        vlist_flush(&services);
+       blob_buf_free(&b);
 }
 }