|
1 /* |
|
2 * Copyright (c) 2008 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: Local address resolver |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include "localaddressresolver.h" |
|
21 #include "ikedebug.h" |
|
22 #include "ikesocketassert.h" |
|
23 |
|
24 using namespace IkeSocket; |
|
25 |
|
26 // ======== MEMBER FUNCTIONS ======== |
|
27 |
|
28 // --------------------------------------------------------------------------- |
|
29 // Two-phased constructor. |
|
30 // --------------------------------------------------------------------------- |
|
31 // |
|
32 CLocalAddressResolver* CLocalAddressResolver::NewL( RSocketServ& aSocketServer, |
|
33 RConnection& aConnection, |
|
34 MIkeDebug& aDebug ) |
|
35 { |
|
36 CLocalAddressResolver* self = |
|
37 new ( ELeave ) CLocalAddressResolver( aSocketServer, |
|
38 aConnection, |
|
39 aDebug ); |
|
40 CleanupStack::PushL( self ); |
|
41 self->ConstructL(); |
|
42 CleanupStack::Pop( self ); |
|
43 return self; |
|
44 } |
|
45 |
|
46 // --------------------------------------------------------------------------- |
|
47 // Destructor |
|
48 // --------------------------------------------------------------------------- |
|
49 // |
|
50 CLocalAddressResolver::~CLocalAddressResolver() |
|
51 { |
|
52 DEBUG_LOG( _L("CLocalAddressResolver::~CLocalAddressResolver") ); |
|
53 } |
|
54 |
|
55 // --------------------------------------------------------------------------- |
|
56 // Constructor |
|
57 // --------------------------------------------------------------------------- |
|
58 // |
|
59 CLocalAddressResolver::CLocalAddressResolver( RSocketServ& aSocketServer, |
|
60 RConnection& aConnection, |
|
61 MIkeDebug& aDebug ) |
|
62 : iSocketServer( aSocketServer ), |
|
63 iConnection( aConnection ), |
|
64 iDebug( aDebug ) |
|
65 { |
|
66 } |
|
67 |
|
68 // --------------------------------------------------------------------------- |
|
69 // Second phase construction. |
|
70 // --------------------------------------------------------------------------- |
|
71 // |
|
72 void CLocalAddressResolver::ConstructL() |
|
73 { |
|
74 DEBUG_LOG( _L("CLocalAddressResolver::ConstructL") ); |
|
75 } |
|
76 |
|
77 // --------------------------------------------------------------------------- |
|
78 // Return information whether local IP address has been resolved or not. |
|
79 // --------------------------------------------------------------------------- |
|
80 // |
|
81 TBool CLocalAddressResolver::HasIPAddr( const TIpVersion aIpVersion ) const |
|
82 { |
|
83 IKESOCKET_ASSERT( aIpVersion == EIPv4 || aIpVersion == EIPv6 ); |
|
84 |
|
85 TBool hasIPAddr( EFalse ); |
|
86 |
|
87 if ( aIpVersion == EIPv4 && |
|
88 !iIPv4Addr.IsUnspecified() ) |
|
89 { |
|
90 hasIPAddr = ETrue; |
|
91 } |
|
92 else if ( aIpVersion == EIPv6 && |
|
93 !iIPv6Addr.IsUnspecified() ) |
|
94 { |
|
95 hasIPAddr = ETrue; |
|
96 } |
|
97 return hasIPAddr; |
|
98 } |
|
99 |
|
100 // --------------------------------------------------------------------------- |
|
101 // Returns local IP address. |
|
102 // --------------------------------------------------------------------------- |
|
103 // |
|
104 const TInetAddr& CLocalAddressResolver::IPAddr( const TIpVersion aIpVersion ) const |
|
105 { |
|
106 IKESOCKET_ASSERT( aIpVersion == EIPv4 || aIpVersion == EIPv6 ); |
|
107 |
|
108 if ( aIpVersion == EIPv4) |
|
109 { |
|
110 return iIPv4Addr; |
|
111 } |
|
112 |
|
113 return iIPv6Addr; |
|
114 } |
|
115 |
|
116 // --------------------------------------------------------------------------- |
|
117 // Refreshes local IP addresses for the connection |
|
118 // --------------------------------------------------------------------------- |
|
119 // |
|
120 TInt CLocalAddressResolver::RefreshLocalAddresses() |
|
121 { |
|
122 // Get IAP Id of the connection |
|
123 _LIT( KIapId, "IAP\\Id" ); |
|
124 TInt ret = iConnection.GetIntSetting( KIapId, iIapId ) ; |
|
125 |
|
126 if ( ret == KErrNone ) |
|
127 { |
|
128 RSocket socket; |
|
129 ret = socket.Open( iSocketServer, |
|
130 KAfInet, |
|
131 KSockDatagram, |
|
132 KProtocolInetUdp, |
|
133 iConnection ); |
|
134 |
|
135 // Initialize IP addresses |
|
136 iIPv4Addr.Init( KAfInet ); |
|
137 iIPv6Addr.Init( KAfInet ); |
|
138 |
|
139 if ( ret == KErrNone ) |
|
140 { |
|
141 // Begin enumeration of network interfaces. |
|
142 TInt err = socket.SetOpt( KSoInetEnumInterfaces, |
|
143 KSolInetIfCtrl ); |
|
144 if ( err == KErrNone ) |
|
145 { |
|
146 // Return details of the first interface in the enumeration. |
|
147 TPckgBuf<TSoInetInterfaceInfo> details; |
|
148 err = socket.GetOpt( KSoInetNextInterface, |
|
149 KSolInetIfCtrl, |
|
150 details ); |
|
151 |
|
152 while ( err == KErrNone ) |
|
153 { |
|
154 // Query IAP for the interface. |
|
155 TPckgBuf<TSoInetIfQuery> query; |
|
156 query().iName = details().iName; |
|
157 err = socket.GetOpt( KSoInetIfQueryByName, |
|
158 KSolInetIfQuery, |
|
159 query ); |
|
160 |
|
161 if ( err == KErrNone ) |
|
162 { |
|
163 // Match information of the interface with the |
|
164 // connection. |
|
165 if ( Match( details(), query() ) ) |
|
166 { |
|
167 // Match found. Set local IP address if not yet |
|
168 // set. |
|
169 SetAddressIfNotSet( details().iAddress ); |
|
170 } |
|
171 } |
|
172 // Return details of the next interface. If the interface |
|
173 // has multiple addresses then details for each address |
|
174 // are returned separately in the enumeration. |
|
175 err = socket.GetOpt( KSoInetNextInterface, |
|
176 KSolInetIfCtrl, |
|
177 details ); |
|
178 } |
|
179 } |
|
180 socket.Close(); |
|
181 } |
|
182 } |
|
183 |
|
184 DEBUG_LOG1( _L("CLocalAddressResolver::RefreshLocalAddresses, ret=%d"), ret ); |
|
185 return ret; |
|
186 } |
|
187 |
|
188 // --------------------------------------------------------------------------- |
|
189 // Gets local IP address. |
|
190 // --------------------------------------------------------------------------- |
|
191 // |
|
192 TInt CLocalAddressResolver::GetLocalAddress( const TIpVersion aIpVersion, |
|
193 TInetAddr& aLocalIp ) |
|
194 { |
|
195 IKESOCKET_ASSERT( aIpVersion == EIPv4 || aIpVersion == EIPv6 ); |
|
196 DEBUG_LOG1( _L("CLocalAddressResolver::GetLocalAddress, aIpVersion=%d"), |
|
197 aIpVersion); |
|
198 |
|
199 TInt ret = RefreshLocalAddresses(); |
|
200 |
|
201 if ( ret ) |
|
202 { |
|
203 return ret; |
|
204 } |
|
205 |
|
206 if ( HasIPAddr( aIpVersion ) ) |
|
207 { |
|
208 aLocalIp = IPAddr( aIpVersion ); |
|
209 } |
|
210 else |
|
211 { |
|
212 ret = KErrNotFound; |
|
213 } |
|
214 return ret; |
|
215 } |
|
216 |
|
217 // --------------------------------------------------------------------------- |
|
218 // Mathches information of an interface and the connection. |
|
219 // --------------------------------------------------------------------------- |
|
220 // |
|
221 TBool CLocalAddressResolver::Match( |
|
222 const TSoInetInterfaceInfo& aInfo, |
|
223 const TSoInetIfQuery& aQuery ) const |
|
224 { |
|
225 TBool match = EFalse; |
|
226 if ( !aInfo.iAddress.IsUnspecified() && |
|
227 !aInfo.iAddress.IsLoopback() && |
|
228 !aInfo.iAddress.IsLinkLocal() && |
|
229 aQuery.iZone[1] == iIapId ) // Match IAPs of interface and connection |
|
230 { |
|
231 match = ETrue; |
|
232 } |
|
233 return match; |
|
234 } |
|
235 |
|
236 // --------------------------------------------------------------------------- |
|
237 // Sets local IP address if not set. |
|
238 // --------------------------------------------------------------------------- |
|
239 // |
|
240 void CLocalAddressResolver::SetAddressIfNotSet( const TInetAddr& aAddr ) |
|
241 { |
|
242 TInetAddr addr = aAddr; |
|
243 |
|
244 if ( addr.Family() == KAfInet ) // IPv4 address |
|
245 { |
|
246 SetIPv4AddressIfNotSet( addr ); |
|
247 } |
|
248 else if ( addr.Family() == KAfInet6 ) |
|
249 { |
|
250 if ( addr.IsV4Mapped() ) // IPv4 mapped address |
|
251 { |
|
252 addr.ConvertToV4(); |
|
253 SetIPv4AddressIfNotSet( addr ); |
|
254 } |
|
255 else // IPv6 address |
|
256 { |
|
257 SetIPv6AddressIfNotSet( addr ); |
|
258 } |
|
259 } |
|
260 } |
|
261 |
|
262 // --------------------------------------------------------------------------- |
|
263 // Sets local IPv4 address if not set. |
|
264 // --------------------------------------------------------------------------- |
|
265 // |
|
266 void CLocalAddressResolver::SetIPv4AddressIfNotSet( const TInetAddr& aAddr ) |
|
267 { |
|
268 if ( iIPv4Addr.IsUnspecified() ) |
|
269 { |
|
270 #ifdef _DEBUG |
|
271 TBuf<100> txt_addr; |
|
272 aAddr.Output( txt_addr ); |
|
273 DEBUG_LOG1( _L("CLocalAddressResolver::SetIPv4AddressIfNotSet, address=%S"), |
|
274 &txt_addr ); |
|
275 #endif |
|
276 |
|
277 iIPv4Addr = aAddr; |
|
278 } |
|
279 } |
|
280 |
|
281 // --------------------------------------------------------------------------- |
|
282 // Sets local IPv6 address if not set. |
|
283 // --------------------------------------------------------------------------- |
|
284 // |
|
285 void CLocalAddressResolver::SetIPv6AddressIfNotSet( const TInetAddr& aAddr ) |
|
286 { |
|
287 if ( iIPv6Addr.IsUnspecified() ) |
|
288 { |
|
289 #ifdef _DEBUG |
|
290 TBuf<100> txt_addr; |
|
291 aAddr.Output( txt_addr ); |
|
292 DEBUG_LOG1( _L("CLocalAddressResolver::SetIPv6AddressIfNotSet, address=%S"), |
|
293 &txt_addr ); |
|
294 #endif |
|
295 |
|
296 iIPv6Addr = aAddr; |
|
297 } |
|
298 } |
|
299 |