<commit_before>// Copyright 2012 Google Inc. All Rights Reserved.
//
// 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.

// Author: <EMAIL> (<NAME>)

package ncc

// This file contains the IP and interface configuration related functions
// for the Seesaw Network Control component. At a later date this probably
// should be rewritten to use netlink directly.

import (
	"fmt"
	"net"
	"os/exec"
	"regexp"
	"strings"

	"github.com/google/seesaw/common/seesaw"

	log "github.com/golang/glog"
)

var (
	ipCmd = "/sbin/ip"

	ifaceNameRegexp        = regexp.MustCompile(`^\w+\d+(\.\d+)?$`)
	ipv6AddrRegexp         = regexp.MustCompile(`^\s*inet6 ([a-f0-9:/]+) .*$`)
	routeDefaultIPv4Regexp = regexp.MustCompile(
		`^default via (\d+\.\d+\.\d+\.\d+) dev ([a-z]+[0-9]+) `)
)

// validateInterface validates the name of a network interface.
func validateInterface(iface *net.Interface) error {
	if !ifaceNameRegexp.MatchString(iface.Name) {
		return fmt.Errorf("Invalid interface name %q", iface.Name)
	}
	return nil
}

// ipRunOutput runs the Linux ip(1) command with the specified arguments and
// returns the output.
func ipRunOutput(cmd string, args ...interface{}) (string, error) {
	cmdStr := fmt.Sprintf(cmd, args...)
	ipArgs := strings.Split(cmdStr, " ")
	log.Infof("%s %s", ipCmd, cmdStr)
	ip := exec.Command(ipCmd, ipArgs...)
	out, err := ip.Output()
	if err != nil {
		return "", fmt.Errorf("IP run %q: %v", cmdStr, err)
	}
	return string(out), nil
}

// ipRun runs the Linux ip(1) command with the specified arguments.
func ipRun(cmd string, args ...interface{}) error {
	_, err := ipRunOutput(cmd, args...)
	return err
}

// ipRunAFOutput runs the Linux ip(1) command with the given address family
// and specified arguments and returns the output.
func ipRunAFOutput(family seesaw.AF, cmd string, args ...interface{}) (string, error) {
	switch family {
	case seesaw.IPv4:
		cmd = fmt.Sprintf("-f inet %s", cmd)
	case seesaw.IPv6:
		cmd = fmt.Sprintf("-f inet6 %s", cmd)
	default:
		return "", fmt.Errorf("Unknown address family %s", family)
	}
	return ipRunOutput(cmd, args...)
}

// ipRunAF runs the Linux ip(1) command with the given address family and
// specified arguments.
func ipRunAF(family seesaw.AF, cmd string, args ...interface{}) error {
	_, err := ipRunAFOutput(family, cmd, args...)
	return err
}

// ipRunIface runs the Linux ip(1) command with the specified arguments, after
// validating the given network interface name. This function should be called
// if the interface name is included in the arguments.
func ipRunIface(iface *net.Interface, cmd string, args ...interface{}) error {
	if err := validateInterface(iface); err != nil {
		return err
	}
	_, err := ipRunOutput(cmd, args...)
	return err
}

// ifaceDown sets the interface link state to down and preserves IPv6 addresses
// for both the given interface and any associated VLAN interfaces.
func ifaceDown(pIface *net.Interface) error {
	out, err := ipRunOutput("link show dev %s", pIface.Name)
	if !strings.Contains(out, "state UP") {
		return nil
	}

	// Unlike IPv4, the kernel removes IPv6 addresses when the link goes down. We
	// don't want that behavior, so we have to preserve the addresses manually.
	// TODO(angusc): See if we can get a sysctl or something added to the kernel
	// so we can avoid doing this.
	ipv6Addrs := make(map[string]*net.Interface)
	ifaces, err := vlanInterfaces(pIface)
	if err != nil {
		return err
	}
	ifaces = append(ifaces, pIface)
	for _, iface := range ifaces {
		out, err = ipRunOutput("-6 addr show dev %s scope global", iface.Name)
		for _, line := range strings.Split(out, "\n") {
			if match := ipv6AddrRegexp.FindStringSubmatch(line); match != nil {
				ipv6Addrs[match[1]] = iface
			}
		}
	}
	if err := ifaceFastDown(pIface); err != nil {
		return err<commit_msg>ncc/ip.go: don't ignore errors<commit_after>// Copyright 2012 Google Inc. All Rights Reserved.
//
// 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.

// Author: <EMAIL> (<NAME>)

package ncc

// This file contains the IP and interface configuration related functions
// for the Seesaw Network Control component. At a later date this probably
// should be rewritten to use netlink directly.

import (
	"fmt"
	"net"
	"os/exec"
	"regexp"
	"strings"

	"github.com/google/seesaw/common/seesaw"

	log "github.com/golang/glog"
)

var (
	ipCmd = "/sbin/ip"

	ifaceNameRegexp        = regexp.MustCompile(`^\w+\d+(\.\d+)?$`)
	ipv6AddrRegexp         = regexp.MustCompile(`^\s*inet6 ([a-f0-9:/]+) .*$`)
	routeDefaultIPv4Regexp = regexp.MustCompile(
		`^default via (\d+\.\d+\.\d+\.\d+) dev ([a-z]+[0-9]+) `)
)

// validateInterface validates the name of a network interface.
func validateInterface(iface *net.Interface) error {
	if !ifaceNameRegexp.MatchString(iface.Name) {
		return fmt.Errorf("Invalid interface name %q", iface.Name)
	}
	return nil
}

// ipRunOutput runs the Linux ip(1) command with the specified arguments and
// returns the output.
func ipRunOutput(cmd string, args ...interface{}) (string, error) {
	cmdStr := fmt.Sprintf(cmd, args...)
	ipArgs := strings.Split(cmdStr, " ")
	log.Infof("%s %s", ipCmd, cmdStr)
	ip := exec.Command(ipCmd, ipArgs...)
	out, err := ip.Output()
	if err != nil {
		return "", fmt.Errorf("IP run %q: %v", cmdStr, err)
	}
	return string(out), nil
}

// ipRun runs the Linux ip(1) command with the specified arguments.
func ipRun(cmd string, args ...interface{}) error {
	_, err := ipRunOutput(cmd, args...)
	return err
}

// ipRunAFOutput runs the Linux ip(1) command with the given address family
// and specified arguments and returns the output.
func ipRunAFOutput(family seesaw.AF, cmd string, args ...interface{}) (string, error) {
	switch family {
	case seesaw.IPv4:
		cmd = fmt.Sprintf("-f inet %s", cmd)
	case seesaw.IPv6:
		cmd = fmt.Sprintf("-f inet6 %s", cmd)
	default:
		return "", fmt.Errorf("Unknown address family %s", family)
	}
	return ipRunOutput(cmd, args...)
}

// ipRunAF runs the Linux ip(1) command with the given address family and
// specified arguments.
func ipRunAF(family seesaw.AF, cmd string, args ...interface{}) error {
	_, err := ipRunAFOutput(family, cmd, args...)
	return err
}

// ipRunIface runs the Linux ip(1) command with the specified arguments, after
// validating the given network interface name. This function should be called
// if the interface name is included in the arguments.
func ipRunIface(iface *net.Interface, cmd string, args ...interface{}) error {
	if err := validateInterface(iface); err != nil {
		return err
	}
	_, err := ipRunOutput(cmd, args...)
	return err
}

// ifaceDown sets the interface link state to down and preserves IPv6 addresses
// for both the given interface and any associated VLAN interfaces.
func ifaceDown(pIface *net.Interface) error {
	out, err := ipRunOutput("link show dev %s", pIface.Name)
	if err != nil {
		return err	
	}
	if !strings.Contains(out, "state UP") {
		return nil
	}

	// Unlike IPv4, the kernel removes IPv6 addresses when the link goes down. We
	// don't want that behavior, so we have to preserve the addresses manually.
	// TODO(angusc): See if we can get a sysctl or something added to the kernel
	// so we can avoid doing this.
	ipv6Addrs := make(map[string]*net.Interface)
	ifaces, err := vlanInterfaces(pIface)
	if err != nil {
		return err
	}
	ifaces = append(ifaces, pIface)
	for _, iface := range ifaces {
		out, err := ipRunOutput("-6 addr show dev %s scope global", iface.Name)
		if err != nil {
			return err	
		}
		for _, line := range strings.Split(out, "\n") {
			if match := ipv6AddrRegexp.FindStringSubmatch(line); match != nil {
				ipv6Addrs[match[1]] = iface
			}
		}
	}
	if err := ifaceFastDown(pIface); err != nil {
		return err
{'fraction_non_alphanumeric': 0.1296445415851835, 'fraction_numerical': 0.007132175313470608, 'mean_word_length': 3.6269292176689727, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4251955', 'n_tokens_mistral': 2829, 'n_tokens_neox': 2752, 'n_words': 1008}