TransferSocket.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. // FileZilla - a Windows ftp client
  2. // Copyright (C) 2002-2004 - Tim Kosse <[email protected]>
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public License
  5. // as published by the Free Software Foundation; either version 2
  6. // of the License, or (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program; if not, write to the Free Software
  13. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. // TransferSocket.cpp: Implementierungsdatei
  15. //
  16. #include "stdafx.h"
  17. #ifndef MPEXT
  18. #include "filezilla.h"
  19. #endif
  20. #include "TransferSocket.h"
  21. #include "mainthread.h"
  22. #include "AsyncProxySocketLayer.h"
  23. #ifndef MPEXT_NO_GSS
  24. #include "AsyncGssSocketLayer.h"
  25. #endif
  26. #ifdef _DEBUG
  27. #define new DEBUG_NEW
  28. #undef THIS_FILE
  29. static char THIS_FILE[] = __FILE__;
  30. #endif
  31. #define BUFSIZE 16384
  32. #define STATE_WAITING 0
  33. #define STATE_STARTING 1
  34. #define STATE_STARTED 2
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CTransferSocket
  37. CTransferSocket::CTransferSocket(CFtpControlSocket *pOwner, int nMode)
  38. {
  39. ASSERT(pOwner);
  40. InitLog(pOwner);
  41. m_pOwner = pOwner;
  42. m_nMode = nMode;
  43. m_nTransferState = STATE_WAITING;
  44. m_bCheckTimeout = FALSE;
  45. m_pBuffer = 0;
  46. #ifndef MPEXT_NO_ZLIB
  47. m_pBuffer2 = 0;
  48. #endif
  49. m_bufferpos = 0;
  50. m_pFile = 0;
  51. m_bListening = FALSE;
  52. m_bSentClose = FALSE;
  53. m_nInternalMessageID = 0;
  54. m_transferdata.transfersize = 0;
  55. m_transferdata.transferleft = 0;
  56. m_transferdata.nTransferStart = 0;
  57. m_nNotifyWaiting = 0;
  58. m_bShutDown = FALSE;
  59. UpdateStatusBar(true);
  60. for (int i = 0; i < SPEED_SECONDS; i++)
  61. {
  62. m_Transfered[i] = 0;
  63. m_UsedForTransfer[i] = 0;
  64. }
  65. m_pProxyLayer = NULL;
  66. #ifndef MPEXT_NO_SSL
  67. m_pSslLayer = NULL;
  68. #endif
  69. #ifndef MPEXT_NO_GSS
  70. m_pGssLayer = NULL;
  71. #endif
  72. if (m_nMode & CSMODE_LIST)
  73. {
  74. m_pListResult = new CFtpListResult(pOwner->m_CurrentServer, &pOwner->m_bUTF8);
  75. m_pListResult->InitLog(this);
  76. }
  77. else
  78. m_pListResult = 0;
  79. m_LastUpdateTime.QuadPart = 0;
  80. #ifndef MPEXT_NO_ZLIB
  81. memset(&m_zlibStream, 0, sizeof(m_zlibStream));
  82. m_useZlib = false;
  83. #endif
  84. }
  85. CTransferSocket::~CTransferSocket()
  86. {
  87. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("~CTransferSocket()"));
  88. delete [] m_pBuffer;
  89. #ifndef MPEXT_NO_ZLIB
  90. delete [] m_pBuffer2;
  91. #endif
  92. PostMessage(m_pOwner->m_pOwner->m_hOwnerWnd, m_pOwner->m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_TRANSFERSTATUS, 0), 0);
  93. Close();
  94. RemoveAllLayers();
  95. delete m_pProxyLayer;
  96. #ifndef MPEXT_NO_SSL
  97. delete m_pSslLayer;
  98. #endif
  99. #ifndef MPEXT_NO_GSS
  100. delete m_pGssLayer;
  101. #endif
  102. m_pOwner->RemoveActiveTransfer();
  103. delete m_pListResult;
  104. #ifndef MPEXT_NO_ZLIB
  105. if (m_useZlib)
  106. {
  107. if (m_nMode & CSMODE_UPLOAD)
  108. deflateEnd(&m_zlibStream);
  109. else
  110. inflateEnd(&m_zlibStream);
  111. }
  112. #endif
  113. }
  114. /////////////////////////////////////////////////////////////////////////////
  115. // Member-Funktion CTransferSocket
  116. void CTransferSocket::OnReceive(int nErrorCode)
  117. {
  118. if (GetState() != connected && GetState() != attached && GetState() != closed)
  119. return;
  120. if (m_nTransferState == STATE_WAITING)
  121. {
  122. m_nNotifyWaiting |= FD_READ;
  123. return;
  124. }
  125. if (m_bSentClose)
  126. return;
  127. if (m_bListening)
  128. return;
  129. if (m_nMode&CSMODE_LIST)
  130. {
  131. if (m_nTransferState == STATE_STARTING)
  132. OnConnect(0);
  133. char *buffer = new char[BUFSIZE];
  134. int numread = CAsyncSocketEx::Receive(buffer, BUFSIZE);
  135. if (numread != SOCKET_ERROR && numread)
  136. {
  137. m_LastActiveTime = CTime::GetCurrentTime();
  138. UpdateRecvLed();
  139. #ifndef MPEXT_NO_ZLIB
  140. if (m_useZlib)
  141. {
  142. m_zlibStream.next_in = (Bytef *)buffer;
  143. m_zlibStream.avail_in = numread;
  144. char *out = new char[BUFSIZE];
  145. m_zlibStream.next_out = (Bytef *)out;
  146. m_zlibStream.avail_out = BUFSIZE;
  147. int res = inflate(&m_zlibStream, 0);
  148. while (res == Z_OK)
  149. {
  150. m_pListResult->AddData(out, BUFSIZE - m_zlibStream.avail_out);
  151. out = new char[BUFSIZE];
  152. m_zlibStream.next_out = (Bytef *)out;
  153. m_zlibStream.avail_out = BUFSIZE;
  154. res = inflate(&m_zlibStream, 0);
  155. }
  156. delete [] buffer;
  157. if (res == Z_STREAM_END)
  158. m_pListResult->AddData(out, BUFSIZE - m_zlibStream.avail_out);
  159. else if (res != Z_OK && res != Z_BUF_ERROR)
  160. {
  161. delete [] out;
  162. CloseAndEnsureSendClose(CSMODE_TRANSFERERROR);
  163. return;
  164. }
  165. else
  166. delete [] out;
  167. }
  168. else
  169. #endif
  170. m_pListResult->AddData(buffer, numread);
  171. m_transferdata.transfersize += numread;
  172. CTimeSpan timespan = CTime::GetCurrentTime() - m_StartTime;
  173. int elapsed = (int)timespan.GetTotalSeconds();
  174. //TODO
  175. //There are servers which report the total number of
  176. //bytes in the list response message, but yet it is not supported by FZ.
  177. /*double leftmodifier=(transfersize-transferstart-transferleft);
  178. leftmodifier*=100;
  179. leftmodifier/=(transfersize-transferstart);
  180. if (leftmodifier==0)
  181. leftmodifier=1;
  182. double leftmodifier2=100-leftmodifier;
  183. int left=(int)((elapsed/leftmodifier)*leftmodifier2);
  184. int percent=MulDiv(100,transfersize-transferleft,transfersize);*/
  185. int transferrate=static_cast<int>( (elapsed && m_transferdata.transfersize)?m_transferdata.transfersize/elapsed:0 );
  186. t_ffam_transferstatus *status = new t_ffam_transferstatus;
  187. status->bFileTransfer = FALSE;
  188. #ifdef MPEXT
  189. status->transfersize = -1;
  190. #endif
  191. status->bytes = m_transferdata.transfersize;
  192. status->percent = -1;
  193. status->timeelapsed = elapsed;
  194. status->timeleft = -1;
  195. status->transferrate = transferrate;
  196. PostMessage(m_pOwner->m_pOwner->m_hOwnerWnd, m_pOwner->m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_TRANSFERSTATUS, 0), (LPARAM)status);
  197. }
  198. else
  199. delete [] buffer;
  200. if (!numread)
  201. {
  202. CloseAndEnsureSendClose(0);
  203. }
  204. if (numread == SOCKET_ERROR)
  205. {
  206. int nError = GetLastError();
  207. if (nError == WSAENOTCONN)
  208. {
  209. //Not yet connected
  210. return;
  211. }
  212. #ifndef MPEXT_NO_SSL
  213. else if (m_pSslLayer && nError == WSAESHUTDOWN)
  214. {
  215. // Do nothing, wait for shutdown complete notification.
  216. return;
  217. }
  218. #endif
  219. else if (nError != WSAEWOULDBLOCK)
  220. {
  221. LogError(nError);
  222. CloseAndEnsureSendClose(CSMODE_TRANSFERERROR);
  223. }
  224. }
  225. }
  226. else if (m_nMode & CSMODE_DOWNLOAD)
  227. {
  228. if (m_nTransferState == STATE_STARTING)
  229. OnConnect(0);
  230. bool beenWaiting = false;
  231. _int64 ableToRead;
  232. if (GetState() != closed)
  233. ableToRead = m_pOwner->GetAbleToTransferSize(CControlSocket::download, beenWaiting);
  234. else
  235. ableToRead = BUFSIZE;
  236. if (!beenWaiting)
  237. ASSERT(ableToRead);
  238. else if (!ableToRead)
  239. {
  240. TriggerEvent(FD_READ);
  241. return;
  242. }
  243. if (!m_pBuffer)
  244. m_pBuffer = new char[BUFSIZE];
  245. int numread = CAsyncSocketEx::Receive(m_pBuffer, static_cast<int>(ableToRead));
  246. if (numread!=SOCKET_ERROR)
  247. {
  248. Transfered( numread, CTime::GetCurrentTime());
  249. m_pOwner->SpeedLimitAddTransferredBytes(CControlSocket::download, numread);
  250. }
  251. if (!numread)
  252. {
  253. CloseAndEnsureSendClose(0);
  254. return;
  255. }
  256. if (numread == SOCKET_ERROR)
  257. {
  258. int nError = GetLastError();
  259. if (nError == WSAENOTCONN)
  260. {
  261. //Not yet connected
  262. return;
  263. }
  264. #ifndef MPEXT_NO_SSL
  265. else if (m_pSslLayer && nError == WSAESHUTDOWN)
  266. {
  267. // Do nothing, wait for shutdown complete notification.
  268. return;
  269. }
  270. #endif
  271. else if (nError != WSAEWOULDBLOCK)
  272. {
  273. LogError(nError);
  274. CloseAndEnsureSendClose(CSMODE_TRANSFERERROR);
  275. }
  276. UpdateStatusBar(false);
  277. return;
  278. }
  279. int written = 0;
  280. m_LastActiveTime = CTime::GetCurrentTime();
  281. UpdateRecvLed();
  282. TRY
  283. {
  284. #ifndef MPEXT_NO_ZLIB
  285. if (m_useZlib)
  286. {
  287. if (!m_pBuffer2)
  288. m_pBuffer2 = new char[BUFSIZE];
  289. m_zlibStream.next_in = (Bytef *)m_pBuffer;
  290. m_zlibStream.avail_in = numread;
  291. m_zlibStream.next_out = (Bytef *)m_pBuffer2;
  292. m_zlibStream.avail_out = BUFSIZE;
  293. int res = inflate(&m_zlibStream, 0);
  294. while (res == Z_OK)
  295. {
  296. m_pFile->Write(m_pBuffer2, BUFSIZE - m_zlibStream.avail_out);
  297. written += BUFSIZE - m_zlibStream.avail_out;
  298. m_zlibStream.next_out = (Bytef *)m_pBuffer2;
  299. m_zlibStream.avail_out = BUFSIZE;
  300. res = inflate(&m_zlibStream, 0);
  301. }
  302. if (res == Z_STREAM_END)
  303. {
  304. m_pFile->Write(m_pBuffer2, BUFSIZE - m_zlibStream.avail_out);
  305. written += BUFSIZE - m_zlibStream.avail_out;
  306. }
  307. else if (res != Z_OK && res != Z_BUF_ERROR)
  308. {
  309. m_pOwner->ShowStatus(L"Compression error", FZ_LOG_ERROR);
  310. CloseAndEnsureSendClose(CSMODE_TRANSFERERROR);
  311. return;
  312. }
  313. }
  314. else
  315. #endif
  316. {
  317. m_pFile->Write(m_pBuffer, numread);
  318. written = numread;
  319. }
  320. }
  321. CATCH(CFileException,e)
  322. {
  323. LPTSTR msg = new TCHAR[BUFSIZE];
  324. if (e->GetErrorMessage(msg, BUFSIZE))
  325. m_pOwner->ShowStatus(msg, FZ_LOG_ERROR);
  326. delete [] msg;
  327. CloseAndEnsureSendClose(CSMODE_TRANSFERERROR);
  328. return;
  329. }
  330. END_CATCH;
  331. m_transferdata.transferleft -= written;
  332. UpdateStatusBar(false);
  333. }
  334. }
  335. void CTransferSocket::OnAccept(int nErrorCode)
  336. {
  337. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("OnAccept(%d)"), nErrorCode);
  338. m_bListening=FALSE;
  339. CAsyncSocketEx tmp;
  340. Accept(tmp);
  341. SOCKET socket=tmp.Detach();
  342. CAsyncSocketEx::Close();
  343. Attach(socket);
  344. /* Set internal socket send buffer
  345. * this should fix the speed problems some users have reported
  346. */
  347. DWORD value;
  348. int len = sizeof(value);
  349. GetSockOpt(SO_SNDBUF, &value, &len);
  350. // MPEXT
  351. int sndbuf = COptions::GetOptionVal(OPTION_MPEXT_SNDBUF);
  352. if (value < sndbuf)
  353. {
  354. value = sndbuf;
  355. SetSockOpt(SO_SNDBUF, &value, sizeof(value));
  356. }
  357. if (m_nTransferState == STATE_STARTING)
  358. {
  359. m_nTransferState = STATE_STARTED;
  360. #ifndef MPEXT_NO_SSL
  361. if (m_pSslLayer)
  362. {
  363. AddLayer(m_pSslLayer);
  364. int res = m_pSslLayer->InitSSLConnection(true, m_pOwner->m_pSslLayer,
  365. COptions::GetOptionVal(OPTION_MPEXT_SSLSESSIONREUSE),
  366. COptions::GetOptionVal(OPTION_MPEXT_MIN_TLS_VERSION),
  367. COptions::GetOptionVal(OPTION_MPEXT_MAX_TLS_VERSION));
  368. #ifndef MPEXT_NO_SSLDLL
  369. if (res == SSL_FAILURE_LOADDLLS)
  370. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTLOADSSLDLLS, FZ_LOG_ERROR);
  371. else
  372. #endif
  373. if (res == SSL_FAILURE_INITSSL)
  374. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTINITSSL, FZ_LOG_ERROR);
  375. if (res)
  376. {
  377. CloseAndEnsureSendClose(CSMODE_TRANSFERERROR);
  378. return;
  379. }
  380. }
  381. #endif
  382. #ifndef MPEXT_NO_GSS
  383. if (m_pGssLayer)
  384. {
  385. AddLayer(m_pGssLayer);
  386. }
  387. #endif
  388. m_TransferedFirst = m_StartTime = CTime::GetCurrentTime();
  389. m_LastActiveTime = CTime::GetCurrentTime();
  390. }
  391. }
  392. void CTransferSocket::OnConnect(int nErrorCode)
  393. {
  394. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("OnConnect(%d)"), nErrorCode);
  395. if (nErrorCode)
  396. {
  397. TCHAR buffer[1000];
  398. memset(buffer, 0, sizeof(buffer));
  399. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, nErrorCode, 0, buffer, 999, 0);
  400. CString str;
  401. str.Format(IDS_ERRORMSG_CANTOPENTRANSFERCHANNEL,buffer);
  402. str.Replace( _T("\n"), _T("\0") );
  403. str.Replace( _T("\r"), _T("\0") );
  404. m_pOwner->ShowStatus(str, FZ_LOG_ERROR);
  405. CloseAndEnsureSendClose(CSMODE_TRANSFERERROR);
  406. }
  407. else
  408. {
  409. /* Set internal socket send buffer
  410. * this should fix the speed problems some users have reported
  411. */
  412. DWORD value;
  413. int len = sizeof(value);
  414. GetSockOpt(SO_SNDBUF, &value, &len);
  415. // MPEXT
  416. int sndbuf = COptions::GetOptionVal(OPTION_MPEXT_SNDBUF);
  417. if (value < sndbuf)
  418. {
  419. value = sndbuf;
  420. SetSockOpt(SO_SNDBUF, &value, sizeof(value));
  421. }
  422. }
  423. if (m_nTransferState == STATE_WAITING)
  424. {
  425. // OnReceive (invoked by m_nNotifyWaiting including FD_READ)
  426. // will call back to OnConnected (as we won't be connected yet).
  427. // This is needed for file transfers only, where SetActive is
  428. // called only after 1xx response to RETR (and similar) arrives.
  429. // But we get FD_CONNECT earlier, hence we get to thisa branch.
  430. // With directory listing, SetActive is called before Connect,
  431. // so we are already STATE_STARTING on FD_CONNECT.
  432. // It should probably behave the same in both scenarios.
  433. m_nNotifyWaiting |= FD_READ;
  434. }
  435. else if (m_nTransferState == STATE_STARTING)
  436. {
  437. m_nTransferState = STATE_STARTED;
  438. m_TransferedFirst = m_StartTime = CTime::GetCurrentTime();
  439. m_LastActiveTime=CTime::GetCurrentTime();
  440. #ifndef MPEXT_NO_SSL
  441. if (m_pSslLayer)
  442. {
  443. AddLayer(m_pSslLayer);
  444. int res = m_pSslLayer->InitSSLConnection(true, m_pOwner->m_pSslLayer,
  445. COptions::GetOptionVal(OPTION_MPEXT_SSLSESSIONREUSE),
  446. COptions::GetOptionVal(OPTION_MPEXT_MIN_TLS_VERSION),
  447. COptions::GetOptionVal(OPTION_MPEXT_MAX_TLS_VERSION));
  448. #ifndef MPEXT_NO_SSLDLL
  449. if (res == SSL_FAILURE_LOADDLLS)
  450. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTLOADSSLDLLS, FZ_LOG_ERROR);
  451. else
  452. #endif
  453. if (res == SSL_FAILURE_INITSSL)
  454. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTINITSSL, FZ_LOG_ERROR);
  455. if (res)
  456. {
  457. CloseAndEnsureSendClose(CSMODE_TRANSFERERROR);
  458. return;
  459. }
  460. }
  461. #endif
  462. #ifndef MPEXT_NO_GSS
  463. if (m_pGssLayer)
  464. {
  465. AddLayer(m_pGssLayer);
  466. }
  467. #endif
  468. }
  469. }
  470. void CTransferSocket::OnClose(int nErrorCode)
  471. {
  472. LogMessage(__FILE__, __LINE__, this, FZ_LOG_DEBUG, _T("OnClose(%d)"), nErrorCode);
  473. if (m_nTransferState == STATE_WAITING)
  474. {
  475. m_nNotifyWaiting |= FD_CLOSE;
  476. return;
  477. }
  478. OnReceive(0);
  479. CloseAndEnsureSendClose(0);
  480. }
  481. int CTransferSocket::CheckForTimeout(int delay)
  482. {
  483. UpdateStatusBar(false);
  484. if (!m_bCheckTimeout)
  485. {
  486. // we are closed, so make sure the FTP control socket is itself checking for
  487. // timeout as we are not
  488. return 0;
  489. }
  490. CTimeSpan span = CTime::GetCurrentTime()-m_LastActiveTime;
  491. if (span.GetTotalSeconds()>=delay)
  492. {
  493. m_pOwner->ShowStatus(IDS_ERRORMSG_TIMEOUT, FZ_LOG_ERROR);
  494. CloseAndEnsureSendClose(CSMODE_TRANSFERTIMEOUT);
  495. return 2;
  496. }
  497. return 1;
  498. }
  499. void CTransferSocket::SetActive()
  500. {
  501. LogMessage(__FILE__, __LINE__, this, FZ_LOG_DEBUG, _T("SetActive()"));
  502. if (m_nTransferState == STATE_WAITING)
  503. m_nTransferState = STATE_STARTING;
  504. m_bCheckTimeout = TRUE;
  505. m_LastActiveTime = CTime::GetCurrentTime();
  506. if (m_nNotifyWaiting & FD_READ)
  507. OnReceive(0);
  508. if (m_nNotifyWaiting & FD_WRITE)
  509. OnSend(0);
  510. if (m_nNotifyWaiting & FD_CLOSE)
  511. OnClose(0);
  512. }
  513. void CTransferSocket::OnSend(int nErrorCode)
  514. {
  515. if (m_nTransferState == STATE_WAITING)
  516. {
  517. m_nNotifyWaiting |= FD_WRITE;
  518. return;
  519. }
  520. if (m_bSentClose)
  521. {
  522. return;
  523. }
  524. if (m_bListening)
  525. {
  526. return;
  527. }
  528. if (!(m_nMode&CSMODE_UPLOAD))
  529. {
  530. return;
  531. }
  532. if (m_nTransferState == STATE_STARTING)
  533. {
  534. OnConnect(0);
  535. }
  536. #ifndef MPEXT_NO_ZLIB
  537. if (m_useZlib)
  538. {
  539. if (!m_pBuffer)
  540. {
  541. m_pBuffer = new char[BUFSIZE];
  542. m_bufferpos = 0;
  543. m_zlibStream.next_out = (Bytef *)m_pBuffer;
  544. m_zlibStream.avail_out = BUFSIZE;
  545. }
  546. if (!m_pBuffer2)
  547. {
  548. m_pBuffer2 = new char[BUFSIZE];
  549. m_zlibStream.next_in = (Bytef *)m_pBuffer2;
  550. }
  551. bool beenWaiting = false;
  552. while (true)
  553. {
  554. int numsend;
  555. if (!m_zlibStream.avail_in)
  556. {
  557. if (m_pFile)
  558. {
  559. DWORD numread;
  560. numread = ReadDataFromFile(m_pBuffer2, BUFSIZE);
  561. if (numread < 0)
  562. {
  563. return;
  564. }
  565. m_transferdata.transferleft -= numread;
  566. m_zlibStream.next_in = (Bytef *)m_pBuffer2;
  567. m_zlibStream.avail_in = numread;
  568. if (numread < BUFSIZE)
  569. m_pFile = 0;
  570. }
  571. }
  572. if (!m_zlibStream.avail_out)
  573. {
  574. if (m_bufferpos >= BUFSIZE)
  575. {
  576. m_bufferpos = 0;
  577. m_zlibStream.next_out = (Bytef *)m_pBuffer;
  578. m_zlibStream.avail_out = BUFSIZE;
  579. }
  580. }
  581. int res = Z_OK;
  582. if (m_zlibStream.avail_out)
  583. {
  584. res = deflate(&m_zlibStream, m_pFile ? 0 : Z_FINISH);
  585. if (res != Z_OK && (!m_pFile && res != Z_STREAM_END))
  586. {
  587. m_pOwner->ShowStatus("Decompression error", FZ_LOG_ERROR);
  588. CloseAndEnsureSendClose(CSMODE_TRANSFERERROR);
  589. return;
  590. }
  591. }
  592. numsend = BUFSIZE;
  593. int len = BUFSIZE - m_bufferpos - m_zlibStream.avail_out;
  594. if (!len && !m_pFile)
  595. {
  596. break;
  597. }
  598. if (len < BUFSIZE)
  599. numsend = len;
  600. int nLimit = (int)m_pOwner->GetAbleToTransferSize(CControlSocket::upload, beenWaiting);
  601. if (nLimit != -1 && GetState() != closed && numsend > nLimit)
  602. numsend = nLimit;
  603. if (!numsend)
  604. {
  605. TriggerEvent(FD_WRITE);
  606. return;
  607. }
  608. int numsent = Send(m_pBuffer + m_bufferpos, numsend);
  609. if (numsent == SOCKET_ERROR)
  610. {
  611. int nError = GetLastError();
  612. if (nError == WSAENOTCONN)
  613. {
  614. //Not yet connected
  615. return;
  616. }
  617. #ifndef MPEXT_NO_SSL
  618. else if (m_pSslLayer && nError == WSAESHUTDOWN)
  619. {
  620. // Do nothing, wait for shutdown complete notification.
  621. return;
  622. }
  623. #endif
  624. else if (nError != WSAEWOULDBLOCK)
  625. {
  626. CloseOnShutDownOrError(CSMODE_TRANSFERERROR);
  627. }
  628. UpdateStatusBar(false);
  629. return;
  630. }
  631. Transfered( numsent, CTime::GetCurrentTime());
  632. m_pOwner->SpeedLimitAddTransferredBytes(CControlSocket::upload, numsent);
  633. m_LastActiveTime = CTime::GetCurrentTime();
  634. UpdateSendLed();
  635. m_bufferpos += numsent;
  636. UpdateStatusBar(false);
  637. if (!m_zlibStream.avail_in && !m_pFile && m_zlibStream.avail_out &&
  638. m_zlibStream.avail_out + m_bufferpos == BUFSIZE && res == Z_STREAM_END)
  639. {
  640. CloseOnShutDownOrError(0);
  641. return;
  642. }
  643. //Check if there are other commands in the command queue.
  644. MSG msg;
  645. if (PeekMessage(&msg,0, 0, 0, PM_NOREMOVE))
  646. {
  647. TriggerEvent(FD_WRITE);
  648. return;
  649. }
  650. }
  651. }
  652. else
  653. #endif
  654. {
  655. if (!m_pFile)
  656. {
  657. return;
  658. }
  659. if (!m_pBuffer)
  660. m_pBuffer = new char[BUFSIZE];
  661. int numread;
  662. bool beenWaiting = false;
  663. _int64 currentBufferSize;
  664. if (GetState() != closed)
  665. currentBufferSize = m_pOwner->GetAbleToTransferSize(CControlSocket::upload, beenWaiting);
  666. else
  667. currentBufferSize = BUFSIZE;
  668. if (!currentBufferSize && !m_bufferpos)
  669. {
  670. // Not allowed to send yet, try later
  671. TriggerEvent(FD_WRITE);
  672. return;
  673. }
  674. else if (m_bufferpos < currentBufferSize)
  675. {
  676. numread = ReadDataFromFile(m_pBuffer + m_bufferpos, static_cast<int>(currentBufferSize - m_bufferpos));
  677. if (numread < 0 )
  678. {
  679. return;
  680. }
  681. else if (!numread && !m_bufferpos)
  682. {
  683. CloseOnShutDownOrError(0);
  684. return;
  685. }
  686. }
  687. else
  688. numread = 0;
  689. ASSERT((numread+m_bufferpos) <= BUFSIZE);
  690. ASSERT(numread>=0);
  691. ASSERT(m_bufferpos>=0);
  692. if (numread+m_bufferpos <= 0)
  693. {
  694. CloseOnShutDownOrError(0);
  695. return;
  696. }
  697. int numsent = Send(m_pBuffer, numread + m_bufferpos);
  698. while (TRUE)
  699. {
  700. if (numsent != SOCKET_ERROR)
  701. {
  702. Transfered(numsent, CTime::GetCurrentTime());
  703. m_pOwner->SpeedLimitAddTransferredBytes(CControlSocket::upload, numsent);
  704. m_LastActiveTime = CTime::GetCurrentTime();
  705. UpdateSendLed();
  706. m_transferdata.transferleft -= numsent;
  707. }
  708. if (numsent==SOCKET_ERROR || !numsent)
  709. {
  710. int nError = GetLastError();
  711. if (nError == WSAENOTCONN)
  712. {
  713. //Not yet connected
  714. m_bufferpos += numread;
  715. return;
  716. }
  717. else if (nError == WSAEWOULDBLOCK)
  718. {
  719. m_bufferpos += numread;
  720. }
  721. #ifndef MPEXT_NO_SSL
  722. else if (m_pSslLayer && nError == WSAESHUTDOWN)
  723. {
  724. m_bufferpos += numread;
  725. // Do nothing, wait for shutdown complete notification.
  726. return;
  727. }
  728. #endif
  729. else
  730. {
  731. CloseOnShutDownOrError(CSMODE_TRANSFERERROR);
  732. }
  733. UpdateStatusBar(false);
  734. return;
  735. }
  736. else
  737. {
  738. int pos = numread + m_bufferpos - numsent;
  739. if (pos < 0 || (numsent + pos) > BUFSIZE)
  740. {
  741. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Index out of range"));
  742. CloseOnShutDownOrError(CSMODE_TRANSFERERROR);
  743. return;
  744. }
  745. else if (!pos && numread < (currentBufferSize-m_bufferpos) && m_bufferpos != currentBufferSize)
  746. {
  747. CloseOnShutDownOrError(0);
  748. return;
  749. }
  750. else if (!pos)
  751. {
  752. m_bufferpos = 0;
  753. }
  754. else
  755. {
  756. memmove(m_pBuffer, m_pBuffer+numsent, pos);
  757. m_bufferpos=pos;
  758. }
  759. }
  760. //Check if there are other commands in the command queue.
  761. MSG msg;
  762. if (PeekMessage(&msg, 0, m_nInternalMessageID, m_nInternalMessageID, PM_NOREMOVE))
  763. {
  764. //Send resume message
  765. LogMessage(__FILE__, __LINE__, this, FZ_LOG_DEBUG, _T("Message waiting in queue, resuming later"));
  766. TriggerEvent(FD_WRITE);
  767. UpdateStatusBar(false);
  768. return;
  769. }
  770. UpdateStatusBar(false);
  771. if (GetState() != closed)
  772. currentBufferSize = m_pOwner->GetAbleToTransferSize(CControlSocket::upload, beenWaiting);
  773. else
  774. currentBufferSize = BUFSIZE;
  775. if (m_bufferpos < currentBufferSize)
  776. {
  777. numread = ReadDataFromFile(m_pBuffer + m_bufferpos, static_cast<int>(currentBufferSize - m_bufferpos));
  778. if (numread < 0 )
  779. {
  780. return;
  781. }
  782. else if (!numread && !m_bufferpos)
  783. {
  784. CloseOnShutDownOrError(0);
  785. return;
  786. }
  787. }
  788. else
  789. {
  790. numread = 0;
  791. }
  792. if (!currentBufferSize && !m_bufferpos)
  793. {
  794. // Not allowed to send yet, try later
  795. TriggerEvent(FD_WRITE);
  796. return;
  797. }
  798. ASSERT(numread>=0);
  799. ASSERT(m_bufferpos>=0);
  800. numsent = Send(m_pBuffer, numread+m_bufferpos);
  801. }
  802. }
  803. }
  804. void CTransferSocket::UpdateStatusBar(bool forceUpdate)
  805. {
  806. if (m_nTransferState != STATE_STARTED)
  807. return;
  808. if (!forceUpdate)
  809. {
  810. //Don't flood the main window with messages
  811. //Else performance would be really low
  812. LARGE_INTEGER curtime;
  813. LARGE_INTEGER freq;
  814. QueryPerformanceFrequency(&freq);
  815. QueryPerformanceCounter(&curtime);
  816. if (((curtime.QuadPart-m_LastUpdateTime.QuadPart) < (freq.QuadPart/15) ) )
  817. return;
  818. m_LastUpdateTime = curtime;
  819. }
  820. //Update the statusbar
  821. CTimeSpan timespan=CTime::GetCurrentTime()-m_StartTime;
  822. int elapsed=(int)timespan.GetTotalSeconds();
  823. t_ffam_transferstatus *status=new t_ffam_transferstatus;
  824. status->bFileTransfer = m_nMode & (CSMODE_DOWNLOAD | CSMODE_UPLOAD);
  825. #ifdef MPEXT
  826. status->transfersize = m_transferdata.transfersize;
  827. #endif
  828. status->timeelapsed=elapsed;
  829. status->bytes=m_transferdata.transfersize-m_transferdata.transferleft;
  830. if (m_transferdata.transfersize>0 && !(m_nMode&CSMODE_LIST))
  831. {
  832. double leftmodifier=static_cast<double>(m_transferdata.transfersize-m_transferdata.nTransferStart-m_transferdata.transferleft);
  833. leftmodifier*=100;
  834. if (m_transferdata.transfersize-m_transferdata.nTransferStart)
  835. leftmodifier /= (m_transferdata.transfersize-m_transferdata.nTransferStart);
  836. else
  837. leftmodifier = 1;
  838. if (leftmodifier == 0)
  839. leftmodifier = 1;
  840. double leftmodifier2 = 100 - leftmodifier;
  841. int left=static_cast<int>((elapsed/leftmodifier)*leftmodifier2);
  842. double percent=100*static_cast<double>(m_transferdata.transfersize-m_transferdata.transferleft);
  843. percent/=m_transferdata.transfersize;
  844. status->percent=static_cast<int>(percent);
  845. if (status->percent>100)
  846. status->percent=100;
  847. if (left < 0)
  848. left = -1;
  849. status->timeleft=left;
  850. }
  851. else
  852. {
  853. status->percent=-1;
  854. status->timeleft=-1;
  855. }
  856. int count = 0;
  857. status->transferrate = 0;
  858. for ( int i = 0; i < SPEED_SECONDS; i++)
  859. {
  860. if ( m_UsedForTransfer[ i])
  861. {
  862. status->transferrate += m_Transfered[ i];
  863. count++;
  864. }
  865. }
  866. if ( count > 0)
  867. status->transferrate = status->transferrate / count;
  868. else if (m_Transfered[0])
  869. status->transferrate = m_Transfered[0];
  870. else
  871. status->timeleft=-1;
  872. PostMessage(m_pOwner->m_pOwner->m_hOwnerWnd, m_pOwner->m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_TRANSFERSTATUS, 0), (LPARAM)status);
  873. }
  874. void CTransferSocket::UpdateSendLed()
  875. {
  876. //Don't flood the main window with messages
  877. //Else performance would be really low
  878. LARGE_INTEGER curtime;
  879. LARGE_INTEGER freq;
  880. QueryPerformanceFrequency(&freq);
  881. QueryPerformanceCounter(&curtime);
  882. static LARGE_INTEGER oldtime={0};
  883. if ( ( (curtime.QuadPart-oldtime.QuadPart) < (freq.QuadPart/15) ) )
  884. return;
  885. oldtime=curtime;
  886. PostMessage(m_pOwner->m_pOwner->m_hOwnerWnd, m_pOwner->m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_SOCKETSTATUS, FZ_SOCKETSTATUS_SEND), 0);
  887. }
  888. void CTransferSocket::UpdateRecvLed()
  889. {
  890. //Don't flood the main window with messages
  891. //Else performance would be really low
  892. LARGE_INTEGER curtime;
  893. LARGE_INTEGER freq;
  894. QueryPerformanceFrequency(&freq);
  895. QueryPerformanceCounter(&curtime);
  896. static LARGE_INTEGER oldtime={0};
  897. if ( ( (curtime.QuadPart-oldtime.QuadPart) < (freq.QuadPart/15) ) )
  898. return;
  899. oldtime=curtime;
  900. PostMessage(m_pOwner->m_pOwner->m_hOwnerWnd, m_pOwner->m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_SOCKETSTATUS, FZ_SOCKETSTATUS_RECV), 0);
  901. }
  902. BOOL CTransferSocket::Create(
  903. #ifndef MPEXT_NO_SSL
  904. BOOL bUseSsl
  905. #endif
  906. )
  907. {
  908. #ifndef MPEXT_NO_SSL
  909. if (bUseSsl)
  910. m_pSslLayer = new CAsyncSslSocketLayer;
  911. #endif
  912. if (!m_pOwner->m_CurrentServer.fwbypass)
  913. {
  914. int nProxyType = COptions::GetOptionVal(OPTION_PROXYTYPE);
  915. if (nProxyType != PROXYTYPE_NOPROXY)
  916. {
  917. USES_CONVERSION;
  918. m_pProxyLayer = new CAsyncProxySocketLayer;
  919. if (nProxyType == PROXYTYPE_SOCKS4)
  920. m_pProxyLayer->SetProxy(PROXYTYPE_SOCKS4, T2CA(COptions::GetOption(OPTION_PROXYHOST)), COptions::GetOptionVal(OPTION_PROXYPORT));
  921. else if (nProxyType == PROXYTYPE_SOCKS4A)
  922. m_pProxyLayer->SetProxy(PROXYTYPE_SOCKS4A, T2CA(COptions::GetOption(OPTION_PROXYHOST)), COptions::GetOptionVal(OPTION_PROXYPORT));
  923. else if (nProxyType == PROXYTYPE_SOCKS5)
  924. if (COptions::GetOptionVal(OPTION_PROXYUSELOGON))
  925. m_pProxyLayer->SetProxy(PROXYTYPE_SOCKS5, T2CA(COptions::GetOption(OPTION_PROXYHOST)),
  926. COptions::GetOptionVal(OPTION_PROXYPORT),
  927. T2CA(COptions::GetOption(OPTION_PROXYUSER)),
  928. T2CA(CCrypt::decrypt(COptions::GetOption(OPTION_PROXYPASS))));
  929. else
  930. m_pProxyLayer->SetProxy(PROXYTYPE_SOCKS5, T2CA(COptions::GetOption(OPTION_PROXYHOST)),
  931. COptions::GetOptionVal(OPTION_PROXYPORT));
  932. else if (nProxyType == PROXYTYPE_HTTP11)
  933. if (COptions::GetOptionVal(OPTION_PROXYUSELOGON))
  934. m_pProxyLayer->SetProxy(PROXYTYPE_HTTP11, T2CA(COptions::GetOption(OPTION_PROXYHOST)), COptions::GetOptionVal(OPTION_PROXYPORT),
  935. T2CA(COptions::GetOption(OPTION_PROXYUSER)),
  936. T2CA(CCrypt::decrypt(COptions::GetOption(OPTION_PROXYPASS))));
  937. else
  938. m_pProxyLayer->SetProxy(PROXYTYPE_HTTP11, T2CA(COptions::GetOption(OPTION_PROXYHOST)), COptions::GetOptionVal(OPTION_PROXYPORT));
  939. else
  940. ASSERT(FALSE);
  941. AddLayer(m_pProxyLayer);
  942. }
  943. }
  944. if (!COptions::GetOptionVal(OPTION_LIMITPORTRANGE))
  945. {
  946. if (!CAsyncSocketEx::Create(0, SOCK_STREAM, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, 0, GetFamily()))
  947. return FALSE;
  948. return TRUE;
  949. }
  950. else
  951. {
  952. int min=COptions::GetOptionVal(OPTION_PORTRANGELOW);
  953. int max=COptions::GetOptionVal(OPTION_PORTRANGEHIGH);
  954. if (min>=max)
  955. {
  956. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTCREATEDUETOPORTRANGE,FZ_LOG_ERROR);
  957. return FALSE;
  958. }
  959. int startport=static_cast<int>(min+((double)rand()*(max-min))/(RAND_MAX+1));
  960. int port=startport;
  961. while (!CAsyncSocketEx::Create(port, SOCK_STREAM, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, 0, GetFamily()))
  962. {
  963. port++;
  964. if (port>max)
  965. port=min;
  966. if (port==startport)
  967. {
  968. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTCREATEDUETOPORTRANGE,FZ_LOG_ERROR);
  969. return FALSE;
  970. }
  971. }
  972. }
  973. return TRUE;
  974. }
  975. void CTransferSocket::Close()
  976. {
  977. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("Close()"));
  978. m_bCheckTimeout = FALSE;
  979. CAsyncSocketEx::Close();
  980. }
  981. int CTransferSocket::OnLayerCallback(std::list<t_callbackMsg>& callbacks)
  982. {
  983. for (std::list<t_callbackMsg>::iterator iter = callbacks.begin(); iter != callbacks.end(); iter++)
  984. {
  985. if (iter->nType == LAYERCALLBACK_STATECHANGE)
  986. {
  987. if (CAsyncSocketEx::LogStateChange(iter->nParam1, iter->nParam2))
  988. {
  989. const TCHAR * state2Desc = CAsyncSocketEx::GetStateDesc(iter->nParam2);
  990. const TCHAR * state1Desc = CAsyncSocketEx::GetStateDesc(iter->nParam1);
  991. if (iter->pLayer == m_pProxyLayer)
  992. LogMessage(__FILE__, __LINE__, this, FZ_LOG_INFO, _T("Proxy layer changed state from %s to %s"), state2Desc, state1Desc);
  993. #ifndef MPEXT_NO_SSL
  994. else if (iter->pLayer == m_pSslLayer)
  995. LogMessage(__FILE__, __LINE__, this, FZ_LOG_INFO, _T("TLS layer changed state from %s to %s"), state2Desc, state1Desc);
  996. #endif
  997. #ifndef MPEXT_NO_GSS
  998. else if (iter->pLayer == m_pGssLayer)
  999. LogMessage(__FILE__, __LINE__, this, FZ_LOG_INFO, _T("GSS layer changed state from %s to %s"), state2Desc, state1Desc);
  1000. #endif
  1001. else
  1002. LogMessage(__FILE__, __LINE__, this, FZ_LOG_INFO, _T("Layer @ %d changed state from %s to %s"), iter->pLayer, state2Desc, state1Desc);
  1003. }
  1004. }
  1005. else if (iter->nType == LAYERCALLBACK_LAYERSPECIFIC)
  1006. {
  1007. if (iter->pLayer == m_pProxyLayer)
  1008. {
  1009. switch (iter->nParam1)
  1010. {
  1011. case PROXYERROR_NOCONN:
  1012. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_NOCONN, FZ_LOG_ERROR);
  1013. break;
  1014. case PROXYERROR_REQUESTFAILED:
  1015. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_REQUESTFAILED, FZ_LOG_ERROR);
  1016. break;
  1017. case PROXYERROR_AUTHTYPEUNKNOWN:
  1018. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_AUTHTYPEUNKNOWN, FZ_LOG_ERROR);
  1019. break;
  1020. case PROXYERROR_AUTHFAILED:
  1021. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_AUTHFAILED, FZ_LOG_ERROR);
  1022. break;
  1023. case PROXYERROR_AUTHNOLOGON:
  1024. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_AUTHNOLOGON, FZ_LOG_ERROR);
  1025. break;
  1026. case PROXYERROR_CANTRESOLVEHOST:
  1027. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_CANTRESOLVEHOST, FZ_LOG_ERROR);
  1028. break;
  1029. default:
  1030. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Unknown proxy error"));
  1031. }
  1032. }
  1033. #ifndef MPEXT_NO_SSL
  1034. else if (iter->pLayer == m_pSslLayer)
  1035. {
  1036. switch (iter->nParam1)
  1037. {
  1038. case SSL_INFO:
  1039. switch(iter->nParam2)
  1040. {
  1041. case SSL_INFO_SHUTDOWNCOMPLETE:
  1042. CloseAndEnsureSendClose(0);
  1043. break;
  1044. case SSL_INFO_ESTABLISHED:
  1045. m_pOwner->ShowStatus(IDS_STATUSMSG_SSLESTABLISHEDTRANSFER, FZ_LOG_STATUS);
  1046. TriggerEvent(FD_FORCEREAD);
  1047. break;
  1048. }
  1049. break;
  1050. case SSL_FAILURE:
  1051. switch (iter->nParam2)
  1052. {
  1053. case SSL_FAILURE_ESTABLISH:
  1054. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTESTABLISHSSLCONNECTION, FZ_LOG_ERROR);
  1055. break;
  1056. #ifndef MPEXT_NO_SSLDLL
  1057. case SSL_FAILURE_LOADDLLS:
  1058. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTLOADSSLDLLS, FZ_LOG_ERROR);
  1059. break;
  1060. #endif
  1061. case SSL_FAILURE_INITSSL:
  1062. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTINITSSL, FZ_LOG_ERROR);
  1063. break;
  1064. }
  1065. EnsureSendClose(CSMODE_TRANSFERERROR);
  1066. break;
  1067. case SSL_VERIFY_CERT:
  1068. t_SslCertData data;
  1069. LPTSTR CertError = NULL;
  1070. if (m_pSslLayer->GetPeerCertificateData(data, CertError))
  1071. m_pSslLayer->SetNotifyReply(data.priv_data, SSL_VERIFY_CERT, 1);
  1072. else
  1073. {
  1074. CString str;
  1075. str.Format(TLS_CERT_DECODE_ERROR, CertError);
  1076. m_pOwner->ShowStatus(str, FZ_LOG_ERROR);
  1077. CloseAndEnsureSendClose(CSMODE_TRANSFERERROR);
  1078. }
  1079. break;
  1080. }
  1081. }
  1082. #endif
  1083. #ifndef MPEXT_NO_GSS
  1084. else if (iter->pLayer == m_pGssLayer)
  1085. {
  1086. USES_CONVERSION;
  1087. switch (iter->nParam1)
  1088. {
  1089. case GSS_INFO:
  1090. LogMessageRaw(FZ_LOG_INFO, A2CT(iter->str));
  1091. break;
  1092. case GSS_ERROR:
  1093. LogMessageRaw(FZ_LOG_APIERROR, A2CT(iter->str));
  1094. break;
  1095. case GSS_SHUTDOWN_COMPLETE:
  1096. CloseAndEnsureSendClose(0);
  1097. break;
  1098. }
  1099. }
  1100. #endif
  1101. }
  1102. delete [] iter->str;
  1103. }
  1104. return 0;
  1105. }
  1106. void CTransferSocket::Transfered(int count, CTime time)
  1107. {
  1108. CTimeSpan ts = time - m_TransferedFirst;
  1109. int diff = (int)ts.GetTotalSeconds();
  1110. if (diff < 0)
  1111. diff = 0;
  1112. if ( diff >= SPEED_SECONDS)
  1113. {
  1114. int move = diff - SPEED_SECONDS + 1;
  1115. int start = SPEED_SECONDS - move;
  1116. if ( start <= 0)
  1117. start = 0;
  1118. else
  1119. {
  1120. for ( int i = 0; i < SPEED_SECONDS - move; i++)
  1121. {
  1122. m_Transfered[ i] = m_Transfered[ i + move];
  1123. m_UsedForTransfer[ i] = m_UsedForTransfer[ i + move];
  1124. }
  1125. }
  1126. for ( int i = start; i < SPEED_SECONDS; i++)
  1127. {
  1128. m_Transfered[ i] = 0;
  1129. m_UsedForTransfer[ i] = false;
  1130. }
  1131. if (move >= SPEED_SECONDS)
  1132. {
  1133. m_TransferedFirst = time;
  1134. diff = 0;
  1135. }
  1136. else
  1137. {
  1138. m_TransferedFirst += CTimeSpan( move);
  1139. ts = time - m_TransferedFirst;
  1140. diff = (int)(ts.GetTotalSeconds() % 60);
  1141. }
  1142. }
  1143. m_Transfered[ diff] += count;
  1144. for ( int i = 0; i < diff - 1; i++)
  1145. m_UsedForTransfer[ i] = true;
  1146. }
  1147. #ifndef MPEXT_NO_GSS
  1148. void CTransferSocket::UseGSS(CAsyncGssSocketLayer *pGssLayer)
  1149. {
  1150. m_pGssLayer = new CAsyncGssSocketLayer;
  1151. m_pGssLayer->InitTransferChannel(pGssLayer);
  1152. }
  1153. #endif
  1154. #ifndef MPEXT_NO_ZLIB
  1155. bool CTransferSocket::InitZlib(int level)
  1156. {
  1157. int res;
  1158. if (m_nMode & CSMODE_UPLOAD)
  1159. res = deflateInit2(&m_zlibStream, level, Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
  1160. else
  1161. res = inflateInit2(&m_zlibStream, 15);
  1162. if (res == Z_OK)
  1163. m_useZlib = true;
  1164. return res == Z_OK;
  1165. }
  1166. #endif
  1167. int CTransferSocket::ReadDataFromFile(char *buffer, int len)
  1168. {
  1169. TRY
  1170. {
  1171. // Comparing to Filezilla 2, we do not do any translation locally,
  1172. // leaving it onto the server (what Filezilla 3 seems to do too)
  1173. const char Bom[3] = "\xEF\xBB\xBF";
  1174. int read = m_pFile->Read(buffer, len);
  1175. if (COptions::GetOptionVal(OPTION_MPEXT_REMOVE_BOM) &&
  1176. m_transferdata.bType && (read >= sizeof(Bom)) && (memcmp(buffer, Bom, sizeof(Bom)) == 0))
  1177. {
  1178. memcpy(buffer, buffer + sizeof(Bom), read - sizeof(Bom));
  1179. read -= sizeof(Bom);
  1180. int read2 = m_pFile->Read(buffer + read, sizeof(Bom));
  1181. if (read2 > 0)
  1182. {
  1183. read += read2;
  1184. }
  1185. }
  1186. return read;
  1187. }
  1188. CATCH_ALL(e)
  1189. {
  1190. TCHAR error[BUFSIZE];
  1191. if (e->GetErrorMessage(error, BUFSIZE))
  1192. m_pOwner->ShowStatus(error, FZ_LOG_ERROR);
  1193. CloseOnShutDownOrError(CSMODE_TRANSFERERROR);
  1194. return -1;
  1195. }
  1196. END_CATCH_ALL;
  1197. }
  1198. void CTransferSocket::LogSocketMessage(int nMessageType, LPCTSTR pMsgFormat)
  1199. {
  1200. LogMessage(nMessageType, pMsgFormat);
  1201. }
  1202. void CTransferSocket::EnsureSendClose(int Mode)
  1203. {
  1204. if (!m_bSentClose)
  1205. {
  1206. if (Mode != 0)
  1207. {
  1208. m_nMode |= Mode;
  1209. }
  1210. m_bSentClose = TRUE;
  1211. VERIFY(m_pOwner->m_pOwner->PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_TRANSFEREND, m_nMode));
  1212. }
  1213. }
  1214. void CTransferSocket::CloseAndEnsureSendClose(int Mode)
  1215. {
  1216. Close();
  1217. EnsureSendClose(Mode);
  1218. }
  1219. void CTransferSocket::CloseOnShutDownOrError(int Mode)
  1220. {
  1221. if (ShutDown())
  1222. {
  1223. CloseAndEnsureSendClose(Mode);
  1224. }
  1225. else
  1226. {
  1227. int Error = GetLastError();
  1228. if (Error != WSAEWOULDBLOCK)
  1229. {
  1230. // Log always or only when (Mode & CSMODE_TRANSFERERROR)?
  1231. // Does it anyway make sense at all to call this with Mode == 0?
  1232. LogError(Error);
  1233. CloseAndEnsureSendClose(Mode);
  1234. }
  1235. }
  1236. }
  1237. void CTransferSocket::LogError(int Error)
  1238. {
  1239. wchar_t * Buffer;
  1240. int Len = FormatMessage(
  1241. FORMAT_MESSAGE_FROM_SYSTEM |
  1242. FORMAT_MESSAGE_IGNORE_INSERTS |
  1243. FORMAT_MESSAGE_ARGUMENT_ARRAY |
  1244. FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, Error, 0, (LPTSTR)&Buffer, 0, NULL);
  1245. if (Len > 0)
  1246. {
  1247. m_pOwner->ShowStatus(Buffer, FZ_LOG_ERROR);
  1248. LocalFree(Buffer);
  1249. }
  1250. }