Пример #1
0
func (manager *LinkManager) addrReceived(msg *netlink.Message) {
	rtmsg, _ := addr.ParseMessage(*msg)
	hdr, _ := rtmsg.Header.(*addr.Header)
	interfaceIndex := hdr.InterfaceIndex()
	ipAddrAttr, err := rtmsg.GetAttribute(addr.IFA_ADDRESS)
	if err != nil {
		log.Printf("Can't get IP address attribute: %v", err)
		return
	}

	link := manager.links[interfaceIndex]
	if link == nil {
		log.Printf("Received IP address for unknown interface index %d", interfaceIndex)
		return
	}
	ip := convertNlAddrToIp(&ipAddrAttr)
	log.Printf("Received new address for index %d: %v", interfaceIndex, ip.String())
	firstAddress := len(link.addresses) == 0
	link.addresses = append(link.addresses, &ip)
	if firstAddress {
		log.Printf("Received first address, interface should now be ready to be used")
		env := manager.createEmitEnv(link)
		upstartController.Emit("net-device-up", env, false)
	}
}
Пример #2
0
func (manager *LinkManager) addressRemoved(msg *netlink.Message) {
	rtmsg, err := addr.ParseMessage(*msg)
	if err != nil {
		log.Printf("Can't parse addr message")
	}
	hdr, _ := rtmsg.Header.(*addr.Header)
	ipAddrAttr, err := rtmsg.GetAttribute(addr.IFA_ADDRESS)
	delIp := convertNlAddrToIp(&ipAddrAttr)
	link := manager.links[hdr.InterfaceIndex()]
	log.Printf("Removing address %s from interface %d", delIp.String(), hdr.InterfaceIndex())
	var i int = -1
	for i = range link.addresses {
		ip := link.addresses[i]
		if delIp.Equal(*ip) {
			break
		}
	}
	if i > -1 {
		link.addresses = append(link.addresses[:i], link.addresses[i+1:]...)
		log.Printf("Removed one address from interface %d, %d addresses left", hdr.InterfaceIndex(), len(link.addresses))

	} else {
		log.Printf("ERROR: Requested to remove unknown address %s from interface %d", delIp.String(), hdr.InterfaceIndex())
	}
	if len(link.addresses) == 0 {
		env := manager.createEmitEnv(link)
		upstartController.Emit("net-device-down", env, false)
	}
}