Add samples for 'interfaces' and 'routes'.

This commit is contained in:
Philip Prindeville
2012-11-13 20:04:45 -07:00
parent 06780a475f
commit c5b69f2da3
5 changed files with 189 additions and 0 deletions

View File

@@ -15,10 +15,12 @@ projects:
$(MAKE) -C HTTPTimeServer $(MAKECMDGOALS)
$(MAKE) -C HTTPFormServer $(MAKECMDGOALS)
$(MAKE) -C HTTPLoadTest $(MAKECMDGOALS)
$(MAKE) -C interfaces $(MAKECMDGOALS)
$(MAKE) -C download $(MAKECMDGOALS)
$(MAKE) -C EchoServer $(MAKECMDGOALS)
$(MAKE) -C Mail $(MAKECMDGOALS)
$(MAKE) -C Ping $(MAKECMDGOALS)
$(MAKE) -C TwitterClient $(MAKECMDGOALS)
$(MAKE) -C WebSocketServer $(MAKECMDGOALS)
$(MAKE) -C routes $(MAKECMDGOALS)
$(MAKE) -C SMTPLogger $(MAKECMDGOALS)

View File

@@ -0,0 +1,17 @@
#
# Makefile
#
# $Id: //poco/Main/template/sample.make#4 $
#
# Makefile for Poco interfaces
#
include $(POCO_BASE)/build/rules/global
objects = interfaces
target = interfaces
target_version = 1
target_libs = PocoUtil PocoNet PocoFoundation
include $(POCO_BASE)/build/rules/exec

View File

@@ -0,0 +1,96 @@
//
// interfaces.cpp
//
// $Id: //poco/1.4/Net/samples/interfaces/src/interfaces.cpp#1 $
//
// This sample demonstrates the NetworkInterface class.
//
#include "Poco/Path.h"
#include "Poco/Exception.h"
#include "Poco/Net/IPAddress.h"
#include "Poco/Net/NetworkInterface.h"
#include <memory>
#include <iostream>
using Poco::Path;
using Poco::Exception;
using Poco::Net::IPAddress;
using Poco::Net::NetworkInterface;
int main(int argc, char** argv)
{
if (argc != 1)
{
Path p(argv[0]);
std::cerr << "usage: " << p.getBaseName() << std::endl;
return 1;
}
try
{
const NetworkInterface::Map map = NetworkInterface::map();
for ( NetworkInterface::Map::const_iterator it = map.begin();
it != map.end(); ++it) {
const NetworkInterface& intf = it->second;
std::string sep("");
std::cout << intf.name() << " [" << intf.index() << "]: ";
std::cout << "<";
if (intf.isUp()) {
std::cout << sep << "UP"; sep = ",";
}
if (intf.isRunning()) {
std::cout << sep << "RUNNING"; sep = ",";
}
if (intf.isLoopback()) {
std::cout << sep << "LOOPBACK"; sep = ",";
}
if (intf.isPointToPoint()) {
std::cout << sep << "P2P"; sep = ",";
}
if (intf.supportsIPv4()) {
std::cout << sep << "IPv4"; sep = ",";
}
if (intf.supportsIPv6()) {
std::cout << sep << "IPv6"; sep = ",";
}
if (intf.supportsBroadcast()) {
std::cout << sep << "BCAST"; sep = ",";
}
if (intf.supportsMulticast()) {
std::cout << sep << "MCAST"; sep = ",";
}
std::cout << sep << std::dec << intf.mtu(); sep = ",";
std::cout << ">" << std::endl;
const NetworkInterface::AddressList& ipList = intf.addressList();
NetworkInterface::AddressList::const_iterator ipIt = ipList.begin();
NetworkInterface::AddressList::const_iterator ipEnd = ipList.end();
for (; ipIt != ipEnd; ++ipIt) {
std::cout << " " << ipIt->get<NetworkInterface::IP_ADDRESS>().toString();
IPAddress addr;
addr = ipIt->get<NetworkInterface::SUBNET_MASK>();
if (!addr.isWildcard()) std::cout << '/' << addr.toString() << " (" << addr.prefixLength() << ')';
addr = ipIt->get<NetworkInterface::BROADCAST_ADDRESS>();
if (!addr.isWildcard()) std::cout << (intf.isPointToPoint() ? " dest " : " bcast ") << addr.toString();
std::cout << std::endl;
}
std::cout << std::endl;
}
}
catch (Exception& exc)
{
std::cerr << exc.displayText() << std::endl;
return 1;
}
return 0;
}

View File

@@ -0,0 +1,21 @@
#
# Makefile
#
# $Id: //poco/Main/template/sample.make#4 $
#
# Makefile for Poco routes
#
include $(POCO_BASE)/build/rules/global
ifeq ($(OSNAME),Linux)
SYSLIBS += -lmnl
endif
objects = routes
target = routes
target_version = 1
target_libs = PocoUtil PocoNet PocoFoundation
include $(POCO_BASE)/build/rules/exec

View File

@@ -0,0 +1,53 @@
#include "Poco/Path.h"
#include <iostream>
#include "Poco/Net/IPAddress.h"
#include "Poco/Net/Route.h"
#include "Poco/Net/NetworkInterface.h"
using Poco::Path;
using Poco::Net::IPAddress;
using Poco::Net::Route;
using Poco::Net::NetworkInterface;
void dumpRoutes(const Route::RouteList& routes)
{
for (Route::RouteList::const_iterator it = routes.begin();
it != routes.end(); it++) {
std::cout << (*it).getDest().toString() << '/' << (*it).getPrefix();
if ((*it).getType() == Route::ROUTE_INDIRECT)
std::cout << " via " << (*it).getNextHop().toString();
std::cout << " dev " << (*it).getNetworkInterface().name();
if ((*it).validProto())
std::cout << " proto " << Route::protocolName((*it).getProto());
if ((*it).validAge())
std::cout << " age " << (*it).getAge();
if ((*it).validMTU())
std::cout << " mtu " << (*it).getMTU();
std::cout << std::endl;
}
}
int main(int argc, char** argv)
{
if (argc != 1) {
Path p(argv[0]);
std::cerr << "usage: " << p.getBaseName() << std::endl;
return 1;
}
Route::RouteList routes = Route::list(IPAddress::IPv4);
dumpRoutes(routes);
#if defined(POCO_HAVE_IPv6)
routes = Route::list(IPAddress::IPv6);
dumpRoutes(routes);
#endif
return 0;
}
// vim:ts=4