| 1 | // Package cidr is a collection of assorted utilities for computing |
| 2 | // network and host addresses within network ranges. |
| 3 | // |
| 4 | // It expects a CIDR-type address structure where addresses are divided into |
| 5 | // some number of prefix bits representing the network and then the remaining |
| 6 | // suffix bits represent the host. |
| 7 | // |
| 8 | // For example, it can help to calculate addresses for sub-networks of a |
| 9 | // parent network, or to calculate host addresses within a particular prefix. |
| 10 | // |
| 11 | // At present this package is prioritizing simplicity of implementation and |
| 12 | // de-prioritizing speed and memory usage. Thus caution is advised before |
| 13 | // using this package in performance-critical applications or hot codepaths. |
| 14 | // Patches to improve the speed and memory usage may be accepted as long as |
| 15 | // they do not result in a significant increase in code complexity. |
| 16 | package cidrscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/ |
| 17 | |
| 18 | import ( |
| 19 | "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/" |
| 20 | "math/bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/" |
| 21 | "netscip-go gomod github.com/golang/go/src go1.16 net/" |
| 22 | ) |
| 23 | |
| 24 | // Subnet takes a parent CIDR range and creates a subnet within it |
| 25 | // with the given number of additional prefix bits and the given |
| 26 | // network number. |
| 27 | // |
| 28 | // For example, 10.3.0.0/16, extended by 8 bits, with a network number |
| 29 | // of 5, becomes 10.3.5.0/24 . |
| 30 | func Subnetscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/Subnet().(base *netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#, newBits int, num int) (*netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#, error) { |
| 31 | return SubnetBigscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/SubnetBig().(base, newBits, bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(num))) |
| 32 | } |
| 33 | |
| 34 | // SubnetBig takes a parent CIDR range and creates a subnet within it with the |
| 35 | // given number of additional prefix bits and the given network number. It |
| 36 | // differs from Subnet in that it takes a *big.Int for the num, instead of an int. |
| 37 | // |
| 38 | // For example, 10.3.0.0/16, extended by 8 bits, with a network number of 5, |
| 39 | // becomes 10.3.5.0/24 . |
| 40 | func SubnetBigscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/SubnetBig().(base *netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#, newBits int, num *bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#) (*netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#, error) { |
| 41 | ip := base.IPscip-go gomod github.com/golang/go/src go1.16 net/IPNet#IP. |
| 42 | mask := base.Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask. |
| 43 | |
| 44 | parentLen, addrLen := mask.Sizescip-go gomod github.com/golang/go/src go1.16 net/IPMask#Size().() |
| 45 | newPrefixLen := parentLen + newBits |
| 46 | |
| 47 | if newPrefixLen > addrLen { |
| 48 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("insufficient address space to extend prefix of %d by %d", parentLen, newBits) |
| 49 | } |
| 50 | |
| 51 | maxNetNum := uint64(1<<uint64(newBits)) - 1 |
| 52 | if num.Uint64scip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Uint64().() > maxNetNum { |
| 53 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("prefix extension of %d does not accommodate a subnet numbered %d", newBits, num) |
| 54 | } |
| 55 | |
| 56 | return &netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#{ |
| 57 | IPscip-go gomod github.com/golang/go/src go1.16 net/IPNet#IP.: insertNumIntoIPscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/insertNumIntoIP().(ip, num, newPrefixLen), |
| 58 | Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask.: netscip-go gomod github.com/golang/go/src go1.16 net/.CIDRMaskscip-go gomod github.com/golang/go/src go1.16 net/CIDRMask().(newPrefixLen, addrLen), |
| 59 | }, nil |
| 60 | } |
| 61 | |
| 62 | // Host takes a parent CIDR range and turns it into a host IP address with the |
| 63 | // given host number. |
| 64 | // |
| 65 | // For example, 10.3.0.0/16 with a host number of 2 gives 10.3.0.2. |
| 66 | func Hostscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/Host().(base *netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#, num int) (netscip-go gomod github.com/golang/go/src go1.16 net/.IPscip-go gomod github.com/golang/go/src go1.16 net/IP#, error) { |
| 67 | return HostBigscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/HostBig().(base, bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(num))) |
| 68 | } |
| 69 | |
| 70 | // HostBig takes a parent CIDR range and turns it into a host IP address with |
| 71 | // the given host number. It differs from Host in that it takes a *big.Int for |
| 72 | // the num, instead of an int. |
| 73 | // |
| 74 | // For example, 10.3.0.0/16 with a host number of 2 gives 10.3.0.2. |
| 75 | func HostBigscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/HostBig().(base *netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#, num *bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#) (netscip-go gomod github.com/golang/go/src go1.16 net/.IPscip-go gomod github.com/golang/go/src go1.16 net/IP#, error) { |
| 76 | ip := base.IPscip-go gomod github.com/golang/go/src go1.16 net/IPNet#IP. |
| 77 | mask := base.Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask. |
| 78 | |
| 79 | parentLen, addrLen := mask.Sizescip-go gomod github.com/golang/go/src go1.16 net/IPMask#Size().() |
| 80 | hostLen := addrLen - parentLen |
| 81 | |
| 82 | maxHostNum := bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(1)) |
| 83 | maxHostNum.Lshscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Lsh().(maxHostNum, uint(hostLen)) |
| 84 | maxHostNum.Subscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Sub().(maxHostNum, bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(1)) |
| 85 | |
| 86 | numUint64 := bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(num.Uint64scip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Uint64().())) |
| 87 | if num.Cmpscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Cmp().(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(0)) == -1 { |
| 88 | numUint64.Negscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Neg().(num) |
| 89 | numUint64.Subscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Sub().(numUint64, bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(int64(1))) |
| 90 | num.Subscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Sub().(maxHostNum, numUint64) |
| 91 | } |
| 92 | |
| 93 | if numUint64.Cmpscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Cmp().(maxHostNum) == 1 { |
| 94 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("prefix of %d does not accommodate a host numbered %d", parentLen, num) |
| 95 | } |
| 96 | var bitlength int |
| 97 | if ip.To4scip-go gomod github.com/golang/go/src go1.16 net/IP#To4().() != nil { |
| 98 | bitlength = 32 |
| 99 | } else { |
| 100 | bitlength = 128 |
| 101 | } |
| 102 | return insertNumIntoIPscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/insertNumIntoIP().(ip, num, bitlength), nil |
| 103 | } |
| 104 | |
| 105 | // AddressRange returns the first and last addresses in the given CIDR range. |
| 106 | func AddressRangescip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/AddressRange().(network *netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#) (netscip-go gomod github.com/golang/go/src go1.16 net/.IPscip-go gomod github.com/golang/go/src go1.16 net/IP#, netscip-go gomod github.com/golang/go/src go1.16 net/.IPscip-go gomod github.com/golang/go/src go1.16 net/IP#) { |
| 107 | // the first IP is easy |
| 108 | firstIP := network.IPscip-go gomod github.com/golang/go/src go1.16 net/IPNet#IP. |
| 109 | |
| 110 | // the last IP is the network address OR NOT the mask address |
| 111 | prefixLen, bits := network.Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask..Sizescip-go gomod github.com/golang/go/src go1.16 net/IPMask#Size().() |
| 112 | if prefixLen == bits { |
| 113 | // Easy! |
| 114 | // But make sure that our two slices are distinct, since they |
| 115 | // would be in all other cases. |
| 116 | lastIP := make([]byte, len(firstIP)) |
| 117 | copy(lastIP, firstIP) |
| 118 | return firstIP, lastIP |
| 119 | } |
| 120 | |
| 121 | firstIPInt, bits := ipToIntscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/ipToInt().(firstIP) |
| 122 | hostLen := uint(bits) - uint(prefixLen) |
| 123 | lastIPInt := bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(1) |
| 124 | lastIPInt.Lshscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Lsh().(lastIPInt, hostLen) |
| 125 | lastIPInt.Subscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Sub().(lastIPInt, bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.16 `math/big`/NewInt().(1)) |
| 126 | lastIPInt.Orscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Or().(lastIPInt, firstIPInt) |
| 127 | |
| 128 | return firstIP, intToIPscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/intToIP().(lastIPInt, bits) |
| 129 | } |
| 130 | |
| 131 | // AddressCount returns the number of distinct host addresses within the given |
| 132 | // CIDR range. |
| 133 | // |
| 134 | // Since the result is a uint64, this function returns meaningful information |
| 135 | // only for IPv4 ranges and IPv6 ranges with a prefix size of at least 65. |
| 136 | func AddressCountscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/AddressCount().(network *netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#) uint64 { |
| 137 | prefixLen, bits := network.Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask..Sizescip-go gomod github.com/golang/go/src go1.16 net/IPMask#Size().() |
| 138 | return 1 << (uint64(bits) - uint64(prefixLen)) |
| 139 | } |
| 140 | |
| 141 | //VerifyNoOverlap takes a list subnets and supernet (CIDRBlock) and verifies |
| 142 | //none of the subnets overlap and all subnets are in the supernet |
| 143 | //it returns an error if any of those conditions are not satisfied |
| 144 | func VerifyNoOverlapscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/VerifyNoOverlap().(subnets []*netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#, CIDRBlock *netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#) error { |
| 145 | firstLastIP := make([][]netscip-go gomod github.com/golang/go/src go1.16 net/.IPscip-go gomod github.com/golang/go/src go1.16 net/IP#, len(subnets)) |
| 146 | for i, s := range subnets { |
| 147 | first, last := AddressRangescip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/AddressRange().(s) |
| 148 | firstLastIP[i] = []netscip-go gomod github.com/golang/go/src go1.16 net/.IPscip-go gomod github.com/golang/go/src go1.16 net/IP#{first, last} |
| 149 | } |
| 150 | for i, s := range subnets { |
| 151 | if !CIDRBlock.Containsscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Contains().(firstLastIP[i][0]) || !CIDRBlock.Containsscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Contains().(firstLastIP[i][1]) { |
| 152 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("%s does not fully contain %s", CIDRBlock.Stringscip-go gomod github.com/golang/go/src go1.16 net/IPNet#String().(), s.Stringscip-go gomod github.com/golang/go/src go1.16 net/IPNet#String().()) |
| 153 | } |
| 154 | for j := 0; j < len(subnets); j++ { |
| 155 | if i == j { |
| 156 | continue |
| 157 | } |
| 158 | |
| 159 | first := firstLastIP[j][0] |
| 160 | last := firstLastIP[j][1] |
| 161 | if s.Containsscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Contains().(first) || s.Containsscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Contains().(last) { |
| 162 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("%s overlaps with %s", subnets[j].Stringscip-go gomod github.com/golang/go/src go1.16 net/IPNet#String().(), s.Stringscip-go gomod github.com/golang/go/src go1.16 net/IPNet#String().()) |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | return nil |
| 167 | } |
| 168 | |
| 169 | // PreviousSubnet returns the subnet of the desired mask in the IP space |
| 170 | // just lower than the start of IPNet provided. If the IP space rolls over |
| 171 | // then the second return value is true |
| 172 | func PreviousSubnetscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/PreviousSubnet().(network *netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#, prefixLen int) (*netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#, bool) { |
| 173 | startIP := checkIPv4scip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/checkIPv4().(network.IPscip-go gomod github.com/golang/go/src go1.16 net/IPNet#IP.) |
| 174 | previousIP := make(netscip-go gomod github.com/golang/go/src go1.16 net/.IPscip-go gomod github.com/golang/go/src go1.16 net/IP#, len(startIP)) |
| 175 | copy(previousIP, startIP) |
| 176 | cMask := netscip-go gomod github.com/golang/go/src go1.16 net/.CIDRMaskscip-go gomod github.com/golang/go/src go1.16 net/CIDRMask().(prefixLen, 8*len(previousIP)) |
| 177 | previousIP = Decscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/Dec().(previousIP) |
| 178 | previous := &netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#{IPscip-go gomod github.com/golang/go/src go1.16 net/IPNet#IP.: previousIP.Maskscip-go gomod github.com/golang/go/src go1.16 net/IP#Mask().(cMask), Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask.: cMask} |
| 179 | if startIP.Equalscip-go gomod github.com/golang/go/src go1.16 net/IP#Equal().(netscip-go gomod github.com/golang/go/src go1.16 net/.IPv4zeroscip-go gomod github.com/golang/go/src go1.16 net/IPv4zero.) || startIP.Equalscip-go gomod github.com/golang/go/src go1.16 net/IP#Equal().(netscip-go gomod github.com/golang/go/src go1.16 net/.IPv6zeroscip-go gomod github.com/golang/go/src go1.16 net/IPv6zero.) { |
| 180 | return previous, true |
| 181 | } |
| 182 | return previous, false |
| 183 | } |
| 184 | |
| 185 | // NextSubnet returns the next available subnet of the desired mask size |
| 186 | // starting for the maximum IP of the offset subnet |
| 187 | // If the IP exceeds the maxium IP then the second return value is true |
| 188 | func NextSubnetscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/NextSubnet().(network *netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#, prefixLen int) (*netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#, bool) { |
| 189 | _, currentLast := AddressRangescip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/AddressRange().(network) |
| 190 | mask := netscip-go gomod github.com/golang/go/src go1.16 net/.CIDRMaskscip-go gomod github.com/golang/go/src go1.16 net/CIDRMask().(prefixLen, 8*len(currentLast)) |
| 191 | currentSubnet := &netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#{IPscip-go gomod github.com/golang/go/src go1.16 net/IPNet#IP.: currentLast.Maskscip-go gomod github.com/golang/go/src go1.16 net/IP#Mask().(mask), Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask.: mask} |
| 192 | _, last := AddressRangescip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/AddressRange().(currentSubnet) |
| 193 | last = Incscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/Inc().(last) |
| 194 | next := &netscip-go gomod github.com/golang/go/src go1.16 net/.IPNetscip-go gomod github.com/golang/go/src go1.16 net/IPNet#{IPscip-go gomod github.com/golang/go/src go1.16 net/IPNet#IP.: last.Maskscip-go gomod github.com/golang/go/src go1.16 net/IP#Mask().(mask), Maskscip-go gomod github.com/golang/go/src go1.16 net/IPNet#Mask.: mask} |
| 195 | if last.Equalscip-go gomod github.com/golang/go/src go1.16 net/IP#Equal().(netscip-go gomod github.com/golang/go/src go1.16 net/.IPv4zeroscip-go gomod github.com/golang/go/src go1.16 net/IPv4zero.) || last.Equalscip-go gomod github.com/golang/go/src go1.16 net/IP#Equal().(netscip-go gomod github.com/golang/go/src go1.16 net/.IPv6zeroscip-go gomod github.com/golang/go/src go1.16 net/IPv6zero.) { |
| 196 | return next, true |
| 197 | } |
| 198 | return next, false |
| 199 | } |
| 200 | |
| 201 | //Inc increases the IP by one this returns a new []byte for the IP |
| 202 | func Incscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/Inc().(IP netscip-go gomod github.com/golang/go/src go1.16 net/.IPscip-go gomod github.com/golang/go/src go1.16 net/IP#) netscip-go gomod github.com/golang/go/src go1.16 net/.IPscip-go gomod github.com/golang/go/src go1.16 net/IP# { |
| 203 | IP = checkIPv4scip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/checkIPv4().(IP) |
| 204 | incIP := make([]byte, len(IP)) |
| 205 | copy(incIP, IP) |
| 206 | for j := len(incIP) - 1; j >= 0; j-- { |
| 207 | incIP[j]++ |
| 208 | if incIP[j] > 0 { |
| 209 | break |
| 210 | } |
| 211 | } |
| 212 | return incIP |
| 213 | } |
| 214 | |
| 215 | //Dec decreases the IP by one this returns a new []byte for the IP |
| 216 | func Decscip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/Dec().(IP netscip-go gomod github.com/golang/go/src go1.16 net/.IPscip-go gomod github.com/golang/go/src go1.16 net/IP#) netscip-go gomod github.com/golang/go/src go1.16 net/.IPscip-go gomod github.com/golang/go/src go1.16 net/IP# { |
| 217 | IP = checkIPv4scip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/checkIPv4().(IP) |
| 218 | decIP := make([]byte, len(IP)) |
| 219 | copy(decIP, IP) |
| 220 | decIP = checkIPv4scip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/checkIPv4().(decIP) |
| 221 | for j := len(decIP) - 1; j >= 0; j-- { |
| 222 | decIP[j]-- |
| 223 | if decIP[j] < 255 { |
| 224 | break |
| 225 | } |
| 226 | } |
| 227 | return decIP |
| 228 | } |
| 229 | |
| 230 | func checkIPv4scip-go gomod github.com/apparentlymart/go-cidr . `github.com/apparentlymart/go-cidr/cidr`/checkIPv4().(ip netscip-go gomod github.com/golang/go/src go1.16 net/.IPscip-go gomod github.com/golang/go/src go1.16 net/IP#) netscip-go gomod github.com/golang/go/src go1.16 net/.IPscip-go gomod github.com/golang/go/src go1.16 net/IP# { |
| 231 | // Go for some reason allocs IPv6len for IPv4 so we have to correct it |
| 232 | if v4 := ip.To4scip-go gomod github.com/golang/go/src go1.16 net/IP#To4().(); v4 != nil { |
| 233 | return v4 |
| 234 | } |
| 235 | return ip |
| 236 | } |