aregm / nff-go

NFF-Go -Network Function Framework for GO (former YANFF)
BSD 3-Clause "New" or "Revised" License
1.38k stars 156 forks source link

How should I do to generate a new *packet.Packet obj from some nessary data? #684

Closed cang233 closed 4 years ago

cang233 commented 4 years ago

I want to save some payloads from the packet in the handle function,and use this payload to new a packet.Packet then output it to a .pcap file,what should I do? I tried some codes but all failed with this error:

=== RUN   TestGenPacket
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x44 pc=0xa30767]

code:

func TestGenPacket(t *testing.T){
    data := "12345576"
    p,err := packet.NewPacket()
    if err!= nil{
        println("err=",err)
    }
    if packet.InitEmptyIPv4TCPPacket(p,uint(len([]byte(data)))){
        print("failed")
    }
    print("yes!")
}
cang233 commented 4 years ago

ok,i guess i know how to do.

        newp,err := packet.NewPacket()
        if err != nil{
            fmt.Println("New packet Error:,",err.Error())
            return
        }
        packet.GeneratePacketFromByte(newp,p.RawBytes)
gshimansky commented 4 years ago

If all you want to do is to read a PCAP file, you can also use SetReceiverFile function which produces packets from a packet dump file. There are also ReadPcapGlobalHdr and ReadPcapOnePacket which allow reading packets from PCAP file.