clean up hostname handling, make service_name() static
authorFelix Fietkau <nbd@openwrt.org>
Mon, 9 Jun 2014 21:58:10 +0000 (23:58 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Mon, 9 Jun 2014 21:58:10 +0000 (23:58 +0200)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
announce.c
dns.c
main.c
service.c
service.h
util.c
util.h

index 4706cc2b435fb1ebc5fb7cbfedee4c37dc26a875..481523c12a9a426ca773f6317b5d5fdd4224917e 100644 (file)
@@ -41,15 +41,12 @@ static void
 announce_timer(struct uloop_timeout *timeout)
 {
        struct interface *iface = container_of(timeout, struct interface, announce_timer);
-       char host[256];
-
-       snprintf(host, sizeof(host), "%s.local", hostname);
 
        switch (iface->announce_state) {
                case STATE_PROBE1:
                case STATE_PROBE2:
                case STATE_PROBE3:
-                       dns_send_question(iface, host, TYPE_ANY);
+                       dns_send_question(iface, mdns_hostname_local, TYPE_ANY);
                        uloop_timeout_set(timeout, 250);
                        iface->announce_state++;
                        break;
@@ -60,8 +57,8 @@ announce_timer(struct uloop_timeout *timeout)
                        break;
 
                case STATE_PROBE_END:
-                       if (cache_host_is_known(host)) {
-                               fprintf(stderr, "the host %s already exists. stopping announce service\n", host);
+                       if (cache_host_is_known(mdns_hostname_local)) {
+                               fprintf(stderr, "the host %s already exists. stopping announce service\n", mdns_hostname_local);
                                return;
                        }
                        iface->announce_state++;
diff --git a/dns.c b/dns.c
index 495177412fdbd2a38a1043ca2d61c29e4c16b145..c8d21006220f1fcb8bbef1e0e192724f31c42257 100644 (file)
--- a/dns.c
+++ b/dns.c
@@ -334,8 +334,7 @@ parse_question(struct interface *iface, char *name, struct dns_question *q)
 
        switch (q->type) {
        case TYPE_ANY:
-               host = service_name("local");
-               if (!strcmp(name, host))
+               if (!strcmp(name, mdns_hostname_local))
                        service_reply(iface, NULL);
                break;
 
@@ -349,7 +348,7 @@ parse_question(struct interface *iface, char *name, struct dns_question *q)
                host = strstr(name, ".local");
                if (host)
                        *host = '\0';
-               if (!strcmp(hostname, name))
+               if (!strcmp(mdns_hostname, name))
                        service_reply_a(iface, q->type);
                break;
        };
diff --git a/main.c b/main.c
index c23daf4f2715453c1616e4e3312bdf1c56ae3878..f07b9e67eba9d71e4d7ebf6730284ea82b3b7820 100644 (file)
--- a/main.c
+++ b/main.c
@@ -45,11 +45,8 @@ main(int argc, char **argv)
 {
        int ch, ttl;
 
-       while ((ch = getopt(argc, argv, "h:t:i:d")) != -1) {
+       while ((ch = getopt(argc, argv, "t:i:d")) != -1) {
                switch (ch) {
-               case 'h':
-                       hostname = optarg;
-                       break;
                case 't':
                        ttl = atoi(optarg);
                        if (ttl > 0)
index 30f477017996c06f13605c14b438ae9348d26ae1..8704a866d9ec6b2d200e86bb343b207ce2b9f285 100644 (file)
--- a/service.c
+++ b/service.c
@@ -68,16 +68,15 @@ 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);
-char *hostname = NULL;
 static char *sdudp =  "_services._dns-sd._udp.local";
 static char *sdtcp =  "_services._dns-sd._tcp.local";
 
-char *
+static const char *
 service_name(const char *domain)
 {
        static char buffer[256];
 
-       snprintf(buffer, sizeof(buffer), "%s.%s", hostname, domain);
+       snprintf(buffer, sizeof(buffer), "%s.%s", mdns_hostname, domain);
 
        return buffer;
 }
@@ -97,16 +96,14 @@ static void
 service_add_srv(struct service *s)
 {
        struct dns_srv_data *sd = (struct dns_srv_data *) mdns_buf;
-       char *host = service_name("local");
        int len = sizeof(*sd);
 
-       len += dn_comp(host, mdns_buf + len, sizeof(mdns_buf) - len, NULL, NULL);
+       len += dn_comp(mdns_hostname_local, mdns_buf + len, sizeof(mdns_buf) - len, NULL, NULL);
        if (len <= sizeof(*sd))
                return;
 
        sd->port = cpu_to_be16(s->port);
        dns_add_answer(TYPE_SRV, mdns_buf, len);
-       service_name(s->service);
 }
 
 #define TOUT_LOOKUP    60
@@ -132,7 +129,7 @@ service_reply_a(struct interface *iface, int type)
 
        dns_init_answer();
        dns_add_answer(TYPE_A, (uint8_t *) &iface->v4_addr.s_addr, 4);
-       dns_send_answer(iface, service_name("local"));
+       dns_send_answer(iface, mdns_hostname_local);
 }
 
 void
@@ -141,7 +138,7 @@ service_reply(struct interface *iface, const char *match)
        struct service *s;
 
        vlist_for_each_element(&services, s, node) {
-               char *host = service_name(s->service);
+               const char *host = service_name(s->service);
                char *service = strstr(host, "._");
 
                if (!s->active || !service || !service_timeout(s))
@@ -281,8 +278,7 @@ service_load(char *path)
 void
 service_init(void)
 {
-       if (!hostname)
-               hostname = get_hostname();
+       get_hostname();
 
        vlist_update(&services);
        service_load("/tmp/run/mdnsd/*");
index 6aeea0f695dfffd51957b6aa7b7aa35ea696fc3a..734e711979413b5405944d342a0ca40023b2b6fc 100644 (file)
--- a/service.h
+++ b/service.h
@@ -14,8 +14,6 @@
 #ifndef _SERVICE_H__
 #define _SERVICE_H__
 
-extern char *hostname;
-extern char *service_name(const char *domain);
 extern void service_init(void);
 extern void service_cleanup(void);
 extern void service_announce(struct interface *iface);
diff --git a/util.c b/util.c
index 87db94c6ee67652fb56babb5aa358c3644b9f68d..05f02b2061181e03c167aa3737b8fbd8d797c091 100644 (file)
--- a/util.c
+++ b/util.c
@@ -36,6 +36,9 @@
 uint8_t mdns_buf[MDNS_BUF_LEN];
 int debug = 0;
 
+char mdns_hostname[HOSTNAME_LEN];
+char mdns_hostname_local[HOSTNAME_LEN + 6];
+
 static void
 signal_shutdown(int signal)
 {
@@ -73,15 +76,18 @@ rand_time_delta(uint32_t t)
        return val;
 }
 
-char*
-get_hostname(void)
+void get_hostname(void)
 {
-       static struct utsname utsname;
+       struct utsname utsname;
+
+       mdns_hostname[0] = 0;
+       mdns_hostname_local[0] = 0;
 
        if (uname(&utsname) < 0)
-               return NULL;
+               return;
 
-       return utsname.nodename;
+       snprintf(mdns_hostname, sizeof(mdns_hostname), "%s", utsname.nodename);
+       snprintf(mdns_hostname_local, sizeof(mdns_hostname_local), "%s.local", utsname.nodename);
 }
 
 void*
diff --git a/util.h b/util.h
index 140e1da5534c74abe0b94453d4e0eaaf25156bee..ab38b7a0fbb43037c6a03cdc5d54602f15995170 100644 (file)
--- a/util.h
+++ b/util.h
        } while (0)
 
 #define MDNS_BUF_LEN   (8 * 1024)
+#define HOSTNAME_LEN   256
 
 extern int debug;
 extern uint8_t mdns_buf[MDNS_BUF_LEN];
+extern char mdns_hostname[HOSTNAME_LEN];
+extern char mdns_hostname_local[HOSTNAME_LEN + 6];
 
 void *memdup(const void *d, int l);
 
 extern void signal_setup(void);
-extern char* get_hostname(void);
+extern void get_hostname(void);
 extern uint32_t rand_time_delta(uint32_t t);
 
 #endif