|
1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // Name : CSIPHostPort.cpp |
|
15 // Part of : SIP Codec |
|
16 // Version : SIP/4.0 |
|
17 // |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 #include "siphostport.h" |
|
23 #include "sipcodecerr.h" |
|
24 #include "SIPSyntaxCheck.h" |
|
25 #include "_sipcodecdefs.h" |
|
26 |
|
27 _LIT8(KPortFormat, "%u"); |
|
28 |
|
29 |
|
30 // ----------------------------------------------------------------------------- |
|
31 // CSIPHostPort::DecodeL |
|
32 // ----------------------------------------------------------------------------- |
|
33 // |
|
34 EXPORT_C CSIPHostPort* CSIPHostPort::DecodeL (const TDesC8& aValue) |
|
35 { |
|
36 __ASSERT_ALWAYS (aValue.Length() > 0, User::Leave(KErrSipCodecHostPort)); |
|
37 |
|
38 CSIPHostPort* hostPort = new(ELeave)CSIPHostPort; |
|
39 CleanupStack::PushL (hostPort); |
|
40 |
|
41 if (aValue.Locate('[') == 0) // seems to be an IPv6 address |
|
42 { |
|
43 TInt hostEndPos = aValue.Locate(']'); |
|
44 if (hostEndPos > 1) |
|
45 { |
|
46 hostPort->SetHostL (aValue.Mid(1,hostEndPos-1)); |
|
47 if (hostEndPos < aValue.Length()-1) // seems to have a port |
|
48 { |
|
49 TLex8 lex(aValue.Mid(hostEndPos+1)); |
|
50 lex.SkipSpace(); |
|
51 TPtrC8 portAsText(lex.Remainder()); |
|
52 if (portAsText.Length() > 0) |
|
53 { |
|
54 TInt colonPos = portAsText.Locate (':'); |
|
55 if (colonPos != 0) User::Leave(KErrSipCodecPort); |
|
56 hostPort->SetPort (ParsePortL(portAsText.Mid(1))); |
|
57 } |
|
58 } |
|
59 } |
|
60 else |
|
61 { |
|
62 User::Leave(KErrSipCodecHostPort); |
|
63 } |
|
64 __ASSERT_ALWAYS (hostPort->HostType() == ESIPIpv6, |
|
65 User::Leave(KErrSipCodecHostPort)); |
|
66 } |
|
67 else // seems to be an IPv4 address or a host name |
|
68 { |
|
69 TInt colonPos = aValue.Locate(':'); |
|
70 if (colonPos == 0 || colonPos == aValue.Length()-1) |
|
71 { |
|
72 User::Leave(KErrSipCodecPort); |
|
73 } |
|
74 if (colonPos > 0) // has port |
|
75 { |
|
76 hostPort->SetPort (ParsePortL (aValue.Mid(colonPos+1))); |
|
77 hostPort->SetHostL (aValue.Left(colonPos)); |
|
78 } |
|
79 else // has only host part |
|
80 { |
|
81 hostPort->SetHostL (aValue); |
|
82 } |
|
83 } |
|
84 CleanupStack::Pop(hostPort); |
|
85 |
|
86 return hostPort; |
|
87 } |
|
88 |
|
89 // ----------------------------------------------------------------------------- |
|
90 // CSIPHostPort::NewL |
|
91 // ----------------------------------------------------------------------------- |
|
92 // |
|
93 EXPORT_C CSIPHostPort* CSIPHostPort::NewL (const CSIPHostPort& aHostPort) |
|
94 { |
|
95 CSIPHostPort* self = CSIPHostPort::NewLC (aHostPort); |
|
96 CleanupStack::Pop(self); |
|
97 return self; |
|
98 } |
|
99 |
|
100 // ----------------------------------------------------------------------------- |
|
101 // CSIPHostPort::NewLC |
|
102 // ----------------------------------------------------------------------------- |
|
103 // |
|
104 EXPORT_C CSIPHostPort* CSIPHostPort::NewLC (const CSIPHostPort& aHostPort) |
|
105 { |
|
106 CSIPHostPort* self = new(ELeave)CSIPHostPort; |
|
107 CleanupStack::PushL(self); |
|
108 self->ConstructL (aHostPort); |
|
109 return self; |
|
110 } |
|
111 |
|
112 // ----------------------------------------------------------------------------- |
|
113 // CSIPHostPort::CSIPHostPort |
|
114 // ----------------------------------------------------------------------------- |
|
115 // |
|
116 CSIPHostPort::CSIPHostPort() |
|
117 : iHostType (ESIPNoHost), |
|
118 iHasPort (EFalse) |
|
119 { |
|
120 } |
|
121 |
|
122 // ----------------------------------------------------------------------------- |
|
123 // CSIPHostPort::ConstructL |
|
124 // ----------------------------------------------------------------------------- |
|
125 // |
|
126 void CSIPHostPort::ConstructL () |
|
127 { |
|
128 iHost = HBufC8::NewL(0); |
|
129 } |
|
130 |
|
131 // ----------------------------------------------------------------------------- |
|
132 // CSIPHostPort::ConstructL |
|
133 // ----------------------------------------------------------------------------- |
|
134 // |
|
135 void CSIPHostPort::ConstructL (const CSIPHostPort& aHostPort) |
|
136 { |
|
137 iHost = aHostPort.iHost->AllocL(); |
|
138 iHostType = aHostPort.iHostType; |
|
139 iHasPort = aHostPort.iHasPort; |
|
140 iPort = aHostPort.iPort; |
|
141 } |
|
142 |
|
143 // ----------------------------------------------------------------------------- |
|
144 // CSIPHostPort::~CSIPHostPort |
|
145 // ----------------------------------------------------------------------------- |
|
146 // |
|
147 EXPORT_C CSIPHostPort::~CSIPHostPort() |
|
148 { |
|
149 delete iHost; |
|
150 } |
|
151 |
|
152 // ----------------------------------------------------------------------------- |
|
153 // CSIPHostPort::SetHostL |
|
154 // ----------------------------------------------------------------------------- |
|
155 // |
|
156 EXPORT_C void CSIPHostPort::SetHostL (const TDesC8& aHost) |
|
157 { |
|
158 HBufC8* tmp = aHost.AllocLC(); |
|
159 tmp->Des().Trim(); |
|
160 if (tmp->Length() == 0) |
|
161 { |
|
162 User::Leave(KErrSipCodecHost); |
|
163 } |
|
164 |
|
165 TType hostType; |
|
166 TInt err = SIPSyntaxCheck::HostType(*tmp,hostType); |
|
167 if (err != KErrNone) |
|
168 { |
|
169 User::Leave (err); |
|
170 } |
|
171 |
|
172 CleanupStack::Pop(tmp); |
|
173 |
|
174 delete iHost; |
|
175 iHost = tmp; |
|
176 iHostType = hostType; |
|
177 } |
|
178 |
|
179 // ----------------------------------------------------------------------------- |
|
180 // CSIPHostPort::HostType |
|
181 // ----------------------------------------------------------------------------- |
|
182 // |
|
183 EXPORT_C CSIPHostPort::TType CSIPHostPort::HostType () const |
|
184 { |
|
185 return iHostType; |
|
186 } |
|
187 |
|
188 // ----------------------------------------------------------------------------- |
|
189 // CSIPHostPort::Host |
|
190 // ----------------------------------------------------------------------------- |
|
191 // |
|
192 EXPORT_C const TDesC8& CSIPHostPort::Host () const |
|
193 { |
|
194 return *iHost; |
|
195 } |
|
196 |
|
197 // ----------------------------------------------------------------------------- |
|
198 // CSIPHostPort::HasPort |
|
199 // ----------------------------------------------------------------------------- |
|
200 // |
|
201 EXPORT_C TBool CSIPHostPort::HasPort() const |
|
202 { |
|
203 return iHasPort; |
|
204 } |
|
205 |
|
206 // ----------------------------------------------------------------------------- |
|
207 // CSIPHostPort::Port |
|
208 // ----------------------------------------------------------------------------- |
|
209 // |
|
210 EXPORT_C TUint CSIPHostPort::Port() const |
|
211 { |
|
212 if (iHasPort) |
|
213 { |
|
214 return iPort; |
|
215 } |
|
216 return 0; |
|
217 } |
|
218 |
|
219 // ----------------------------------------------------------------------------- |
|
220 // CSIPHostPort::SetPort |
|
221 // ----------------------------------------------------------------------------- |
|
222 // |
|
223 EXPORT_C void CSIPHostPort::SetPort (TUint aPort) |
|
224 { |
|
225 iPort = aPort; |
|
226 iHasPort = ETrue; |
|
227 } |
|
228 |
|
229 // ----------------------------------------------------------------------------- |
|
230 // CSIPHostPort::DeletePort |
|
231 // ----------------------------------------------------------------------------- |
|
232 // |
|
233 EXPORT_C TInt CSIPHostPort::DeletePort () |
|
234 { |
|
235 if (!iHasPort) |
|
236 { |
|
237 return KErrNotFound; |
|
238 } |
|
239 iPort = 0; |
|
240 iHasPort = EFalse; |
|
241 return KErrNone; |
|
242 } |
|
243 |
|
244 // ----------------------------------------------------------------------------- |
|
245 // CSIPHostPort::operator== |
|
246 // ----------------------------------------------------------------------------- |
|
247 // |
|
248 EXPORT_C TBool CSIPHostPort::operator==(const CSIPHostPort& aHostPort) const |
|
249 { |
|
250 if (iHostType != aHostPort.iHostType) |
|
251 { |
|
252 return EFalse; |
|
253 } |
|
254 if (iHost->CompareF(*(aHostPort.iHost)) != 0) |
|
255 { |
|
256 return EFalse; |
|
257 } |
|
258 if (iHasPort != aHostPort.iHasPort) |
|
259 { |
|
260 return EFalse; |
|
261 } |
|
262 if (iHasPort) |
|
263 { |
|
264 if (iPort != aHostPort.iPort) |
|
265 { |
|
266 return EFalse; |
|
267 } |
|
268 } |
|
269 return ETrue; |
|
270 } |
|
271 |
|
272 // ----------------------------------------------------------------------------- |
|
273 // CSIPHostPort::ToTextLC |
|
274 // ----------------------------------------------------------------------------- |
|
275 // |
|
276 HBufC8* CSIPHostPort::ToTextLC () const |
|
277 { |
|
278 TUint encodedLength = iHost->Length(); |
|
279 if (iHostType == ESIPIpv6) |
|
280 { |
|
281 const TInt KBracketsLength = 2; // [] |
|
282 encodedLength += KBracketsLength; |
|
283 } |
|
284 |
|
285 const TInt KMaxPortAsText = 40; |
|
286 TBuf8<KMaxPortAsText> portAsText; |
|
287 if (iHasPort) |
|
288 { |
|
289 portAsText.Format(KPortFormat, iPort); |
|
290 encodedLength += 1 + portAsText.Length(); // COLON + port |
|
291 } |
|
292 |
|
293 HBufC8* encodedHostPort = HBufC8::NewLC(encodedLength); |
|
294 TPtr8 encodedHostPortPtr = encodedHostPort->Des(); |
|
295 if (iHostType == ESIPIpv6) |
|
296 { |
|
297 encodedHostPortPtr.Append('['); |
|
298 encodedHostPortPtr.Append(*iHost); |
|
299 encodedHostPortPtr.Append(']'); |
|
300 } |
|
301 else |
|
302 { |
|
303 encodedHostPortPtr.Append(*iHost); |
|
304 } |
|
305 |
|
306 if (iHasPort) |
|
307 { |
|
308 encodedHostPortPtr.Append(':'); |
|
309 encodedHostPortPtr.Append(portAsText); |
|
310 } |
|
311 return encodedHostPort; |
|
312 } |
|
313 |
|
314 // ----------------------------------------------------------------------------- |
|
315 // CSIPHostPort::ParsePortL |
|
316 // ----------------------------------------------------------------------------- |
|
317 // |
|
318 TUint CSIPHostPort::ParsePortL (const TDesC8& aValue) |
|
319 { |
|
320 TLex8 lex(aValue); |
|
321 lex.SkipSpace(); |
|
322 TPtrC8 remainder(lex.Remainder()); |
|
323 if (remainder.Length() == 0) |
|
324 { |
|
325 User::Leave(KErrSipCodecPort); |
|
326 } |
|
327 TUint parsedValue=0; |
|
328 if (lex.Val(parsedValue) != KErrNone) |
|
329 { |
|
330 User::Leave (KErrSipCodecPort); |
|
331 } |
|
332 lex.SkipSpace(); |
|
333 if (lex.Remainder().Length() != 0) |
|
334 { |
|
335 User::Leave (KErrSipCodecPort); |
|
336 } |
|
337 return parsedValue; |
|
338 } |