Merge pull request #123 from klondi/master

Fix any remaning aliasing rules with propper memcpy wrappers
This commit is contained in:
orignal 2014-12-30 09:50:28 -05:00
commit ed82f388e6
11 changed files with 140 additions and 94 deletions

View File

@ -117,7 +117,7 @@ namespace datagram
compressor.MessageEnd(); compressor.MessageEnd();
int size = compressor.MaxRetrievable (); int size = compressor.MaxRetrievable ();
uint8_t * buf = msg->GetPayload (); uint8_t * buf = msg->GetPayload ();
*(uint32_t *)buf = htobe32 (size); // length htobe32buf (buf, size); // length
buf += 4; buf += 4;
compressor.Get (buf, size); compressor.Get (buf, size);
memset (buf + 4, 0, 4); // source and destination are zeroes memset (buf + 4, 0, 4); // source and destination are zeroes

View File

@ -138,7 +138,7 @@ namespace garlic
} }
// AES block // AES block
len += CreateAESBlock (buf, msg); len += CreateAESBlock (buf, msg);
*(uint32_t *)(m->GetPayload ()) = htobe32 (len); htobe32buf (m->GetPayload (), len);
m->len += len + 4; m->len += len + 4;
FillI2NPMessageHeader (m, eI2NPGarlic); FillI2NPMessageHeader (m, eI2NPGarlic);
if (msg) if (msg)
@ -151,7 +151,7 @@ namespace garlic
size_t blockSize = 0; size_t blockSize = 0;
bool createNewTags = m_Owner && m_NumTags && ((int)m_SessionTags.size () <= m_NumTags/2); bool createNewTags = m_Owner && m_NumTags && ((int)m_SessionTags.size () <= m_NumTags/2);
UnconfirmedTags * newTags = createNewTags ? GenerateSessionTags () : nullptr; UnconfirmedTags * newTags = createNewTags ? GenerateSessionTags () : nullptr;
*(uint16_t *)buf = newTags ? htobe16 (newTags->numTags) : 0; // tag count htobuf16 (buf, newTags ? htobe16 (newTags->numTags) : 0); // tag count
blockSize += 2; blockSize += 2;
if (newTags) // session tags recreated if (newTags) // session tags recreated
{ {
@ -220,9 +220,9 @@ namespace garlic
memset (payload + size, 0, 3); // certificate of message memset (payload + size, 0, 3); // certificate of message
size += 3; size += 3;
*(uint32_t *)(payload + size) = htobe32 (msgID); // MessageID htobe32buf (payload + size, msgID); // MessageID
size += 4; size += 4;
*(uint64_t *)(payload + size) = htobe64 (ts); // Expiration of message htobe64buf (payload + size, ts); // Expiration of message
size += 8; size += 8;
return size; return size;
} }
@ -246,9 +246,9 @@ namespace garlic
memcpy (buf + size, msg->GetBuffer (), msg->GetLength ()); memcpy (buf + size, msg->GetBuffer (), msg->GetLength ());
size += msg->GetLength (); size += msg->GetLength ();
*(uint32_t *)(buf + size) = htobe32 (m_Rnd.GenerateWord32 ()); // CloveID htobe32buf (buf + size, m_Rnd.GenerateWord32 ()); // CloveID
size += 4; size += 4;
*(uint64_t *)(buf + size) = htobe64 (ts); // Expiration of clove htobe64buf (buf + size, ts); // Expiration of clove
size += 8; size += 8;
memset (buf + size, 0, 3); // certificate of clove memset (buf + size, 0, 3); // certificate of clove
size += 3; size += 3;
@ -269,7 +269,7 @@ namespace garlic
// hash and tunnelID sequence is reversed for Garlic // hash and tunnelID sequence is reversed for Garlic
memcpy (buf + size, leases[i].tunnelGateway, 32); // To Hash memcpy (buf + size, leases[i].tunnelGateway, 32); // To Hash
size += 32; size += 32;
*(uint32_t *)(buf + size) = htobe32 (leases[i].tunnelID); // tunnelID htobe32buf (buf + size, leases[i].tunnelID); // tunnelID
size += 4; size += 4;
// create msg // create msg
I2NPMessage * msg = CreateDeliveryStatusMsg (msgID); I2NPMessage * msg = CreateDeliveryStatusMsg (msgID);
@ -288,9 +288,9 @@ namespace garlic
DeleteI2NPMessage (msg); DeleteI2NPMessage (msg);
// fill clove // fill clove
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec
*(uint32_t *)(buf + size) = htobe32 (m_Rnd.GenerateWord32 ()); // CloveID htobe32buf (buf + size, m_Rnd.GenerateWord32 ()); // CloveID
size += 4; size += 4;
*(uint64_t *)(buf + size) = htobe64 (ts); // Expiration of clove htobe64buf (buf + size, ts); // Expiration of clove
size += 8; size += 8;
memset (buf + size, 0, 3); // certificate of clove memset (buf + size, 0, 3); // certificate of clove
size += 3; size += 3;

View File

@ -115,7 +115,7 @@ namespace i2p
if (replyTunnelID) if (replyTunnelID)
{ {
*buf = encryption ? 0x03: 0x01; // set delivery flag *buf = encryption ? 0x03: 0x01; // set delivery flag
*(uint32_t *)(buf+1) = htobe32 (replyTunnelID); htobe32buf (buf+1, replyTunnelID);
buf += 5; buf += 5;
} }
else else
@ -127,7 +127,7 @@ namespace i2p
if (exploratory) if (exploratory)
{ {
*(uint16_t *)buf = htobe16 (1); // one exlude record htobe16buf (buf,1); // one exlude record
buf += 2; buf += 2;
// reply with non-floodfill routers only // reply with non-floodfill routers only
memset (buf, 0, 32); memset (buf, 0, 32);
@ -138,7 +138,7 @@ namespace i2p
if (excludedPeers) if (excludedPeers)
{ {
int cnt = excludedPeers->size (); int cnt = excludedPeers->size ();
*(uint16_t *)buf = htobe16 (cnt); htobe16buf (buf, cnt);
buf += 2; buf += 2;
for (auto& it: *excludedPeers) for (auto& it: *excludedPeers)
{ {
@ -149,7 +149,7 @@ namespace i2p
else else
{ {
// nothing to exclude // nothing to exclude
*(uint16_t *)buf = htobe16 (0); htobuf16 (buf, 0);
buf += 2; buf += 2;
} }
} }
@ -210,7 +210,7 @@ namespace i2p
compressor.MessageEnd(); compressor.MessageEnd();
auto size = compressor.MaxRetrievable (); auto size = compressor.MaxRetrievable ();
uint8_t * buf = m->GetPayload () + sizeof (I2NPDatabaseStoreMsg); uint8_t * buf = m->GetPayload () + sizeof (I2NPDatabaseStoreMsg);
*(uint16_t *)buf = htobe16 (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);
@ -235,7 +235,7 @@ namespace i2p
auto leases = leaseSet->GetNonExpiredLeases (); auto leases = leaseSet->GetNonExpiredLeases ();
if (leases.size () > 0) if (leases.size () > 0)
{ {
*(uint32_t *)(payload + size) = htobe32 (leases[0].tunnelID); htobe32buf (payload + size, leases[0].tunnelID);
size += 4; // reply tunnelID size += 4; // reply tunnelID
memcpy (payload + size, leases[0].tunnelGateway, 32); memcpy (payload + size, leases[0].tunnelGateway, 32);
size += 32; // reply tunnel gateway size += 32; // reply tunnel gateway
@ -422,7 +422,7 @@ namespace i2p
{ {
I2NPMessage * msg = NewI2NPMessage (); I2NPMessage * msg = NewI2NPMessage ();
memcpy (msg->GetPayload () + 4, payload, i2p::tunnel::TUNNEL_DATA_MSG_SIZE - 4); memcpy (msg->GetPayload () + 4, payload, i2p::tunnel::TUNNEL_DATA_MSG_SIZE - 4);
*(uint32_t *)(msg->GetPayload ()) = htobe32 (tunnelID); htobe32buf (msg->GetPayload (), tunnelID);
msg->len += i2p::tunnel::TUNNEL_DATA_MSG_SIZE; msg->len += i2p::tunnel::TUNNEL_DATA_MSG_SIZE;
FillI2NPMessageHeader (msg, eI2NPTunnelData); FillI2NPMessageHeader (msg, eI2NPTunnelData);
return msg; return msg;

View File

@ -47,27 +47,73 @@ uint64_t be64toh(uint64_t big64);
#endif #endif
inline uint16_t buf16toh(const uint8_t *buf)
{
uint16_t b16;
memcpy(&b16, buf, sizeof(uint16_t));
return b16;
}
inline uint32_t buf32toh(const uint8_t *buf)
{
uint32_t b32;
memcpy(&b32, buf, sizeof(uint32_t));
return b32;
}
inline uint64_t buf64toh(const uint8_t *buf)
{
uint64_t b64;
memcpy(&b64, buf, sizeof(uint64_t));
return b64;
}
inline uint16_t bufbe16toh(const uint8_t *buf) inline uint16_t bufbe16toh(const uint8_t *buf)
{ {
uint16_t big16; return be16toh(buf16toh(buf));
memcpy(&big16, buf, sizeof(uint16_t));
return be16toh(big16);
} }
inline uint32_t bufbe32toh(const uint8_t *buf) inline uint32_t bufbe32toh(const uint8_t *buf)
{ {
uint32_t big32; return be32toh(buf32toh(buf));
memcpy(&big32, buf, sizeof(uint32_t));
return be32toh(big32);
} }
inline uint64_t bufbe64toh(const uint8_t *buf) inline uint64_t bufbe64toh(const uint8_t *buf)
{ {
uint64_t big64; return be64toh(buf64toh(buf));
memcpy(&big64, buf, sizeof(uint64_t));
return be64toh(big64);
} }
inline void htobuf16(uint8_t *buf, uint16_t b16)
{
memcpy(buf, &b16, sizeof(uint16_t));
}
inline void htobuf32(uint8_t *buf, uint32_t b32)
{
memcpy(buf, &b32, sizeof(uint32_t));
}
inline void htobuf64(uint8_t *buf, uint64_t b64)
{
memcpy(buf, &b64, sizeof(uint64_t));
}
inline void htobe16buf(uint8_t *buf, uint16_t big16)
{
htobuf16(buf, htobe16(big16));
}
inline void htobe32buf(uint8_t *buf, uint32_t big32)
{
htobuf32(buf, htobe32(big32));
}
inline void htobe64buf(uint8_t *buf, uint64_t big64)
{
htobuf64(buf, htobe64(big64));
}
#endif // I2PENDIAN_H__ #endif // I2PENDIAN_H__

View File

@ -101,8 +101,8 @@ namespace data
m_StandardIdentity.certificate.length = htobe16 (m_ExtendedLen); m_StandardIdentity.certificate.length = htobe16 (m_ExtendedLen);
// fill extended buffer // fill extended buffer
m_ExtendedBuffer = new uint8_t[m_ExtendedLen]; m_ExtendedBuffer = new uint8_t[m_ExtendedLen];
*(uint16_t *)m_ExtendedBuffer = htobe16 (type); htobe16buf (m_ExtendedBuffer, type);
*(uint16_t *)(m_ExtendedBuffer + 2) = htobe16 (CRYPTO_KEY_TYPE_ELGAMAL); htobe16buf (m_ExtendedBuffer + 2, CRYPTO_KEY_TYPE_ELGAMAL);
if (excessLen && excessBuf) if (excessLen && excessBuf)
{ {
memcpy (m_ExtendedBuffer + 4, excessBuf, excessLen); memcpy (m_ExtendedBuffer + 4, excessBuf, excessLen);

View File

@ -274,11 +274,11 @@ namespace transport
{ {
auto keys = i2p::context.GetPrivateKeys (); auto keys = i2p::context.GetPrivateKeys ();
uint8_t * buf = m_ReceiveBuffer; uint8_t * buf = m_ReceiveBuffer;
*(uint16_t *)buf = htobe16 (keys.GetPublic ().GetFullLen ()); htobe16buf (buf, keys.GetPublic ().GetFullLen ());
buf += 2; buf += 2;
buf += i2p::context.GetIdentity ().ToBuffer (buf, NTCP_BUFFER_SIZE); buf += i2p::context.GetIdentity ().ToBuffer (buf, NTCP_BUFFER_SIZE);
uint32_t tsA = htobe32 (i2p::util::GetSecondsSinceEpoch ()); uint32_t tsA = htobe32 (i2p::util::GetSecondsSinceEpoch ());
*(uint32_t *)buf = tsA; htobuf32(buf,tsA);
buf += 4; buf += 4;
size_t signatureLen = keys.GetPublic ().GetSignatureLen (); size_t signatureLen = keys.GetPublic ().GetSignatureLen ();
size_t len = (buf - m_ReceiveBuffer) + signatureLen; size_t len = (buf - m_ReceiveBuffer) + signatureLen;
@ -376,7 +376,7 @@ namespace transport
void NTCPSession::HandlePhase3 (uint32_t tsB, size_t paddingLen) void NTCPSession::HandlePhase3 (uint32_t tsB, size_t paddingLen)
{ {
uint8_t * buf = m_ReceiveBuffer + m_RemoteIdentity.GetFullLen () + 2 /*size*/; uint8_t * buf = m_ReceiveBuffer + m_RemoteIdentity.GetFullLen () + 2 /*size*/;
uint32_t tsA = *(uint32_t *)buf; uint32_t tsA = buf32toh(buf);
buf += 4; buf += 4;
buf += paddingLen; buf += paddingLen;
@ -580,15 +580,15 @@ namespace transport
} }
sendBuffer = msg->GetBuffer () - 2; sendBuffer = msg->GetBuffer () - 2;
len = msg->GetLength (); len = msg->GetLength ();
*((uint16_t *)sendBuffer) = htobe16 (len); htobe16buf (sendBuffer, len);
} }
else else
{ {
// prepare timestamp // prepare timestamp
sendBuffer = m_TimeSyncBuffer; sendBuffer = m_TimeSyncBuffer;
len = 4; len = 4;
*((uint16_t *)sendBuffer) = 0; htobuf16(sendBuffer, 0);
*((uint32_t *)(sendBuffer + 2)) = htobe32 (time (0)); htobe32buf (sendBuffer + 2, time (0));
} }
int rem = (len + 6) & 0x0F; // %16 int rem = (len + 6) & 0x0F; // %16
int padding = 0; int padding = 0;

View File

@ -316,7 +316,7 @@ namespace transport
payload++; payload++;
*payload = 1; // always 1 message fragment per message *payload = 1; // always 1 message fragment per message
payload++; payload++;
*(uint32_t *)payload = msgID; htobuf32(payload, msgID);
payload += 4; payload += 4;
bool isLast = (len <= payloadSize); bool isLast = (len <= payloadSize);
size_t size = isLast ? len : payloadSize; size_t size = isLast ? len : payloadSize;
@ -359,7 +359,7 @@ namespace transport
payload++; payload++;
*payload = 1; // number of ACKs *payload = 1; // number of ACKs
payload++; payload++;
*(uint32_t *)(payload) = htobe32 (msgID); // msgID htobe32buf (payload, msgID); // msgID
payload += 4; payload += 4;
*payload = 0; // number of fragments *payload = 0; // number of fragments
@ -382,7 +382,7 @@ namespace transport
*payload = 1; // number of ACK bitfields *payload = 1; // number of ACK bitfields
payload++; payload++;
// one ack // one ack
*(uint32_t *)(payload) = htobe32 (msgID); // msgID htobe32buf (payload, msgID); // msgID
payload += 4; payload += 4;
div_t d = div (fragmentNum, 7); div_t d = div (fragmentNum, 7);
memset (payload, 0x80, d.quot); // 0x80 means non-last memset (payload, 0x80, d.quot); // 0x80 means non-last

View File

@ -299,18 +299,18 @@ namespace transport
uint8_t buf[96 + 18]; uint8_t buf[96 + 18];
uint8_t * payload = buf + sizeof (SSUHeader); uint8_t * payload = buf + sizeof (SSUHeader);
*(uint32_t *)payload = htobe32 (iTag); htobe32buf (payload, iTag);
payload += 4; payload += 4;
*payload = 0; // no address *payload = 0; // no address
payload++; payload++;
*(uint16_t *)payload = 0; // port = 0 htobuf16(payload, 0); // port = 0
payload += 2; payload += 2;
*payload = 0; // challenge *payload = 0; // challenge
payload++; payload++;
memcpy (payload, (const uint8_t *)address->key, 32); memcpy (payload, (const uint8_t *)address->key, 32);
payload += 32; payload += 32;
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
*(uint32_t *)payload = htobe32 (rnd.GenerateWord32 ()); // nonce htobe32buf (payload, rnd.GenerateWord32 ()); // nonce
uint8_t iv[16]; uint8_t iv[16];
rnd.GenerateBlock (iv, 16); // random iv rnd.GenerateBlock (iv, 16); // random iv
@ -358,7 +358,7 @@ namespace transport
s.Insert (payload, 16); // remote endpoint IP V6 s.Insert (payload, 16); // remote endpoint IP V6
payload += 16; payload += 16;
} }
*(uint16_t *)(payload) = htobe16 (m_RemoteEndpoint.port ()); htobe16buf (payload, m_RemoteEndpoint.port ());
s.Insert (payload, 2); // remote port s.Insert (payload, 2); // remote port
payload += 2; payload += 2;
if (address->host.is_v4 ()) if (address->host.is_v4 ())
@ -373,9 +373,9 @@ namespace transport
if (!relayTag) relayTag = 1; if (!relayTag) relayTag = 1;
m_Server.AddRelay (relayTag, m_RemoteEndpoint); m_Server.AddRelay (relayTag, m_RemoteEndpoint);
} }
*(uint32_t *)(payload) = htobe32 (relayTag); htobe32buf (payload, relayTag);
payload += 4; // relay tag payload += 4; // relay tag
*(uint32_t *)(payload) = htobe32 (i2p::util::GetSecondsSinceEpoch ()); // signed on time htobe32buf (payload, i2p::util::GetSecondsSinceEpoch ()); // signed on time
payload += 4; payload += 4;
s.Insert (payload - 8, 8); // relayTag and signed on time s.Insert (payload - 8, 8); // relayTag and signed on time
s.Sign (i2p::context.GetPrivateKeys (), payload); // DSA signature s.Sign (i2p::context.GetPrivateKeys (), payload); // DSA signature
@ -404,12 +404,12 @@ namespace transport
*payload = 1; // 1 fragment *payload = 1; // 1 fragment
payload++; // info payload++; // info
size_t identLen = i2p::context.GetIdentity ().GetFullLen (); // 387+ bytes size_t identLen = i2p::context.GetIdentity ().GetFullLen (); // 387+ bytes
*(uint16_t *)(payload) = htobe16 (identLen); htobe16buf (payload, identLen);
payload += 2; // cursize payload += 2; // cursize
i2p::context.GetIdentity ().ToBuffer (payload, identLen); i2p::context.GetIdentity ().ToBuffer (payload, identLen);
payload += identLen; payload += identLen;
uint32_t signedOnTime = i2p::util::GetSecondsSinceEpoch (); uint32_t signedOnTime = i2p::util::GetSecondsSinceEpoch ();
*(uint32_t *)(payload) = htobe32 (signedOnTime); // signed on time htobe32buf (payload, signedOnTime); // signed on time
payload += 4; payload += 4;
auto signatureLen = i2p::context.GetIdentity ().GetSignatureLen (); auto signatureLen = i2p::context.GetIdentity ().GetSignatureLen ();
size_t paddingSize = ((payload - buf) + signatureLen)%16; size_t paddingSize = ((payload - buf) + signatureLen)%16;
@ -476,9 +476,9 @@ namespace transport
} }
*payload = 4; *payload = 4;
payload++; // size payload++; // size
*(uint32_t *)payload = htobe32 (to.address ().to_v4 ().to_ulong ()); // Charlie's IP htobe32buf (payload, to.address ().to_v4 ().to_ulong ()); // Charlie's IP
payload += 4; // address payload += 4; // address
*(uint16_t *)payload = htobe16 (to.port ()); // Charlie's port htobe16buf (payload, to.port ()); // Charlie's port
payload += 2; // port payload += 2; // port
// Alice // Alice
bool isV4 = from.address ().is_v4 (); // Alice's bool isV4 = from.address ().is_v4 (); // Alice's
@ -496,9 +496,9 @@ namespace transport
memcpy (payload, from.address ().to_v6 ().to_bytes ().data (), 16); // Alice's IP V6 memcpy (payload, from.address ().to_v6 ().to_bytes ().data (), 16); // Alice's IP V6
payload += 16; // address payload += 16; // address
} }
*(uint16_t *)payload = htobe16 (from.port ()); // Alice's port htobe16buf (payload, from.port ()); // Alice's port
payload += 2; // port payload += 2; // port
*(uint32_t *)payload = htobe32 (nonce); htobe32buf (payload, nonce);
if (m_State == eSessionStateEstablished) if (m_State == eSessionStateEstablished)
{ {
@ -531,9 +531,9 @@ namespace transport
uint8_t * payload = buf + sizeof (SSUHeader); uint8_t * payload = buf + sizeof (SSUHeader);
*payload = 4; *payload = 4;
payload++; // size payload++; // size
*(uint32_t *)payload = htobe32 (from.address ().to_v4 ().to_ulong ()); // Alice's IP htobe32buf (payload, from.address ().to_v4 ().to_ulong ()); // Alice's IP
payload += 4; // address payload += 4; // address
*(uint16_t *)payload = htobe16 (from.port ()); // Alice's port htobe16buf (payload, from.port ()); // Alice's port
payload += 2; // port payload += 2; // port
*payload = 0; // challenge size *payload = 0; // challenge size
uint8_t iv[16]; uint8_t iv[16];
@ -612,7 +612,7 @@ namespace transport
encryption.Encrypt (encrypted, encryptedLen, encrypted); encryption.Encrypt (encrypted, encryptedLen, encrypted);
// assume actual buffer size is 18 (16 + 2) bytes more // assume actual buffer size is 18 (16 + 2) bytes more
memcpy (buf + len, iv, 16); memcpy (buf + len, iv, 16);
*(uint16_t *)(buf + len + 16) = htobe16 (encryptedLen); htobe16buf (buf + len + 16, encryptedLen);
i2p::crypto::HMACMD5Digest (encrypted, encryptedLen + 18, macKey, header->mac); i2p::crypto::HMACMD5Digest (encrypted, encryptedLen + 18, macKey, header->mac);
} }
@ -633,7 +633,7 @@ namespace transport
m_SessionKeyEncryption.Encrypt (encrypted, encryptedLen, encrypted); m_SessionKeyEncryption.Encrypt (encrypted, encryptedLen, encrypted);
// assume actual buffer size is 18 (16 + 2) bytes more // assume actual buffer size is 18 (16 + 2) bytes more
memcpy (buf + len, header->iv, 16); memcpy (buf + len, header->iv, 16);
*(uint16_t *)(buf + len + 16) = htobe16 (encryptedLen); htobe16buf (buf + len + 16, encryptedLen);
i2p::crypto::HMACMD5Digest (encrypted, encryptedLen + 18, m_MacKey, header->mac); i2p::crypto::HMACMD5Digest (encrypted, encryptedLen + 18, m_MacKey, header->mac);
} }
@ -682,7 +682,7 @@ namespace transport
uint16_t encryptedLen = len - (encrypted - buf); uint16_t encryptedLen = len - (encrypted - buf);
// assume actual buffer size is 18 (16 + 2) bytes more // assume actual buffer size is 18 (16 + 2) bytes more
memcpy (buf + len, header->iv, 16); memcpy (buf + len, header->iv, 16);
*(uint16_t *)(buf + len + 16) = htobe16 (encryptedLen); htobe16buf (buf + len + 16, encryptedLen);
uint8_t digest[16]; uint8_t digest[16];
i2p::crypto::HMACMD5Digest (encrypted, encryptedLen + 18, macKey, digest); i2p::crypto::HMACMD5Digest (encrypted, encryptedLen + 18, macKey, digest);
return !memcmp (header->mac, digest, 16); return !memcmp (header->mac, digest, 16);
@ -849,9 +849,9 @@ namespace transport
uint8_t size = *buf; uint8_t size = *buf;
buf++; // size buf++; // size
uint32_t address = (size == 4) ? *(uint32_t *)buf : 0; // use it as is uint32_t address = (size == 4) ? buf32toh(buf) : 0; // use it as is
buf += size; // address buf += size; // address
uint16_t port = *(uint16_t *)buf; // use it as is uint16_t port = buf16toh(buf); // use it as is
buf += 2; // port buf += 2; // port
uint8_t * introKey = buf; uint8_t * introKey = buf;
if (port && !address) if (port && !address)
@ -915,13 +915,13 @@ namespace transport
uint8_t buf[80 + 18]; uint8_t buf[80 + 18];
uint8_t iv[16]; uint8_t iv[16];
uint8_t * payload = buf + sizeof (SSUHeader); uint8_t * payload = buf + sizeof (SSUHeader);
*(uint32_t *)payload = htobe32 (nonce); htobe32buf (payload, nonce);
payload += 4; // nonce payload += 4; // nonce
if (address) if (address)
{ {
*payload = 4; *payload = 4;
payload++; // size payload++; // size
*(uint32_t *)payload = htobe32 (address); htobe32buf (payload, address);
payload += 4; // address payload += 4; // address
} }
else else
@ -929,7 +929,7 @@ namespace transport
*payload = 0; *payload = 0;
payload++; //size payload++; //size
} }
*(uint16_t *)payload = htobe16 (port); htobe16buf (payload, port);
payload += 2; // port payload += 2; // port
memcpy (payload, introKey, 32); // intro key memcpy (payload, introKey, 32); // intro key

View File

@ -256,16 +256,16 @@ namespace stream
uint8_t * packet = p->GetBuffer (); uint8_t * packet = p->GetBuffer ();
// TODO: implement setters // TODO: implement setters
size_t size = 0; size_t size = 0;
*(uint32_t *)(packet + size) = htobe32 (m_SendStreamID); htobe32buf (packet + size, m_SendStreamID);
size += 4; // sendStreamID size += 4; // sendStreamID
*(uint32_t *)(packet + size) = htobe32 (m_RecvStreamID); htobe32buf (packet + size, m_RecvStreamID);
size += 4; // receiveStreamID size += 4; // receiveStreamID
*(uint32_t *)(packet + size) = htobe32 (m_SequenceNumber++); htobe32buf (packet + size, m_SequenceNumber++);
size += 4; // sequenceNum size += 4; // sequenceNum
if (isNoAck) if (isNoAck)
*(uint32_t *)(packet + size) = htobe32 (m_LastReceivedSequenceNumber); htobe32buf (packet + size, m_LastReceivedSequenceNumber);
else else
*(uint32_t *)(packet + size) = 0; htobuf32 (packet + size, 0);
size += 4; // ack Through size += 4; // ack Through
packet[size] = 0; packet[size] = 0;
size++; // NACK count size++; // NACK count
@ -277,15 +277,15 @@ namespace stream
uint16_t flags = PACKET_FLAG_SYNCHRONIZE | PACKET_FLAG_FROM_INCLUDED | uint16_t flags = PACKET_FLAG_SYNCHRONIZE | PACKET_FLAG_FROM_INCLUDED |
PACKET_FLAG_SIGNATURE_INCLUDED | PACKET_FLAG_MAX_PACKET_SIZE_INCLUDED; PACKET_FLAG_SIGNATURE_INCLUDED | PACKET_FLAG_MAX_PACKET_SIZE_INCLUDED;
if (isNoAck) flags |= PACKET_FLAG_NO_ACK; if (isNoAck) flags |= PACKET_FLAG_NO_ACK;
*(uint16_t *)(packet + size) = htobe16 (flags); htobe16buf (packet + size, flags);
size += 2; // flags size += 2; // flags
size_t identityLen = m_LocalDestination.GetOwner ().GetIdentity ().GetFullLen (); size_t identityLen = m_LocalDestination.GetOwner ().GetIdentity ().GetFullLen ();
size_t signatureLen = m_LocalDestination.GetOwner ().GetIdentity ().GetSignatureLen (); size_t signatureLen = m_LocalDestination.GetOwner ().GetIdentity ().GetSignatureLen ();
*(uint16_t *)(packet + size) = htobe16 (identityLen + signatureLen + 2); // identity + signature + packet size htobe16buf (packet + size, identityLen + signatureLen + 2); // identity + signature + packet size
size += 2; // options size size += 2; // options size
m_LocalDestination.GetOwner ().GetIdentity ().ToBuffer (packet + size, identityLen); m_LocalDestination.GetOwner ().GetIdentity ().ToBuffer (packet + size, identityLen);
size += identityLen; // from size += identityLen; // from
*(uint16_t *)(packet + size) = htobe16 (STREAMING_MTU); htobe16buf (packet + size, STREAMING_MTU);
size += 2; // max packet size size += 2; // max packet size
uint8_t * signature = packet + size; // set it later uint8_t * signature = packet + size; // set it later
memset (signature, 0, signatureLen); // zeroes for now memset (signature, 0, signatureLen); // zeroes for now
@ -301,9 +301,9 @@ namespace stream
else else
{ {
// follow on packet // follow on packet
*(uint16_t *)(packet + size) = 0; htobuf16 (packet + size, 0);
size += 2; // flags size += 2; // flags
*(uint16_t *)(packet + size) = 0; // no options htobuf16 (packet + size, 0); // no options
size += 2; // options size size += 2; // options size
size_t sentLen = STREAMING_MTU - size; size_t sentLen = STREAMING_MTU - size;
if (len < sentLen) sentLen = len; if (len < sentLen) sentLen = len;
@ -338,13 +338,13 @@ namespace stream
Packet p; Packet p;
uint8_t * packet = p.GetBuffer (); uint8_t * packet = p.GetBuffer ();
size_t size = 0; size_t size = 0;
*(uint32_t *)(packet + size) = htobe32 (m_SendStreamID); htobe32buf (packet + size, m_SendStreamID);
size += 4; // sendStreamID size += 4; // sendStreamID
*(uint32_t *)(packet + size) = htobe32 (m_RecvStreamID); htobe32buf (packet + size, m_RecvStreamID);
size += 4; // receiveStreamID size += 4; // receiveStreamID
*(uint32_t *)(packet + size) = 0; // this is plain Ack message htobuf32 (packet + size, 0); // this is plain Ack message
size += 4; // sequenceNum size += 4; // sequenceNum
*(uint32_t *)(packet + size) = htobe32 (lastReceivedSeqn); htobe32buf (packet + size, lastReceivedSeqn);
size += 4; // ack Through size += 4; // ack Through
if (lastReceivedSeqn > m_LastReceivedSequenceNumber) if (lastReceivedSeqn > m_LastReceivedSequenceNumber)
{ {
@ -357,7 +357,7 @@ namespace stream
auto seqn = it->GetSeqn (); auto seqn = it->GetSeqn ();
for (uint32_t i = nextSeqn; i < seqn; i++) for (uint32_t i = nextSeqn; i < seqn; i++)
{ {
*(uint32_t *)nacks = htobe32 (i); htobe32buf (nacks, i);
nacks += 4; nacks += 4;
numNacks++; numNacks++;
} }
@ -374,9 +374,9 @@ namespace stream
size++; // NACK count size++; // NACK count
} }
size++; // resend delay size++; // resend delay
*(uint16_t *)(packet + size) = 0; // nof flags set htobuf16 (packet + size, 0); // nof flags set
size += 2; // flags size += 2; // flags
*(uint16_t *)(packet + size) = 0; // no options htobuf16 (packet + size, 0); // no options
size += 2; // options size size += 2; // options size
p.len = size; p.len = size;
@ -392,21 +392,21 @@ namespace stream
Packet * p = new Packet (); Packet * p = new Packet ();
uint8_t * packet = p->GetBuffer (); uint8_t * packet = p->GetBuffer ();
size_t size = 0; size_t size = 0;
*(uint32_t *)(packet + size) = htobe32 (m_SendStreamID); htobe32buf (packet + size, m_SendStreamID);
size += 4; // sendStreamID size += 4; // sendStreamID
*(uint32_t *)(packet + size) = htobe32 (m_RecvStreamID); htobe32buf (packet + size, m_RecvStreamID);
size += 4; // receiveStreamID size += 4; // receiveStreamID
*(uint32_t *)(packet + size) = htobe32 (m_SequenceNumber++); htobe32buf (packet + size, m_SequenceNumber++);
size += 4; // sequenceNum size += 4; // sequenceNum
*(uint32_t *)(packet + size) = htobe32 (m_LastReceivedSequenceNumber); htobe32buf (packet + size, m_LastReceivedSequenceNumber);
size += 4; // ack Through size += 4; // ack Through
packet[size] = 0; packet[size] = 0;
size++; // NACK count size++; // NACK count
size++; // resend delay size++; // resend delay
*(uint16_t *)(packet + size) = htobe16 (PACKET_FLAG_CLOSE | PACKET_FLAG_SIGNATURE_INCLUDED); htobe16buf (packet + size, PACKET_FLAG_CLOSE | PACKET_FLAG_SIGNATURE_INCLUDED);
size += 2; // flags size += 2; // flags
size_t signatureLen = m_LocalDestination.GetOwner ().GetIdentity ().GetSignatureLen (); size_t signatureLen = m_LocalDestination.GetOwner ().GetIdentity ().GetSignatureLen ();
*(uint16_t *)(packet + size) = htobe16 (signatureLen); // signature only htobe16buf (packet + size, signatureLen); // signature only
size += 2; // options size size += 2; // options size
uint8_t * signature = packet + size; uint8_t * signature = packet + size;
memset (packet + size, 0, signatureLen); memset (packet + size, 0, signatureLen);
@ -617,11 +617,11 @@ namespace stream
compressor.MessageEnd(); compressor.MessageEnd();
int size = compressor.MaxRetrievable (); int size = compressor.MaxRetrievable ();
uint8_t * buf = msg->GetPayload (); uint8_t * buf = msg->GetPayload ();
*(uint32_t *)buf = htobe32 (size); // length htobe32buf (buf, size); // length
buf += 4; buf += 4;
compressor.Get (buf, size); compressor.Get (buf, size);
*(uint16_t *)(buf + 4) = 0; // source port htobuf16(buf + 4, 0); // source port
*(uint16_t *)(buf + 6) = htobe16 (m_Port); // destination port htobe16buf (buf + 6, m_Port); // destination port
buf[9] = i2p::client::PROTOCOL_TYPE_STREAMING; // streaming protocol buf[9] = i2p::client::PROTOCOL_TYPE_STREAMING; // streaming protocol
msg->len += size + 4; msg->len += size + 4;
FillI2NPMessageHeader (msg, eI2NPData); FillI2NPMessageHeader (msg, eI2NPData);

View File

@ -31,7 +31,7 @@ namespace tunnel
LogPrint ("TransitTunnel: ",m_TunnelID,"->", m_NextTunnelID); LogPrint ("TransitTunnel: ",m_TunnelID,"->", m_NextTunnelID);
m_NumTransmittedBytes += tunnelMsg->GetLength (); m_NumTransmittedBytes += tunnelMsg->GetLength ();
*(uint32_t *)(tunnelMsg->GetPayload ()) = htobe32 (m_NextTunnelID); htobe32buf (tunnelMsg->GetPayload (), m_NextTunnelID);
FillI2NPMessageHeader (tunnelMsg, eI2NPTunnelData); FillI2NPMessageHeader (tunnelMsg, eI2NPTunnelData);
i2p::transport::transports.SendMessage (m_NextIdent, tunnelMsg); i2p::transport::transports.SendMessage (m_NextIdent, tunnelMsg);

View File

@ -26,7 +26,7 @@ namespace tunnel
{ {
if (block.deliveryType == eDeliveryTypeTunnel) if (block.deliveryType == eDeliveryTypeTunnel)
{ {
*(uint32_t *)(di + diLen) = htobe32 (block.tunnelID); htobe32buf (di + diLen, block.tunnelID);
diLen += 4; // tunnelID diLen += 4; // tunnelID
} }
@ -41,7 +41,7 @@ namespace tunnel
if (fullMsgLen <= m_RemainingSize) if (fullMsgLen <= m_RemainingSize)
{ {
// message fits. First and last fragment // message fits. First and last fragment
*(uint16_t *)(di + diLen) = htobe16 (msg->GetLength ()); htobe16buf (di + diLen, msg->GetLength ());
diLen += 2; // size diLen += 2; // size
memcpy (m_CurrentTunnelDataMsg->buf + m_CurrentTunnelDataMsg->len, di, diLen); memcpy (m_CurrentTunnelDataMsg->buf + m_CurrentTunnelDataMsg->len, di, diLen);
memcpy (m_CurrentTunnelDataMsg->buf + m_CurrentTunnelDataMsg->len + diLen, msg->GetBuffer (), msg->GetLength ()); memcpy (m_CurrentTunnelDataMsg->buf + m_CurrentTunnelDataMsg->len + diLen, msg->GetBuffer (), msg->GetLength ());
@ -73,9 +73,9 @@ namespace tunnel
// first fragment // first fragment
di[0] |= 0x08; // fragmented di[0] |= 0x08; // fragmented
*(uint32_t *)(di + diLen) = msgID; htobuf32 (di + diLen, msgID);
diLen += 4; // Message ID diLen += 4; // Message ID
*(uint16_t *)(di + diLen) = htobe16 (size); htobe16buf (di + diLen, size);
diLen += 2; // size diLen += 2; // size
memcpy (m_CurrentTunnelDataMsg->buf + m_CurrentTunnelDataMsg->len, di, diLen); memcpy (m_CurrentTunnelDataMsg->buf + m_CurrentTunnelDataMsg->len, di, diLen);
memcpy (m_CurrentTunnelDataMsg->buf + m_CurrentTunnelDataMsg->len + diLen, msg->GetBuffer (), size); memcpy (m_CurrentTunnelDataMsg->buf + m_CurrentTunnelDataMsg->len + diLen, msg->GetBuffer (), size);
@ -97,8 +97,8 @@ namespace tunnel
buf[0] |= 0x01; buf[0] |= 0x01;
isLastFragment = true; isLastFragment = true;
} }
*(uint32_t *)(buf + 1) = msgID; //Message ID htobuf32 (buf + 1, msgID); //Message ID
*(uint16_t *)(buf + 5) = htobe16 (s); // size htobe16buf (buf + 5, s); // size
memcpy (buf + 7, msg->GetBuffer () + size, s); memcpy (buf + 7, msg->GetBuffer () + size, s);
m_CurrentTunnelDataMsg->len += s+7; m_CurrentTunnelDataMsg->len += s+7;
if (isLastFragment) if (isLastFragment)
@ -147,7 +147,7 @@ namespace tunnel
m_CurrentTunnelDataMsg->offset = m_CurrentTunnelDataMsg->len - TUNNEL_DATA_MSG_SIZE - sizeof (I2NPHeader); m_CurrentTunnelDataMsg->offset = m_CurrentTunnelDataMsg->len - TUNNEL_DATA_MSG_SIZE - sizeof (I2NPHeader);
uint8_t * buf = m_CurrentTunnelDataMsg->GetPayload (); uint8_t * buf = m_CurrentTunnelDataMsg->GetPayload ();
*(uint32_t *)(buf) = htobe32 (m_TunnelID); htobe32buf (buf, m_TunnelID);
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
rnd.GenerateBlock (buf + 4, 16); // original IV rnd.GenerateBlock (buf + 4, 16); // original IV
memcpy (payload + size, buf + 4, 16); // copy IV for checksum memcpy (payload + size, buf + 4, 16); // copy IV for checksum