veyron/lib/netstat.go: Added SameMachine
Add a SameMachine call on a endpoint that can determine if an address
originates on the same machine (node) as the invoker.
Change-Id: I392258fb4300edb37063b399f6af826a8d957081
diff --git a/lib/netstate/netstate.go b/lib/netstate/netstate.go
index cd9acfb..68ee679 100644
--- a/lib/netstate/netstate.go
+++ b/lib/netstate/netstate.go
@@ -369,3 +369,30 @@
func FindRemoved(a, b AddrList) AddrList {
return diffAB(a, b)
}
+
+// SameMachine returns true if the provided addr is on the node
+// executing this function.
+func SameMachine(addr net.Addr) (bool, error) {
+ // The available interfaces may change between calls.
+ addrs, err := GetAll()
+ if err != nil {
+ return false, err
+ }
+
+ ips := make(map[string]struct{})
+ for _, a := range addrs {
+ ip, _, err := net.ParseCIDR(a.Address().String())
+ if err != nil {
+ return false, err
+ }
+ ips[ip.String()] = struct{}{}
+ }
+
+ client, _, err := net.SplitHostPort(addr.String())
+ if err != nil {
+ return false, err
+ }
+
+ _, islocal := ips[client]
+ return islocal, nil
+}