70 #ifndef UDP_LOCAL_PORT_RANGE_START 73 #define UDP_LOCAL_PORT_RANGE_START 0xc000 74 #define UDP_LOCAL_PORT_RANGE_END 0xffff 75 #define UDP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & ~UDP_LOCAL_PORT_RANGE_START) + UDP_LOCAL_PORT_RANGE_START)) 79 static u16_t udp_port = UDP_LOCAL_PORT_RANGE_START;
83 struct udp_pcb *udp_pcbs;
91 #if LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) 92 udp_port = UDP_ENSURE_LOCAL_PORT_RANGE(
LWIP_RAND());
108 if (udp_port++ == UDP_LOCAL_PORT_RANGE_END) {
109 udp_port = UDP_LOCAL_PORT_RANGE_START;
112 for (pcb = udp_pcbs; pcb !=
NULL; pcb = pcb->next) {
113 if (pcb->local_port == udp_port) {
114 if (++n > (UDP_LOCAL_PORT_RANGE_END - UDP_LOCAL_PORT_RANGE_START)) {
122 struct udp_pcb *ipcb = udp_pcbs;
123 while ((ipcb !=
NULL) && (udp_port != UDP_LOCAL_PORT_RANGE_END)) {
124 if (ipcb->local_port == udp_port) {
154 udp_input(
struct pbuf *p,
struct netif *inp)
156 struct udp_hdr *udphdr;
157 struct udp_pcb *pcb, *prev;
158 struct udp_pcb *uncon_pcb;
173 if (p->
len < UDP_HLEN) {
176 (
"udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->
tot_len));
184 udphdr = (
struct udp_hdr *)p->
payload;
194 src =
ntohs(udphdr->src);
195 dest =
ntohs(udphdr->dest);
197 udp_debug_print(udphdr);
210 if (dest == DHCP_CLIENT_PORT) {
212 if (src == DHCP_SERVER_PORT) {
213 if ((inp->dhcp !=
NULL) && (inp->dhcp->pcb !=
NULL)) {
220 pcb = inp->dhcp->pcb;
234 for (pcb = udp_pcbs; pcb !=
NULL; pcb = pcb->next) {
244 if (pcb->local_port == dest) {
248 (ip6_addr_isany(
ip_2_ip6(&pcb->local_ip)) ||
250 ip6_addr_ismulticast(ip6_current_dest_addr()) ||
252 ip6_addr_cmp(
ip_2_ip6(&pcb->local_ip), ip6_current_dest_addr())))
256 (ip4_current_header() !=
NULL) &&
279 if ((uncon_pcb ==
NULL) &&
280 ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) {
287 if ((local_match != 0) &&
295 prev->next = pcb->next;
296 pcb->next = udp_pcbs;
316 if (ip_current_is_v6()) {
317 for_us = netif_get_ip6_addr_match(inp, ip6_current_dest_addr()) >= 0;
325 for_us = ip4_addr_cmp(netif_ip4_addr(inp), ip4_current_dest_addr());
331 #if CHECKSUM_CHECK_UDP 337 if (chklen <
sizeof(
struct udp_hdr)) {
356 if (udphdr->chksum != 0) {
376 #if SO_REUSE && SO_REUSE_RXTOALL 382 ip6_addr_ismulticast(ip6_current_dest_addr()) ||
388 struct udp_pcb *mpcb;
389 u8_t p_header_changed = 0;
391 for (mpcb = udp_pcbs; mpcb !=
NULL; mpcb = mpcb->next) {
394 if ((mpcb->local_port == dest) &&
397 (ip6_addr_ismulticast(ip6_current_dest_addr()) ||
398 ip6_addr_cmp(
ip_2_ip6(&mpcb->local_ip), ip6_current_dest_addr()))) ||
422 if (mpcb->recv !=
NULL) {
425 if (p_header_changed == 0) {
427 p_header_changed = 1;
442 if (p_header_changed) {
449 if (pcb->recv !=
NULL) {
460 #if LWIP_ICMP || LWIP_ICMP6 471 !ip6_addr_ismulticast(ip6_current_dest_addr())
490 #if CHECKSUM_CHECK_UDP 493 (
"udp_input: UDP (or UDP Lite) datagram discarded due to failing checksum\n"));
521 udp_send(
struct udp_pcb *pcb,
struct pbuf *p)
524 return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port);
527 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 531 udp_send_chksum(
struct udp_pcb *pcb,
struct pbuf *p,
535 return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port,
536 have_chksum, chksum);
558 udp_sendto(
struct udp_pcb *pcb,
struct pbuf *p,
561 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 562 return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0);
567 udp_sendto_chksum(
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *dst_ip,
580 #if LWIP_IPV6 || (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) 585 dst_ip_route = &pcb->local_ip;
589 #if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS 595 !ip4_addr_cmp(ip_2_ip4(&pcb->multicast_ip), IP4_ADDR_BROADCAST)) {
596 dst_ip_route = & pcb->multicast_ip;
604 netif = ip_route(
PCB_ISIPV6(pcb), &pcb->local_ip, dst_ip_route);
614 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 615 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum);
617 return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
641 udp_sendto_if(
struct udp_pcb *pcb,
struct pbuf *p,
644 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 645 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0);
650 udp_sendto_if_chksum(
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *dst_ip,
651 u16_t dst_port,
struct netif *netif,
u8_t have_chksum,
664 if (ip6_addr_isany(
ip_2_ip6(&pcb->local_ip))) {
665 src_ip = ip6_select_source_address(netif,
ip_2_ip6(dst_ip));
666 if (src_ip ==
NULL) {
672 if (netif_get_ip6_addr_match(netif,
ip_2_ip6(&pcb->local_ip)) < 0) {
676 src_ip = &pcb->local_ip;
680 #if LWIP_IPV4 && LWIP_IPV6 684 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip))) {
686 src_ip = netif_ip_addr4(netif);
690 if (!ip4_addr_cmp(ip_2_ip4(&(pcb->local_ip)), netif_ip4_addr(netif))) {
695 src_ip = &pcb->local_ip;
698 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 699 return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum, src_ip);
701 return udp_sendto_if_src(pcb, p, dst_ip, dst_port, netif, src_ip);
707 udp_sendto_if_src(
struct udp_pcb *pcb,
struct pbuf *p,
710 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 711 return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0, src_ip);
716 udp_sendto_if_src_chksum(
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *dst_ip,
717 u16_t dst_port,
struct netif *netif,
u8_t have_chksum,
721 struct udp_hdr *udphdr;
732 #if LWIP_IPV4 && IP_SOF_BROADCAST 740 (
"udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (
void *)pcb));
746 if (pcb->local_port == 0) {
748 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
770 (
"udp_send: added header pbuf %p before given pbuf %p\n", (
void *)q, (
void *)p));
777 LWIP_ASSERT(
"check that first pbuf can hold struct udp_hdr",
778 (q->
len >=
sizeof(
struct udp_hdr)));
780 udphdr = (
struct udp_hdr *)q->
payload;
781 udphdr->src =
htons(pcb->local_port);
782 udphdr->dest =
htons(dst_port);
784 udphdr->chksum = 0x0000;
787 #if (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) || (LWIP_IPV6 && LWIP_IPV6_MLD) 797 if (pcb->flags & UDP_FLAGS_UDPLITE) {
798 u16_t chklen, chklen_hdr;
801 chklen_hdr = chklen = pcb->chksum_len_tx;
802 if ((chklen <
sizeof(
struct udp_hdr)) || (chklen > q->
tot_len)) {
815 udphdr->len =
htons(chklen_hdr);
819 #if LWIP_CHECKSUM_ON_COPY 825 q->
tot_len, chklen, src_ip, dst_ip);
826 #if LWIP_CHECKSUM_ON_COPY 829 acc = udphdr->chksum + (
u16_t)~(chksum);
835 if (udphdr->chksum == 0x0000) {
836 udphdr->chksum = 0xffff;
851 if (
PCB_ISIPV6(pcb) || (pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
853 #if LWIP_CHECKSUM_ON_COPY 857 q->
tot_len, UDP_HLEN, src_ip, dst_ip);
858 acc = udpchksum + (
u16_t)~(chksum);
868 if (udpchksum == 0x0000) {
871 udphdr->chksum = udpchksum;
879 #if LWIP_MULTICAST_TX_OPTIONS 889 err = ip_output_if_src(
PCB_ISIPV6(pcb), q, src_ip, dst_ip, ttl, pcb->tos, ip_proto, netif);
927 udp_bind(
struct udp_pcb *pcb,
const ip_addr_t *ipaddr,
u16_t port)
929 struct udp_pcb *ipcb;
942 for (ipcb = udp_pcbs; ipcb !=
NULL; ipcb = ipcb->next) {
952 port = udp_new_port();
959 for (ipcb = udp_pcbs; ipcb !=
NULL; ipcb = ipcb->next) {
977 (
"udp_bind: local port %"U16_F" already bound by another pcb\n", port));
987 pcb->local_port = port;
992 pcb->next = udp_pcbs;
1019 udp_connect(
struct udp_pcb *pcb,
const ip_addr_t *ipaddr,
u16_t port)
1021 struct udp_pcb *ipcb;
1027 if (pcb->local_port == 0) {
1028 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
1035 pcb->remote_port = port;
1036 pcb->flags |= UDP_FLAGS_CONNECTED;
1038 #ifdef LWIP_UDP_TODO 1045 struct netif *netif;
1067 for (ipcb = udp_pcbs; ipcb !=
NULL; ipcb = ipcb->next) {
1074 pcb->next = udp_pcbs;
1085 udp_disconnect(
struct udp_pcb *pcb)
1089 pcb->remote_port = 0;
1091 pcb->flags &= ~UDP_FLAGS_CONNECTED;
1104 udp_recv(
struct udp_pcb *pcb, udp_recv_fn recv,
void *recv_arg)
1108 pcb->recv_arg = recv_arg;
1120 udp_remove(
struct udp_pcb *pcb)
1122 struct udp_pcb *pcb2;
1126 if (udp_pcbs == pcb) {
1128 udp_pcbs = udp_pcbs->next;
1131 for (pcb2 = udp_pcbs; pcb2 !=
NULL; pcb2 = pcb2->next) {
1133 if (pcb2->next !=
NULL && pcb2->next == pcb) {
1135 pcb2->next = pcb->next;
1154 struct udp_pcb *pcb;
1155 pcb = (
struct udp_pcb *)
memp_malloc(MEMP_UDP_PCB);
1162 memset(pcb, 0,
sizeof(
struct udp_pcb));
1164 #if LWIP_MULTICAST_TX_OPTIONS 1183 struct udp_pcb *pcb;
1198 void udp_netif_ipv4_addr_changed(
const ip4_addr_t* old_addr,
const ip4_addr_t* new_addr)
1200 struct udp_pcb* upcb;
1202 if (!ip4_addr_isany(new_addr)) {
1203 for (upcb = udp_pcbs; upcb !=
NULL; upcb = upcb->next) {
1207 if (!ip4_addr_isany(ip_2_ip4(&upcb->local_ip)) &&
1208 ip4_addr_cmp(ip_2_ip4(&upcb->local_ip), old_addr)) {
1211 ip_addr_copy_from_ip4(upcb->local_ip, *new_addr);
1226 udp_debug_print(
struct udp_hdr *udphdr)
u16_t ip_chksum_pseudo(struct pbuf *p, u8_t proto, u16_t proto_len, const ip_addr_t *src, const ip_addr_t *dest)
#define MIB2_STATS_INC(x)
#define ip_current_netif()
#define LWIP_DBG_LEVEL_SERIOUS
#define ip_addr_isany(ipaddr)
#define IP_SOF_BROADCAST_RECV
#define IP_PCB_IPVER_EQ(pcb1, pcb2)
#define ip_current_header_tot_len()
#define ip_addr_isany_val(ipaddr)
#define icmp_port_unreach(isipv6, pbuf)
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
#define IF__NETIF_CHECKSUM_ENABLED(netif, chksumflag)
#define ip_addr_isbroadcast(addr, netif)
void memp_free(memp_t type, void *mem)
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
#define ip_current_dest_addr()
#define mib2_udp_unbind(pcb)
#define mib2_udp_bind(pcb)
#define ip_addr_ismulticast(ipaddr)
#define IP_ADDR_PCB_VERSION_MATCH(addr, pcb)
#define PBUF_FLAG_MCASTLOOP
u8_t pbuf_free(struct pbuf *p)
err_t pbuf_copy(struct pbuf *p_to, struct pbuf *p_from)
#define ip_addr_netcmp(addr1, addr2, mask)
void pbuf_chain(struct pbuf *h, struct pbuf *t)
#define ip_addr_set_any(is_ipv6, ipaddr)
#define NETIF_SET_HWADDRHINT(netif, hint)
#define IP_IS_V6_VAL(ipaddr)
#define ip_addr_cmp(addr1, addr2)
#define LWIP_ASSERT(message, assertion)
#define ip_addr_set(dest, src)
#define ip_addr_debug_print(debug, ipaddr)
#define ipaddr_ntoa(ipaddr)
#define ip_current_src_addr()
u8_t pbuf_header_force(struct pbuf *p, s16_t header_size_increment)
u16_t ip_chksum_pseudo_partial(struct pbuf *p, u8_t proto, u16_t proto_len, u16_t chksum_len, const ip_addr_t *src, const ip_addr_t *dest)
#define IP_PCB_IPVER_INPUT_MATCH(pcb)
void * memp_malloc(memp_t type)
#define LWIP_DEBUGF(debug, message)
#define CHECKSUM_CHECK_UDP
#define LWIP_UNUSED_ARG(x)
#define ip_get_option(pcb, opt)
#define ip_addr_set_ipaddr(dest, src)