sym_iap_util.h Example File
network/qftp/sym_iap_util.h
#ifndef QSYM_IAP_UTIL_H
#define QSYM_IAP_UTIL_H
#include <utf.h>
#include <es_sock.h>
#include <in_sock.h>
#include <es_enum.h>
#include <in_iface.h>
#include <commdbconnpref.h>
#include <e32cmn.h>
#include <sys/socket.h>
#include <net/if.h>
#include <QSettings>
#include <QStringList>
_LIT(KIapNameSetting, "IAP\\Name");
_LIT(KIapTableIdField, "IAP\Id");
_LIT(KIapDialogPref, "IAP\\DialogPref");
_LIT(KIapService, "IAP\\IAPService");
_LIT(KIapServiceType, "IAP\\IAPServiceType");
_LIT(KIapBearer, "IAP\\IAPBearer");
_LIT(KIapBearerType, "IAP\\IAPBearerType");
_LIT(KIapNetwork, "IAP\\IAPNetwork");
const QLatin1String qtOrganizationTag("Trolltech");
const QLatin1String qtNetworkModuleTag("QtNetwork");
const QLatin1String iapGroupTag("IAP");
const QLatin1String iapNamesArrayTag("Names");
const QLatin1String iapNameItemTag("Name");
static QTextCodec *utf16LETextCodec = 0;
void clearIapNamesSettings(QSettings &settings) {
settings.beginGroup(qtNetworkModuleTag);
settings.beginGroup(iapGroupTag);
settings.remove(iapNamesArrayTag);
settings.endGroup();
settings.endGroup();
}
void writeIapNamesSettings(QSettings &settings, const QStringList& iapNames) {
clearIapNamesSettings(settings);
settings.beginGroup(qtNetworkModuleTag);
settings.beginGroup(iapGroupTag);
settings.beginWriteArray(iapNamesArrayTag);
for (int index = 0; index < iapNames.size(); ++index) {
settings.setArrayIndex(index);
settings.setValue(iapNameItemTag, iapNames.at(index));
}
settings.endArray();
settings.endGroup();
settings.endGroup();
}
void readIapNamesSettings(QSettings &settings, QStringList& iapNames) {
settings.beginGroup(qtNetworkModuleTag);
settings.beginGroup(iapGroupTag);
int last = settings.beginReadArray(iapNamesArrayTag);
for (int index = 0; index < last; ++index) {
settings.setArrayIndex(index);
iapNames.append(settings.value(iapNameItemTag).toString());
}
settings.endArray();
settings.endGroup();
settings.endGroup();
}
static QString qt_TNameToQString(TName data) {
if(utf16LETextCodec == 0)
utf16LETextCodec = QTextCodec::codecForName("UTF-16LE");
QByteArray tmpByteArray = QByteArray::fromRawData((char*)(data.PtrZ()), data.Length() * 2);
return utf16LETextCodec->toUnicode(tmpByteArray);
}
static QString qt_InterfaceInfoL()
{
QString output;
TBuf8<512> buffer;
TBuf<128> t;
TAutoClose<RSocketServ> ss;
User::LeaveIfError(ss.iObj.Connect());
ss.PushL();
TAutoClose<RSocket> sock;
User::LeaveIfError(sock.iObj.Open(ss.iObj, _L("udp")));
sock.PushL();
User::LeaveIfError(sock.iObj.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl));
TProtocolDesc in;
User::LeaveIfError(sock.iObj.Info(in));
printf("EPOC32 IP Configuration TCPIP Version %d.%d.%d\n", in.iVersion.iMajor, in.iVersion.iMinor, in.iVersion.iBuild);
TPckgBuf<TSoInetInterfaceInfo> info, next;
TInt res=sock.iObj.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info);
if(res!=KErrNone)
User::Leave(res);
TInt count = 0;
while(res==KErrNone) {
res=sock.iObj.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, next);
if(info().iName != _L("") && info().iName != _L("loop6") && info().iName != _L("loop4")) {
printf("Interface %d\n", count++);
printf("Name \"%s\"\n", qt_TNameToQString(info().iName).toLatin1().data());
printf("NIF tag \"%s\"\n", qt_TNameToQString(info().iTag).toLatin1().data());
printf("State ");
switch (info().iState)
{
case EIfPending:
printf("pending\n");
break;
case EIfUp:
printf("up\n");
break;
case EIfBusy:
printf("busy\n");
break;
default:
printf("down\n");
break;
}
printf("Mtu %d\n", info().iMtu);
printf("Speed Metric %d\n", info().iSpeedMetric);
printf("Features:");
info().iFeatures & KIfIsLoopback ? printf(" loopback") : printf("");
info().iFeatures & KIfIsDialup ? printf(" dialup") : printf("");
info().iFeatures & KIfIsPointToPoint ? printf(" pointtopoint") : printf("");
info().iFeatures & KIfCanBroadcast ? printf(" canbroadcast") : printf("");
info().iFeatures & KIfCanMulticast ? printf(" canmulticast") : printf("");
info().iFeatures & KIfCanSetMTU ? printf(" cansetmtu") : printf("");
info().iFeatures & KIfHasHardwareAddr ? printf(" hardwareaddr") : printf("");
info().iFeatures & KIfCanSetHardwareAddr ? printf(" cansethardwareaddr") : printf("");
printf("\n");
TName address;
info().iAddress.Output(address);
printf("Addr: %s\n", qt_TNameToQString(address).toLatin1().data());
if(info().iAddress.IsLinkLocal()) {
printf(" -link local\n");
} else if(info().iAddress.IsSiteLocal()) {
printf(" -site local\n");
} else {
printf(" -global\n");
}
info().iNetMask.Output(address);
printf("Netmask %s\n", qt_TNameToQString(address).toLatin1().data());
info().iBrdAddr.Output(address);
printf("Broadcast address %s\n", qt_TNameToQString(address).toLatin1().data());
info().iDefGate.Output(address);
printf("Gatew: %s\n", qt_TNameToQString(address).toLatin1().data());
info().iNameSer1.Output(address);
printf("DNS 1: %s\n", qt_TNameToQString(address).toLatin1().data());
info().iNameSer2.Output(address);
printf("DNS 2: %s\n", qt_TNameToQString(address).toLatin1().data());
if (info().iHwAddr.Family() != KAFUnspec) {
printf("Hardware address ");
TUint j;
for(j = sizeof(SSockAddr) ; j < sizeof(SSockAddr) + 6 ; ++j) {
if(j < (TUint)info().iHwAddr.Length()) {
printf("%02X", info().iHwAddr[j]);
} else {
printf("??");
}
if(j < sizeof(SSockAddr) + 5)
printf("-");
else
printf("\n");
}
}
}
if(res == KErrNone) {
info = next;
printf("\n");
} else {
printf("\n");
}
}
sock.Pop();
ss.Pop();
return output;
}
static QString qt_RouteInfoL() {
QString output;
TAutoClose<RSocketServ> ss;
User::LeaveIfError(ss.iObj.Connect());
ss.PushL();
TAutoClose<RSocket> sock;
User::LeaveIfError(sock.iObj.Open(ss.iObj, _L("udp")));
sock.PushL();
TSoInetRouteInfo routeInfo;
TPckg<TSoInetRouteInfo> routeInfoPkg(routeInfo);
TName destAddr;
TName netMask;
TName gateway;
TName ifAddr;
User::LeaveIfError(sock.iObj.SetOpt(KSoInetEnumRoutes, KSolInetRtCtrl));
for(TInt i = 0; sock.iObj.GetOpt(KSoInetNextRoute, KSolInetRtCtrl, routeInfoPkg) == KErrNone ; i++)
{
routeInfo.iDstAddr.Output(destAddr);
routeInfo.iNetMask.Output(netMask);
routeInfo.iGateway.Output(gateway);
routeInfo.iIfAddr.Output(ifAddr);
printf("Route Info #[%i]\n", i);
printf("DstAddr %s\n", qt_TNameToQString(destAddr).toLatin1().data());
printf("NetMask %s\n", qt_TNameToQString(netMask).toLatin1().data());
printf("Gateway %s\n", qt_TNameToQString(gateway).toLatin1().data());
printf("IfAddr %s\n", qt_TNameToQString(ifAddr).toLatin1().data());
printf("\n");
}
sock.Pop();
ss.Pop();
return output;
}
QString qt_TDesC2QStringL(const TDesC& aDescriptor)
{
#ifdef QT_NO_UNICODE
return QString::fromLocal8Bit(aDescriptor.Ptr(), aDescriptor.Length());
#else
return QString((const QChar *)aDescriptor.Ptr(), aDescriptor.Length());
#endif
}
static bool qt_SetDefaultIapName(const QString &iapName, int &error) {
struct ifreq ifReq;
memset(&ifReq, 0, sizeof(struct ifreq));
strcpy(ifReq.ifr_name, iapName.toUtf8().data());
if(setdefaultif(&ifReq) == 0) {
error = 0;
return true;
} else {
error = errno;
return false;
}
}
static bool qt_SetDefaultSnapId(const int snapId, int &error) {
struct ifreq ifReq;
memset(&ifReq, 0, sizeof(struct ifreq));
ifReq.ifr_ifru.snap_id = snapId;
if(setdefaultif(&ifReq) == 0) {
error = 0;
return true;
} else {
error = errno;
return false;
}
}
static void qt_SaveIapName(QSettings& settings, QStringList& iapNames, QString& iapNameValue) {
if(iapNames.contains(iapNameValue) && iapNames.first() == iapNameValue) {
} else {
if(iapNameValue != QString("Easy WLAN")) {
iapNames.removeAll(iapNameValue);
iapNames.prepend(iapNameValue);
writeIapNamesSettings(settings, iapNames);
} else {
}
}
}
static QString qt_OfferIapDialog() {
TBuf8<256> iapName;
RSocketServ socketServ;
CleanupClosePushL(socketServ);
RConnection connection;
CleanupClosePushL(connection);
socketServ.Connect();
TCommDbConnPref prefs;
prefs.SetDialogPreference(ECommDbDialogPrefPrompt);
connection.Open(socketServ);
connection.Start(prefs);
connection.GetDesSetting(TPtrC(KIapNameSetting), iapName);
iapName.ZeroTerminate();
QString strIapName((char*)iapName.Ptr());
int error = 0;
if(!strIapName.isEmpty()) {
if(!qt_SetDefaultIapName(strIapName, error)) {
strIapName = QString("");
}
}
CleanupStack::PopAndDestroy(&connection);
CleanupStack::PopAndDestroy(&socketServ);
return strIapName;
}
static QString qt_CheckForActiveConnection() {
TUint count;
RSocketServ serv;
CleanupClosePushL(serv);
RConnection conn;
CleanupClosePushL(conn);
serv.Connect();
conn.Open(serv);
TConnectionInfoBuf connInfo;
TBuf8<256> iapName;
TBuf8<256> iapServiceType;
QString strIapName;
if (conn.EnumerateConnections(count) == KErrNone) {
if(count > 0) {
for (TUint i = 1; i <= count; i++) {
if (conn.GetConnectionInfo(i, connInfo) == KErrNone) {
RConnection tempConn;
CleanupClosePushL(tempConn);
tempConn.Open(serv);
if (tempConn.Attach(connInfo, RConnection::EAttachTypeNormal) == KErrNone) {
tempConn.GetDesSetting(TPtrC(KIapNameSetting), iapName);
tempConn.GetDesSetting(TPtrC(KIapServiceType), iapServiceType);
iapName.ZeroTerminate();
iapServiceType.ZeroTerminate();
!= KErrNotFound) {
strIapName = QString((char*)iapName.Ptr());
int error = 0;
if(!qt_SetDefaultIapName(strIapName, error)) {
strIapName = QString("");
}
CleanupStack::PopAndDestroy(&tempConn);
break;
}
}
}
}
}
CleanupStack::PopAndDestroy(&conn);
CleanupStack::PopAndDestroy(&serv);
return strIapName;
}
static QString qt_CheckSettingsForConnection(QStringList& iapNames) {
QString strIapName;
for(int index = 0; index < iapNames.size(); ++index) {
strIapName = iapNames.at(index);
int error = 0;
if(!qt_SetDefaultIapName(strIapName, error)) {
strIapName = QString("");
} else {
return strIapName;
}
}
return strIapName;
}
static void qt_SetDefaultIapL()
{
QSettings settings(QSettings::UserScope, qtOrganizationTag);
QStringList iapNames;
readIapNamesSettings(settings, iapNames);
QString iapNameValue;
iapNameValue = qt_CheckForActiveConnection();
if(!iapNameValue.isEmpty()) {
qt_SaveIapName(settings, iapNames, iapNameValue);
return;
}
iapNameValue = qt_CheckSettingsForConnection(iapNames);
if(!iapNameValue.isEmpty()) {
qt_SaveIapName(settings, iapNames, iapNameValue);
return;
}
iapNameValue = qt_OfferIapDialog();
qt_SaveIapName(settings, iapNames, iapNameValue);
return;
}
static int qt_SetDefaultIap()
{
#ifndef __WINS__
TRAPD(err1, qt_SetDefaultIapL());
return err1;
#else
return 0;
#endif
}
#endif