bettercap/net/arp.go

36 lines
708 B
Go

package net
import (
"fmt"
"sync"
)
type ArpTable map[string]string
var (
arpWasParsed = false
arpLock = &sync.Mutex{}
arpTable = make(ArpTable)
)
func ArpLookup(iface string, address string, refresh bool) (string, error) {
// Refresh ARP table if first run or if a force refresh has been instructed.
if ArpParsed() == false || refresh == true {
if _, err := ArpUpdate(iface); err != nil {
return "", err
}
}
// Lookup the hardware address of this ip.
if mac, found := arpTable[address]; found == true {
return mac, nil
}
return "", fmt.Errorf("Could not find mac for %s", address)
}
func ArpParsed() bool {
arpLock.Lock()
defer arpLock.Unlock()
return arpWasParsed
}