示例#1
0
        public static UdpHeader Create(byte[] udpData, ref int bytesCopied)
        {
            UdpHeader udpPacketHeader = new UdpHeader();

            udpPacketHeader.srcPort     = BitConverter.ToUInt16(udpData, 0);
            udpPacketHeader.destPort    = BitConverter.ToUInt16(udpData, 2);
            udpPacketHeader.udpLength   = BitConverter.ToUInt16(udpData, 4);
            udpPacketHeader.udpChecksum = BitConverter.ToUInt16(udpData, 6);
            return(udpPacketHeader);
        }
示例#2
0
        private static UdpHeader BuildUDPHeader(byte[] payLoad, ushort sourcePort, ushort destPort)
        {
            Console.WriteLine("Filling out the UDP header...");
            UdpHeader udpPacket = new UdpHeader();

            udpPacket.SourcePort      = sourcePort;
            udpPacket.DestinationPort = destPort;
            udpPacket.Length          = (ushort)(UdpHeader.UdpHeaderLength + payLoad.Length);
            udpPacket.Checksum        = 0;
            return(udpPacket);
        }
示例#3
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();
                }
            }
        }