123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- /*
- * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License version 2.1
- * as published by the Free Software Foundation
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
- #include <sys/types.h>
- #include <arpa/nameser.h>
- #include <sys/socket.h>
- #include <resolv.h>
- #include <glob.h>
- #include <stdio.h>
- #include <time.h>
- #include <uci.h>
- #include <uci_blob.h>
- #include <libubox/vlist.h>
- #include <libubox/uloop.h>
- #include <libubox/avl-cmp.h>
- #include <libubox/blobmsg_json.h>
- #include "dns.h"
- #include "service.h"
- #include "util.h"
- #include "interface.h"
- enum {
- SERVICE_PORT,
- SERVICE_TXT,
- __SERVICE_MAX,
- };
- struct service {
- struct vlist_node node;
- time_t t;
- const char *service;
- const char *daemon;
- const uint8_t *txt;
- int txt_len;
- int port;
- int active;
- };
- static const struct blobmsg_policy service_policy[__SERVICE_MAX] = {
- [SERVICE_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
- [SERVICE_TXT] = { .name = "txt", .type = BLOBMSG_TYPE_ARRAY },
- };
- static const struct uci_blob_param_list service_attr_list = {
- .n_params = __SERVICE_MAX,
- .params = service_policy,
- };
- static void
- service_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);
- char *hostname = NULL;
- static char *sdudp = "_services._dns-sd._udp.local";
- static char *sdtcp = "_services._dns-sd._tcp.local";
- char *
- service_name(const char *domain)
- {
- static char buffer[256];
- snprintf(buffer, sizeof(buffer), "%s.%s", hostname, domain);
- return buffer;
- }
- static void
- service_add_ptr(const char *host)
- {
- unsigned char buffer[MAX_NAME_LEN];
- int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
- if (len < 1)
- return;
- dns_add_answer(TYPE_PTR, buffer, len);
- }
- static void
- service_add_srv(struct service *s)
- {
- unsigned char buffer[MAX_NAME_LEN];
- struct dns_srv_data *sd;
- char *host = service_name("local");
- int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
- if (len < 1)
- return;
- sd = calloc(1, len + sizeof(struct dns_srv_data));
- if (!sd)
- return;
- sd->port = cpu_to_be16(s->port);
- memcpy(&sd[1], buffer, len);
- host = service_name(s->service);
- dns_add_answer(TYPE_SRV, (uint8_t *) sd, len + sizeof(struct dns_srv_data));
- free(sd);
- }
- #define TOUT_LOOKUP 60
- static int
- service_timeout(struct service *s)
- {
- time_t t = time(NULL);
- if (t - s->t <= TOUT_LOOKUP)
- return 0;
- s->t = t;
- return 1;
- }
- void
- service_reply_a(struct interface *iface, int type)
- {
- if (type != TYPE_A)
- return;
- dns_init_answer();
- dns_add_answer(TYPE_A, (uint8_t *) &iface->v4_addr.s_addr, 4);
- dns_send_answer(iface, service_name("local"));
- }
- void
- service_reply(struct interface *iface, const char *match)
- {
- struct service *s;
- vlist_for_each_element(&services, s, node) {
- char *host = service_name(s->service);
- char *service = strstr(host, "._");
- if (!s->active || !service || !service_timeout(s))
- continue;
- service++;
- if (match && strcmp(match, s->service))
- continue;
- dns_init_answer();
- service_add_ptr(service_name(s->service));
- dns_send_answer(iface, service);
- dns_init_answer();
- service_add_srv(s);
- if (s->txt && s->txt_len)
- dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len);
- dns_send_answer(iface, host);
- }
- if (match)
- return;
- service_reply_a(iface, TYPE_A);
- }
- void
- service_announce_services(struct interface *iface, const char *service)
- {
- struct service *s;
- int tcp = 1;
- if (!strcmp(service, sdudp))
- tcp = 0;
- else if (strcmp(service, sdtcp))
- return;
- vlist_for_each_element(&services, s, node) {
- if (!strstr(s->service, "._tcp") && tcp)
- continue;
- if (!strstr(s->service, "._udp") && !tcp)
- continue;
- s->t = 0;
- dns_init_answer();
- service_add_ptr(s->service);
- if (tcp)
- dns_send_answer(iface, sdtcp);
- else
- dns_send_answer(iface, sdudp);
- service_reply(iface, s->service);
- }
- }
- void
- service_announce(struct interface *iface)
- {
- service_announce_services(iface, sdudp);
- service_announce_services(iface, sdtcp);
- }
- static void
- service_update(struct vlist_tree *tree, struct vlist_node *node_new,
- struct vlist_node *node_old)
- {
- struct service *s;
- if (!node_old)
- return;
- s = container_of(node_old, struct service, node);
- free(s);
- }
- static void
- service_load(char *path)
- {
- struct blob_attr *txt, *cur, *_tb[__SERVICE_MAX];
- int rem, i;
- glob_t gl;
- if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
- return;
- for (i = 0; i < gl.gl_pathc; i++) {
- blob_buf_init(&b, 0);
- if (!blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
- continue;
- blob_for_each_attr(cur, b.head, rem) {
- struct service *s;
- char *d_service, *d_daemon;
- uint8_t *d_txt;
- int rem2;
- int txt_len = 0;
- blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
- _tb, blobmsg_data(cur), blobmsg_data_len(cur));
- if (!_tb[SERVICE_PORT] || !_tb[SERVICE_TXT])
- continue;
- blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
- txt_len += 1 + strlen(blobmsg_get_string(txt));
- s = calloc_a(sizeof(*s),
- &d_daemon, strlen(gl.gl_pathv[i]) + 1,
- &d_service, strlen(blobmsg_name(cur)) + 1,
- &d_txt, txt_len);
- if (!s)
- continue;
- s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
- s->service = strcpy(d_service, blobmsg_name(cur));
- s->daemon = strcpy(d_daemon, gl.gl_pathv[i]);
- s->active = 1;
- s->t = 0;
- s->txt_len = txt_len;
- s->txt = d_txt;
- blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
- int len = strlen(blobmsg_get_string(txt));
- if (!len)
- continue;
- if (len > 0xff)
- len = 0xff;
- *d_txt = len;
- d_txt++;
- memcpy(d_txt, blobmsg_get_string(txt), len);
- d_txt += len;
- }
- vlist_add(&services, &s->node, s->service);
- }
- }
- }
- void
- service_init(void)
- {
- if (!hostname)
- hostname = get_hostname();
- vlist_update(&services);
- service_load("/tmp/run/mdnsd/*");
- vlist_flush(&services);
- }
- void
- service_cleanup(void)
- {
- vlist_flush(&services);
- blob_buf_free(&b);
- }
|