From 444987db98c88dfef6b3965051bf2aba6114aaa2 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Mon, 9 Jun 2014 23:58:10 +0200 Subject: [PATCH] clean up hostname handling, make service_name() static Signed-off-by: Felix Fietkau --- announce.c | 9 +++------ dns.c | 5 ++--- main.c | 5 +---- service.c | 16 ++++++---------- service.h | 2 -- util.c | 16 +++++++++++----- util.h | 5 ++++- 7 files changed, 27 insertions(+), 31 deletions(-) diff --git a/announce.c b/announce.c index 4706cc2..481523c 100644 --- a/announce.c +++ b/announce.c @@ -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 4951774..c8d2100 100644 --- 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 c23daf4..f07b9e6 100644 --- 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) diff --git a/service.c b/service.c index 30f4770..8704a86 100644 --- 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/*"); diff --git a/service.h b/service.h index 6aeea0f..734e711 100644 --- 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 87db94c..05f02b2 100644 --- 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 140e1da..ab38b7a 100644 --- a/util.h +++ b/util.h @@ -22,14 +22,17 @@ } 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 -- 2.30.2