]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
fix spare id random selection.
authorW.C.A. Wijngaards <wouter@nlnetlabs.nl>
Wed, 3 Jun 2020 12:23:06 +0000 (14:23 +0200)
committerW.C.A. Wijngaards <wouter@nlnetlabs.nl>
Wed, 3 Jun 2020 12:23:06 +0000 (14:23 +0200)
services/outside_network.c

index cfc467af17bd5031af9d128b8f78a1728a9b6c19..8710c637ae4eeb5c3cfc5465050487f74c7a20ca 100644 (file)
@@ -1589,12 +1589,6 @@ reuse_tcp_select_id(struct reuse_tcp* reuse, struct outside_network* outnet)
        int i;
        unsigned select, count, space;
        rbnode_type* node;
-       for(i = 0; i<try_random; i++) {
-               id = ((unsigned)ub_random(outnet->rnd)>>8) & 0xffff;
-               if(!reuse_tcp_by_id_find(reuse, id)) {
-                       return id;
-               }
-       }
 
        /* make really sure the tree is not empty */
        if(reuse->tree_by_id.count == 0) {
@@ -1602,6 +1596,14 @@ reuse_tcp_select_id(struct reuse_tcp* reuse, struct outside_network* outnet)
                return id;
        }
 
+       /* try to find random empty spots by picking them */
+       for(i = 0; i<try_random; i++) {
+               id = ((unsigned)ub_random(outnet->rnd)>>8) & 0xffff;
+               if(!reuse_tcp_by_id_find(reuse, id)) {
+                       return id;
+               }
+       }
+
        /* equally pick a random unused element from the tree that is
         * not in use.  Pick a the n-th index of an ununused number,
         * then loop over the empty spaces in the tree and find it */
@@ -1627,8 +1629,7 @@ reuse_tcp_select_id(struct reuse_tcp* reuse, struct outside_network* outnet)
                                space = nextid - curid - 1;
                                if(select < count + space) {
                                        /* here it is */
-                                       return curid + 1 + ub_random_max(
-                                               outnet->rnd, space);
+                                       return curid + 1 + (select - count);
                                }
                                count += space;
                        }
@@ -1642,9 +1643,7 @@ reuse_tcp_select_id(struct reuse_tcp* reuse, struct outside_network* outnet)
        node = rbtree_last(&reuse->tree_by_id);
        log_assert(node && node != RBTREE_NULL); /* tree not empty */
        curid = tree_by_id_get_id(node);
-       space = 0xffff - curid;
-       log_assert(select < count + space);
-       return curid + 1 + ub_random_max(outnet->rnd, space);
+       return curid + 1 + (select - count);
 }
 
 struct waiting_tcp*