68 #ifndef UDP_LOCAL_PORT_RANGE_START
71 #define UDP_LOCAL_PORT_RANGE_START 0xc000
72 #define UDP_LOCAL_PORT_RANGE_END 0xffff
73 #define UDP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & ~UDP_LOCAL_PORT_RANGE_START) + UDP_LOCAL_PORT_RANGE_START))
77 static u16_t udp_port = UDP_LOCAL_PORT_RANGE_START;
81 struct udp_pcb *udp_pcbs;
89 #if LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND)
90 udp_port = UDP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND());
106 if (udp_port++ == UDP_LOCAL_PORT_RANGE_END) {
107 udp_port = UDP_LOCAL_PORT_RANGE_START;
110 for (pcb = udp_pcbs; pcb !=
NULL; pcb = pcb->next) {
111 if (pcb->local_port == udp_port) {
112 if (++n > (UDP_LOCAL_PORT_RANGE_END - UDP_LOCAL_PORT_RANGE_START)) {
130 udp_input_local_match(
struct udp_pcb *pcb,
struct netif *inp,
u8_t broadcast)
137 #if LWIP_IPV4 && IP_SOF_BROADCAST_RECV
150 if (broadcast != 0) {
151 #if IP_SOF_BROADCAST_RECV
155 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) ||
156 ((ip4_current_dest_addr()->addr == IPADDR_BROADCAST)) ||
157 ip4_addr_netcmp(ip_2_ip4(&pcb->local_ip), ip4_current_dest_addr(), netif_ip4_netmask(inp))) {
185 udp_input(
struct pbuf *p,
struct netif *inp)
188 struct udp_pcb *pcb, *prev;
189 struct udp_pcb *uncon_pcb;
204 (
"udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->
tot_len));
223 udp_debug_print(udphdr);
239 for (pcb = udp_pcbs; pcb !=
NULL; pcb = pcb->next) {
248 if ((pcb->local_port == dest) &&
249 (udp_input_local_match(pcb, inp, broadcast) != 0)) {
250 if (((pcb->flags & UDP_FLAGS_CONNECTED) == 0) &&
262 if ((pcb->remote_port == src) &&
269 prev->next = pcb->next;
270 pcb->next = udp_pcbs;
291 if (ip_current_is_v6()) {
292 for_us = netif_get_ip6_addr_match(inp, ip6_current_dest_addr()) >= 0;
296 if (!ip_current_is_v6()) {
297 for_us = ip4_addr_cmp(netif_ip4_addr(inp), ip4_current_dest_addr());
304 #if CHECKSUM_CHECK_UDP
310 if (chklen <
sizeof(
struct udp_hdr)) {
329 if (udphdr->chksum != 0) {
350 #if SO_REUSE && SO_REUSE_RXTOALL
355 struct udp_pcb *mpcb;
356 u8_t p_header_changed = 0;
358 for (mpcb = udp_pcbs; mpcb !=
NULL; mpcb = mpcb->next) {
361 if ((mpcb->local_port == dest) &&
362 (udp_input_local_match(mpcb, inp, broadcast) != 0)) {
364 if (mpcb->recv !=
NULL) {
367 if (p_header_changed == 0) {
369 p_header_changed = 1;
384 if (p_header_changed) {
391 if (pcb->recv !=
NULL) {
402 #if LWIP_ICMP || LWIP_ICMP6
422 #if CHECKSUM_CHECK_UDP
425 (
"udp_input: UDP (or UDP Lite) datagram discarded due to failing checksum\n"));
455 udp_send(
struct udp_pcb *pcb,
struct pbuf *p)
462 return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port);
465 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
470 udp_send_chksum(
struct udp_pcb *pcb,
struct pbuf *p,
478 return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port,
479 have_chksum, chksum);
502 udp_sendto(
struct udp_pcb *pcb,
struct pbuf *p,
505 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
506 return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0);
512 udp_sendto_chksum(
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *dst_ip,
525 #if LWIP_IPV6 || (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS)
530 dst_ip_route = &pcb->local_ip;
534 #if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS
540 !ip4_addr_cmp(ip_2_ip4(&pcb->multicast_ip), IP4_ADDR_BROADCAST)) {
541 dst_ip_route = &pcb->multicast_ip;
553 netif = ip_route(&pcb->local_ip, dst_ip_route);
564 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
565 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port,
netif, have_chksum, chksum);
567 return udp_sendto_if(pcb, p, dst_ip, dst_port,
netif);
592 udp_sendto_if(
struct udp_pcb *pcb,
struct pbuf *p,
595 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
596 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port,
netif, 0, 0);
601 udp_sendto_if_chksum(
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *dst_ip,
615 if (ip6_addr_isany(
ip_2_ip6(&pcb->local_ip))) {
617 if (src_ip ==
NULL) {
623 if (netif_get_ip6_addr_match(
netif,
ip_2_ip6(&pcb->local_ip)) < 0) {
627 src_ip = &pcb->local_ip;
631 #if LWIP_IPV4 && LWIP_IPV6
635 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) ||
636 ip4_addr_ismulticast(ip_2_ip4(&pcb->local_ip))) {
639 src_ip = netif_ip_addr4(
netif);
643 if (!ip4_addr_cmp(ip_2_ip4(&(pcb->local_ip)), netif_ip4_addr(
netif))) {
648 src_ip = &pcb->local_ip;
651 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
652 return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port,
netif, have_chksum, chksum, src_ip);
654 return udp_sendto_if_src(pcb, p, dst_ip, dst_port,
netif, src_ip);
661 udp_sendto_if_src(
struct udp_pcb *pcb,
struct pbuf *p,
664 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
665 return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port,
netif, 0, 0, src_ip);
670 udp_sendto_if_src_chksum(
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *dst_ip,
686 #if LWIP_IPV4 && IP_SOF_BROADCAST
694 (
"udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (
void *)pcb));
700 if (pcb->local_port == 0) {
702 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
724 (
"udp_send: added header pbuf %p before given pbuf %p\n", (
void *)q, (
void *)p));
731 LWIP_ASSERT(
"check that first pbuf can hold struct udp_hdr",
738 udphdr->chksum = 0x0000;
741 #if (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) || (LWIP_IPV6 && LWIP_IPV6_MLD)
751 if (pcb->flags & UDP_FLAGS_UDPLITE) {
752 u16_t chklen, chklen_hdr;
755 chklen_hdr = chklen = pcb->chksum_len_tx;
773 #if LWIP_CHECKSUM_ON_COPY
779 q->
tot_len, chklen, src_ip, dst_ip);
780 #if LWIP_CHECKSUM_ON_COPY
783 acc = udphdr->chksum + (
u16_t)~(chksum);
789 if (udphdr->chksum == 0x0000) {
790 udphdr->chksum = 0xffff;
805 if (
IP_IS_V6(dst_ip) || (pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
807 #if LWIP_CHECKSUM_ON_COPY
812 acc = udpchksum + (
u16_t)~(chksum);
822 if (udpchksum == 0x0000) {
825 udphdr->chksum = udpchksum;
833 #if LWIP_MULTICAST_TX_OPTIONS
843 err = ip_output_if_src(q, src_ip, dst_ip, ttl, pcb->tos, ip_proto,
netif);
882 udp_bind(
struct udp_pcb *pcb,
const ip_addr_t *ipaddr,
u16_t port)
884 struct udp_pcb *ipcb;
889 if (ipaddr ==
NULL) {
890 ipaddr = IP4_ADDR_ANY;
895 if ((pcb ==
NULL) || (ipaddr ==
NULL)) {
905 for (ipcb = udp_pcbs; ipcb !=
NULL; ipcb = ipcb->next) {
915 port = udp_new_port();
922 for (ipcb = udp_pcbs; ipcb !=
NULL; ipcb = ipcb->next) {
933 if ((ipcb->local_port == port) &&
938 (
"udp_bind: local port %"U16_F" already bound by another pcb\n", port));
948 pcb->local_port = port;
953 pcb->next = udp_pcbs;
981 udp_connect(
struct udp_pcb *pcb,
const ip_addr_t *ipaddr,
u16_t port)
983 struct udp_pcb *ipcb;
985 if ((pcb ==
NULL) || (ipaddr ==
NULL)) {
989 if (pcb->local_port == 0) {
990 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
997 pcb->remote_port = port;
998 pcb->flags |= UDP_FLAGS_CONNECTED;
1006 for (ipcb = udp_pcbs; ipcb !=
NULL; ipcb = ipcb->next) {
1013 pcb->next = udp_pcbs;
1025 udp_disconnect(
struct udp_pcb *pcb)
1028 #if LWIP_IPV4 && LWIP_IPV6
1034 #if LWIP_IPV4 && LWIP_IPV6
1037 pcb->remote_port = 0;
1039 pcb->flags &= ~UDP_FLAGS_CONNECTED;
1053 udp_recv(
struct udp_pcb *pcb, udp_recv_fn recv,
void *recv_arg)
1057 pcb->recv_arg = recv_arg;
1070 udp_remove(
struct udp_pcb *pcb)
1072 struct udp_pcb *pcb2;
1076 if (udp_pcbs == pcb) {
1078 udp_pcbs = udp_pcbs->next;
1081 for (pcb2 = udp_pcbs; pcb2 !=
NULL; pcb2 = pcb2->next) {
1083 if (pcb2->next !=
NULL && pcb2->next == pcb) {
1085 pcb2->next = pcb->next;
1105 struct udp_pcb *pcb;
1106 pcb = (
struct udp_pcb *)
memp_malloc(MEMP_UDP_PCB);
1113 memset(pcb, 0,
sizeof(
struct udp_pcb));
1115 #if LWIP_MULTICAST_TX_OPTIONS
1116 udp_set_multicast_ttl(pcb,
UDP_TTL);
1135 udp_new_ip_type(
u8_t type)
1137 struct udp_pcb *pcb;
1139 #if LWIP_IPV4 && LWIP_IPV6
1157 struct udp_pcb* upcb;
1160 for (upcb = udp_pcbs; upcb !=
NULL; upcb = upcb->next) {
1178 udp_debug_print(
struct udp_hdr *udphdr)