Example #1
0
        /// <summary>
        /// Convert an array of keys into unmanaged memory
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private static IntPtr KeysToIntPtr(List <AirPcapKey> value)
        {
            // allocate memory for the entire collection
            IntPtr pKeyCollection         = Marshal.AllocHGlobal(AirPcapDevice.KeyCollectionSize(value.Count));
            var    pKeyCollectionPosition = pKeyCollection;

            // build the collection struct
            var collection = new AirPcapUnmanagedStructures.AirpcapKeysCollection();

            collection.nKeys = (uint)value.Count;

            // convert this collection to unmanaged memory
            Marshal.StructureToPtr(collection, pKeyCollectionPosition, false);

            // advance the pointer
            pKeyCollectionPosition = new IntPtr(pKeyCollectionPosition.ToInt64() +
                                                Marshal.SizeOf(typeof(AirPcapUnmanagedStructures.AirpcapKeysCollection)));

            // write the keys to memory
            for (int x = 0; x < value.Count; x++)
            {
                var key = new AirPcapUnmanagedStructures.AirpcapKey();
                key.KeyType = value[x].Type;
                key.KeyLen  = (uint)value[x].Data.Length;

                // make sure we have the right size byte[], the fields in the structure passed to Marshal.StructureToPtr()
                // have to match the specified sizes or an exception will be thrown
                key.KeyData = new byte[AirPcapUnmanagedStructures.WepKeyMaxSize];
                Array.Copy(value[x].Data, key.KeyData, value[x].Data.Length);

                // copy the managed memory into the unmanaged memory
                Marshal.StructureToPtr(key, pKeyCollectionPosition, false);

                // advance the pointer
                pKeyCollectionPosition = new IntPtr(pKeyCollectionPosition.ToInt64() + Marshal.SizeOf(typeof(AirPcapUnmanagedStructures.AirpcapKey)));
            }

            return(pKeyCollection);
        }
Example #2
0
        /// <summary>
        /// Marshal a chunk of captured packets into a packet list
        /// </summary>
        /// <param name="packetsBuffer"></param>
        /// <param name="bufferEnd"></param>
        /// <param name="packets"></param>
        protected virtual void MarshalPackets(IntPtr packetsBuffer, IntPtr bufferEnd,
                                              out List <RawCapture> packets)
        {
            RawCapture p;

            var linkType = LinkType;

            packets = new List <RawCapture>();

            IntPtr bufferPointer = packetsBuffer;

            while (bufferPointer.ToInt64() < bufferEnd.ToInt64())
            {
                // marshal the header
                var header = new AirPcapPacketHeader(bufferPointer);

                // advance the pointer to the packet data and marshal that data
                // into a managed buffer
                bufferPointer = new IntPtr(bufferPointer.ToInt64() + header.Hdrlen);
                var pkt_data = new byte[header.Caplen];
                Marshal.Copy(bufferPointer, pkt_data, 0, (int)header.Caplen);

                p = new RawCapture(linkType,
                                   new PosixTimeval(header.TsSec,
                                                    header.TsUsec),
                                   pkt_data);

                packets.Add(p);

                // advance the pointer by the size of the data
                // and round up to the next word offset since each frame header is on a word boundry
                int alignment = 4;
                var pointer   = bufferPointer.ToInt64() + header.Caplen;
                pointer       = AirPcapDevice.RoundUp(pointer, alignment);
                bufferPointer = new IntPtr(pointer);
            }
        }