overflow_discovery/discovery/ipv4/port_udp.go

158 lines
3.4 KiB
Go
Raw Permalink Normal View History

2017-11-18 13:03:37 +00:00
package ipv4
2017-11-21 05:31:17 +00:00
import (
2017-12-04 09:37:39 +00:00
"encoding/json"
2017-11-21 12:47:55 +00:00
"fmt"
"net"
2017-12-04 11:59:31 +00:00
"strconv"
2017-11-21 05:31:17 +00:00
"sync"
2017-11-21 12:47:55 +00:00
"time"
2017-11-18 13:03:37 +00:00
2017-11-21 12:47:55 +00:00
"git.loafle.net/commons_go/logging"
2017-12-04 09:37:39 +00:00
oocmd "git.loafle.net/overflow/overflow_commons_go/modules/discovery"
discoveryM "git.loafle.net/overflow/overflow_commons_go/modules/discovery/model"
2017-11-21 12:47:55 +00:00
"git.loafle.net/overflow/overflow_discovery/commons/pcap"
2017-12-04 09:37:39 +00:00
"git.loafle.net/overflow/overflow_discovery/matcher"
2017-11-21 12:47:55 +00:00
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
2017-11-21 05:31:17 +00:00
)
2017-12-04 09:37:39 +00:00
func scanPortUDP(host *discoveryM.Host, dp *discoveryM.DiscoveryPort, resultChan chan interface{}, errChan chan error, stopChan chan struct{}, wg *sync.WaitGroup) {
2017-11-21 05:31:17 +00:00
defer func() {
wg.Done()
}()
2017-11-18 13:03:37 +00:00
2017-11-21 12:47:55 +00:00
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.IP)
defer func() {
ps.CloseUDP(host.IP, udpChan)
}()
go func() {
2017-12-04 09:37:39 +00:00
ports := make(map[int]*discoveryM.Port)
2017-11-22 06:33:41 +00:00
2017-11-21 12:47:55 +00:00
for {
select {
case packet, ok := <-udpChan:
if !ok {
2018-03-21 10:34:40 +00:00
logging.Logger().Debugf("Discovery: udp channel is closed")
2017-11-21 12:47:55 +00:00
return
}
2017-11-22 06:33:41 +00:00
if p := handlePacketUDP(host, dp, ports, packet); nil != p {
2017-11-21 12:47:55 +00:00
resultChan <- p
}
case <-stopChan:
return
}
}
}()
if err := sendUDP(host, dp, stopChan); nil != err {
errChan <- err
return
}
timer := time.NewTimer(3 * time.Second)
select {
case <-stopChan:
return
case <-timer.C:
return
}
}
2017-12-04 09:37:39 +00:00
func sendUDP(host *discoveryM.Host, dp *discoveryM.DiscoveryPort, stopChan chan struct{}) error {
2017-11-22 05:55:40 +00:00
ip := net.ParseIP(host.IP)
if nil == ip {
return fmt.Errorf("Discovery: IP(%s) of host is not valid", host.IP)
}
2017-12-04 09:37:39 +00:00
ms := matcher.GetUDPMatchers()
2017-11-22 05:55:40 +00:00
conn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0})
if err != nil {
2017-11-21 12:47:55 +00:00
return err
}
defer func() {
2017-11-22 05:55:40 +00:00
conn.Close()
2017-11-21 12:47:55 +00:00
}()
2017-11-22 05:55:40 +00:00
for indexI := 0; indexI < len(ms); indexI++ {
m := ms[indexI]
2017-11-21 12:47:55 +00:00
2017-11-22 05:55:40 +00:00
Loop:
for portNumber := dp.FirstScanRange; portNumber < dp.LastScanRange; portNumber++ {
if nil != dp.ExcludePorts {
for _, exPortNumber := range dp.ExcludePorts {
if portNumber == exPortNumber {
continue Loop
}
2017-11-21 12:47:55 +00:00
}
}
2017-11-22 05:55:40 +00:00
if !m.IsSend(portNumber) {
continue
}
2017-11-21 12:47:55 +00:00
2017-11-22 05:55:40 +00:00
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 {
2018-03-21 10:34:40 +00:00
logging.Logger().Errorf("Discovery: UDP write error %v", err)
2017-11-22 05:55:40 +00:00
}
}
2017-11-21 12:47:55 +00:00
2017-11-22 05:55:40 +00:00
timer := time.NewTimer(time.Microsecond * 200)
2017-11-21 12:47:55 +00:00
2017-11-22 05:55:40 +00:00
select {
case <-stopChan:
return nil
case <-timer.C:
}
}
2017-11-21 12:47:55 +00:00
}
return nil
}
2017-12-04 09:37:39 +00:00
func handlePacketUDP(host *discoveryM.Host, dp *discoveryM.DiscoveryPort, ports map[int]*discoveryM.Port, packet gopacket.Packet) *discoveryM.Port {
2017-11-22 05:55:40 +00:00
ipLayer := packet.Layer(layers.LayerTypeIPv4)
2017-11-21 12:47:55 +00:00
2017-11-22 05:55:40 +00:00
if ipLayer.(*layers.IPv4).SrcIP.String() == host.Zone.IP {
2017-11-21 12:47:55 +00:00
return nil
}
2017-11-22 05:55:40 +00:00
if net := packet.NetworkLayer(); net == nil {
} else if udpLayer := packet.Layer(layers.LayerTypeUDP); udpLayer == nil {
} else if udp, ok := udpLayer.(*layers.UDP); ok {
2017-11-21 12:47:55 +00:00
2017-11-22 05:55:40 +00:00
srcIP := ipLayer.(*layers.IPv4).SrcIP
port := int(udp.SrcPort)
2018-03-21 10:34:40 +00:00
logging.Logger().Debugf("Discovery: IP of UDP(%d) src %v", port, srcIP)
2017-11-22 06:33:41 +00:00
if _, ok := ports[port]; ok || !dp.Contains(port) {
return nil
}
2017-11-21 12:47:55 +00:00
2017-12-04 09:37:39 +00:00
p := &discoveryM.Port{
PortType: oocmd.PortTypeUDP,
2017-12-04 11:59:31 +00:00
PortNumber: json.Number(strconv.Itoa(port)),
2017-11-22 10:04:04 +00:00
UDPLayer: udpLayer,
2017-11-22 05:55:40 +00:00
}
p.Host = host
2017-11-22 06:33:41 +00:00
ports[port] = p
2017-11-22 05:55:40 +00:00
return p
2017-11-21 12:47:55 +00:00
}
2017-11-22 05:55:40 +00:00
return nil
2017-11-18 13:03:37 +00:00
}