public void HandleData(MemBlock packet, ISender from, object node) { _message_count++; long stop_time, rt_ticks = -10000; if ( !from.Equals(node)) { if (packet[0] == 0) { //log.Debug("Echo Response:"); stop_time = System.DateTime.Now.Ticks; int received_uid = NumberSerializer.ReadInt(packet, 1); if(uid_starttime.ContainsKey(received_uid)){ rt_ticks = stop_time - (long)EchoTester.uid_starttime[received_uid]; } double rt_ms = (double) rt_ticks/10000.0; uid_brunetpingtime.Add(received_uid, rt_ms); Console.WriteLine("Packet ID = {0}, Round-trip = {1}", received_uid, rt_ms); } else { //log.Debug("Echo Request:"); } //log.Debug(packet.ToString()); //System.Console.WriteLine("{0}", packet.ToString()); if (packet[0] > 0) { //Send a reply back, this is a request byte[] new_payload = new byte[ packet.Length ]; packet.CopyTo(new_payload, 0); new_payload[0] = (byte) 0; from.Send(new CopyList(PType.Protocol.Echo, MemBlock.Reference(new_payload))); } } }
public static RpcDHCPServer GetRpcDHCPServer(MemBlock ip, MemBlock netmask) { DHCPConfig config = new DHCPConfig(); config.LeaseTime = 3200; config.Netmask = Utils.MemBlockToString(netmask, '.'); config.IPBase = Utils.MemBlockToString(ip, '.'); config.ReservedIPs = new DHCPConfig.ReservedIP[1]; config.ReservedIPs[0] = new DHCPConfig.ReservedIP(); byte[] local_ip = new byte[4]; ip.CopyTo(local_ip, 0); local_ip[3] = 2; config.ReservedIPs[0].IPBase = Utils.BytesToString(local_ip, '.'); config.ReservedIPs[0].Mask = "255.255.255.255"; return new RpcDHCPServer(config); }
public DHCPPacket(byte op, MemBlock xid, MemBlock ciaddr, MemBlock yiaddr, MemBlock siaddr, MemBlock chaddr, Dictionary<OptionTypes, MemBlock> Options) { this.op = op; this.xid = xid; this.ciaddr = ciaddr; this.yiaddr = yiaddr; this.siaddr = siaddr; this.chaddr = chaddr; this.Options = Options; byte[] header = new byte[240]; header[0] = op; header[1] = 1; header[2] = (byte) chaddr.Length; header[3] = 0; xid.CopyTo(header, 4); for(int i = 8; i < 12; i++) { header[i] = 0; } ciaddr.CopyTo(header, 12); yiaddr.CopyTo(header, 16); siaddr.CopyTo(header, 20); for(int i = 24; i < 28; i++) { header[i] = 0; } chaddr.CopyTo(header, 28); for(int i = 34; i < 236; i++) { header[i] = 0; } magic_key.CopyTo(header, 236); _icpacket = new CopyList(MemBlock.Reference(header)); foreach(KeyValuePair<OptionTypes, MemBlock> kvp in Options) { MemBlock value = kvp.Value; byte[] tmp = new byte[2]; tmp[0] = (byte) kvp.Key; tmp[1] = (byte) value.Length; _icpacket = new CopyList(_icpacket, MemBlock.Reference(tmp), value); } byte []end = new byte[1]{255}; /* End of Options */ _icpacket = new CopyList(_icpacket, MemBlock.Reference(end)); }
protected byte[] GenToken(MemBlock key) { RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider(); byte[] token = new byte[20]; provider.GetBytes(token); byte[] res = new byte[40]; key.CopyTo(res, 0); token.CopyTo(res, 20); return res; }
public static MemBlock MakePseudoHeader(MemBlock SourceIP, MemBlock DestinationIP, byte Protocol, int Length) { byte[] pseudoheader = new byte[12]; SourceIP.CopyTo(pseudoheader, 0); DestinationIP.CopyTo(pseudoheader, 4); pseudoheader[9] = Protocol; pseudoheader[10] = (byte) ((Length >> 8) & 0xFF); pseudoheader[11] = (byte) (Length & 0xFF); return MemBlock.Reference(pseudoheader); }
/** <summary>If we're not creating a packet from scratch, this will keep its integrity and transform UDP and TCP headers as well (due to their checksums being dependent on source and destination ip addresses.</summary> <param name="Packet">The packet to translate.</param> <param name="SourceIP">The new source ip.</param> <param name="DestinationIP">The new destination ip.</param> */ public static MemBlock Translate(MemBlock Packet, MemBlock SourceIP, MemBlock DestinationIP) { byte[] new_packet = new byte[Packet.Length]; Packet.CopyTo(new_packet, 0); int length = (Packet[0] & 0xF) * 4; SourceIP.CopyTo(new_packet, 12); // Do not copy over multicast addresses! if(new_packet[16] < 224 || new_packet[16] > 239) { DestinationIP.CopyTo(new_packet, 16); } // Zero out the checksum so we don't use it in our future calculations new_packet[10] = 0; new_packet[11] = 0; MemBlock header = MemBlock.Reference(new_packet, 0, length); int checksum = GenerateChecksum(header); new_packet[10] = (byte) ((checksum >> 8) & 0xFF); new_packet[11] = (byte) (checksum & 0xFF); Protocols p = (Protocols) Packet[9]; bool fragment = ((Packet[6] & 0x1F) | Packet[7]) != 0; if(p == Protocols.UDP && !fragment) { // Zero out the checksum to disable it new_packet[length + 6] = 0; new_packet[length + 7] = 0; } else if(p == Protocols.TCP && !fragment) { // Zero out the checksum so we don't use it in our future calculations new_packet[length + 16] = 0; new_packet[length + 17] = 0; MemBlock payload = MemBlock.Reference(new_packet).Slice(length); MemBlock pseudoheader = IPPacket.MakePseudoHeader(SourceIP, DestinationIP, (byte) Protocols.TCP, Packet.Length - 20); checksum = IPPacket.GenerateChecksum(payload, pseudoheader); new_packet[length + 16] = (byte) ((checksum >> 8) & 0xFF); new_packet[length + 17] = (byte) (checksum & 0xFF); } return MemBlock.Reference(new_packet); }
public static double ReadDouble(MemBlock mb) { byte[] bin = new byte[8]; mb.CopyTo(bin,0); return ReadDouble(bin, 0); }
public static float ReadFloat(MemBlock mb) { byte[] bin = new byte[4]; mb.CopyTo(bin,0); return ReadFloat(bin, 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); }