MCPcopy
hub / github.com/lxc/incus / complementRanges

Function complementRanges

internal/server/network/network_utils.go:1557–1604  ·  view source on GitHub ↗

complementRanges returns the complement of the provided IP network ranges. It calculates the IP ranges that are *not* covered by the input slice.

(ranges []*iprange.Range, netAddr *net.IPNet)

Source from the content-addressed store, hash-verified

1555// complementRanges returns the complement of the provided IP network ranges.
1556// It calculates the IP ranges that are *not* covered by the input slice.
1557func complementRanges(ranges []*iprange.Range, netAddr *net.IPNet) ([]iprange.Range, error) {
1558 var complement []iprange.Range
1559
1560 ipv4NetPrefix, err := netip.ParsePrefix(netAddr.String())
1561 if err != nil {
1562 return nil, err
1563 }
1564
1565 previousEnd := ipv4NetPrefix.Addr()
1566
1567 for _, r := range ranges {
1568 startAddr, err := netip.ParseAddr(r.Start.String())
1569 if err != nil {
1570 return nil, err
1571 }
1572
1573 endAddr, err := netip.ParseAddr(r.End.String())
1574 if err != nil {
1575 return nil, err
1576 }
1577
1578 if startAddr.Compare(previousEnd.Next()) == 1 {
1579 newStart := previousEnd.Next()
1580 newEnd := startAddr.Prev()
1581
1582 if newStart.Compare(newEnd) == 0 {
1583 complement = append(complement, iprange.Range{Start: net.ParseIP(newStart.String())})
1584 } else {
1585 complement = append(complement, iprange.Range{Start: net.ParseIP(newStart.String()), End: net.ParseIP(newEnd.String())})
1586 }
1587 }
1588
1589 if endAddr.Compare(previousEnd) == 1 {
1590 previousEnd = endAddr
1591 }
1592 }
1593
1594 endAddr, err := netip.ParseAddr(dhcpalloc.GetIP(netAddr, -2).String())
1595 if err != nil {
1596 return nil, err
1597 }
1598
1599 if previousEnd.Compare(endAddr) == -1 {
1600 complement = append(complement, iprange.Range{Start: net.ParseIP(previousEnd.Next().String()), End: net.ParseIP(endAddr.String())})
1601 }
1602
1603 return complement, nil
1604}
1605
1606// ipInRanges checks whether the given IP address is contained within any of the
1607// provided IP network ranges.

Callers 2

Example_complementRangesFunction · 0.85
getDHCPv4ReservationsMethod · 0.85

Calls 4

GetIPFunction · 0.92
AddrMethod · 0.80
CompareMethod · 0.80
StringMethod · 0.65

Tested by 1

Example_complementRangesFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…