diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index f063293f0..4d1ab0278 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -504,12 +504,13 @@ otError Interpreter::ParseToIp6Address(otInstance *aInstance, otIp6Address &aAddress, bool &aSynthesized) { - Error error = kErrorNone; + Error error = OT_ERROR_NONE; VerifyOrExit(!aArg.IsEmpty(), error = OT_ERROR_INVALID_ARGS); error = aArg.ParseAsIp6Address(aAddress); aSynthesized = false; - if (error != kErrorNone) + + if (error != OT_ERROR_NONE) { // It might be an IPv4 address, let's have a try. otIp4Address ip4Address; @@ -758,9 +759,6 @@ template <> otError Interpreter::Process(Arg aArgs[]) */ else if (aArgs[0] == "mappings") { - otNat64AddressMappingIterator iterator; - otNat64AddressMapping mapping; - static const char *const kNat64StatusLevel1Title[] = {"", "Address", "", "4 to 6", "6 to 4"}; static const uint8_t kNat64StatusLevel1ColumnWidths[] = { @@ -775,6 +773,9 @@ template <> otError Interpreter::Process(Arg aArgs[]) 18, 42, 18, 8, 10, 14, 10, 14, }; + otNat64AddressMappingIterator iterator; + otNat64AddressMapping mapping; + OutputTableHeader(kNat64StatusLevel1Title, kNat64StatusLevel1ColumnWidths); OutputTableHeader(kNat64StatusTableHeader, kNat64StatusTableColumnWidths); @@ -7563,7 +7564,7 @@ template <> otError Interpreter::Process(Arg aArgs[]) } else { - VerifyOrExit(aArgs[1].IsEmpty(), error = kErrorInvalidArgs); + VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); } if (isTable) diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index f69807110..142f02c33 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -258,14 +258,14 @@ public: * If the argument string is an IPv4 address, this method will try to synthesize an IPv6 address using preferred * NAT64 prefix in the network data. * - * @param[in] aInstance A pointer to openthread instance. + * @param[in] aInstance A pointer to OpenThread instance. * @param[in] aArg The argument string to parse. * @param[out] aAddress A reference to an `otIp6Address` to output the parsed IPv6 address. * @param[out] aSynthesized Whether @p aAddress is synthesized from an IPv4 address. * - * @retval OT_ERROR_NONE The argument was parsed successfully. - * @retval OT_ERROR_INVALID_ARGS The argument is empty or does not contain valid IP address. - * @retval OT_ERROR_INVALID_STATE No valid NAT64 prefix in the network data. + * @retval OT_ERROR_NONE The argument was parsed successfully. + * @retval OT_ERROR_INVALID_ARGS The argument is empty or does not contain a valid IP address. + * @retval OT_ERROR_INVALID_STATE No valid NAT64 prefix in the network data. * */ static otError ParseToIp6Address(otInstance *aInstance, diff --git a/src/cli/cli_dns.cpp b/src/cli/cli_dns.cpp index 7aef47976..e5dff9a13 100644 --- a/src/cli/cli_dns.cpp +++ b/src/cli/cli_dns.cpp @@ -416,15 +416,15 @@ otError Dns::GetDnsConfig(Arg aArgs[], otDnsQueryConfig *&aConfig) otError error = OT_ERROR_NONE; bool recursionDesired; - bool nat64SynthesizedAddress; + bool nat64Synth; ClearAllBytes(*aConfig); VerifyOrExit(!aArgs[0].IsEmpty(), aConfig = nullptr); SuccessOrExit(error = Interpreter::ParseToIp6Address(GetInstancePtr(), aArgs[0], aConfig->mServerSockAddr.mAddress, - nat64SynthesizedAddress)); - if (nat64SynthesizedAddress) + nat64Synth)); + if (nat64Synth) { OutputFormat("Synthesized IPv6 DNS server address: "); OutputIp6AddressLine(aConfig->mServerSockAddr.mAddress); diff --git a/src/cli/cli_ping.cpp b/src/cli/cli_ping.cpp index e8e762157..7d8825908 100644 --- a/src/cli/cli_ping.cpp +++ b/src/cli/cli_ping.cpp @@ -55,7 +55,7 @@ otError PingSender::Process(Arg aArgs[]) otError error = OT_ERROR_NONE; otPingSenderConfig config; bool async = false; - bool nat64SynthesizedAddress; + bool nat64Synth; /** * @cli ping stop @@ -96,9 +96,9 @@ otError PingSender::Process(Arg aArgs[]) aArgs++; } - SuccessOrExit(error = Interpreter::GetInterpreter().ParseToIp6Address( - GetInstancePtr(), aArgs[0], config.mDestination, nat64SynthesizedAddress)); - if (nat64SynthesizedAddress) + SuccessOrExit(error = Interpreter::ParseToIp6Address(GetInstancePtr(), aArgs[0], config.mDestination, nat64Synth)); + + if (nat64Synth) { OutputFormat("Pinging synthesized IPv6 address: "); OutputIp6AddressLine(config.mDestination); diff --git a/src/cli/cli_tcat.cpp b/src/cli/cli_tcat.cpp index e754bca93..491aaf6c0 100644 --- a/src/cli/cli_tcat.cpp +++ b/src/cli/cli_tcat.cpp @@ -93,10 +93,12 @@ static void HandleBleSecureReceive(otInstance *aInstance, OT_UNUSED_VARIABLE(aContext); OT_UNUSED_VARIABLE(aTcatApplicationProtocol); OT_UNUSED_VARIABLE(aServiceName); + static constexpr int kTextMaxLen = 100; static constexpr uint8_t kBufPrefixLen = 5; - uint16_t nLen; - uint8_t buf[kTextMaxLen]; + + uint16_t nLen; + uint8_t buf[kTextMaxLen]; nLen = otMessageRead(aMessage, (uint16_t)aOffset, buf + kBufPrefixLen, sizeof(buf) - kBufPrefixLen - 1); @@ -137,11 +139,10 @@ exit: template <> otError Tcat::Process(Arg aArgs[]) { OT_UNUSED_VARIABLE(aArgs); - otError error = OT_ERROR_NONE; otBleSecureStop(GetInstancePtr()); - return error; + return OT_ERROR_NONE; } otError Tcat::Process(Arg aArgs[]) diff --git a/src/cli/cli_tcp.cpp b/src/cli/cli_tcp.cpp index ebf6a37b6..90ce1aaef 100644 --- a/src/cli/cli_tcp.cpp +++ b/src/cli/cli_tcp.cpp @@ -81,9 +81,10 @@ TcpExample::TcpExample(otInstance *aInstance, OutputImplementer &aOutputImplemen void TcpExample::MbedTlsDebugOutput(void *ctx, int level, const char *file, int line, const char *str) { TcpExample &tcpExample = *static_cast(ctx); + tcpExample.OutputLine("%s:%d:%d: %s", file, line, level, str); } -#endif // OPENTHREAD_CONFIG_TLS_ENABLE +#endif /** * @cli tcp init @@ -221,6 +222,7 @@ template <> otError TcpExample::Process(Arg aArgs[]) ClearAllBytes(endpointArgs); endpointArgs.mEstablishedCallback = HandleTcpEstablishedCallback; + if (mUseCircularSendBuffer) { endpointArgs.mForwardProgressCallback = HandleTcpForwardProgressCallback; @@ -229,6 +231,7 @@ template <> otError TcpExample::Process(Arg aArgs[]) { endpointArgs.mSendDoneCallback = HandleTcpSendDoneCallback; } + endpointArgs.mReceiveAvailableCallback = HandleTcpReceiveAvailableCallback; endpointArgs.mDisconnectedCallback = HandleTcpDisconnectedCallback; endpointArgs.mContext = this; @@ -247,6 +250,7 @@ template <> otError TcpExample::Process(Arg aArgs[]) listenerArgs.mContext = this; error = otTcpListenerInitialize(GetInstancePtr(), &mListener, &listenerArgs); + if (error != OT_ERROR_NONE) { IgnoreReturnValue(otTcpEndpointDeinitialize(&mEndpoint)); @@ -289,7 +293,7 @@ template <> otError TcpExample::Process(Arg aArgs[]) mbedtls_pk_free(&mPKey); mbedtls_x509_crt_free(&mSrvCert); } -#endif // OPENTHREAD_CONFIG_TLS_ENABLE +#endif endpointError = otTcpEndpointDeinitialize(&mEndpoint); mSendBusy = false; @@ -374,20 +378,21 @@ template <> otError TcpExample::Process(Arg aArgs[]) { otError error; otSockAddr sockaddr; - bool nat64SynthesizedAddress; + bool nat64Synth; uint32_t flags; VerifyOrExit(mInitialized, error = OT_ERROR_INVALID_STATE); - SuccessOrExit( - error = Interpreter::ParseToIp6Address(GetInstancePtr(), aArgs[0], sockaddr.mAddress, nat64SynthesizedAddress)); - if (nat64SynthesizedAddress) + SuccessOrExit(error = Interpreter::ParseToIp6Address(GetInstancePtr(), aArgs[0], sockaddr.mAddress, nat64Synth)); + + if (nat64Synth) { OutputFormat("Connecting to synthesized IPv6 address: "); OutputIp6AddressLine(sockaddr.mAddress); } SuccessOrExit(error = aArgs[1].ParseAsUint16(sockaddr.mPort)); + if (aArgs[2].IsEmpty()) { flags = OT_TCP_CONNECT_NO_FAST_OPEN; @@ -406,6 +411,7 @@ template <> otError TcpExample::Process(Arg aArgs[]) { ExitNow(error = OT_ERROR_INVALID_ARGS); } + VerifyOrExit(aArgs[3].IsEmpty(), error = OT_ERROR_INVALID_ARGS); } @@ -419,7 +425,7 @@ template <> otError TcpExample::Process(Arg aArgs[]) OutputLine("mbedtls_ssl_config_defaults returned %d", rv); } } -#endif // OPENTHREAD_CONFIG_TLS_ENABLE +#endif SuccessOrExit(error = otTcpConnect(&mEndpoint, &sockaddr, flags)); mEndpointConnected = true; @@ -466,16 +472,19 @@ template <> otError TcpExample::Process(Arg aArgs[]) { int rv = mbedtls_ssl_write(&mSslContext, reinterpret_cast(aArgs[0].GetCString()), aArgs[0].GetLength()); + if (rv < 0 && rv != MBEDTLS_ERR_SSL_WANT_WRITE && rv != MBEDTLS_ERR_SSL_WANT_READ) { - ExitNow(error = kErrorFailed); + ExitNow(error = OT_ERROR_FAILED); } - error = kErrorNone; + + error = OT_ERROR_NONE; } else -#endif // OPENTHREAD_CONFIG_TLS_ENABLE +#endif { size_t written; + SuccessOrExit(error = otTcpCircularSendBufferWrite(&mEndpoint, &mSendBuffer, aArgs[0].GetCString(), aArgs[0].GetLength(), &written, 0)); } @@ -526,6 +535,7 @@ template <> otError TcpExample::Process(Arg aArgs[]) if (aArgs[0] == "result") { OutputFormat("TCP Benchmark Status: "); + if (mBenchmarkBytesTotal != 0) { OutputLine("Ongoing"); @@ -571,6 +581,7 @@ template <> otError TcpExample::Process(Arg aArgs[]) SuccessOrExit(error = aArgs[1].ParseAsUint32(mBenchmarkBytesTotal)); VerifyOrExit(mBenchmarkBytesTotal != 0, error = OT_ERROR_INVALID_ARGS); } + VerifyOrExit(aArgs[2].IsEmpty(), error = OT_ERROR_INVALID_ARGS); mBenchmarkStart = TimerMilli::GetNow(); @@ -594,10 +605,12 @@ template <> otError TcpExample::Process(Arg aArgs[]) mBenchmarkLinks[i].mNext = nullptr; mBenchmarkLinks[i].mData = mSendBufferBytes; mBenchmarkLinks[i].mLength = sizeof(mSendBufferBytes); + if (i == 0 && mBenchmarkBytesTotal % sizeof(mSendBufferBytes) != 0) { mBenchmarkLinks[i].mLength = mBenchmarkBytesTotal % sizeof(mSendBufferBytes); } + error = otTcpSendByReference(&mEndpoint, &mBenchmarkLinks[i], i == toSendOut - 1 ? 0 : OT_TCP_SEND_MORE_TO_COME); VerifyOrExit(error == OT_ERROR_NONE, mBenchmarkBytesTotal = 0); @@ -812,7 +825,7 @@ void TcpExample::HandleTcpEstablished(otTcpEndpoint *aEndpoint) PrepareTlsHandshake(); ContinueTlsHandshake(); } -#endif // OPENTHREAD_CONFIG_TLS_ENABLE +#endif } void TcpExample::HandleTcpSendDone(otTcpEndpoint *aEndpoint, otLinkedBuffer *aData) @@ -834,10 +847,13 @@ void TcpExample::HandleTcpSendDone(otTcpEndpoint *aEndpoint, otLinkedBuffer *aDa { OT_ASSERT(aData != &mSendLink); OT_ASSERT(mBenchmarkBytesUnsent >= aData->mLength); + mBenchmarkBytesUnsent -= aData->mLength; // could be less than sizeof(mSendBufferBytes) for the first link + if (mBenchmarkBytesUnsent >= OT_ARRAY_LENGTH(mBenchmarkLinks) * sizeof(mSendBufferBytes)) { aData->mLength = sizeof(mSendBufferBytes); + if (otTcpSendByReference(&mEndpoint, aData, 0) != OT_ERROR_NONE) { OutputLine("TCP Benchmark Failed"); @@ -907,7 +923,7 @@ void TcpExample::HandleTcpReceiveAvailable(otTcpEndpoint *aEndpoint, #if OPENTHREAD_CONFIG_TLS_ENABLE if (mUseTls && ContinueTlsHandshake()) { - return; + ExitNow(); } #endif @@ -917,15 +933,18 @@ void TcpExample::HandleTcpReceiveAvailable(otTcpEndpoint *aEndpoint, if (mUseTls) { uint8_t buffer[500]; + for (;;) { int rv = mbedtls_ssl_read(&mSslContext, buffer, sizeof(buffer)); + if (rv < 0) { if (rv == MBEDTLS_ERR_SSL_WANT_READ) { break; } + OutputLine("TLS receive failure: %d", rv); } else @@ -941,13 +960,16 @@ void TcpExample::HandleTcpReceiveAvailable(otTcpEndpoint *aEndpoint, { const otLinkedBuffer *data; size_t totalReceived = 0; + IgnoreError(otTcpReceiveByReference(aEndpoint, &data)); + for (; data != nullptr; data = data->mNext) { OutputLine("TCP: Received %u bytes: %.*s", static_cast(data->mLength), static_cast(data->mLength), reinterpret_cast(data->mData)); totalReceived += data->mLength; } + OT_ASSERT(aBytesAvailable == totalReceived); IgnoreReturnValue(otTcpCommitReceive(aEndpoint, totalReceived, 0)); } @@ -957,6 +979,11 @@ void TcpExample::HandleTcpReceiveAvailable(otTcpEndpoint *aEndpoint, { OutputLine("TCP: Reached end of stream"); } + + ExitNow(); + +exit: + return; } void TcpExample::HandleTcpDisconnected(otTcpEndpoint *aEndpoint, otTcpDisconnectedReason aReason) @@ -1035,8 +1062,10 @@ otTcpIncomingConnectionAction TcpExample::HandleTcpAcceptReady(otTcpListener { OutputLine("mbedtls_ssl_config_defaults returned %d", rv); } + mbedtls_ssl_conf_ca_chain(&mSslConfig, mSrvCert.next, nullptr); rv = mbedtls_ssl_conf_own_cert(&mSslConfig, &mSrvCert, &mPKey); + if (rv != 0) { OutputLine("mbedtls_ssl_conf_own_cert returned %d", rv); @@ -1076,6 +1105,7 @@ otError TcpExample::ContinueBenchmarkCircularSend(void) { int rv = mbedtls_ssl_write(&mSslContext, reinterpret_cast(sBenchmarkData), toSendThisIteration); + if (rv > 0) { written = static_cast(rv); @@ -1083,12 +1113,13 @@ otError TcpExample::ContinueBenchmarkCircularSend(void) } else if (rv != MBEDTLS_ERR_SSL_WANT_WRITE && rv != MBEDTLS_ERR_SSL_WANT_READ) { - ExitNow(error = kErrorFailed); + ExitNow(error = OT_ERROR_FAILED); } - error = kErrorNone; + + error = OT_ERROR_NONE; } else -#endif // OPENTHREAD_CONFIG_TLS_ENABLE +#endif { SuccessOrExit(error = otTcpCircularSendBufferWrite(&mEndpoint, &mSendBuffer, sBenchmarkData, toSendThisIteration, &written, flag)); @@ -1132,17 +1163,21 @@ void TcpExample::CompleteBenchmark(void) void TcpExample::PrepareTlsHandshake(void) { int rv; + rv = mbedtls_ssl_set_hostname(&mSslContext, "localhost"); + if (rv != 0) { OutputLine("mbedtls_ssl_set_hostname returned %d", rv); } + rv = mbedtls_ssl_set_hs_ecjpake_password(&mSslContext, reinterpret_cast(sEcjpakePassword), sEcjpakePasswordLength); if (rv != 0) { OutputLine("mbedtls_ssl_set_hs_ecjpake_password returned %d", rv); } + mbedtls_ssl_set_bio(&mSslContext, &mEndpointAndCircularSendBuffer, otTcpMbedTlsSslSendCallback, otTcpMbedTlsSslRecvCallback, nullptr); mTlsHandshakeComplete = false; @@ -1156,6 +1191,7 @@ bool TcpExample::ContinueTlsHandshake(void) if (!mTlsHandshakeComplete) { rv = mbedtls_ssl_handshake(&mSslContext); + if (rv == 0) { OutputLine("TLS Handshake Complete"); @@ -1165,6 +1201,7 @@ bool TcpExample::ContinueTlsHandshake(void) { OutputLine("TLS Handshake Failed: %d", rv); } + wasNotAlreadyDone = true; } diff --git a/src/cli/cli_udp.cpp b/src/cli/cli_udp.cpp index d9b9dc6c8..9f8afdf2a 100644 --- a/src/cli/cli_udp.cpp +++ b/src/cli/cli_udp.cpp @@ -132,11 +132,11 @@ template <> otError UdpExample::Process(Arg aArgs[]) { otError error; otSockAddr sockaddr; - bool nat64SynthesizedAddress; + bool nat64Synth; - SuccessOrExit( - error = Interpreter::ParseToIp6Address(GetInstancePtr(), aArgs[0], sockaddr.mAddress, nat64SynthesizedAddress)); - if (nat64SynthesizedAddress) + SuccessOrExit(error = Interpreter::ParseToIp6Address(GetInstancePtr(), aArgs[0], sockaddr.mAddress, nat64Synth)); + + if (nat64Synth) { OutputFormat("Connecting to synthesized IPv6 address: "); OutputIp6AddressLine(sockaddr.mAddress); @@ -267,11 +267,12 @@ template <> otError UdpExample::Process(Arg aArgs[]) if (!aArgs[2].IsEmpty()) { - bool nat64SynthesizedAddress; + bool nat64Synth; - SuccessOrExit(error = Interpreter::ParseToIp6Address(GetInstancePtr(), aArgs[0], messageInfo.mPeerAddr, - nat64SynthesizedAddress)); - if (nat64SynthesizedAddress) + SuccessOrExit( + error = Interpreter::ParseToIp6Address(GetInstancePtr(), aArgs[0], messageInfo.mPeerAddr, nat64Synth)); + + if (nat64Synth) { OutputFormat("Sending to synthesized IPv6 address: "); OutputIp6AddressLine(messageInfo.mPeerAddr);