netutil

package
v0.0.0-...-9e8a4c4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 7, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenConfig

func ListenConfig() (lc *net.ListenConfig)

ListenConfig returns the default net.ListenConfig used by the plain-DNS servers in this module.

TODO(a.garipov): Add tests.

TODO(a.garipov): Add an option to not set SO_REUSEPORT on Unix to prevent issues with OpenWrt.

See https://github.com/AdguardTeam/AdGuardHome/issues/5872.

TODO(a.garipov): DRY with AdGuard DNS when we can.

func ParseSubnet

func ParseSubnet(s string) (p netip.Prefix, err error)

ParseSubnet parses s either as a CIDR prefix itself, or as an IP address, returning the corresponding single-IP CIDR prefix.

TODO(e.burkov): Move to golibs.

func SortIPAddrs

func SortIPAddrs(addrs []net.IPAddr, preferIPv6 bool)

SortIPAddrs sorts addrs in accordance with the protocol preferences. Invalid addresses are sorted near the end. Zones are ignored.

Example
package main

import (
	"fmt"
	"net"

	"github.com/Potterli20/dnsproxy-go-fork/internal/netutil"
)

func main() {
	printAddrs := func(header string, addrs []net.IPAddr) {
		fmt.Printf("%s:\n", header)
		for i, a := range addrs {
			fmt.Printf("%d: %s\n", i+1, a.IP)
		}

		fmt.Println()
	}

	addrs := []net.IPAddr{{
		IP: net.ParseIP("1.2.3.4"),
	}, {
		IP: net.ParseIP("1.2.3.5"),
	}, {
		IP: net.ParseIP("2a00::1234"),
	}, {
		IP: net.ParseIP("2a00::1235"),
	}, {
		IP: nil,
	}}
	netutil.SortIPAddrs(addrs, false)
	printAddrs("IPv4 preferred", addrs)

	netutil.SortIPAddrs(addrs, true)
	printAddrs("IPv6 preferred", addrs)

}
Output:


IPv4 preferred:
1: 1.2.3.4
2: 1.2.3.5
3: 2a00::1234
4: 2a00::1235
5: <nil>

IPv6 preferred:
1: 2a00::1234
2: 2a00::1235
3: 1.2.3.4
4: 1.2.3.5
5: <nil>

func SortNetIPAddrs

func SortNetIPAddrs(addrs []netip.Addr, preferIPv6 bool)

SortNetIPAddrs sorts addrs in accordance with the protocol preferences. Invalid addresses are sorted near the end. Zones are ignored.

func UDPGetOOBSize

func UDPGetOOBSize() (oobSize int)

UDPGetOOBSize returns maximum size of the received OOB data.

func UDPRead

func UDPRead(
	conn *net.UDPConn,
	buf []byte,
	udpOOBSize int,
) (n int, localIP net.IP, remoteAddr *net.UDPAddr, err error)

UDPRead reads the message from conn using buf and receives a control-message payload of size udpOOBSize from it. It returns the number of bytes copied into buf and the source address of the message.

TODO(s.chzhen): Consider using netip.Addr.

func UDPSetOptions

func UDPSetOptions(c *net.UDPConn) error

UDPSetOptions sets flag options on a UDP socket to be able to receive the necessary OOB data.

func UDPWrite

func UDPWrite(
	data []byte,
	conn *net.UDPConn,
	remoteAddr *net.UDPAddr,
	localIP net.IP,
) (n int, err error)

UDPWrite writes the data to the remoteAddr using conn.

TODO(s.chzhen): Consider using netip.Addr.

Types

type Hosts

type Hosts struct {
	// contains filtered or unexported fields
}

Hosts is a hostsfile.HandleSet that removes duplicates.

It must be initialized with NewHosts.

TODO(e.burkov): Think of storing only slices.

TODO(e.burkov): Move to netutil/hostsfile in module golibs as a default implementation of some storage interface.

func NewHosts

func NewHosts(readers ...io.Reader) (h *Hosts, err error)

NewHosts parses hosts files from r and returns a new Hosts set. readers are optional, the error is only returned in case of parsing error.

func (*Hosts) Add

func (h *Hosts) Add(rec *hostsfile.Record)

Add implements the hostsfile.Set interface for *Hosts.

func (*Hosts) ByAddr

func (h *Hosts) ByAddr(addr netip.Addr) (hosts []string)

ByAddr returns each host for addr in original case, in original adding order without duplicates. It returns nil if h doesn't contain the addr.

func (*Hosts) ByName

func (h *Hosts) ByName(host string) (addrs []netip.Addr)

ByName returns each address for host in original adding order without duplicates. It returns nil if h doesn't contain the host.

func (*Hosts) HandleInvalid

func (h *Hosts) HandleInvalid(srcName string, _ []byte, err error)

HandleInvalid implements the hostsfile.HandleSet interface for *Hosts.

func (*Hosts) Mappings

func (h *Hosts) Mappings() (names map[netip.Addr][]string, addrs map[string][]netip.Addr)

Mappings returns a deep clone of the internal mappings.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL