I'm looking for a good algorithm (or code if you speak it better than English) to do the following:
For a given IP range (e.g. 1.1.1.1 - 1.1.2.247), find the smallest combination of subnets/addresses that includes all the IPs in the specified range. Ignore broadcast, subnet 0 restrictions, and network class.
Examples:
- For 1.1.1.1 - 1.1.2.1 you'd get {1.1.1.1/24, 1.1.2.1} which is better/smaller than {1.1.1.1, 1.1.1.2, ..., 1.1.1.255, 1.1.2.1}
- For 1.1.1.12 - 1.1.1.31 you'd get {1.1.1.12/30, 1.1.1.16/28} which is better/smaller than {1.1.1.12, 1.1.1.13, 1.1.1.14, 1.1.1.15, 1.1.1.16/28}
For the curious, the use case is matching network traffic on an arbitrary range of source/destination IPs with the smallest number of flows, using the Openflow protocol. The need for this optimization arises from the fact that hardware switches/routers have limited space for these flow configurations, and it takes a relatively long time to program/modify.