Poco::Net::NetworkInterface

Poco::Net::NetworkInterface を紹介します。

NetworkInterfaceTest.cpp

Poco::Net::NetworkInterface::NetworkInterfaceList を iterate し、全インターフェースを列挙。
Poco::Net::NetworkInterface の各 method を呼んで結果を表示し、さらに
 Poco::Net::IPAddress の属性も表示。

#include <Poco/Net/NetworkInterface.h>
#include <Poco/Net/IPAddress.h>
 
#include <string>
 
#include "ScopedLogMessage.h"
#include "PrepareConsoleLogger.h"
 
int main(int /*argc*/, char** /*argv*/)
{
	PrepareConsoleLogger logger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION);
 
	ScopedLogMessage msg("NetworkInterfaceTest ", "start", "end");
 
	Poco::Net::NetworkInterface::NetworkInterfaceList list = Poco::Net::NetworkInterface::list();
	int index = 0;
	if(!list.empty())
	{
		for(Poco::Net::NetworkInterface::NetworkInterfaceList::const_iterator itr=list.begin(); itr!=list.end(); ++itr)
		{
			msg.Message(Poco::format("#%d %s (%s)", ++index, itr->name(), itr->displayName()));
			msg.Message(Poco::format("     %s %s (%s)"
					,	std::string(itr->supportsIPv4() ? "IPv4":(itr->supportsIPv6() ? "IPv6":"    "))
					,	itr->address().toString()
					,	itr->broadcastAddress().toString()));
			std::string addressInfo;
			addressInfo += std::string((Poco::Net::IPAddress::IPv4 == itr->address().family()) ? "IPv4":"IPv6");
			addressInfo += itr->address().isBroadcast() ? " isBroadcast":"";
			addressInfo += itr->address().isGlobalMC() ? " isGlobalMC":"";
			addressInfo += itr->address().isIPv4Compatible() ? " isIPv4Compatible":"";
			addressInfo += itr->address().isIPv4Mapped() ? " isIPv4Mapped":"";
			addressInfo += itr->address().isLinkLocal() ? " isLinkLocal":"";
			addressInfo += itr->address().isLinkLocalMC() ? " isLinkLocalMC":"";
			addressInfo += itr->address().isLoopback() ? " isLoopback":"";
			addressInfo += itr->address().isMulticast() ? " isMulticast":"";
			addressInfo += itr->address().isNodeLocalMC() ? " isNodeLocalMC":"";
			addressInfo += itr->address().isOrgLocalMC() ? " isOrgLocalMC":"";
			addressInfo += itr->address().isSiteLocal() ? " isSiteLocal":"";
			addressInfo += itr->address().isSiteLocalMC() ? " isSiteLocalMC":"";
			addressInfo += itr->address().isUnicast() ? " isUnicast":"";
			addressInfo += itr->address().isWellKnownMC() ? " isWellKnownMC":"";
			addressInfo += itr->address().isWildcard() ? " isWildcard":"";
			msg.Message(Poco::format("    [%s]", addressInfo));
		}
	}
 
	return 0;
}

Results of execution

・On Linux

[0] NetworkInterfaceTest start
[0] #1 lo (lo)
[0]      IPv6 127.0.0.1 (0.0.0.0)
[0]     [IPv4 isIPv4Compatible isIPv4Mapped isLoopback isUnicast]
[0] #2 eth0 (eth0)
[0]      IPv6 172.16.250.129 (0.0.0.0)
[0]     [IPv4 isIPv4Compatible isIPv4Mapped isSiteLocal isUnicast]
[0] #3 lo (lo)
[0]      IPv6 ::0.0.0.1 (0.0.0.0)
[0]     [IPv6 isIPv4Compatible isLoopback isUnicast]
[0] #4 eth0 (eth0)
[0]      IPv6 FE80::20C:29FF:FED0:48A0 (0.0.0.0)
[0]     [IPv6 isLinkLocal isUnicast]
[0] NetworkInterfaceTest end

・On Macintosh OSX 10.6.6

[0] NetworkInterfaceTest start
[0] #1 lo0 (lo0)
[0]      IPv6 ::0.0.0.1 (0.0.0.0)
[0]     [IPv6 isIPv4Compatible isLoopback isUnicast]
[0] #2 lo0 (lo0)
[0]      IPv6 FE80::1 (0.0.0.0)
[0]     [IPv6 isLinkLocal isUnicast]
[0] #3 lo0 (lo0)
[0]      IPv4 127.0.0.1 (0.0.0.0)
[0]     [IPv4 isIPv4Compatible isIPv4Mapped isLoopback isUnicast]
[0] #4 lo0 (lo0)
[0]      IPv6 FD13:333A:B679:997F:216:CBFF:FEB0:AAE4 (0.0.0.0)
[0]     [IPv6 isUnicast]
[0] #5 en0 (en0)
[0]      IPv6 FE80::216:CBFF:FEB0:AAE4 (0.0.0.0)
[0]     [IPv6 isLinkLocal isUnicast]
[0] #6 en0 (en0)
[0]      IPv4 192.168.1.135 (192.168.1.255)
[0]     [IPv4 isIPv4Compatible isIPv4Mapped isSiteLocal isUnicast]
[0] #7 vmnet8 (vmnet8)
[0]      IPv4 172.16.250.1 (172.16.250.255)
[0]     [IPv4 isIPv4Compatible isIPv4Mapped isSiteLocal isUnicast]
[0] #8 vmnet1 (vmnet1)
[0]      IPv4 172.16.101.1 (172.16.101.255)
[0]     [IPv4 isIPv4Compatible isIPv4Mapped isSiteLocal isUnicast]
[0] #9 utun0 (utun0)
[0]      IPv6 FE80::216:CBFF:FEB0:AAE4 (0.0.0.0)
[0]     [IPv6 isLinkLocal isUnicast]
[0] #10 utun0 (utun0)
[0]      IPv6 FD00:6587:52D7:C2:216:CBFF:FEB0:AAE4 (0.0.0.0)
[0]     [IPv6 isUnicast]
[0] NetworkInterfaceTest end

・On Windows XP sp3

[0] NetworkInterfaceTest start
[0] #1 {14EF5D01-4C01-4C0B-8A79-0D4C7FDB7370} (AMD PCNET Family PCI Ethernet Adapter - パケット スケジューラ ミニポート)
[0]      IPv6 172.16.250.130 (0.0.0.0)
[0]     [IPv4 isIPv4Compatible isIPv4Mapped isSiteLocal isUnicast]
[0] #2 MS TCP Loopback interface (MS TCP Loopback interface)
[0]      IPv6 127.0.0.1 (0.0.0.0)
[0]     [IPv4 isIPv4Compatible isIPv4Mapped isLoopback isUnicast]
[0] NetworkInterfaceTest end

Downloads

ここをクリックすると、makefile や VC++ プロジェクトなど一式がダウンロードできます。
(2013.05.31 updated)
・2011年2月19日からのダウンロード数:744

Subversion

・フリーの Subversion ホスティングサービス Assemblaで、ソースコードを管理しています。

IPAddress.cppにバグ(PowerPC Mac など限定)

SourceForge にバグレポートを出しましたが、Poco の v1.4.1p1 (まで)では IPv6 有効でコンパイルすると、PowerPC Mac など Big_Endian な機器で IPAddress.cpp の次の method に不具合があります:

isLoopback()
isMulticast()
isLinkLocal()
isSiteLocal()
isIPv4Mapped()
isWellKnownMC()
isNodeLocalMC()
isLinkLocalMC()
isSiteLocalMC()
isOrgLocalMC()
isGlobalMC()

IPAddress.cpp を修正する patch ファイルを用意したので、該当する機器で使う方は、次の処理を行ってください:
– Patch ファイルのダウンロード:
 こちらのリンク先のファイルをダウンロードして、poco-1.4.1p1/Net/src/ に置きます。
(相対パスで書いてあるので注意。下記も同様)
– 下記を実行:

cd poco-1.4.1p1/Net/src/
patch < IPAddress.cpp.diff
cd poco-1.4.1p1
make
sudo make install
Powered by POCO Copyright © 2011 Round Square Inc. All rights reserved.


Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>