#include "main.h" #include #ifdef WIN32 #include #endif unsigned long gAnetIps[15]; void PacketChop3(unsigned char* pDataArray, NetMessage* pData, int size, Server* se) { assert(pDataArray!=NULL); if (size==-1){return;} if (size==pData->dataSize){return;} NetMessage s; s.pData=pDataArray+size; s.dataSize=pData->dataSize-size; s.networkId=pData->networkId; se->AddIncomingFront(&s); } bool PopulateAnetIps() { //Gets IP addresses of ArenaNet file servers for(int i=0; i<13; i++) { char file_server_hostname[100]; sprintf(file_server_hostname, "file%u.arenanetworks.com", i); //Resolve the server host name gCoreLog.notice("Resolving %s...", file_server_hostname); hostent* pHost=gethostbyname(file_server_hostname); if (pHost == NULL) { gCoreLog.error("Host name lookup failed!"); return false; } in_addr addr; addr.s_addr = *(u_long *) pHost->h_addr_list[0]; gCoreLog.notice("Resolved: %s", inet_ntoa(addr)); gAnetIps[i]=addr.s_addr; } return true; } bool GetFileFromAnet(unsigned long hash) { //Select a random ArenaNet file server int file_server_id=1 + (rand() % 12); unsigned long ip=gAnetIps[file_server_id]; //Establish a connection to the server Server* client=new Server; if (client == NULL) { gCoreLog.error("Out of memory!"); return false; } //Initialize the connection if (client->Init(6112, "Client", NULL, true, &gCoreLog) == -1) { gCoreLog.error("Could not initialize file client!"); delete client; return false; } SERVERCONFIG server_config; server_config.maxConnections=10; client->UpdateServerConfig(server_config); //Attempts to connect to the remote server if (client->Connect((unsigned char*)&ip)!=0) { delete client; gCoreLog.error("Could not connect to ArenaNet file server!"); return false; } gCoreLog.notice("Connected!"); //Send the connection packet CompiledPacket pck; pck.AddLong(1); //Strange 5 byte header pck.AddByte(0); pck.AddByte(0xF1); //wtfbyte pck.AddByte(0); //Packet opcode pck.AddShort(16); pck.AddLong(1); pck.AddLong(0); pck.AddLong(0); Util::SendCompiledPacket(&pck, client->GetClientModeId(), client); //Variables used in the code following bool receiving_data=false; bool fatal_error=false; bool finished=false; long file_size=0; long file_decomp_size=0; long expected_segment_size=1; long got_x_data=0; CompiledPacket file_data(true); //Do our packet processing while(true) { client->Update(); NetMessage pck_data; while (client->GetNextNetMessage(&pck_data) == 0) { switch(pck_data.dataSize) { case CLIENT_DISCONNECTED: fatal_error=true; break; default: if (!receiving_data) { switch(pck_data.pData[1]) { case 2: //Request the file from the server pck.AddByte(0xF2); pck.AddByte(3); pck.AddShort(12); pck.AddLong(hash); pck.AddLong(0); Util::SendCompiledPacket(&pck, client->GetClientModeId(), client); // gCoreLog.debug("Sent file request"); break; case 4: fatal_error=true; //gCoreLog.debug("No file on remote server"); break; case 5: file_size=Read_uLong(pck_data.pData+12); file_decomp_size=Read_uLong(pck_data.pData+8); gCoreLog.notice("Downloading..."); PacketChop3(pck_data.pData, &pck_data, 20,client); break; case 6: expected_segment_size=Read_uShort(pck_data.pData + 2) - 4; int size=Util::Limit(pck_data.dataSize - 4, expected_segment_size); file_data.Add(pck_data.pData + 4, size, false); got_x_data+=size; receiving_data=true; break; } } else { int size=Util::Limit(pck_data.dataSize, expected_segment_size-got_x_data); file_data.Add(pck_data.pData, size, false); got_x_data+=size; //We have a new packet in the stream (not part of the data segment) if(pck_data.dataSize-size>0) { PacketChop3(pck_data.pData, &pck_data, size, client); receiving_data=false; file_size-=expected_segment_size; got_x_data=0; } } } } if (got_x_data >= expected_segment_size) { file_size-=expected_segment_size; if (file_size <= 0) { finished=true; int ds=0; unsigned char* s=file_data.GetString(&ds); gCoreLog.notice("Download Complete! %d", ds); } else { pck.AddByte(0xF3); pck.AddByte(7); pck.AddShort(8); pck.AddLong(got_x_data); Util::SendCompiledPacket(&pck, client->GetClientModeId(), client); //gCoreLog.debug("Requesting more data %d : %d", got_x_data, file_size); } got_x_data=0; receiving_data=false; } if (fatal_error || finished) { break; } } //Shutdown the network client client->Shutdown(false); delete client; //Create the first four bytes of our file footer unsigned char footer[4]={8,0,1,0x80}; int ds=0; unsigned char* pFile_data=file_data.GetString(&ds); //Create the filename of the cache file char filename[100]; sprintf(filename, "Files/FileCache/Hash0x%X.bin", hash); //Write the file into the cache directory _mkdir("Files/FileCache"); ofstream fout(filename, ios::binary); fout.write((char*)pFile_data, ds); fout.write((char*)footer, 4); fout.write((char*)&file_decomp_size, 4); fout.close(); //Modify the filename so RegisterFile() can find it sprintf(filename, "FileCache/Hash0x%X.bin", hash); //Register the file with the server RegisterFile(filename, hash, file_decomp_size); //Add this file to our cache list fout.open("Files/CacheList.txt", ios::app); fout << hash << ' ' << filename << ' ' << file_decomp_size << '\n'; fout.close(); //We have a fatal error (No file found, connection interrupted, etc.) if (fatal_error) { gCoreLog.error("Fatal error downloading!"); return false; } return true; }