具有多个netif接口的LwIP回显客户端,如何选择特定的netif接口

问题描述 投票:0回答:1

在STM32H753上,我从STM32Cube_FW_H7运行了一个示例tcp回显客户端。但是,该板具有三个具有不同IP地址的网络接口,所有接口均由LwIP(1x LAN8742 + 2x KSZ8851)提供服务。

使用ping,通过断开其他端口的连接,我验证了正确的端口正在响应。例如,将IP 192.168.0.101分配给端口1,将IP 192.168.0.102分配给端口2,将IP 192.168.0.103分配给端口3。在ethernetif * .c中,三个接口的名称分别为“ st”,“ M1”和“ M2”。

一切正常,但是tcp_echoclient_connect函数始终选择第3个端口作为本地端口。如何告诉LwIP选择第一个或第二个端口?

tcp_echoclient_connect函数调用

/* connect to destination address/port */
tcp_connect(echoclient_pcb, &DestIPaddr, DEST_PORT, tcp_echoclient_connected);

然后选择第三个接口(和IP地址)。

在tcp.c中,函数tcp_connect显示了如何获取合适的接口:

/* check if we have a route to the remote host */
if (ip_addr_isany(&pcb->local_ip)) {
    /* no local IP address set, yet. */
    struct netif *netif;
    const ip_addr_t *local_ip;
    ip_route_get_local_ip(&pcb->local_ip, &pcb->remote_ip, netif, local_ip);
    if ((netif == NULL) || (local_ip == NULL)) {
        /* Don't even try to send a SYN packet if we have no route
        since that will fail. */
        return ERR_RTE;
    }
    /* Use the address as local address of the pcb. */
    ip_addr_copy(pcb->local_ip, *local_ip);
}

所以我想指定一个特定的netif(接口)。我该怎么办?

当我运行tcp echo服务器时,它使用正确的界面。我可以将消息发送到相应的端口,并且它会回复。错误的端口无法响应。

我的来源https://github.com/bkht/LAN8742A_KSZ8851SNL_LwIP

注意:ip_route_get_local_ip调用ip_route,该ip_route返回与网络掩码匹配的第一个netif。是ip4.c中的功能ip4_route。

就目前而言,由于找不到另一种选择特殊的netif的方法,我创建了一些函数来通过名称选择所需的netif:在ip4.c

struct netif *ip4_route_by_name(const char *name, const ip4_addr_t *dest)

在tcp.c中

err_t tcp_connect_by_name(const char *name, struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port, tcp_connected_fn connected)

在tcp_echoclient服务器中,我在创建连接时指定一个netif名称:

/* from a specified netif connect to destination address/port */
tcp_connect_by_name("m1", echoclient_pcb, &DestIPaddr, DEST_PORT, tcp_echoclient_connected);

但是也许已经存在用于指定与netif名称,MAC地址或IP地址匹配的netif的功能?

tcp stm32 hal lwip
1个回答
0
投票

所以,您是说您可以使用lwip堆栈处理两个物理以太网端口?

© www.soinside.com 2019 - 2024. All rights reserved.