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 umdns_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
34 struct ubus_request_data
*req
, const char *method
,
35 struct blob_attr
*msg
)
42 umdns_update(struct ubus_context
*ctx
, struct ubus_object
*obj
,
43 struct ubus_request_data
*req
, const char *method
,
44 struct blob_attr
*msg
)
57 static const struct blobmsg_policy browse_policy
[] = {
58 [BROWSE_SERVICE
] = { "service", BLOBMSG_TYPE_STRING
},
59 [BROWSE_ARRAY
] = { "array", BLOBMSG_TYPE_BOOL
},
60 [BROWSE_ADDRESS
] = { "address", BLOBMSG_TYPE_BOOL
},
64 umdns_browse(struct ubus_context
*ctx
, struct ubus_object
*obj
,
65 struct ubus_request_data
*req
, const char *method
,
66 struct blob_attr
*msg
)
68 struct cache_service
*s
, *q
;
69 char *buffer
= (char *) mdns_buf
;
70 struct blob_attr
*data
[BROWSE_MAX
];
76 blobmsg_parse(browse_policy
, BROWSE_MAX
, data
, blob_data(msg
), blob_len(msg
));
77 if (data
[BROWSE_SERVICE
])
78 service
= blobmsg_get_string(data
[BROWSE_SERVICE
]);
79 if (data
[BROWSE_ARRAY
])
80 array
= blobmsg_get_u8(data
[BROWSE_ARRAY
]);
81 if (data
[BROWSE_ADDRESS
])
82 address
= blobmsg_get_bool(data
[BROWSE_ADDRESS
]);
85 avl_for_each_element(&services
, s
, avl
) {
86 const char *hostname
= buffer
;
89 snprintf(buffer
, MAX_NAME_LEN
, "%s", (const char *) s
->avl
.key
);
90 local
= strstr(buffer
, ".local");
93 if (!strcmp(buffer
, "_tcp") || !strcmp(buffer
, "_udp"))
95 if (service
&& strcmp(buffer
, service
))
98 c1
= blobmsg_open_table(&b
, buffer
);
100 snprintf(buffer
, MAX_NAME_LEN
, "%s", s
->entry
);
101 local
= strstr(buffer
, "._");
104 c2
= blobmsg_open_table(&b
, buffer
);
105 strncat(buffer
, ".local", MAX_NAME_LEN
);
106 blobmsg_add_string(&b
, "iface", s
->iface
->name
);
107 cache_dump_records(&b
, s
->entry
, array
, &hostname
);
109 cache_dump_records(&b
, hostname
, array
, NULL
);
110 blobmsg_close_table(&b
, c2
);
111 q
= avl_next_element(s
, avl
);
112 if (!q
|| avl_is_last(&services
, &s
->avl
) || strcmp(s
->avl
.key
, q
->avl
.key
)) {
113 blobmsg_close_table(&b
, c1
);
117 ubus_send_reply(ctx
, req
, b
.head
);
119 return UBUS_STATUS_OK
;
123 umdns_hosts(struct ubus_context
*ctx
, struct ubus_object
*obj
,
124 struct ubus_request_data
*req
, const char *method
,
125 struct blob_attr
*msg
)
127 struct cache_record
*prev
= NULL
;
128 struct cache_record
*r
;
131 blob_buf_init(&b
, 0);
132 avl_for_each_element(&records
, r
, avl
) {
133 if (r
->type
!= TYPE_A
&& r
->type
!= TYPE_AAAA
)
135 /* Query each domain just once */
136 if (!prev
|| strcmp(r
->record
, prev
->record
)) {
137 c
= blobmsg_open_table(&b
, r
->record
);
138 cache_dump_records(&b
, r
->record
, false, NULL
);
139 blobmsg_close_table(&b
, c
);
143 ubus_send_reply(ctx
, req
, b
.head
);
145 return UBUS_STATUS_OK
;
154 static const struct blobmsg_policy config_policy
[] = {
155 [CFG_INTERFACES
] = { "interfaces", BLOBMSG_TYPE_ARRAY
},
156 [CFG_KEEP
] = { "keep", BLOBMSG_TYPE_BOOL
},
160 umdns_set_config(struct ubus_context
*ctx
, struct ubus_object
*obj
,
161 struct ubus_request_data
*req
, const char *method
,
162 struct blob_attr
*msg
)
164 struct blob_attr
*data
[CFG_MAX
], *cur
;
165 int rem
, keep
= false;
167 blobmsg_parse(config_policy
, CFG_MAX
, data
, blob_data(msg
), blob_len(msg
));
168 if (!data
[CFG_INTERFACES
])
169 return UBUS_STATUS_INVALID_ARGUMENT
;
171 if (!blobmsg_check_attr_list(data
[CFG_INTERFACES
], BLOBMSG_TYPE_STRING
))
172 return UBUS_STATUS_INVALID_ARGUMENT
;
174 keep
= data
[CFG_KEEP
] && blobmsg_get_bool(data
[CFG_KEEP
]);
176 vlist_update(&interfaces
);
177 ubus_notify(ctx
, obj
, "set_config", NULL
, 1000);
180 blobmsg_for_each_attr(cur
, data
[CFG_INTERFACES
], rem
)
181 interface_add(blobmsg_data(cur
));
184 vlist_flush(&interfaces
);
196 static const struct blobmsg_policy query_policy
[QUERY_MAX
] = {
197 [QUERY_QUESTION
]= { "question", BLOBMSG_TYPE_STRING
},
198 [QUERY_IFACE
] = { "interface", BLOBMSG_TYPE_STRING
},
199 [QUERY_TYPE
] = { "type", BLOBMSG_TYPE_INT32
},
203 umdns_query(struct ubus_context
*ctx
, struct ubus_object
*obj
,
204 struct ubus_request_data
*req
, const char *method
,
205 struct blob_attr
*msg
)
207 struct blob_attr
*tb
[QUERY_MAX
], *c
;
208 const char *question
= C_DNS_SD
;
212 blobmsg_parse(query_policy
, QUERY_MAX
, tb
, blob_data(msg
), blob_len(msg
));
214 if (!(c
= tb
[QUERY_IFACE
]))
215 return UBUS_STATUS_INVALID_ARGUMENT
;
217 ifname
= blobmsg_get_string(c
);
219 if ((c
= tb
[QUERY_QUESTION
]))
220 question
= blobmsg_get_string(c
);
222 if ((c
= tb
[QUERY_TYPE
]))
223 type
= blobmsg_get_u32(c
);
225 struct interface
*iface_v4
= interface_get(ifname
, SOCK_MC_IPV4
);
226 struct interface
*iface_v6
= interface_get(ifname
, SOCK_MC_IPV6
);
228 if (!iface_v4
&& !iface_v6
)
229 return UBUS_STATUS_NOT_FOUND
;
231 if (!strcmp(method
, "query")) {
233 dns_send_question(iface_v4
, NULL
, question
, type
, 1);
236 dns_send_question(iface_v6
, NULL
, question
, type
, 1);
238 return UBUS_STATUS_OK
;
239 } else if (!strcmp(method
, "fetch")) {
240 blob_buf_init(&b
, 0);
241 void *k
= blobmsg_open_array(&b
, "records");
242 cache_dump_recursive(&b
, question
, type
, iface_v4
? iface_v4
: iface_v6
);
243 blobmsg_close_array(&b
, k
);
244 ubus_send_reply(ctx
, req
, b
.head
);
245 return UBUS_STATUS_OK
;
247 return UBUS_STATUS_INVALID_ARGUMENT
;
252 static const struct ubus_method umdns_methods
[] = {
253 UBUS_METHOD("set_config", umdns_set_config
, config_policy
),
254 UBUS_METHOD("query", umdns_query
, query_policy
),
255 UBUS_METHOD("fetch", umdns_query
, query_policy
),
256 UBUS_METHOD("browse", umdns_browse
, browse_policy
),
257 UBUS_METHOD_NOARG("update", umdns_update
),
258 UBUS_METHOD_NOARG("hosts", umdns_hosts
),
259 UBUS_METHOD_NOARG("reload", umdns_reload
),
262 static struct ubus_object_type umdns_object_type
=
263 UBUS_OBJECT_TYPE("umdns", umdns_methods
);
265 static struct ubus_object umdns_object
= {
267 .type
= &umdns_object_type
,
268 .methods
= umdns_methods
,
269 .n_methods
= ARRAY_SIZE(umdns_methods
),
273 ubus_connect_handler(struct ubus_context
*ctx
)
277 ret
= ubus_add_object(ctx
, &umdns_object
);
279 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
285 conn
.cb
= ubus_connect_handler
;
286 ubus_auto_connect(&conn
);
289 int ubus_service_list(ubus_data_handler_t cb
)
294 blob_buf_init(&b
, 0);
295 ret
= ubus_lookup_id(&conn
.ctx
, "service", &id
);
299 return ubus_invoke(&conn
.ctx
, id
, "list", b
.head
, cb
, NULL
, 5 * 1000);