Skip to content

Commit 133ca0f

Browse files
authored
Merge pull request #12 from lestrrat-go/fix-broadcast-address
Fix broadcast address calculation
2 parents 3cae5bb + 40d1391 commit 133ca0f

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

discover.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/binary"
45
"fmt"
56
"log"
67
"net"
@@ -95,8 +96,15 @@ func getBroadcastAddresses() ([]string, error) {
9596
continue
9697
}
9798
for _, addr := range addrs {
98-
if n, ok := addr.(*net.IPNet); ok && !n.IP.IsLoopback() && n.IP.To4() != nil {
99-
baddrs = append(baddrs, n.IP.Mask(n.IP.DefaultMask()).String())
99+
if n, ok := addr.(*net.IPNet); ok && !n.IP.IsLoopback() {
100+
if v4addr := n.IP.To4(); v4addr != nil {
101+
// convert all parts of the masked bits to its maximum value
102+
// by converting the address into a 32 bit integer and then
103+
// ORing it with the inverted mask
104+
baddr := make(net.IP, len(v4addr))
105+
binary.BigEndian.PutUint32(baddr, binary.BigEndian.Uint32(v4addr)|^binary.BigEndian.Uint32(n.IP.DefaultMask()))
106+
baddrs = append(baddrs, baddr.String())
107+
}
100108
}
101109
}
102110
}

0 commit comments

Comments
 (0)