예제 #1
0
        /// <summary>
        /// Marshal this structure into the platform dependent version and return
        /// and IntPtr to that memory
        ///
        /// NOTE: IntPtr MUST BE FREED via Marshal.FreeHGlobal()
        /// </summary>
        /// <returns>
        /// A <see cref="IntPtr"/>
        /// </returns>
        public IntPtr MarshalToIntPtr()
        {
            IntPtr hdrPtr;

            if (Environment.OSVersion.Platform == PlatformID.Unix)
            {
                // setup the structure to marshal
                PcapUnmanagedStructures.pcap_pkthdr_unix pkthdr = new PcapUnmanagedStructures.pcap_pkthdr_unix();
                pkthdr.caplen     = this.CaptureLength;
                pkthdr.len        = this.PacketLength;
                pkthdr.ts.tv_sec  = (IntPtr)this.Seconds;
                pkthdr.ts.tv_usec = (IntPtr)this.MicroSeconds;

                hdrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PcapUnmanagedStructures.pcap_pkthdr_unix)));
                Marshal.StructureToPtr(pkthdr, hdrPtr, true);
            }
            else
            {
                PcapUnmanagedStructures.pcap_pkthdr_windows pkthdr = new PcapUnmanagedStructures.pcap_pkthdr_windows();
                pkthdr.caplen     = this.CaptureLength;
                pkthdr.len        = this.PacketLength;
                pkthdr.ts.tv_sec  = (int)this.Seconds;
                pkthdr.ts.tv_usec = (int)this.MicroSeconds;

                hdrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PcapUnmanagedStructures.pcap_pkthdr_windows)));
                Marshal.StructureToPtr(pkthdr, hdrPtr, true);
            }

            return(hdrPtr);
        }
예제 #2
0
 internal PcapHeader(IntPtr pcap_pkthdr)
 {
     if (Environment.OSVersion.Platform == PlatformID.Unix)
     {
         PcapUnmanagedStructures.pcap_pkthdr_unix pkthdr =
             (PcapUnmanagedStructures.pcap_pkthdr_unix)Marshal.PtrToStructure(pcap_pkthdr,
                                                                              typeof(PcapUnmanagedStructures.pcap_pkthdr_unix));
         this.CaptureLength = pkthdr.caplen;
         this.PacketLength  = pkthdr.len;
         this.Seconds       = (ulong)pkthdr.ts.tv_sec;
         this.MicroSeconds  = (ulong)pkthdr.ts.tv_usec;
     }
     else
     {
         PcapUnmanagedStructures.pcap_pkthdr_windows pkthdr =
             (PcapUnmanagedStructures.pcap_pkthdr_windows)Marshal.PtrToStructure(pcap_pkthdr,
                                                                                 typeof(PcapUnmanagedStructures.pcap_pkthdr_windows));
         this.CaptureLength = pkthdr.caplen;
         this.PacketLength  = pkthdr.len;
         this.Seconds       = (ulong)pkthdr.ts.tv_sec;
         this.MicroSeconds  = (ulong)pkthdr.ts.tv_usec;
     }
 }
예제 #3
0
        /// <summary>
        /// Marshal this structure into the platform dependent version and return
        /// and IntPtr to that memory
        ///
        /// NOTE: IntPtr MUST BE FREED via Marshal.FreeHGlobal()
        /// </summary>
        /// <returns>
        /// A <see cref="IntPtr"/>
        /// </returns>
        public IntPtr MarshalToIntPtr()
        {
            IntPtr hdrPtr;

            if (Environment.OSVersion.Platform == PlatformID.Unix)
            {
                // setup the structure to marshal
                PcapUnmanagedStructures.pcap_pkthdr_unix pkthdr = new PcapUnmanagedStructures.pcap_pkthdr_unix();
                pkthdr.caplen = this.CaptureLength;
                pkthdr.len = this.PacketLength;
                pkthdr.ts.tv_sec = (IntPtr)this.Seconds;
                pkthdr.ts.tv_usec = (IntPtr)this.MicroSeconds;

                hdrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PcapUnmanagedStructures.pcap_pkthdr_unix)));
                Marshal.StructureToPtr(pkthdr, hdrPtr, true);
            }
            else
            {
                PcapUnmanagedStructures.pcap_pkthdr_windows pkthdr = new PcapUnmanagedStructures.pcap_pkthdr_windows();
                pkthdr.caplen = this.CaptureLength;
                pkthdr.len = this.PacketLength;
                pkthdr.ts.tv_sec = (int)this.Seconds;
                pkthdr.ts.tv_usec = (int)this.MicroSeconds;

                hdrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PcapUnmanagedStructures.pcap_pkthdr_windows)));
                Marshal.StructureToPtr(pkthdr, hdrPtr, true);
            }

            return hdrPtr;
        }