network

package
v1.34.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Inspect = func(addr string) (*Parameters, error) {

	ip, network, err := ParseAddr(addr)
	if err != nil {
		return nil, err
	}

	n := &Parameters{}

	ifParams, ifNet, err := lookupInInterfaces(ip)
	if err != nil {
		return nil, err
	}
	if ifNet != nil {
		network = ifNet
		n = ifParams
	}

	if network == nil {
		ipnet := &net.IPNet{
			IP:   ip,
			Mask: ip.DefaultMask(),
		}
		_, network, err = net.ParseCIDR(ipnet.String())
		if err != nil {
			return nil, fmt.Errorf("failed determining address of network from %s: %w", addr, err)
		}
	}

	n.IP = network.IP.String()
	n.Netmask = net.IP(network.Mask).String()
	n.Prefix, _ = network.Mask.Size()
	n.CIDR = network.String()
	n.IsPrivate = network.IP.IsPrivate()

	networkIP := binary.BigEndian.Uint32(network.IP)
	networkMask := binary.BigEndian.Uint32(network.Mask)
	broadcastIP := (networkIP & networkMask) | (networkMask ^ 0xffffffff)

	broadcast := make(net.IP, 4)
	binary.BigEndian.PutUint32(broadcast, broadcastIP)
	n.Broadcast = broadcast.String()

	gateway := net.ParseIP(n.Gateway).To4()
	if gateway == nil {
		gateway = make(net.IP, 4)
		binary.BigEndian.PutUint32(gateway, networkIP+1)
		n.Gateway = gateway.String()
	}
	gatewayIP := binary.BigEndian.Uint32(gateway)

	min := make(net.IP, 4)
	binary.BigEndian.PutUint32(min, gatewayIP+1)
	n.ClientMin = min.String()

	max := make(net.IP, 4)
	binary.BigEndian.PutUint32(max, broadcastIP-1)
	n.ClientMax = max.String()

	return n, nil
}

Inspect initialises IPv4 network parameters struct from given address addr. addr can be single address (like "192.168.17.42"), network address (like "192.168.17.0") or in CIDR form (like "192.168.17.42/24 or "192.168.17.0/24"). If addr belongs to network of local network interface, parameters will also contain info about that network interface.

Functions

func IsBuiltinQEMU added in v1.30.0

func IsBuiltinQEMU(network string) bool

IsBuiltinQEMU returns if network is builtin or the legacy value user.

func ParseAddr added in v1.29.0

func ParseAddr(addr string) (net.IP, *net.IPNet, error)

ParseAddr will try to parse an ip or a cidr address

Types

type Interface

type Interface struct {
	IfaceName string
	IfaceIPv4 string
	IfaceMTU  int
	IfaceMAC  string
}

Interface contains main network interface parameters.

type Parameters

type Parameters struct {
	IP        string // IP address of network
	Netmask   string // dotted-decimal format ('a.b.c.d')
	Prefix    int    // network prefix length (number of leading ones in network mask)
	CIDR      string // CIDR format ('a.b.c.d/n')
	Gateway   string // taken from network interface address or assumed as first network IP address from given addr
	ClientMin string // first available client IP address after gateway
	ClientMax string // last available client IP address before broadcast
	Broadcast string // last network IP address
	IsPrivate bool   // whether the IP is private or not
	Interface
	// contains filtered or unexported fields
}

Parameters contains main network parameters.

func FreeSubnet

func FreeSubnet(startSubnet string, step, tries int) (*Parameters, error)

FreeSubnet will try to find free private network beginning with startSubnet, incrementing it in steps up to number of tries.

Jump to

Keyboard shortcuts

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