#include <resolv.h>
#include <glob.h>
+#include <inttypes.h>
#include <stdio.h>
#include <time.h>
SERVICE_SERVICE,
SERVICE_PORT,
SERVICE_TXT,
+ SERVICE_HOSTNAME,
__SERVICE_MAX,
};
[SERVICE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
[SERVICE_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
[SERVICE_TXT] = { .name = "txt", .type = BLOBMSG_TYPE_ARRAY },
+ [SERVICE_HOSTNAME] = { .name = "hostname", .type = BLOBMSG_TYPE_STRING },
};
static void
service_update(struct vlist_tree *tree, struct vlist_node *node_new,
struct vlist_node *node_old);
+static void
+hostname_update(struct vlist_tree *tree, struct vlist_node *node_new,
+ struct vlist_node *node_old);
+
static struct blob_buf b;
static VLIST_TREE(services, avl_strcmp, service_update, false, false);
+VLIST_TREE(hostnames, avl_strcmp, hostname_update, false, false);
static int service_init_announce;
/**
time_t t = monotonic_time();
if (t - s->t <= TOUT_LOOKUP) {
- DBG(2, "t=%lu, s->t=%lu, t - s->t = %lu\n", t, s->t, t - s->t);
+ DBG(2, "t=%" PRId64 ", s->t=%" PRId64 ", t - s->t = %" PRId64 "\n", (int64_t)t, (int64_t)s->t, (int64_t)(t - s->t));
return 0;
}
free(s);
}
+static void
+hostname_update(struct vlist_tree *tree, struct vlist_node *node_new,
+ struct vlist_node *node_old)
+{
+ struct interface *iface;
+ struct hostname *h;
+
+ if (!node_old) {
+ h = container_of(node_new, struct hostname, node);
+ vlist_for_each_element(&interfaces, iface, node)
+ dns_reply_a(iface, NULL, announce_ttl, h->hostname);
+ return;
+ }
+
+ h = container_of(node_old, struct hostname, node);
+ if (!node_new)
+ vlist_for_each_element(&interfaces, iface, node)
+ dns_reply_a(iface, NULL, 0, h->hostname);
+
+ free(h);
+}
+
+static void
+service_load_hostname(struct blob_attr *b)
+{
+ struct hostname *h;
+ char *hostname, *d_hostname;
+
+ hostname = blobmsg_get_string(b);
+ h = calloc_a(sizeof(*h), &d_hostname, strlen(hostname) + 1);
+ if (!h)
+ return;
+
+ h->hostname = strcpy(d_hostname, hostname);
+
+ vlist_add(&hostnames, &h->node, h->hostname);
+}
+
static void
service_load_blob(struct blob_attr *b)
{
blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
_tb, blobmsg_data(b), blobmsg_data_len(b));
+
+ if (_tb[SERVICE_HOSTNAME]) {
+ service_load_hostname(_tb[SERVICE_HOSTNAME]);
+ return;
+ }
+
if (!_tb[SERVICE_PORT] || !_tb[SERVICE_SERVICE])
return;
blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
txt_len += 1 + strlen(blobmsg_get_string(txt));
- n = strlen(blobmsg_name(b)) + 1;
+ n = strlen(blobmsg_name(b));
s = calloc_a(sizeof(*s),
- &d_id, n,
+ &d_id, n + 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);
get_hostname();
vlist_update(&services);
+ vlist_update(&hostnames);
service_load("/etc/umdns/*");
blob_for_each_attr(cur, msg, rem) {
}
}
vlist_flush(&services);
+ vlist_flush(&hostnames);
}
void