87 #ifndef UDP_LOCAL_PORT_RANGE_START 90 #define UDP_LOCAL_PORT_RANGE_START 0xc000 91 #define UDP_LOCAL_PORT_RANGE_END 0xffff 92 #define UDP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & ~UDP_LOCAL_PORT_RANGE_START) + UDP_LOCAL_PORT_RANGE_START)) 96 static u16_t udp_port = UDP_LOCAL_PORT_RANGE_START;
100 struct udp_pcb *udp_pcbs;
108 #if LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) 109 udp_port = UDP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND());
125 if (udp_port++ == UDP_LOCAL_PORT_RANGE_END) {
126 udp_port = UDP_LOCAL_PORT_RANGE_START;
129 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
130 if (pcb->local_port == udp_port) {
131 if (++n > (UDP_LOCAL_PORT_RANGE_END - UDP_LOCAL_PORT_RANGE_START)) {
149 udp_input_local_match(
struct udp_pcb *pcb,
struct netif *inp, u8_t broadcast)
155 if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
156 #if LWIP_IPV4 && IP_SOF_BROADCAST_RECV 157 if ((broadcast != 0) && !
ip_get_option(pcb, SOF_BROADCAST)) {
169 if (broadcast != 0) {
170 #if IP_SOF_BROADCAST_RECV 174 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) ||
175 ((ip4_current_dest_addr()->addr == IPADDR_BROADCAST)) ||
176 ip4_addr_netcmp(ip_2_ip4(&pcb->local_ip), ip4_current_dest_addr(), netif_ip4_netmask(inp))) {
204 udp_input(
struct pbuf *p,
struct netif *inp)
207 struct udp_pcb *pcb, *prev;
208 struct udp_pcb *uncon_pcb;
217 UDP_STATS_INC(udp.recv);
220 if (p->
len < UDP_HLEN) {
223 (
"udp_input: short UDP datagram (%"U16_F
" bytes) discarded\n", p->
tot_len));
224 UDP_STATS_INC(udp.lenerr);
225 UDP_STATS_INC(udp.drop);
226 MIB2_STATS_INC(mib2.udpinerrors);
239 src = lwip_ntohs(udphdr->src);
240 dest = lwip_ntohs(udphdr->dest);
242 udp_debug_print(udphdr);
258 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
261 ip_addr_debug_print(
UDP_DEBUG, &pcb->local_ip);
263 ip_addr_debug_print(
UDP_DEBUG, &pcb->remote_ip);
267 if ((pcb->local_port == dest) &&
268 (udp_input_local_match(pcb, inp, broadcast) != 0)) {
269 if (((pcb->flags & UDP_FLAGS_CONNECTED) == 0) &&
273 || !ip_addr_isany(&pcb->local_ip)
281 if ((pcb->remote_port == src) &&
282 (ip_addr_isany_val(pcb->remote_ip) ||
288 prev->next = pcb->next;
289 pcb->next = udp_pcbs;
292 UDP_STATS_INC(udp.cachehit);
310 if (ip_current_is_v6()) {
311 for_us = netif_get_ip6_addr_match(inp, ip6_current_dest_addr()) >= 0;
315 if (!ip_current_is_v6()) {
316 for_us = ip4_addr_cmp(netif_ip4_addr(inp), ip4_current_dest_addr());
323 #if CHECKSUM_CHECK_UDP 326 if (ip_current_header_proto() == IP_PROTO_UDPLITE) {
328 u16_t chklen = lwip_ntohs(udphdr->len);
329 if (chklen <
sizeof(
struct udp_hdr)) {
340 if (ip_chksum_pseudo_partial(p, IP_PROTO_UDPLITE,
348 if (udphdr->chksum != 0) {
349 if (ip_chksum_pseudo(p, IP_PROTO_UDP, p->
tot_len,
360 LWIP_ASSERT(
"pbuf_header failed\n", 0);
361 UDP_STATS_INC(udp.drop);
362 MIB2_STATS_INC(mib2.udpinerrors);
368 MIB2_STATS_INC(mib2.udpindatagrams);
369 #if SO_REUSE && SO_REUSE_RXTOALL 374 struct udp_pcb *mpcb;
375 u8_t p_header_changed = 0;
377 for (mpcb = udp_pcbs; mpcb != NULL; mpcb = mpcb->next) {
380 if ((mpcb->local_port == dest) &&
381 (udp_input_local_match(mpcb, inp, broadcast) != 0)) {
383 if (mpcb->recv != NULL) {
386 if (p_header_changed == 0) {
388 p_header_changed = 1;
403 if (p_header_changed) {
410 if (pcb->recv != NULL) {
421 #if LWIP_ICMP || LWIP_ICMP6 427 icmp_port_unreach(ip_current_is_v6(), p);
430 UDP_STATS_INC(udp.proterr);
431 UDP_STATS_INC(udp.drop);
432 MIB2_STATS_INC(mib2.udpnoports);
439 PERF_STOP(
"udp_input");
441 #if CHECKSUM_CHECK_UDP 444 (
"udp_input: UDP (or UDP Lite) datagram discarded due to failing checksum\n"));
445 UDP_STATS_INC(udp.chkerr);
446 UDP_STATS_INC(udp.drop);
447 MIB2_STATS_INC(mib2.udpinerrors);
449 PERF_STOP(
"udp_input");
474 udp_send(
struct udp_pcb *pcb,
struct pbuf *p)
476 if ((pcb == NULL) || IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) {
481 return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port);
484 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 489 udp_send_chksum(
struct udp_pcb *pcb,
struct pbuf *p,
490 u8_t have_chksum, u16_t chksum)
492 if ((pcb == NULL) || IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) {
497 return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port,
498 have_chksum, chksum);
521 udp_sendto(
struct udp_pcb *pcb,
struct pbuf *p,
522 const ip_addr_t *dst_ip, u16_t dst_port)
524 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 525 return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0);
531 udp_sendto_chksum(
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *dst_ip,
532 u16_t dst_port, u8_t have_chksum, u16_t chksum)
536 const ip_addr_t *dst_ip_route = dst_ip;
538 if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
544 #if LWIP_IPV6 || (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) 545 if (ip_addr_ismulticast(dst_ip_route)) {
547 if (IP_IS_V6(dst_ip)) {
549 dst_ip_route = &pcb->local_ip;
553 #if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS 558 if (!ip_addr_isany_val(pcb->multicast_ip) &&
559 !ip4_addr_cmp(ip_2_ip4(&pcb->multicast_ip), IP4_ADDR_BROADCAST)) {
560 dst_ip_route = &pcb->multicast_ip;
568 if(IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
570 netif = ip_route(IP46_ADDR_ANY(IP_GET_TYPE(dst_ip_route)), dst_ip_route);
572 netif = ip_route(&pcb->local_ip, dst_ip_route);
580 UDP_STATS_INC(udp.rterr);
583 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 584 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum);
586 return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
611 udp_sendto_if(
struct udp_pcb *pcb,
struct pbuf *p,
612 const ip_addr_t *dst_ip, u16_t dst_port,
struct netif *netif)
614 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 615 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0);
620 udp_sendto_if_chksum(
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *dst_ip,
621 u16_t dst_port,
struct netif *netif, u8_t have_chksum,
625 const ip_addr_t *src_ip;
627 if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
633 if (IP_IS_V6(dst_ip)) {
634 if (ip6_addr_isany(ip_2_ip6(&pcb->local_ip))) {
635 src_ip = ip6_select_source_address(netif, ip_2_ip6(dst_ip));
636 if (src_ip == NULL) {
642 if (netif_get_ip6_addr_match(netif, ip_2_ip6(&pcb->local_ip)) < 0) {
646 src_ip = &pcb->local_ip;
650 #if LWIP_IPV4 && LWIP_IPV6 654 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) ||
655 ip4_addr_ismulticast(ip_2_ip4(&pcb->local_ip))) {
658 src_ip = netif_ip_addr4(netif);
662 if (!ip4_addr_cmp(ip_2_ip4(&(pcb->local_ip)), netif_ip4_addr(netif))) {
667 src_ip = &pcb->local_ip;
670 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 671 return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum, src_ip);
673 return udp_sendto_if_src(pcb, p, dst_ip, dst_port, netif, src_ip);
680 udp_sendto_if_src(
struct udp_pcb *pcb,
struct pbuf *p,
681 const ip_addr_t *dst_ip, u16_t dst_port,
struct netif *netif,
const ip_addr_t *src_ip)
683 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 684 return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0, src_ip);
689 udp_sendto_if_src_chksum(
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *dst_ip,
690 u16_t dst_port,
struct netif *netif, u8_t have_chksum,
691 u16_t chksum,
const ip_addr_t *src_ip)
700 if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) ||
701 !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
705 #if LWIP_IPV4 && IP_SOF_BROADCAST 711 ip_addr_isbroadcast(dst_ip, netif)) {
713 (
"udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (
void *)pcb));
719 if (pcb->local_port == 0) {
721 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
743 (
"udp_send: added header pbuf %p before given pbuf %p\n", (
void *)q, (
void *)p));
750 LWIP_ASSERT(
"check that first pbuf can hold struct udp_hdr",
754 udphdr->src = lwip_htons(pcb->local_port);
755 udphdr->dest = lwip_htons(dst_port);
757 udphdr->chksum = 0x0000;
760 #if (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) || (LWIP_IPV6 && LWIP_IPV6_MLD) 761 if (((pcb->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) {
770 if (pcb->flags & UDP_FLAGS_UDPLITE) {
771 u16_t chklen, chklen_hdr;
774 chklen_hdr = chklen = pcb->chksum_len_tx;
788 udphdr->len = lwip_htons(chklen_hdr);
791 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) {
792 #if LWIP_CHECKSUM_ON_COPY 797 udphdr->chksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDPLITE,
798 q->
tot_len, chklen, src_ip, dst_ip);
799 #if LWIP_CHECKSUM_ON_COPY 802 acc = udphdr->chksum + (u16_t)~(chksum);
808 if (udphdr->chksum == 0x0000) {
809 udphdr->chksum = 0xffff;
814 ip_proto = IP_PROTO_UDPLITE;
819 udphdr->len = lwip_htons(q->
tot_len);
822 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) {
824 if (IP_IS_V6(dst_ip) || (pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
826 #if LWIP_CHECKSUM_ON_COPY 829 udpchksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDP,
830 q->
tot_len, UDP_HLEN, src_ip, dst_ip);
831 acc = udpchksum + (u16_t)~(chksum);
836 udpchksum = ip_chksum_pseudo(q, IP_PROTO_UDP, q->
tot_len,
841 if (udpchksum == 0x0000) {
844 udphdr->chksum = udpchksum;
848 ip_proto = IP_PROTO_UDP;
852 #if LWIP_MULTICAST_TX_OPTIONS 853 ttl = (ip_addr_ismulticast(dst_ip) ? udp_get_multicast_ttl(pcb) : pcb->ttl);
861 NETIF_SET_HWADDRHINT(netif, &(pcb->addr_hint));
862 err = ip_output_if_src(q, src_ip, dst_ip, ttl, pcb->tos, ip_proto, netif);
863 NETIF_SET_HWADDRHINT(netif, NULL);
866 MIB2_STATS_INC(mib2.udpoutdatagrams);
876 UDP_STATS_INC(udp.xmit);
901 udp_bind(
struct udp_pcb *pcb,
const ip_addr_t *ipaddr, u16_t port)
903 struct udp_pcb *ipcb;
908 if (ipaddr == NULL) {
909 ipaddr = IP4_ADDR_ANY;
914 if ((pcb == NULL) || (ipaddr == NULL)) {
924 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
934 port = udp_new_port();
941 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
952 if ((ipcb->local_port == port) &&
954 ip_addr_cmp(&ipcb->local_ip, ipaddr)) {
957 (
"udp_bind: local port %"U16_F
" already bound by another pcb\n", port));
965 ip_addr_set_ipaddr(&pcb->local_ip, ipaddr);
967 pcb->local_port = port;
972 pcb->next = udp_pcbs;
1000 udp_connect(
struct udp_pcb *pcb,
const ip_addr_t *ipaddr, u16_t port)
1002 struct udp_pcb *ipcb;
1004 if ((pcb == NULL) || (ipaddr == NULL)) {
1008 if (pcb->local_port == 0) {
1009 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
1015 ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr);
1016 pcb->remote_port = port;
1017 pcb->flags |= UDP_FLAGS_CONNECTED;
1025 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
1032 pcb->next = udp_pcbs;
1044 udp_disconnect(
struct udp_pcb *pcb)
1047 #if LWIP_IPV4 && LWIP_IPV6 1048 if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
1049 ip_addr_copy(pcb->remote_ip, *IP_ANY_TYPE);
1052 ip_addr_set_any(IP_IS_V6_VAL(pcb->remote_ip), &pcb->remote_ip);
1053 #if LWIP_IPV4 && LWIP_IPV6 1056 pcb->remote_port = 0;
1058 pcb->flags &= ~UDP_FLAGS_CONNECTED;
1072 udp_recv(
struct udp_pcb *pcb, udp_recv_fn recv,
void *recv_arg)
1076 pcb->recv_arg = recv_arg;
1089 udp_remove(
struct udp_pcb *pcb)
1091 struct udp_pcb *pcb2;
1093 mib2_udp_unbind(pcb);
1095 if (udp_pcbs == pcb) {
1097 udp_pcbs = udp_pcbs->next;
1100 for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
1102 if (pcb2->next != NULL && pcb2->next == pcb) {
1104 pcb2->next = pcb->next;
1124 struct udp_pcb *pcb;
1125 pcb = (
struct udp_pcb *)
memp_malloc(MEMP_UDP_PCB);
1132 memset(pcb, 0,
sizeof(
struct udp_pcb));
1134 #if LWIP_MULTICAST_TX_OPTIONS 1135 udp_set_multicast_ttl(pcb,
UDP_TTL);
1154 udp_new_ip_type(u8_t type)
1156 struct udp_pcb *pcb;
1158 #if LWIP_IPV4 && LWIP_IPV6 1160 IP_SET_TYPE_VAL(pcb->local_ip, type);
1161 IP_SET_TYPE_VAL(pcb->remote_ip, type);
1174 void udp_netif_ip_addr_changed(
const ip_addr_t* old_addr,
const ip_addr_t* new_addr)
1176 struct udp_pcb* upcb;
1178 if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) {
1179 for (upcb = udp_pcbs; upcb != NULL; upcb = upcb->next) {
1181 if (ip_addr_cmp(&upcb->local_ip, old_addr)) {
1184 ip_addr_copy(upcb->local_ip, *new_addr);
1197 udp_debug_print(
struct udp_hdr *udphdr)
1202 lwip_ntohs(udphdr->src), lwip_ntohs(udphdr->dest)));
1205 lwip_ntohs(udphdr->len), lwip_ntohs(udphdr->chksum)));
#define CHECKSUM_CHECK_UDP
#define ip_current_src_addr()
#define ip_current_header_tot_len()
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
#define ip_current_netif()
void memp_free(memp_t type, void *mem)
err_t pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from)
#define ip_current_dest_addr()
#define LWIP_DBG_LEVEL_SERIOUS
#define PBUF_FLAG_MCASTLOOP
void pbuf_chain(struct pbuf *h, struct pbuf *t)
#define LWIP_DEBUGF(debug, message)
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
u8_t pbuf_header_force(struct pbuf *p, s16_t header_size_increment)
#define LWIP_UNUSED_ARG(x)
u8_t pbuf_free(struct pbuf *p)
void * memp_malloc(memp_t type)
#define ip_get_option(pcb, opt)