1 diff --git a/src/dnsmasq.c b/src/dnsmasq.c
2 index 30fb419..776351a 100644
5 @@ -2025,6 +2025,10 @@ static void check_dns_listeners(time_t now)
6 daemon->pipe_to_parent = pipefd[1];
10 + drop_ubus_listeners();
13 /* start with no upstream connections. */
14 for (s = daemon->servers; s; s = s->next)
16 diff --git a/src/dnsmasq.h b/src/dnsmasq.h
17 index e455c3f..c84ba48 100644
20 @@ -1673,14 +1673,26 @@ void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname);
25 +typedef void (*ubus_dns_notify_cb)(struct blob_attr *msg, void *priv);
27 char *ubus_init(void);
28 void set_ubus_listeners(void);
29 void check_ubus_listeners(void);
30 +void drop_ubus_listeners(void);
31 +int ubus_dns_notify_has_subscribers(void);
32 +struct blob_buf *ubus_dns_notify_prepare(void);
33 +int ubus_dns_notify(const char *type, ubus_dns_notify_cb cb, void *priv);
34 void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface);
35 # ifdef HAVE_CONNTRACK
36 void ubus_event_bcast_connmark_allowlist_refused(u32 mark, const char *name);
37 void ubus_event_bcast_connmark_allowlist_resolved(u32 mark, const char *pattern, const char *ip, u32 ttl);
40 +static inline int ubus_dns_notify_has_subscribers(void)
47 diff --git a/src/forward.c b/src/forward.c
48 index 32f37e4..3d28963 100644
51 @@ -803,7 +803,7 @@ static size_t process_reply(struct dns_header *header, time_t now, struct server
55 - if (daemon->doctors && do_doctor(header, n, daemon->namebuff))
56 + if ((daemon->doctors || ubus_dns_notify_has_subscribers()) && do_doctor(header, n, daemon->namebuff))
59 /* check_for_bogus_wildcard() does it's own caching, so
60 diff --git a/src/rfc1035.c b/src/rfc1035.c
61 index 387d894..7bf7967 100644
65 You should have received a copy of the GNU General Public License
66 along with this program. If not, see <http://www.gnu.org/licenses/>.
71 +#include <libubox/blobmsg.h>
74 int extract_name(struct dns_header *header, size_t plen, unsigned char **pp,
75 char *name, int isExtract, int extrabytes)
76 @@ -384,10 +386,65 @@ static int private_net6(struct in6_addr *a, int ban_localhost)
77 ((u32 *)a)[0] == htonl(0x20010db8); /* RFC 6303 4.6 */
81 +static void ubus_dns_doctor_cb(struct blob_attr *msg, void *priv)
83 + static const struct blobmsg_policy policy = {
85 + .type = BLOBMSG_TYPE_STRING,
87 + struct blob_attr *val;
90 + blobmsg_parse(&policy, 1, &val, blobmsg_data(msg), blobmsg_data_len(msg));
92 + *dest = blobmsg_get_string(val);
95 +static int ubus_dns_doctor(const char *name, int ttl, void *p, int af)
103 + b = ubus_dns_notify_prepare();
107 + blobmsg_add_string(b, "name", name);
109 + blobmsg_add_u32(b, "ttl", ttl);
111 + blobmsg_add_string(b, "type", af == AF_INET6 ? "AAAA" : "A");
113 + addr = blobmsg_alloc_string_buffer(b, "address", INET6_ADDRSTRLEN);
117 + inet_ntop(af, p, addr, INET6_ADDRSTRLEN);
118 + blobmsg_add_string_buffer(b);
121 + ubus_dns_notify("dns_result", ubus_dns_doctor_cb, &addr);
126 + return inet_pton(af, addr, p) == 1;
129 +static int ubus_dns_doctor(const char *name, int ttl, void *p, int af)
135 int do_doctor(struct dns_header *header, size_t qlen, char *namebuff)
138 - int i, qtype, qclass, rdlen;
139 + int i, qtype, qclass, rdlen, ttl;
142 if (!(p = skip_questions(header, qlen)))
143 @@ -404,7 +461,7 @@ int do_doctor(struct dns_header *header, size_t qlen, char *namebuff)
148 + GETLONG(ttl, p); /* ttl */
151 if (qclass == C_IN && qtype == T_A)
152 @@ -415,6 +472,9 @@ int do_doctor(struct dns_header *header, size_t qlen, char *namebuff)
153 if (!CHECK_LEN(header, p, qlen, INADDRSZ))
156 + if (ubus_dns_doctor(daemon->namebuff, ttl, p, AF_INET))
157 + header->hb3 &= ~HB3_AA;
160 memcpy(&addr.addr4, p, INADDRSZ);
162 @@ -444,6 +504,14 @@ int do_doctor(struct dns_header *header, size_t qlen, char *namebuff)
166 + else if (qclass == C_IN && qtype == T_AAAA)
168 + if (!CHECK_LEN(header, p, qlen, IN6ADDRSZ))
171 + if (ubus_dns_doctor(daemon->namebuff, ttl, p, AF_INET6))
172 + header->hb3 &= ~HB3_AA;
175 if (!ADD_RDLEN(header, p, qlen, rdlen))
176 return done; /* bad packet */
177 diff --git a/src/ubus.c b/src/ubus.c
178 index a5758e7..f2a75a8 100644
181 @@ -72,6 +72,13 @@ static struct ubus_object ubus_object = {
182 .subscribe_cb = ubus_subscribe_cb,
185 +static struct ubus_object_type ubus_dns_object_type =
186 + { .name = "dnsmasq.dns" };
188 +static struct ubus_object ubus_dns_object = {
189 + .type = &ubus_dns_object_type,
192 static void ubus_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
195 @@ -105,13 +112,21 @@ static void ubus_disconnect_cb(struct ubus_context *ubus)
198 struct ubus_context *ubus = NULL;
202 if (!(ubus = ubus_connect(NULL)))
205 + dns_name = whine_malloc(strlen(daemon->ubus_name) + 5);
206 + sprintf(dns_name, "%s.dns", daemon->ubus_name);
208 ubus_object.name = daemon->ubus_name;
209 + ubus_dns_object.name = dns_name;
211 ret = ubus_add_object(ubus, &ubus_object);
213 + ret = ubus_add_object(ubus, &ubus_dns_object);
217 @@ -181,6 +196,17 @@ void check_ubus_listeners()
221 +void drop_ubus_listeners()
223 + struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
229 + daemon->ubus = NULL;
232 static int ubus_handle_metrics(struct ubus_context *ctx, struct ubus_object *obj,
233 struct ubus_request_data *req, const char *method,
234 struct blob_attr *msg)
235 @@ -328,6 +354,53 @@ fail:
239 +int ubus_dns_notify_has_subscribers(void)
241 + return (daemon->ubus && ubus_dns_object.has_subscribers);
244 +struct blob_buf *ubus_dns_notify_prepare(void)
246 + if (!ubus_dns_notify_has_subscribers())
249 + blob_buf_init(&b, 0);
253 +struct ubus_dns_notify_req {
254 + struct ubus_notify_request req;
255 + ubus_dns_notify_cb cb;
259 +static void dns_notify_cb(struct ubus_notify_request *req, int type, struct blob_attr *msg)
261 + struct ubus_dns_notify_req *dreq = container_of(req, struct ubus_dns_notify_req, req);
263 + dreq->cb(msg, dreq->priv);
266 +int ubus_dns_notify(const char *type, ubus_dns_notify_cb cb, void *priv)
268 + struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
269 + struct ubus_dns_notify_req dreq;
272 + if (!ubus || !ubus_dns_object.has_subscribers)
275 + ret = ubus_notify_async(ubus, &ubus_dns_object, type, b.head, &dreq.req);
279 + dreq.req.data_cb = dns_notify_cb;
283 + return ubus_complete_request(ubus, &dreq.req.req, 100);
286 void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface)
288 struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;