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 @@ -209,15 +209,14 @@ namespace client
void ClientDestination::HandleDatabaseStoreMessage (const uint8_t * buf, size_t len)
{
I2NPDatabaseStoreMsg msg;
memcpy(&msg,buf,sizeof (I2NPDatabaseStoreMsg));
size_t offset = sizeof (I2NPDatabaseStoreMsg);
if (msg.replyToken) // TODO:
uint32_t replyToken = bufbe32toh (buf + DATABASE_STORE_REPLY_TOKEN_OFFSET);
size_t offset = DATABASE_STORE_HEADER_SIZE;
if (replyToken) // TODO:
offset += 36;
if (msg.type == 1) // LeaseSet
if (buf[DATABASE_STORE_TYPE_OFFSET] == 1) // 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 ())
{
it->second->Update (buf + offset, len - offset);
@ -226,13 +225,13 @@ namespace client @@ -226,13 +225,13 @@ namespace client
else
{
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
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 ())
{
it1->second->requestTimeoutTimer.cancel ();

23
I2NPProtocol.cpp

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

10
I2NPProtocol.h

@ -35,14 +35,20 @@ namespace i2p @@ -35,14 +35,20 @@ namespace i2p
const size_t DELIVERY_STATUS_TIMESTAMP_OFFSET = DELIVERY_STATUS_MSGID_OFFSET + 4;
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)
struct I2NPDatabaseStoreMsg
/*struct I2NPDatabaseStoreMsg
{
uint8_t key[32];
uint8_t type;
uint32_t replyToken;
};
};*/
struct I2NPBuildRequestRecordClearText
{

13
NetDb.cpp

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

Loading…
Cancel
Save