From cf5352fb4329f1ec83c7eb623372479ad77f9d90 Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Thu, 8 Nov 2018 14:35:39 +0800 Subject: [PATCH] [api] add API to find source address (#3273) --- include/openthread/ip6.h | 11 +++++++++++ src/core/api/ip6_api.cpp | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/include/openthread/ip6.h b/include/openthread/ip6.h index 7a0e7b6c4..bc025c63b 100644 --- a/include/openthread/ip6.h +++ b/include/openthread/ip6.h @@ -537,6 +537,17 @@ OTAPI uint8_t OTCALL otIp6PrefixMatch(const otIp6Address *aFirst, const otIp6Add */ bool otIp6IsAddressUnspecified(const otIp6Address *aAddress); +/** + * This function perform OpenThread source address selection. + * + * @param[inout] aMessageInfo A pointer to the message information. + * + * @retval OT_ERROR_NONE Found a source address and is filled into mSockAddr of @p aMessageInfo. + * @retval OT_ERROR_NOT_FOUND No source address was found and @p aMessageInfo is unchanged. + * + */ +otError otIp6SelectSourceAddress(otInstance *aInstance, otMessageInfo *aMessageInfo); + /** * @} * diff --git a/src/core/api/ip6_api.cpp b/src/core/api/ip6_api.cpp index 8459f6ad3..c13b7ad5f 100644 --- a/src/core/api/ip6_api.cpp +++ b/src/core/api/ip6_api.cpp @@ -264,3 +264,17 @@ bool otIp6IsAddressUnspecified(const otIp6Address *aAddress) { return static_cast(aAddress)->IsUnspecified(); } + +otError otIp6SelectSourceAddress(otInstance *aInstance, otMessageInfo *aMessageInfo) +{ + otError error = OT_ERROR_NONE; + Instance & instance = *static_cast(aInstance); + const Ip6::NetifUnicastAddress *netifAddr; + + netifAddr = instance.GetIp6().SelectSourceAddress(*static_cast(aMessageInfo)); + VerifyOrExit(netifAddr != NULL, error = OT_ERROR_NOT_FOUND); + memcpy(&aMessageInfo->mSockAddr, &netifAddr->mAddress, sizeof(aMessageInfo->mSockAddr)); + +exit: + return error; +}