• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang netlink.ParseAddr函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/vishvananda/netlink.ParseAddr函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseAddr函数的具体用法?Golang ParseAddr怎么用?Golang ParseAddr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了ParseAddr函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: IpAddrDel

func IpAddrDel(cResp chan<- *Response, rawArgs *json.RawMessage, tag string) {
	args := &struct {
		Ifname string `json:"ifname"`
		IpCidr string `json:"ip"`
	}{}

	json.Unmarshal(*rawArgs, &args)

	iface, err := netlink.LinkByName(args.Ifname)
	if err != nil {
		cResp <- &Response{nil, tag, NewRTNetlinkError(err)}
		return
	}

	ip, err := netlink.ParseAddr(args.IpCidr)
	if err != nil {
		cResp <- &Response{nil, tag, NewRTNetlinkError(err)}
		return
	}

	if err := netlink.AddrDel(iface, ip); err != nil {
		cResp <- &Response{nil, tag, NewRTNetlinkError(err)}
		return
	}

	cResp <- &Response{true, tag, nil}
}
开发者ID:0xef53,项目名称:phoenix-guest-agent,代码行数:27,代码来源:network.go


示例2: main

func main() {
	lo, _ := netlink.LinkByName("lo")
	fmt.Println(lo)
	addr, _ := netlink.ParseAddr("127.0.0.2/8")
	fmt.Println(addr)
	netlink.AddrAdd(lo, addr)
	fmt.Println(netlink.AddrList(lo, netlink.FAMILY_ALL))

	req := nl.NewNetlinkRequest(syscall.NLMSG_MIN_TYPE, syscall.NLM_F_REQUEST)

	data := append([]byte("TASKSTATS"), 0)
	a := &Attr{
		Type: 2,
		Data: data,
	}
	Hdr := []byte{
		3,
		0,
		0,
		0,
	}
	m := &Message{
		Type:    16,
		Pid:     -1,
		Seq:     -1,
		Flags:   NLM_F_REQUEST,
		Payload: append(Hdr, a.Dump()...),
	}

	req.AddData(m)
	res, _ := req.Execute(syscall.NETLINK_GENERIC, 0)
	fmt.Println(res)
	fmt.Println(parse_attributes(res[0][4:])[1])
}
开发者ID:shiaho,项目名称:netlink_tool,代码行数:34,代码来源:main_linux.go


示例3: applyNetConf

func applyNetConf(link netlink.Link, netConf config.InterfaceConfig) error {
	if netConf.DHCP {
		log.Infof("Running DHCP on %s", link.Attrs().Name)
		cmd := exec.Command("dhcpcd", "-A4", "-e", "force_hostname=true", link.Attrs().Name)
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		if err := cmd.Run(); err != nil {
			log.Error(err)
		}
	} else if netConf.IPV4LL {
		if err := AssignLinkLocalIP(link); err != nil {
			log.Error("IPV4LL set failed")
			return err
		}
	} else if netConf.Address == "" {
		return nil
	} else {
		addr, err := netlink.ParseAddr(netConf.Address)
		if err != nil {
			return err
		}
		if err := netlink.AddrAdd(link, addr); err != nil {
			log.Error("addr add failed")
			return err
		}
		log.Infof("Set %s on %s", netConf.Address, link.Attrs().Name)
	}

	if netConf.MTU > 0 {
		if err := netlink.LinkSetMTU(link, netConf.MTU); err != nil {
			log.Error("set MTU Failed")
			return err
		}
	}

	if err := netlink.LinkSetUp(link); err != nil {
		log.Error("failed to setup link")
		return err
	}

	if netConf.Gateway != "" {
		gatewayIp := net.ParseIP(netConf.Gateway)
		if gatewayIp == nil {
			return errors.New("Invalid gateway address " + netConf.Gateway)
		}

		route := netlink.Route{
			Scope: netlink.SCOPE_UNIVERSE,
			Gw:    net.ParseIP(netConf.Gateway),
		}
		if err := netlink.RouteAdd(&route); err != nil {
			log.Error("gateway set failed")
			return err
		}

		log.Infof("Set default gateway %s", netConf.Gateway)
	}

	return nil
}
开发者ID:jgatkinsn,项目名称:os,代码行数:60,代码来源:network.go


示例4: SetupNetwork

/*   SetupNetwork()
 */
func SetupNetwork(addr string) error {
	// Bring up loop back interface.
	lo, e := netlink.LinkByName("lo")
	if e != nil {
		return fmt.Errorf("Failed to find loopback interface: %v", e)
	}

	if e := netlink.LinkSetUp(lo); e != nil {
		return fmt.Errorf("Failed to setup loopback interface: %v", e)
	}

	if len(addr) > 0 {
		veth, e := netlink.LinkByName("veth0")
		if e != nil {
			return fmt.Errorf("Failed to find veth interface: %v", e)
		}

		addr, e := netlink.ParseAddr(addr)
		if e != nil {
			return fmt.Errorf("Failed to parse NetworkAddr: %v", e)
		}

		netlink.AddrAdd(veth, addr)
		if e := netlink.LinkSetUp(veth); e != nil {
			return fmt.Errorf("Network link failed to come up: %v", e)
		}
	}

	return nil
}
开发者ID:tswindell,项目名称:nest-boot,代码行数:32,代码来源:main.go


示例5: routingUp

func routingUp() {
	veth, err := netlink.LinkByName("myveth1")
	if err != nil {
		panic(err)
	}

	err = netlink.LinkSetUp(veth)
	if err != nil {
		panic(err)
	}

	addr, _ := netlink.ParseAddr("172.19.80.2/24")
	err = netlink.AddrAdd(veth, addr)
	if err != nil {
		panic(err)
	}

	routes := createRoutes(veth)

	for _, route := range routes {
		fmt.Println("Adding route", route)
		err := netlink.RouteAdd(route)
		if err != nil {
			fmt.Println(err)
			// panic(err)
		}
	}
}
开发者ID:mycroft,项目名称:go-snippets,代码行数:28,代码来源:main.go


示例6: addrAdd

func addrAdd(t *testing.T, l netlink.Link, a string) {
	addr, err := netlink.ParseAddr(a)
	if err != nil {
		t.Fatal(err)
	}

	err = netlink.AddrAdd(l, addr)
	if err != nil && err != syscall.EEXIST {
		t.Fatal(err)
	}
}
开发者ID:vishvananda,项目名称:wormhole,代码行数:11,代码来源:functional_test.go


示例7: applyNetConf

func applyNetConf(link netlink.Link, netConf InterfaceConfig) error {
	if netConf.IPV4LL {
		if err := AssignLinkLocalIP(link); err != nil {
			log.Errorf("IPV4LL set failed: %v", err)
			return err
		}
	} else if netConf.Address == "" {
		return nil
	} else {
		addr, err := netlink.ParseAddr(netConf.Address)
		if err != nil {
			return err
		}
		if err := netlink.AddrAdd(link, addr); err != nil {
			//Ignore this error
			log.Errorf("addr add failed: %v", err)
		} else {
			log.Infof("Set %s on %s", netConf.Address, link.Attrs().Name)
		}
	}

	if netConf.MTU > 0 {
		if err := netlink.LinkSetMTU(link, netConf.MTU); err != nil {
			log.Errorf("set MTU Failed: %v", err)
			return err
		}
	}

	if err := netlink.LinkSetUp(link); err != nil {
		log.Errorf("failed to setup link: %v", err)
		return err
	}

	if netConf.Gateway != "" {
		gatewayIp := net.ParseIP(netConf.Gateway)
		if gatewayIp == nil {
			return errors.New("Invalid gateway address " + netConf.Gateway)
		}

		route := netlink.Route{
			Scope: netlink.SCOPE_UNIVERSE,
			Gw:    net.ParseIP(netConf.Gateway),
		}
		if err := netlink.RouteAdd(&route); err != nil {
			log.Errorf("gateway set failed: %v", err)
			return err
		}

		log.Infof("Set default gateway %s", netConf.Gateway)
	}

	return nil
}
开发者ID:UNwS,项目名称:docker-from-scratch,代码行数:53,代码来源:netconf_linux.go


示例8: TestPortInformation

func TestPortInformation(t *testing.T) {
	mockContainerInfo := &plmodels.ContainerInfo{}
	mockContainerConfig := &plmodels.ContainerConfig{}
	containerID := "foo"
	mockContainerConfig.ContainerID = &containerID

	mockHostConfig := &container.HostConfig{}

	portMap := nat.PortMap{}
	port, _ := nat.NewPort("tcp", "80")
	portBinding := nat.PortBinding{
		HostIP:   "127.0.0.1",
		HostPort: "8000",
	}
	portBindings := []nat.PortBinding{portBinding}
	portMap[port] = portBindings
	mockHostConfig.PortBindings = portMap

	mockContainerInfo.ContainerConfig = mockContainerConfig

	ip, _ := netlink.ParseAddr("192.168.1.1/24")
	ips := []netlink.Addr{*ip}

	co := viccontainer.NewVicContainer()
	co.HostConfig = mockHostConfig
	co.ContainerID = containerID
	co.Name = "bar"
	cache.ContainerCache().AddContainer(co)

	ports := portInformation(mockContainerInfo, ips)

	assert.NotEmpty(t, ports, "There should be bound IPs")
	assert.Equal(t, len(ports), 1, "Expected 1 port binding, found %d", len(ports))

	port, _ = nat.NewPort("tcp", "80")
	portBinding = nat.PortBinding{
		HostIP:   "127.0.0.1",
		HostPort: "00",
	}
	portMap[port] = portBindings
	ports = portInformation(mockContainerInfo, ips)
	assert.NotEmpty(t, ports, "There should be 1 bound IP")
	assert.Equal(t, len(ports), 1, "Expected 1 port binding, found %d", len(ports))

	port, _ = nat.NewPort("tcp", "800")
	portBinding = nat.PortBinding{
		HostIP:   "127.0.0.1",
		HostPort: "800",
	}
	portMap[port] = portBindings
	ports = portInformation(mockContainerInfo, ips)
	assert.Equal(t, len(ports), 2, "Expected 2 port binding, found %d", len(ports))
}
开发者ID:vmware,项目名称:vic,代码行数:53,代码来源:container_test.go


示例9: SetInterfaceIP

// SetInterfaceIP : Set IP address of an interface
func SetInterfaceIP(name string, ipstr string) error {
	iface, err := netlink.LinkByName(name)
	if err != nil {
		return err
	}
	ipaddr, err := netlink.ParseAddr(ipstr)
	if err != nil {
		return err
	}
	netlink.LinkSetUp(iface)
	return netlink.AddrAdd(iface, ipaddr)
}
开发者ID:jojimt,项目名称:netplugin,代码行数:13,代码来源:netutils.go


示例10: DelIp

func DelIp(ip, iface string) error {
	link, err := netlink.LinkByName(iface)
	if err != nil {
		return err
	}

	addr, err := netlink.ParseAddr(ip)
	if err != nil {
		return err
	}

	return netlink.AddrDel(link, addr)
}
开发者ID:luizbafilho,项目名称:fusis,代码行数:13,代码来源:net.go


示例11: AssignLinkLocalIP

func AssignLinkLocalIP(link netlink.Link) error {
	ifaceName := link.Attrs().Name
	iface, err := net.InterfaceByName(ifaceName)
	if err != nil {
		log.Error("could not get information about interface")
		return err
	}
	addrs, err := iface.Addrs()
	if err != nil {
		log.Error("Error fetching existing ip on interface")
	}
	for _, addr := range addrs {
		if addr.String()[:7] == "169.254" {
			log.Info("Link Local IP already set on interface")
			return nil
		}
	}
	randSource, err := getPseudoRandomGenerator(link.Attrs().HardwareAddr)
	if err != nil {
		return err
	}
	// try a random address upto 10 times
	for i := 0; i < 10; i++ {
		randGenerator := rand.New(*randSource)
		randomNum := randGenerator.Uint32()
		dstIP := getNewIPV4LLAddr(randomNum)
		if dstIP[2] == 0 || dstIP[2] == 255 {
			i--
			continue
		}
		_, _, err := arping.PingOverIfaceByName(dstIP, ifaceName)
		if err != nil {
			// this ip is not being used
			addr, err := netlink.ParseAddr(dstIP.String() + "/16")
			if err != nil {
				log.Errorf("error while parsing ipv4ll addr, err = %v", err)
				return err
			}
			if err := netlink.AddrAdd(link, addr); err != nil {
				log.Error("ipv4ll addr add failed")
				return err
			}
			log.Infof("Set %s on %s", dstIP.String(), link.Attrs().Name)
			return nil
		}
	}
	log.Error("Could not find a suitable ipv4ll")
	return fmt.Errorf("Could not find a suitable ipv4ll")
}
开发者ID:pirater,项目名称:os,代码行数:49,代码来源:ipv4ll_linux.go


示例12: BindAndSetup

func BindAndSetup(veth netlink.Link, ips string) error {
	addr, err := netlink.ParseAddr(ips)
	if err != nil {
		return err
	}

	if err := netlink.AddrAdd(veth, addr); err != nil {
		return err
	}

	if err := netlink.LinkSetUp(veth); err != nil {
		return err
	}
	return nil
}
开发者ID:CMGS,项目名称:eru-agent,代码行数:15,代码来源:vlan_linux.go


示例13: setupIface

func setupIface(link netlink.Link, cfg Cfg) error {
	// up loopback
	lo, err := netlink.LinkByName("lo")
	if err != nil {
		return fmt.Errorf("lo interface: %v", err)
	}
	if err := netlink.LinkSetUp(lo); err != nil {
		return fmt.Errorf("up veth: %v", err)
	}
	addr, err := netlink.ParseAddr(cfg.IP)
	if err != nil {
		return fmt.Errorf("parse IP: %v", err)
	}
	return netlink.AddrAdd(link, addr)
}
开发者ID:v1k0d3n,项目名称:unc,代码行数:15,代码来源:network.go


示例14: applyAddress

func applyAddress(address string, link netlink.Link, netConf InterfaceConfig) error {
	addr, err := netlink.ParseAddr(address)
	if err != nil {
		return err
	}
	if err := netlink.AddrAdd(link, addr); err == syscall.EEXIST {
		//Ignore this error
	} else if err != nil {
		log.Errorf("addr add failed: %v", err)
	} else {
		log.Infof("Set %s on %s", netConf.Address, link.Attrs().Name)
	}

	return nil
}
开发者ID:rancher,项目名称:netconf,代码行数:15,代码来源:netconf_linux.go


示例15: initBridge

// initBridge creates a bridge if it does not exist
func initBridge() error {
	_, err := net.InterfaceByName(bridgeName)
	if err == nil {
		return nil
	}
	if !strings.Contains(err.Error(), "no such network interface") {
		return err
	}

	// Create *netlink.Bridge object
	la := netlink.NewLinkAttrs()
	la.Name = bridgeName
	la.MTU = mtu
	br := &netlink.Bridge{LinkAttrs: la}
	if err := netlink.LinkAdd(br); err != nil {
		return fmt.Errorf("Bridge creation failed: %v", err)
	}

	// Setup ip address for bridge
	addr, err := netlink.ParseAddr(ipAddr)
	if err != nil {
		return fmt.Errorf("Parsing address %s: %v", ipAddr, err)
	}
	if err := netlink.AddrAdd(br, addr); err != nil {
		return fmt.Errorf("Adding address %v to bridge %s failed: %v", addr, bridgeName, err)
	}

	// Validate that the IPAddress is there!
	_, err = getIfaceAddr(bridgeName)
	if err != nil {
		return fmt.Errorf("No IP address found on bridge %s", bridgeName)
	}

	// Add NAT rules for iptables
	if err = natOut(ipAddr, iptables.Insert); err != nil {
		return fmt.Errorf("Could not set NAT rules for bridge %s", bridgeName)
	}

	// Bring the bridge up
	if err := netlink.LinkSetUp(br); err != nil {
		return err
	}

	return nil
}
开发者ID:jfrazelle,项目名称:netns,代码行数:46,代码来源:bridge.go


示例16: bridgeCreate

func bridgeCreate() {
	la := netlink.NewLinkAttrs()
	la.Name = "mycbridge"
	mybridge := &netlink.Bridge{la}
	err := netlink.LinkAdd(mybridge)
	if err != nil {
		panic(err)
	}

	addr, _ := netlink.ParseAddr("172.19.80.1/24")
	err = netlink.AddrAdd(mybridge, addr)
	if err != nil {
		panic(err)
	}

	err = netlink.LinkSetUp(mybridge)
	if err != nil {
		panic(err)
	}
}
开发者ID:mycroft,项目名称:go-snippets,代码行数:20,代码来源:main.go


示例17: setUpVLan

func setUpVLan(cid, ips string, pid int, veth netlink.Link) bool {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	origns, err := netns.Get()
	if err != nil {
		logs.Info("Get orignal namespace failed", err)
		return false
	}
	defer origns.Close()

	ns, err := netns.GetFromPid(pid)
	if err != nil {
		logs.Info("Get container namespace failed", err)
		return false
	}

	netns.Set(ns)
	defer ns.Close()
	defer netns.Set(origns)

	addr, err := netlink.ParseAddr(ips)
	if err != nil {
		logs.Info("Parse CIDR failed", err)
		return false
	}

	if err := netlink.AddrAdd(veth, addr); err != nil {
		logs.Info("Add addr to veth failed", err)
		return false
	}

	if err := netlink.LinkSetUp(veth); err != nil {
		logs.Info("Setup veth failed", err)
		return false
	}

	logs.Info("Add vlan device success", cid[:12])
	return true
}
开发者ID:kevinsu1989,项目名称:eru-agent,代码行数:40,代码来源:vlan_linux.go


示例18: SetAddr

/* Configure @iface with address @cidr. Deletes any existing addresses */
func SetAddr(iface string, cidr string) {
	if strings.Index(cidr, "/") == -1 {
		/* No scope given: assume full netmask */
		cidr = cidr + "/32"
	}
	addr, err := netlink.ParseAddr(cidr)
	if err != nil {
		log.Fatalf("failed to parse %s interface address %s: %s", iface, cidr, err)
	}
	link, err := netlink.LinkByName(iface)
	if err != nil {
		log.Fatalf("failed to look up interface %s: %s", iface, err)
	}

	addrs, err := netlink.AddrList(link, 0)
	if err != nil {
		log.Fatalf("failed to get %s interface addresses: %s", iface, err)
	}

	already_configured := false
	for _, old_addr := range addrs {
		/* Test if already configured, otherwise EEXISTS */
		if old_addr.Equal(*addr) {
			already_configured = true
		} else {
			log.Printf("removing %s from %s", old_addr.IPNet, iface)
			if err := netlink.AddrDel(link, &old_addr); err != nil {
				log.Fatalf("failed to remove address from %s: %s", iface, err)
			}
		}
	}

	log.Printf("configuring %s on %s", addr.IPNet, iface)
	if !already_configured {
		if err := netlink.AddrAdd(link, addr); err != nil {
			log.Fatalf("failed to add address to %s: %s", iface, err)
		}
	}
}
开发者ID:leobcn,项目名称:presentations,代码行数:40,代码来源:iface.go


示例19: AddContainerNetworking

//netlink is not avaible in MAC OS, build fail.
func AddContainerNetworking() {
	if CliIF == "" {
		log.Fatal("the host-interface is missing,please give one")
	}
	if ok := utils.ValidateHostIface(CliIF); !ok {
		log.Fatalf("the host-interface [ %s ] was not found.", CliIF)
	}

	hostmacvlanname, _ := utils.GenerateRandomName(hostprefix, hostlen)
	hostEth, _ := netlink.LinkByName(CliIF)

	//create the macvlan device
	macvlandev := &netlink.Macvlan{
		LinkAttrs: netlink.LinkAttrs{
			Name:        hostmacvlanname,
			ParentIndex: hostEth.Attrs().Index,
		},
		Mode: netlink.MACVLAN_MODE_BRIDGE,
	}
	if err := netlink.LinkAdd(macvlandev); err != nil {
		log.Fatalf("failed to create Macvlan: [ %v ] with the error: %s", macvlandev.Attrs().Name, err)
	}
	//	log.Infof("Created Macvlan port: [ %s ] using the mode: [ %s ]", macvlan.Name, macvlanMode)
	// ugly, actually ,can get the ns from netns.getfromDocker. the netns have many function, netns.getformpid
	// netns.getfromdocker the arg can not be the container name
	dockerPid := utils.DockerPid(CliCName)
	//the macvlandev can be use directly, don't get netlink.byname again.
	netlink.LinkSetNsPid(macvlandev, dockerPid)

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	//get root network namespace
	origns, _ := netns.Get()
	defer origns.Close()

	//enter the docker container network
	dockerNS, _ := netns.GetFromPid(dockerPid)
	defer dockerNS.Close()

	netns.Set(dockerNS)

	// use macvlandev can cause error,need type assertion. netlink.Macvlan not must be netlink.Link,fmz(realy? the vlan action add)
	// it is wrong,
	macvlandev1, _ := netlink.LinkByName(macvlandev.Attrs().Name)

	// when the eth is up, set name fail,: Device or resource busy
	netlink.LinkSetDown(macvlandev1)
	netlink.LinkSetName(macvlandev1, "eth1")

	addr, err := netlink.ParseAddr(CliIP)
	if err != nil {
		log.Fatalf("failed to parse the ip address %v", CliIP)
	}
	netlink.AddrAdd(macvlandev1, addr)
	netlink.LinkSetUp(macvlandev1)

	/* set the default route, have some problem. Dst == 0.0.0.0/0? no
	defaultgw := &netlink.Route{
		Dst: nil,
	}
	netlink.RouteDel(defaultgw)
	ip, _ := net.ParseIP("8.8.8.8")
	routes, _ := netlink.RouteGet(ip)
	for _, r := range routes {
		netlink.RouteDel(&r)
	}
	*/

	//if use ip instruction,  it also can config the container, --privileged have no effect.
	// The sublime test code(test this function) is strange, it only can avaiable in first time. And then fail(even need to reboot)
	// got it,

	//following code successfully delete the default route in docker container,but error in my host ,no such process
	routes, _ := netlink.RouteList(nil, netlink.FAMILY_V4)
	for _, r := range routes {
		if r.Dst == nil {
			if err := netlink.RouteDel(&r); err != nil {
				log.Warnf("delete the default error: ", err)
			}
		}
	}

	if CligwIP == "" {
		log.Fatal("container gw is null")
	}

	defaultRoute := &netlink.Route{
		Dst:       nil,
		Gw:        net.ParseIP(CligwIP),
		LinkIndex: macvlandev1.Attrs().Index,
	}
	if err := netlink.RouteAdd(defaultRoute); err != nil {
		log.Warnf("create default route error: ", err)
	}
	netns.Set(origns)
}
开发者ID:fmzhen,项目名称:docker-macvlan,代码行数:98,代码来源:flat.go


示例20: main

func main() {
	if len(os.Args) != 3 {
		panic("not enough args")
	}

	intf, err := net.InterfaceByName(os.Args[1])
	if err != nil {
		panic(err)
	}

	containerID := os.Args[2]

	log.Infof("Requesting for container ID %q on interface %q", containerID, intf.Name)

	contPid := getContainerInfo(containerID)
	log.Infof("Container has PID %s", contPid)

	runtime.LockOSThread()
	f, err := os.Open(fmt.Sprintf("/proc/%s/ns/net", contPid))
	if err != nil {
		panic(err)
	}

	if _, _, err := unix.RawSyscall(unix.SYS_SETNS, f.Fd(), syscall.CLONE_NEWNET, 0); err != 0 {
		panic(err)
	}

	link, err := netlink.LinkByName("eth0")
	if err != nil {
		panic(err)
	}

	chaddr := link.Attrs().HardwareAddr

	p := dhcp.RequestPacket(dhcp.Discover, chaddr, nil, []byte{1, 2, 3, 4}, true, []dhcp.Option{})
	conn, err := net.ListenPacket("udp", ":68")
	if err != nil {
		panic(err)
	}

	ipc := ipv4.NewPacketConn(conn)
	ipc.SetControlMessage(ipv4.FlagInterface, true)
	to := &net.UDPAddr{
		IP:   net.IPv4(255, 255, 255, 255),
		Port: 67,
	}

	eth0, err := net.InterfaceByName("eth0")
	if err != nil {
		panic(err)
	}

	if _, err := ipc.WriteTo(p, &ipv4.ControlMessage{IfIndex: eth0.Index}, to); err != nil {
		panic(err)
	}

	newPacket := dhcp.NewPacket(dhcp.BootReply)

	for {
		_, cm, _, err := ipc.ReadFrom(newPacket)
		if err != nil {
			panic(err)
		}

		if cm != nil && cm.IfIndex == eth0.Index {
			log.Infof("Received reply: %q", newPacket.YIAddr().String())
			break
		}
	}

	addr, err := netlink.ParseAddr(newPacket.YIAddr().String() + "/16")
	if err != nil {
		panic(err)
	}

	log.Infof("Setting interface eth0...")

	if err := netlink.AddrAdd(link, addr); err != nil {
		panic(err)
	}
}
开发者ID:erikh,项目名称:dhcpd,代码行数:81,代码来源:client.go



注:本文中的github.com/vishvananda/netlink.ParseAddr函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang netlink.RouteAdd函数代码示例发布时间:2022-05-28
下一篇:
Golang netlink.NewHandleAt函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap