Browse Source

eliminated cast to DatabaseStoreMsg

pull/131/head
orignal 10 years ago
parent
commit
fd9a8fd2b1
  1. 17
      Destination.cpp
  2. 23
      I2NPProtocol.cpp
  3. 10
      I2NPProtocol.h
  4. 13
      NetDb.cpp

17
Destination.cpp

@ -209,15 +209,14 @@ namespace client
void ClientDestination::HandleDatabaseStoreMessage (const uint8_t * buf, size_t len) void ClientDestination::HandleDatabaseStoreMessage (const uint8_t * buf, size_t len)
{ {
I2NPDatabaseStoreMsg msg; uint32_t replyToken = bufbe32toh (buf + DATABASE_STORE_REPLY_TOKEN_OFFSET);
memcpy(&msg,buf,sizeof (I2NPDatabaseStoreMsg)); size_t offset = DATABASE_STORE_HEADER_SIZE;
size_t offset = sizeof (I2NPDatabaseStoreMsg); if (replyToken) // TODO:
if (msg.replyToken) // TODO:
offset += 36; offset += 36;
if (msg.type == 1) // LeaseSet if (buf[DATABASE_STORE_TYPE_OFFSET] == 1) // LeaseSet
{ {
LogPrint (eLogDebug, "Remote LeaseSet"); LogPrint (eLogDebug, "Remote LeaseSet");
auto it = m_RemoteLeaseSets.find (msg.key); auto it = m_RemoteLeaseSets.find (buf + DATABASE_STORE_KEY_OFFSET);
if (it != m_RemoteLeaseSets.end ()) if (it != m_RemoteLeaseSets.end ())
{ {
it->second->Update (buf + offset, len - offset); it->second->Update (buf + offset, len - offset);
@ -226,13 +225,13 @@ namespace client
else else
{ {
LogPrint (eLogDebug, "New remote LeaseSet added"); LogPrint (eLogDebug, "New remote LeaseSet added");
m_RemoteLeaseSets[msg.key] = new i2p::data::LeaseSet (buf + offset, len - offset); m_RemoteLeaseSets[buf + DATABASE_STORE_KEY_OFFSET] = new i2p::data::LeaseSet (buf + offset, len - offset);
} }
} }
else else
LogPrint (eLogError, "Unexpected client's DatabaseStore type ", msg.type, ". Dropped"); LogPrint (eLogError, "Unexpected client's DatabaseStore type ", buf[DATABASE_STORE_TYPE_OFFSET], ". Dropped");
auto it1 = m_LeaseSetRequests.find (msg.key); auto it1 = m_LeaseSetRequests.find (buf + DATABASE_STORE_KEY_OFFSET);
if (it1 != m_LeaseSetRequests.end ()) if (it1 != m_LeaseSetRequests.end ())
{ {
it1->second->requestTimeoutTimer.cancel (); it1->second->requestTimeoutTimer.cancel ();

23
I2NPProtocol.cpp

@ -235,22 +235,22 @@ namespace i2p
router = &context.GetRouterInfo (); router = &context.GetRouterInfo ();
I2NPMessage * m = NewI2NPShortMessage (); I2NPMessage * m = NewI2NPShortMessage ();
I2NPDatabaseStoreMsg * msg = (I2NPDatabaseStoreMsg *)m->GetPayload (); uint8_t * payload = m->GetPayload ();
memcpy (msg->key, router->GetIdentHash (), 32); memcpy (payload + DATABASE_STORE_KEY_OFFSET, router->GetIdentHash (), 32);
msg->type = 0; payload[DATABASE_STORE_TYPE_OFFSET] = 0;
msg->replyToken = 0; htobe32buf (payload + DATABASE_STORE_REPLY_TOKEN_OFFSET, 0);
CryptoPP::Gzip compressor; CryptoPP::Gzip compressor;
compressor.Put (router->GetBuffer (), router->GetBufferLen ()); compressor.Put (router->GetBuffer (), router->GetBufferLen ());
compressor.MessageEnd(); compressor.MessageEnd();
auto size = compressor.MaxRetrievable (); auto size = compressor.MaxRetrievable ();
uint8_t * buf = m->GetPayload () + sizeof (I2NPDatabaseStoreMsg); uint8_t * buf = payload + DATABASE_STORE_HEADER_SIZE;
htobe16buf (buf, size); // size htobe16buf (buf, size); // size
buf += 2; buf += 2;
// TODO: check if size doesn't exceed buffer // TODO: check if size doesn't exceed buffer
compressor.Get (buf, size); compressor.Get (buf, size);
m->len += sizeof (I2NPDatabaseStoreMsg) + 2 + size; // payload size m->len += DATABASE_STORE_HEADER_SIZE + 2 + size; // payload size
FillI2NPMessageHeader (m, eI2NPDatabaseStore); FillI2NPMessageHeader (m, eI2NPDatabaseStore);
return m; return m;
@ -261,11 +261,10 @@ namespace i2p
if (!leaseSet) return nullptr; if (!leaseSet) return nullptr;
I2NPMessage * m = NewI2NPShortMessage (); I2NPMessage * m = NewI2NPShortMessage ();
uint8_t * payload = m->GetPayload (); uint8_t * payload = m->GetPayload ();
I2NPDatabaseStoreMsg * msg = (I2NPDatabaseStoreMsg *)payload; memcpy (payload + DATABASE_STORE_KEY_OFFSET, leaseSet->GetIdentHash (), 32);
memcpy (msg->key, leaseSet->GetIdentHash (), 32); payload[DATABASE_STORE_TYPE_OFFSET] = 1; // LeaseSet
msg->type = 1; // LeaseSet htobe32buf (payload + DATABASE_STORE_REPLY_TOKEN_OFFSET, replyToken);
msg->replyToken = htobe32 (replyToken); size_t size = DATABASE_STORE_HEADER_SIZE;
size_t size = sizeof (I2NPDatabaseStoreMsg);
if (replyToken) if (replyToken)
{ {
auto leases = leaseSet->GetNonExpiredLeases (); auto leases = leaseSet->GetNonExpiredLeases ();
@ -277,7 +276,7 @@ namespace i2p
size += 32; // reply tunnel gateway size += 32; // reply tunnel gateway
} }
else else
msg->replyToken = 0; htobe32buf (payload + DATABASE_STORE_REPLY_TOKEN_OFFSET, 0);
} }
memcpy (payload + size, leaseSet->GetBuffer (), leaseSet->GetBufferLen ()); memcpy (payload + size, leaseSet->GetBuffer (), leaseSet->GetBufferLen ());
size += leaseSet->GetBufferLen (); size += leaseSet->GetBufferLen ();

10
I2NPProtocol.h

@ -35,14 +35,20 @@ namespace i2p
const size_t DELIVERY_STATUS_TIMESTAMP_OFFSET = DELIVERY_STATUS_MSGID_OFFSET + 4; const size_t DELIVERY_STATUS_TIMESTAMP_OFFSET = DELIVERY_STATUS_MSGID_OFFSET + 4;
const size_t DELIVERY_STATUS_SIZE = DELIVERY_STATUS_TIMESTAMP_OFFSET + 8; const size_t DELIVERY_STATUS_SIZE = DELIVERY_STATUS_TIMESTAMP_OFFSET + 8;
// DatabaseStore
const size_t DATABASE_STORE_KEY_OFFSET = 0;
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_HEADER_SIZE = DATABASE_STORE_REPLY_TOKEN_OFFSET + 4;
#pragma pack (1) #pragma pack (1)
struct I2NPDatabaseStoreMsg /*struct I2NPDatabaseStoreMsg
{ {
uint8_t key[32]; uint8_t key[32];
uint8_t type; uint8_t type;
uint32_t replyToken; uint32_t replyToken;
}; };*/
struct I2NPBuildRequestRecordClearText struct I2NPBuildRequestRecordClearText
{ {

13
NetDb.cpp

@ -425,15 +425,14 @@ namespace data
{ {
const uint8_t * buf = m->GetPayload (); const uint8_t * buf = m->GetPayload ();
size_t len = m->GetSize (); size_t len = m->GetSize ();
I2NPDatabaseStoreMsg msg; uint32_t replyToken = bufbe32toh (buf + DATABASE_STORE_REPLY_TOKEN_OFFSET);
memcpy (&msg, buf, sizeof (I2NPDatabaseStoreMsg)); size_t offset = DATABASE_STORE_HEADER_SIZE;
size_t offset = sizeof (I2NPDatabaseStoreMsg); if (replyToken)
if (msg.replyToken)
offset += 36; offset += 36;
if (msg.type) if (buf[DATABASE_STORE_TYPE_OFFSET]) // type
{ {
LogPrint ("LeaseSet"); LogPrint ("LeaseSet");
AddLeaseSet (msg.key, buf + offset, len - offset, m->from); AddLeaseSet (buf + DATABASE_STORE_KEY_OFFSET, buf + offset, len - offset, m->from);
} }
else else
{ {
@ -451,7 +450,7 @@ namespace data
uint8_t uncompressed[2048]; uint8_t uncompressed[2048];
size_t uncomressedSize = decompressor.MaxRetrievable (); size_t uncomressedSize = decompressor.MaxRetrievable ();
decompressor.Get (uncompressed, uncomressedSize); decompressor.Get (uncompressed, uncomressedSize);
AddRouterInfo (msg.key, uncompressed, uncomressedSize); AddRouterInfo (buf + DATABASE_STORE_KEY_OFFSET, uncompressed, uncomressedSize);
} }
i2p::DeleteI2NPMessage (m); i2p::DeleteI2NPMessage (m);
} }

Loading…
Cancel
Save