PageRenderTime 31ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmessage/message.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 2414 lines | 1773 code | 315 blank | 326 comment | 219 complexity | 8400c111b21e6920cc8d92b43a44cd55 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file message.cpp
  3. * @brief LLMessageSystem class implementation
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "message.h"
  28. // system library includes
  29. #if !LL_WINDOWS
  30. // following header files required for inet_addr()
  31. #include <sys/types.h>
  32. #include <sys/socket.h>
  33. #include <netinet/in.h>
  34. #include <arpa/inet.h>
  35. #endif
  36. #include <iomanip>
  37. #include <iterator>
  38. #include <sstream>
  39. #include "llapr.h"
  40. #include "apr_portable.h"
  41. #include "apr_network_io.h"
  42. #include "apr_poll.h"
  43. // linden library headers
  44. #include "indra_constants.h"
  45. #include "lldarray.h"
  46. #include "lldir.h"
  47. #include "llerror.h"
  48. #include "llerrorlegacy.h"
  49. #include "llfasttimer.h"
  50. #include "llhttpclient.h"
  51. #include "llhttpnodeadapter.h"
  52. #include "llhttpsender.h"
  53. #include "llmd5.h"
  54. #include "llmessagebuilder.h"
  55. #include "llmessageconfig.h"
  56. #include "lltemplatemessagedispatcher.h"
  57. #include "llpumpio.h"
  58. #include "lltemplatemessagebuilder.h"
  59. #include "lltemplatemessagereader.h"
  60. #include "lltrustedmessageservice.h"
  61. #include "llmessagetemplate.h"
  62. #include "llmessagetemplateparser.h"
  63. #include "llsd.h"
  64. #include "llsdmessagebuilder.h"
  65. #include "llsdmessagereader.h"
  66. #include "llsdserialize.h"
  67. #include "llstring.h"
  68. #include "lltransfermanager.h"
  69. #include "lluuid.h"
  70. #include "llxfermanager.h"
  71. #include "timing.h"
  72. #include "llquaternion.h"
  73. #include "u64.h"
  74. #include "v3dmath.h"
  75. #include "v3math.h"
  76. #include "v4math.h"
  77. #include "lltransfertargetvfile.h"
  78. #include "llmemtype.h"
  79. // Constants
  80. //const char* MESSAGE_LOG_FILENAME = "message.log";
  81. static const F32 CIRCUIT_DUMP_TIMEOUT = 30.f;
  82. static const S32 TRUST_TIME_WINDOW = 3;
  83. // *NOTE: This needs to be moved into a seperate file so that it never gets
  84. // included in the viewer. 30 Sep 2002 mark
  85. // *NOTE: I don't think it's important that the messgage system tracks
  86. // this since it must get set externally. 2004.08.25 Phoenix.
  87. static std::string g_shared_secret;
  88. std::string get_shared_secret();
  89. class LLMessagePollInfo
  90. {
  91. public:
  92. apr_socket_t *mAPRSocketp;
  93. apr_pollfd_t mPollFD;
  94. };
  95. namespace
  96. {
  97. class LLFnPtrResponder : public LLHTTPClient::Responder
  98. {
  99. LOG_CLASS(LLFnPtrResponder);
  100. public:
  101. LLFnPtrResponder(void (*callback)(void **,S32), void **callbackData, const std::string& name) :
  102. mCallback(callback),
  103. mCallbackData(callbackData),
  104. mMessageName(name)
  105. {
  106. }
  107. virtual void error(U32 status, const std::string& reason)
  108. {
  109. // don't spam when agent communication disconnected already
  110. if (status != 410)
  111. {
  112. LL_WARNS("Messaging") << "error status " << status
  113. << " for message " << mMessageName
  114. << " reason " << reason << llendl;
  115. }
  116. // TODO: Map status in to useful error code.
  117. if(NULL != mCallback) mCallback(mCallbackData, LL_ERR_TCP_TIMEOUT);
  118. }
  119. virtual void result(const LLSD& content)
  120. {
  121. if(NULL != mCallback) mCallback(mCallbackData, LL_ERR_NOERR);
  122. }
  123. private:
  124. void (*mCallback)(void **,S32);
  125. void **mCallbackData;
  126. std::string mMessageName;
  127. };
  128. }
  129. class LLMessageHandlerBridge : public LLHTTPNode
  130. {
  131. virtual bool validate(const std::string& name, LLSD& context) const
  132. { return true; }
  133. virtual void post(LLHTTPNode::ResponsePtr response, const LLSD& context,
  134. const LLSD& input) const;
  135. };
  136. //virtual
  137. void LLMessageHandlerBridge::post(LLHTTPNode::ResponsePtr response,
  138. const LLSD& context, const LLSD& input) const
  139. {
  140. std::string name = context["request"]["wildcard"]["message-name"];
  141. char* namePtr = LLMessageStringTable::getInstance()->getString(name.c_str());
  142. lldebugs << "Setting mLastSender " << input["sender"].asString() << llendl;
  143. gMessageSystem->mLastSender = LLHost(input["sender"].asString());
  144. gMessageSystem->mPacketsIn += 1;
  145. gMessageSystem->mLLSDMessageReader->setMessage(namePtr, input["body"]);
  146. gMessageSystem->mMessageReader = gMessageSystem->mLLSDMessageReader;
  147. if(gMessageSystem->callHandler(namePtr, false, gMessageSystem))
  148. {
  149. response->result(LLSD());
  150. }
  151. else
  152. {
  153. response->notFound();
  154. }
  155. }
  156. LLHTTPRegistration<LLMessageHandlerBridge>
  157. gHTTPRegistrationMessageWildcard("/message/<message-name>");
  158. //virtual
  159. LLUseCircuitCodeResponder::~LLUseCircuitCodeResponder()
  160. {
  161. // even abstract base classes need a concrete destructor
  162. }
  163. static const char* nullToEmpty(const char* s)
  164. {
  165. static char emptyString[] = "";
  166. return s? s : emptyString;
  167. }
  168. void LLMessageSystem::init()
  169. {
  170. // initialize member variables
  171. mVerboseLog = FALSE;
  172. mbError = FALSE;
  173. mErrorCode = 0;
  174. mSendReliable = FALSE;
  175. mUnackedListDepth = 0;
  176. mUnackedListSize = 0;
  177. mDSMaxListDepth = 0;
  178. mNumberHighFreqMessages = 0;
  179. mNumberMediumFreqMessages = 0;
  180. mNumberLowFreqMessages = 0;
  181. mPacketsIn = mPacketsOut = 0;
  182. mBytesIn = mBytesOut = 0;
  183. mCompressedPacketsIn = mCompressedPacketsOut = 0;
  184. mReliablePacketsIn = mReliablePacketsOut = 0;
  185. mCompressedBytesIn = 0;
  186. mCompressedBytesOut = 0;
  187. mUncompressedBytesIn = 0;
  188. mUncompressedBytesOut = 0;
  189. mTotalBytesIn = 0;
  190. mTotalBytesOut = 0;
  191. mDroppedPackets = 0; // total dropped packets in
  192. mResentPackets = 0; // total resent packets out
  193. mFailedResendPackets = 0; // total resend failure packets out
  194. mOffCircuitPackets = 0; // total # of off-circuit packets rejected
  195. mInvalidOnCircuitPackets = 0; // total # of on-circuit packets rejected
  196. mOurCircuitCode = 0;
  197. mIncomingCompressedSize = 0;
  198. mCurrentRecvPacketID = 0;
  199. mMessageFileVersionNumber = 0.f;
  200. mTimingCallback = NULL;
  201. mTimingCallbackData = NULL;
  202. mMessageBuilder = NULL;
  203. mMessageReader = NULL;
  204. }
  205. // Read file and build message templates
  206. LLMessageSystem::LLMessageSystem(const std::string& filename, U32 port,
  207. S32 version_major,
  208. S32 version_minor,
  209. S32 version_patch,
  210. bool failure_is_fatal,
  211. const F32 circuit_heartbeat_interval, const F32 circuit_timeout) :
  212. mCircuitInfo(circuit_heartbeat_interval, circuit_timeout),
  213. mLastMessageFromTrustedMessageService(false)
  214. {
  215. init();
  216. mSendSize = 0;
  217. mSystemVersionMajor = version_major;
  218. mSystemVersionMinor = version_minor;
  219. mSystemVersionPatch = version_patch;
  220. mSystemVersionServer = 0;
  221. mVersionFlags = 0x0;
  222. // default to not accepting packets from not alive circuits
  223. mbProtected = TRUE;
  224. // default to blocking trusted connections on a public interface if one is specified
  225. mBlockUntrustedInterface = true;
  226. mSendPacketFailureCount = 0;
  227. mCircuitPrintFreq = 60.f; // seconds
  228. loadTemplateFile(filename, failure_is_fatal);
  229. mTemplateMessageBuilder = new LLTemplateMessageBuilder(mMessageTemplates);
  230. mLLSDMessageBuilder = new LLSDMessageBuilder();
  231. mMessageBuilder = NULL;
  232. mTemplateMessageReader = new LLTemplateMessageReader(mMessageNumbers);
  233. mLLSDMessageReader = new LLSDMessageReader();
  234. mMessageReader = NULL;
  235. // initialize various bits of net info
  236. mSocket = 0;
  237. mPort = port;
  238. S32 error = start_net(mSocket, mPort);
  239. if (error != 0)
  240. {
  241. mbError = TRUE;
  242. mErrorCode = error;
  243. }
  244. // LL_DEBUGS("Messaging") << << "*** port: " << mPort << llendl;
  245. //
  246. // Create the data structure that we can poll on
  247. //
  248. if (!gAPRPoolp)
  249. {
  250. LL_ERRS("Messaging") << "No APR pool before message system initialization!" << llendl;
  251. ll_init_apr();
  252. }
  253. apr_socket_t *aprSocketp = NULL;
  254. apr_os_sock_put(&aprSocketp, (apr_os_sock_t*)&mSocket, gAPRPoolp);
  255. mPollInfop = new LLMessagePollInfo;
  256. mPollInfop->mAPRSocketp = aprSocketp;
  257. mPollInfop->mPollFD.p = gAPRPoolp;
  258. mPollInfop->mPollFD.desc_type = APR_POLL_SOCKET;
  259. mPollInfop->mPollFD.reqevents = APR_POLLIN;
  260. mPollInfop->mPollFD.rtnevents = 0;
  261. mPollInfop->mPollFD.desc.s = aprSocketp;
  262. mPollInfop->mPollFD.client_data = NULL;
  263. F64 mt_sec = getMessageTimeSeconds();
  264. mResendDumpTime = mt_sec;
  265. mMessageCountTime = mt_sec;
  266. mCircuitPrintTime = mt_sec;
  267. mCurrentMessageTimeSeconds = mt_sec;
  268. // Constants for dumping output based on message processing time/count
  269. mNumMessageCounts = 0;
  270. mMaxMessageCounts = 200; // >= 0 means dump warnings
  271. mMaxMessageTime = 1.f;
  272. mTrueReceiveSize = 0;
  273. mReceiveTime = 0.f;
  274. }
  275. // Read file and build message templates
  276. void LLMessageSystem::loadTemplateFile(const std::string& filename, bool failure_is_fatal)
  277. {
  278. if(filename.empty())
  279. {
  280. LL_ERRS("Messaging") << "No template filename specified" << llendl;
  281. mbError = TRUE;
  282. return;
  283. }
  284. std::string template_body;
  285. if(!_read_file_into_string(template_body, filename))
  286. {
  287. if (failure_is_fatal) {
  288. LL_ERRS("Messaging") << "Failed to open template: " << filename << llendl;
  289. } else {
  290. LL_WARNS("Messaging") << "Failed to open template: " << filename << llendl;
  291. }
  292. mbError = TRUE;
  293. return;
  294. }
  295. LLTemplateTokenizer tokens(template_body);
  296. LLTemplateParser parsed(tokens);
  297. mMessageFileVersionNumber = parsed.getVersion();
  298. for(LLTemplateParser::message_iterator iter = parsed.getMessagesBegin();
  299. iter != parsed.getMessagesEnd();
  300. iter++)
  301. {
  302. addTemplate(*iter);
  303. }
  304. }
  305. LLMessageSystem::~LLMessageSystem()
  306. {
  307. mMessageTemplates.clear(); // don't delete templates.
  308. for_each(mMessageNumbers.begin(), mMessageNumbers.end(), DeletePairedPointer());
  309. mMessageNumbers.clear();
  310. if (!mbError)
  311. {
  312. end_net(mSocket);
  313. }
  314. mSocket = 0;
  315. delete mTemplateMessageReader;
  316. mTemplateMessageReader = NULL;
  317. mMessageReader = NULL;
  318. delete mTemplateMessageBuilder;
  319. mTemplateMessageBuilder = NULL;
  320. mMessageBuilder = NULL;
  321. delete mLLSDMessageReader;
  322. mLLSDMessageReader = NULL;
  323. delete mLLSDMessageBuilder;
  324. mLLSDMessageBuilder = NULL;
  325. delete mPollInfop;
  326. mPollInfop = NULL;
  327. mIncomingCompressedSize = 0;
  328. mCurrentRecvPacketID = 0;
  329. }
  330. void LLMessageSystem::clearReceiveState()
  331. {
  332. mCurrentRecvPacketID = 0;
  333. mIncomingCompressedSize = 0;
  334. mLastSender.invalidate();
  335. mLastReceivingIF.invalidate();
  336. mMessageReader->clearMessage();
  337. mLastMessageFromTrustedMessageService = false;
  338. }
  339. BOOL LLMessageSystem::poll(F32 seconds)
  340. {
  341. S32 num_socks;
  342. apr_status_t status;
  343. status = apr_poll(&(mPollInfop->mPollFD), 1, &num_socks,(U64)(seconds*1000000.f));
  344. if (status != APR_TIMEUP)
  345. {
  346. ll_apr_warn_status(status);
  347. }
  348. if (num_socks)
  349. {
  350. return TRUE;
  351. }
  352. else
  353. {
  354. return FALSE;
  355. }
  356. }
  357. bool LLMessageSystem::isTrustedSender(const LLHost& host) const
  358. {
  359. LLCircuitData* cdp = mCircuitInfo.findCircuit(host);
  360. if(NULL == cdp)
  361. {
  362. return false;
  363. }
  364. return cdp->getTrusted();
  365. }
  366. void LLMessageSystem::receivedMessageFromTrustedSender()
  367. {
  368. mLastMessageFromTrustedMessageService = true;
  369. }
  370. bool LLMessageSystem::isTrustedSender() const
  371. {
  372. return mLastMessageFromTrustedMessageService ||
  373. isTrustedSender(getSender());
  374. }
  375. static LLMessageSystem::message_template_name_map_t::const_iterator
  376. findTemplate(const LLMessageSystem::message_template_name_map_t& templates,
  377. std::string name)
  378. {
  379. const char* namePrehash = LLMessageStringTable::getInstance()->getString(name.c_str());
  380. if(NULL == namePrehash) {return templates.end();}
  381. return templates.find(namePrehash);
  382. }
  383. bool LLMessageSystem::isTrustedMessage(const std::string& name) const
  384. {
  385. message_template_name_map_t::const_iterator iter =
  386. findTemplate(mMessageTemplates, name);
  387. if(iter == mMessageTemplates.end()) {return false;}
  388. return iter->second->getTrust() == MT_TRUST;
  389. }
  390. bool LLMessageSystem::isUntrustedMessage(const std::string& name) const
  391. {
  392. message_template_name_map_t::const_iterator iter =
  393. findTemplate(mMessageTemplates, name);
  394. if(iter == mMessageTemplates.end()) {return false;}
  395. return iter->second->getTrust() == MT_NOTRUST;
  396. }
  397. LLCircuitData* LLMessageSystem::findCircuit(const LLHost& host,
  398. bool resetPacketId)
  399. {
  400. LLCircuitData* cdp = mCircuitInfo.findCircuit(host);
  401. if (!cdp)
  402. {
  403. // This packet comes from a circuit we don't know about.
  404. // Are we rejecting off-circuit packets?
  405. if (mbProtected)
  406. {
  407. // cdp is already NULL, so we don't need to unset it.
  408. }
  409. else
  410. {
  411. // nope, open the new circuit
  412. cdp = mCircuitInfo.addCircuitData(host, mCurrentRecvPacketID);
  413. if(resetPacketId)
  414. {
  415. // I added this - I think it's correct - DJS
  416. // reset packet in ID
  417. cdp->setPacketInID(mCurrentRecvPacketID);
  418. }
  419. // And claim the packet is on the circuit we just added.
  420. }
  421. }
  422. else
  423. {
  424. // this is an old circuit. . . is it still alive?
  425. if (!cdp->isAlive())
  426. {
  427. // nope. don't accept if we're protected
  428. if (mbProtected)
  429. {
  430. // don't accept packets from unexpected sources
  431. cdp = NULL;
  432. }
  433. else
  434. {
  435. // wake up the circuit
  436. cdp->setAlive(TRUE);
  437. if(resetPacketId)
  438. {
  439. // reset packet in ID
  440. cdp->setPacketInID(mCurrentRecvPacketID);
  441. }
  442. }
  443. }
  444. }
  445. return cdp;
  446. }
  447. // Returns TRUE if a valid, on-circuit message has been received.
  448. BOOL LLMessageSystem::checkMessages( S64 frame_count )
  449. {
  450. // Pump
  451. BOOL valid_packet = FALSE;
  452. mMessageReader = mTemplateMessageReader;
  453. LLTransferTargetVFile::updateQueue();
  454. if (!mNumMessageCounts)
  455. {
  456. // This is the first message being handled after a resetReceiveCounts,
  457. // we must be starting the message processing loop. Reset the timers.
  458. mCurrentMessageTimeSeconds = totalTime() * SEC_PER_USEC;
  459. mMessageCountTime = getMessageTimeSeconds();
  460. }
  461. // loop until either no packets or a valid packet
  462. // i.e., burn through packets from unregistered circuits
  463. S32 receive_size = 0;
  464. do
  465. {
  466. clearReceiveState();
  467. BOOL recv_reliable = FALSE;
  468. BOOL recv_resent = FALSE;
  469. S32 acks = 0;
  470. S32 true_rcv_size = 0;
  471. U8* buffer = mTrueReceiveBuffer;
  472. mTrueReceiveSize = mPacketRing.receivePacket(mSocket, (char *)mTrueReceiveBuffer);
  473. // If you want to dump all received packets into SecondLife.log, uncomment this
  474. //dumpPacketToLog();
  475. receive_size = mTrueReceiveSize;
  476. mLastSender = mPacketRing.getLastSender();
  477. mLastReceivingIF = mPacketRing.getLastReceivingInterface();
  478. if (receive_size < (S32) LL_MINIMUM_VALID_PACKET_SIZE)
  479. {
  480. // A receive size of zero is OK, that means that there are no more packets available.
  481. // Ones that are non-zero but below the minimum packet size are worrisome.
  482. if (receive_size > 0)
  483. {
  484. LL_WARNS("Messaging") << "Invalid (too short) packet discarded " << receive_size << llendl;
  485. callExceptionFunc(MX_PACKET_TOO_SHORT);
  486. }
  487. // no data in packet receive buffer
  488. valid_packet = FALSE;
  489. }
  490. else
  491. {
  492. LLHost host;
  493. LLCircuitData* cdp;
  494. // note if packet acks are appended.
  495. if(buffer[0] & LL_ACK_FLAG)
  496. {
  497. acks += buffer[--receive_size];
  498. true_rcv_size = receive_size;
  499. if(receive_size >= ((S32)(acks * sizeof(TPACKETID) + LL_MINIMUM_VALID_PACKET_SIZE)))
  500. {
  501. receive_size -= acks * sizeof(TPACKETID);
  502. }
  503. else
  504. {
  505. // mal-formed packet. ignore it and continue with
  506. // the next one
  507. LL_WARNS("Messaging") << "Malformed packet received. Packet size "
  508. << receive_size << " with invalid no. of acks " << acks
  509. << llendl;
  510. valid_packet = FALSE;
  511. continue;
  512. }
  513. }
  514. // process the message as normal
  515. mIncomingCompressedSize = zeroCodeExpand(&buffer, &receive_size);
  516. mCurrentRecvPacketID = ntohl(*((U32*)(&buffer[1])));
  517. host = getSender();
  518. const bool resetPacketId = true;
  519. cdp = findCircuit(host, resetPacketId);
  520. // At this point, cdp is now a pointer to the circuit that
  521. // this message came in on if it's valid, and NULL if the
  522. // circuit was bogus.
  523. if(cdp && (acks > 0) && ((S32)(acks * sizeof(TPACKETID)) < (true_rcv_size)))
  524. {
  525. TPACKETID packet_id;
  526. U32 mem_id=0;
  527. for(S32 i = 0; i < acks; ++i)
  528. {
  529. true_rcv_size -= sizeof(TPACKETID);
  530. memcpy(&mem_id, &mTrueReceiveBuffer[true_rcv_size], /* Flawfinder: ignore*/
  531. sizeof(TPACKETID));
  532. packet_id = ntohl(mem_id);
  533. //LL_INFOS("Messaging") << "got ack: " << packet_id << llendl;
  534. cdp->ackReliablePacket(packet_id);
  535. }
  536. if (!cdp->getUnackedPacketCount())
  537. {
  538. // Remove this circuit from the list of circuits with unacked packets
  539. mCircuitInfo.mUnackedCircuitMap.erase(cdp->mHost);
  540. }
  541. }
  542. if (buffer[0] & LL_RELIABLE_FLAG)
  543. {
  544. recv_reliable = TRUE;
  545. }
  546. if (buffer[0] & LL_RESENT_FLAG)
  547. {
  548. recv_resent = TRUE;
  549. if (cdp && cdp->isDuplicateResend(mCurrentRecvPacketID))
  550. {
  551. // We need to ACK here to suppress
  552. // further resends of packets we've
  553. // already seen.
  554. if (recv_reliable)
  555. {
  556. //mAckList.addData(new LLPacketAck(host, mCurrentRecvPacketID));
  557. // ***************************************
  558. // TESTING CODE
  559. //if(mCircuitInfo.mCurrentCircuit->mHost != host)
  560. //{
  561. // LL_WARNS("Messaging") << "DISCARDED PACKET HOST MISMATCH! HOST: "
  562. // << host << " CIRCUIT: "
  563. // << mCircuitInfo.mCurrentCircuit->mHost
  564. // << llendl;
  565. //}
  566. // ***************************************
  567. //mCircuitInfo.mCurrentCircuit->mAcks.put(mCurrentRecvPacketID);
  568. cdp->collectRAck(mCurrentRecvPacketID);
  569. }
  570. LL_DEBUGS("Messaging") << "Discarding duplicate resend from " << host << llendl;
  571. if(mVerboseLog)
  572. {
  573. std::ostringstream str;
  574. str << "MSG: <- " << host;
  575. std::string tbuf;
  576. tbuf = llformat( "\t%6d\t%6d\t%6d ", receive_size, (mIncomingCompressedSize ? mIncomingCompressedSize : receive_size), mCurrentRecvPacketID);
  577. str << tbuf << "(unknown)"
  578. << (recv_reliable ? " reliable" : "")
  579. << " resent "
  580. << ((acks > 0) ? "acks" : "")
  581. << " DISCARD DUPLICATE";
  582. LL_INFOS("Messaging") << str.str() << llendl;
  583. }
  584. mPacketsIn++;
  585. valid_packet = FALSE;
  586. continue;
  587. }
  588. }
  589. // UseCircuitCode can be a valid, off-circuit packet.
  590. // But we don't want to acknowledge UseCircuitCode until the circuit is
  591. // available, which is why the acknowledgement test is done above. JC
  592. bool trusted = cdp && cdp->getTrusted();
  593. valid_packet = mTemplateMessageReader->validateMessage(
  594. buffer,
  595. receive_size,
  596. host,
  597. trusted);
  598. if (!valid_packet)
  599. {
  600. clearReceiveState();
  601. }
  602. // UseCircuitCode is allowed in even from an invalid circuit, so that
  603. // we can toss circuits around.
  604. if(
  605. valid_packet &&
  606. !cdp &&
  607. (mTemplateMessageReader->getMessageName() !=
  608. _PREHASH_UseCircuitCode))
  609. {
  610. logMsgFromInvalidCircuit( host, recv_reliable );
  611. clearReceiveState();
  612. valid_packet = FALSE;
  613. }
  614. if(
  615. valid_packet &&
  616. cdp &&
  617. !cdp->getTrusted() &&
  618. mTemplateMessageReader->isTrusted())
  619. {
  620. logTrustedMsgFromUntrustedCircuit( host );
  621. clearReceiveState();
  622. sendDenyTrustedCircuit(host);
  623. valid_packet = FALSE;
  624. }
  625. if( valid_packet )
  626. {
  627. logValidMsg(cdp, host, recv_reliable, recv_resent, (BOOL)(acks>0) );
  628. valid_packet = mTemplateMessageReader->readMessage(buffer, host);
  629. }
  630. // It's possible that the circuit went away, because ANY message can disable the circuit
  631. // (for example, UseCircuit, CloseCircuit, DisableSimulator). Find it again.
  632. cdp = mCircuitInfo.findCircuit(host);
  633. if (valid_packet)
  634. {
  635. mPacketsIn++;
  636. mBytesIn += mTrueReceiveSize;
  637. // ACK here for valid packets that we've seen
  638. // for the first time.
  639. if (cdp && recv_reliable)
  640. {
  641. // Add to the recently received list for duplicate suppression
  642. cdp->mRecentlyReceivedReliablePackets[mCurrentRecvPacketID] = getMessageTimeUsecs();
  643. // Put it onto the list of packets to be acked
  644. cdp->collectRAck(mCurrentRecvPacketID);
  645. mReliablePacketsIn++;
  646. }
  647. }
  648. else
  649. {
  650. if (mbProtected && (!cdp))
  651. {
  652. LL_WARNS("Messaging") << "Invalid Packet from invalid circuit " << host << llendl;
  653. mOffCircuitPackets++;
  654. }
  655. else
  656. {
  657. mInvalidOnCircuitPackets++;
  658. }
  659. }
  660. }
  661. } while (!valid_packet && receive_size > 0);
  662. F64 mt_sec = getMessageTimeSeconds();
  663. // Check to see if we need to print debug info
  664. if ((mt_sec - mCircuitPrintTime) > mCircuitPrintFreq)
  665. {
  666. dumpCircuitInfo();
  667. mCircuitPrintTime = mt_sec;
  668. }
  669. if( !valid_packet )
  670. {
  671. clearReceiveState();
  672. }
  673. return valid_packet;
  674. }
  675. S32 LLMessageSystem::getReceiveBytes() const
  676. {
  677. if (getReceiveCompressedSize())
  678. {
  679. return getReceiveCompressedSize() * 8;
  680. }
  681. else
  682. {
  683. return getReceiveSize() * 8;
  684. }
  685. }
  686. void LLMessageSystem::processAcks()
  687. {
  688. LLMemType mt_pa(LLMemType::MTYPE_MESSAGE_PROCESS_ACKS);
  689. F64 mt_sec = getMessageTimeSeconds();
  690. {
  691. gTransferManager.updateTransfers();
  692. if (gXferManager)
  693. {
  694. gXferManager->retransmitUnackedPackets();
  695. }
  696. if (gAssetStorage)
  697. {
  698. gAssetStorage->checkForTimeouts();
  699. }
  700. }
  701. BOOL dump = FALSE;
  702. {
  703. // Check the status of circuits
  704. mCircuitInfo.updateWatchDogTimers(this);
  705. //resend any necessary packets
  706. mCircuitInfo.resendUnackedPackets(mUnackedListDepth, mUnackedListSize);
  707. //cycle through ack list for each host we need to send acks to
  708. mCircuitInfo.sendAcks();
  709. if (!mDenyTrustedCircuitSet.empty())
  710. {
  711. LL_INFOS("Messaging") << "Sending queued DenyTrustedCircuit messages." << llendl;
  712. for (host_set_t::iterator hostit = mDenyTrustedCircuitSet.begin(); hostit != mDenyTrustedCircuitSet.end(); ++hostit)
  713. {
  714. reallySendDenyTrustedCircuit(*hostit);
  715. }
  716. mDenyTrustedCircuitSet.clear();
  717. }
  718. if (mMaxMessageCounts >= 0)
  719. {
  720. if (mNumMessageCounts >= mMaxMessageCounts)
  721. {
  722. dump = TRUE;
  723. }
  724. }
  725. if (mMaxMessageTime >= 0.f)
  726. {
  727. // This is one of the only places where we're required to get REAL message system time.
  728. mReceiveTime = (F32)(getMessageTimeSeconds(TRUE) - mMessageCountTime);
  729. if (mReceiveTime > mMaxMessageTime)
  730. {
  731. dump = TRUE;
  732. }
  733. }
  734. }
  735. if (dump)
  736. {
  737. dumpReceiveCounts();
  738. }
  739. resetReceiveCounts();
  740. if ((mt_sec - mResendDumpTime) > CIRCUIT_DUMP_TIMEOUT)
  741. {
  742. mResendDumpTime = mt_sec;
  743. mCircuitInfo.dumpResends();
  744. }
  745. }
  746. void LLMessageSystem::copyMessageReceivedToSend()
  747. {
  748. // NOTE: babbage: switch builder to match reader to avoid
  749. // converting message format
  750. if(mMessageReader == mTemplateMessageReader)
  751. {
  752. mMessageBuilder = mTemplateMessageBuilder;
  753. }
  754. else
  755. {
  756. mMessageBuilder = mLLSDMessageBuilder;
  757. }
  758. mSendReliable = FALSE;
  759. mMessageBuilder->newMessage(mMessageReader->getMessageName());
  760. mMessageReader->copyToBuilder(*mMessageBuilder);
  761. }
  762. LLSD LLMessageSystem::getReceivedMessageLLSD() const
  763. {
  764. LLSDMessageBuilder builder;
  765. mMessageReader->copyToBuilder(builder);
  766. return builder.getMessage();
  767. }
  768. LLSD LLMessageSystem::getBuiltMessageLLSD() const
  769. {
  770. LLSD result;
  771. if (mLLSDMessageBuilder == mMessageBuilder)
  772. {
  773. result = mLLSDMessageBuilder->getMessage();
  774. }
  775. else
  776. {
  777. // TODO: implement as below?
  778. llerrs << "Message not built as LLSD." << llendl;
  779. }
  780. return result;
  781. }
  782. LLSD LLMessageSystem::wrapReceivedTemplateData() const
  783. {
  784. if(mMessageReader == mTemplateMessageReader)
  785. {
  786. LLTemplateMessageBuilder builder(mMessageTemplates);
  787. builder.newMessage(mMessageReader->getMessageName());
  788. mMessageReader->copyToBuilder(builder);
  789. U8 buffer[MAX_BUFFER_SIZE];
  790. const U8 offset_to_data = 0;
  791. U32 size = builder.buildMessage(buffer, MAX_BUFFER_SIZE,
  792. offset_to_data);
  793. std::vector<U8> binary_data(buffer, buffer+size);
  794. LLSD wrapped_data = LLSD::emptyMap();
  795. wrapped_data["binary-template-data"] = binary_data;
  796. return wrapped_data;
  797. }
  798. else
  799. {
  800. return getReceivedMessageLLSD();
  801. }
  802. }
  803. LLSD LLMessageSystem::wrapBuiltTemplateData() const
  804. {
  805. LLSD result;
  806. if (mLLSDMessageBuilder == mMessageBuilder)
  807. {
  808. result = getBuiltMessageLLSD();
  809. }
  810. else
  811. {
  812. U8 buffer[MAX_BUFFER_SIZE];
  813. const U8 offset_to_data = 0;
  814. U32 size = mTemplateMessageBuilder->buildMessage(
  815. buffer, MAX_BUFFER_SIZE,
  816. offset_to_data);
  817. std::vector<U8> binary_data(buffer, buffer+size);
  818. LLSD wrapped_data = LLSD::emptyMap();
  819. wrapped_data["binary-template-data"] = binary_data;
  820. result = wrapped_data;
  821. }
  822. return result;
  823. }
  824. LLStoredMessagePtr LLMessageSystem::getReceivedMessage() const
  825. {
  826. const std::string& name = mMessageReader->getMessageName();
  827. LLSD message = wrapReceivedTemplateData();
  828. return LLStoredMessagePtr(new LLStoredMessage(name, message));
  829. }
  830. LLStoredMessagePtr LLMessageSystem::getBuiltMessage() const
  831. {
  832. const std::string& name = mMessageBuilder->getMessageName();
  833. LLSD message = wrapBuiltTemplateData();
  834. return LLStoredMessagePtr(new LLStoredMessage(name, message));
  835. }
  836. S32 LLMessageSystem::sendMessage(const LLHost &host, LLStoredMessagePtr message)
  837. {
  838. return sendMessage(host, message->mName.c_str(), message->mMessage);
  839. }
  840. void LLMessageSystem::clearMessage()
  841. {
  842. mSendReliable = FALSE;
  843. mMessageBuilder->clearMessage();
  844. }
  845. // set block to add data to within current message
  846. void LLMessageSystem::nextBlockFast(const char *blockname)
  847. {
  848. mMessageBuilder->nextBlock(blockname);
  849. }
  850. void LLMessageSystem::nextBlock(const char *blockname)
  851. {
  852. nextBlockFast(LLMessageStringTable::getInstance()->getString(blockname));
  853. }
  854. BOOL LLMessageSystem::isSendFull(const char* blockname)
  855. {
  856. char* stringTableName = NULL;
  857. if(NULL != blockname)
  858. {
  859. stringTableName = LLMessageStringTable::getInstance()->getString(blockname);
  860. }
  861. return isSendFullFast(stringTableName);
  862. }
  863. BOOL LLMessageSystem::isSendFullFast(const char* blockname)
  864. {
  865. return mMessageBuilder->isMessageFull(blockname);
  866. }
  867. // blow away the last block of a message, return FALSE if that leaves no blocks or there wasn't a block to remove
  868. // TODO: Babbage: Remove this horror.
  869. BOOL LLMessageSystem::removeLastBlock()
  870. {
  871. return mMessageBuilder->removeLastBlock();
  872. }
  873. S32 LLMessageSystem::sendReliable(const LLHost &host)
  874. {
  875. return sendReliable(host, LL_DEFAULT_RELIABLE_RETRIES, TRUE, LL_PING_BASED_TIMEOUT_DUMMY, NULL, NULL);
  876. }
  877. S32 LLMessageSystem::sendSemiReliable(const LLHost &host, void (*callback)(void **,S32), void ** callback_data)
  878. {
  879. F32 timeout;
  880. LLCircuitData *cdp = mCircuitInfo.findCircuit(host);
  881. if (cdp)
  882. {
  883. timeout = llmax(LL_MINIMUM_SEMIRELIABLE_TIMEOUT_SECONDS,
  884. LL_SEMIRELIABLE_TIMEOUT_FACTOR * cdp->getPingDelayAveraged());
  885. }
  886. else
  887. {
  888. timeout = LL_SEMIRELIABLE_TIMEOUT_FACTOR * LL_AVERAGED_PING_MAX;
  889. }
  890. const S32 retries = 0;
  891. const BOOL ping_based_timeout = FALSE;
  892. return sendReliable(host, retries, ping_based_timeout, timeout, callback, callback_data);
  893. }
  894. // send the message via a UDP packet
  895. S32 LLMessageSystem::sendReliable( const LLHost &host,
  896. S32 retries,
  897. BOOL ping_based_timeout,
  898. F32 timeout,
  899. void (*callback)(void **,S32),
  900. void ** callback_data)
  901. {
  902. if (ping_based_timeout)
  903. {
  904. LLCircuitData *cdp = mCircuitInfo.findCircuit(host);
  905. if (cdp)
  906. {
  907. timeout = llmax(LL_MINIMUM_RELIABLE_TIMEOUT_SECONDS, LL_RELIABLE_TIMEOUT_FACTOR * cdp->getPingDelayAveraged());
  908. }
  909. else
  910. {
  911. timeout = llmax(LL_MINIMUM_RELIABLE_TIMEOUT_SECONDS, LL_RELIABLE_TIMEOUT_FACTOR * LL_AVERAGED_PING_MAX);
  912. }
  913. }
  914. mSendReliable = TRUE;
  915. mReliablePacketParams.set(host, retries, ping_based_timeout, timeout,
  916. callback, callback_data,
  917. const_cast<char*>(mMessageBuilder->getMessageName()));
  918. return sendMessage(host);
  919. }
  920. void LLMessageSystem::forwardMessage(const LLHost &host)
  921. {
  922. copyMessageReceivedToSend();
  923. sendMessage(host);
  924. }
  925. void LLMessageSystem::forwardReliable(const LLHost &host)
  926. {
  927. copyMessageReceivedToSend();
  928. sendReliable(host);
  929. }
  930. void LLMessageSystem::forwardReliable(const U32 circuit_code)
  931. {
  932. copyMessageReceivedToSend();
  933. sendReliable(findHost(circuit_code));
  934. }
  935. S32 LLMessageSystem::forwardReliable( const LLHost &host,
  936. S32 retries,
  937. BOOL ping_based_timeout,
  938. F32 timeout,
  939. void (*callback)(void **,S32),
  940. void ** callback_data)
  941. {
  942. copyMessageReceivedToSend();
  943. return sendReliable(host, retries, ping_based_timeout, timeout, callback, callback_data);
  944. }
  945. S32 LLMessageSystem::flushSemiReliable(const LLHost &host, void (*callback)(void **,S32), void ** callback_data)
  946. {
  947. F32 timeout;
  948. LLCircuitData *cdp = mCircuitInfo.findCircuit(host);
  949. if (cdp)
  950. {
  951. timeout = llmax(LL_MINIMUM_SEMIRELIABLE_TIMEOUT_SECONDS,
  952. LL_SEMIRELIABLE_TIMEOUT_FACTOR * cdp->getPingDelayAveraged());
  953. }
  954. else
  955. {
  956. timeout = LL_SEMIRELIABLE_TIMEOUT_FACTOR * LL_AVERAGED_PING_MAX;
  957. }
  958. S32 send_bytes = 0;
  959. if (mMessageBuilder->getMessageSize())
  960. {
  961. mSendReliable = TRUE;
  962. // No need for ping-based retry as not going to retry
  963. mReliablePacketParams.set(host, 0, FALSE, timeout, callback,
  964. callback_data,
  965. const_cast<char*>(mMessageBuilder->getMessageName()));
  966. send_bytes = sendMessage(host);
  967. clearMessage();
  968. }
  969. else
  970. {
  971. delete callback_data;
  972. }
  973. return send_bytes;
  974. }
  975. S32 LLMessageSystem::flushReliable(const LLHost &host)
  976. {
  977. S32 send_bytes = 0;
  978. if (mMessageBuilder->getMessageSize())
  979. {
  980. send_bytes = sendReliable(host);
  981. }
  982. clearMessage();
  983. return send_bytes;
  984. }
  985. LLHTTPClient::ResponderPtr LLMessageSystem::createResponder(const std::string& name)
  986. {
  987. if(mSendReliable)
  988. {
  989. return new LLFnPtrResponder(
  990. mReliablePacketParams.mCallback,
  991. mReliablePacketParams.mCallbackData,
  992. name);
  993. }
  994. else
  995. {
  996. // These messages aren't really unreliable, they just weren't
  997. // explicitly sent as reliable, so they don't have a callback
  998. // LL_WARNS("Messaging") << "LLMessageSystem::sendMessage: Sending unreliable "
  999. // << mMessageBuilder->getMessageName() << " message via HTTP"
  1000. // << llendl;
  1001. return new LLFnPtrResponder(
  1002. NULL,
  1003. NULL,
  1004. name);
  1005. }
  1006. }
  1007. // This can be called from signal handlers,
  1008. // so should should not use llinfos.
  1009. S32 LLMessageSystem::sendMessage(const LLHost &host)
  1010. {
  1011. if (! mMessageBuilder->isBuilt())
  1012. {
  1013. mSendSize = mMessageBuilder->buildMessage(
  1014. mSendBuffer,
  1015. MAX_BUFFER_SIZE,
  1016. 0);
  1017. }
  1018. if (!(host.isOk())) // if port and ip are zero, don't bother trying to send the message
  1019. {
  1020. return 0;
  1021. }
  1022. LLCircuitData *cdp = mCircuitInfo.findCircuit(host);
  1023. if (!cdp)
  1024. {
  1025. // this is a new circuit!
  1026. // are we protected?
  1027. if (mbProtected)
  1028. {
  1029. // yup! don't send packets to an unknown circuit
  1030. if(mVerboseLog)
  1031. {
  1032. LL_INFOS_ONCE("Messaging") << "MSG: -> " << host << "\tUNKNOWN CIRCUIT:\t"
  1033. << mMessageBuilder->getMessageName() << llendl;
  1034. }
  1035. LL_WARNS_ONCE("Messaging") << "sendMessage - Trying to send "
  1036. << mMessageBuilder->getMessageName() << " on unknown circuit "
  1037. << host << llendl;
  1038. return 0;
  1039. }
  1040. else
  1041. {
  1042. // nope, open the new circuit
  1043. cdp = mCircuitInfo.addCircuitData(host, 0);
  1044. }
  1045. }
  1046. else
  1047. {
  1048. // this is an old circuit. . . is it still alive?
  1049. if (!cdp->isAlive())
  1050. {
  1051. // nope. don't send to dead circuits
  1052. if(mVerboseLog)
  1053. {
  1054. LL_INFOS("Messaging") << "MSG: -> " << host << "\tDEAD CIRCUIT\t\t"
  1055. << mMessageBuilder->getMessageName() << llendl;
  1056. }
  1057. LL_WARNS("Messaging") << "sendMessage - Trying to send message "
  1058. << mMessageBuilder->getMessageName() << " to dead circuit "
  1059. << host << llendl;
  1060. return 0;
  1061. }
  1062. }
  1063. // NOTE: babbage: LLSD message -> HTTP, template message -> UDP
  1064. if(mMessageBuilder == mLLSDMessageBuilder)
  1065. {
  1066. LLSD message = mLLSDMessageBuilder->getMessage();
  1067. const LLHTTPSender& sender = LLHTTPSender::getSender(host);
  1068. sender.send(
  1069. host,
  1070. mLLSDMessageBuilder->getMessageName(),
  1071. message,
  1072. createResponder(mLLSDMessageBuilder->getMessageName()));
  1073. mSendReliable = FALSE;
  1074. mReliablePacketParams.clear();
  1075. return 1;
  1076. }
  1077. // zero out the flags and packetid. Subtract 1 here so that we do
  1078. // not overwrite the offset if it was set set in buildMessage().
  1079. memset(mSendBuffer, 0, LL_PACKET_ID_SIZE - 1);
  1080. // add the send id to the front of the message
  1081. cdp->nextPacketOutID();
  1082. // Packet ID size is always 4
  1083. *((S32*)&mSendBuffer[PHL_PACKET_ID]) = htonl(cdp->getPacketOutID());
  1084. // Compress the message, which will usually reduce its size.
  1085. U8 * buf_ptr = (U8 *)mSendBuffer;
  1086. U32 buffer_length = mSendSize;
  1087. mMessageBuilder->compressMessage(buf_ptr, buffer_length);
  1088. if (buffer_length > 1500)
  1089. {
  1090. if((mMessageBuilder->getMessageName() != _PREHASH_ChildAgentUpdate)
  1091. && (mMessageBuilder->getMessageName() != _PREHASH_SendXferPacket))
  1092. {
  1093. LL_WARNS("Messaging") << "sendMessage - Trying to send "
  1094. << ((buffer_length > 4000) ? "EXTRA " : "")
  1095. << "BIG message " << mMessageBuilder->getMessageName() << " - "
  1096. << buffer_length << llendl;
  1097. }
  1098. }
  1099. if (mSendReliable)
  1100. {
  1101. buf_ptr[0] |= LL_RELIABLE_FLAG;
  1102. if (!cdp->getUnackedPacketCount())
  1103. {
  1104. // We are adding the first packed onto the unacked packet list(s)
  1105. // Add this circuit to the list of circuits with unacked packets
  1106. mCircuitInfo.mUnackedCircuitMap[cdp->mHost] = cdp;
  1107. }
  1108. cdp->addReliablePacket(mSocket,buf_ptr,buffer_length, &mReliablePacketParams);
  1109. mReliablePacketsOut++;
  1110. }
  1111. // tack packet acks onto the end of this message
  1112. S32 space_left = (MTUBYTES - buffer_length) / sizeof(TPACKETID); // space left for packet ids
  1113. S32 ack_count = (S32)cdp->mAcks.size();
  1114. BOOL is_ack_appended = FALSE;
  1115. std::vector<TPACKETID> acks;
  1116. if((space_left > 0) && (ack_count > 0) &&
  1117. (mMessageBuilder->getMessageName() != _PREHASH_PacketAck))
  1118. {
  1119. buf_ptr[0] |= LL_ACK_FLAG;
  1120. S32 append_ack_count = llmin(space_left, ack_count);
  1121. const S32 MAX_ACKS = 250;
  1122. append_ack_count = llmin(append_ack_count, MAX_ACKS);
  1123. std::vector<TPACKETID>::iterator iter = cdp->mAcks.begin();
  1124. std::vector<TPACKETID>::iterator last = cdp->mAcks.begin();
  1125. last += append_ack_count;
  1126. TPACKETID packet_id;
  1127. for( ; iter != last ; ++iter)
  1128. {
  1129. // grab the next packet id.
  1130. packet_id = (*iter);
  1131. if(mVerboseLog)
  1132. {
  1133. acks.push_back(packet_id);
  1134. }
  1135. // put it on the end of the buffer
  1136. packet_id = htonl(packet_id);
  1137. if((S32)(buffer_length + sizeof(TPACKETID)) < MAX_BUFFER_SIZE)
  1138. {
  1139. memcpy(&buf_ptr[buffer_length], &packet_id, sizeof(TPACKETID)); /* Flawfinder: ignore */
  1140. // Do the accounting
  1141. buffer_length += sizeof(TPACKETID);
  1142. }
  1143. else
  1144. {
  1145. // Just reporting error is likely not enough. Need to
  1146. // check how to abort or error out gracefully from
  1147. // this function. XXXTBD
  1148. // *NOTE: Actually hitting this error would indicate
  1149. // the calculation above for space_left, ack_count,
  1150. // append_acout_count is incorrect or that
  1151. // MAX_BUFFER_SIZE has fallen below MTU which is bad
  1152. // and probably programmer error.
  1153. LL_ERRS("Messaging") << "Buffer packing failed due to size.." << llendl;
  1154. }
  1155. }
  1156. // clean up the source
  1157. cdp->mAcks.erase(cdp->mAcks.begin(), last);
  1158. // tack the count in the final byte
  1159. U8 count = (U8)append_ack_count;
  1160. buf_ptr[buffer_length++] = count;
  1161. is_ack_appended = TRUE;
  1162. }
  1163. BOOL success;
  1164. success = mPacketRing.sendPacket(mSocket, (char *)buf_ptr, buffer_length, host);
  1165. if (!success)
  1166. {
  1167. mSendPacketFailureCount++;
  1168. }
  1169. else
  1170. {
  1171. // mCircuitInfo already points to the correct circuit data
  1172. cdp->addBytesOut( buffer_length );
  1173. }
  1174. if(mVerboseLog)
  1175. {
  1176. std::ostringstream str;
  1177. str << "MSG: -> " << host;
  1178. std::string buffer;
  1179. buffer = llformat( "\t%6d\t%6d\t%6d ", mSendSize, buffer_length, cdp->getPacketOutID());
  1180. str << buffer
  1181. << mMessageBuilder->getMessageName()
  1182. << (mSendReliable ? " reliable " : "");
  1183. if(is_ack_appended)
  1184. {
  1185. str << "\tACKS:\t";
  1186. std::ostream_iterator<TPACKETID> append(str, " ");
  1187. std::copy(acks.begin(), acks.end(), append);
  1188. }
  1189. LL_INFOS("Messaging") << str.str() << llendl;
  1190. }
  1191. mPacketsOut++;
  1192. mBytesOut += buffer_length;
  1193. mSendReliable = FALSE;
  1194. mReliablePacketParams.clear();
  1195. return buffer_length;
  1196. }
  1197. void LLMessageSystem::logMsgFromInvalidCircuit( const LLHost& host, BOOL recv_reliable )
  1198. {
  1199. if(mVerboseLog)
  1200. {
  1201. std::ostringstream str;
  1202. str << "MSG: <- " << host;
  1203. std::string buffer;
  1204. buffer = llformat( "\t%6d\t%6d\t%6d ", mMessageReader->getMessageSize(), (mIncomingCompressedSize ? mIncomingCompressedSize: mMessageReader->getMessageSize()), mCurrentRecvPacketID);
  1205. str << buffer
  1206. << nullToEmpty(mMessageReader->getMessageName())
  1207. << (recv_reliable ? " reliable" : "")
  1208. << " REJECTED";
  1209. LL_INFOS("Messaging") << str.str() << llendl;
  1210. }
  1211. // nope!
  1212. // cout << "Rejecting unexpected message " << mCurrentMessageTemplate->mName << " from " << hex << ip << " , " << dec << port << endl;
  1213. // Keep track of rejected messages as well
  1214. if (mNumMessageCounts >= MAX_MESSAGE_COUNT_NUM)
  1215. {
  1216. LL_WARNS("Messaging") << "Got more than " << MAX_MESSAGE_COUNT_NUM << " packets without clearing counts" << llendl;
  1217. }
  1218. else
  1219. {
  1220. // TODO: babbage: work out if we need these
  1221. // mMessageCountList[mNumMessageCounts].mMessageNum = mCurrentRMessageTemplate->mMessageNumber;
  1222. mMessageCountList[mNumMessageCounts].mMessageBytes = mMessageReader->getMessageSize();
  1223. mMessageCountList[mNumMessageCounts].mInvalid = TRUE;
  1224. mNumMessageCounts++;
  1225. }
  1226. }
  1227. S32 LLMessageSystem::sendMessage(
  1228. const LLHost &host,
  1229. const char* name,
  1230. const LLSD& message)
  1231. {
  1232. if (!(host.isOk()))
  1233. {
  1234. LL_WARNS("Messaging") << "trying to send message to invalid host" << llendl;
  1235. return 0;
  1236. }
  1237. const LLHTTPSender& sender = LLHTTPSender::getSender(host);
  1238. sender.send(host, name, message, createResponder(name));
  1239. return 1;
  1240. }
  1241. void LLMessageSystem::logTrustedMsgFromUntrustedCircuit( const LLHost& host )
  1242. {
  1243. // RequestTrustedCircuit is how we establish trust, so don't spam
  1244. // if it's received on a trusted circuit. JC
  1245. if (strcmp(mMessageReader->getMessageName(), "RequestTrustedCircuit"))
  1246. {
  1247. LL_WARNS("Messaging") << "Received trusted message on untrusted circuit. "
  1248. << "Will reply with deny. "
  1249. << "Message: " << nullToEmpty(mMessageReader->getMessageName())
  1250. << " Host: " << host << llendl;
  1251. }
  1252. if (mNumMessageCounts >= MAX_MESSAGE_COUNT_NUM)
  1253. {
  1254. LL_WARNS("Messaging") << "got more than " << MAX_MESSAGE_COUNT_NUM
  1255. << " packets without clearing counts"
  1256. << llendl;
  1257. }
  1258. else
  1259. {
  1260. // TODO: babbage: work out if we need these
  1261. //mMessageCountList[mNumMessageCounts].mMessageNum
  1262. // = mCurrentRMessageTemplate->mMessageNumber;
  1263. mMessageCountList[mNumMessageCounts].mMessageBytes
  1264. = mMessageReader->getMessageSize();
  1265. mMessageCountList[mNumMessageCounts].mInvalid = TRUE;
  1266. mNumMessageCounts++;
  1267. }
  1268. }
  1269. void LLMessageSystem::logValidMsg(LLCircuitData *cdp, const LLHost& host, BOOL recv_reliable, BOOL recv_resent, BOOL recv_acks )
  1270. {
  1271. if (mNumMessageCounts >= MAX_MESSAGE_COUNT_NUM)
  1272. {
  1273. LL_WARNS("Messaging") << "Got more than " << MAX_MESSAGE_COUNT_NUM << " packets without clearing counts" << llendl;
  1274. }
  1275. else
  1276. {
  1277. // TODO: babbage: work out if we need these
  1278. //mMessageCountList[mNumMessageCounts].mMessageNum = mCurrentRMessageTemplate->mMessageNumber;
  1279. mMessageCountList[mNumMessageCounts].mMessageBytes = mMessageReader->getMessageSize();
  1280. mMessageCountList[mNumMessageCounts].mInvalid = FALSE;
  1281. mNumMessageCounts++;
  1282. }
  1283. if (cdp)
  1284. {
  1285. // update circuit packet ID tracking (missing/out of order packets)
  1286. cdp->checkPacketInID( mCurrentRecvPacketID, recv_resent );
  1287. cdp->addBytesIn( mTrueReceiveSize );
  1288. }
  1289. if(mVerboseLog)
  1290. {
  1291. std::ostringstream str;
  1292. str << "MSG: <- " << host;
  1293. std::string buffer;
  1294. buffer = llformat( "\t%6d\t%6d\t%6d ", mMessageReader->getMessageSize(), (mIncomingCompressedSize ? mIncomingCompressedSize : mMessageReader->getMessageSize()), mCurrentRecvPacketID);
  1295. str << buffer
  1296. << nullToEmpty(mMessageReader->getMessageName())
  1297. << (recv_reliable ? " reliable" : "")
  1298. << (recv_resent ? " resent" : "")
  1299. << (recv_acks ? " acks" : "");
  1300. LL_INFOS("Messaging") << str.str() << llendl;
  1301. }
  1302. }
  1303. void LLMessageSystem::sanityCheck()
  1304. {
  1305. // TODO: babbage: reinstate
  1306. // if (!mCurrentRMessageData)
  1307. // {
  1308. // LL_ERRS("Messaging") << "mCurrentRMessageData is NULL" << llendl;
  1309. // }
  1310. // if (!mCurrentRMessageTemplate)
  1311. // {
  1312. // LL_ERRS("Messaging") << "mCurrentRMessageTemplate is NULL" << llendl;
  1313. // }
  1314. // if (!mCurrentRTemplateBlock)
  1315. // {
  1316. // LL_ERRS("Messaging") << "mCurrentRTemplateBlock is NULL" << llendl;
  1317. // }
  1318. // if (!mCurrentRDataBlock)
  1319. // {
  1320. // LL_ERRS("Messaging") << "mCurrentRDataBlock is NULL" << llendl;
  1321. // }
  1322. // if (!mCurrentSMessageData)
  1323. // {
  1324. // LL_ERRS("Messaging") << "mCurrentSMessageData is NULL" << llendl;
  1325. // }
  1326. // if (!mCurrentSMessageTemplate)
  1327. // {
  1328. // LL_ERRS("Messaging") << "mCurrentSMessageTemplate is NULL" << llendl;
  1329. // }
  1330. // if (!mCurrentSTemplateBlock)
  1331. // {
  1332. // LL_ERRS("Messaging") << "mCurrentSTemplateBlock is NULL" << llendl;
  1333. // }
  1334. // if (!mCurrentSDataBlock)
  1335. // {
  1336. // LL_ERRS("Messaging") << "mCurrentSDataBlock is NULL" << llendl;
  1337. // }
  1338. }
  1339. void LLMessageSystem::showCircuitInfo()
  1340. {
  1341. LL_INFOS("Messaging") << mCircuitInfo << llendl;
  1342. }
  1343. void LLMessageSystem::dumpCircuitInfo()
  1344. {
  1345. lldebugst(LLERR_CIRCUIT_INFO) << mCircuitInfo << llendl;
  1346. }
  1347. /* virtual */
  1348. U32 LLMessageSystem::getOurCircuitCode()
  1349. {
  1350. return mOurCircuitCode;
  1351. }
  1352. void LLMessageSystem::getCircuitInfo(LLSD& info) const
  1353. {
  1354. mCircuitInfo.getInfo(info);
  1355. }
  1356. // returns whether the given host is on a trusted circuit
  1357. BOOL LLMessageSystem::getCircuitTrust(const LLHost &host)
  1358. {
  1359. LLCircuitData *cdp = mCircuitInfo.findCircuit(host);
  1360. if (cdp)
  1361. {
  1362. return cdp->getTrusted();
  1363. }
  1364. return FALSE;
  1365. }
  1366. // Activate a circuit, and set its trust level (TRUE if trusted,
  1367. // FALSE if not).
  1368. void LLMessageSystem::enableCircuit(const LLHost &host, BOOL trusted)
  1369. {
  1370. LLCircuitData *cdp = mCircuitInfo.findCircuit(host);
  1371. if (!cdp)
  1372. {
  1373. cdp = mCircuitInfo.addCircuitData(host, 0);
  1374. }
  1375. else
  1376. {
  1377. cdp->setAlive(TRUE);
  1378. }
  1379. cdp->setTrusted(trusted);
  1380. }
  1381. void LLMessageSystem::disableCircuit(const LLHost &host)
  1382. {
  1383. LL_INFOS("Messaging") << "LLMessageSystem::disableCircuit for " << host << llendl;
  1384. U32 code = gMessageSystem->findCircuitCode( host );
  1385. // Don't need to do this, as we're removing the circuit info anyway - djs 01/28/03
  1386. // don't clean up 0 circuit code entries
  1387. // because many hosts (neighbor sims, etc) can have the 0 circuit
  1388. if (code)
  1389. {
  1390. //if (mCircuitCodes.checkKey(code))
  1391. code_session_map_t::iterator it = mCircuitCodes.find(code);
  1392. if(it != mCircuitCodes.end())
  1393. {
  1394. LL_INFOS("Messaging") << "Circuit " << code << " removed from list" << llendl;
  1395. //mCircuitCodes.removeData(code);
  1396. mCircuitCodes.erase(it);
  1397. }
  1398. U64 ip_port = 0;
  1399. std::map<U32, U64>::iterator iter = gMessageSystem->mCircuitCodeToIPPort.find(code);
  1400. if (iter != gMessageSystem->mCircuitCodeToIPPort.end())
  1401. {
  1402. ip_port = iter->second;
  1403. gMessageSystem->mCircuitCodeToIPPort.erase(iter);
  1404. U32 old_port = (U32)(ip_port & (U64)0xFFFFFFFF);
  1405. U32 old_ip = (U32)(ip_port >> 32);
  1406. LL_INFOS("Messaging") << "Host " << LLHost(old_ip, old_port) << " circuit " << code << " removed from lookup table" << llendl;
  1407. gMessageSystem->mIPPortToCircuitCode.erase(ip_port);
  1408. }
  1409. mCircuitInfo.removeCircuitData(host);
  1410. }
  1411. else
  1412. {
  1413. // Sigh, since we can open circuits which don't have circuit
  1414. // codes, it's possible for this to happen...
  1415. LL_WARNS("Messaging") << "Couldn't find circuit code for " << host << llendl;
  1416. }
  1417. }
  1418. void LLMessageSystem::setCircuitAllowTimeout(const LLHost &host, BOOL allow)
  1419. {
  1420. LLCircuitData *cdp = mCircuitInfo.findCircuit(host);
  1421. if (cdp)
  1422. {
  1423. cdp->setAllowTimeout(allow);
  1424. }
  1425. }
  1426. void LLMessageSystem::setCircuitTimeoutCallback(const LLHost &host, void (*callback_func)(const LLHost & host, void *user_data), void *user_data)
  1427. {
  1428. LLCircuitData *cdp = mCircuitInfo.findCircuit(host);
  1429. if (cdp)
  1430. {
  1431. cdp->setTimeoutCallback(callback_func, user_data);
  1432. }
  1433. }
  1434. BOOL LLMessageSystem::checkCircuitBlocked(const U32 circuit)
  1435. {
  1436. LLHost host = findHost(circuit);
  1437. if (!host.isOk())
  1438. {
  1439. LL_DEBUGS("Messaging") << "checkCircuitBlocked: Unknown circuit " << circuit << llendl;
  1440. return TRUE;
  1441. }
  1442. LLCircuitData *cdp = mCircuitInfo.findCircuit(host);
  1443. if (cdp)
  1444. {
  1445. return cdp->isBlocked();
  1446. }
  1447. else
  1448. {
  1449. LL_INFOS("Messaging") << "checkCircuitBlocked(circuit): Unknown host - " << host << llendl;
  1450. return FALSE;
  1451. }
  1452. }
  1453. BOOL LLMessageSystem::checkCircuitAlive(const U32 circuit)
  1454. {
  1455. LLHost host = findHost(circuit);
  1456. if (!host.isOk())
  1457. {
  1458. LL_DEBUGS("Messaging") << "checkCircuitAlive: Unknown circuit " << circuit << llendl;
  1459. return FALSE;
  1460. }
  1461. LLCircuitData *cdp = mCircuitInfo.findCircuit(host);
  1462. if (cdp)
  1463. {
  1464. return cdp->isAlive();
  1465. }
  1466. else
  1467. {
  1468. LL_INFOS("Messaging") << "checkCircuitAlive(circuit): Unknown host - " << host << llendl;
  1469. return FALSE;
  1470. }
  1471. }
  1472. BOOL LLMessageSystem::checkCircuitAlive(const LLHost &host)
  1473. {
  1474. LLCircuitData *cdp = mCircuitInfo.findCircuit(host);
  1475. if (cdp)
  1476. {
  1477. return cdp->isAlive();
  1478. }
  1479. else
  1480. {
  1481. LL_DEBUGS("Messaging") << "checkCircuitAlive(host): Unknown host - " << host << llendl;
  1482. return FALSE;
  1483. }
  1484. }
  1485. void LLMessageSystem::setCircuitProtection(BOOL b_protect)
  1486. {
  1487. mbProtected = b_protect;
  1488. }
  1489. U32 LLMessageSystem::findCircuitCode(const LLHost &host)
  1490. {
  1491. U64 ip64 = (U64) host.getAddress();
  1492. U64 port64 = (U64) host.getPort();
  1493. U64 ip_port = (ip64 << 32) | port64;
  1494. return get_if_there(mIPPortToCircuitCode, ip_port, U32(0));
  1495. }
  1496. LLHost LLMessageSystem::findHost(const U32 circuit_code)
  1497. {
  1498. if (mCircuitCodeToIPPort.count(circuit_code) > 0)
  1499. {
  1500. return LLHost(mCircuitCodeToIPPort[circuit_code]);
  1501. }
  1502. else
  1503. {
  1504. return LLHost::invalid;
  1505. }
  1506. }
  1507. void LLMessageSystem::setMaxMessageTime(const F32 seconds)
  1508. {
  1509. mMaxMessageTime = seconds;
  1510. }
  1511. void LLMessageSystem::setMaxMessageCounts(const S32 num)
  1512. {
  1513. mMaxMessageCounts = num;
  1514. }
  1515. std::ostream& operator<<(std::ostream& s, LLMessageSystem &msg)
  1516. {
  1517. U32 i;
  1518. if (msg.mbError)
  1519. {
  1520. s << "Message system not correctly initialized";
  1521. }
  1522. else
  1523. {
  1524. s << "Message system open on port " << msg.mPort << " and socket " << msg.mSocket << "\n";
  1525. // s << "Message template file " << msg.mName << " loaded\n";
  1526. s << "\nHigh frequency messages:\n";
  1527. for (i = 1; msg.mMessageNumbers[i] && (i < 255); i++)
  1528. {
  1529. s << *(msg.mMessageNumbers[i]);
  1530. }
  1531. s << "\nMedium frequency messages:\n";
  1532. for (i = (255 << 8) + 1; msg.mMessageNumbers[i] && (i < (255 << 8) + 255); i++)
  1533. {
  1534. s << *msg.mMessageNumbers[i];
  1535. }
  1536. s << "\nLow frequency messages:\n";
  1537. for (i = (0xFFFF0000) + 1; msg.mMessageNumbers[i] && (i < 0xFFFFFFFF); i++)
  1538. {
  1539. s << *msg.mMessageNumbers[i];
  1540. }
  1541. }
  1542. return s;
  1543. }
  1544. LLMessageSystem *gMessageSystem = NULL;
  1545. // update appropriate ping info
  1546. void process_complete_ping_check(LLMessageSystem *msgsystem, void** /*user_data*/)
  1547. {
  1548. U8 ping_id;
  1549. msgsystem->getU8Fast(_PREHASH_PingID, _PREHASH_PingID, ping_id);
  1550. LLCircuitData *cdp;
  1551. cdp = msgsystem->mCircuitInfo.findCircuit(msgsystem->getSender());
  1552. // stop the appropriate timer
  1553. if (cdp)
  1554. {
  1555. cdp->pingTimerStop(ping_id);
  1556. }
  1557. }
  1558. void process_start_ping_check(LLMessageSystem *msgsystem, void** /*user_data*/)
  1559. {
  1560. U8 ping_id;
  1561. msgsystem->getU8Fast(_PREHASH_PingID, _PREHASH_PingID, ping_id);
  1562. LLCircuitData *cdp;
  1563. cdp = msgsystem->mCircuitInfo.findCircuit(msgsystem->getSender());
  1564. if (cdp)
  1565. {
  1566. // Grab the packet id of the oldest unacked packet
  1567. U32 packet_id;
  1568. msgsystem->getU32Fast(_PREHASH_PingID, _PREHASH_OldestUnacked, packet_id);
  1569. cdp->clearDuplicateList(packet_id);
  1570. }
  1571. // Send off the response
  1572. msgsystem->newMessageFast(_PREHASH_CompletePingCheck);
  1573. msgsystem->nextBlockFast(_PREHASH_PingID);
  1574. msgsystem->addU8(_PREHASH_PingID, ping_id);
  1575. msgsystem->sendMessage(msgsystem->getSender());
  1576. }
  1577. // Note: this is currently unused. --mark
  1578. void open_circuit(LLMessageSystem *msgsystem, void** /*user_data*/)
  1579. {
  1580. U32 ip;
  1581. U16 port;
  1582. msgsystem->getIPAddrFast(_PREHASH_CircuitInfo, _PREHASH_IP, ip);
  1583. msgsystem->getIPPortFast(_PREHASH_CircuitInfo, _PREHASH_Port, port);
  1584. // By default, OpenCircuit's are untrusted
  1585. msgsystem->enableCircuit(LLHost(ip, port), FALSE);
  1586. }
  1587. void close_circuit(LLMessageSystem *msgsystem, void** /*user_data*/)
  1588. {
  1589. msgsystem->disableCircuit(msgsystem->getSender());
  1590. }
  1591. // static
  1592. /*
  1593. void LLMessageSystem::processAssignCircuitCode(LLMessageSystem* msg, void**)
  1594. {
  1595. // if we already have a circuit code, we can bail
  1596. if(msg->mOurCircuitCode) return;
  1597. LLUUID session_id;
  1598. msg->getUUIDFast(_PREHASH_CircuitCode, _PREHASH_SessionID, session_id);
  1599. if(session_id != msg->getMySessionID())
  1600. {
  1601. LL_WARNS("Messaging") << "AssignCircuitCode, bad session id. Expecting "
  1602. << msg->getMySessionID() << " but got " << session_id
  1603. << llendl;
  1604. return;
  1605. }
  1606. U32 code;
  1607. msg->getU32Fast(_PREHASH_CircuitCode, _PREHASH_Code, code);
  1608. if (!code)
  1609. {
  1610. LL_ERRS("Messaging") << "Assigning circuit code of zero!" << llendl;
  1611. }
  1612. msg->mOurCircuitCode = code;
  1613. LL_INFOS("Messaging") << "Circuit code " << code << " assigned." << llendl;
  1614. }
  1615. */
  1616. // static
  1617. void LLMessageSystem::processAddCircuitCode(LLMessageSystem* msg, void**)
  1618. {
  1619. U32 code;
  1620. msg->getU32Fast(_PREHASH_CircuitCode, _PREHASH_Code, code);
  1621. LLUUID session_id;
  1622. msg->getUUIDFast(_PREHASH_CircuitCode, _PREHASH_SessionID, session_id);
  1623. (void)msg->addCircuitCode(code, session_id);
  1624. // Send the ack back
  1625. //msg->newMessageFast(_PREHASH_AckAddCircuitCode);
  1626. //msg->nextBlockFast(_PREHASH_CircuitCode);
  1627. //msg->addU32Fast(_PREHASH_Code, code);
  1628. //msg->sendMessage(msg->getSender());
  1629. }
  1630. bool LLMessageSystem::addCircuitCode(U32 code, const LLUUID& session_id)
  1631. {
  1632. if(!code)
  1633. {
  1634. LL_WARNS("Messaging") << "addCircuitCode: zero circuit code" << llendl;
  1635. return false;
  1636. }
  1637. code_session_map_t::iterator it = mCircuitCodes.find(code);
  1638. if(it == mCircuitCodes.end())
  1639. {
  1640. LL_INFOS("Messaging") << "New circuit code " << code << " added" << llendl;
  1641. //msg->mCircuitCodes[circuit_code] = circuit_code;
  1642. mCircuitCodes.insert(code_session_map_t::value_type(code, session_id));
  1643. }
  1644. else
  1645. {
  1646. LL_INFOS("Messaging") << "Duplicate circuit code " << code << " added" << llendl;
  1647. }
  1648. return true;
  1649. }
  1650. //void ack_add_circuit_code(LLMessageSystem *msgsystem, void** /*user_data*/)
  1651. //{
  1652. // By default, we do nothing. This particular message is only handled by the spaceserver
  1653. //}
  1654. // static
  1655. void LLMessageSystem::processUseCircuitCode(LLMessageSystem* msg,
  1656. void** user)
  1657. {
  1658. U32 circuit_code_in;
  1659. msg->getU32Fast(_PREHASH_CircuitCode, _PREHASH_Code, circuit_code_in);
  1660. U32 ip = msg->getSenderIP();
  1661. U32 port = msg->getSenderPort();
  1662. U64 ip64 = ip;
  1663. U64 port64 = port;
  1664. U64 ip_port_in = (ip64 << 32) | port64;
  1665. if (circuit_code_in)
  1666. {
  1667. //if (!msg->mCircuitCodes.checkKey(circuit_code_in))
  1668. code_session_map_t::iterator it;
  1669. it = msg->mCircuitCodes.find(circuit_code_in);
  1670. if(it == msg->mCircuitCodes.end())
  1671. {
  1672. // Whoah, abort! We don't know anything about this circuit code.
  1673. LL_WARNS("Messaging") << "UseCircuitCode for " << circuit_code_in
  1674. << " received without AddCircuitCode message - aborting"
  1675. << llendl;
  1676. return;
  1677. }
  1678. LLUUID id;
  1679. msg->getUUIDFast(_PREHASH_CircuitCode, _PREHASH_ID, id);
  1680. LLUUID session_id;
  1681. msg->getUUIDFast(_PREHASH_CircuitCode, _PREHASH_SessionID, session_id);
  1682. if(session_id != (*it).second)
  1683. {
  1684. LL_WARNS("Messaging") << "UseCircuitCode unmatched session id. Got "
  1685. << session_id << " but expected " << (*it).second
  1686. << llendl;
  1687. return;
  1688. }
  1689. // Clean up previous references to this ip/port or circuit
  1690. U64 ip_port_old = get_if_there(msg->mCircuitCodeToIPPort, circuit_code_in, U64(0));
  1691. U32 circuit_code_old = get_if_there(msg->mIPPortToCircuitCode, ip_port_in, U32(0));
  1692. if (ip_port_old)
  1693. {
  1694. if ((ip_port_old == ip_port_in) && (circuit_code_old == circuit_code_in))
  1695. {
  1696. // Current information is the same as incoming info, ignore
  1697. LL_INFOS("Messaging") << "Got duplicate UseCircuitCode for circuit " << circuit_code_in << " to " << msg->getSender() << llendl;
  1698. return;
  1699. }
  1700. // Hmm, got a different IP and port for the same circuit code.
  1701. U32 circut_code_old_ip_port = get_if_there(msg->mIPPortToCircuitCode, ip_port_old, U32(0));
  1702. msg->mCircuitCodeToIPPort.erase(circut_code_old_ip_port);
  1703. msg->mIPPortToCircuitCode.erase(ip_port_old);
  1704. U32 old_port = (U32)(ip_port_old & (U64)0xFFFFFFFF);
  1705. U32 old_ip = (U32)(ip_port_old >> 32);
  1706. LL_INFOS("Messaging") << "Removing derelict lookup entry for circuit " << circuit_code_old << " to " << LLHost(old_ip, old_port) << llendl;
  1707. }
  1708. if (circuit_code_old)
  1709. {
  1710. LLHost cur_host(ip, port);
  1711. LL_WARNS("Messaging") << "Disabling existing circuit for " << cur_host << llendl;
  1712. msg->disableCircuit(cur_host);
  1713. if (circuit_code_old == circuit_code_in)
  1714. {
  1715. LL_WARNS("Messaging") << "Asymmetrical circuit to ip/port lookup!" << llendl;
  1716. LL_WARNS("Messaging") << "Multiple circuit codes for " << cur_host << " probably!" << llendl;
  1717. LL_WARNS("Messaging") << "Permanently disabling circuit" << llendl;
  1718. return;
  1719. }
  1720. else
  1721. {
  1722. LL_WARNS("Messaging") << "Circuit code changed for " << msg->getSender()
  1723. << " from " << circuit_code_old << " to "
  1724. << circuit_code_in << llendl;
  1725. }
  1726. }
  1727. // Since this comes from the viewer, it's untrusted, but it
  1728. // passed the circuit code and session id check, so we will go
  1729. // ahead and persist the ID associated.
  1730. LLCircuitData *cdp = msg->mCircuitInfo.findCircuit(msg->getSender());
  1731. BOOL had_circuit_already = cdp ? TRUE : FALSE;
  1732. msg->enableCircuit(msg->getSender(), FALSE);
  1733. cdp = msg->mCircuitInfo.findCircuit(msg->getSender());
  1734. if(cdp)
  1735. {
  1736. cdp->setRemoteID(id);
  1737. cdp->setRemoteSessionID(session_id);
  1738. }
  1739. if (!had_circuit_already)
  1740. {
  1741. //
  1742. // HACK HACK HACK HACK HACK!
  1743. //
  1744. // This would NORMALLY happen inside logValidMsg, but at the point that this happens
  1745. // inside logValidMsg, there's no circuit for this message yet. So the awful thing that
  1746. // we do here is do it inside this message handler immediately AFTER the message is
  1747. // handled.
  1748. //
  1749. // We COULD not do this, but then what happens is that some of the circuit bookkeeping
  1750. // gets broken, especially the packets in count. That causes some later packets to flush
  1751. // the RecentlyReceivedReliable list, resulting in an error in which UseCircuitCode
  1752. // doesn't get properly duplicate suppressed. Not a BIG deal, but it's somewhat confusing
  1753. // (and bad from a state point of view). DJS 9/23/04
  1754. //
  1755. cdp->checkPacketInID(gMessageSystem->mCurrentRecvPacketID, FALSE ); // Since this is the first message on the circuit, by definition it's not resent.
  1756. }
  1757. msg->mIPPortToCircuitCode[ip_port_in] = circuit_code_in;
  1758. msg->mCircuitCodeToIPPort[circuit_code_in] = ip_port_in;
  1759. LL_INFOS("Messaging") << "Circuit code " << circuit_code_in << " from "
  1760. << msg->getSender() << " for agent " << id << " in session "
  1761. << session_id << llendl;
  1762. const LLUseCircuitCodeResponder* responder =
  1763. (const LLUseCircuitCodeResponder*) user;
  1764. if(responder)
  1765. {
  1766. responder->complete(msg->getSender(), id);
  1767. }
  1768. }
  1769. else
  1770. {
  1771. LL_WARNS("Messaging") << "Got zero circuit code in use_circuit_code" << llendl;
  1772. }
  1773. }
  1774. // static
  1775. void LLMessageSystem::processError(LLMessageSystem* msg, void**)
  1776. {
  1777. S32 error_code = 0;
  1778. msg->getS32("Data", "Code", error_code);
  1779. std::string error_token;
  1780. msg->getString("Data", "Token", error_token);
  1781. LLUUID error_id;
  1782. msg->getUUID("Data", "ID", error_id);
  1783. std::string error_system;
  1784. msg->getString("Data", "System", error_system);
  1785. std::string error_message;
  1786. msg->getString("Data", "Message", error_message);
  1787. LL_WARNS("Messaging") << "Message error from " << msg->getSender() << " - "
  1788. << error_code << " " << error_token << " " << error_id << " \""
  1789. << error_system << "\" \"" << error_message << "\"" << llendl;
  1790. }
  1791. static LLHTTPNode& messageRootNode()
  1792. {
  1793. static LLHTTPNode root_node;
  1794. static bool initialized = false;
  1795. if (!initialized) {
  1796. initialized = true;
  1797. LLHTTPRegistrar::buildAllServices(root_node);
  1798. }
  1799. return root_node;
  1800. }
  1801. //static
  1802. void LLMessageSystem::dispatch(
  1803. const std::string& msg_name,
  1804. const LLSD& message)
  1805. {
  1806. LLPointer<LLSimpleResponse> responsep = LLSimpleResponse::create();
  1807. dispatch(msg_name, message, responsep);
  1808. }
  1809. //static
  1810. void LLMessageSystem::dispatch(
  1811. const std::string& msg_name,
  1812. const LLSD& message,
  1813. LLHTTPNode::ResponsePtr responsep)
  1814. {
  1815. if ((gMessageSystem->mMessageTemplates.find
  1816. (LLMessageStringTable::getInstance()->getString(msg_name.c_str())) ==
  1817. gMessageSystem->mMessageTemplates.end()) &&
  1818. !LLMessageConfig::isValidMessage(msg_name))
  1819. {
  1820. LL_WARNS("Messaging") << "Ignoring unknown message " << msg_name << llendl;
  1821. responsep->notFound("Invalid message name");
  1822. return;
  1823. }
  1824. std::string path = "/message/" + msg_name;
  1825. LLSD context;
  1826. const LLHTTPNode* handler = messageRootNode().traverse(path, context);
  1827. if (!handler)
  1828. {
  1829. LL_WARNS("Messaging") << "LLMessageService::dispatch > no handler for "
  1830. << path << llendl;
  1831. return;
  1832. }
  1833. // enable this for output of message names
  1834. //LL_INFOS("Messaging") << "< \"" << msg_name << "\"" << llendl;
  1835. //lldebugs << "data: " << LLSDNotationStreamer(message) << llendl;
  1836. handler->post(responsep, context, message);
  1837. }
  1838. //static
  1839. void LLMessageSystem::dispatchTemplate(const std::string& msg_name,
  1840. const LLSD& message,
  1841. LLHTTPNode::ResponsePtr responsep)
  1842. {
  1843. LLTemplateMessageDispatcher dispatcher(*(gMessageSystem->mTemplateMessageReader));
  1844. dispatcher.dispatch(msg_name, message, responsep);
  1845. }
  1846. static void check_for_unrecognized_messages(
  1847. const char* type,
  1848. const LLSD& map,
  1849. LLMessageSystem::message_template_name_map_t& templates)
  1850. {
  1851. for (LLSD::map_const_iterator iter = map.beginMap(),
  1852. end = map.endMap();
  1853. iter != end; ++iter)
  1854. {
  1855. const char* name = LLMessageStringTable::getInstance()->getString(iter->first.c_str());
  1856. if (templates.find(name) == templates.end())
  1857. {
  1858. LL_INFOS("AppInit") << " " << type
  1859. << " ban list contains unrecognized message "
  1860. << name << LL_ENDL;
  1861. }
  1862. }
  1863. }
  1864. void LLMessageSystem::setMessageBans(
  1865. const LLSD& trusted, const LLSD& untrusted)
  1866. {
  1867. LL_DEBUGS("AppInit") << "LLMessageSystem::setMessageBans:" << LL_ENDL;
  1868. bool any_set = false;
  1869. for (message_template_name_map_t::iterator iter = mMessageTemplates.begin(),
  1870. end = mMessageTemplates.end();
  1871. iter != end; ++iter)
  1872. {
  1873. LLMessageTemplate* mt = iter->second;
  1874. std::string name(mt->mName);
  1875. bool ban_from_trusted
  1876. = trusted.has(name) && trusted.get(name).asBoolean();
  1877. bool ban_from_untrusted
  1878. = untrusted.has(name) && untrusted.get(name).asBoolean();
  1879. mt->mBanFromTrusted = ban_from_trusted;
  1880. mt->mBanFromUntrusted = ban_from_untrusted;
  1881. if (ban_from_trusted || ban_from_untrusted)
  1882. {
  1883. LL_INFOS("AppInit") << " " << name << " banned from "
  1884. << (ban_from_trusted ? "TRUSTED " : " ")
  1885. << (ban_from_untrusted ? "UNTRUSTED " : " ")
  1886. << LL_ENDL;
  1887. any_set = true;
  1888. }
  1889. }
  1890. if (!any_set)
  1891. {
  1892. LL_DEBUGS("AppInit") << " no messages banned" << LL_ENDL;
  1893. }
  1894. check_for_unrecognized_messages("trusted", trusted, mMessageTemplates);
  1895. check_for_unrecognized_messages("untrusted", untrusted, mMessageTemplates);
  1896. }
  1897. S32 LLMessageSystem::sendError(
  1898. const LLHost& host,
  1899. const LLUUID& agent_id,
  1900. S32 code,
  1901. const std::string& token,
  1902. const LLUUID& id,
  1903. const std::string& system,
  1904. const std::string& message,
  1905. const LLSD& data)
  1906. {
  1907. newMessage("Error");
  1908. nextBlockFast(_PREHASH_AgentData);
  1909. addUUIDFast(_PREHASH_AgentID, agent_id);
  1910. nextBlockFast(_PREHASH_Data);
  1911. addS32("Code", code);
  1912. addString("Token", token);
  1913. addUUID("ID", id);
  1914. addString("System", system);
  1915. std::string temp;
  1916. temp = message;
  1917. if(temp.size() > (size_t)MTUBYTES) temp.resize((size_t)MTUBYTES);
  1918. addString("Message", message);
  1919. LLPointer<LLSDBinaryFormatter> formatter = new LLSDBinaryFormatter;
  1920. std::ostringstream ostr;
  1921. formatter->format(data, ostr);
  1922. temp = ostr.str();
  1923. bool pack_data = true;
  1924. static const std::string ERROR_MESSAGE_NAME("Error");
  1925. if (LLMessageConfig::getMessageFlavor(ERROR_MESSAGE_NAME) ==
  1926. LLMessageConfig::TEMPLATE_FLAVOR)
  1927. {
  1928. S32 msg_size = temp.size() + mMessageBuilder->getMessageSize();
  1929. if(msg_size >= ETHERNET_MTU_BYTES)
  1930. {
  1931. pack_data = false;
  1932. }
  1933. }
  1934. if(pack_data)
  1935. {
  1936. addBinaryData("Data", (void*)temp.c_str(), temp.size());
  1937. }
  1938. else
  1939. {
  1940. LL_WARNS("Messaging") << "Data and message were too large -- data removed."
  1941. << llendl;
  1942. addBinaryData("Data", NULL, 0);
  1943. }
  1944. return sendReliable(host);
  1945. }
  1946. void process_packet_ack(LLMessageSystem *msgsystem, void** /*user_data*/)
  1947. {
  1948. TPACKETID packet_id;
  1949. LLHost host = msgsystem->getSender();
  1950. LLCircuitData *cdp = msgsystem->mCircuitInfo.findCircuit(host);
  1951. if (cdp)
  1952. {
  1953. S32 ack_count = msgsystem->getNumberOfBlocksFast(_PREHASH_Packets);
  1954. for (S32 i = 0; i < ack_count; i++)
  1955. {
  1956. msgsystem->getU32Fast(_PREHASH_Packets, _PREHASH_ID, packet_id, i);
  1957. // LL_DEBUGS("Messaging") << "ack recvd' from " << host << " for packet " << (TPACKETID)packet_id << llendl;
  1958. cdp->ackReliablePacket(packet_id);
  1959. }
  1960. if (!cdp->getUnackedPacketCount())
  1961. {
  1962. // Remove this circuit from the list of circuits with unacked packets
  1963. gMessageSystem->mCircuitInfo.mUnackedCircuitMap.erase(host);
  1964. }
  1965. }
  1966. }
  1967. /*
  1968. void process_log_messages(LLMessageSystem* msg, void**)
  1969. {
  1970. U8 log_message;
  1971. msg->getU8Fast(_PREHASH_Options, _PREHASH_Enable, log_message);
  1972. if (log_message)
  1973. {
  1974. LL_INFOS("Messaging") << "Starting logging via message" << llendl;
  1975. msg->startLogging();
  1976. }
  1977. else
  1978. {
  1979. LL_INFOS("Messaging") << "Stopping logging via message" << llendl;
  1980. msg->stopLogging();
  1981. }
  1982. }*/
  1983. // Make circuit trusted if the MD5 Digest matches, otherwise
  1984. // notify remote end that they are not trusted.
  1985. void process_create_trusted_circuit(LLMessageSystem *msg, void **)
  1986. {
  1987. // don't try to create trust on machines with no shared secret
  1988. std::string shared_secret = get_shared_secret();
  1989. if(shared_secret.empty()) return;
  1990. LLUUID remote_id;
  1991. msg->getUUIDFast(_PREHASH_DataBlock, _PREHASH_EndPointID, remote_id);
  1992. LLCircuitData *cdp = msg->mCircuitInfo.findCircuit(msg->getSender());
  1993. if (!cdp)
  1994. {
  1995. LL_WARNS("Messaging") << "Attempt to create trusted circuit without circuit data: "
  1996. << msg->getSender() << llendl;
  1997. return;
  1998. }
  1999. LLUUID local_id;
  2000. local_id = cdp->getLocalEndPointID();
  2001. if (remote_id == local_id)
  2002. {
  2003. // Don't respond to requests that use the same end point ID
  2004. return;
  2005. }
  2006. U32 untrusted_interface = msg->getUntrustedInterface().getAddress();
  2007. U32 last_interface = msg->getReceivingInterface().getAddress();
  2008. if ( ( untrusted_interface != INVALID_HOST_IP_ADDRESS ) && ( untrusted_interface == last_interface ) )
  2009. {
  2010. if( msg->getBlockUntrustedInterface() )
  2011. {
  2012. LL_WARNS("Messaging") << "Ignoring CreateTrustedCircuit on public interface from host: "
  2013. << msg->getSender() << llendl;
  2014. return;
  2015. }
  2016. else
  2017. {
  2018. LL_WARNS("Messaging") << "Processing CreateTrustedCircuit on public interface from host: "
  2019. << msg->getSender() << llendl;
  2020. }
  2021. }
  2022. char their_digest[MD5HEX_STR_SIZE]; /* Flawfinder: ignore */
  2023. S32 size = msg->getSizeFast(_PREHASH_DataBlock, _PREHASH_Digest);
  2024. if(size != MD5HEX_STR_BYTES)
  2025. {
  2026. // ignore requests which pack the wrong amount of data.
  2027. return;
  2028. }
  2029. msg->getBinaryDataFast(_PREHASH_DataBlock, _PREHASH_Digest, their_digest, MD5HEX_STR_BYTES);
  2030. their_digest[MD5HEX_STR_SIZE - 1] = '\0';
  2031. if(msg->isMatchingDigestForWindowAndUUIDs(their_digest, TRUST_TIME_WINDOW, local_id, remote_id))
  2032. {
  2033. cdp->setTrusted(TRUE);
  2034. LL_INFOS("Messaging") << "Trusted digest from " << msg->getSender() << llendl;
  2035. return;
  2036. }
  2037. else if (cdp->getTrusted())
  2038. {
  2039. // The digest is bad, but this circuit is already trusted.
  2040. // This means that this could just be the result of a stale deny sent from a while back, and
  2041. // the message system is being slow. Don't bother sending the deny, as it may continually
  2042. // ping-pong back and forth on a very hosed circuit.
  2043. LL_WARNS("Messaging") << "Ignoring bad digest from known trusted circuit: " << their_digest
  2044. << " host: " << msg->getSender() << llendl;
  2045. return;
  2046. }
  2047. else
  2048. {
  2049. LL_WARNS("Messaging") << "Bad digest from known circuit: " << their_digest
  2050. << " host: " << msg->getSender() << llendl;
  2051. msg->sendDenyTrustedCircuit(msg->getSender());
  2052. return;
  2053. }
  2054. }
  2055. void process_deny_trusted_circuit(LLMessageSystem *msg, void **)
  2056. {
  2057. // don't try to create trust on machines with no shared secret
  2058. std::string shared_secret = get_shared_secret();
  2059. if(shared_secret.empty()) return;
  2060. LLUUID remote_id;
  2061. msg->getUUIDFast(_PREHASH_DataBlock, _PREHASH_EndPointID, remote_id);
  2062. LLCircuitData *cdp = msg->mCircuitInfo.findCircuit(msg->getSender());
  2063. if (!cdp)
  2064. {
  2065. return;
  2066. }
  2067. LLUUID local_id;
  2068. local_id = cdp->getLocalEndPointID();
  2069. if (remote_id == local_id)
  2070. {
  2071. // Don't respond to requests that use the same end point ID
  2072. return;
  2073. }
  2074. U32 untrusted_interface = msg->getUntrustedInterface().getAddress();
  2075. U32 last_interface = msg->getReceivingInterface().getAddress();
  2076. if ( ( untrusted_interface != INVALID_HOST_IP_ADDRESS ) && ( untrusted_interface == last_interface ) )
  2077. {
  2078. if( msg->getBlockUntrustedInterface() )
  2079. {
  2080. LL_WARNS("Messaging") << "Ignoring DenyTrustedCircuit on public interface from host: "
  2081. << msg->getSender() << llendl;
  2082. return;
  2083. }
  2084. else
  2085. {
  2086. LL_WARNS("Messaging") << "Processing DenyTrustedCircuit on public interface from host: "
  2087. << msg->getSender() << llendl;
  2088. }
  2089. }
  2090. // Assume that we require trust to proceed, so resend.
  2091. // This catches the case where a circuit that was trusted
  2092. // times out, and allows us to re-establish it, but does
  2093. // mean that if our shared_secret or clock is wrong, we'll
  2094. // spin.
  2095. // *TODO: probably should keep a count of number of resends
  2096. // per circuit, and stop resending after a whi