Home » Source Code » » PcapUnmanagedStructures.cs

PcapUnmanagedStructures.cs ( File view )

From:
  • By 2010-08-21
  • View(s):38
  • Download(s):2
  • Point(s): 1
			/*
This file is part of SharpPcap.

SharpPcap is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

SharpPcap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with SharpPcap.  If not, see <http://www.gnu.org/licenses/>.
*/
/* 
 * Copyright 2005 Tamir Gal <tamir@tamirgal.com>
 * Copyright 2008-2009 Chris Morgan <chmorgan@gmail.com>
 */

using System;
using System.Runtime.InteropServices;

namespace SharpPcap
{

    internal class PcapUnmanagedStructures
    {

        #region Unmanaged Structs Implementation

        /// <summary>
        /// Item in a list of interfaces.
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct pcap_if 
        {

            public IntPtr /* pcap_if* */    Next;           
            public string                   Name;           /* name to hand to "pcap_open_live()" */                
            public string                   Description;    /* textual description of interface, or NULL */
            public IntPtr /*pcap_addr * */  Addresses;
            public UInt32                   Flags;          /* PCAP_IF_ interface flags */
        
};

        /// <summary>
        /// Representation of an interface address.
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct pcap_addr 
        {

            public IntPtr /* pcap_addr* */  Next;
            public IntPtr /* sockaddr * */  Addr;       /* address */
            public IntPtr /* sockaddr * */  Netmask;    /* netmask for that address */
            public IntPtr /* sockaddr * */  Broadaddr;  /* broadcast address for that address */
            public IntPtr /* sockaddr * */  Dstaddr;    /* P2P destination address for that address */
        
};

        /// <summary>
        /// Structure used by kernel to store a generic address
        /// Look at the sa_family value to determine which specific structure to use
        /// 'struct sockaddr'
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct sockaddr 
        {

            public UInt16       sa_family;      /* address family */
            [MarshalAs(UnmanagedType.ByValArray, SizeConst=14)]
            public byte[]       sa_data;        /* 14 bytes of protocol address */
        
};

        /// <summary>
        /// Structure that holds an ipv4 address
        /// </summary>
        public struct in_addr
        {

            public UInt32 s_addr;
        
}

        /// <summary>
        /// Structure that holds an ipv4 address
        /// 'struct sockaddr'
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct sockaddr_in
        {

            public UInt16       sa_family;      /* address family */
            public UInt16       sa_port;        /* port */
            public in_addr      sin_addr;       /* address */

            // TODO: would be great to be able to have the compiler take care of this for us
            //       but I'm not sure how to

            // pad the size of sockaddr_in out to 16 bytes
            [MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
            private byte[]       pad;
        
};

        /// <summary>
        /// Structure that holds an ipv6 address
        /// NOTE: we cast the 'struct sockaddr*' to this structure based on the sa_family type
        /// 'struct sockaddr_in6'
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        internal struct sockaddr_in6
        {

            public UInt16       sin6_family;    /* address family */
            public UInt16       sin6_port;      /* Transport layer port # */
            public UInt32       sin6_flowinfo;  /* IPv6 flow information */
            [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
            public byte[]       sin6_addr;      /* IPv6 address */
            public UInt32       sin6_scope_id;  /* scope id (new in RFC2553) */
        
};

        /// <summary>
        /// Structure to represent a low level address, like a hardware address
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        internal struct sockaddr_ll
        {

            public UInt16 sll_family;
            public UInt16 sll_protocol;
            public UInt32 sll_ifindex;
            public UInt16 sll_hatype;
            public byte   sll_pkttype;
            public byte   sll_halen;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
            public byte[] sll_addr;
        
};

        #region timeval
        /// <summary>
        /// Windows and Unix differ in their memory models and make it difficult to
        /// support struct timeval in a single library, like this one, across
        /// multiple platforms.
        ///
        /// See http://en.wikipedia.org/wiki/64bit#Specific_data_models
        ///
        /// The issue is that struct timeval {
 long tv_sec; long tv_usec; 
}
        /// has different sizes on Linux 32 and 64bit but the same size on
        /// Windows 32 and 64 bit
        ///
        /// Thanks to Jon Pryor for his help in figuring out both the issue with Linux
        /// 32/64bit and the issue between Windows and Unix
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]    
        public struct timeval_unix
        {

            // NOTE: The use of IntPtr here is due to the issue with the timeval structure
            //       The timeval structure contains long values, which differ between 32 bit and
            //       64 bit platforms. One trick, thanks to Jon Pryor for the suggestion, is to
            //       use IntP
...
...
(Please download the complete source code to view)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
Changelog16.90 kB10-08-09|02:39
010.00 B
010.00 B
AssemblyInfo.cs2.32 kB10-08-09|02:39
Example01.IfList.csproj3.96 kB10-08-09|02:39
Example1.IfList.cs1.27 kB10-08-09|02:39
010.00 B
App.ico1.05 kB10-08-09|02:39
AssemblyInfo.cs2.32 kB10-08-09|02:39
Example10.SendQueue.csproj4.49 kB10-08-09|02:39
Example10.SendQueues.cs4.61 kB10-08-09|02:39
010.00 B
App.ico1.05 kB10-08-09|02:39
AssemblyInfo.cs2.32 kB10-08-09|02:39
Example11.Statistics.cs4.35 kB10-08-09|02:39
Example11.Statistics.csproj4.00 kB10-08-09|02:39
010.00 B
App.ico1.05 kB10-08-09|02:39
AssemblyInfo.cs2.32 kB10-08-09|02:39
Example12.PacketManipulation.cs5.36 kB10-08-09|02:39
Example12.PacketManipulation.csproj4.02 kB10-08-09|02:39
010.00 B
AssemblyInfo.cs2.32 kB10-08-09|02:39
Example02.ArpResolve.csproj4.31 kB10-08-09|02:39
Example2.ArpResolve.cs3.67 kB10-08-09|02:39
010.00 B
App.ico1.05 kB10-08-09|02:39
AssemblyInfo.cs2.32 kB10-08-09|02:39
Example03.BasicCap.csproj3.99 kB10-08-09|02:39
Example3.BasicCap.cs2.87 kB10-08-09|02:39
010.00 B
App.ico1.05 kB10-08-09|02:39
AssemblyInfo.cs2.32 kB10-08-09|02:39
Example04.BasicCapNoCallback.csproj4.02 kB10-08-09|02:39
Example4.BasicCapNoCallback.cs2.65 kB10-08-09|02:39
010.00 B
App.ico1.05 kB10-08-09|02:39
AssemblyInfo.cs2.32 kB10-08-09|02:39
Example05.PcapFilter.csproj4.00 kB10-08-09|02:39
Example5.PcapFilter.cs3.13 kB10-08-09|02:39
010.00 B
App.ico1.05 kB10-08-09|02:39
AssemblyInfo.cs2.32 kB10-08-09|02:39
Example06.DumpTCP.csproj3.99 kB10-08-09|02:39
Example6.DumpTCP.cs3.70 kB10-08-09|02:39
010.00 B
App.ico1.05 kB10-08-09|02:39
AssemblyInfo.cs2.32 kB10-08-09|02:39
Example07.DumpToFile.csproj4.00 kB10-08-09|02:39
Example7.DumpToFile.cs3.18 kB10-08-09|02:39
010.00 B
App.ico1.05 kB10-08-09|02:39
AssemblyInfo.cs2.32 kB10-08-09|02:39
Example08.ReadFile.csproj3.99 kB10-08-09|02:39
Example8.ReadFile.cs2.54 kB10-08-09|02:39
010.00 B
App.ico1.05 kB10-08-09|02:39
AssemblyInfo.cs2.32 kB10-08-09|02:39
Example09.SendPacket.csproj4.00 kB10-08-09|02:39
Example9.SendPacket.cs2.92 kB10-08-09|02:39
LICENSE7.46 kB10-08-09|02:39
ReleaseProcess2.10 kB10-08-09|02:39
SharpPcap.sln8.49 kB10-08-09|02:39
010.00 B
App.ico1.05 kB10-08-09|02:39
AssemblyInfo.cs2.39 kB10-08-09|02:39
010.00 B
ARPFields.cs4.55 kB10-08-09|02:39
ARPPacket.cs10.30 kB10-08-09|02:39
EthernetFields.cs2.06 kB10-08-09|02:39
EthernetPacket.cs14.43 kB10-08-09|02:39
ICMPFields.cs2.13 kB10-08-09|02:39
ICMPMessage.cs4.84 kB10-08-09|02:39
ICMPMessages.cs4.43 kB10-08-09|02:39
ICMPPacket.cs7.85 kB10-08-09|02:39
IGMPFields.cs2.53 kB10-08-09|02:39
IGMPMessage.cs2.24 kB10-08-09|02:39
IGMPMessages.cs1.26 kB10-08-09|02:39
IGMPPacket.cs5.94 kB10-08-09|02:39
IPPacket.cs18.98 kB10-08-09|02:39
IPPort.cs99.14 kB10-08-09|02:39
IPPorts.cs34.59 kB10-08-09|02:39
IPProtocol.cs7.89 kB10-08-09|02:39
IPv4Fields.cs3.97 kB10-08-09|02:39
IPv4Packet.cs22.01 kB10-08-09|02:39
IPv6Fields.cs3.35 kB10-08-09|02:39
IPv6Packet.cs12.96 kB10-08-09|02:39
LinkLayer.cs7.54 kB10-08-09|02:39
LinkLayers.cs3.23 kB10-08-09|02:39
Packet.cs2.62 kB10-08-09|02:39
PacketEncoding.cs5.01 kB10-08-09|02:39
PacketFactory.cs3.21 kB10-08-09|02:39
RawPacket.cs3.58 kB10-08-09|02:39
TCPFields.cs3.67 kB10-08-09|02:39
TCPPacket.cs23.19 kB10-08-09|02:39
UDPFields.cs2.23 kB10-08-09|02:39
UDPPacket.cs10.09 kB10-08-09|02:39
010.00 B
AnsiEscapeSequences.cs5.99 kB10-08-09|02:39
ArrayHelper.cs6.92 kB10-08-09|02:39
ChecksumUtils.cs2.93 kB10-08-09|02:39
HexHelper.cs3.80 kB10-08-09|02:39
Timeval.cs1.97 kB10-08-09|02:39
makefile579.00 B10-08-09|02:39
makefile-test0.00 B10-08-09|02:39
util.csproj4.32 kB10-08-09|02:39
package.html1.07 kB10-08-09|02:39
Pcap.cs5.39 kB10-08-09|02:39
PcapAddress.cs2.15 kB10-08-09|02:39
PcapDevice.cs24.12 kB10-08-09|02:39
PcapDeviceCaptureLoop.cs5.21 kB10-08-09|02:39
PcapDeviceList.cs3.96 kB10-08-09|02:39
PcapEventArgs.cs1.69 kB10-08-09|02:39
PcapException.cs1.45 kB10-08-09|02:39
PcapHeader.cs5.91 kB10-08-09|02:39
PcapInterface.cs5.18 kB10-08-09|02:39
PcapOfflineDevice.cs3.96 kB10-08-09|02:39
PcapSendQueue.cs6.61 kB10-08-09|02:39
PcapStatistics.cs3.16 kB10-08-09|02:39
PcapUnmanagedStructures.cs8.85 kB10-08-09|02:39
010.00 B
ARP.cs7.52 kB10-08-09|02:39
Readme.Mono725.00 B10-08-09|02:39
SafeNativeMethods.cs11.41 kB10-08-09|02:39
SharpPcap.csproj8.54 kB10-08-09|02:39
SharpPcap.dll.config492.00 B10-08-09|02:39
Sockaddr.cs4.72 kB10-08-09|02:39
010.00 B
Convert.cs3.49 kB10-08-09|02:39
IPAddressRange.cs4.04 kB10-08-09|02:39
IPSubnet.cs2.41 kB10-08-09|02:39
IPUtil.cs8.93 kB10-08-09|02:39
Int64Range.cs1.77 kB10-08-09|02:39
NumberRange.cs5.54 kB10-08-09|02:39
Random.cs2.53 kB10-08-09|02:39
Version.cs1.43 kB10-08-09|02:39
010.00 B
SharpPcap.html31.81 kB10-08-09|02:39
010.00 B
SharpPcap.png15.47 kB10-08-09|02:39
010.00 B
App.ico1.05 kB10-08-09|02:39
ArpPacketTest.cs2.62 kB10-08-09|02:39
AssemblyInfo.cs2.28 kB10-08-09|02:39
CheckFilterTest.cs528.00 B10-08-09|02:39
EthernetPacketTest.cs7.32 kB10-08-09|02:39
Form1.cs2.91 kB10-08-09|02:39
Form1.resx6.83 kB10-08-09|02:39
IPAddressRangeTest.cs3.42 kB10-08-09|02:39
IPSubnetTest.cs599.00 B10-08-09|02:39
IPv6PacketTest.cs4.07 kB10-08-09|02:39
Int64RangeTest.cs3.41 kB10-08-09|02:39
InvalidPacketTest.cs1.25 kB10-08-09|02:39
IpUtilTest.cs2.12 kB10-08-09|02:39
PacketsTest.cs1.09 kB10-08-09|02:39
PcapGetAllDevices.cs781.00 B10-08-09|02:39
PcapTest.cs313.00 B10-08-09|02:39
SendTcpSynExample.cs6.63 kB10-08-09|02:39
SetFilterTest.cs646.00 B10-08-09|02:39
TCPPacketTest.cs3.24 kB10-08-09|02:39
Test.csproj6.20 kB10-08-09|02:39
UDPPacketTest.cs1.04 kB10-08-09|02:39
UdpTest.cs2.13 kB10-08-09|02:39
010.00 B
README827.00 B10-08-09|02:39
arp_request_response.pcap158.00 B10-08-09|02:39
ip_packet_bogus_length.pcap1.53 kB10-08-09|02:39
ipv6_http.pcap3.37 kB10-08-09|02:39
ipv6_icmpv6_packet.pcap110.00 B10-08-09|02:39
tcp.pcap106.00 B10-08-09|02:39
tcp_with_extra_bytes.pcap100.00 B10-08-09|02:39
test_stream.pcap613.00 B10-08-09|02:39
udp_dns_request_response.pcap521.00 B10-08-09|02:39
010.00 B
nunit.framework.dll44.00 kB10-08-09|02:39
make_bin_release1.31 kB10-08-09|02:39
make_src_release888.00 B10-08-09|02:39
...
Sponsored links

PcapUnmanagedStructures.cs (241.84 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D