/** receive callback function */ udp_recv_fn recv; /** user-supplied argument for the recv callback */ void *recv_arg; };
IP_PCB控制块结构体定义,准确来说,它是所有控制块的父类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/** This is the common part of all PCB types. It needs to be at the beginning of a PCB type definition. It is located here so that changes to this common part are made in one location instead of having to change all PCB structs. */ #define IP_PCB \ /* ip addresses in network byte order */ \ ip_addr_t local_ip; \ ip_addr_t remote_ip; \ /* Bound netif index */ \ u8_t netif_idx; \ /* Socket options */ \ u8_t so_options; \ /* Type Of Service */ \ u8_t tos; \ /* Time To Live */ \ u8_t ttl \ /* link layer address resolution hint */ \ IP_PCB_NETIFHINT
/** @ingroup udp_raw * Same as @ref udp_sendto_if, but with source address */ err_t udp_sendto_if_src(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif, const ip_addr_t *src_ip) { struct udp_hdr *udphdr; err_t err; struct pbuf *q; /* q will be sent down the stack */ u8_t ip_proto; u8_t ttl;
/* if the PCB is not yet bound to a port, bind it here */ if (pcb->local_port == 0) { err = udp_bind(pcb, &pcb->local_ip, pcb->local_port); if (err != ERR_OK) { return err; } }
/* packet too large to add a UDP header without causing an overflow? */ if ((u16_t)(p->tot_len + UDP_HLEN) < p->tot_len) { return ERR_MEM; }
/* not enough space to add an UDP header to first pbuf in given p chain? */ if (pbuf_add_header(p, UDP_HLEN)) { /* allocate header in a separate new pbuf */ q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM); if (q == NULL) { return ERR_MEM; } if (p->tot_len != 0) { /* chain header q in front of given pbuf p */ pbuf_chain(q, p); } } else { q = p; } udphdr = (struct udp_hdr *)q->payload; udphdr->src = lwip_htons(pcb->local_port); udphdr->dest = lwip_htons(dst_port); udphdr->chksum = 0x0000;