std::shared_ptr<I2NPMessage> CreateDatabaseStoreMsg (std::shared_ptr<const i2p::data::LeaseSet> leaseSet, uint32_t replyToken) { if (!leaseSet) return nullptr; auto m = NewI2NPShortMessage (); uint8_t * payload = m->GetPayload (); 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 (); if (leases.size () > 0) { htobe32buf (payload + size, leases[0].tunnelID); size += 4; // reply tunnelID memcpy (payload + size, leases[0].tunnelGateway, 32); size += 32; // reply tunnel gateway } else htobe32buf (payload + DATABASE_STORE_REPLY_TOKEN_OFFSET, 0); } memcpy (payload + size, leaseSet->GetBuffer (), leaseSet->GetBufferLen ()); size += leaseSet->GetBufferLen (); m->len += size; m->FillI2NPMessageHeader (eI2NPDatabaseStore); return m; }
void TransitTunnelParticipant::HandleTunnelDataMsg( std::shared_ptr<const i2p::I2NPMessage> tunnelMsg) { auto newMsg = CreateEmptyTunnelDataMsg(); EncryptTunnelMsg(tunnelMsg, newMsg); m_NumTransmittedBytes += tunnelMsg->GetLength(); htobe32buf(newMsg->GetPayload(), GetNextTunnelID()); newMsg->FillI2NPMessageHeader(e_I2NPTunnelData); m_TunnelDataMsgs.push_back(newMsg); }
std::shared_ptr<I2NPMessage> CreateTunnelDataMsg (uint32_t tunnelID, const uint8_t * payload) { auto msg = NewI2NPShortMessage (); htobe32buf (msg->GetPayload (), tunnelID); msg->len += 4; // tunnelID msg->Concat (payload, i2p::tunnel::TUNNEL_DATA_MSG_SIZE - 4); msg->FillI2NPMessageHeader (eI2NPTunnelData); return msg; }
std::shared_ptr<I2NPMessage> CreateRouterInfoDatabaseLookupMsg (const uint8_t * key, const uint8_t * from, uint32_t replyTunnelID, bool exploratory, std::set<i2p::data::IdentHash> * excludedPeers) { auto m = excludedPeers ? NewI2NPMessage () : NewI2NPShortMessage (); uint8_t * buf = m->GetPayload (); memcpy (buf, key, 32); // key buf += 32; memcpy (buf, from, 32); // from buf += 32; uint8_t flag = exploratory ? DATABASE_LOOKUP_TYPE_EXPLORATORY_LOOKUP : DATABASE_LOOKUP_TYPE_ROUTERINFO_LOOKUP; if (replyTunnelID) { *buf = flag | DATABASE_LOOKUP_DELIVERY_FLAG; // set delivery flag htobe32buf (buf+1, replyTunnelID); buf += 5; } else { *buf = flag; // flag buf++; } if (excludedPeers) { int cnt = excludedPeers->size (); htobe16buf (buf, cnt); buf += 2; for (auto& it: *excludedPeers) { memcpy (buf, it, 32); buf += 32; } } else { // nothing to exclude htobuf16 (buf, 0); buf += 2; } m->len += (buf - m->GetPayload ()); m->FillI2NPMessageHeader (eI2NPDatabaseLookup); return m; }
std::shared_ptr<I2NPMessage> CreateTunnelGatewayMsg (uint32_t tunnelID, const uint8_t * buf, size_t len) { auto msg = NewI2NPMessage (len); uint8_t * payload = msg->GetPayload (); htobe32buf (payload + TUNNEL_GATEWAY_HEADER_TUNNELID_OFFSET, tunnelID); htobe16buf (payload + TUNNEL_GATEWAY_HEADER_LENGTH_OFFSET, len); msg->len += TUNNEL_GATEWAY_HEADER_SIZE; if (msg->Concat (buf, len) < len) LogPrint (eLogError, "I2NP: tunnel gateway buffer overflow ", msg->maxLen); msg->FillI2NPMessageHeader (eI2NPTunnelGateway); return msg; }
TEST_F(UDPTest, Packetv4) { Packet packet; packet.push_front("0123456789012345678901234567890123456789"); auto src_mac = MACAddress{ 0x00, 0xff, 0x23, 0x00, 0x00, 0x01 }; auto src_ip = IPv4Address{ 1, 1, 1, 1 }; auto src_port = uint16_t(24810); auto dst_mac = MACAddress{ 0x00, 0xff, 0x23, 0x00, 0x00, 0x01 }; auto dst_ip = IPv4Address{ 1, 1, 1, 2 }; auto dst_port = uint16_t(24811); // Generate packet MakeUDPPacket(packet, src_port, dst_port, src_ip, dst_ip); MakeIPv4Packet(packet, IPProtNum::UDP, src_ip, dst_ip); MakeEthernetPacket(packet, src_mac, dst_mac, EtherType::IPv4); // Write out pcap (useful for checking with Wireshark) WritePCAP("UDPv4.pcap", packet); // Decode packet EthernetDecoder ethdec(packet.begin(), packet.end()); ASSERT_EQ(ethdec.getDestinationMAC(), dst_mac); ASSERT_EQ(ethdec.getSourceMAC(), src_mac); ASSERT_EQ(ethdec.getEtherType(), EtherType::IPv4); IPv4Decoder ipdec(GetPayload(ethdec)); ASSERT_EQ(ipdec.getSourceIP(), src_ip); ASSERT_EQ(ipdec.getDestinationIP(), dst_ip); ASSERT_EQ(ipdec.getProtocol(), IPProtNum::UDP); UDPDecoder udpdec(GetPayload(ipdec)); ASSERT_EQ(udpdec.getSourcePort(), src_port); ASSERT_EQ(udpdec.getDestinationPort(), dst_port); std::string decoded_payload(udpdec.getPayload(), udpdec.getPayload() + udpdec.getPayloadLength()); ASSERT_EQ(decoded_payload, std::string("0123456789012345678901234567890123456789")); }
std::shared_ptr<I2NPMessage> CreateLeaseSetDatabaseLookupMsg (const i2p::data::IdentHash& dest, const std::set<i2p::data::IdentHash>& excludedFloodfills, const i2p::tunnel::InboundTunnel * replyTunnel, const uint8_t * replyKey, const uint8_t * replyTag) { int cnt = excludedFloodfills.size (); auto m = cnt > 0 ? NewI2NPMessage () : NewI2NPShortMessage (); uint8_t * buf = m->GetPayload (); memcpy (buf, dest, 32); // key buf += 32; memcpy (buf, replyTunnel->GetNextIdentHash (), 32); // reply tunnel GW buf += 32; *buf = DATABASE_LOOKUP_DELIVERY_FLAG | DATABASE_LOOKUP_ENCYPTION_FLAG | DATABASE_LOOKUP_TYPE_LEASESET_LOOKUP; // flags htobe32buf (buf + 1, replyTunnel->GetNextTunnelID ()); // reply tunnel ID buf += 5; // excluded htobe16buf (buf, cnt); buf += 2; if (cnt > 0) { for (auto& it: excludedFloodfills) { memcpy (buf, it, 32); buf += 32; } } // encryption memcpy (buf, replyKey, 32); buf[32] = 1; // 1 tag memcpy (buf + 33, replyTag, 32); buf += 65; m->len += (buf - m->GetPayload ()); m->FillI2NPMessageHeader (eI2NPDatabaseLookup); return m; }
std::shared_ptr<I2NPMessage> CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessageType msgType, const uint8_t * buf, size_t len, uint32_t replyMsgID) { auto msg = NewI2NPMessage (len); size_t gatewayMsgOffset = I2NP_HEADER_SIZE + TUNNEL_GATEWAY_HEADER_SIZE; msg->offset += gatewayMsgOffset; msg->len += gatewayMsgOffset; if (msg->Concat (buf, len) < len) LogPrint (eLogError, "I2NP: tunnel gateway buffer overflow ", msg->maxLen); msg->FillI2NPMessageHeader (msgType, replyMsgID); // create content message len = msg->GetLength (); msg->offset -= gatewayMsgOffset; uint8_t * payload = msg->GetPayload (); htobe32buf (payload + TUNNEL_GATEWAY_HEADER_TUNNELID_OFFSET, tunnelID); htobe16buf (payload + TUNNEL_GATEWAY_HEADER_LENGTH_OFFSET, len); msg->FillI2NPMessageHeader (eI2NPTunnelGateway); // gateway message return msg; }
std::shared_ptr<I2NPMessage> CreateDeliveryStatusMsg (uint32_t msgID) { auto m = NewI2NPShortMessage (); uint8_t * buf = m->GetPayload (); if (msgID) { htobe32buf (buf + DELIVERY_STATUS_MSGID_OFFSET, msgID); htobe64buf (buf + DELIVERY_STATUS_TIMESTAMP_OFFSET, i2p::util::GetMillisecondsSinceEpoch ()); } else // for SSU establishment { RAND_bytes ((uint8_t *)&msgID, 4); htobe32buf (buf + DELIVERY_STATUS_MSGID_OFFSET, msgID); htobe64buf (buf + DELIVERY_STATUS_TIMESTAMP_OFFSET, 2); // netID = 2 } m->len += DELIVERY_STATUS_SIZE; m->FillI2NPMessageHeader (eI2NPDeliveryStatus); return m; }
std::shared_ptr<I2NPMessage> CreateDatabaseSearchReply (const i2p::data::IdentHash& ident, std::vector<i2p::data::IdentHash> routers) { auto m = NewI2NPShortMessage (); uint8_t * buf = m->GetPayload (); size_t len = 0; memcpy (buf, ident, 32); len += 32; buf[len] = routers.size (); len++; for (auto it: routers) { memcpy (buf + len, it, 32); len += 32; } memcpy (buf + len, i2p::context.GetRouterInfo ().GetIdentHash (), 32); len += 32; m->len += len; m->FillI2NPMessageHeader (eI2NPDatabaseSearchReply); return m; }
std::shared_ptr<I2NPMessage> CreateDatabaseStoreMsg (std::shared_ptr<const i2p::data::RouterInfo> router, uint32_t replyToken) { if (!router) // we send own RouterInfo router = context.GetSharedRouterInfo (); auto m = NewI2NPShortMessage (); uint8_t * payload = m->GetPayload (); memcpy (payload + DATABASE_STORE_KEY_OFFSET, router->GetIdentHash (), 32); payload[DATABASE_STORE_TYPE_OFFSET] = 0; // RouterInfo htobe32buf (payload + DATABASE_STORE_REPLY_TOKEN_OFFSET, replyToken); uint8_t * buf = payload + DATABASE_STORE_HEADER_SIZE; if (replyToken) { memset (buf, 0, 4); // zero tunnelID means direct reply buf += 4; memcpy (buf, router->GetIdentHash (), 32); buf += 32; } uint8_t * sizePtr = buf; buf += 2; m->len += (buf - payload); // payload size i2p::data::GzipDeflator deflator; size_t size = deflator.Deflate (router->GetBuffer (), router->GetBufferLen (), buf, m->maxLen -m->len); if (size) { htobe16buf (sizePtr, size); // size m->len += size; } else m = nullptr; if (m) m->FillI2NPMessageHeader (eI2NPDatabaseStore); return m; }
std::shared_ptr<I2NPMessage> GarlicRoutingSession::WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg) { auto m = NewI2NPMessage (); m->Align (12); // in order to get buf aligned to 16 (12 + 4) size_t len = 0; uint8_t * buf = m->GetPayload () + 4; // 4 bytes for length // find non-expired tag bool tagFound = false; SessionTag tag; if (m_NumTags > 0) { uint32_t ts = i2p::util::GetSecondsSinceEpoch (); while (!m_SessionTags.empty ()) { if (ts < m_SessionTags.front ().creationTime + OUTGOING_TAGS_EXPIRATION_TIMEOUT) { tag = m_SessionTags.front (); m_SessionTags.pop_front (); // use same tag only once tagFound = true; break; } else m_SessionTags.pop_front (); // remove expired tag } } // create message if (!tagFound) // new session { LogPrint (eLogInfo, "Garlic: No tags available, will use ElGamal"); if (!m_Destination) { LogPrint (eLogError, "Garlic: Can't use ElGamal for unknown destination"); return nullptr; } // create ElGamal block ElGamalBlock elGamal; memcpy (elGamal.sessionKey, m_SessionKey, 32); RAND_bytes (elGamal.preIV, 32); // Pre-IV uint8_t iv[32]; // IV is first 16 bytes SHA256(elGamal.preIV, 32, iv); BN_CTX * ctx = BN_CTX_new (); m_Destination->Encrypt ((uint8_t *)&elGamal, buf, ctx); BN_CTX_free (ctx); m_Encryption.SetIV (iv); buf += 514; len += 514; } else // existing session { // session tag memcpy (buf, tag, 32); uint8_t iv[32]; // IV is first 16 bytes SHA256(tag, 32, iv); m_Encryption.SetIV (iv); buf += 32; len += 32; } // AES block len += CreateAESBlock (buf, msg); htobe32buf (m->GetPayload (), len); m->len += len + 4; m->FillI2NPMessageHeader (eI2NPGarlic); return m; }
//----------------------------------------------------------------------------- // Function: UpdateRegistrationCommand // Purpose: Updates peer name registration information // Parameters: None // // NOTE: The signature of this function must adhere to the MENU_COMMAND typedef HRESULT UpdateRegistrationCommand() { HRESULT hr = S_OK; //ppRegAddrs is a pointer to an array of pointers to SOCKADDR structures SOCKADDR** ppRegAddrs = NULL; ULONG cAddresses = 0; // NOTE: You may not update cAddresses to or from // PEER_PNRP_AUTO_ADDRESSES - this can only be specified at the // initial registration if (g_RegInfo.cAddresses != PEER_PNRP_AUTO_ADDRESSES) { // Get addresses hr = GetAddress(FALSE, &cAddresses, &ppRegAddrs); if (SUCCEEDED(hr)) { //Copy in the address count and pointer to the array of pointers g_RegInfo.cAddresses = cAddresses; g_RegInfo.ppAddresses = ppRegAddrs; } } // Update the comment - since the pointers in the g_RegInfo struct // already point to g_wzComment, the comment in g_RegInfo will be // implicitly updated if (SUCCEEDED(hr)) { hr = GetComment(celems(g_wzComment), g_wzComment); } // Update the payload. If no addresses are specified, ensure that the // payload is non-empty if (SUCCEEDED(hr)) { hr = GetPayload(sizeof(g_PayloadData), (PBYTE)g_PayloadData); if ((g_RegInfo.cAddresses == 0) && SUCCEEDED(hr)) { while(PayloadSize(g_PayloadData) == 0 && SUCCEEDED(hr)) { wprintf(L"Payload must be non-empty if no addresses are specified.\n"); hr = GetPayload(sizeof(g_PayloadData), (PBYTE)g_PayloadData); } } } if (SUCCEEDED(hr)) { if (PayloadSize(g_PayloadData) == 0) { g_RegInfo.payload.cbData = 0; g_RegInfo.payload.pbData = NULL; } else { g_RegInfo.payload.cbData = PayloadSize(g_PayloadData); g_RegInfo.payload.pbData = (PBYTE) g_PayloadData; } } // Perform the update and return the result if (SUCCEEDED(hr)) { hr = PeerPnrpUpdateRegistration(g_hRegistration, &g_RegInfo); } if (SUCCEEDED(hr)) { wprintf(L"Registration successfully updated\n"); } else { wprintf(L"Error while updating registration.\n"); } if (ppRegAddrs != NULL) { free(ppRegAddrs); } return hr; }
//----------------------------------------------------------------------------- // Function: RegisterPeerNameCommand // Purpose: Prompts user for registration information, fills out a // PEER_PNRP_REGISTRATION_INFO struct, creates a peer name, // and calls PeerPnrpRegister to register the peer name // Parameters: None // // NOTE: The signature of this function must adhere to the MENU_COMMAND typedef HRESULT RegisterPeerNameCommand() { WCHAR wzInputBuffer[256] = {0}; WCHAR wzIdentity[MAX_PEERNAME_LENGTH] = {0}; WCHAR wzClassifier[MAX_CLASSIFIER_LENGTH] = {0}; HRESULT hr = S_OK; PWSTR pwzPeerName = NULL; ULONG cAddresses = 0; //ppRegAddrs is a pointer to an array of pointers to SOCKADDR structures SOCKADDR** ppRegAddrs = NULL; ZeroMemory(&g_RegInfo, sizeof(PEER_PNRP_REGISTRATION_INFO)); //Collect Registration Information //--------------------------------- // Addresses hr = GetAddress(TRUE, &cAddresses, &ppRegAddrs); if (SUCCEEDED(hr)) { // Cloud name hr = GetCloudName(cAddresses != 1, celems(g_wzCloudName), g_wzCloudName); } if (SUCCEEDED(hr)) { // Comment hr = GetComment(celems(g_wzComment), g_wzComment); } if (SUCCEEDED(hr)) { // Payload hr = GetPayload(sizeof(g_PayloadData), (PBYTE)g_PayloadData); if (cAddresses == 0) { // If no addresses were specified, the payload must be non-null. while (PayloadSize(g_PayloadData) == 0 && SUCCEEDED(hr)) { wprintf(L"Payload must be non-empty if no addresses are specified.\n"); hr = GetPayload(sizeof(g_PayloadData), (PBYTE)g_PayloadData); } } } // Create Peer Name //------------------ // If secure, get identity if (SUCCEEDED(hr)) { wprintf(L"Secured peer name [no]: "); FLUSH_AND_GET_RESPONSE(hr, wzInputBuffer); if (SUCCEEDED(hr) && _wcsicmp(wzInputBuffer, L"yes") == 0) { hr = GetIdentity(celems(wzIdentity), wzIdentity); } } // Classifier if (SUCCEEDED(hr)) { do { wprintf(L"Classifier: "); FLUSH_AND_GET_RESPONSE(hr, wzClassifier); // Classifier must be non-null. if (SUCCEEDED(hr) && wzClassifier[0] == L'\0') { wprintf(L"Classifier must be non-empty.\n"); } } while (SUCCEEDED(hr) && wzClassifier[0] == L'\0'); } // Create peer name if (SUCCEEDED(hr)) { if (wcslen(wzIdentity) != 0) { // Create secured peer name hr = PeerCreatePeerName(wzIdentity, wzClassifier, &pwzPeerName); } else { // Create unsecured peer name hr = PeerCreatePeerName(NULL, wzClassifier, &pwzPeerName); } } // Fill out g_RegInfo struct // ------------------------- if (wcslen(g_wzCloudName) == 0) { g_RegInfo.pwzCloudName = NULL; } else { g_RegInfo.pwzCloudName = g_wzCloudName; } //Copy in the address count and pointer to the array of pointers g_RegInfo.cAddresses = cAddresses; g_RegInfo.ppAddresses = ppRegAddrs; g_RegInfo.pwzComment = g_wzComment; if (PayloadSize(g_PayloadData) == 0) { g_RegInfo.payload.cbData = 0; g_RegInfo.payload.pbData = NULL; } else { g_RegInfo.payload.cbData = PayloadSize(g_PayloadData); g_RegInfo.payload.pbData = (PBYTE) g_PayloadData; } // Perform registration if (SUCCEEDED(hr)) { hr = PeerPnrpRegister(pwzPeerName, &g_RegInfo, &g_hRegistration); } // Display result if (SUCCEEDED(hr)) { wprintf(L"\nSuccessfully registered name: %s\n", pwzPeerName); } else { wprintf(L"\nError while registering name. HRESULT=0x%x\n", hr); PrintError(hr); } if (pwzPeerName != NULL) { PeerFreeData(pwzPeerName); } if (ppRegAddrs != NULL) { free(ppRegAddrs); } return hr; }
uint8_t Frame::GetHeaderLength(void) const { return static_cast<uint8_t>(GetPayload() - GetPsdu()); }