aboutsummaryrefslogtreecommitdiff
path: root/libvirt/utils_net.go
blob: 140c4a8d9d42ee7d5afcd6631b965ed650eb8bf4 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package libvirt

import (
	"fmt"
	"io/ioutil"
	"math/rand"
	"net"
	"net/http"
	"os"
	"path"
	"time"
)

const (
	maxIfaceNum = 100
)

// RandomMACAddress returns a randomized MAC address
func RandomMACAddress() (string, error) {
	buf := make([]byte, 6)
	_, err := rand.Read(buf)
	if err != nil {
		return "", err
	}

	// set local bit and unicast
	buf[0] = (buf[0] | 2) & 0xfe
	// Set the local bit
	buf[0] |= 2

	// avoid libvirt-reserved addresses
	if buf[0] == 0xfe {
		buf[0] = 0xee
	}

	return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x",
		buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]), nil
}

// RandomPort returns a random port
func RandomPort() int {
	const minPort = 1024
	const maxPort = 65535

	rand.Seed(time.Now().Unix())
	return rand.Intn(maxPort-minPort) + minPort
}

// FreeNetworkInterface returns a free network interface
func FreeNetworkInterface(basename string) (string, error) {
	for i := 0; i < maxIfaceNum; i++ {
		ifaceName := fmt.Sprintf("%s%d", basename, i)
		_, err := net.InterfaceByName(ifaceName)
		if err != nil {
			return ifaceName, nil
		}
	}
	return "", fmt.Errorf("could not obtain a free network interface")
}

// NetworkRange calculates the first and last IP addresses in an IPNet
func NetworkRange(network *net.IPNet) (net.IP, net.IP) {
	netIP := network.IP.To4()
	lastIP := net.IPv4(0, 0, 0, 0).To4()
	if netIP == nil {
		netIP = network.IP.To16()
		lastIP = net.IPv6zero.To16()
	}
	firstIP := netIP.Mask(network.Mask)
	for i := 0; i < len(lastIP); i++ {
		lastIP[i] = netIP[i] | ^network.Mask[i]
	}
	return firstIP, lastIP
}

// a HTTP server that serves files in a directory, used mostly for testing
type fileWebServer struct {
	Dir  string
	Port int
	URL  string

	server *http.Server
}

func (fws *fileWebServer) Start() error {
	dir, err := ioutil.TempDir(fws.Dir, "")
	if err != nil {
		return err
	}

	fws.Dir = dir
	fws.Port = RandomPort()
	fws.URL = fmt.Sprintf("http://127.0.0.1:%d", fws.Port)

	handler := http.NewServeMux()
	handler.Handle("/", http.FileServer(http.Dir(dir)))
	fws.server = &http.Server{Addr: fmt.Sprintf(":%d", fws.Port), Handler: handler}
	ln, err := net.Listen("tcp", fws.server.Addr)
	if err != nil {
		return err
	}
	go fws.server.Serve(ln)
	return nil
}

// Adds a file (with some content) in the directory served by the fileWebServer
func (fws *fileWebServer) AddFile(content []byte) (string, *os.File, error) {
	tmpfile, err := ioutil.TempFile(fws.Dir, "file-")
	if err != nil {
		return "", nil, err
	}

	if len(content) > 0 {
		if _, err := tmpfile.Write(content); err != nil {
			return "", nil, err
		}
	}

	return fmt.Sprintf("%s/%s", fws.URL, path.Base(tmpfile.Name())), tmpfile, nil
}

func (fws *fileWebServer) Stop() {
	os.RemoveAll(fws.Dir)
}