/// <summary>Sends data as an UDP datagram from a spoofed port.</summary> /// <param name="spoofedEndPoint">The spoofed local endpoint.</param> /// <param name="remoteEndPoint">The remote endpoint.</param> /// <param name="data">An array that contains the data to send.</param> public static unsafe void Send(IPEndPoint spoofedEndPoint, IPEndPoint remoteEndPoint, byte[] data) { UDPHeader header = new UDPHeader(spoofedEndPoint.Address, remoteEndPoint.Address, (ushort)spoofedEndPoint.Port, (ushort)remoteEndPoint.Port, data.Length); // compute IP header checksum uint sum = 0; for (int i = 0; i < 10; ++i) { sum += header.IPHeader.Words[i]; } while ((sum >> 16) != 0) { sum = (sum & 0xffff) + (sum >> 16); } header.IPHeader.HeaderChecksum = (ushort)~sum; // compute UDP checksum sum = 0; PseudoHeader pseudoHeader = new PseudoHeader(header); for (int i = 0; i < 10; ++i) { sum += pseudoHeader.Words[i]; fixed(byte *dataPtr = data) { for (int i = 0; i < data.Length / 2; ++i) { sum += ((ushort *)dataPtr)[i]; } } if (data.Length % 2 == 1) { sum += data[data.Length - 1]; } while ((sum >> 16) != 0) { sum = (sum & 0xffff) + (sum >> 16); } header.Checksum = (ushort)~sum; // copy header and data into datagram byte[] datagram = new byte[data.Length + sizeof(UDPHeader)]; for (int i = 0; i < sizeof(UDPHeader); ++i) { datagram[i] = ((byte *)&header)[i]; } for (int i = 0; i < data.Length; ++i) datagram[i + sizeof(UDPHeader)] = data[i]; } // send datagram using (Socket rawSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Raw)) { rawSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true); rawSocket.SendTo(datagram, remoteEndPoint); } }
public IPpacket(ushort IPLength, PacketType type) { IPHeaderLength = IPLength; packetType = type; pseudoheader = new PseudoHeader(); }