Example #1
0
        public override byte[] GetBytes()
        {
            int nLength = 6;

            if (AddressType == AddressType.IPV4)
            {
                nLength += 4;
            }
            else if (AddressType == AddressType.IPV6)
            {
                nLength += 16;
            }
            else if (AddressType == AddressType.DomainName)
            {
                nLength += (1 + DestinationDomain.Length);
            }

            m_bBytes    = new byte[nLength];
            m_bBytes[0] = Version;
            m_bBytes[1] = (byte)SOCKSCommand;
            m_bBytes[2] = (byte)Reserved;
            m_bBytes[3] = (byte)AddressType;
            if (AddressType == AddressType.IPV4)
            {
                byte[] bAddr = DestinationAddress.GetAddressBytes();
                Array.Copy(bAddr, 0, m_bBytes, 4, bAddr.Length);

                m_bBytes[4 + bAddr.Length]     = (byte)((DestinationPort & 0xFF00) >> 8);
                m_bBytes[4 + bAddr.Length + 1] = (byte)((DestinationPort & 0xFF) >> 0);
            }
            else if (AddressType == AddressType.IPV6)
            {
                byte[] bAddr = DestinationAddress.GetAddressBytes();
                Array.Copy(bAddr, 0, m_bBytes, 4, bAddr.Length);

                m_bBytes[4 + bAddr.Length]     = (byte)((DestinationPort & 0xFF00) >> 8);
                m_bBytes[4 + bAddr.Length + 1] = (byte)((DestinationPort & 0xFF) >> 0);
            }
            else if (AddressType == AddressType.DomainName)
            {
                byte[] bAddr = System.Text.UTF8Encoding.UTF8.GetBytes(DestinationDomain);
                m_bBytes[4] = (byte)bAddr.Length;
                Array.Copy(bAddr, 0, m_bBytes, 4 + 1, bAddr.Length);

                m_bBytes[4 + 1 + bAddr.Length]     = (byte)((DestinationPort & 0xFF00) >> 8);
                m_bBytes[4 + 1 + bAddr.Length + 1] = (byte)((DestinationPort & 0xFF) >> 0);
            }


            return(m_bBytes);
        }
Example #2
0
        public byte[] PacketToBytes()
        {
            List <byte> packetbytes = new List <byte>();

            PacketLength = PacketHeaderLength + Payload.Length;

            packetbytes.AddRange(BitConverter.GetBytes(PacketLength));
            packetbytes.AddRange(BitConverter.GetBytes(ID));
            packetbytes.AddRange(SourceAddress.GetAddressBytes());
            packetbytes.AddRange(DestinationAddress.GetAddressBytes());
            packetbytes.AddRange(BitConverter.GetBytes(Frequency));
            packetbytes.AddRange(BitConverter.GetBytes(Bandwith));
            packetbytes.AddRange(BitConverter.GetBytes(BitRate));
            packetbytes.AddRange(BitConverter.GetBytes(Performance));
            packetbytes.AddRange(BitConverter.GetBytes(Port));
            packetbytes.AddRange(BitConverter.GetBytes(slotWindow.FirstSlot));
            packetbytes.AddRange(BitConverter.GetBytes(slotWindow.NumberofSlots));
            packetbytes.AddRange(Encoding.ASCII.GetBytes(Payload ?? ""));
            return(packetbytes.ToArray());
        }
Example #3
0
        public byte[] convertToBytes()
        {
            List <byte> package_in_Bytes = new List <byte>();
            // const int headerLength=24+label
            int headerLength = 24 + labelStack.GetLengthOfStack();

            package_length = payload.Length + headerLength;

            package_in_Bytes.AddRange(labelStack.GetStackInBytes());           //length of stack
            package_in_Bytes.AddRange(BitConverter.GetBytes(messageID));       // 4 bytes
            package_in_Bytes.AddRange(BitConverter.GetBytes(package_length));  //4bytes
            package_in_Bytes.AddRange(BitConverter.GetBytes(TTL));             // 2 bytes
            package_in_Bytes.AddRange(SourceAddress.GetAddressBytes());        //4 bytes
            package_in_Bytes.AddRange(DestinationAddress.GetAddressBytes());   // 4 bytes
            package_in_Bytes.AddRange(BitConverter.GetBytes(Port));            // 2 bytes
            package_in_Bytes.AddRange(CurrentNodeIP.GetAddressBytes());        //4 bytes
            package_in_Bytes.AddRange(Encoding.ASCII.GetBytes(payload ?? "")); // length of payload

            //package_length = package_in_Bytes.Count;
            //Console.WriteLine("Package length: " +package_length);
            return(package_in_Bytes.ToArray());
        }