void CUrlClient::OnSocketConnected(int nErrorCode) { if (nErrorCode == 0) SendHttpBlockRequests(); }
void CUpDownClient::ProcessHttpBlockPacket(const BYTE* pucData, UINT uSize) { if (reqfile == NULL) throw CString(_T("Failed to process HTTP data block - No 'reqfile' attached")); if (reqfile->IsStopped() || (reqfile->GetStatus() != PS_READY && reqfile->GetStatus() != PS_EMPTY)) throw CString(_T("Failed to process HTTP data block - File not ready for receiving data")); if (m_nUrlStartPos == -1) throw CString(_T("Failed to process HTTP data block - Unexpected file data")); uint32 nStartPos = m_nUrlStartPos; uint32 nEndPos = m_nUrlStartPos + uSize; m_nUrlStartPos += uSize; // if (thePrefs.GetDebugClientTCPLevel() > 0) // Debug(" Start=%u End=%u Size=%u %s\n", nStartPos, nEndPos, size, DbgGetFileInfo(reqfile->GetFileHash())); if (!(GetDownloadState() == DS_DOWNLOADING || GetDownloadState() == DS_NONEEDEDPARTS)) throw CString(_T("Failed to process HTTP data block - Invalid download state")); m_dwLastBlockReceived = ::GetTickCount(); if (nEndPos == nStartPos || uSize != nEndPos - nStartPos) throw CString(_T("Failed to process HTTP data block - Invalid block start/end offsets")); thePrefs.Add2SessionTransferData(GetClientSoft(), (GetClientSoft()==SO_URL) ? (UINT)-2 : (UINT)-1, false, false, uSize); m_nDownDataRateMS += uSize; if (credits) credits->AddDownloaded(uSize, GetIP()); nEndPos--; for (POSITION pos = m_PendingBlocks_list.GetHeadPosition(); pos != NULL; ) { POSITION posLast = pos; Pending_Block_Struct *cur_block = m_PendingBlocks_list.GetNext(pos); if (cur_block->block->StartOffset <= nStartPos && nStartPos <= cur_block->block->EndOffset) { if (thePrefs.GetDebugClientTCPLevel() > 0){ // NOTE: 'Left' is only accurate in case we have one(!) request block! void* p = m_pPCDownSocket ? (void*)m_pPCDownSocket : (void*)socket; Debug(_T("%08x Start=%u End=%u Size=%u Left=%u %s\n"), p, nStartPos, nEndPos, uSize, cur_block->block->EndOffset - (nStartPos + uSize) + 1, DbgGetFileInfo(reqfile->GetFileHash())); } m_nLastBlockOffset = nStartPos; uint32 lenWritten = reqfile->WriteToBuffer(uSize, pucData, nStartPos, nEndPos, cur_block->block, this); if (lenWritten > 0) { m_nTransferredDown += uSize; SetTransferredDownMini(); if (nEndPos >= cur_block->block->EndOffset) { reqfile->RemoveBlockFromList(cur_block->block->StartOffset, cur_block->block->EndOffset); delete cur_block->block; delete cur_block; m_PendingBlocks_list.RemoveAt(posLast); if (m_PendingBlocks_list.IsEmpty()) { if (thePrefs.GetDebugClientTCPLevel() > 0) DebugSend("More block requests", this); m_nUrlStartPos = (UINT)-1; SendHttpBlockRequests(); } } // else // TRACE("%hs - %d bytes missing\n", __FUNCTION__, cur_block->block->EndOffset - nEndPos); } return; } } TRACE("%s - Dropping packet\n", __FUNCTION__); }
bool CUpDownClient::ProcessPeerCacheAnswer(const uchar* packet, UINT size) { const bool bDebug = (thePrefs.GetDebugClientTCPLevel() > 0); ASSERT( GetDownloadState() == DS_DOWNLOADING ); ASSERT( m_ePeerCacheDownState == PCDS_WAIT_CLIENT_REPLY ); if (bDebug) DebugRecv("OP_PeerCacheAnswer", this); if (socket == NULL || reqfile == NULL){ ASSERT(0); return false; } CSafeMemFile dataRecv(packet, size); uint8 uPCVersion = dataRecv.ReadUInt8(); if (uPCVersion != PCPCK_VERSION){ if (bDebug) Debug(_T(" ***Invalid packet version: 0x%02x\n"), uPCVersion); ASSERT(0); return false; } uint8 uPCOpcode = dataRecv.ReadUInt8(); if (uPCOpcode == PCOP_NONE){ if (thePrefs.GetVerbose()) AddDebugLogLine(false, _T("Client does not support PeerCache; %s"), DbgGetClientInfo()); return false; } if (uPCOpcode != PCOP_RES){ if (bDebug) Debug(_T(" ***Invalid packet opcode: 0x%02x\n"), uPCOpcode); ASSERT(0); return false; } uint32 uPushId = 0; uint32 uRemoteIP = 0; uchar aucFileHash[16]; md4clr(aucFileHash); CString strInfo; UINT uTags = dataRecv.ReadUInt8(); while (uTags--) { CTag tag(&dataRecv, GetUnicodeSupport()!=utf8strNone); if (tag.GetNameID() == PCTAG_PUSHID && tag.IsInt()) { uPushId = tag.GetInt(); if (bDebug) strInfo.AppendFormat(_T(" PushId=%u"), uPushId); } else if (tag.GetNameID() == PCTAG_PUBLICIP && tag.IsInt()) { uRemoteIP = tag.GetInt(); if (bDebug) strInfo.AppendFormat(_T(" RemoteIP=%s"), ipstr(uRemoteIP)); } else if (tag.GetNameID() == PCTAG_FILEID && tag.IsHash() && tag.GetHash() != NULL) { md4cpy(aucFileHash, tag.GetHash()); if (bDebug) strInfo.AppendFormat(_T(" FileId=%s"), md4str(aucFileHash)); } else { if (bDebug) strInfo.AppendFormat(_T(" ***UnkTag: %s"), tag.GetFullInfo()); ASSERT(0); } } if (bDebug) { if (dataRecv.GetPosition() < dataRecv.GetLength()) strInfo.AppendFormat(_T(" ***AddData: %u bytes"), (UINT)(dataRecv.GetLength() - dataRecv.GetPosition())); Debug(_T("%s\n"), strInfo); } if (uPushId == 0 || uRemoteIP == 0 || isnulmd4(aucFileHash)){ if (thePrefs.GetVerbose()) AddDebugLogLine(false, _T("Invalid PeerCacheAnswer; %s"), DbgGetClientInfo()); return false; } if (md4cmp(aucFileHash, reqfile->GetFileHash()) != 0){ if (thePrefs.GetVerbose()) AddDebugLogLine(false, _T("PeerCacheAnswer reqfile does not match ed2k reqfile; %s"), DbgGetClientInfo()); return false; } m_uPeerCacheDownloadPushId = uPushId; m_uPeerCacheRemoteIP = uRemoteIP; if (!SendHttpBlockRequests()) return false; theApp.m_pPeerCache->DownloadAttemptStarted(); ASSERT( m_ePeerCacheDownState == PCDS_WAIT_CACHE_REPLY ); return true; }