diff --git a/pkg/netutil/net.go b/pkg/netutil/net.go new file mode 100644 index 000000000..1ffe2b76c --- /dev/null +++ b/pkg/netutil/net.go @@ -0,0 +1,55 @@ +/* +http://www.apache.org/licenses/LICENSE-2.0.txt + + +Copyright 2015 Intel Corporation + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package netutil + +import "net" + +// GetIP returns the first non-loopback ipv4 interface. The loopback interface +// is returned if no other interface is present. +func GetIP() string { + ifaces, err := net.Interfaces() + if err != nil { + return "127.0.0.1" + } + for _, i := range ifaces { + addrs, err := i.Addrs() + if err != nil { + return "127.0.0.1" + } + for _, addr := range addrs { + var ip net.IP + switch v := addr.(type) { + case *net.IPAddr: + ip = v.IP + case *net.IPNet: + ip = v.IP + } + if ip == nil || ip.IsLoopback() { + continue + } + ip = ip.To4() + if ip == nil { + continue // not an ipv4 address + } + return ip.String() + } + } + return "127.0.0.1" +} diff --git a/pkg/netutil/net_test.go b/pkg/netutil/net_test.go new file mode 100644 index 000000000..014193cf3 --- /dev/null +++ b/pkg/netutil/net_test.go @@ -0,0 +1,40 @@ +/* +http://www.apache.org/licenses/LICENSE-2.0.txt + + +Copyright 2015 Intel Corporation + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package netutil + +import ( + "net" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestNetUtil(t *testing.T) { + Convey("Get the first non-loopback ipv4 interface", t, func() { + ip := GetIP() + ifaces, err := net.Interfaces() + So(err, ShouldBeNil) + if len(ifaces) > 1 { + So(ip, ShouldNotResemble, "127.0.0.1") + } else { + So(ip, ShouldResemble, "127.0.0.1") + } + }) +} diff --git a/pkg/rpcutil/rpc.go b/pkg/rpcutil/rpc.go new file mode 100644 index 000000000..75894f36c --- /dev/null +++ b/pkg/rpcutil/rpc.go @@ -0,0 +1,41 @@ +/* +http://www.apache.org/licenses/LICENSE-2.0.txt + + +Copyright 2015 Intel Corporation + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package rpcutil + +import ( + "fmt" + "time" + + "google.golang.org/grpc" +) + +// GetClientConnection returns a grcp.ClientConn that is unsecured +// TODO: Add TLS security to connection +func GetClientConnection(addr string, port int) (*grpc.ClientConn, error) { + grpcDialOpts := []grpc.DialOption{ + grpc.WithTimeout(2 * time.Second), + } + grpcDialOpts = append(grpcDialOpts, grpc.WithInsecure()) + conn, err := grpc.Dial(fmt.Sprintf("%v:%v", addr, port), grpcDialOpts...) + if err != nil { + return nil, err + } + return conn, nil +} diff --git a/pkg/rpcutil/rpc_test.go b/pkg/rpcutil/rpc_test.go new file mode 100644 index 000000000..9f244fd07 --- /dev/null +++ b/pkg/rpcutil/rpc_test.go @@ -0,0 +1,36 @@ +/* +http://www.apache.org/licenses/LICENSE-2.0.txt + + +Copyright 2015 Intel Corporation + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package rpcutil + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestRpcUtil(t *testing.T) { + Convey("Get a client connection", t, func() { + conn, err := GetClientConnection("127.0.0.1", 8183) + Convey("Provided a valid address, port", func() { + So(err, ShouldBeNil) + So(conn, ShouldNotBeNil) + }) + }) +}