Ejemplo n.º 1
0
        /**
         * Methods
         */
        /// <summary>
        /// Initializes a new instance of the <see cref="ProtocolDataUnit"/> class.
        /// </summary>
        /// <param name="bhs"></param>
        /// <param name="rawHeaderData"></param>
        /// <param name="rawContentData"></param>
        public ProtocolDataUnit(byte[] rawHeaderData, byte[] rawContentData)
        {
            this.HeaderData = rawHeaderData;

            HandshakeHeader header = new HandshakeHeader();

            header.ReadFrom(rawHeaderData, 0);
            this.Header = header;

            // decompress content data
            if ((header.DataLength > 0) && (header.CompressedLength > 0))
            {
                // put raw data into a stream to decompress
                MemoryStream memoryStream = new MemoryStream(ZStream.UncompressBuffer(rawContentData));
                this.ContentData = Util.ReadFully(memoryStream, header.DataLength);
            }
            else
            {
                this.ContentData = null;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ar"></param>
        private void NetworkReadBuffer(IAsyncResult ar)
        {
            int bytesRead = Stream.EndRead(ar);

            try
            {
                // read PDU data from stream
                ProtocolDataUnit pdu = ProtocolDataUnit.ReadFrom(tempBuffer);
#if TRACE
                if (pdu != null)
                {
                    Messages.Trace("[SNIP .. Packet Rx from " + PhysicalAddress.ToString() + "]");
                    Messages.TraceHex("hdr", pdu.HeaderData, pdu.HeaderData.Length);
                    Messages.Trace("hdr->checksum = " + pdu.Header.CRC.ToString("X"));
                    Messages.Trace("hdr->length = " + pdu.Header.Length.ToString());
                    Messages.Trace("hdr->macAddr = " +
                                   pdu.Header.MacAddr[0].ToString("X") + ":" +
                                   pdu.Header.MacAddr[1].ToString("X") + ":" +
                                   pdu.Header.MacAddr[2].ToString("X") + ":" +
                                   pdu.Header.MacAddr[3].ToString("X") + ":" +
                                   pdu.Header.MacAddr[4].ToString("X") + ":" +
                                   pdu.Header.MacAddr[5].ToString("X"));
                    Messages.Trace("hdr->dataLength = " + pdu.Header.DataLength);
                    Messages.Trace("hdr->compressLength = " + pdu.Header.CompressedLength);
                    Messages.Trace("hdr length " + pdu.Header.Length);
                    if (pdu.ContentData != null)
                    {
                        Messages.TraceHex("frame", pdu.ContentData, pdu.ContentData.Length);
                        Messages.Trace("frame length " + pdu.ContentData.Length);
                        Messages.Trace("MAC Destination = " +
                                       pdu.ContentData[0].ToString("X") + ":" +
                                       pdu.ContentData[1].ToString("X") + ":" +
                                       pdu.ContentData[2].ToString("X") + ":" +
                                       pdu.ContentData[3].ToString("X") + ":" +
                                       pdu.ContentData[4].ToString("X") + ":" +
                                       pdu.ContentData[5].ToString("X"));
                        Messages.Trace("MAC Source = " +
                                       pdu.ContentData[6].ToString("X") + ":" +
                                       pdu.ContentData[7].ToString("X") + ":" +
                                       pdu.ContentData[8].ToString("X") + ":" +
                                       pdu.ContentData[9].ToString("X") + ":" +
                                       pdu.ContentData[10].ToString("X") + ":" +
                                       pdu.ContentData[11].ToString("X"));
                    }
                    Messages.Trace("[SNIP .. Packet Rx from " + PhysicalAddress.ToString() + "]");
                }
#endif

                // reset exception counter
                if (streamExceptionCount > 0)
                {
                    streamExceptionCount = 0;
                }

                // did we receive a pdu?
                if (pdu != null)
                {
                    // are we trying to shut down the session?
                    if ((pdu.Header.CRC == 250) && (pdu.Header.DataLength == 65535))
                    {
                        Close();
                        return;
                    }

                    // are we trying to register a client?
                    if ((pdu.Header.CRC == 255) && (pdu.Header.DataLength == 0))
                    {
                        HandshakeHeader header = new HandshakeHeader();
                        header.CRC              = 255;
                        header.DataLength       = 0;
                        header.CompressedLength = 0;
                        header.Length           = (ushort)header.Size;

                        byte[] node = new byte[6];

                        // generate a new MAC address
                        // set first 3 octets
                        node[0] = 0xAC;
                        node[1] = 0xDE;
                        node[2] = 0x48;

                        // maybe this is a reconnect? we have the last 3 of an assigned MAC
                        if ((pdu.Header.MacAddr[3] != 0) && (pdu.Header.MacAddr[4] != 0) && (pdu.Header.MacAddr[5] != 0))
                        {
                            Messages.WriteWarning("session reconnect? we recieved the last 3 " +
                                                  pdu.Header.MacAddr[3].ToString("X") + ":" +
                                                  pdu.Header.MacAddr[4].ToString("X") + ":" +
                                                  pdu.Header.MacAddr[5].ToString("X"));
                            node[3] = pdu.Header.MacAddr[3];
                            node[4] = pdu.Header.MacAddr[4];
                            node[5] = pdu.Header.MacAddr[5];
                        }
                        else
                        {
                            Random rand = new Random();
                            node[3] = (byte)rand.Next(255);
                            node[4] = (byte)rand.Next(255);
                            node[5] = (byte)rand.Next(255);
                        }

                        header.MacAddr  = node;
                        this.macAddress = node;

                        Messages.Trace("new session macAddr = " +
                                       macAddress[0].ToString("X") + ":" +
                                       macAddress[1].ToString("X") + ":" +
                                       macAddress[2].ToString("X") + ":" +
                                       macAddress[3].ToString("X") + ":" +
                                       macAddress[4].ToString("X") + ":" +
                                       macAddress[5].ToString("X"));

                        // build final payload
                        byte[] buffer = new byte[Util.RoundUp(header.Size, 4)];
                        header.WriteTo(buffer, 0);

                        // transmit
                        if (Stream.CanWrite)
                        {
                            Stream.Flush();
                            Stream.Write(buffer, 0, buffer.Length);
                            Stream.Flush();
                        }
                        return;
                    }

                    // otherwise handle actual packet data
                    Packet packet = Packet.ParsePacket(LinkLayers.Ethernet, pdu.ContentData);
                    CommManager.Instance.CaptureDevice.SendPacket(packet);
                }
            }
            catch (IOException)
            {
                waitMRE.Wait(1); // wait a bit before reading again
                waitMRE.Set();

                // increment exception counter
                streamExceptionCount++;
                if (streamExceptionCount >= MAX_STREAM_EXECPTIONS)
                {
                    this.Close();
                }
            }
            finally
            {
                waitMRE.Set();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="packet"></param>
        private void HandleIncoming(EthernetPacket packet)
        {
            if (isShutdown)
            {
                return;
            }
            if (client == null)
            {
                return;
            }
            if (!client.Connected)
            {
                return;
            }

            // is this our mac?
            if (PhysicalAddress.ToString() != packet.DestinationHwAddress.ToString())
            {
                return;
            }

#if TRACE
            Messages.Trace("session [" + PhysicalAddress.ToString() + "] destHW [" + packet.DestinationHwAddress.ToString() + "]");
            Messages.Trace("packet dump " + packet.ToString());
#endif
            HandshakeHeader header = new HandshakeHeader();
            header.CRC        = PacketCRC(packet.Bytes, packet.Bytes.Length);
            header.DataLength = (ushort)packet.Bytes.Length;
            header.MacAddr    = this.macAddress;

            // compress data
            byte[] compressedData = ZStream.CompressBuffer(packet.Bytes);
            header.CompressedLength = (ushort)compressedData.Length;
            header.Length           = (ushort)(header.Size + header.CompressedLength);

            // build final payload
            byte[] buffer = new byte[Util.RoundUp(header.Size + header.CompressedLength, 4)];
            header.WriteTo(buffer, 0);

            Array.Copy(compressedData, 0, buffer, header.Size, compressedData.Length);

#if TRACE
            Messages.Trace("Source HW Addr [" + packet.SourceHwAddress.ToString() + "]");
            Messages.Trace("Dest HW Addr [" + packet.DestinationHwAddress.ToString() + "]");

            if (packet != null)
            {
                Messages.Trace("[SNIP .. Packet Tx to Client]");
                Messages.TraceHex("hdr", buffer, header.Size);
                Messages.Trace("hdr->checksum = " + header.CRC.ToString("X"));
                Messages.Trace("hdr->length = " + header.Length.ToString());
                Messages.Trace("hdr->macAddr = " +
                               header.MacAddr[0].ToString("X") + ":" +
                               header.MacAddr[1].ToString("X") + ":" +
                               header.MacAddr[2].ToString("X") + ":" +
                               header.MacAddr[3].ToString("X") + ":" +
                               header.MacAddr[4].ToString("X") + ":" +
                               header.MacAddr[5].ToString("X"));
                Messages.Trace("hdr->dataLength = " + header.DataLength);
                Messages.Trace("hdr->compressLength = " + header.CompressedLength);
                Messages.Trace("hdr length " + header.Length);
                if (packet.Bytes != null)
                {
                    Messages.TraceHex("frame", packet.Bytes, packet.Bytes.Length);
                    Messages.Trace("frame length " + packet.Bytes.Length);
                }
                Messages.Trace("[SNIP .. Packet Tx to Client]");
            }
#endif

            // transmit
            if (Stream.CanWrite)
            {
                Stream.Flush();
                Stream.Write(buffer, 0, buffer.Length);
                Stream.Flush();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="pdu"></param>
        public void HandleIncoming(ProtocolDataUnit pdu)
        {
            if (isShutdown)
            {
                return;
            }
            if (client == null)
            {
                return;
            }
            if (!client.Connected)
            {
                return;
            }
#if TRACE
            if (pdu != null)
            {
                Messages.Trace("[SNIP .. Packet Tx To " + PhysicalAddress.ToString() + "]");
                Messages.TraceHex("hdr", pdu.HeaderData, pdu.HeaderData.Length);
                Messages.Trace("hdr->checksum = " + pdu.Header.CRC.ToString("X"));
                Messages.Trace("hdr->length = " + pdu.Header.Length.ToString());
                Messages.Trace("hdr->macAddr = " +
                               pdu.Header.MacAddr[0].ToString("X") + ":" +
                               pdu.Header.MacAddr[1].ToString("X") + ":" +
                               pdu.Header.MacAddr[2].ToString("X") + ":" +
                               pdu.Header.MacAddr[3].ToString("X") + ":" +
                               pdu.Header.MacAddr[4].ToString("X") + ":" +
                               pdu.Header.MacAddr[5].ToString("X"));
                Messages.Trace("hdr->dataLength = " + pdu.Header.DataLength);
                Messages.Trace("hdr->compressLength = " + pdu.Header.CompressedLength);
                Messages.Trace("hdr length " + pdu.Header.Length);
                if (pdu.ContentData != null)
                {
                    Messages.TraceHex("frame", pdu.ContentData, pdu.ContentData.Length);
                    Messages.Trace("frame length " + pdu.ContentData.Length);
                    Messages.Trace("MAC Destination = " +
                                   pdu.ContentData[0].ToString("X") + ":" +
                                   pdu.ContentData[1].ToString("X") + ":" +
                                   pdu.ContentData[2].ToString("X") + ":" +
                                   pdu.ContentData[3].ToString("X") + ":" +
                                   pdu.ContentData[4].ToString("X") + ":" +
                                   pdu.ContentData[5].ToString("X"));
                    Messages.Trace("MAC Source = " +
                                   pdu.ContentData[6].ToString("X") + ":" +
                                   pdu.ContentData[7].ToString("X") + ":" +
                                   pdu.ContentData[8].ToString("X") + ":" +
                                   pdu.ContentData[9].ToString("X") + ":" +
                                   pdu.ContentData[10].ToString("X") + ":" +
                                   pdu.ContentData[11].ToString("X"));
                }
                Messages.Trace("[SNIP .. Packet Tx To " + PhysicalAddress.ToString() + "]");
            }
#endif
            // repackage data
            HandshakeHeader header = new HandshakeHeader();
            header.CRC        = PacketCRC(pdu.ContentData, pdu.ContentData.Length);
            header.DataLength = (ushort)pdu.ContentData.Length;
            header.MacAddr    = pdu.Header.MacAddr;

            // compress data
            byte[] compressedData = ZStream.CompressBuffer(pdu.ContentData);
            header.CompressedLength = (ushort)compressedData.Length;
            header.Length           = (ushort)(header.Size + header.CompressedLength);

            // build final payload
            byte[] buffer = new byte[Util.RoundUp(header.Size + header.CompressedLength, 4)];
            header.WriteTo(buffer, 0);

            Array.Copy(compressedData, 0, buffer, header.Size, compressedData.Length);

            // transmit
            if (client.Connected)
            {
                if (Stream.CanWrite)
                {
                    Stream.Flush();
                    Stream.Write(buffer, 0, buffer.Length);
                    Stream.Flush();
                }
            }
        }