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/inet.h>
20 #include <libubox/avl.h>
21 #include <libubox/uloop.h>
27 static struct ubus_auto_conn conn;
28 static struct blob_buf b;
31 mdns_reload(struct ubus_context *ctx, struct ubus_object *obj,
32 struct ubus_request_data *req, const char *method,
33 struct blob_attr *msg)
40 mdns_scan(struct ubus_context *ctx, struct ubus_object *obj,
41 struct ubus_request_data *req, const char *method,
42 struct blob_attr *msg)
49 mdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
50 struct ubus_request_data *req, const char *method,
51 struct blob_attr *msg)
53 struct cache_entry *s, *q;
54 char buffer[MAX_NAME_LEN];
58 avl_for_each_element(&entries, s, avl) {
60 if (*((char *) s->avl.key) != '_')
62 snprintf(buffer, MAX_NAME_LEN, s->avl.key);
63 local = strstr(buffer, ".local");
66 if (!strcmp(buffer, "_tcp") || !strcmp(buffer, "_udp"))
70 c1 = blobmsg_open_table(&b, buffer);
72 snprintf(buffer, MAX_NAME_LEN, s->entry);
73 local = strstr(buffer, "._");
76 c2 = blobmsg_open_table(&b, buffer);
77 strncat(buffer, ".local", MAX_NAME_LEN);
78 cache_dump_records(&b, buffer);
79 cache_dump_records(&b, s->entry);
80 blobmsg_close_table(&b, c2);
81 q = avl_next_element(s, avl);
82 if (!q || avl_is_last(&entries, &s->avl) || strcmp(s->avl.key, q->avl.key)) {
83 blobmsg_close_table(&b, c1);
87 ubus_send_reply(ctx, req, b.head);
89 return UBUS_STATUS_OK;
93 mdns_hosts(struct ubus_context *ctx, struct ubus_object *obj,
94 struct ubus_request_data *req, const char *method,
95 struct blob_attr *msg)
97 struct cache_entry *s;
98 char buffer[MAX_NAME_LEN];
101 blob_buf_init(&b, 0);
102 avl_for_each_element(&entries, s, avl) {
104 if (*((char *) s->avl.key) == '_')
106 snprintf(buffer, MAX_NAME_LEN, s->entry);
107 local = strstr(buffer, "._");
110 c = blobmsg_open_table(&b, buffer);
111 strncat(buffer, ".local", MAX_NAME_LEN);
112 cache_dump_records(&b, buffer);
113 cache_dump_records(&b, s->entry);
114 blobmsg_close_table(&b, c);
116 ubus_send_reply(ctx, req, b.head);
118 return UBUS_STATUS_OK;
121 static const struct ubus_method mdns_methods[] = {
122 UBUS_METHOD_NOARG("scan", mdns_scan),
123 UBUS_METHOD_NOARG("browse", mdns_browse),
124 UBUS_METHOD_NOARG("hosts", mdns_hosts),
125 UBUS_METHOD_NOARG("reload", mdns_reload),
128 static struct ubus_object_type mdns_object_type =
129 UBUS_OBJECT_TYPE("mdns", mdns_methods);
131 static struct ubus_object mdns_object = {
133 .type = &mdns_object_type,
134 .methods = mdns_methods,
135 .n_methods = ARRAY_SIZE(mdns_methods),
139 ubus_connect_handler(struct ubus_context *ctx)
143 ret = ubus_add_object(ctx, &mdns_object);
145 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
151 conn.cb = ubus_connect_handler;
152 ubus_auto_connect(&conn);