github.com/cilium/cilium@v1.16.2/pkg/fqdn/dnsproxy/helpers.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package dnsproxy
     5  
     6  import (
     7  	"fmt"
     8  	"net"
     9  
    10  	"github.com/cilium/dns"
    11  
    12  	"github.com/cilium/cilium/pkg/fqdn/restore"
    13  	"github.com/cilium/cilium/pkg/u8proto"
    14  )
    15  
    16  // lookupTargetDNSServer finds the intended DNS target server for a specific
    17  // request (passed in via ServeDNS). The IP:port:protocol combination is
    18  // returned.
    19  func lookupTargetDNSServer(w dns.ResponseWriter) (serverIP net.IP, serverPortProto restore.PortProto, addrStr string, err error) {
    20  	switch addr := (w.LocalAddr()).(type) {
    21  	case *net.UDPAddr:
    22  		return addr.IP, restore.MakeV2PortProto(uint16(addr.Port), uint8(u8proto.UDP)), addr.String(), nil
    23  	case *net.TCPAddr:
    24  		return addr.IP, restore.MakeV2PortProto(uint16(addr.Port), uint8(u8proto.TCP)), addr.String(), nil
    25  	default:
    26  		return nil, 0, addr.String(), fmt.Errorf("Cannot extract address information for type %T: %+v", addr, addr)
    27  	}
    28  }