|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtNetwork of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 //#define QNETWORKINTERFACE_DEBUG |
|
43 |
|
44 #include "qnetworkinterface.h" |
|
45 #include "qnetworkinterface_p.h" |
|
46 #include "../corelib/kernel/qcore_symbian_p.h" |
|
47 |
|
48 #ifndef QT_NO_NETWORKINTERFACE |
|
49 |
|
50 #include <in_sock.h> |
|
51 #include <in_iface.h> |
|
52 #include <es_sock.h> |
|
53 |
|
54 QT_BEGIN_NAMESPACE |
|
55 |
|
56 |
|
57 static QNetworkInterface::InterfaceFlags convertFlags(const TSoInetInterfaceInfo& aInfo) |
|
58 { |
|
59 QNetworkInterface::InterfaceFlags flags = 0; |
|
60 flags |= (aInfo.iState == EIfUp) ? QNetworkInterface::IsUp : QNetworkInterface::InterfaceFlag(0); |
|
61 // We do not have separate flag for running in Symbian OS |
|
62 flags |= (aInfo.iState == EIfUp) ? QNetworkInterface::IsRunning : QNetworkInterface::InterfaceFlag(0); |
|
63 flags |= (aInfo.iFeatures & KIfCanBroadcast) ? QNetworkInterface::CanBroadcast : QNetworkInterface::InterfaceFlag(0); |
|
64 flags |= (aInfo.iFeatures & KIfIsLoopback) ? QNetworkInterface::IsLoopBack : QNetworkInterface::InterfaceFlag(0); |
|
65 flags |= (aInfo.iFeatures & KIfIsPointToPoint) ? QNetworkInterface::IsPointToPoint : QNetworkInterface::InterfaceFlag(0); |
|
66 flags |= (aInfo.iFeatures & KIfCanMulticast) ? QNetworkInterface::CanMulticast : QNetworkInterface::InterfaceFlag(0); |
|
67 return flags; |
|
68 } |
|
69 |
|
70 static QList<QNetworkInterfacePrivate *> interfaceListing() |
|
71 { |
|
72 TInt err(KErrNone); |
|
73 QList<QNetworkInterfacePrivate *> interfaces; |
|
74 |
|
75 // Connect to Native socket server |
|
76 RSocketServ socketServ; |
|
77 err = socketServ.Connect(); |
|
78 if (err) |
|
79 return interfaces; |
|
80 |
|
81 // Open dummy socket for interface queries |
|
82 RSocket socket; |
|
83 err = socket.Open(socketServ, _L("udp")); |
|
84 if (err) { |
|
85 socketServ.Close(); |
|
86 return interfaces; |
|
87 } |
|
88 |
|
89 // Ask socket to start enumerating interfaces |
|
90 err = socket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl); |
|
91 if (err) { |
|
92 socket.Close(); |
|
93 socketServ.Close(); |
|
94 return interfaces; |
|
95 } |
|
96 |
|
97 int ifindex = 0; |
|
98 TPckgBuf<TSoInetInterfaceInfo> infoPckg; |
|
99 TSoInetInterfaceInfo &info = infoPckg(); |
|
100 while (socket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, infoPckg) == KErrNone) { |
|
101 // Do not include IPv6 addresses because netmask and broadcast address cannot be determined correctly |
|
102 if (info.iName != KNullDesC && info.iAddress.IsV4Mapped()) { |
|
103 TName address; |
|
104 QNetworkAddressEntry entry; |
|
105 QNetworkInterfacePrivate *iface = 0; |
|
106 |
|
107 iface = new QNetworkInterfacePrivate; |
|
108 iface->index = ifindex++; |
|
109 interfaces << iface; |
|
110 iface->name = qt_TDesC2QString(info.iName); |
|
111 iface->flags = convertFlags(info); |
|
112 |
|
113 if (/*info.iFeatures&KIfHasHardwareAddr &&*/ info.iHwAddr.Family() != KAFUnspec) { |
|
114 for (TInt i = sizeof(SSockAddr); i < sizeof(SSockAddr) + info.iHwAddr.GetUserLen(); i++) { |
|
115 address.AppendNumFixedWidth(info.iHwAddr[i], EHex, 2); |
|
116 if ((i + 1) < sizeof(SSockAddr) + info.iHwAddr.GetUserLen()) |
|
117 address.Append(_L(":")); |
|
118 } |
|
119 address.UpperCase(); |
|
120 iface->hardwareAddress = qt_TDesC2QString(address); |
|
121 } |
|
122 |
|
123 // Get the address of the interface |
|
124 info.iAddress.Output(address); |
|
125 entry.setIp(QHostAddress(qt_TDesC2QString(address))); |
|
126 |
|
127 // Get the interface netmask |
|
128 // For some reason netmask is always 0.0.0.0 |
|
129 // info.iNetMask.Output(address); |
|
130 // entry.setNetmask( QHostAddress( qt_TDesC2QString( address ) ) ); |
|
131 |
|
132 // Workaround: Let Symbian determine netmask based on IP address class |
|
133 // TODO: Works only for IPv4 - Task: 259128 Implement IPv6 support |
|
134 TInetAddr netmask; |
|
135 netmask.NetMask(info.iAddress); |
|
136 netmask.Output(address); |
|
137 entry.setNetmask(QHostAddress(qt_TDesC2QString(address))); |
|
138 |
|
139 // Get the interface broadcast address |
|
140 if (iface->flags & QNetworkInterface::CanBroadcast) { |
|
141 // For some reason broadcast address is always 0.0.0.0 |
|
142 // info.iBrdAddr.Output(address); |
|
143 // entry.setBroadcast( QHostAddress( qt_TDesC2QString( address ) ) ); |
|
144 |
|
145 // Workaround: Let Symbian determine broadcast address based on IP address |
|
146 // TODO: Works only for IPv4 - Task: 259128 Implement IPv6 support |
|
147 TInetAddr broadcast; |
|
148 broadcast.NetBroadcast(info.iAddress); |
|
149 broadcast.Output(address); |
|
150 entry.setBroadcast(QHostAddress(qt_TDesC2QString(address))); |
|
151 } |
|
152 |
|
153 // Add new entry to interface address entries |
|
154 iface->addressEntries << entry; |
|
155 |
|
156 #if defined(QNETWORKINTERFACE_DEBUG) |
|
157 printf("\n Found network interface %s, interface flags:\n\ |
|
158 IsUp = %d, IsRunning = %d, CanBroadcast = %d,\n\ |
|
159 IsLoopBack = %d, IsPointToPoint = %d, CanMulticast = %d, \n\ |
|
160 ip = %s, netmask = %s, broadcast = %s,\n\ |
|
161 hwaddress = %s", |
|
162 iface->name.toLatin1().constData(), |
|
163 iface->flags & QNetworkInterface::IsUp, iface->flags & QNetworkInterface::IsRunning, iface->flags & QNetworkInterface::CanBroadcast, |
|
164 iface->flags & QNetworkInterface::IsLoopBack, iface->flags & QNetworkInterface::IsPointToPoint, iface->flags & QNetworkInterface::CanMulticast, |
|
165 entry.ip().toString().toLatin1().constData(), entry.netmask().toString().toLatin1().constData(), entry.broadcast().toString().toLatin1().constData(), |
|
166 iface->hardwareAddress.toLatin1().constData()); |
|
167 #endif |
|
168 } |
|
169 } |
|
170 |
|
171 // we will try to use routing info to detect more precisely |
|
172 // netmask and then ::postProcess() should calculate |
|
173 // broadcast addresses |
|
174 |
|
175 // use dummy socket to start enumerating routes |
|
176 err = socket.SetOpt(KSoInetEnumRoutes, KSolInetRtCtrl); |
|
177 if (err) { |
|
178 socket.Close(); |
|
179 socketServ.Close(); |
|
180 // return what we have |
|
181 // up to this moment |
|
182 return interfaces; |
|
183 } |
|
184 |
|
185 TSoInetRouteInfo routeInfo; |
|
186 TPckg<TSoInetRouteInfo> routeInfoPkg(routeInfo); |
|
187 while (socket.GetOpt(KSoInetNextRoute, KSolInetRtCtrl, routeInfoPkg) == KErrNone) { |
|
188 TName address; |
|
189 |
|
190 // get interface address |
|
191 routeInfo.iIfAddr.Output(address); |
|
192 QHostAddress ifAddr(qt_TDesC2QString(address)); |
|
193 if (ifAddr.isNull()) |
|
194 continue; |
|
195 |
|
196 routeInfo.iDstAddr.Output(address); |
|
197 QHostAddress destination(qt_TDesC2QString(address)); |
|
198 if (destination.isNull() || destination != ifAddr) |
|
199 continue; |
|
200 |
|
201 // search interfaces |
|
202 for (int ifindex = 0; ifindex < interfaces.size(); ++ifindex) { |
|
203 QNetworkInterfacePrivate *iface = interfaces.at(ifindex); |
|
204 for (int eindex = 0; eindex < iface->addressEntries.size(); ++eindex) { |
|
205 QNetworkAddressEntry entry = iface->addressEntries.at(eindex); |
|
206 if (entry.ip() != ifAddr) { |
|
207 continue; |
|
208 } else if (entry.ip().protocol() != QAbstractSocket::IPv4Protocol) { |
|
209 // skip if not IPv4 address (e.g. IPv6) |
|
210 // as results not reliable on Symbian |
|
211 continue; |
|
212 } else { |
|
213 routeInfo.iNetMask.Output(address); |
|
214 QHostAddress netmask(qt_TDesC2QString(address)); |
|
215 entry.setNetmask(netmask); |
|
216 // NULL boradcast address for |
|
217 // ::postProcess to have effect |
|
218 entry.setBroadcast(QHostAddress()); |
|
219 iface->addressEntries.replace(eindex, entry); |
|
220 } |
|
221 } |
|
222 } |
|
223 } |
|
224 |
|
225 socket.Close(); |
|
226 socketServ.Close(); |
|
227 |
|
228 return interfaces; |
|
229 } |
|
230 |
|
231 QList<QNetworkInterfacePrivate *> QNetworkInterfaceManager::scan() |
|
232 { |
|
233 return interfaceListing(); |
|
234 } |
|
235 |
|
236 QT_END_NAMESPACE |
|
237 |
|
238 #endif // QT_NO_NETWORKINTERFACE |