Skip to content

Commit

Permalink
add finalAddress() method to Net6
Browse files Browse the repository at this point in the history
  • Loading branch information
c-robinson committed Jan 6, 2024
1 parent 9c00c6f commit 47b95ce
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions net6.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,7 @@ func (n Net6) FirstAddress() net.IP {

// LastAddress returns the last usable address for the represented network
func (n Net6) LastAddress() net.IP {
xip := make([]byte, len(n.IPNet.IP))
wc := n.wildcard()
for pos := range n.IP() {
xip[pos] = n.IP()[pos] + (wc[pos] - n.Hostmask[pos])
}
xip, _ := n.finalAddress()
return xip
}

Expand Down Expand Up @@ -340,6 +336,20 @@ func (n Net6) contained(ip net.IP) bool {
return true
}

// finalAddress is here mostly because it also exists in Net4, but there
// are cases where it is valuable to have a method for both Net
// implementations that returns the last address in the netblock
func (n Net6) finalAddress() (net.IP, int) {
xip := make([]byte, len(n.IPNet.IP))
ones, _ := n.Mask().Size()

wc := n.wildcard()
for pos := range n.IP() {
xip[pos] = n.IP()[pos] + (wc[pos] - n.Hostmask[pos])
}
return xip, ones
}

func (n Net6) wildcard() net.IPMask {
wc := make([]byte, len(n.Mask()))
for i, b := range n.Mask() {
Expand Down

0 comments on commit 47b95ce

Please sign in to comment.