package ipv4 import ( "encoding/json" "fmt" "net" "strconv" "sync" "sync/atomic" "time" "go.uber.org/zap" olog "git.loafle.net/overflow/log-go" omd "git.loafle.net/overflow/model/discovery" omm "git.loafle.net/overflow/model/meta" omu "git.loafle.net/overflow/model/util" "git.loafle.net/overflow_scanner/probe/matcher" "git.loafle.net/overflow_scanner/probe/pcap" "github.com/google/gopacket" "github.com/google/gopacket/layers" ) func scanPortUDP(host *omd.Host, dp *omd.DiscoverPort, resultChan chan interface{}, errChan chan error, stopChan chan struct{}, wg *sync.WaitGroup) { defer func() { wg.Done() }() ps, err := pcap.RetainScanner(host.Zone) if nil != err { errChan <- fmt.Errorf("Discovery: Cannot retain pcap instance %v", err) return } defer func() { pcap.ReleaseScanner(host.Zone) }() udpChan := ps.OpenUDP(host.Address) defer func() { ps.CloseUDP(host.Address, udpChan) }() timerStopped := make(chan struct{}) go func() { ports := make(map[int]*omd.Port) var delay atomic.Value delay.Store(false) ticker := time.NewTicker(time.Second * 3) for { select { case packet, ok := <-udpChan: if !ok { olog.Logger().Debug("Discovery: udp channel is closed") return } delay.Store(true) if p := handlePacketUDP(host, dp, ports, packet); nil != p { resultChan <- p } case <-ticker.C: if false == delay.Load().(bool) { ticker.Stop() timerStopped <- struct{}{} return } delay.Store(false) case <-stopChan: return } } }() if err := sendUDP(host, dp, stopChan); nil != err { errChan <- err return } select { case <-stopChan: return case <-timerStopped: return } } func sendUDP(host *omd.Host, dp *omd.DiscoverPort, stopChan chan struct{}) error { ip := net.ParseIP(host.Address) if nil == ip { return fmt.Errorf("Discovery: IP(%s) of host is not valid", host.Address) } ms := matcher.GetUDPMatchers() conn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0}) if err != nil { return err } defer func() { conn.Close() }() for indexI := 0; indexI < len(ms); indexI++ { m := ms[indexI] Loop: for portNumber := dp.FirstScanRange; portNumber < dp.LastScanRange; portNumber++ { if nil != dp.ExcludePorts { for _, exPortNumber := range dp.ExcludePorts { if portNumber == exPortNumber { continue Loop } } } if !m.IsSend(portNumber) { continue } addr := &net.UDPAddr{IP: ip.To4(), Port: portNumber} for i := 0; i < m.PacketCount(); i++ { p := m.Packet(i) if _, err := conn.WriteToUDP(p.Buffer, addr); err != nil { olog.Logger().Error("Discovery: UDP write error", zap.Error(err)) } } timer := time.NewTimer(time.Microsecond * 200) select { case <-stopChan: return nil case <-timer.C: } } } return nil } func handlePacketUDP(host *omd.Host, dp *omd.DiscoverPort, ports map[int]*omd.Port, packet gopacket.Packet) *omd.Port { ipLayer := packet.Layer(layers.LayerTypeIPv4) if ipLayer.(*layers.IPv4).SrcIP.String() == host.Zone.Address { return nil } if net := packet.NetworkLayer(); net == nil { } else if udpLayer := packet.Layer(layers.LayerTypeUDP); udpLayer == nil { } else if udp, ok := udpLayer.(*layers.UDP); ok { srcIP := ipLayer.(*layers.IPv4).SrcIP port := int(udp.SrcPort) olog.Logger().Debug("Discovery", zap.String("ip", srcIP.String()), zap.Int("port", port)) if _, ok := ports[port]; ok || !dp.Contains(port) { return nil } p := &omd.Port{ MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumUDP), PortNumber: json.Number(strconv.Itoa(port)), UDPLayer: udpLayer, DiscoveredDate: omu.NowPtr(), } p.Host = host ports[port] = p return p } return nil }