Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending gopacket generated bytes #712

Open
justmumu opened this issue Jun 10, 2020 · 1 comment
Open

Sending gopacket generated bytes #712

justmumu opened this issue Jun 10, 2020 · 1 comment

Comments

@justmumu
Copy link

Hi,

First of all thank you for this amazing project. I am trying to use in my project.
But i can't figure out how to generate packets with gopacket. Is this possible ?

I saw GeneratePacketFromByte function and I used with gopacket's bytes but I got error "wrong destination MAC address". I think somehow gopacket bytes is not suitable.

Example Code:

func generatePacketFromByte(emptyPacket *packet.Packet, context flow.UserContext) {
        srcMAC, _ := hex.DecodeString("xxx")
	dstMAC, _ := hex.DecodeString("xxx")

	eth := &layers.Ethernet{
		SrcMAC: net.HardwareAddr(srcMAC),
		DstMAC: net.HardwareAddr(dstMAC),
		EthernetType: layers.EthernetTypeIPv4,
	}
	ip := &layers.IPv4{
		Version: 4,
		IHL: 5,
		TTL: 64,
		Id: 0,
		Protocol: layers.IPProtocolUDP,
		SrcIP: net.ParseIP(SrcIP).To4(),
		DstIP: net.ParseIP(DstIP).To4(),
	}
	udp := &layers.UDP{
		SrcPort: layers.UDPPort(1234),
		DstPort: layers.UDPPort(1234),
	}
	udp.SetNetworkLayerForChecksum(ip)
        
        buf := gopacket.NewSerializeBuffer()
	opts := gopacket.SerializeOptions{
		FixLengths: true,
		ComputeChecksums: true,
	}
	err = gopacket.SerializeLayers(buf, opts, eth, ip, udp)
        if err != nil {
		panic(err)
	}

        packet.GeneratePacketFromByte(emptyPacket, buf.Bytes())
}
@aregm
Copy link
Owner

aregm commented Jul 21, 2020

Hello @Rasity ! You can use https://github.com/intel-go/nff-go/blob/master/examples/gopacketParserExample.go as a reference. You just need to specify the source and destination MAC addresses correctly. There are builtin types so you can do something like this:

DAddr:     types.MACAddress{0x00, 0x40, 0x05, 0x40, 0xef, 0x24},
SAddr:     types.MACAddress{0x00, 0x60, 0x08, 0x9f, 0xb1, 0xf3},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants