From bf917c27d11891f6373c1b82c9761bea9a02124e1c6048509f49261572470a2a Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Mon, 5 May 2025 12:12:45 -0400 Subject: [PATCH] Update the ipam code to support geofeed. --- Makefile | 4 +++- ipam.cc | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 4de309a..6dfa811 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,9 @@ CPPFLAGS+= -I . -g -O0 all: test ipam -test: ipam +test: + +run: ipam ./ipam clean: diff --git a/ipam.cc b/ipam.cc index 8d0ffb7..80769f0 100644 --- a/ipam.cc +++ b/ipam.cc @@ -229,6 +229,51 @@ try } ); + + add_command + ( + { + "geofeed", + "Describe a network's various physical locations.", + []( const std::vector< std::string > &args ) + { + if( args.empty() ) throw std::runtime_error{ "Requires one argument." }; + if( args.size() != 1 ) throw std::runtime_error{ "Too many arguments." }; + + const auto &network= boost::lexical_cast< IP_Address >( args.at( 0 ) ); + std::cout << "Parsed: `" << network << "` -- it has a " << network.prefix_length() + << " bit mask." << std::endl; + + const Allocation *p= nullptr; + + std::optional< IP_Address > found; + auto better= [&]( const auto &where ) + { + return not found.has_value() or where.is_subnet_of( found.value() ); + }; + + for( const auto &space: spaces ) + { + const auto &[ where, owner, name, purpose, country, state, city, postal ]= space; + if( network.is_subnet_of( where ) and better( where ) ) + { + found= where; + p = &space; + } + } + + const auto &[ _, owner, name, purpose, country, state, city, postal ]= *p; + + for( const auto &sub: p->subAllocations() ) + { + std::cout << sub.network << "," << sub.country << "," << sub.region + << "," << sub.city << "," << sub.postal + << std::endl; + } + } + } + ); + while( not std::cin.eof() )