예제 #1
0
        static public Ipv4Header Create(byte[] ipv4Packet, ref int bytesCopied)
        {
            Ipv4Header ipv4Header = new Ipv4Header();

            // Make sure byte array is large enough to contain an IPv4 header
            if (ipv4Packet.Length < Ipv4Header.Ipv4HeaderLength)
            {
                return(null);
            }

            // Decode the data in the array back into the class properties
            ipv4Header.ipVersion            = (byte)((ipv4Packet[0] >> 4) & 0xF);
            ipv4Header.ipLength             = (byte)(ipv4Packet[0] & 0xF);
            ipv4Header.ipTypeOfService      = ipv4Packet[1];
            ipv4Header.ipTotalLength        = BitConverter.ToUInt16(ipv4Packet, 2);
            ipv4Header.ipId                 = BitConverter.ToUInt16(ipv4Packet, 4);
            ipv4Header.ipOffset             = BitConverter.ToUInt16(ipv4Packet, 6);
            ipv4Header.ipTtl                = ipv4Packet[8];
            ipv4Header.ipProtocol           = ipv4Packet[9];
            ipv4Header.ipChecksum           = BitConverter.ToUInt16(ipv4Packet, 10);
            ipv4Header.ipSourceAddress      = new IPAddress(BitConverter.ToUInt32(ipv4Packet, 12));
            ipv4Header.ipDestinationAddress = new IPAddress(BitConverter.ToUInt32(ipv4Packet, 16));
            bytesCopied = ipv4Header.Length;
            return(ipv4Header);
        }
예제 #2
0
 public UdpHeader() : base()
 {
     srcPort          = 0;
     destPort         = 0;
     udpLength        = 0;
     udpChecksum      = 0;
     ipv4PacketHeader = null;
 }
예제 #3
0
        private static Ipv4Header BuildIPV4Header(IPAddress sourceAddress, IPAddress destAddress, int
                                                  messageSize)
        {
            Ipv4Header ipv4Packet = new Ipv4Header();

            // Build the IPv4 header

            Console.WriteLine("Building the IPv4 header...");
            ipv4Packet.Version            = 4;
            ipv4Packet.Protocol           = (byte)ProtocolType.Udp;
            ipv4Packet.Ttl                = 2;
            ipv4Packet.Offset             = 0;
            ipv4Packet.Length             = (byte)Ipv4Header.Ipv4HeaderLength;
            ipv4Packet.TotalLength        = (ushort)System.Convert.ToUInt16(Ipv4Header.Ipv4HeaderLength + UdpHeader.UdpHeaderLength + messageSize);
            ipv4Packet.SourceAddress      = sourceAddress;
            ipv4Packet.DestinationAddress = destAddress;

            return(ipv4Packet);
        }
예제 #4
0
        /// <summary>
        /// To do checksum, need to define headers
        /// </summary>
        private static void SendUDPPacket()
        {
            Console.WriteLine("Building the packet header...");
            byte[]            builtPacket, payLoad;
            ArrayList         headerList    = new ArrayList();
            Socket            rawSocket     = null;
            SocketOptionLevel socketLevel   = SocketOptionLevel.IP;
            IPAddress         bindAddress   = IPAddress.Any;
            IPAddress         sourceAddress = IPAddress.Parse("127.0.0.1");
            IPAddress         destAddress   = IPAddress.Parse("127.0.0.1");
            ushort            sourcePort    = 5150;
            ushort            destPort      = 1234;

            while (true)
            {
                headerList.Clear();
                Console.WriteLine("\nPlease enter a message to send to server and hit enter");
                var message = Console.ReadLine();
                Console.WriteLine("You enter - " + message);
                if (string.IsNullOrWhiteSpace(message))
                {
                    Console.WriteLine("OK, Try again.");
                    continue;
                }
                Console.WriteLine("Initialize the payload...");
                payLoad = Encoding.ASCII.GetBytes(message);

                // Fill out the UDP header first
                UdpHeader  udpPacket  = BuildUDPHeader(payLoad, sourcePort, destPort);
                Ipv4Header ipv4Packet = BuildIPV4Header(sourceAddress, destAddress, payLoad.Length);
                Console.WriteLine("Setting the IPv4 header for pseudo header checksum...");
                udpPacket.ipv4PacketHeader = ipv4Packet;

                Console.WriteLine("Adding the IPv4 header to the list of header, encapsulating packet...");
                headerList.Add(ipv4Packet);

                Console.WriteLine("Adding the UDP header to the list of header, after IP header...");
                headerList.Add(udpPacket);

                Console.WriteLine("Converting the header classes into the binary...");
                builtPacket = udpPacket.BuildPacket(headerList, payLoad);

                Console.WriteLine("Message Length ={0}, Total Packet Length = {1}", payLoad.Length, builtPacket.Length);

                Console.WriteLine("Creating the raw socket using Socket()...");
                rawSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                Console.WriteLine("Binding the socket to the specified interface using Bind()...");
                rawSocket.Bind(new IPEndPoint(bindAddress, 0));

                Console.WriteLine("Setting the HeaderIncluded option for IP header...");

                try
                {
                    // Send the packet!
                    Console.WriteLine("Sending the packet...");
                    int rc = rawSocket.SendTo(builtPacket, new IPEndPoint(destAddress, destPort));
                    Console.WriteLine("send {0} bytes to {1}", rc, destAddress);

                    byte[] echoedDataFromServer = new byte[builtPacket.Length];
                    rawSocket.Receive(echoedDataFromServer);

                    Console.WriteLine("Data Received from Server {0} bytes to {1}", rc, destAddress);

                    Console.WriteLine("Sent packet's checksum = {0}", udpPacket.Checksum);

                    // offset for checksum will be 26 and checksum size is 2 bytes
                    byte[] checksumArray = new byte[2];
                    Array.Copy(echoedDataFromServer, 26, checksumArray, 0, 2);

                    var checksumOfReceivedPacket =
                        (ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(checksumArray, 0));

                    Console.WriteLine("Received packet's checksum = {0}", checksumOfReceivedPacket);

                    Console.WriteLine(udpPacket.Checksum != checksumOfReceivedPacket
                        ? "Checksum of sent packet and received packet don't match."
                        : "Checksum of sent packet and received packet match.");
                }
                catch (SocketException err)
                {
                    Console.WriteLine("Socket error occurred: {0}", err.Message);
                }
                finally
                {
                    // Close the socket
                    Console.WriteLine("Closing the socket...");
                    rawSocket.Close();
                }
            }
        }