3 @@ -2021,6 +2021,10 @@ static void check_dns_listeners(time_t n
4 daemon->pipe_to_parent = pipefd[1];
8 + drop_ubus_listeners();
11 /* start with no upstream connections. */
12 for (s = daemon->servers; s; s = s->next)
16 @@ -1670,14 +1670,26 @@ void emit_dbus_signal(int action, struct
21 +typedef void (*ubus_dns_notify_cb)(struct blob_attr *msg, void *priv);
23 char *ubus_init(void);
24 void set_ubus_listeners(void);
25 void check_ubus_listeners(void);
26 +void drop_ubus_listeners(void);
27 +int ubus_dns_notify_has_subscribers(void);
28 +struct blob_buf *ubus_dns_notify_prepare(void);
29 +int ubus_dns_notify(const char *type, ubus_dns_notify_cb cb, void *priv);
30 void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface);
31 # ifdef HAVE_CONNTRACK
32 void ubus_event_bcast_connmark_allowlist_refused(u32 mark, const char *name);
33 void ubus_event_bcast_connmark_allowlist_resolved(u32 mark, const char *pattern, const char *ip, u32 ttl);
36 +static inline int ubus_dns_notify_has_subscribers(void)
45 @@ -803,7 +803,7 @@ static size_t process_reply(struct dns_h
49 - if (daemon->doctors && do_doctor(header, n, daemon->namebuff))
50 + if ((daemon->doctors || ubus_dns_notify_has_subscribers()) && do_doctor(header, n, daemon->namebuff))
53 /* check_for_bogus_wildcard() does it's own caching, so
57 You should have received a copy of the GNU General Public License
58 along with this program. If not, see <http://www.gnu.org/licenses/>.
63 +#include <libubox/blobmsg.h>
66 int extract_name(struct dns_header *header, size_t plen, unsigned char **pp,
67 char *name, int isExtract, int extrabytes)
68 @@ -384,10 +386,65 @@ static int private_net6(struct in6_addr
69 ((u32 *)a)[0] == htonl(0x20010db8); /* RFC 6303 4.6 */
73 +static void ubus_dns_doctor_cb(struct blob_attr *msg, void *priv)
75 + static const struct blobmsg_policy policy = {
77 + .type = BLOBMSG_TYPE_STRING,
79 + struct blob_attr *val;
82 + blobmsg_parse(&policy, 1, &val, blobmsg_data(msg), blobmsg_data_len(msg));
84 + *dest = blobmsg_get_string(val);
87 +static int ubus_dns_doctor(const char *name, int ttl, void *p, int af)
95 + b = ubus_dns_notify_prepare();
99 + blobmsg_add_string(b, "name", name);
101 + blobmsg_add_u32(b, "ttl", ttl);
103 + blobmsg_add_string(b, "type", af == AF_INET6 ? "AAAA" : "A");
105 + addr = blobmsg_alloc_string_buffer(b, "address", INET6_ADDRSTRLEN);
109 + inet_ntop(af, p, addr, INET6_ADDRSTRLEN);
110 + blobmsg_add_string_buffer(b);
113 + ubus_dns_notify("dns_result", ubus_dns_doctor_cb, &addr);
118 + return inet_pton(af, addr, p) == 1;
121 +static int ubus_dns_doctor(const char *name, int ttl, void *p, int af)
127 int do_doctor(struct dns_header *header, size_t qlen, char *namebuff)
130 - int i, qtype, qclass, rdlen;
131 + int i, qtype, qclass, rdlen, ttl;
134 if (!(p = skip_questions(header, qlen)))
135 @@ -404,7 +461,7 @@ int do_doctor(struct dns_header *header,
140 + GETLONG(ttl, p); /* ttl */
143 if (qclass == C_IN && qtype == T_A)
144 @@ -415,6 +472,9 @@ int do_doctor(struct dns_header *header,
145 if (!CHECK_LEN(header, p, qlen, INADDRSZ))
148 + if (ubus_dns_doctor(daemon->namebuff, ttl, p, AF_INET))
149 + header->hb3 &= ~HB3_AA;
152 memcpy(&addr.addr4, p, INADDRSZ);
154 @@ -444,6 +504,14 @@ int do_doctor(struct dns_header *header,
158 + else if (qclass == C_IN && qtype == T_AAAA)
160 + if (!CHECK_LEN(header, p, qlen, IN6ADDRSZ))
163 + if (ubus_dns_doctor(daemon->namebuff, ttl, p, AF_INET6))
164 + header->hb3 &= ~HB3_AA;
167 if (!ADD_RDLEN(header, p, qlen, rdlen))
168 return done; /* bad packet */
171 @@ -72,6 +72,13 @@ static struct ubus_object ubus_object =
172 .subscribe_cb = ubus_subscribe_cb,
175 +static struct ubus_object_type ubus_dns_object_type =
176 + { .name = "dnsmasq.dns" };
178 +static struct ubus_object ubus_dns_object = {
179 + .type = &ubus_dns_object_type,
182 static void ubus_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
185 @@ -105,13 +112,21 @@ static void ubus_disconnect_cb(struct ub
188 struct ubus_context *ubus = NULL;
192 if (!(ubus = ubus_connect(NULL)))
195 + dns_name = whine_malloc(strlen(daemon->ubus_name) + 5);
196 + sprintf(dns_name, "%s.dns", daemon->ubus_name);
198 ubus_object.name = daemon->ubus_name;
199 + ubus_dns_object.name = dns_name;
201 ret = ubus_add_object(ubus, &ubus_object);
203 + ret = ubus_add_object(ubus, &ubus_dns_object);
207 @@ -181,6 +196,17 @@ void check_ubus_listeners()
211 +void drop_ubus_listeners()
213 + struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
219 + daemon->ubus = NULL;
222 static int ubus_handle_metrics(struct ubus_context *ctx, struct ubus_object *obj,
223 struct ubus_request_data *req, const char *method,
224 struct blob_attr *msg)
225 @@ -328,6 +354,53 @@ fail:
229 +int ubus_dns_notify_has_subscribers(void)
231 + return (daemon->ubus && ubus_dns_object.has_subscribers);
234 +struct blob_buf *ubus_dns_notify_prepare(void)
236 + if (!ubus_dns_notify_has_subscribers())
239 + blob_buf_init(&b, 0);
243 +struct ubus_dns_notify_req {
244 + struct ubus_notify_request req;
245 + ubus_dns_notify_cb cb;
249 +static void dns_notify_cb(struct ubus_notify_request *req, int type, struct blob_attr *msg)
251 + struct ubus_dns_notify_req *dreq = container_of(req, struct ubus_dns_notify_req, req);
253 + dreq->cb(msg, dreq->priv);
256 +int ubus_dns_notify(const char *type, ubus_dns_notify_cb cb, void *priv)
258 + struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
259 + struct ubus_dns_notify_req dreq;
262 + if (!ubus || !ubus_dns_object.has_subscribers)
265 + ret = ubus_notify_async(ubus, &ubus_dns_object, type, b.head, &dreq.req);
269 + dreq.req.data_cb = dns_notify_cb;
273 + return ubus_complete_request(ubus, &dreq.req.req, 100);
276 void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface)
278 struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;