This commit is contained in:
crusader 2018-09-03 16:01:35 +09:00
parent 51bf633ee8
commit 60e5f1c7c3
4 changed files with 18 additions and 43 deletions

View File

@ -109,7 +109,7 @@ func sendUDP4(discoverySession session.DiscoverySession, ps pcap.PCapScanner, ho
addr := &net.UDPAddr{IP: ip.To4(), Port: portNumber} addr := &net.UDPAddr{IP: ip.To4(), Port: portNumber}
for i := 0; i < _matcher.PacketCount(); i++ { for i := 0; i < _matcher.PacketCount(); i++ {
p := _matcher.Packet(i) p := _matcher.Packet(i)
if _, err := conn.WriteToUDP(p.Buffer, addr); err != nil { if _, err := conn.WriteToUDP(p.Bytes(), addr); err != nil {
log.Print("UDP write error", err) log.Print("UDP write error", err)
} }
} }

View File

@ -205,7 +205,7 @@ func (ds *ofDiscoverySession) AddHost(host *omd.Host) *omd.Host {
ds.discoveredMtx.Lock() ds.discoveredMtx.Lock()
defer ds.discoveredMtx.Unlock() defer ds.discoveredMtx.Unlock()
h, modified := ds.findHost(host, true) h, modified := ds.findHost(host)
if "" == h.Mac && "" != host.Mac { if "" == h.Mac && "" != host.Mac {
h.Mac = host.Mac h.Mac = host.Mac
@ -229,7 +229,7 @@ func (ds *ofDiscoverySession) AddPort(port *omd.Port) *omd.Port {
ds.discoveredMtx.Lock() ds.discoveredMtx.Lock()
defer ds.discoveredMtx.Unlock() defer ds.discoveredMtx.Unlock()
p, modified := ds.findPort(port, true) p, modified := ds.findPort(port)
meta, metaModified := ds.appendMeta(p.Meta, port.Meta) meta, metaModified := ds.appendMeta(p.Meta, port.Meta)
if metaModified { if metaModified {
@ -248,7 +248,7 @@ func (ds *ofDiscoverySession) AddService(service *omd.Service) *omd.Service {
ds.discoveredMtx.Lock() ds.discoveredMtx.Lock()
defer ds.discoveredMtx.Unlock() defer ds.discoveredMtx.Unlock()
s, modified := ds.findService(service, true) s, modified := ds.findService(service)
meta, metaModified := ds.appendMeta(s.Meta, service.Meta) meta, metaModified := ds.appendMeta(s.Meta, service.Meta)
if metaModified { if metaModified {
@ -286,7 +286,7 @@ func (ds *ofDiscoverySession) DiscoveredPort(host *omd.Host, portNumber int) map
ds.discoveredMtx.RLock() ds.discoveredMtx.RLock()
defer ds.discoveredMtx.RUnlock() defer ds.discoveredMtx.RUnlock()
h, _ := ds.findHost(host, false) h, _ := ds.findHost(host)
if nil == h { if nil == h {
return nil return nil
} }
@ -312,7 +312,7 @@ func (ds *ofDiscoverySession) DiscoveredService(port *omd.Port, name string) map
ds.discoveredMtx.RLock() ds.discoveredMtx.RLock()
defer ds.discoveredMtx.RUnlock() defer ds.discoveredMtx.RUnlock()
p, _ := ds.findPort(port, false) p, _ := ds.findPort(port)
if nil == p { if nil == p {
return nil return nil
} }
@ -334,15 +334,12 @@ func (ds *ofDiscoverySession) DiscoveredAllServices() map[*omd.Port]map[string]m
return ds.services return ds.services
} }
func (ds *ofDiscoverySession) findHost(host *omd.Host, add bool) (h *omd.Host, modified bool) { func (ds *ofDiscoverySession) findHost(host *omd.Host) (h *omd.Host, modified bool) {
modified = false modified = false
var ok bool var ok bool
h, ok = ds.hosts[host.Address] h, ok = ds.hosts[host.Address]
if !ok { if !ok {
if !add {
return
}
ds.hosts[host.Address] = host ds.hosts[host.Address] = host
if "" != host.Mac { if "" != host.Mac {
ds.includeMachosts[host.Address] = host ds.includeMachosts[host.Address] = host
@ -354,41 +351,29 @@ func (ds *ofDiscoverySession) findHost(host *omd.Host, add bool) (h *omd.Host, m
return return
} }
func (ds *ofDiscoverySession) findPort(port *omd.Port, add bool) (p *omd.Port, modified bool) { func (ds *ofDiscoverySession) findPort(port *omd.Port) (p *omd.Port, modified bool) {
modified = false modified = false
var ok bool var ok bool
h, _ := ds.findHost(port.Host, false) h, _ := ds.findHost(port.Host)
if nil == h { if nil == h {
return return
} }
hostPorts, ok := ds.ports[h] hostPorts, ok := ds.ports[h]
if !ok { if !ok {
if !add {
return
}
ds.ports[h] = make(map[json.Number]map[string]*omd.Port) ds.ports[h] = make(map[json.Number]map[string]*omd.Port)
hostPorts = ds.ports[h] hostPorts = ds.ports[h]
} }
ports, ok := hostPorts[port.PortNumber] ports, ok := hostPorts[port.PortNumber]
if !ok { if !ok {
if !add {
return
}
hostPorts[port.PortNumber] = make(map[string]*omd.Port) hostPorts[port.PortNumber] = make(map[string]*omd.Port)
ports = hostPorts[port.PortNumber] ports = hostPorts[port.PortNumber]
} }
p, ok = ports[port.MetaPortType.Key] p, ok = ports[port.MetaPortType.Key]
if !ok { if !ok {
if !add {
return
}
ports[port.MetaPortType.Key] = port ports[port.MetaPortType.Key] = port
p = ports[port.MetaPortType.Key] p = ports[port.MetaPortType.Key]
modified = true modified = true
@ -397,41 +382,29 @@ func (ds *ofDiscoverySession) findPort(port *omd.Port, add bool) (p *omd.Port, m
return return
} }
func (ds *ofDiscoverySession) findService(service *omd.Service, add bool) (s *omd.Service, modified bool) { func (ds *ofDiscoverySession) findService(service *omd.Service) (s *omd.Service, modified bool) {
modified = false modified = false
var ok bool var ok bool
p, _ := ds.findPort(service.Port, false) p, _ := ds.findPort(service.Port)
if nil == p { if nil == p {
return return
} }
portServices, ok := ds.services[p] portServices, ok := ds.services[p]
if !ok { if !ok {
if !add {
return
}
ds.services[p] = make(map[string]map[string]*omd.Service) ds.services[p] = make(map[string]map[string]*omd.Service)
portServices = ds.services[p] portServices = ds.services[p]
} }
services, ok := portServices[service.Key] services, ok := portServices[service.Key]
if !ok { if !ok {
if !add {
return
}
portServices[service.Key] = make(map[string]*omd.Service) portServices[service.Key] = make(map[string]*omd.Service)
services = portServices[service.Key] services = portServices[service.Key]
} }
s, ok = services[service.MetaCryptoType.Key] s, ok = services[service.MetaCryptoType.Key]
if !ok { if !ok {
if !add {
return
}
services[service.MetaCryptoType.Key] = service services[service.MetaCryptoType.Key] = service
s = services[service.MetaCryptoType.Key] s = services[service.MetaCryptoType.Key]
modified = true modified = true

View File

@ -2,6 +2,7 @@ package service
import ( import (
"fmt" "fmt"
"log"
"net" "net"
"time" "time"
@ -64,7 +65,7 @@ LOOP:
if nil != discoveredMatcher { if nil != discoveredMatcher {
// log.Printf("discovered matcher: %s(%s) %v", discoveredMatcher.Name(), discoveredMatcher.Key(), discoveredMatcher) // log.Printf("discovered matcher: %s(%s) %v", discoveredMatcher.Name(), discoveredMatcher.Key(), discoveredMatcher)
discoverySession.AddService(&omd.Service{ s := discoverySession.AddService(&omd.Service{
MetaCryptoType: discoveredConnector.metaCryptoType(), MetaCryptoType: discoveredConnector.metaCryptoType(),
Key: discoveredMatcher.Key(), Key: discoveredMatcher.Key(),
Name: discoveredMatcher.Name(), Name: discoveredMatcher.Name(),
@ -72,6 +73,7 @@ LOOP:
DiscoveredDate: omu.NowPtr(), DiscoveredDate: omu.NowPtr(),
Port: targetPort, Port: targetPort,
}) })
log.Print("Discovered Service: ", s, " \n Port: ", s.Port, " \n Host: ", s.Port.Host)
} }
return nil return nil
@ -105,7 +107,7 @@ LOOP:
if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); nil != err { if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); nil != err {
return nil return nil
} }
_, err := conn.Write(_packet.Buffer) _, err := conn.Write(_packet.Bytes())
if nil != err { if nil != err {
return nil return nil
} }
@ -162,7 +164,7 @@ LOOP:
if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); nil != err { if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); nil != err {
break INNER_LOOP break INNER_LOOP
} }
_, err := conn.Write(_packet.Buffer) _, err := conn.Write(_packet.Bytes())
if nil != err { if nil != err {
break INNER_LOOP break INNER_LOOP
} }

View File

@ -39,7 +39,7 @@ func processPrepacket(ch chan *Res, conn net.Conn, m csm.Matcher) {
n, _ := conn.Read(bytes) n, _ := conn.Read(bytes)
p := csm.NewPacket(bytes, n) p := csm.NewPacket(bytes, n)
_, err := conn.Write(m.Packet(i).Buffer) _, err := conn.Write(m.Packet(i).Bytes())
if err != nil { if err != nil {
ch <- &Res{nil, err} ch <- &Res{nil, err}
close(ch) close(ch)
@ -57,7 +57,7 @@ func processPrepacket(ch chan *Res, conn net.Conn, m csm.Matcher) {
func processPostpacket(ch chan *Res, conn net.Conn, m csm.Matcher) { func processPostpacket(ch chan *Res, conn net.Conn, m csm.Matcher) {
for i := 0; i < m.PacketCount(); i++ { for i := 0; i < m.PacketCount(); i++ {
_, err := conn.Write(m.Packet(i).Buffer) _, err := conn.Write(m.Packet(i).Bytes())
if err != nil { if err != nil {
ch <- &Res{nil, err} ch <- &Res{nil, err}
close(ch) close(ch)