Skip to content

Commit

Permalink
Refactor SRC/DST selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Herms committed May 16, 2019
1 parent 654a182 commit bfb7fd8
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions pkg/prober/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package prober

import (
"fmt"
"net"

"github.com/google/gopacket"
"github.com/google/gopacket/layers"
Expand All @@ -11,8 +12,15 @@ const (
ttl = 64
)

func (p *Prober) craftPacket(pr *probe) ([]byte, error) {
func (p *Prober) getSrcAddrHop(hop int, seq uint64) net.IP {
return p.hops[hop-1].srcRange[seq%uint64(len(p.hops[hop-1].srcRange))]
}

func (p *Prober) getDstAddr(hop int, seq uint64) net.IP {
return p.hops[hop].dstRange[seq%uint64(len(p.hops[hop].dstRange))]
}

func (p *Prober) craftPacket(pr *probe) ([]byte, error) {
probeSer, err := pr.marshal()
if err != nil {
return nil, fmt.Errorf("Unable to marshal probe: %v", err)
Expand All @@ -34,11 +42,9 @@ func (p *Prober) craftPacket(pr *probe) ([]byte, error) {
continue
}

srcAddr := p.hops[i-1].srcRange[pr.Seq%uint64(len(p.hops[i-1].srcRange))]
dstAddr := p.hops[i].dstRange[pr.Seq%uint64(len(p.hops[i].dstRange))]
l = append(l, &layers.IPv4{
SrcIP: srcAddr,
DstIP: dstAddr,
SrcIP: p.getSrcAddrHop(i, pr.Seq),
DstIP: p.getDstAddr(i, pr.Seq),
Version: 4,
Protocol: layers.IPProtocolGRE,
TOS: p.tos,
Expand All @@ -52,7 +58,7 @@ func (p *Prober) craftPacket(pr *probe) ([]byte, error) {

// Create final UDP packet that will return
ip := &layers.IPv4{
SrcIP: p.hops[len(p.hops)-1].srcRange[pr.Seq%uint64(len(p.hops[len(p.hops)-1].srcRange))],
SrcIP: p.getSrcAddrHop(len(p.hops)-1, pr.Seq),
DstIP: p.localAddr,
Version: 4,
Protocol: layers.IPProtocolUDP,
Expand Down

0 comments on commit bfb7fd8

Please sign in to comment.