public void Send(byte[] buffer, int length) { // make sure that a default destination IPEndpoint has been configured. if ((destinationIpAddress == IPv6Any) || (destinationPort == IPPortAny)) { throw new SocketsException("Socket is not connected."); } UdpDatagram udpDatagram = new UdpDatagram(); udpDatagram.DestinationPort = destinationPort; udpDatagram.SourcePort = sourcePort; udpDatagram.AddPayload(buffer); udpDatagram.Checksum = 0; IPv6Packet packetUDP = new IPv6Packet(); packetUDP.SourceAddress = sourceIpAddress; packetUDP.DestinationAddress = destinationIpAddress; packetUDP.NextHeader = IPv6Protocol.Udp; packetUDP.Payload = udpDatagram; packetUDP.PayloadLength = udpDatagram.Length; IPv6PseudoHeader ipv6PseudoHeader = new IPv6PseudoHeader(packetUDP.SourceAddress, packetUDP.DestinationAddress, packetUDP.PayloadLength, (byte)packetUDP.NextHeader); ushort checkSum = ipv6PseudoHeader.GetCheckSum(); checkSum = NetUtilities.ComputeChecksum(checkSum, udpDatagram.ToBytes(), true); udpDatagram.Checksum = checkSum; NetworkingInterface.Send(packetUDP.ToBytes()); }
public static void PacketHandler(IPv6Packet ipv6Packet) { Icmpv6Packet icmpv6Handler = (Icmpv6Packet)ipv6Packet.Payload; if (icmpv6Handler.Icmpv6MessageType == Icmpv6MessageType.EchoRequest) { IPv6Packet packetEchoReply = new IPv6Packet(); packetEchoReply.SourceAddress = ipv6Packet.DestinationAddress; packetEchoReply.DestinationAddress = ipv6Packet.SourceAddress; packetEchoReply.NextHeader = ipv6Packet.NextHeader; packetEchoReply.Flow = ipv6Packet.Flow; packetEchoReply.HopLimit = ipv6Packet.HopLimit; packetEchoReply.PayloadLength = ipv6Packet.PayloadLength; packetEchoReply.TrafficClass = ipv6Packet.TrafficClass; packetEchoReply.Version = ipv6Packet.Version; icmpv6Handler.Icmpv6MessageType = Icmpv6MessageType.EchoReply; Icmpv6EchoReply icmpv6EchoReply = new Icmpv6EchoReply((Icmpv6EchoRequest)icmpv6Handler.IcmpMessage); icmpv6Handler.IcmpMessage = icmpv6EchoReply; IPv6PseudoHeader ipv6PseudoHeader = new IPv6PseudoHeader(packetEchoReply.SourceAddress, packetEchoReply.DestinationAddress, packetEchoReply.PayloadLength, (byte)packetEchoReply.NextHeader); ushort checkSum = ipv6PseudoHeader.GetCheckSum(); byte[] icmpData = icmpv6Handler.ToBytes(); icmpData[2] = 0; icmpData[3] = 0; checkSum = NetUtilities.ComputeChecksum(checkSum, icmpData, true); icmpv6Handler.Checksum = checkSum; packetEchoReply.Payload = icmpv6Handler; NetworkingInterface.Send(packetEchoReply.ToBytes()); } else if (icmpv6Handler.Icmpv6MessageType == Icmpv6MessageType.EchoReply) { if (isResponseTimeout) { return; } TimeSpan elapsed = DateTime.Now - pingStart; replyTime = (short)elapsed.TotalMilliseconds; pingTimeout.Set(); } }