From e448c779ee6bc416a8678c0f1f35b263b04f64b9 Mon Sep 17 00:00:00 2001 From: "Dr. Adedayo Adetoye" Date: Wed, 6 Mar 2019 09:46:29 +0000 Subject: [PATCH] Fix: some channel closing issues --- portscan.go | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/portscan.go b/portscan.go index 99b5dc0..ac70dc2 100644 --- a/portscan.go +++ b/portscan.go @@ -521,31 +521,36 @@ func listenForACKPackets(handle *pcap.Handle, route routeFinder, config ScanConf go func() { defer close(out) for { - packet, err := packetSource.NextPacket() - if err == io.EOF { + select { + case <-stop: return - } - if err != nil { - if err.Error() == pcap.NextErrorTimeoutExpired.Error() { - continue + default: + packet, err := packetSource.NextPacket() + if err == io.EOF { + return } - return - } - parser.DecodeLayers(packet.Data(), &decodedLayers) - for _, lyr := range decodedLayers { - //Look for TCP ACK - if lyr.Contains(layers.LayerTypeTCP) { - ack := PortACK{ - Host: ip.SrcIP.String(), - Port: strings.Split(tcp.SrcPort.String(), "(")[0], - SYN: tcp.SYN, - RST: tcp.RST, + if err != nil { + if err.Error() == pcap.NextErrorTimeoutExpired.Error() { + continue } - output <- ack - if !config.Quiet && ack.IsOpen() { - fmt.Printf("%s:%s (%s) is %s\n", ack.Host, ack.Port, ack.GetServiceName(), ack.Status()) + return + } + parser.DecodeLayers(packet.Data(), &decodedLayers) + for _, lyr := range decodedLayers { + //Look for TCP ACK + if lyr.Contains(layers.LayerTypeTCP) { + ack := PortACK{ + Host: ip.SrcIP.String(), + Port: strings.Split(tcp.SrcPort.String(), "(")[0], + SYN: tcp.SYN, + RST: tcp.RST, + } + output <- ack + if !config.Quiet && ack.IsOpen() { + fmt.Printf("%s:%s (%s) is %s\n", ack.Host, ack.Port, ack.GetServiceName(), ack.Status()) + } + break } - break } } }