예제 #1
0
        protected bool check_checksum(UInt32 ip_source_in_network_order, UInt32 ip_destination_in_network_order, ref byte[] incoming_udp_data, int incoming_udp_data_size)
        {
            if (incoming_udp_data == null)
            {
                return(false);
            }
            if (incoming_udp_data_size < udp_header.size)
            {
                return(false);
            }
            UInt16 packet_checksum = System.BitConverter.ToUInt16(incoming_udp_data, 6);

            byte[] udp_packet_and_pseudo_header = new byte[incoming_udp_data_size + udp_pseudo_header.size];

            udp_pseudo_header uph = new udp_pseudo_header();

            uph.destination_address = ip_source_in_network_order;
            uph.source_address      = ip_destination_in_network_order;
            uph.protocol            = easy_socket.ip_header.ipv4_header.protocol_udp;
            uph.UdpLength           = (ushort)incoming_udp_data_size;
            byte[] encoded_pseudo_header = uph.encode();
            System.Array.Copy(encoded_pseudo_header, 0, udp_packet_and_pseudo_header, 0, udp_pseudo_header.size);
            System.Array.Copy(incoming_udp_data, 0, udp_packet_and_pseudo_header, udp_pseudo_header.size, incoming_udp_data_size);
            // putting the checksum to 0
            udp_packet_and_pseudo_header[6 + udp_pseudo_header.size] = 0;
            udp_packet_and_pseudo_header[7 + udp_pseudo_header.size] = 0;
            UInt16 computed_checksum = easy_socket.Cchecksum.checksum(udp_packet_and_pseudo_header, true);

            return(computed_checksum == packet_checksum);
        }
예제 #2
0
        public byte[] encode(UInt32 ip_source_in_network_order, UInt32 ip_destination_in_network_order)
        {
            int data_size = 0;

            if (data != null)
            {
                data_size = this.data.Length;
            }

            byte[] ret = new byte[udp_header.size + data_size];
            System.Array.Copy(System.BitConverter.GetBytes(this.source_port), 0, ret, 0, 2);
            System.Array.Copy(System.BitConverter.GetBytes(this.destination_port), 0, ret, 2, 2);
            // if udp_length not set comput it if this.data
            if (!this.b_udp_length_set)
            {
                this.UdpLength = (ushort)(udp_header.size + data_size);
            }
            System.Array.Copy(System.BitConverter.GetBytes(this.udp_length), 0, ret, 4, 2);
            // if checksum not set put it to 0
            if (!this.b_checksum_set)
            {
                this.checksum = 0;
            }
            System.Array.Copy(System.BitConverter.GetBytes(this.checksum), 0, ret, 6, 2);
            // copy data
            if (data_size > 0)
            {
                System.Array.Copy(this.data, 0, ret, 8, data_size);
            }

            if (!this.b_checksum_set)
            {
                // comput checksum
                // create udp_pseudo_header
                udp_pseudo_header udpph = new udp_pseudo_header();
                udpph.destination_address = ip_destination_in_network_order;
                udpph.source_address      = ip_source_in_network_order;
                udpph.udp_length          = this.udp_length;
                // create a byte array containing udp_pseudo_header and the datagram
                byte[] full_array_for_checksum = new byte[udp_pseudo_header.size + udp_header.size + data_size];
                System.Array.Copy(udpph.encode(), 0, full_array_for_checksum, 0, udp_pseudo_header.size);
                System.Array.Copy(ret, 0, full_array_for_checksum, udp_pseudo_header.size, udp_header.size + data_size);
                // comput checksum
                this.checksum = easy_socket.Cchecksum.checksum(full_array_for_checksum, true);
                // put new check some in udp datagram
                System.Array.Copy(System.BitConverter.GetBytes(this.checksum), 0, ret, 6, 2);
            }
            return(ret);
        }