diff --git a/libi2pd_client/SAM.cpp b/libi2pd_client/SAM.cpp index e997f9a2..1ad279e7 100644 --- a/libi2pd_client/SAM.cpp +++ b/libi2pd_client/SAM.cpp @@ -1288,9 +1288,25 @@ namespace client else { #ifdef _MSC_VER - size_t l = sprintf_s ((char *)m_StreamBuffer, SAM_STREAM_BUFFER_SIZE, SAM_DATAGRAM_RECEIVED, base64.c_str (), (long unsigned int)len); + size_t l = sprintf_s ( + (char *)m_StreamBuffer, + SAM_STREAM_BUFFER_SIZE, + SAM_DATAGRAM_RECEIVED, + base64.c_str (), + (long unsigned int)len, + (unsigned)fromPort, + (unsigned)toPort + ); #else - size_t l = snprintf ((char *)m_StreamBuffer, SAM_STREAM_BUFFER_SIZE, SAM_DATAGRAM_RECEIVED, base64.c_str (), (long unsigned int)len); + size_t l = snprintf ( + (char *)m_StreamBuffer, + SAM_STREAM_BUFFER_SIZE, + SAM_DATAGRAM_RECEIVED, + base64.c_str (), + (long unsigned int)len, + (unsigned)fromPort, + (unsigned)toPort + ); #endif if (len < SAM_STREAM_BUFFER_SIZE - l) { @@ -1661,6 +1677,18 @@ namespace client auto session = FindSession (sessionID); if (session) { + uint16_t fromPort = 0; + uint16_t toPort = 0; + char *raw_params = strchr(destination, ' '); + if (raw_params) + { + *raw_params = 0; raw_params++; + auto params = SAMSocket::ExtractParams(raw_params); + params.Get(SAM_PARAM_FROM_PORT, fromPort); + params.Get(SAM_PARAM_TO_PORT, toPort); + LogPrint (eLogInfo, "SAM: Datagram params are FROM_PORT=", fromPort, " TO_PORT=", toPort); + } + auto localDest = session->GetLocalDestination (); auto datagramDest = localDest ? localDest->GetDatagramDestination () : nullptr; if (datagramDest) @@ -1687,9 +1715,9 @@ namespace client if (isDest) { if (session->Type == SAMSessionType::eSAMSessionTypeDatagram) - datagramDest->SendDatagramTo ((uint8_t *)eol, payloadLen, ident); + datagramDest->SendDatagramTo ((uint8_t *)eol, payloadLen, ident, fromPort, toPort); else if (session->Type == SAMSessionType::eSAMSessionTypeRaw) - datagramDest->SendRawDatagramTo ((uint8_t *)eol, payloadLen, ident); + datagramDest->SendRawDatagramTo ((uint8_t *)eol, payloadLen, ident, fromPort, toPort); else LogPrint (eLogError, "SAM: Unexpected session type ", (int)session->Type, "for session ", sessionID); } diff --git a/libi2pd_client/SAM.h b/libi2pd_client/SAM.h index afefae26..a71fae91 100644 --- a/libi2pd_client/SAM.h +++ b/libi2pd_client/SAM.h @@ -66,7 +66,7 @@ namespace client const char SAM_DEST_REPLY_I2P_ERROR[] = "DEST REPLY RESULT=I2P_ERROR\n"; const char SAM_NAMING_LOOKUP[] = "NAMING LOOKUP"; const char SAM_NAMING_REPLY[] = "NAMING REPLY RESULT=OK NAME=%s VALUE=%s\n"; - const char SAM_DATAGRAM_RECEIVED[] = "DATAGRAM RECEIVED DESTINATION=%s SIZE=%lu\n"; + const char SAM_DATAGRAM_RECEIVED[] = "DATAGRAM RECEIVED DESTINATION=%s SIZE=%lu FROM_PORT=%u TO_PORT=%u\n"; const char SAM_RAW_RECEIVED[] = "RAW RECEIVED SIZE=%lu\n"; const char SAM_NAMING_REPLY_INVALID_KEY[] = "NAMING REPLY RESULT=INVALID_KEY NAME=%s\n"; const char SAM_NAMING_REPLY_KEY_NOT_FOUND[] = "NAMING REPLY RESULT=KEY_NOT_FOUND NAME=%s\n"; @@ -83,6 +83,7 @@ namespace client const char SAM_PARAM_HOST[] = "HOST"; const char SAM_PARAM_PORT[] = "PORT"; const char SAM_PARAM_FROM_PORT[] = "FROM_PORT"; + const char SAM_PARAM_TO_PORT[] = "TO_PORT"; const char SAM_VALUE_TRANSIENT[] = "TRANSIENT"; const char SAM_VALUE_TRUE[] = "true"; const char SAM_VALUE_FALSE[] = "false"; @@ -129,6 +130,8 @@ namespace client bool IsSession(std::string_view id) const; + static i2p::util::Mapping ExtractParams (std::string_view buf); + private: void TerminateClose() { Terminate(nullptr); } @@ -164,7 +167,6 @@ namespace client void SendStreamI2PError(const std::string & msg); void SendStreamCantReachPeer(const std::string & msg); size_t ProcessDatagramSend (char * buf, size_t len, const char * data); // from SAM 1.0 - static i2p::util::Mapping ExtractParams (std::string_view buf); void Connect (std::shared_ptr remote, std::shared_ptr session = nullptr); void HandleConnectLeaseSetRequestComplete (std::shared_ptr leaseSet);