Adding error handling for socket connection

This commit is contained in:
Pin
2023-04-18 23:18:56 -04:00
parent 963db705d9
commit 46e2d35fbe

14
main.go
View File

@@ -9,6 +9,7 @@ import (
"os/exec" "os/exec"
"runtime" "runtime"
"strings" "strings"
"time"
"strconv" "strconv"
@@ -70,12 +71,21 @@ func sInit(os string) *targetInfo {
return &tInfo return &tInfo
} }
func openCapture(tInfo *targetInfo) *pcap.Handle {
for {
handle, err := pcap.OpenLive(tInfo.iFace, tInfo.snaplen, true, pcap.BlockForever)
if err == nil {
return handle
}
time.Sleep(5 * time.Second)
}
}
func main() { func main() {
tInfo := sInit(runtime.GOOS) tInfo := sInit(runtime.GOOS)
// Read package and analze them // Read package and analze them
handle, err := pcap.OpenLive(tInfo.iFace, tInfo.snaplen, true, pcap.BlockForever) handle := openCapture(tInfo)
errorPrinter(err)
handle.SetBPFFilter(tInfo.filter) handle.SetBPFFilter(tInfo.filter)
packets := gopacket.NewPacketSource(handle, handle.LinkType()).Packets() packets := gopacket.NewPacketSource(handle, handle.LinkType()).Packets()