From d179552a86a0946987061ef313722a97d737dda1 Mon Sep 17 00:00:00 2001 From: phillip-stephens Date: Mon, 10 Jun 2024 12:53:16 +0900 Subject: [PATCH 1/6] added monkey-patch back to Conn struct --- client.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 9549fa9232..c00a5c5986 100644 --- a/client.go +++ b/client.go @@ -31,7 +31,11 @@ func isPacketConn(c net.Conn) bool { // A Conn represents a connection to a DNS server. type Conn struct { - net.Conn // a net.Conn holding the connection + net.Conn // a net.Conn holding the connection + // BEGIN FAST UDP MONKEY PATCH + UnboundUDP bool + RemoteAddr net.Addr + // END FAST UDP MONKEY PATCH UDPSize uint16 // minimum receive buffer for UDP messages TsigSecret map[string]string // secret(s) for Tsig map[], zonename must be in canonical form (lowercase, fqdn, see RFC 4034 Section 6.2) TsigProvider TsigProvider // An implementation of the TsigProvider interface. If defined it replaces TsigSecret and is used for all TSIG operations. From 9cc0090be86922fa644daf3bfde0671733c3786c Mon Sep 17 00:00:00 2001 From: phillip-stephens Date: Mon, 10 Jun 2024 12:54:33 +0900 Subject: [PATCH 2/6] added ExchangeWithConnTo --- client.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/client.go b/client.go index c00a5c5986..559f1b7331 100644 --- a/client.go +++ b/client.go @@ -174,6 +174,15 @@ func (c *Client) Exchange(m *Msg, address string) (r *Msg, rtt time.Duration, er return c.ExchangeWithConn(m, co) } +// BEGIN MONKEY PATCH +func (c *Client) ExchangeWithConnTo(m *Msg, conn *Conn, addr net.Addr) (r *Msg, rtt time.Duration, err error) { + conn.UnboundUDP = true + conn.RemoteAddr = addr + return c.ExchangeWithConn(m, conn) +} + +// END MONKEY PATCH + // ExchangeWithConn has the same behavior as Exchange, just with a predetermined connection // that will be used instead of creating a new one. // Usage pattern with a *dns.Client: From ae5efe68e3516d110c7950dee3a0def012ab7b74 Mon Sep 17 00:00:00 2001 From: phillip-stephens Date: Mon, 10 Jun 2024 13:01:03 +0900 Subject: [PATCH 3/6] re-added patch to Write --- client.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 559f1b7331..9cb24a9c2a 100644 --- a/client.go +++ b/client.go @@ -368,8 +368,17 @@ func (co *Conn) Write(p []byte) (int, error) { return 0, &Error{err: "message too large"} } - if isPacketConn(co.Conn) { + isPacketAConnection := isPacketConn(co.Conn) + + if isPacketAConnection && co.UnboundUDP { + pc, ok := co.Conn.(net.PacketConn) + if !ok { + return 0, &Error{err: "not a packet connection"} + } + return pc.WriteTo(p, co.RemoteAddr) + } else if isPacketAConnection { return co.Conn.Write(p) + } msg := make([]byte, 2+len(p)) From 9953c2641def25d666876246f19678f40ba68e5f Mon Sep 17 00:00:00 2001 From: phillip-stephens Date: Mon, 10 Jun 2024 13:01:36 +0900 Subject: [PATCH 4/6] added comments about monkey patch --- client.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 9cb24a9c2a..5b427d8278 100644 --- a/client.go +++ b/client.go @@ -368,6 +368,7 @@ func (co *Conn) Write(p []byte) (int, error) { return 0, &Error{err: "message too large"} } + // Begin Monkey Patch isPacketAConnection := isPacketConn(co.Conn) if isPacketAConnection && co.UnboundUDP { @@ -378,8 +379,8 @@ func (co *Conn) Write(p []byte) (int, error) { return pc.WriteTo(p, co.RemoteAddr) } else if isPacketAConnection { return co.Conn.Write(p) - } + // End Monkey Patch msg := make([]byte, 2+len(p)) binary.BigEndian.PutUint16(msg, uint16(len(p))) From 02ea112e8dacc916c50eb1fb2524434da219be89 Mon Sep 17 00:00:00 2001 From: phillip-stephens Date: Mon, 10 Jun 2024 13:02:52 +0900 Subject: [PATCH 5/6] added monkey-patched with context exchange --- client.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/client.go b/client.go index 5b427d8278..9b7b8b3227 100644 --- a/client.go +++ b/client.go @@ -181,6 +181,12 @@ func (c *Client) ExchangeWithConnTo(m *Msg, conn *Conn, addr net.Addr) (r *Msg, return c.ExchangeWithConn(m, conn) } +func (c *Client) ExchangeWithConnToContext(ctx context.Context, m *Msg, conn *Conn, addr net.Addr) (r *Msg, rtt time.Duration, err error) { + conn.UnboundUDP = true + conn.RemoteAddr = addr + return c.ExchangeWithConnContext(ctx, m, conn) +} + // END MONKEY PATCH // ExchangeWithConn has the same behavior as Exchange, just with a predetermined connection From 5e53c701e45f51085b6e94a937a2732fd6b289e3 Mon Sep 17 00:00:00 2001 From: phillip-stephens Date: Mon, 10 Jun 2024 13:05:31 +0900 Subject: [PATCH 6/6] formatting --- client.go | 1 + 1 file changed, 1 insertion(+) diff --git a/client.go b/client.go index 9b7b8b3227..5c3f012a00 100644 --- a/client.go +++ b/client.go @@ -175,6 +175,7 @@ func (c *Client) Exchange(m *Msg, address string) (r *Msg, rtt time.Duration, er } // BEGIN MONKEY PATCH + func (c *Client) ExchangeWithConnTo(m *Msg, conn *Conn, addr net.Addr) (r *Msg, rtt time.Duration, err error) { conn.UnboundUDP = true conn.RemoteAddr = addr