QString DeviceManager::getDeviceIP() { QNetworkInterface *qni; qni = new QNetworkInterface(); *qni = qni->interfaceFromName(QString("%1").arg("wlan0")); return qni->addressEntries().at(0).ip().toString(); }
bool Utilities::isOnline(QNetworkAccessManager *manager) { bool ret = false; QSettings settings("Entomologist"); if (settings.value("work-offline", false).toBool()) return(false); #if QT_VERSION >= 0x040700 if (manager->networkAccessible() != QNetworkAccessManager::NotAccessible) ret = true; else ret = false; #else QNetworkInterface iface; QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces(); for (int i = 0; i < ifaces.count(); ++i) { iface = ifaces.at(i); if (iface.flags().testFlag(QNetworkInterface::IsUp) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack) ) { if (iface.addressEntries().count() >= 1) { ret = true; break; } } } #endif return(ret); }
bool QNativeSocketEnginePrivate::nativeSetMulticastInterface(const QNetworkInterface &iface) { #ifndef QT_NO_IPV6 if (socketProtocol == QAbstractSocket::IPv6Protocol) { uint v = iface.isValid() ? iface.index() : 0; return (::setsockopt(socketDescriptor, IPPROTO_IPV6, IPV6_MULTICAST_IF, (char *) &v, sizeof(v)) != -1); } #endif struct in_addr v; if (iface.isValid()) { QList<QNetworkAddressEntry> entries = iface.addressEntries(); for (int i = 0; i < entries.count(); ++i) { const QNetworkAddressEntry &entry = entries.at(i); const QHostAddress &ip = entry.ip(); if (ip.protocol() == QAbstractSocket::IPv4Protocol) { v.s_addr = htonl(ip.toIPv4Address()); int r = ::setsockopt(socketDescriptor, IPPROTO_IP, IP_MULTICAST_IF, (char *) &v, sizeof(v)); if (r != -1) return true; } } return false; } v.s_addr = INADDR_ANY; return (::setsockopt(socketDescriptor, IPPROTO_IP, IP_MULTICAST_IF, (char *) &v, sizeof(v)) != -1); }
/* public ***********************************************/ Dialog::Dialog(QWidget *parent) : QDialog(parent), ui_(new Ui::Dialog), Server_(0), Client_(0) { LOG << "Dialog init."; ui_->setupUi(this); QNetworkInterface *inter = new QNetworkInterface(); QList<QHostAddress> list; list = inter->allAddresses(); QString hostname = ""; for (int i = 0; i < list.size(); ++i) { QHostAddress tmp(list.at(i).toIPv4Address()); if (tmp.toString().compare("127.0.0.1") && tmp.toString().compare("0.0.0.0")) hostname = tmp.toString(); } // ui_->serverIPEdit->setText(hostname); ui_->serverIPEdit->setText("127.0.0.1"); ui_->serverPortEdit->setText("11111"); ui_->serverMsgEdit->setText("Hello"); // ui_->serverFileEdit->setText("/home/dn/yabss.crt"); ui_->clientIPEdit->setText("127.0.0.1"); ui_->clientPortEdit->setText("11111"); ui_->clientFileWidget->hide(); // ui_->recieveFileEdit->setText("/tmp/"); }
void ImageProcessing::checkNetwork() { bool running = false; QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces(); qWarning() << __FUNCTION__ << ": Running the network check"; if (pNextImage->isActive()) { qWarning() << __FUNCTION__ << ": Turning off image display timer"; pNextImage->stop(); } for (int i = 0; i < interfaces.size(); i++) { QNetworkInterface ni = interfaces[i]; if (ni.name() != "wlan0") continue; qWarning() << __FUNCTION__ << ": Checking interface" << ni.name() << " with flags" << ni.flags(); if (ni.flags().testFlag(QNetworkInterface::IsUp) && ni.flags().testFlag(QNetworkInterface::IsRunning)) { running = true; getContentList(); } } if (!running) { qWarning() << __FUNCTION__ << ": Network check didn't find an available interface"; showLocalImage(); } }
// Go through all interfaces and add all IPs for interfaces that can // broadcast and are not local. foreach (QNetworkInterface networkInterface, QNetworkInterface::allInterfaces()) { if (!networkInterface.addressEntries().isEmpty() && networkInterface.isValid()) { QNetworkInterface::InterfaceFlags interfaceFlags = networkInterface.flags(); if (interfaceFlags & QNetworkInterface::IsUp && interfaceFlags & QNetworkInterface::IsRunning && interfaceFlags & QNetworkInterface::CanBroadcast && !(interfaceFlags & QNetworkInterface::IsLoopBack)) { qDebug() << "ConnectionManager::handleNetworkSessionOpened():" << "The connection is valid!"; if (mNetworkSession) { mAccessPoint = mNetworkSession->configuration().name(); emit accessPointChanged(mAccessPoint); } mMyIp.setAddress(networkInterface.addressEntries().at(0).ip().toString()); emit myIpChanged(mMyIp.toString()); setState(Connected); return; } } }
QString DeviceManager::getDeviceMac() { QNetworkInterface *qni; qni = new QNetworkInterface(); *qni = qni->interfaceFromName(QString("%1").arg("wlan0")); return qni->hardwareAddress(); }
bool Network::openConnection(QString &netInterface) { // Internet Access Point QNetworkConfigurationManager manager; const bool canStartIAP = (manager.capabilities() & QNetworkConfigurationManager::CanStartAndStopInterfaces); // If there is a default access point, use it QNetworkConfiguration cfg = manager.defaultConfiguration(); if (!cfg.isValid() || !canStartIAP) { return false; } // Open session m_session = new QNetworkSession(cfg); m_session->open(); // Waits for session to be open and continues after that m_session->waitForOpened(); // Show interface name to the user QNetworkInterface iff = m_session->interface(); netInterface = iff.humanReadableName(); return true; }
/* * Checks if we have internet access! */ bool UnixCommand::hasInternetConnection() { QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces(); bool result = false; for (int i = 0; i < ifaces.count(); i++){ QNetworkInterface iface = ifaces.at(i); if ( iface.flags().testFlag(QNetworkInterface::IsUp) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack) ){ for (int j=0; j<iface.addressEntries().count(); j++){ /* We have an interface that is up, and has an ip address therefore the link is present. We will only enable this check on first positive, all later results are incorrect */ if (result == false) result = true; } } } //It seems to be alright, but let's make a ping to see the result /*if (result == true) { result = UnixCommand::doInternetPingTest(); }*/ return result; }
//someone selected another interface void ApplicationWindow::interfaceBoxIndexChanged(int boxIndex) { //get the right index data int interfaceIndex=interfaceBox->itemData(boxIndex).toInt(); QNetworkInterface tmpInterface = QNetworkInterface::interfaceFromIndex (interfaceIndex); macLine->setText(tmpInterface.hardwareAddress()); // indexLine->setText(QString("%1").arg(interfaceIndex)); QList<QNetworkAddressEntry> interfaceAdresses; interfaceAdresses=tmpInterface.addressEntries(); if (interfaceAdresses.size()>0) { ipLine->setText(interfaceAdresses[0].ip().toString()); maskLine->setText(interfaceAdresses[0].netmask().toString()); } else { ipLine->setText(""); maskLine->setText(""); } writeLog(createHeaderText()); }
bool isConnectedToNetwork2() { QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces(); bool result = false; for (int i = 0; i < ifaces.count(); i++) { QNetworkInterface iface = ifaces.at(i); if ( iface.flags().testFlag(QNetworkInterface::IsUp) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack) ) { // this loop is important for (int j=0; j<iface.addressEntries().count(); j++) { // we have an interface that is up, and has an ip address // therefore the link is present // we will only enable this check on first positive, // all later results are incorrect if (result == false) result = true; } } } return result; }
/** * @brief wavrNetwork::getNetworkAddressEntry * @param pAddressEntry Gets the address entries from network interface and fills in this variable. * @return Returns true opon success. * Gets the address entries from network interface whether IPV4 or IPV6 and fills the data in pAddressEntry. */ bool wavrNetwork::getNetworkAddressEntry(QNetworkAddressEntry* pAddressEntry) { // get the first active network interface QNetworkInterface networkInterface; if(getNetworkInterface(&networkInterface)) { wavrTrace::write("Querying IP address from network interface..."); // get a list of all associated ip addresses of the interface QList<QNetworkAddressEntry> addressEntries = networkInterface.addressEntries(); // return the first address which is an ipv4 address for(int index = 0; index < addressEntries.count(); index++) { if(addressEntries[index].ip().protocol() == QAbstractSocket::IPv4Protocol) { *pAddressEntry = addressEntries[index]; this->networkInterface = networkInterface; this->interfaceName = networkInterface.name(); wavrTrace::write("IPv4 address found for network interface."); return true; } } // if ipv4 address is not present, check for ipv6 address for(int index = 0; index < addressEntries.count(); index++) { if(addressEntries[index].ip().protocol() == QAbstractSocket::IPv6Protocol) { *pAddressEntry = addressEntries[index]; this->networkInterface = networkInterface; this->interfaceName = networkInterface.name(); wavrTrace::write("IPv6 address found for network interface."); return true; } } wavrTrace::write("Warning: No IP address found for network interface."); } return false; }
//Source : http://stackoverflow.com/a/7616111 foreach(QNetworkInterface netInterface, QNetworkInterface::allInterfaces()) { // Return only the first non-loopback MAC Address if (!(netInterface.flags() & QNetworkInterface::IsLoopBack)){ macAddress = netInterface.hardwareAddress(); break; } }
void SpeedWrapper::gconf_value_changed() { bool ok; //check netspeed enabled if(!m_gconf_enabled->value().isValid()) m_gconf_enabled->set(true); //check netspeed interface if(!m_gconf_speed_interface->value().isValid()) m_gconf_speed_interface->set("wlan0"); //check netspeed update time if(!m_gconf_update_time->value().isValid()) m_gconf_update_time->set(1000); //check when online if(!m_gconf_when_online->value().isValid()) m_gconf_when_online->set(false); m_is_enabled = m_gconf_enabled->value().toBool(); m_speed_interface = m_gconf_speed_interface->value().toString(); m_when_online = m_gconf_when_online->value().toBool(); if(m_speed_interface.isEmpty() || m_speed_interface.isNull()) m_speed_interface = "wlan0"; m_update_time = m_gconf_update_time->value().toInt(&ok); if(!ok) m_update_time = 1000; QNetworkInterface interface = QNetworkInterface::interfaceFromName(m_speed_interface); m_network_interface_valid = interface.isValid(); bool online = (interface.flags().testFlag(QNetworkInterface::IsUp) && interface.flags().testFlag(QNetworkInterface::IsRunning)); if(online != m_isOnline) { emit onlineChanged(online); updateData(); } m_isOnline = online; if(m_is_enabled) { if(m_when_online) { if(m_isOnline) { m_timer->stop(); m_timer->start(m_update_time); }else{ m_timer->stop(); } }else{ m_timer->stop(); m_timer->start(m_update_time); } }else{ m_timer->stop(); } }
// ---------------------------------------------------------------------------- // getHostAddress (static) // ---------------------------------------------------------------------------- QHostAddress NetworkTools::getHostAddress(const QNetworkInterface &in_interface) { if (in_interface.isValid() == false) return QHostAddress(); QList<QNetworkAddressEntry> addr_entry_list = in_interface.addressEntries(); if (addr_entry_list.isEmpty()) return QHostAddress(); return addr_entry_list.first().ip(); }
OrderSave::OrderSave(QObject *parent) : QObject(parent) { QNetworkInterface *qni; qni = new QNetworkInterface(); *qni = qni->interfaceFromName(QString("%1").arg("eth0")); mac = qni->hardwareAddress(); //»ñÈ¡MACµØÖ· }
//--------------------------------------------- //-----------------DHCP PACKET----------------- //--------------------------------------------- dhcp_packet::dhcp_packet(QHostAddress ip, QHostAddress netmask, QNetworkInterface inter) { this->op = 0x01; this->htype = 0x01; this->hlen = 0x06; this->hops = 0x00; this->xid[0] = 0x63; this->xid[1] = 0x9F; this->xid[2] = 0xC0; this->xid[3] = 0x4B; this->secs[0] = 0x00; this->secs[1] =0x00; this->flags[0] = 0x00; this->flags[1] = 0x00; this->ciaddr = ip.toIPv4Address(); this->yiaddr = 0x00000000; this->siaddr = ip.toIPv4Address() & netmask.toIPv4Address(); for (int i = 0; i < 4; i++) this->giaddr[i] = 0x00; qDebug() << inter.hardwareAddress(); bool ok; this->chaddr[0] = QString::fromStdString(inter.hardwareAddress().toStdString().substr(0, 2)).toInt(&ok, 16); this->chaddr[1] = QString::fromStdString(inter.hardwareAddress().toStdString().substr(3, 2)).toInt(&ok, 16); this->chaddr[2] = QString::fromStdString(inter.hardwareAddress().toStdString().substr(6, 2)).toInt(&ok, 16); this->chaddr[3] = QString::fromStdString(inter.hardwareAddress().toStdString().substr(9, 2)).toInt(&ok, 16); this->chaddr[4] = QString::fromStdString(inter.hardwareAddress().toStdString().substr(12, 2)).toInt(&ok, 16); this->chaddr[5] = QString::fromStdString(inter.hardwareAddress().toStdString().substr(15, 2)).toInt(&ok, 16); for (int i = 0; i < 5; i++) qDebug() << chaddr[i]; for (int i = 0; i < 10; i++) this->chaddr[i+6] = 0x00; for (int i = 0; i < 64; i++) this->sname[i] = 0x00; for (int i = 0; i < 128; i++) this->file[i] = 0x00; //DHCP protocols options.append(0x63); options.append(0x82); options.append(0x53); options.append(0x63); }
void ConfigureDlg::populateAdaptors() { QNetworkInterface cInterface; int index; m_adaptor->insertItem(0, "<any>"); index = 1; QList<QNetworkInterface> ani = QNetworkInterface::allInterfaces(); foreach (cInterface, ani) m_adaptor->insertItem(index++, cInterface.humanReadableName()); }
void getMyIP() { QNetworkInterface *qni = new QNetworkInterface(); QList<QHostAddress> ips; QList<QString> ipstr; QString str; ips = qni->allAddresses(); for (int i = 0; i < ips.size(); i++) { str = ips.at(i).toString(); qDebug() << i << " : " << str; } }
void Inet6AddressHolder::init(QByteArray addr, QNetworkInterface nif) throw (UnknownHostException) { setAddr(addr); if (nif.isValid()) { this->scope_id = nif.index();//deriveNumericScope(ipaddress, nif); this->scope_id_set = true; this->scope_ifname = nif; this->scope_ifname_set = true; } }
void MainWindow::onlineStateChange() { static bool netUp = false; bool bState = false; QNetworkInterface netInterface = QNetworkInterface::interfaceFromName(NET_NAME); //qDebug()<<netInterface; if (netInterface.isValid()) { if (netInterface.flags().testFlag(QNetworkInterface::IsUp) && netInterface.flags().testFlag(QNetworkInterface::IsRunning)) { if (!netUp) { repaint(); netUp = true; } QHostInfo host = QHostInfo::fromName(WEB_SITE); if (!host.addresses().isEmpty()) { repaint(); Global::s_netState = true; bState = true; m_shangChun->UploadData(); } else { repaint(); Global::s_netState = false; bState = false; } } else { repaint(); Global::s_netState = false; bState = false; netUp = false; } } if(bState) { QImage NetImage(":/images/NetConn24.ico"); this->network->setPixmap(QPixmap::fromImage(NetImage)); } else { QImage NetImage(":/images/NetDisConn24.ico"); this->network->setPixmap(QPixmap::fromImage(NetImage)); } SetUnUpCount(); }
// Set up bind address combobox foreach (const QNetworkInterface &netif, QNetworkInterface::allInterfaces()) { const QString &hname = netif.name(); foreach (const QNetworkAddressEntry &netaddr, netif.addressEntries()) { const QHostAddress &addr = netaddr.ip(); if (addr.protocol() == QAbstractSocket::IPv4Protocol) { QString item_text(addr.toString() + " (" + hname + ")"); settings_dialog.bind_address->addItem(item_text, addr.toString()); } } }
DeviceList::DeviceList() : QObject(NULL), _scanner() { qRegisterMetaType<DeviceInfo>("DeviceInfo"); _started = false; _currentDev = NULL; QNetworkInterface iface = NetUtils::getPreferedInterface(); DeviceInfo computer; computer.mac = NetUtils::strToMac(iface.hardwareAddress()); computer.ip = NetUtils::getIp(iface).toString(); computer.name = "Cet Ordinateur"; computer.status = Device::CurrentComputer; computer.type = Device::ChapiServer; _currentDev = generateDevice(computer, true); _devices.insert(computer.mac, _currentDev); }
OceanSocket::OceanSocket(QObject *parent) : QObject(parent) { // setup the heartbeat timer heartbeat = new QTimer(this); connect(heartbeat, SIGNAL(timeout()), this, SLOT(heartbeatTimer())); // setup our ocean sockets groupAddress = QHostAddress(MULTICAST_ADDRESS); QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces(); for (int i = 0; i < ifaces.count(); i++) { QNetworkInterface iface = ifaces.at(i); if (iface.flags().testFlag(QNetworkInterface::IsUp) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack) && iface.flags().testFlag(QNetworkInterface::CanMulticast)) { for (int j = 0; j < iface.addressEntries().count(); j++) { if (iface.addressEntries().at(j).ip().protocol() != QAbstractSocket::IPv4Protocol) continue; qDebug() << "Ocean bind to iface: " << iface.name() << endl << "IP: " << iface.addressEntries().at(j).ip().toString() << endl; QUdpSocket* socket = new QUdpSocket(this); socket->bind(QHostAddress::AnyIPv4, 8047, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint); socket->joinMulticastGroup(groupAddress, iface); connect(socket, SIGNAL(readyRead()), this, SLOT(processIncomingWaves())); boundAddresses.append(iface.addressEntries().at(j).ip()); multicastSockets.append(socket); } } } sendSocket = new QUdpSocket(this); sendSocket->bind(); }
quint32 getHostAddress() { QList<QNetworkInterface> list = QNetworkInterface::allInterfaces(); for (int i = 0; i < list.size(); ++i) { QNetworkInterface inter = list.at(i); QNetworkInterface::InterfaceFlags flags = inter.flags(); if ((flags & QNetworkInterface::IsUp) && !(flags & QNetworkInterface::IsLoopBack)) { QList<QNetworkAddressEntry> addresses = inter.addressEntries(); if (!addresses.empty()) { return addresses.at(0).ip().toIPv4Address(); } } } return 0; }
bool OceanSocket::sendMessage(const char message_type, QString data) { QByteArray pulse = QString(message_type+data).toUtf8(); QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces(); for (int i = 0; i < ifaces.count(); i++) { QNetworkInterface iface = ifaces.at(i); if (iface.flags().testFlag(QNetworkInterface::IsUp) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack) && iface.flags().testFlag(QNetworkInterface::CanMulticast)) { bool isIp4 = false; for (int j = 0; j < iface.addressEntries().count(); j++) { if (iface.addressEntries().at(j).ip().scopeId() == "") isIp4 = true; } if (isIp4) { sendSocket->setMulticastInterface(iface); sendSocket->writeDatagram(pulse.data(), pulse.size(), groupAddress, 8047); } } } return true; }
void squeezeLiteGui::getplayerMACAddress(void) { DEBUGF(""); MacAddress = QByteArray( "00:00:00:00:00:04" ); QList<QNetworkInterface> netList = QNetworkInterface::allInterfaces(); QListIterator<QNetworkInterface> i( netList ); QNetworkInterface t; while( i.hasNext() ) { // note: this grabs the first functional, non-loopback address there is. It may not the be interface on which you really connect to the slimserver t = i.next(); if( !t.flags().testFlag( QNetworkInterface::IsLoopBack ) && t.flags().testFlag( QNetworkInterface::IsUp ) && t.flags().testFlag( QNetworkInterface::IsRunning ) ) { MacAddress = t.hardwareAddress().toLatin1().toLower(); if(!MacAddress.contains("%3A")) // not escape encoded encodedMacAddress = QString(MacAddress).toLatin1().toPercentEncoding(); return; } } }
bool MythCoreContext::Init(void) { if (!d) { VERBOSE(VB_IMPORTANT, LOC_ERR + "Init() Out-of-memory"); return false; } if (d->m_appBinaryVersion != MYTH_BINARY_VERSION) { VERBOSE(VB_GENERAL, QString("Application binary version (%1) does not " "match libraries (%2)") .arg(d->m_appBinaryVersion) .arg(MYTH_BINARY_VERSION)); QString warning = QObject::tr( "This application is not compatible " "with the installed MythTV libraries. " "Please recompile after a make distclean"); VERBOSE(VB_IMPORTANT, warning); return false; } has_ipv6 = false; // If any of the IPs on any interfaces look like IPv6 addresses, assume IPv6 // is available QNetworkInterface interface; QList<QHostAddress> IpList = interface.allAddresses(); for (int i = 0; i < IpList.size(); i++) { if (IpList.at(i).toString().contains(":")) has_ipv6 = true; }; return true; }
// ---------------------------------------------------------------------------- // 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; }
void AdvancedSettings::updateInterfaceAddressCombo() { // Try to get the currently selected interface name const QString ifaceName = comboBoxInterface.itemData(comboBoxInterface.currentIndex()).toString(); // Empty string for the first element const QString currentAddress = BitTorrent::Session::instance()->networkInterfaceAddress(); // Clear all items and reinsert them, default to all comboBoxInterfaceAddress.clear(); comboBoxInterfaceAddress.addItem(tr("All addresses")); comboBoxInterfaceAddress.setCurrentIndex(0); auto populateCombo = [this, ¤tAddress](const QString &ip, const QAbstractSocket::NetworkLayerProtocol &protocol) { Q_ASSERT((protocol == QAbstractSocket::IPv4Protocol) || (protocol == QAbstractSocket::IPv6Protocol)); // Only take ipv4 for now? if ((protocol != QAbstractSocket::IPv4Protocol) && (protocol != QAbstractSocket::IPv6Protocol)) return; comboBoxInterfaceAddress.addItem(ip); //Try to select the last added one if (ip == currentAddress) comboBoxInterfaceAddress.setCurrentIndex(comboBoxInterfaceAddress.count() - 1); }; if (ifaceName.isEmpty()) { for (const QHostAddress &ip : asConst(QNetworkInterface::allAddresses())) populateCombo(ip.toString(), ip.protocol()); } else { const QNetworkInterface iface = QNetworkInterface::interfaceFromName(ifaceName); const QList<QNetworkAddressEntry> addresses = iface.addressEntries(); for (const QNetworkAddressEntry &entry : addresses) { const QHostAddress ip = entry.ip(); populateCombo(ip.toString(), ip.protocol()); } } }