|
1 /* |
|
2 * Copyright (c) 2006-2009 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 * Name : SIPMessageUtility.h |
|
16 * Part of : TransactionUser |
|
17 * Version : SIP/5.0 |
|
18 * |
|
19 */ |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 /** |
|
25 @internalComponent |
|
26 */ |
|
27 |
|
28 #ifndef SIPMESSAGEUTILITY_H |
|
29 #define SIPMESSAGEUTILITY_H |
|
30 |
|
31 // INCLUDES |
|
32 #include <e32base.h> |
|
33 #include "SipStackServerDefs.h" |
|
34 #include "SipLogs.h" //USE_SIP_LOGS is defined here |
|
35 #include "TransactionBase.h" |
|
36 |
|
37 // FORWARD DECLARATIONS |
|
38 class RStringF; |
|
39 class TInetAddr; |
|
40 class CURIContainer; |
|
41 class CSIPMessage; |
|
42 class CSIPResponse; |
|
43 class CSIPHeaderBase; |
|
44 class CSIPFromToHeaderBase; |
|
45 class CSIPViaHeader; |
|
46 |
|
47 class CUserAgent; |
|
48 |
|
49 // CONSTANTS |
|
50 |
|
51 // CLASS DECLARATION |
|
52 /* |
|
53 * Miscellaneous SIP message related functions |
|
54 */ |
|
55 class CSIPMessageUtility : public CBase |
|
56 { |
|
57 public: // Constructor and destructor |
|
58 |
|
59 CSIPMessageUtility(); |
|
60 |
|
61 ~CSIPMessageUtility(); |
|
62 |
|
63 public: // New functions |
|
64 |
|
65 /** |
|
66 * Compares two TInetAddr objects. |
|
67 * |
|
68 * @pre aAddr2 has port filled |
|
69 * @param aAddr TInetAddr to compare |
|
70 * @param aTransportProtocol Transport protocol of aAddr |
|
71 * @param aAddr2 Another TInetAddr to compare |
|
72 * @return value ETrue if ip-addresses and ports match, EFalse otherwise |
|
73 */ |
|
74 static TBool CompareTInetAddr(const TInetAddr& aAddr, |
|
75 RStringF aTransportProtocol, |
|
76 const TInetAddr& aAddr2); |
|
77 |
|
78 /** |
|
79 * Adds random characters to the given descriptor. |
|
80 * |
|
81 * @pre aBuf must have space for at least aLength additional characters |
|
82 * |
|
83 * @param aBuf Descriptor into which the random characters are added |
|
84 * @param aLength Amount of characters added to aBuf |
|
85 * @param aCaseSensitive ETrue if both upper and lowercase characters are |
|
86 * allowed to be added into aBuf. EFalse if only lowercase characters are |
|
87 * to be used. |
|
88 * @param aMsg SIP message, contains useful input for generating random |
|
89 * data. Can be NULL. Ownership isn't transferred. |
|
90 * @param aTransactionId Id of the associated transaction |
|
91 * @param aUserAgent Address of the associated UserAgent object, can be |
|
92 * NULL. Ownership isn't transferred. |
|
93 */ |
|
94 void AddRandomStringL(TDes8& aBuf, |
|
95 TInt aLength, |
|
96 TBool aCaseSensitive, |
|
97 CSIPMessage* aMsg, |
|
98 TTransactionId aTransactionId, |
|
99 const CUserAgent* aUserAgent); |
|
100 |
|
101 /** |
|
102 * Computes a checksum over the data pointed by aPtr, and adds the result |
|
103 * into aBuf. |
|
104 * |
|
105 * @param aBuf Buffer to which the result is appended |
|
106 * @param aPtr Pointer to data for which the checksum is calculated |
|
107 * @param aLength Length of the data to be checksummed |
|
108 */ |
|
109 static void ComputeChecksum(TDes8& aBuf, const TAny* aPtr, TInt aLength); |
|
110 |
|
111 /** |
|
112 * Returns a reference to the method of the SIP message. In case of request |
|
113 * this is the request method, and in case of response, it is the method in |
|
114 * CSeq header. |
|
115 * |
|
116 * @param aMsg SIP message |
|
117 * @return value Reference to method |
|
118 */ |
|
119 static RStringF MessageMethod(CSIPMessage& aMsg); |
|
120 |
|
121 /** |
|
122 * Returns the transaction type. |
|
123 * |
|
124 * @param aMsg SIP message |
|
125 * @param aIncomingMsg |
|
126 * ETrue if the message was received from remote endpoint |
|
127 * EFalse if the message is sent by the local endpoint |
|
128 * @return Transaction type |
|
129 */ |
|
130 static CTransactionBase::TTransactionType |
|
131 TransactionType(CSIPMessage& aMsg, TBool aIncomingMsg); |
|
132 |
|
133 /** |
|
134 * Checks whether the top Via header has a branch beginning with the magic |
|
135 * cookie. |
|
136 * |
|
137 * @param aMsg SIP message |
|
138 * @return value ETrue if top via has branch beginning with the magic |
|
139 * cookie |
|
140 */ |
|
141 static TBool HasViaMagicCookie(CSIPMessage& aMsg); |
|
142 |
|
143 /** |
|
144 * Checks whether aTransport has an allowed value. |
|
145 * |
|
146 * @param aTransport Transport protocol |
|
147 * @return value ETrue if aTransport is valid, EFalse otherwise |
|
148 */ |
|
149 static TBool CheckTransport(RStringF aTransport); |
|
150 |
|
151 /** |
|
152 * Reads transport protocol from the top Via header of aMsg, and fills the |
|
153 * value into aTransport. |
|
154 * |
|
155 * @param aMsg IN: SIP message |
|
156 * @param aTransport OUT: transport protocol from aMsg |
|
157 * @return ETrue: Transport was found and set, EFalse: Transport not found, |
|
158 * or has unknown value |
|
159 */ |
|
160 static TBool TransportProtocol(CSIPMessage& aMsg, RStringF& aTransport); |
|
161 |
|
162 /** |
|
163 * Updates the top via header's transport parameter to match the value of |
|
164 * aTransport. |
|
165 * |
|
166 * @pre CheckTransport(aTransport) == ETrue |
|
167 * aMsg has Via header |
|
168 * |
|
169 * @param aMsg SIP message |
|
170 * @param aTransport Transport protocol |
|
171 */ |
|
172 static void UpdateViaTransportL(CSIPMessage& aMsg, RStringF aTransport); |
|
173 |
|
174 /** |
|
175 * Compares tags of two To (or From) headers. |
|
176 * |
|
177 * @param aHeader To (or From) header |
|
178 * @param aHeader2 To (or From) header |
|
179 * @return value ETrue if the tags are equal, EFalse otherwise |
|
180 */ |
|
181 static TBool CompareTags(const CSIPFromToHeaderBase& aHeader, |
|
182 const CSIPFromToHeaderBase& aHeader2); |
|
183 |
|
184 /** |
|
185 * Copies the specified headers from aSrc into aDest, maintaining the |
|
186 * order. |
|
187 * |
|
188 * @param aSrc SIP message from where the headers are copied |
|
189 * @param aDest SIP message to which the headers are copied. If aDest |
|
190 * contains same headers before copying, the old headers are removed. |
|
191 * @param aHeaderName Identifies the headers |
|
192 */ |
|
193 static void CopyHeadersL(CSIPMessage& aSrc, |
|
194 CSIPMessage& aDest, |
|
195 RStringF aHeaderName); |
|
196 |
|
197 /** |
|
198 * Copies the Authorization and Proxy-Authorization headers from aSrc into |
|
199 * aDest. If aSrc is NULL, nothing is done. |
|
200 * |
|
201 * @param aSrc SIP message from where the headers are copied, can be NULL. |
|
202 * Ownership is not transferred. |
|
203 * @param aDest SIP message to which the headers are copied. If aDest |
|
204 * contains same headers before copying, the old headers are removed. |
|
205 */ |
|
206 static void CopyAuthorizationHeadersL(CSIPMessage* aSrc, |
|
207 CSIPMessage& aDest); |
|
208 |
|
209 /** |
|
210 * Makes a copy of the specified header, and returns a pointer to it. |
|
211 * This function only copies one header, and if there are multiple headers |
|
212 * in aMsg, only the first is copied. |
|
213 * |
|
214 * @param aMsg SIP message from where the header is copied |
|
215 * @param aHeaderName Identifies the header |
|
216 * @return Copy of the header, or NULL if header wasn't present in aMsg. |
|
217 * Ownership is transferred. |
|
218 */ |
|
219 static CSIPHeaderBase* CopyHeaderFromMsgL(CSIPMessage& aMsg, |
|
220 RStringF aHeaderName); |
|
221 |
|
222 /** |
|
223 * Fills the CSeq header into aMsg. If aMsg already contains a CSeq header, |
|
224 * it isn't changed. |
|
225 * |
|
226 * @param aMsg IN/OUT: SIP message |
|
227 * @param aSeq CSeq sequence number |
|
228 * @param aMethod CSeq method |
|
229 */ |
|
230 static void FillCSeqL(CSIPMessage& aMsg, |
|
231 TUint aSeq, |
|
232 RStringF aMethod); |
|
233 |
|
234 /** |
|
235 * Checks whether the SIP message is an ACK request |
|
236 * |
|
237 * @param aMsg SIP message |
|
238 * @return ETrue aMsg is ACK, EFalse otherwise |
|
239 */ |
|
240 static TBool IsAck(const CSIPMessage& aMsg); |
|
241 |
|
242 /** |
|
243 * Checks whether the SIP response is a final response. |
|
244 * |
|
245 * @param aResp SIP response |
|
246 * @return ETrue aResp is a final response (2xx-6xx), |
|
247 * EFalse Otherwise |
|
248 */ |
|
249 static TBool IsFinalResponse(const CSIPResponse& aResp); |
|
250 |
|
251 /** |
|
252 * Checks whether the URI has a parameter comp=sigcomp |
|
253 * |
|
254 * @param aUri URI |
|
255 * @return ETrue If aUri has a parameter comp=sigcomp, EFalse otherwise |
|
256 */ |
|
257 static TBool HasSigCompParam(const CURIContainer& aUri); |
|
258 |
|
259 /** |
|
260 * Checks whether the SIP message is a 2xx class response |
|
261 * |
|
262 * @param aMsg SIP message |
|
263 * @return ETrue aMsg is a 2xx response, EFalse otherwise |
|
264 */ |
|
265 static TBool Is2xxResponse(const CSIPMessage& aMsg); |
|
266 |
|
267 /** |
|
268 * Returns the topmost Via header of SIP message. |
|
269 * |
|
270 * @param aMsg SIP message |
|
271 * @return Via header, or NULL if no Via header exists in aMsg. Ownership |
|
272 * is not transferred. |
|
273 */ |
|
274 static CSIPViaHeader* TopVia(CSIPMessage& aMsg); |
|
275 |
|
276 static const TDesC8& BranchMagicCookie(); |
|
277 |
|
278 static const TDesC8& UriDescriptor(const CURIContainer& aUri); |
|
279 |
|
280 /** |
|
281 * Determines if the local address is in the IPv4 private address space. |
|
282 * |
|
283 * @param aTransportMgr TransportMgr |
|
284 * @param aIapId IAP-id |
|
285 * @return ETrue Local address is a private address |
|
286 * EFalse Otherwise |
|
287 */ |
|
288 static TBool IsPrivateAddressL(MSIPTransportMgr& aTransportMgr, |
|
289 TUint32 aIapId); |
|
290 |
|
291 private: |
|
292 |
|
293 static HBufC* ConvertUtf8LC(const TDesC8& aUtf8); |
|
294 |
|
295 /** |
|
296 * Returns the SIP port of aAddr. |
|
297 * |
|
298 * @param aAddr Address |
|
299 * @param aTransportProtocol Transport protocol |
|
300 * @return value Port number |
|
301 */ |
|
302 static TUint SIPPort(const TInetAddr& aAddr, RStringF aTransportProtocol); |
|
303 |
|
304 /** |
|
305 * Builds a buffer containing random data. |
|
306 * |
|
307 * @param aLength Amount of bytes the buffer will contain |
|
308 * @param aMsg SIP message, contains useful input for generating random |
|
309 * data. Can be NULL. Ownership isn't transferred. |
|
310 * @param aTransactionId Id of the associated transaction |
|
311 * @param aUserAgent Address of the associated UserAgent object, can be |
|
312 * NULL. Ownership isn't transferred. |
|
313 * @return value Buffer containing random data. Ownership is transferred. |
|
314 */ |
|
315 HBufC8* BuildInputDataL(TUint aLength, |
|
316 CSIPMessage* aMsg, |
|
317 TTransactionId aTransactionId, |
|
318 const CUserAgent* aUserAgent); |
|
319 |
|
320 /** |
|
321 * Checks if there is space left for aSize amount of bytes in the aBuf. |
|
322 * |
|
323 * @param aBuf Descriptor |
|
324 * @param aSize Amount of bytes |
|
325 * @return value ETrue if aBuf has free space for at least aSize bytes, |
|
326 * EFalse otherwise. |
|
327 */ |
|
328 static TBool FitsInBuf(const TDes8& aBuf, TInt aSize); |
|
329 |
|
330 /** |
|
331 * Returns aBits long sequence of bits from the aBuf. |
|
332 * |
|
333 * @param aBuf Descriptor from where the bits are read |
|
334 * @param aBits How many bits are read |
|
335 * @param aCounter Offset into the descriptor aBuf, indicating the position |
|
336 * from where the bits are read. Zero means the bits are read from the |
|
337 * beginning of aBuf. |
|
338 * @return value Bit sequence containing aBits bits |
|
339 */ |
|
340 static TUint8 GetNextBits(const TDesC8& aBuf, |
|
341 TInt aBits, |
|
342 TUint& aCounter); |
|
343 |
|
344 /** |
|
345 * Adds random data into the aBuf. The random data is generated using the |
|
346 * SIP message (aMsg) as input. |
|
347 * |
|
348 * @param aMsg SIP message, can be NULL. Ownership isn't transferred |
|
349 * @param aBuf Random data is appended to this buffer |
|
350 */ |
|
351 void AddCheckSumOfSipMessageL(CSIPMessage* aMsg, TDes8& aBuf); |
|
352 |
|
353 /** |
|
354 * Adds random data into the aBuf. The random data is generated using |
|
355 * aHeader and iCounter as inputs and by combining the user, host and tag |
|
356 * into a single descriptor, and calculating a checksum over the data. |
|
357 * |
|
358 * @param aHeader To or From header |
|
359 * @param aBuf Random data is appended to this buffer |
|
360 */ |
|
361 void AddCheckSumOfFromToHeaderL(CSIPFromToHeaderBase& aHeader, |
|
362 TDes8& aBuf); |
|
363 |
|
364 /** |
|
365 * Adds random data into the aBuf. The random data is generated using the |
|
366 * CSeq sequence number of the aMsg as input. |
|
367 * |
|
368 * @param aMsg SIP message |
|
369 * @param aBuf Random data is appended to this buffer |
|
370 */ |
|
371 static void AddCheckSumOfCSeq(CSIPMessage& aMsg, TDes8& aBuf); |
|
372 |
|
373 /** |
|
374 * Adds random data into the aBuf. The random data is generated using the |
|
375 * iCounter and current time as inputs. |
|
376 * |
|
377 * @param aBuf Random data is appended to this buffer |
|
378 */ |
|
379 void AddCheckSumOfClock(TDes8& aBuf) const; |
|
380 |
|
381 /** |
|
382 * Adds random data into the aBuf. The random data is generated using the |
|
383 * iCounter and aTransactionId as inputs. |
|
384 * |
|
385 * @param aTransactionId Id of the associated transaction |
|
386 * @param aBuf Random data is appended to this buffer |
|
387 */ |
|
388 void AddCheckSumOfTaIdL(TTransactionId aTransactionId, TDes8& aBuf); |
|
389 |
|
390 /** |
|
391 * Adds random data into the aBuf. The random data is generated using the |
|
392 * request method and Request URI of the aMsg as inputs. If aMsg is a |
|
393 * response, nothing is added to aBuf. |
|
394 * |
|
395 * @param aMsg SIP message |
|
396 * @param aBuf Random data is appended to this buffer |
|
397 */ |
|
398 static void AddCheckSumOfRequestLineL(CSIPMessage& aMsg, TDes8& aBuf); |
|
399 |
|
400 /** |
|
401 * Adds random data into the aBuf. The random data is generated using the |
|
402 * clock, amount of free and used memory and the inactivity time. |
|
403 * |
|
404 * @param aBuf Random data is appended to this buffer |
|
405 */ |
|
406 void AddSystemInfo(TDes8& aBuf) const; |
|
407 |
|
408 private: // Data |
|
409 |
|
410 //Seed value for Math::Rand() |
|
411 TInt64 iSeed; |
|
412 |
|
413 //Counter value used for generating random values |
|
414 TInt32 iCounter; |
|
415 |
|
416 private: // For testing purposes |
|
417 |
|
418 #ifdef CPPUNIT_TEST |
|
419 friend class CSIPMessageUtility_Test; |
|
420 #endif |
|
421 }; |
|
422 |
|
423 #endif // end of SIPMESSAGEUTILITY_H |
|
424 |
|
425 // End of File |