Skip to content

Commit 88cc39e

Browse files
committed
Refactor ARP scan
1 parent 181fb31 commit 88cc39e

File tree

3 files changed

+22
-43
lines changed

3 files changed

+22
-43
lines changed

command/arp.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"os"
77
"os/signal"
8+
"runtime"
89
"strings"
910
"time"
1011

@@ -48,11 +49,7 @@ var arpCmd = &cobra.Command{
4849
logger = log.NewUniqueLogger(logger)
4950
}
5051

51-
var opts []arp.ScanMethodOption
52-
if arpLiveModeFlag {
53-
opts = append(opts, arp.LiveMode(1*time.Second))
54-
}
55-
m := arp.NewScanMethod(ctx, opts...)
52+
m := newARPScanMethod(ctx)
5653

5754
return startEngine(ctx, &engineConfig{
5855
logger: logger,
@@ -62,3 +59,13 @@ var arpCmd = &cobra.Command{
6259
})
6360
},
6461
}
62+
63+
func newARPScanMethod(ctx context.Context) *arp.ScanMethod {
64+
var reqgen scan.RequestGenerator = scan.RequestGeneratorFunc(scan.Requests)
65+
if arpLiveModeFlag {
66+
reqgen = scan.NewLiveRequestGenerator(1 * time.Second)
67+
}
68+
pktgen := scan.NewPacketMultiGenerator(arp.NewPacketFiller(), runtime.NumCPU())
69+
psrc := scan.NewPacketSource(reqgen, pktgen)
70+
return arp.NewScanMethod(ctx, psrc)
71+
}

pkg/scan/arp/arp.go

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1916
type 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

7558
func (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-
9062
func (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},

pkg/scan/arp/arp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestProcessPacketData(t *testing.T) {
2222

2323
ctx, cancel := context.WithCancel(context.Background())
2424
defer cancel()
25-
sm := NewScanMethod(ctx)
25+
sm := NewScanMethod(ctx, nil)
2626

2727
// generate packet data
2828
packet := gopacket.NewSerializeBuffer()

0 commit comments

Comments
 (0)