测试socket之[1]:bind一个同网段非本地地址

20200804

这几天设计了socket,设计的socket的bind 功能是:创建socket并绑定IP和port,想测试各种情况。

bind 同网段非本地IP地址,那么是可以bind成功的,看看udp_bind(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)里面的内容就看出来,没有netif相关的东西。

但是udp却无法send成功,因为send之前要选网卡,由于bind一个同网段非本地地址,所以可以找到这个网段对应的网卡,但是进入udp_sendto_if函数后,却发现IP不一样,如下:

      if (netif_get_ip6_addr_match(netif, ip_2_ip6(&pcb->local_ip)) < 0) {
        /* Address isn't valid anymore. */
        return ERR_RTE;
      }

对于接收来说:

进入udp_input(struct pbuf *p, struct netif *inp)函数后,进入:

  /* no fully matching pcb found? then look for an unconnected pcb */
  if (pcb == NULL) {
    pcb = uncon_pcb;
  }

所以说:bind一个同网段非本地地址,既发不出去也收不到,仅仅能bind成功,并分配socket fd描述符

猜你喜欢

转载自blog.csdn.net/unsv29/article/details/107780425