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>
28 static struct ubus_auto_conn conn
;
29 static struct blob_buf b
;
32 mdns_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
33 struct ubus_request_data
*req
, const char *method
,
34 struct blob_attr
*msg
)
41 mdns_scan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
42 struct ubus_request_data
*req
, const char *method
,
43 struct blob_attr
*msg
)
50 mdns_browse(struct ubus_context
*ctx
, struct ubus_object
*obj
,
51 struct ubus_request_data
*req
, const char *method
,
52 struct blob_attr
*msg
)
54 struct cache_entry
*s
, *q
;
55 char *buffer
= (char *) mdns_buf
;
59 avl_for_each_element(&entries
, s
, avl
) {
61 if (*((char *) s
->avl
.key
) != '_')
63 snprintf(buffer
, MAX_NAME_LEN
, "%s", (const char *) s
->avl
.key
);
64 local
= strstr(buffer
, ".local");
67 if (!strcmp(buffer
, "_tcp") || !strcmp(buffer
, "_udp"))
71 c1
= blobmsg_open_table(&b
, buffer
);
73 snprintf(buffer
, MAX_NAME_LEN
, "%s", (const char *) s
->entry
);
74 local
= strstr(buffer
, "._");
77 c2
= blobmsg_open_table(&b
, buffer
);
78 strncat(buffer
, ".local", MAX_NAME_LEN
);
79 cache_dump_records(&b
, buffer
);
80 cache_dump_records(&b
, s
->entry
);
81 blobmsg_close_table(&b
, c2
);
82 q
= avl_next_element(s
, avl
);
83 if (!q
|| avl_is_last(&entries
, &s
->avl
) || strcmp(s
->avl
.key
, q
->avl
.key
)) {
84 blobmsg_close_table(&b
, c1
);
88 ubus_send_reply(ctx
, req
, b
.head
);
90 return UBUS_STATUS_OK
;
94 mdns_hosts(struct ubus_context
*ctx
, struct ubus_object
*obj
,
95 struct ubus_request_data
*req
, const char *method
,
96 struct blob_attr
*msg
)
98 struct cache_entry
*s
;
99 char *buffer
= (char *) mdns_buf
;
102 blob_buf_init(&b
, 0);
103 avl_for_each_element(&entries
, s
, avl
) {
105 if (*((char *) s
->avl
.key
) == '_')
107 snprintf(buffer
, MAX_NAME_LEN
, "%s", (const char *) s
->entry
);
108 local
= strstr(buffer
, "._");
111 c
= blobmsg_open_table(&b
, buffer
);
112 strncat(buffer
, ".local", MAX_NAME_LEN
);
113 cache_dump_records(&b
, buffer
);
114 cache_dump_records(&b
, s
->entry
);
115 blobmsg_close_table(&b
, c
);
117 ubus_send_reply(ctx
, req
, b
.head
);
119 return UBUS_STATUS_OK
;
122 static const struct ubus_method mdns_methods
[] = {
123 UBUS_METHOD_NOARG("scan", mdns_scan
),
124 UBUS_METHOD_NOARG("browse", mdns_browse
),
125 UBUS_METHOD_NOARG("hosts", mdns_hosts
),
126 UBUS_METHOD_NOARG("reload", mdns_reload
),
129 static struct ubus_object_type mdns_object_type
=
130 UBUS_OBJECT_TYPE("mdns", mdns_methods
);
132 static struct ubus_object mdns_object
= {
134 .type
= &mdns_object_type
,
135 .methods
= mdns_methods
,
136 .n_methods
= ARRAY_SIZE(mdns_methods
),
140 ubus_connect_handler(struct ubus_context
*ctx
)
144 ret
= ubus_add_object(ctx
, &mdns_object
);
146 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
152 conn
.cb
= ubus_connect_handler
;
153 ubus_auto_connect(&conn
);