void DatagramDestination::SendMsg (I2NPMessage * msg, std::shared_ptr<const i2p::data::LeaseSet> remote) { auto outboundTunnel = m_Owner.GetTunnelPool ()->GetNextOutboundTunnel (); auto leases = remote->GetNonExpiredLeases (); if (!leases.empty () && outboundTunnel) { std::vector<i2p::tunnel::TunnelMessageBlock> msgs; uint32_t i = i2p::context.GetRandomNumberGenerator ().GenerateWord32 (0, leases.size () - 1); auto garlic = m_Owner.WrapMessage (remote, ToSharedI2NPMessage (msg), true); msgs.push_back (i2p::tunnel::TunnelMessageBlock { i2p::tunnel::eDeliveryTypeTunnel, leases[i].tunnelGateway, leases[i].tunnelID, garlic }); outboundTunnel->SendTunnelDataMsg (msgs); } else { if (outboundTunnel) LogPrint (eLogWarning, "Failed to send datagram. All leases expired"); else LogPrint (eLogWarning, "Failed to send datagram. No outbound tunnels"); DeleteI2NPMessage (msg); } }
void DatagramDestination::SendMsg( I2NPMessage* msg, std::shared_ptr<const i2p::data::LeaseSet> remote) { auto outboundTunnel = m_Owner.GetTunnelPool()->GetNextOutboundTunnel(); auto leases = remote->GetNonExpiredLeases(); if (!leases.empty() && outboundTunnel) { std::vector<i2p::tunnel::TunnelMessageBlock> msgs; uint32_t i = i2p::crypto::RandInRange<uint32_t>(0, leases.size() - 1); auto garlic = m_Owner.WrapMessage(remote, ToSharedI2NPMessage(msg), true); msgs.push_back( i2p::tunnel::TunnelMessageBlock { i2p::tunnel::e_DeliveryTypeTunnel, leases[i].tunnel_gateway, leases[i].tunnel_ID, garlic}); outboundTunnel->SendTunnelDataMsg(msgs); } else { if (outboundTunnel) LogPrint(eLogWarn, "DatagramDestination: failed to send datagram: all leases expired"); else LogPrint(eLogWarn, "DatagramDestination: failed to send datagram: no outbound tunnels"); DeleteI2NPMessage(msg); } }
void DatagramDestination::HandleLeaseSetRequestComplete (std::shared_ptr<i2p::data::LeaseSet> remote, I2NPMessage * msg) { if (remote) SendMsg (msg, remote); else DeleteI2NPMessage (msg); }
void TunnelPool::ProcessDeliveryStatus (I2NPMessage * msg) { I2NPDeliveryStatusMsg * deliveryStatus = (I2NPDeliveryStatusMsg *)msg->GetPayload (); auto it = m_Tests.find (be32toh (deliveryStatus->msgID)); if (it != m_Tests.end ()) { LogPrint ("Tunnel test ", it->first, " successive. ", i2p::util::GetMillisecondsSinceEpoch () - be64toh (deliveryStatus->timestamp), " milliseconds"); m_Tests.erase (it); } else i2p::garlic::routing.HandleDeliveryStatusMessage (msg->GetPayload (), msg->GetLength ()); // TODO: DeleteI2NPMessage (msg); }
I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessage * msg) { if (msg->offset >= sizeof (I2NPHeader) + sizeof (TunnelGatewayHeader)) { // message is capable to be used without copying TunnelGatewayHeader * header = (TunnelGatewayHeader *)(msg->GetBuffer () - sizeof (TunnelGatewayHeader)); header->tunnelID = htobe32 (tunnelID); int len = msg->GetLength (); header->length = htobe16 (len); msg->offset -= (sizeof (I2NPHeader) + sizeof (TunnelGatewayHeader)); msg->len = msg->offset + sizeof (I2NPHeader) + sizeof (TunnelGatewayHeader) +len; FillI2NPMessageHeader (msg, eI2NPTunnelGateway); return msg; } else { I2NPMessage * msg1 = CreateTunnelGatewayMsg (tunnelID, msg->GetBuffer (), msg->GetLength ()); DeleteI2NPMessage (msg); return msg1; } }
void HandleI2NPMessage (I2NPMessage * msg) { if (msg) { switch (msg->GetHeader ()->typeID) { case eI2NPTunnelData: LogPrint ("TunnelData"); i2p::tunnel::tunnels.PostTunnelData (msg); break; case eI2NPTunnelGateway: LogPrint ("TunnelGateway"); HandleTunnelGatewayMsg (msg); break; case eI2NPGarlic: LogPrint ("Garlic"); if (msg->from && msg->from->GetTunnelPool ()) msg->from->GetTunnelPool ()->GetLocalDestination ().ProcessGarlicMessage (msg); else i2p::context.ProcessGarlicMessage (msg); break; case eI2NPDatabaseStore: case eI2NPDatabaseSearchReply: case eI2NPDatabaseLookup: // forward to netDb i2p::data::netdb.PostI2NPMsg (msg); break; case eI2NPDeliveryStatus: LogPrint ("DeliveryStatus"); if (msg->from && msg->from->GetTunnelPool ()) msg->from->GetTunnelPool ()->ProcessDeliveryStatus (msg); else i2p::context.ProcessDeliveryStatusMessage (msg); break; default: HandleI2NPMessage (msg->GetBuffer (), msg->GetLength ()); DeleteI2NPMessage (msg); } } }
void TunnelGatewayBuffer::PutI2NPMsg (const TunnelMessageBlock& block) { if (!m_CurrentTunnelDataMsg) CreateCurrentTunnelDataMessage (); // create delivery instructions uint8_t di[43]; // max delivery instruction length is 43 for tunnel size_t diLen = 1;// flag if (block.deliveryType != eDeliveryTypeLocal) // tunnel or router { if (block.deliveryType == eDeliveryTypeTunnel) { *(uint32_t *)(di + diLen) = htobe32 (block.tunnelID); diLen += 4; // tunnelID } memcpy (di + diLen, block.hash, 32); diLen += 32; //len } di[0] = block.deliveryType << 5; // set delivery type // create fragments I2NPMessage * msg = block.data; if (diLen + msg->GetLength () + 2<= m_RemainingSize) { // message fits. First and last fragment *(uint16_t *)(di + diLen) = htobe16 (msg->GetLength ()); diLen += 2; // size memcpy (m_CurrentTunnelDataMsg->buf + m_CurrentTunnelDataMsg->len, di, diLen); memcpy (m_CurrentTunnelDataMsg->buf + m_CurrentTunnelDataMsg->len + diLen, msg->GetBuffer (), msg->GetLength ()); m_CurrentTunnelDataMsg->len += diLen + msg->GetLength (); m_RemainingSize -= diLen + msg->GetLength (); if (!m_RemainingSize) CompleteCurrentTunnelDataMessage (); DeleteI2NPMessage (msg); } else { if (diLen + 6 <= m_RemainingSize) { // delivery instructions fit uint32_t msgID = msg->GetHeader ()->msgID; size_t size = m_RemainingSize - diLen - 6; // 6 = 4 (msgID) + 2 (size) // first fragment di[0] |= 0x08; // fragmented *(uint32_t *)(di + diLen) = htobe32 (msgID); diLen += 4; // Message ID *(uint16_t *)(di + diLen) = htobe16 (size); diLen += 2; // size memcpy (m_CurrentTunnelDataMsg->buf + m_CurrentTunnelDataMsg->len, di, diLen); memcpy (m_CurrentTunnelDataMsg->buf + m_CurrentTunnelDataMsg->len + diLen, msg->GetBuffer (), size); m_CurrentTunnelDataMsg->len += diLen + size; CompleteCurrentTunnelDataMessage (); // follow on fragments int fragmentNumber = 1; while (size < msg->GetLength ()) { CreateCurrentTunnelDataMessage (); uint8_t * buf = m_CurrentTunnelDataMsg->GetBuffer (); buf[0] = 0x80 | (fragmentNumber << 1); // frag bool isLastFragment = false; size_t s = msg->GetLength () - size; if (s > TUNNEL_DATA_MAX_PAYLOAD_SIZE - 7) // 7 follow on instructions s = TUNNEL_DATA_MAX_PAYLOAD_SIZE - 7; else // last fragment { buf[0] |= 0x01; isLastFragment = true; } *(uint32_t *)(buf + 1) = htobe32 (msgID); //Message ID *(uint16_t *)(buf + 5) = htobe16 (s); // size memcpy (buf + 7, msg->GetBuffer () + size, s); m_CurrentTunnelDataMsg->len += s+7; if (isLastFragment) { m_RemainingSize -= s+7; if (!m_RemainingSize) CompleteCurrentTunnelDataMessage (); } else CompleteCurrentTunnelDataMessage (); size += s; fragmentNumber++; } DeleteI2NPMessage (msg); } else { // delivery instructions don't fit. Create new message CompleteCurrentTunnelDataMessage (); PutI2NPMsg (block); // don't delete msg because it's taken care inside } } }