[LEDE-DEV] [PATCH umdns 3/3] Support specifying instance name in JSON file

Rafał Miłecki zajec5 at gmail.com
Wed May 10 03:47:34 PDT 2017


From: Rafał Miłecki <rafal at milecki.pl>

So far we were using host label as the instance name for every service.
This change allows specifying it manually and fallbacks to the label for
backward compatibility.

Signed-off-by: Rafał Miłecki <rafal at milecki.pl>
---
 service.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/service.c b/service.c
index c8187de..253dec3 100644
--- a/service.c
+++ b/service.c
@@ -34,6 +34,7 @@
 #include "announce.h"
 
 enum {
+	SERVICE_INSTANCE,
 	SERVICE_SERVICE,
 	SERVICE_PORT,
 	SERVICE_TXT,
@@ -46,6 +47,7 @@ struct service {
 	time_t t;
 
 	const char *id;
+	const char *instance;
 	const char *service;
 	const uint8_t *txt;
 	int txt_len;
@@ -54,6 +56,7 @@ struct service {
 };
 
 static const struct blobmsg_policy service_policy[__SERVICE_MAX] = {
+	[SERVICE_INSTANCE] = { .name = "instance", .type = BLOBMSG_TYPE_STRING },
 	[SERVICE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
 	[SERVICE_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
 	[SERVICE_TXT] = { .name = "txt", .type = BLOBMSG_TYPE_ARRAY },
@@ -74,14 +77,15 @@ static int service_init_announce;
  *
  * Service Instance Name = <Instance> . <Service> . <Domain>
  *
- * @service_domain: service name (a pair of labels) with domain name appended
+ * @s: service to generate service instance name for
  */
 static const char *
-service_instance_name(const char *service_domain)
+service_instance_name(struct service *s)
 {
+	const char *instance = s->instance ? : umdns_host_label;
 	static char buffer[256];
 
-	snprintf(buffer, sizeof(buffer), "%s.%s", umdns_host_label, service_domain);
+	snprintf(buffer, sizeof(buffer), "%s.%s", instance, s->service);
 
 	return buffer;
 }
@@ -127,7 +131,7 @@ service_timeout(struct service *s)
 static void
 service_reply_single(struct interface *iface, struct sockaddr *to, struct service *s, int ttl, int force)
 {
-	const char *host = service_instance_name(s->service);
+	const char *host = service_instance_name(s);
 	char *service = strstr(host, "._");
 	time_t t = service_timeout(s);
 
@@ -140,7 +144,7 @@ service_reply_single(struct interface *iface, struct sockaddr *to, struct servic
 	s->t = t;
 
 	dns_init_answer();
-	service_add_ptr(service_instance_name(s->service), ttl);
+	service_add_ptr(service_instance_name(s), ttl);
 	dns_send_answer(iface, to, service);
 
 	dns_init_answer();
@@ -206,7 +210,7 @@ service_load_blob(struct blob_attr *b)
 {
 	struct blob_attr *txt, *_tb[__SERVICE_MAX];
 	struct service *s;
-	char *d_service, *d_id;
+	char *d_instance, *d_service, *d_id;
 	uint8_t *d_txt;
 	int rem2;
 	int txt_len = 0;
@@ -222,6 +226,7 @@ service_load_blob(struct blob_attr *b)
 
 	s = calloc_a(sizeof(*s),
 		&d_id, strlen(blobmsg_name(b)) + 1,
+		&d_instance, _tb[SERVICE_INSTANCE] ? strlen(blobmsg_get_string(_tb[SERVICE_INSTANCE])) + 1 : 0,
 		&d_service, strlen(blobmsg_get_string(_tb[SERVICE_SERVICE])) + 1,
 		&d_txt, txt_len);
 	if (!s)
@@ -229,6 +234,8 @@ service_load_blob(struct blob_attr *b)
 
 	s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
 	s->id = strcpy(d_id, blobmsg_name(b));
+	if (_tb[SERVICE_INSTANCE])
+		s->instance = strcpy(d_instance, blobmsg_get_string(_tb[SERVICE_INSTANCE]));
 	s->service = strcpy(d_service, blobmsg_get_string(_tb[SERVICE_SERVICE]));
 	s->active = 1;
 	s->t = 0;
-- 
2.11.0




More information about the Lede-dev mailing list