github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/tests/testutils/iputils.go (about)

     1  // Copyright 2015 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testutils
    16  
    17  import (
    18  	"bufio"
    19  	"fmt"
    20  	"net"
    21  	"os"
    22  	"regexp"
    23  	"strconv"
    24  	"strings"
    25  
    26  	"github.com/coreos/rkt/Godeps/_workspace/src/github.com/vishvananda/netlink"
    27  )
    28  
    29  func getDefaultGW(family int) (string, error) {
    30  	l, err := netlink.LinkByName("lo")
    31  	if err != nil {
    32  		return "", err
    33  	}
    34  
    35  	routes, err := netlink.RouteList(l, family)
    36  	if err != nil {
    37  		return "", err
    38  	}
    39  
    40  	return routes[0].Gw.String(), nil
    41  }
    42  func GetDefaultGWv4() (string, error) {
    43  	return getDefaultGW(netlink.FAMILY_V4)
    44  }
    45  
    46  func GetDefaultGWv6() (string, error) {
    47  	return getDefaultGW(netlink.FAMILY_V6)
    48  }
    49  
    50  func GetIPs(ifaceWanted string, familyWanted int) ([]string, error) {
    51  	ips := make([]string, 0)
    52  	ifaces, err := net.Interfaces()
    53  	if err != nil {
    54  		return ips, err
    55  	}
    56  	for _, iface := range ifaces {
    57  		if iface.Name != ifaceWanted {
    58  			continue
    59  		}
    60  
    61  		addrs, _ := iface.Addrs()
    62  		for _, addr := range addrs {
    63  			addrString := addr.String()
    64  			ip, _, err := net.ParseCIDR(addrString)
    65  			if err != nil {
    66  				return ips, err
    67  			}
    68  
    69  			if strings.Contains(addrString, ".") && familyWanted == netlink.FAMILY_V4 ||
    70  				strings.Contains(addrString, ":") && familyWanted == netlink.FAMILY_V6 {
    71  				ips = append(ips, ip.String())
    72  			}
    73  		}
    74  	}
    75  	return ips, err
    76  }
    77  
    78  func GetIPsv4(iface string) ([]string, error) {
    79  	return GetIPs(iface, netlink.FAMILY_V4)
    80  }
    81  func GetIPsv6(iface string) ([]string, error) {
    82  	return GetIPs(iface, netlink.FAMILY_V6)
    83  }
    84  
    85  func GetGW(iface string, family int) (string, error) {
    86  	return "", fmt.Errorf("Not implemented")
    87  }
    88  func GetGWv4(iface string) (string, error) {
    89  	return GetGW(iface, netlink.FAMILY_V4)
    90  }
    91  
    92  func GetGWv6(iface string) (string, error) {
    93  	return GetGW(iface, netlink.FAMILY_V4)
    94  }
    95  
    96  func GetNonLoIfaceWithAddrs(ipFamily int) (iface net.Interface, addrs []string, err error) {
    97  	ifaces, err := net.Interfaces()
    98  	if err != nil {
    99  		return iface, nil, err
   100  	}
   101  
   102  	for _, i := range ifaces {
   103  		if i.Flags&net.FlagLoopback == 0 {
   104  			addrs, err = GetIPs(i.Name, ipFamily)
   105  			if err != nil {
   106  				return iface, addrs, fmt.Errorf("Cannot get IP address for interface %v: %v", i.Name, err)
   107  			}
   108  
   109  			if len(addrs) == 0 {
   110  				continue
   111  			}
   112  			iface = i
   113  
   114  			ifaceNameLower := strings.ToLower(i.Name)
   115  			// Don't use rkt's interfaces
   116  			if strings.Contains(ifaceNameLower, "cni") ||
   117  				strings.Contains(ifaceNameLower, "veth") {
   118  				continue
   119  			}
   120  			break
   121  		}
   122  	}
   123  	return iface, addrs, err
   124  }
   125  
   126  func GetNonLoIfaceIPv4() (string, error) {
   127  	iface, ifaceIPsv4, err := GetNonLoIfaceWithAddrs(netlink.FAMILY_V4)
   128  	if err != nil {
   129  		return "", fmt.Errorf("Error while getting non-lo host interface: %v\n", err)
   130  	}
   131  
   132  	if iface.Name == "" || ifaceIPsv4 == nil {
   133  		return "", nil
   134  	}
   135  
   136  	return ifaceIPsv4[0], nil
   137  }
   138  
   139  func CheckTcp4Port(port int) (bool, error) {
   140  	tcpFile, err := os.Open("/proc/net/tcp")
   141  	if err != nil {
   142  		return false, err
   143  	}
   144  	defer tcpFile.Close()
   145  
   146  	re := regexp.MustCompile(`:([A-Z0-9]+) `)
   147  	scanner := bufio.NewScanner(tcpFile)
   148  	for scanner.Scan() {
   149  		line := scanner.Text()
   150  		result := re.FindAllStringSubmatch(line, -1)
   151  		if result != nil {
   152  			i, err := strconv.ParseInt(result[0][1], 16, 32)
   153  			if err != nil {
   154  				return false, err
   155  			}
   156  			if int(i) == port {
   157  				return false, nil
   158  			}
   159  		}
   160  	}
   161  	return true, nil
   162  }
   163  
   164  func GetNextFreePort4() (int, error) {
   165  	for port := 49152; port <= 65535; port++ {
   166  		avail, err := CheckTcp4Port(port)
   167  		if err != nil {
   168  			return 0, err
   169  		}
   170  		if avail {
   171  			return port, nil
   172  		}
   173  	}
   174  	return 0, fmt.Errorf("No available ports")
   175  }
   176  
   177  func GetIfaceCount() (int, error) {
   178  	ifaces, err := net.Interfaces()
   179  	if err != nil {
   180  		return 0, err
   181  	}
   182  	return len(ifaces), nil
   183  }