57 #include "lwip/ip_addr.h"
59 #include "lwip/icmp.h"
69 struct udp_pcb *udp_pcbs;
84 udp_input(
struct pbuf *p,
struct netif *inp)
86 struct udp_hdr *udphdr;
87 struct udp_pcb *pcb, *prev;
88 struct udp_pcb *uncon_pcb;
105 (
"udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->
tot_len));
113 udphdr = (
struct udp_hdr *)p->
payload;
121 src =
ntohs(udphdr->src);
122 dest =
ntohs(udphdr->dest);
124 udp_debug_print(udphdr);
139 if (dest == DHCP_CLIENT_PORT) {
141 if (src == DHCP_SERVER_PORT) {
142 if ((inp->dhcp !=
NULL) && (inp->dhcp->pcb !=
NULL)) {
148 pcb = inp->dhcp->pcb;
162 for (pcb = udp_pcbs; pcb !=
NULL; pcb = pcb->next) {
166 (
"pcb (%"U16_F
".%"U16_F
".%"U16_F
".%"U16_F
", %"U16_F
") --- "
167 "(%"U16_F
".%"U16_F
".%"U16_F
".%"U16_F
", %"U16_F
")\n",
174 if ((pcb->local_port == dest) &&
186 if ((uncon_pcb ==
NULL) &&
187 ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) {
193 if ((local_match != 0) &&
194 (pcb->remote_port == src) &&
201 prev->next = pcb->next;
202 pcb->next = udp_pcbs;
223 #if CHECKSUM_CHECK_UDP
225 if (chklen <
sizeof(
struct udp_hdr)) {
243 (
"udp_input: UDP Lite datagram discarded due to failing checksum\n"));
254 #if CHECKSUM_CHECK_UDP
255 if (udphdr->chksum != 0) {
259 (
"udp_input: UDP datagram discarded due to failing checksum\n"));
279 #if SO_REUSE && SO_REUSE_RXTOALL
284 struct udp_pcb *mpcb;
285 u8_t p_header_changed = 0;
286 for (mpcb = udp_pcbs; mpcb !=
NULL; mpcb = mpcb->next) {
289 if ((mpcb->local_port == dest) &&
301 if (mpcb->recv !=
NULL) {
304 if (p_header_changed == 0) {
306 p_header_changed = 1;
321 if (p_header_changed) {
328 if (pcb->recv !=
NULL) {
381 udp_send(
struct udp_pcb *pcb,
struct pbuf *p)
384 return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port);
387 #if LWIP_CHECKSUM_ON_COPY
391 udp_send_chksum(
struct udp_pcb *pcb,
struct pbuf *p,
395 return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port,
396 have_chksum, chksum);
418 udp_sendto(
struct udp_pcb *pcb,
struct pbuf *p,
421 #if LWIP_CHECKSUM_ON_COPY
422 return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0);
427 udp_sendto_chksum(
struct udp_pcb *pcb,
struct pbuf *p,
ip_addr_t *dst_ip,
449 #if LWIP_CHECKSUM_ON_COPY
450 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum);
452 return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
476 udp_sendto_if(
struct udp_pcb *pcb,
struct pbuf *p,
479 #if LWIP_CHECKSUM_ON_COPY
480 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0);
485 udp_sendto_if_chksum(
struct udp_pcb *pcb,
struct pbuf *p,
ip_addr_t *dst_ip,
486 u16_t dst_port,
struct netif *netif,
u8_t have_chksum,
490 struct udp_hdr *udphdr;
499 (
"udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (
void *)pcb));
505 if (pcb->local_port == 0) {
507 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
529 (
"udp_send: added header pbuf %p before given pbuf %p\n", (
void *)q, (
void *)p));
536 LWIP_ASSERT(
"check that first pbuf can hold struct udp_hdr",
537 (q->
len >=
sizeof(
struct udp_hdr)));
539 udphdr = (
struct udp_hdr *)q->
payload;
540 udphdr->src =
htons(pcb->local_port);
541 udphdr->dest =
htons(dst_port);
543 udphdr->chksum = 0x0000;
571 src_ip = &(pcb->local_ip);
578 if (pcb->flags & UDP_FLAGS_UDPLITE) {
579 u16_t chklen, chklen_hdr;
582 chklen_hdr = chklen = pcb->chksum_len_tx;
583 if ((chklen <
sizeof(
struct udp_hdr)) || (chklen > q->
tot_len)) {
596 udphdr->len =
htons(chklen_hdr);
604 (have_chksum ? UDP_HLEN : chklen));
607 acc = udphdr->chksum + (
u16_t)~(chksum);
613 if (udphdr->chksum == 0x0000) {
614 udphdr->chksum = 0xffff;
619 #if LWIP_NETIF_HWADDRHINT
620 netif->addr_hint = &(pcb->addr_hint);
623 #if LWIP_NETIF_HWADDRHINT
624 netif->addr_hint =
NULL;
633 if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
635 #if LWIP_CHECKSUM_ON_COPY
640 acc = udpchksum + (
u16_t)~(chksum);
649 if (udpchksum == 0x0000) {
652 udphdr->chksum = udpchksum;
658 #if LWIP_NETIF_HWADDRHINT
659 netif->addr_hint = &(pcb->addr_hint);
662 #if LWIP_NETIF_HWADDRHINT
663 netif->addr_hint =
NULL;
703 struct udp_pcb *ipcb;
712 for (ipcb = udp_pcbs; ipcb !=
NULL; ipcb = ipcb->next) {
731 if ((ipcb->local_port == port) &&
738 (
"udp_bind: local port %"U16_F
" already bound by another pcb\n", port));
748 #ifndef UDP_LOCAL_PORT_RANGE_START
749 #define UDP_LOCAL_PORT_RANGE_START 4096
750 #define UDP_LOCAL_PORT_RANGE_END 0x7fff
752 port = UDP_LOCAL_PORT_RANGE_START;
754 while ((ipcb !=
NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) {
755 if (ipcb->local_port == port) {
771 pcb->local_port = port;
776 pcb->next = udp_pcbs;
780 (
"udp_bind: bound to %"U16_F
".%"U16_F
".%"U16_F
".%"U16_F
", port %"U16_F
"\n",
806 struct udp_pcb *ipcb;
808 if (pcb->local_port == 0) {
809 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
816 pcb->remote_port = port;
817 pcb->flags |= UDP_FLAGS_CONNECTED;
832 pcb->local_ip = netif->
ip_addr;
834 pcb->local_ip.addr = 0;
838 (
"udp_connect: connected to %"U16_F
".%"U16_F
".%"U16_F
".%"U16_F
",port %"U16_F
"\n",
844 for (ipcb = udp_pcbs; ipcb !=
NULL; ipcb = ipcb->next) {
851 pcb->next = udp_pcbs;
862 udp_disconnect(
struct udp_pcb *pcb)
866 pcb->remote_port = 0;
868 pcb->flags &= ~UDP_FLAGS_CONNECTED;
881 udp_recv(
struct udp_pcb *pcb, udp_recv_fn recv,
void *recv_arg)
885 pcb->recv_arg = recv_arg;
897 udp_remove(
struct udp_pcb *pcb)
899 struct udp_pcb *pcb2;
903 if (udp_pcbs == pcb) {
905 udp_pcbs = udp_pcbs->next;
908 for (pcb2 = udp_pcbs; pcb2 !=
NULL; pcb2 = pcb2->next) {
910 if (pcb2->next !=
NULL && pcb2->next == pcb) {
912 pcb2->next = pcb->next;
938 memset(pcb, 0,
sizeof(
struct udp_pcb));
951 udp_debug_print(
struct udp_hdr *udphdr)