From 1a5709501f94014d41987b956338bb6424b9f90c Mon Sep 17 00:00:00 2001 From: sr Date: Mon, 4 Feb 2013 19:50:31 +0100 Subject: Initial commit --- src/shared/network.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/shared/network.cpp (limited to 'src/shared/network.cpp') diff --git a/src/shared/network.cpp b/src/shared/network.cpp new file mode 100644 index 0000000..6dd6a73 --- /dev/null +++ b/src/shared/network.cpp @@ -0,0 +1,50 @@ +/* + * network.cpp + * + * Created on: 28.01.2013 + * Author: sr + */ + +#include "network.h" +#include +#include +#include + +namespace Network +{ + + +/** + * Returns list of all addresses assigned to the interfaces of this machine. + * Every address is surrounded by '|', eg: + * |1.2.3.4|3.66.77.88|123:34::1| + */ +QString interfaceAddressesToString() +{ + QList list(QNetworkInterface::allAddresses()); + return interfaceAddressesToString(list); +} + +QString interfaceAddressesToString(const QList& list) +{ + QString ret; + ret.reserve(500); + for (QList::const_iterator it(list.begin()); it != list.end(); ++it) + { + if (*it == QHostAddress::LocalHost || *it == QHostAddress::LocalHostIPv6) + continue; // TODO: Filter other addresses/ranges? + ret.append("|"); + ret.append(it->toString()); + } + ret.append("|"); + return ret; +} + +bool isAddressInList(const QString& list, const QString& address) +{ + static const QString find("|%1|"); + return list.contains(find.arg(address), Qt::CaseSensitive); +} + + +} -- cgit v1.2.3-55-g7522