@@ -6,19 +6,15 @@ import (
66 "context"
77 "fmt"
88 "net"
9- "runtime"
10- "time"
119
1210 "github.com/google/gopacket"
1311 "github.com/google/gopacket/layers"
1412 "github.com/google/gopacket/macs"
15- "github.com/v-byte-cpu/sx/pkg/packet"
1613 "github.com/v-byte-cpu/sx/pkg/scan"
1714)
1815
1916type ScanMethod struct {
20- reqgen scan.RequestGenerator
21- pktgen scan.PacketGenerator
17+ scan.PacketSource
2218 parser * gopacket.DecodingLayerParser
2319 results * scan.ResultChan
2420 ctx context.Context
@@ -47,46 +43,22 @@ func (r *ScanResult) ID() string {
4743 return r .IP
4844}
4945
50- type ScanMethodOption func (sm * ScanMethod )
51-
52- func LiveMode (rescanTimeout time.Duration ) ScanMethodOption {
53- return func (sm * ScanMethod ) {
54- sm .reqgen = scan .NewLiveRequestGenerator (rescanTimeout )
55- }
56- }
57-
58- func NewScanMethod (ctx context.Context , opts ... ScanMethodOption ) * ScanMethod {
46+ func NewScanMethod (ctx context.Context , psrc scan.PacketSource ) * ScanMethod {
5947 sm := & ScanMethod {
60- ctx : ctx ,
61- results : scan .NewResultChan (ctx , 1000 ),
62- reqgen : scan .RequestGeneratorFunc (scan .Requests ),
63- pktgen : scan .NewPacketMultiGenerator (newPacketFiller (), runtime .NumCPU ()),
48+ PacketSource : psrc ,
49+ ctx : ctx ,
50+ results : scan .NewResultChan (ctx , 1000 ),
6451 }
6552 parser := gopacket .NewDecodingLayerParser (layers .LayerTypeEthernet , & sm .rcvEth , & sm .rcvARP )
6653 parser .IgnoreUnsupported = true
6754 sm .parser = parser
68-
69- for _ , o := range opts {
70- o (sm )
71- }
7255 return sm
7356}
7457
7558func (s * ScanMethod ) Results () <- chan scan.Result {
7659 return s .results .Chan ()
7760}
7861
79- func (s * ScanMethod ) Packets (ctx context.Context , r * scan.Range ) <- chan * packet.BufferData {
80- requests , err := s .reqgen .GenerateRequests (ctx , r )
81- if err != nil {
82- out := make (chan * packet.BufferData , 1 )
83- out <- & packet.BufferData {Err : err }
84- close (out )
85- return out
86- }
87- return s .pktgen .Packets (ctx , requests )
88- }
89-
9062func (s * ScanMethod ) ProcessPacketData (data []byte , _ * gopacket.CaptureInfo ) error {
9163 // try to exit as early as possible
9264 select {
@@ -115,13 +87,13 @@ func (s *ScanMethod) ProcessPacketData(data []byte, _ *gopacket.CaptureInfo) err
11587 return nil
11688}
11789
118- type packetFiller struct {}
90+ type PacketFiller struct {}
11991
120- func newPacketFiller () * packetFiller {
121- return & packetFiller {}
92+ func NewPacketFiller () * PacketFiller {
93+ return & PacketFiller {}
12294}
12395
124- func (* packetFiller ) Fill (packet gopacket.SerializeBuffer , pair * scan.Request ) error {
96+ func (* PacketFiller ) Fill (packet gopacket.SerializeBuffer , pair * scan.Request ) error {
12597 eth := & layers.Ethernet {
12698 SrcMAC : pair .SrcMAC ,
12799 DstMAC : net.HardwareAddr {0xff , 0xff , 0xff , 0xff , 0xff , 0xff },
0 commit comments