<commit_before>package macvlan

import (
	"encoding/json"
	"fmt"
	"io"
	"net"
	"net/http"
	"strings"

	log "github.com/Sirupsen/logrus"
	"github.com/codegangsta/cli"
	"github.com/docker/libnetwork/ipallocator"
	"github.com/gorilla/mux"
	"github.com/samalba/dockerclient"
	"github.com/vishvananda/netlink"
)

const (
	MethodReceiver       = "NetworkDriver"
	bridgeMode           = "bridge"
	defaultRoute         = "0.0.0.0/0"
	containerIfacePrefix = "eth"
	defaultMTU           = 1500
	minMTU               = 68
)

type Driver interface {
	Listen(string) error
}

// Struct for binding bridge options CLI flags
type bridgeOpts struct {
	brName   string
	brSubnet net.IPNet
	brIP     net.IPNet
}

type driver struct {
	dockerer
	pluginConfig
	ipAllocator *ipallocator.IPAllocator
	version     string
	network     string
	cidr        *net.IPNet
	nameserver  string
}

// Struct for binding plugin specific configurations (cli.go for details).
type pluginConfig struct {
	mtu             int
	mode            string
	hostIface       string
	containerSubnet *net.IPNet
	gatewayIP       net.IP
}

func New(version string, ctx *cli.Context) (Driver, error) {
	docker, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
	if err != nil {
		return nil, fmt.Errorf("could not connect to docker: %s", err)
	}
	if ctx.String("host-interface") == "" {
		log.Fatalf("Required flag [ host-interface ] that is used for off box communication was not defined. Example: --host-interface=eth1")
	}

	// bind CLI opts to the user config struct
	if ok := validateHostIface(ctx.String("host-interface")); !ok {
		log.Fatalf("Requird field [ host-interface ] ethernet interface [ %s ] was not found. Exiting since this is required for both l2 and l3 modes.", ctx.String("host-interface"))
	}
	macvlanEthIface = ctx.String("host-interface")

	// lower bound of v4 MTU is 68-bytes per rfc791
	if ctx.Int("mtu") <= 0 {
		cliMTU = cliMTU
	} else if ctx.Int("mtu") >= minMTU {
		cliMTU = ctx.Int("mtu")
	} else {
		log.Fatalf("The MTU value passed [ %d ] must be greater then [ %d ] bytes per rfc791", ctx.Int("mtu"), minMTU)
	}

	// Parse the container IP subnet and network addr to be used to guess the gateway if necessary
	containerGW, containerNet, err := net.ParseCIDR(ctx.String("macvlan-subnet"))
	if err != nil {
		log.Fatalf("Error parsing cidr from the subnet flag provided [ %s ]: %s", ctx.String("macvlan-subnet"), err)
	}

	// Set the default mode to bridge
	if ctx.String("mode") == "" {
		macvlanMode = bridgeMode
	}
	switch ctx.String("mode") {
	case bridgeMode:
		macvlanMode = bridgeMode
		// todo: in other modes if relevant
	default:
		log.Fatalf("Invalid macvlan mode supplied [ %s ] we currently only support [ %s ] mode. If mode is left empty, bridge is the default mode.", ctx.String("mode"), macvlanMode)
	}

	// if no gateway was passed, use the first valid addr on the container subnet
	if ctx.String("gateway") != "" {
		// bind the container gateway to the IP passed from the CLI
		cliGateway := net.ParseIP(ctx.String("gateway"))
		if err != nil {
			log.Fatalf("The IP passed with the [ gateway ] flag [ %s ] was not a valid address: %s", ctx.String("gateway"), err)
		}
		containerGW = cliGateway
	} else {
		containerGW = ipIncrement(containerGW)
	}

	pluginOpts := &pluginConfig{
		mtu:             cliMTU,
		mode:            macvlanMode,
		containerSubnet: containerNet,
		gatewayIP:       containerGW,
		hostIface:       macvlanEthIface,
	}
	// Leaving as info for now to stdout the plugin config
	log.Infof("Plugin configuration options are: \n %s", pluginOpts)

	ipAllocator := ipallocator.New()<commit_msg>fix the parameters not invalid bug<commit_after>package macvlan

import (
	"encoding/json"
	"fmt"
	"io"
	"net"
	"net/http"
	"strings"

	log "github.com/Sirupsen/logrus"
	"github.com/codegangsta/cli"
	"github.com/docker/libnetwork/ipallocator"
	"github.com/gorilla/mux"
	"github.com/samalba/dockerclient"
	"github.com/vishvananda/netlink"
)

const (
	MethodReceiver       = "NetworkDriver"
	bridgeMode           = "bridge"
	defaultRoute         = "0.0.0.0/0"
	containerIfacePrefix = "eth"
	defaultMTU           = 1500
	minMTU               = 68
)

type Driver interface {
	Listen(string) error
}

// Struct for binding bridge options CLI flags
type bridgeOpts struct {
	brName   string
	brSubnet net.IPNet
	brIP     net.IPNet
}

type driver struct {
	dockerer
	pluginConfig
	ipAllocator *ipallocator.IPAllocator
	version     string
	network     string
	cidr        *net.IPNet
	nameserver  string
}

// Struct for binding plugin specific configurations (cli.go for details).
type pluginConfig struct {
	mtu             int
	mode            string
	hostIface       string
	containerSubnet *net.IPNet
	gatewayIP       net.IP
}

func New(version string, ctx *cli.Context) (Driver, error) {
	docker, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
	if err != nil {
		return nil, fmt.Errorf("could not connect to docker: %s", err)
	}
	if ctx.String("host-interface") == "" {
		log.Fatalf("Required flag [ host-interface ] that is used for off box communication was not defined. Example: --host-interface=eth1")
	}

	// bind CLI opts to the user config struct
	if ok := validateHostIface(ctx.String("host-interface")); !ok {
		log.Fatalf("Requird field [ host-interface ] ethernet interface [ %s ] was not found. Exiting since this is required for both l2 and l3 modes.", ctx.String("host-interface"))
	}
	macvlanEthIface = ctx.String("host-interface")

	// lower bound of v4 MTU is 68-bytes per rfc791
	if ctx.Int("mtu") <= 0 {
		cliMTU = cliMTU
	} else if ctx.Int("mtu") >= minMTU {
		cliMTU = ctx.Int("mtu")
	} else {
		log.Fatalf("The MTU value passed [ %d ] must be greater than [ %d ] bytes per rfc791", ctx.Int("mtu"), minMTU)
	}

	// Parse the container IP subnet and network addr to be used to guess the gateway if necessary
	defaultSubnet = ctx.String("macvlan-subnet")
	containerGW, containerNet, err := net.ParseCIDR(ctx.String("macvlan-subnet"))
	if err != nil {
		log.Fatalf("Error parsing cidr from the subnet flag provided [ %s ]: %s", ctx.String("macvlan-subnet"), err)
	}

	// Set the default mode to bridge
	if ctx.String("mode") == "" {
		macvlanMode = bridgeMode
	}
	switch ctx.String("mode") {
	case bridgeMode:
		macvlanMode = bridgeMode
		// todo: in other modes if relevant
	default:
		log.Fatalf("Invalid macvlan mode supplied [ %s ] we currently only support [ %s ] mode. If mode is left empty, bridge is the default mode.", ctx.String("mode"), macvlanMode)
	}

	// if no gateway was passed, use the first valid addr on the container subnet
	if ctx.String("gateway") != "" {
		// bind the container gateway to the IP passed from the CLI
		cliGateway := net.ParseIP(ctx.String("gateway"))
		if err != nil {
			log.Fatalf("The IP passed with the [ gateway ] flag [ %s ] was not a valid address: %s", ctx.String("gateway"), err)
		}
		containerGW = cliGateway
	} else {
		containerGW = ipIncrement(containerGW)
	}

	gatewayIP = containerGW.String()

	pluginOpts := &pluginConfig{
		mtu:             cliMTU,
		mode:            macvlanMode,
		containerSubnet: containerNet,
		gatewayIP:       containerGW,
		hostIface:       macvlanEthIface,
	}
	// Leaving as info for now to stdout the plugin config
	log.Infof("Plugin configuration options are: \n %s", pluginOpts)

	ipAllocator := ipallocator.New()
{'fraction_non_alphanumeric': 0.11618763557483731, 'fraction_numerical': 0.006507592190889371, 'mean_word_length': 3.3599290780141846, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5415260', 'n_tokens_mistral': 2570, 'n_tokens_neox': 2455, 'n_words': 736}