fix spare id random selection.

This commit is contained in:
W.C.A. Wijngaards 2020-06-03 14:23:06 +02:00
parent 150e1b0491
commit 7cc6a89e21

View 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*