Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/networks/usernet/config.go
2621 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package usernet
5
6
import (
7
"fmt"
8
"net"
9
"path/filepath"
10
11
"github.com/apparentlymart/go-cidr/cidr"
12
13
"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
14
"github.com/lima-vm/lima/v2/pkg/networks"
15
"github.com/lima-vm/lima/v2/pkg/osutil"
16
)
17
18
type SockType = string
19
20
const (
21
FDSock = "fd"
22
QEMUSock = "qemu"
23
EndpointSock = "ep"
24
)
25
26
// Sock returns a usernet socket based on name and sockType.
27
func Sock(name string, sockType SockType) (string, error) {
28
dir, err := dirnames.LimaNetworksDir()
29
if err != nil {
30
return "", err
31
}
32
return SockWithDirectory(filepath.Join(dir, name), name, sockType)
33
}
34
35
// SockWithDirectory return a usernet socket based on dir, name and sockType.
36
func SockWithDirectory(dir, name string, sockType SockType) (string, error) {
37
if name == "" {
38
name = "default"
39
}
40
sockPath := filepath.Join(dir, fmt.Sprintf("%s_%s.sock", name, sockType))
41
if len(sockPath) >= osutil.UnixPathMax {
42
return "", fmt.Errorf("usernet socket path %q too long: must be less than UNIX_PATH_MAX=%d characters, but is %d",
43
sockPath, osutil.UnixPathMax, len(sockPath))
44
}
45
return sockPath, nil
46
}
47
48
// PIDFile returns a path for usernet PID file.
49
func PIDFile(name string) (string, error) {
50
dir, err := dirnames.LimaNetworksDir()
51
if err != nil {
52
return "", err
53
}
54
return filepath.Join(dir, name, fmt.Sprintf("usernet_%s.pid", name)), nil
55
}
56
57
// SubnetCIDR returns a subnet in form of net.IPNet for the given network name.
58
func SubnetCIDR(name string) (*net.IPNet, error) {
59
cfg, err := networks.LoadConfig()
60
if err != nil {
61
return nil, err
62
}
63
err = cfg.Check(name)
64
if err != nil {
65
return nil, err
66
}
67
_, ipNet, err := netmaskToCidr(cfg.Networks[name].Gateway, cfg.Networks[name].NetMask)
68
if err != nil {
69
return nil, err
70
}
71
return ipNet, err
72
}
73
74
// Subnet returns a subnet net.IP for the given network name.
75
func Subnet(name string) (net.IP, error) {
76
cfg, err := networks.LoadConfig()
77
if err != nil {
78
return nil, err
79
}
80
err = cfg.Check(name)
81
if err != nil {
82
return nil, err
83
}
84
_, ipNet, err := netmaskToCidr(cfg.Networks[name].Gateway, cfg.Networks[name].NetMask)
85
if err != nil {
86
return nil, err
87
}
88
return ipNet.IP, err
89
}
90
91
// GatewayIP returns the 2nd IP for the given subnet.
92
func GatewayIP(subnet net.IP) string {
93
return cidr.Inc(cidr.Inc(subnet)).String()
94
}
95
96
// DNSIP returns the 3rd IP for the given subnet.
97
func DNSIP(subnet net.IP) string {
98
return cidr.Inc(cidr.Inc(cidr.Inc(subnet))).String()
99
}
100
101
// Leases returns a leases file based on network name.
102
func Leases(name string) (string, error) {
103
dir, err := dirnames.LimaNetworksDir()
104
if err != nil {
105
return "", err
106
}
107
sockPath := filepath.Join(filepath.Join(dir, name), "leases.json")
108
if len(sockPath) >= osutil.UnixPathMax {
109
return "", fmt.Errorf("usernet leases path %q too long: must be less than UNIX_PATH_MAX=%d characters, but is %d",
110
sockPath, osutil.UnixPathMax, len(sockPath))
111
}
112
return sockPath, nil
113
}
114
115
func netmaskToCidr(baseIP, netMask net.IP) (net.IP, *net.IPNet, error) {
116
size, _ := net.IPMask(netMask.To4()).Size()
117
return net.ParseCIDR(fmt.Sprintf("%s/%d", baseIP.String(), size))
118
}
119
120