refactor: put all network detection code in one place

This commit is contained in:
Matthias Radestock
2017-06-20 09:23:52 +01:00
parent 19a6551de2
commit 4e0065a57d
2 changed files with 16 additions and 14 deletions

View File

@@ -2,7 +2,6 @@ package host
import (
"fmt"
"net"
"runtime"
"sync"
"time"
@@ -86,19 +85,7 @@ func NewReporter(hostID, hostName, probeID, version string, pipes controls.PipeC
func (*Reporter) Name() string { return "Host" }
// GetLocalNetworks is exported for mocking
var GetLocalNetworks = func() ([]*net.IPNet, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
localNets := report.Networks{}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && ipnet.IP.To4() != nil {
localNets = append(localNets, ipnet)
}
}
return localNets, nil
}
var GetLocalNetworks = report.GetLocalNetworks
// Report implements Reporter.
func (r *Reporter) Report() (report.Report, error) {

View File

@@ -82,3 +82,18 @@ func AddLocalBridge(name string) error {
return nil
}
// GetLocalNetworks returns all the local networks.
func GetLocalNetworks() ([]*net.IPNet, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
localNets := Networks{}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && ipnet.IP.To4() != nil {
localNets = append(localNets, ipnet)
}
}
return localNets, nil
}