Skip to content

Commit

Permalink
Refactor ARP scan
Browse files Browse the repository at this point in the history
  • Loading branch information
v-byte-cpu committed Mar 20, 2021
1 parent 181fb31 commit 88cc39e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 43 deletions.
17 changes: 12 additions & 5 deletions command/arp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"os"
"os/signal"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -48,11 +49,7 @@ var arpCmd = &cobra.Command{
logger = log.NewUniqueLogger(logger)
}

var opts []arp.ScanMethodOption
if arpLiveModeFlag {
opts = append(opts, arp.LiveMode(1*time.Second))
}
m := arp.NewScanMethod(ctx, opts...)
m := newARPScanMethod(ctx)

return startEngine(ctx, &engineConfig{
logger: logger,
Expand All @@ -62,3 +59,13 @@ var arpCmd = &cobra.Command{
})
},
}

func newARPScanMethod(ctx context.Context) *arp.ScanMethod {
var reqgen scan.RequestGenerator = scan.RequestGeneratorFunc(scan.Requests)
if arpLiveModeFlag {
reqgen = scan.NewLiveRequestGenerator(1 * time.Second)
}
pktgen := scan.NewPacketMultiGenerator(arp.NewPacketFiller(), runtime.NumCPU())
psrc := scan.NewPacketSource(reqgen, pktgen)
return arp.NewScanMethod(ctx, psrc)
}
46 changes: 9 additions & 37 deletions pkg/scan/arp/arp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@ import (
"context"
"fmt"
"net"
"runtime"
"time"

"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/macs"
"github.com/v-byte-cpu/sx/pkg/packet"
"github.com/v-byte-cpu/sx/pkg/scan"
)

type ScanMethod struct {
reqgen scan.RequestGenerator
pktgen scan.PacketGenerator
scan.PacketSource
parser *gopacket.DecodingLayerParser
results *scan.ResultChan
ctx context.Context
Expand Down Expand Up @@ -47,46 +43,22 @@ func (r *ScanResult) ID() string {
return r.IP
}

type ScanMethodOption func(sm *ScanMethod)

func LiveMode(rescanTimeout time.Duration) ScanMethodOption {
return func(sm *ScanMethod) {
sm.reqgen = scan.NewLiveRequestGenerator(rescanTimeout)
}
}

func NewScanMethod(ctx context.Context, opts ...ScanMethodOption) *ScanMethod {
func NewScanMethod(ctx context.Context, psrc scan.PacketSource) *ScanMethod {
sm := &ScanMethod{
ctx: ctx,
results: scan.NewResultChan(ctx, 1000),
reqgen: scan.RequestGeneratorFunc(scan.Requests),
pktgen: scan.NewPacketMultiGenerator(newPacketFiller(), runtime.NumCPU()),
PacketSource: psrc,
ctx: ctx,
results: scan.NewResultChan(ctx, 1000),
}
parser := gopacket.NewDecodingLayerParser(layers.LayerTypeEthernet, &sm.rcvEth, &sm.rcvARP)
parser.IgnoreUnsupported = true
sm.parser = parser

for _, o := range opts {
o(sm)
}
return sm
}

func (s *ScanMethod) Results() <-chan scan.Result {
return s.results.Chan()
}

func (s *ScanMethod) Packets(ctx context.Context, r *scan.Range) <-chan *packet.BufferData {
requests, err := s.reqgen.GenerateRequests(ctx, r)
if err != nil {
out := make(chan *packet.BufferData, 1)
out <- &packet.BufferData{Err: err}
close(out)
return out
}
return s.pktgen.Packets(ctx, requests)
}

func (s *ScanMethod) ProcessPacketData(data []byte, _ *gopacket.CaptureInfo) error {
// try to exit as early as possible
select {
Expand Down Expand Up @@ -115,13 +87,13 @@ func (s *ScanMethod) ProcessPacketData(data []byte, _ *gopacket.CaptureInfo) err
return nil
}

type packetFiller struct{}
type PacketFiller struct{}

func newPacketFiller() *packetFiller {
return &packetFiller{}
func NewPacketFiller() *PacketFiller {
return &PacketFiller{}
}

func (*packetFiller) Fill(packet gopacket.SerializeBuffer, pair *scan.Request) error {
func (*PacketFiller) Fill(packet gopacket.SerializeBuffer, pair *scan.Request) error {
eth := &layers.Ethernet{
SrcMAC: pair.SrcMAC,
DstMAC: net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
Expand Down
2 changes: 1 addition & 1 deletion pkg/scan/arp/arp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestProcessPacketData(t *testing.T) {

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sm := NewScanMethod(ctx)
sm := NewScanMethod(ctx, nil)

// generate packet data
packet := gopacket.NewSerializeBuffer()
Expand Down

0 comments on commit 88cc39e

Please sign in to comment.