public override void ParseBytes(byte[] data) { if (Version == P2PVersion.P2PV1) { base.ParseBytes(data); P2Pv1Header head = this.V1Header; Guid = new Guid( (int)head.AckSessionId, (short)(head.AckIdentifier & 0x0000FFFF), (short)((head.AckIdentifier & 0xFFFF0000) >> 16), (byte)((head.AckTotalSize & 0x00000000000000FF)), (byte)((head.AckTotalSize & 0x000000000000FF00) >> 8), (byte)((head.AckTotalSize & 0x0000000000FF0000) >> 16), (byte)((head.AckTotalSize & 0x00000000FF000000) >> 24), (byte)((head.AckTotalSize & 0x000000FF00000000) >> 32), (byte)((head.AckTotalSize & 0x0000FF0000000000) >> 40), (byte)((head.AckTotalSize & 0x00FF000000000000) >> 48), (byte)((head.AckTotalSize & 0xFF00000000000000) >> 56) ); } else { // Don't call base.ParseBytes(); Data is 16 bytes for v2. Guid = HashedNonceGenerator.CreateGuidFromData(Version, data); } InnerBody = new byte[0]; }
private P2PDCHandshakeMessage VerifyHandshake(byte[] data) { P2PVersion authVersion = P2PVersion.P2PV1; P2PDCHandshakeMessage ret = null; if (data.Length == 48) { authVersion = P2PVersion.P2PV1; } else if (data.Length == 16) { authVersion = P2PVersion.P2PV2; } else { Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning, "Invalid handshake length, the data was: " + Encoding.ASCII.GetString(data), GetType().Name); return(null); } if (authVersion != this.version) { Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning, String.Format("Received version is {0}, expected {1}", authVersion, this.version), GetType().Name); return(null); } P2PDCHandshakeMessage incomingHandshake = new P2PDCHandshakeMessage(version); incomingHandshake.ParseBytes(data); Guid incomingGuid = incomingHandshake.Guid; if (incomingHandshake.Version == P2PVersion.P2PV1 && (P2PFlag.DirectHandshake != (incomingHandshake.V1Header.Flags & P2PFlag.DirectHandshake))) { Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning, "Handshake flag not set for v1, the flag was: " + incomingHandshake.V1Header.Flags, GetType().Name); return(null); } Guid compareGuid = incomingGuid; if (needHash) { compareGuid = HashedNonceGenerator.HashNonce(compareGuid); } if (this.nonce == compareGuid) { ret = new P2PDCHandshakeMessage(version); ret.ParseBytes(data); // copy identifiers ret.Guid = compareGuid; // set new guid (hashed) ret.Header.Identifier = 0; // our id return(ret); // OK this is our handshake message } return(null); }