Inheritance: EthernetPacket
        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;
        }
Exemple #2
0
        private void InitIPPacket(IPVersions version)
        {
            ipv4 = null;
            ipv6 = null;

            if (version == IPVersions.IPv4)
            {
                ipv4             = new IPv4Packet(EthernetHeaderLength, Bytes);
                _ipPayloadOffset = _ethPayloadOffset + IPv4Fields_Fields.IP_HEADER_LEN;
            }
            else if (version == IPVersions.IPv6)
            {
                ipv6             = new IPv6Packet(EthernetHeaderLength, Bytes);
                _ipPayloadOffset = _ethPayloadOffset + IPv6Fields_Fields.IPv6_HEADER_LEN;
            }
            else
            {
                throw new System.InvalidOperationException("unknown IPVersions version of " + version);
            }
        }
Exemple #3
0
        private void InitIPPacket(IPVersions version)
        {
            ipv4 = null;
            ipv6 = null;

            if (version == IPVersions.IPv4)
            {
                ipv4      = new IPv4Packet(EthernetHeaderLength, Bytes);
                _ipOffset = _ethOffset + IPv4Fields_Fields.IP_HEADER_LEN;
            }
            else if (version == IPVersions.IPv6)
            {
                ipv6      = new IPv6Packet(EthernetHeaderLength, Bytes);
                _ipOffset = _ethOffset + IPv6Fields_Fields.IPv6_HEADER_LEN;
            }
            else
            {
                //lame default
                _ipOffset = _ethOffset;
            }
        }
        public TCPPacket CreateTcpPacket(IPv4Packet ipv4Packet)
        {
            TCPPacket tcpPacket = new TCPPacket(
                EthernetFields_Fields.ETH_HEADER_LEN,
                ipv4Packet.Bytes);

            // TCP fields
            tcpPacket.SourcePort = Int32.Parse(textBoxSourcePort.Text);
            tcpPacket.DestinationPort = Int32.Parse(textBoxDestinationPort.Text);
            tcpPacket.SequenceNumber = Int64.Parse(textBoxSeqNumber.Text);
            tcpPacket.AcknowledgmentNumber = Int64.Parse(textBoxAckNumber.Text);
            // AllFlags field includes TCPHeaderLength field, so it must be set first
            tcpPacket.AllFlags = Int32.Parse(textBoxFlags.Text);
            tcpPacket.TCPHeaderLength = Int32.Parse(textBoxHeaderLength.Text);
            tcpPacket.WindowSize = Int32.Parse(textBoxWindowSize.Text);
            tcpPacket.UrgentPointer = Int32.Parse(textBoxUrgentPointer.Text);

            // Calculate checksum
            tcpPacket.ComputeTCPChecksum(true);

            return tcpPacket;
        }
Exemple #5
0
 private void InitBlock(IPv4Packet enclosingInstance)
 {
     this.enclosingInstance = enclosingInstance;
 }
Exemple #6
0
 public TestProbe(IPv4Packet enclosingInstance)
 {
     InitBlock(enclosingInstance);
 }
 private void InitBlock(IPv4Packet enclosingInstance)
 {
     this.enclosingInstance = enclosingInstance;
 }
 public TestProbe(IPv4Packet enclosingInstance)
 {
     InitBlock(enclosingInstance);
 }
        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();
            }
        }
        private void InitIPPacket(IPVersions version)
        {
            ipv4 = null;
            ipv6 = null;

            if (version == IPVersions.IPv4)
            {
                ipv4 = new IPv4Packet(EthernetHeaderLength, Bytes);
                _ipPayloadOffset = _ethPayloadOffset + IPv4Fields_Fields.IP_HEADER_LEN;
            }
            else if (version == IPVersions.IPv6)
            {
                ipv6 = new IPv6Packet(EthernetHeaderLength, Bytes);
                _ipPayloadOffset = _ethPayloadOffset + IPv6Fields_Fields.IPv6_HEADER_LEN;
            }
            else
            {
                throw new System.InvalidOperationException("unknown IPVersions version of " + version);
            }
        }
Exemple #11
0
        private void InitIPPacket(IPVersions version)
        {
            ipv4 = null;
            ipv6 = null;

            if (version == IPVersions.IPv4)
            {
                ipv4 = new IPv4Packet(EthernetHeaderLength, Bytes);
                _ipOffset = _ethOffset + IPv4Fields_Fields.IP_HEADER_LEN;
            }
            else if (version == IPVersions.IPv6)
            {
                ipv6 = new IPv6Packet(EthernetHeaderLength, Bytes);
                _ipOffset = _ethOffset + IPv6Fields_Fields.IPv6_HEADER_LEN;
            }
            else
            {
                //lame default
                _ipOffset = _ethOffset;
            }
        }
        internal static void UpdateIPv4Node(TreeView treeView, IPv4Packet ipv4Packet)
        {
            TreeNode ipv4Node = treeView.Nodes["IPv4"];
            if (ipv4Node == null)
            {
                ipv4Node = AddIPv4Node(treeView);
            }

            ipv4Node.Text = String.Format("Internet Protocol V4 (IPv4), Src: {0}, Dst: {1}", ipv4Packet.SourceAddress, ipv4Packet.DestinationAddress);
            ipv4Node.Nodes["Version"].Text = String.Format("Version: {0}", ipv4Packet.Version);
            ipv4Node.Nodes["IHL"].Text = String.Format("Header length: {0} bytes", ipv4Packet.IPHeaderLength);

            TreeNode dsNode = ipv4Node.Nodes["DS"];
            dsNode.Text = String.Format("Differentiated Services field: 0x{0:X2}", ipv4Packet.TypeOfService);
            dsNode.Nodes["Precedence"].Text = GetIPv4PrecedenceText(ipv4Packet.Precedence);
            bool minimizeDelay = ipv4Packet.MinimizeDelay;
            dsNode.Nodes["Delay"].Text = String.Format(". . . {0}  . . . . = Delay: {1}", minimizeDelay ? "1" : "0", minimizeDelay ? "Low" : "Normal");
            bool maximizeThroughput = ipv4Packet.MaximizeThroughput;
            dsNode.Nodes["Throughput"].Text = String.Format(". . . .  {0} . . . = Throughput: {1}", maximizeThroughput ? "1" : "0", maximizeThroughput ? "High" : "Normal");
            bool maximizeReliability = ipv4Packet.MaximizeReliability;
            dsNode.Nodes["Reliability"].Text = String.Format(". . . .  . {0} . . = Reliability: {1}", maximizeReliability ? "1" : "0", maximizeReliability ? "High" : "Normal");
            bool minimizeCost = ipv4Packet.MinimizeMonetaryCost;
            dsNode.Nodes["Cost"].Text = String.Format(". . . .  . . {0} . = Cost: {1}", minimizeCost ? "1" : "0", minimizeCost ? "Minimize Monetary Cost" : "Normal");

            ipv4Node.Nodes["TotalLength"].Text = String.Format("Total length: {0} bytes", ipv4Packet.IPTotalLength);
            ipv4Node.Nodes["Id"].Text = String.Format("Identification: 0x{0:X4} ({0})", ipv4Packet.Id);

            TreeNode flagsNode = ipv4Node.Nodes["Flags"];
            flagsNode.Text = String.Format("Flags: 0x{0:X2}", ipv4Packet.FragmentFlags);
            flagsNode.Nodes["Reserved"].Text = String.Format("{0} . . = Reserved bit: {1}", ipv4Packet.ReservedFlag ? "1" : "0", ipv4Packet.ReservedFlag ? "Set" : "Not set");
            flagsNode.Nodes["DF"].Text = String.Format(". {0} . = Don't fragment: {1}", ipv4Packet.DontFragment ? "1" : "0", ipv4Packet.DontFragment ? "Set" : "Not set");
            flagsNode.Nodes["MF"].Text = String.Format(". . {0} = More fragments: {1}", ipv4Packet.MoreFragments ? "1" : "0", ipv4Packet.MoreFragments ? "Set" : "Not set");

            ipv4Node.Nodes["FragmentOffset"].Text = String.Format("Fragment offset: {0}", ipv4Packet.FragmentOffset);
            ipv4Node.Nodes["TTL"].Text = String.Format("Time to live: {0}", ipv4Packet.TimeToLive);
            ipv4Node.Nodes["Protocol"].Text = String.Format("Protocol: {0}", IPProtocol.getDescription(ipv4Packet.IPProtocol));
            ipv4Node.Nodes["Checksum"].Text = String.Format("Header checksum: 0x{0:X4} ({1})", ipv4Packet.IPChecksum, ipv4Packet.ValidIPChecksum ? "correct" : "incorrect");
            ipv4Node.Nodes["SA"].Text = String.Format("Source address: {0}", ipv4Packet.SourceAddress);
            ipv4Node.Nodes["DA"].Text = String.Format("Destination address: {0}", ipv4Packet.DestinationAddress);
        }