blob: cb64702441267832b7342c4e516fec05b4d30c60 [file] [log] [blame]
Adrian Villin4677d922024-06-14 09:32:39 +02001package hst
Filip Tehlar3a910ab2023-06-08 17:39:39 +02002
adrianvillin28bd8f02024-02-13 06:00:02 -05003import (
4 "errors"
5 "fmt"
6 "os"
7 "strings"
8
9 "github.com/edwarnicke/exechelper"
10)
Filip Tehlar3a910ab2023-06-08 17:39:39 +020011
12type AddressCounter = int
13
14type Ip4AddressAllocator struct {
Adrian Villincee15aa2024-03-14 11:42:55 -040015 networks map[int]AddressCounter
adrianvillin28bd8f02024-02-13 06:00:02 -050016 chosenOctet int
17 assignedIps []string
Filip Tehlar3a910ab2023-06-08 17:39:39 +020018}
19
20func (a *Ip4AddressAllocator) AddNetwork(networkNumber int) {
21 a.networks[networkNumber] = 1
22}
23
24func (a *Ip4AddressAllocator) NewIp4InterfaceAddress(inputNetworkNumber ...int) (string, error) {
25 var networkNumber int = 0
26 if len(inputNetworkNumber) > 0 {
27 networkNumber = inputNetworkNumber[0]
28 }
29
30 if _, ok := a.networks[networkNumber]; !ok {
31 a.AddNetwork(networkNumber)
32 }
33
34 numberOfAddresses := a.networks[networkNumber]
35
36 if numberOfAddresses == 254 {
37 return "", fmt.Errorf("no available IPv4 addresses")
38 }
39
adrianvillin28bd8f02024-02-13 06:00:02 -050040 address, err := a.createIpAddress(networkNumber, numberOfAddresses)
41
Filip Tehlar3a910ab2023-06-08 17:39:39 +020042 a.networks[networkNumber] = numberOfAddresses + 1
43
adrianvillin28bd8f02024-02-13 06:00:02 -050044 return address + "/24", err
45}
46
47// Creates a file every time an IP is assigned: used to keep track of addresses in use.
48// If an address is not in use, 'counter' is then copied to 'chosenOctet' and it is used for the remaining tests.
49// Also checks host IP addresses.
Adrian Villincee15aa2024-03-14 11:42:55 -040050func (a *Ip4AddressAllocator) createIpAddress(networkNumber int, numberOfAddresses int) (string, error) {
adrianvillin28bd8f02024-02-13 06:00:02 -050051 hostIps, _ := exechelper.CombinedOutput("ip a")
52 counter := 10
53 var address string
54
55 for {
56 if a.chosenOctet != 0 {
57 address = fmt.Sprintf("10.%v.%v.%v", a.chosenOctet, networkNumber, numberOfAddresses)
58 file, err := os.Create(address)
Adrian Villincee15aa2024-03-14 11:42:55 -040059 if err != nil {
adrianvillin28bd8f02024-02-13 06:00:02 -050060 return "", errors.New("unable to create file: " + fmt.Sprint(err))
61 }
62 file.Close()
63 break
64 } else {
65 address = fmt.Sprintf("10.%v.%v.%v", counter, networkNumber, numberOfAddresses)
66 _, err := os.Stat(address)
67 if err == nil || strings.Contains(string(hostIps), address) {
68 counter++
69 } else if os.IsNotExist(err) {
70 file, err := os.Create(address)
Adrian Villincee15aa2024-03-14 11:42:55 -040071 if err != nil {
adrianvillin28bd8f02024-02-13 06:00:02 -050072 return "", errors.New("unable to create file: " + fmt.Sprint(err))
Adrian Villincee15aa2024-03-14 11:42:55 -040073 }
adrianvillin28bd8f02024-02-13 06:00:02 -050074 file.Close()
75 a.chosenOctet = counter
76 break
77 } else {
Adrian Villincee15aa2024-03-14 11:42:55 -040078 return "", errors.New("an error occurred while checking if a file exists: " + fmt.Sprint(err))
adrianvillin28bd8f02024-02-13 06:00:02 -050079 }
80 }
81 }
82
83 a.assignedIps = append(a.assignedIps, address)
Filip Tehlar3a910ab2023-06-08 17:39:39 +020084 return address, nil
85}
86
Adrian Villin4677d922024-06-14 09:32:39 +020087func (a *Ip4AddressAllocator) DeleteIpAddresses() {
Adrian Villincee15aa2024-03-14 11:42:55 -040088 for ip := range a.assignedIps {
adrianvillin28bd8f02024-02-13 06:00:02 -050089 os.Remove(a.assignedIps[ip])
90 }
91}
92
Filip Tehlar3a910ab2023-06-08 17:39:39 +020093func NewIp4AddressAllocator() *Ip4AddressAllocator {
94 var ip4AddrAllocator = new(Ip4AddressAllocator)
95 ip4AddrAllocator.networks = make(map[int]AddressCounter)
96 ip4AddrAllocator.AddNetwork(0)
97 return ip4AddrAllocator
98}