@@ -186,19 +186,17 @@ func (t *dnsgateway) querySecondary(t2 Transport, network string, q []byte, out
186
186
ticker .Stop ()
187
187
return
188
188
}
189
- } else { // query secondary to get answer for q
190
- if r , err = t2 .Query (network , q , result .summary ); err != nil {
191
- log .D ("alg: skip; sec transport %s err %v" , t2 .ID (), err )
192
- return
193
- }
189
+ } else {
190
+ // query secondary to get answer for q
191
+ r , err = Req (t2 , network , q , result .summary )
194
192
}
195
193
196
- if len ( r ) == 0 {
197
- log .W ("alg: skip; no primary or sec ans" )
194
+ if err != nil {
195
+ log .D ("alg: skip; sec transport %s err? %v" , t2 . ID (), err )
198
196
return
199
197
}
200
198
201
- // check if answer r is blocked
199
+ // check if answer r is blocked; r is either from t2 or from <-in
202
200
if ans2 := xdns .AsMsg (r ); ans2 == nil {
203
201
// not a valid dns answer
204
202
return
@@ -254,16 +252,19 @@ func (t *dnsgateway) q(t1, t2 Transport, preset []*netip.Addr, network string, q
254
252
if usepreset {
255
253
r , err = synthesizeOrQuery (preset , t1 , q , network , innersummary )
256
254
} else {
257
- r , err = query (t1 , network , q , innersummary )
255
+ r , err = Req (t1 , network , q , innersummary )
258
256
}
259
257
resch <- r
260
258
261
259
// override relevant values in summary
262
260
fillSummary (innersummary , summary )
263
261
264
262
if err != nil {
265
- log .D ("alg: abort; qerr %v" , err )
266
- return
263
+ if len (r ) <= 0 {
264
+ log .D ("alg: abort; r: 0, qerr %v" , err )
265
+ return
266
+ }
267
+ log .D ("alg: err but r ok; r: %d, qerr %v" , len (r ), err )
267
268
}
268
269
269
270
ansin := & dns.Msg {}
@@ -824,7 +825,7 @@ func hash48(s string) uint64 {
824
825
func synthesizeOrQuery (pre []* netip.Addr , tr Transport , q []byte , network string , smm * x.DNSSummary ) ([]byte , error ) {
825
826
// synthesize a response with the given ips
826
827
if len (pre ) == 0 {
827
- return query (tr , network , q , smm )
828
+ return Req (tr , network , q , smm )
828
829
}
829
830
msg := xdns .AsMsg (q )
830
831
if msg == nil {
@@ -840,7 +841,7 @@ func synthesizeOrQuery(pre []*netip.Addr, tr Transport, q []byte, network string
840
841
// if no ips are of the same family as the question xdns.AQuadAForQuery returns error
841
842
ans , err := xdns .AQuadAForQuery (msg , unptr (pre )... )
842
843
if err != nil { // errors on invalid msg, question, or mismatched ips
843
- return query (tr , network , q , smm )
844
+ return Req (tr , network , q , smm )
844
845
}
845
846
withPresetSummary (smm )
846
847
smm .RCode = xdns .Rcode (ans )
@@ -850,7 +851,7 @@ func synthesizeOrQuery(pre []*netip.Addr, tr Transport, q []byte, network string
850
851
log .D ("alg: synthesize: q(4? %t / 6? %t) rdata(%s)" , qname , is4 , is6 , smm .RData )
851
852
return ans .Pack ()
852
853
} else if isHTTPS || isSVCB {
853
- r , err := tr . Query ( network , q , smm )
854
+ r , err := Req ( tr , network , q , smm )
854
855
if err != nil {
855
856
return r , err
856
857
}
@@ -877,12 +878,29 @@ func synthesizeOrQuery(pre []*netip.Addr, tr Transport, q []byte, network string
877
878
878
879
return ans .Pack ()
879
880
} else {
880
- return query (tr , network , q , smm )
881
+ return Req (tr , network , q , smm )
881
882
}
882
883
}
883
884
884
- func query (t Transport , network string , q []byte , smm * x.DNSSummary ) ([]byte , error ) {
885
- return t .Query (network , q , smm )
885
+ // Req sends q to transport t and returns the answer, if any;
886
+ // errors are unset unless answer is empty;
887
+ // smm, the in/out parameter, is dns summary as got from t.
888
+ func Req (t Transport , network string , q []byte , smm * x.DNSSummary ) ([]byte , error ) {
889
+ if t == nil {
890
+ return nil , errNoSuchTransport
891
+ }
892
+ if len (q ) <= 0 {
893
+ return nil , errNoQuestion
894
+ }
895
+ if smm == nil { // discard smm
896
+ discarded := new (x.DNSSummary )
897
+ smm = discarded
898
+ }
899
+ r , err := t .Query (network , q , smm )
900
+ if len (r ) > 0 {
901
+ return r , nil
902
+ }
903
+ return r , err
886
904
}
887
905
888
906
func splitIPFamilies (ips []* netip.Addr ) (ip4s , ip6s []* netip.Addr ) {
0 commit comments