TransferSocket.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  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. m_bufferpos += numread;
  719. #ifndef MPEXT_NO_SSL
  720. else if (m_pSslLayer && nError == WSAESHUTDOWN)
  721. {
  722. m_bufferpos += numread;
  723. // Do nothing, wait for shutdown complete notification.
  724. return;
  725. }
  726. #endif
  727. else
  728. {
  729. CloseOnShutDownOrError(CSMODE_TRANSFERERROR);
  730. }
  731. UpdateStatusBar(false);
  732. return;
  733. }
  734. else
  735. {
  736. int pos = numread + m_bufferpos - numsent;
  737. if (pos < 0 || (numsent + pos) > BUFSIZE)
  738. {
  739. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Index out of range"));
  740. CloseOnShutDownOrError(CSMODE_TRANSFERERROR);
  741. return;
  742. }
  743. else if (!pos && numread < (currentBufferSize-m_bufferpos) && m_bufferpos != currentBufferSize)
  744. {
  745. CloseOnShutDownOrError(0);
  746. return;
  747. }
  748. else if (!pos)
  749. m_bufferpos = 0;
  750. else
  751. {
  752. memmove(m_pBuffer, m_pBuffer+numsent, pos);
  753. m_bufferpos=pos;
  754. }
  755. }
  756. //Check if there are other commands in the command queue.
  757. MSG msg;
  758. if (PeekMessage(&msg, 0, m_nInternalMessageID, m_nInternalMessageID, PM_NOREMOVE))
  759. {
  760. //Send resume message
  761. LogMessage(__FILE__, __LINE__, this, FZ_LOG_DEBUG, _T("Message waiting in queue, resuming later"));
  762. TriggerEvent(FD_WRITE);
  763. UpdateStatusBar(false);
  764. return;
  765. }
  766. UpdateStatusBar(false);
  767. if (GetState() != closed)
  768. currentBufferSize = m_pOwner->GetAbleToTransferSize(CControlSocket::upload, beenWaiting);
  769. else
  770. currentBufferSize = BUFSIZE;
  771. if (m_bufferpos < currentBufferSize)
  772. {
  773. numread = ReadDataFromFile(m_pBuffer + m_bufferpos, static_cast<int>(currentBufferSize - m_bufferpos));
  774. if (numread < 0 )
  775. {
  776. return;
  777. }
  778. else if (!numread && !m_bufferpos)
  779. {
  780. CloseOnShutDownOrError(0);
  781. return;
  782. }
  783. }
  784. else
  785. numread = 0;
  786. if (!currentBufferSize && !m_bufferpos)
  787. {
  788. // Not allowed to send yet, try later
  789. TriggerEvent(FD_WRITE);
  790. return;
  791. }
  792. ASSERT(numread>=0);
  793. ASSERT(m_bufferpos>=0);
  794. numsent = Send(m_pBuffer, numread+m_bufferpos);
  795. }
  796. }
  797. }
  798. void CTransferSocket::UpdateStatusBar(bool forceUpdate)
  799. {
  800. if (m_nTransferState != STATE_STARTED)
  801. return;
  802. if (!forceUpdate)
  803. {
  804. //Don't flood the main window with messages
  805. //Else performance would be really low
  806. LARGE_INTEGER curtime;
  807. LARGE_INTEGER freq;
  808. QueryPerformanceFrequency(&freq);
  809. QueryPerformanceCounter(&curtime);
  810. if (((curtime.QuadPart-m_LastUpdateTime.QuadPart) < (freq.QuadPart/15) ) )
  811. return;
  812. m_LastUpdateTime = curtime;
  813. }
  814. //Update the statusbar
  815. CTimeSpan timespan=CTime::GetCurrentTime()-m_StartTime;
  816. int elapsed=(int)timespan.GetTotalSeconds();
  817. t_ffam_transferstatus *status=new t_ffam_transferstatus;
  818. status->bFileTransfer = m_nMode & (CSMODE_DOWNLOAD | CSMODE_UPLOAD);
  819. #ifdef MPEXT
  820. status->transfersize = m_transferdata.transfersize;
  821. #endif
  822. status->timeelapsed=elapsed;
  823. status->bytes=m_transferdata.transfersize-m_transferdata.transferleft;
  824. if (m_transferdata.transfersize>0 && !(m_nMode&CSMODE_LIST))
  825. {
  826. double leftmodifier=static_cast<double>(m_transferdata.transfersize-m_transferdata.nTransferStart-m_transferdata.transferleft);
  827. leftmodifier*=100;
  828. if (m_transferdata.transfersize-m_transferdata.nTransferStart)
  829. leftmodifier /= (m_transferdata.transfersize-m_transferdata.nTransferStart);
  830. else
  831. leftmodifier = 1;
  832. if (leftmodifier == 0)
  833. leftmodifier = 1;
  834. double leftmodifier2 = 100 - leftmodifier;
  835. int left=static_cast<int>((elapsed/leftmodifier)*leftmodifier2);
  836. double percent=100*static_cast<double>(m_transferdata.transfersize-m_transferdata.transferleft);
  837. percent/=m_transferdata.transfersize;
  838. status->percent=static_cast<int>(percent);
  839. if (status->percent>100)
  840. status->percent=100;
  841. if (left < 0)
  842. left = -1;
  843. status->timeleft=left;
  844. }
  845. else
  846. {
  847. status->percent=-1;
  848. status->timeleft=-1;
  849. }
  850. int count = 0;
  851. status->transferrate = 0;
  852. for ( int i = 0; i < SPEED_SECONDS; i++)
  853. {
  854. if ( m_UsedForTransfer[ i])
  855. {
  856. status->transferrate += m_Transfered[ i];
  857. count++;
  858. }
  859. }
  860. if ( count > 0)
  861. status->transferrate = status->transferrate / count;
  862. else if (m_Transfered[0])
  863. status->transferrate = m_Transfered[0];
  864. else
  865. status->timeleft=-1;
  866. PostMessage(m_pOwner->m_pOwner->m_hOwnerWnd, m_pOwner->m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_TRANSFERSTATUS, 0), (LPARAM)status);
  867. }
  868. void CTransferSocket::UpdateSendLed()
  869. {
  870. //Don't flood the main window with messages
  871. //Else performance would be really low
  872. LARGE_INTEGER curtime;
  873. LARGE_INTEGER freq;
  874. QueryPerformanceFrequency(&freq);
  875. QueryPerformanceCounter(&curtime);
  876. static LARGE_INTEGER oldtime={0};
  877. if ( ( (curtime.QuadPart-oldtime.QuadPart) < (freq.QuadPart/15) ) )
  878. return;
  879. oldtime=curtime;
  880. PostMessage(m_pOwner->m_pOwner->m_hOwnerWnd, m_pOwner->m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_SOCKETSTATUS, FZ_SOCKETSTATUS_SEND), 0);
  881. }
  882. void CTransferSocket::UpdateRecvLed()
  883. {
  884. //Don't flood the main window with messages
  885. //Else performance would be really low
  886. LARGE_INTEGER curtime;
  887. LARGE_INTEGER freq;
  888. QueryPerformanceFrequency(&freq);
  889. QueryPerformanceCounter(&curtime);
  890. static LARGE_INTEGER oldtime={0};
  891. if ( ( (curtime.QuadPart-oldtime.QuadPart) < (freq.QuadPart/15) ) )
  892. return;
  893. oldtime=curtime;
  894. PostMessage(m_pOwner->m_pOwner->m_hOwnerWnd, m_pOwner->m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_SOCKETSTATUS, FZ_SOCKETSTATUS_RECV), 0);
  895. }
  896. BOOL CTransferSocket::Create(
  897. #ifndef MPEXT_NO_SSL
  898. BOOL bUseSsl
  899. #endif
  900. )
  901. {
  902. #ifndef MPEXT_NO_SSL
  903. if (bUseSsl)
  904. m_pSslLayer = new CAsyncSslSocketLayer;
  905. #endif
  906. if (!m_pOwner->m_CurrentServer.fwbypass)
  907. {
  908. int nProxyType = COptions::GetOptionVal(OPTION_PROXYTYPE);
  909. if (nProxyType != PROXYTYPE_NOPROXY)
  910. {
  911. USES_CONVERSION;
  912. m_pProxyLayer = new CAsyncProxySocketLayer;
  913. if (nProxyType == PROXYTYPE_SOCKS4)
  914. m_pProxyLayer->SetProxy(PROXYTYPE_SOCKS4, T2CA(COptions::GetOption(OPTION_PROXYHOST)), COptions::GetOptionVal(OPTION_PROXYPORT));
  915. else if (nProxyType == PROXYTYPE_SOCKS4A)
  916. m_pProxyLayer->SetProxy(PROXYTYPE_SOCKS4A, T2CA(COptions::GetOption(OPTION_PROXYHOST)), COptions::GetOptionVal(OPTION_PROXYPORT));
  917. else if (nProxyType == PROXYTYPE_SOCKS5)
  918. if (COptions::GetOptionVal(OPTION_PROXYUSELOGON))
  919. m_pProxyLayer->SetProxy(PROXYTYPE_SOCKS5, T2CA(COptions::GetOption(OPTION_PROXYHOST)),
  920. COptions::GetOptionVal(OPTION_PROXYPORT),
  921. T2CA(COptions::GetOption(OPTION_PROXYUSER)),
  922. T2CA(CCrypt::decrypt(COptions::GetOption(OPTION_PROXYPASS))));
  923. else
  924. m_pProxyLayer->SetProxy(PROXYTYPE_SOCKS5, T2CA(COptions::GetOption(OPTION_PROXYHOST)),
  925. COptions::GetOptionVal(OPTION_PROXYPORT));
  926. else if (nProxyType == PROXYTYPE_HTTP11)
  927. if (COptions::GetOptionVal(OPTION_PROXYUSELOGON))
  928. m_pProxyLayer->SetProxy(PROXYTYPE_HTTP11, T2CA(COptions::GetOption(OPTION_PROXYHOST)), COptions::GetOptionVal(OPTION_PROXYPORT),
  929. T2CA(COptions::GetOption(OPTION_PROXYUSER)),
  930. T2CA(CCrypt::decrypt(COptions::GetOption(OPTION_PROXYPASS))));
  931. else
  932. m_pProxyLayer->SetProxy(PROXYTYPE_HTTP11, T2CA(COptions::GetOption(OPTION_PROXYHOST)), COptions::GetOptionVal(OPTION_PROXYPORT));
  933. else
  934. ASSERT(FALSE);
  935. AddLayer(m_pProxyLayer);
  936. }
  937. }
  938. if (!COptions::GetOptionVal(OPTION_LIMITPORTRANGE))
  939. {
  940. if (!CAsyncSocketEx::Create(0, SOCK_STREAM, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, 0, GetFamily()))
  941. return FALSE;
  942. return TRUE;
  943. }
  944. else
  945. {
  946. int min=COptions::GetOptionVal(OPTION_PORTRANGELOW);
  947. int max=COptions::GetOptionVal(OPTION_PORTRANGEHIGH);
  948. if (min>=max)
  949. {
  950. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTCREATEDUETOPORTRANGE,FZ_LOG_ERROR);
  951. return FALSE;
  952. }
  953. int startport=static_cast<int>(min+((double)rand()*(max-min))/(RAND_MAX+1));
  954. int port=startport;
  955. while (!CAsyncSocketEx::Create(port, SOCK_STREAM, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, 0, GetFamily()))
  956. {
  957. port++;
  958. if (port>max)
  959. port=min;
  960. if (port==startport)
  961. {
  962. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTCREATEDUETOPORTRANGE,FZ_LOG_ERROR);
  963. return FALSE;
  964. }
  965. }
  966. }
  967. return TRUE;
  968. }
  969. void CTransferSocket::Close()
  970. {
  971. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("Close()"));
  972. m_bCheckTimeout = FALSE;
  973. CAsyncSocketEx::Close();
  974. }
  975. int CTransferSocket::OnLayerCallback(std::list<t_callbackMsg>& callbacks)
  976. {
  977. for (std::list<t_callbackMsg>::iterator iter = callbacks.begin(); iter != callbacks.end(); iter++)
  978. {
  979. if (iter->nType == LAYERCALLBACK_STATECHANGE)
  980. {
  981. if (CAsyncSocketEx::LogStateChange(iter->nParam1, iter->nParam2))
  982. {
  983. const TCHAR * state2Desc = CAsyncSocketEx::GetStateDesc(iter->nParam2);
  984. const TCHAR * state1Desc = CAsyncSocketEx::GetStateDesc(iter->nParam1);
  985. if (iter->pLayer == m_pProxyLayer)
  986. LogMessage(__FILE__, __LINE__, this, FZ_LOG_INFO, _T("Proxy layer changed state from %s to %s"), state2Desc, state1Desc);
  987. #ifndef MPEXT_NO_SSL
  988. else if (iter->pLayer == m_pSslLayer)
  989. LogMessage(__FILE__, __LINE__, this, FZ_LOG_INFO, _T("TLS layer changed state from %s to %s"), state2Desc, state1Desc);
  990. #endif
  991. #ifndef MPEXT_NO_GSS
  992. else if (iter->pLayer == m_pGssLayer)
  993. LogMessage(__FILE__, __LINE__, this, FZ_LOG_INFO, _T("GSS layer changed state from %s to %s"), state2Desc, state1Desc);
  994. #endif
  995. else
  996. LogMessage(__FILE__, __LINE__, this, FZ_LOG_INFO, _T("Layer @ %d changed state from %s to %s"), iter->pLayer, state2Desc, state1Desc);
  997. }
  998. }
  999. else if (iter->nType == LAYERCALLBACK_LAYERSPECIFIC)
  1000. {
  1001. if (iter->pLayer == m_pProxyLayer)
  1002. {
  1003. switch (iter->nParam1)
  1004. {
  1005. case PROXYERROR_NOCONN:
  1006. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_NOCONN, FZ_LOG_ERROR);
  1007. break;
  1008. case PROXYERROR_REQUESTFAILED:
  1009. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_REQUESTFAILED, FZ_LOG_ERROR);
  1010. break;
  1011. case PROXYERROR_AUTHTYPEUNKNOWN:
  1012. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_AUTHTYPEUNKNOWN, FZ_LOG_ERROR);
  1013. break;
  1014. case PROXYERROR_AUTHFAILED:
  1015. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_AUTHFAILED, FZ_LOG_ERROR);
  1016. break;
  1017. case PROXYERROR_AUTHNOLOGON:
  1018. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_AUTHNOLOGON, FZ_LOG_ERROR);
  1019. break;
  1020. case PROXYERROR_CANTRESOLVEHOST:
  1021. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_CANTRESOLVEHOST, FZ_LOG_ERROR);
  1022. break;
  1023. default:
  1024. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Unknown proxy error"));
  1025. }
  1026. }
  1027. #ifndef MPEXT_NO_SSL
  1028. else if (iter->pLayer == m_pSslLayer)
  1029. {
  1030. switch (iter->nParam1)
  1031. {
  1032. case SSL_INFO:
  1033. switch(iter->nParam2)
  1034. {
  1035. case SSL_INFO_SHUTDOWNCOMPLETE:
  1036. CloseAndEnsureSendClose(0);
  1037. break;
  1038. case SSL_INFO_ESTABLISHED:
  1039. m_pOwner->ShowStatus(IDS_STATUSMSG_SSLESTABLISHEDTRANSFER, FZ_LOG_STATUS);
  1040. TriggerEvent(FD_FORCEREAD);
  1041. break;
  1042. }
  1043. break;
  1044. case SSL_FAILURE:
  1045. switch (iter->nParam2)
  1046. {
  1047. case SSL_FAILURE_ESTABLISH:
  1048. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTESTABLISHSSLCONNECTION, FZ_LOG_ERROR);
  1049. break;
  1050. #ifndef MPEXT_NO_SSLDLL
  1051. case SSL_FAILURE_LOADDLLS:
  1052. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTLOADSSLDLLS, FZ_LOG_ERROR);
  1053. break;
  1054. #endif
  1055. case SSL_FAILURE_INITSSL:
  1056. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTINITSSL, FZ_LOG_ERROR);
  1057. break;
  1058. }
  1059. EnsureSendClose(CSMODE_TRANSFERERROR);
  1060. break;
  1061. case SSL_VERIFY_CERT:
  1062. t_SslCertData data;
  1063. LPTSTR CertError = NULL;
  1064. if (m_pSslLayer->GetPeerCertificateData(data, CertError))
  1065. m_pSslLayer->SetNotifyReply(data.priv_data, SSL_VERIFY_CERT, 1);
  1066. else
  1067. {
  1068. CString str;
  1069. str.Format(TLS_CERT_DECODE_ERROR, CertError);
  1070. m_pOwner->ShowStatus(str, FZ_LOG_ERROR);
  1071. CloseAndEnsureSendClose(CSMODE_TRANSFERERROR);
  1072. }
  1073. break;
  1074. }
  1075. }
  1076. #endif
  1077. #ifndef MPEXT_NO_GSS
  1078. else if (iter->pLayer == m_pGssLayer)
  1079. {
  1080. USES_CONVERSION;
  1081. switch (iter->nParam1)
  1082. {
  1083. case GSS_INFO:
  1084. LogMessageRaw(FZ_LOG_INFO, A2CT(iter->str));
  1085. break;
  1086. case GSS_ERROR:
  1087. LogMessageRaw(FZ_LOG_APIERROR, A2CT(iter->str));
  1088. break;
  1089. case GSS_SHUTDOWN_COMPLETE:
  1090. CloseAndEnsureSendClose(0);
  1091. break;
  1092. }
  1093. }
  1094. #endif
  1095. }
  1096. delete [] iter->str;
  1097. }
  1098. return 0;
  1099. }
  1100. void CTransferSocket::Transfered(int count, CTime time)
  1101. {
  1102. CTimeSpan ts = time - m_TransferedFirst;
  1103. int diff = (int)ts.GetTotalSeconds();
  1104. if (diff < 0)
  1105. diff = 0;
  1106. if ( diff >= SPEED_SECONDS)
  1107. {
  1108. int move = diff - SPEED_SECONDS + 1;
  1109. int start = SPEED_SECONDS - move;
  1110. if ( start <= 0)
  1111. start = 0;
  1112. else
  1113. {
  1114. for ( int i = 0; i < SPEED_SECONDS - move; i++)
  1115. {
  1116. m_Transfered[ i] = m_Transfered[ i + move];
  1117. m_UsedForTransfer[ i] = m_UsedForTransfer[ i + move];
  1118. }
  1119. }
  1120. for ( int i = start; i < SPEED_SECONDS; i++)
  1121. {
  1122. m_Transfered[ i] = 0;
  1123. m_UsedForTransfer[ i] = false;
  1124. }
  1125. if (move >= SPEED_SECONDS)
  1126. {
  1127. m_TransferedFirst = time;
  1128. diff = 0;
  1129. }
  1130. else
  1131. {
  1132. m_TransferedFirst += CTimeSpan( move);
  1133. ts = time - m_TransferedFirst;
  1134. diff = (int)(ts.GetTotalSeconds() % 60);
  1135. }
  1136. }
  1137. m_Transfered[ diff] += count;
  1138. for ( int i = 0; i < diff - 1; i++)
  1139. m_UsedForTransfer[ i] = true;
  1140. }
  1141. #ifndef MPEXT_NO_GSS
  1142. void CTransferSocket::UseGSS(CAsyncGssSocketLayer *pGssLayer)
  1143. {
  1144. m_pGssLayer = new CAsyncGssSocketLayer;
  1145. m_pGssLayer->InitTransferChannel(pGssLayer);
  1146. }
  1147. #endif
  1148. #ifndef MPEXT_NO_ZLIB
  1149. bool CTransferSocket::InitZlib(int level)
  1150. {
  1151. int res;
  1152. if (m_nMode & CSMODE_UPLOAD)
  1153. res = deflateInit2(&m_zlibStream, level, Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
  1154. else
  1155. res = inflateInit2(&m_zlibStream, 15);
  1156. if (res == Z_OK)
  1157. m_useZlib = true;
  1158. return res == Z_OK;
  1159. }
  1160. #endif
  1161. int CTransferSocket::ReadDataFromFile(char *buffer, int len)
  1162. {
  1163. TRY
  1164. {
  1165. // Comparing to Filezilla 2, we do not do any translation locally,
  1166. // leaving it onto the server (what Filezilla 3 seems to do too)
  1167. const char Bom[3] = "\xEF\xBB\xBF";
  1168. int read = m_pFile->Read(buffer, len);
  1169. if (COptions::GetOptionVal(OPTION_MPEXT_REMOVE_BOM) &&
  1170. m_transferdata.bType && (read >= sizeof(Bom)) && (memcmp(buffer, Bom, sizeof(Bom)) == 0))
  1171. {
  1172. memcpy(buffer, buffer + sizeof(Bom), read - sizeof(Bom));
  1173. read -= sizeof(Bom);
  1174. int read2 = m_pFile->Read(buffer + read, sizeof(Bom));
  1175. if (read2 > 0)
  1176. {
  1177. read += read2;
  1178. }
  1179. }
  1180. return read;
  1181. }
  1182. CATCH_ALL(e)
  1183. {
  1184. TCHAR error[BUFSIZE];
  1185. if (e->GetErrorMessage(error, BUFSIZE))
  1186. m_pOwner->ShowStatus(error, FZ_LOG_ERROR);
  1187. CloseOnShutDownOrError(CSMODE_TRANSFERERROR);
  1188. return -1;
  1189. }
  1190. END_CATCH_ALL;
  1191. }
  1192. void CTransferSocket::LogSocketMessage(int nMessageType, LPCTSTR pMsgFormat)
  1193. {
  1194. LogMessage(nMessageType, pMsgFormat);
  1195. }
  1196. void CTransferSocket::EnsureSendClose(int Mode)
  1197. {
  1198. if (!m_bSentClose)
  1199. {
  1200. if (Mode != 0)
  1201. {
  1202. m_nMode |= Mode;
  1203. }
  1204. m_bSentClose = TRUE;
  1205. VERIFY(m_pOwner->m_pOwner->PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_TRANSFEREND, m_nMode));
  1206. }
  1207. }
  1208. void CTransferSocket::CloseAndEnsureSendClose(int Mode)
  1209. {
  1210. Close();
  1211. EnsureSendClose(Mode);
  1212. }
  1213. void CTransferSocket::CloseOnShutDownOrError(int Mode)
  1214. {
  1215. if (ShutDown())
  1216. {
  1217. CloseAndEnsureSendClose(Mode);
  1218. }
  1219. else
  1220. {
  1221. int Error = GetLastError();
  1222. if (Error != WSAEWOULDBLOCK)
  1223. {
  1224. // Log always or only when (Mode & CSMODE_TRANSFERERROR)?
  1225. // Does it anyway make sense at all to call this with Mode == 0?
  1226. LogError(Error);
  1227. CloseAndEnsureSendClose(Mode);
  1228. }
  1229. }
  1230. }
  1231. void CTransferSocket::LogError(int Error)
  1232. {
  1233. wchar_t * Buffer;
  1234. int Len = FormatMessage(
  1235. FORMAT_MESSAGE_FROM_SYSTEM |
  1236. FORMAT_MESSAGE_IGNORE_INSERTS |
  1237. FORMAT_MESSAGE_ARGUMENT_ARRAY |
  1238. FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, Error, 0, (LPTSTR)&Buffer, 0, NULL);
  1239. if (Len > 0)
  1240. {
  1241. m_pOwner->ShowStatus(Buffer, FZ_LOG_ERROR);
  1242. LocalFree(Buffer);
  1243. }
  1244. }