Skip to content

Commit bf12839

Browse files
committed
fix(openthread): use OpenThread API in lwIP source address selection hook
1 parent 77660c2 commit bf12839

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

components/openthread/src/esp_openthread_lwip_netif.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -153,21 +153,29 @@ static err_t openthread_netif_init(struct netif *netif)
153153

154154
const ip_addr_t *lwip_hook_ip6_select_source_address(struct netif *netif, const ip6_addr_t *dest)
155155
{
156-
const ip6_addr_t *cur_addr;
156+
ip6_addr_t cand_addr = { 0 };
157157
uint8_t idx = 0;
158+
otError err = OT_ERROR_NONE;
158159
// Only process with ot netif.
159160
if (!(netif->name[0] == 'o' && netif->name[1] == 't')) {
160161
return NULL;
161162
}
162-
// Currently, prefer the address with the same prefix of the destination address.
163-
// If no address found, return NULL for selection source address using the default algorithm.
164-
for (idx = 0; idx < LWIP_IPV6_NUM_ADDRESSES; idx++) {
165-
if (!ip6_addr_isvalid(netif_ip6_addr_state(netif, idx))) {
166-
continue;
167-
}
168-
cur_addr = netif_ip6_addr(netif, idx);
169-
if (ip6_addr_netcmp_zoneless(cur_addr, dest)) {
170-
return netif_ip_addr6(netif, idx);
163+
otMessageInfo message_info = { 0 };
164+
memcpy(message_info.mPeerAddr.mFields.m32, dest->addr, sizeof(message_info.mPeerAddr.mFields.m32));
165+
otInstance *instance = esp_openthread_get_instance();
166+
esp_openthread_task_switching_lock_acquire(portMAX_DELAY);
167+
err = otIp6SelectSourceAddress(instance, &message_info);
168+
esp_openthread_task_switching_lock_release();
169+
if (err == OT_ERROR_NONE) {
170+
// If a Src address was selected by the OT stack, use this address.
171+
memcpy(cand_addr.addr, message_info.mSockAddr.mFields.m32, sizeof(cand_addr.addr));
172+
for (idx = 0; idx < LWIP_IPV6_NUM_ADDRESSES; idx++) {
173+
if (!ip6_addr_isvalid(netif_ip6_addr_state(netif, idx))) {
174+
continue;
175+
}
176+
if (ip6_addr_zoneless_eq(netif_ip6_addr(netif, idx), &cand_addr)) {
177+
return netif_ip_addr6(netif, idx);
178+
}
171179
}
172180
}
173181
return NULL;

0 commit comments

Comments
 (0)