Earlier quoted context omitted.
CIDR blocks are your friend, at the firewall.
Know of a lib to manage them?
Not offhand. The Routeviews project is a useful way to turn them up, though.
http://www.routeviews.org/routeviews/
Particularly reverse DNS queries.