summaryrefslogtreecommitdiffstats
path: root/src/shared/network.cpp
blob: 392cd4331a339487ac580233e3dec0bab98fd777 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * network.cpp
 *
 *  Created on: 28.01.2013
 *      Author: sr
 */

#include "network.h"
#include <QNetworkInterface>
#include <QHostAddress>
#include <QList>

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<QHostAddress> list(QNetworkInterface::allAddresses());
	return interfaceAddressesToString(list);
}

QString interfaceAddressesToString(const QList<QHostAddress>& list)
{
	QString ret;
	ret.reserve(500);
	for (QList<QHostAddress>::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);
}


}