fix(forward): use netip package for parsing (#7472)

Replace manual host:port parsing using net.SplitHostPort +
strconv.ParseUint with the standard library net/netip function
ParseAddrPort. This eliminates integer conversion warnings and
improves type safety.

Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
This commit is contained in:
Ville Vesilehto
2025-08-25 23:06:44 +03:00
committed by GitHub
parent f4ab4d9ed5
commit 2f981ff797

View File

@@ -3,7 +3,7 @@ package forward
import ( import (
"context" "context"
"net" "net"
"strconv" "net/netip"
"time" "time"
"github.com/coredns/coredns/plugin/dnstap/msg" "github.com/coredns/coredns/plugin/dnstap/msg"
@@ -16,11 +16,14 @@ import (
// toDnstap will send the forward and received message to the dnstap plugin. // toDnstap will send the forward and received message to the dnstap plugin.
func toDnstap(ctx context.Context, f *Forward, host string, state request.Request, opts proxy.Options, reply *dns.Msg, start time.Time) { func toDnstap(ctx context.Context, f *Forward, host string, state request.Request, opts proxy.Options, reply *dns.Msg, start time.Time) {
h, p, _ := net.SplitHostPort(host) // this is preparsed and can't err here ap, _ := netip.ParseAddrPort(host) // this is preparsed and can't err here
port, _ := strconv.ParseUint(p, 10, 32) // same here ip := net.IP(ap.Addr().AsSlice())
ip := net.ParseIP(h) port := int(ap.Port())
var ta net.Addr = &net.UDPAddr{IP: ip, Port: int(port)} var ta net.Addr = &net.UDPAddr{
IP: ip,
Port: port,
}
t := state.Proto() t := state.Proto()
switch { switch {
case opts.ForceTCP: case opts.ForceTCP:
@@ -30,7 +33,7 @@ func toDnstap(ctx context.Context, f *Forward, host string, state request.Reques
} }
if t == "tcp" { if t == "tcp" {
ta = &net.TCPAddr{IP: ip, Port: int(port)} ta = &net.TCPAddr{IP: ip, Port: port}
} }
for _, t := range f.tapPlugins { for _, t := range f.tapPlugins {