From 6fde95f2921208361f3e5de1f419930bdefdc7fd Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Tue, 8 Mar 2011 20:09:24 +0100 Subject: host lookup, better MAC/IP info functions, IP one doesn't quite work yet --- src/sysInfo.cpp | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) (limited to 'src/sysInfo.cpp') diff --git a/src/sysInfo.cpp b/src/sysInfo.cpp index 8c93c52..266c210 100644 --- a/src/sysInfo.cpp +++ b/src/sysInfo.cpp @@ -3,43 +3,46 @@ #include #include +//static int eth0_index = 0; +// ------------------------------------------------------------------------------------------------ sysInfo::sysInfo(){ if (debug) qDebug() << "sysInfo created."; + // Maybe search for eth0, etc } +// ------------------------------------------------------------------------------------------------ sysInfo::~sysInfo(){} +// ------------------------------------------------------------------------------------------------ QString sysInfo::getInfo(QString& infoName){ if (debug) qDebug() << "sysInfo : getInfo(" << infoName << ")"; if (infoName == QString("mac")) return getMACAddress(); else if (infoName == QString("ip")) return getIPAddress(); - // still here? --> error - return "error"; + // still here? + return "info_error"; } +// ------------------------------------------------------------------------------------------------ QString sysInfo::getMACAddress(){ - QNetworkInterface *qNetI = new QNetworkInterface(); - QList list; - list=qNetI->allInterfaces(); - QString str; - QString macAddress; - for (int i = 0; i < list.size(); ++i) { - str = list.at(i).name(); - macAddress = list.at(i).hardwareAddress(); - if (debug) qDebug() << str << " : " << macAddress; + /* Returns MAC address of eth0 for now. */ + QNetworkInterface qni = QNetworkInterface::interfaceFromName(QString("eth0")); + if (!qni.isValid()){ + if (debug) qDebug() << "No interface matching \"eth0\" found."; + return "no_eth0"; } - // returns only the latest found interface ... to fix - return macAddress; + //eth0_index = qni.index(); + return qni.hardwareAddress(); } - +// ------------------------------------------------------------------------------------------------ QString sysInfo::getIPAddress(){ - QString ipAddress = ""; - QNetworkInterface *qNetI = new QNetworkInterface(); - QList list; - list=qNetI->allAddresses(); - for (int i = 0; i < list.size(); ++i) { - ipAddress = list.at(i).toString(); - if (debug) qDebug() << "IP Address" << " : " << ipAddress; - } - // returns only the latest found interface ... to fix - return ipAddress; + // Again for eth0 only at the moment. + // TODO: this doesn't quite work yet... + QNetworkInterface qni = QNetworkInterface::interfaceFromName(QString("eth0")); + QList addrlist = qni.allAddresses(); + foreach(QHostAddress addr, addrlist){ + if (addr.protocol() == QAbstractSocket::IPv6Protocol) + qDebug() << "eth0: IPv4 Address: " << addr.toString(); + return addr.toString(); + } + // still here? + return "ip_error"; } -- cgit v1.2.3-55-g7522