TransferSocket.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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 (m_transferdata.bType && (read >= sizeof(Bom)) && (memcmp(buffer, Bom, sizeof(Bom)) == 0))
  1170. {
  1171. memcpy(buffer, buffer + sizeof(Bom), read - sizeof(Bom));
  1172. read -= sizeof(Bom);
  1173. int read2 = m_pFile->Read(buffer + read, sizeof(Bom));
  1174. if (read2 > 0)
  1175. {
  1176. read += read2;
  1177. }
  1178. }
  1179. return read;
  1180. }
  1181. CATCH_ALL(e)
  1182. {
  1183. TCHAR error[BUFSIZE];
  1184. if (e->GetErrorMessage(error, BUFSIZE))
  1185. m_pOwner->ShowStatus(error, FZ_LOG_ERROR);
  1186. CloseOnShutDownOrError(CSMODE_TRANSFERERROR);
  1187. return -1;
  1188. }
  1189. END_CATCH_ALL;
  1190. }
  1191. void CTransferSocket::LogSocketMessage(int nMessageType, LPCTSTR pMsgFormat)
  1192. {
  1193. LogMessage(nMessageType, pMsgFormat);
  1194. }
  1195. void CTransferSocket::EnsureSendClose(int Mode)
  1196. {
  1197. if (!m_bSentClose)
  1198. {
  1199. if (Mode != 0)
  1200. {
  1201. m_nMode |= Mode;
  1202. }
  1203. m_bSentClose = TRUE;
  1204. VERIFY(m_pOwner->m_pOwner->PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_TRANSFEREND, m_nMode));
  1205. }
  1206. }
  1207. void CTransferSocket::CloseAndEnsureSendClose(int Mode)
  1208. {
  1209. Close();
  1210. EnsureSendClose(Mode);
  1211. }
  1212. void CTransferSocket::CloseOnShutDownOrError(int Mode)
  1213. {
  1214. if (ShutDown())
  1215. {
  1216. CloseAndEnsureSendClose(Mode);
  1217. }
  1218. else
  1219. {
  1220. int Error = GetLastError();
  1221. if (Error != WSAEWOULDBLOCK)
  1222. {
  1223. // Log always or only when (Mode & CSMODE_TRANSFERERROR)?
  1224. // Does it anyway make sense at all to call this with Mode == 0?
  1225. LogError(Error);
  1226. CloseAndEnsureSendClose(Mode);
  1227. }
  1228. }
  1229. }
  1230. void CTransferSocket::LogError(int Error)
  1231. {
  1232. wchar_t * Buffer;
  1233. int Len = FormatMessage(
  1234. FORMAT_MESSAGE_FROM_SYSTEM |
  1235. FORMAT_MESSAGE_IGNORE_INSERTS |
  1236. FORMAT_MESSAGE_ARGUMENT_ARRAY |
  1237. FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, Error, 0, (LPTSTR)&Buffer, 0, NULL);
  1238. if (Len > 0)
  1239. {
  1240. m_pOwner->ShowStatus(Buffer, FZ_LOG_ERROR);
  1241. LocalFree(Buffer);
  1242. }
  1243. }