Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.2 kB
4
Indexable
func prova(conn *connbuilder.Conn, msg *message.Message) {
	var err error
	m := []byte{1, 2, 3, 4}
	udpLayer := &layers.UDP{
		SrcPort:  layers.UDPPort(1),
		DstPort:  layers.UDPPort(2),
		Length:   3,
		Checksum: 0xdddd,
	}
	originalIPv6Layer := &layers.IPv6{
		Version:      6,
		TrafficClass: 0xab,
		FlowLabel:    1,
		Length:       0xdddd,
		NextHeader:   layers.IPProtocolUDP,
		HopLimit:     60,
		SrcIP:        net.IP{0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
		DstIP:        net.IP{0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
		HopByHop:     nil,
	}
	
	if err = udpLayer.SetNetworkLayerForChecksum(originalIPv6Layer); err != nil {
		fmt.Printf("set network layer udp error: %v\n", err)
		return
	}
	icmpLayer := &layers.ICMPv6{
		TypeCode: layers.CreateICMPv6TypeCode(layers.ICMPv6TypeDestinationUnreachable, layers.ICMPv6CodePortUnreachable),
		Checksum: 0xdddd,
	}
	newIPv6Layer := &layers.IPv6{
		Version:      6,
		TrafficClass: 0xab,
		FlowLabel:    1,
		Length:       0xdddd,
		NextHeader:   layers.IPProtocolUDP,
		HopLimit:     60,
		SrcIP:        net.IP{0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
		DstIP:        net.IP{0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
		HopByHop:     nil,
	}
	err = icmpLayer.SetNetworkLayerForChecksum(newIPv6Layer)
	if err != nil {
		fmt.Printf("set network layer icmp error: %v\n", err)
		return
	}
	ethernetLayer := &layers.Ethernet{
		SrcMAC:       net.HardwareAddr{0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa},
		DstMAC:       net.HardwareAddr{0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb},
		EthernetType: layers.EthernetTypeIPv6,
	}

	buffer := gopacket.NewSerializeBuffer()
	options := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}
	if err = gopacket.SerializeLayers(buffer, options,
		ethernetLayer,
		newIPv6Layer,
		icmpLayer,
		originalIPv6Layer,
		udpLayer,
		gopacket.Payload(m),
	); err != nil {
		fmt.Println(err)
		return
	}

	str := hex.EncodeToString(buffer.Bytes())
	fmt.Println(str)
}