Exemple #1
0
func TestPackEthIPv4TCPRaw(t *testing.T) {
	eth_pkt := eth.Make()
	eth_pkt.SrcAddr, _ = net.ParseMAC(hwsrc_str)
	eth_pkt.DstAddr, _ = net.ParseMAC(hwdst_str)

	ip4_pkt := ipv4.Make()
	ip4_pkt.SrcAddr = net.ParseIP(ipsrc_str)
	ip4_pkt.DstAddr = net.ParseIP(ipdst_str)

	tcp_pkt := tcp.Make()
	tcp_pkt.SrcPort = 41562
	tcp_pkt.DstPort = 8338
	tcp_pkt.Flags = tcp.Syn
	tcp_pkt.WindowSize = 8192

	raw_pkt := raw.Make()
	raw_pkt.Data = []byte("fdg agfh ldfhgk hfdkgh kfjdhsg kshfdgk")

	data, err := layers.Pack(eth_pkt, ip4_pkt, tcp_pkt, raw_pkt)
	if err != nil {
		t.Fatalf("Error packing: %s", err)
	}

	if ip4_pkt.GetLength() != 78 {
		t.Fatalf("IPv4 length mismatch: %d", ip4_pkt.GetLength())
	}

	if tcp_pkt.GetLength() != 58 {
		t.Fatalf("TCP length mismatch: %d", tcp_pkt.GetLength())
	}

	if !bytes.Equal(test_eth_ipv4_tcp_raw, data) {
		t.Fatalf("Raw packet mismatch: %x", data)
	}
}
Exemple #2
0
func TestPackEthIPv4UDPRaw(t *testing.T) {
	eth_pkt := eth.Make()
	eth_pkt.SrcAddr, _ = net.ParseMAC(hwsrc_str)
	eth_pkt.DstAddr, _ = net.ParseMAC(hwdst_str)

	ip4_pkt := ipv4.Make()
	ip4_pkt.SrcAddr = net.ParseIP(ipsrc_str)
	ip4_pkt.DstAddr = net.ParseIP(ipdst_str)

	udp_pkt := udp.Make()
	udp_pkt.SrcPort = 41562
	udp_pkt.DstPort = 8338

	raw_pkt := raw.Make()
	raw_pkt.Data = []byte("fdg agfh ldfhgk hfdkgh kfjdhsg kshfdgk")

	data, err := layers.Pack(eth_pkt, ip4_pkt, udp_pkt, raw_pkt)
	if err != nil {
		t.Fatalf("Error packing: %s", err)
	}

	if ip4_pkt.GetLength() != 66 {
		t.Fatalf("IPv4 length mismatch: %d", ip4_pkt.GetLength())
	}

	if udp_pkt.GetLength() != 46 {
		t.Fatalf("UDP length mismatch: %d", udp_pkt.GetLength())
	}

	if !bytes.Equal(test_eth_ipv4_udp_raw, data) {
		t.Fatalf("Raw packet mismatch: %x", data)
	}
}
Exemple #3
0
// Pack packets into their binary form and inject them in the given capture
// handle.. This will stack the packets before encoding them and also calculate
// the checksums.
func Send(c capture.Handle, pkts ...packet.Packet) error {
	if pkts[0].GetType() != c.LinkType() {
		return fmt.Errorf("Expected packet type %s, got %s",
			pkts[0].GetType(), c.LinkType())
	}

	buf, err := layers.Pack(pkts...)
	if err != nil {
		return fmt.Errorf("Could not pack: %s", err)
	}

	err = c.Inject(buf)
	if err != nil {
		return fmt.Errorf("Could not inject: %s", err)
	}

	return nil
}
Exemple #4
0
func TestPackEthArp(t *testing.T) {
	eth_pkt := eth.Make()
	eth_pkt.SrcAddr, _ = net.ParseMAC(hwsrc_str)
	eth_pkt.DstAddr, _ = net.ParseMAC("ff:ff:ff:ff:ff:ff")

	arp_pkt := arp.Make()
	arp_pkt.HWSrcAddr, _ = net.ParseMAC(hwsrc_str)
	arp_pkt.HWDstAddr, _ = net.ParseMAC("00:00:00:00:00:00")
	arp_pkt.ProtoSrcAddr = net.ParseIP(ipsrc_str)
	arp_pkt.ProtoDstAddr = net.ParseIP(ipdst_str)

	buf, err := layers.Pack(eth_pkt, arp_pkt)
	if err != nil {
		t.Fatalf("Error packing: %s", err)
	}

	if !bytes.Equal(test_eth_arp, buf) {
		t.Fatalf("Raw packet mismatch: %x", buf)
	}
}
Exemple #5
0
func ExamplePack() {
	// Create an Ethernet packet
	eth_pkt := eth.Make()
	eth_pkt.SrcAddr, _ = net.ParseMAC("4c:72:b9:54:e5:3d")
	eth_pkt.DstAddr, _ = net.ParseMAC("ff:ff:ff:ff:ff:ff")

	// Create an ARP packet
	arp_pkt := arp.Make()
	arp_pkt.HWSrcAddr, _ = net.ParseMAC("4c:72:b9:54:e5:3d")
	arp_pkt.HWDstAddr, _ = net.ParseMAC("00:00:00:00:00:00")
	arp_pkt.ProtoSrcAddr = net.ParseIP("192.168.1.135")
	arp_pkt.ProtoDstAddr = net.ParseIP("192.168.1.254")

	buf, err := layers.Pack(eth_pkt, arp_pkt)
	if err != nil {
		log.Fatal(err)
	}

	// do something with the packet
	log.Println(buf)
}
Exemple #6
0
func TestPackEthIPv4UDP(t *testing.T) {
	eth_pkt := eth.Make()
	eth_pkt.SrcAddr, _ = net.ParseMAC(hwsrc_str)
	eth_pkt.DstAddr, _ = net.ParseMAC(hwdst_str)

	ip4_pkt := ipv4.Make()
	ip4_pkt.SrcAddr = net.ParseIP(ipsrc_str)
	ip4_pkt.DstAddr = net.ParseIP(ipdst_str)

	udp_pkt := udp.Make()
	udp_pkt.SrcPort = 41562
	udp_pkt.DstPort = 8338

	buf, err := layers.Pack(eth_pkt, ip4_pkt, udp_pkt)
	if err != nil {
		t.Fatalf("Error packing: %s", err)
	}

	if !bytes.Equal(test_eth_ipv4_udp, buf) {
		t.Fatalf("Raw packet mismatch: %x", buf)
	}
}
Exemple #7
0
func TestPackEthIPv4TCP(t *testing.T) {
	eth_pkt := eth.Make()
	eth_pkt.SrcAddr, _ = net.ParseMAC(hwsrc_str)
	eth_pkt.DstAddr, _ = net.ParseMAC(hwdst_str)

	ip4_pkt := ipv4.Make()
	ip4_pkt.SrcAddr = net.ParseIP(ipsrc_str)
	ip4_pkt.DstAddr = net.ParseIP(ipdst_str)

	tcp_pkt := tcp.Make()
	tcp_pkt.SrcPort = 41562
	tcp_pkt.DstPort = 8338
	tcp_pkt.Flags = tcp.Syn
	tcp_pkt.WindowSize = 8192

	buf, err := layers.Pack(eth_pkt, ip4_pkt, tcp_pkt)
	if err != nil {
		t.Fatalf("Error packing: %s", err)
	}

	if !bytes.Equal(test_eth_ipv4_tcp, buf) {
		t.Fatalf("Raw packet mismatch: %x", buf)
	}
}