-
Notifications
You must be signed in to change notification settings - Fork 1
/
scan.go
51 lines (42 loc) · 787 Bytes
/
scan.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
43
44
45
46
47
48
49
50
51
package netutils
import (
"fmt"
"log"
"net"
)
func scan(iface *net.Interface) error {
var (
addr *net.IPNet
addrs []net.Addr
err error
)
if addrs, err = iface.Addrs(); err != nil {
return err
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok {
if ip4 := ipnet.IP.To4(); ip4 != nil {
addr = &net.IPNet{
IP: ip4,
Mask: ipnet.Mask[len(ipnet.Mask)-4:],
}
break
}
}
}
if addr == nil {
return fmt.Errorf("there's no IP network found")
}
if addr.IP[0] == 127 {
return fmt.Errorf("skipping localhost")
}
if addr.Mask[0] != 0xff || addr.Mask[1] != 0xff {
return fmt.Errorf("mask means network is too large")
}
log.Printf(
"Using network range %+v for interface %v",
addr,
iface.Name,
)
return nil
}