예제 #1
0
        public static void Run(string[] args)
        {
            int lLen = EthernetFields_Fields.ETH_HEADER_LEN;
            const int MIN_PKT_LEN = 42;
            byte[] data = System.Text.Encoding.ASCII.GetBytes("HELLO");
            byte[] bytes = new byte[MIN_PKT_LEN + data.Length];
            Array.Copy(data, 0, bytes, MIN_PKT_LEN, data.Length);

            List<PcapDevice> devices = Pcap.GetAllDevices();
            PcapDevice device = devices[2];

            UDPPacket packet = new UDPPacket(lLen, bytes);

            //Ethernet Fields
            packet.DestinationHwAddress = PhysicalAddress.Parse("001122334455");
            // NOTE: the source hw address will be filled in by the network stack or the
            //       network hardware
            //          packet.SourceHwAddress = device.MacAddress;
            packet.EthernetProtocol = EthernetPacket.EtherType.IP;

            //IP Fields
            packet.DestinationAddress = System.Net.IPAddress.Parse("58.100.187.167");

            // NOTE: the source address will be filled in by the network stack based on
            //       the device used for sending
            //          packet.SourceAddress = System.Net.IPAddress.Parse(device.IpAddress);
            packet.IPProtocol = IPProtocol.IPProtocolType.UDP;
            packet.TimeToLive = 20;
            packet.ipv4.Id = 100;
            packet.IPVersion = IPPacket.IPVersions.IPv4;
            packet.ipv4.IPTotalLength = bytes.Length - lLen;
            packet.ipv4.IPHeaderLength = IPv4Fields_Fields.IP_HEADER_LEN;

            //UDP Fields
            packet.DestinationPort = 9898;
            packet.SourcePort = 80;
            //TODO: checksum methods are disabled due to unfinished ipv4/ipv6 work
            throw new System.NotImplementedException();
            //          packet.ComputeIPChecksum();
            //          packet.ComputeUDPChecksum();

            device.Open();
            device.SendPacket(packet);
            device.Close();
        }
예제 #2
0
        /// <summary> Convert captured packet data into an object.</summary>
        public static Packet dataToPacket(LinkLayers linkType, byte[] bytes, Timeval tv)
        {
            EthernetPacketType ethProtocol;

            // retrieve the length of the headers associated with this link layer type.
            // this length is the offset to the header embedded in the packet.
            int byteOffsetToEthernetPayload = LinkLayer.LinkLayerLength(linkType);

            // extract the protocol code for the type of header embedded in the
            // link-layer of the packet
            int offset = LinkLayer.ProtocolOffset(linkType);
            if (offset == -1)
            {
                // if there is no embedded protocol, assume IpV4
                ethProtocol = EthernetPacketType.IPv4;
            }
            else
            {
                ethProtocol = (EthernetPacketType)ArrayHelper.extractInteger(bytes, offset, EthernetFields_Fields.ETH_CODE_LEN);
            }

            string errorString;
            Packet parsedPacket = null;
            try
            {
                // try to recognize the ethernet type..
                switch (ethProtocol)
                {
                    // arp
                    case EthernetPacketType.ARP:
                        parsedPacket = new ARPPacket(byteOffsetToEthernetPayload, bytes, tv);
                        break;

                    case EthernetPacketType.IPv6:
                    case EthernetPacketType.IPv4:
                        try
                        {
                            // ethernet level code is recognized as IP, figure out what kind..
                            int ipProtocol = IPProtocol.extractProtocol(byteOffsetToEthernetPayload, bytes);
                            switch (ipProtocol)
                            {
                                case (int)IPProtocol.IPProtocolType.ICMP:
                                    parsedPacket = new ICMPPacket(byteOffsetToEthernetPayload, bytes, tv);
                                    break;

                                case (int)IPProtocol.IPProtocolType.IGMP:
                                    parsedPacket = new IGMPPacket(byteOffsetToEthernetPayload, bytes, tv);
                                    break;

                                case (int)IPProtocol.IPProtocolType.TCP:
                                    parsedPacket = new TCPPacket(byteOffsetToEthernetPayload, bytes, tv);
                                    break;

                                case (int)IPProtocol.IPProtocolType.UDP:
                                    parsedPacket = new UDPPacket(byteOffsetToEthernetPayload, bytes, tv);
                                    break;

                                // unidentified ip..
                                default:
                                    parsedPacket = new IPPacket(byteOffsetToEthernetPayload, bytes, tv);
                                    break;
                            }

                            // check that the parsed packet is valid
                            if (!parsedPacket.IsValid(out errorString))
                            {
                                throw new PcapException(errorString);
                            }
                            else
                            {
                                return parsedPacket;
                            }
                        }
                        catch
                        {
                            // error parsing the specific ip packet type, parse as a generic IPPacket
                            parsedPacket = new IPPacket(byteOffsetToEthernetPayload, bytes, tv);

                            // check that the parsed packet is valid
                            if (!parsedPacket.IsValid(out errorString))
                            {
                                throw new PcapException(errorString);
                            }
                            else
                            {
                                return parsedPacket;
                            }
                        }

                    // ethernet level code not recognized, default to anonymous packet..
                    default:
                        parsedPacket = new EthernetPacket(byteOffsetToEthernetPayload, bytes, tv);
                        break;
                }

                return parsedPacket;
            }
            catch
            {
                // we know we have at least an ethernet packet, so return that
                return new EthernetPacket(byteOffsetToEthernetPayload, bytes, tv);
            }
        }
        internal static void UpdateUdpNode(TreeView treeView, UDPPacket udpPacket)
        {
            TreeNode udpNode = treeView.Nodes["UDP"];
            if (udpNode == null)
            {
                udpNode = AddUdpNode(treeView);
            }

            udpNode.Text = String.Format("User Datagram Protocol, Src port: {0}, Dst port: {1}", udpPacket.SourcePort, udpPacket.DestinationPort);
            udpNode.Nodes["SrcPort"].Text = String.Format("Source port: {0}", udpPacket.SourcePort);
            udpNode.Nodes["DstPort"].Text = String.Format("Destination port: {0}", udpPacket.DestinationPort);
            udpNode.Nodes["Length"].Text = String.Format("Length: {0} bytes", udpPacket.Length);
            udpNode.Nodes["Checksum"].Text = String.Format("Checksum: 0x{0:X4} ({1})", udpPacket.UDPChecksum, udpPacket.ValidUDPChecksum ? "correct" : "incorrect");
        }
예제 #4
0
        /// <summary> Convert captured packet data into an object.</summary>
        public static Packet dataToPacket(LinkLayers linkType, byte[] bytes, Timeval tv)
        {
            EthernetPacketType ethProtocol;

            // retrieve the length of the headers associated with this link layer type.
            // this length is the offset to the header embedded in the packet.
            int byteOffsetToEthernetPayload = LinkLayer.LinkLayerLength(linkType);

            // extract the protocol code for the type of header embedded in the
            // link-layer of the packet
            int offset = LinkLayer.ProtocolOffset(linkType);

            if (offset == -1)
            {
                // if there is no embedded protocol, assume IpV4
                ethProtocol = EthernetPacketType.IPv4;
            }
            else
            {
                ethProtocol = (EthernetPacketType)ArrayHelper.extractInteger(bytes, offset, EthernetFields_Fields.ETH_CODE_LEN);
            }

            string errorString;
            Packet parsedPacket = null;

            try
            {
                // try to recognize the ethernet type..
                switch (ethProtocol)
                {
                // arp
                case EthernetPacketType.ARP:
                    parsedPacket = new ARPPacket(byteOffsetToEthernetPayload, bytes, tv);
                    break;

                case EthernetPacketType.IPv6:
                case EthernetPacketType.IPv4:
                    try
                    {
                        // ethernet level code is recognized as IP, figure out what kind..
                        int ipProtocol = IPProtocol.extractProtocol(byteOffsetToEthernetPayload, bytes);
                        switch (ipProtocol)
                        {
                        case (int)IPProtocol.IPProtocolType.ICMP:
                            parsedPacket = new ICMPPacket(byteOffsetToEthernetPayload, bytes, tv);
                            break;

                        case (int)IPProtocol.IPProtocolType.IGMP:
                            parsedPacket = new IGMPPacket(byteOffsetToEthernetPayload, bytes, tv);
                            break;

                        case (int)IPProtocol.IPProtocolType.TCP:
                            parsedPacket = new TCPPacket(byteOffsetToEthernetPayload, bytes, tv);
                            break;

                        case (int)IPProtocol.IPProtocolType.UDP:
                            parsedPacket = new UDPPacket(byteOffsetToEthernetPayload, bytes, tv);
                            break;

                        // unidentified ip..
                        default:
                            parsedPacket = new IPPacket(byteOffsetToEthernetPayload, bytes, tv);
                            break;
                        }

                        // check that the parsed packet is valid
                        if (!parsedPacket.IsValid(out errorString))
                        {
                            throw new PcapException(errorString);
                        }
                        else
                        {
                            return(parsedPacket);
                        }
                    }
                    catch
                    {
                        // error parsing the specific ip packet type, parse as a generic IPPacket
                        parsedPacket = new IPPacket(byteOffsetToEthernetPayload, bytes, tv);

                        // check that the parsed packet is valid
                        if (!parsedPacket.IsValid(out errorString))
                        {
                            throw new PcapException(errorString);
                        }
                        else
                        {
                            return(parsedPacket);
                        }
                    }

                // ethernet level code not recognized, default to anonymous packet..
                default:
                    parsedPacket = new EthernetPacket(byteOffsetToEthernetPayload, bytes, tv);
                    break;
                }

                return(parsedPacket);
            }
            catch
            {
                // we know we have at least an ethernet packet, so return that
                return(new EthernetPacket(byteOffsetToEthernetPayload, bytes, tv));
            }
        }