Move common code into new package, 'common'

This commit is contained in:
2019-11-29 14:32:07 +01:00
parent 14f36a8511
commit aa94fa12ec
9 changed files with 28 additions and 79 deletions

View File

@@ -2,6 +2,7 @@ package arp
import (
"fmt"
"git.darknebu.la/maride/pancap/common"
"github.com/fatih/color"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
@@ -33,14 +34,14 @@ func ProcessARPPacket(packet gopacket.Packet) error {
if arppacket.Operation == layers.ARPRequest {
// Request packet
participant.asked++
participant.askedList = appendIfUnique(net.IP(arppacket.DstProtAddress).String(), participant.askedList)
participant.askedList = common.AppendIfUnique(net.IP(arppacket.DstProtAddress).String(), participant.askedList)
// Add device entry
addDeviceEntry(sourceAddr, net.IP(arppacket.SourceProtAddress).String())
} else {
// Response packet
participant.answered++
participant.answeredList = appendIfUnique(net.IP(arppacket.SourceProtAddress).String(), participant.answeredList)
participant.answeredList = common.AppendIfUnique(net.IP(arppacket.SourceProtAddress).String(), participant.answeredList)
// Add device entry
addDeviceEntry(sourceAddr, net.IP(arppacket.SourceProtAddress).String())
@@ -68,7 +69,7 @@ func printTrafficStats() {
}
// And print it as a tree
printTree(tmparr)
common.PrintTree(tmparr)
}
// Prints an overview over all connected devices in the LAN
@@ -81,7 +82,7 @@ func printLANOverview() {
}
// And print it as a tree
printTree(tmparr)
common.PrintTree(tmparr)
}
// Returns the arpStats object for the given MAC address, or creates a new one

View File

@@ -1,30 +0,0 @@
package arp
import "fmt"
// Appends the appendee to the array if it does not contain appendee yet
func appendIfUnique(appendee string, array []string) []string {
// Iterate over all elements and check values
for _, elem := range array {
if elem == appendee {
// ... found. Stop here
return array
}
}
// None found, append
return append(array, appendee)
}
// Prints each element, along with a small ASCII tree
func printTree(strarr []string) {
// iterate over each element
for iter, elem := range strarr {
// check if we got the last element
if iter < len(strarr) - 1 {
fmt.Printf("|- %s\n", elem)
} else {
fmt.Printf("'- %s\n\n", elem)
}
}
}