Пример #1
0
 /// <summary>Create a new Attribute.</summary>
 public Attribute(AttributeType type, MemBlock value)
 {
     Type  = type;
     Value = value;
     byte[] data = new byte[4 + value.Length];
     NumberSerializer.WriteUShort((ushort)type, data, 0);
     NumberSerializer.WriteUShort((ushort)value.Length, data, 2);
     value.CopyTo(data, 4);
     Data = MemBlock.Reference(data);
 }
Пример #2
0
        public static MemBlock MakePseudoHeader(MemBlock source_address,
                                                MemBlock destination_address, byte protocol, ushort length)
        {
            byte[] pseudoheader = new byte[source_address.Length + destination_address.Length + 4];
            int    pos          = 0;

            source_address.CopyTo(pseudoheader, pos);
            pos += source_address.Length;
            destination_address.CopyTo(pseudoheader, pos);
            pos += destination_address.Length;
            pseudoheader[++pos] = protocol;
            NumberSerializer.WriteUShort(length, pseudoheader, ++pos);
            return(MemBlock.Reference(pseudoheader));
        }
Пример #3
0
        public ArpPacket(int HardwareType, int ProtocolType, Operations Operation,
                         MemBlock SenderHWAddress, MemBlock SenderProtoAddress, MemBlock TargetHWAddress,
                         MemBlock TargetProtoAddress)
        {
            byte[] header = new byte[8];
            NumberSerializer.WriteUShort((ushort)HardwareType, header, 0);
            NumberSerializer.WriteUShort((ushort)ProtocolType, header, 2);
            header[4] = (byte)SenderHWAddress.Length;
            header[5] = (byte)SenderProtoAddress.Length;
            NumberSerializer.WriteUShort((ushort)Operation, header, 6);

            _icpacket = new CopyList(MemBlock.Reference(header), SenderHWAddress,
                                     SenderProtoAddress, TargetHWAddress, TargetProtoAddress);
        }
Пример #4
0
 public ForwardingSender(Node n, Address forwarder, ushort init_option, Address destination, short ttl, ushort option)
 {
     _n        = n;
     _dest     = destination;
     _sender   = new AHSender(n, forwarder, init_option);
     _f_ttl    = ttl;
     _f_option = option;
     byte[] f_buffer = new byte[4];
     NumberSerializer.WriteShort(ttl, f_buffer, 0);
     NumberSerializer.WriteUShort(option, f_buffer, 2);
     _header = new CopyList(PType.Protocol.Forwarding,
                            MemBlock.Reference(new byte[] { 0 }),
                            destination,
                            MemBlock.Reference(f_buffer)
                            );
 }
Пример #5
0
 public static MemBlock CreateAddressData(IPAddress ip, ushort port)
 {
     byte[] addr = ip.GetAddressBytes();
     byte[] data = new byte[4 + addr.Length];
     if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
     {
         data[1] = (byte)FamilyType.IPv4;
     }
     else
     {
         data[1] = (byte)FamilyType.IPv6;
     }
     NumberSerializer.WriteUShort(port, data, 2);
     addr.CopyTo(data, 4);
     return(MemBlock.Reference(data));
 }
Пример #6
0
        /// <summary>Generate an Stun packet.</summary>
        public StunPacket(ClassType ct, MessageType mt, List <Attribute> attributes)
        {
            Class      = ct;
            Message    = mt;
            Attributes = new List <Attribute>(attributes);

            int size = MINIMUM_SIZE;

            // Let's get the size of this packet
            foreach (Attribute attr in attributes)
            {
                size += attr.Data.Length;
            }

            byte[] data = new byte[size];
            // Make the message
            ushort message = (ushort)((ushort)ct | (ushort)mt);

            NumberSerializer.WriteUShort(message, data, 0);
            // Write the size
            NumberSerializer.WriteUShort((ushort)(size - MINIMUM_SIZE), data, 2);
            // Add the cookie
            MAGIC_COOKIE.CopyTo(data, 4);
            // Add a transaction id
            byte[] transaction_id = new byte[12];
            (new Random()).NextBytes(transaction_id);
            transaction_id.CopyTo(data, 8);
            TransactionID = MemBlock.Reference(data, 8, 12);
            // Finished

            int offset = MINIMUM_SIZE;

            foreach (Attribute attr in attributes)
            {
                attr.Data.CopyTo(data, offset);
                offset += attr.Data.Length;
            }
            Data = MemBlock.Reference(data);
        }