Skip to content

Instantly share code, notes, and snippets.

@gshimansky
Created February 13, 2019 21:51
Show Gist options
  • Save gshimansky/11caf38f8a71cf0c0c4912ccdbc27c17 to your computer and use it in GitHub Desktop.
Save gshimansky/11caf38f8a71cf0c0c4912ccdbc27c17 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"log"
"os"
"os/signal"
"sync"
"time"
"github.com/intel-go/nff-go/flow"
"github.com/intel-go/nff-go/packet"
)
var duration time.Duration
var delay time.Duration
var outport *uint
var lock sync.Mutex
var dumpFile *os.File
func main() {
inport := flag.Uint("inport", 0, "port for receiver")
outport = flag.Uint("outport", 0, "port for send packet")
du := flag.Uint("durtion", 30, "time to send packets in seconds")
de := flag.Uint("delay", 2, "delay between packets")
dump := flag.String("dump", "", "dump packets to output pcap file")
flag.Parse()
duration = time.Duration(*du) * time.Second
delay = time.Duration(*de) * time.Second
if *dump != "" {
log.Println("Dumping packets to file ", *dump)
var err error
dumpFile, err = os.Create(*dump)
if err != nil {
log.Fatalln(err)
}
packet.WritePcapGlobalHdr(dumpFile)
if err != nil {
log.Fatalln(err)
}
// Set up reaction to SIGINT (Ctrl-C)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
sig := <-c
log.Printf("Received signal %v\n", sig)
lock.Lock()
err := dumpFile.Close()
if err != nil {
log.Fatalln(err)
}
os.Exit(2)
}()
}
config := flow.Config{}
flow.CheckFatal(flow.SystemInit(&config))
inputFlow, err := flow.SetReceiver(uint16(*inport))
flow.CheckFatal(err)
flow.CheckFatal(flow.SetHandler(inputFlow, dumpHandler, nil))
flow.CheckFatal(flow.SetSender(inputFlow, uint16(*inport)))
flow.CheckFatal(flow.SystemInitPortsAndMemory())
go func() {
flow.CheckFatal(flow.SystemStartScheduler())
}()
sendPackets()
}
func sendPackets() {
stopTime := time.Now().Add(duration)
mac := flow.GetPortMACAddress(uint16(*outport))
myAddr := uint32(0x0a000e0a) // 10.0.14.10
targetAddr := uint32(0x0a000e14) // 10.0.14.20
for time.Now().Before(stopTime) {
time.Sleep(delay)
pkt, err := packet.NewPacket()
if err != nil {
log.Fatalln(err)
}
packet.InitARPRequestPacket(pkt, mac,
packet.SwapBytesUint32(myAddr), packet.SwapBytesUint32(targetAddr))
dumpPacket(pkt)
pkt.SendPacket(uint16(*outport))
}
}
func dumpHandler(pkt *packet.Packet, ctx flow.UserContext) {
dumpPacket(pkt)
}
func dumpPacket(pkt *packet.Packet) {
if dumpFile != nil {
lock.Lock()
err := pkt.WritePcapOnePacket(dumpFile)
lock.Unlock()
if err != nil {
log.Fatalln(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment