@@ -135,16 +135,31 @@ let try_etc_hosts =
135
135
end
136
136
| _ -> None
137
137
138
- let try_builtins local_ip host_names question =
138
+ let try_builtins builtin_names question =
139
139
let open Dns.Packet in
140
- match local_ip, question with
141
- | Ipaddr. V4 local_ip, { q_class = Q_IN ; q_type = (Q_A | Q_AAAA ); q_name; _ }
142
- when List. mem q_name host_names ->
143
- Log. info (fun f ->
144
- f " DNS: %s is a builtin: %a" (Dns.Name. to_string q_name)
145
- Ipaddr.V4. pp_hum local_ip);
146
- Some [ { name = q_name; cls = RR_IN ; flush = false ; ttl = 0l ;
147
- rdata = A local_ip } ]
140
+ match question with
141
+ | { q_class = Q_IN ; q_type = (Q_A |Q_AAAA ); q_name; _ } ->
142
+ let bindings = List. filter (fun (name , _ ) -> name = q_name) builtin_names in
143
+ let ipv4_rrs =
144
+ List. fold_left (fun acc (_ , ip ) ->
145
+ match ip with
146
+ | Ipaddr. V4 ipv4 -> { name = q_name; cls = RR_IN ; flush = false ; ttl = 0l ; rdata = A ipv4 } :: acc
147
+ | _ -> acc
148
+ ) [] bindings in
149
+ let ipv6_rrs =
150
+ List. fold_left (fun acc (_ , ip ) ->
151
+ match ip with
152
+ | Ipaddr. V6 ipv6 -> { name = q_name; cls = RR_IN ; flush = false ; ttl = 0l ; rdata = AAAA ipv6 } :: acc
153
+ | _ -> acc
154
+ ) [] bindings in
155
+ let rrs = if question.q_type = Q_A then ipv4_rrs else ipv6_rrs in
156
+ if rrs = [] then None else begin
157
+ Log. info (fun f ->
158
+ f " DNS: %s is a builtin: %s" (Dns.Name. to_string q_name)
159
+ (String. concat " ; " (List. map (fun rr -> Dns.Packet. rr_to_string rr) rrs))
160
+ );
161
+ Some rrs
162
+ end
148
163
| _ -> None
149
164
150
165
module Make
@@ -191,7 +206,7 @@ struct
191
206
192
207
type t = {
193
208
local_ip : Ipaddr .t ;
194
- host_names : Dns.Name .t list ;
209
+ builtin_names : ( Dns.Name .t * Ipaddr .t ) list ;
195
210
resolver : resolver ;
196
211
}
197
212
@@ -271,13 +286,13 @@ struct
271
286
| None ->
272
287
Random. int bound
273
288
274
- let create ~local_address ~host_names =
289
+ let create ~local_address ~builtin_names =
275
290
let local_ip = local_address.Dns_forward.Config.Address. ip in
276
291
Log. info (fun f ->
277
- let prefix = match host_names with
278
- | [] -> " No DNS names"
279
- | _ -> Printf. sprintf " DNS names [ %s ]" (String. concat " , " @@ List. map Dns.Name. to_string host_names ) in
280
- f " %s will map to local IP %s" prefix ( Ipaddr. to_string local_ip) );
292
+ let suffix = match builtin_names with
293
+ | [] -> " no builtin DNS names; everything will be forwarded "
294
+ | _ -> Printf. sprintf " builtin DNS names [ %s ]" (String. concat " , " @@ List. map ( fun ( name , ip ) -> Dns.Name. to_string name ^ " -> " ^ ( Ipaddr. to_string ip)) builtin_names ) in
295
+ f " DNS server configured with %s" suffix );
281
296
fun clock -> function
282
297
| `Upstream config ->
283
298
let open Dns_forward.Config.Address in
@@ -300,11 +315,11 @@ struct
300
315
>> = fun dns_udp_resolver ->
301
316
Dns_tcp_resolver. create ~gen_transaction_id ~message_cb config clock
302
317
>> = fun dns_tcp_resolver ->
303
- Lwt. return { local_ip; host_names ;
318
+ Lwt. return { local_ip; builtin_names ;
304
319
resolver = Upstream { dns_tcp_resolver; dns_udp_resolver } }
305
320
| `Host ->
306
321
Log. info (fun f -> f " Will use the host's DNS resolver" );
307
- Lwt. return { local_ip; host_names ; resolver = Host }
322
+ Lwt. return { local_ip; builtin_names ; resolver = Host }
308
323
309
324
let answer t is_tcp buf =
310
325
let open Dns.Packet in
@@ -327,7 +342,7 @@ struct
327
342
| Some answers ->
328
343
Lwt. return (Ok (marshal @@ reply answers))
329
344
| None ->
330
- match try_builtins t.local_ip t.host_names question with
345
+ match try_builtins t.builtin_names question with
331
346
| Some answers ->
332
347
Lwt. return (Ok (marshal @@ reply answers))
333
348
| None ->
0 commit comments