private void HandleClanMemberInformation(ParseData pd) { DataReader dr = new DataReader(pd.Data); int cookie = dr.ReadInt32(); if (!m_warcraftProfileRequests.ContainsKey(cookie)) { Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Unable to locate profile request with cookie {0:x2}", cookie)); return; } WarcraftProfileEventArgs args = m_warcraftProfileRequests[cookie]; byte success = dr.ReadByte(); if (success != 0) { m_warcraftProfileRequests.Remove(cookie); ProfileLookupFailedEventArgs profileFailed = new ProfileLookupFailedEventArgs(args.Username, args.Product) { EventData = pd }; OnProfileLookupFailed(profileFailed); return; } string clanName = dr.ReadCString(); ClanRank rank = (ClanRank)dr.ReadByte(); DateTime joined = DateTime.FromFileTime(dr.ReadInt64()); args.Clan = new ClanProfile(clanName, rank, joined); BncsPacket pck = new BncsPacket((byte)BncsPacketId.WarcraftGeneral); pck.InsertByte((byte)WarcraftCommands.ClanInfoRequest); pck.InsertInt32(cookie); pck.InsertDwordString(args.Profile.ClanTag, 0); pck.InsertDwordString(args.Product.ProductCode); Send(pck); BattleNetClientResources.IncomingBufferPool.FreeBuffer(pd.Data); }
public override void ExecuteRequest() { DataBuffer buf1 = new DataBuffer(); buf1.InsertInt16(20); buf1.InsertInt16(0x0200); buf1.InsertDwordString("IX86"); buf1.InsertDwordString(Product); if (m_ad) { buf1.InsertInt32(m_adId); buf1.InsertDwordString(m_adExt); } else { buf1.InsertInt64(0); } Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sck.Connect(Server, 6112); sck.Send(new byte[] { 2 }); sck.Send(buf1.UnderlyingBuffer, 0, buf1.Count, SocketFlags.None); NetworkStream ns = new NetworkStream(sck, false); DataReader rdr = new DataReader(ns, 4); int serverToken = rdr.ReadInt32(); DataBuffer buf2 = new DataBuffer(); buf2.InsertInt32(0); // no resuming if (FileTime.HasValue) { buf2.InsertInt64(FileTime.Value.ToFileTimeUtc()); } else { buf2.InsertInt64(0); } int clientToken = new Random().Next(); buf2.InsertInt32(clientToken); buf2.InsertInt32(m_key.Key.Length); buf2.InsertInt32(m_key.Product); buf2.InsertInt32(m_key.Value1); buf2.InsertInt32(0); buf2.InsertByteArray(m_key.GetHash(clientToken, serverToken)); buf2.InsertCString(FileName); sck.Send(buf2.UnderlyingBuffer, 0, buf2.Count, SocketFlags.None); rdr = new DataReader(ns, 4); int msg2Size = rdr.ReadInt32() - 4; rdr = new DataReader(ns, msg2Size); this.FileSize = rdr.ReadInt32(); rdr.Seek(8); long fileTime = rdr.ReadInt64(); DateTime time = DateTime.FromFileTimeUtc(fileTime); string name = rdr.ReadCString(); if (string.Compare(name, FileName, StringComparison.OrdinalIgnoreCase) != 0 || FileSize == 0) { throw new FileNotFoundException(Resources.bnftp_filenotfound); } byte[] data = ReceiveLoop(sck, FileSize); sck.Close(); FileStream fs = new FileStream(LocalFileName, FileMode.OpenOrCreate, FileAccess.Write); fs.Write(data, 0, FileSize); fs.Flush(); fs.Close(); }
/// <summary> /// Executes the BnFTP request, downloading the file to where <see cref="BnFtpRequestBase.LocalFileName">LocalFileName</see> /// specifies, and closes the connection. /// </summary> /// <remarks> /// <para>By default, <c>LocalFileName</c> is the same name as the remote file, which will cause the file /// to be saved in the local application path. The desired location of the file must be set before /// <b>ExecuteRequest</b> is called.</para> /// </remarks> /// <exception cref="IOException">Thrown if the local file cannot be written.</exception> /// <exception cref="SocketException">Thrown if the remote host closes the connection prematurely.</exception> public override void ExecuteRequest() { DataBuffer buffer = new DataBuffer(); buffer.InsertInt16((short)(33 + FileName.Length)); buffer.InsertInt16(0x0100); buffer.InsertDwordString("IX86"); buffer.InsertDwordString(Product); if (m_ad) { buffer.InsertInt32(m_adId); buffer.InsertDwordString(m_adExt); } else { buffer.InsertInt64(0); } // currently resuming is not supported buffer.InsertInt32(0); if (FileTime.HasValue) { buffer.InsertInt64(FileTime.Value.ToFileTimeUtc()); } else { buffer.InsertInt64(0); } buffer.InsertCString(FileName); Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sck.Connect(Server, 6112); sck.Send(new byte[] { 2 }); sck.Send(buffer.UnderlyingBuffer, 0, buffer.Count, SocketFlags.None); BattleNetClientResources.OutgoingBufferPool.FreeBuffer(buffer.UnderlyingBuffer); byte[] hdrLengthBytes = new byte[2]; sck.Receive(hdrLengthBytes, 2, SocketFlags.None); int hdrLen = BitConverter.ToInt16(hdrLengthBytes, 0); Debug.WriteLine(hdrLen, "Header Length"); byte[] hdrBytes = new byte[hdrLen - 2]; sck.Receive(hdrBytes, hdrLen - 2, SocketFlags.None); DataReader rdr = new DataReader(hdrBytes); rdr.Seek(2); int fileSize = rdr.ReadInt32(); this.FileSize = fileSize; rdr.Seek(8); long fileTime = rdr.ReadInt64(); string name = rdr.ReadCString(); if (string.Compare(name, FileName, StringComparison.OrdinalIgnoreCase) != 0 || FileSize == 0) { throw new FileNotFoundException(Resources.bnftp_filenotfound); } Debug.WriteLine(fileSize, "File Size"); byte[] data = ReceiveLoop(sck, fileSize); sck.Close(); FileStream fs = new FileStream(LocalFileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); fs.SetLength(fileSize); fs.Write(data, 0, fileSize); fs.Flush(); fs.Close(); DateTime time = DateTime.FromFileTimeUtc(fileTime); File.SetLastWriteTimeUtc(LocalFileName, time); }
private void HandleWarcraftUserInfoRequest(ParseData data, DataReader dr) { int cookie = dr.ReadInt32(); if (!m_warcraftProfileRequests.ContainsKey(cookie)) { Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Unable to locate profile request with cookie {0:x2}", cookie)); return; } WarcraftProfileEventArgs args = m_warcraftProfileRequests[cookie]; string iconID = dr.ReadDwordString(0); args.Profile.IconID = iconID; int recordCount = dr.ReadByte(); WarcraftLadderRecord[] ladderRecords = new WarcraftLadderRecord[recordCount]; for (int i = 0; i < recordCount; i++) { WarcraftLadderType ladderType = (WarcraftLadderType)dr.ReadInt32(); int wins = dr.ReadInt16(); int losses = dr.ReadInt16(); int level = dr.ReadByte(); int hrs = dr.ReadByte(); int xp = dr.ReadInt16(); int rank = dr.ReadInt32(); WarcraftLadderRecord record = new WarcraftLadderRecord(ladderType, wins, losses, level, hrs, xp, rank); ladderRecords[i] = record; } int raceRecordCount = dr.ReadByte(); Warcraft3IconRace[] raceOrder = new Warcraft3IconRace[] { Warcraft3IconRace.Random, Warcraft3IconRace.Human, Warcraft3IconRace.Orc, Warcraft3IconRace.Undead, Warcraft3IconRace.NightElf, Warcraft3IconRace.Tournament }; WarcraftRaceRecord[] raceRecords = new WarcraftRaceRecord[raceRecordCount]; for (int i = 0; i < raceRecordCount; i++) { int wins = dr.ReadInt16(); int losses = dr.ReadInt16(); WarcraftRaceRecord record = new WarcraftRaceRecord(raceOrder[i], wins, losses); raceRecords[i] = record; } int teamRecordsCount = dr.ReadByte(); ArrangedTeamRecord[] teamRecords = new ArrangedTeamRecord[teamRecordsCount]; for (int i = 0; i < teamRecordsCount; i++) { ArrangedTeamType teamType = (ArrangedTeamType)dr.ReadInt32(); int wins = dr.ReadInt16(); int losses = dr.ReadInt16(); int level = dr.ReadByte(); int hrs = dr.ReadByte(); int xp = dr.ReadInt16(); int rank = dr.ReadInt32(); long ftLastGameplay = dr.ReadInt64(); DateTime lastGamePlayed = DateTime.FromFileTime(ftLastGameplay); int numPartners = dr.ReadByte(); string[] partnerList = new string[numPartners]; for (int p = 0; p < numPartners; p++) partnerList[p] = dr.ReadCString(); ArrangedTeamRecord record = new ArrangedTeamRecord(teamType, wins, losses, level, hrs, xp, rank, lastGamePlayed, partnerList); teamRecords[i] = record; } args.Profile.SetStats(ladderRecords, teamRecords, raceRecords); args.EventData = data; OnWarcraftProfileReceived(args); }
private void HandleAuthInfo(ParseData data) { try { DataReader dr = new DataReader(data.Data); if (m_pingPck != null) { Send(m_pingPck); m_pingPck = null; } m_received0x50 = true; m_loginType = dr.ReadUInt32(); m_srvToken = dr.ReadUInt32(); m_udpVal = dr.ReadUInt32(); m_mpqFiletime = dr.ReadInt64(); m_versioningFilename = dr.ReadCString(); m_usingLockdown = m_versioningFilename.StartsWith("LOCKDOWN", StringComparison.OrdinalIgnoreCase); int crResult = -1, exeVer = -1; string exeInfo = null; if (!m_usingLockdown) { m_valString = dr.ReadCString(); int mpqNum = CheckRevision.ExtractMPQNumber(m_versioningFilename); crResult = CheckRevision.DoCheckRevision(m_valString, new string[] { m_settings.GameExe, m_settings.GameFile2, m_settings.GameFile3 }, mpqNum); exeVer = CheckRevision.GetExeInfo(m_settings.GameExe, out exeInfo); } else { m_ldValStr = dr.ReadNullTerminatedByteArray(); string dllName = m_versioningFilename.Replace(".mpq", ".dll"); BnFtpVersion1Request req = new BnFtpVersion1Request(m_settings.Client, m_versioningFilename, null); req.Server = m_settings.Gateway.ServerHost; req.LocalFileName = Path.Combine(Path.GetTempPath(), m_versioningFilename); req.ExecuteRequest(); string ldPath = null; using (MpqArchive arch = MpqServices.OpenArchive(req.LocalFileName)) { if (arch.ContainsFile(dllName)) { ldPath = Path.Combine(Path.GetTempPath(), dllName); arch.SaveToPath(dllName, Path.GetTempPath(), false); } } m_ldDigest = CheckRevision.DoLockdownCheckRevision(m_ldValStr, new string[] { m_settings.GameExe, m_settings.GameFile2, m_settings.GameFile3 }, ldPath, m_settings.ImageFile, ref exeVer, ref crResult); } m_prodCode = m_settings.Client; if (m_prodCode == "WAR3" || m_prodCode == "W3XP") { m_w3srv = dr.ReadByteArray(128); if (!NLS.ValidateServerSignature(m_w3srv, RemoteEP.Address.GetAddressBytes())) { OnError(new ErrorEventArgs(ErrorType.Warcraft3ServerValidationFailure, Strings.War3ServerValidationFailed, false)); //Close(); //return; } } BattleNetClientResources.IncomingBufferPool.FreeBuffer(data.Data); CdKey key1, key2 = null; key1 = new CdKey(m_settings.CdKey1); if (m_prodCode == "D2XP" || m_prodCode == "W3XP") { key2 = new CdKey(m_settings.CdKey2); } m_clientToken = unchecked((uint)new Random().Next()); byte[] key1Hash = key1.GetHash(m_clientToken, m_srvToken); if (m_warden != null) { try { if (!m_warden.InitWarden(BitConverter.ToInt32(key1Hash, 0))) { m_warden.UninitWarden(); OnError(new ErrorEventArgs(ErrorType.WardenModuleFailure, "The Warden module failed to initialize. You will not be immediately disconnected; however, you may be disconnected after a short period of time.", false)); m_warden = null; } } catch (Win32Exception we) { OnError(new ErrorEventArgs(ErrorType.WardenModuleFailure, "The Warden module failed to initialize. You will not be immediately disconnected; however, you may be disconnected after a short period of time.", false)); OnError(new ErrorEventArgs(ErrorType.WardenModuleFailure, string.Format(CultureInfo.CurrentCulture, "Additional information: {0}", we.Message), false)); m_warden.UninitWarden(); m_warden = null; } } BncsPacket pck0x51 = new BncsPacket((byte)BncsPacketId.AuthCheck); pck0x51.Insert(m_clientToken); pck0x51.Insert(exeVer); pck0x51.Insert(crResult); if (m_prodCode == "D2XP" || m_prodCode == "W3XP") pck0x51.Insert(2); else pck0x51.Insert(1); pck0x51.Insert(false); pck0x51.Insert(key1.Key.Length); pck0x51.Insert(key1.Product); pck0x51.Insert(key1.Value1); pck0x51.Insert(0); pck0x51.Insert(key1Hash); if (key2 != null) { pck0x51.Insert(key2.Key.Length); pck0x51.Insert(key2.Product); pck0x51.Insert(key2.Value1); pck0x51.Insert(0); pck0x51.Insert(key2.GetHash(m_clientToken, m_srvToken)); } if (m_usingLockdown) { pck0x51.InsertByteArray(m_ldDigest); pck0x51.InsertByte(0); } else pck0x51.InsertCString(exeInfo); pck0x51.InsertCString(m_settings.CdKeyOwner); Send(pck0x51); } catch (Exception ex) { OnError(new ErrorEventArgs(ErrorType.General, "There was an error while initializing your client. Refer to the exception message for more information.\n" + ex.ToString(), true)); Close(); } }
private void HandleCheckAd(ParseData data) { DataReader dr = new DataReader(data.Data); int adID = dr.ReadInt32(); dr.Seek(4); // extension long filetime = dr.ReadInt64(); DateTime ft = DateTime.FromFileTimeUtc(filetime); string filename = dr.ReadCString(Encoding.ASCII); string link = dr.ReadCString(Encoding.ASCII); AdChangedEventArgs args = new AdChangedEventArgs(adID, ft, filename, link); m_lastAd = adID; OnAdChanged(args); BattleNetClientResources.IncomingBufferPool.FreeBuffer(data.Data); }