From 0c6716b4283e93379b89a21663abd40efa13151e Mon Sep 17 00:00:00 2001 From: John Crispin Date: Fri, 29 Aug 2014 09:01:31 +0200 Subject: [PATCH] send unsolicitated answers when a new service appears Signed-off-by: John Crispin --- main.c | 2 +- service.c | 26 +++++++++++++++++--------- service.h | 2 +- ubus.c | 2 +- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/main.c b/main.c index f559c19..93db2e5 100644 --- a/main.c +++ b/main.c @@ -77,7 +77,7 @@ main(int argc, char **argv) if (cache_init()) return -1; - service_init(); + service_init(0); ubus_startup(); uloop_run(); diff --git a/service.c b/service.c index 01a55b6..610cac5 100644 --- a/service.c +++ b/service.c @@ -67,6 +67,7 @@ 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"; +static int service_init_announce; static const char * service_name(const char *domain) @@ -148,12 +149,12 @@ service_reply_a(struct interface *iface, int type, int ttl) } static void -service_reply_single(struct interface *iface, struct service *s, const char *match, int ttl) +service_reply_single(struct interface *iface, struct service *s, const char *match, int ttl, int force) { const char *host = service_name(s->service); char *service = strstr(host, "._"); - if (!s->active || !service || !service_timeout(s)) + if (!force && (!s->active || !service || !service_timeout(s))) return; service++; @@ -178,7 +179,7 @@ service_reply(struct interface *iface, const char *match, int ttl) struct service *s; vlist_for_each_element(&services, s, node) - service_reply_single(iface, s, match, ttl); + service_reply_single(iface, s, match, ttl, 0); if (match) return; @@ -227,15 +228,20 @@ service_update(struct vlist_tree *tree, struct vlist_node *node_new, struct interface *iface; struct service *s; - if (!node_old) + if (!node_old) { + s = container_of(node_new, struct service, node); + if (service_init_announce) + vlist_for_each_element(&interfaces, iface, node) { + s->t = 0; + service_reply_single(iface, s, NULL, announce_ttl, 1); + } return; + } s = container_of(node_old, struct service, node); - - if (!node_new) + if (!node_new && service_init_announce) vlist_for_each_element(&interfaces, iface, node) - service_reply_single(iface, s, NULL, 0); - + service_reply_single(iface, s, NULL, 0, 1); free(s); } @@ -302,8 +308,10 @@ service_load(char *path) } void -service_init(void) +service_init(int announce) { + service_init_announce = announce; + get_hostname(); vlist_update(&services); diff --git a/service.h b/service.h index cc49abe..e17c860 100644 --- a/service.h +++ b/service.h @@ -14,7 +14,7 @@ #ifndef _SERVICE_H__ #define _SERVICE_H__ -extern void service_init(void); +extern void service_init(int announce); extern void service_cleanup(void); extern void service_announce(struct interface *iface); extern void service_announce_services(struct interface *iface, const char *service); diff --git a/ubus.c b/ubus.c index c5c8e04..b9bb9ee 100644 --- a/ubus.c +++ b/ubus.c @@ -34,7 +34,7 @@ mdns_reload(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { - service_init(); + service_init(1); return 0; } -- 2.30.2