File: utils.go

package info (click to toggle)
incus 6.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,392 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (64 lines) | stat: -rw-r--r-- 1,483 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package ip

import (
	"fmt"
	"net"
	"strconv"
	"strings"

	"github.com/vishvananda/netlink"
	"golang.org/x/sys/unix"
)

// Family can be { FamilyAll, FamilyV4, FamilyV6 }.
type Family int

const (
	// FamilyAll specifies any/all family.
	FamilyAll Family = unix.AF_UNSPEC

	// FamilyV4 specifies the IPv4 family.
	FamilyV4 Family = unix.AF_INET

	// FamilyV6 specifies the IPv6 family.
	FamilyV6 Family = unix.AF_INET6
)

func linkByName(name string) (netlink.Link, error) {
	link, err := netlink.LinkByName(name)
	if err != nil {
		return nil, fmt.Errorf("Failed to get link %q: %w", name, err)
	}

	return link, nil
}

func parseHandle(id string) (uint32, error) {
	if id == "root" {
		return netlink.HANDLE_ROOT, nil
	}

	majorStr, minorStr, found := strings.Cut(id, ":")

	if !found {
		return 0, fmt.Errorf("Invalid handle %q", id)
	}

	major, err := strconv.ParseUint(majorStr, 16, 16)
	if err != nil {
		return 0, fmt.Errorf("Invalid handle %q: %w", id, err)
	}

	minor, err := strconv.ParseUint(minorStr, 16, 16)
	if err != nil {
		return 0, fmt.Errorf("Invalid handle %q: %w", id, err)
	}

	return netlink.MakeHandle(uint16(major), uint16(minor)), nil
}

// ParseIPNet parses a CIDR string and returns a *net.IPNet containing both the full address and the netmask,
// Unlike net.ParseCIDR which zeroes out the host part in the returned *net.IPNet and returns the full address separately.
func ParseIPNet(addr string) (*net.IPNet, error) {
	return netlink.ParseIPNet(addr)
}