Click here to Skip to main content
15,885,910 members
Articles / Web Development / HTML

SharpPcap - A Packet Capture Framework for .NET

,
Rate me:
Please Sign up or sign in to vote.
4.94/5 (192 votes)
5 May 2014MPL21 min read 2.1M   10.3K   518  
A framework for capturing, injecting and analyzing network packets for .NET applications based on the WinPcap packet capture driver
// $Id: IPProtocol.java,v 1.5 2004/02/24 19:21:31 pcharles Exp $

/// <summary>************************************************************************
/// Copyright (C) 2001, Patrick Charles and Jonas Lehmann                   *
/// Distributed under the Mozilla Public License                            *
/// http://www.mozilla.org/NPL/MPL-1.1.txt                                *
/// *************************************************************************
/// </summary>
namespace Tamir.IPLib.Packets
{
	using System;
	using HashMap =  System.Collections.Hashtable;
	/// <summary> IPProtocol utility class.
	/// *
	/// </summary>
	/// <author>  Patrick Charles and Jonas Lehmann
	/// </author>
	/// <version>  $Revision: 1.5 $
	/// @lastModifiedBy $Author: pcharles $
	/// @lastModifiedAt $Date: 2004/02/24 19:21:31 $
	/// 
	/// </version>
	public class IPProtocol : IPProtocols
	{
		/// <summary> Fetch a protocol description.
		/// </summary>
		/// <param name="code">the code associated with the message.
		/// </param>
		/// <returns> a message describing the significance of the IP protocol.
		/// 
		/// </returns>
		public static System.String getDescription(int code)
		{
			System.Int32 c = code;
			if (messages.ContainsKey(c))
				return (System.String) messages[c];
			else
				return "unknown";
		}
		
		/// <summary> 'Human-readable' IP protocol descriptions.
		/// </summary>
		//UPGRADE_NOTE: The initialization of  'messages' was moved to static method 'Tamir.IPLib.Packets.IPProtocol'. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1005"'
		private static HashMap messages;
		
		/// <summary> 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.
		/// *
		/// </summary>
		/// <param name="lLen">the length of the link-level header.
		/// </param>
		/// <param name="packetBytes">packet bytes, including the link-layer header.
		/// </param>
		/// <returns> the IP protocol code. i.e. 0x06 signifies TCP protocol.
		/// 
		/// </returns>
		public static int extractProtocol(int lLen, byte[] packetBytes)
		{
			return packetBytes[lLen + Tamir.IPLib.Packets.IPFields_Fields.IP_CODE_POS];
		}
		
		
		private System.String _rcsid = "$Id: IPProtocol.java,v 1.5 2004/02/24 19:21:31 pcharles Exp $";
		static IPProtocol()
		{
			messages = new HashMap();
			{
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.IP] = "Dummy protocol for TCP";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.HOPOPTS] = "IPv6 Hop-by-Hop options";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.ICMP] = "Internet Control Message Protocol";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.IGMP] = "Internet Group Management Protocol";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.IPIP] = "IPIP tunnels";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.TCP] = "Transmission Control Protocol";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.EGP] = "Exterior Gateway Protocol";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.PUP] = "PUP protocol";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.UDP] = "User Datagram Protocol";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.IDP] = "XNS IDP protocol";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.TP] = "SO Transport Protocol Class 4";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.IPV6] = "IPv6 header";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.ROUTING] = "IPv6 routing header";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.FRAGMENT] = "IPv6 fragmentation header";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.RSVP] = "Reservation Protocol";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.GRE] = "General Routing Encapsulation";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.ESP] = "encapsulating security payload";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.AH] = "authentication header";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.ICMPV6] = "ICMPv6";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.NONE] = "IPv6 no next header";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.DSTOPTS] = "IPv6 destination options";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.MTP] = "Multicast Transport Protocol";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.ENCAP] = "Encapsulation Header";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.PIM] = "Protocol Independent Multicast";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.COMP] = "Compression Header Protocol";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.RAW] = "Raw IP Packet";
				messages[Tamir.IPLib.Packets.IPProtocols_Fields.INVALID] = "INVALID IP";
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Mozilla Public License 1.1 (MPL 1.1)


Written By
Software Developer
Israel Israel
Works as a Network Engineer for a leading networking company.

Written By
United States United States
Entrepreneur and product developer with a wide range of technical and business experience.

Comments and Discussions