eliminated cast to BuildResponse Record

This commit is contained in:
orignal 2015-01-03 09:47:24 -05:00
parent b58d58ef29
commit 8a293f45fa
3 changed files with 21 additions and 25 deletions

View file

@ -327,7 +327,7 @@ namespace i2p
i2p::crypto::ElGamalDecrypt (i2p::context.GetEncryptionPrivateKey (), records[i].encrypted, (uint8_t *)&clearText); i2p::crypto::ElGamalDecrypt (i2p::context.GetEncryptionPrivateKey (), records[i].encrypted, (uint8_t *)&clearText);
// replace record to reply // replace record to reply
I2NPBuildResponseRecord * reply = (I2NPBuildResponseRecord *)(records + i); uint8_t * reply = (uint8_t *)(records + i);
if (i2p::context.AcceptsTunnels ()) if (i2p::context.AcceptsTunnels ())
{ {
i2p::tunnel::TransitTunnel * transitTunnel = i2p::tunnel::TransitTunnel * transitTunnel =
@ -337,13 +337,14 @@ namespace i2p
clearText.layerKey, clearText.ivKey, clearText.layerKey, clearText.ivKey,
clearText.flag & 0x80, clearText.flag & 0x40); clearText.flag & 0x80, clearText.flag & 0x40);
i2p::tunnel::tunnels.AddTransitTunnel (transitTunnel); i2p::tunnel::tunnels.AddTransitTunnel (transitTunnel);
reply->ret = 0; reply[BUILD_RESPONSE_RECORD_RET_OFFSET] = 0;
} }
else else
reply->ret = 30; // always reject with bandwidth reason (30) reply[BUILD_RESPONSE_RECORD_RET_OFFSET] = 30; // always reject with bandwidth reason (30)
//TODO: fill filler //TODO: fill filler
CryptoPP::SHA256().CalculateDigest(reply->hash, reply->padding, sizeof (reply->padding) + 1); // + 1 byte of ret CryptoPP::SHA256().CalculateDigest(reply + BUILD_RESPONSE_RECORD_HASH_OFFSET,
reply + BUILD_RESPONSE_RECORD_PADDING_OFFSET, BUILD_RESPONSE_RECORD_PADDING_SIZE + 1); // + 1 byte of ret
// encrypt reply // encrypt reply
i2p::crypto::CBCEncryption encryption; i2p::crypto::CBCEncryption encryption;
for (int j = 0; j < num; j++) for (int j = 0; j < num; j++)

View file

@ -40,15 +40,17 @@ namespace i2p
const size_t DATABASE_STORE_TYPE_OFFSET = DATABASE_STORE_KEY_OFFSET + 32; const size_t DATABASE_STORE_TYPE_OFFSET = DATABASE_STORE_KEY_OFFSET + 32;
const size_t DATABASE_STORE_REPLY_TOKEN_OFFSET = DATABASE_STORE_TYPE_OFFSET + 1; const size_t DATABASE_STORE_REPLY_TOKEN_OFFSET = DATABASE_STORE_TYPE_OFFSET + 1;
const size_t DATABASE_STORE_HEADER_SIZE = DATABASE_STORE_REPLY_TOKEN_OFFSET + 4; const size_t DATABASE_STORE_HEADER_SIZE = DATABASE_STORE_REPLY_TOKEN_OFFSET + 4;
#pragma pack (1)
/*struct I2NPDatabaseStoreMsg // TunnelBuild
{ const size_t TUNNEL_BUILD_RECORD_SIZE = 528;
uint8_t key[32];
uint8_t type; // BuildResponseRecord
uint32_t replyToken; const size_t BUILD_RESPONSE_RECORD_HASH_OFFSET = 0;
};*/ const size_t BUILD_RESPONSE_RECORD_PADDING_OFFSET = 32;
const size_t BUILD_RESPONSE_RECORD_PADDING_SIZE = 495;
const size_t BUILD_RESPONSE_RECORD_RET_OFFSET = BUILD_RESPONSE_RECORD_PADDING_OFFSET + BUILD_RESPONSE_RECORD_PADDING_SIZE;
#pragma pack (1)
struct I2NPBuildRequestRecordClearText struct I2NPBuildRequestRecordClearText
{ {
@ -65,13 +67,6 @@ namespace i2p
uint32_t nextMessageID; uint32_t nextMessageID;
uint8_t filler[29]; uint8_t filler[29];
}; };
struct I2NPBuildResponseRecord
{
uint8_t hash[32];
uint8_t padding[495];
uint8_t ret;
};
struct I2NPBuildRequestRecordElGamalEncrypted struct I2NPBuildRequestRecordElGamalEncrypted
{ {

View file

@ -113,9 +113,9 @@ namespace tunnel
auto idx = hop1->recordIndex; auto idx = hop1->recordIndex;
if (idx >= 0 && idx < msg[0]) if (idx >= 0 && idx < msg[0])
{ {
uint8_t * record = msg + 1 + idx*sizeof (I2NPBuildResponseRecord); uint8_t * record = msg + 1 + idx*TUNNEL_BUILD_RECORD_SIZE;
decryption.SetIV (hop->replyIV); decryption.SetIV (hop->replyIV);
decryption.Decrypt(record, sizeof (I2NPBuildResponseRecord), record); decryption.Decrypt(record, TUNNEL_BUILD_RECORD_SIZE, record);
} }
else else
LogPrint ("Tunnel hop index ", idx, " is out of range"); LogPrint ("Tunnel hop index ", idx, " is out of range");
@ -128,10 +128,10 @@ namespace tunnel
hop = m_Config->GetFirstHop (); hop = m_Config->GetFirstHop ();
while (hop) while (hop)
{ {
I2NPBuildResponseRecord record; const uint8_t * record = msg + 1 + hop->recordIndex*TUNNEL_BUILD_RECORD_SIZE;
memcpy (&record, msg + 1 + hop->recordIndex*sizeof (I2NPBuildResponseRecord), sizeof (I2NPBuildResponseRecord)); uint8_t ret = record[BUILD_RESPONSE_RECORD_RET_OFFSET];
LogPrint ("Ret code=", (int)record.ret); LogPrint ("Ret code=", (int)ret);
if (record.ret) if (ret)
// if any of participants declined the tunnel is not established // if any of participants declined the tunnel is not established
established = false; established = false;
hop = hop->next; hop = hop->next;