-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fingerprint: config parameter for interface and native method for IP address #189
Changes from 5 commits
7fc3f83
a70493f
776d42c
f639ad1
2a08cba
256d390
e3bcf44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
package fingerprint | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
|
@@ -38,10 +39,14 @@ func (f *NetworkFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) | |
if "darwin" == runtime.GOOS { | ||
defaultDevice = "en0" | ||
} | ||
// User-defined override for the default interface | ||
if cfg.Iface != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put a comment above to the tune of: // Allow the user to override the default network device. |
||
defaultDevice = cfg.Iface | ||
} | ||
|
||
newNetwork.Device = defaultDevice | ||
|
||
if ip := f.ifConfig(defaultDevice); ip != "" { | ||
if ip := f.ipAddress(defaultDevice); ip != "" { | ||
node.Attributes["network.ip-address"] = ip | ||
newNetwork.IP = ip | ||
newNetwork.CIDR = newNetwork.IP + "/32" | ||
|
@@ -129,6 +134,56 @@ func (f *NetworkFingerprint) linkSpeedEthtool(path, device string) int { | |
return mbs | ||
} | ||
|
||
// ipAddress returns the first IPv4 address on the configured default interface | ||
// Tries Golang native functions and falls back onto ifconfig | ||
func (f *NetworkFingerprint) ipAddress(device string) string { | ||
if ip, err := f.nativeIpAddress(device); err == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't that be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, no, that function should return the IP if there was no error, so if If err is different than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, I see. Thanks. |
||
return ip | ||
} | ||
|
||
return f.ifConfig(device) | ||
} | ||
|
||
func (f *NetworkFingerprint) nativeIpAddress(device string) (string, error) { | ||
// Find IP address on configured interface | ||
var ip string | ||
ifaces, err := net.Interfaces() | ||
if err != nil { | ||
return "", errors.New("could not retrieve interface list") | ||
} | ||
|
||
// TODO: should we handle IPv6 here? How do we determine precedence? | ||
for _, i := range ifaces { | ||
if i.Name != device { | ||
continue | ||
} | ||
|
||
addrs, err := i.Addrs() | ||
if err != nil { | ||
return "", errors.New("could not retrieve interface IP addresses") | ||
} | ||
|
||
for _, a := range addrs { | ||
switch v := a.(type) { | ||
case *net.IPNet: | ||
if v.IP.To4() != nil { | ||
ip = v.IP.String() | ||
} | ||
case *net.IPAddr: | ||
if v.IP.To4() != nil { | ||
ip = v.IP.String() | ||
} | ||
} | ||
} | ||
} | ||
|
||
if net.ParseIP(ip) == nil { | ||
return "", errors.New(fmt.Sprintf("could not parse IP address `%s`", ip)) | ||
} | ||
|
||
return ip, nil | ||
} | ||
|
||
// ifConfig returns the IP Address for this node according to ifConfig, for the | ||
// specified device. | ||
func (f *NetworkFingerprint) ifConfig(device string) string { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets change Iface to "NetworkInterface" for long term clarity both in code and docs. After that lets merge! Thanks