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/vlist.h>
21 #include <libubox/uloop.h>
27 #include "interface.h"
29 static struct ubus_auto_conn conn
;
30 static struct blob_buf b
;
33 mdns_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
34 struct ubus_request_data
*req
, const char *method
,
35 struct blob_attr
*msg
)
42 mdns_scan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
43 struct ubus_request_data
*req
, const char *method
,
44 struct blob_attr
*msg
)
51 mdns_browse(struct ubus_context
*ctx
, struct ubus_object
*obj
,
52 struct ubus_request_data
*req
, const char *method
,
53 struct blob_attr
*msg
)
55 struct cache_entry
*s
, *q
;
56 char *buffer
= (char *) mdns_buf
;
60 avl_for_each_element(&entries
, s
, avl
) {
62 if (*((char *) s
->avl
.key
) != '_')
64 snprintf(buffer
, MAX_NAME_LEN
, "%s", (const char *) s
->avl
.key
);
65 local
= strstr(buffer
, ".local");
68 if (!strcmp(buffer
, "_tcp") || !strcmp(buffer
, "_udp"))
72 c1
= blobmsg_open_table(&b
, buffer
);
74 snprintf(buffer
, MAX_NAME_LEN
, "%s", (const char *) s
->entry
);
75 local
= strstr(buffer
, "._");
78 c2
= blobmsg_open_table(&b
, buffer
);
79 strncat(buffer
, ".local", MAX_NAME_LEN
);
80 cache_dump_records(&b
, buffer
);
81 cache_dump_records(&b
, s
->entry
);
82 blobmsg_close_table(&b
, c2
);
83 q
= avl_next_element(s
, avl
);
84 if (!q
|| avl_is_last(&entries
, &s
->avl
) || strcmp(s
->avl
.key
, q
->avl
.key
)) {
85 blobmsg_close_table(&b
, c1
);
89 ubus_send_reply(ctx
, req
, b
.head
);
91 return UBUS_STATUS_OK
;
95 mdns_hosts(struct ubus_context
*ctx
, struct ubus_object
*obj
,
96 struct ubus_request_data
*req
, const char *method
,
97 struct blob_attr
*msg
)
99 struct cache_entry
*s
;
100 char *buffer
= (char *) mdns_buf
;
103 blob_buf_init(&b
, 0);
104 avl_for_each_element(&entries
, s
, avl
) {
106 if (*((char *) s
->avl
.key
) == '_')
108 snprintf(buffer
, MAX_NAME_LEN
, "%s", (const char *) s
->entry
);
109 local
= strstr(buffer
, "._");
112 c
= blobmsg_open_table(&b
, buffer
);
113 strncat(buffer
, ".local", MAX_NAME_LEN
);
114 cache_dump_records(&b
, buffer
);
115 cache_dump_records(&b
, s
->entry
);
116 blobmsg_close_table(&b
, c
);
118 ubus_send_reply(ctx
, req
, b
.head
);
120 return UBUS_STATUS_OK
;
123 static const struct blobmsg_policy config_policy
[] = {
124 { "interfaces", BLOBMSG_TYPE_ARRAY
},
128 mdns_set_config(struct ubus_context
*ctx
, struct ubus_object
*obj
,
129 struct ubus_request_data
*req
, const char *method
,
130 struct blob_attr
*msg
)
132 struct blob_attr
*data
, *cur
;
135 blobmsg_parse(config_policy
, 1, &data
, blob_data(msg
), blob_len(msg
));
137 return UBUS_STATUS_INVALID_ARGUMENT
;
139 if (!blobmsg_check_attr_list(data
, BLOBMSG_TYPE_STRING
))
140 return UBUS_STATUS_INVALID_ARGUMENT
;
142 vlist_update(&interfaces
);
143 blobmsg_for_each_attr(cur
, data
, rem
)
144 interface_add(blobmsg_data(cur
));
145 vlist_flush(&interfaces
);
151 static const struct ubus_method mdns_methods
[] = {
152 UBUS_METHOD("set_config", mdns_set_config
, config_policy
),
153 UBUS_METHOD_NOARG("scan", mdns_scan
),
154 UBUS_METHOD_NOARG("browse", mdns_browse
),
155 UBUS_METHOD_NOARG("hosts", mdns_hosts
),
156 UBUS_METHOD_NOARG("reload", mdns_reload
),
159 static struct ubus_object_type mdns_object_type
=
160 UBUS_OBJECT_TYPE("mdns", mdns_methods
);
162 static struct ubus_object mdns_object
= {
164 .type
= &mdns_object_type
,
165 .methods
= mdns_methods
,
166 .n_methods
= ARRAY_SIZE(mdns_methods
),
170 ubus_connect_handler(struct ubus_context
*ctx
)
174 ret
= ubus_add_object(ctx
, &mdns_object
);
176 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
182 conn
.cb
= ubus_connect_handler
;
183 ubus_auto_connect(&conn
);