diff --git a/Destination.cpp b/Destination.cpp index d61a3811..d670d915 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -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 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 (); diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index f03388b0..568cfea4 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -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 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 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 (); diff --git a/I2NPProtocol.h b/I2NPProtocol.h index bff61897..b2fcd495 100644 --- a/I2NPProtocol.h +++ b/I2NPProtocol.h @@ -34,15 +34,21 @@ namespace i2p const size_t DELIVERY_STATUS_MSGID_OFFSET = 0; 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 { diff --git a/NetDb.cpp b/NetDb.cpp index e9a2f0c1..ee7e2fa2 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -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 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); }