forked from icedream/go-stagelinq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
42 lines (35 loc) · 872 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package stagelinq
import (
"net"
)
func getPort(address net.Addr) int {
switch convertedAddress := address.(type) {
case *net.UDPAddr:
return convertedAddress.Port
case *net.TCPAddr:
return convertedAddress.Port
default:
panic("unsupported network address type")
}
}
func makeBroadcastIP(ip net.IP, mask net.IPMask) (bip net.IP) {
// get 4-byte representation of ipv4 is possible, nil if not an ipv4 address
convertedIPv4 := false
if ip4 := ip.To4(); ip4 != nil {
convertedIPv4 = len(ip) != len(ip4)
ip = ip4
}
if len(mask) != len(ip) {
// mask and ip are different sizes, panic!
panic("net mask and ip address are different sizes")
}
bip = make(net.IP, len(ip))
for i := range mask {
bip[i] = ip[i] | ^mask[i]
}
// convert back to 16-byte representation if input was 16-byte, too
if convertedIPv4 {
bip = bip.To16()
}
return
}