1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.6: sym_iap_util.h Example File (network/ftp/sym_iap_util.h)</title>
7
<link href="classic.css" rel="stylesheet" type="text/css" />
10
<table border="0" cellpadding="0" cellspacing="0" width="100%">
12
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
13
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="functions.html"><font color="#004faf">All Functions</font></a> · <a href="overviews.html"><font color="#004faf">Overviews</font></a></td><td class="searchBar" align="right" valign="center"><form action="http://www.google.com/cse" id="cse-search-box"><div><input type="hidden" name="cx" value="000136343326384750312:dhbxnqlakyu" /><input type="hidden" name="ie" value="UTF-8" /><input type="text" name="q" size="31" /><input type="submit" name="sa" value="Search" /></div></form></td>
14
</tr></table><h1 class="title">sym_iap_util.h Example File<br /><span class="small-subtitle">network/ftp/sym_iap_util.h</span>
16
<pre><span class="comment"> /****************************************************************************
18
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
19
** All rights reserved.
20
** Contact: Nokia Corporation (qt-info@nokia.com)
22
** This file is part of the examples of the Qt Toolkit.
24
** $QT_BEGIN_LICENSE:LGPL$
25
** No Commercial Usage
26
** This file contains pre-release code and may not be distributed.
27
** You may use this file in accordance with the terms and conditions
28
** contained in the Technology Preview License Agreement accompanying
31
** GNU Lesser General Public License Usage
32
** Alternatively, this file may be used under the terms of the GNU Lesser
33
** General Public License version 2.1 as published by the Free Software
34
** Foundation and appearing in the file LICENSE.LGPL included in the
35
** packaging of this file. Please review the following information to
36
** ensure the GNU Lesser General Public License version 2.1 requirements
37
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
39
** In addition, as a special exception, Nokia gives you certain additional
40
** rights. These rights are described in the Nokia Qt LGPL Exception
41
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
43
** If you have questions regarding the use of this file, please contact
44
** Nokia at qt-info@nokia.com.
55
****************************************************************************/</span>
56
#ifndef QSYM_IAP_UTIL_H
57
#define QSYM_IAP_UTIL_H
59
<span class="comment"> // Symbian</span>
60
#include <utf.h>
61
#include <es_sock.h>
62
#include <in_sock.h>
63
#include <es_enum.h>
64
#include <in_iface.h>
65
#include <commdbconnpref.h>
66
#include <e32cmn.h>
68
<span class="comment"> // OpenC</span>
69
#include <sys/socket.h>
70
#include <net/if.h>
72
<span class="comment"> //Qt</span>
73
#include <QSettings>
74
#include <QStringList>
75
<span class="comment"> //#include <QTextCodec></span>
77
_LIT(KIapNameSetting, "IAP\\Name"); <span class="comment">// text - mandatory</span>
78
_LIT(KIapDialogPref, "IAP\\DialogPref"); <span class="comment">// TUnit32 - optional</span>
79
_LIT(KIapService, "IAP\\IAPService"); <span class="comment">// TUnit32 - mandatory</span>
80
_LIT(KIapServiceType, "IAP\\IAPServiceType"); <span class="comment">// text - mandatory</span>
81
_LIT(KIapBearer, "IAP\\IAPBearer"); <span class="comment">// TUint32 - optional</span>
82
_LIT(KIapBearerType, "IAP\\IAPBearerType"); <span class="comment">// text - optional</span>
83
_LIT(KIapNetwork, "IAP\\IAPNetwork"); <span class="comment">// TUint32 - optional</span>
85
const QLatin1String qtOrganizationTag("Trolltech");
86
const QLatin1String qtNetworkModuleTag("QtNetwork");
87
const QLatin1String iapGroupTag("IAP");
88
const QLatin1String iapNamesArrayTag("Names");
89
const QLatin1String iapNameItemTag("Name");
91
static QTextCodec *utf16LETextCodec = 0;
93
void clearIapNamesSettings(QSettings &settings) {
94
settings.beginGroup(qtNetworkModuleTag);
95
settings.beginGroup(iapGroupTag);
96
settings.remove(iapNamesArrayTag);
101
void writeIapNamesSettings(QSettings &settings, const QStringList& iapNames) {
102
clearIapNamesSettings(settings);
103
settings.beginGroup(qtNetworkModuleTag);
104
settings.beginGroup(iapGroupTag);
105
settings.beginWriteArray(iapNamesArrayTag);
106
for (int index = 0; index < iapNames.size(); ++index) {
107
settings.setArrayIndex(index);
108
settings.setValue(iapNameItemTag, iapNames.at(index));
115
void readIapNamesSettings(QSettings &settings, QStringList& iapNames) {
116
settings.beginGroup(qtNetworkModuleTag);
117
settings.beginGroup(iapGroupTag);
118
int last = settings.beginReadArray(iapNamesArrayTag);
119
for (int index = 0; index < last; ++index) {
120
settings.setArrayIndex(index);
121
iapNames.append(settings.value(iapNameItemTag).toString());
128
static QString qt_TNameToQString(TName data) {
129
if(utf16LETextCodec == 0)
130
utf16LETextCodec = QTextCodec::codecForName("UTF-16LE");
132
QByteArray tmpByteArray = QByteArray::fromRawData((char*)(data.PtrZ()), data.Length() * 2);
133
return utf16LETextCodec->toUnicode(tmpByteArray);
136
static QString qt_InterfaceInfoL()
140
TBuf8<512> buffer;
142
TAutoClose<RSocketServ> ss;
143
User::LeaveIfError(ss.iObj.Connect());
146
TAutoClose<RSocket> sock;
147
User::LeaveIfError(sock.iObj.Open(ss.iObj, _L("udp")));
150
User::LeaveIfError(sock.iObj.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl));
153
User::LeaveIfError(sock.iObj.Info(in));
154
printf("EPOC32 IP Configuration TCPIP Version %d.%d.%d\n", in.iVersion.iMajor, in.iVersion.iMinor, in.iVersion.iBuild);
156
TPckgBuf<TSoInetInterfaceInfo> info, next;
158
TInt res=sock.iObj.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info);
162
while(res==KErrNone) {
163
res=sock.iObj.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, next);
165
if(info().iName != _L("") && info().iName != _L("loop6") && info().iName != _L("loop4")) {
166
printf("Interface %d\n", count++);
168
printf("Name \"%s\"\n", qt_TNameToQString(info().iName).toLatin1().data());
169
printf("NIF tag \"%s\"\n", qt_TNameToQString(info().iTag).toLatin1().data());
171
printf("State ");
172
switch (info().iState)
175
printf("pending\n");
178
printf("up\n");
181
printf("busy\n");
184
printf("down\n");
188
printf("Mtu %d\n", info().iMtu);
189
printf("Speed Metric %d\n", info().iSpeedMetric);
191
printf("Features:");
192
info().iFeatures & KIfIsLoopback ? printf(" loopback") : printf("");
193
info().iFeatures & KIfIsDialup ? printf(" dialup") : printf("");
194
info().iFeatures & KIfIsPointToPoint ? printf(" pointtopoint") : printf("");
195
info().iFeatures & KIfCanBroadcast ? printf(" canbroadcast") : printf("");
196
info().iFeatures & KIfCanMulticast ? printf(" canmulticast") : printf("");
197
info().iFeatures & KIfCanSetMTU ? printf(" cansetmtu") : printf("");
198
info().iFeatures & KIfHasHardwareAddr ? printf(" hardwareaddr") : printf("");
199
info().iFeatures & KIfCanSetHardwareAddr ? printf(" cansethardwareaddr") : printf("");
200
printf("\n");
203
info().iAddress.Output(address);
204
printf("Addr: %s\n", qt_TNameToQString(address).toLatin1().data());
206
if(info().iAddress.IsLinkLocal()) {
207
printf(" -link local\n");
208
} else if(info().iAddress.IsSiteLocal()) {
209
printf(" -site local\n");
211
printf(" -global\n");
214
info().iNetMask.Output(address);
215
printf("Netmask %s\n", qt_TNameToQString(address).toLatin1().data());
217
info().iBrdAddr.Output(address);
218
printf("Broadcast address %s\n", qt_TNameToQString(address).toLatin1().data());
220
info().iDefGate.Output(address);
221
printf("Gatew: %s\n", qt_TNameToQString(address).toLatin1().data());
223
info().iNameSer1.Output(address);
224
printf("DNS 1: %s\n", qt_TNameToQString(address).toLatin1().data());
226
info().iNameSer2.Output(address);
227
printf("DNS 2: %s\n", qt_TNameToQString(address).toLatin1().data());
229
if (info().iHwAddr.Family() != KAFUnspec) {
230
printf("Hardware address ");
232
for(j = sizeof(SSockAddr) ; j < sizeof(SSockAddr) + 6 ; ++j) {
233
if(j < (TUint)info().iHwAddr.Length()) {
234
printf("%02X", info().iHwAddr[j]);
236
printf("??");
238
if(j < sizeof(SSockAddr) + 5)
239
printf("-");
241
printf("\n");
245
if(res == KErrNone) {
247
printf("\n");
249
printf("\n");
259
static QString qt_RouteInfoL() {
261
TAutoClose<RSocketServ> ss;
262
User::LeaveIfError(ss.iObj.Connect());
265
TAutoClose<RSocket> sock;
266
User::LeaveIfError(sock.iObj.Open(ss.iObj, _L("udp")));
269
TSoInetRouteInfo routeInfo;
270
TPckg<TSoInetRouteInfo> routeInfoPkg(routeInfo);
277
<span class="comment">// Begins enumeration of routes by setting this option</span>
278
User::LeaveIfError(sock.iObj.SetOpt(KSoInetEnumRoutes, KSolInetRtCtrl));
280
<span class="comment">// The TSoInetRouteInfo contains information for a new route each time GetOpt returns KErrNone</span>
281
for(TInt i = 0; sock.iObj.GetOpt(KSoInetNextRoute, KSolInetRtCtrl, routeInfoPkg) == KErrNone ; i++)
283
<span class="comment">// Extract the destination and netmask</span>
284
routeInfo.iDstAddr.Output(destAddr);
285
routeInfo.iNetMask.Output(netMask);
286
routeInfo.iGateway.Output(gateway);
287
routeInfo.iIfAddr.Output(ifAddr);
288
<span class="comment"> /*
289
if(destAddr.Length() <= 2)
292
if(netMask.Find(_L("255.255.255.255")) != KErrNotFound
293
|| netMask.Find(_L("0.0.0.0")) != KErrNotFound
294
|| netMask.Find(_L("ffff:ffff:ffff:ffff")) != KErrNotFound)
297
printf("Route Info #[%i]\n", i);
298
printf("DstAddr %s\n", qt_TNameToQString(destAddr).toLatin1().data());
299
printf("NetMask %s\n", qt_TNameToQString(netMask).toLatin1().data());
300
printf("Gateway %s\n", qt_TNameToQString(gateway).toLatin1().data());
301
printf("IfAddr %s\n", qt_TNameToQString(ifAddr).toLatin1().data());
302
printf("\n");
311
QString qt_TDesC2QStringL(const TDesC& aDescriptor)
314
return QString::fromLocal8Bit(aDescriptor.Ptr(), aDescriptor.Length());
316
return QString::fromUtf16(aDescriptor.Ptr(), aDescriptor.Length());
320
static bool qt_SetDefaultIapName(const QString &iapName, int &error) {
322
<span class="comment">// clear structure</span>
323
memset(&ifReq, 0, sizeof(struct ifreq));
324
<span class="comment">// set IAP name value</span>
325
<span class="comment">// make sure it is in UTF8</span>
326
strcpy(ifReq.ifr_name, iapName.toUtf8().data());
328
if(setdefaultif(&ifReq) == 0) {
329
<span class="comment">// OK</span>
338
static bool qt_SetDefaultSnapId(const int snapId, int &error) {
340
<span class="comment">// clear structure</span>
341
memset(&ifReq, 0, sizeof(struct ifreq));
342
<span class="comment">// set SNAP ID value</span>
343
ifReq.ifr_ifru.snap_id = snapId;
345
if(setdefaultif(&ifReq) == 0) {
346
<span class="comment">// OK</span>
356
static void qt_SaveIapName(QSettings& settings, QStringList& iapNames, QString& iapNameValue) {
357
if(iapNames.contains(iapNameValue) && iapNames.first() == iapNameValue) {
358
<span class="comment">// no need to update</span>
360
if(iapNameValue != QString("Easy WLAN")) {
361
<span class="comment">// new selection alway on top</span>
362
iapNames.removeAll(iapNameValue);
363
iapNames.prepend(iapNameValue);
364
writeIapNamesSettings(settings, iapNames);
366
<span class="comment">// Unbeliveable ... if IAP dodn't exist before</span>
367
<span class="comment">// no matter what you choose from IAP selection list</span>
368
<span class="comment">// you will get "Easy WLAN" as IAP name value</span>
370
<span class="comment">// somehow commsdb is not in sync</span>
375
static QString qt_OfferIapDialog() {
376
TBuf8<256> iapName;
378
RSocketServ socketServ;
379
CleanupClosePushL(socketServ);
381
RConnection connection;
382
CleanupClosePushL(connection);
384
socketServ.Connect();
385
connection.Open(socketServ);
388
connection.GetDesSetting(TPtrC(KIapNameSetting), iapName);
390
<span class="comment">//connection.Stop();</span>
392
iapName.ZeroTerminate();
393
QString strIapName((char*)iapName.Ptr());
396
if(!qt_SetDefaultIapName(strIapName, error)) {
397
<span class="comment">//printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);</span>
398
strIapName = QString("");
401
CleanupStack::PopAndDestroy(&connection);
402
CleanupStack::PopAndDestroy(&socketServ);
407
static QString qt_CheckForActiveConnection() {
411
CleanupClosePushL(serv);
414
CleanupClosePushL(conn);
419
TConnectionInfoBuf connInfo;
421
TBuf8<256> iapName;
422
TBuf8<256> iapServiceType;
426
if (conn.EnumerateConnections(count) == KErrNone) {
428
for (TUint i = 1; i <= count; i++) {
429
if (conn.GetConnectionInfo(i, connInfo) == KErrNone) {
430
RConnection tempConn;
431
CleanupClosePushL(tempConn);
433
if (tempConn.Attach(connInfo, RConnection::EAttachTypeNormal) == KErrNone) {
434
tempConn.GetDesSetting(TPtrC(KIapNameSetting), iapName);
435
tempConn.GetDesSetting(TPtrC(KIapServiceType), iapServiceType);
436
<span class="comment">//tempConn.Stop();</span>
437
iapName.ZeroTerminate();
438
iapServiceType.ZeroTerminate();
440
<span class="comment"> // if(iapServiceType.Find(_L8("LANService")) </span>!= KErrNotFound) {
441
<span class="comment"> // activeLanConnectionFound = ETrue;</span>
442
<span class="comment"> // break;</span>
443
<span class="comment"> // }</span>
444
strIapName = QString((char*)iapName.Ptr());
446
if(!qt_SetDefaultIapName(strIapName, error)) {
447
<span class="comment">//printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);</span>
448
strIapName = QString("");
451
CleanupStack::PopAndDestroy(&tempConn);
459
<span class="comment">//conn.Stop();</span>
461
CleanupStack::PopAndDestroy(&conn);
462
CleanupStack::PopAndDestroy(&serv);
467
static QString qt_CheckSettingsForConnection(QStringList& iapNames) {
469
for(int index = 0; index < iapNames.size(); ++index) {
470
strIapName = iapNames.at(index);
472
if(!qt_SetDefaultIapName(strIapName, error)) {
473
<span class="comment">//printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);</span>
474
strIapName = QString("");
482
static void qt_SetDefaultIapL()
484
<span class="comment">// settings @ /c/data/.config/Trolltech.com</span>
485
QSettings settings(QSettings::UserScope, qtOrganizationTag);
486
<span class="comment">// populate iap name list</span>
487
QStringList iapNames;
488
readIapNamesSettings(settings, iapNames);
490
QString iapNameValue;
492
iapNameValue = qt_CheckForActiveConnection();
494
if(!iapNameValue.isEmpty()) {
495
qt_SaveIapName(settings, iapNames, iapNameValue);
499
iapNameValue = qt_CheckSettingsForConnection(iapNames);
501
if(!iapNameValue.isEmpty()) {
502
qt_SaveIapName(settings, iapNames, iapNameValue);
506
<span class="comment">/*
507
* no active LAN connections yet
509
* offer IAP dialog to user
511
iapNameValue = qt_OfferIapDialog();
512
qt_SaveIapName(settings, iapNames, iapNameValue);
517
static int qt_SetDefaultIap()
519
TRAPD(err1, qt_SetDefaultIapL());
520
<span class="comment"> // TRAPD(err2, qt_InterfaceInfoL());</span>
521
<span class="comment"> // TRAPD(err3, qt_RouteInfoL());</span>
525
#endif <span class="comment">// QSYM_IAP_UTIL_H</span></pre>
526
<p /><address><hr /><div align="center">
527
<table width="100%" cellspacing="0" border="0"><tr class="address">
528
<td width="40%" align="left">Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies)</td>
529
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
530
<td width="40%" align="right"><div align="right">Qt 4.6.0</div></td>
531
<script type="text/javascript" src="http://www.google.com/jsapi"></script><script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script><script type="text/javascript" src="http://www.google.com/coop/cse/t13n?form=cse-search-box&t13n_langs=en"></script><script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script></tr></table></div></address></body>