Skip to content

Commit

Permalink
generic tag introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jul 7, 2014
1 parent a01e221 commit 13fec9b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 57 deletions.
5 changes: 2 additions & 3 deletions Garlic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,7 @@ namespace garlic
uint8_t * buf = msg->GetPayload ();
uint32_t length = be32toh (*(uint32_t *)buf);
buf += 4;
std::string sessionTag((const char *)buf, 32);
auto it = m_SessionTags.find (sessionTag);
auto it = m_SessionTags.find (SessionTag(buf));
if (it != m_SessionTags.end ())
{
// existing session
Expand Down Expand Up @@ -340,7 +339,7 @@ namespace garlic
uint16_t tagCount = be16toh (*(uint16_t *)buf);
buf += 2;
for (int i = 0; i < tagCount; i++)
m_SessionTags[std::string ((const char *)(buf + i*32), 32)] = decryption;
m_SessionTags[SessionTag(buf + i*32)] = decryption;
buf += tagCount*32;
uint32_t payloadSize = be32toh (*(uint32_t *)buf);
if (payloadSize > len)
Expand Down
3 changes: 2 additions & 1 deletion Garlic.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ namespace garlic

private:

typedef i2p::data::Tag<32> SessionTag;
bool m_IsRunning;
std::thread * m_Thread;
i2p::util::Queue<I2NPMessage> m_Queue;
Expand All @@ -106,7 +107,7 @@ namespace garlic
std::map<uint32_t, GarlicRoutingSession *> m_CreatedSessions; // msgID -> session
// incoming session
std::list<i2p::crypto::CBCDecryption *> m_SessionDecryptions; // multiple tags refer to one decyption
std::map<std::string, i2p::crypto::CBCDecryption *> m_SessionTags; // tag -> decryption
std::map<SessionTag, i2p::crypto::CBCDecryption *> m_SessionTags; // tag -> decryption
};

extern GarlicRouting routing;
Expand Down
5 changes: 0 additions & 5 deletions Identity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ namespace data
return *this;
}

bool IdentHash::FromBase32(const std::string& s)
{
size_t count = Base32ToByteStream(s.c_str(), s.length(), m_Hash, sizeof(m_Hash));
return count == sizeof(m_Hash);
}

Keys CreateRandomKeys ()
{
Expand Down
70 changes: 36 additions & 34 deletions Identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,41 @@ namespace i2p
{
namespace data
{
class IdentHash;
template<int sz>
class Tag
{
public:

Tag (const uint8_t * buf) { memcpy (m_Buf, buf, sz); };
Tag (const Tag<sz>& ) = default;
#ifndef _WIN32 // FIXME!!! msvs 2013 can't compile it
Tag (Tag<sz>&& ) = default;
#endif
Tag () = default;

Tag<sz>& operator= (const Tag<sz>& ) = default;
#ifndef _WIN32
Tag<sz>& operator= (Tag<sz>&& ) = default;
#endif

uint8_t * operator()() { return m_Buf; };
const uint8_t * operator()() const { return m_Buf; };

operator uint8_t * () { return m_Buf; };
operator const uint8_t * () const { return m_Buf; };

bool operator== (const Tag<sz>& other) const { return !memcmp (m_Buf, other.m_Buf, sz); };
bool operator< (const Tag<sz>& other) const { return memcmp (m_Buf, other.m_Buf, sz) < 0; };

private:

union // 8 bytes alignment
{
uint8_t m_Buf[sz];
uint64_t ll[sz/8];
};
};
typedef Tag<32> IdentHash;

#pragma pack(1)

Expand Down Expand Up @@ -52,39 +86,7 @@ namespace data
};

#pragma pack()

class IdentHash
{
public:

IdentHash (const uint8_t * hash) { memcpy (m_Hash, hash, 32); };
IdentHash (const IdentHash& ) = default;
#ifndef _WIN32 // FIXME!!! msvs 2013 can't compile it
IdentHash (IdentHash&& ) = default;
#endif
IdentHash () = default;

IdentHash& operator= (const IdentHash& ) = default;
#ifndef _WIN32
IdentHash& operator= (IdentHash&& ) = default;
#endif

uint8_t * operator()() { return m_Hash; };
const uint8_t * operator()() const { return m_Hash; };

operator uint8_t * () { return m_Hash; };
operator const uint8_t * () const { return m_Hash; };

bool operator== (const IdentHash& other) const { return !memcmp (m_Hash, other.m_Hash, 32); };
bool operator< (const IdentHash& other) const { return memcmp (m_Hash, other.m_Hash, 32) < 0; };

bool FromBase32(const std::string&);

private:

uint8_t m_Hash[32];
};


Keys CreateRandomKeys ();
void CreateRandomDHKeysPair (DHKeysPair * keys); // for transport sessions

Expand Down
15 changes: 1 addition & 14 deletions SSU.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,7 @@ namespace ssu

private:

union IV
{
uint8_t buf[16];
uint64_t ll[2];

IV (const IV&) = default;
IV (const uint8_t * iv) { memcpy (buf, iv, 16); };
bool operator< (const IV& other) const
{
if (ll[0] != other.ll[0]) return ll[0] < other.ll[0];
return ll[1] < other.ll[1];
};
};

typedef i2p::data::Tag<16> IV;
friend class SSUData; // TODO: change in later
SSUServer& m_Server;
boost::asio::ip::udp::endpoint m_RemoteEndpoint;
Expand Down

0 comments on commit 13fec9b

Please sign in to comment.