All pastes #92905 Raw Edit

PCap

public text v1 · immutable
#92905 ·published 2006-07-20 00:47 UTC
rendered paste body
using System;
using System.Text; // StringBuilder and Regex
using System.Collections; // For IEnumerable and CollectionBase
using System.Runtime.InteropServices; // For DLLImports and other InterOpServices

namespace ninjaneo
{
    class Application
    {
        static void Main(string[] args)
        {
            #region Application Control
            EtherNinja _etherNinja = new EtherNinja();
            foreach (EtherNinja.Device _device in _etherNinja.Devices)
            {
                Console.WriteLine("\"{0:s}\"\r\n\t{1:s}", _device.Description, _device.Name);
                foreach (EtherNinja.Device.Address _address in _device.Addresses)
                {
                    Console.WriteLine();
                    Console.WriteLine("\t\tIP Address\t: {0:s}", _address.IP.ToString());
                    Console.WriteLine("\t\tBroadcast\t: {0:s}", _address.Broadcast.ToString());
                    Console.WriteLine("\t\tSubnet Mask\t: {0:s}", _address.Netmask.ToString());
                    Console.WriteLine();
                }
                Console.WriteLine();
            }
            Console.ReadLine();
            #endregion
        }
    }
    public class EtherNinja
    {
        public DeviceCollection Devices = new DeviceCollection();

        public EtherNinja()
        {
            foreach (Device _device in WinPCap.GetDevices())
            {
                Devices.Add(_device);
            }
        }

        public enum NetworkType : ushort
        {
            UNSPEC,
            LOCAL,
            INET,
            IMPLINK,
            PUP,
            CHAOS,
            NS,
            ISO,
            ECMA,
            DATAKIT,
            CCITT,
            SNA,
            DECnet,
            DLI,
            LAT,
            HYLINK,
            APPLETALK,
            ROUTE,
            LINK,
            pseudo_XTP,
            COIP,
            CNT,
            pseudo_RTIP,
            IPX,
            SIP,
            pseudo_PIP,
            ISDN,
            pseudo_KEY,
            INET6,
            NATM,
            ATM,
            pseudo_HDRCMPLT,
            NETGRAPH,
            MAX
        }
        static class Functions
        {
            #region Hex Encoding and Parsing Functions
            public static class HexEncoding
            {
                public static string GetString(string _hex)
                {
                    System.Text.RegularExpressions.Regex regex;
                    System.Text.RegularExpressions.MatchCollection matchColllection;
                    string _output = "";
                    regex = new System.Text.RegularExpressions.Regex("[a-fA-F0-9]{1,2}");
                    matchColllection = regex.Matches(_hex);
                    foreach (System.Text.RegularExpressions.Match _match in matchColllection)
                    {
                        _output += (char)byte.Parse(_match.ToString(), System.Globalization.NumberStyles.HexNumber);
                    }
                    return _output;
                }
            }
            #endregion
            #region General Data Manipulation and Extraction
            public static byte[] GetBytes(byte[] _obj, int _length)
            {
                return GetBytes(_obj, 0, _length);
            }
            public static byte[] GetBytes(byte[] _obj, int _offset, int _length)
            {
                byte[] _elements = new byte[_length];
                Array.ConstrainedCopy(_obj, _offset, _elements, 0, _length);
                return _elements;
            }
            #endregion
            #region Network Math such as finding the broadcast/network addresses
            #endregion
        }
        #region IPHLPAPI Library
        class iphlpapi
        {


        }
        #endregion
        #region WinPCap Library
        public static class WinPCap
        {
            const int pcapErrorBufferSize = 256;
            #region DLL Imports
            [DllImport("wpcap.dll", CharSet = CharSet.Auto)]
            extern static int pcap_findalldevs(ref IntPtr handle, StringBuilder errBuff);
            [DllImport("wpcap.dll", CharSet = CharSet.Ansi)]
            extern static void pcap_freealldevs(IntPtr enumHandle);
            #endregion
            public enum PCapDeviceFlags : uint
            {
                testflaga = 1, testflagb = 2
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct SocketAddress
            {
                public NetworkType Family;
                public ushort Port;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
                public byte[] Data;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct PCapAddress
            {
                public IntPtr NextPCapAddress;
                public IntPtr SocketAddress;
                public IntPtr NetmaskSocketAddress;
                public IntPtr BroadcastSocketAddress;
                public IntPtr DestinationSocketAddress;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct PCapDevice
            {
                public IntPtr NextPCapDevice;
                public string Name;
                public string Description;
                public IntPtr addressesPointer;
                public PCapDeviceFlags Flags;
            }
            public static IEnumerable GetDevices()
            {
                StringBuilder error = new StringBuilder(pcapErrorBufferSize);
                IntPtr _pcapDevicePtr = IntPtr.Zero;
                IntPtr _pcapFindAllDevs = IntPtr.Zero;
                PCapDevice _pcapDevice;
                pcap_findalldevs(ref _pcapFindAllDevs, error);
                try
                {
                    for (_pcapDevicePtr = _pcapFindAllDevs; _pcapDevicePtr != IntPtr.Zero; _pcapDevicePtr = _pcapDevice.NextPCapDevice)
                    {
                        _pcapDevice = (PCapDevice)Marshal.PtrToStructure(_pcapDevicePtr, typeof(PCapDevice));
                        yield return new Device(_pcapDevice);
                    }
                }
                finally
                {
                    pcap_freealldevs(_pcapFindAllDevs);
                }
            }
        }
        #endregion
        #region Network Stack
        public class MacAddress
        {
            public char[] Address = new char[6];
            public MacAddress(string value)
            {
                Hex = value;
            }
            public MacAddress(char[] value)
            {
                Address = value;
            }
            public string Hex
            {
                get
                {
                    return Address.ToString();
                }
                set
                {
                    Address = Functions.HexEncoding.GetString(value).ToCharArray();
                }
            }
            public override string ToString()
            {
                return Address.ToString();
            }
        }
        public class Raw : NetworkStack
        {
            string _pdu;
            public override object Pdu
            {
                get
                {
                    return _pdu;
                }
                set
                {
                    _pdu = (string)value;
                }
            }
        }
        public abstract class NetworkStack
        {
            public abstract object Pdu
            {
                get;
                set;
            }
            public virtual string ToString()
            {
                return Pdu.ToString();
            }
        }
        #endregion
        public class Device
        {
            public string Name;
            public string Description;
            public AddressCollection Addresses;
            public Device(WinPCap.PCapDevice _device)
            {
                Name = _device.Name;
                Description = _device.Description;
                Addresses = new AddressCollection();
                foreach (Address _address in GetAddresses(_device.addressesPointer))
                {
                    Addresses.Add(_address);
                }
            }
            public class Address
            {
                public System.Net.IPAddress IP;
                public System.Net.IPAddress Destination;
                public System.Net.IPAddress Broadcast;
                public System.Net.IPAddress Netmask;
                public Address(System.Net.IPAddress _ip, System.Net.IPAddress _destination, System.Net.IPAddress _broadcast, System.Net.IPAddress _netmask)
                {
                    IP = _ip;
                    Destination = _destination;
                    Broadcast = _broadcast;
                    Netmask = _netmask;
                }
                public Address(IntPtr _ip, IntPtr _destination, IntPtr _broadcast, IntPtr _netmask)
                    : this(GetIP(_ip), GetIP(_destination), GetIP(_broadcast), GetIP(_netmask))
                {
                }
                static System.Net.IPAddress GetIP(IntPtr _addressPointer)
                {
                    if (_addressPointer == IntPtr.Zero)
                        return new System.Net.IPAddress(0);
                    WinPCap.SocketAddress addr = (WinPCap.SocketAddress)Marshal.PtrToStructure(_addressPointer, typeof(WinPCap.SocketAddress));
                    switch (addr.Family)
                    {
                        case NetworkType.INET:
                            return new System.Net.IPAddress(Functions.GetBytes(addr.Data, 4));
                        case NetworkType.INET6:
                            return new System.Net.IPAddress(addr.Data);
                        default:
                            return new System.Net.IPAddress(0);
                    }
                }
            }
            public class AddressCollection : CollectionBase
            {
                public Address this[int index]
                {
                    get
                    {
                        return (Address)List[index];
                    }
                }
                public int Add(Address addr)
                {
                    return List.Add(addr);
                }
            }
            public static IEnumerable GetAddresses(IntPtr _pcapAddress)
            {
                for (WinPCap.PCapAddress _addressStruct;
                    _pcapAddress != IntPtr.Zero;
                    _pcapAddress = _addressStruct.NextPCapAddress)
                {
                    _addressStruct = (WinPCap.PCapAddress)Marshal.PtrToStructure(_pcapAddress, typeof(WinPCap.PCapAddress));
                    yield return new Address(_addressStruct.SocketAddress, _addressStruct.DestinationSocketAddress, _addressStruct.BroadcastSocketAddress, _addressStruct.NetmaskSocketAddress);
                }
            }
        }
        public class DeviceCollection : CollectionBase
        {
            public Device this[int _index]
            {
                get
                {
                    return (Device)List[_index];
                }
            }
            public int Add(Device _device)
            {
                return List.Add(_device);
            }
        }
    }
    public class Crypto
    {
        #region Cycilic Redudancy Check algorithim
        public class Crc
        {
            public string Hash(string value)
            {
                return "CHKS";
            }
        }
        #endregion
    }
}