|
1 /* |
|
2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 #include <hash.h> |
|
22 #include <e32math.h> |
|
23 #include "stunassert.h" |
|
24 #include "ctransactionidgenerator.h" |
|
25 |
|
26 |
|
27 // ======== MEMBER FUNCTIONS ======== |
|
28 |
|
29 // --------------------------------------------------------------------------- |
|
30 // CTransactionIDGenerator::NewL |
|
31 // --------------------------------------------------------------------------- |
|
32 // |
|
33 CTransactionIDGenerator* CTransactionIDGenerator::NewL() |
|
34 { |
|
35 return new ( ELeave ) CTransactionIDGenerator(); |
|
36 } |
|
37 |
|
38 // --------------------------------------------------------------------------- |
|
39 // CTransactionIDGenerator::CTransactionIDGenerator |
|
40 // --------------------------------------------------------------------------- |
|
41 // |
|
42 CTransactionIDGenerator::CTransactionIDGenerator() |
|
43 { |
|
44 TUint ticks = User::TickCount(); |
|
45 TTime now; |
|
46 now.UniversalTime(); |
|
47 TInt64 us = now.Int64(); |
|
48 |
|
49 iSeed = static_cast<TInt64>( ticks ) + us; |
|
50 iCounter = I64LOW( us ) - ticks; |
|
51 } |
|
52 |
|
53 // --------------------------------------------------------------------------- |
|
54 // CTransactionIDGenerator::CTransactionIDGenerator |
|
55 // Dummy implementation, as copy constructor is declared private and not used. |
|
56 // --------------------------------------------------------------------------- |
|
57 // |
|
58 CTransactionIDGenerator::CTransactionIDGenerator( |
|
59 const CTransactionIDGenerator& /*aTransactionIDGenerator*/ ) : |
|
60 CBase() |
|
61 { |
|
62 } |
|
63 |
|
64 // --------------------------------------------------------------------------- |
|
65 // CTransactionIDGenerator::~CTransactionIDGenerator |
|
66 // --------------------------------------------------------------------------- |
|
67 // |
|
68 CTransactionIDGenerator::~CTransactionIDGenerator() |
|
69 { |
|
70 } |
|
71 |
|
72 // --------------------------------------------------------------------------- |
|
73 // CTransactionIDGenerator::GetIDL |
|
74 // CMD5 exists only during hash computing, to save memory. |
|
75 // --------------------------------------------------------------------------- |
|
76 // |
|
77 void CTransactionIDGenerator::GetIDL( TAny* aObject, |
|
78 TInt aObjectSize, |
|
79 TNATFWUNSAFTransactionID& aTransactionID ) |
|
80 { |
|
81 ++iCounter; |
|
82 HBufC8* data = BuildInputDataLC( aObject, aObjectSize ); |
|
83 |
|
84 CMD5* md5 = CMD5::NewL(); |
|
85 CleanupStack::PushL( md5 ); |
|
86 TPtrC8 hash = md5->Hash( *data ); |
|
87 |
|
88 __STUN_ASSERT_L( md5->HashSize() >= KMaxNATFWUNSAFTransactionIdLength, |
|
89 KErrUnderflow ); |
|
90 TPtrC8 ptrToHash = hash.Left(KMaxNATFWUNSAFTransactionIdLength); |
|
91 aTransactionID = ptrToHash; |
|
92 |
|
93 CleanupStack::PopAndDestroy( md5 ); |
|
94 CleanupStack::PopAndDestroy( data ); |
|
95 } |
|
96 |
|
97 // --------------------------------------------------------------------------- |
|
98 // CTransactionIDGenerator::BuildInputDataL |
|
99 // --------------------------------------------------------------------------- |
|
100 // |
|
101 HBufC8* CTransactionIDGenerator::BuildInputDataLC( TAny* aObject, |
|
102 TInt aObjectSize ) |
|
103 { |
|
104 const TInt KAmountOfRandomNbrsToFill = 10; |
|
105 |
|
106 //Size of the input data buffer. It contains the following items: |
|
107 // |
|
108 //item item's size |
|
109 //---- ----------- |
|
110 //iCounter sizeof( iCounter ) |
|
111 //aObject's state aObjectSize |
|
112 //checksum of this sizeof( TUint16 ) |
|
113 //clock info sizeof( TInt64 ) + sizeof( TUint ) |
|
114 //system info sizeof( TUint16 ) + sizeof( TUint8 ) |
|
115 //KAmountOfRandomNbrsToFill |
|
116 //random integer values KAmountOfRandomNbrsToFill * sizeof( TInt ) |
|
117 TInt dataSize = sizeof( iCounter ) + aObjectSize + |
|
118 sizeof( TUint16 ) + sizeof( TInt64 ) + sizeof( TUint ) + |
|
119 sizeof( TUint16 ) + sizeof( TUint8 ) + |
|
120 KAmountOfRandomNbrsToFill * sizeof( TInt ); |
|
121 HBufC8* buf = HBufC8::NewLC( dataSize ); |
|
122 |
|
123 TPtr8 ptr = buf->Des(); |
|
124 |
|
125 ptr.Append( reinterpret_cast<const TUint8*>( &iCounter ), |
|
126 sizeof( iCounter ) ); |
|
127 ptr.Append( reinterpret_cast<const TUint8*>( aObject ), aObjectSize ); |
|
128 |
|
129 ComputeChecksum( ptr, this, sizeof( this ) ); |
|
130 |
|
131 AddClockInfo( ptr ); |
|
132 AddSystemInfo( ptr ); |
|
133 |
|
134 TInt random = 0; |
|
135 TInt randomNumberSize = sizeof( random ); |
|
136 while ( ptr.Size() <= ( ptr.MaxSize() - randomNumberSize ) ) |
|
137 { |
|
138 random = Math::Rand( iSeed ); |
|
139 ptr.Append( reinterpret_cast<const TUint8*>( &random ), |
|
140 sizeof( random ) ); |
|
141 } |
|
142 |
|
143 return buf; |
|
144 } |
|
145 |
|
146 // ----------------------------------------------------------------------------- |
|
147 // CTransactionIDGenerator::AddClockInfo |
|
148 // ----------------------------------------------------------------------------- |
|
149 // |
|
150 void CTransactionIDGenerator::AddClockInfo( TDes8& aBuf ) const |
|
151 { |
|
152 TTime now; |
|
153 now.UniversalTime(); |
|
154 TInt64 timeAsInt = now.Int64(); |
|
155 |
|
156 aBuf.Append( reinterpret_cast<const TUint8*>( &timeAsInt ), |
|
157 sizeof( timeAsInt ) ); |
|
158 |
|
159 TUint ticks = User::TickCount(); |
|
160 aBuf.Append( reinterpret_cast<const TUint8*>( &ticks ), sizeof( ticks ) ); |
|
161 } |
|
162 |
|
163 // ----------------------------------------------------------------------------- |
|
164 // CTransactionIDGenerator::AddSystemInfo |
|
165 // ----------------------------------------------------------------------------- |
|
166 // |
|
167 void CTransactionIDGenerator::AddSystemInfo( TDes8& aBuf ) const |
|
168 { |
|
169 TInt biggestBlock = 0; |
|
170 TInt totalAvailable = User::Available( biggestBlock ); |
|
171 TInt value = biggestBlock + totalAvailable - User::CountAllocCells(); |
|
172 ComputeChecksum( aBuf, &value, sizeof( value ) ); |
|
173 |
|
174 TTimeIntervalSeconds inactivity = User::InactivityTime(); |
|
175 if ( inactivity.Int() > 0 ) |
|
176 { |
|
177 TUint8 byteVal = static_cast<TUint8>( inactivity.Int() & 0xff ); |
|
178 aBuf.Append( &byteVal, sizeof( byteVal ) ); |
|
179 } |
|
180 } |
|
181 |
|
182 // --------------------------------------------------------------------------- |
|
183 // CTransactionIDGenerator::ComputeChecksum |
|
184 // --------------------------------------------------------------------------- |
|
185 // |
|
186 void CTransactionIDGenerator::ComputeChecksum( TDes8& aBuf, |
|
187 const TAny* aPtr, |
|
188 TInt aLength ) const |
|
189 { |
|
190 __STUN_ASSERT_RETURN( aPtr != NULL, KErrArgument ); |
|
191 |
|
192 TUint16 cs = 0; |
|
193 Mem::Crc( cs, aPtr, aLength ); |
|
194 aBuf.Append( reinterpret_cast<const TUint8*>( &cs ), sizeof( cs ) ); |
|
195 } |