public IcmpPacket(Types type, short id, short seq_num) { Type = type; Identifier = id; SequenceNumber = seq_num; Code = (byte)0; byte[] msg = new byte[64]; Random rand = new Random(); rand.NextBytes(msg); msg[0] = (byte)type; msg[1] = Code; msg[2] = (byte)0; msg[3] = (byte)0; NumberSerializer.WriteShort(Identifier, msg, 4); NumberSerializer.WriteShort(SequenceNumber, msg, 6); short checksum = (short)IPPacket.GenerateChecksum(MemBlock.Reference(msg)); NumberSerializer.WriteShort(checksum, msg, 2); _icpacket = MemBlock.Reference(msg); _packet = MemBlock.Reference(msg); }
public int CopyTo(byte[] dest, int off) { _data.CopyTo(dest, off); //The Hops is not neccesarily correct in _data NumberSerializer.WriteShort(Hops, dest, off); return(LENGTH); }
public AHHeader(short hops, short ttl, Address source, Address dest, ushort options) { //Make the header part: byte[] header = new byte[LENGTH]; int offset = 0; //Write hops: NumberSerializer.WriteShort(hops, header, offset); Hops = hops; offset += 2; NumberSerializer.WriteShort(ttl, header, offset); Ttl = ttl; offset += 2; _src = source; offset += source.CopyTo(header, offset); _dest = dest; offset += dest.CopyTo(header, offset); Opts = options; NumberSerializer.WriteShort((short)options, header, offset); offset += 2; _data = MemBlock.Reference(header, 0, offset); }
public void Send(ICopyable data) { //We always use the fragmenting header to signal //to the receiving end that we support (and want) //fragmentation. MemBlock rest = data as MemBlock; if (null == rest) { rest = MemBlock.Copy(data); } int len = rest.Length; if (len > MAX_SEND_SIZE) { throw new SendException(true, System.String.Format("Packet too large: {0}, MaxSize = {1}", len, MAX_SEND_SIZE)); } uint crc32 = rest.Read <uint>(Crc32.ComputeChecksum); //Avoid a RNG operation for now, might need to reconsider //If we use RNG, we have to touch state, that has thread-safety issues int id = _wrapped_sender.GetHashCode() ^ rest.GetHashCode(); byte[] header = new byte[HEADER_SIZE - 2]; int off = FragPType.CopyTo(header, 0); NumberSerializer.WriteInt((int)crc32, header, off); NumberSerializer.WriteInt(id, header, off + 4); MemBlock header_mb = MemBlock.Reference(header); ushort block = 0; int blocksize = ComputeBlockSize(len); while (rest.Length > 0) { int this_size = System.Math.Min(blocksize, rest.Length); MemBlock this_block = rest.Slice(0, this_size); rest = rest.Slice(this_size); //Check to see if this is the last block: if (rest.Length == 0) { block = (ushort)(block ^ (0x8000)); //Set the highest bit, this is last } byte[] block_b = new byte[2]; NumberSerializer.WriteShort((short)block, block_b, 0); MemBlock block_mb = MemBlock.Reference(block_b); _wrapped_sender.Send(new CopyList(header_mb, block_mb, this_block)); block += 1; } }
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) ); }