Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="endPoint"></param>
        /// <param name="buf"></param>
        private void SendPacket(PhysicalAddress addr, IPEndPoint endPoint, byte[] buf)
        {
            HandshakeHeader header = new HandshakeHeader();

            header.CheckSum   = PacketCRC(buf, buf.Length);
            header.DataLength = (ushort)buf.Length;
            header.MacAddr    = addr.GetAddressBytes();

            // compress data
            byte[] compressedData = ZStream.CompressBuffer(buf);
            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 (Program.foreground && Program.debug)
            {
                if (buf != null)
                {
                    Console.WriteLine("[SNIP .. Packet Tx to Client]");
                    Util.TraceHex("hdr", buffer, header.Size);
                    if (buf != null)
                    {
                        Util.TraceHex("packet", buf, buf.Length);
                        Console.WriteLine("packet length " + buf.Length);
                    }
                    Console.WriteLine("hdr->checksum = " + header.CheckSum.ToString("X"));
                    Console.WriteLine("hdr->length = " + header.Length.ToString());
                    Console.WriteLine("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"));
                    Console.WriteLine("hdr->dataLength = " + header.DataLength);
                    Console.WriteLine("hdr->compressLength = " + header.CompressedLength);
                    Console.WriteLine("[SNIP .. Packet Tx to Client]");
                }
            }

            socket.SendTo(buffer, endPoint);
        }
Ejemplo n.º 2
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.º 3
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();
                }
            }
        }