llmessage.cpp 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236
  1. /**
  2. * @file llmessage.cpp
  3. * @brief LLMessageSystem class implementation
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include <algorithm>
  34. #include "llmessage.h"
  35. #if !LL_WINDOWS
  36. // Following header files required for inet_addr()
  37. # include <sys/types.h>
  38. # include <sys/socket.h>
  39. # include <netinet/in.h>
  40. # include <arpa/inet.h>
  41. #endif
  42. #include <iomanip>
  43. #include <iterator>
  44. #include <sstream>
  45. #include "apr_portable.h"
  46. #include "apr_network_io.h"
  47. #include "apr_poll.h"
  48. #include "indra_constants.h"
  49. #include "llapr.h"
  50. #include "llcorehttplibcurl.h"
  51. #include "llcorehttputil.h"
  52. #include "lldir.h"
  53. #include "llfasttimer.h"
  54. #include "llmessageconfig.h"
  55. #include "llmessagetemplate.h"
  56. #include "llmessagetemplateparser.h"
  57. #include "llnet.h" // For start_net(), end_net()
  58. #include "llpumpio.h"
  59. #include "llsd.h"
  60. #include "llsdmessagebuilder.h"
  61. #include "llsdmessagereader.h"
  62. #include "llsdserialize.h"
  63. #include "llstring.h"
  64. #include "lltemplatemessagebuilder.h"
  65. #include "lltemplatemessagereader.h"
  66. #include "lltimer.h"
  67. #include "lltransfermanager.h"
  68. #include "lltransfertargetvfile.h"
  69. #include "llquaternion.h"
  70. #include "llvector3.h"
  71. #include "llvector3d.h"
  72. #include "llvector4.h"
  73. #include "llxfermanager.h"
  74. constexpr F32 CIRCUIT_DUMP_TIMEOUT = 30.f;
  75. void swizzle_size_error(size_t n, size_t s)
  76. {
  77. llerrs << "Size argument passed (" << n
  78. << ")to htonmemcpy does not match swizzle type size: "
  79. << s << llendl;
  80. }
  81. class LLMessagePollInfo
  82. {
  83. public:
  84. apr_socket_t* mAPRSocketp;
  85. apr_pollfd_t mPollFD;
  86. };
  87. class LLMessageHandlerBridge : public LLHTTPNode
  88. {
  89. bool validate(const std::string& name, LLSD& context) const override
  90. {
  91. return true;
  92. }
  93. void post(LLHTTPNode::ResponsePtr response, const LLSD& context,
  94. const LLSD& input) const override;
  95. };
  96. //virtual
  97. void LLMessageHandlerBridge::post(LLHTTPNode::ResponsePtr response,
  98. const LLSD& context, const LLSD& input) const
  99. {
  100. std::string name =
  101. context[CONTEXT_REQUEST][CONTEXT_WILDCARD]["message-name"];
  102. char* namep = gMessageStringTable.getString(name.c_str());
  103. LLMessageSystem* msg = gMessageSystemp;
  104. msg->mLastSender = LLHost(input["sender"].asString());
  105. msg->mPacketsIn += 1;
  106. msg->mLLSDMessageReader->setMessage(namep, input["body"]);
  107. #if LL_USE_FIBER_AWARE_MUTEX
  108. LockMessageReader rdr(msg->mMessageReader, msg->mLLSDMessageReader);
  109. #else
  110. msg->mMessageReader = msg->mLLSDMessageReader;
  111. #endif
  112. if (msg->callHandler(namep))
  113. {
  114. response->result(LLSD());
  115. }
  116. else
  117. {
  118. response->notFound();
  119. }
  120. }
  121. LLHTTPRegistration<LLMessageHandlerBridge>
  122. gHTTPRegistrationMessageWildcard("/message/<message-name>");
  123. #if LL_USE_FIBER_AWARE_MUTEX
  124. // For the lifespan of this LockMessageChecker instance, we use
  125. // LLTemplateMessageReader as msgsystem's mMessageReader
  126. LockMessageChecker::LockMessageChecker(LLMessageSystem* msgsystem)
  127. : LockMessageReader(msgsystem->mMessageReader,
  128. msgsystem->mTemplateMessageReader),
  129. mMessageSystem(msgsystem)
  130. {
  131. }
  132. #endif
  133. static const char* nullToEmpty(const char* s)
  134. {
  135. static const char empty_string[] = "";
  136. return s ? s : empty_string;
  137. }
  138. void LLMessageSystem::init()
  139. {
  140. // Initialize member variables
  141. mVerboseLog = false;
  142. mError = false;
  143. mErrorCode = 0;
  144. mSendReliable = false;
  145. mUnackedListDepth = 0;
  146. mUnackedListSize = 0;
  147. mDSMaxListDepth = 0;
  148. mNumberHighFreqMessages = 0;
  149. mNumberMediumFreqMessages = 0;
  150. mNumberLowFreqMessages = 0;
  151. mPacketsIn = mPacketsOut = 0;
  152. mBytesIn = mBytesOut = 0;
  153. mCompressedPacketsIn = mCompressedPacketsOut = 0;
  154. mReliablePacketsIn = mReliablePacketsOut = 0;
  155. mCompressedBytesIn = mCompressedBytesOut = 0;
  156. mUncompressedBytesIn = mUncompressedBytesOut = 0;
  157. mTotalBytesIn = mTotalBytesOut = 0;
  158. mDroppedPackets = 0; // Total dropped packets in
  159. mResentPackets = 0; // Total resent packets out
  160. mFailedResendPackets = 0; // Total resend failure packets out
  161. mOffCircuitPackets = 0; // Total # of off-circuit packets rejected
  162. mInvalidOnCircuitPackets = 0; // Total # of on-circuit packets rejected
  163. mOurCircuitCode = 0;
  164. mIncomingCompressedSize = 0;
  165. mCurrentRecvPacketID = 0;
  166. mMessageFileVersionNumber = 0.f;
  167. mTimingCallback = NULL;
  168. mTimingCallbackData = NULL;
  169. mMessageBuilder = NULL;
  170. #if LL_USE_FIBER_AWARE_MUTEX
  171. LockMessageReader(mMessageReader, NULL);
  172. #else
  173. mMessageReader = NULL;
  174. #endif
  175. if (strcmp(_PREHASH_AgentID, "AgentID"))
  176. {
  177. llerrs << "Message prehash table not properly initialized !" << llendl;
  178. }
  179. }
  180. void LLMessageSystem::setHttpOptionsWithTimeout(U32 timeout)
  181. {
  182. mHttpOptions->setRetries(0);
  183. mHttpOptions->setTimeout(timeout);
  184. mHttpOptions->setTransferTimeout(timeout);
  185. }
  186. // Read file and build message templates
  187. LLMessageSystem::LLMessageSystem(const std::string& filename, U32 port,
  188. S32 version_major, S32 version_minor,
  189. S32 version_patch, F32 heartbeat_interval,
  190. F32 circuit_timeout)
  191. : mCircuitInfo(heartbeat_interval, circuit_timeout),
  192. mPort(port),
  193. mSocket(0),
  194. mSystemVersionMajor(version_major),
  195. mSystemVersionMinor(version_minor),
  196. mSystemVersionPatch(version_patch),
  197. mSystemVersionServer(0),
  198. mVersionFlags(0x0),
  199. mMessageBuilder(NULL),
  200. #if !LL_USE_FIBER_AWARE_MUTEX
  201. mMessageReader(NULL),
  202. #endif
  203. mSendSize(0),
  204. mHttpOptions(new LLCore::HttpOptions),
  205. // Default to not accepting packets from not alive circuits
  206. mProtected(true),
  207. mCircuitPrintFreq(60.f), // In seconds
  208. // Constants for dumping output based on message processing time/count
  209. mNumMessageCounts(0),
  210. mMaxMessageCounts(200), // >= 0 means dump warnings
  211. mMaxMessageTime(1.f),
  212. // Statistics
  213. mTrueReceiveSize(0),
  214. mReceiveTime(0.f),
  215. mSendPacketFailureCount(0)
  216. {
  217. init();
  218. loadTemplateFile(filename);
  219. mTemplateMessageBuilder = new LLTemplateMessageBuilder(mMessageTemplates);
  220. mLLSDMessageBuilder = new LLSDMessageBuilder();
  221. mTemplateMessageReader = new LLTemplateMessageReader(mMessageNumbers);
  222. mLLSDMessageReader = new LLSDMessageReader();
  223. // Initialize various bits of net info
  224. S32 error = start_net(mSocket, mPort);
  225. if (error != 0)
  226. {
  227. mError = true;
  228. mErrorCode = error;
  229. }
  230. //
  231. // Create the data structure that we can poll on
  232. //
  233. if (!gAPRPoolp)
  234. {
  235. llerrs << "No APR pool before message system initialization !"
  236. << llendl;
  237. }
  238. apr_socket_t* apr_socketp = NULL;
  239. apr_os_sock_put(&apr_socketp, (apr_os_sock_t*)&mSocket, gAPRPoolp);
  240. mPollInfop = new LLMessagePollInfo;
  241. mPollInfop->mAPRSocketp = apr_socketp;
  242. mPollInfop->mPollFD.p = gAPRPoolp;
  243. mPollInfop->mPollFD.desc_type = APR_POLL_SOCKET;
  244. mPollInfop->mPollFD.reqevents = APR_POLLIN;
  245. mPollInfop->mPollFD.rtnevents = 0;
  246. mPollInfop->mPollFD.desc.s = apr_socketp;
  247. mPollInfop->mPollFD.client_data = NULL;
  248. F64 mt_sec = getMessageTimeSeconds();
  249. mResendDumpTime = mt_sec;
  250. mMessageCountTime = mt_sec;
  251. mCircuitPrintTime = mt_sec;
  252. mCurrentMessageTimeSeconds = mt_sec;
  253. }
  254. // Read file and build message templates
  255. void LLMessageSystem::loadTemplateFile(const std::string& filename)
  256. {
  257. if (filename.empty())
  258. {
  259. llerrs << "No template filename specified !" << llendl;
  260. #if 0 // Enable if we ever change the above for llwarns
  261. mError = true;
  262. return;
  263. #endif
  264. }
  265. std::string template_body = LLFile::getContents(filename);
  266. if (template_body.empty())
  267. {
  268. llerrs << "Failed to open template file: " << filename << llendl;
  269. #if 0 // Enable if we ever change the above for llwarns
  270. mError = true;
  271. return;
  272. #endif
  273. }
  274. LLTemplateTokenizer tokens(template_body);
  275. LLTemplateParser parsed(tokens);
  276. mMessageFileVersionNumber = parsed.getVersion();
  277. for (LLTemplateParser::message_iterator iter = parsed.getMessagesBegin();
  278. iter != parsed.getMessagesEnd(); iter++)
  279. {
  280. addTemplate(*iter);
  281. }
  282. }
  283. LLMessageSystem::~LLMessageSystem()
  284. {
  285. mMessageTemplates.clear(); // Do not delete templates.
  286. for (template_number_map_t::iterator it = mMessageNumbers.begin(),
  287. end = mMessageNumbers.end();
  288. it != end; ++it)
  289. {
  290. delete it->second;
  291. }
  292. mMessageNumbers.clear();
  293. if (!mError)
  294. {
  295. end_net(mSocket);
  296. }
  297. mSocket = 0;
  298. delete mTemplateMessageReader;
  299. mTemplateMessageReader = NULL;
  300. #if !LL_USE_FIBER_AWARE_MUTEX
  301. mMessageReader = NULL;
  302. #endif
  303. delete mTemplateMessageBuilder;
  304. mTemplateMessageBuilder = NULL;
  305. mMessageBuilder = NULL;
  306. delete mLLSDMessageReader;
  307. mLLSDMessageReader = NULL;
  308. delete mLLSDMessageBuilder;
  309. mLLSDMessageBuilder = NULL;
  310. delete mPollInfop;
  311. mPollInfop = NULL;
  312. mIncomingCompressedSize = 0;
  313. mCurrentRecvPacketID = 0;
  314. }
  315. void LLMessageSystem::clearReceiveState()
  316. {
  317. mCurrentRecvPacketID = 0;
  318. mIncomingCompressedSize = 0;
  319. mLastSender.invalidate();
  320. mLastReceivingIF.invalidate();
  321. mMessageReader->clearMessage();
  322. }
  323. bool LLMessageSystem::poll(F32 seconds)
  324. {
  325. S32 num_socks;
  326. apr_status_t status = apr_poll(&(mPollInfop->mPollFD), 1, &num_socks,
  327. (U64)(seconds * 1000000.f));
  328. if (status != APR_TIMEUP)
  329. {
  330. ll_apr_warn_status(status);
  331. }
  332. return num_socks != 0;
  333. }
  334. LLCircuitData* LLMessageSystem::findCircuit(const LLHost& host,
  335. bool reset_packet_id)
  336. {
  337. LLCircuitData* cdp = mCircuitInfo.findCircuit(host);
  338. if (!cdp) // if this packet comes from a circuit we do not know about
  339. {
  340. // Are we rejecting off-circuit packets ?
  341. if (!mProtected)
  342. {
  343. // Nope, open the new circuit
  344. cdp = mCircuitInfo.addCircuitData(host, mCurrentRecvPacketID);
  345. if (reset_packet_id)
  346. {
  347. // I added this - I think it's correct - DJS
  348. // reset packet in Id
  349. cdp->setPacketInID(mCurrentRecvPacketID);
  350. }
  351. // And claim the packet is on the circuit we just added.
  352. }
  353. }
  354. // This is an old circuit... Is it still alive ?
  355. else if (!cdp->isAlive())
  356. {
  357. // Nope. Do not accept if weare protected
  358. if (mProtected)
  359. {
  360. // Do not accept packets from unexpected sources
  361. cdp = NULL;
  362. }
  363. else
  364. {
  365. // Wake up the circuit
  366. cdp->setAlive(true);
  367. if (reset_packet_id)
  368. {
  369. // Reset packet in Id
  370. cdp->setPacketInID(mCurrentRecvPacketID);
  371. }
  372. }
  373. }
  374. return cdp;
  375. }
  376. // Returns true if a valid, on-circuit message has been received.
  377. #if LL_USE_FIBER_AWARE_MUTEX
  378. bool LLMessageSystem::checkMessages(LockMessageChecker&, S64 frame_count)
  379. #else
  380. bool LLMessageSystem::checkMessages(S64 frame_count)
  381. #endif
  382. {
  383. // Pump
  384. bool valid_packet = false;
  385. #if !LL_USE_FIBER_AWARE_MUTEX
  386. mMessageReader = mTemplateMessageReader;
  387. #endif
  388. LLTransferTargetVFile::updateQueue();
  389. if (!mNumMessageCounts)
  390. {
  391. // This is the first message being handled after a resetReceiveCounts,
  392. // we must be starting the message processing loop. Reset the timers.
  393. mCurrentMessageTimeSeconds = LLTimer::totalTime() * SEC_PER_USEC;
  394. mMessageCountTime = getMessageTimeSeconds();
  395. }
  396. // Loop until either no packets or a valid packet, i.e. burn through
  397. // packets from unregistered circuits
  398. S32 receive_size = 0;
  399. do
  400. {
  401. clearReceiveState();
  402. bool recv_reliable = false;
  403. bool recv_resent = false;
  404. S32 acks = 0;
  405. S32 true_rcv_size = 0;
  406. U8* buffer = mTrueReceiveBuffer;
  407. mTrueReceiveSize =
  408. mPacketRing.receivePacket(mSocket, (char*)mTrueReceiveBuffer);
  409. #if 0 // Enable this if you want to dump all received packets into the log
  410. dumpPacketToLog();
  411. #endif
  412. receive_size = mTrueReceiveSize;
  413. mLastSender = mPacketRing.getLastSender();
  414. mLastReceivingIF = mPacketRing.getLastReceivingInterface();
  415. if (receive_size < (S32)LL_MINIMUM_VALID_PACKET_SIZE)
  416. {
  417. // A receive size of zero is OK, that means that there are no more
  418. // packets available. Ones that are non-zero but below the minimum
  419. // packet size are worrisome.
  420. if (receive_size > 0)
  421. {
  422. llwarns << "Invalid (too short) packet discarded "
  423. << receive_size << llendl;
  424. callExceptionFunc(MX_PACKET_TOO_SHORT);
  425. }
  426. // No data in packet receive buffer
  427. valid_packet = false;
  428. }
  429. else
  430. {
  431. LLHost host;
  432. LLCircuitData* cdp;
  433. // Note if packet acks are appended.
  434. if (buffer[0] & LL_ACK_FLAG)
  435. {
  436. acks += buffer[--receive_size];
  437. true_rcv_size = receive_size;
  438. if (receive_size >= ((S32)(acks * sizeof(TPACKETID) +
  439. LL_MINIMUM_VALID_PACKET_SIZE)))
  440. {
  441. receive_size -= acks * sizeof(TPACKETID);
  442. }
  443. else
  444. {
  445. // Malformed packet; ignore it and continue with next one.
  446. llwarns << "Malformed packet received. Packet size "
  447. << receive_size << " with invalid no. of acks "
  448. << acks << llendl;
  449. valid_packet = false;
  450. continue;
  451. }
  452. }
  453. // Process the message as normal
  454. mIncomingCompressedSize = zeroCodeExpand(&buffer, &receive_size);
  455. mCurrentRecvPacketID = ntohl(*((U32*)(&buffer[1])));
  456. host = getSender();
  457. constexpr bool reset_packet_id = true;
  458. cdp = findCircuit(host, reset_packet_id);
  459. // At this point, cdp is now a pointer to the circuit that this
  460. // message came in on if it's valid, and NULL if the circuit was
  461. // bogus.
  462. if (cdp && acks > 0 &&
  463. acks * (S32)sizeof(TPACKETID) < true_rcv_size)
  464. {
  465. TPACKETID packet_id;
  466. U32 mem_id = 0;
  467. for (S32 i = 0; i < acks; ++i)
  468. {
  469. true_rcv_size -= sizeof(TPACKETID);
  470. memcpy(&mem_id, &mTrueReceiveBuffer[true_rcv_size],
  471. sizeof(TPACKETID));
  472. packet_id = ntohl(mem_id);
  473. cdp->ackReliablePacket(packet_id);
  474. }
  475. if (!cdp->getUnackedPacketCount())
  476. {
  477. // Remove this circuit from the list of circuits with
  478. // unacked packets
  479. mCircuitInfo.mUnackedCircuitMap.erase(cdp->mHost);
  480. }
  481. }
  482. if (buffer[0] & LL_RELIABLE_FLAG)
  483. {
  484. recv_reliable = true;
  485. }
  486. if (buffer[0] & LL_RESENT_FLAG)
  487. {
  488. recv_resent = true;
  489. if (cdp && cdp->isDuplicateResend(mCurrentRecvPacketID))
  490. {
  491. // We need to ACK here to suppress further resends of
  492. // packets we have already seen.
  493. if (recv_reliable)
  494. {
  495. cdp->collectRAck(mCurrentRecvPacketID);
  496. }
  497. LL_DEBUGS("Messaging") << "Discarding duplicate resend from "
  498. << host << LL_ENDL;
  499. if (mVerboseLog)
  500. {
  501. std::ostringstream str;
  502. str << "MSG: <- " << host;
  503. std::string tbuf =
  504. llformat("\t%6d\t%6d\t%6d ", receive_size,
  505. (mIncomingCompressedSize ? mIncomingCompressedSize
  506. : receive_size),
  507. mCurrentRecvPacketID);
  508. str << tbuf << "(unknown)"
  509. << (recv_reliable ? " reliable" : "")
  510. << " resent "
  511. << ((acks > 0) ? "acks" : "")
  512. << " DISCARD DUPLICATE";
  513. llinfos << str.str() << llendl;
  514. }
  515. ++mPacketsIn;
  516. valid_packet = false;
  517. continue;
  518. }
  519. }
  520. // UseCircuitCode can be a valid, off-circuit packet. But we do not
  521. // want to acknowledge UseCircuitCode until the circuit is
  522. // available, which is why the acknowledgement test is done above.
  523. // JC
  524. bool trusted = cdp && cdp->getTrusted();
  525. valid_packet =
  526. mTemplateMessageReader->validateMessage(buffer, receive_size,
  527. host, trusted);
  528. if (!valid_packet)
  529. {
  530. clearReceiveState();
  531. }
  532. // UseCircuitCode is allowed in even from an invalid circuit, so
  533. // that we can toss circuits around.
  534. if (valid_packet && !cdp &&
  535. mTemplateMessageReader->getMessageName() != _PREHASH_UseCircuitCode)
  536. {
  537. logMsgFromInvalidCircuit(host, recv_reliable);
  538. clearReceiveState();
  539. valid_packet = false;
  540. }
  541. if (valid_packet && cdp && !cdp->getTrusted() &&
  542. mTemplateMessageReader->isTrusted())
  543. {
  544. logTrustedMsgFromUntrustedCircuit(host);
  545. clearReceiveState();
  546. sendDenyTrustedCircuit(host);
  547. valid_packet = false;
  548. }
  549. if (valid_packet)
  550. {
  551. logValidMsg(cdp, host, recv_reliable, recv_resent, acks > 0);
  552. valid_packet = mTemplateMessageReader->readMessage(buffer,
  553. host);
  554. }
  555. // It is possible that the circuit went away, because ANY message
  556. // can disable the circuit (for example, UseCircuit, CloseCircuit,
  557. // DisableSimulator). Find it again.
  558. cdp = mCircuitInfo.findCircuit(host);
  559. if (valid_packet)
  560. {
  561. ++mPacketsIn;
  562. mBytesIn += mTrueReceiveSize;
  563. // ACK here for valid packets that we have seen for the first
  564. // time
  565. if (cdp && recv_reliable)
  566. {
  567. // Add to the recently received list for duplicate
  568. // suppression
  569. cdp->mRecentlyReceivedReliablePackets[mCurrentRecvPacketID] =
  570. getMessageTimeUsecs();
  571. // Put it onto the list of packets to be acked
  572. cdp->collectRAck(mCurrentRecvPacketID);
  573. ++mReliablePacketsIn;
  574. }
  575. }
  576. else if (mProtected && !cdp)
  577. {
  578. llwarns << "Invalid Packet from invalid circuit " << host
  579. << llendl;
  580. ++mOffCircuitPackets;
  581. }
  582. else
  583. {
  584. ++mInvalidOnCircuitPackets;
  585. }
  586. }
  587. }
  588. while (!valid_packet && receive_size > 0);
  589. F64 mt_sec = getMessageTimeSeconds();
  590. // Check to see if we need to print debug info
  591. if (mt_sec - mCircuitPrintTime > mCircuitPrintFreq)
  592. {
  593. LL_DEBUGS("CircuitInfo") << mCircuitInfo << LL_ENDL;
  594. mCircuitPrintTime = mt_sec;
  595. }
  596. if (!valid_packet)
  597. {
  598. clearReceiveState();
  599. }
  600. return valid_packet;
  601. }
  602. S32 LLMessageSystem::getReceiveBytes() const
  603. {
  604. if (mIncomingCompressedSize)
  605. {
  606. return mIncomingCompressedSize * 8;
  607. }
  608. return getReceiveSize() * 8;
  609. }
  610. #if LL_USE_FIBER_AWARE_MUTEX
  611. void LLMessageSystem::processAcks(LockMessageChecker&, F32 collect_time)
  612. #else
  613. void LLMessageSystem::processAcks(F32 collect_time)
  614. #endif
  615. {
  616. F64 mt_sec = getMessageTimeSeconds();
  617. {
  618. gTransferManager.updateTransfers();
  619. if (gXferManagerp)
  620. {
  621. gXferManagerp->retransmitUnackedPackets();
  622. }
  623. if (gAssetStoragep)
  624. {
  625. gAssetStoragep->checkForTimeouts();
  626. }
  627. }
  628. bool dump = false;
  629. // Check the status of circuits
  630. mCircuitInfo.updateWatchDogTimers(this);
  631. // Resend any necessary packets
  632. mCircuitInfo.resendUnackedPackets(mUnackedListDepth, mUnackedListSize);
  633. // Cycle through ack list for each host we need to send acks to
  634. mCircuitInfo.sendAcks(collect_time);
  635. if (!mDenyTrustedCircuitSet.empty())
  636. {
  637. llinfos << "Sending queued DenyTrustedCircuit messages." << llendl;
  638. for (host_set_t::iterator hostit = mDenyTrustedCircuitSet.begin();
  639. hostit != mDenyTrustedCircuitSet.end(); ++hostit)
  640. {
  641. reallySendDenyTrustedCircuit(*hostit);
  642. }
  643. mDenyTrustedCircuitSet.clear();
  644. }
  645. if (mMaxMessageCounts >= 0 && mNumMessageCounts >= mMaxMessageCounts)
  646. {
  647. dump = true;
  648. }
  649. if (mMaxMessageTime >= 0.f)
  650. {
  651. // This is one of the only places where we are required to get REAL
  652. // message system time.
  653. mReceiveTime = (F32)(getMessageTimeSeconds(true) - mMessageCountTime);
  654. if (mReceiveTime > mMaxMessageTime)
  655. {
  656. dump = true;
  657. }
  658. }
  659. if (dump)
  660. {
  661. dumpReceiveCounts();
  662. }
  663. resetReceiveCounts();
  664. if (mt_sec - mResendDumpTime > CIRCUIT_DUMP_TIMEOUT)
  665. {
  666. mResendDumpTime = mt_sec;
  667. mCircuitInfo.dumpResends();
  668. }
  669. }
  670. void LLMessageSystem::copyMessageReceivedToSend()
  671. {
  672. // NOTE: babbage: switch builder to match reader to avoid converting
  673. // message format
  674. if (mMessageReader == mTemplateMessageReader)
  675. {
  676. mMessageBuilder = mTemplateMessageBuilder;
  677. }
  678. else
  679. {
  680. mMessageBuilder = mLLSDMessageBuilder;
  681. }
  682. mSendReliable = false;
  683. mMessageBuilder->newMessage(mMessageReader->getMessageName());
  684. mMessageReader->copyToBuilder(*mMessageBuilder);
  685. }
  686. LLSD LLMessageSystem::getReceivedMessageLLSD() const
  687. {
  688. LLSDMessageBuilder builder;
  689. mMessageReader->copyToBuilder(builder);
  690. return builder.getMessage();
  691. }
  692. LLSD LLMessageSystem::getBuiltMessageLLSD() const
  693. {
  694. if (mLLSDMessageBuilder != mMessageBuilder)
  695. {
  696. // *TODO: implement as below ?
  697. llerrs << "Message not built as LLSD." << llendl;
  698. }
  699. return mLLSDMessageBuilder->getMessage();
  700. }
  701. LLSD LLMessageSystem::wrapReceivedTemplateData() const
  702. {
  703. if (mMessageReader != mTemplateMessageReader)
  704. {
  705. return getReceivedMessageLLSD();
  706. }
  707. LLTemplateMessageBuilder builder(mMessageTemplates);
  708. builder.newMessage(mMessageReader->getMessageName());
  709. mMessageReader->copyToBuilder(builder);
  710. U8 buffer[MAX_BUFFER_SIZE];
  711. constexpr U8 offset_to_data = 0;
  712. U32 size = builder.buildMessage(buffer, MAX_BUFFER_SIZE,
  713. offset_to_data);
  714. LLSD::Binary binary_data(buffer, buffer + size);
  715. LLSD wrapped_data = LLSD::emptyMap();
  716. wrapped_data["binary-template-data"] = binary_data;
  717. return wrapped_data;
  718. }
  719. LLSD LLMessageSystem::wrapBuiltTemplateData() const
  720. {
  721. if (mLLSDMessageBuilder == mMessageBuilder)
  722. {
  723. return getBuiltMessageLLSD();
  724. }
  725. U8 buffer[MAX_BUFFER_SIZE];
  726. constexpr U8 offset_to_data = 0;
  727. U32 size = mTemplateMessageBuilder->buildMessage(buffer, MAX_BUFFER_SIZE,
  728. offset_to_data);
  729. LLSD::Binary binary_data(buffer, buffer + size);
  730. LLSD wrapped_data = LLSD::emptyMap();
  731. wrapped_data["binary-template-data"] = binary_data;
  732. return wrapped_data;
  733. }
  734. void LLMessageSystem::clearMessage()
  735. {
  736. mSendReliable = false;
  737. mMessageBuilder->clearMessage();
  738. }
  739. // Sets block to add data to within current message
  740. void LLMessageSystem::nextBlockFast(const char* blockname)
  741. {
  742. mMessageBuilder->nextBlock(blockname);
  743. }
  744. void LLMessageSystem::nextBlock(const char* blockname)
  745. {
  746. nextBlockFast(gMessageStringTable.getString(blockname));
  747. }
  748. bool LLMessageSystem::isSendFull(const char* blockname)
  749. {
  750. char* str_table_name = NULL;
  751. if (blockname)
  752. {
  753. str_table_name = gMessageStringTable.getString(blockname);
  754. }
  755. return isSendFullFast(str_table_name);
  756. }
  757. bool LLMessageSystem::isSendFullFast(const char* blockname)
  758. {
  759. return mMessageBuilder->isMessageFull(blockname);
  760. }
  761. // Blows away the last block of a message, returns false if that leaves no
  762. // blocks or there was not a block to remove.
  763. // *TODO: Babbage: Remove this horror.
  764. bool LLMessageSystem::removeLastBlock()
  765. {
  766. return mMessageBuilder->removeLastBlock();
  767. }
  768. S32 LLMessageSystem::sendReliable(const LLHost& host, U32 retries_factor)
  769. {
  770. return sendReliable(host, LL_DEFAULT_RELIABLE_RETRIES * retries_factor,
  771. true, LL_PING_BASED_TIMEOUT_DUMMY, NULL, NULL);
  772. }
  773. S32 LLMessageSystem::sendSemiReliable(const LLHost& host,
  774. void (*callback)(void**,S32),
  775. void** callback_data)
  776. {
  777. F32 timeout;
  778. LLCircuitData* cdp = mCircuitInfo.findCircuit(host);
  779. if (cdp)
  780. {
  781. timeout = llmax(LL_MINIMUM_SEMIRELIABLE_TIMEOUT_SECONDS,
  782. LL_SEMIRELIABLE_TIMEOUT_FACTOR *
  783. cdp->getPingDelayAveraged());
  784. }
  785. else
  786. {
  787. timeout = LL_SEMIRELIABLE_TIMEOUT_FACTOR * LL_AVERAGED_PING_MAX;
  788. }
  789. // 0 retry and not ping-based timeout
  790. return sendReliable(host, 0, false, timeout, callback, callback_data);
  791. }
  792. // Sends the message via an UDP packet
  793. S32 LLMessageSystem::sendReliable(const LLHost& host, S32 retries,
  794. bool ping_based_timeout, F32 timeout,
  795. void (*callback)(void**, S32),
  796. void** callback_data)
  797. {
  798. if (ping_based_timeout)
  799. {
  800. LLCircuitData* cdp = mCircuitInfo.findCircuit(host);
  801. if (cdp)
  802. {
  803. timeout = llmax(LL_MINIMUM_RELIABLE_TIMEOUT_SECONDS,
  804. LL_RELIABLE_TIMEOUT_FACTOR *
  805. cdp->getPingDelayAveraged());
  806. }
  807. else
  808. {
  809. timeout = llmax(LL_MINIMUM_RELIABLE_TIMEOUT_SECONDS,
  810. LL_RELIABLE_TIMEOUT_FACTOR * LL_AVERAGED_PING_MAX);
  811. }
  812. }
  813. mSendReliable = true;
  814. mReliablePacketParams.set(host, retries, ping_based_timeout, timeout,
  815. callback, callback_data,
  816. const_cast<char*>(mMessageBuilder->getMessageName()));
  817. return sendMessage(host);
  818. }
  819. S32 LLMessageSystem::sendMessage(const LLHost& host)
  820. {
  821. if (!mMessageBuilder->isBuilt())
  822. {
  823. mSendSize = mMessageBuilder->buildMessage(mSendBuffer, MAX_BUFFER_SIZE,
  824. 0);
  825. }
  826. // If port and ip are zero, do not bother trying to send the message
  827. if (!host.isOk())
  828. {
  829. return 0;
  830. }
  831. LLCircuitData* cdp = mCircuitInfo.findCircuit(host);
  832. if (!cdp)
  833. {
  834. // This is a new circuit ! Are we protected ?
  835. if (mProtected)
  836. {
  837. // Yup ! Do not send packets to an unknown circuit
  838. if (mVerboseLog)
  839. {
  840. llinfos_once << "MSG: -> " << host << "\tUNKNOWN CIRCUIT:\t"
  841. << mMessageBuilder->getMessageName() << llendl;
  842. }
  843. llwarns_once << "Trying to send "
  844. << mMessageBuilder->getMessageName()
  845. << " on unknown circuit " << host << llendl;
  846. return 0;
  847. }
  848. else
  849. {
  850. // Nope, open the new circuit
  851. cdp = mCircuitInfo.addCircuitData(host, 0);
  852. }
  853. }
  854. else if (!cdp->isAlive()) // This is an old circuit... Is it still alive ?
  855. {
  856. // Nope. Do not send to dead circuits
  857. if (mVerboseLog)
  858. {
  859. llinfos << "MSG: -> " << host << "\tDEAD CIRCUIT\t\t"
  860. << mMessageBuilder->getMessageName() << llendl;
  861. }
  862. llwarns << "Trying to send message "
  863. << mMessageBuilder->getMessageName() << " to dead circuit "
  864. << host << llendl;
  865. return 0;
  866. }
  867. // NOTE: babbage: LLSD message -> HTTP, template message -> UDP
  868. if (mMessageBuilder == mLLSDMessageBuilder)
  869. {
  870. UntrustedCallback_t cb = NULL;
  871. if (mSendReliable && mReliablePacketParams.mCallback)
  872. {
  873. cb = boost::bind(mReliablePacketParams.mCallback,
  874. mReliablePacketParams.mCallbackData, _1);
  875. }
  876. gCoros.launch("LLMessageSystem::sendUntrustedSimulatorMessageCoro",
  877. boost::bind(&LLMessageSystem::sendUntrustedSimulatorMessageCoro,
  878. this, host,
  879. mLLSDMessageBuilder->getMessageName(),
  880. mLLSDMessageBuilder->getMessage(), cb));
  881. mSendReliable = false;
  882. mReliablePacketParams.clear();
  883. return 1;
  884. }
  885. const char* msg_name = mMessageBuilder->getMessageName();
  886. if (msg_name != _PREHASH_PacketAck)
  887. {
  888. LL_DEBUGS("Messaging") << "Sending " << msg_name
  889. << " to host " << host.getIPandPort()
  890. << LL_ENDL;
  891. }
  892. // Zero out the flags and packetid. Subtract 1 here so that we do not
  893. // overwrite the offset if it was set set in buildMessage().
  894. memset(mSendBuffer, 0, LL_PACKET_ID_SIZE - 1);
  895. // Add the send id to the front of the message
  896. cdp->nextPacketOutID();
  897. // Packet ID size is always 4
  898. *((S32*)&mSendBuffer[PHL_PACKET_ID]) = htonl(cdp->getPacketOutID());
  899. // Compress the message, which will usually reduce its size.
  900. U8* buf_ptr = (U8*)mSendBuffer;
  901. U32 buffer_length = mSendSize;
  902. mMessageBuilder->compressMessage(buf_ptr, buffer_length);
  903. if (buffer_length > 1500)
  904. {
  905. if (msg_name != _PREHASH_ChildAgentUpdate &&
  906. msg_name != _PREHASH_SendXferPacket)
  907. {
  908. llwarns << "Trying to send "
  909. << (buffer_length > 4000 ? "EXTRA " : "")
  910. << "BIG message " << msg_name
  911. << " - " << buffer_length << llendl;
  912. }
  913. }
  914. if (mSendReliable)
  915. {
  916. buf_ptr[0] |= LL_RELIABLE_FLAG;
  917. if (!cdp->getUnackedPacketCount())
  918. {
  919. // We are adding the first packed onto the unacked packet list(s)
  920. // Add this circuit to the list of circuits with unacked packets
  921. mCircuitInfo.mUnackedCircuitMap[cdp->mHost] = cdp;
  922. }
  923. cdp->addReliablePacket(mSocket, buf_ptr, buffer_length,
  924. &mReliablePacketParams);
  925. ++mReliablePacketsOut;
  926. }
  927. // Tack packet acks onto the end of this message
  928. // Space left for packet ids:
  929. S32 space_left = (MTUBYTES - buffer_length) / sizeof(TPACKETID);
  930. S32 ack_count = (S32)cdp->mAcks.size();
  931. bool is_ack_appended = false;
  932. std::vector<TPACKETID> acks;
  933. if (space_left > 0 && ack_count > 0 && msg_name != _PREHASH_PacketAck)
  934. {
  935. buf_ptr[0] |= LL_ACK_FLAG;
  936. S32 append_ack_count = llmin(space_left, ack_count);
  937. constexpr S32 MAX_ACKS = 250;
  938. append_ack_count = llmin(append_ack_count, MAX_ACKS);
  939. std::vector<TPACKETID>::iterator iter = cdp->mAcks.begin();
  940. std::vector<TPACKETID>::iterator last = cdp->mAcks.begin();
  941. last += append_ack_count;
  942. TPACKETID packet_id;
  943. for ( ; iter != last ; ++iter)
  944. {
  945. // Grab the next packet id.
  946. packet_id = (*iter);
  947. if (mVerboseLog)
  948. {
  949. acks.push_back(packet_id);
  950. }
  951. // Put it on the end of the buffer
  952. packet_id = htonl(packet_id);
  953. if ((S32)(buffer_length + sizeof(TPACKETID)) < MAX_BUFFER_SIZE)
  954. {
  955. memcpy(&buf_ptr[buffer_length], &packet_id, sizeof(TPACKETID));
  956. // Do the accounting
  957. buffer_length += sizeof(TPACKETID);
  958. }
  959. else
  960. {
  961. // Just reporting error is likely not enough. Need to check how
  962. // to abort or error out gracefully from this function. XXXTBD
  963. // *NOTE: actually, hitting this error would indicate that the
  964. // calculation above for space_left, ack_count,
  965. // append_acout_count is incorrect or that MAX_BUFFER_SIZE has
  966. // fallen below MTU which is bad and probably programmer error.
  967. llerrs << "Buffer packing failed due to size." << llendl;
  968. }
  969. }
  970. // Clean up the source
  971. cdp->mAcks.erase(cdp->mAcks.begin(), last);
  972. // Tack the count in the final byte
  973. U8 count = (U8)append_ack_count;
  974. buf_ptr[buffer_length++] = count;
  975. is_ack_appended = true;
  976. }
  977. if (mPacketRing.sendPacket(mSocket, (char*)buf_ptr, buffer_length, host))
  978. {
  979. // mCircuitInfo already points to the correct circuit data
  980. cdp->addBytesOut(buffer_length);
  981. }
  982. else
  983. {
  984. ++mSendPacketFailureCount;
  985. }
  986. if (mVerboseLog)
  987. {
  988. std::ostringstream str;
  989. str << "MSG: -> " << host;
  990. std::string buffer;
  991. buffer = llformat("\t%6d\t%6d\t%6d ", mSendSize, buffer_length,
  992. cdp->getPacketOutID());
  993. str << buffer << msg_name << (mSendReliable ? " reliable " : "");
  994. if (is_ack_appended)
  995. {
  996. str << "\tACKS:\t";
  997. std::ostream_iterator<TPACKETID> append(str, " ");
  998. std::copy(acks.begin(), acks.end(), append);
  999. }
  1000. llinfos << str.str() << llendl;
  1001. }
  1002. ++mPacketsOut;
  1003. mTotalBytesOut += buffer_length;
  1004. mSendReliable = false;
  1005. mReliablePacketParams.clear();
  1006. return buffer_length;
  1007. }
  1008. void LLMessageSystem::logMsgFromInvalidCircuit(const LLHost& host,
  1009. bool recv_reliable)
  1010. {
  1011. if (mVerboseLog)
  1012. {
  1013. std::ostringstream str;
  1014. str << "MSG: <- " << host;
  1015. std::string buffer = llformat("\t%6d\t%6d\t%6d ",
  1016. mMessageReader->getMessageSize(),
  1017. mIncomingCompressedSize ?
  1018. mIncomingCompressedSize :
  1019. mMessageReader->getMessageSize(),
  1020. mCurrentRecvPacketID);
  1021. str << buffer << nullToEmpty(mMessageReader->getMessageName())
  1022. << (recv_reliable ? " reliable" : "") << " REJECTED";
  1023. llinfos << str.str() << llendl;
  1024. }
  1025. // Keep track of rejected messages as well
  1026. if (mNumMessageCounts >= MAX_MESSAGE_COUNT_NUM)
  1027. {
  1028. llwarns << "Got more than " << MAX_MESSAGE_COUNT_NUM
  1029. << " packets without clearing counts" << llendl;
  1030. }
  1031. else
  1032. {
  1033. #if 0 // *TODO: babbage: work out if we need these
  1034. mMessageCountList[mNumMessageCounts].mMessageNum =
  1035. mCurrentRMessageTemplate->mMessageNumber;
  1036. #endif
  1037. mMessageCountList[mNumMessageCounts].mMessageBytes =
  1038. mMessageReader->getMessageSize();
  1039. mMessageCountList[mNumMessageCounts++].mInvalid = true;
  1040. }
  1041. }
  1042. S32 LLMessageSystem::sendMessage(const LLHost& host, const char* name,
  1043. const LLSD& message)
  1044. {
  1045. if (!host.isOk())
  1046. {
  1047. llwarns << "trying to send message to invalid host" << llendl;
  1048. return 0;
  1049. }
  1050. UntrustedCallback_t cb = NULL;
  1051. if (mSendReliable && mReliablePacketParams.mCallback)
  1052. {
  1053. cb = boost::bind(mReliablePacketParams.mCallback,
  1054. mReliablePacketParams.mCallbackData, _1);
  1055. }
  1056. gCoros.launch("LLMessageSystem::sendUntrustedSimulatorMessageCoro",
  1057. boost::bind(&LLMessageSystem::sendUntrustedSimulatorMessageCoro,
  1058. this, host, name, message, cb));
  1059. return 1;
  1060. }
  1061. void LLMessageSystem::logTrustedMsgFromUntrustedCircuit(const LLHost& host)
  1062. {
  1063. // RequestTrustedCircuit is how we establish trust, so do not spam if it is
  1064. // received on a trusted circuit. JC
  1065. if (strcmp(mMessageReader->getMessageName(),
  1066. _PREHASH_RequestTrustedCircuit))
  1067. {
  1068. llwarns << "Received trusted message on untrusted circuit. "
  1069. << "Will reply with deny. "
  1070. << "Message: " << nullToEmpty(mMessageReader->getMessageName())
  1071. << " Host: " << host << llendl;
  1072. }
  1073. if (mNumMessageCounts >= MAX_MESSAGE_COUNT_NUM)
  1074. {
  1075. llwarns << "got more than " << MAX_MESSAGE_COUNT_NUM
  1076. << " packets without clearing counts" << llendl;
  1077. }
  1078. else
  1079. {
  1080. #if 0 // *TODO: Babbage: work out if we need these
  1081. mMessageCountList[mNumMessageCounts].mMessageNum =
  1082. mCurrentRMessageTemplate->mMessageNumber;
  1083. #endif
  1084. mMessageCountList[mNumMessageCounts].mMessageBytes =
  1085. mMessageReader->getMessageSize();
  1086. mMessageCountList[mNumMessageCounts++].mInvalid = true;
  1087. }
  1088. }
  1089. void LLMessageSystem::logValidMsg(LLCircuitData* cdp, const LLHost& host,
  1090. bool recv_reliable, bool recv_resent,
  1091. bool recv_acks)
  1092. {
  1093. if (mNumMessageCounts >= MAX_MESSAGE_COUNT_NUM)
  1094. {
  1095. llwarns << "Got more than " << MAX_MESSAGE_COUNT_NUM
  1096. << " packets without clearing counts" << llendl;
  1097. }
  1098. else
  1099. {
  1100. #if 0 // *TODO: Babbage: work out if we need these
  1101. mMessageCountList[mNumMessageCounts].mMessageNum =
  1102. mCurrentRMessageTemplate->mMessageNumber;
  1103. #endif
  1104. mMessageCountList[mNumMessageCounts].mMessageBytes =
  1105. mMessageReader->getMessageSize();
  1106. mMessageCountList[mNumMessageCounts++].mInvalid = false;
  1107. }
  1108. if (cdp)
  1109. {
  1110. // Update circuit packet ID tracking (missing/out of order packets)
  1111. cdp->checkPacketInID(mCurrentRecvPacketID, recv_resent);
  1112. cdp->addBytesIn(mTrueReceiveSize);
  1113. }
  1114. if (mVerboseLog)
  1115. {
  1116. std::ostringstream str;
  1117. str << "MSG: <- " << host;
  1118. std::string buffer = llformat("\t%6d\t%6d\t%6d ",
  1119. mMessageReader->getMessageSize(),
  1120. mIncomingCompressedSize ?
  1121. mIncomingCompressedSize :
  1122. mMessageReader->getMessageSize(),
  1123. mCurrentRecvPacketID);
  1124. str << buffer << nullToEmpty(mMessageReader->getMessageName())
  1125. << (recv_reliable ? " reliable" : "")
  1126. << (recv_resent ? " resent" : "") << (recv_acks ? " acks" : "");
  1127. llinfos << str.str() << llendl;
  1128. }
  1129. }
  1130. void LLMessageSystem::getCircuitInfo(LLSD& info) const
  1131. {
  1132. mCircuitInfo.getInfo(info);
  1133. }
  1134. // Activates a circuit, and set its trust level (true if trusted, false if not)
  1135. void LLMessageSystem::enableCircuit(const LLHost& host, bool trusted)
  1136. {
  1137. LLCircuitData* cdp = mCircuitInfo.findCircuit(host);
  1138. if (!cdp)
  1139. {
  1140. cdp = mCircuitInfo.addCircuitData(host, 0);
  1141. }
  1142. else
  1143. {
  1144. cdp->setAlive(true);
  1145. }
  1146. cdp->setTrusted(trusted);
  1147. }
  1148. void LLMessageSystem::disableCircuit(const LLHost& host)
  1149. {
  1150. llinfos << "Disabling " << host << llendl;
  1151. LLMessageSystem* msg = gMessageSystemp;
  1152. U32 code = msg->findCircuitCode(host);
  1153. // Do not clean up 0 circuit code entries because many hosts (neighbor
  1154. // sims, etc) can have the 0 circuit
  1155. if (code)
  1156. {
  1157. code_session_map_t::iterator it = mCircuitCodes.find(code);
  1158. if (it != mCircuitCodes.end())
  1159. {
  1160. llinfos << "Circuit " << code << " removed from list" << llendl;
  1161. mCircuitCodes.hmap_erase(it);
  1162. }
  1163. U64 ip_port = 0;
  1164. fast_hmap<U32, U64>::iterator iter =
  1165. msg->mCircuitCodeToIPPort.find(code);
  1166. if (iter != msg->mCircuitCodeToIPPort.end())
  1167. {
  1168. ip_port = iter->second;
  1169. msg->mCircuitCodeToIPPort.erase(iter);
  1170. U32 old_port = (U32)(ip_port & (U64)0xFFFFFFFF);
  1171. U32 old_ip = (U32)(ip_port >> 32);
  1172. llinfos << "Host " << LLHost(old_ip, old_port) << " circuit "
  1173. << code << " removed from lookup table" << llendl;
  1174. msg->mIPPortToCircuitCode.erase(ip_port);
  1175. }
  1176. mCircuitInfo.removeCircuitData(host);
  1177. }
  1178. else
  1179. {
  1180. // Since we can open circuits which do not have circuit codes, it is
  1181. // possible for this to happen...
  1182. llinfos << "Could not find circuit code for " << host
  1183. << ", ignoring..." << llendl;
  1184. }
  1185. }
  1186. void LLMessageSystem::setCircuitAllowTimeout(const LLHost& host, bool allow)
  1187. {
  1188. LLCircuitData* cdp = mCircuitInfo.findCircuit(host);
  1189. if (cdp)
  1190. {
  1191. cdp->setAllowTimeout(allow);
  1192. }
  1193. }
  1194. void LLMessageSystem::setCircuitTimeoutCallback(const LLHost& host,
  1195. void (*callback_func)(const LLHost&,
  1196. void*),
  1197. void* user_data)
  1198. {
  1199. LLCircuitData* cdp = mCircuitInfo.findCircuit(host);
  1200. if (cdp)
  1201. {
  1202. cdp->setTimeoutCallback(callback_func, user_data);
  1203. }
  1204. }
  1205. bool LLMessageSystem::checkCircuitBlocked(U32 circuit)
  1206. {
  1207. LLHost host = findHost(circuit);
  1208. if (!host.isOk())
  1209. {
  1210. LL_DEBUGS("Messaging") << "Unknown circuit: " << circuit << LL_ENDL;
  1211. return true;
  1212. }
  1213. LLCircuitData* cdp = mCircuitInfo.findCircuit(host);
  1214. if (cdp)
  1215. {
  1216. return cdp->isBlocked();
  1217. }
  1218. llinfos << "Unknown host: " << host << llendl;
  1219. return false;
  1220. }
  1221. bool LLMessageSystem::checkCircuitAlive(U32 circuit)
  1222. {
  1223. LLHost host = findHost(circuit);
  1224. if (!host.isOk())
  1225. {
  1226. LL_DEBUGS("Messaging") << "Unknown circuit: " << circuit << LL_ENDL;
  1227. return false;
  1228. }
  1229. LLCircuitData* cdp = mCircuitInfo.findCircuit(host);
  1230. if (cdp)
  1231. {
  1232. return cdp->isAlive();
  1233. }
  1234. llinfos << "Unknown host: " << host << llendl;
  1235. return false;
  1236. }
  1237. bool LLMessageSystem::checkCircuitAlive(const LLHost& host)
  1238. {
  1239. LLCircuitData* cdp = mCircuitInfo.findCircuit(host);
  1240. if (cdp)
  1241. {
  1242. return cdp->isAlive();
  1243. }
  1244. LL_DEBUGS("Messaging") << "Unknown host: " << host << LL_ENDL;
  1245. return false;
  1246. }
  1247. U32 LLMessageSystem::findCircuitCode(const LLHost& host)
  1248. {
  1249. U64 ip64 = (U64)host.getAddress();
  1250. U64 port64 = (U64)host.getPort();
  1251. U64 ip_port = (ip64 << 32) | port64;
  1252. return get_if_there(mIPPortToCircuitCode, ip_port, U32(0));
  1253. }
  1254. LLHost LLMessageSystem::findHost(U32 circuit_code)
  1255. {
  1256. if (!mCircuitCodeToIPPort.count(circuit_code))
  1257. {
  1258. return LLHost();
  1259. }
  1260. return LLHost(mCircuitCodeToIPPort[circuit_code]);
  1261. }
  1262. std::ostream& operator<<(std::ostream& s, LLMessageSystem& msg)
  1263. {
  1264. U32 i;
  1265. if (msg.mError)
  1266. {
  1267. s << "Message system not correctly initialized";
  1268. }
  1269. else
  1270. {
  1271. s << "Message system open on port " << msg.mPort << " and socket "
  1272. << msg.mSocket << "\n";
  1273. #if 0
  1274. s << "Message template file " << msg.mName << " loaded\n";
  1275. #endif
  1276. s << "\nHigh frequency messages:\n";
  1277. for (i = 1; i < 255 && msg.mMessageNumbers[i]; ++i)
  1278. {
  1279. s << *(msg.mMessageNumbers[i]);
  1280. }
  1281. s << "\nMedium frequency messages:\n";
  1282. for (i = (255 << 8) + 1;
  1283. i < (255 << 8) + 255 && msg.mMessageNumbers[i]; ++i)
  1284. {
  1285. s << *msg.mMessageNumbers[i];
  1286. }
  1287. s << "\nLow frequency messages:\n";
  1288. for (i = 0xFFFF0001;
  1289. i < 0xFFFFFFFF && msg.mMessageNumbers[i]; ++i)
  1290. {
  1291. s << *msg.mMessageNumbers[i];
  1292. }
  1293. }
  1294. return s;
  1295. }
  1296. LLMessageSystem* gMessageSystemp = NULL;
  1297. // Update appropriate ping info
  1298. void process_complete_ping_check(LLMessageSystem* msgsystem, void**)
  1299. {
  1300. U8 ping_id;
  1301. msgsystem->getU8Fast(_PREHASH_PingID, _PREHASH_PingID, ping_id);
  1302. LLCircuitData* cdp;
  1303. cdp = msgsystem->mCircuitInfo.findCircuit(msgsystem->getSender());
  1304. if (cdp)
  1305. {
  1306. // Stop the appropriate timer
  1307. cdp->pingTimerStop(ping_id);
  1308. }
  1309. }
  1310. void process_start_ping_check(LLMessageSystem* msgsystem, void**)
  1311. {
  1312. U8 ping_id;
  1313. msgsystem->getU8Fast(_PREHASH_PingID, _PREHASH_PingID, ping_id);
  1314. LLCircuitData* cdp;
  1315. cdp = msgsystem->mCircuitInfo.findCircuit(msgsystem->getSender());
  1316. if (cdp)
  1317. {
  1318. // Grab the packet id of the oldest unacked packet
  1319. U32 packet_id;
  1320. msgsystem->getU32Fast(_PREHASH_PingID, _PREHASH_OldestUnacked,
  1321. packet_id);
  1322. cdp->clearDuplicateList(packet_id);
  1323. }
  1324. // Send off the response
  1325. msgsystem->newMessageFast(_PREHASH_CompletePingCheck);
  1326. msgsystem->nextBlockFast(_PREHASH_PingID);
  1327. msgsystem->addU8(_PREHASH_PingID, ping_id);
  1328. msgsystem->sendMessage(msgsystem->getSender());
  1329. }
  1330. // Note: this is currently unused. --mark
  1331. void open_circuit(LLMessageSystem* msgsystem, void**)
  1332. {
  1333. U32 ip;
  1334. msgsystem->getIPAddrFast(_PREHASH_CircuitInfo, _PREHASH_IP, ip);
  1335. U16 port;
  1336. msgsystem->getIPPortFast(_PREHASH_CircuitInfo, _PREHASH_Port, port);
  1337. // By default, OpenCircuit's are untrusted
  1338. msgsystem->enableCircuit(LLHost(ip, port), false);
  1339. }
  1340. void close_circuit(LLMessageSystem* msgsystem, void**)
  1341. {
  1342. msgsystem->disableCircuit(msgsystem->getSender());
  1343. }
  1344. // static
  1345. void LLMessageSystem::processAddCircuitCode(LLMessageSystem* msg, void**)
  1346. {
  1347. U32 code;
  1348. msg->getU32Fast(_PREHASH_CircuitCode, _PREHASH_Code, code);
  1349. LLUUID session_id;
  1350. msg->getUUIDFast(_PREHASH_CircuitCode, _PREHASH_SessionID, session_id);
  1351. (void)msg->addCircuitCode(code, session_id);
  1352. }
  1353. bool LLMessageSystem::addCircuitCode(U32 code, const LLUUID& session_id)
  1354. {
  1355. if (!code)
  1356. {
  1357. llwarns << "Zero circuit code" << llendl;
  1358. return false;
  1359. }
  1360. code_session_map_t::iterator it = mCircuitCodes.find(code);
  1361. if (it == mCircuitCodes.end())
  1362. {
  1363. llinfos << "New circuit code " << code << " added" << llendl;
  1364. mCircuitCodes[code] = session_id;
  1365. }
  1366. else
  1367. {
  1368. llinfos << "Duplicate circuit code " << code << " added" << llendl;
  1369. }
  1370. return true;
  1371. }
  1372. // static
  1373. void LLMessageSystem::processUseCircuitCode(LLMessageSystem* msg,
  1374. void** user)
  1375. {
  1376. U32 circuit_code_in;
  1377. msg->getU32Fast(_PREHASH_CircuitCode, _PREHASH_Code, circuit_code_in);
  1378. U32 ip = msg->getSenderIP();
  1379. U32 port = msg->getSenderPort();
  1380. U64 ip64 = ip;
  1381. U64 port64 = port;
  1382. U64 ip_port_in = (ip64 << 32) | port64;
  1383. if (!circuit_code_in)
  1384. {
  1385. llwarns << "Got zero circuit code in use_circuit_code" << llendl;
  1386. return;
  1387. }
  1388. code_session_map_t::iterator it;
  1389. it = msg->mCircuitCodes.find(circuit_code_in);
  1390. if (it == msg->mCircuitCodes.end())
  1391. {
  1392. // Whoah, abort ! We do not know anything about this circuit code.
  1393. llwarns << "UseCircuitCode for " << circuit_code_in
  1394. << " received without AddCircuitCode message. Aborting."
  1395. << llendl;
  1396. return;
  1397. }
  1398. LLUUID id;
  1399. msg->getUUIDFast(_PREHASH_CircuitCode, _PREHASH_ID, id);
  1400. LLUUID session_id;
  1401. msg->getUUIDFast(_PREHASH_CircuitCode, _PREHASH_SessionID, session_id);
  1402. if (session_id != it->second)
  1403. {
  1404. llwarns << "UseCircuitCode unmatched session id. Got " << session_id
  1405. << " but expected " << it->second << llendl;
  1406. return;
  1407. }
  1408. // Clean up previous references to this ip/port or circuit
  1409. U64 ip_port_old = get_if_there(msg->mCircuitCodeToIPPort,
  1410. circuit_code_in, U64(0));
  1411. U32 circuit_code_old = get_if_there(msg->mIPPortToCircuitCode,
  1412. ip_port_in, U32(0));
  1413. if (ip_port_old)
  1414. {
  1415. if (ip_port_old == ip_port_in && circuit_code_old == circuit_code_in)
  1416. {
  1417. // Current information is the same as incoming info, ignore
  1418. llinfos << "Got duplicate UseCircuitCode for circuit "
  1419. << circuit_code_in << " to " << msg->getSender() << llendl;
  1420. return;
  1421. }
  1422. // Hmm, got a different IP and port for the same circuit code.
  1423. U32 circut_code_old_ip_port = get_if_there(msg->mIPPortToCircuitCode,
  1424. ip_port_old, U32(0));
  1425. msg->mCircuitCodeToIPPort.erase(circut_code_old_ip_port);
  1426. msg->mIPPortToCircuitCode.erase(ip_port_old);
  1427. U32 old_port = (U32)(ip_port_old & (U64)0xFFFFFFFF);
  1428. U32 old_ip = (U32)(ip_port_old >> 32);
  1429. llinfos << "Removing derelict lookup entry for circuit "
  1430. << circuit_code_old << " to " << LLHost(old_ip, old_port)
  1431. << llendl;
  1432. }
  1433. if (circuit_code_old)
  1434. {
  1435. LLHost cur_host(ip, port);
  1436. llwarns << "Disabling existing circuit for " << cur_host << llendl;
  1437. msg->disableCircuit(cur_host);
  1438. if (circuit_code_old == circuit_code_in)
  1439. {
  1440. llwarns << "Asymmetrical circuit to IP/port lookup ! Multiple circuit codes for "
  1441. << cur_host
  1442. << ", probably... Permanently disabling circuit."
  1443. << llendl;
  1444. return;
  1445. }
  1446. llwarns << "Circuit code changed for " << msg->getSender() << " from "
  1447. << circuit_code_old << " to " << circuit_code_in << llendl;
  1448. }
  1449. // Since this comes from the viewer, it is untrusted, but it passed the
  1450. // circuit code and session id check, so we will go ahead and persist the
  1451. // Id associated.
  1452. LLCircuitData* cdp = msg->mCircuitInfo.findCircuit(msg->getSender());
  1453. bool had_circuit_already = cdp != NULL;
  1454. msg->enableCircuit(msg->getSender(), false);
  1455. cdp = msg->mCircuitInfo.findCircuit(msg->getSender());
  1456. if (cdp)
  1457. {
  1458. cdp->setRemoteID(id);
  1459. cdp->setRemoteSessionID(session_id);
  1460. }
  1461. if (!had_circuit_already)
  1462. {
  1463. // *HACK: this would NORMALLY happen inside logValidMsg, but at the
  1464. // point that this happens inside logValidMsg, there's no circuit for
  1465. // this message yet. So the awful thing that we do here is do it inside
  1466. // this message handler immediately AFTER the message is handled. We
  1467. // COULD not do this, but then what happens is that some of the circuit
  1468. // bookkeeping gets broken, especially the packets in count. That
  1469. // causes some later packets to flush the RecentlyReceivedReliable
  1470. // list, resulting in an error in which UseCircuitCode does not get
  1471. // properly duplicate suppressed. Not a BIG deal, but it's somewhat
  1472. // confusing (and bad from a state point of view). DJS 9/23/04
  1473. // Since this is the first message on the circuit, by definition it is
  1474. // not resent.
  1475. cdp->checkPacketInID(msg->mCurrentRecvPacketID, false);
  1476. }
  1477. msg->mIPPortToCircuitCode[ip_port_in] = circuit_code_in;
  1478. msg->mCircuitCodeToIPPort[circuit_code_in] = ip_port_in;
  1479. llinfos << "Circuit code " << circuit_code_in << " from "
  1480. << msg->getSender() << " for agent " << id << " in session "
  1481. << session_id << llendl;
  1482. const LLUseCircuitCodeResponder* responder =
  1483. (const LLUseCircuitCodeResponder*)user;
  1484. if (responder)
  1485. {
  1486. responder->complete(msg->getSender(), id);
  1487. }
  1488. }
  1489. // static
  1490. void LLMessageSystem::processError(LLMessageSystem* msg, void**)
  1491. {
  1492. S32 error_code = 0;
  1493. msg->getS32("Data", "Code", error_code);
  1494. std::string error_token;
  1495. msg->getString("Data", "Token", error_token);
  1496. LLUUID error_id;
  1497. msg->getUUID("Data", "ID", error_id);
  1498. std::string error_system;
  1499. msg->getString("Data", "System", error_system);
  1500. std::string error_message;
  1501. msg->getString("Data", "Message", error_message);
  1502. llwarns << "Message error from " << msg->getSender() << " - "
  1503. << error_code << " " << error_token << " " << error_id << " \""
  1504. << error_system << "\" \"" << error_message << "\"" << llendl;
  1505. }
  1506. static LLHTTPNode& messageRootNode()
  1507. {
  1508. static LLHTTPNode root_node;
  1509. static bool initialized = false;
  1510. if (!initialized)
  1511. {
  1512. initialized = true;
  1513. LLHTTPRegistrar::buildAllServices(root_node);
  1514. }
  1515. return root_node;
  1516. }
  1517. //static
  1518. void LLMessageSystem::dispatch(const std::string& msg_name,
  1519. const LLSD& message)
  1520. {
  1521. LLPointer<LLSimpleResponse> responsep = LLSimpleResponse::create();
  1522. dispatch(msg_name, message, responsep);
  1523. }
  1524. //static
  1525. void LLMessageSystem::dispatch(const std::string& msg_name,
  1526. const LLSD& message,
  1527. LLHTTPNode::ResponsePtr responsep)
  1528. {
  1529. LLMessageSystem* msg = gMessageSystemp;
  1530. if (msg->mMessageTemplates.find(gMessageStringTable.getString(msg_name.c_str())) ==
  1531. msg->mMessageTemplates.end() &&
  1532. !LLMessageConfig::isValidMessage(msg_name))
  1533. {
  1534. llwarns << "Ignoring unknown message " << msg_name << llendl;
  1535. responsep->notFound("Invalid message name");
  1536. return;
  1537. }
  1538. std::string path = "/message/" + msg_name;
  1539. LLSD context;
  1540. const LLHTTPNode* handler = messageRootNode().traverse(path, context);
  1541. if (!handler)
  1542. {
  1543. llwarns << "No handler for " << path << llendl;
  1544. return;
  1545. }
  1546. LL_DEBUGS("Messaging") << "Received: " << msg_name << " from "
  1547. << message["sender"].asString() << LL_ENDL;
  1548. handler->post(responsep, context, message);
  1549. }
  1550. static void check_for_unknown_msg(const char* type, const LLSD& map,
  1551. LLMessageSystem::template_name_map_t& templates)
  1552. {
  1553. for (LLSD::map_const_iterator iter = map.beginMap(), end = map.endMap();
  1554. iter != end; ++iter)
  1555. {
  1556. const char* name = gMessageStringTable.getString(iter->first.c_str());
  1557. if (templates.find(name) == templates.end())
  1558. {
  1559. llinfos << "Ban list type " << type
  1560. << " contains unrecognized message " << name << llendl;
  1561. }
  1562. }
  1563. }
  1564. void LLMessageSystem::setMessageBans(const LLSD& trusted,
  1565. const LLSD& untrusted)
  1566. {
  1567. LL_DEBUGS("AppInit") << "Setting message bans" << LL_ENDL;
  1568. bool any_set = false;
  1569. for (template_name_map_t::iterator iter = mMessageTemplates.begin(),
  1570. end = mMessageTemplates.end();
  1571. iter != end; ++iter)
  1572. {
  1573. LLMessageTemplate* mt = iter->second;
  1574. std::string name(mt->mName);
  1575. bool ban_from_trusted = trusted.has(name) &&
  1576. trusted.get(name).asBoolean();
  1577. bool ban_from_untrusted = untrusted.has(name) &&
  1578. untrusted.get(name).asBoolean();
  1579. mt->mBanFromTrusted = ban_from_trusted;
  1580. mt->mBanFromUntrusted = ban_from_untrusted;
  1581. if (ban_from_trusted || ban_from_untrusted)
  1582. {
  1583. llinfos << name << " banned from "
  1584. << (ban_from_trusted ? "TRUSTED " : " ")
  1585. << (ban_from_untrusted ? "UNTRUSTED " : " ") << llendl;
  1586. any_set = true;
  1587. }
  1588. }
  1589. if (!any_set)
  1590. {
  1591. LL_DEBUGS("AppInit") << "No messages banned" << LL_ENDL;
  1592. }
  1593. check_for_unknown_msg("trusted", trusted, mMessageTemplates);
  1594. check_for_unknown_msg("untrusted", untrusted, mMessageTemplates);
  1595. }
  1596. S32 LLMessageSystem::sendError(const LLHost& host, const LLUUID& agent_id,
  1597. S32 code, const std::string& token,
  1598. const LLUUID& id, const std::string& system,
  1599. const std::string& message, const LLSD& data)
  1600. {
  1601. newMessage("Error");
  1602. nextBlockFast(_PREHASH_AgentData);
  1603. addUUIDFast(_PREHASH_AgentID, agent_id);
  1604. nextBlockFast(_PREHASH_Data);
  1605. addS32("Code", code);
  1606. addString("Token", token);
  1607. addUUID("ID", id);
  1608. addString("System", system);
  1609. std::string temp;
  1610. temp = message;
  1611. if (temp.size() > (size_t)MTUBYTES)
  1612. {
  1613. temp.resize((size_t)MTUBYTES);
  1614. }
  1615. addString("Message", message);
  1616. LLPointer<LLSDBinaryFormatter> formatter = new LLSDBinaryFormatter;
  1617. std::ostringstream ostr;
  1618. formatter->format(data, ostr);
  1619. temp = ostr.str();
  1620. bool pack_data = true;
  1621. static const std::string ERROR_MESSAGE_NAME("Error");
  1622. if (LLMessageConfig::getMessageFlavor(ERROR_MESSAGE_NAME) ==
  1623. LLMessageConfig::TEMPLATE_FLAVOR)
  1624. {
  1625. S32 msg_size = temp.size() + mMessageBuilder->getMessageSize();
  1626. if (msg_size >= ETHERNET_MTU_BYTES)
  1627. {
  1628. pack_data = false;
  1629. }
  1630. }
  1631. if (pack_data)
  1632. {
  1633. addBinaryData("Data", (void*)temp.c_str(), temp.size());
  1634. }
  1635. else
  1636. {
  1637. llwarns << "Data and message were too large; data removed." << llendl;
  1638. addBinaryData("Data", NULL, 0);
  1639. }
  1640. return sendReliable(host);
  1641. }
  1642. void process_packet_ack(LLMessageSystem* msgsystem, void**)
  1643. {
  1644. LLHost host = msgsystem->getSender();
  1645. LLCircuitData* cdp = msgsystem->mCircuitInfo.findCircuit(host);
  1646. if (!cdp)
  1647. {
  1648. return;
  1649. }
  1650. TPACKETID packet_id;
  1651. S32 ack_count = msgsystem->getNumberOfBlocksFast(_PREHASH_Packets);
  1652. for (S32 i = 0; i < ack_count; i++)
  1653. {
  1654. msgsystem->getU32Fast(_PREHASH_Packets, _PREHASH_ID, packet_id, i);
  1655. cdp->ackReliablePacket(packet_id);
  1656. }
  1657. if (!cdp->getUnackedPacketCount())
  1658. {
  1659. // Remove this circuit from the list of circuits with unacked packets
  1660. msgsystem->mCircuitInfo.mUnackedCircuitMap.erase(host);
  1661. }
  1662. }
  1663. bool start_messaging_system(const std::string& template_name, U32 port,
  1664. S32 major, S32 minor, S32 patch,
  1665. const LLUseCircuitCodeResponder* responder,
  1666. F32 heartbeat_interval, F32 timeout)
  1667. {
  1668. gMessageSystemp = new LLMessageSystem(template_name, port, major, minor,
  1669. patch, heartbeat_interval, timeout);
  1670. if (!gMessageSystemp)
  1671. {
  1672. llerrs << "Messaging system initialization failed." << llendl;
  1673. }
  1674. // Bail if system encountered an error.
  1675. if (!gMessageSystemp->isOK())
  1676. {
  1677. return false;
  1678. }
  1679. if (gMessageSystemp->mMessageFileVersionNumber != gPrehashVersionNumber)
  1680. {
  1681. llinfos << "Message template version does not match prehash version number. Run simulator with -prehash command line option to rebuild prehash data"
  1682. << llendl;
  1683. }
  1684. else
  1685. {
  1686. LL_DEBUGS("AppInit") << "Message template version matches prehash version number"
  1687. << LL_ENDL;
  1688. }
  1689. LLMessageSystem* msg = gMessageSystemp;
  1690. msg->setHandlerFuncFast(_PREHASH_StartPingCheck,
  1691. process_start_ping_check, NULL);
  1692. msg->setHandlerFuncFast(_PREHASH_CompletePingCheck,
  1693. process_complete_ping_check, NULL);
  1694. msg->setHandlerFuncFast(_PREHASH_OpenCircuit, open_circuit, NULL);
  1695. msg->setHandlerFuncFast(_PREHASH_CloseCircuit, close_circuit, NULL);
  1696. msg->setHandlerFuncFast(_PREHASH_AddCircuitCode,
  1697. LLMessageSystem::processAddCircuitCode);
  1698. msg->setHandlerFuncFast(_PREHASH_UseCircuitCode,
  1699. LLMessageSystem::processUseCircuitCode,
  1700. (void**)responder);
  1701. msg->setHandlerFuncFast(_PREHASH_PacketAck, process_packet_ack, NULL);
  1702. // These two are only relevant to SL servers and rely on a "shared secret"
  1703. // (a MD5 sum) that we always passed as a empty string in the viewer. So do
  1704. // not bother with them and replace the server-specific callbacks (which I
  1705. // removed from this module) with a null callback. HB
  1706. msg->setHandlerFuncFast(_PREHASH_CreateTrustedCircuit,
  1707. null_message_callback, NULL);
  1708. msg->setHandlerFuncFast(_PREHASH_DenyTrustedCircuit,
  1709. null_message_callback, NULL);
  1710. msg->setHandlerFunc("Error", LLMessageSystem::processError);
  1711. // We can hand this to the null_message_callback since it is a trusted
  1712. // message, so it will automatically be denied if it is not trusted and
  1713. // ignored if it is: exactly what we want.
  1714. msg->setHandlerFunc(_PREHASH_RequestTrustedCircuit, null_message_callback, NULL);
  1715. // Initialize the transfer manager
  1716. gTransferManager.init();
  1717. return true;
  1718. }
  1719. void LLMessageSystem::startLogging()
  1720. {
  1721. mVerboseLog = true;
  1722. std::ostringstream str;
  1723. str << "START MESSAGE LOG" << std::endl;
  1724. str << "Legend:" << std::endl;
  1725. str << "\t<-\tincoming message" <<std::endl;
  1726. str << "\t->\toutgoing message" << std::endl;
  1727. str << " <> host size zero id name";
  1728. llinfos << str.str() << llendl;
  1729. }
  1730. void LLMessageSystem::stopLogging()
  1731. {
  1732. if (mVerboseLog)
  1733. {
  1734. mVerboseLog = false;
  1735. llinfos << "END MESSAGE LOG" << llendl;
  1736. }
  1737. }
  1738. void LLMessageSystem::summarizeLogs(std::ostream& str)
  1739. {
  1740. F32 run_time = mMessageSystemTimer.getElapsedTimeF32();
  1741. str << "START MESSAGE LOG SUMMARY" << std::endl;
  1742. std::string buffer = llformat("Run time: %12.3f seconds", run_time);
  1743. F32 kbps = 0.008f / run_time;
  1744. F32 packets_in = (F32)llmax(mPacketsIn, 1U);
  1745. F32 packets_out = (F32)llmax(mPacketsOut, 1U);
  1746. // Incoming
  1747. str << buffer << std::endl << "Incoming (sim traffic):" << std::endl;
  1748. std::string tmp_str = U64_to_str(mTotalBytesIn);
  1749. buffer = llformat("Total bytes received: %20s (%5.2f kbits per second)",
  1750. tmp_str.c_str(), (F32)mTotalBytesIn * kbps);
  1751. str << buffer << std::endl;
  1752. tmp_str = U64_to_str(mPacketsIn);
  1753. buffer = llformat("Total packets received: %20s (%5.2f packets per second)",
  1754. tmp_str.c_str(), (F32)mPacketsIn / run_time);
  1755. str << buffer << std::endl;
  1756. buffer = llformat("Average packet size: %20.0f bytes",
  1757. (F32)mTotalBytesIn / packets_in);
  1758. str << buffer << std::endl;
  1759. tmp_str = U64_to_str(mReliablePacketsIn);
  1760. buffer = llformat("Total reliable packets: %20s (%5.2f%%)",
  1761. tmp_str.c_str(),
  1762. 100.f * (F32)mReliablePacketsIn / packets_in);
  1763. str << buffer << std::endl;
  1764. tmp_str = U64_to_str(mCompressedPacketsIn);
  1765. buffer = llformat("Total compressed packets: %20s (%5.2f%%)",
  1766. tmp_str.c_str(),
  1767. 100.f * (F32)mCompressedPacketsIn / packets_in);
  1768. str << buffer << std::endl;
  1769. S64 savings = mUncompressedBytesIn - mCompressedBytesIn;
  1770. tmp_str = U64_to_str(savings);
  1771. buffer = llformat("Total compression savings: %20s bytes",
  1772. tmp_str.c_str());
  1773. str << buffer << std::endl;
  1774. tmp_str = U64_to_str(savings / (mCompressedPacketsIn + 1));
  1775. buffer = llformat("Avg comp packet savings: %20s (%5.2f : 1)",
  1776. tmp_str.c_str(),
  1777. (F32)mUncompressedBytesIn /
  1778. (F32)(mCompressedBytesIn + 1));
  1779. str << buffer << std::endl;
  1780. tmp_str = U64_to_str(savings / (mPacketsIn + 1));
  1781. buffer = llformat("Avg overall comp savings: %20s (%5.2f : 1)",
  1782. tmp_str.c_str(),
  1783. ((F32)mTotalBytesIn +
  1784. (F32)savings) / (F32)(mTotalBytesIn + 1));
  1785. // Outgoing
  1786. str << buffer << std::endl << std::endl << "Outgoing (sim traffic):"
  1787. << std::endl;
  1788. tmp_str = U64_to_str(mTotalBytesOut);
  1789. buffer = llformat("Total bytes sent: %20s (%5.2f kbits per second)",
  1790. tmp_str.c_str(), (F32)mTotalBytesOut * kbps);
  1791. str << buffer << std::endl;
  1792. tmp_str = U64_to_str(mPacketsOut);
  1793. buffer = llformat("Total packets sent: %20s (%5.2f packets per second)",
  1794. tmp_str.c_str(), (F32)mPacketsOut / run_time);
  1795. str << buffer << std::endl;
  1796. buffer = llformat("Average packet size: %20.0f bytes",
  1797. (F32)mTotalBytesOut / packets_out);
  1798. str << buffer << std::endl;
  1799. tmp_str = U64_to_str(mReliablePacketsOut);
  1800. buffer = llformat("Total reliable packets: %20s (%5.2f%%)",
  1801. tmp_str.c_str(),
  1802. 100.f * (F32)mReliablePacketsOut / packets_out);
  1803. str << buffer << std::endl;
  1804. tmp_str = U64_to_str(mCompressedPacketsOut);
  1805. buffer = llformat("Total compressed packets: %20s (%5.2f%%)",
  1806. tmp_str.c_str(),
  1807. 100.f * (F32)mCompressedPacketsOut / packets_out);
  1808. str << buffer << std::endl;
  1809. savings = mUncompressedBytesOut - mCompressedBytesOut;
  1810. tmp_str = U64_to_str(savings);
  1811. buffer = llformat("Total compression savings: %20s bytes",
  1812. tmp_str.c_str());
  1813. str << buffer << std::endl;
  1814. tmp_str = U64_to_str(savings / (mCompressedPacketsOut + 1));
  1815. buffer = llformat("Avg comp packet savings: %20s (%5.2f : 1)",
  1816. tmp_str.c_str(),
  1817. (F32)mUncompressedBytesOut /
  1818. (F32)(mCompressedBytesOut + 1));
  1819. str << buffer << std::endl;
  1820. tmp_str = U64_to_str(savings/(mPacketsOut+1));
  1821. buffer = llformat("Avg overall comp savings: %20s (%5.2f : 1)",
  1822. tmp_str.c_str(),
  1823. ((F32)mTotalBytesOut +
  1824. (F32)savings) / (F32)(mTotalBytesOut + 1));
  1825. str << buffer << std::endl << std::endl;
  1826. buffer = llformat("SendPacket failures: %20d",
  1827. mSendPacketFailureCount);
  1828. str << buffer << std::endl;
  1829. buffer = llformat("Dropped packets: %20d", mDroppedPackets);
  1830. str << buffer << std::endl;
  1831. buffer = llformat("Resent packets: %20d", mResentPackets);
  1832. str << buffer << std::endl;
  1833. buffer = llformat("Failed reliable resends: %20d", mFailedResendPackets);
  1834. str << buffer << std::endl;
  1835. buffer = llformat("Off-circuit rejected packets: %17d",
  1836. mOffCircuitPackets);
  1837. str << buffer << std::endl;
  1838. buffer = llformat("On-circuit invalid packets: %17d",
  1839. mInvalidOnCircuitPackets);
  1840. str << buffer << std::endl << std::endl;
  1841. if (LLMessageReader::getTimeDecodes())
  1842. {
  1843. str << "Decoding: " << std::endl;
  1844. buffer = llformat("%35s%10s%10s%10s%10s",
  1845. "Message", "Count", "Time", "Max", "Avg");
  1846. str << buffer << std:: endl;
  1847. for (template_name_map_t::const_iterator
  1848. iter = mMessageTemplates.begin(), end = mMessageTemplates.end();
  1849. iter != end; ++iter)
  1850. {
  1851. const LLMessageTemplate* mt = iter->second;
  1852. if (mt->mTotalDecoded > 0.f)
  1853. {
  1854. F32 avg = mt->mTotalDecodeTime / (F32)mt->mTotalDecoded;
  1855. buffer = llformat("%35s%10u%10f%10f%10f", mt->mName,
  1856. mt->mTotalDecoded, mt->mTotalDecodeTime,
  1857. mt->mMaxDecodeTimePerMsg, avg);
  1858. str << buffer << std::endl;
  1859. }
  1860. }
  1861. str << std::endl;
  1862. }
  1863. str << "Incoming (curl HTTP traffic):" << std::endl;
  1864. U64 bytes = LLCore::HttpLibcurl::getDownloadedBytes();
  1865. tmp_str = U64_to_str(bytes);
  1866. buffer = llformat("Total bytes received: %20s (%5.2f kbits per second)",
  1867. tmp_str.c_str(), (F32)bytes * kbps);
  1868. str << buffer << std::endl << std::endl << "Outgoing (curl HTTP traffic):"
  1869. << std::endl;
  1870. bytes = LLCore::HttpLibcurl::getUploadedBytes();
  1871. tmp_str = U64_to_str(bytes);
  1872. buffer = llformat("Total bytes sent: %20s (%5.2f kbits per second)",
  1873. tmp_str.c_str(), (F32)bytes * kbps);
  1874. str << buffer << std::endl;
  1875. str << "END MESSAGE LOG SUMMARY" << std::endl;
  1876. }
  1877. void end_messaging_system(bool print_summary)
  1878. {
  1879. gTransferManager.cleanup();
  1880. LLTransferTargetVFile::updateQueue(true); // Shutdown LLTransferTargetVFile
  1881. if (gMessageSystemp)
  1882. {
  1883. gMessageSystemp->stopLogging();
  1884. if (print_summary)
  1885. {
  1886. std::ostringstream str;
  1887. gMessageSystemp->summarizeLogs(str);
  1888. llinfos << str.str().c_str() << llendl;
  1889. }
  1890. delete gMessageSystemp;
  1891. gMessageSystemp = NULL;
  1892. }
  1893. }
  1894. void LLMessageSystem::resetReceiveCounts()
  1895. {
  1896. mNumMessageCounts = 0;
  1897. for (template_name_map_t::iterator iter = mMessageTemplates.begin(),
  1898. end = mMessageTemplates.end();
  1899. iter != end; ++iter)
  1900. {
  1901. LLMessageTemplate* mt = iter->second;
  1902. mt->mDecodeTimeThisFrame = 0.f;
  1903. }
  1904. }
  1905. void LLMessageSystem::dumpReceiveCounts()
  1906. {
  1907. LLMessageTemplate* mt;
  1908. for (template_name_map_t::iterator iter = mMessageTemplates.begin(),
  1909. end = mMessageTemplates.end();
  1910. iter != end; ++iter)
  1911. {
  1912. LLMessageTemplate* mt = iter->second;
  1913. mt->mReceiveCount = 0;
  1914. mt->mReceiveBytes = 0;
  1915. mt->mReceiveInvalid = 0;
  1916. }
  1917. S32 i;
  1918. for (i = 0; i < mNumMessageCounts; ++i)
  1919. {
  1920. mt = get_ptr_in_map(mMessageNumbers,mMessageCountList[i].mMessageNum);
  1921. if (mt)
  1922. {
  1923. ++mt->mReceiveCount;
  1924. mt->mReceiveBytes += mMessageCountList[i].mMessageBytes;
  1925. if (mMessageCountList[i].mInvalid)
  1926. {
  1927. ++mt->mReceiveInvalid;
  1928. }
  1929. }
  1930. }
  1931. if (mNumMessageCounts > 0)
  1932. {
  1933. LL_DEBUGS("Messaging") << "Dump: " << mNumMessageCounts
  1934. << " messages processed in " << mReceiveTime
  1935. << " seconds" << LL_ENDL;
  1936. for (template_name_map_t::const_iterator
  1937. iter = mMessageTemplates.begin(),
  1938. end = mMessageTemplates.end();
  1939. iter != end; ++iter)
  1940. {
  1941. const LLMessageTemplate* mt = iter->second;
  1942. if (mt->mReceiveCount > 0)
  1943. {
  1944. llinfos << "Num: " << std::setw(3) << mt->mReceiveCount
  1945. << " Bytes: " << std::setw(6) << mt->mReceiveBytes
  1946. << " Invalid: " << std::setw(3) << mt->mReceiveInvalid
  1947. << " " << mt->mName << " "
  1948. << ll_round(100 * mt->mDecodeTimeThisFrame / mReceiveTime)
  1949. << "%" << llendl;
  1950. }
  1951. }
  1952. }
  1953. }
  1954. S32 LLMessageSystem::flush(const LLHost& host)
  1955. {
  1956. if (mMessageBuilder->getMessageSize())
  1957. {
  1958. S32 sentbytes = sendMessage(host);
  1959. clearMessage();
  1960. return sentbytes;
  1961. }
  1962. return 0;
  1963. }
  1964. S32 LLMessageSystem::zeroCodeExpand(U8** data, S32* data_size)
  1965. {
  1966. if (*data_size < LL_MINIMUM_VALID_PACKET_SIZE)
  1967. {
  1968. llwarns << "Call done with invalid data size: " << *data_size
  1969. << llendl;
  1970. }
  1971. mTotalBytesIn += *data_size;
  1972. // If we are not zero-coded, simply return.
  1973. if (!(*data[0] & LL_ZERO_CODE_FLAG))
  1974. {
  1975. return 0;
  1976. }
  1977. S32 in_size = *data_size;
  1978. ++mCompressedPacketsIn;
  1979. mCompressedBytesIn += *data_size;
  1980. *data[0] &= ~LL_ZERO_CODE_FLAG;
  1981. S32 count = *data_size;
  1982. U8* inptr = (U8*)*data;
  1983. U8* outptr = (U8*)mEncodedRecvBuffer;
  1984. // Skip the packet id field
  1985. for (U32 ii = 0; ii < LL_PACKET_ID_SIZE; ++ii)
  1986. {
  1987. --count;
  1988. *outptr++ = *inptr++;
  1989. }
  1990. // Reconstruct encoded packet, keeping track of net size gain. Sequential
  1991. // zero bytes are encoded as 0 [U8 count] with 0 0 [count] representing
  1992. // wrap (>256 zeros)
  1993. while (count--)
  1994. {
  1995. if (outptr > (&mEncodedRecvBuffer[MAX_BUFFER_SIZE - 1]))
  1996. {
  1997. llwarns << "attempt to write past reasonable encoded buffer size 1"
  1998. << llendl;
  1999. callExceptionFunc(MX_WROTE_PAST_BUFFER_SIZE);
  2000. outptr = mEncodedRecvBuffer;
  2001. break;
  2002. }
  2003. if (!((*outptr++ = *inptr++)))
  2004. {
  2005. while (((count--)) && (!(*inptr)))
  2006. {
  2007. *outptr++ = *inptr++;
  2008. if (outptr > (&mEncodedRecvBuffer[MAX_BUFFER_SIZE - 256]))
  2009. {
  2010. llwarns << "attempt to write past reasonable encoded buffer size 2"
  2011. << llendl;
  2012. callExceptionFunc(MX_WROTE_PAST_BUFFER_SIZE);
  2013. outptr = mEncodedRecvBuffer;
  2014. count = -1;
  2015. break;
  2016. }
  2017. memset(outptr, 0, 255);
  2018. outptr += 255;
  2019. }
  2020. if (count < 0)
  2021. {
  2022. break;
  2023. }
  2024. else
  2025. {
  2026. if (outptr > (&mEncodedRecvBuffer[MAX_BUFFER_SIZE - (*inptr)]))
  2027. {
  2028. llwarns << "attempt to write past reasonable encoded buffer size 3"
  2029. << llendl;
  2030. callExceptionFunc(MX_WROTE_PAST_BUFFER_SIZE);
  2031. outptr = mEncodedRecvBuffer;
  2032. }
  2033. memset(outptr, 0, *inptr - 1);
  2034. outptr += (*inptr++) - 1;
  2035. }
  2036. }
  2037. }
  2038. *data = mEncodedRecvBuffer;
  2039. *data_size = (S32)(outptr - mEncodedRecvBuffer);
  2040. mUncompressedBytesIn += *data_size;
  2041. return in_size;
  2042. }
  2043. void LLMessageSystem::addTemplate(LLMessageTemplate* templatep)
  2044. {
  2045. if (mMessageTemplates.count(templatep->mName) > 0)
  2046. {
  2047. llerrs << templatep->mName << " already used as a template name !"
  2048. << llendl;
  2049. }
  2050. mMessageTemplates[templatep->mName] = templatep;
  2051. mMessageNumbers[templatep->mMessageNumber] = templatep;
  2052. }
  2053. void LLMessageSystem::setHandlerFuncFast(const char* name,
  2054. void (*handler_func)(LLMessageSystem*,
  2055. void**),
  2056. void** user_data)
  2057. {
  2058. LLMessageTemplate* msgtemplate = get_ptr_in_map(mMessageTemplates, name);
  2059. if (!msgtemplate)
  2060. {
  2061. llerrs << name << " is not a known message name !" << llendl;
  2062. }
  2063. msgtemplate->setHandlerFunc(handler_func, user_data);
  2064. }
  2065. bool LLMessageSystem::callHandler(const char* name, bool trusted_source)
  2066. {
  2067. name = gMessageStringTable.getString(name);
  2068. template_name_map_t::const_iterator iter = mMessageTemplates.find(name);
  2069. if (iter == mMessageTemplates.end())
  2070. {
  2071. llwarns << "Unknown message " << name << llendl;
  2072. return false;
  2073. }
  2074. const LLMessageTemplate* msg_template = iter->second;
  2075. if (msg_template->isBanned(trusted_source))
  2076. {
  2077. llwarns << "Banned message " << name << " from "
  2078. << (trusted_source ? "trusted " : "untrusted ") << "source"
  2079. << llendl;
  2080. return false;
  2081. }
  2082. return msg_template->callHandlerFunc(this);
  2083. }
  2084. void LLMessageSystem::setExceptionFunc(EMessageException e,
  2085. msg_exception_callback func,
  2086. void* data)
  2087. {
  2088. callbacks_t::iterator it = mExceptionCallbacks.find(e);
  2089. if (it != mExceptionCallbacks.end())
  2090. {
  2091. mExceptionCallbacks.erase(it);
  2092. }
  2093. if (func)
  2094. {
  2095. mExceptionCallbacks.emplace(e, exception_t(func, data));
  2096. }
  2097. }
  2098. bool LLMessageSystem::callExceptionFunc(EMessageException exception)
  2099. {
  2100. callbacks_t::iterator it = mExceptionCallbacks.find(exception);
  2101. if (it == mExceptionCallbacks.end())
  2102. {
  2103. return false;
  2104. }
  2105. exception_t& ex = it->second;
  2106. msg_exception_callback ex_cb = ex.first;
  2107. if (!ex_cb)
  2108. {
  2109. llwarns << "NULL message exception callback !" << llendl;
  2110. return false;
  2111. }
  2112. (ex_cb)(this, ex.second, exception);
  2113. return true;
  2114. }
  2115. void LLMessageSystem::setTimingFunc(msg_timing_callback func, void* data)
  2116. {
  2117. mTimingCallback = func;
  2118. mTimingCallbackData = data;
  2119. }
  2120. bool LLMessageSystem::isMessageFast(const char* msg)
  2121. {
  2122. return msg == mMessageReader->getMessageName();
  2123. }
  2124. char* LLMessageSystem::getMessageName()
  2125. {
  2126. return const_cast<char*>(mMessageReader->getMessageName());
  2127. }
  2128. const LLUUID& LLMessageSystem::getSenderID() const
  2129. {
  2130. LLCircuitData* cdp = mCircuitInfo.findCircuit(mLastSender);
  2131. return cdp ? cdp->mRemoteID : LLUUID::null;
  2132. }
  2133. const LLUUID& LLMessageSystem::getSenderSessionID() const
  2134. {
  2135. LLCircuitData* cdp = mCircuitInfo.findCircuit(mLastSender);
  2136. return cdp ? cdp->mRemoteSessionID : LLUUID::null;
  2137. }
  2138. void LLMessageSystem::sendDenyTrustedCircuit(const LLHost& host)
  2139. {
  2140. mDenyTrustedCircuitSet.emplace(host);
  2141. }
  2142. void LLMessageSystem::reallySendDenyTrustedCircuit(const LLHost& host)
  2143. {
  2144. LLCircuitData* cdp = mCircuitInfo.findCircuit(host);
  2145. if (!cdp)
  2146. {
  2147. llwarns << "Not sending DenyTrustedCircuit to host without a circuit."
  2148. << llendl;
  2149. return;
  2150. }
  2151. llinfos << "Sending DenyTrustedCircuit to " << host << llendl;
  2152. newMessageFast(_PREHASH_DenyTrustedCircuit);
  2153. nextBlockFast(_PREHASH_DataBlock);
  2154. addUUIDFast(_PREHASH_EndPointID, cdp->getLocalEndPointID());
  2155. sendMessage(host);
  2156. }
  2157. void null_message_callback(LLMessageSystem*, void**)
  2158. {
  2159. // Nothing should ever go here, but we use this to register messages that
  2160. // we are expecting to see (and spinning on) at startup.
  2161. }
  2162. void LLMessageSystem::dumpPacketToLog()
  2163. {
  2164. llwarns << "Packet Dump from:" << mPacketRing.getLastSender() << llendl;
  2165. llwarns << "Packet Size:" << mTrueReceiveSize << llendl;
  2166. char line_buffer[256];
  2167. S32 i;
  2168. S32 cur_line_pos = 0;
  2169. S32 cur_line = 0;
  2170. for (i = 0; i < mTrueReceiveSize; ++i)
  2171. {
  2172. S32 offset = cur_line_pos * 3;
  2173. snprintf(line_buffer + offset, sizeof(line_buffer) - offset,
  2174. "%02x ", mTrueReceiveBuffer[i]);
  2175. if (++cur_line_pos >= 16)
  2176. {
  2177. cur_line_pos = 0;
  2178. llwarns << "PD:" << cur_line << "PD:" << line_buffer << llendl;
  2179. ++cur_line;
  2180. }
  2181. }
  2182. if (cur_line_pos)
  2183. {
  2184. llwarns << "PD:" << cur_line << "PD:" << line_buffer << llendl;
  2185. }
  2186. }
  2187. //static
  2188. U64 LLMessageSystem::getMessageTimeUsecs(bool update)
  2189. {
  2190. LLMessageSystem* msg = gMessageSystemp;
  2191. if (msg)
  2192. {
  2193. if (update)
  2194. {
  2195. msg->mCurrentMessageTimeSeconds = LLTimer::totalTime() *
  2196. SEC_PER_USEC;
  2197. }
  2198. return (U64)(msg->mCurrentMessageTimeSeconds * USEC_PER_SEC);
  2199. }
  2200. return LLTimer::totalTime();
  2201. }
  2202. //static
  2203. F64 LLMessageSystem::getMessageTimeSeconds(bool update)
  2204. {
  2205. LLMessageSystem* msg = gMessageSystemp;
  2206. if (msg)
  2207. {
  2208. if (update)
  2209. {
  2210. msg->mCurrentMessageTimeSeconds = LLTimer::totalTime() *
  2211. SEC_PER_USEC;
  2212. }
  2213. return msg->mCurrentMessageTimeSeconds;
  2214. }
  2215. return LLTimer::totalTime() * SEC_PER_USEC;
  2216. }
  2217. typedef std::map<const char*, LLMessageBuilder*> BuilderMap;
  2218. void LLMessageSystem::newMessageFast(const char* name)
  2219. {
  2220. LLMessageConfig::Flavor message_flavor =
  2221. LLMessageConfig::getMessageFlavor(name);
  2222. LLMessageConfig::Flavor server_flavor =
  2223. LLMessageConfig::getServerDefaultFlavor();
  2224. if (message_flavor == LLMessageConfig::TEMPLATE_FLAVOR)
  2225. {
  2226. mMessageBuilder = mTemplateMessageBuilder;
  2227. }
  2228. else if (message_flavor == LLMessageConfig::LLSD_FLAVOR)
  2229. {
  2230. mMessageBuilder = mLLSDMessageBuilder;
  2231. }
  2232. // NO_FLAVOR
  2233. else if (server_flavor == LLMessageConfig::LLSD_FLAVOR)
  2234. {
  2235. mMessageBuilder = mLLSDMessageBuilder;
  2236. }
  2237. // TEMPLATE_FLAVOR or NO_FLAVOR
  2238. else
  2239. {
  2240. mMessageBuilder = mTemplateMessageBuilder;
  2241. }
  2242. mSendReliable = false;
  2243. mMessageBuilder->newMessage(name);
  2244. }
  2245. void LLMessageSystem::newMessage(const char* name)
  2246. {
  2247. newMessageFast(gMessageStringTable.getString(name));
  2248. }
  2249. void LLMessageSystem::addBinaryDataFast(const char* varname, const void* data,
  2250. S32 size)
  2251. {
  2252. mMessageBuilder->addBinaryData(varname, data, size);
  2253. }
  2254. void LLMessageSystem::addBinaryData(const char* varname, const void* data,
  2255. S32 size)
  2256. {
  2257. mMessageBuilder->addBinaryData(gMessageStringTable.getString(varname),
  2258. data, size);
  2259. }
  2260. void LLMessageSystem::addS8Fast(const char* varname, S8 v)
  2261. {
  2262. mMessageBuilder->addS8(varname, v);
  2263. }
  2264. void LLMessageSystem::addS8(const char* varname, S8 v)
  2265. {
  2266. mMessageBuilder->addS8(gMessageStringTable.getString(varname), v);
  2267. }
  2268. void LLMessageSystem::addU8Fast(const char* varname, U8 v)
  2269. {
  2270. mMessageBuilder->addU8(varname, v);
  2271. }
  2272. void LLMessageSystem::addU8(const char* varname, U8 v)
  2273. {
  2274. mMessageBuilder->addU8(gMessageStringTable.getString(varname), v);
  2275. }
  2276. void LLMessageSystem::addS16Fast(const char* varname, S16 v)
  2277. {
  2278. mMessageBuilder->addS16(varname, v);
  2279. }
  2280. void LLMessageSystem::addS16(const char* varname, S16 v)
  2281. {
  2282. mMessageBuilder->addS16(gMessageStringTable.getString(varname), v);
  2283. }
  2284. void LLMessageSystem::addU16Fast(const char* varname, U16 v)
  2285. {
  2286. mMessageBuilder->addU16(varname, v);
  2287. }
  2288. void LLMessageSystem::addU16(const char* varname, U16 v)
  2289. {
  2290. mMessageBuilder->addU16(gMessageStringTable.getString(varname), v);
  2291. }
  2292. void LLMessageSystem::addF32Fast(const char* varname, F32 v)
  2293. {
  2294. mMessageBuilder->addF32(varname, v);
  2295. }
  2296. void LLMessageSystem::addF32(const char* varname, F32 v)
  2297. {
  2298. mMessageBuilder->addF32(gMessageStringTable.getString(varname), v);
  2299. }
  2300. void LLMessageSystem::addS32Fast(const char* varname, S32 v)
  2301. {
  2302. mMessageBuilder->addS32(varname, v);
  2303. }
  2304. void LLMessageSystem::addS32(const char* varname, S32 v)
  2305. {
  2306. mMessageBuilder->addS32(gMessageStringTable.getString(varname), v);
  2307. }
  2308. void LLMessageSystem::addU32Fast(const char* varname, U32 v)
  2309. {
  2310. mMessageBuilder->addU32(varname, v);
  2311. }
  2312. void LLMessageSystem::addU32(const char* varname, U32 v)
  2313. {
  2314. mMessageBuilder->addU32(gMessageStringTable.getString(varname), v);
  2315. }
  2316. void LLMessageSystem::addU64Fast(const char* varname, U64 v)
  2317. {
  2318. mMessageBuilder->addU64(varname, v);
  2319. }
  2320. void LLMessageSystem::addU64(const char* varname, U64 v)
  2321. {
  2322. mMessageBuilder->addU64(gMessageStringTable.getString(varname), v);
  2323. }
  2324. void LLMessageSystem::addF64Fast(const char* varname, F64 v)
  2325. {
  2326. mMessageBuilder->addF64(varname, v);
  2327. }
  2328. void LLMessageSystem::addF64(const char* varname, F64 v)
  2329. {
  2330. mMessageBuilder->addF64(gMessageStringTable.getString(varname), v);
  2331. }
  2332. void LLMessageSystem::addIPAddrFast(const char* varname, U32 v)
  2333. {
  2334. mMessageBuilder->addIPAddr(varname, v);
  2335. }
  2336. void LLMessageSystem::addIPAddr(const char* varname, U32 v)
  2337. {
  2338. mMessageBuilder->addIPAddr(gMessageStringTable.getString(varname), v);
  2339. }
  2340. void LLMessageSystem::addIPPortFast(const char* varname, U16 v)
  2341. {
  2342. mMessageBuilder->addIPPort(varname, v);
  2343. }
  2344. void LLMessageSystem::addIPPort(const char* varname, U16 v)
  2345. {
  2346. mMessageBuilder->addIPPort(gMessageStringTable.getString(varname), v);
  2347. }
  2348. void LLMessageSystem::addBoolFast(const char* varname, bool v)
  2349. {
  2350. mMessageBuilder->addBool(varname, v);
  2351. }
  2352. void LLMessageSystem::addBool(const char* varname, bool v)
  2353. {
  2354. mMessageBuilder->addBool(gMessageStringTable.getString(varname), v);
  2355. }
  2356. void LLMessageSystem::addStringFast(const char* varname, const char* v)
  2357. {
  2358. mMessageBuilder->addString(varname, v);
  2359. }
  2360. void LLMessageSystem::addString(const char* varname, const char* v)
  2361. {
  2362. mMessageBuilder->addString(gMessageStringTable.getString(varname), v);
  2363. }
  2364. void LLMessageSystem::addStringFast(const char* varname, const std::string& v)
  2365. {
  2366. mMessageBuilder->addString(varname, v);
  2367. }
  2368. void LLMessageSystem::addString(const char* varname, const std::string& v)
  2369. {
  2370. mMessageBuilder->addString(gMessageStringTable.getString(varname), v);
  2371. }
  2372. void LLMessageSystem::addVector3Fast(const char* varname, const LLVector3& v)
  2373. {
  2374. mMessageBuilder->addVector3(varname, v);
  2375. }
  2376. void LLMessageSystem::addVector3(const char* varname, const LLVector3& v)
  2377. {
  2378. mMessageBuilder->addVector3(gMessageStringTable.getString(varname), v);
  2379. }
  2380. void LLMessageSystem::addVector4Fast(const char* varname, const LLVector4& v)
  2381. {
  2382. mMessageBuilder->addVector4(varname, v);
  2383. }
  2384. void LLMessageSystem::addVector4(const char* varname, const LLVector4& v)
  2385. {
  2386. mMessageBuilder->addVector4(gMessageStringTable.getString(varname), v);
  2387. }
  2388. void LLMessageSystem::addVector3dFast(const char* varname, const LLVector3d& v)
  2389. {
  2390. mMessageBuilder->addVector3d(varname, v);
  2391. }
  2392. void LLMessageSystem::addVector3d(const char* varname, const LLVector3d& v)
  2393. {
  2394. mMessageBuilder->addVector3d(gMessageStringTable.getString(varname), v);
  2395. }
  2396. void LLMessageSystem::addQuatFast(const char* varname, const LLQuaternion& v)
  2397. {
  2398. mMessageBuilder->addQuat(varname, v);
  2399. }
  2400. void LLMessageSystem::addQuat(const char* varname, const LLQuaternion& v)
  2401. {
  2402. mMessageBuilder->addQuat(gMessageStringTable.getString(varname), v);
  2403. }
  2404. void LLMessageSystem::addUUIDFast(const char* varname, const LLUUID& v)
  2405. {
  2406. mMessageBuilder->addUUID(varname, v);
  2407. }
  2408. void LLMessageSystem::addUUID(const char* varname, const LLUUID& v)
  2409. {
  2410. mMessageBuilder->addUUID(gMessageStringTable.getString(varname), v);
  2411. }
  2412. S32 LLMessageSystem::getCurrentSendTotal() const
  2413. {
  2414. return mMessageBuilder->getMessageSize();
  2415. }
  2416. void LLMessageSystem::getS8Fast(const char* block, const char* var, S8& u,
  2417. S32 blocknum)
  2418. {
  2419. mMessageReader->getS8(block, var, u, blocknum);
  2420. }
  2421. void LLMessageSystem::getS8(const char* block, const char* var, S8& u,
  2422. S32 blocknum)
  2423. {
  2424. getS8Fast(gMessageStringTable.getString(block),
  2425. gMessageStringTable.getString(var), u, blocknum);
  2426. }
  2427. void LLMessageSystem::getU8Fast(const char* block, const char* var, U8& u,
  2428. S32 blocknum)
  2429. {
  2430. mMessageReader->getU8(block, var, u, blocknum);
  2431. }
  2432. void LLMessageSystem::getU8(const char* block, const char* var, U8& u,
  2433. S32 blocknum)
  2434. {
  2435. getU8Fast(gMessageStringTable.getString(block),
  2436. gMessageStringTable.getString(var), u, blocknum);
  2437. }
  2438. void LLMessageSystem::getBoolFast(const char* block, const char* var, bool& b,
  2439. S32 blocknum)
  2440. {
  2441. mMessageReader->getBool(block, var, b, blocknum);
  2442. }
  2443. void LLMessageSystem::getBool(const char* block, const char* var, bool& b,
  2444. S32 blocknum)
  2445. {
  2446. getBoolFast(gMessageStringTable.getString(block),
  2447. gMessageStringTable.getString(var), b, blocknum);
  2448. }
  2449. void LLMessageSystem::getS16Fast(const char* block, const char* var, S16& d,
  2450. S32 blocknum)
  2451. {
  2452. mMessageReader->getS16(block, var, d, blocknum);
  2453. }
  2454. void LLMessageSystem::getS16(const char* block, const char* var, S16& d,
  2455. S32 blocknum)
  2456. {
  2457. getS16Fast(gMessageStringTable.getString(block),
  2458. gMessageStringTable.getString(var), d, blocknum);
  2459. }
  2460. void LLMessageSystem::getU16Fast(const char* block, const char* var, U16& d,
  2461. S32 blocknum)
  2462. {
  2463. mMessageReader->getU16(block, var, d, blocknum);
  2464. }
  2465. void LLMessageSystem::getU16(const char* block, const char* var, U16& d,
  2466. S32 blocknum)
  2467. {
  2468. getU16Fast(gMessageStringTable.getString(block),
  2469. gMessageStringTable.getString(var), d, blocknum);
  2470. }
  2471. void LLMessageSystem::getS32Fast(const char* block, const char* var, S32& d,
  2472. S32 blocknum)
  2473. {
  2474. mMessageReader->getS32(block, var, d, blocknum);
  2475. }
  2476. void LLMessageSystem::getS32(const char* block, const char* var, S32& d,
  2477. S32 blocknum)
  2478. {
  2479. getS32Fast(gMessageStringTable.getString(block),
  2480. gMessageStringTable.getString(var), d, blocknum);
  2481. }
  2482. void LLMessageSystem::getU32Fast(const char* block, const char* var, U32& d,
  2483. S32 blocknum)
  2484. {
  2485. mMessageReader->getU32(block, var, d, blocknum);
  2486. }
  2487. void LLMessageSystem::getU32(const char* block, const char* var, U32& d,
  2488. S32 blocknum)
  2489. {
  2490. getU32Fast(gMessageStringTable.getString(block),
  2491. gMessageStringTable.getString(var), d, blocknum);
  2492. }
  2493. void LLMessageSystem::getU64Fast(const char* block, const char* var, U64& d,
  2494. S32 blocknum)
  2495. {
  2496. mMessageReader->getU64(block, var, d, blocknum);
  2497. }
  2498. void LLMessageSystem::getU64(const char* block, const char* var, U64& d,
  2499. S32 blocknum)
  2500. {
  2501. getU64Fast(gMessageStringTable.getString(block),
  2502. gMessageStringTable.getString(var), d, blocknum);
  2503. }
  2504. void LLMessageSystem::getBinaryDataFast(const char* blockname,
  2505. const char* varname,
  2506. void* datap, S32 size,
  2507. S32 blocknum, S32 max_size)
  2508. {
  2509. mMessageReader->getBinaryData(blockname, varname, datap, size, blocknum,
  2510. max_size);
  2511. }
  2512. void LLMessageSystem::getBinaryData(const char* blockname,
  2513. const char* varname,
  2514. void* datap, S32 size,
  2515. S32 blocknum, S32 max_size)
  2516. {
  2517. getBinaryDataFast(gMessageStringTable.getString(blockname),
  2518. gMessageStringTable.getString(varname),
  2519. datap, size, blocknum, max_size);
  2520. }
  2521. void LLMessageSystem::getF32Fast(const char* block, const char* var, F32& d,
  2522. S32 blocknum)
  2523. {
  2524. mMessageReader->getF32(block, var, d, blocknum);
  2525. }
  2526. void LLMessageSystem::getF32(const char* block, const char* var, F32& d,
  2527. S32 blocknum)
  2528. {
  2529. getF32Fast(gMessageStringTable.getString(block),
  2530. gMessageStringTable.getString(var), d, blocknum);
  2531. }
  2532. void LLMessageSystem::getF64Fast(const char* block, const char* var, F64& d,
  2533. S32 blocknum)
  2534. {
  2535. mMessageReader->getF64(block, var, d, blocknum);
  2536. }
  2537. void LLMessageSystem::getF64(const char* block, const char* var, F64& d,
  2538. S32 blocknum)
  2539. {
  2540. getF64Fast(gMessageStringTable.getString(block),
  2541. gMessageStringTable.getString(var), d, blocknum);
  2542. }
  2543. void LLMessageSystem::getVector3Fast(const char* block, const char* var,
  2544. LLVector3& v, S32 blocknum)
  2545. {
  2546. mMessageReader->getVector3(block, var, v, blocknum);
  2547. }
  2548. void LLMessageSystem::getVector3(const char* block, const char* var,
  2549. LLVector3& v, S32 blocknum)
  2550. {
  2551. getVector3Fast(gMessageStringTable.getString(block),
  2552. gMessageStringTable.getString(var), v, blocknum);
  2553. }
  2554. void LLMessageSystem::getVector4Fast(const char* block, const char* var,
  2555. LLVector4& v, S32 blocknum)
  2556. {
  2557. mMessageReader->getVector4(block, var, v, blocknum);
  2558. }
  2559. void LLMessageSystem::getVector4(const char* block, const char* var,
  2560. LLVector4& v, S32 blocknum)
  2561. {
  2562. getVector4Fast(gMessageStringTable.getString(block),
  2563. gMessageStringTable.getString(var), v, blocknum);
  2564. }
  2565. void LLMessageSystem::getVector3dFast(const char* block, const char* var,
  2566. LLVector3d& v, S32 blocknum)
  2567. {
  2568. mMessageReader->getVector3d(block, var, v, blocknum);
  2569. }
  2570. void LLMessageSystem::getVector3d(const char* block, const char* var,
  2571. LLVector3d& v, S32 blocknum)
  2572. {
  2573. getVector3dFast(gMessageStringTable.getString(block),
  2574. gMessageStringTable.getString(var), v, blocknum);
  2575. }
  2576. void LLMessageSystem::getQuatFast(const char* block, const char* var,
  2577. LLQuaternion& q, S32 blocknum)
  2578. {
  2579. mMessageReader->getQuat(block, var, q, blocknum);
  2580. }
  2581. void LLMessageSystem::getQuat(const char* block, const char* var,
  2582. LLQuaternion& q, S32 blocknum)
  2583. {
  2584. getQuatFast(gMessageStringTable.getString(block),
  2585. gMessageStringTable.getString(var), q, blocknum);
  2586. }
  2587. void LLMessageSystem::getUUIDFast(const char* block, const char* var,
  2588. LLUUID& u, S32 blocknum)
  2589. {
  2590. mMessageReader->getUUID(block, var, u, blocknum);
  2591. }
  2592. void LLMessageSystem::getUUID(const char* block, const char* var, LLUUID& u,
  2593. S32 blocknum)
  2594. {
  2595. getUUIDFast(gMessageStringTable.getString(block),
  2596. gMessageStringTable.getString(var), u, blocknum);
  2597. }
  2598. void LLMessageSystem::getIPAddrFast(const char* block, const char* var,
  2599. U32& u, S32 blocknum)
  2600. {
  2601. mMessageReader->getIPAddr(block, var, u, blocknum);
  2602. }
  2603. void LLMessageSystem::getIPAddr(const char* block, const char* var, U32& u,
  2604. S32 blocknum)
  2605. {
  2606. getIPAddrFast(gMessageStringTable.getString(block),
  2607. gMessageStringTable.getString(var), u, blocknum);
  2608. }
  2609. void LLMessageSystem::getIPPortFast(const char* block, const char* var,
  2610. U16& u, S32 blocknum)
  2611. {
  2612. mMessageReader->getIPPort(block, var, u, blocknum);
  2613. }
  2614. void LLMessageSystem::getIPPort(const char* block, const char* var, U16& u,
  2615. S32 blocknum)
  2616. {
  2617. getIPPortFast(gMessageStringTable.getString(block),
  2618. gMessageStringTable.getString(var), u,
  2619. blocknum);
  2620. }
  2621. void LLMessageSystem::getStringFast(const char* block, const char* var,
  2622. S32 buffer_size, char* s, S32 blocknum)
  2623. {
  2624. if (buffer_size <= 0)
  2625. {
  2626. llwarns << "buffer_size <= 0" << llendl;
  2627. }
  2628. mMessageReader->getString(block, var, buffer_size, s, blocknum);
  2629. }
  2630. void LLMessageSystem::getString(const char* block, const char* var,
  2631. S32 buffer_size, char* s, S32 blocknum)
  2632. {
  2633. getStringFast(gMessageStringTable.getString(block),
  2634. gMessageStringTable.getString(var), buffer_size, s,
  2635. blocknum);
  2636. }
  2637. void LLMessageSystem::getStringFast(const char* block, const char* var,
  2638. std::string& outstr, S32 blocknum)
  2639. {
  2640. mMessageReader->getString(block, var, outstr, blocknum);
  2641. }
  2642. void LLMessageSystem::getString(const char* block, const char* var,
  2643. std::string& outstr, S32 blocknum)
  2644. {
  2645. getStringFast(gMessageStringTable.getString(block),
  2646. gMessageStringTable.getString(var), outstr,
  2647. blocknum);
  2648. }
  2649. bool LLMessageSystem::has(const char* blockname) const
  2650. {
  2651. return getNumberOfBlocks(blockname) > 0;
  2652. }
  2653. S32 LLMessageSystem::getNumberOfBlocksFast(const char* blockname) const
  2654. {
  2655. return mMessageReader->getNumberOfBlocks(blockname);
  2656. }
  2657. S32 LLMessageSystem::getNumberOfBlocks(const char* blockname) const
  2658. {
  2659. return getNumberOfBlocksFast(gMessageStringTable.getString(blockname));
  2660. }
  2661. S32 LLMessageSystem::getSizeFast(const char* blockname,
  2662. const char* varname) const
  2663. {
  2664. return mMessageReader->getSize(blockname, varname);
  2665. }
  2666. S32 LLMessageSystem::getSize(const char* blockname, const char* varname) const
  2667. {
  2668. return getSizeFast(gMessageStringTable.getString(blockname),
  2669. gMessageStringTable.getString(varname));
  2670. }
  2671. // size in bytes of variable length data
  2672. S32 LLMessageSystem::getSizeFast(const char* blockname, S32 blocknum,
  2673. const char* varname) const
  2674. {
  2675. return mMessageReader->getSize(blockname, blocknum, varname);
  2676. }
  2677. S32 LLMessageSystem::getSize(const char* blockname, S32 blocknum,
  2678. const char* varname) const
  2679. {
  2680. return getSizeFast(gMessageStringTable.getString(blockname), blocknum,
  2681. gMessageStringTable.getString(varname));
  2682. }
  2683. S32 LLMessageSystem::getReceiveSize() const
  2684. {
  2685. return mMessageReader->getMessageSize();
  2686. }
  2687. //static
  2688. void LLMessageSystem::setTimeDecodes(bool b)
  2689. {
  2690. LLMessageReader::setTimeDecodes(b);
  2691. }
  2692. //static
  2693. void LLMessageSystem::setTimeDecodesSpamThreshold(F32 seconds)
  2694. {
  2695. LLMessageReader::setTimeDecodesSpamThreshold(seconds);
  2696. }
  2697. // *HACK: babbage: return true if message rxed via either UDP or HTTP
  2698. // *TODO: babbage: move gServicePump in to LLMessageSystem?
  2699. #if LL_USE_FIBER_AWARE_MUTEX
  2700. bool LLMessageSystem::checkAllMessages(LockMessageChecker& lmc,
  2701. S64 frame_count, LLPumpIO* pumpp)
  2702. #else
  2703. bool LLMessageSystem::checkAllMessages(S64 frame_count, LLPumpIO* pumpp)
  2704. #endif
  2705. {
  2706. if (!pumpp)
  2707. {
  2708. return false;
  2709. }
  2710. #if LL_USE_FIBER_AWARE_MUTEX
  2711. if (lmc.checkMessages(frame_count))
  2712. #else
  2713. if (checkMessages(frame_count))
  2714. #endif
  2715. {
  2716. return true;
  2717. }
  2718. U32 packetsIn = mPacketsIn;
  2719. pumpp->pump();
  2720. return mPacketsIn - packetsIn > 0;
  2721. }
  2722. void LLMessageSystem::banUdpMessage(const std::string& name)
  2723. {
  2724. template_name_map_t::iterator it =
  2725. mMessageTemplates.find(gMessageStringTable.getString(name.c_str()));
  2726. if (it != mMessageTemplates.end())
  2727. {
  2728. it->second->banUdp();
  2729. }
  2730. else
  2731. {
  2732. llwarns << "Attempted to ban an unknown message: " << name << llendl;
  2733. }
  2734. }
  2735. const LLHost& LLMessageSystem::getSender() const
  2736. {
  2737. return mLastSender;
  2738. }
  2739. void LLMessageSystem::sendUntrustedSimulatorMessageCoro(const LLHost& host,
  2740. const char* msg_name,
  2741. const LLSD& body,
  2742. UntrustedCallback_t cb)
  2743. {
  2744. const std::string& url = host.getUntrustedSimulatorCap();
  2745. if (url.empty())
  2746. {
  2747. llwarns << "Empty capability !" << llendl;
  2748. return;
  2749. }
  2750. LL_DEBUGS("Messaging") << "Sending " << msg_name
  2751. << " to host " << host.getIPandPort()
  2752. << " via capability: " << url << LL_ENDL;
  2753. LLSD postdata;
  2754. postdata["message"] = msg_name;
  2755. postdata["body"] = body;
  2756. LLCoreHttpUtil::HttpCoroutineAdapter adapter("untrustedSimMessage");
  2757. LLSD result = adapter.postAndSuspend(url, postdata, mHttpOptions);
  2758. if (cb && !cb.empty())
  2759. {
  2760. LLSD results =
  2761. result[LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS];
  2762. LLCore::HttpStatus status =
  2763. LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(results);
  2764. cb(status ? LL_ERR_NOERR : LL_ERR_TCP_TIMEOUT);
  2765. }
  2766. }
  2767. ///////////////////////////////////////////////////////////////////////////////
  2768. // LLMessageStringTable class (formerly held in message_string_table.cpp)
  2769. ///////////////////////////////////////////////////////////////////////////////
  2770. LL_INLINE U32 message_hash_my_string(const char* str)
  2771. {
  2772. U32 retval = 0;
  2773. while (*str++)
  2774. {
  2775. retval += *str;
  2776. retval <<= 1;
  2777. }
  2778. return retval % MESSAGE_NUMBER_OF_HASH_BUCKETS;
  2779. }
  2780. LLMessageStringTable::LLMessageStringTable()
  2781. : mUsed(0)
  2782. {
  2783. for (U32 i = 0; i < MESSAGE_NUMBER_OF_HASH_BUCKETS; ++i)
  2784. {
  2785. mEmpty[i] = true;
  2786. mString[i][0] = 0;
  2787. }
  2788. }
  2789. char* LLMessageStringTable::getString(const char* str)
  2790. {
  2791. U32 hash_value = message_hash_my_string(str);
  2792. while (!mEmpty[hash_value])
  2793. {
  2794. if (!strncmp(str, mString[hash_value], MESSAGE_MAX_STRINGS_LENGTH))
  2795. {
  2796. return mString[hash_value];
  2797. }
  2798. else
  2799. {
  2800. ++hash_value;
  2801. hash_value %= MESSAGE_NUMBER_OF_HASH_BUCKETS;
  2802. }
  2803. }
  2804. // Not found, so add it !
  2805. strncpy(mString[hash_value], str, MESSAGE_MAX_STRINGS_LENGTH);
  2806. mString[hash_value][MESSAGE_MAX_STRINGS_LENGTH - 1] = 0;
  2807. mEmpty[hash_value] = false;
  2808. if (++mUsed >= MESSAGE_NUMBER_OF_HASH_BUCKETS - 1)
  2809. {
  2810. llinfos << "Dumping string table before crashing on HashTable full !"
  2811. << llendl;
  2812. for (U32 i = 0; i < MESSAGE_NUMBER_OF_HASH_BUCKETS; ++i)
  2813. {
  2814. llinfos << "Entry #" << i << ": " << mString[i] << llendl;
  2815. }
  2816. }
  2817. return mString[hash_value];
  2818. }
  2819. ///////////////////////////////////////////////////////////////////////////////
  2820. // LLGenericStreamingMessage helper class for Generic Streaming messages
  2821. ///////////////////////////////////////////////////////////////////////////////
  2822. void LLGenericStreamingMessage::unpack(LLMessageSystem* msg)
  2823. {
  2824. U16* m = (U16*)&mMethod; // Squirrely pass enum as U16 by reference
  2825. msg->getU16Fast(_PREHASH_MethodData, _PREHASH_Method, *m);
  2826. constexpr S32 MAX_SIZE = 7 * 1024;
  2827. static char buffer[MAX_SIZE];
  2828. // Note: do not use getStringFast() to avoid 1200 bytes truncation.
  2829. S32 size = msg->getSizeFast(_PREHASH_DataBlock, _PREHASH_Data);
  2830. msg->getBinaryDataFast(_PREHASH_DataBlock, _PREHASH_Data, buffer, size, 0,
  2831. MAX_SIZE);
  2832. mData.assign(buffer, size);
  2833. }