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

@@ -1,30 +0,0 @@
package dhcpv4
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)
}
}
}

View File

@@ -2,6 +2,7 @@ package dhcpv4
import (
"fmt"
"git.darknebu.la/maride/pancap/common"
"github.com/google/gopacket/layers"
"log"
)
@@ -71,7 +72,7 @@ func printHostnames() {
}
// and print it as a tree.
printTree(tmparr)
common.PrintTree(tmparr)
}
// Adds the given hostname to the hostname array, or patches an existing entry if found

View File

@@ -2,6 +2,7 @@ package dhcpv4
import (
"fmt"
"git.darknebu.la/maride/pancap/common"
"github.com/google/gopacket/layers"
)
@@ -11,11 +12,11 @@ var (
// Processes the DHCP request packet handed over
func processRequestPacket(dhcppacket layers.DHCPv4) {
requestMAC = appendIfUnique(dhcppacket.ClientHWAddr.String(), requestMAC)
requestMAC = common.AppendIfUnique(dhcppacket.ClientHWAddr.String(), requestMAC)
}
// Prints the summary of all DHCP request packets
func printRequestSummary() {
fmt.Printf("%d unique DHCP requests\n", len(requestMAC))
printTree(requestMAC)
common.PrintTree(requestMAC)
}

View File

@@ -2,6 +2,7 @@ package dhcpv4
import (
"fmt"
"git.darknebu.la/maride/pancap/common"
"github.com/google/gopacket/layers"
"log"
)
@@ -30,7 +31,7 @@ func printResponseSummary() {
}
// Draw as tree
printTree(tmpaddr)
common.PrintTree(tmpaddr)
}
// Adds a new response entry. If an IP address was already issued or a MAC asks multiple times for DNS, the case is examined further