LwIP 接收函数udp_input精简版

本文深入探讨了LwIP网络库中的udp_input函数,解析了其如何处理UDP数据包的接收流程,包括数据包的解析、端口匹配和回调函数的调用,为理解LwIP UDP通信提供了关键洞察。
摘要由CSDN通过智能技术生成
void
udp_input(struct pbuf *p, struct netif *inp)
{
   
  struct udp_hdr *udphdr;
  struct udp_pcb *pcb, *prev;
  struct udp_pcb *uncon_pcb;
  u16_t src, dest;
  u8_t broadcast;
  u8_t for_us = 0;


  /* Check minimum length (UDP header) 检查最小长度,不能小于UDP首部*/
  if (p->len < UDP_HLEN) {
   
    /* drop short packets */
    pbuf_free(p);
    goto end;
  }
  //指向UDP报文首部,并且强制转换成udp_hdr类型,方便操作
  udphdr = (struct udp_hdr *)p->payload;

  /* is broadcast packet ? 判断是不是广播包*/
  broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif());


  /* convert src and dest ports to host byte order 得到UDP首部中的源地址和目标主机端口号*/
  src = lwip_ntohs(udphdr->src);
  dest = lwip_ntohs(udphdr->dest);



  pcb = NULL;
  prev = NULL;
  uncon_pcb = NULL;
  /* Iterate through the UDP pcb list for a matching pcb.遍历UDP链表,找到对应的端口号,如果找不到,
   * 'Perfect match' pcbs (connected to the remote port & ip address) are就用链表的第一个未使用的UDP控制块
   * preferred. If no perfect match is found, the first unconnected pcb that
   * matches the local port and ip address gets the datagram. */
  for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值