From 365eb085d9f53d289e6ed93ebb543c218d4a7db3 Mon Sep 17 00:00:00 2001 From: Philip Prindeville Date: Tue, 13 Nov 2012 19:01:53 -0700 Subject: [PATCH 1/4] Fix build issues for Linux. --- Net/src/Route_Linux.cpp | 9 +++++++-- build/config/Linux | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Net/src/Route_Linux.cpp b/Net/src/Route_Linux.cpp index 97c49c44e4..381392e76d 100644 --- a/Net/src/Route_Linux.cpp +++ b/Net/src/Route_Linux.cpp @@ -42,6 +42,9 @@ #include #include +namespace Poco { +namespace Net { + class RouteHelper { public: @@ -117,7 +120,7 @@ void RouteHelper::createRouteIPv4(Route::RouteList *routes, struct rt_container // route->setPriority(rt_stuff->priority); // no hops, usage, mtu, or age... - routes->push_back(route); + routes->push_back(*route); } } @@ -136,7 +139,7 @@ void RouteHelper::createRouteIPv6(Route::RouteList *routes, struct rt_container // route->setPriority(rt_stuff->priority); // no hops, usage, mtu, or age... - routes->push_back(route); + routes->push_back(*route); } } @@ -330,3 +333,5 @@ Route::RouteList Route::list(IPAddress::Family family) return routes; } + +}} // namespace Poco::Net diff --git a/build/config/Linux b/build/config/Linux index 4781a12f8e..df18fdf345 100644 --- a/build/config/Linux +++ b/build/config/Linux @@ -60,6 +60,9 @@ RELEASEOPT_CC = -O2 -DNDEBUG RELEASEOPT_CXX = -O2 -DNDEBUG RELEASEOPT_LINK = -O2 +# needed to build libmnl +CXXFLAGS += -std=c++0x + # # System Specific Flags # From 50e23eaf2a475854771613ed10010dc255008c27 Mon Sep 17 00:00:00 2001 From: Philip Prindeville Date: Tue, 13 Nov 2012 19:03:31 -0700 Subject: [PATCH 2/4] Fix getDefaultAddress(es) for multi-homed hosts. --- Net/include/Poco/Net/Route.h | 4 ++-- Net/src/Route.cpp | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Net/include/Poco/Net/Route.h b/Net/include/Poco/Net/Route.h index 8043ba90c4..6b3484fa25 100644 --- a/Net/include/Poco/Net/Route.h +++ b/Net/include/Poco/Net/Route.h @@ -206,8 +206,8 @@ class Net_API Route static RouteList match(IPAddress target); /// Retruns routes matching target. - static const IPAddress getDefaultAddress(IPAddress::Family family); - /// Returns default IP address for the family. + static IPAddress::List getDefaultAddresses(IPAddress::Family family); + /// Returns default IP addresses for the family. static std::string protocolName(RouteProto proto); /// Returns protocol as string. diff --git a/Net/src/Route.cpp b/Net/src/Route.cpp index 3bfbf8ccaf..0782ee8ba3 100644 --- a/Net/src/Route.cpp +++ b/Net/src/Route.cpp @@ -207,18 +207,20 @@ Route::RouteList Route::match(IPAddress target) } -const IPAddress Route::getDefaultAddress(IPAddress::Family family) +IPAddress::List Route::getDefaultAddresses(IPAddress::Family family) { Route::RouteList routes = Route::defaults(family); + IPAddress::List addresses; - if (! routes.empty()) { - Route::RouteList::const_iterator it = routes.begin(); - + RouteList::const_iterator it = routes.begin(); + RouteList::const_iterator end = routes.end(); + for (; it != end; ++it) { IPAddress addr = it->getNetworkInterface().firstAddress(family); - return addr; + + addresses.push_back(addr); } - return IPAddress::wildcard(family); + return addresses; } From 06780a475f01d64a6240cc67e40af753f26b4c87 Mon Sep 17 00:00:00 2001 From: Philip Prindeville Date: Tue, 13 Nov 2012 19:46:58 -0700 Subject: [PATCH 3/4] Fix leak. --- Net/Makefile | 2 ++ Net/src/Route_Linux.cpp | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Net/Makefile b/Net/Makefile index f27a222679..c7bdbfa9e0 100644 --- a/Net/Makefile +++ b/Net/Makefile @@ -37,4 +37,6 @@ target = PocoNet target_version = $(LIBVERSION) target_libs = PocoFoundation +SYSLIBS += -lmnl + include $(POCO_BASE)/build/rules/lib diff --git a/Net/src/Route_Linux.cpp b/Net/src/Route_Linux.cpp index 381392e76d..9e295bdf1e 100644 --- a/Net/src/Route_Linux.cpp +++ b/Net/src/Route_Linux.cpp @@ -108,7 +108,7 @@ static Route::RouteProto xlateProto(unsigned prot) void RouteHelper::createRouteIPv4(Route::RouteList *routes, struct rt_container *rt_stuff) { if (rt_stuff->table == RT_TABLE_MAIN && rt_stuff->type <= RTN_MULTICAST) { - Route *route; + Route* route; if (rt_stuff->gw.in4.s_addr != 0) route = new Route(IPAddress(&rt_stuff->dest.in4, sizeof(rt_stuff->dest.in4)), IPAddress(rt_stuff->prefix, IPAddress::IPv4), IPAddress(&rt_stuff->gw.in4, sizeof(rt_stuff->gw.in4)), rt_stuff->oif, Route::ROUTE_INDIRECT); @@ -121,13 +121,15 @@ void RouteHelper::createRouteIPv4(Route::RouteList *routes, struct rt_container // no hops, usage, mtu, or age... routes->push_back(*route); + + delete route; } } void RouteHelper::createRouteIPv6(Route::RouteList *routes, struct rt_container *rt_stuff) { if (rt_stuff->table == RT_TABLE_MAIN && rt_stuff->type <= RTN_MULTICAST) { - Route *route; + Route* route; if (!in6zero(rt_stuff->gw.in6)) route = new Route(IPAddress(&rt_stuff->dest.in6, sizeof(rt_stuff->dest.in6)), IPAddress(rt_stuff->prefix, IPAddress::IPv6), IPAddress(&rt_stuff->gw.in6, sizeof(rt_stuff->gw.in6), rt_stuff->oif), rt_stuff->oif, Route::ROUTE_INDIRECT); @@ -140,6 +142,8 @@ void RouteHelper::createRouteIPv6(Route::RouteList *routes, struct rt_container // no hops, usage, mtu, or age... routes->push_back(*route); + + delete route; } } From c5b69f2da3bef87c3ec11e3702b91caeaf6163b5 Mon Sep 17 00:00:00 2001 From: Philip Prindeville Date: Tue, 13 Nov 2012 20:04:45 -0700 Subject: [PATCH 4/4] Add samples for 'interfaces' and 'routes'. --- Net/samples/Makefile | 2 + Net/samples/interfaces/Makefile | 17 ++++ Net/samples/interfaces/src/interfaces.cpp | 96 +++++++++++++++++++++++ Net/samples/routes/Makefile | 21 +++++ Net/samples/routes/src/routes.cpp | 53 +++++++++++++ 5 files changed, 189 insertions(+) create mode 100644 Net/samples/interfaces/Makefile create mode 100644 Net/samples/interfaces/src/interfaces.cpp create mode 100644 Net/samples/routes/Makefile create mode 100644 Net/samples/routes/src/routes.cpp diff --git a/Net/samples/Makefile b/Net/samples/Makefile index b4daacab81..90fb20878a 100644 --- a/Net/samples/Makefile +++ b/Net/samples/Makefile @@ -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) diff --git a/Net/samples/interfaces/Makefile b/Net/samples/interfaces/Makefile new file mode 100644 index 0000000000..2b3caaa97e --- /dev/null +++ b/Net/samples/interfaces/Makefile @@ -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 diff --git a/Net/samples/interfaces/src/interfaces.cpp b/Net/samples/interfaces/src/interfaces.cpp new file mode 100644 index 0000000000..42b3d47858 --- /dev/null +++ b/Net/samples/interfaces/src/interfaces.cpp @@ -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 +#include + +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().toString(); + IPAddress addr; + addr = ipIt->get(); + if (!addr.isWildcard()) std::cout << '/' << addr.toString() << " (" << addr.prefixLength() << ')'; + addr = ipIt->get(); + 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; +} diff --git a/Net/samples/routes/Makefile b/Net/samples/routes/Makefile new file mode 100644 index 0000000000..70adee9348 --- /dev/null +++ b/Net/samples/routes/Makefile @@ -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 diff --git a/Net/samples/routes/src/routes.cpp b/Net/samples/routes/src/routes.cpp new file mode 100644 index 0000000000..8313fb1a6d --- /dev/null +++ b/Net/samples/routes/src/routes.cpp @@ -0,0 +1,53 @@ +#include "Poco/Path.h" +#include + +#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