forked from I2P_Developers/i2p.i2p
* SendMessageOptions: Increase tag fields to 4 bits and use
table lookup for more flexibility * Streaming: Use packet type and current window size to adjust number of tags sent and tag threshold, to improve efficiency and reliability
This commit is contained in:
@ -21,39 +21,32 @@ public class SendMessageOptions extends DateAndFlags {
|
||||
/**
|
||||
* 1 means don't send, 0 means default
|
||||
*/
|
||||
private static final int LS_MASK = 0x0001;
|
||||
private static final int LS_MASK = 0x0100;
|
||||
|
||||
/**
|
||||
* Tags to send field:
|
||||
*<pre>
|
||||
* 000 - default
|
||||
* 001 - 2
|
||||
* 010 - 4
|
||||
* 011 - 8
|
||||
* 100 - 16
|
||||
* 101 - 32
|
||||
* 110 - 64
|
||||
* 111 - 128
|
||||
*</pre>
|
||||
* see below for possible values
|
||||
*/
|
||||
private static final int TAGS_SEND_MASK = 0x000e;
|
||||
private static final int MAX_SEND_TAGS = 128;
|
||||
private static final int TAGS_SEND_MASK = 0x000f;
|
||||
/**
|
||||
* Possible values. Configured values will be rounded down.
|
||||
* Note that ElGamalAESEngine enforces a max of 200 on receive.
|
||||
*/
|
||||
private static final int[] TAGS_SEND = {
|
||||
0, 2, 4, 6, 8, 12, 16, 24,
|
||||
32, 40, 51, 64, 80, 100, 125, 160
|
||||
};
|
||||
|
||||
/**
|
||||
* Tags threshold field:
|
||||
*<pre>
|
||||
* 000 - default
|
||||
* 001 - 1
|
||||
* 010 - 2
|
||||
* 011 - 4
|
||||
* 100 - 8
|
||||
* 101 - 16
|
||||
* 110 - 32
|
||||
* 111 - 64
|
||||
*</pre>
|
||||
* see below for possible values
|
||||
*/
|
||||
private static final int TAGS_REQD_MASK = 0x0070;
|
||||
private static final int MAX_REQD_TAGS = 64;
|
||||
private static final int TAGS_REQD_MASK = 0x00f0;
|
||||
/** Possible values. Configured values will be rounded down. */
|
||||
private static final int[] TAGS_REQD = {
|
||||
0, 2, 3, 6, 9, 14, 20, 27,
|
||||
35, 45, 57, 72, 92, 117, 147, 192
|
||||
};
|
||||
|
||||
/** default true */
|
||||
public void setSendLeaseSet(boolean yes) {
|
||||
@ -76,19 +69,19 @@ public class SendMessageOptions extends DateAndFlags {
|
||||
/**
|
||||
* If we are low on tags, send this many.
|
||||
* Power of 2 recommended - rounds down.
|
||||
* default 0, meaning unset
|
||||
* default 0, meaning unset, use the SKM config (default 40)
|
||||
* @param tags 0 or 2 to 128
|
||||
*/
|
||||
public void setTagsToSend(int tags) {
|
||||
if (tags < 0)
|
||||
throw new IllegalArgumentException();
|
||||
_flags &= ~TAGS_SEND_MASK;
|
||||
_flags |= linToExp(Math.min(tags, MAX_SEND_TAGS) / 2) << 1;
|
||||
_flags |= valToCode(tags, TAGS_SEND);
|
||||
}
|
||||
|
||||
/**
|
||||
* If we are low on tags, send this many.
|
||||
* @return default 0, meaning unset
|
||||
* @return default 0, meaning unset, use the SKM config (default 40)
|
||||
*/
|
||||
public int getTagsToSend() {
|
||||
return getTagsToSend(_flags);
|
||||
@ -96,29 +89,29 @@ public class SendMessageOptions extends DateAndFlags {
|
||||
|
||||
/**
|
||||
* If we are low on tags, send this many.
|
||||
* @return default 0, meaning unset
|
||||
* @return default 0, meaning unset, use the SKM config (default 40)
|
||||
*/
|
||||
public static int getTagsToSend(int flags) {
|
||||
int exp = (flags & TAGS_SEND_MASK) >> 1;
|
||||
return 2 * expToLin(exp);
|
||||
int exp = (flags & TAGS_SEND_MASK);
|
||||
return codeToVal(exp, TAGS_SEND);
|
||||
}
|
||||
|
||||
/**
|
||||
* Low tag threshold. If less than this many, send more.
|
||||
* Power of 2 recommended - rounds down.
|
||||
* default 0, meaning unset
|
||||
* @param tags 0 to 64
|
||||
* default 0, meaning unset, use the SKM config (default 30)
|
||||
* @param tags 0 to 90
|
||||
*/
|
||||
public void setTagThreshold(int tags) {
|
||||
if (tags < 0)
|
||||
throw new IllegalArgumentException();
|
||||
_flags &= ~TAGS_REQD_MASK;
|
||||
_flags |= linToExp(Math.min(tags, MAX_REQD_TAGS)) << 4;
|
||||
_flags |= valToCode(tags, TAGS_REQD) << 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Low tag threshold. If less than this many, send more.
|
||||
* @return default 0, meaning unset
|
||||
* @return default 0, meaning unset, use the SKM config (default 30)
|
||||
*/
|
||||
public int getTagThreshold() {
|
||||
return getTagThreshold(_flags);
|
||||
@ -126,26 +119,26 @@ public class SendMessageOptions extends DateAndFlags {
|
||||
|
||||
/**
|
||||
* Low tag threshold. If less than this many, send more.
|
||||
* @return default 0, meaning unset
|
||||
* @return default 0, meaning unset, use the SKM config (default 30)
|
||||
*/
|
||||
public static int getTagThreshold(int flags) {
|
||||
int exp = (flags & TAGS_REQD_MASK) >> 4;
|
||||
return expToLin(exp);
|
||||
return codeToVal(exp, TAGS_REQD);
|
||||
}
|
||||
|
||||
/** rounds down */
|
||||
private static int linToExp(int lin) {
|
||||
int exp = 0;
|
||||
while (lin > 0) {
|
||||
exp++;
|
||||
lin >>= 1;
|
||||
private static int valToCode(int val, int[] codes) {
|
||||
// special case, round up so we don't turn it into default
|
||||
if (val > 0 && val <= codes[1])
|
||||
return 1;
|
||||
for (int i = 1; i < codes.length; i++) {
|
||||
if (val < codes[i])
|
||||
return i - 1;
|
||||
}
|
||||
return exp;
|
||||
return codes.length - 1;
|
||||
}
|
||||
|
||||
private static int expToLin(int exp) {
|
||||
if (exp <= 0)
|
||||
return 0;
|
||||
return 1 << (exp - 1);
|
||||
private static int codeToVal(int code, int[] codes) {
|
||||
return codes[code];
|
||||
}
|
||||
}
|
||||
|
@ -38,6 +38,8 @@ public class ElGamalAESEngine {
|
||||
private final Log _log;
|
||||
private final static int MIN_ENCRYPTED_SIZE = 80; // smallest possible resulting size
|
||||
private final I2PAppContext _context;
|
||||
/** enforced since release 0.6 */
|
||||
public static final int MAX_TAGS_RECEIVED = 200;
|
||||
|
||||
public ElGamalAESEngine(I2PAppContext ctx) {
|
||||
_context = ctx;
|
||||
@ -328,7 +330,7 @@ public class ElGamalAESEngine {
|
||||
//ByteArrayInputStream bais = new ByteArrayInputStream(decrypted);
|
||||
int cur = 0;
|
||||
long numTags = DataHelper.fromLong(decrypted, cur, 2);
|
||||
if ((numTags < 0) || (numTags > 200)) throw new Exception("Invalid number of session tags");
|
||||
if ((numTags < 0) || (numTags > MAX_TAGS_RECEIVED)) throw new Exception("Invalid number of session tags");
|
||||
if (numTags > 0) tags = new ArrayList((int)numTags);
|
||||
cur += 2;
|
||||
//_log.debug("# tags: " + numTags);
|
||||
|
Reference in New Issue
Block a user