Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ExposesUnderlyingConns interface #1581

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ type response struct {
writer Writer // writer to output the raw DNS bits
}

// IncomingConn exposes the underlying TCP connection, if it is used.
// For UDP connections, it returns nil.
func (r *response) IncomingConn() net.Conn {
return r.tcp
}

// IncomingPacketConn exposes the underlying UDP connection, if it is used.
// For TCP connections, it returns nil.
func (r *response) IncomingPacketConn() net.PacketConn {
return r.udp
}

// The ExposesUnderlyingConns interface is used by a DNS Handler to access underlying connections.
type ExposesUnderlyingConns interface {
// IncomingConn is only valid for TCP connections. It is otherwise nil.
IncomingConn() net.Conn
// IncomingPackageConn is only valid for UDP connections. It is otherwise nil.
IncomingPacketConn() net.PacketConn
}

// handleRefused returns a HandlerFunc that returns REFUSED for every request it gets.
func handleRefused(w ResponseWriter, r *Msg) {
m := new(Msg)
Expand Down
3 changes: 3 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
"golang.org/x/sync/errgroup"
)

// Assert that the response exposes the underlying connections.
var _ ExposesUnderlyingConns = &response{}

func HelloServer(w ResponseWriter, req *Msg) {
m := new(Msg)
m.SetReply(req)
Expand Down