diff --git a/net/arp.go b/net/arp.go index b86f7cbe..de97b67f 100644 --- a/net/arp.go +++ b/net/arp.go @@ -2,7 +2,10 @@ package net import ( "fmt" + "strings" "sync" + + "github.com/evilsocket/bettercap-ng/core" ) type ArpTable map[string]string @@ -13,6 +16,38 @@ var ( arpTable = make(ArpTable) ) +func ArpUpdate(iface string) (ArpTable, error) { + arpLock.Lock() + defer arpLock.Unlock() + + // Signal we parsed the ARP table at least once. + arpWasParsed = true + + // Run "arp -an" (darwin) or "ip neigh" (linux) and parse the output + output, err := core.Exec(ArpCmd, ArpCmdOpts) + if err != nil { + return arpTable, err + } + + newTable := make(ArpTable) + for _, line := range strings.Split(output, "\n") { + m := ArpTableParser.FindStringSubmatch(line) + if len(m) == ArpTableTokens { + address := m[ArpTableTokenIndex[0]] + mac := m[ArpTableTokenIndex[1]] + ifname := m[ArpTableTokenIndex[2]] + + if ifname == iface { + newTable[address] = mac + } + } + } + + arpTable = newTable + + return arpTable, nil +} + 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 { diff --git a/net/arp_unix.go b/net/arp_unix.go deleted file mode 100644 index ae5b04ab..00000000 --- a/net/arp_unix.go +++ /dev/null @@ -1,38 +0,0 @@ -package net - -import ( - "github.com/evilsocket/bettercap-ng/core" - "strings" -) - -func ArpUpdate(iface string) (ArpTable, error) { - arpLock.Lock() - defer arpLock.Unlock() - - // Signal we parsed the ARP table at least once. - arpWasParsed = true - - // Run "arp -an" (darwin) or "ip neigh" (linux) and parse the output - output, err := core.Exec(ArpCmd, ArpCmdOpts) - if err != nil { - return arpTable, err - } - - newTable := make(ArpTable) - for _, line := range strings.Split(output, "\n") { - m := ArpTableParser.FindStringSubmatch(line) - if len(m) == ArpTableTokens { - address := m[ArpTableTokenIndex[0]] - mac := m[ArpTableTokenIndex[1]] - ifname := m[ArpTableTokenIndex[2]] - - if ifname == iface { - newTable[address] = mac - } - } - } - - arpTable = newTable - - return arpTable, nil -}