|
32 | 32 | import java.util.List; |
33 | 33 | import java.util.Random; |
34 | 34 | import java.util.Set; |
| 35 | +import java.util.SortedSet; |
35 | 36 | import java.util.StringTokenizer; |
36 | 37 | import java.util.TreeSet; |
37 | 38 | import java.util.regex.Matcher; |
@@ -651,39 +652,48 @@ public static Set<Long> getAllIpsFromCidr(String cidr, long size) { |
651 | 652 | * @param avoid set of ips to avoid |
652 | 653 | * @return ip that is within the cidr range but not in the avoid set. -1 if unable to find one. |
653 | 654 | */ |
654 | | - public static long getRandomIpFromCidr(String startIp, int size, Set<Long> avoid) { |
| 655 | + public static long getRandomIpFromCidr(String startIp, int size, SortedSet<Long> avoid) { |
655 | 656 | return getRandomIpFromCidr(ip2Long(startIp), size, avoid); |
656 | 657 |
|
657 | 658 | } |
658 | 659 |
|
659 | 660 | /** |
660 | 661 | * Given a cidr, this method returns an ip address within the range but |
661 | | - * is not in the avoid list. |
| 662 | + * is not in the avoid list. |
| 663 | + * Note: the gateway address has to be specified in the avoid list |
662 | 664 | * |
663 | | - * @param startIp ip that the cidr starts with |
| 665 | + * @param cidr ip that the cidr starts with |
664 | 666 | * @param size size of the cidr |
665 | 667 | * @param avoid set of ips to avoid |
666 | 668 | * @return ip that is within the cidr range but not in the avoid set. -1 if unable to find one. |
667 | 669 | */ |
668 | | - public static long getRandomIpFromCidr(long cidr, int size, Set<Long> avoid) { |
| 670 | + public static long getRandomIpFromCidr(long cidr, int size, SortedSet<Long> avoid) { |
669 | 671 | assert (size < 32) : "You do know this is not for ipv6 right? Keep it smaller than 32 but you have " + size; |
670 | 672 |
|
671 | 673 | long startNetMask = ip2Long(getCidrNetmask(size)); |
672 | | - long startIp = (cidr & startNetMask) + 2; |
673 | | - int range = 1 << (32 - size); |
| 674 | + long startIp = (cidr & startNetMask) + 1; //exclude the first ip since it isnt valid, e.g., 192.168.10.0 |
| 675 | + int range = 1 << (32 - size); //e.g., /24 = 2^8 = 256 |
| 676 | + range = range -1; //exclude end of the range since that is the broadcast address, e.g., 192.168.10.255 |
674 | 677 |
|
675 | | - if (avoid.size() > range) { |
| 678 | + if (avoid.size() >= range) { |
676 | 679 | return -1; |
677 | 680 | } |
678 | | - |
679 | | - for (int i = 0; i < range; i++) { |
680 | | - int next = _rand.nextInt(range); |
681 | | - if (!avoid.contains(startIp + next)) { |
682 | | - return startIp + next; |
| 681 | + |
| 682 | + //Reduce the range by the size of the avoid set |
| 683 | + //e.g., cidr = 192.168.10.0, size = /24, avoid = 192.168.10.1, 192.168.10.20, 192.168.10.254 |
| 684 | + // range = 2^8 - 1 - 3 = 252 |
| 685 | + range = range - avoid.size(); |
| 686 | + int next = _rand.nextInt(range); //note: nextInt excludes last value |
| 687 | + long ip = startIp + next; |
| 688 | + for (Long avoidable : avoid) { |
| 689 | + if (ip >= avoidable) { |
| 690 | + ip++; |
| 691 | + } else { |
| 692 | + break; |
683 | 693 | } |
684 | 694 | } |
685 | 695 |
|
686 | | - return -1; |
| 696 | + return ip; |
687 | 697 | } |
688 | 698 |
|
689 | 699 | public static String getIpRangeStartIpFromCidr(String cidr, long size) { |
|
0 commit comments