Skip to content

Commit

Permalink
Fix transport will modify input
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Feb 11, 2024
1 parent f01612d commit 9e21015
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
5 changes: 3 additions & 2 deletions quic/transport_http3.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ func (t *HTTP3Transport) Raw() bool {
}

func (t *HTTP3Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) {
message.Id = 0
rawMessage, err := message.Pack()
exMessage := *message
exMessage.Id = 0
rawMessage, err := exMessage.Pack()
if err != nil {
return nil, err
}
Expand Down
9 changes: 5 additions & 4 deletions quic/transport_quic.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ func (t *Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg,
)
for i := 0; i < 2; i++ {
conn, err = t.openConnection()
if conn == nil {
return nil, err
if err == nil {
break
}
response, err = t.exchange(ctx, message, conn)
if err == nil {
Expand All @@ -138,8 +138,9 @@ func (t *Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg,
}

func (t *Transport) exchange(ctx context.Context, message *mDNS.Msg, conn quic.Connection) (*mDNS.Msg, error) {
message.Id = 0
rawMessage, err := message.Pack()
exMessage := *message
exMessage.Id = 0
rawMessage, err := exMessage.Pack()
if err != nil {
return nil, err
}
Expand Down
41 changes: 23 additions & 18 deletions transport_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,42 +114,47 @@ func (t *myTransportAdapter) recvLoop(conn *dnsConnection) {

func (t *myTransportAdapter) Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg, error) {
messageId := message.Id
var response *dns.Msg
var err error
for attempts := 0; attempts < 3; attempts++ {
response, err = t.exchange(ctx, message)
if err != nil && !common.Done(ctx) {
continue
var (
conn *dnsConnection
err error
)
for attempts := 0; attempts < 2; attempts++ {
conn, err = t.open(t.ctx)
if err == nil {
break
}
break
}
if err == nil {
response.Id = messageId
if err != nil {
return nil, err
}
return response, err
}

func (t *myTransportAdapter) exchange(ctx context.Context, message *dns.Msg) (*dns.Msg, error) {
conn, err := t.open(t.ctx)
response, err := t.exchange(ctx, conn, message)
if err != nil {
return nil, err
}
response.Id = messageId
return response, nil
}

func (t *myTransportAdapter) exchange(ctx context.Context, conn *dnsConnection, message *dns.Msg) (*dns.Msg, error) {
messageId := message.Id
callback := make(chan *dns.Msg)
exMessage := *message
conn.access.Lock()
conn.queryId++
message.Id = conn.queryId
conn.callbacks[message.Id] = callback
exMessage.Id = conn.queryId
conn.callbacks[exMessage.Id] = callback
conn.access.Unlock()
defer t.cleanup(conn, message.Id, callback)
defer t.cleanup(conn, exMessage.Id, callback)
conn.writeAccess.Lock()
err = t.handler.WriteMessage(conn, message)
err := t.handler.WriteMessage(conn, &exMessage)
conn.writeAccess.Unlock()
if err != nil {
conn.cancel()
return nil, err
}
select {
case response := <-callback:
response.Id = messageId
return response, nil
case <-conn.ctx.Done():
return nil, E.Errors(conn.err, conn.ctx.Err())
Expand Down
5 changes: 3 additions & 2 deletions transport_https.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ func (t *HTTPSTransport) Raw() bool {
}

func (t *HTTPSTransport) Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg, error) {
message.Id = 0
rawMessage, err := message.Pack()
exMessage := *message
exMessage.Id = 0
rawMessage, err := exMessage.Pack()
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 9e21015

Please sign in to comment.