Browse Source

use shared_ptr for NetDb's I2NPMessages

pull/195/head
orignal 10 years ago
parent
commit
2b797fcd54
  1. 5
      I2NPProtocol.cpp
  2. 2
      I2NPProtocol.h
  3. 21
      NetDb.cpp
  4. 6
      NetDb.h

5
I2NPProtocol.cpp

@ -36,6 +36,11 @@ namespace i2p
delete msg; delete msg;
} }
std::shared_ptr<I2NPMessage> ToSharedI2NPMessage (I2NPMessage * msg)
{
return std::shared_ptr<I2NPMessage>(msg, DeleteI2NPMessage);
}
static std::atomic<uint32_t> I2NPmsgID(0); // TODO: create class static std::atomic<uint32_t> I2NPmsgID(0); // TODO: create class
void FillI2NPMessageHeader (I2NPMessage * msg, I2NPMessageType msgType, uint32_t replyMsgID) void FillI2NPMessageHeader (I2NPMessage * msg, I2NPMessageType msgType, uint32_t replyMsgID)
{ {

2
I2NPProtocol.h

@ -196,6 +196,8 @@ namespace tunnel
I2NPMessage * NewI2NPShortMessage (); I2NPMessage * NewI2NPShortMessage ();
I2NPMessage * NewI2NPMessage (size_t len); I2NPMessage * NewI2NPMessage (size_t len);
void DeleteI2NPMessage (I2NPMessage * msg); void DeleteI2NPMessage (I2NPMessage * msg);
std::shared_ptr<I2NPMessage> ToSharedI2NPMessage (I2NPMessage * msg);
void FillI2NPMessageHeader (I2NPMessage * msg, I2NPMessageType msgType, uint32_t replyMsgID = 0); void FillI2NPMessageHeader (I2NPMessage * msg, I2NPMessageType msgType, uint32_t replyMsgID = 0);
void RenewI2NPMessageHeader (I2NPMessage * msg); void RenewI2NPMessageHeader (I2NPMessage * msg);
I2NPMessage * CreateI2NPMessage (I2NPMessageType msgType, const uint8_t * buf, int len, uint32_t replyMsgID = 0); I2NPMessage * CreateI2NPMessage (I2NPMessageType msgType, const uint8_t * buf, int len, uint32_t replyMsgID = 0);

21
NetDb.cpp

@ -102,18 +102,18 @@ namespace data
{ {
case eI2NPDatabaseStore: case eI2NPDatabaseStore:
LogPrint ("DatabaseStore"); LogPrint ("DatabaseStore");
HandleDatabaseStoreMsg (msg); HandleDatabaseStoreMsg (ToSharedI2NPMessage (msg));
break; break;
case eI2NPDatabaseSearchReply: case eI2NPDatabaseSearchReply:
LogPrint ("DatabaseSearchReply"); LogPrint ("DatabaseSearchReply");
HandleDatabaseSearchReplyMsg (msg); HandleDatabaseSearchReplyMsg (ToSharedI2NPMessage (msg));
break; break;
case eI2NPDatabaseLookup: case eI2NPDatabaseLookup:
LogPrint ("DatabaseLookup"); LogPrint ("DatabaseLookup");
HandleDatabaseLookupMsg (msg); HandleDatabaseLookupMsg (ToSharedI2NPMessage (msg));
break; break;
default: // WTF? default: // WTF?
LogPrint ("NetDb: unexpected message type ", msg->GetTypeID ()); LogPrint (eLogError, "NetDb: unexpected message type ", msg->GetTypeID ());
i2p::HandleI2NPMessage (msg); i2p::HandleI2NPMessage (msg);
} }
if (numMsgs > 100) break; if (numMsgs > 100) break;
@ -469,7 +469,7 @@ namespace data
} }
} }
void NetDb::HandleDatabaseStoreMsg (I2NPMessage * m) void NetDb::HandleDatabaseStoreMsg (std::shared_ptr<I2NPMessage> m)
{ {
const uint8_t * buf = m->GetPayload (); const uint8_t * buf = m->GetPayload ();
size_t len = m->GetSize (); size_t len = m->GetSize ();
@ -477,7 +477,6 @@ namespace data
if (ident.IsZero ()) if (ident.IsZero ())
{ {
LogPrint (eLogError, "Database store with zero ident. Dropped"); LogPrint (eLogError, "Database store with zero ident. Dropped");
i2p::DeleteI2NPMessage (m);
return; return;
} }
uint32_t replyToken = bufbe32toh (buf + DATABASE_STORE_REPLY_TOKEN_OFFSET); uint32_t replyToken = bufbe32toh (buf + DATABASE_STORE_REPLY_TOKEN_OFFSET);
@ -539,7 +538,6 @@ namespace data
if (size > 2048 || size > len - offset) if (size > 2048 || size > len - offset)
{ {
LogPrint ("Invalid RouterInfo length ", (int)size); LogPrint ("Invalid RouterInfo length ", (int)size);
i2p::DeleteI2NPMessage (m);
return; return;
} }
try try
@ -562,10 +560,9 @@ namespace data
LogPrint (eLogError, "DatabaseStore: ", ex.what ()); LogPrint (eLogError, "DatabaseStore: ", ex.what ());
} }
} }
i2p::DeleteI2NPMessage (m);
} }
void NetDb::HandleDatabaseSearchReplyMsg (I2NPMessage * msg) void NetDb::HandleDatabaseSearchReplyMsg (std::shared_ptr<I2NPMessage> msg)
{ {
uint8_t * buf = msg->GetPayload (); uint8_t * buf = msg->GetPayload ();
char key[48]; char key[48];
@ -652,18 +649,15 @@ namespace data
else else
LogPrint ("Bayan"); LogPrint ("Bayan");
} }
i2p::DeleteI2NPMessage (msg);
} }
void NetDb::HandleDatabaseLookupMsg (I2NPMessage * msg) void NetDb::HandleDatabaseLookupMsg (std::shared_ptr<I2NPMessage> msg)
{ {
uint8_t * buf = msg->GetPayload (); uint8_t * buf = msg->GetPayload ();
IdentHash ident (buf); IdentHash ident (buf);
if (ident.IsZero ()) if (ident.IsZero ())
{ {
LogPrint (eLogError, "DatabaseLookup for zero ident. Ignored"); LogPrint (eLogError, "DatabaseLookup for zero ident. Ignored");
i2p::DeleteI2NPMessage (msg);
return; return;
} }
char key[48]; char key[48];
@ -789,7 +783,6 @@ namespace data
else else
transports.SendMessage (buf+32, replyMsg); transports.SendMessage (buf+32, replyMsg);
} }
i2p::DeleteI2NPMessage (msg);
} }
void NetDb::Explore (int numDestinations) void NetDb::Explore (int numDestinations)

6
NetDb.h

@ -41,9 +41,9 @@ namespace data
void RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete = nullptr); void RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete = nullptr);
void HandleDatabaseStoreMsg (I2NPMessage * msg); void HandleDatabaseStoreMsg (std::shared_ptr<I2NPMessage> msg);
void HandleDatabaseSearchReplyMsg (I2NPMessage * msg); void HandleDatabaseSearchReplyMsg (std::shared_ptr<I2NPMessage> msg);
void HandleDatabaseLookupMsg (I2NPMessage * msg); void HandleDatabaseLookupMsg (std::shared_ptr<I2NPMessage> msg);
std::shared_ptr<const RouterInfo> GetRandomRouter () const; std::shared_ptr<const RouterInfo> GetRandomRouter () const;
std::shared_ptr<const RouterInfo> GetRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith) const; std::shared_ptr<const RouterInfo> GetRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith) const;

Loading…
Cancel
Save