refact: divided os specific code for arp parsing

This commit is contained in:
evilsocket 2018-01-09 20:03:39 +01:00
parent 6106ec80a2
commit debdeba956
2 changed files with 43 additions and 39 deletions

View file

@ -2,11 +2,7 @@ package net
import ( import (
"fmt" "fmt"
"regexp"
"strings"
"sync" "sync"
"github.com/evilsocket/bettercap-ng/core"
) )
type ArpTable map[string]string type ArpTable map[string]string
@ -17,9 +13,6 @@ var (
arp_table = make(ArpTable) arp_table = make(ArpTable)
) )
var ArpTableParser = regexp.MustCompile("^[^\\d\\.]+([\\d\\.]+).+\\s+([a-f0-9:]{17}).+\\s+(.+)$")
var ArpTableTokens = 4
func ArpDiff(current, before ArpTable) ArpTable { func ArpDiff(current, before ArpTable) ArpTable {
diff := make(ArpTable) diff := make(ArpTable)
for ip, mac := range current { for ip, mac := range current {
@ -53,35 +46,3 @@ func ArpParsed() bool {
defer arp_lock.Unlock() defer arp_lock.Unlock()
return arp_parsed return arp_parsed
} }
func ArpUpdate(iface string) (ArpTable, error) {
arp_lock.Lock()
defer arp_lock.Unlock()
// Signal we parsed the ARP table at least once.
arp_parsed = true
// Run "arp -an" and parse the output.
output, err := core.Exec("arp", []string{"-a", "-n"})
if err != nil {
return arp_table, err
}
new_table := make(ArpTable)
for _, line := range strings.Split(output, "\n") {
m := ArpTableParser.FindStringSubmatch(line)
if len(m) == ArpTableTokens {
address := m[1]
mac := m[2]
ifname := m[3]
if ifname == iface {
new_table[address] = mac
}
}
}
arp_table = new_table
return arp_table, nil
}

43
net/arp_unix.go Normal file
View file

@ -0,0 +1,43 @@
package net
import (
"regexp"
"strings"
"github.com/evilsocket/bettercap-ng/core"
)
var ArpTableParser = regexp.MustCompile("^[^\\d\\.]+([\\d\\.]+).+\\s+([a-f0-9:]{17}).+\\s+(.+)$")
var ArpTableTokens = 4
func ArpUpdate(iface string) (ArpTable, error) {
arp_lock.Lock()
defer arp_lock.Unlock()
// Signal we parsed the ARP table at least once.
arp_parsed = true
// Run "arp -an" and parse the output.
output, err := core.Exec("arp", []string{"-a", "-n"})
if err != nil {
return arp_table, err
}
new_table := make(ArpTable)
for _, line := range strings.Split(output, "\n") {
m := ArpTableParser.FindStringSubmatch(line)
if len(m) == ArpTableTokens {
address := m[1]
mac := m[2]
ifname := m[3]
if ifname == iface {
new_table[address] = mac
}
}
}
arp_table = new_table
return arp_table, nil
}