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) & (u16_t)~UDP_LOCAL_PORT_RANGE_START) + UDP_LOCAL_PORT_RANGE_START))
77static u16_t udp_port = UDP_LOCAL_PORT_RANGE_START;
81struct udp_pcb *udp_pcbs;
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)) {
146#if LWIP_IPV4 && IP_SOF_BROADCAST_RECV
160#if IP_SOF_BROADCAST_RECV
164 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) ||
165 ((ip4_current_dest_addr()->
addr == IPADDR_BROADCAST)) ||
166 ip4_addr_net_eq(ip_2_ip4(&pcb->local_ip), ip4_current_dest_addr(), netif_ip4_netmask(inp))) {
197 struct udp_pcb *pcb, *prev;
198 struct udp_pcb *uncon_pcb;
218 (
"udp_input: short UDP datagram (%"U16_F" bytes) discarded\n",
p->tot_len));
253 for (pcb = udp_pcbs; pcb !=
NULL; pcb = pcb->
next) {
262 if ((pcb->local_port ==
dest) &&
263 (udp_input_local_match(pcb, inp,
broadcast) != 0)) {
264 if ((pcb->flags & UDP_FLAGS_CONNECTED) == 0) {
265 if (uncon_pcb ==
NULL) {
269 }
else if (
broadcast && ip4_current_dest_addr()->
addr == IPADDR_BROADCAST) {
271 if (!
IP_IS_V4_VAL(uncon_pcb->local_ip) || !ip4_addr_eq(ip_2_ip4(&uncon_pcb->local_ip), netif_ip4_addr(inp))) {
273 if (
IP_IS_V4_VAL(pcb->local_ip) && ip4_addr_eq(ip_2_ip4(&pcb->local_ip), netif_ip4_addr(inp))) {
289 if ((pcb->remote_port ==
src) &&
296 prev->next = pcb->next;
297 pcb->next = udp_pcbs;
318 if (ip_current_is_v6()) {
319 for_us = netif_get_ip6_addr_match(inp, ip6_current_dest_addr()) >= 0;
323 if (!ip_current_is_v6()) {
324 for_us = ip4_addr_eq(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) {
377#if SO_REUSE && SO_REUSE_RXTOALL
382 struct udp_pcb *mpcb;
383 for (mpcb = udp_pcbs; mpcb !=
NULL; mpcb = mpcb->
next) {
386 if ((mpcb->local_port ==
dest) &&
387 (udp_input_local_match(mpcb, inp,
broadcast) != 0)) {
389 if (mpcb->recv !=
NULL) {
402 if (pcb->recv !=
NULL) {
413#if LWIP_ICMP || LWIP_ICMP6
433#if CHECKSUM_CHECK_UDP
436 (
"udp_input: UDP (or UDP Lite) datagram discarded due to failing checksum\n"));
467udp_send(
struct udp_pcb *pcb,
struct pbuf *
p)
477 return udp_sendto(pcb,
p, &pcb->remote_ip, pcb->remote_port);
480#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
485udp_send_chksum(
struct udp_pcb *pcb,
struct pbuf *
p,
496 return udp_sendto_chksum(pcb,
p, &pcb->remote_ip, pcb->remote_port,
497 have_chksum, chksum);
520udp_sendto(
struct udp_pcb *pcb,
struct pbuf *
p,
523#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
524 return udp_sendto_chksum(pcb,
p, dst_ip, dst_port, 0, 0);
530udp_sendto_chksum(
struct udp_pcb *pcb,
struct pbuf *
p,
const ip_addr_t *dst_ip,
549#if LWIP_MULTICAST_TX_OPTIONS
572 if (!ip4_addr_isany_val(pcb->mcast_ip4) &&
573 !ip4_addr_eq(&pcb->mcast_ip4, IP4_ADDR_BROADCAST)) {
574 netif = ip4_route_src(ip_2_ip4(&pcb->local_ip), &pcb->mcast_ip4);
584 netif = ip_route(&pcb->local_ip, dst_ip);
596#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
597 return udp_sendto_if_chksum(pcb,
p, dst_ip, dst_port,
netif, have_chksum, chksum);
599 return udp_sendto_if(pcb,
p, dst_ip, dst_port,
netif);
624udp_sendto_if(
struct udp_pcb *pcb,
struct pbuf *
p,
627#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
628 return udp_sendto_if_chksum(pcb,
p, dst_ip, dst_port,
netif, 0, 0);
633udp_sendto_if_chksum(
struct udp_pcb *pcb,
struct pbuf *
p,
const ip_addr_t *dst_ip,
652 if (ip6_addr_isany(
ip_2_ip6(&pcb->local_ip)) ||
653 ip6_addr_ismulticast(
ip_2_ip6(&pcb->local_ip))) {
655 if (src_ip ==
NULL) {
661 if (netif_get_ip6_addr_match(
netif,
ip_2_ip6(&pcb->local_ip)) < 0) {
665 src_ip = &pcb->local_ip;
669#if LWIP_IPV4 && LWIP_IPV6
673 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) ||
674 ip4_addr_ismulticast(ip_2_ip4(&pcb->local_ip))) {
677 src_ip = netif_ip_addr4(
netif);
681 if (!ip4_addr_eq(ip_2_ip4(&(pcb->local_ip)), netif_ip4_addr(
netif))) {
686 src_ip = &pcb->local_ip;
689#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
690 return udp_sendto_if_src_chksum(pcb,
p, dst_ip, dst_port,
netif, have_chksum, chksum, src_ip);
692 return udp_sendto_if_src(pcb,
p, dst_ip, dst_port,
netif, src_ip);
699udp_sendto_if_src(
struct udp_pcb *pcb,
struct pbuf *
p,
702#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
703 return udp_sendto_if_src_chksum(pcb,
p, dst_ip, dst_port,
netif, 0, 0, src_ip);
708udp_sendto_if_src_chksum(
struct udp_pcb *pcb,
struct pbuf *
p,
const ip_addr_t *dst_ip,
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);
768 if (
p->tot_len != 0) {
774 (
"udp_send: added header pbuf %p before given pbuf %p\n", (
void *)
q, (
void *)
p));
781 LWIP_ASSERT(
"check that first pbuf can hold struct udp_hdr",
782 (
q->len >=
sizeof(
struct udp_hdr)));
791#if LWIP_MULTICAST_TX_OPTIONS
801 if (pcb->flags & UDP_FLAGS_UDPLITE) {
802 u16_t chklen, chklen_hdr;
805 chklen_hdr = chklen = pcb->chksum_len_tx;
806 if ((chklen <
sizeof(
struct udp_hdr)) || (chklen >
q->tot_len)) {
823#if LWIP_CHECKSUM_ON_COPY
829 q->tot_len, chklen, src_ip, dst_ip);
830#if LWIP_CHECKSUM_ON_COPY
839 if (
udphdr->chksum == 0x0000) {
855 if (
IP_IS_V6(dst_ip) || (pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
857#if LWIP_CHECKSUM_ON_COPY
862 acc = udpchksum + (
u16_t)~(chksum);
872 if (udpchksum == 0x0000) {
875 udphdr->chksum = udpchksum;
883#if LWIP_MULTICAST_TX_OPTIONS
893 err = ip_output_if_src(
q, src_ip, dst_ip, ttl, pcb->tos, ip_proto,
netif);
934 struct udp_pcb *ipcb;
936#if LWIP_IPV6 && LWIP_IPV6_SCOPES
944 if (ipaddr ==
NULL) {
945 ipaddr = IP4_ADDR_ANY;
959 for (ipcb = udp_pcbs; ipcb !=
NULL; ipcb = ipcb->
next) {
967#if LWIP_IPV6 && LWIP_IPV6_SCOPES
972 if (
IP_IS_V6(ipaddr) && ip6_addr_lacks_zone(
ip_2_ip6(ipaddr), IP6_UNKNOWN)) {
975 ipaddr = &zoned_ipaddr;
981 port = udp_new_port();
988 for (ipcb = udp_pcbs; ipcb !=
NULL; ipcb = ipcb->
next) {
999 if ((ipcb->local_port ==
port) &&
1009 (
"udp_bind: local port %"U16_F" already bound by another pcb\n",
port));
1019 pcb->local_port =
port;
1024 pcb->next = udp_pcbs;
1046udp_bind_netif(
struct udp_pcb *pcb,
const struct netif *
netif)
1077 struct udp_pcb *ipcb;
1084 if (pcb->local_port == 0) {
1085 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
1092#if LWIP_IPV6 && LWIP_IPV6_SCOPES
1096 ip6_addr_lacks_zone(
ip_2_ip6(&pcb->remote_ip), IP6_UNKNOWN)) {
1101 pcb->remote_port =
port;
1102 pcb->flags |= UDP_FLAGS_CONNECTED;
1110 for (ipcb = udp_pcbs; ipcb !=
NULL; ipcb = ipcb->
next) {
1117 pcb->next = udp_pcbs;
1130udp_disconnect(
struct udp_pcb *pcb)
1137#if LWIP_IPV4 && LWIP_IPV6
1143#if LWIP_IPV4 && LWIP_IPV6
1146 pcb->remote_port = 0;
1149 udp_clear_flags(pcb, UDP_FLAGS_CONNECTED);
1162udp_recv(
struct udp_pcb *pcb, udp_recv_fn
recv,
void *recv_arg)
1170 pcb->recv_arg = recv_arg;
1183udp_remove(
struct udp_pcb *pcb)
1185 struct udp_pcb *pcb2;
1193 if (udp_pcbs == pcb) {
1195 udp_pcbs = udp_pcbs->next;
1198 for (pcb2 = udp_pcbs; pcb2 !=
NULL; pcb2 = pcb2->
next) {
1200 if (pcb2->next !=
NULL && pcb2->
next == pcb) {
1202 pcb2->next = pcb->next;
1225 struct udp_pcb *pcb;
1229 pcb = (
struct udp_pcb *)
memp_malloc(MEMP_UDP_PCB);
1236 memset(pcb, 0,
sizeof(
struct udp_pcb));
1238#if LWIP_MULTICAST_TX_OPTIONS
1239 udp_set_multicast_ttl(pcb,
UDP_TTL);
1264 struct udp_pcb *pcb;
1269#if LWIP_IPV4 && LWIP_IPV6
1287 struct udp_pcb *upcb;
1290 for (upcb = udp_pcbs; upcb !=
NULL; upcb = upcb->
next) {
INT WSAAPI recv(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags)
#define LWIP_DEBUGF(debug, message)
#define LWIP_ERROR(message, expression, handler)
#define LWIP_ASSERT(message, assertion)
#define ip_get_option(pcb, opt)
#define ip_current_header_tot_len()
#define ip_current_src_addr()
#define ip_current_dest_addr()
#define ip_current_netif()
struct ip_globals ip_data
#define pcb_tci_init(pcb)
#define mib2_udp_unbind(pcb)
#define mib2_udp_bind(pcb)
GLuint GLuint GLsizei GLenum type
GLdouble GLdouble GLdouble GLdouble q
GLenum const GLvoid * addr
#define LWIP_UNUSED_ARG(x)
#define LWIP_DBG_LEVEL_SERIOUS
#define LWIP_ASSERT_CORE_LOCKED()
struct netif * netif_get_by_index(u8_t idx)
void pbuf_chain(struct pbuf *h, struct pbuf *t)
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
u8_t pbuf_free(struct pbuf *p)
struct pbuf * pbuf_clone(pbuf_layer layer, pbuf_type type, struct pbuf *p)
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)
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_addr_isbroadcast(addr, netif)
#define IP_IS_V6_VAL(ipaddr)
#define ip_addr_ismulticast(ipaddr)
#define IP_ADDR_PCB_VERSION_MATCH(addr, pcb)
#define ip_addr_isany(ipaddr)
#define ip_addr_eq(addr1, addr2)
#define ip_addr_copy(dest, src)
#define IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ipaddr)
#define IP_IS_ANY_TYPE_VAL(ipaddr)
#define ip_addr_set_any(is_ipv6, ipaddr)
#define IP_GET_TYPE(ipaddr)
#define IP_SET_TYPE_VAL(ipaddr, iptype)
#define IP_IS_V4_VAL(ipaddr)
#define ip_addr_debug_print_val(debug, ipaddr)
#define ip_addr_isany_val(ipaddr)
#define ip_addr_set_ipaddr(dest, src)
#define ip_addr_debug_print(debug, ipaddr)
#define icmp_port_unreach(isipv6, pbuf)
void * memp_malloc(memp_t type)
void memp_free(memp_t type, void *mem)
#define NETIF_RESET_HINTS(netif)
#define netif_get_index(netif)
#define IF__NETIF_CHECKSUM_ENABLED(netif, chksumflag)
#define NETIF_SET_HINTS(netif, netifhint)
u8_t pbuf_add_header(struct pbuf *p, size_t header_size_increment)
u8_t pbuf_remove_header(struct pbuf *p, size_t header_size_decrement)
u8_t pbuf_header_force(struct pbuf *p, s16_t header_size_increment)
#define PBUF_FLAG_MCASTLOOP
#define MIB2_STATS_INC(x)
static const u8_t broadcast[6]