-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
39 lines (33 loc) · 829 Bytes
/
utils.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
package portscanner
import (
"fmt"
"net"
"github.com/ziutek/utils/netaddr"
)
// ExpandCIDR - Util function for expanding a CIDR into a list of IPs
func ExpandCIDR(cidr string) ([]string, error) {
ipFirst, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}
var ips []string
for ip := ipFirst.Mask(ipnet.Mask); ipnet.Contains(ip); ip = netaddr.IPAdd(ip, 1) {
ips = append(ips, ip.String())
}
return ips, nil
}
// utitlity function for getting a non-loopback IP
func myIPNet() (*net.IPNet, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet, nil
}
}
}
return nil, fmt.Errorf("Cannot find a non-loopback interface")
}