2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <sys/types.h>
15 #include <arpa/nameser.h>
16 #include <sys/socket.h>
25 #include <libubox/vlist.h>
26 #include <libubox/uloop.h>
27 #include <libubox/avl-cmp.h>
28 #include <libubox/blobmsg_json.h>
34 #include "interface.h"
47 struct vlist_node node
;
60 static const struct blobmsg_policy service_policy
[__SERVICE_MAX
] = {
61 [SERVICE_INSTANCE
] = { .name
= "instance", .type
= BLOBMSG_TYPE_STRING
},
62 [SERVICE_SERVICE
] = { .name
= "service", .type
= BLOBMSG_TYPE_STRING
},
63 [SERVICE_PORT
] = { .name
= "port", .type
= BLOBMSG_TYPE_INT32
},
64 [SERVICE_TXT
] = { .name
= "txt", .type
= BLOBMSG_TYPE_ARRAY
},
65 [SERVICE_HOSTNAME
] = { .name
= "hostname", .type
= BLOBMSG_TYPE_STRING
},
69 service_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
70 struct vlist_node
*node_old
);
73 hostname_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
74 struct vlist_node
*node_old
);
76 static struct blob_buf b
;
77 static VLIST_TREE(services
, avl_strcmp
, service_update
, false, false);
78 VLIST_TREE(hostnames
, avl_strcmp
, hostname_update
, false, false);
79 static int service_init_announce
;
82 * service_instance_name - construct Service Instance Name as in RFC 6763
84 * RFC 6763 specifies Service Instance Names in the following way:
86 * Service Instance Name = <Instance> . <Service> . <Domain>
88 * @s: service to generate service instance name for
91 service_instance_name(struct service
*s
)
93 static char buffer
[256];
95 snprintf(buffer
, sizeof(buffer
), "%s.%s", s
->instance
, s
->service
);
101 service_add_ptr(const char *host
, int ttl
)
103 int len
= dn_comp(host
, mdns_buf
, sizeof(mdns_buf
), NULL
, NULL
);
108 dns_add_answer(TYPE_PTR
, mdns_buf
, len
, ttl
);
112 service_add_srv(struct service
*s
, int ttl
)
114 struct dns_srv_data
*sd
= (struct dns_srv_data
*) mdns_buf
;
115 int len
= sizeof(*sd
);
117 len
+= dn_comp(mdns_hostname_local
, mdns_buf
+ len
, sizeof(mdns_buf
) - len
, NULL
, NULL
);
118 if (len
<= sizeof(*sd
))
121 sd
->port
= cpu_to_be16(s
->port
);
122 dns_add_answer(TYPE_SRV
, mdns_buf
, len
, ttl
);
125 #define TOUT_LOOKUP 60
128 service_timeout(struct service
*s
)
130 time_t t
= monotonic_time();
132 if (t
- s
->t
<= TOUT_LOOKUP
) {
133 DBG(2, "t=%" PRId64
", s->t=%" PRId64
", t - s->t = %" PRId64
"\n", (int64_t)t
, (int64_t)s
->t
, (int64_t)(t
- s
->t
));
141 service_reply_single(struct interface
*iface
, struct sockaddr
*to
, struct service
*s
, int ttl
, int force
)
143 const char *host
= service_instance_name(s
);
144 char *service
= strstr(host
, "._");
145 time_t t
= service_timeout(s
);
148 if (!force
&& (!s
->active
|| !service
|| !t
))
156 service_add_ptr(service_instance_name(s
), ttl
);
157 dns_send_answer(iface
, to
, service
);
160 service_add_srv(s
, ttl
);
161 if (s
->txt
&& s
->txt_len
)
162 dns_add_answer(TYPE_TXT
, (uint8_t *) s
->txt
, s
->txt_len
, ttl
);
163 dns_send_answer(iface
, to
, host
);
167 service_reply(struct interface
*iface
, struct sockaddr
*to
, const char *instance
, const char *service_domain
, int ttl
)
171 vlist_for_each_element(&services
, s
, node
) {
172 if (instance
&& strcmp(s
->instance
, instance
))
174 if (service_domain
&& strcmp(s
->service
, service_domain
))
176 service_reply_single(iface
, to
, s
, ttl
, 0);
181 service_announce_services(struct interface
*iface
, struct sockaddr
*to
, int ttl
)
185 vlist_for_each_element(&services
, s
, node
) {
189 service_add_ptr(s
->service
, ttl
);
190 dns_send_answer(iface
, to
, C_DNS_SD
);
192 service_reply_single(iface
, to
, s
, ttl
, 0);
197 service_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
198 struct vlist_node
*node_old
)
200 struct interface
*iface
;
204 s
= container_of(node_new
, struct service
, node
);
205 if (service_init_announce
)
206 vlist_for_each_element(&interfaces
, iface
, node
) {
208 service_reply_single(iface
, NULL
, s
, announce_ttl
, 1);
213 s
= container_of(node_old
, struct service
, node
);
214 if (!node_new
&& service_init_announce
)
215 vlist_for_each_element(&interfaces
, iface
, node
)
216 service_reply_single(iface
, NULL
, s
, 0, 1);
221 hostname_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
222 struct vlist_node
*node_old
)
224 struct interface
*iface
;
228 h
= container_of(node_new
, struct hostname
, node
);
229 vlist_for_each_element(&interfaces
, iface
, node
)
230 dns_reply_a(iface
, NULL
, announce_ttl
, h
->hostname
);
234 h
= container_of(node_old
, struct hostname
, node
);
236 vlist_for_each_element(&interfaces
, iface
, node
)
237 dns_reply_a(iface
, NULL
, 0, h
->hostname
);
243 service_load_hostname(struct blob_attr
*b
)
246 char *hostname
, *d_hostname
;
248 hostname
= blobmsg_get_string(b
);
249 h
= calloc_a(sizeof(*h
), &d_hostname
, strlen(hostname
) + 1);
253 h
->hostname
= strcpy(d_hostname
, hostname
);
255 vlist_add(&hostnames
, &h
->node
, h
->hostname
);
259 service_load_blob(struct blob_attr
*b
)
261 struct blob_attr
*txt
, *_tb
[__SERVICE_MAX
];
263 char *d_instance
, *d_service
, *d_id
;
269 blobmsg_parse(service_policy
, ARRAY_SIZE(service_policy
),
270 _tb
, blobmsg_data(b
), blobmsg_data_len(b
));
272 if (_tb
[SERVICE_HOSTNAME
]) {
273 service_load_hostname(_tb
[SERVICE_HOSTNAME
]);
277 if (!_tb
[SERVICE_PORT
] || !_tb
[SERVICE_SERVICE
])
280 if (_tb
[SERVICE_TXT
])
281 blobmsg_for_each_attr(txt
, _tb
[SERVICE_TXT
], rem2
)
282 txt_len
+= 1 + strlen(blobmsg_get_string(txt
));
284 n
= strlen(blobmsg_name(b
));
285 s
= calloc_a(sizeof(*s
),
287 &d_instance
, _tb
[SERVICE_INSTANCE
] ? strlen(blobmsg_get_string(_tb
[SERVICE_INSTANCE
])) + 1 : 0,
288 &d_service
, strlen(blobmsg_get_string(_tb
[SERVICE_SERVICE
])) + 1,
293 s
->port
= blobmsg_get_u32(_tb
[SERVICE_PORT
]);
294 s
->id
= strncpy(d_id
, blobmsg_name(b
), n
);
295 if (_tb
[SERVICE_INSTANCE
])
296 s
->instance
= strcpy(d_instance
, blobmsg_get_string(_tb
[SERVICE_INSTANCE
]));
298 s
->instance
= umdns_host_label
;
299 s
->service
= strcpy(d_service
, blobmsg_get_string(_tb
[SERVICE_SERVICE
]));
302 s
->txt_len
= txt_len
;
305 if (_tb
[SERVICE_TXT
])
306 blobmsg_for_each_attr(txt
, _tb
[SERVICE_TXT
], rem2
) {
307 int len
= strlen(blobmsg_get_string(txt
));
314 memcpy(d_txt
, blobmsg_get_string(txt
), len
);
318 vlist_add(&services
, &s
->node
, s
->id
);
322 service_load(char *path
)
324 struct blob_attr
*cur
;
328 if (glob(path
, GLOB_NOESCAPE
| GLOB_MARK
, NULL
, &gl
))
331 for (i
= 0; i
< gl
.gl_pathc
; i
++) {
332 blob_buf_init(&b
, 0);
333 if (blobmsg_add_json_from_file(&b
, gl
.gl_pathv
[i
])) {
334 blob_for_each_attr(cur
, b
.head
, rem
)
335 service_load_blob(cur
);
337 fprintf(stderr
, "Error reading %s JSON\n", gl
.gl_pathv
[i
]);
344 service_init_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
346 struct blob_attr
*cur
;
351 vlist_update(&services
);
352 vlist_update(&hostnames
);
353 service_load("/etc/umdns/*");
355 blob_for_each_attr(cur
, msg
, rem
) {
356 struct blob_attr
*cur2
;
359 blobmsg_for_each_attr(cur2
, cur
, rem2
) {
360 struct blob_attr
*cur3
;
363 if (strcmp(blobmsg_name(cur2
), "instances"))
366 blobmsg_for_each_attr(cur3
, cur2
, rem3
) {
367 struct blob_attr
*cur4
;
371 blobmsg_for_each_attr(cur4
, cur3
, rem4
) {
372 const char *name
= blobmsg_name(cur4
);
374 if (!strcmp(name
, "running")) {
375 running
= blobmsg_get_bool(cur4
);
376 } else if (running
&& !strcmp(name
, "data")) {
377 struct blob_attr
*cur5
;
380 blobmsg_for_each_attr(cur5
, cur4
, rem5
) {
381 struct blob_attr
*cur6
;
384 if (strcmp(blobmsg_name(cur5
), "mdns"))
387 blobmsg_for_each_attr(cur6
, cur5
, rem6
)
388 service_load_blob(cur6
);
396 vlist_flush(&services
);
397 vlist_flush(&hostnames
);
401 service_init(int announce
)
405 service_init_announce
= announce
;
406 ubus_service_list(service_init_cb
);
410 service_cleanup(void)
412 vlist_flush(&services
);