PageRenderTime 83ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmessage/message.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 1055 lines | 673 code | 184 blank | 198 comment | 20 complexity | e65720b5f26239bd090faf059646f40b MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file message.h
  3. * @brief LLMessageSystem class header file
  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. #ifndef LL_MESSAGE_H
  27. #define LL_MESSAGE_H
  28. #include <cstring>
  29. #include <set>
  30. #if LL_LINUX
  31. #include <endian.h>
  32. #include <netinet/in.h>
  33. #endif
  34. #if LL_SOLARIS
  35. #include <netinet/in.h>
  36. #endif
  37. #if LL_WINDOWS
  38. #include "winsock2.h" // htons etc.
  39. #endif
  40. #include "llerror.h"
  41. #include "net.h"
  42. #include "string_table.h"
  43. #include "llcircuit.h"
  44. #include "lltimer.h"
  45. #include "llpacketring.h"
  46. #include "llhost.h"
  47. #include "llhttpclient.h"
  48. #include "llhttpnode.h"
  49. #include "llpacketack.h"
  50. #include "llsingleton.h"
  51. #include "message_prehash.h"
  52. #include "llstl.h"
  53. #include "llmsgvariabletype.h"
  54. #include "llmessagesenderinterface.h"
  55. #include "llstoredmessage.h"
  56. const U32 MESSAGE_MAX_STRINGS_LENGTH = 64;
  57. const U32 MESSAGE_NUMBER_OF_HASH_BUCKETS = 8192;
  58. const S32 MESSAGE_MAX_PER_FRAME = 400;
  59. class LLMessageStringTable : public LLSingleton<LLMessageStringTable>
  60. {
  61. public:
  62. LLMessageStringTable();
  63. ~LLMessageStringTable();
  64. char *getString(const char *str);
  65. U32 mUsed;
  66. BOOL mEmpty[MESSAGE_NUMBER_OF_HASH_BUCKETS];
  67. char mString[MESSAGE_NUMBER_OF_HASH_BUCKETS][MESSAGE_MAX_STRINGS_LENGTH]; /* Flawfinder: ignore */
  68. };
  69. // Individual Messages are described with the following format
  70. // Note that to ease parsing, keywords are used
  71. //
  72. // // Comment (Comment like a C++ single line comment)
  73. // Comments can only be placed between Messages
  74. // {
  75. // MessageName (same naming restrictions as C variable)
  76. // Frequency ("High", "Medium", or "Low" - determines whether message ID is 8, 16, or 32-bits --
  77. // there can 254 messages in the first 2 groups, 32K in the last group)
  78. // (A message can be made up only of the Name if it is only a signal)
  79. // Trust ("Trusted", "NotTrusted" - determines if a message will be accepted
  80. // on a circuit. "Trusted" messages are not accepted from NotTrusted circuits
  81. // while NotTrusted messages are accepted on any circuit. An example of a
  82. // NotTrusted circuit is any circuit from the viewer.)
  83. // Encoding ("Zerocoded", "Unencoded" - zerocoded messages attempt to compress sequences of
  84. // zeros, but if there is no space win, it discards the compression and goes unencoded)
  85. // {
  86. // Block Name (same naming restrictions as C variable)
  87. // Block Type ("Single", "Multiple", or "Variable" - determines if the block is coded once,
  88. // a known number of times, or has a 8 bit argument encoded to tell the decoder
  89. // how many times the group is repeated)
  90. // Block Repeat Number (Optional - used only with the "Multiple" type - tells how many times the field is repeated
  91. // {
  92. // Variable 1 Name (same naming restrictions as C variable)
  93. // Variable Type ("Fixed" or "Variable" - determines if the variable is of fixed size or needs to
  94. // encode an argument describing the size in bytes)
  95. // Variable Size (In bytes, either of the "Fixed" variable itself or of the size argument)
  96. //
  97. // repeat variables
  98. //
  99. // }
  100. //
  101. // Repeat for number of variables in block
  102. // }
  103. //
  104. // Repeat for number of blocks in message
  105. // }
  106. // Repeat for number of messages in file
  107. //
  108. // Constants
  109. const S32 MAX_MESSAGE_INTERNAL_NAME_SIZE = 255;
  110. const S32 MAX_BUFFER_SIZE = NET_BUFFER_SIZE;
  111. const S32 MAX_BLOCKS = 255;
  112. const U8 LL_ZERO_CODE_FLAG = 0x80;
  113. const U8 LL_RELIABLE_FLAG = 0x40;
  114. const U8 LL_RESENT_FLAG = 0x20;
  115. const U8 LL_ACK_FLAG = 0x10;
  116. // 1 byte flags, 4 bytes sequence, 1 byte offset + 1 byte message name (high)
  117. const S32 LL_MINIMUM_VALID_PACKET_SIZE = LL_PACKET_ID_SIZE + 1;
  118. enum EPacketHeaderLayout
  119. {
  120. PHL_FLAGS = 0,
  121. PHL_PACKET_ID = 1,
  122. PHL_OFFSET = 5,
  123. PHL_NAME = 6
  124. };
  125. const S32 LL_DEFAULT_RELIABLE_RETRIES = 3;
  126. const F32 LL_MINIMUM_RELIABLE_TIMEOUT_SECONDS = 1.f;
  127. const F32 LL_MINIMUM_SEMIRELIABLE_TIMEOUT_SECONDS = 1.f;
  128. const F32 LL_PING_BASED_TIMEOUT_DUMMY = 0.0f;
  129. // *NOTE: Maybe these factors shouldn't include the msec to sec conversion
  130. // implicitly.
  131. // However, all units should be MKS.
  132. const F32 LL_SEMIRELIABLE_TIMEOUT_FACTOR = 5.f / 1000.f; // factor * averaged ping
  133. const F32 LL_RELIABLE_TIMEOUT_FACTOR = 5.f / 1000.f; // factor * averaged ping
  134. const F32 LL_FILE_XFER_TIMEOUT_FACTOR = 5.f / 1000.f; // factor * averaged ping
  135. const F32 LL_LOST_TIMEOUT_FACTOR = 16.f / 1000.f; // factor * averaged ping for marking packets "Lost"
  136. const F32 LL_MAX_LOST_TIMEOUT = 5.f; // Maximum amount of time before considering something "lost"
  137. const S32 MAX_MESSAGE_COUNT_NUM = 1024;
  138. // Forward declarations
  139. class LLCircuit;
  140. class LLVector3;
  141. class LLVector4;
  142. class LLVector3d;
  143. class LLQuaternion;
  144. class LLSD;
  145. class LLUUID;
  146. class LLMessageSystem;
  147. class LLPumpIO;
  148. // message system exceptional condition handlers.
  149. enum EMessageException
  150. {
  151. MX_UNREGISTERED_MESSAGE, // message number not part of template
  152. MX_PACKET_TOO_SHORT, // invalid packet, shorter than minimum packet size
  153. MX_RAN_OFF_END_OF_PACKET, // ran off the end of the packet during decode
  154. MX_WROTE_PAST_BUFFER_SIZE // wrote past buffer size in zero code expand
  155. };
  156. typedef void (*msg_exception_callback)(LLMessageSystem*,void*,EMessageException);
  157. // message data pieces are used to collect the data called for by the message template
  158. class LLMsgData;
  159. class LLMsgBlkData;
  160. class LLMessageTemplate;
  161. class LLMessagePollInfo;
  162. class LLMessageBuilder;
  163. class LLTemplateMessageBuilder;
  164. class LLSDMessageBuilder;
  165. class LLMessageReader;
  166. class LLTemplateMessageReader;
  167. class LLSDMessageReader;
  168. class LLUseCircuitCodeResponder
  169. {
  170. LOG_CLASS(LLMessageSystem);
  171. public:
  172. virtual ~LLUseCircuitCodeResponder();
  173. virtual void complete(const LLHost& host, const LLUUID& agent) const = 0;
  174. };
  175. class LLMessageSystem : public LLMessageSenderInterface
  176. {
  177. private:
  178. U8 mSendBuffer[MAX_BUFFER_SIZE];
  179. S32 mSendSize;
  180. bool mBlockUntrustedInterface;
  181. LLHost mUntrustedInterface;
  182. public:
  183. LLPacketRing mPacketRing;
  184. LLReliablePacketParams mReliablePacketParams;
  185. // Set this flag to TRUE when you want *very* verbose logs.
  186. BOOL mVerboseLog;
  187. F32 mMessageFileVersionNumber;
  188. typedef std::map<const char *, LLMessageTemplate*> message_template_name_map_t;
  189. typedef std::map<U32, LLMessageTemplate*> message_template_number_map_t;
  190. private:
  191. message_template_name_map_t mMessageTemplates;
  192. message_template_number_map_t mMessageNumbers;
  193. public:
  194. S32 mSystemVersionMajor;
  195. S32 mSystemVersionMinor;
  196. S32 mSystemVersionPatch;
  197. S32 mSystemVersionServer;
  198. U32 mVersionFlags;
  199. BOOL mbProtected;
  200. U32 mNumberHighFreqMessages;
  201. U32 mNumberMediumFreqMessages;
  202. U32 mNumberLowFreqMessages;
  203. S32 mPort;
  204. S32 mSocket;
  205. U32 mPacketsIn; // total packets in, including compressed and uncompressed
  206. U32 mPacketsOut; // total packets out, including compressed and uncompressed
  207. U64 mBytesIn; // total bytes in, including compressed and uncompressed
  208. U64 mBytesOut; // total bytes out, including compressed and uncompressed
  209. U32 mCompressedPacketsIn; // total compressed packets in
  210. U32 mCompressedPacketsOut; // total compressed packets out
  211. U32 mReliablePacketsIn; // total reliable packets in
  212. U32 mReliablePacketsOut; // total reliable packets out
  213. U32 mDroppedPackets; // total dropped packets in
  214. U32 mResentPackets; // total resent packets out
  215. U32 mFailedResendPackets; // total resend failure packets out
  216. U32 mOffCircuitPackets; // total # of off-circuit packets rejected
  217. U32 mInvalidOnCircuitPackets; // total # of on-circuit but invalid packets rejected
  218. S64 mUncompressedBytesIn; // total uncompressed size of compressed packets in
  219. S64 mUncompressedBytesOut; // total uncompressed size of compressed packets out
  220. S64 mCompressedBytesIn; // total compressed size of compressed packets in
  221. S64 mCompressedBytesOut; // total compressed size of compressed packets out
  222. S64 mTotalBytesIn; // total size of all uncompressed packets in
  223. S64 mTotalBytesOut; // total size of all uncompressed packets out
  224. BOOL mSendReliable; // does the outgoing message require a pos ack?
  225. LLCircuit mCircuitInfo;
  226. F64 mCircuitPrintTime; // used to print circuit debug info every couple minutes
  227. F32 mCircuitPrintFreq; // seconds
  228. std::map<U64, U32> mIPPortToCircuitCode;
  229. std::map<U32, U64> mCircuitCodeToIPPort;
  230. U32 mOurCircuitCode;
  231. S32 mSendPacketFailureCount;
  232. S32 mUnackedListDepth;
  233. S32 mUnackedListSize;
  234. S32 mDSMaxListDepth;
  235. public:
  236. // Read file and build message templates
  237. LLMessageSystem(const std::string& filename, U32 port, S32 version_major,
  238. S32 version_minor, S32 version_patch,
  239. bool failure_is_fatal,
  240. const F32 circuit_heartbeat_interval, const F32 circuit_timeout);
  241. ~LLMessageSystem();
  242. BOOL isOK() const { return !mbError; }
  243. S32 getErrorCode() const { return mErrorCode; }
  244. // Read file and build message templates filename must point to a
  245. // valid string which specifies the path of a valid linden
  246. // template.
  247. void loadTemplateFile(const std::string& filename, bool failure_is_fatal);
  248. // methods for building, sending, receiving, and handling messages
  249. void setHandlerFuncFast(const char *name, void (*handler_func)(LLMessageSystem *msgsystem, void **user_data), void **user_data = NULL);
  250. void setHandlerFunc(const char *name, void (*handler_func)(LLMessageSystem *msgsystem, void **user_data), void **user_data = NULL)
  251. {
  252. setHandlerFuncFast(LLMessageStringTable::getInstance()->getString(name), handler_func, user_data);
  253. }
  254. // Set a callback function for a message system exception.
  255. void setExceptionFunc(EMessageException exception, msg_exception_callback func, void* data = NULL);
  256. // Call the specified exception func, and return TRUE if a
  257. // function was found and called. Otherwise return FALSE.
  258. BOOL callExceptionFunc(EMessageException exception);
  259. // Set a function that will be called once per packet processed with the
  260. // hashed message name and the time spent in the processing handler function
  261. // measured in seconds. JC
  262. typedef void (*msg_timing_callback)(const char* hashed_name, F32 time, void* data);
  263. void setTimingFunc(msg_timing_callback func, void* data = NULL);
  264. msg_timing_callback getTimingCallback()
  265. {
  266. return mTimingCallback;
  267. }
  268. void* getTimingCallbackData()
  269. {
  270. return mTimingCallbackData;
  271. }
  272. // This method returns true if the code is in the circuit codes map.
  273. BOOL isCircuitCodeKnown(U32 code) const;
  274. // usually called in response to an AddCircuitCode message, but
  275. // may also be called by the login process.
  276. bool addCircuitCode(U32 code, const LLUUID& session_id);
  277. BOOL poll(F32 seconds); // Number of seconds that we want to block waiting for data, returns if data was received
  278. BOOL checkMessages( S64 frame_count = 0 );
  279. void processAcks();
  280. BOOL isMessageFast(const char *msg);
  281. BOOL isMessage(const char *msg)
  282. {
  283. return isMessageFast(LLMessageStringTable::getInstance()->getString(msg));
  284. }
  285. void dumpPacketToLog();
  286. char *getMessageName();
  287. const LLHost& getSender() const;
  288. U32 getSenderIP() const; // getSender() is preferred
  289. U32 getSenderPort() const; // getSender() is preferred
  290. const LLHost& getReceivingInterface() const;
  291. // This method returns the uuid associated with the sender. The
  292. // UUID will be null if it is not yet known or is a server
  293. // circuit.
  294. const LLUUID& getSenderID() const;
  295. // This method returns the session id associated with the last
  296. // sender.
  297. const LLUUID& getSenderSessionID() const;
  298. // set & get the session id (useful for viewers for now.)
  299. void setMySessionID(const LLUUID& session_id) { mSessionID = session_id; }
  300. const LLUUID& getMySessionID() { return mSessionID; }
  301. void newMessageFast(const char *name);
  302. void newMessage(const char *name);
  303. public:
  304. LLStoredMessagePtr getReceivedMessage() const;
  305. LLStoredMessagePtr getBuiltMessage() const;
  306. S32 sendMessage(const LLHost &host, LLStoredMessagePtr message);
  307. private:
  308. LLSD getReceivedMessageLLSD() const;
  309. LLSD getBuiltMessageLLSD() const;
  310. // NOTE: babbage: Only use to support legacy misuse of the
  311. // LLMessageSystem API where values are dangerously written
  312. // as one type and read as another. LLSD does not support
  313. // dangerous conversions and so converting the message to an
  314. // LLSD would result in the reads failing. All code which
  315. // misuses the message system in this way should be made safe
  316. // but while the unsafe code is run in old processes, this
  317. // method should be used to forward unsafe messages.
  318. LLSD wrapReceivedTemplateData() const;
  319. LLSD wrapBuiltTemplateData() const;
  320. public:
  321. void copyMessageReceivedToSend();
  322. void clearMessage();
  323. void nextBlockFast(const char *blockname);
  324. void nextBlock(const char *blockname);
  325. public:
  326. void addBinaryDataFast(const char *varname, const void *data, S32 size);
  327. void addBinaryData(const char *varname, const void *data, S32 size);
  328. void addBOOLFast( const char* varname, BOOL b); // typed, checks storage space
  329. void addBOOL( const char* varname, BOOL b); // typed, checks storage space
  330. void addS8Fast( const char *varname, S8 s); // typed, checks storage space
  331. void addS8( const char *varname, S8 s); // typed, checks storage space
  332. void addU8Fast( const char *varname, U8 u); // typed, checks storage space
  333. void addU8( const char *varname, U8 u); // typed, checks storage space
  334. void addS16Fast( const char *varname, S16 i); // typed, checks storage space
  335. void addS16( const char *varname, S16 i); // typed, checks storage space
  336. void addU16Fast( const char *varname, U16 i); // typed, checks storage space
  337. void addU16( const char *varname, U16 i); // typed, checks storage space
  338. void addF32Fast( const char *varname, F32 f); // typed, checks storage space
  339. void addF32( const char *varname, F32 f); // typed, checks storage space
  340. void addS32Fast( const char *varname, S32 s); // typed, checks storage space
  341. void addS32( const char *varname, S32 s); // typed, checks storage space
  342. void addU32Fast( const char *varname, U32 u); // typed, checks storage space
  343. void addU32( const char *varname, U32 u); // typed, checks storage space
  344. void addU64Fast( const char *varname, U64 lu); // typed, checks storage space
  345. void addU64( const char *varname, U64 lu); // typed, checks storage space
  346. void addF64Fast( const char *varname, F64 d); // typed, checks storage space
  347. void addF64( const char *varname, F64 d); // typed, checks storage space
  348. void addVector3Fast( const char *varname, const LLVector3& vec); // typed, checks storage space
  349. void addVector3( const char *varname, const LLVector3& vec); // typed, checks storage space
  350. void addVector4Fast( const char *varname, const LLVector4& vec); // typed, checks storage space
  351. void addVector4( const char *varname, const LLVector4& vec); // typed, checks storage space
  352. void addVector3dFast( const char *varname, const LLVector3d& vec); // typed, checks storage space
  353. void addVector3d( const char *varname, const LLVector3d& vec); // typed, checks storage space
  354. void addQuatFast( const char *varname, const LLQuaternion& quat); // typed, checks storage space
  355. void addQuat( const char *varname, const LLQuaternion& quat); // typed, checks storage space
  356. void addUUIDFast( const char *varname, const LLUUID& uuid); // typed, checks storage space
  357. void addUUID( const char *varname, const LLUUID& uuid); // typed, checks storage space
  358. void addIPAddrFast( const char *varname, const U32 ip); // typed, checks storage space
  359. void addIPAddr( const char *varname, const U32 ip); // typed, checks storage space
  360. void addIPPortFast( const char *varname, const U16 port); // typed, checks storage space
  361. void addIPPort( const char *varname, const U16 port); // typed, checks storage space
  362. void addStringFast( const char* varname, const char* s); // typed, checks storage space
  363. void addString( const char* varname, const char* s); // typed, checks storage space
  364. void addStringFast( const char* varname, const std::string& s); // typed, checks storage space
  365. void addString( const char* varname, const std::string& s); // typed, checks storage space
  366. S32 getCurrentSendTotal() const;
  367. TPACKETID getCurrentRecvPacketID() { return mCurrentRecvPacketID; }
  368. // This method checks for current send total and returns true if
  369. // you need to go to the next block type or need to start a new
  370. // message. Specify the current blockname to check block counts,
  371. // otherwise the method only checks against MTU.
  372. BOOL isSendFull(const char* blockname = NULL);
  373. BOOL isSendFullFast(const char* blockname = NULL);
  374. BOOL removeLastBlock();
  375. //void buildMessage();
  376. S32 zeroCode(U8 **data, S32 *data_size);
  377. S32 zeroCodeExpand(U8 **data, S32 *data_size);
  378. S32 zeroCodeAdjustCurrentSendTotal();
  379. // Uses ping-based retry
  380. S32 sendReliable(const LLHost &host);
  381. // Uses ping-based retry
  382. S32 sendReliable(const U32 circuit) { return sendReliable(findHost(circuit)); }
  383. // Use this one if you DON'T want automatic ping-based retry.
  384. S32 sendReliable( const LLHost &host,
  385. S32 retries,
  386. BOOL ping_based_retries,
  387. F32 timeout,
  388. void (*callback)(void **,S32),
  389. void ** callback_data);
  390. S32 sendSemiReliable( const LLHost &host,
  391. void (*callback)(void **,S32), void ** callback_data);
  392. // flush sends a message only if data's been pushed on it.
  393. S32 flushSemiReliable( const LLHost &host,
  394. void (*callback)(void **,S32), void ** callback_data);
  395. S32 flushReliable( const LLHost &host );
  396. void forwardMessage(const LLHost &host);
  397. void forwardReliable(const LLHost &host);
  398. void forwardReliable(const U32 circuit_code);
  399. S32 forwardReliable(
  400. const LLHost &host,
  401. S32 retries,
  402. BOOL ping_based_timeout,
  403. F32 timeout,
  404. void (*callback)(void **,S32),
  405. void ** callback_data);
  406. LLHTTPClient::ResponderPtr createResponder(const std::string& name);
  407. S32 sendMessage(const LLHost &host);
  408. S32 sendMessage(const U32 circuit);
  409. private:
  410. S32 sendMessage(const LLHost &host, const char* name,
  411. const LLSD& message);
  412. public:
  413. // BOOL decodeData(const U8 *buffer, const LLHost &host);
  414. /**
  415. gets binary data from the current message.
  416. @param blockname the name of the block in the message (from the message template)
  417. @param varname
  418. @param datap
  419. @param size expected size - set to zero to get any amount of data up to max_size.
  420. Make sure max_size is set in that case!
  421. @param blocknum
  422. @param max_size the max number of bytes to read
  423. */
  424. void getBinaryDataFast(const char *blockname, const char *varname, void *datap, S32 size, S32 blocknum = 0, S32 max_size = S32_MAX);
  425. void getBinaryData(const char *blockname, const char *varname, void *datap, S32 size, S32 blocknum = 0, S32 max_size = S32_MAX);
  426. void getBOOLFast( const char *block, const char *var, BOOL &data, S32 blocknum = 0);
  427. void getBOOL( const char *block, const char *var, BOOL &data, S32 blocknum = 0);
  428. void getS8Fast( const char *block, const char *var, S8 &data, S32 blocknum = 0);
  429. void getS8( const char *block, const char *var, S8 &data, S32 blocknum = 0);
  430. void getU8Fast( const char *block, const char *var, U8 &data, S32 blocknum = 0);
  431. void getU8( const char *block, const char *var, U8 &data, S32 blocknum = 0);
  432. void getS16Fast( const char *block, const char *var, S16 &data, S32 blocknum = 0);
  433. void getS16( const char *block, const char *var, S16 &data, S32 blocknum = 0);
  434. void getU16Fast( const char *block, const char *var, U16 &data, S32 blocknum = 0);
  435. void getU16( const char *block, const char *var, U16 &data, S32 blocknum = 0);
  436. void getS32Fast( const char *block, const char *var, S32 &data, S32 blocknum = 0);
  437. void getS32( const char *block, const char *var, S32 &data, S32 blocknum = 0);
  438. void getF32Fast( const char *block, const char *var, F32 &data, S32 blocknum = 0);
  439. void getF32( const char *block, const char *var, F32 &data, S32 blocknum = 0);
  440. void getU32Fast( const char *block, const char *var, U32 &data, S32 blocknum = 0);
  441. void getU32( const char *block, const char *var, U32 &data, S32 blocknum = 0);
  442. void getU64Fast( const char *block, const char *var, U64 &data, S32 blocknum = 0);
  443. void getU64( const char *block, const char *var, U64 &data, S32 blocknum = 0);
  444. void getF64Fast( const char *block, const char *var, F64 &data, S32 blocknum = 0);
  445. void getF64( const char *block, const char *var, F64 &data, S32 blocknum = 0);
  446. void getVector3Fast( const char *block, const char *var, LLVector3 &vec, S32 blocknum = 0);
  447. void getVector3( const char *block, const char *var, LLVector3 &vec, S32 blocknum = 0);
  448. void getVector4Fast( const char *block, const char *var, LLVector4 &vec, S32 blocknum = 0);
  449. void getVector4( const char *block, const char *var, LLVector4 &vec, S32 blocknum = 0);
  450. void getVector3dFast(const char *block, const char *var, LLVector3d &vec, S32 blocknum = 0);
  451. void getVector3d(const char *block, const char *var, LLVector3d &vec, S32 blocknum = 0);
  452. void getQuatFast( const char *block, const char *var, LLQuaternion &q, S32 blocknum = 0);
  453. void getQuat( const char *block, const char *var, LLQuaternion &q, S32 blocknum = 0);
  454. void getUUIDFast( const char *block, const char *var, LLUUID &uuid, S32 blocknum = 0);
  455. void getUUID( const char *block, const char *var, LLUUID &uuid, S32 blocknum = 0);
  456. void getIPAddrFast( const char *block, const char *var, U32 &ip, S32 blocknum = 0);
  457. void getIPAddr( const char *block, const char *var, U32 &ip, S32 blocknum = 0);
  458. void getIPPortFast( const char *block, const char *var, U16 &port, S32 blocknum = 0);
  459. void getIPPort( const char *block, const char *var, U16 &port, S32 blocknum = 0);
  460. void getStringFast( const char *block, const char *var, S32 buffer_size, char *buffer, S32 blocknum = 0);
  461. void getString( const char *block, const char *var, S32 buffer_size, char *buffer, S32 blocknum = 0);
  462. void getStringFast( const char *block, const char *var, std::string& outstr, S32 blocknum = 0);
  463. void getString( const char *block, const char *var, std::string& outstr, S32 blocknum = 0);
  464. // Utility functions to generate a replay-resistant digest check
  465. // against the shared secret. The window specifies how much of a
  466. // time window is allowed - 1 second is good for tight
  467. // connections, but multi-process windows might want to be upwards
  468. // of 5 seconds. For generateDigest, you want to pass in a
  469. // character array of at least MD5HEX_STR_SIZE so that the hex
  470. // digest and null termination will fit.
  471. bool generateDigestForNumberAndUUIDs(char* digest, const U32 number, const LLUUID &id1, const LLUUID &id2) const;
  472. bool generateDigestForWindowAndUUIDs(char* digest, const S32 window, const LLUUID &id1, const LLUUID &id2) const;
  473. bool isMatchingDigestForWindowAndUUIDs(const char* digest, const S32 window, const LLUUID &id1, const LLUUID &id2) const;
  474. bool generateDigestForNumber(char* digest, const U32 number) const;
  475. bool generateDigestForWindow(char* digest, const S32 window) const;
  476. bool isMatchingDigestForWindow(const char* digest, const S32 window) const;
  477. void showCircuitInfo();
  478. void getCircuitInfo(LLSD& info) const;
  479. U32 getOurCircuitCode();
  480. void enableCircuit(const LLHost &host, BOOL trusted);
  481. void disableCircuit(const LLHost &host);
  482. // Use this to establish trust on startup and in response to
  483. // DenyTrustedCircuit.
  484. void sendCreateTrustedCircuit(const LLHost& host, const LLUUID & id1, const LLUUID & id2);
  485. // Use this to inform a peer that they aren't currently trusted...
  486. // This now enqueues the request so that we can ensure that we only send
  487. // one deny per circuit per message loop so that this doesn't become a DoS.
  488. // The actual sending is done by reallySendDenyTrustedCircuit()
  489. void sendDenyTrustedCircuit(const LLHost &host);
  490. /** Return false if host is unknown or untrusted */
  491. // Note:DaveH/Babbage some trusted messages can be received without a circuit
  492. bool isTrustedSender(const LLHost& host) const;
  493. /** Return true if current message is from trusted source */
  494. bool isTrustedSender() const;
  495. /** Return false true if name is unknown or untrusted */
  496. bool isTrustedMessage(const std::string& name) const;
  497. /** Return false true if name is unknown or trusted */
  498. bool isUntrustedMessage(const std::string& name) const;
  499. // Mark an interface ineligible for trust
  500. void setUntrustedInterface( const LLHost host ) { mUntrustedInterface = host; }
  501. LLHost getUntrustedInterface() const { return mUntrustedInterface; }
  502. void setBlockUntrustedInterface( bool block ) { mBlockUntrustedInterface = block; } // Throw a switch to allow, sending warnings only
  503. bool getBlockUntrustedInterface() const { return mBlockUntrustedInterface; }
  504. // Change this message to be UDP black listed.
  505. void banUdpMessage(const std::string& name);
  506. private:
  507. // A list of the circuits that need to be sent DenyTrustedCircuit messages.
  508. typedef std::set<LLHost> host_set_t;
  509. host_set_t mDenyTrustedCircuitSet;
  510. // Really sends the DenyTrustedCircuit message to a given host
  511. // related to sendDenyTrustedCircuit()
  512. void reallySendDenyTrustedCircuit(const LLHost &host);
  513. public:
  514. // Use this to establish trust to and from a host. This blocks
  515. // until trust has been established, and probably should only be
  516. // used on startup.
  517. void establishBidirectionalTrust(const LLHost &host, S64 frame_count = 0);
  518. // returns whether the given host is on a trusted circuit
  519. // Note:DaveH/Babbage some trusted messages can be received without a circuit
  520. BOOL getCircuitTrust(const LLHost &host);
  521. void setCircuitAllowTimeout(const LLHost &host, BOOL allow);
  522. void setCircuitTimeoutCallback(const LLHost &host, void (*callback_func)(const LLHost &host, void *user_data), void *user_data);
  523. BOOL checkCircuitBlocked(const U32 circuit);
  524. BOOL checkCircuitAlive(const U32 circuit);
  525. BOOL checkCircuitAlive(const LLHost &host);
  526. void setCircuitProtection(BOOL b_protect);
  527. U32 findCircuitCode(const LLHost &host);
  528. LLHost findHost(const U32 circuit_code);
  529. void sanityCheck();
  530. BOOL has(const char *blockname) const;
  531. S32 getNumberOfBlocksFast(const char *blockname) const;
  532. S32 getNumberOfBlocks(const char *blockname) const;
  533. S32 getSizeFast(const char *blockname, const char *varname) const;
  534. S32 getSize(const char *blockname, const char *varname) const;
  535. S32 getSizeFast(const char *blockname, S32 blocknum,
  536. const char *varname) const; // size in bytes of data
  537. S32 getSize(const char *blockname, S32 blocknum, const char *varname) const;
  538. void resetReceiveCounts(); // resets receive counts for all message types to 0
  539. void dumpReceiveCounts(); // dumps receive count for each message type to llinfos
  540. void dumpCircuitInfo(); // Circuit information to llinfos
  541. BOOL isClear() const; // returns mbSClear;
  542. S32 flush(const LLHost &host);
  543. U32 getListenPort( void ) const;
  544. void startLogging(); // start verbose logging
  545. void stopLogging(); // flush and close file
  546. void summarizeLogs(std::ostream& str); // log statistics
  547. S32 getReceiveSize() const;
  548. S32 getReceiveCompressedSize() const { return mIncomingCompressedSize; }
  549. S32 getReceiveBytes() const;
  550. S32 getUnackedListSize() const { return mUnackedListSize; }
  551. //const char* getCurrentSMessageName() const { return mCurrentSMessageName; }
  552. //const char* getCurrentSBlockName() const { return mCurrentSBlockName; }
  553. // friends
  554. friend std::ostream& operator<<(std::ostream& s, LLMessageSystem &msg);
  555. void setMaxMessageTime(const F32 seconds); // Max time to process messages before warning and dumping (neg to disable)
  556. void setMaxMessageCounts(const S32 num); // Max number of messages before dumping (neg to disable)
  557. static U64 getMessageTimeUsecs(const BOOL update = FALSE); // Get the current message system time in microseconds
  558. static F64 getMessageTimeSeconds(const BOOL update = FALSE); // Get the current message system time in seconds
  559. static void setTimeDecodes(BOOL b);
  560. static void setTimeDecodesSpamThreshold(F32 seconds);
  561. // message handlers internal to the message systesm
  562. //static void processAssignCircuitCode(LLMessageSystem* msg, void**);
  563. static void processAddCircuitCode(LLMessageSystem* msg, void**);
  564. static void processUseCircuitCode(LLMessageSystem* msg, void**);
  565. static void processError(LLMessageSystem* msg, void**);
  566. // dispatch llsd message to http node tree
  567. static void dispatch(const std::string& msg_name,
  568. const LLSD& message);
  569. static void dispatch(const std::string& msg_name,
  570. const LLSD& message,
  571. LLHTTPNode::ResponsePtr responsep);
  572. // this is added to support specific legacy messages and is
  573. // ***not intended for general use*** Si, Gabriel, 2009
  574. static void dispatchTemplate(const std::string& msg_name,
  575. const LLSD& message,
  576. LLHTTPNode::ResponsePtr responsep);
  577. void setMessageBans(const LLSD& trusted, const LLSD& untrusted);
  578. /**
  579. * @brief send an error message to the host. This is a helper method.
  580. *
  581. * @param host Destination host.
  582. * @param agent_id Destination agent id (may be null)
  583. * @param code An HTTP status compatible error code.
  584. * @param token A specific short string based message
  585. * @param id The transactionid/uniqueid/sessionid whatever.
  586. * @param system The hierarchical path to the system (255 bytes)
  587. * @param message Human readable message (1200 bytes)
  588. * @param data Extra info.
  589. * @return Returns value returned from sendReliable().
  590. */
  591. S32 sendError(
  592. const LLHost& host,
  593. const LLUUID& agent_id,
  594. S32 code,
  595. const std::string& token,
  596. const LLUUID& id,
  597. const std::string& system,
  598. const std::string& message,
  599. const LLSD& data);
  600. // Check UDP messages and pump http_pump to receive HTTP messages.
  601. bool checkAllMessages(S64 frame_count, LLPumpIO* http_pump);
  602. // Moved to allow access from LLTemplateMessageDispatcher
  603. void clearReceiveState();
  604. // This will cause all trust queries to return true until the next message
  605. // is read: use with caution!
  606. void receivedMessageFromTrustedSender();
  607. private:
  608. bool mLastMessageFromTrustedMessageService;
  609. // The mCircuitCodes is a map from circuit codes to session
  610. // ids. This allows us to verify sessions on connect.
  611. typedef std::map<U32, LLUUID> code_session_map_t;
  612. code_session_map_t mCircuitCodes;
  613. // Viewers need to track a process session in order to make sure
  614. // that no one gives them a bad circuit code.
  615. LLUUID mSessionID;
  616. void addTemplate(LLMessageTemplate *templatep);
  617. BOOL decodeTemplate( const U8* buffer, S32 buffer_size, LLMessageTemplate** msg_template );
  618. void logMsgFromInvalidCircuit( const LLHost& sender, BOOL recv_reliable );
  619. void logTrustedMsgFromUntrustedCircuit( const LLHost& sender );
  620. void logValidMsg(LLCircuitData *cdp, const LLHost& sender, BOOL recv_reliable, BOOL recv_resent, BOOL recv_acks );
  621. void logRanOffEndOfPacket( const LLHost& sender );
  622. class LLMessageCountInfo
  623. {
  624. public:
  625. U32 mMessageNum;
  626. U32 mMessageBytes;
  627. BOOL mInvalid;
  628. };
  629. LLMessagePollInfo *mPollInfop;
  630. U8 mEncodedRecvBuffer[MAX_BUFFER_SIZE];
  631. U8 mTrueReceiveBuffer[MAX_BUFFER_SIZE];
  632. S32 mTrueReceiveSize;
  633. // Must be valid during decode
  634. BOOL mbError;
  635. S32 mErrorCode;
  636. F64 mResendDumpTime; // The last time we dumped resends
  637. LLMessageCountInfo mMessageCountList[MAX_MESSAGE_COUNT_NUM];
  638. S32 mNumMessageCounts;
  639. F32 mReceiveTime;
  640. F32 mMaxMessageTime; // Max number of seconds for processing messages
  641. S32 mMaxMessageCounts; // Max number of messages to process before dumping.
  642. F64 mMessageCountTime;
  643. F64 mCurrentMessageTimeSeconds; // The current "message system time" (updated the first call to checkMessages after a resetReceiveCount
  644. // message system exceptions
  645. typedef std::pair<msg_exception_callback, void*> exception_t;
  646. typedef std::map<EMessageException, exception_t> callbacks_t;
  647. callbacks_t mExceptionCallbacks;
  648. // stuff for logging
  649. LLTimer mMessageSystemTimer;
  650. static F32 mTimeDecodesSpamThreshold; // If mTimeDecodes is on, all this many seconds for each msg decode before spamming
  651. static BOOL mTimeDecodes; // Measure time for all message decodes if TRUE;
  652. msg_timing_callback mTimingCallback;
  653. void* mTimingCallbackData;
  654. void init(); // ctor shared initialisation.
  655. LLHost mLastSender;
  656. LLHost mLastReceivingIF;
  657. S32 mIncomingCompressedSize; // original size of compressed msg (0 if uncomp.)
  658. TPACKETID mCurrentRecvPacketID; // packet ID of current receive packet (for reporting)
  659. LLMessageBuilder* mMessageBuilder;
  660. LLTemplateMessageBuilder* mTemplateMessageBuilder;
  661. LLSDMessageBuilder* mLLSDMessageBuilder;
  662. LLMessageReader* mMessageReader;
  663. LLTemplateMessageReader* mTemplateMessageReader;
  664. LLSDMessageReader* mLLSDMessageReader;
  665. friend class LLMessageHandlerBridge;
  666. bool callHandler(const char *name, bool trustedSource,
  667. LLMessageSystem* msg);
  668. /** Find, create or revive circuit for host as needed */
  669. LLCircuitData* findCircuit(const LLHost& host, bool resetPacketId);
  670. };
  671. // external hook into messaging system
  672. extern LLMessageSystem *gMessageSystem;
  673. // Must specific overall system version, which is used to determine
  674. // if a patch is available in the message template checksum verification.
  675. // Return true if able to initialize system.
  676. bool start_messaging_system(
  677. const std::string& template_name,
  678. U32 port,
  679. S32 version_major,
  680. S32 version_minor,
  681. S32 version_patch,
  682. bool b_dump_prehash_file,
  683. const std::string& secret,
  684. const LLUseCircuitCodeResponder* responder,
  685. bool failure_is_fatal,
  686. const F32 circuit_heartbeat_interval,
  687. const F32 circuit_timeout);
  688. void end_messaging_system(bool print_summary = true);
  689. void null_message_callback(LLMessageSystem *msg, void **data);
  690. //
  691. // Inlines
  692. //
  693. #if !defined( LL_BIG_ENDIAN ) && !defined( LL_LITTLE_ENDIAN )
  694. #error Unknown endianness for htonmemcpy. Did you miss a common include?
  695. #endif
  696. static inline void *htonmemcpy(void *vs, const void *vct, EMsgVariableType type, size_t n)
  697. {
  698. char *s = (char *)vs;
  699. const char *ct = (const char *)vct;
  700. #ifdef LL_BIG_ENDIAN
  701. S32 i, length;
  702. #endif
  703. switch(type)
  704. {
  705. case MVT_FIXED:
  706. case MVT_VARIABLE:
  707. case MVT_U8:
  708. case MVT_S8:
  709. case MVT_BOOL:
  710. case MVT_LLUUID:
  711. case MVT_IP_ADDR: // these two are swizzled in the getters and setters
  712. case MVT_IP_PORT: // these two are swizzled in the getters and setters
  713. return(memcpy(s,ct,n)); /* Flawfinder: ignore */
  714. case MVT_U16:
  715. case MVT_S16:
  716. if (n != 2)
  717. {
  718. llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
  719. }
  720. #ifdef LL_BIG_ENDIAN
  721. *(s + 1) = *(ct);
  722. *(s) = *(ct + 1);
  723. return(vs);
  724. #else
  725. return(memcpy(s,ct,n)); /* Flawfinder: ignore */
  726. #endif
  727. case MVT_U32:
  728. case MVT_S32:
  729. case MVT_F32:
  730. if (n != 4)
  731. {
  732. llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
  733. }
  734. #ifdef LL_BIG_ENDIAN
  735. *(s + 3) = *(ct);
  736. *(s + 2) = *(ct + 1);
  737. *(s + 1) = *(ct + 2);
  738. *(s) = *(ct + 3);
  739. return(vs);
  740. #else
  741. return(memcpy(s,ct,n)); /* Flawfinder: ignore */
  742. #endif
  743. case MVT_U64:
  744. case MVT_S64:
  745. case MVT_F64:
  746. if (n != 8)
  747. {
  748. llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
  749. }
  750. #ifdef LL_BIG_ENDIAN
  751. *(s + 7) = *(ct);
  752. *(s + 6) = *(ct + 1);
  753. *(s + 5) = *(ct + 2);
  754. *(s + 4) = *(ct + 3);
  755. *(s + 3) = *(ct + 4);
  756. *(s + 2) = *(ct + 5);
  757. *(s + 1) = *(ct + 6);
  758. *(s) = *(ct + 7);
  759. return(vs);
  760. #else
  761. return(memcpy(s,ct,n)); /* Flawfinder: ignore */
  762. #endif
  763. case MVT_LLVector3:
  764. case MVT_LLQuaternion: // We only send x, y, z and infer w (we set x, y, z to ensure that w >= 0)
  765. if (n != 12)
  766. {
  767. llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
  768. }
  769. #ifdef LL_BIG_ENDIAN
  770. htonmemcpy(s + 8, ct + 8, MVT_F32, 4);
  771. htonmemcpy(s + 4, ct + 4, MVT_F32, 4);
  772. return(htonmemcpy(s, ct, MVT_F32, 4));
  773. #else
  774. return(memcpy(s,ct,n)); /* Flawfinder: ignore */
  775. #endif
  776. case MVT_LLVector3d:
  777. if (n != 24)
  778. {
  779. llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
  780. }
  781. #ifdef LL_BIG_ENDIAN
  782. htonmemcpy(s + 16, ct + 16, MVT_F64, 8);
  783. htonmemcpy(s + 8, ct + 8, MVT_F64, 8);
  784. return(htonmemcpy(s, ct, MVT_F64, 8));
  785. #else
  786. return(memcpy(s,ct,n)); /* Flawfinder: ignore */
  787. #endif
  788. case MVT_LLVector4:
  789. if (n != 16)
  790. {
  791. llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
  792. }
  793. #ifdef LL_BIG_ENDIAN
  794. htonmemcpy(s + 12, ct + 12, MVT_F32, 4);
  795. htonmemcpy(s + 8, ct + 8, MVT_F32, 4);
  796. htonmemcpy(s + 4, ct + 4, MVT_F32, 4);
  797. return(htonmemcpy(s, ct, MVT_F32, 4));
  798. #else
  799. return(memcpy(s,ct,n)); /* Flawfinder: ignore */
  800. #endif
  801. case MVT_U16Vec3:
  802. if (n != 6)
  803. {
  804. llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
  805. }
  806. #ifdef LL_BIG_ENDIAN
  807. htonmemcpy(s + 4, ct + 4, MVT_U16, 2);
  808. htonmemcpy(s + 2, ct + 2, MVT_U16, 2);
  809. return(htonmemcpy(s, ct, MVT_U16, 2));
  810. #else
  811. return(memcpy(s,ct,n)); /* Flawfinder: ignore */
  812. #endif
  813. case MVT_U16Quat:
  814. if (n != 8)
  815. {
  816. llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
  817. }
  818. #ifdef LL_BIG_ENDIAN
  819. htonmemcpy(s + 6, ct + 6, MVT_U16, 2);
  820. htonmemcpy(s + 4, ct + 4, MVT_U16, 2);
  821. htonmemcpy(s + 2, ct + 2, MVT_U16, 2);
  822. return(htonmemcpy(s, ct, MVT_U16, 2));
  823. #else
  824. return(memcpy(s,ct,n)); /* Flawfinder: ignore */
  825. #endif
  826. case MVT_S16Array:
  827. if (n % 2)
  828. {
  829. llerrs << "Size argument passed to htonmemcpy doesn't match swizzle type size" << llendl;
  830. }
  831. #ifdef LL_BIG_ENDIAN
  832. length = n % 2;
  833. for (i = 1; i < length; i++)
  834. {
  835. htonmemcpy(s + i*2, ct + i*2, MVT_S16, 2);
  836. }
  837. return(htonmemcpy(s, ct, MVT_S16, 2));
  838. #else
  839. return(memcpy(s,ct,n));
  840. #endif
  841. default:
  842. return(memcpy(s,ct,n)); /* Flawfinder: ignore */
  843. }
  844. }
  845. inline void *ntohmemcpy(void *s, const void *ct, EMsgVariableType type, size_t n)
  846. {
  847. return(htonmemcpy(s,ct,type, n));
  848. }
  849. inline const LLHost& LLMessageSystem::getReceivingInterface() const {return mLastReceivingIF;}
  850. inline U32 LLMessageSystem::getSenderIP() const
  851. {
  852. return mLastSender.getAddress();
  853. }
  854. inline U32 LLMessageSystem::getSenderPort() const
  855. {
  856. return mLastSender.getPort();
  857. }
  858. //-----------------------------------------------------------------------------
  859. // Transmission aliases
  860. //-----------------------------------------------------------------------------
  861. inline S32 LLMessageSystem::sendMessage(const U32 circuit)
  862. {
  863. return sendMessage(findHost(circuit));
  864. }
  865. #endif