Create() 공개 정적인 메소드

This routine creates an instance of the IcmpHeader class from a byte array that is a received IGMP packet. This is useful when a packet is received from the network and the header object needs to be constructed from those values.
public static Create ( byte icmpPacket, int &bytesCopied ) : IcmpHeader,
icmpPacket byte Byte array containing the binary ICMP header
bytesCopied int Number of bytes used in header
리턴 IcmpHeader,
예제 #1
0
        /// <summary>
        /// This is the asynchronous callback that is fired when an async ReceiveFrom.
        /// An asynchronous ReceiveFrom is posted by calling BeginReceiveFrom. When this
        /// function is invoked, it calculates the elapsed time between when the ping
        /// packet was sent and when it was completed.
        /// </summary>
        /// <param name="ar">Asynchronous context for operation that completed</param>
        static void PingReceiveCallback(IAsyncResult ar)
        {
            RawSocketPing rawSock = (RawSocketPing)ar.AsyncState;
            TimeSpan      elapsedTime;
            int           bytesReceived = 0;
            ushort        receivedId    = 0;

            try
            {
                // Keep a count of how many async operations are outstanding -- one just completed
                //    so decrement the count.
                Interlocked.Decrement(ref rawSock.pingOutstandingReceives);

                // If we're done because ping is exiting and the socket has been closed,
                //    set the done event
                if (rawSock.pingSocket == null)
                {
                    if (rawSock.pingOutstandingReceives == 0)
                    {
                        rawSock.pingDoneEvent.Set();
                    }
                    return;
                }

                // Complete the receive op by calling EndReceiveFrom. This will return the number
                //    of bytes received as well as the source address of who sent this packet.
                bytesReceived = rawSock.pingSocket.EndReceiveFrom(ar, ref rawSock.castResponseEndPoint);

                // Calculate the elapsed time from when the ping request was sent and a response was
                //    received.
                elapsedTime = DateTime.Now - rawSock.pingSentTime;

                rawSock.responseEndPoint = (IPEndPoint)rawSock.castResponseEndPoint;

                // Here we unwrap the data received back into the respective protocol headers such
                //    that we can find the ICMP ID in the ICMP or ICMPv6 packet to verify that
                //    the echo response we received was really a response to our request.
                if (rawSock.pingSocket.AddressFamily == AddressFamily.InterNetwork)
                {
                    Ipv4Header v4Header;
                    IcmpHeader icmpv4Header;
                    byte[]     pktIcmp;
                    int        offset = 0;

                    // Remember, raw IPv4 sockets will return the IPv4 header along with all
                    //    subsequent protocol headers
                    v4Header = Ipv4Header.Create(rawSock.receiveBuffer, ref offset);
                    pktIcmp  = new byte[bytesReceived - offset];
                    Array.Copy(rawSock.receiveBuffer, offset, pktIcmp, 0, pktIcmp.Length);
                    icmpv4Header = IcmpHeader.Create(pktIcmp, ref offset);

                    /*Console.WriteLine("Icmp.Id = {0}; Icmp.Sequence = {1}",
                     *  icmpv4Header.Id,
                     *  icmpv4Header.Sequence
                     *  );*/

                    receivedId = icmpv4Header.Id;
                }
                else if (rawSock.pingSocket.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    Icmpv6Header      icmp6Header;
                    Icmpv6EchoRequest echoHeader;
                    byte[]            pktEchoRequest;
                    int offset = 0;

                    // For IPv6 raw sockets, the IPv6 header is never returned along with the
                    //    data received -- the received data always starts with the header
                    //    following the IPv6 header.
                    icmp6Header    = Icmpv6Header.Create(rawSock.receiveBuffer, ref offset);
                    pktEchoRequest = new byte[bytesReceived - offset];
                    Array.Copy(rawSock.receiveBuffer, offset, pktEchoRequest, 0, pktEchoRequest.Length);
                    echoHeader = Icmpv6EchoRequest.Create(pktEchoRequest, ref offset);

                    /*Console.WriteLine("Icmpv6.Id = {0}; Icmp.Sequence = {1}",
                     *  echoHeader.Id,
                     *  echoHeader.Sequence
                     *  );*/

                    receivedId = echoHeader.Id;
                }

                if (receivedId == rawSock.pingId)
                {
                    string elapsedString;

                    // Print out the usual statistics for ping
                    if (elapsedTime.Milliseconds < 1)
                    {
                        elapsedString = "<1";
                    }
                    else
                    {
                        elapsedString = "=" + elapsedTime.Milliseconds.ToString();
                    }

                    Console.WriteLine("Reply from {0}: byte={1} time{2}ms TTL={3} ",
                                      rawSock.responseEndPoint.Address.ToString(),
                                      bytesReceived,
                                      elapsedString,
                                      rawSock.pingTtl
                                      );
                }

                // Post another async receive if the count indicates for us to do so.
                if (rawSock.pingCount > 0)
                {
                    rawSock.pingSocket.BeginReceiveFrom(
                        rawSock.receiveBuffer,
                        0,
                        rawSock.receiveBuffer.Length,
                        SocketFlags.None,
                        ref rawSock.castResponseEndPoint,
                        rawSock.receiveCallback,
                        rawSock
                        );

                    // Keep track of outstanding async operations
                    Interlocked.Increment(ref rawSock.pingOutstandingReceives);
                }
                else
                {
                    // If we're done then set the done event
                    if (rawSock.pingOutstandingReceives == 0)
                    {
                        rawSock.pingDoneEvent.Set();
                    }
                }
                // If this is indeed the response to our echo request then signal the main thread
                //    that we received the response so it can send additional echo requests if
                //    necessary. This is done after another async ReceiveFrom is already posted.
                if (receivedId == rawSock.pingId)
                {
                    rawSock.pingReceiveEvent.Set();
                }
            }
            catch (SocketException err)
            {
                Console.WriteLine("Socket error occurred in async callback: {0}", err.Message);
            }
        }
예제 #2
0
        public static void WireguardTest()
        {
            var protocol = new Protocol(
                HandshakePattern.IK,
                CipherFunction.ChaChaPoly,
                HashFunction.Blake2s,
                PatternModifiers.Psk2
                );
            var buffer     = new byte[Protocol.MaxMessageLength];
            var buffer2    = new byte[Protocol.MaxMessageLength];
            int bufferRead = 0;

            using (var hs = protocol.Create(true,
                                            new ReadOnlySpan <byte>(Encoding.UTF8.GetBytes("WireGuard v1 zx2c4 [email protected]")),
                                            OurPrivate,
                                            TheirPublic,
                                            new byte[][] { Preshared }))
            {
                var now    = DateTimeOffset.UtcNow; //replace with Noda.Time?
                var tai64n = new byte[12];
                (4611686018427387914ul + (ulong)now.ToUnixTimeSeconds()).ToBigEndian(tai64n);
                ((uint)(now.Millisecond * 1e6)).ToBigEndian(tai64n, 8);

                var initiationPacket = new List <byte> {
                    1, 0, 0, 0
                };                                                              //type initiation
                initiationPacket.AddRange(((uint)28).ToLittleEndian());         //sender, random 4byte

                var(bytesWritten, _, _) = hs.WriteMessage(tai64n, buffer);
                initiationPacket.AddRange(buffer.Take(bytesWritten));           // should be 24byte, ephemeral, static, timestamp

                var hasher   = Blake2s.CreateIncrementalHasher(32);
                var hashThis = Encoding.UTF8.GetBytes("mac1----").Concat(TheirPublic).ToArray();
                hasher.Update(hashThis);
                var finishedHash = hasher.Finish();
                hasher   = Blake2s.CreateIncrementalHasher(16, finishedHash);
                hashThis = initiationPacket.ToArray();
                hasher.Update(hashThis);

                finishedHash = hasher.Finish();
                initiationPacket.AddRange(finishedHash);                        //mac1
                initiationPacket.AddRange(Enumerable.Repeat((byte)0, 16));      //mac2 = zeros if no cookie last received


                var socket         = new DatagramSocket();
                var responsePacket = new TaskCompletionSource <int>();
                var autoResetEvent = new AutoResetEvent(false);
                socket.MessageReceived += (sender, args) =>
                {
                    bufferRead = args.GetDataStream().AsStreamForRead().Read(buffer);
                    autoResetEvent.Set();
                };
                socket.ConnectAsync(new HostName("demo.wireguard.com"), "12913").AsTask().Wait();
                var streamWriter = new BinaryWriter(socket.OutputStream.AsStreamForWrite());
                streamWriter.Write(initiationPacket.ToArray());
                streamWriter.Flush();

                var successful = autoResetEvent.WaitOne(5000);
                if (!successful)
                {
                    return;
                }

                if (buffer[0] != 2)   //type init response
                {
                    return;           //"response packet type wrong: want %d, got %d"
                }
                if (bufferRead != 92) //always this length! for type=2
                {
                    return;           //"response packet too short: want %d, got %d"
                }
                if (buffer[1] != 0 || buffer[2] != 0 || buffer[3] != 0)
                {
                    return; //"response packet has non-zero reserved fields"
                }
                var theirIndex = buffer.LittleEndianToUInt32(4);
                var ourIndex   = buffer.LittleEndianToUInt32(8);
                if (ourIndex != 28)
                {
                    return; //log.Fatalf("response packet index wrong: want %d, got %d", 28, ourIndex)
                }
                var span = new Span <byte>(buffer);
                var(bytesRead, handshakeHash, transport) = hs.ReadMessage(span.Slice(12, 48),
                                                                          span.Slice(100)); //write on same buffer behind the received package (which
                if (bytesRead != 0)
                {
                    return; //"unexpected payload: %x"
                }
                var icmpHeader = new IcmpHeader()
                {
                    Type = 8, Id = 921, Sequence = 438
                };
                var pingMessage = icmpHeader.GetProtocolPacketBytes(Encoding.UTF8.GetBytes("WireGuard"));
                var pingHeader  = new Ipv4Header()
                {
                    Version            = 4, Length = 20, TotalLength = (ushort)(20 + pingMessage.Length),
                    Protocol           = 1, Ttl = 20,
                    SourceAddress      = new IPAddress(new byte[] { 10, 189, 129, 2 }),
                    DestinationAddress = new IPAddress(new byte[] { 10, 189, 129, 1 })
                }.GetProtocolPacketBytes(new byte[0]);


                span[0] = 4;
                span.Slice(1, 3).Assign((byte)0);
                theirIndex.ToLittleEndian(buffer, 4);
                0L.ToLittleEndian(buffer, 8);                                                        //this is the counter, little endian u64
                bytesWritten = transport.WriteMessage(
                    pingHeader.Concat(pingMessage).Concat(Enumerable.Repeat((byte)0, 11)).ToArray(), //pad message with 0 to make mod 16=0
                    span.Slice(16));

                //using (var streamWriter = new BinaryWriter(socket.OutputStream.AsStreamForWrite()))
                streamWriter.Write(span.Slice(0, 16 + bytesWritten).ToArray());
                streamWriter.Flush();
                successful = autoResetEvent.WaitOne(5000);
                if (!successful)
                {
                    return;
                }

                if (buffer[0] != 4)
                {
                    return;//"response packet type wrong: want %d, got %d"
                }
                if (buffer[1] != 0 || buffer[2] != 0 || buffer[3] != 0)
                {
                    return; //"response packet has non-zero reserved fields"
                }
                var replyPacket = buffer2.AsSpan(0, transport.ReadMessage(span.Slice(16, bufferRead - 16), buffer2));
                if (replyPacket.Length != 48)
                {
                    return;
                }

                var replyHeaderLen     = ((int)(replyPacket[0] & 0x0f)) << 2;
                var replyLen           = buffer2.BigEndianToUInt16(2);
                var our_index_received = buffer.LittleEndianToUInt32(4);
                if (our_index_received != 28)
                {
                    return;
                }
                var nonce = buffer2.LittleEndianToUInt64(8);
                //if (nonce != 0)//not parsed correctly?
                //    return;
                var replyMessage = IcmpHeader.Create(buffer2.AsSpan(replyHeaderLen, replyLen - replyHeaderLen).ToArray(), ref bytesRead);
                if (replyMessage.Type != 0 || replyMessage.Code != 0)
                {
                    return;
                }
                if (replyMessage.Id != 921 || replyMessage.Sequence != 438)
                {
                    return;
                }
                var replyPayload = Encoding.UTF8.GetString(buffer2.AsSpan(replyLen - replyHeaderLen + bytesRead, replyHeaderLen - bytesRead));
                if (replyPayload != "WireGuard") //trim necessary?
                {
                    return;
                }
            }
        }