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_add_records(const char *name
)
51 struct cache_record
*r
, *q
= avl_find_element(&records
, name
, r
, avl
);
53 char buffer
[MAX_NAME_LEN
];
62 if (r
->txt
&& strlen(r
->txt
)) {
65 blobmsg_add_string(&b
, "txt", txt
);
66 txt
= &txt
[strlen(txt
) + 1];
73 blobmsg_add_u32(&b
, "port", r
->port
);
77 if ((r
->rdlength
== 4) && inet_ntop(AF_INET
, r
->rdata
, buffer
, INET6_ADDRSTRLEN
))
78 blobmsg_add_string(&b
, "ipv4", buffer
);
82 if ((r
->rdlength
== 16) && inet_ntop(AF_INET6
, r
->rdata
, buffer
, INET6_ADDRSTRLEN
))
83 blobmsg_add_string(&b
, "ipv6", buffer
);
86 q
= avl_next_element(r
, avl
);
87 } while (q
&& !strcmp(r
->record
, q
->record
));
91 mdns_browse(struct ubus_context
*ctx
, struct ubus_object
*obj
,
92 struct ubus_request_data
*req
, const char *method
,
93 struct blob_attr
*msg
)
95 struct cache_entry
*s
, *q
;
96 char buffer
[MAX_NAME_LEN
];
100 avl_for_each_element(&entries
, s
, avl
) {
102 if (*((char *) s
->avl
.key
) != '_')
104 snprintf(buffer
, MAX_NAME_LEN
, s
->avl
.key
);
105 local
= strstr(buffer
, ".local");
108 if (!strcmp(buffer
, "_tcp") || !strcmp(buffer
, "_udp"))
112 c1
= blobmsg_open_table(&b
, buffer
);
114 snprintf(buffer
, MAX_NAME_LEN
, s
->entry
);
115 local
= strstr(buffer
, "._");
118 c2
= blobmsg_open_table(&b
, buffer
);
119 strncat(buffer
, ".local", MAX_NAME_LEN
);
120 mdns_add_records(buffer
);
121 mdns_add_records(s
->entry
);
122 blobmsg_close_table(&b
, c2
);
123 q
= avl_next_element(s
, avl
);
124 if (!q
|| avl_is_last(&entries
, &s
->avl
) || strcmp(s
->avl
.key
, q
->avl
.key
)) {
125 blobmsg_close_table(&b
, c1
);
129 ubus_send_reply(ctx
, req
, b
.head
);
131 return UBUS_STATUS_OK
;
135 mdns_hosts(struct ubus_context
*ctx
, struct ubus_object
*obj
,
136 struct ubus_request_data
*req
, const char *method
,
137 struct blob_attr
*msg
)
139 struct cache_entry
*s
;
140 char buffer
[MAX_NAME_LEN
];
143 blob_buf_init(&b
, 0);
144 avl_for_each_element(&entries
, s
, avl
) {
146 if (*((char *) s
->avl
.key
) == '_')
148 snprintf(buffer
, MAX_NAME_LEN
, s
->entry
);
149 local
= strstr(buffer
, "._");
152 c
= blobmsg_open_table(&b
, buffer
);
153 strncat(buffer
, ".local", MAX_NAME_LEN
);
154 mdns_add_records(buffer
);
155 mdns_add_records(s
->entry
);
156 blobmsg_close_table(&b
, c
);
158 ubus_send_reply(ctx
, req
, b
.head
);
160 return UBUS_STATUS_OK
;
163 static const struct ubus_method mdns_methods
[] = {
164 UBUS_METHOD_NOARG("scan", mdns_scan
),
165 UBUS_METHOD_NOARG("browse", mdns_browse
),
166 UBUS_METHOD_NOARG("hosts", mdns_hosts
),
167 UBUS_METHOD_NOARG("reload", mdns_reload
),
170 static struct ubus_object_type mdns_object_type
=
171 UBUS_OBJECT_TYPE("mdns", mdns_methods
);
173 static struct ubus_object mdns_object
= {
175 .type
= &mdns_object_type
,
176 .methods
= mdns_methods
,
177 .n_methods
= ARRAY_SIZE(mdns_methods
),
181 ubus_connect_handler(struct ubus_context
*ctx
)
185 ret
= ubus_add_object(ctx
, &mdns_object
);
187 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
193 conn
.cb
= ubus_connect_handler
;
194 ubus_auto_connect(&conn
);