ComputeIPChecksum() 공개 메소드

Same as computeIPChecksum(true);
public ComputeIPChecksum ( ) : int
리턴 int
        public IPv4Packet CreateIPv4Packet(EthernetPacket ethernetHeader)
        {
            int ipv4PacketTotalLength = Int32.Parse(textBoxTotalLength.Text);
            byte[] bytes = new byte[EthernetFields_Fields.ETH_HEADER_LEN + ipv4PacketTotalLength];

            IPv4Packet ipv4Packet = new IPv4Packet(
                EthernetFields_Fields.ETH_HEADER_LEN,
                bytes);

            // Ethernet fields
            ipv4Packet.EthernetHeader = ethernetHeader.Bytes;

            // IP fields
            ipv4Packet.Version = 4;
            ipv4Packet.IPHeaderLength = IPv4Fields_Fields.IP_HEADER_LEN;
            ipv4Packet.TypeOfService = Int32.Parse(textBoxDS.Text);
            ipv4Packet.IPTotalLength = ipv4PacketTotalLength;
            ipv4Packet.Id = 0;
            ipv4Packet.FragmentFlags = Int32.Parse(textBoxFlags.Text);
            ipv4Packet.FragmentOffset = 0;
            ipv4Packet.TimeToLive = Int32.Parse(textBoxTTL.Text);
            ipv4Packet.IPProtocol = (IPProtocol.IPProtocolType)((DictionaryEntry)comboBoxIPProtocols.SelectedItem).Key;
            ipv4Packet.SourceAddress = IPAddress.Parse(textBoxSourceAddress.Text);
            ipv4Packet.DestinationAddress = IPAddress.Parse(textBoxDestinationAddress.Text);

            ipv4Packet.ComputeIPChecksum(true);

            ipv4Packet.IPData = GetRandomPacketData(ipv4Packet.IPPayloadLength);

            return ipv4Packet;
        }
예제 #2
0
 /// <summary> Computes the IP checksum, optionally updating the IP checksum header.
 ///
 /// </summary>
 /// <param name="update">Specifies whether or not to update the IP checksum
 /// header after computing the checksum.  A value of true indicates
 /// the header should be updated, a value of false indicates it
 /// should not be updated.
 /// </param>
 /// <returns> The computed IP checksum.
 /// </returns>
 public int ComputeIPChecksum(bool update)
 {
     if (ipv4 != null)
     {
         return(ipv4.ComputeIPChecksum(update));
     }
     else if (ipv6 != null)
     {
         return(0); // ipv6 packets don't contain a checksum
     }
     else
     {
         throw new System.InvalidOperationException("ipv4 and ipv6 are both null");
     }
 }
        public ScanMessage Connect(IPEndPoint ipEndPoint)
        {
            // SYN packet creation

            //MAC address of gateway is provided by arp protocol
            ARP arper = new ARP(device.Name);
            arper.LocalIP = device.Interface.Addresses[0].Addr.ipAddress;
            arper.LocalMAC = device.Interface.MacAddress;
            PhysicalAddress gatewayHwAddress = arper.Resolve(gatewayAddress);

            EthernetPacket ethernetHeader = new EthernetPacket(
                device.Interface.MacAddress,
                gatewayHwAddress,
                EthernetPacketType.IPv4,
                null);

            byte[] content = new byte[
                EthernetFields_Fields.ETH_HEADER_LEN +
                IPv4Fields_Fields.IP_HEADER_LEN +
                TCPFields_Fields.TCP_HEADER_LEN];

            IPv4Packet ipv4Packet = new IPv4Packet(
                EthernetFields_Fields.ETH_HEADER_LEN,
                content);

            // Ethernet header
            ipv4Packet.EthernetHeader = ethernetHeader.Bytes;

            // IP fields
            ipv4Packet.Version = 4;
            ipv4Packet.IPHeaderLength = IPv4Fields_Fields.IP_HEADER_LEN;
            ipv4Packet.IPTotalLength = content.Length - EthernetFields_Fields.ETH_HEADER_LEN;
            ipv4Packet.Id = 100;
            ipv4Packet.TimeToLive = 20;
            ipv4Packet.IPProtocol = IPProtocol.IPProtocolType.TCP;
            ipv4Packet.SourceAddress = device.Interface.Addresses[0].Addr.ipAddress;
            ipv4Packet.DestinationAddress = ipEndPoint.Address;

            ipv4Packet.ComputeIPChecksum(true);

            TCPPacket tcpPacket = new TCPPacket(
                EthernetFields_Fields.ETH_HEADER_LEN,
                content);

            // TCP fields
            tcpPacket.SourcePort = 2222;
            tcpPacket.DestinationPort = ipEndPoint.Port;
            tcpPacket.SequenceNumber = 1000;
            tcpPacket.AcknowledgmentNumber = 1000;
            tcpPacket.Syn = true;
            tcpPacket.TCPHeaderLength = TCPFields_Fields.TCP_HEADER_LEN;
            tcpPacket.WindowSize = 555;

            // Calculate checksum
            tcpPacket.ComputeTCPChecksum(true);

            try
            {
                device.Open(false, 20);

                device.SetFilter(String.Format("ip src {0} and tcp src port {1} and tcp dst port {2}",
                    tcpPacket.DestinationAddress,
                    tcpPacket.DestinationPort,
                    tcpPacket.SourcePort));

                // Send the packet
                device.SendPacket(tcpPacket);

                TCPPacket replyPacket = null;
                bool replyReceived = false;

                Stopwatch watch = new Stopwatch();
                watch.Start();

                // Condition including timeout check.
                while (watch.ElapsedMilliseconds < timeout && replyReceived != true)
                {
                    if ((replyPacket = (TCPPacket)device.GetNextPacket()) != null)
                    {
                        replyReceived = true;
                    }
                }

                if (!replyReceived) // A reply hasn't been received
                {
                    return ScanMessage.Timeout;
                }
                else if (replyPacket.Rst) // Remote host reset the connection
                {
                    return ScanMessage.PortClosed;
                }
                else if (replyPacket.Ack) // Remote host replied with a TCP packet
                {
                    tcpPacket.Syn = false;
                    tcpPacket.Rst = true;
                    tcpPacket.WindowSize = 0;
                    tcpPacket.ComputeTCPChecksum(true);
                    device.SendPacket(tcpPacket);

                    return ScanMessage.PortOpened;
                }
                else
                {
                    return ScanMessage.Unknown;
                }
            }
            catch (Exception)
            {
                return ScanMessage.Unknown;
            }
            finally
            {
                device.Close();
            }
        }