TransferSocket.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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. if (res == SSL_FAILURE_INITSSL)
  369. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTINITSSL, FZ_LOG_ERROR);
  370. if (res)
  371. {
  372. CloseAndEnsureSendClose(CSMODE_TRANSFERERROR);
  373. return;
  374. }
  375. }
  376. #endif
  377. #ifndef MPEXT_NO_GSS
  378. if (m_pGssLayer)
  379. {
  380. AddLayer(m_pGssLayer);
  381. }
  382. #endif
  383. m_TransferedFirst = m_StartTime = CTime::GetCurrentTime();
  384. m_LastActiveTime = CTime::GetCurrentTime();
  385. }
  386. }
  387. void CTransferSocket::OnConnect(int nErrorCode)
  388. {
  389. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("OnConnect(%d)"), nErrorCode);
  390. if (nErrorCode)
  391. {
  392. TCHAR buffer[1000];
  393. memset(buffer, 0, sizeof(buffer));
  394. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, nErrorCode, 0, buffer, 999, 0);
  395. CString str;
  396. str.Format(IDS_ERRORMSG_CANTOPENTRANSFERCHANNEL,buffer);
  397. str.Replace( _T("\n"), _T("\0") );
  398. str.Replace( _T("\r"), _T("\0") );
  399. m_pOwner->ShowStatus(str, FZ_LOG_ERROR);
  400. CloseAndEnsureSendClose(CSMODE_TRANSFERERROR);
  401. }
  402. else
  403. {
  404. /* Set internal socket send buffer
  405. * this should fix the speed problems some users have reported
  406. */
  407. DWORD value;
  408. int len = sizeof(value);
  409. GetSockOpt(SO_SNDBUF, &value, &len);
  410. // MPEXT
  411. int sndbuf = COptions::GetOptionVal(OPTION_MPEXT_SNDBUF);
  412. if (value < sndbuf)
  413. {
  414. value = sndbuf;
  415. SetSockOpt(SO_SNDBUF, &value, sizeof(value));
  416. }
  417. }
  418. if (m_nTransferState == STATE_WAITING)
  419. {
  420. // OnReceive (invoked by m_nNotifyWaiting including FD_READ)
  421. // will call back to OnConnected (as we won't be connected yet).
  422. // This is needed for file transfers only, where SetActive is
  423. // called only after 1xx response to RETR (and similar) arrives.
  424. // But we get FD_CONNECT earlier, hence we get to thisa branch.
  425. // With directory listing, SetActive is called before Connect,
  426. // so we are already STATE_STARTING on FD_CONNECT.
  427. // It should probably behave the same in both scenarios.
  428. m_nNotifyWaiting |= FD_READ;
  429. }
  430. else if (m_nTransferState == STATE_STARTING)
  431. {
  432. m_nTransferState = STATE_STARTED;
  433. m_TransferedFirst = m_StartTime = CTime::GetCurrentTime();
  434. m_LastActiveTime=CTime::GetCurrentTime();
  435. #ifndef MPEXT_NO_SSL
  436. if (m_pSslLayer)
  437. {
  438. AddLayer(m_pSslLayer);
  439. int res = m_pSslLayer->InitSSLConnection(true, m_pOwner->m_pSslLayer,
  440. COptions::GetOptionVal(OPTION_MPEXT_SSLSESSIONREUSE),
  441. COptions::GetOptionVal(OPTION_MPEXT_MIN_TLS_VERSION),
  442. COptions::GetOptionVal(OPTION_MPEXT_MAX_TLS_VERSION));
  443. if (res == SSL_FAILURE_INITSSL)
  444. {
  445. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTINITSSL, FZ_LOG_ERROR);
  446. }
  447. if (res)
  448. {
  449. CloseAndEnsureSendClose(CSMODE_TRANSFERERROR);
  450. return;
  451. }
  452. }
  453. #endif
  454. #ifndef MPEXT_NO_GSS
  455. if (m_pGssLayer)
  456. {
  457. AddLayer(m_pGssLayer);
  458. }
  459. #endif
  460. }
  461. }
  462. void CTransferSocket::OnClose(int nErrorCode)
  463. {
  464. LogMessage(__FILE__, __LINE__, this, FZ_LOG_DEBUG, _T("OnClose(%d)"), nErrorCode);
  465. if (m_nTransferState == STATE_WAITING)
  466. {
  467. m_nNotifyWaiting |= FD_CLOSE;
  468. return;
  469. }
  470. OnReceive(0);
  471. CloseAndEnsureSendClose(0);
  472. }
  473. int CTransferSocket::CheckForTimeout(int delay)
  474. {
  475. UpdateStatusBar(false);
  476. if (!m_bCheckTimeout)
  477. {
  478. // we are closed, so make sure the FTP control socket is itself checking for
  479. // timeout as we are not
  480. return 0;
  481. }
  482. CTimeSpan span = CTime::GetCurrentTime()-m_LastActiveTime;
  483. if (span.GetTotalSeconds()>=delay)
  484. {
  485. m_pOwner->ShowStatus(IDS_ERRORMSG_TIMEOUT, FZ_LOG_ERROR);
  486. CloseAndEnsureSendClose(CSMODE_TRANSFERTIMEOUT);
  487. return 2;
  488. }
  489. return 1;
  490. }
  491. void CTransferSocket::SetActive()
  492. {
  493. LogMessage(__FILE__, __LINE__, this, FZ_LOG_DEBUG, _T("SetActive()"));
  494. if (m_nTransferState == STATE_WAITING)
  495. m_nTransferState = STATE_STARTING;
  496. m_bCheckTimeout = TRUE;
  497. m_LastActiveTime = CTime::GetCurrentTime();
  498. if (m_nNotifyWaiting & FD_READ)
  499. OnReceive(0);
  500. if (m_nNotifyWaiting & FD_WRITE)
  501. OnSend(0);
  502. if (m_nNotifyWaiting & FD_CLOSE)
  503. OnClose(0);
  504. }
  505. void CTransferSocket::OnSend(int nErrorCode)
  506. {
  507. if (m_nTransferState == STATE_WAITING)
  508. {
  509. m_nNotifyWaiting |= FD_WRITE;
  510. return;
  511. }
  512. if (m_bSentClose)
  513. {
  514. return;
  515. }
  516. if (m_bListening)
  517. {
  518. return;
  519. }
  520. if (!(m_nMode&CSMODE_UPLOAD))
  521. {
  522. return;
  523. }
  524. if (m_nTransferState == STATE_STARTING)
  525. {
  526. OnConnect(0);
  527. }
  528. #ifndef MPEXT_NO_ZLIB
  529. if (m_useZlib)
  530. {
  531. if (!m_pBuffer)
  532. {
  533. m_pBuffer = new char[BUFSIZE];
  534. m_bufferpos = 0;
  535. m_zlibStream.next_out = (Bytef *)m_pBuffer;
  536. m_zlibStream.avail_out = BUFSIZE;
  537. }
  538. if (!m_pBuffer2)
  539. {
  540. m_pBuffer2 = new char[BUFSIZE];
  541. m_zlibStream.next_in = (Bytef *)m_pBuffer2;
  542. }
  543. bool beenWaiting = false;
  544. while (true)
  545. {
  546. int numsend;
  547. if (!m_zlibStream.avail_in)
  548. {
  549. if (m_pFile)
  550. {
  551. DWORD numread;
  552. numread = ReadDataFromFile(m_pBuffer2, BUFSIZE);
  553. if (numread < 0)
  554. {
  555. return;
  556. }
  557. m_transferdata.transferleft -= numread;
  558. m_zlibStream.next_in = (Bytef *)m_pBuffer2;
  559. m_zlibStream.avail_in = numread;
  560. if (numread < BUFSIZE)
  561. m_pFile = 0;
  562. }
  563. }
  564. if (!m_zlibStream.avail_out)
  565. {
  566. if (m_bufferpos >= BUFSIZE)
  567. {
  568. m_bufferpos = 0;
  569. m_zlibStream.next_out = (Bytef *)m_pBuffer;
  570. m_zlibStream.avail_out = BUFSIZE;
  571. }
  572. }
  573. int res = Z_OK;
  574. if (m_zlibStream.avail_out)
  575. {
  576. res = deflate(&m_zlibStream, m_pFile ? 0 : Z_FINISH);
  577. if (res != Z_OK && (!m_pFile && res != Z_STREAM_END))
  578. {
  579. m_pOwner->ShowStatus("Decompression error", FZ_LOG_ERROR);
  580. CloseAndEnsureSendClose(CSMODE_TRANSFERERROR);
  581. return;
  582. }
  583. }
  584. numsend = BUFSIZE;
  585. int len = BUFSIZE - m_bufferpos - m_zlibStream.avail_out;
  586. if (!len && !m_pFile)
  587. {
  588. break;
  589. }
  590. if (len < BUFSIZE)
  591. numsend = len;
  592. int nLimit = (int)m_pOwner->GetAbleToTransferSize(CControlSocket::upload, beenWaiting);
  593. if (nLimit != -1 && GetState() != closed && numsend > nLimit)
  594. numsend = nLimit;
  595. if (!numsend)
  596. {
  597. TriggerEvent(FD_WRITE);
  598. return;
  599. }
  600. int numsent = Send(m_pBuffer + m_bufferpos, numsend);
  601. if (numsent == SOCKET_ERROR)
  602. {
  603. int nError = GetLastError();
  604. if (nError == WSAENOTCONN)
  605. {
  606. //Not yet connected
  607. return;
  608. }
  609. #ifndef MPEXT_NO_SSL
  610. else if (m_pSslLayer && nError == WSAESHUTDOWN)
  611. {
  612. // Do nothing, wait for shutdown complete notification.
  613. return;
  614. }
  615. #endif
  616. else if (nError != WSAEWOULDBLOCK)
  617. {
  618. CloseOnShutDownOrError(CSMODE_TRANSFERERROR);
  619. }
  620. UpdateStatusBar(false);
  621. return;
  622. }
  623. Transfered( numsent, CTime::GetCurrentTime());
  624. m_pOwner->SpeedLimitAddTransferredBytes(CControlSocket::upload, numsent);
  625. m_LastActiveTime = CTime::GetCurrentTime();
  626. UpdateSendLed();
  627. m_bufferpos += numsent;
  628. UpdateStatusBar(false);
  629. if (!m_zlibStream.avail_in && !m_pFile && m_zlibStream.avail_out &&
  630. m_zlibStream.avail_out + m_bufferpos == BUFSIZE && res == Z_STREAM_END)
  631. {
  632. CloseOnShutDownOrError(0);
  633. return;
  634. }
  635. //Check if there are other commands in the command queue.
  636. MSG msg;
  637. if (PeekMessage(&msg,0, 0, 0, PM_NOREMOVE))
  638. {
  639. TriggerEvent(FD_WRITE);
  640. return;
  641. }
  642. }
  643. }
  644. else
  645. #endif
  646. {
  647. if (!m_pFile)
  648. {
  649. return;
  650. }
  651. if (!m_pBuffer)
  652. m_pBuffer = new char[BUFSIZE];
  653. int numread;
  654. bool beenWaiting = false;
  655. _int64 currentBufferSize;
  656. if (GetState() != closed)
  657. currentBufferSize = m_pOwner->GetAbleToTransferSize(CControlSocket::upload, beenWaiting);
  658. else
  659. currentBufferSize = BUFSIZE;
  660. if (!currentBufferSize && !m_bufferpos)
  661. {
  662. // Not allowed to send yet, try later
  663. TriggerEvent(FD_WRITE);
  664. return;
  665. }
  666. else if (m_bufferpos < currentBufferSize)
  667. {
  668. numread = ReadDataFromFile(m_pBuffer + m_bufferpos, static_cast<int>(currentBufferSize - m_bufferpos));
  669. if (numread < 0 )
  670. {
  671. return;
  672. }
  673. else if (!numread && !m_bufferpos)
  674. {
  675. CloseOnShutDownOrError(0);
  676. return;
  677. }
  678. }
  679. else
  680. numread = 0;
  681. ASSERT((numread+m_bufferpos) <= BUFSIZE);
  682. ASSERT(numread>=0);
  683. ASSERT(m_bufferpos>=0);
  684. if (numread+m_bufferpos <= 0)
  685. {
  686. CloseOnShutDownOrError(0);
  687. return;
  688. }
  689. int numsent = Send(m_pBuffer, numread + m_bufferpos);
  690. while (TRUE)
  691. {
  692. if (numsent != SOCKET_ERROR)
  693. {
  694. Transfered(numsent, CTime::GetCurrentTime());
  695. m_pOwner->SpeedLimitAddTransferredBytes(CControlSocket::upload, numsent);
  696. m_LastActiveTime = CTime::GetCurrentTime();
  697. UpdateSendLed();
  698. m_transferdata.transferleft -= numsent;
  699. }
  700. if (numsent==SOCKET_ERROR || !numsent)
  701. {
  702. int nError = GetLastError();
  703. if (nError == WSAENOTCONN)
  704. {
  705. //Not yet connected
  706. m_bufferpos += numread;
  707. return;
  708. }
  709. else if (nError == WSAEWOULDBLOCK)
  710. {
  711. m_bufferpos += numread;
  712. }
  713. #ifndef MPEXT_NO_SSL
  714. else if (m_pSslLayer && nError == WSAESHUTDOWN)
  715. {
  716. m_bufferpos += numread;
  717. // Do nothing, wait for shutdown complete notification.
  718. return;
  719. }
  720. #endif
  721. else
  722. {
  723. CloseOnShutDownOrError(CSMODE_TRANSFERERROR);
  724. }
  725. UpdateStatusBar(false);
  726. return;
  727. }
  728. else
  729. {
  730. int pos = numread + m_bufferpos - numsent;
  731. if (pos < 0 || (numsent + pos) > BUFSIZE)
  732. {
  733. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Index out of range"));
  734. CloseOnShutDownOrError(CSMODE_TRANSFERERROR);
  735. return;
  736. }
  737. else if (!pos && numread < (currentBufferSize-m_bufferpos) && m_bufferpos != currentBufferSize)
  738. {
  739. CloseOnShutDownOrError(0);
  740. return;
  741. }
  742. else if (!pos)
  743. {
  744. m_bufferpos = 0;
  745. }
  746. else
  747. {
  748. memmove(m_pBuffer, m_pBuffer+numsent, pos);
  749. m_bufferpos=pos;
  750. }
  751. }
  752. //Check if there are other commands in the command queue.
  753. MSG msg;
  754. if (PeekMessage(&msg, 0, m_nInternalMessageID, m_nInternalMessageID, PM_NOREMOVE))
  755. {
  756. //Send resume message
  757. LogMessage(__FILE__, __LINE__, this, FZ_LOG_DEBUG, _T("Message waiting in queue, resuming later"));
  758. TriggerEvent(FD_WRITE);
  759. UpdateStatusBar(false);
  760. return;
  761. }
  762. UpdateStatusBar(false);
  763. if (GetState() != closed)
  764. currentBufferSize = m_pOwner->GetAbleToTransferSize(CControlSocket::upload, beenWaiting);
  765. else
  766. currentBufferSize = BUFSIZE;
  767. if (m_bufferpos < currentBufferSize)
  768. {
  769. numread = ReadDataFromFile(m_pBuffer + m_bufferpos, static_cast<int>(currentBufferSize - m_bufferpos));
  770. if (numread < 0 )
  771. {
  772. return;
  773. }
  774. else if (!numread && !m_bufferpos)
  775. {
  776. CloseOnShutDownOrError(0);
  777. return;
  778. }
  779. }
  780. else
  781. {
  782. numread = 0;
  783. }
  784. if (!currentBufferSize && !m_bufferpos)
  785. {
  786. // Not allowed to send yet, try later
  787. TriggerEvent(FD_WRITE);
  788. return;
  789. }
  790. ASSERT(numread>=0);
  791. ASSERT(m_bufferpos>=0);
  792. numsent = Send(m_pBuffer, numread+m_bufferpos);
  793. }
  794. }
  795. }
  796. void CTransferSocket::UpdateStatusBar(bool forceUpdate)
  797. {
  798. if (m_nTransferState != STATE_STARTED)
  799. return;
  800. if (!forceUpdate)
  801. {
  802. //Don't flood the main window with messages
  803. //Else performance would be really low
  804. LARGE_INTEGER curtime;
  805. LARGE_INTEGER freq;
  806. QueryPerformanceFrequency(&freq);
  807. QueryPerformanceCounter(&curtime);
  808. if (((curtime.QuadPart-m_LastUpdateTime.QuadPart) < (freq.QuadPart/15) ) )
  809. return;
  810. m_LastUpdateTime = curtime;
  811. }
  812. //Update the statusbar
  813. CTimeSpan timespan=CTime::GetCurrentTime()-m_StartTime;
  814. int elapsed=(int)timespan.GetTotalSeconds();
  815. t_ffam_transferstatus *status=new t_ffam_transferstatus;
  816. status->bFileTransfer = m_nMode & (CSMODE_DOWNLOAD | CSMODE_UPLOAD);
  817. #ifdef MPEXT
  818. status->transfersize = m_transferdata.transfersize;
  819. #endif
  820. status->timeelapsed=elapsed;
  821. status->bytes=m_transferdata.transfersize-m_transferdata.transferleft;
  822. if (m_transferdata.transfersize>0 && !(m_nMode&CSMODE_LIST))
  823. {
  824. double leftmodifier=static_cast<double>(m_transferdata.transfersize-m_transferdata.nTransferStart-m_transferdata.transferleft);
  825. leftmodifier*=100;
  826. if (m_transferdata.transfersize-m_transferdata.nTransferStart)
  827. leftmodifier /= (m_transferdata.transfersize-m_transferdata.nTransferStart);
  828. else
  829. leftmodifier = 1;
  830. if (leftmodifier == 0)
  831. leftmodifier = 1;
  832. double leftmodifier2 = 100 - leftmodifier;
  833. int left=static_cast<int>((elapsed/leftmodifier)*leftmodifier2);
  834. double percent=100*static_cast<double>(m_transferdata.transfersize-m_transferdata.transferleft);
  835. percent/=m_transferdata.transfersize;
  836. status->percent=static_cast<int>(percent);
  837. if (status->percent>100)
  838. status->percent=100;
  839. if (left < 0)
  840. left = -1;
  841. status->timeleft=left;
  842. }
  843. else
  844. {
  845. status->percent=-1;
  846. status->timeleft=-1;
  847. }
  848. int count = 0;
  849. status->transferrate = 0;
  850. for ( int i = 0; i < SPEED_SECONDS; i++)
  851. {
  852. if ( m_UsedForTransfer[ i])
  853. {
  854. status->transferrate += m_Transfered[ i];
  855. count++;
  856. }
  857. }
  858. if ( count > 0)
  859. status->transferrate = status->transferrate / count;
  860. else if (m_Transfered[0])
  861. status->transferrate = m_Transfered[0];
  862. else
  863. status->timeleft=-1;
  864. PostMessage(m_pOwner->m_pOwner->m_hOwnerWnd, m_pOwner->m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_TRANSFERSTATUS, 0), (LPARAM)status);
  865. }
  866. void CTransferSocket::UpdateSendLed()
  867. {
  868. //Don't flood the main window with messages
  869. //Else performance would be really low
  870. LARGE_INTEGER curtime;
  871. LARGE_INTEGER freq;
  872. QueryPerformanceFrequency(&freq);
  873. QueryPerformanceCounter(&curtime);
  874. static LARGE_INTEGER oldtime={0};
  875. if ( ( (curtime.QuadPart-oldtime.QuadPart) < (freq.QuadPart/15) ) )
  876. return;
  877. oldtime=curtime;
  878. PostMessage(m_pOwner->m_pOwner->m_hOwnerWnd, m_pOwner->m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_SOCKETSTATUS, FZ_SOCKETSTATUS_SEND), 0);
  879. }
  880. void CTransferSocket::UpdateRecvLed()
  881. {
  882. //Don't flood the main window with messages
  883. //Else performance would be really low
  884. LARGE_INTEGER curtime;
  885. LARGE_INTEGER freq;
  886. QueryPerformanceFrequency(&freq);
  887. QueryPerformanceCounter(&curtime);
  888. static LARGE_INTEGER oldtime={0};
  889. if ( ( (curtime.QuadPart-oldtime.QuadPart) < (freq.QuadPart/15) ) )
  890. return;
  891. oldtime=curtime;
  892. PostMessage(m_pOwner->m_pOwner->m_hOwnerWnd, m_pOwner->m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_SOCKETSTATUS, FZ_SOCKETSTATUS_RECV), 0);
  893. }
  894. BOOL CTransferSocket::Create(
  895. #ifndef MPEXT_NO_SSL
  896. BOOL bUseSsl
  897. #endif
  898. )
  899. {
  900. #ifndef MPEXT_NO_SSL
  901. if (bUseSsl)
  902. m_pSslLayer = new CAsyncSslSocketLayer;
  903. #endif
  904. if (!m_pOwner->m_CurrentServer.fwbypass)
  905. {
  906. int nProxyType = COptions::GetOptionVal(OPTION_PROXYTYPE);
  907. if (nProxyType != PROXYTYPE_NOPROXY)
  908. {
  909. USES_CONVERSION;
  910. m_pProxyLayer = new CAsyncProxySocketLayer;
  911. if (nProxyType == PROXYTYPE_SOCKS4)
  912. m_pProxyLayer->SetProxy(PROXYTYPE_SOCKS4, T2CA(COptions::GetOption(OPTION_PROXYHOST)), COptions::GetOptionVal(OPTION_PROXYPORT));
  913. else if (nProxyType == PROXYTYPE_SOCKS4A)
  914. m_pProxyLayer->SetProxy(PROXYTYPE_SOCKS4A, T2CA(COptions::GetOption(OPTION_PROXYHOST)), COptions::GetOptionVal(OPTION_PROXYPORT));
  915. else if (nProxyType == PROXYTYPE_SOCKS5)
  916. if (COptions::GetOptionVal(OPTION_PROXYUSELOGON))
  917. m_pProxyLayer->SetProxy(PROXYTYPE_SOCKS5, T2CA(COptions::GetOption(OPTION_PROXYHOST)),
  918. COptions::GetOptionVal(OPTION_PROXYPORT),
  919. T2CA(COptions::GetOption(OPTION_PROXYUSER)),
  920. T2CA(CCrypt::decrypt(COptions::GetOption(OPTION_PROXYPASS))));
  921. else
  922. m_pProxyLayer->SetProxy(PROXYTYPE_SOCKS5, T2CA(COptions::GetOption(OPTION_PROXYHOST)),
  923. COptions::GetOptionVal(OPTION_PROXYPORT));
  924. else if (nProxyType == PROXYTYPE_HTTP11)
  925. if (COptions::GetOptionVal(OPTION_PROXYUSELOGON))
  926. m_pProxyLayer->SetProxy(PROXYTYPE_HTTP11, T2CA(COptions::GetOption(OPTION_PROXYHOST)), 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_HTTP11, T2CA(COptions::GetOption(OPTION_PROXYHOST)), COptions::GetOptionVal(OPTION_PROXYPORT));
  931. else
  932. ASSERT(FALSE);
  933. AddLayer(m_pProxyLayer);
  934. }
  935. }
  936. if (!COptions::GetOptionVal(OPTION_LIMITPORTRANGE))
  937. {
  938. if (!CAsyncSocketEx::Create(0, SOCK_STREAM, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, 0, GetFamily()))
  939. return FALSE;
  940. return TRUE;
  941. }
  942. else
  943. {
  944. int min=COptions::GetOptionVal(OPTION_PORTRANGELOW);
  945. int max=COptions::GetOptionVal(OPTION_PORTRANGEHIGH);
  946. if (min>=max)
  947. {
  948. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTCREATEDUETOPORTRANGE,FZ_LOG_ERROR);
  949. return FALSE;
  950. }
  951. int startport=static_cast<int>(min+((double)rand()*(max-min))/(RAND_MAX+1));
  952. int port=startport;
  953. while (!CAsyncSocketEx::Create(port, SOCK_STREAM, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, 0, GetFamily()))
  954. {
  955. port++;
  956. if (port>max)
  957. port=min;
  958. if (port==startport)
  959. {
  960. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTCREATEDUETOPORTRANGE,FZ_LOG_ERROR);
  961. return FALSE;
  962. }
  963. }
  964. }
  965. return TRUE;
  966. }
  967. void CTransferSocket::Close()
  968. {
  969. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("Close()"));
  970. m_bCheckTimeout = FALSE;
  971. CAsyncSocketEx::Close();
  972. }
  973. int CTransferSocket::OnLayerCallback(std::list<t_callbackMsg>& callbacks)
  974. {
  975. for (std::list<t_callbackMsg>::iterator iter = callbacks.begin(); iter != callbacks.end(); iter++)
  976. {
  977. if (iter->nType == LAYERCALLBACK_STATECHANGE)
  978. {
  979. if (CAsyncSocketEx::LogStateChange(iter->nParam1, iter->nParam2))
  980. {
  981. const TCHAR * state2Desc = CAsyncSocketEx::GetStateDesc(iter->nParam2);
  982. const TCHAR * state1Desc = CAsyncSocketEx::GetStateDesc(iter->nParam1);
  983. if (iter->pLayer == m_pProxyLayer)
  984. LogMessage(__FILE__, __LINE__, this, FZ_LOG_INFO, _T("Proxy layer changed state from %s to %s"), state2Desc, state1Desc);
  985. #ifndef MPEXT_NO_SSL
  986. else if (iter->pLayer == m_pSslLayer)
  987. LogMessage(__FILE__, __LINE__, this, FZ_LOG_INFO, _T("TLS layer changed state from %s to %s"), state2Desc, state1Desc);
  988. #endif
  989. #ifndef MPEXT_NO_GSS
  990. else if (iter->pLayer == m_pGssLayer)
  991. LogMessage(__FILE__, __LINE__, this, FZ_LOG_INFO, _T("GSS layer changed state from %s to %s"), state2Desc, state1Desc);
  992. #endif
  993. else
  994. LogMessage(__FILE__, __LINE__, this, FZ_LOG_INFO, _T("Layer @ %d changed state from %s to %s"), iter->pLayer, state2Desc, state1Desc);
  995. }
  996. }
  997. else if (iter->nType == LAYERCALLBACK_LAYERSPECIFIC)
  998. {
  999. if (iter->pLayer == m_pProxyLayer)
  1000. {
  1001. switch (iter->nParam1)
  1002. {
  1003. case PROXYERROR_NOERROR:
  1004. m_pOwner->ShowStatus(IDS_PROXY_CONNECTED, FZ_LOG_STATUS);
  1005. break;
  1006. case PROXYERROR_NOCONN:
  1007. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_NOCONN, FZ_LOG_ERROR);
  1008. break;
  1009. case PROXYERROR_REQUESTFAILED:
  1010. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_REQUESTFAILED, FZ_LOG_ERROR);
  1011. break;
  1012. case PROXYERROR_AUTHTYPEUNKNOWN:
  1013. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_AUTHTYPEUNKNOWN, FZ_LOG_ERROR);
  1014. break;
  1015. case PROXYERROR_AUTHFAILED:
  1016. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_AUTHFAILED, FZ_LOG_ERROR);
  1017. break;
  1018. case PROXYERROR_AUTHNOLOGON:
  1019. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_AUTHNOLOGON, FZ_LOG_ERROR);
  1020. break;
  1021. case PROXYERROR_CANTRESOLVEHOST:
  1022. m_pOwner->ShowStatus(IDS_ERRORMSG_PROXY_CANTRESOLVEHOST, FZ_LOG_ERROR);
  1023. break;
  1024. default:
  1025. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Unknown proxy error"));
  1026. }
  1027. }
  1028. #ifndef MPEXT_NO_SSL
  1029. else if (iter->pLayer == m_pSslLayer)
  1030. {
  1031. switch (iter->nParam1)
  1032. {
  1033. case SSL_INFO:
  1034. switch(iter->nParam2)
  1035. {
  1036. case SSL_INFO_SHUTDOWNCOMPLETE:
  1037. CloseAndEnsureSendClose(0);
  1038. break;
  1039. case SSL_INFO_ESTABLISHED:
  1040. m_pOwner->ShowStatus(IDS_STATUSMSG_SSLESTABLISHEDTRANSFER, FZ_LOG_STATUS);
  1041. TriggerEvent(FD_FORCEREAD);
  1042. break;
  1043. }
  1044. break;
  1045. case SSL_FAILURE:
  1046. switch (iter->nParam2)
  1047. {
  1048. case SSL_FAILURE_ESTABLISH:
  1049. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTESTABLISHSSLCONNECTION, FZ_LOG_ERROR);
  1050. break;
  1051. case SSL_FAILURE_INITSSL:
  1052. m_pOwner->ShowStatus(IDS_ERRORMSG_CANTINITSSL, FZ_LOG_ERROR);
  1053. break;
  1054. }
  1055. EnsureSendClose(CSMODE_TRANSFERERROR);
  1056. break;
  1057. case SSL_VERIFY_CERT:
  1058. t_SslCertData data;
  1059. LPTSTR CertError = NULL;
  1060. if (m_pSslLayer->GetPeerCertificateData(data, CertError))
  1061. m_pSslLayer->SetNotifyReply(data.priv_data, SSL_VERIFY_CERT, 1);
  1062. else
  1063. {
  1064. CString str;
  1065. str.Format(TLS_CERT_DECODE_ERROR, CertError);
  1066. m_pOwner->ShowStatus(str, FZ_LOG_ERROR);
  1067. CloseAndEnsureSendClose(CSMODE_TRANSFERERROR);
  1068. }
  1069. break;
  1070. }
  1071. }
  1072. #endif
  1073. #ifndef MPEXT_NO_GSS
  1074. else if (iter->pLayer == m_pGssLayer)
  1075. {
  1076. USES_CONVERSION;
  1077. switch (iter->nParam1)
  1078. {
  1079. case GSS_INFO:
  1080. LogMessageRaw(FZ_LOG_INFO, A2CT(iter->str));
  1081. break;
  1082. case GSS_ERROR:
  1083. LogMessageRaw(FZ_LOG_APIERROR, A2CT(iter->str));
  1084. break;
  1085. case GSS_SHUTDOWN_COMPLETE:
  1086. CloseAndEnsureSendClose(0);
  1087. break;
  1088. }
  1089. }
  1090. #endif
  1091. }
  1092. delete [] iter->str;
  1093. }
  1094. return 0;
  1095. }
  1096. void CTransferSocket::Transfered(int count, CTime time)
  1097. {
  1098. CTimeSpan ts = time - m_TransferedFirst;
  1099. int diff = (int)ts.GetTotalSeconds();
  1100. if (diff < 0)
  1101. diff = 0;
  1102. if ( diff >= SPEED_SECONDS)
  1103. {
  1104. int move = diff - SPEED_SECONDS + 1;
  1105. int start = SPEED_SECONDS - move;
  1106. if ( start <= 0)
  1107. start = 0;
  1108. else
  1109. {
  1110. for ( int i = 0; i < SPEED_SECONDS - move; i++)
  1111. {
  1112. m_Transfered[ i] = m_Transfered[ i + move];
  1113. m_UsedForTransfer[ i] = m_UsedForTransfer[ i + move];
  1114. }
  1115. }
  1116. for ( int i = start; i < SPEED_SECONDS; i++)
  1117. {
  1118. m_Transfered[ i] = 0;
  1119. m_UsedForTransfer[ i] = false;
  1120. }
  1121. if (move >= SPEED_SECONDS)
  1122. {
  1123. m_TransferedFirst = time;
  1124. diff = 0;
  1125. }
  1126. else
  1127. {
  1128. m_TransferedFirst += CTimeSpan( move);
  1129. ts = time - m_TransferedFirst;
  1130. diff = (int)(ts.GetTotalSeconds() % 60);
  1131. }
  1132. }
  1133. m_Transfered[ diff] += count;
  1134. for ( int i = 0; i < diff - 1; i++)
  1135. m_UsedForTransfer[ i] = true;
  1136. }
  1137. #ifndef MPEXT_NO_GSS
  1138. void CTransferSocket::UseGSS(CAsyncGssSocketLayer *pGssLayer)
  1139. {
  1140. m_pGssLayer = new CAsyncGssSocketLayer;
  1141. m_pGssLayer->InitTransferChannel(pGssLayer);
  1142. }
  1143. #endif
  1144. #ifndef MPEXT_NO_ZLIB
  1145. bool CTransferSocket::InitZlib(int level)
  1146. {
  1147. int res;
  1148. if (m_nMode & CSMODE_UPLOAD)
  1149. res = deflateInit2(&m_zlibStream, level, Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
  1150. else
  1151. res = inflateInit2(&m_zlibStream, 15);
  1152. if (res == Z_OK)
  1153. m_useZlib = true;
  1154. return res == Z_OK;
  1155. }
  1156. #endif
  1157. int CTransferSocket::ReadDataFromFile(char *buffer, int len)
  1158. {
  1159. TRY
  1160. {
  1161. // Comparing to Filezilla 2, we do not do any translation locally,
  1162. // leaving it onto the server (what Filezilla 3 seems to do too)
  1163. const char Bom[3] = "\xEF\xBB\xBF";
  1164. int read = m_pFile->Read(buffer, len);
  1165. if (COptions::GetOptionVal(OPTION_MPEXT_REMOVE_BOM) &&
  1166. m_transferdata.bType && (read >= sizeof(Bom)) && (memcmp(buffer, Bom, sizeof(Bom)) == 0))
  1167. {
  1168. memcpy(buffer, buffer + sizeof(Bom), read - sizeof(Bom));
  1169. read -= sizeof(Bom);
  1170. int read2 = m_pFile->Read(buffer + read, sizeof(Bom));
  1171. if (read2 > 0)
  1172. {
  1173. read += read2;
  1174. }
  1175. }
  1176. return read;
  1177. }
  1178. CATCH_ALL(e)
  1179. {
  1180. TCHAR error[BUFSIZE];
  1181. if (e->GetErrorMessage(error, BUFSIZE))
  1182. m_pOwner->ShowStatus(error, FZ_LOG_ERROR);
  1183. CloseOnShutDownOrError(CSMODE_TRANSFERERROR);
  1184. return -1;
  1185. }
  1186. END_CATCH_ALL;
  1187. }
  1188. void CTransferSocket::LogSocketMessage(int nMessageType, LPCTSTR pMsgFormat)
  1189. {
  1190. LogMessage(nMessageType, pMsgFormat);
  1191. }
  1192. void CTransferSocket::EnsureSendClose(int Mode)
  1193. {
  1194. if (!m_bSentClose)
  1195. {
  1196. if (Mode != 0)
  1197. {
  1198. m_nMode |= Mode;
  1199. }
  1200. m_bSentClose = TRUE;
  1201. VERIFY(m_pOwner->m_pOwner->PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_TRANSFEREND, m_nMode));
  1202. }
  1203. }
  1204. void CTransferSocket::CloseAndEnsureSendClose(int Mode)
  1205. {
  1206. Close();
  1207. EnsureSendClose(Mode);
  1208. }
  1209. void CTransferSocket::CloseOnShutDownOrError(int Mode)
  1210. {
  1211. if (ShutDown())
  1212. {
  1213. CloseAndEnsureSendClose(Mode);
  1214. }
  1215. else
  1216. {
  1217. int Error = GetLastError();
  1218. if (Error != WSAEWOULDBLOCK)
  1219. {
  1220. // Log always or only when (Mode & CSMODE_TRANSFERERROR)?
  1221. // Does it anyway make sense at all to call this with Mode == 0?
  1222. LogError(Error);
  1223. CloseAndEnsureSendClose(Mode);
  1224. }
  1225. }
  1226. }
  1227. void CTransferSocket::LogError(int Error)
  1228. {
  1229. wchar_t * Buffer;
  1230. int Len = FormatMessage(
  1231. FORMAT_MESSAGE_FROM_SYSTEM |
  1232. FORMAT_MESSAGE_IGNORE_INSERTS |
  1233. FORMAT_MESSAGE_ARGUMENT_ARRAY |
  1234. FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, Error, 0, (LPTSTR)&Buffer, 0, NULL);
  1235. if (Len > 0)
  1236. {
  1237. m_pOwner->ShowStatus(Buffer, FZ_LOG_ERROR);
  1238. LocalFree(Buffer);
  1239. }
  1240. }