1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
|
#include "lltemplatemessagereader.h"
#include "llfasttimer.h"
#include "llmessagebuilder.h"
#include "llmessagetemplate.h"
#include "llquaternion.h"
#include "message.h"
#include "u64.h"
#include "v3dmath.h"
#include "v3math.h"
#include "v4math.h"
LLTemplateMessageReader::LLTemplateMessageReader(message_template_number_map_t&
number_template_map) :
mReceiveSize(0),
mCurrentRMessageTemplate(NULL),
mCurrentRMessageData(NULL),
mMessageNumbers(number_template_map)
{
}
//virtual
LLTemplateMessageReader::~LLTemplateMessageReader()
{
delete mCurrentRMessageData;
mCurrentRMessageData = NULL;
}
//virtual
void LLTemplateMessageReader::clearMessage()
{
mReceiveSize = -1;
mCurrentRMessageTemplate = NULL;
delete mCurrentRMessageData;
mCurrentRMessageData = NULL;
}
void LLTemplateMessageReader::getData(const char *blockname, const char *varname, void *datap, S32 size, S32 blocknum, S32 max_size)
{
// is there a message ready to go?
if (mReceiveSize == -1)
{
llerrs << "No message waiting for decode 2!" << llendl;
return;
}
if (!mCurrentRMessageData)
{
llerrs << "Invalid mCurrentMessageData in getData!" << llendl;
return;
}
char *bnamep = (char *)blockname + blocknum; // this works because it's just a hash. The bnamep is never derefference
char *vnamep = (char *)varname;
LLMsgData::msg_blk_data_map_t::iterator iter = mCurrentRMessageData->mMemberBlocks.find(bnamep);
if (iter == mCurrentRMessageData->mMemberBlocks.end())
{
llerrs << "Block " << blockname << " #" << blocknum
<< " not in message " << mCurrentRMessageData->mName << llendl;
return;
}
LLMsgBlkData *msg_block_data = iter->second;
LLMsgVarData& vardata = msg_block_data->mMemberVarData[vnamep];
if (!vardata.getName())
{
llerrs << "Variable "<< vnamep << " not in message "
<< mCurrentRMessageData->mName<< " block " << bnamep << llendl;
return;
}
if (size && size != vardata.getSize())
{
llerrs << "Msg " << mCurrentRMessageData->mName
<< " variable " << vnamep
<< " is size " << vardata.getSize()
<< " but copying into buffer of size " << size
<< llendl;
return;
}
const S32 vardata_size = vardata.getSize();
if( max_size >= vardata_size )
{
switch( vardata_size )
{
case 1:
*((U8*)datap) = *((U8*)vardata.getData());
break;
case 2:
*((U16*)datap) = *((U16*)vardata.getData());
break;
case 4:
*((U32*)datap) = *((U32*)vardata.getData());
break;
case 8:
((U32*)datap)[0] = ((U32*)vardata.getData())[0];
((U32*)datap)[1] = ((U32*)vardata.getData())[1];
break;
default:
memcpy(datap, vardata.getData(), vardata_size);
break;
}
}
else
{
llwarns << "Msg " << mCurrentRMessageData->mName
<< " variable " << vnamep
<< " is size " << vardata.getSize()
<< " but truncated to max size of " << max_size
<< llendl;
memcpy(datap, vardata.getData(), max_size);
}
}
S32 LLTemplateMessageReader::getNumberOfBlocks(const char *blockname)
{
// is there a message ready to go?
if (mReceiveSize == -1)
{
llerrs << "No message waiting for decode 3!" << llendl;
return -1;
}
if (!mCurrentRMessageData)
{
llerrs << "Invalid mCurrentRMessageData in getData!" << llendl;
return -1;
}
char *bnamep = (char *)blockname;
LLMsgData::msg_blk_data_map_t::iterator iter = mCurrentRMessageData->mMemberBlocks.find(bnamep);
if (iter == mCurrentRMessageData->mMemberBlocks.end())
{
// sprintf(errmsg, "Block %s not in message %s", bnamep, mCurrentRMessageData->mName);
// llerrs << errmsg << llendl;
// return -1;
return 0;
}
return (iter->second)->mBlockNumber;
}
S32 LLTemplateMessageReader::getSize(const char *blockname, const char *varname)
{
// is there a message ready to go?
if (mReceiveSize == -1)
{
llerrs << "No message waiting for decode 4!" << llendl;
return -1;
}
if (!mCurrentRMessageData)
{
llerrs << "Invalid mCurrentRMessageData in getData!" << llendl;
return -1;
}
char *bnamep = (char *)blockname;
LLMsgData::msg_blk_data_map_t::iterator iter = mCurrentRMessageData->mMemberBlocks.find(bnamep);
if (iter == mCurrentRMessageData->mMemberBlocks.end())
{
llerrs << "Block " << bnamep << " not in message "
<< mCurrentRMessageData->mName << llendl;
return -1;
}
char *vnamep = (char *)varname;
LLMsgBlkData* msg_data = iter->second;
LLMsgVarData& vardata = msg_data->mMemberVarData[vnamep];
if (!vardata.getName())
{
llerrs << "Variable " << varname << " not in message "
<< mCurrentRMessageData->mName << " block " << bnamep << llendl;
return -1;
}
if (mCurrentRMessageTemplate->mMemberBlocks[bnamep]->mType != MBT_SINGLE)
{
llerrs << "Block " << bnamep << " isn't type MBT_SINGLE,"
" use getSize with blocknum argument!" << llendl;
return -1;
}
return vardata.getSize();
}
S32 LLTemplateMessageReader::getSize(const char *blockname, S32 blocknum, const char *varname)
{
// is there a message ready to go?
if (mReceiveSize == -1)
{
llerrs << "No message waiting for decode 5!" << llendl;
return -1;
}
if (!mCurrentRMessageData)
{
llerrs << "Invalid mCurrentRMessageData in getData!" << llendl;
return -1;
}
char *bnamep = (char *)blockname + blocknum;
char *vnamep = (char *)varname;
LLMsgData::msg_blk_data_map_t::iterator iter = mCurrentRMessageData->mMemberBlocks.find(bnamep);
if (iter == mCurrentRMessageData->mMemberBlocks.end())
{
llerrs << "Block " << bnamep << " not in message "
<< mCurrentRMessageData->mName << llendl;
return -1;
}
LLMsgBlkData* msg_data = iter->second;
LLMsgVarData& vardata = msg_data->mMemberVarData[vnamep];
if (!vardata.getName())
{
llerrs << "Variable " << vnamep << " not in message "
<< mCurrentRMessageData->mName << " block " << bnamep << llendl;
return -1;
}
return vardata.getSize();
}
void LLTemplateMessageReader::getBinaryData(const char *blockname,
const char *varname, void *datap,
S32 size, S32 blocknum,
S32 max_size)
{
getData(blockname, varname, datap, size, blocknum, max_size);
}
void LLTemplateMessageReader::getS8(const char *block, const char *var,
S8 &u, S32 blocknum)
{
getData(block, var, &u, sizeof(S8), blocknum);
}
void LLTemplateMessageReader::getU8(const char *block, const char *var,
U8 &u, S32 blocknum)
{
getData(block, var, &u, sizeof(U8), blocknum);
}
void LLTemplateMessageReader::getBOOL(const char *block, const char *var,
BOOL &b, S32 blocknum )
{
U8 value;
getData(block, var, &value, sizeof(U8), blocknum);
b = (BOOL) value;
}
void LLTemplateMessageReader::getS16(const char *block, const char *var,
S16 &d, S32 blocknum)
{
getData(block, var, &d, sizeof(S16), blocknum);
}
void LLTemplateMessageReader::getU16(const char *block, const char *var,
U16 &d, S32 blocknum)
{
getData(block, var, &d, sizeof(U16), blocknum);
}
void LLTemplateMessageReader::getS32(const char *block, const char *var,
S32 &d, S32 blocknum)
{
getData(block, var, &d, sizeof(S32), blocknum);
}
void LLTemplateMessageReader::getU32(const char *block, const char *var,
U32 &d, S32 blocknum)
{
getData(block, var, &d, sizeof(U32), blocknum);
}
void LLTemplateMessageReader::getU64(const char *block, const char *var,
U64 &d, S32 blocknum)
{
getData(block, var, &d, sizeof(U64), blocknum);
}
void LLTemplateMessageReader::getF32(const char *block, const char *var,
F32 &d, S32 blocknum)
{
getData(block, var, &d, sizeof(F32), blocknum);
if( !llfinite( d ) )
{
llwarns << "non-finite in getF32Fast " << block << " " << var
<< llendl;
d = 0;
}
}
void LLTemplateMessageReader::getF64(const char *block, const char *var,
F64 &d, S32 blocknum)
{
getData(block, var, &d, sizeof(F64), blocknum);
if( !llfinite( d ) )
{
llwarns << "non-finite in getF64Fast " << block << " " << var
<< llendl;
d = 0;
}
}
void LLTemplateMessageReader::getVector3(const char *block, const char *var,
LLVector3 &v, S32 blocknum )
{
getData(block, var, v.mV, sizeof(v.mV), blocknum);
if( !v.isFinite() )
{
llwarns << "non-finite in getVector3Fast " << block << " "
<< var << llendl;
v.zeroVec();
}
}
void LLTemplateMessageReader::getVector4(const char *block, const char *var,
LLVector4 &v, S32 blocknum)
{
getData(block, var, v.mV, sizeof(v.mV), blocknum);
if( !v.isFinite() )
{
llwarns << "non-finite in getVector4Fast " << block << " "
<< var << llendl;
v.zeroVec();
}
}
void LLTemplateMessageReader::getVector3d(const char *block, const char *var,
LLVector3d &v, S32 blocknum )
{
getData(block, var, v.mdV, sizeof(v.mdV), blocknum);
if( !v.isFinite() )
{
llwarns << "non-finite in getVector3dFast " << block << " "
<< var << llendl;
v.zeroVec();
}
}
void LLTemplateMessageReader::getQuat(const char *block, const char *var,
LLQuaternion &q, S32 blocknum)
{
LLVector3 vec;
getData(block, var, vec.mV, sizeof(vec.mV), blocknum);
if( vec.isFinite() )
{
q.unpackFromVector3( vec );
}
else
{
llwarns << "non-finite in getQuatFast " << block << " " << var
<< llendl;
q.loadIdentity();
}
}
void LLTemplateMessageReader::getUUID(const char *block, const char *var,
LLUUID &u, S32 blocknum)
{
getData(block, var, u.mData, sizeof(u.mData), blocknum);
}
inline void LLTemplateMessageReader::getIPAddr(const char *block, const char *var, U32 &u, S32 blocknum)
{
getData(block, var, &u, sizeof(U32), blocknum);
}
inline void LLTemplateMessageReader::getIPPort(const char *block, const char *var, U16 &u, S32 blocknum)
{
getData(block, var, &u, sizeof(U16), blocknum);
u = ntohs(u);
}
inline void LLTemplateMessageReader::getString(const char *block, const char *var, S32 buffer_size, char *s, S32 blocknum )
{
s[0] = '\0';
getData(block, var, s, 0, blocknum, buffer_size);
s[buffer_size - 1] = '\0';
}
//virtual
S32 LLTemplateMessageReader::getMessageSize() const
{
return mReceiveSize;
}
// Returns template for the message contained in buffer
BOOL LLTemplateMessageReader::decodeTemplate(
const U8* buffer, S32 buffer_size, // inputs
LLMessageTemplate** msg_template ) // outputs
{
const U8* header = buffer + LL_PACKET_ID_SIZE;
// is there a message ready to go?
if (buffer_size <= 0)
{
llwarns << "No message waiting for decode!" << llendl;
return(FALSE);
}
U32 num = 0;
if (header[0] != 255)
{
// high frequency message
num = header[0];
}
else if ((buffer_size >= ((S32) LL_MINIMUM_VALID_PACKET_SIZE + 1)) && (header[1] != 255))
{
// medium frequency message
num = (255 << 8) | header[1];
}
else if ((buffer_size >= ((S32) LL_MINIMUM_VALID_PACKET_SIZE + 3)) && (header[1] == 255))
{
// low frequency message
U16 message_id_U16 = 0;
// I think this check busts the message system.
// it appears that if there is a NULL in the message #, it won't copy it....
// what was the goal?
//if(header[2])
memcpy(&message_id_U16, &header[2], 2);
// dependant on endian-ness:
// U32 temp = (255 << 24) | (255 << 16) | header[2];
// independant of endian-ness:
message_id_U16 = ntohs(message_id_U16);
num = 0xFFFF0000 | message_id_U16;
}
else // bogus packet received (too short)
{
llwarns << "Packet with unusable length received (too short): "
<< buffer_size << llendl;
return(FALSE);
}
LLMessageTemplate* temp = get_ptr_in_map(mMessageNumbers,num);
if (temp)
{
*msg_template = temp;
}
else
{
llwarns << "Message #" << std::hex << num << std::dec
<< " received but not registered!" << llendl;
gMessageSystem->callExceptionFunc(MX_UNREGISTERED_MESSAGE);
return(FALSE);
}
return(TRUE);
}
void LLTemplateMessageReader::logRanOffEndOfPacket( const LLHost& host )
{
// we've run off the end of the packet!
llwarns << "Ran off end of packet " << mCurrentRMessageTemplate->mName
// << " with id " << mCurrentRecvPacketID
<< " from " << host
<< llendl;
if(gMessageSystem->mVerboseLog)
{
llinfos << "MSG: -> " << host << "\tREAD PAST END:\t"
// << mCurrentRecvPacketID << " "
<< getMessageName() << llendl;
}
gMessageSystem->callExceptionFunc(MX_RAN_OFF_END_OF_PACKET);
}
// decode a given message
BOOL LLTemplateMessageReader::decodeData(const U8* buffer, const LLHost& sender )
{
llassert( mReceiveSize >= 0 );
llassert( mCurrentRMessageTemplate);
llassert( !mCurrentRMessageData );
delete mCurrentRMessageData; // just to make sure
S32 decode_pos = LL_PACKET_ID_SIZE + (S32)(mCurrentRMessageTemplate->mFrequency);
// create base working data set
mCurrentRMessageData = new LLMsgData(mCurrentRMessageTemplate->mName);
// loop through the template building the data structure as we go
for (LLMessageTemplate::message_block_map_t::iterator iter = mCurrentRMessageTemplate->mMemberBlocks.begin();
iter != mCurrentRMessageTemplate->mMemberBlocks.end(); iter++)
{
LLMessageBlock* mbci = iter->second;
U8 repeat_number;
S32 i;
// how many of this block?
if (mbci->mType == MBT_SINGLE)
{
// just one
repeat_number = 1;
}
else if (mbci->mType == MBT_MULTIPLE)
{
// a known number
repeat_number = mbci->mNumber;
}
else if (mbci->mType == MBT_VARIABLE)
{
// need to read the number from the message
// repeat number is a single byte
if (decode_pos >= mReceiveSize)
{
logRanOffEndOfPacket( sender );
return FALSE;
}
repeat_number = buffer[decode_pos];
decode_pos++;
}
else
{
llerrs << "Unknown block type" << llendl;
return FALSE;
}
LLMsgBlkData* cur_data_block = NULL;
// now loop through the block
for (i = 0; i < repeat_number; i++)
{
if (i)
{
// build new name to prevent collisions
// TODO: This should really change to a vector
cur_data_block = new LLMsgBlkData(mbci->mName, repeat_number);
cur_data_block->mName = mbci->mName + i;
}
else
{
cur_data_block = new LLMsgBlkData(mbci->mName, repeat_number);
}
// add the block to the message
mCurrentRMessageData->addBlock(cur_data_block);
// now read the variables
for (LLMessageBlock::message_variable_map_t::iterator iter = mbci->mMemberVariables.begin();
iter != mbci->mMemberVariables.end(); iter++)
{
LLMessageVariable& mvci = *(iter->second);
// ok, build out the variables
// add variable block
cur_data_block->addVariable(mvci.getName(), mvci.getType());
// what type of variable?
if (mvci.getType() == MVT_VARIABLE)
{
// variable, get the number of bytes to read from the template
S32 data_size = mvci.getSize();
U8 tsizeb = 0;
U16 tsizeh = 0;
U32 tsize = 0;
if ((decode_pos + data_size) > mReceiveSize)
{
logRanOffEndOfPacket( sender );
return FALSE;
}
switch(data_size)
{
case 1:
htonmemcpy(&tsizeb, &buffer[decode_pos], MVT_U8, 1);
tsize = tsizeb;
break;
case 2:
htonmemcpy(&tsizeh, &buffer[decode_pos], MVT_U16, 2);
tsize = tsizeh;
break;
case 4:
htonmemcpy(&tsize, &buffer[decode_pos], MVT_U32, 4);
break;
default:
llerrs << "Attempting to read variable field with unknown size of " << data_size << llendl;
break;
}
decode_pos += data_size;
if ((decode_pos + (S32)tsize) > mReceiveSize)
{
logRanOffEndOfPacket( sender );
return FALSE;
}
cur_data_block->addData(mvci.getName(), &buffer[decode_pos], tsize, mvci.getType());
decode_pos += tsize;
}
else
{
// fixed!
// so, copy data pointer and set data size to fixed size
if ((decode_pos + mvci.getSize()) > mReceiveSize)
{
logRanOffEndOfPacket( sender );
return FALSE;
}
cur_data_block->addData(mvci.getName(), &buffer[decode_pos], mvci.getSize(), mvci.getType());
decode_pos += mvci.getSize();
}
}
}
}
if (mCurrentRMessageData->mMemberBlocks.empty()
&& !mCurrentRMessageTemplate->mMemberBlocks.empty())
{
lldebugs << "Empty message '" << mCurrentRMessageTemplate->mName << "' (no blocks)" << llendl;
return FALSE;
}
{
static LLTimer decode_timer;
if(LLMessageReader::getTimeDecodes() || gMessageSystem->getTimingCallback())
{
decode_timer.reset();
}
// if( mCurrentRMessageTemplate->mName == _PREHASH_AgentToNewRegion )
// {
// VTResume(); // VTune
// }
{
LLFastTimer t(LLFastTimer::FTM_PROCESS_MESSAGES);
if( !mCurrentRMessageTemplate->callHandlerFunc(gMessageSystem) )
{
llwarns << "Message from " << sender << " with no handler function received: " << mCurrentRMessageTemplate->mName << llendl;
}
}
// if( mCurrentRMessageTemplate->mName == _PREHASH_AgentToNewRegion )
// {
// VTPause(); // VTune
// }
if(LLMessageReader::getTimeDecodes() || gMessageSystem->getTimingCallback())
{
F32 decode_time = decode_timer.getElapsedTimeF32();
if (gMessageSystem->getTimingCallback())
{
(gMessageSystem->getTimingCallback())(mCurrentRMessageTemplate->mName,
decode_time,
gMessageSystem->getTimingCallbackData());
}
if (LLMessageReader::getTimeDecodes())
{
mCurrentRMessageTemplate->mDecodeTimeThisFrame += decode_time;
mCurrentRMessageTemplate->mTotalDecoded++;
mCurrentRMessageTemplate->mTotalDecodeTime += decode_time;
if( mCurrentRMessageTemplate->mMaxDecodeTimePerMsg < decode_time )
{
mCurrentRMessageTemplate->mMaxDecodeTimePerMsg = decode_time;
}
if(decode_time > LLMessageReader::getTimeDecodesSpamThreshold())
{
lldebugs << "--------- Message " << mCurrentRMessageTemplate->mName << " decode took " << decode_time << " seconds. (" <<
mCurrentRMessageTemplate->mMaxDecodeTimePerMsg << " max, " <<
(mCurrentRMessageTemplate->mTotalDecodeTime / mCurrentRMessageTemplate->mTotalDecoded) << " avg)" << llendl;
}
}
}
}
return TRUE;
}
BOOL LLTemplateMessageReader::validateMessage(const U8* buffer,
S32 buffer_size,
const LLHost& sender)
{
mReceiveSize = buffer_size;
BOOL result = decodeTemplate(buffer, buffer_size, &mCurrentRMessageTemplate );
if(result)
{
mCurrentRMessageTemplate->mReceiveCount++;
lldebugst(LLERR_MESSAGE) << "MessageRecvd:"
<< mCurrentRMessageTemplate->mName
<< " from " << sender << llendl;
}
return result;
}
BOOL LLTemplateMessageReader::readMessage(const U8* buffer,
const LLHost& sender)
{
return decodeData(buffer, sender);
}
//virtual
const char* LLTemplateMessageReader::getMessageName() const
{
static char empty_string[] = "";
return mCurrentRMessageTemplate ? mCurrentRMessageTemplate->mName : empty_string;
}
//virtual
bool LLTemplateMessageReader::isTrusted() const
{
return mCurrentRMessageTemplate->getTrust() == MT_TRUST;
}
//virtual
bool LLTemplateMessageReader::isBanned(bool trustedSource) const
{
return mCurrentRMessageTemplate->isBanned(trustedSource);
}
//virtual
void LLTemplateMessageReader::copyToBuilder(LLMessageBuilder& builder) const
{
if(NULL == mCurrentRMessageTemplate)
{
return;
}
builder.copyFromMessageData(*mCurrentRMessageData);
}
|