llxfermanager.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /**
  2. * @file llxfermanager.cpp
  3. * @brief implementation of LLXferManager class for a collection of xfers
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llxfermanager.h"
  34. #include "llstring.h"
  35. #include "llxfer.h"
  36. #include "llxfer_file.h"
  37. #include "llxfer_mem.h"
  38. #include "llxfer_vfile.h"
  39. // Timeout if a registered transfer has not been requested in 60 seconds:
  40. constexpr F32 LL_XFER_REGISTRATION_TIMEOUT = 60.f;
  41. constexpr F32 LL_PACKET_TIMEOUT = 3.f; // Packet timeout at 3s
  42. constexpr S32 LL_PACKET_RETRY_LIMIT = 10; // Packet retransmission limit
  43. constexpr S32 LL_DEFAULT_MAX_SIMULTANEOUS_XFERS = 10;
  44. constexpr S32 LL_DEFAULT_MAX_REQUEST_FIFO_XFERS = 1000;
  45. // Kills the connection if a viewer download queue hits this many requests
  46. // backed up:
  47. constexpr S32 LL_DEFAULT_MAX_HARD_LIMIT_SIMULTANEOUS_XFERS = 500;
  48. LLXferManager* gXferManagerp = NULL;
  49. LLXferManager::LLXferManager()
  50. {
  51. setMaxOutgoingXfersPerCircuit(LL_DEFAULT_MAX_SIMULTANEOUS_XFERS);
  52. setHardLimitOutgoingXfersPerCircuit(LL_DEFAULT_MAX_HARD_LIMIT_SIMULTANEOUS_XFERS);
  53. setMaxIncomingXfers(LL_DEFAULT_MAX_REQUEST_FIFO_XFERS);
  54. // Turn on or off ack throttling
  55. mUseAckThrottling = false;
  56. setAckThrottleBPS(100000);
  57. }
  58. LLXferManager::~LLXferManager()
  59. {
  60. for_each(mOutgoingHosts.begin(), mOutgoingHosts.end(), DeletePointer());
  61. mOutgoingHosts.clear();
  62. for_each(mSendList.begin(), mSendList.end(), DeletePointer());
  63. mSendList.clear();
  64. for_each(mReceiveList.begin(), mReceiveList.end(), DeletePointer());
  65. mReceiveList.clear();
  66. }
  67. void LLXferManager::setAckThrottleBPS(F32 bps)
  68. {
  69. // Let's figure out the min we can set based on the ack retry rate and
  70. // number of simultaneous.
  71. // Assuming we're running as slow as possible, this is the lowest ack
  72. // rate we can use.
  73. F32 min_bps = 8000.f * mMaxIncomingXfers / LL_PACKET_TIMEOUT;
  74. // Set
  75. F32 actual_rate = llmax(min_bps * 1.1f, bps);
  76. LL_DEBUGS("AppInit") << "LLXferManager ack throttle min rate: "
  77. << min_bps << " - actual rate: " << actual_rate
  78. << LL_ENDL;
  79. mAckThrottle.setRate(actual_rate);
  80. }
  81. void LLXferManager::updateHostStatus()
  82. {
  83. // Clear the outgoing host list
  84. for_each(mOutgoingHosts.begin(), mOutgoingHosts.end(), DeletePointer());
  85. mOutgoingHosts.clear();
  86. // Loop through all outgoing xfers and re-build mOutgoingHosts
  87. for (xfer_list_t::iterator send_iter = mSendList.begin(),
  88. send_end = mSendList.end();
  89. send_iter != send_end; ++send_iter)
  90. {
  91. const LLHost& remote_host = (*send_iter)->mRemoteHost;
  92. LLHostStatus* host_statusp = NULL;
  93. for (status_list_t::iterator iter = mOutgoingHosts.begin(),
  94. end = mOutgoingHosts.end();
  95. iter != end; ++iter)
  96. {
  97. if (*iter && (*iter)->mHost == remote_host)
  98. {
  99. host_statusp = *iter;
  100. break;
  101. }
  102. }
  103. if (!host_statusp)
  104. {
  105. // We do not have this host, so add it
  106. host_statusp = new LLHostStatus();
  107. if (host_statusp)
  108. {
  109. host_statusp->mHost = remote_host;
  110. mOutgoingHosts.push_front(host_statusp);
  111. }
  112. }
  113. if (host_statusp)
  114. {
  115. // Do the accounting
  116. if ((*send_iter)->mStatus == e_LL_XFER_PENDING)
  117. {
  118. ++host_statusp->mNumPending;
  119. }
  120. else if ((*send_iter)->mStatus == e_LL_XFER_IN_PROGRESS)
  121. {
  122. ++host_statusp->mNumActive;
  123. }
  124. }
  125. }
  126. }
  127. void LLXferManager::printHostStatus()
  128. {
  129. LLHostStatus* host_statusp = NULL;
  130. if (!mOutgoingHosts.empty())
  131. {
  132. llinfos << "Outgoing Xfers:" << llendl;
  133. for (status_list_t::iterator iter = mOutgoingHosts.begin(),
  134. end = mOutgoingHosts.end();
  135. iter != end; ++iter)
  136. {
  137. host_statusp = *iter;
  138. llinfos << " " << host_statusp->mHost << " - active: "
  139. << host_statusp->mNumActive << " - pending: "
  140. << host_statusp->mNumPending << llendl;
  141. }
  142. }
  143. }
  144. LLXfer* LLXferManager::findXferByID(U64 id, xfer_list_t& xfer_list)
  145. {
  146. for (xfer_list_t::iterator iter = xfer_list.begin(), end = xfer_list.end();
  147. iter != end; ++iter)
  148. {
  149. if ((*iter)->mID == id)
  150. {
  151. return *iter;
  152. }
  153. }
  154. return NULL;
  155. }
  156. // This method assumes that delp will only occur in the list zero or one time.
  157. void LLXferManager::removeXfer(LLXfer* delp, xfer_list_t& xfer_list)
  158. {
  159. if (!delp)
  160. {
  161. return;
  162. }
  163. bool receiving = &xfer_list == &mReceiveList;
  164. for (xfer_list_t::iterator iter = xfer_list.begin(), end = xfer_list.end();
  165. iter != end; ++iter)
  166. {
  167. if (*iter == delp)
  168. {
  169. LL_DEBUGS("FileTransfer") << "Deleting xfer to host "
  170. << (*iter)->mRemoteHost
  171. << " of " << (*iter)->mXferSize
  172. << " bytes, status "
  173. << (S32)(*iter)->mStatus << " from the "
  174. << (receiving ? "receive" : "send")
  175. << " list" << LL_ENDL;
  176. xfer_list.erase(iter);
  177. delete delp;
  178. break;
  179. }
  180. }
  181. }
  182. LLHostStatus* LLXferManager::findHostStatus(const LLHost& host)
  183. {
  184. for (status_list_t::iterator iter = mOutgoingHosts.begin(),
  185. end = mOutgoingHosts.end();
  186. iter != end; ++iter)
  187. {
  188. LLHostStatus* host_statusp = *iter;
  189. if (host_statusp && host_statusp->mHost == host)
  190. {
  191. return host_statusp;
  192. }
  193. }
  194. return NULL;
  195. }
  196. S32 LLXferManager::numPendingXfers(const LLHost& host)
  197. {
  198. LLHostStatus* host_statusp = findHostStatus(host);
  199. return host_statusp ? host_statusp->mNumPending : 0;
  200. }
  201. S32 LLXferManager::numActiveXfers(const LLHost& host)
  202. {
  203. LLHostStatus* host_statusp = findHostStatus(host);
  204. return host_statusp ? host_statusp->mNumActive : 0;
  205. }
  206. void LLXferManager::changeNumActiveXfers(const LLHost& host, S32 delta)
  207. {
  208. for (status_list_t::iterator iter = mOutgoingHosts.begin(),
  209. end = mOutgoingHosts.end();
  210. iter != end; ++iter)
  211. {
  212. LLHostStatus* host_statusp = *iter;
  213. if (host_statusp && host_statusp->mHost == host)
  214. {
  215. host_statusp->mNumActive += delta;
  216. }
  217. }
  218. }
  219. void LLXferManager::registerCallbacks(LLMessageSystem* msg)
  220. {
  221. msg->setHandlerFuncFast(_PREHASH_ConfirmXferPacket, process_confirm_packet,
  222. NULL);
  223. msg->setHandlerFuncFast(_PREHASH_RequestXfer, process_request_xfer, NULL);
  224. msg->setHandlerFuncFast(_PREHASH_SendXferPacket, continue_file_receive,
  225. NULL);
  226. msg->setHandlerFuncFast(_PREHASH_AbortXfer, process_abort_xfer, NULL);
  227. }
  228. U64 LLXferManager::getNextID()
  229. {
  230. LLUUID a_guid;
  231. a_guid.generate();
  232. return *((U64*)a_guid.mData);
  233. }
  234. U64 LLXferManager::requestFile(const std::string& local_filename,
  235. const std::string& remote_filename,
  236. ELLPath remote_path, const LLHost& remote_host,
  237. bool delete_remote_on_completion,
  238. void (*callback)(void**, S32, LLExtStat),
  239. void** user_data, bool is_priority,
  240. bool use_big_packets)
  241. {
  242. LLXfer_File* file_xferp = NULL;
  243. // First check to see if it's already requested
  244. for (xfer_list_t::iterator iter = mReceiveList.begin(),
  245. end = mReceiveList.end();
  246. iter != end; ++iter)
  247. {
  248. if (!*iter || (*iter)->getXferTypeTag() != LLXfer::XFER_FILE)
  249. {
  250. continue;
  251. }
  252. file_xferp = (LLXfer_File*)*iter;
  253. if (file_xferp->matchesLocalFilename(local_filename) &&
  254. file_xferp->matchesRemoteFilename(remote_filename, remote_path) &&
  255. remote_host == file_xferp->mRemoteHost &&
  256. callback == file_xferp->mCallback &&
  257. user_data == file_xferp->mCallbackDataHandle)
  258. {
  259. LL_DEBUGS("FileTransfer") << "Requested a xfer already in progress"
  260. << LL_ENDL;
  261. return file_xferp->mID;
  262. }
  263. }
  264. U64 xfer_id = 0;
  265. S32 chunk_size = use_big_packets ? LL_XFER_LARGE_PAYLOAD : -1;
  266. file_xferp = new LLXfer_File(chunk_size);
  267. if (file_xferp)
  268. {
  269. addToList(file_xferp, mReceiveList, is_priority);
  270. // Remove any file by the same name that happens to be lying around.
  271. // Note: according to AaronB, this is here to deal with locks on files
  272. // that were in transit during a crash.
  273. if (delete_remote_on_completion &&
  274. remote_filename.substr(remote_filename.length() - 4) == ".tmp" &&
  275. LLFile::exists(local_filename))
  276. {
  277. LLFile::remove(local_filename);
  278. }
  279. xfer_id = getNextID();
  280. file_xferp->initializeRequest(xfer_id, local_filename, remote_filename,
  281. remote_path, remote_host,
  282. delete_remote_on_completion, callback,
  283. user_data);
  284. startPendingDownloads();
  285. }
  286. else
  287. {
  288. llwarns << "LLXfer allocation error: out of memory ?" << llendl;
  289. llassert(false);
  290. }
  291. return xfer_id;
  292. }
  293. void LLXferManager::requestVFile(const LLUUID& local_id,
  294. const LLUUID& remote_id,
  295. LLAssetType::EType type,
  296. const LLHost& remote_host,
  297. void (*callback)(void**, S32, LLExtStat),
  298. void** user_data,
  299. bool is_priority)
  300. {
  301. LLXfer_VFile* xferp = NULL;
  302. for (xfer_list_t::iterator iter = mReceiveList.begin(),
  303. end = mReceiveList.end();
  304. iter != end; ++iter)
  305. {
  306. if (!*iter || (*iter)->getXferTypeTag() != LLXfer::XFER_VFILE)
  307. {
  308. continue;
  309. }
  310. xferp = (LLXfer_VFile*)*iter;
  311. // Find any matching existing requests
  312. if (xferp->matchesLocalFile(local_id, type) &&
  313. xferp->matchesRemoteFile(remote_id, type) &&
  314. remote_host == xferp->mRemoteHost &&
  315. callback == xferp->mCallback &&
  316. user_data == xferp->mCallbackDataHandle)
  317. {
  318. // We have match, so do not add a duplicate
  319. LL_DEBUGS("FileTransfer") << "Requested a xfer already in progress"
  320. << LL_ENDL;
  321. return;
  322. }
  323. }
  324. xferp = new LLXfer_VFile();
  325. if (xferp)
  326. {
  327. addToList(xferp, mReceiveList, is_priority);
  328. xferp->initializeRequest(getNextID(), local_id, remote_id, type,
  329. remote_host, callback, user_data);
  330. startPendingDownloads();
  331. }
  332. else
  333. {
  334. llwarns << "Xfer allocation error: out of memory ?" << llendl;
  335. llassert(false);
  336. }
  337. }
  338. void LLXferManager::processReceiveData(LLMessageSystem* msg, void**)
  339. {
  340. U64 id;
  341. msg->getU64Fast(_PREHASH_XferID, _PREHASH_ID, id);
  342. S32 packetnum;
  343. msg->getS32Fast(_PREHASH_XferID, _PREHASH_Packet, packetnum);
  344. // There is sometimes an extra 4 bytes added to an xfer payload
  345. constexpr S32 BUF_SIZE = LL_XFER_LARGE_PAYLOAD + 4;
  346. S32 fdata_size = msg->getSizeFast(_PREHASH_DataPacket, _PREHASH_Data);
  347. if (fdata_size < 0 || fdata_size > BUF_SIZE)
  348. {
  349. char U64_BUF[MAX_STRING];
  350. llwarns << "Received invalid xfer data size of " << fdata_size
  351. << " in packet number " << packetnum << " from "
  352. << msg->getSender() << " for xfer Id: "
  353. << U64_to_str(id, U64_BUF, sizeof(U64_BUF)) << llendl;
  354. return;
  355. }
  356. char fdata_buf[BUF_SIZE];
  357. msg->getBinaryDataFast(_PREHASH_DataPacket, _PREHASH_Data, fdata_buf, 0, 0,
  358. BUF_SIZE);
  359. LLXfer* xferp = findXferByID(id, mReceiveList);
  360. if (!xferp)
  361. {
  362. char U64_BUF[MAX_STRING];
  363. llwarns << "received xfer data from " << msg->getSender()
  364. << " for non-existent xfer id: "
  365. << U64_to_str(id, U64_BUF, sizeof(U64_BUF)) << llendl;
  366. return;
  367. }
  368. // Is the packet different from what we were expecting ?
  369. if (decodePacketNum(packetnum) != xferp->mPacketNum)
  370. {
  371. // Confirm it if it was a resend of the last one, since the
  372. // confirmation might have gotten dropped
  373. if (decodePacketNum(packetnum) == xferp->mPacketNum - 1)
  374. {
  375. llinfos << "Reconfirming xfer " << xferp->mRemoteHost << ":"
  376. << xferp->getFileName() << " packet " << packetnum
  377. << llendl;
  378. sendConfirmPacket(msg, id, decodePacketNum(packetnum),
  379. msg->getSender());
  380. }
  381. else
  382. {
  383. llinfos << "Ignoring xfer " << xferp->mRemoteHost << ":"
  384. << xferp->getFileName() << " received packet " << packetnum
  385. << "; expecting " << xferp->mPacketNum << llendl;
  386. }
  387. return;
  388. }
  389. S32 result = 0;
  390. // First packet has size encoded as additional S32 at beginning of data
  391. if (xferp->mPacketNum == 0)
  392. {
  393. S32 xfer_size;
  394. ntohmemcpy(&xfer_size, fdata_buf, MVT_S32, sizeof(S32));
  395. // Do any necessary things on first packet ie. allocate memory
  396. xferp->setXferSize(xfer_size);
  397. // Adjust buffer start and size
  398. result = xferp->receiveData(&fdata_buf[sizeof(S32)],
  399. fdata_size - sizeof(S32));
  400. }
  401. else
  402. {
  403. result = xferp->receiveData(fdata_buf, fdata_size);
  404. }
  405. if (result == LL_ERR_CANNOT_OPEN_FILE)
  406. {
  407. xferp->abort(LL_ERR_CANNOT_OPEN_FILE);
  408. removeXfer(xferp, mReceiveList);
  409. startPendingDownloads();
  410. return;
  411. }
  412. ++xferp->mPacketNum; // Expect next packet
  413. if (!mUseAckThrottling)
  414. {
  415. // No throttling, confirm right away
  416. sendConfirmPacket(msg, id, decodePacketNum(packetnum),
  417. msg->getSender());
  418. }
  419. else
  420. {
  421. // Throttling, put on queue to be confirmed later.
  422. LLXferAckInfo ack_info;
  423. ack_info.mID = id;
  424. ack_info.mPacketNum = decodePacketNum(packetnum);
  425. ack_info.mRemoteHost = msg->getSender();
  426. mXferAckQueue.push_back(ack_info);
  427. }
  428. if (isLastPacket(packetnum))
  429. {
  430. xferp->processEOF();
  431. removeXfer(xferp, mReceiveList);
  432. startPendingDownloads();
  433. }
  434. }
  435. void LLXferManager::sendConfirmPacket(LLMessageSystem* msg, U64 id,
  436. S32 packetnum, const LLHost& remote_host)
  437. {
  438. msg->newMessageFast(_PREHASH_ConfirmXferPacket);
  439. msg->nextBlockFast(_PREHASH_XferID);
  440. msg->addU64Fast(_PREHASH_ID, id);
  441. msg->addU32Fast(_PREHASH_Packet, packetnum);
  442. // Ignore a circuit failure here; we will catch it with another message.
  443. msg->sendMessage(remote_host);
  444. }
  445. static bool find_and_remove(std::multiset<std::string>& files,
  446. const std::string& filename)
  447. {
  448. std::multiset<std::string>::iterator ptr;
  449. if ((ptr = files.find(filename)) != files.end())
  450. {
  451. // erase(filename) erases *all* entries with that key
  452. files.erase(ptr);
  453. return true;
  454. }
  455. return false;
  456. }
  457. void LLXferManager::expectFileForRequest(const std::string& filename)
  458. {
  459. mExpectedRequests.emplace(filename);
  460. }
  461. bool LLXferManager::validateFileForRequest(const std::string& filename)
  462. {
  463. return find_and_remove(mExpectedRequests, filename);
  464. }
  465. void LLXferManager::expectFileForTransfer(const std::string& filename)
  466. {
  467. mExpectedTransfers.emplace(filename);
  468. }
  469. bool LLXferManager::validateFileForTransfer(const std::string& filename)
  470. {
  471. return find_and_remove(mExpectedTransfers, filename);
  472. }
  473. static bool remove_prefix(std::string& filename, const std::string& prefix)
  474. {
  475. if (std::equal(prefix.begin(), prefix.end(), filename.begin()))
  476. {
  477. filename = filename.substr(prefix.length());
  478. return true;
  479. }
  480. return false;
  481. }
  482. // NOTE: This function is only used to check file names that our own code
  483. // places in the cache directory. As such, it can be limited to this very
  484. // restrictive file name pattern. It does not need to handle other characters.
  485. static bool verify_cache_filename(const std::string& filename)
  486. {
  487. size_t len = filename.size();
  488. if (len < 1 || len > 50)
  489. {
  490. return false;
  491. }
  492. for (U32 i = 0; i < len; ++i)
  493. {
  494. char c = filename[i];
  495. bool ok = isalnum(c);
  496. if (!ok && i > 0)
  497. {
  498. ok = '_' == c || '-' == c || '.' == c;
  499. }
  500. if (!ok)
  501. {
  502. return false;
  503. }
  504. }
  505. return true;
  506. }
  507. void LLXferManager::processFileRequest(LLMessageSystem* msg, void**)
  508. {
  509. S32 result = LL_ERR_NOERR;
  510. bool use_big_pkts;
  511. msg->getBool("XferID", "UseBigPackets", use_big_pkts);
  512. U64 id;
  513. msg->getU64Fast(_PREHASH_XferID, _PREHASH_ID, id);
  514. char U64_BUF[MAX_STRING];
  515. llinfos << "xfer request id: " << U64_to_str(id, U64_BUF, sizeof(U64_BUF))
  516. << " to " << msg->getSender() << llendl;
  517. std::string local_filename;
  518. msg->getStringFast(_PREHASH_XferID, _PREHASH_Filename, local_filename);
  519. U8 local_path_u8;
  520. msg->getU8("XferID", "FilePath", local_path_u8);
  521. ELLPath local_path = (ELLPath)local_path_u8;
  522. LLUUID uuid;
  523. msg->getUUIDFast(_PREHASH_XferID, _PREHASH_VFileID, uuid);
  524. S16 type_s16;
  525. msg->getS16Fast(_PREHASH_XferID, _PREHASH_VFileType, type_s16);
  526. LLAssetType::EType type = (LLAssetType::EType)type_s16;
  527. LLXfer* xferp;
  528. if (uuid.notNull())
  529. {
  530. // Request for an asset: use a cache file
  531. if (!LLAssetType::lookup(type))
  532. {
  533. llwarns << "Invalid type for xfer request: " << uuid << ":"
  534. << type_s16 << " to " << msg->getSender() << llendl;
  535. return;
  536. }
  537. llinfos << "starting vfile transfer: " << uuid << ","
  538. << LLAssetType::lookup(type) << " to " << msg->getSender()
  539. << llendl;
  540. xferp = (LLXfer*)new LLXfer_VFile(uuid, type);
  541. if (xferp)
  542. {
  543. mSendList.push_front(xferp);
  544. result = xferp->startSend(id, msg->getSender());
  545. }
  546. else
  547. {
  548. llwarns << "Xfer allocation error: out of memory ?" << llendl;
  549. llassert(false);
  550. }
  551. }
  552. else if (!local_filename.empty())
  553. {
  554. // Was given a file name to send. See DEV-21775 for detailed security
  555. // issues
  556. if (local_path == LL_PATH_NONE)
  557. {
  558. // This handles legacy simulators that are passing objects by
  559. // giving a filename that explicitly names the cache directory
  560. static const std::string legacy_cache_prefix = "data/";
  561. if (remove_prefix(local_filename, legacy_cache_prefix))
  562. {
  563. local_path = LL_PATH_CACHE;
  564. }
  565. }
  566. switch (local_path)
  567. {
  568. case LL_PATH_NONE:
  569. if (!validateFileForTransfer(local_filename))
  570. {
  571. llwarns << "SECURITY: Unapproved filename '"
  572. << local_filename << llendl;
  573. return;
  574. }
  575. break;
  576. case LL_PATH_CACHE:
  577. if (!verify_cache_filename(local_filename))
  578. {
  579. llwarns << "SECURITY: Illegal cache filename '"
  580. << local_filename << llendl;
  581. return;
  582. }
  583. break;
  584. default:
  585. llwarns << "SECURITY: Restricted file dir enum: "
  586. << (U32)local_path << llendl;
  587. return;
  588. }
  589. // If we want to use a special path (e.g. LL_PATH_CACHE), we want to
  590. // make sure we create the proper expanded filename.
  591. std::string expanded_filename;
  592. if (local_path != LL_PATH_NONE)
  593. {
  594. expanded_filename = gDirUtil.getFullPath(local_path,
  595. local_filename);
  596. }
  597. else
  598. {
  599. expanded_filename = local_filename;
  600. }
  601. llinfos << "starting file transfer: " << expanded_filename << " to "
  602. << msg->getSender() << llendl;
  603. bool delete_local_on_completion = false;
  604. msg->getBool("XferID", "DeleteOnCompletion",
  605. delete_local_on_completion);
  606. // -1 chunk_size causes it to use the default
  607. xferp = (LLXfer*)new LLXfer_File(expanded_filename,
  608. delete_local_on_completion,
  609. use_big_pkts ? LL_XFER_LARGE_PAYLOAD
  610. : -1);
  611. if (xferp)
  612. {
  613. mSendList.push_front(xferp);
  614. result = xferp->startSend(id, msg->getSender());
  615. }
  616. else
  617. {
  618. llwarns << "Xfer allocation error: out of memory ?" << llendl;
  619. llassert(false);
  620. }
  621. }
  622. else
  623. {
  624. // No UUID or filename; use the Id sent.
  625. char U64_BUF[MAX_STRING];
  626. llinfos << "Starting memory transfer: "
  627. << U64_to_str(id, U64_BUF, sizeof(U64_BUF)) << " to "
  628. << msg->getSender() << llendl;
  629. xferp = findXferByID(id, mSendList);
  630. if (xferp)
  631. {
  632. result = xferp->startSend(id, msg->getSender());
  633. }
  634. else
  635. {
  636. llwarns << U64_BUF << " not found." << llendl;
  637. result = LL_ERR_FILE_NOT_FOUND;
  638. }
  639. }
  640. if (result)
  641. {
  642. if (xferp)
  643. {
  644. xferp->abort(result);
  645. removeXfer(xferp, mSendList);
  646. }
  647. else // Can happen with a memory transfer not found
  648. {
  649. llinfos << "Aborting xfer to " << msg->getSender()
  650. << " with error: " << result << llendl;
  651. msg->newMessageFast(_PREHASH_AbortXfer);
  652. msg->nextBlockFast(_PREHASH_XferID);
  653. msg->addU64Fast(_PREHASH_ID, id);
  654. msg->addS32Fast(_PREHASH_Result, result);
  655. msg->sendMessage(msg->getSender());
  656. }
  657. }
  658. else if (xferp)
  659. {
  660. // Figure out how many transfers the host has requested
  661. LLHostStatus* host_statusp = findHostStatus(xferp->mRemoteHost);
  662. if (host_statusp)
  663. {
  664. if (host_statusp->mNumActive < mMaxOutgoingXfersPerCircuit)
  665. {
  666. // Not many transfers in progress already, so start immediately
  667. xferp->sendNextPacket();
  668. changeNumActiveXfers(xferp->mRemoteHost, 1);
  669. LL_DEBUGS("FileTransfer") << "Starting xfer immediately"
  670. << LL_ENDL;
  671. }
  672. else if (!mHardLimitOutgoingXfersPerCircuit ||
  673. host_statusp->mNumActive + host_statusp->mNumPending <
  674. mHardLimitOutgoingXfersPerCircuit)
  675. {
  676. // Must close the file handle and wait for earlier ones to
  677. // complete
  678. llinfos << "Queueing xfer request Id " << U64_to_str(id)
  679. << ", " << host_statusp->mNumActive << " active and "
  680. << host_statusp->mNumPending
  681. << " pending ahead of this one" << llendl;
  682. xferp->closeFileHandle();
  683. }
  684. else if (mHardLimitOutgoingXfersPerCircuit > 0)
  685. {
  686. // Way too many requested ... It is time to stop being nice and
  687. // kill the circuit.
  688. xferp->closeFileHandle();
  689. LLCircuitData* cdp =
  690. msg->mCircuitInfo.findCircuit(xferp->mRemoteHost);
  691. if (cdp && cdp->getTrusted())
  692. {
  693. // Trusted internal circuit: do not kill it
  694. llwarns << "Trusted circuit to " << xferp->mRemoteHost
  695. << " has too many xfer requests in the queue: "
  696. << host_statusp->mNumActive << " active and "
  697. << host_statusp->mNumPending
  698. << " pending ahead of this one" << llendl;
  699. }
  700. else
  701. {
  702. llwarns << "Killing " << (cdp ? "active" : "missing (!)")
  703. << " circuit to " << xferp->mRemoteHost
  704. << " for having too many xfer requests queued: "
  705. << host_statusp->mNumActive << " active and "
  706. << host_statusp->mNumPending
  707. << " pending ahead of this one" << llendl;
  708. msg->disableCircuit(xferp->mRemoteHost);
  709. }
  710. }
  711. }
  712. else
  713. {
  714. llwarns << "No LLHostStatus found for Id " << U64_to_str(id)
  715. << " and host " << xferp->mRemoteHost << llendl;
  716. }
  717. }
  718. else
  719. {
  720. llwarns << "No xfer found for Id " << U64_to_str(id) << llendl;
  721. }
  722. }
  723. // Returns true if host is in a transfer-flood sitation. Same check for both
  724. // internal and external hosts.
  725. bool LLXferManager::isHostFlooded(const LLHost& host)
  726. {
  727. LLHostStatus* host_statusp = findHostStatus(host);
  728. return host_statusp && mHardLimitOutgoingXfersPerCircuit > 0 &&
  729. host_statusp->mNumActive + host_statusp->mNumPending >=
  730. 80 * mHardLimitOutgoingXfersPerCircuit / 100;
  731. }
  732. void LLXferManager::processConfirmation(LLMessageSystem* msg, void**)
  733. {
  734. U64 id = 0;
  735. S32 packetNum = 0;
  736. msg->getU64Fast(_PREHASH_XferID, _PREHASH_ID, id);
  737. msg->getS32Fast(_PREHASH_XferID, _PREHASH_Packet, packetNum);
  738. LLXfer* xferp = findXferByID(id, mSendList);
  739. if (xferp)
  740. {
  741. xferp->mWaitingForACK = false;
  742. if (xferp->mStatus == e_LL_XFER_IN_PROGRESS)
  743. {
  744. xferp->sendNextPacket();
  745. }
  746. else
  747. {
  748. removeXfer(xferp, mSendList);
  749. }
  750. }
  751. }
  752. void LLXferManager::retransmitUnackedPackets()
  753. {
  754. LLXfer* xferp;
  755. xfer_list_t::iterator iter = mReceiveList.begin();
  756. while (iter != mReceiveList.end())
  757. {
  758. xferp = *iter;
  759. if (!xferp) // Paranoia
  760. {
  761. iter = mReceiveList.erase(iter);
  762. continue;
  763. }
  764. if (xferp->mStatus == e_LL_XFER_IN_PROGRESS &&
  765. // If the circuit dies, abort
  766. !gMessageSystemp->mCircuitInfo.isCircuitAlive(xferp->mRemoteHost))
  767. {
  768. llwarns << "Xfer found in progress on dead circuit, aborting"
  769. << llendl;
  770. xferp->mCallbackResult = LL_ERR_CIRCUIT_GONE;
  771. xferp->processEOF();
  772. iter = mReceiveList.erase(iter);
  773. delete xferp;
  774. continue;
  775. }
  776. ++iter;
  777. }
  778. updateHostStatus();
  779. iter = mSendList.begin();
  780. while (iter != mSendList.end())
  781. {
  782. xferp = *iter;
  783. if (!xferp) // Paranoia
  784. {
  785. iter = mSendList.erase(iter);
  786. continue;
  787. }
  788. if (xferp->mWaitingForACK &&
  789. xferp->ACKTimer.getElapsedTimeF32() > LL_PACKET_TIMEOUT)
  790. {
  791. if (xferp->mRetries > LL_PACKET_RETRY_LIMIT)
  792. {
  793. llinfos << "Dropping xfer " << xferp->mRemoteHost << ":"
  794. << xferp->getFileName()
  795. << " packet retransmit limit exceeded, xfer dropped"
  796. << llendl;
  797. xferp->abort(LL_ERR_TCP_TIMEOUT);
  798. iter = mSendList.erase(iter);
  799. delete xferp;
  800. continue;
  801. }
  802. llinfos << "Resending xfer " << xferp->mRemoteHost << ":"
  803. << xferp->getFileName()
  804. << " packet unconfirmed after " << LL_PACKET_TIMEOUT
  805. << " seconds, packet: " << xferp->mPacketNum << llendl;
  806. xferp->resendLastPacket();
  807. }
  808. else if (xferp->mStatus == e_LL_XFER_REGISTERED &&
  809. xferp->ACKTimer.getElapsedTimeF32() > LL_XFER_REGISTRATION_TIMEOUT)
  810. {
  811. llinfos << "Registered xfer never requested, xfer dropped"
  812. << llendl;
  813. xferp->abort(LL_ERR_TCP_TIMEOUT);
  814. iter = mSendList.erase(iter);
  815. delete xferp;
  816. continue;
  817. }
  818. else if (xferp->mStatus == e_LL_XFER_ABORTED)
  819. {
  820. llwarns << "Removing aborted xfer " << xferp->mRemoteHost << ":"
  821. << xferp->getFileName() << llendl;
  822. iter = mSendList.erase(iter);
  823. delete xferp;
  824. continue;
  825. }
  826. else if (xferp->mStatus == e_LL_XFER_PENDING)
  827. {
  828. LL_DEBUGS("FileTransfer") << "numActiveXfers = "
  829. << numActiveXfers(xferp->mRemoteHost)
  830. << " - mMaxOutgoingXfersPerCircuit = "
  831. << mMaxOutgoingXfersPerCircuit
  832. << LL_ENDL;
  833. if (numActiveXfers(xferp->mRemoteHost) <
  834. mMaxOutgoingXfersPerCircuit)
  835. {
  836. if (xferp->reopenFileHandle())
  837. {
  838. llwarns << "Removing failed xfer to " << xferp->mRemoteHost
  839. << " for Id " << U64_to_str(xferp->mID) << llendl;
  840. xferp->abort(LL_ERR_CANNOT_OPEN_FILE);
  841. iter = mSendList.erase(iter);
  842. delete xferp;
  843. continue;
  844. }
  845. LL_DEBUGS("FileTransfer") << "Moving pending xfer ID "
  846. << U64_to_str(xferp->mID)
  847. << " to active" << LL_ENDL;
  848. xferp->sendNextPacket();
  849. changeNumActiveXfers(xferp->mRemoteHost, 1);
  850. }
  851. }
  852. ++iter;
  853. }
  854. // *HACK: if we are using xfer confirm throttling, throttle our xfer
  855. // confirms here so we don't blow through bandwidth.
  856. while (mXferAckQueue.size())
  857. {
  858. if (mAckThrottle.checkOverflow(8000.0f))
  859. {
  860. break;
  861. }
  862. LL_DEBUGS("FileTransfer") << "Confirm packet queue length:"
  863. << mXferAckQueue.size() << LL_ENDL;
  864. LLXferAckInfo ack_info = mXferAckQueue.front();
  865. mXferAckQueue.pop_front();
  866. sendConfirmPacket(gMessageSystemp, ack_info.mID, ack_info.mPacketNum,
  867. ack_info.mRemoteHost);
  868. mAckThrottle.throttleOverflow(8000.f); // Assume 1000 bytes/packet
  869. }
  870. }
  871. void LLXferManager::abortRequestById(U64 xfer_id, S32 result_code)
  872. {
  873. LLXfer* xferp = findXferByID(xfer_id, mReceiveList);
  874. if (xferp)
  875. {
  876. if (xferp->mStatus == e_LL_XFER_IN_PROGRESS)
  877. {
  878. // Causes processAbort();
  879. xferp->abort(result_code);
  880. }
  881. else
  882. {
  883. xferp->mCallbackResult = result_code;
  884. xferp->processEOF(); // Should notify the requester
  885. removeXfer(xferp, mReceiveList);
  886. }
  887. // Since already removed or marked as aborted no need to wait for
  888. // processAbort() to start new download
  889. startPendingDownloads();
  890. }
  891. }
  892. void LLXferManager::processAbort(LLMessageSystem* msg, void**)
  893. {
  894. U64 id = 0;
  895. msg->getU64Fast(_PREHASH_XferID, _PREHASH_ID, id);
  896. S32 result_code = 0;
  897. msg->getS32Fast(_PREHASH_XferID, _PREHASH_Result, result_code);
  898. LLXfer* xferp = findXferByID(id, mReceiveList);
  899. if (xferp)
  900. {
  901. xferp->mCallbackResult = result_code;
  902. xferp->processEOF();
  903. removeXfer(xferp, mReceiveList);
  904. startPendingDownloads();
  905. }
  906. }
  907. // This method goes through the list, and starts pending operations until
  908. // active downloads == mMaxIncomingXfers.
  909. void LLXferManager::startPendingDownloads()
  910. {
  911. // We copy the pending xfers into a temporary data structure because the
  912. // xfers are stored as an intrusive linked list where older requests get
  913. // pushed toward the back. Thus, if we did not do a stateful iteration, it
  914. // would be possible for old requests to never start.
  915. std::list<LLXfer*> pending_downloads;
  916. S32 download_count = 0;
  917. S32 pending_count = 0;
  918. LLXfer* xferp;
  919. for (xfer_list_t::iterator iter = mReceiveList.begin(),
  920. end = mReceiveList.end();
  921. iter != end; ++iter)
  922. {
  923. xferp = *iter;
  924. if (!xferp) continue; // Paranoia
  925. if (xferp->mStatus == e_LL_XFER_PENDING)
  926. {
  927. ++pending_count;
  928. pending_downloads.push_front(xferp);
  929. }
  930. else if (xferp->mStatus == e_LL_XFER_IN_PROGRESS)
  931. {
  932. ++download_count;
  933. }
  934. }
  935. S32 start_count = mMaxIncomingXfers - download_count;
  936. LL_DEBUGS("FileTransfer") << "Xfer in progress: " << download_count
  937. << " - xfer pending: " << pending_count
  938. << " - Starting: "
  939. << llmin(start_count, pending_count)
  940. << LL_ENDL;
  941. if (start_count > 0 && pending_count > 0)
  942. {
  943. S32 result;
  944. for (std::list<LLXfer*>::iterator iter = pending_downloads.begin(),
  945. end = pending_downloads.end();
  946. iter != end; ++iter)
  947. {
  948. xferp = *iter;
  949. if (start_count-- <= 0)
  950. {
  951. break;
  952. }
  953. result = xferp->startDownload();
  954. if (result)
  955. {
  956. xferp->abort(result);
  957. ++start_count;
  958. }
  959. }
  960. }
  961. }
  962. void LLXferManager::addToList(LLXfer* xferp, xfer_list_t& xfer_list,
  963. bool is_priority)
  964. {
  965. if (xferp)
  966. {
  967. if (is_priority)
  968. {
  969. xfer_list.push_back(xferp);
  970. }
  971. else
  972. {
  973. xfer_list.push_front(xferp);
  974. }
  975. }
  976. else
  977. {
  978. llwarns << "Tried to add a NULL LLXfer !" << llendl;
  979. llassert(false);
  980. }
  981. }
  982. ///////////////////////////////////////////////////////////
  983. // C routines
  984. ///////////////////////////////////////////////////////////
  985. void process_confirm_packet(LLMessageSystem* msg, void** user_data)
  986. {
  987. if (gXferManagerp)
  988. {
  989. gXferManagerp->processConfirmation(msg, user_data);
  990. }
  991. }
  992. void process_request_xfer(LLMessageSystem* msg, void** user_data)
  993. {
  994. if (gXferManagerp)
  995. {
  996. gXferManagerp->processFileRequest(msg, user_data);
  997. }
  998. }
  999. void continue_file_receive(LLMessageSystem* msg, void** user_data)
  1000. {
  1001. if (gXferManagerp)
  1002. {
  1003. gXferManagerp->processReceiveData(msg, user_data);
  1004. }
  1005. }
  1006. void process_abort_xfer(LLMessageSystem* msg, void** user_data)
  1007. {
  1008. if (gXferManagerp)
  1009. {
  1010. gXferManagerp->processAbort(msg, user_data);
  1011. }
  1012. }