Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release IP address on session stop #5970

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions services/wireguard/endpoint/endpoint_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build !windows

/*
* Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
*
Expand Down
14 changes: 0 additions & 14 deletions services/wireguard/endpoint/endpoint_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,3 @@
*/

package endpoint

import (
"net"

"github.com/rs/zerolog/log"
)

func (ce *connectionEndpoint) consumerIP(subnet net.IPNet) net.IP {
ipnet, err := ce.resourceAllocator.AllocateIPNet()
if err != nil {
log.Error().Err(err).Msgf("Failed to allocate IPNet")
}
return ipnet.IP
}
2 changes: 0 additions & 2 deletions services/wireguard/resources/allocator_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build !windows

/*
* Copyright (C) 2018 The "MysteriumNetwork/node" Authors.
*
Expand Down
101 changes: 0 additions & 101 deletions services/wireguard/resources/allocator_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,104 +16,3 @@
*/

package resources

import (
"net"
"sync"

"github.com/mysteriumnetwork/node/core/port"
"github.com/pkg/errors"
)

// MaxConnections sets the limit to the maximum number of wireguard connections.
// x.x.x.0, x.x.x.1 and x.x.x.255 are reserved
var MaxConnections = 253

type portSupplier interface {
Acquire() (port.Port, error)
}

// Allocator is mock wireguard resource handler.
// It will manage lists of network interfaces names, IP addresses and port for endpoints.
type Allocator struct {
IPAddresses map[int]struct{}
mu sync.Mutex

portSupplier portSupplier
subnet net.IPNet
}

// NewAllocator creates new resource pool for wireguard connection.
func NewAllocator(portSupplier portSupplier, subnet net.IPNet) *Allocator {
return &Allocator{
IPAddresses: make(map[int]struct{}),

portSupplier: portSupplier,
subnet: subnet,
}
}

// AbandonedInterfaces is not required for Windows implementation and left here just to satisfy the interface.
func (a *Allocator) AbandonedInterfaces() ([]net.Interface, error) {
return nil, nil
}

// AllocateInterface provides available name for the wireguard network interface.
func (a *Allocator) AllocateInterface() (string, error) {
return interfacePrefix, nil
}

// AllocateIPNet provides available IP address for the wireguard connection.
func (a *Allocator) AllocateIPNet() (net.IPNet, error) {
a.mu.Lock()
defer a.mu.Unlock()

availableOctetMin := 2
availableOctetMax := MaxConnections + 2

for i := availableOctetMin; i < availableOctetMax; i++ {
if _, ok := a.IPAddresses[i]; !ok {
a.IPAddresses[i] = struct{}{}
return calcIPNet(a.subnet, i), nil
}
}
return net.IPNet{}, errors.New("no more unused subnets")
}

// AllocatePort provides available UDP port for the wireguard endpoint.
func (a *Allocator) AllocatePort() (int, error) {
p, err := a.portSupplier.Acquire()
return int(p), err
}

// ReleaseInterface is not required for Windows implementation and left here just to satisfy the interface.
func (a *Allocator) ReleaseInterface(iface string) error {
return nil
}

// ReleaseIPNet releases IP address.
func (a *Allocator) ReleaseIPNet(ipnet net.IPNet) error {
a.mu.Lock()
defer a.mu.Unlock()

ip4 := ipnet.IP.To4()
if ip4 == nil {
return errors.New("allocated subnet not found")
}

i := int(ip4[3])
if _, ok := a.IPAddresses[i]; !ok {
return errors.New("allocated subnet not found")
}

delete(a.IPAddresses, i)
return nil
}

func calcIPNet(ipnet net.IPNet, index int) net.IPNet {
ip := make(net.IP, len(ipnet.IP))
copy(ip, ipnet.IP)
ip = ip.To4()
ip[3] = byte(index)
return net.IPNet{IP: ip, Mask: net.IPv4Mask(255, 255, 255, 0)}
}