allocator.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2018-present the CoreDHCP Authors. All rights reserved
  2. // This source code is licensed under the MIT license found in the
  3. // LICENSE file in the root directory of this source tree.
  4. // Package allocators provides the interface and the algorithm(s) for allocation of ipv6
  5. // prefixes of various sizes within a larger prefix.
  6. // There are many many parallels with memory allocation.
  7. package allocators
  8. import (
  9. "errors"
  10. "fmt"
  11. "net"
  12. )
  13. // Allocator is the interface to the address allocator. It only finds and
  14. // allocates blocks and is not concerned with lease-specific questions like
  15. // expiration (ie garbage collection needs to be handled separately)
  16. type Allocator interface {
  17. // Allocate finds a suitable prefix of the given size and returns it.
  18. //
  19. // hint is a prefix, which the client desires especially, and that the
  20. // allocator MAY try to return; the allocator SHOULD try to return a prefix of
  21. // the same size as the given hint prefix. The allocator MUST NOT return an
  22. // error if a prefix was successfully assigned, even if the prefix has nothing
  23. // in common with the hinted prefix
  24. Allocate(hint net.IPNet) (net.IPNet, error)
  25. // Free returns the prefix containing the given network to the pool
  26. //
  27. // Free may return a DoubleFreeError if the prefix being returned was not
  28. // previously allocated
  29. Free(net.IPNet) error
  30. }
  31. // ErrDoubleFree is an error type returned by Allocator.Free() when a
  32. // non-allocated block is passed
  33. type ErrDoubleFree struct {
  34. Loc net.IPNet
  35. }
  36. // String returns a human-readable error message for a DoubleFree error
  37. func (err *ErrDoubleFree) Error() string {
  38. return fmt.Sprint("Attempted to free unallocated block at ", err.Loc.String())
  39. }
  40. // ErrNoAddrAvail is returned when we can't allocate an IP because there's no unallocated space left
  41. var ErrNoAddrAvail = errors.New("no address available to allocate")