|
1 | 1 | /* |
2 | | - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD |
| 2 | + * SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD |
3 | 3 | * |
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
@@ -153,21 +153,29 @@ static err_t openthread_netif_init(struct netif *netif) |
153 | 153 |
|
154 | 154 | const ip_addr_t *lwip_hook_ip6_select_source_address(struct netif *netif, const ip6_addr_t *dest) |
155 | 155 | { |
156 | | - const ip6_addr_t *cur_addr; |
| 156 | + ip6_addr_t cand_addr = { 0 }; |
157 | 157 | uint8_t idx = 0; |
| 158 | + otError err = OT_ERROR_NONE; |
158 | 159 | // Only process with ot netif. |
159 | 160 | if (!(netif->name[0] == 'o' && netif->name[1] == 't')) { |
160 | 161 | return NULL; |
161 | 162 | } |
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 | + } |
171 | 179 | } |
172 | 180 | } |
173 | 181 | return NULL; |
|
0 commit comments