QString connection::Broadcast() { // Om ip adressen te ontdekken QNetworkAddressEntry inter; // sockets aanmaken en verbinden met enerzijds broadcast en anderzijds een luister poort QUdpSocket udpSocketSend; QUdpSocket udpSocketGet; udpSocketSend.connectToHost(inter.broadcast(), 40000); // udpSocketGet->bind(inter->ip(),667); // udpSocketGet->bind(QHostAddress::Any,667) if(udpSocketGet.bind(udpgetport,QUdpSocket::ShareAddress)) Label->setText(Label->text() + "[INFO] Could properly bind udpSocketget to " + QString::number(udpgetport) + "\n"); else Label->setText(Label->text() + "[INFO] Couldn't properly bind udpSocketget to " + QString::number(udpgetport) + "\n"); // Pakket verzenden QByteArray send_datagram = "DISCOVER-STRATEGO-SERVER"; // Optimalisatie voor in de loop QByteArray receive_datagram; quint16 serverPort; forever{ udpSocketSend.writeDatagram(send_datagram, QHostAddress::Broadcast, 40000); if(udpSocketGet.waitForReadyRead(3000)){ receive_datagram.resize(udpSocketGet.pendingDatagramSize()); udpSocketGet.readDatagram(receive_datagram.data(),receive_datagram.size(),&server,&serverPort); if(QString::fromUtf8(receive_datagram.data()) == "DISCOVERED-STRATEGO-SERVER") { receive_datagram.resize(udpSocketGet.pendingDatagramSize()); udpSocketGet.readDatagram(receive_datagram.data(),receive_datagram.size(),&server,&serverPort); Label->setText(Label->text() +"[INFO] PLAYER DATA: "+ receive_datagram.data() +"\n"); speler = receive_datagram.toInt(); if(speler==1) { attacker=true; } else { attacker=false; } receive_datagram.resize(udpSocketGet.pendingDatagramSize()); udpSocketGet.readDatagram(receive_datagram.data(),receive_datagram.size(),&server,&serverPort); Label->setText(Label->text() + "[INFO] GAME DATA: "+receive_datagram.data()+"\n"); spel = receive_datagram.toInt(); Label->setText(Label->text() + " SPEL:" + QString::number(spel) + "\n"); Label->setText(Label->text() + "[INFO] Found STRATEGO-SERVER on " + server.toString().toUtf8().constData() + "\n"); return server.toString(); } } else { Label->setText(Label->text() + "[INFO] UDP Discover TimeOut!\n"); static int timeout=0; timeout++; if(timeout==5) { Label->setText(Label->text() + "[ERROR] Server is not online. Please try again later!"); return ""; } } } return ""; }
static bool contains(QNetworkAddressEntry host, QHostAddress addr) { #if !defined(QT_NO_IPV6) if (addr.protocol() == QAbstractSocket::IPv6Protocol && addr.isInSubnet(kLinkLocal6) && host.ip().scopeId() != addr.scopeId()) { return false; } #endif return addr.isInSubnet(host.ip(), host.prefixLength()); }
static QList<QNetworkInterfacePrivate *> interfaceListingWin2k() { QList<QNetworkInterfacePrivate *> interfaces; IP_ADAPTER_INFO staticBuf[2]; // 2 is arbitrary PIP_ADAPTER_INFO pAdapter = staticBuf; ULONG bufSize = sizeof staticBuf; DWORD retval = ptrGetAdaptersInfo(pAdapter, &bufSize); if (retval == ERROR_BUFFER_OVERFLOW) { // need more memory pAdapter = (IP_ADAPTER_INFO *)malloc(bufSize); if (!pAdapter) return interfaces; // try again if (ptrGetAdaptersInfo(pAdapter, &bufSize) != ERROR_SUCCESS) { free(pAdapter); return interfaces; } } else if (retval != ERROR_SUCCESS) { // error return interfaces; } // iterate over the list and add the entries to our listing for (PIP_ADAPTER_INFO ptr = pAdapter; ptr; ptr = ptr->Next) { QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate; interfaces << iface; iface->index = ptr->Index; iface->flags = QNetworkInterface::IsUp | QNetworkInterface::IsRunning; if (ptr->Type == MIB_IF_TYPE_PPP) iface->flags |= QNetworkInterface::IsPointToPoint; else iface->flags |= QNetworkInterface::CanBroadcast; iface->name = QString::fromLocal8Bit(ptr->AdapterName); iface->hardwareAddress = QNetworkInterfacePrivate::makeHwAddress(ptr->AddressLength, ptr->Address); for (PIP_ADDR_STRING addr = &ptr->IpAddressList; addr; addr = addr->Next) { QNetworkAddressEntry entry; entry.setIp(QHostAddress(QLatin1String(addr->IpAddress.String))); entry.setNetmask(QHostAddress(QLatin1String(addr->IpMask.String))); // broadcast address is set on postProcess() iface->addressEntries << entry; } } if (pAdapter != staticBuf) free(pAdapter); return interfaces; }
static QList<QNetworkInterfacePrivate *> interfaceListing() { QList<QNetworkInterfacePrivate *> interfaces; int socket; if ((socket = qt_safe_socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1) return interfaces; // error ifaddrs *interfaceListing; if (getifaddrs(&interfaceListing) == -1) { // error ::close(socket); return interfaces; } interfaces = createInterfaces(interfaceListing); for (ifaddrs *ptr = interfaceListing; ptr; ptr = ptr->ifa_next) { // Get the interface index int ifindex = if_nametoindex(ptr->ifa_name); QNetworkInterfacePrivate *iface = 0; QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin(); for ( ; if_it != interfaces.end(); ++if_it) if ((*if_it)->index == ifindex) { // found this interface already iface = *if_it; break; } if (!iface) { // skip all non-IP interfaces continue; } QNetworkAddressEntry entry; entry.setIp(addressFromSockaddr(ptr->ifa_addr)); if (entry.ip().isNull()) // could not parse the address continue; entry.setNetmask(addressFromSockaddr(ptr->ifa_netmask)); if (iface->flags & QNetworkInterface::CanBroadcast) entry.setBroadcast(addressFromSockaddr(ptr->ifa_broadaddr)); iface->addressEntries << entry; } freeifaddrs(interfaceListing); ::close(socket); return interfaces; }
static QList<QNetworkInterfacePrivate *> interfaceListing() { QList<QNetworkInterfacePrivate *> interfaces; int socket; if ((socket = qt_safe_socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1) return interfaces; // error QSet<QByteArray> names = interfaceNames(socket); QSet<QByteArray>::ConstIterator it = names.constBegin(); for ( ; it != names.constEnd(); ++it) { ifreq req; memset(&req, 0, sizeof(ifreq)); memcpy(req.ifr_name, *it, qMin<int>(it->length() + 1, sizeof(req.ifr_name) - 1)); QNetworkInterfacePrivate *iface = findInterface(socket, interfaces, req); // Get the interface broadcast address QNetworkAddressEntry entry; if (iface->flags & QNetworkInterface::CanBroadcast) { if (qt_safe_ioctl(socket, SIOCGIFBRDADDR, &req) >= 0) { sockaddr *sa = &req.ifr_addr; if (sa->sa_family == AF_INET) entry.setBroadcast(addressFromSockaddr(sa)); } } // Get the interface netmask if (qt_safe_ioctl(socket, SIOCGIFNETMASK, &req) >= 0) { sockaddr *sa = &req.ifr_addr; entry.setNetmask(addressFromSockaddr(sa)); } // Get the address of the interface if (qt_safe_ioctl(socket, SIOCGIFADDR, &req) >= 0) { sockaddr *sa = &req.ifr_addr; entry.setIp(addressFromSockaddr(sa)); } iface->addressEntries << entry; } ::close(socket); return interfaces; }
static QList<QNetworkInterfacePrivate *> interfaceListing() { QList<QNetworkInterfacePrivate *> interfaces; ifaddrs *interfaceListing; if (getifaddrs(&interfaceListing) == -1) { // error return interfaces; } interfaces = createInterfaces(interfaceListing); for (ifaddrs *ptr = interfaceListing; ptr; ptr = ptr->ifa_next) { // Get the interface index QString name = QString::fromLatin1(ptr->ifa_name); QNetworkInterfacePrivate *iface = 0; QList<QNetworkInterfacePrivate *>::Iterator if_it = interfaces.begin(); for ( ; if_it != interfaces.end(); ++if_it) if ((*if_it)->name == name) { // found this interface already iface = *if_it; break; } if (!iface) { // skip all non-IP interfaces continue; } QNetworkAddressEntry entry; entry.setIp(addressFromSockaddr(ptr->ifa_addr, iface->index, iface->name)); if (entry.ip().isNull()) // could not parse the address continue; entry.setNetmask(addressFromSockaddr(ptr->ifa_netmask, iface->index, iface->name)); if (iface->flags & QNetworkInterface::CanBroadcast) entry.setBroadcast(addressFromSockaddr(ptr->ifa_broadaddr, iface->index, iface->name)); iface->addressEntries << entry; } freeifaddrs(interfaceListing); return interfaces; }
void IpV6AdvancedWidget::setAdditionalAddresses(const QList<Solid::Control::IPv6Address> &list) { d->model.removeRows(0, d->model.rowCount()); foreach (const Solid::Control::IPv6Address &addr, list) { QList<QStandardItem *> item; QNetworkAddressEntry entry; // we need to set up IP before prefix/netmask manipulation entry.setIp(QHostAddress(addr.address())); item << new QStandardItem(entry.ip().toString()) << new QStandardItem(QString::number(addr.netMask(),10)); QString gateway; if (!QHostAddress(addr.gateway()).isNull()) { gateway = QHostAddress(addr.gateway()).toString(); } item << new QStandardItem(gateway); d->model.appendRow(item); }
/** * @brief Sets the IPAddress of Active network connection. * @param verbose Self expanatory. * @return Returns true upon success. * It first checks for already used interface, if it has, then use it and ignore all others. * Then tries to get preferred active network interface from the network interface list. * Upon success returns true, else false. */ bool wavrNetwork::getIPAddress(bool verbose) { // If an interface is already being used, get it. Ignore all others networkInterface = QNetworkInterface::interfaceFromName(interfaceName); if(networkInterface.isValid()) { QNetworkAddressEntry addressEntry; if(isInterfaceUp(&networkInterface) && getIPAddress(&networkInterface, &addressEntry)) { ipAddress = addressEntry.ip().toString(); subnetMask = addressEntry.netmask().toString(); return true; } ipAddress = QString::null; subnetMask = QString::null; return false; } // Currently, not using preferred connection, since using preferred connection is not // working properly. // Get the preferred interface name from settings if checking for the first time //if(szInterfaceName.isNull()) // szInterfaceName = pSettings->value(IDS_CONNECTION, IDS_CONNECTION_VAL).toString(); //bool usePreferred = (szInterfaceName.compare(IDS_CONNECTION_VAL, Qt::CaseInsensitive) != 0); bool usePreferred = false; wavrTrace::write("Checking for active network interface...", verbose); // get a list of all network interfaces available in the system QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces(); bool activeFound = false; // return the preferred interface if it is active for(int index = 0; index < allInterfaces.count(); index++) { // Skip to the next interface if it is not the preferred one // Checked only if searching for the preferred adapter if(usePreferred && interfaceName.compare(allInterfaces[index].name()) != 0) continue; if(isInterfaceUp(&allInterfaces[index])) { activeFound = true; wavrTrace::write("Active network interface found: " + allInterfaces[index].humanReadableName(), verbose); QNetworkAddressEntry addressEntry; if(getIPAddress(&allInterfaces[index], &addressEntry)) { ipAddress = addressEntry.ip().toString(); subnetMask = addressEntry.netmask().toString(); networkInterface = allInterfaces[index]; interfaceName = allInterfaces[index].name(); return true; } } } wavrTrace::write(QString("Warning: ") + (activeFound ? "No IP address found" : "No active network interface found"), verbose); ipAddress = QString::null; subnetMask = QString::null; return false; }
// ---------------------------------------------------------------------------- // getHostAddress (static) // ---------------------------------------------------------------------------- QHostAddress NetworkTools::getHostAddress(const QNetworkConfiguration &in_network_config) { if (in_network_config.isValid() == false) return QHostAddress(); qDebug() << "NetworkTools::getHostAddress - configuration bearer type:" << in_network_config.bearerTypeName(); QNetworkSession nws(in_network_config); if (nws.state() == QNetworkSession::Invalid || nws.state() == QNetworkSession::NotAvailable) return QHostAddress(); qDebug() << "NetworkTools::getHostAddress - session state:" << nws.state(); QNetworkInterface nwi = nws.interface(); if (nwi.isValid() == false) return QHostAddress(); if (nwi.addressEntries().isEmpty()) return QHostAddress(); foreach(QNetworkAddressEntry temp, nwi.addressEntries()) qDebug() << "NetworkTools::getHostAddress - session addr entry:" << temp.ip().toString(); QNetworkAddressEntry nwae = nwi.addressEntries().first(); QHostAddress host_address = nwae.ip(); return host_address; }
static QList<QNetworkInterfacePrivate *> interfaceListing() { TInt err(KErrNone); QList<QNetworkInterfacePrivate *> interfaces; QList<QHostAddress> addressesWithEstimatedNetmasks; // Open dummy socket for interface queries RSocket socket; err = socket.Open(qt_symbianGetSocketServer(), _L("udp")); if (err) { return interfaces; } // Ask socket to start enumerating interfaces err = socket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl); if (err) { socket.Close(); return interfaces; } int ifindex = 0; TPckgBuf<TSoInetInterfaceInfo> infoPckg; TSoInetInterfaceInfo &info = infoPckg(); while (socket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, infoPckg) == KErrNone) { if (info.iName != KNullDesC) { TName address; QNetworkAddressEntry entry; QNetworkInterfacePrivate *iface = 0; iface = new QNetworkInterfacePrivate; iface->index = ifindex++; interfaces << iface; iface->name = qt_TDesC2QString(info.iName); iface->flags = convertFlags(info); if (/*info.iFeatures&KIfHasHardwareAddr &&*/ info.iHwAddr.Family() != KAFUnspec) { for (TInt i = sizeof(SSockAddr); i < sizeof(SSockAddr) + info.iHwAddr.GetUserLen(); i++) { address.AppendNumFixedWidth(info.iHwAddr[i], EHex, 2); if ((i + 1) < sizeof(SSockAddr) + info.iHwAddr.GetUserLen()) address.Append(_L(":")); } address.UpperCase(); iface->hardwareAddress = qt_TDesC2QString(address); } // Get the address of the interface entry.setIp(qt_QHostAddressFromTInetAddr(info.iAddress)); #if defined(QNETWORKINTERFACE_DEBUG) qDebug() << "address is" << info.iAddress.Family() << entry.ip(); qDebug() << "netmask is" << info.iNetMask.Family() << qt_QHostAddressFromTInetAddr( info.iNetMask ); #endif // Get the interface netmask if (info.iNetMask.IsUnspecified()) { // For some reason netmask is always 0.0.0.0 for IPv4 interfaces // and loopback interfaces (which we statically know) if (info.iAddress.IsV4Mapped()) { if (info.iFeatures & KIfIsLoopback) { entry.setPrefixLength(32); } else { // Workaround: Let Symbian determine netmask based on IP address class (IPv4 only API) TInetAddr netmask; netmask.NetMask(info.iAddress); entry.setNetmask(QHostAddress(netmask.Address())); //binary convert v4 address addressesWithEstimatedNetmasks << entry.ip(); #if defined(QNETWORKINTERFACE_DEBUG) qDebug() << "address class determined netmask" << entry.netmask(); #endif } } else { // For IPv6 interfaces if (info.iFeatures & KIfIsLoopback) { entry.setPrefixLength(128); } else if (info.iNetMask.IsUnspecified()) { //Don't see this error for IPv6, but try to handle it if it happens entry.setPrefixLength(64); //most common #if defined(QNETWORKINTERFACE_DEBUG) qDebug() << "total guess netmask" << entry.netmask(); #endif addressesWithEstimatedNetmasks << entry.ip(); } } } else { //Expected code path for IPv6 non loopback interfaces (IPv4 could come here if symbian is fixed) entry.setNetmask(qt_QHostAddressFromTInetAddr(info.iNetMask)); #if defined(QNETWORKINTERFACE_DEBUG) qDebug() << "reported netmask" << entry.netmask(); #endif } // broadcast address is determined from the netmask in postProcess() // Add new entry to interface address entries iface->addressEntries << entry; #if defined(QNETWORKINTERFACE_DEBUG) qDebug("\n Found network interface %s, interface flags:\n\ IsUp = %d, IsRunning = %d, CanBroadcast = %d,\n\ IsLoopBack = %d, IsPointToPoint = %d, CanMulticast = %d, \n\ ip = %s, netmask = %s, broadcast = %s,\n\ hwaddress = %s", iface->name.toLatin1().constData(), iface->flags & QNetworkInterface::IsUp, iface->flags & QNetworkInterface::IsRunning, iface->flags & QNetworkInterface::CanBroadcast, iface->flags & QNetworkInterface::IsLoopBack, iface->flags & QNetworkInterface::IsPointToPoint, iface->flags & QNetworkInterface::CanMulticast, entry.ip().toString().toLatin1().constData(), entry.netmask().toString().toLatin1().constData(), entry.broadcast().toString().toLatin1().constData(), iface->hardwareAddress.toLatin1().constData()); #endif } }
static QList<QNetworkInterfacePrivate *> interfaceListingWinXP() { QList<QNetworkInterfacePrivate *> interfaces; IP_ADAPTER_ADDRESSES staticBuf[2]; // 2 is arbitrary PIP_ADAPTER_ADDRESSES pAdapter = staticBuf; ULONG bufSize = sizeof staticBuf; const QHash<QHostAddress, QHostAddress> &ipv4netmasks = ipv4Netmasks(); ULONG flags = GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_MULTICAST; ULONG retval = ptrGetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAdapter, &bufSize); if (retval == ERROR_BUFFER_OVERFLOW) { // need more memory pAdapter = (IP_ADAPTER_ADDRESSES *)malloc(bufSize); if (!pAdapter) return interfaces; // try again if (ptrGetAdaptersAddresses(AF_UNSPEC, flags, NULL, pAdapter, &bufSize) != ERROR_SUCCESS) { free(pAdapter); return interfaces; } } else if (retval != ERROR_SUCCESS) { // error return interfaces; } // iterate over the list and add the entries to our listing for (PIP_ADAPTER_ADDRESSES ptr = pAdapter; ptr; ptr = ptr->Next) { QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate; interfaces << iface; iface->index = 0; if (ptr->Length >= offsetof(IP_ADAPTER_ADDRESSES, Ipv6IfIndex) && ptr->Ipv6IfIndex != 0) iface->index = ptr->Ipv6IfIndex; else if (ptr->IfIndex != 0) iface->index = ptr->IfIndex; iface->flags = QNetworkInterface::CanBroadcast; if (ptr->OperStatus == IfOperStatusUp) iface->flags |= QNetworkInterface::IsUp | QNetworkInterface::IsRunning; if ((ptr->Flags & IP_ADAPTER_NO_MULTICAST) == 0) iface->flags |= QNetworkInterface::CanMulticast; if (ptr->IfType == IF_TYPE_PPP) iface->flags |= QNetworkInterface::IsPointToPoint; iface->name = QString::fromLocal8Bit(ptr->AdapterName); iface->friendlyName = QString::fromWCharArray(ptr->FriendlyName); if (ptr->PhysicalAddressLength) iface->hardwareAddress = iface->makeHwAddress(ptr->PhysicalAddressLength, ptr->PhysicalAddress); else // loopback if it has no address iface->flags |= QNetworkInterface::IsLoopBack; // The GetAdaptersAddresses call has an interesting semantic: // It can return a number N of addresses and a number M of prefixes. // But if you have IPv6 addresses, generally N > M. // I cannot find a way to relate the Address to the Prefix, aside from stopping // the iteration at the last Prefix entry and assume that it applies to all addresses // from that point on. PIP_ADAPTER_PREFIX pprefix = 0; if (ptr->Length >= offsetof(IP_ADAPTER_ADDRESSES, FirstPrefix)) pprefix = ptr->FirstPrefix; for (PIP_ADAPTER_UNICAST_ADDRESS addr = ptr->FirstUnicastAddress; addr; addr = addr->Next) { QNetworkAddressEntry entry; entry.setIp(addressFromSockaddr(addr->Address.lpSockaddr)); if (pprefix) { if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) { entry.setNetmask(ipv4netmasks[entry.ip()]); // broadcast address is set on postProcess() } else { //IPV6 entry.setPrefixLength(pprefix->PrefixLength); } pprefix = pprefix->Next ? pprefix->Next : pprefix; } iface->addressEntries << entry; } } if (pAdapter != staticBuf) free(pAdapter); return interfaces; }
void tst_QNetworkAddressEntry::getSetCheck() { QNetworkAddressEntry entry; QVERIFY(entry.ip().isNull()); QVERIFY(entry.netmask().isNull()); QVERIFY(entry.broadcast().isNull()); QCOMPARE(entry.prefixLength(), -1); entry.setIp(QHostAddress::LocalHost); QCOMPARE(entry.ip(), QHostAddress(QHostAddress::LocalHost)); entry.setIp(QHostAddress()); QVERIFY(entry.ip().isNull()); entry.setBroadcast(QHostAddress::LocalHost); QCOMPARE(entry.broadcast(), QHostAddress(QHostAddress::LocalHost)); entry.setBroadcast(QHostAddress()); QVERIFY(entry.broadcast().isNull()); // netmask and prefix length tested in the next test entry.setIp(QHostAddress::LocalHost); entry.setBroadcast(QHostAddress::LocalHost); QNetworkAddressEntry entry2; QVERIFY(entry != entry2); QVERIFY(!(entry == entry2)); entry = entry2; QCOMPARE(entry, entry2); QVERIFY(entry == entry); QVERIFY(!(entry != entry2)); }
void tst_QNetworkAddressEntry::prefixAndNetmask() { QFETCH(QHostAddress, ip); QFETCH(QHostAddress, netmask); QFETCH(int, prefix); QNetworkAddressEntry entry; // first, without setting the IP, all must be invalid: entry.setNetmask(netmask); QVERIFY(entry.netmask().isNull()); entry.setPrefixLength(prefix); QCOMPARE(entry.prefixLength(), -1); // set the IP: entry.setIp(ip); // set the netmask: if (!netmask.isNull()) { entry.setNetmask(netmask); // was it a valid one? if (prefix != -1) { QVERIFY(!entry.netmask().isNull()); QCOMPARE(entry.netmask(), netmask); QCOMPARE(entry.prefixLength(), prefix); } else { // not valid QVERIFY(entry.netmask().isNull()); QCOMPARE(entry.prefixLength(), -1); } } entry.setNetmask(QHostAddress()); QVERIFY(entry.netmask().isNull()); QCOMPARE(entry.prefixLength(), -1); // set the prefix if (prefix != -1) { entry.setPrefixLength(prefix); // was it a valid one? if (!netmask.isNull()) { QVERIFY(!entry.netmask().isNull()); QCOMPARE(entry.netmask(), netmask); QCOMPARE(entry.prefixLength(), prefix); } else { // not valid QVERIFY(entry.netmask().isNull()); QCOMPARE(entry.prefixLength(), -1); } } entry.setPrefixLength(-1); QVERIFY(entry.netmask().isNull()); QCOMPARE(entry.prefixLength(), -1); }
static QList<QNetworkInterfacePrivate *> interfaceListing() { QList<QNetworkInterfacePrivate *> interfaces; QList<HostNameInfo> hostList; ComPtr<INetworkInformationStatics> hostNameStatics; GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Networking_Connectivity_NetworkInformation).Get(), &hostNameStatics); ComPtr<IVectorView<HostName *>> hostNames; hostNameStatics->GetHostNames(&hostNames); if (!hostNames) return interfaces; unsigned int hostNameCount; hostNames->get_Size(&hostNameCount); for (unsigned i = 0; i < hostNameCount; ++i) { HostNameInfo hostInfo; ComPtr<IHostName> hostName; hostNames->GetAt(i, &hostName); HostNameType type; hostName->get_Type(&type); if (type == HostNameType_DomainName) continue; ComPtr<IIPInformation> ipInformation; hostName->get_IPInformation(&ipInformation); ComPtr<INetworkAdapter> currentAdapter; ipInformation->get_NetworkAdapter(¤tAdapter); currentAdapter->get_NetworkAdapterId(&hostInfo.adapterId); ComPtr<IReference<unsigned char>> prefixLengthReference; ipInformation->get_PrefixLength(&prefixLengthReference); prefixLengthReference->get_Value(&hostInfo.prefixLength); // invalid prefixes if ((type == HostNameType_Ipv4 && hostInfo.prefixLength > 32) || (type == HostNameType_Ipv6 && hostInfo.prefixLength > 128)) continue; HString name; hostName->get_CanonicalName(name.GetAddressOf()); UINT32 length; PCWSTR rawString = name.GetRawBuffer(&length); hostInfo.address = QString::fromWCharArray(rawString, length); hostList << hostInfo; } INetworkInformationStatics *networkInfoStatics; GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Networking_Connectivity_NetworkInformation).Get(), &networkInfoStatics); ComPtr<IVectorView<ConnectionProfile *>> connectionProfiles; networkInfoStatics->GetConnectionProfiles(&connectionProfiles); if (!connectionProfiles) return interfaces; unsigned int size; connectionProfiles->get_Size(&size); for (unsigned int i = 0; i < size; ++i) { QNetworkInterfacePrivate *iface = new QNetworkInterfacePrivate; interfaces << iface; ComPtr<IConnectionProfile> profile; connectionProfiles->GetAt(i, &profile); NetworkConnectivityLevel connectivityLevel; profile->GetNetworkConnectivityLevel(&connectivityLevel); if (connectivityLevel != NetworkConnectivityLevel_None) iface->flags = QNetworkInterface::IsUp | QNetworkInterface::IsRunning; ComPtr<INetworkAdapter> adapter; profile->get_NetworkAdapter(&adapter); UINT32 type; adapter->get_IanaInterfaceType(&type); if (type == 23) iface->flags |= QNetworkInterface::IsPointToPoint; GUID id; adapter->get_NetworkAdapterId(&id); OLECHAR adapterName[39]={0}; StringFromGUID2(id, adapterName, 39); iface->name = QString::fromWCharArray(adapterName); // According to http://stackoverflow.com/questions/12936193/how-unique-is-the-ethernet-network-adapter-id-in-winrt-it-is-derived-from-the-m // obtaining the MAC address using WinRT API is impossible // iface->hardwareAddress = ? for (int i = 0; i < hostList.length(); ++i) { const HostNameInfo hostInfo = hostList.at(i); if (id != hostInfo.adapterId) continue; QNetworkAddressEntry entry; entry.setIp(QHostAddress(hostInfo.address)); entry.setPrefixLength(hostInfo.prefixLength); iface->addressEntries << entry; hostList.takeAt(i); --i; } } return interfaces; }
static QList<QNetworkInterfacePrivate *> interfaceListing() { TInt err(KErrNone); QList<QNetworkInterfacePrivate *> interfaces; // Connect to Native socket server RSocketServ socketServ; err = socketServ.Connect(); if (err) return interfaces; // Open dummy socket for interface queries RSocket socket; err = socket.Open(socketServ, _L("udp")); if (err) { socketServ.Close(); return interfaces; } // Ask socket to start enumerating interfaces err = socket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl); if (err) { socket.Close(); socketServ.Close(); return interfaces; } int ifindex = 0; TPckgBuf<TSoInetInterfaceInfo> infoPckg; TSoInetInterfaceInfo &info = infoPckg(); while (socket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, infoPckg) == KErrNone) { // Do not include IPv6 addresses because netmask and broadcast address cannot be determined correctly if (info.iName != KNullDesC && info.iAddress.IsV4Mapped()) { TName address; QNetworkAddressEntry entry; QNetworkInterfacePrivate *iface = 0; iface = new QNetworkInterfacePrivate; iface->index = ifindex++; interfaces << iface; iface->name = qt_TDesC2QString(info.iName); iface->flags = convertFlags(info); if (/*info.iFeatures&KIfHasHardwareAddr &&*/ info.iHwAddr.Family() != KAFUnspec) { for (TInt i = sizeof(SSockAddr); i < sizeof(SSockAddr) + info.iHwAddr.GetUserLen(); i++) { address.AppendNumFixedWidth(info.iHwAddr[i], EHex, 2); if ((i + 1) < sizeof(SSockAddr) + info.iHwAddr.GetUserLen()) address.Append(_L(":")); } address.UpperCase(); iface->hardwareAddress = qt_TDesC2QString(address); } // Get the address of the interface info.iAddress.Output(address); entry.setIp(QHostAddress(qt_TDesC2QString(address))); // Get the interface netmask // For some reason netmask is always 0.0.0.0 // info.iNetMask.Output(address); // entry.setNetmask( QHostAddress( qt_TDesC2QString( address ) ) ); // Workaround: Let Symbian determine netmask based on IP address class // TODO: Works only for IPv4 - Task: 259128 Implement IPv6 support TInetAddr netmask; netmask.NetMask(info.iAddress); netmask.Output(address); entry.setNetmask(QHostAddress(qt_TDesC2QString(address))); // Get the interface broadcast address if (iface->flags & QNetworkInterface::CanBroadcast) { // For some reason broadcast address is always 0.0.0.0 // info.iBrdAddr.Output(address); // entry.setBroadcast( QHostAddress( qt_TDesC2QString( address ) ) ); // Workaround: Let Symbian determine broadcast address based on IP address // TODO: Works only for IPv4 - Task: 259128 Implement IPv6 support TInetAddr broadcast; broadcast.NetBroadcast(info.iAddress); broadcast.Output(address); entry.setBroadcast(QHostAddress(qt_TDesC2QString(address))); } // Add new entry to interface address entries iface->addressEntries << entry; #if defined(QNETWORKINTERFACE_DEBUG) printf("\n Found network interface %s, interface flags:\n\ IsUp = %d, IsRunning = %d, CanBroadcast = %d,\n\ IsLoopBack = %d, IsPointToPoint = %d, CanMulticast = %d, \n\ ip = %s, netmask = %s, broadcast = %s,\n\ hwaddress = %s", iface->name.toLatin1().constData(), iface->flags & QNetworkInterface::IsUp, iface->flags & QNetworkInterface::IsRunning, iface->flags & QNetworkInterface::CanBroadcast, iface->flags & QNetworkInterface::IsLoopBack, iface->flags & QNetworkInterface::IsPointToPoint, iface->flags & QNetworkInterface::CanMulticast, entry.ip().toString().toLatin1().constData(), entry.netmask().toString().toLatin1().constData(), entry.broadcast().toString().toLatin1().constData(), iface->hardwareAddress.toLatin1().constData()); #endif } }