Skip to content

Commit

Permalink
move packetgen connection into an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Mar 31, 2022
1 parent 8563935 commit 26ded23
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 51 deletions.
6 changes: 4 additions & 2 deletions examples/config/advanced/packetgen-ipv6.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ jobs:
- type: packetgen
args:
connection:
name: "ip6:tcp"
address: "::1"
type: raw
args:
name: "ip6:tcp"
address: "::1"
packet:
network:
type: ipv6
Expand Down
6 changes: 4 additions & 2 deletions examples/config/advanced/packetgen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ jobs:
- type: packetgen
args:
connection:
name: "ip4:tcp"
address: "0.0.0.0"
type: raw
args:
name: "ip4:tcp"
address: "0.0.0.0"
packet:
network:
type: ipv4
Expand Down
48 changes: 45 additions & 3 deletions src/core/packetgen/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,66 @@
package packetgen

import (
"fmt"
"net"

"github.com/google/gopacket"
"golang.org/x/net/ipv6"

"github.com/Arriven/db1000n/src/utils"
)

// ConnectionConfig describes which network to use when sending packets
type ConnectionConfig struct {
Type string
Args map[string]interface{}
}

func OpenConnection(c ConnectionConfig) (Connection, error) {
switch c.Type {
case "raw":
var cfg rawConnectionConfig
if err := utils.Decode(c.Args, &cfg); err != nil {
return nil, fmt.Errorf("error decoding connection config: %w", err)
}

return openRawConnection(cfg)
default:
return nil, fmt.Errorf("unknown connection type: %v", c.Type)
}
}

type Connection interface {
Write(Packet) (int, error)
}

// raw ipv4/ipv6 connection
type rawConnectionConfig struct {
Name string
Address string
}

// OpenRawConnection opens a raw ip network connection based on the provided config
type rawConn struct {
*ipv6.PacketConn
}

// openRawConnection opens a raw ip network connection based on the provided config
// use ipv6 as it also supports ipv4
func OpenRawConnection(c ConnectionConfig) (*ipv6.PacketConn, error) {
func openRawConnection(c rawConnectionConfig) (*rawConn, error) {
packetConn, err := net.ListenPacket(c.Name, c.Address)
if err != nil {
return nil, err
}

return ipv6.NewPacketConn(packetConn), nil
return &rawConn{PacketConn: ipv6.NewPacketConn(packetConn)}, nil
}

func (conn rawConn) Write(packet Packet) (n int, err error) {
payloadBuf := gopacket.NewSerializeBuffer()

if err = packet.Serialize(payloadBuf); err != nil {
return 0, fmt.Errorf("error serializing packet: %w", err)
}

return conn.WriteTo(payloadBuf.Bytes(), nil, &net.IPAddr{IP: packet.IP()})
}
16 changes: 2 additions & 14 deletions src/job/packetgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ package job
import (
"context"
"fmt"
"net"
"time"

"github.com/google/gopacket"
"github.com/google/uuid"
"go.uber.org/zap"

Expand All @@ -53,7 +51,7 @@ func packetgenJob(ctx context.Context, logger *zap.Logger, globalConfig *GlobalC
return nil, fmt.Errorf("error parsing job config: %w", err)
}

rawConn, err := packetgen.OpenRawConnection(jobConfig.Connection)
conn, err := packetgen.OpenConnection(jobConfig.Connection)
if err != nil {
return nil, fmt.Errorf("error building raw connection: %w", err)
}
Expand All @@ -63,16 +61,10 @@ func packetgenJob(ctx context.Context, logger *zap.Logger, globalConfig *GlobalC
return nil, fmt.Errorf("error parsing packet: %w", err)
}

payloadBuf := gopacket.NewSerializeBuffer()

trafficMonitor := metrics.Default.NewWriter(metrics.Traffic, uuid.New().String())
go trafficMonitor.Update(ctx, time.Second)

for jobConfig.Next(ctx) {
if err := payloadBuf.Clear(); err != nil {
return nil, fmt.Errorf("error clearing payload buffer: %w", err)
}

packetConfigRaw := packetTpl.Execute(logger, ctx)
logger.Debug("rendered packet config template", zap.Reflect("config", packetConfigRaw))

Expand All @@ -86,11 +78,7 @@ func packetgenJob(ctx context.Context, logger *zap.Logger, globalConfig *GlobalC
return nil, fmt.Errorf("error building packet: %w", err)
}

if err = packet.Serialize(payloadBuf); err != nil {
return nil, fmt.Errorf("error serializing packet: %w", err)
}

n, err := rawConn.WriteTo(payloadBuf.Bytes(), nil, &net.IPAddr{IP: packet.IP()})
n, err := conn.Write(packet)
if err != nil {
return nil, fmt.Errorf("error sending packet: %w", err)
}
Expand Down
30 changes: 0 additions & 30 deletions testconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,36 +71,6 @@
"args": {
"path": "https://localhost:9090/inc2/common/js/doo/modules/searchSite/u.php"
}
},
{
"type": "packetgen",
"filter": "{{ (.Value (ctx_key \"global\")).EnablePrimitiveJobs }}",
"args": {
"host": "{{ resolve_host \"localhost\" }}",
"port": "{{ random_port }}",
"packet": {
"payload": "test:blah",
"ethernet": {
"src_mac": "{{ random_mac_addr }}",
"dst_mac": "{{ random_mac_addr }}"
},
"ip": {
"src_ip": "{{ local_ip }}",
"dst_ip": "{{ resolve_host \"localhost\" }}"
},
"tcp": {
"src_port": "{{ random_port }}",
"dst_port": "{{ random_port }}",
"flags": {
"syn": true
}
},
"udp": {
"src_port": "{{ random_port }}",
"dst_port": "{{ random_port }}"
}
}
}
}
]
}

0 comments on commit 26ded23

Please sign in to comment.