Skip to content

Commit b72d714

Browse files
authored
[cli] refactor IPv6 address parsing and synthesis helper (openthread#13205)
This commit renames the static helper `Utils::ParseToIp6Address()` to `Utils::ParseOrSynthesizeIp6Address()` to better reflect its behavior of parsing an IPv6 address or synthesizing one from an IPv4 address via NAT64. Additionally, the method is refactored into a non-static member of the `Utils` class. This eliminates the need to manually pass the `otInstance` pointer, as the `Utils` class already maintains it. The internal implementation is also simplified to reduce nesting by exiting early upon successful IPv6 address parsing. All callers in the CLI module (TCP, UDP, Ping, DNS) have been updated to use the new member method.
1 parent b2093f4 commit b72d714

6 files changed

Lines changed: 24 additions & 25 deletions

File tree

src/cli/cli_dns.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,8 @@ otError Dns::GetDnsConfig(Arg aArgs[], otDnsQueryConfig *&aConfig)
452452

453453
VerifyOrExit(!aArgs[0].IsEmpty(), aConfig = nullptr);
454454

455-
SuccessOrExit(error = ParseToIp6Address(GetInstancePtr(), aArgs[0], aConfig->mServerSockAddr.mAddress, nat64Synth));
455+
SuccessOrExit(error = ParseOrSynthesizeIp6Address(aArgs[0], aConfig->mServerSockAddr.mAddress, nat64Synth));
456+
456457
if (nat64Synth)
457458
{
458459
OutputFormat("Synthesized IPv6 DNS server address: ");

src/cli/cli_ping.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ otError PingSender::Process(Arg aArgs[])
9696
aArgs++;
9797
}
9898

99-
SuccessOrExit(error = ParseToIp6Address(GetInstancePtr(), aArgs[0], config.mDestination, nat64Synth));
99+
SuccessOrExit(error = ParseOrSynthesizeIp6Address(aArgs[0], config.mDestination, nat64Synth));
100100

101101
if (nat64Synth)
102102
{

src/cli/cli_tcp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ template <> otError TcpExample::Process<Cmd("connect")>(Arg aArgs[])
400400

401401
VerifyOrExit(mInitialized, error = OT_ERROR_INVALID_STATE);
402402

403-
SuccessOrExit(error = ParseToIp6Address(GetInstancePtr(), aArgs[0], sockaddr.mAddress, nat64Synth));
403+
SuccessOrExit(error = ParseOrSynthesizeIp6Address(aArgs[0], sockaddr.mAddress, nat64Synth));
404404

405405
if (nat64Synth)
406406
{

src/cli/cli_udp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ template <> otError UdpExample::Process<Cmd("connect")>(Arg aArgs[])
144144
otSockAddr sockaddr;
145145
bool nat64Synth;
146146

147-
SuccessOrExit(error = ParseToIp6Address(GetInstancePtr(), aArgs[0], sockaddr.mAddress, nat64Synth));
147+
SuccessOrExit(error = ParseOrSynthesizeIp6Address(aArgs[0], sockaddr.mAddress, nat64Synth));
148148

149149
if (nat64Synth)
150150
{
@@ -281,7 +281,7 @@ template <> otError UdpExample::Process<Cmd("send")>(Arg aArgs[])
281281
{
282282
bool nat64Synth;
283283

284-
SuccessOrExit(error = ParseToIp6Address(GetInstancePtr(), aArgs[0], messageInfo.mPeerAddr, nat64Synth));
284+
SuccessOrExit(error = ParseOrSynthesizeIp6Address(aArgs[0], messageInfo.mPeerAddr, nat64Synth));
285285

286286
if (nat64Synth)
287287
{

src/cli/cli_utils.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -629,25 +629,27 @@ const char *Utils::PreferenceToString(signed int aPreference)
629629
}
630630

631631
#if OPENTHREAD_FTD || OPENTHREAD_MTD
632-
otError Utils::ParseToIp6Address(otInstance *aInstance, const Arg &aArg, otIp6Address &aAddress, bool &aSynthesized)
632+
633+
otError Utils::ParseOrSynthesizeIp6Address(const Arg &aArg, otIp6Address &aAddress, bool &aSynthesized)
633634
{
634-
Error error = OT_ERROR_NONE;
635+
Error error;
636+
otIp4Address ip4Address;
635637

636-
VerifyOrExit(!aArg.IsEmpty(), error = OT_ERROR_INVALID_ARGS);
637-
error = aArg.ParseAsIp6Address(aAddress);
638638
aSynthesized = false;
639639

640-
if (error != OT_ERROR_NONE)
641-
{
642-
// It might be an IPv4 address, let's have a try.
643-
otIp4Address ip4Address;
640+
error = aArg.ParseAsIp6Address(aAddress);
644641

645-
// Do not touch the error value if we failed to parse it as an IPv4 address.
646-
SuccessOrExit(aArg.ParseAsIp4Address(ip4Address));
647-
SuccessOrExit(error = otNat64SynthesizeIp6Address(aInstance, &ip4Address, &aAddress));
648-
aSynthesized = true;
642+
if (error == OT_ERROR_NONE)
643+
{
644+
ExitNow();
649645
}
650646

647+
// Try to parse it as an IPv4 address and synthesize.
648+
649+
SuccessOrExit(error = aArg.ParseAsIp4Address(ip4Address));
650+
SuccessOrExit(error = otNat64SynthesizeIp6Address(GetInstancePtr(), &ip4Address, &aAddress));
651+
aSynthesized = true;
652+
651653
exit:
652654
return error;
653655
}

src/cli/cli_utils.hpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -674,24 +674,20 @@ class Utils
674674
static const char *PreferenceToString(signed int aPreference);
675675

676676
/**
677-
* Parses the argument as an IP address.
677+
* Parses the argument as an IPv6 address or synthesizes it from an IPv4 address.
678678
*
679679
* If the argument string is an IPv4 address, this method will try to synthesize an IPv6 address using preferred
680680
* NAT64 prefix in the network data.
681681
*
682-
* @param[in] aInstance A pointer to OpenThread instance.
683682
* @param[in] aArg The argument string to parse.
684-
* @param[out] aAddress A reference to an `otIp6Address` to output the parsed IPv6 address.
683+
* @param[out] aAddress A reference to an `otIp6Address` to output the parsed/synthesized IPv6 address.
685684
* @param[out] aSynthesized Whether @p aAddress is synthesized from an IPv4 address.
686685
*
687-
* @retval OT_ERROR_NONE The argument was parsed successfully.
686+
* @retval OT_ERROR_NONE The argument was parsed/synthesized successfully.
688687
* @retval OT_ERROR_INVALID_ARGS The argument is empty or does not contain a valid IP address.
689688
* @retval OT_ERROR_INVALID_STATE No valid NAT64 prefix in the network data.
690689
*/
691-
static otError ParseToIp6Address(otInstance *aInstance,
692-
const Arg &aArg,
693-
otIp6Address &aAddress,
694-
bool &aSynthesized);
690+
otError ParseOrSynthesizeIp6Address(const Arg &aArg, otIp6Address &aAddress, bool &aSynthesized);
695691

696692
/**
697693
* Parses the argument as a Joiner Discerner.

0 commit comments

Comments
 (0)