netutil

package
v0.65.2 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package netutil contains network-related utilities common among dnsproxy packages.

TODO(a.garipov): Move improved versions of these into netutil in module golibs.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenConfig added in v0.49.0

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 added in v0.58.0

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 PreferIPv4 added in v0.62.0

func PreferIPv4(a, b netip.Addr) (res int)

PreferIPv4 compares two addresses, preferring IPv4 addresses over IPv6 ones. Invalid addresses are sorted near the end.

func PreferIPv6 added in v0.62.0

func PreferIPv6(a, b netip.Addr) (res int)

PreferIPv6 compares two addresses, preferring IPv6 addresses over IPv4 ones. Invalid addresses are sorted near the end.

func SortNetIPAddrs added in v0.49.0

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.

Example
package main

import (
	"fmt"
	"net/netip"

	"github.com/AdguardTeam/dnsproxy/internal/netutil"
)

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

		fmt.Println()
	}

	addrs := []netip.Addr{
		netip.MustParseAddr("1.2.3.4"),
		netip.MustParseAddr("1.2.3.5"),
		netip.MustParseAddr("2a00::1234"),
		netip.MustParseAddr("2a00::1235"),
		{},
	}
	netutil.SortNetIPAddrs(addrs, false)
	printAddrs("IPv4 preferred", addrs)

	netutil.SortNetIPAddrs(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: invalid IP

IPv6 preferred:
1: 2a00::1234
2: 2a00::1235
3: 1.2.3.4
4: 1.2.3.5
5: invalid IP

func UDPGetOOBSize added in v0.49.0

func UDPGetOOBSize() (oobSize int)

UDPGetOOBSize returns maximum size of the received OOB data.

func UDPRead added in v0.49.0

func UDPRead(
	conn *net.UDPConn,
	buf []byte,
	udpOOBSize int,
) (n int, localIP netip.Addr, 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 added in v0.49.0

func UDPSetOptions(c *net.UDPConn) (err error)

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

func UDPWrite added in v0.49.0

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

UDPWrite writes the data to the remoteAddr using conn.

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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