diff --git a/services/outside_network.c b/services/outside_network.c index cfc467af1..8710c637a 100644 --- a/services/outside_network.c +++ b/services/outside_network.c @@ -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; irnd)>>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; irnd)>>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*