Simplify UDP vs. TCP handling in service_announce_services
authorRafał Miłecki <rafal@milecki.pl>
Fri, 10 Feb 2017 21:55:00 +0000 (22:55 +0100)
committerRafał Miłecki <rafal@milecki.pl>
Fri, 10 Feb 2017 21:55:00 +0000 (22:55 +0100)
Out of 3 calls of this function only one doesn't have TCP vs. UDP
hardcoded. It's easier to move string check to that place and make this
function take "int tcp" argument instead.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
dns.c
service.c
service.h

diff --git a/dns.c b/dns.c
index de0c21ade1b79f87244572732ac4ddba580d77f6..550befc24c1593ca2f7bcef14fed22456304908f 100644 (file)
--- a/dns.c
+++ b/dns.c
@@ -367,7 +367,10 @@ parse_question(struct interface *iface, char *name, struct dns_question *q)
                break;
 
        case TYPE_PTR:
-               service_announce_services(iface, name, announce_ttl);
+               if (!strcmp(name, sdudp))
+                       service_announce_services(iface, 0, announce_ttl);
+               else if (!strcmp(name, sdtcp))
+                       service_announce_services(iface, 1, announce_ttl);
                service_reply(iface, name, announce_ttl);
                break;
 
index 45b93459034c44c9234255916cb0f0f2b6ddaa56..e375fced85ffb8f1a13a8d0b4cd8933a06c79f56 100644 (file)
--- a/service.c
+++ b/service.c
@@ -65,8 +65,8 @@ service_update(struct vlist_tree *tree, struct vlist_node *node_new,
 
 static struct blob_buf b;
 static VLIST_TREE(services, avl_strcmp, service_update, false, false);
-static char *sdudp =  "_services._dns-sd._udp.local";
-static char *sdtcp =  "_services._dns-sd._tcp.local";
+char *sdudp =  "_services._dns-sd._udp.local";
+char *sdtcp =  "_services._dns-sd._tcp.local";
 static int service_init_announce;
 
 static const char *
@@ -155,15 +155,9 @@ service_reply(struct interface *iface, const char *match, int ttl)
 }
 
 void
-service_announce_services(struct interface *iface, const char *service, int ttl)
+service_announce_services(struct interface *iface, int tcp, int ttl)
 {
        struct service *s;
-       int tcp = 1;
-
-       if (!strcmp(service, sdudp))
-               tcp = 0;
-       else if (strcmp(service, sdtcp))
-               return;
 
        vlist_for_each_element(&services, s, node) {
                if (!strstr(s->service, "._tcp") && tcp)
@@ -186,8 +180,8 @@ service_announce_services(struct interface *iface, const char *service, int ttl)
 void
 service_announce(struct interface *iface, int ttl)
 {
-       service_announce_services(iface, sdudp, ttl);
-       service_announce_services(iface, sdtcp, ttl);
+       service_announce_services(iface, 0, ttl);
+       service_announce_services(iface, 1, ttl);
 }
 
 static void
index c2f51f4b6a236b046f2735cc12de779262966265..901bcbd007cab75785bc6f646a0211ba34738661 100644 (file)
--- a/service.h
+++ b/service.h
 #ifndef _SERVICE_H__
 #define _SERVICE_H__
 
+extern char *sdudp;
+extern char *sdtcp;
 extern void service_init(int announce);
 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_announce_services(struct interface *iface, int tcp, int ttl);
 
 #endif