Ejemplo n.º 1
0
func (iml *IPMacLearner) learner() {
	for {
		pair := C.pcapRead(iml.handle)
		if pair == nil {
			if iml.closed {
				return
			}
			continue
		}
		mac := C.GoString(pair.mac)

		var ip, ip6 string
		if pair.ip != nil {
			ip = C.GoString(pair.ip)
		}
		if pair.ip6 != nil {
			ip6 = C.GoString(pair.ip6)
		}

		log.Debug("got mac/ip pair:", mac, ip, ip6)

		iml.lock.Lock()

		// skip macs we aren't tracking
		if _, ok := iml.pairs[mac]; !ok {
			iml.lock.Unlock()
			continue
		}

		// TODO: Need to send from goroutine?
		iml.pairs[mac] <- IP{ip, ip6}

		iml.lock.Unlock()
	}
}
Ejemplo n.º 2
0
func (iml *IPMacLearner) learner() {
	for {
		pair := C.pcapRead(iml.handle)
		if pair == nil {
			if iml.closed {
				return
			}
			continue
		}
		mac := C.GoString(pair.mac)

		var ip, ip6 string
		if pair.ip != nil {
			ip = C.GoString(pair.ip)
		}
		if pair.ip6 != nil {
			ip6 = C.GoString(pair.ip6)
		}

		log.Debug("got mac/ip pair:", mac, ip, ip6)

		iml.lock.Lock()

		// skip macs we aren't tracking
		if _, ok := iml.pairs[mac]; !ok {
			iml.lock.Unlock()
			continue
		}

		if ip != "" {
			iml.pairs[mac].IP4 = ip
		} else if ip6 != "" {
			if iml.pairs[mac].IP6 != "" && strings.HasPrefix(ip6, "fe80") {
				log.Debugln("ignoring link-local over existing IPv6 address")
			} else {
				iml.pairs[mac].IP6 = ip6
			}
		}

		iml.lock.Unlock()
	}
}