container_discovery/internal/discoverer/ipv4/port_udp.go

173 lines
3.7 KiB
Go
Raw Normal View History

2018-04-19 11:36:56 +00:00
package ipv4
import (
"encoding/json"
"fmt"
"net"
"strconv"
"sync"
2018-04-27 15:33:22 +00:00
"sync/atomic"
2018-04-19 11:36:56 +00:00
"time"
"git.loafle.net/commons/logging-go"
occc "git.loafle.net/overflow/commons-go/core/constants"
2018-05-11 02:44:47 +00:00
occu "git.loafle.net/overflow/commons-go/core/util"
2018-04-26 09:00:24 +00:00
ocmd "git.loafle.net/overflow/commons-go/model/discovery"
2018-04-19 11:36:56 +00:00
"git.loafle.net/overflow/container_discovery/internal/pcap"
"git.loafle.net/overflow/container_discovery/internal/matcher"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
2018-04-27 16:20:01 +00:00
func scanPortUDP(host *ocmd.Host, dp *ocmd.DiscoverPort, resultChan chan interface{}, errChan chan error, stopChan chan struct{}, wg *sync.WaitGroup) {
2018-04-19 11:36:56 +00:00
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)
}()
2018-04-27 16:56:47 +00:00
udpChan := ps.OpenUDP(host.IPV4)
2018-04-19 11:36:56 +00:00
defer func() {
2018-04-27 16:56:47 +00:00
ps.CloseUDP(host.IPV4, udpChan)
2018-04-19 11:36:56 +00:00
}()
2018-04-27 15:33:22 +00:00
timerStopped := make(chan struct{})
2018-04-19 11:36:56 +00:00
go func() {
2018-04-26 09:00:24 +00:00
ports := make(map[int]*ocmd.Port)
2018-04-19 11:36:56 +00:00
2018-04-27 15:33:22 +00:00
var delay atomic.Value
delay.Store(false)
ticker := time.NewTicker(time.Second * 3)
2018-04-19 11:36:56 +00:00
for {
select {
case packet, ok := <-udpChan:
if !ok {
logging.Logger().Debugf("Discovery: udp channel is closed")
return
}
2018-04-27 15:33:22 +00:00
delay.Store(true)
2018-04-19 11:36:56 +00:00
if p := handlePacketUDP(host, dp, ports, packet); nil != p {
resultChan <- p
}
2018-04-27 15:33:22 +00:00
case <-ticker.C:
if false == delay.Load().(bool) {
ticker.Stop()
timerStopped <- struct{}{}
return
}
delay.Store(false)
2018-04-19 11:36:56 +00:00
case <-stopChan:
return
}
}
}()
if err := sendUDP(host, dp, stopChan); nil != err {
errChan <- err
return
}
select {
case <-stopChan:
return
2018-04-27 15:33:22 +00:00
case <-timerStopped:
2018-04-19 11:36:56 +00:00
return
}
}
2018-04-27 16:20:01 +00:00
func sendUDP(host *ocmd.Host, dp *ocmd.DiscoverPort, stopChan chan struct{}) error {
2018-04-27 16:56:47 +00:00
ip := net.ParseIP(host.IPV4)
2018-04-19 11:36:56 +00:00
if nil == ip {
2018-04-27 16:56:47 +00:00
return fmt.Errorf("Discovery: IP(%s) of host is not valid", host.IPV4)
2018-04-19 11:36:56 +00:00
}
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 {
logging.Logger().Errorf("Discovery: UDP write error %v", err)
}
}
timer := time.NewTimer(time.Microsecond * 200)
select {
case <-stopChan:
return nil
case <-timer.C:
}
}
}
return nil
}
2018-04-27 16:20:01 +00:00
func handlePacketUDP(host *ocmd.Host, dp *ocmd.DiscoverPort, ports map[int]*ocmd.Port, packet gopacket.Packet) *ocmd.Port {
2018-04-19 11:36:56 +00:00
ipLayer := packet.Layer(layers.LayerTypeIPv4)
2018-04-27 16:56:47 +00:00
if ipLayer.(*layers.IPv4).SrcIP.String() == host.Zone.IPV4 {
2018-04-19 11:36:56 +00:00
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)
logging.Logger().Debugf("Discovery: IP of UDP(%d) src %v", port, srcIP)
if _, ok := ports[port]; ok || !dp.Contains(port) {
return nil
}
2018-04-26 09:00:24 +00:00
p := &ocmd.Port{
2018-05-11 02:44:47 +00:00
PortType: occc.PortTypeUDP,
PortNumber: json.Number(strconv.Itoa(port)),
UDPLayer: udpLayer,
DiscoveredDate: occu.NowPtr(),
2018-04-19 11:36:56 +00:00
}
p.Host = host
ports[port] = p
return p
}
return nil
}