示例#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);
        }
示例#2
0
        /// <summary>
        /// Implements the comm manager thread.
        /// </summary>
        private void CommManagerThread()
        {
            byte[] rxBuf = new byte[65535];
            byte[] buf   = null;

            EndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);

            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
            socket.Bind(new IPEndPoint(IPAddress.Any, port));

            try
            {
                // implement main work loop
                while (runThread)
                {
                    try
                    {
                        int len = socket.ReceiveFrom(rxBuf, ref endPoint);
                        if (len > 0)
                        {
                            buf = new byte[len];
                            Buffer.BlockCopy(rxBuf, 0, buf, 0, buf.Length);
                        }
                        else
                        {
                            buf = null;
                        }

                        if (buf != null)
                        {
                            Util.TraceHex("buf", buf, buf.Length);

                            // read PDU data from stream
                            ProtocolDataUnit pdu = ProtocolDataUnit.ReadFrom(buf);
                            if (pdu != null && Program.foreground && Program.debug)
                            {
                                Console.WriteLine("[SNIP .. Packet Rx from Client]");
                                Util.TraceHex("hdr", pdu.HeaderData, pdu.HeaderData.Length);
                                if (pdu.ContentData != null)
                                {
                                    Util.TraceHex("packet", pdu.ContentData, pdu.ContentData.Length);
                                    Console.WriteLine("packet length " + pdu.ContentData.Length);
                                }
                                Console.WriteLine("hdr->checksum = " + pdu.Header.CheckSum.ToString("X"));
                                Console.WriteLine("hdr->length = " + pdu.Header.Length.ToString());
                                Console.WriteLine("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"));
                                Console.WriteLine("hdr->dataLength = " + pdu.Header.DataLength);
                                Console.WriteLine("hdr->compressLength = " + pdu.Header.CompressedLength);
                                Console.WriteLine("[SNIP .. Packet Rx from Client]");
                            }

                            // did we receive a pdu?
                            if (pdu != null)
                            {
                                // are we trying to shut down the session?
                                if ((pdu.Header.CheckSum == 0xFA) && (pdu.Header.DataLength == 0))
                                {
                                    if (Program.foreground)
                                    {
                                        Console.WriteLine("disconnecting session 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"));
                                    }

                                    PhysicalAddress addr = new PhysicalAddress(pdu.Header.MacAddr);
                                    if (this.endPoints.ContainsKey(addr))
                                    {
                                        this.endPoints.Remove(addr);
                                    }
                                    return;
                                }

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

                                    if (Program.foreground)
                                    {
                                        Console.WriteLine("new session 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"));
                                    }

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

                                    PhysicalAddress addr = new PhysicalAddress(header.MacAddr);

                                    if (!this.endPoints.ContainsKey(addr))
                                    {
                                        this.endPoints.Add(addr, (IPEndPoint)endPoint);
                                    }
                                    else
                                    {
                                        Console.WriteLine("macAddr already exists?");
                                    }
                                    socket.SendTo(buffer, endPoint);
                                }
                                else
                                {
                                    if (!Program.privateNetwork)
                                    {
                                        // otherwise handle actual packet data
                                        Packet packet = Packet.ParsePacket(LinkLayers.Ethernet, pdu.ContentData);
                                        capDevice.SendPacket(packet);
                                    }
                                    else
                                    {
                                        foreach (KeyValuePair <PhysicalAddress, IPEndPoint> kvp in endPoints)
                                        {
                                            // is this our mac?
                                            if (kvp.Key.GetAddressBytes() == pdu.Header.MacAddr)
                                            {
                                                continue;
                                            }

                                            SendPacket(kvp.Key, kvp.Value, pdu.ContentData);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // do nothing
                    }
                } // while (runThread)
            }
            catch (ThreadAbortException)
            {
                Console.WriteLine("comm manager thread commanded to abort!");
                runThread = false;
            }
            catch (Exception e)
            {
                Util.StackTrace(e, false);
                runThread = false; // terminate thread
            }
        }