2006-01-17 22:56:15 +00:00
|
|
|
package net.i2p.data.i2np;
|
|
|
|
|
2008-07-16 13:42:54 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2006-01-17 22:56:15 +00:00
|
|
|
import net.i2p.I2PAppContext;
|
2008-07-16 13:42:54 +00:00
|
|
|
import net.i2p.data.ByteArray;
|
2006-01-17 22:56:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transmitted from the new outbound endpoint to the creator through a
|
|
|
|
* reply tunnel
|
|
|
|
*/
|
|
|
|
public class TunnelBuildReplyMessage extends I2NPMessageImpl {
|
2010-01-29 19:22:10 +00:00
|
|
|
protected ByteArray _records[];
|
|
|
|
protected int RECORD_COUNT;
|
|
|
|
public static final int MAX_RECORD_COUNT = TunnelBuildMessage.MAX_RECORD_COUNT;
|
2006-01-17 22:56:15 +00:00
|
|
|
|
|
|
|
public static final int MESSAGE_TYPE = 22;
|
|
|
|
|
|
|
|
public TunnelBuildReplyMessage(I2PAppContext context) {
|
2010-01-29 19:22:10 +00:00
|
|
|
this(context, MAX_RECORD_COUNT);
|
|
|
|
}
|
|
|
|
|
2010-03-15 14:34:25 +00:00
|
|
|
/** @since 0.7.12 */
|
2010-01-29 19:22:10 +00:00
|
|
|
protected TunnelBuildReplyMessage(I2PAppContext context, int records) {
|
2006-01-17 22:56:15 +00:00
|
|
|
super(context);
|
2010-01-29 19:22:10 +00:00
|
|
|
if (records > 0) {
|
|
|
|
RECORD_COUNT = records;
|
|
|
|
_records = new ByteArray[records];
|
|
|
|
}
|
|
|
|
// else will be initialized by readMessage() in VTBRM
|
2006-01-17 22:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setRecord(int index, ByteArray record) { _records[index] = record; }
|
|
|
|
public ByteArray getRecord(int index) { return _records[index]; }
|
2010-03-15 14:34:25 +00:00
|
|
|
/** @since 0.7.12 */
|
2010-01-29 19:22:10 +00:00
|
|
|
public int getRecordCount() { return RECORD_COUNT; }
|
2006-01-17 22:56:15 +00:00
|
|
|
|
|
|
|
public static final int RECORD_SIZE = TunnelBuildMessage.RECORD_SIZE;
|
|
|
|
|
|
|
|
protected int calculateWrittenLength() { return RECORD_SIZE * RECORD_COUNT; }
|
|
|
|
public int getType() { return MESSAGE_TYPE; }
|
|
|
|
public void readMessage(byte[] data, int offset, int dataSize, int type) throws I2NPMessageException, IOException {
|
|
|
|
if (type != MESSAGE_TYPE)
|
|
|
|
throw new I2NPMessageException("Message type is incorrect for this message");
|
|
|
|
if (dataSize != calculateWrittenLength())
|
|
|
|
throw new I2NPMessageException("Wrong length (expects " + calculateWrittenLength() + ", recv " + dataSize + ")");
|
|
|
|
|
|
|
|
for (int i = 0; i < RECORD_COUNT; i++) {
|
|
|
|
int off = offset + (i * RECORD_SIZE);
|
|
|
|
int len = RECORD_SIZE;
|
2006-02-15 05:33:17 +00:00
|
|
|
byte rec[] = new byte[RECORD_SIZE];
|
|
|
|
System.arraycopy(data, off, rec, 0, RECORD_SIZE);
|
|
|
|
setRecord(i, new ByteArray(rec));
|
|
|
|
//setRecord(i, new ByteArray(data, off, len));
|
2006-01-17 22:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int writeMessageBody(byte[] out, int curIndex) throws I2NPMessageException {
|
|
|
|
int remaining = out.length - (curIndex + calculateWrittenLength());
|
2006-02-15 05:33:17 +00:00
|
|
|
if (remaining < 0)
|
2006-01-17 22:56:15 +00:00
|
|
|
throw new I2NPMessageException("Not large enough (too short by " + remaining + ")");
|
|
|
|
for (int i = 0; i < RECORD_COUNT; i++) {
|
|
|
|
System.arraycopy(_records[i].getData(), _records[i].getOffset(), out, curIndex, RECORD_SIZE);
|
|
|
|
curIndex += RECORD_SIZE;
|
|
|
|
}
|
|
|
|
return curIndex;
|
|
|
|
}
|
2010-01-29 19:22:10 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "[TunnelBuildReplyMessage]";
|
|
|
|
}
|
2006-01-17 22:56:15 +00:00
|
|
|
}
|