SharpPcap.Packets.IPProtocol.extractProtocol C# (CSharp) Method

extractProtocol() public static method

Extract the protocol code from packet data. The packet data must contain an IP datagram. The protocol code specifies what kind of information is contained in the data block of the ip datagram.
public static extractProtocol ( int lLen, byte packetBytes ) : int
lLen int the length of the link-level header. ///
packetBytes byte packet bytes, including the link-layer header. ///
return int
        public static int extractProtocol(int lLen, byte[] packetBytes)
        {
            IPPacket.IPVersions ipVer = ExtractVersion(lLen, packetBytes);
            int protoOffset;

            switch (ipVer)
            {
                case IPPacket.IPVersions.IPv4:
                    protoOffset = IPv4Fields_Fields.IP_CODE_POS;
                    break;
                case IPPacket.IPVersions.IPv6:
                    protoOffset = IPv6Fields_Fields.NEXT_HEADER_POS;
                    break;
                default:
                    return -1;//unknown ip version
            }
            return packetBytes[lLen + protoOffset];
        }

Usage Example

Example #1
0
        /// <summary> Convert captured packet data into an object.</summary>
        public static Packet dataToPacket(int linkType, byte[] bytes, Timeval tv)
        {
            int 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 lLen = LinkLayer.getLinkLayerLength(linkType);

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

            if (offset == -1)
            {
                // if there is no embedded protocol, assume IP?
                ethProtocol = EthernetPacket.EtherType.IP;
            }
            else
            {
                ethProtocol = ArrayHelper.extractInteger(bytes, offset, EthernetFields_Fields.ETH_CODE_LEN);
            }

            // try to recognize the ethernet type..
            switch (ethProtocol)
            {
            // arp
            case EthernetPacket.EtherType.ARP:
                return(new ARPPacket(lLen, bytes, tv));

            case EthernetPacket.EtherType.IPV6:
            case EthernetPacket.EtherType.IP:
                // ethernet level code is recognized as IP, figure out what kind..
                int ipProtocol = IPProtocol.extractProtocol(lLen, bytes);
                switch (ipProtocol)
                {
                case (int)IPProtocol.IPProtocolType.ICMP: return(new ICMPPacket(lLen, bytes, tv));

                case (int)IPProtocol.IPProtocolType.IGMP: return(new IGMPPacket(lLen, bytes, tv));

                case (int)IPProtocol.IPProtocolType.TCP:  return(new TCPPacket(lLen, bytes, tv));

                case (int)IPProtocol.IPProtocolType.UDP:  return(new UDPPacket(lLen, bytes, tv));

                // unidentified ip..
                default:
                    return(new IPPacket(lLen, bytes, tv));
                }

            // ethernet level code not recognized, default to anonymous packet..
            default:
                return(new EthernetPacket(lLen, bytes, tv));
            }
        }
All Usage Examples Of SharpPcap.Packets.IPProtocol::extractProtocol