Onenet
Dependents: K64F_eCompass_OneNET_JW
lwip/core/udp.c@0:b2805b6888dc, 2016-06-20 (annotated)
- Committer:
- robert_jw
- Date:
- Mon Jun 20 01:40:20 2016 +0000
- Revision:
- 0:b2805b6888dc
ADS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robert_jw | 0:b2805b6888dc | 1 | /** |
robert_jw | 0:b2805b6888dc | 2 | * @file |
robert_jw | 0:b2805b6888dc | 3 | * User Datagram Protocol module |
robert_jw | 0:b2805b6888dc | 4 | * |
robert_jw | 0:b2805b6888dc | 5 | */ |
robert_jw | 0:b2805b6888dc | 6 | |
robert_jw | 0:b2805b6888dc | 7 | /* |
robert_jw | 0:b2805b6888dc | 8 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
robert_jw | 0:b2805b6888dc | 9 | * All rights reserved. |
robert_jw | 0:b2805b6888dc | 10 | * |
robert_jw | 0:b2805b6888dc | 11 | * Redistribution and use in source and binary forms, with or without modification, |
robert_jw | 0:b2805b6888dc | 12 | * are permitted provided that the following conditions are met: |
robert_jw | 0:b2805b6888dc | 13 | * |
robert_jw | 0:b2805b6888dc | 14 | * 1. Redistributions of source code must retain the above copyright notice, |
robert_jw | 0:b2805b6888dc | 15 | * this list of conditions and the following disclaimer. |
robert_jw | 0:b2805b6888dc | 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
robert_jw | 0:b2805b6888dc | 17 | * this list of conditions and the following disclaimer in the documentation |
robert_jw | 0:b2805b6888dc | 18 | * and/or other materials provided with the distribution. |
robert_jw | 0:b2805b6888dc | 19 | * 3. The name of the author may not be used to endorse or promote products |
robert_jw | 0:b2805b6888dc | 20 | * derived from this software without specific prior written permission. |
robert_jw | 0:b2805b6888dc | 21 | * |
robert_jw | 0:b2805b6888dc | 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
robert_jw | 0:b2805b6888dc | 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
robert_jw | 0:b2805b6888dc | 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
robert_jw | 0:b2805b6888dc | 25 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
robert_jw | 0:b2805b6888dc | 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
robert_jw | 0:b2805b6888dc | 27 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
robert_jw | 0:b2805b6888dc | 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
robert_jw | 0:b2805b6888dc | 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
robert_jw | 0:b2805b6888dc | 30 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
robert_jw | 0:b2805b6888dc | 31 | * OF SUCH DAMAGE. |
robert_jw | 0:b2805b6888dc | 32 | * |
robert_jw | 0:b2805b6888dc | 33 | * This file is part of the lwIP TCP/IP stack. |
robert_jw | 0:b2805b6888dc | 34 | * |
robert_jw | 0:b2805b6888dc | 35 | * Author: Adam Dunkels <adam@sics.se> |
robert_jw | 0:b2805b6888dc | 36 | * |
robert_jw | 0:b2805b6888dc | 37 | */ |
robert_jw | 0:b2805b6888dc | 38 | |
robert_jw | 0:b2805b6888dc | 39 | |
robert_jw | 0:b2805b6888dc | 40 | /* udp.c |
robert_jw | 0:b2805b6888dc | 41 | * |
robert_jw | 0:b2805b6888dc | 42 | * The code for the User Datagram Protocol UDP & UDPLite (RFC 3828). |
robert_jw | 0:b2805b6888dc | 43 | * |
robert_jw | 0:b2805b6888dc | 44 | */ |
robert_jw | 0:b2805b6888dc | 45 | |
robert_jw | 0:b2805b6888dc | 46 | /* @todo Check the use of '(struct udp_pcb).chksum_len_rx'! |
robert_jw | 0:b2805b6888dc | 47 | */ |
robert_jw | 0:b2805b6888dc | 48 | |
robert_jw | 0:b2805b6888dc | 49 | #include "lwip/opt.h" |
robert_jw | 0:b2805b6888dc | 50 | |
robert_jw | 0:b2805b6888dc | 51 | #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */ |
robert_jw | 0:b2805b6888dc | 52 | |
robert_jw | 0:b2805b6888dc | 53 | #include "lwip/udp.h" |
robert_jw | 0:b2805b6888dc | 54 | #include "lwip/def.h" |
robert_jw | 0:b2805b6888dc | 55 | #include "lwip/memp.h" |
robert_jw | 0:b2805b6888dc | 56 | #include "lwip/inet_chksum.h" |
robert_jw | 0:b2805b6888dc | 57 | #include "lwip/ip_addr.h" |
robert_jw | 0:b2805b6888dc | 58 | #include "lwip/netif.h" |
robert_jw | 0:b2805b6888dc | 59 | #include "lwip/icmp.h" |
robert_jw | 0:b2805b6888dc | 60 | #include "lwip/stats.h" |
robert_jw | 0:b2805b6888dc | 61 | #include "lwip/snmp.h" |
robert_jw | 0:b2805b6888dc | 62 | #include "arch/perf.h" |
robert_jw | 0:b2805b6888dc | 63 | #include "lwip/dhcp.h" |
robert_jw | 0:b2805b6888dc | 64 | |
robert_jw | 0:b2805b6888dc | 65 | #include <string.h> |
robert_jw | 0:b2805b6888dc | 66 | |
robert_jw | 0:b2805b6888dc | 67 | /* The list of UDP PCBs */ |
robert_jw | 0:b2805b6888dc | 68 | /* exported in udp.h (was static) */ |
robert_jw | 0:b2805b6888dc | 69 | struct udp_pcb *udp_pcbs; |
robert_jw | 0:b2805b6888dc | 70 | |
robert_jw | 0:b2805b6888dc | 71 | /** |
robert_jw | 0:b2805b6888dc | 72 | * Process an incoming UDP datagram. |
robert_jw | 0:b2805b6888dc | 73 | * |
robert_jw | 0:b2805b6888dc | 74 | * Given an incoming UDP datagram (as a chain of pbufs) this function |
robert_jw | 0:b2805b6888dc | 75 | * finds a corresponding UDP PCB and hands over the pbuf to the pcbs |
robert_jw | 0:b2805b6888dc | 76 | * recv function. If no pcb is found or the datagram is incorrect, the |
robert_jw | 0:b2805b6888dc | 77 | * pbuf is freed. |
robert_jw | 0:b2805b6888dc | 78 | * |
robert_jw | 0:b2805b6888dc | 79 | * @param p pbuf to be demultiplexed to a UDP PCB. |
robert_jw | 0:b2805b6888dc | 80 | * @param inp network interface on which the datagram was received. |
robert_jw | 0:b2805b6888dc | 81 | * |
robert_jw | 0:b2805b6888dc | 82 | */ |
robert_jw | 0:b2805b6888dc | 83 | void |
robert_jw | 0:b2805b6888dc | 84 | udp_input(struct pbuf *p, struct netif *inp) |
robert_jw | 0:b2805b6888dc | 85 | { |
robert_jw | 0:b2805b6888dc | 86 | struct udp_hdr *udphdr; |
robert_jw | 0:b2805b6888dc | 87 | struct udp_pcb *pcb, *prev; |
robert_jw | 0:b2805b6888dc | 88 | struct udp_pcb *uncon_pcb; |
robert_jw | 0:b2805b6888dc | 89 | struct ip_hdr *iphdr; |
robert_jw | 0:b2805b6888dc | 90 | u16_t src, dest; |
robert_jw | 0:b2805b6888dc | 91 | u8_t local_match; |
robert_jw | 0:b2805b6888dc | 92 | u8_t broadcast; |
robert_jw | 0:b2805b6888dc | 93 | |
robert_jw | 0:b2805b6888dc | 94 | PERF_START; |
robert_jw | 0:b2805b6888dc | 95 | |
robert_jw | 0:b2805b6888dc | 96 | UDP_STATS_INC(udp.recv); |
robert_jw | 0:b2805b6888dc | 97 | |
robert_jw | 0:b2805b6888dc | 98 | iphdr = (struct ip_hdr *)p->payload; |
robert_jw | 0:b2805b6888dc | 99 | |
robert_jw | 0:b2805b6888dc | 100 | /* Check minimum length (IP header + UDP header) |
robert_jw | 0:b2805b6888dc | 101 | * and move payload pointer to UDP header */ |
robert_jw | 0:b2805b6888dc | 102 | if (p->tot_len < (IPH_HL(iphdr) * 4 + UDP_HLEN) || pbuf_header(p, -(s16_t)(IPH_HL(iphdr) * 4))) { |
robert_jw | 0:b2805b6888dc | 103 | /* drop short packets */ |
robert_jw | 0:b2805b6888dc | 104 | LWIP_DEBUGF(UDP_DEBUG, |
robert_jw | 0:b2805b6888dc | 105 | ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len)); |
robert_jw | 0:b2805b6888dc | 106 | UDP_STATS_INC(udp.lenerr); |
robert_jw | 0:b2805b6888dc | 107 | UDP_STATS_INC(udp.drop); |
robert_jw | 0:b2805b6888dc | 108 | snmp_inc_udpinerrors(); |
robert_jw | 0:b2805b6888dc | 109 | pbuf_free(p); |
robert_jw | 0:b2805b6888dc | 110 | goto end; |
robert_jw | 0:b2805b6888dc | 111 | } |
robert_jw | 0:b2805b6888dc | 112 | |
robert_jw | 0:b2805b6888dc | 113 | udphdr = (struct udp_hdr *)p->payload; |
robert_jw | 0:b2805b6888dc | 114 | |
robert_jw | 0:b2805b6888dc | 115 | /* is broadcast packet ? */ |
robert_jw | 0:b2805b6888dc | 116 | broadcast = ip_addr_isbroadcast(¤t_iphdr_dest, inp); |
robert_jw | 0:b2805b6888dc | 117 | |
robert_jw | 0:b2805b6888dc | 118 | LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len)); |
robert_jw | 0:b2805b6888dc | 119 | |
robert_jw | 0:b2805b6888dc | 120 | /* convert src and dest ports to host byte order */ |
robert_jw | 0:b2805b6888dc | 121 | src = ntohs(udphdr->src); |
robert_jw | 0:b2805b6888dc | 122 | dest = ntohs(udphdr->dest); |
robert_jw | 0:b2805b6888dc | 123 | |
robert_jw | 0:b2805b6888dc | 124 | udp_debug_print(udphdr); |
robert_jw | 0:b2805b6888dc | 125 | |
robert_jw | 0:b2805b6888dc | 126 | /* print the UDP source and destination */ |
robert_jw | 0:b2805b6888dc | 127 | LWIP_DEBUGF(UDP_DEBUG, |
robert_jw | 0:b2805b6888dc | 128 | ("udp (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") <-- " |
robert_jw | 0:b2805b6888dc | 129 | "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n", |
robert_jw | 0:b2805b6888dc | 130 | ip4_addr1_16(&iphdr->dest), ip4_addr2_16(&iphdr->dest), |
robert_jw | 0:b2805b6888dc | 131 | ip4_addr3_16(&iphdr->dest), ip4_addr4_16(&iphdr->dest), ntohs(udphdr->dest), |
robert_jw | 0:b2805b6888dc | 132 | ip4_addr1_16(&iphdr->src), ip4_addr2_16(&iphdr->src), |
robert_jw | 0:b2805b6888dc | 133 | ip4_addr3_16(&iphdr->src), ip4_addr4_16(&iphdr->src), ntohs(udphdr->src))); |
robert_jw | 0:b2805b6888dc | 134 | |
robert_jw | 0:b2805b6888dc | 135 | #if LWIP_DHCP |
robert_jw | 0:b2805b6888dc | 136 | pcb = NULL; |
robert_jw | 0:b2805b6888dc | 137 | /* when LWIP_DHCP is active, packets to DHCP_CLIENT_PORT may only be processed by |
robert_jw | 0:b2805b6888dc | 138 | the dhcp module, no other UDP pcb may use the local UDP port DHCP_CLIENT_PORT */ |
robert_jw | 0:b2805b6888dc | 139 | if (dest == DHCP_CLIENT_PORT) { |
robert_jw | 0:b2805b6888dc | 140 | /* all packets for DHCP_CLIENT_PORT not coming from DHCP_SERVER_PORT are dropped! */ |
robert_jw | 0:b2805b6888dc | 141 | if (src == DHCP_SERVER_PORT) { |
robert_jw | 0:b2805b6888dc | 142 | if ((inp->dhcp != NULL) && (inp->dhcp->pcb != NULL)) { |
robert_jw | 0:b2805b6888dc | 143 | /* accept the packe if |
robert_jw | 0:b2805b6888dc | 144 | (- broadcast or directed to us) -> DHCP is link-layer-addressed, local ip is always ANY! |
robert_jw | 0:b2805b6888dc | 145 | - inp->dhcp->pcb->remote == ANY or iphdr->src */ |
robert_jw | 0:b2805b6888dc | 146 | if ((ip_addr_isany(&inp->dhcp->pcb->remote_ip) || |
robert_jw | 0:b2805b6888dc | 147 | ip_addr_cmp(&(inp->dhcp->pcb->remote_ip), ¤t_iphdr_src))) { |
robert_jw | 0:b2805b6888dc | 148 | pcb = inp->dhcp->pcb; |
robert_jw | 0:b2805b6888dc | 149 | } |
robert_jw | 0:b2805b6888dc | 150 | } |
robert_jw | 0:b2805b6888dc | 151 | } |
robert_jw | 0:b2805b6888dc | 152 | } else |
robert_jw | 0:b2805b6888dc | 153 | #endif /* LWIP_DHCP */ |
robert_jw | 0:b2805b6888dc | 154 | { |
robert_jw | 0:b2805b6888dc | 155 | prev = NULL; |
robert_jw | 0:b2805b6888dc | 156 | local_match = 0; |
robert_jw | 0:b2805b6888dc | 157 | uncon_pcb = NULL; |
robert_jw | 0:b2805b6888dc | 158 | /* Iterate through the UDP pcb list for a matching pcb. |
robert_jw | 0:b2805b6888dc | 159 | * 'Perfect match' pcbs (connected to the remote port & ip address) are |
robert_jw | 0:b2805b6888dc | 160 | * preferred. If no perfect match is found, the first unconnected pcb that |
robert_jw | 0:b2805b6888dc | 161 | * matches the local port and ip address gets the datagram. */ |
robert_jw | 0:b2805b6888dc | 162 | for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) { |
robert_jw | 0:b2805b6888dc | 163 | local_match = 0; |
robert_jw | 0:b2805b6888dc | 164 | /* print the PCB local and remote address */ |
robert_jw | 0:b2805b6888dc | 165 | LWIP_DEBUGF(UDP_DEBUG, |
robert_jw | 0:b2805b6888dc | 166 | ("pcb (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") --- " |
robert_jw | 0:b2805b6888dc | 167 | "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n", |
robert_jw | 0:b2805b6888dc | 168 | ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip), |
robert_jw | 0:b2805b6888dc | 169 | ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip), pcb->local_port, |
robert_jw | 0:b2805b6888dc | 170 | ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip), |
robert_jw | 0:b2805b6888dc | 171 | ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip), pcb->remote_port)); |
robert_jw | 0:b2805b6888dc | 172 | |
robert_jw | 0:b2805b6888dc | 173 | /* compare PCB local addr+port to UDP destination addr+port */ |
robert_jw | 0:b2805b6888dc | 174 | if ((pcb->local_port == dest) && |
robert_jw | 0:b2805b6888dc | 175 | ((!broadcast && ip_addr_isany(&pcb->local_ip)) || |
robert_jw | 0:b2805b6888dc | 176 | ip_addr_cmp(&(pcb->local_ip), ¤t_iphdr_dest) || |
robert_jw | 0:b2805b6888dc | 177 | #if LWIP_IGMP |
robert_jw | 0:b2805b6888dc | 178 | ip_addr_ismulticast(¤t_iphdr_dest) || |
robert_jw | 0:b2805b6888dc | 179 | #endif /* LWIP_IGMP */ |
robert_jw | 0:b2805b6888dc | 180 | #if IP_SOF_BROADCAST_RECV |
robert_jw | 0:b2805b6888dc | 181 | (broadcast && (pcb->so_options & SOF_BROADCAST)))) { |
robert_jw | 0:b2805b6888dc | 182 | #else /* IP_SOF_BROADCAST_RECV */ |
robert_jw | 0:b2805b6888dc | 183 | (broadcast))) { |
robert_jw | 0:b2805b6888dc | 184 | #endif /* IP_SOF_BROADCAST_RECV */ |
robert_jw | 0:b2805b6888dc | 185 | local_match = 1; |
robert_jw | 0:b2805b6888dc | 186 | if ((uncon_pcb == NULL) && |
robert_jw | 0:b2805b6888dc | 187 | ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) { |
robert_jw | 0:b2805b6888dc | 188 | /* the first unconnected matching PCB */ |
robert_jw | 0:b2805b6888dc | 189 | uncon_pcb = pcb; |
robert_jw | 0:b2805b6888dc | 190 | } |
robert_jw | 0:b2805b6888dc | 191 | } |
robert_jw | 0:b2805b6888dc | 192 | /* compare PCB remote addr+port to UDP source addr+port */ |
robert_jw | 0:b2805b6888dc | 193 | if ((local_match != 0) && |
robert_jw | 0:b2805b6888dc | 194 | (pcb->remote_port == src) && |
robert_jw | 0:b2805b6888dc | 195 | (ip_addr_isany(&pcb->remote_ip) || |
robert_jw | 0:b2805b6888dc | 196 | ip_addr_cmp(&(pcb->remote_ip), ¤t_iphdr_src))) { |
robert_jw | 0:b2805b6888dc | 197 | /* the first fully matching PCB */ |
robert_jw | 0:b2805b6888dc | 198 | if (prev != NULL) { |
robert_jw | 0:b2805b6888dc | 199 | /* move the pcb to the front of udp_pcbs so that is |
robert_jw | 0:b2805b6888dc | 200 | found faster next time */ |
robert_jw | 0:b2805b6888dc | 201 | prev->next = pcb->next; |
robert_jw | 0:b2805b6888dc | 202 | pcb->next = udp_pcbs; |
robert_jw | 0:b2805b6888dc | 203 | udp_pcbs = pcb; |
robert_jw | 0:b2805b6888dc | 204 | } else { |
robert_jw | 0:b2805b6888dc | 205 | UDP_STATS_INC(udp.cachehit); |
robert_jw | 0:b2805b6888dc | 206 | } |
robert_jw | 0:b2805b6888dc | 207 | break; |
robert_jw | 0:b2805b6888dc | 208 | } |
robert_jw | 0:b2805b6888dc | 209 | prev = pcb; |
robert_jw | 0:b2805b6888dc | 210 | } |
robert_jw | 0:b2805b6888dc | 211 | /* no fully matching pcb found? then look for an unconnected pcb */ |
robert_jw | 0:b2805b6888dc | 212 | if (pcb == NULL) { |
robert_jw | 0:b2805b6888dc | 213 | pcb = uncon_pcb; |
robert_jw | 0:b2805b6888dc | 214 | } |
robert_jw | 0:b2805b6888dc | 215 | } |
robert_jw | 0:b2805b6888dc | 216 | |
robert_jw | 0:b2805b6888dc | 217 | /* Check checksum if this is a match or if it was directed at us. */ |
robert_jw | 0:b2805b6888dc | 218 | if (pcb != NULL || ip_addr_cmp(&inp->ip_addr, ¤t_iphdr_dest)) { |
robert_jw | 0:b2805b6888dc | 219 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n")); |
robert_jw | 0:b2805b6888dc | 220 | #if LWIP_UDPLITE |
robert_jw | 0:b2805b6888dc | 221 | if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) { |
robert_jw | 0:b2805b6888dc | 222 | /* Do the UDP Lite checksum */ |
robert_jw | 0:b2805b6888dc | 223 | #if CHECKSUM_CHECK_UDP |
robert_jw | 0:b2805b6888dc | 224 | u16_t chklen = ntohs(udphdr->len); |
robert_jw | 0:b2805b6888dc | 225 | if (chklen < sizeof(struct udp_hdr)) { |
robert_jw | 0:b2805b6888dc | 226 | if (chklen == 0) { |
robert_jw | 0:b2805b6888dc | 227 | /* For UDP-Lite, checksum length of 0 means checksum |
robert_jw | 0:b2805b6888dc | 228 | over the complete packet (See RFC 3828 chap. 3.1) */ |
robert_jw | 0:b2805b6888dc | 229 | chklen = p->tot_len; |
robert_jw | 0:b2805b6888dc | 230 | } else { |
robert_jw | 0:b2805b6888dc | 231 | /* At least the UDP-Lite header must be covered by the |
robert_jw | 0:b2805b6888dc | 232 | checksum! (Again, see RFC 3828 chap. 3.1) */ |
robert_jw | 0:b2805b6888dc | 233 | UDP_STATS_INC(udp.chkerr); |
robert_jw | 0:b2805b6888dc | 234 | UDP_STATS_INC(udp.drop); |
robert_jw | 0:b2805b6888dc | 235 | snmp_inc_udpinerrors(); |
robert_jw | 0:b2805b6888dc | 236 | pbuf_free(p); |
robert_jw | 0:b2805b6888dc | 237 | goto end; |
robert_jw | 0:b2805b6888dc | 238 | } |
robert_jw | 0:b2805b6888dc | 239 | } |
robert_jw | 0:b2805b6888dc | 240 | if (inet_chksum_pseudo_partial(p, ¤t_iphdr_src, ¤t_iphdr_dest, |
robert_jw | 0:b2805b6888dc | 241 | IP_PROTO_UDPLITE, p->tot_len, chklen) != 0) { |
robert_jw | 0:b2805b6888dc | 242 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
robert_jw | 0:b2805b6888dc | 243 | ("udp_input: UDP Lite datagram discarded due to failing checksum\n")); |
robert_jw | 0:b2805b6888dc | 244 | UDP_STATS_INC(udp.chkerr); |
robert_jw | 0:b2805b6888dc | 245 | UDP_STATS_INC(udp.drop); |
robert_jw | 0:b2805b6888dc | 246 | snmp_inc_udpinerrors(); |
robert_jw | 0:b2805b6888dc | 247 | pbuf_free(p); |
robert_jw | 0:b2805b6888dc | 248 | goto end; |
robert_jw | 0:b2805b6888dc | 249 | } |
robert_jw | 0:b2805b6888dc | 250 | #endif /* CHECKSUM_CHECK_UDP */ |
robert_jw | 0:b2805b6888dc | 251 | } else |
robert_jw | 0:b2805b6888dc | 252 | #endif /* LWIP_UDPLITE */ |
robert_jw | 0:b2805b6888dc | 253 | { |
robert_jw | 0:b2805b6888dc | 254 | #if CHECKSUM_CHECK_UDP |
robert_jw | 0:b2805b6888dc | 255 | if (udphdr->chksum != 0) { |
robert_jw | 0:b2805b6888dc | 256 | if (inet_chksum_pseudo(p, ip_current_src_addr(), ip_current_dest_addr(), |
robert_jw | 0:b2805b6888dc | 257 | IP_PROTO_UDP, p->tot_len) != 0) { |
robert_jw | 0:b2805b6888dc | 258 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
robert_jw | 0:b2805b6888dc | 259 | ("udp_input: UDP datagram discarded due to failing checksum\n")); |
robert_jw | 0:b2805b6888dc | 260 | UDP_STATS_INC(udp.chkerr); |
robert_jw | 0:b2805b6888dc | 261 | UDP_STATS_INC(udp.drop); |
robert_jw | 0:b2805b6888dc | 262 | snmp_inc_udpinerrors(); |
robert_jw | 0:b2805b6888dc | 263 | pbuf_free(p); |
robert_jw | 0:b2805b6888dc | 264 | goto end; |
robert_jw | 0:b2805b6888dc | 265 | } |
robert_jw | 0:b2805b6888dc | 266 | } |
robert_jw | 0:b2805b6888dc | 267 | #endif /* CHECKSUM_CHECK_UDP */ |
robert_jw | 0:b2805b6888dc | 268 | } |
robert_jw | 0:b2805b6888dc | 269 | if(pbuf_header(p, -UDP_HLEN)) { |
robert_jw | 0:b2805b6888dc | 270 | /* Can we cope with this failing? Just assert for now */ |
robert_jw | 0:b2805b6888dc | 271 | LWIP_ASSERT("pbuf_header failed\n", 0); |
robert_jw | 0:b2805b6888dc | 272 | UDP_STATS_INC(udp.drop); |
robert_jw | 0:b2805b6888dc | 273 | snmp_inc_udpinerrors(); |
robert_jw | 0:b2805b6888dc | 274 | pbuf_free(p); |
robert_jw | 0:b2805b6888dc | 275 | goto end; |
robert_jw | 0:b2805b6888dc | 276 | } |
robert_jw | 0:b2805b6888dc | 277 | if (pcb != NULL) { |
robert_jw | 0:b2805b6888dc | 278 | snmp_inc_udpindatagrams(); |
robert_jw | 0:b2805b6888dc | 279 | #if SO_REUSE && SO_REUSE_RXTOALL |
robert_jw | 0:b2805b6888dc | 280 | if ((broadcast || ip_addr_ismulticast(¤t_iphdr_dest)) && |
robert_jw | 0:b2805b6888dc | 281 | ((pcb->so_options & SOF_REUSEADDR) != 0)) { |
robert_jw | 0:b2805b6888dc | 282 | /* pass broadcast- or multicast packets to all multicast pcbs |
robert_jw | 0:b2805b6888dc | 283 | if SOF_REUSEADDR is set on the first match */ |
robert_jw | 0:b2805b6888dc | 284 | struct udp_pcb *mpcb; |
robert_jw | 0:b2805b6888dc | 285 | u8_t p_header_changed = 0; |
robert_jw | 0:b2805b6888dc | 286 | for (mpcb = udp_pcbs; mpcb != NULL; mpcb = mpcb->next) { |
robert_jw | 0:b2805b6888dc | 287 | if (mpcb != pcb) { |
robert_jw | 0:b2805b6888dc | 288 | /* compare PCB local addr+port to UDP destination addr+port */ |
robert_jw | 0:b2805b6888dc | 289 | if ((mpcb->local_port == dest) && |
robert_jw | 0:b2805b6888dc | 290 | ((!broadcast && ip_addr_isany(&mpcb->local_ip)) || |
robert_jw | 0:b2805b6888dc | 291 | ip_addr_cmp(&(mpcb->local_ip), ¤t_iphdr_dest) || |
robert_jw | 0:b2805b6888dc | 292 | #if LWIP_IGMP |
robert_jw | 0:b2805b6888dc | 293 | ip_addr_ismulticast(¤t_iphdr_dest) || |
robert_jw | 0:b2805b6888dc | 294 | #endif /* LWIP_IGMP */ |
robert_jw | 0:b2805b6888dc | 295 | #if IP_SOF_BROADCAST_RECV |
robert_jw | 0:b2805b6888dc | 296 | (broadcast && (mpcb->so_options & SOF_BROADCAST)))) { |
robert_jw | 0:b2805b6888dc | 297 | #else /* IP_SOF_BROADCAST_RECV */ |
robert_jw | 0:b2805b6888dc | 298 | (broadcast))) { |
robert_jw | 0:b2805b6888dc | 299 | #endif /* IP_SOF_BROADCAST_RECV */ |
robert_jw | 0:b2805b6888dc | 300 | /* pass a copy of the packet to all local matches */ |
robert_jw | 0:b2805b6888dc | 301 | if (mpcb->recv != NULL) { |
robert_jw | 0:b2805b6888dc | 302 | struct pbuf *q; |
robert_jw | 0:b2805b6888dc | 303 | /* for that, move payload to IP header again */ |
robert_jw | 0:b2805b6888dc | 304 | if (p_header_changed == 0) { |
robert_jw | 0:b2805b6888dc | 305 | pbuf_header(p, (s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN)); |
robert_jw | 0:b2805b6888dc | 306 | p_header_changed = 1; |
robert_jw | 0:b2805b6888dc | 307 | } |
robert_jw | 0:b2805b6888dc | 308 | q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM); |
robert_jw | 0:b2805b6888dc | 309 | if (q != NULL) { |
robert_jw | 0:b2805b6888dc | 310 | err_t err = pbuf_copy(q, p); |
robert_jw | 0:b2805b6888dc | 311 | if (err == ERR_OK) { |
robert_jw | 0:b2805b6888dc | 312 | /* move payload to UDP data */ |
robert_jw | 0:b2805b6888dc | 313 | pbuf_header(q, -(s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN)); |
robert_jw | 0:b2805b6888dc | 314 | mpcb->recv(mpcb->recv_arg, mpcb, q, ip_current_src_addr(), src); |
robert_jw | 0:b2805b6888dc | 315 | } |
robert_jw | 0:b2805b6888dc | 316 | } |
robert_jw | 0:b2805b6888dc | 317 | } |
robert_jw | 0:b2805b6888dc | 318 | } |
robert_jw | 0:b2805b6888dc | 319 | } |
robert_jw | 0:b2805b6888dc | 320 | } |
robert_jw | 0:b2805b6888dc | 321 | if (p_header_changed) { |
robert_jw | 0:b2805b6888dc | 322 | /* and move payload to UDP data again */ |
robert_jw | 0:b2805b6888dc | 323 | pbuf_header(p, -(s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN)); |
robert_jw | 0:b2805b6888dc | 324 | } |
robert_jw | 0:b2805b6888dc | 325 | } |
robert_jw | 0:b2805b6888dc | 326 | #endif /* SO_REUSE && SO_REUSE_RXTOALL */ |
robert_jw | 0:b2805b6888dc | 327 | /* callback */ |
robert_jw | 0:b2805b6888dc | 328 | if (pcb->recv != NULL) { |
robert_jw | 0:b2805b6888dc | 329 | /* now the recv function is responsible for freeing p */ |
robert_jw | 0:b2805b6888dc | 330 | pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr(), src); |
robert_jw | 0:b2805b6888dc | 331 | } else { |
robert_jw | 0:b2805b6888dc | 332 | /* no recv function registered? then we have to free the pbuf! */ |
robert_jw | 0:b2805b6888dc | 333 | pbuf_free(p); |
robert_jw | 0:b2805b6888dc | 334 | goto end; |
robert_jw | 0:b2805b6888dc | 335 | } |
robert_jw | 0:b2805b6888dc | 336 | } else { |
robert_jw | 0:b2805b6888dc | 337 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n")); |
robert_jw | 0:b2805b6888dc | 338 | |
robert_jw | 0:b2805b6888dc | 339 | #if LWIP_ICMP |
robert_jw | 0:b2805b6888dc | 340 | /* No match was found, send ICMP destination port unreachable unless |
robert_jw | 0:b2805b6888dc | 341 | destination address was broadcast/multicast. */ |
robert_jw | 0:b2805b6888dc | 342 | if (!broadcast && |
robert_jw | 0:b2805b6888dc | 343 | !ip_addr_ismulticast(¤t_iphdr_dest)) { |
robert_jw | 0:b2805b6888dc | 344 | /* move payload pointer back to ip header */ |
robert_jw | 0:b2805b6888dc | 345 | pbuf_header(p, (IPH_HL(iphdr) * 4) + UDP_HLEN); |
robert_jw | 0:b2805b6888dc | 346 | LWIP_ASSERT("p->payload == iphdr", (p->payload == iphdr)); |
robert_jw | 0:b2805b6888dc | 347 | icmp_dest_unreach(p, ICMP_DUR_PORT); |
robert_jw | 0:b2805b6888dc | 348 | } |
robert_jw | 0:b2805b6888dc | 349 | #endif /* LWIP_ICMP */ |
robert_jw | 0:b2805b6888dc | 350 | UDP_STATS_INC(udp.proterr); |
robert_jw | 0:b2805b6888dc | 351 | UDP_STATS_INC(udp.drop); |
robert_jw | 0:b2805b6888dc | 352 | snmp_inc_udpnoports(); |
robert_jw | 0:b2805b6888dc | 353 | pbuf_free(p); |
robert_jw | 0:b2805b6888dc | 354 | } |
robert_jw | 0:b2805b6888dc | 355 | } else { |
robert_jw | 0:b2805b6888dc | 356 | pbuf_free(p); |
robert_jw | 0:b2805b6888dc | 357 | } |
robert_jw | 0:b2805b6888dc | 358 | end: |
robert_jw | 0:b2805b6888dc | 359 | PERF_STOP("udp_input"); |
robert_jw | 0:b2805b6888dc | 360 | } |
robert_jw | 0:b2805b6888dc | 361 | |
robert_jw | 0:b2805b6888dc | 362 | /** |
robert_jw | 0:b2805b6888dc | 363 | * Send data using UDP. |
robert_jw | 0:b2805b6888dc | 364 | * |
robert_jw | 0:b2805b6888dc | 365 | * @param pcb UDP PCB used to send the data. |
robert_jw | 0:b2805b6888dc | 366 | * @param p chain of pbuf's to be sent. |
robert_jw | 0:b2805b6888dc | 367 | * |
robert_jw | 0:b2805b6888dc | 368 | * The datagram will be sent to the current remote_ip & remote_port |
robert_jw | 0:b2805b6888dc | 369 | * stored in pcb. If the pcb is not bound to a port, it will |
robert_jw | 0:b2805b6888dc | 370 | * automatically be bound to a random port. |
robert_jw | 0:b2805b6888dc | 371 | * |
robert_jw | 0:b2805b6888dc | 372 | * @return lwIP error code. |
robert_jw | 0:b2805b6888dc | 373 | * - ERR_OK. Successful. No error occured. |
robert_jw | 0:b2805b6888dc | 374 | * - ERR_MEM. Out of memory. |
robert_jw | 0:b2805b6888dc | 375 | * - ERR_RTE. Could not find route to destination address. |
robert_jw | 0:b2805b6888dc | 376 | * - More errors could be returned by lower protocol layers. |
robert_jw | 0:b2805b6888dc | 377 | * |
robert_jw | 0:b2805b6888dc | 378 | * @see udp_disconnect() udp_sendto() |
robert_jw | 0:b2805b6888dc | 379 | */ |
robert_jw | 0:b2805b6888dc | 380 | err_t |
robert_jw | 0:b2805b6888dc | 381 | udp_send(struct udp_pcb *pcb, struct pbuf *p) |
robert_jw | 0:b2805b6888dc | 382 | { |
robert_jw | 0:b2805b6888dc | 383 | /* send to the packet using remote ip and port stored in the pcb */ |
robert_jw | 0:b2805b6888dc | 384 | return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port); |
robert_jw | 0:b2805b6888dc | 385 | } |
robert_jw | 0:b2805b6888dc | 386 | |
robert_jw | 0:b2805b6888dc | 387 | #if LWIP_CHECKSUM_ON_COPY |
robert_jw | 0:b2805b6888dc | 388 | /** Same as udp_send() but with checksum |
robert_jw | 0:b2805b6888dc | 389 | */ |
robert_jw | 0:b2805b6888dc | 390 | err_t |
robert_jw | 0:b2805b6888dc | 391 | udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p, |
robert_jw | 0:b2805b6888dc | 392 | u8_t have_chksum, u16_t chksum) |
robert_jw | 0:b2805b6888dc | 393 | { |
robert_jw | 0:b2805b6888dc | 394 | /* send to the packet using remote ip and port stored in the pcb */ |
robert_jw | 0:b2805b6888dc | 395 | return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port, |
robert_jw | 0:b2805b6888dc | 396 | have_chksum, chksum); |
robert_jw | 0:b2805b6888dc | 397 | } |
robert_jw | 0:b2805b6888dc | 398 | #endif /* LWIP_CHECKSUM_ON_COPY */ |
robert_jw | 0:b2805b6888dc | 399 | |
robert_jw | 0:b2805b6888dc | 400 | /** |
robert_jw | 0:b2805b6888dc | 401 | * Send data to a specified address using UDP. |
robert_jw | 0:b2805b6888dc | 402 | * |
robert_jw | 0:b2805b6888dc | 403 | * @param pcb UDP PCB used to send the data. |
robert_jw | 0:b2805b6888dc | 404 | * @param p chain of pbuf's to be sent. |
robert_jw | 0:b2805b6888dc | 405 | * @param dst_ip Destination IP address. |
robert_jw | 0:b2805b6888dc | 406 | * @param dst_port Destination UDP port. |
robert_jw | 0:b2805b6888dc | 407 | * |
robert_jw | 0:b2805b6888dc | 408 | * dst_ip & dst_port are expected to be in the same byte order as in the pcb. |
robert_jw | 0:b2805b6888dc | 409 | * |
robert_jw | 0:b2805b6888dc | 410 | * If the PCB already has a remote address association, it will |
robert_jw | 0:b2805b6888dc | 411 | * be restored after the data is sent. |
robert_jw | 0:b2805b6888dc | 412 | * |
robert_jw | 0:b2805b6888dc | 413 | * @return lwIP error code (@see udp_send for possible error codes) |
robert_jw | 0:b2805b6888dc | 414 | * |
robert_jw | 0:b2805b6888dc | 415 | * @see udp_disconnect() udp_send() |
robert_jw | 0:b2805b6888dc | 416 | */ |
robert_jw | 0:b2805b6888dc | 417 | err_t |
robert_jw | 0:b2805b6888dc | 418 | udp_sendto(struct udp_pcb *pcb, struct pbuf *p, |
robert_jw | 0:b2805b6888dc | 419 | ip_addr_t *dst_ip, u16_t dst_port) |
robert_jw | 0:b2805b6888dc | 420 | { |
robert_jw | 0:b2805b6888dc | 421 | #if LWIP_CHECKSUM_ON_COPY |
robert_jw | 0:b2805b6888dc | 422 | return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0); |
robert_jw | 0:b2805b6888dc | 423 | } |
robert_jw | 0:b2805b6888dc | 424 | |
robert_jw | 0:b2805b6888dc | 425 | /** Same as udp_sendto(), but with checksum */ |
robert_jw | 0:b2805b6888dc | 426 | err_t |
robert_jw | 0:b2805b6888dc | 427 | udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip, |
robert_jw | 0:b2805b6888dc | 428 | u16_t dst_port, u8_t have_chksum, u16_t chksum) |
robert_jw | 0:b2805b6888dc | 429 | { |
robert_jw | 0:b2805b6888dc | 430 | #endif /* LWIP_CHECKSUM_ON_COPY */ |
robert_jw | 0:b2805b6888dc | 431 | struct netif *netif; |
robert_jw | 0:b2805b6888dc | 432 | |
robert_jw | 0:b2805b6888dc | 433 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send\n")); |
robert_jw | 0:b2805b6888dc | 434 | |
robert_jw | 0:b2805b6888dc | 435 | /* find the outgoing network interface for this packet */ |
robert_jw | 0:b2805b6888dc | 436 | #if LWIP_IGMP |
robert_jw | 0:b2805b6888dc | 437 | netif = ip_route((ip_addr_ismulticast(dst_ip))?(&(pcb->multicast_ip)):(dst_ip)); |
robert_jw | 0:b2805b6888dc | 438 | #else |
robert_jw | 0:b2805b6888dc | 439 | netif = ip_route(dst_ip); |
robert_jw | 0:b2805b6888dc | 440 | #endif /* LWIP_IGMP */ |
robert_jw | 0:b2805b6888dc | 441 | |
robert_jw | 0:b2805b6888dc | 442 | /* no outgoing network interface could be found? */ |
robert_jw | 0:b2805b6888dc | 443 | if (netif == NULL) { |
robert_jw | 0:b2805b6888dc | 444 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
robert_jw | 0:b2805b6888dc | 445 | ip4_addr1_16(dst_ip), ip4_addr2_16(dst_ip), ip4_addr3_16(dst_ip), ip4_addr4_16(dst_ip))); |
robert_jw | 0:b2805b6888dc | 446 | UDP_STATS_INC(udp.rterr); |
robert_jw | 0:b2805b6888dc | 447 | return ERR_RTE; |
robert_jw | 0:b2805b6888dc | 448 | } |
robert_jw | 0:b2805b6888dc | 449 | #if LWIP_CHECKSUM_ON_COPY |
robert_jw | 0:b2805b6888dc | 450 | return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum); |
robert_jw | 0:b2805b6888dc | 451 | #else /* LWIP_CHECKSUM_ON_COPY */ |
robert_jw | 0:b2805b6888dc | 452 | return udp_sendto_if(pcb, p, dst_ip, dst_port, netif); |
robert_jw | 0:b2805b6888dc | 453 | #endif /* LWIP_CHECKSUM_ON_COPY */ |
robert_jw | 0:b2805b6888dc | 454 | } |
robert_jw | 0:b2805b6888dc | 455 | |
robert_jw | 0:b2805b6888dc | 456 | /** |
robert_jw | 0:b2805b6888dc | 457 | * Send data to a specified address using UDP. |
robert_jw | 0:b2805b6888dc | 458 | * The netif used for sending can be specified. |
robert_jw | 0:b2805b6888dc | 459 | * |
robert_jw | 0:b2805b6888dc | 460 | * This function exists mainly for DHCP, to be able to send UDP packets |
robert_jw | 0:b2805b6888dc | 461 | * on a netif that is still down. |
robert_jw | 0:b2805b6888dc | 462 | * |
robert_jw | 0:b2805b6888dc | 463 | * @param pcb UDP PCB used to send the data. |
robert_jw | 0:b2805b6888dc | 464 | * @param p chain of pbuf's to be sent. |
robert_jw | 0:b2805b6888dc | 465 | * @param dst_ip Destination IP address. |
robert_jw | 0:b2805b6888dc | 466 | * @param dst_port Destination UDP port. |
robert_jw | 0:b2805b6888dc | 467 | * @param netif the netif used for sending. |
robert_jw | 0:b2805b6888dc | 468 | * |
robert_jw | 0:b2805b6888dc | 469 | * dst_ip & dst_port are expected to be in the same byte order as in the pcb. |
robert_jw | 0:b2805b6888dc | 470 | * |
robert_jw | 0:b2805b6888dc | 471 | * @return lwIP error code (@see udp_send for possible error codes) |
robert_jw | 0:b2805b6888dc | 472 | * |
robert_jw | 0:b2805b6888dc | 473 | * @see udp_disconnect() udp_send() |
robert_jw | 0:b2805b6888dc | 474 | */ |
robert_jw | 0:b2805b6888dc | 475 | err_t |
robert_jw | 0:b2805b6888dc | 476 | udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p, |
robert_jw | 0:b2805b6888dc | 477 | ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif) |
robert_jw | 0:b2805b6888dc | 478 | { |
robert_jw | 0:b2805b6888dc | 479 | #if LWIP_CHECKSUM_ON_COPY |
robert_jw | 0:b2805b6888dc | 480 | return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0); |
robert_jw | 0:b2805b6888dc | 481 | } |
robert_jw | 0:b2805b6888dc | 482 | |
robert_jw | 0:b2805b6888dc | 483 | /** Same as udp_sendto_if(), but with checksum */ |
robert_jw | 0:b2805b6888dc | 484 | err_t |
robert_jw | 0:b2805b6888dc | 485 | udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip, |
robert_jw | 0:b2805b6888dc | 486 | u16_t dst_port, struct netif *netif, u8_t have_chksum, |
robert_jw | 0:b2805b6888dc | 487 | u16_t chksum) |
robert_jw | 0:b2805b6888dc | 488 | { |
robert_jw | 0:b2805b6888dc | 489 | #endif /* LWIP_CHECKSUM_ON_COPY */ |
robert_jw | 0:b2805b6888dc | 490 | struct udp_hdr *udphdr; |
robert_jw | 0:b2805b6888dc | 491 | ip_addr_t *src_ip; |
robert_jw | 0:b2805b6888dc | 492 | err_t err; |
robert_jw | 0:b2805b6888dc | 493 | struct pbuf *q; /* q will be sent down the stack */ |
robert_jw | 0:b2805b6888dc | 494 | |
robert_jw | 0:b2805b6888dc | 495 | #if IP_SOF_BROADCAST |
robert_jw | 0:b2805b6888dc | 496 | /* broadcast filter? */ |
robert_jw | 0:b2805b6888dc | 497 | if ( ((pcb->so_options & SOF_BROADCAST) == 0) && ip_addr_isbroadcast(dst_ip, netif) ) { |
robert_jw | 0:b2805b6888dc | 498 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
robert_jw | 0:b2805b6888dc | 499 | ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb)); |
robert_jw | 0:b2805b6888dc | 500 | return ERR_VAL; |
robert_jw | 0:b2805b6888dc | 501 | } |
robert_jw | 0:b2805b6888dc | 502 | #endif /* IP_SOF_BROADCAST */ |
robert_jw | 0:b2805b6888dc | 503 | |
robert_jw | 0:b2805b6888dc | 504 | /* if the PCB is not yet bound to a port, bind it here */ |
robert_jw | 0:b2805b6888dc | 505 | if (pcb->local_port == 0) { |
robert_jw | 0:b2805b6888dc | 506 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n")); |
robert_jw | 0:b2805b6888dc | 507 | err = udp_bind(pcb, &pcb->local_ip, pcb->local_port); |
robert_jw | 0:b2805b6888dc | 508 | if (err != ERR_OK) { |
robert_jw | 0:b2805b6888dc | 509 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n")); |
robert_jw | 0:b2805b6888dc | 510 | return err; |
robert_jw | 0:b2805b6888dc | 511 | } |
robert_jw | 0:b2805b6888dc | 512 | } |
robert_jw | 0:b2805b6888dc | 513 | |
robert_jw | 0:b2805b6888dc | 514 | /* not enough space to add an UDP header to first pbuf in given p chain? */ |
robert_jw | 0:b2805b6888dc | 515 | if (pbuf_header(p, UDP_HLEN)) { |
robert_jw | 0:b2805b6888dc | 516 | /* allocate header in a separate new pbuf */ |
robert_jw | 0:b2805b6888dc | 517 | q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM); |
robert_jw | 0:b2805b6888dc | 518 | /* new header pbuf could not be allocated? */ |
robert_jw | 0:b2805b6888dc | 519 | if (q == NULL) { |
robert_jw | 0:b2805b6888dc | 520 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n")); |
robert_jw | 0:b2805b6888dc | 521 | return ERR_MEM; |
robert_jw | 0:b2805b6888dc | 522 | } |
robert_jw | 0:b2805b6888dc | 523 | if (p->tot_len != 0) { |
robert_jw | 0:b2805b6888dc | 524 | /* chain header q in front of given pbuf p (only if p contains data) */ |
robert_jw | 0:b2805b6888dc | 525 | pbuf_chain(q, p); |
robert_jw | 0:b2805b6888dc | 526 | } |
robert_jw | 0:b2805b6888dc | 527 | /* first pbuf q points to header pbuf */ |
robert_jw | 0:b2805b6888dc | 528 | LWIP_DEBUGF(UDP_DEBUG, |
robert_jw | 0:b2805b6888dc | 529 | ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p)); |
robert_jw | 0:b2805b6888dc | 530 | } else { |
robert_jw | 0:b2805b6888dc | 531 | /* adding space for header within p succeeded */ |
robert_jw | 0:b2805b6888dc | 532 | /* first pbuf q equals given pbuf */ |
robert_jw | 0:b2805b6888dc | 533 | q = p; |
robert_jw | 0:b2805b6888dc | 534 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p)); |
robert_jw | 0:b2805b6888dc | 535 | } |
robert_jw | 0:b2805b6888dc | 536 | LWIP_ASSERT("check that first pbuf can hold struct udp_hdr", |
robert_jw | 0:b2805b6888dc | 537 | (q->len >= sizeof(struct udp_hdr))); |
robert_jw | 0:b2805b6888dc | 538 | /* q now represents the packet to be sent */ |
robert_jw | 0:b2805b6888dc | 539 | udphdr = (struct udp_hdr *)q->payload; |
robert_jw | 0:b2805b6888dc | 540 | udphdr->src = htons(pcb->local_port); |
robert_jw | 0:b2805b6888dc | 541 | udphdr->dest = htons(dst_port); |
robert_jw | 0:b2805b6888dc | 542 | /* in UDP, 0 checksum means 'no checksum' */ |
robert_jw | 0:b2805b6888dc | 543 | udphdr->chksum = 0x0000; |
robert_jw | 0:b2805b6888dc | 544 | |
robert_jw | 0:b2805b6888dc | 545 | /* Multicast Loop? */ |
robert_jw | 0:b2805b6888dc | 546 | #if LWIP_IGMP |
robert_jw | 0:b2805b6888dc | 547 | if (ip_addr_ismulticast(dst_ip) && ((pcb->flags & UDP_FLAGS_MULTICAST_LOOP) != 0)) { |
robert_jw | 0:b2805b6888dc | 548 | q->flags |= PBUF_FLAG_MCASTLOOP; |
robert_jw | 0:b2805b6888dc | 549 | } |
robert_jw | 0:b2805b6888dc | 550 | #endif /* LWIP_IGMP */ |
robert_jw | 0:b2805b6888dc | 551 | |
robert_jw | 0:b2805b6888dc | 552 | |
robert_jw | 0:b2805b6888dc | 553 | /* PCB local address is IP_ANY_ADDR? */ |
robert_jw | 0:b2805b6888dc | 554 | if (ip_addr_isany(&pcb->local_ip)) { |
robert_jw | 0:b2805b6888dc | 555 | /* use outgoing network interface IP address as source address */ |
robert_jw | 0:b2805b6888dc | 556 | src_ip = &(netif->ip_addr); |
robert_jw | 0:b2805b6888dc | 557 | } else { |
robert_jw | 0:b2805b6888dc | 558 | /* check if UDP PCB local IP address is correct |
robert_jw | 0:b2805b6888dc | 559 | * this could be an old address if netif->ip_addr has changed */ |
robert_jw | 0:b2805b6888dc | 560 | if (!ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) { |
robert_jw | 0:b2805b6888dc | 561 | /* local_ip doesn't match, drop the packet */ |
robert_jw | 0:b2805b6888dc | 562 | if (q != p) { |
robert_jw | 0:b2805b6888dc | 563 | /* free the header pbuf */ |
robert_jw | 0:b2805b6888dc | 564 | pbuf_free(q); |
robert_jw | 0:b2805b6888dc | 565 | q = NULL; |
robert_jw | 0:b2805b6888dc | 566 | /* p is still referenced by the caller, and will live on */ |
robert_jw | 0:b2805b6888dc | 567 | } |
robert_jw | 0:b2805b6888dc | 568 | return ERR_VAL; |
robert_jw | 0:b2805b6888dc | 569 | } |
robert_jw | 0:b2805b6888dc | 570 | /* use UDP PCB local IP address as source address */ |
robert_jw | 0:b2805b6888dc | 571 | src_ip = &(pcb->local_ip); |
robert_jw | 0:b2805b6888dc | 572 | } |
robert_jw | 0:b2805b6888dc | 573 | |
robert_jw | 0:b2805b6888dc | 574 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len)); |
robert_jw | 0:b2805b6888dc | 575 | |
robert_jw | 0:b2805b6888dc | 576 | #if LWIP_UDPLITE |
robert_jw | 0:b2805b6888dc | 577 | /* UDP Lite protocol? */ |
robert_jw | 0:b2805b6888dc | 578 | if (pcb->flags & UDP_FLAGS_UDPLITE) { |
robert_jw | 0:b2805b6888dc | 579 | u16_t chklen, chklen_hdr; |
robert_jw | 0:b2805b6888dc | 580 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len)); |
robert_jw | 0:b2805b6888dc | 581 | /* set UDP message length in UDP header */ |
robert_jw | 0:b2805b6888dc | 582 | chklen_hdr = chklen = pcb->chksum_len_tx; |
robert_jw | 0:b2805b6888dc | 583 | if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) { |
robert_jw | 0:b2805b6888dc | 584 | if (chklen != 0) { |
robert_jw | 0:b2805b6888dc | 585 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen)); |
robert_jw | 0:b2805b6888dc | 586 | } |
robert_jw | 0:b2805b6888dc | 587 | /* For UDP-Lite, checksum length of 0 means checksum |
robert_jw | 0:b2805b6888dc | 588 | over the complete packet. (See RFC 3828 chap. 3.1) |
robert_jw | 0:b2805b6888dc | 589 | At least the UDP-Lite header must be covered by the |
robert_jw | 0:b2805b6888dc | 590 | checksum, therefore, if chksum_len has an illegal |
robert_jw | 0:b2805b6888dc | 591 | value, we generate the checksum over the complete |
robert_jw | 0:b2805b6888dc | 592 | packet to be safe. */ |
robert_jw | 0:b2805b6888dc | 593 | chklen_hdr = 0; |
robert_jw | 0:b2805b6888dc | 594 | chklen = q->tot_len; |
robert_jw | 0:b2805b6888dc | 595 | } |
robert_jw | 0:b2805b6888dc | 596 | udphdr->len = htons(chklen_hdr); |
robert_jw | 0:b2805b6888dc | 597 | /* calculate checksum */ |
robert_jw | 0:b2805b6888dc | 598 | #if CHECKSUM_GEN_UDP |
robert_jw | 0:b2805b6888dc | 599 | udphdr->chksum = inet_chksum_pseudo_partial(q, src_ip, dst_ip, |
robert_jw | 0:b2805b6888dc | 600 | IP_PROTO_UDPLITE, q->tot_len, |
robert_jw | 0:b2805b6888dc | 601 | #if !LWIP_CHECKSUM_ON_COPY |
robert_jw | 0:b2805b6888dc | 602 | chklen); |
robert_jw | 0:b2805b6888dc | 603 | #else /* !LWIP_CHECKSUM_ON_COPY */ |
robert_jw | 0:b2805b6888dc | 604 | (have_chksum ? UDP_HLEN : chklen)); |
robert_jw | 0:b2805b6888dc | 605 | if (have_chksum) { |
robert_jw | 0:b2805b6888dc | 606 | u32_t acc; |
robert_jw | 0:b2805b6888dc | 607 | acc = udphdr->chksum + (u16_t)~(chksum); |
robert_jw | 0:b2805b6888dc | 608 | udphdr->chksum = FOLD_U32T(acc); |
robert_jw | 0:b2805b6888dc | 609 | } |
robert_jw | 0:b2805b6888dc | 610 | #endif /* !LWIP_CHECKSUM_ON_COPY */ |
robert_jw | 0:b2805b6888dc | 611 | |
robert_jw | 0:b2805b6888dc | 612 | /* chksum zero must become 0xffff, as zero means 'no checksum' */ |
robert_jw | 0:b2805b6888dc | 613 | if (udphdr->chksum == 0x0000) { |
robert_jw | 0:b2805b6888dc | 614 | udphdr->chksum = 0xffff; |
robert_jw | 0:b2805b6888dc | 615 | } |
robert_jw | 0:b2805b6888dc | 616 | #endif /* CHECKSUM_GEN_UDP */ |
robert_jw | 0:b2805b6888dc | 617 | /* output to IP */ |
robert_jw | 0:b2805b6888dc | 618 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDPLITE,)\n")); |
robert_jw | 0:b2805b6888dc | 619 | #if LWIP_NETIF_HWADDRHINT |
robert_jw | 0:b2805b6888dc | 620 | netif->addr_hint = &(pcb->addr_hint); |
robert_jw | 0:b2805b6888dc | 621 | #endif /* LWIP_NETIF_HWADDRHINT*/ |
robert_jw | 0:b2805b6888dc | 622 | err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDPLITE, netif); |
robert_jw | 0:b2805b6888dc | 623 | #if LWIP_NETIF_HWADDRHINT |
robert_jw | 0:b2805b6888dc | 624 | netif->addr_hint = NULL; |
robert_jw | 0:b2805b6888dc | 625 | #endif /* LWIP_NETIF_HWADDRHINT*/ |
robert_jw | 0:b2805b6888dc | 626 | } else |
robert_jw | 0:b2805b6888dc | 627 | #endif /* LWIP_UDPLITE */ |
robert_jw | 0:b2805b6888dc | 628 | { /* UDP */ |
robert_jw | 0:b2805b6888dc | 629 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len)); |
robert_jw | 0:b2805b6888dc | 630 | udphdr->len = htons(q->tot_len); |
robert_jw | 0:b2805b6888dc | 631 | /* calculate checksum */ |
robert_jw | 0:b2805b6888dc | 632 | #if CHECKSUM_GEN_UDP |
robert_jw | 0:b2805b6888dc | 633 | if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) { |
robert_jw | 0:b2805b6888dc | 634 | u16_t udpchksum; |
robert_jw | 0:b2805b6888dc | 635 | #if LWIP_CHECKSUM_ON_COPY |
robert_jw | 0:b2805b6888dc | 636 | if (have_chksum) { |
robert_jw | 0:b2805b6888dc | 637 | u32_t acc; |
robert_jw | 0:b2805b6888dc | 638 | udpchksum = inet_chksum_pseudo_partial(q, src_ip, dst_ip, IP_PROTO_UDP, |
robert_jw | 0:b2805b6888dc | 639 | q->tot_len, UDP_HLEN); |
robert_jw | 0:b2805b6888dc | 640 | acc = udpchksum + (u16_t)~(chksum); |
robert_jw | 0:b2805b6888dc | 641 | udpchksum = FOLD_U32T(acc); |
robert_jw | 0:b2805b6888dc | 642 | } else |
robert_jw | 0:b2805b6888dc | 643 | #endif /* LWIP_CHECKSUM_ON_COPY */ |
robert_jw | 0:b2805b6888dc | 644 | { |
robert_jw | 0:b2805b6888dc | 645 | udpchksum = inet_chksum_pseudo(q, src_ip, dst_ip, IP_PROTO_UDP, q->tot_len); |
robert_jw | 0:b2805b6888dc | 646 | } |
robert_jw | 0:b2805b6888dc | 647 | |
robert_jw | 0:b2805b6888dc | 648 | /* chksum zero must become 0xffff, as zero means 'no checksum' */ |
robert_jw | 0:b2805b6888dc | 649 | if (udpchksum == 0x0000) { |
robert_jw | 0:b2805b6888dc | 650 | udpchksum = 0xffff; |
robert_jw | 0:b2805b6888dc | 651 | } |
robert_jw | 0:b2805b6888dc | 652 | udphdr->chksum = udpchksum; |
robert_jw | 0:b2805b6888dc | 653 | } |
robert_jw | 0:b2805b6888dc | 654 | #endif /* CHECKSUM_GEN_UDP */ |
robert_jw | 0:b2805b6888dc | 655 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum)); |
robert_jw | 0:b2805b6888dc | 656 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n")); |
robert_jw | 0:b2805b6888dc | 657 | /* output to IP */ |
robert_jw | 0:b2805b6888dc | 658 | #if LWIP_NETIF_HWADDRHINT |
robert_jw | 0:b2805b6888dc | 659 | netif->addr_hint = &(pcb->addr_hint); |
robert_jw | 0:b2805b6888dc | 660 | #endif /* LWIP_NETIF_HWADDRHINT*/ |
robert_jw | 0:b2805b6888dc | 661 | err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif); |
robert_jw | 0:b2805b6888dc | 662 | #if LWIP_NETIF_HWADDRHINT |
robert_jw | 0:b2805b6888dc | 663 | netif->addr_hint = NULL; |
robert_jw | 0:b2805b6888dc | 664 | #endif /* LWIP_NETIF_HWADDRHINT*/ |
robert_jw | 0:b2805b6888dc | 665 | } |
robert_jw | 0:b2805b6888dc | 666 | /* TODO: must this be increased even if error occured? */ |
robert_jw | 0:b2805b6888dc | 667 | snmp_inc_udpoutdatagrams(); |
robert_jw | 0:b2805b6888dc | 668 | |
robert_jw | 0:b2805b6888dc | 669 | /* did we chain a separate header pbuf earlier? */ |
robert_jw | 0:b2805b6888dc | 670 | if (q != p) { |
robert_jw | 0:b2805b6888dc | 671 | /* free the header pbuf */ |
robert_jw | 0:b2805b6888dc | 672 | pbuf_free(q); |
robert_jw | 0:b2805b6888dc | 673 | q = NULL; |
robert_jw | 0:b2805b6888dc | 674 | /* p is still referenced by the caller, and will live on */ |
robert_jw | 0:b2805b6888dc | 675 | } |
robert_jw | 0:b2805b6888dc | 676 | |
robert_jw | 0:b2805b6888dc | 677 | UDP_STATS_INC(udp.xmit); |
robert_jw | 0:b2805b6888dc | 678 | return err; |
robert_jw | 0:b2805b6888dc | 679 | } |
robert_jw | 0:b2805b6888dc | 680 | |
robert_jw | 0:b2805b6888dc | 681 | /** |
robert_jw | 0:b2805b6888dc | 682 | * Bind an UDP PCB. |
robert_jw | 0:b2805b6888dc | 683 | * |
robert_jw | 0:b2805b6888dc | 684 | * @param pcb UDP PCB to be bound with a local address ipaddr and port. |
robert_jw | 0:b2805b6888dc | 685 | * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to |
robert_jw | 0:b2805b6888dc | 686 | * bind to all local interfaces. |
robert_jw | 0:b2805b6888dc | 687 | * @param port local UDP port to bind with. Use 0 to automatically bind |
robert_jw | 0:b2805b6888dc | 688 | * to a random port between UDP_LOCAL_PORT_RANGE_START and |
robert_jw | 0:b2805b6888dc | 689 | * UDP_LOCAL_PORT_RANGE_END. |
robert_jw | 0:b2805b6888dc | 690 | * |
robert_jw | 0:b2805b6888dc | 691 | * ipaddr & port are expected to be in the same byte order as in the pcb. |
robert_jw | 0:b2805b6888dc | 692 | * |
robert_jw | 0:b2805b6888dc | 693 | * @return lwIP error code. |
robert_jw | 0:b2805b6888dc | 694 | * - ERR_OK. Successful. No error occured. |
robert_jw | 0:b2805b6888dc | 695 | * - ERR_USE. The specified ipaddr and port are already bound to by |
robert_jw | 0:b2805b6888dc | 696 | * another UDP PCB. |
robert_jw | 0:b2805b6888dc | 697 | * |
robert_jw | 0:b2805b6888dc | 698 | * @see udp_disconnect() |
robert_jw | 0:b2805b6888dc | 699 | */ |
robert_jw | 0:b2805b6888dc | 700 | err_t |
robert_jw | 0:b2805b6888dc | 701 | udp_bind(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port) |
robert_jw | 0:b2805b6888dc | 702 | { |
robert_jw | 0:b2805b6888dc | 703 | struct udp_pcb *ipcb; |
robert_jw | 0:b2805b6888dc | 704 | u8_t rebind; |
robert_jw | 0:b2805b6888dc | 705 | |
robert_jw | 0:b2805b6888dc | 706 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = ")); |
robert_jw | 0:b2805b6888dc | 707 | ip_addr_debug_print(UDP_DEBUG, ipaddr); |
robert_jw | 0:b2805b6888dc | 708 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port)); |
robert_jw | 0:b2805b6888dc | 709 | |
robert_jw | 0:b2805b6888dc | 710 | rebind = 0; |
robert_jw | 0:b2805b6888dc | 711 | /* Check for double bind and rebind of the same pcb */ |
robert_jw | 0:b2805b6888dc | 712 | for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) { |
robert_jw | 0:b2805b6888dc | 713 | /* is this UDP PCB already on active list? */ |
robert_jw | 0:b2805b6888dc | 714 | if (pcb == ipcb) { |
robert_jw | 0:b2805b6888dc | 715 | /* pcb may occur at most once in active list */ |
robert_jw | 0:b2805b6888dc | 716 | LWIP_ASSERT("rebind == 0", rebind == 0); |
robert_jw | 0:b2805b6888dc | 717 | /* pcb already in list, just rebind */ |
robert_jw | 0:b2805b6888dc | 718 | rebind = 1; |
robert_jw | 0:b2805b6888dc | 719 | } |
robert_jw | 0:b2805b6888dc | 720 | |
robert_jw | 0:b2805b6888dc | 721 | /* By default, we don't allow to bind to a port that any other udp |
robert_jw | 0:b2805b6888dc | 722 | PCB is alread bound to, unless *all* PCBs with that port have tha |
robert_jw | 0:b2805b6888dc | 723 | REUSEADDR flag set. */ |
robert_jw | 0:b2805b6888dc | 724 | #if SO_REUSE |
robert_jw | 0:b2805b6888dc | 725 | else if (((pcb->so_options & SOF_REUSEADDR) == 0) && |
robert_jw | 0:b2805b6888dc | 726 | ((ipcb->so_options & SOF_REUSEADDR) == 0)) { |
robert_jw | 0:b2805b6888dc | 727 | #else /* SO_REUSE */ |
robert_jw | 0:b2805b6888dc | 728 | /* port matches that of PCB in list and REUSEADDR not set -> reject */ |
robert_jw | 0:b2805b6888dc | 729 | else { |
robert_jw | 0:b2805b6888dc | 730 | #endif /* SO_REUSE */ |
robert_jw | 0:b2805b6888dc | 731 | if ((ipcb->local_port == port) && |
robert_jw | 0:b2805b6888dc | 732 | /* IP address matches, or one is IP_ADDR_ANY? */ |
robert_jw | 0:b2805b6888dc | 733 | (ip_addr_isany(&(ipcb->local_ip)) || |
robert_jw | 0:b2805b6888dc | 734 | ip_addr_isany(ipaddr) || |
robert_jw | 0:b2805b6888dc | 735 | ip_addr_cmp(&(ipcb->local_ip), ipaddr))) { |
robert_jw | 0:b2805b6888dc | 736 | /* other PCB already binds to this local IP and port */ |
robert_jw | 0:b2805b6888dc | 737 | LWIP_DEBUGF(UDP_DEBUG, |
robert_jw | 0:b2805b6888dc | 738 | ("udp_bind: local port %"U16_F" already bound by another pcb\n", port)); |
robert_jw | 0:b2805b6888dc | 739 | return ERR_USE; |
robert_jw | 0:b2805b6888dc | 740 | } |
robert_jw | 0:b2805b6888dc | 741 | } |
robert_jw | 0:b2805b6888dc | 742 | } |
robert_jw | 0:b2805b6888dc | 743 | |
robert_jw | 0:b2805b6888dc | 744 | ip_addr_set(&pcb->local_ip, ipaddr); |
robert_jw | 0:b2805b6888dc | 745 | |
robert_jw | 0:b2805b6888dc | 746 | /* no port specified? */ |
robert_jw | 0:b2805b6888dc | 747 | if (port == 0) { |
robert_jw | 0:b2805b6888dc | 748 | #ifndef UDP_LOCAL_PORT_RANGE_START |
robert_jw | 0:b2805b6888dc | 749 | /* From http://www.iana.org/assignments/port-numbers: |
robert_jw | 0:b2805b6888dc | 750 | "The Dynamic and/or Private Ports are those from 49152 through 65535" */ |
robert_jw | 0:b2805b6888dc | 751 | #define UDP_LOCAL_PORT_RANGE_START 0xc000 |
robert_jw | 0:b2805b6888dc | 752 | #define UDP_LOCAL_PORT_RANGE_END 0xffff |
robert_jw | 0:b2805b6888dc | 753 | #endif |
robert_jw | 0:b2805b6888dc | 754 | port = UDP_LOCAL_PORT_RANGE_START; |
robert_jw | 0:b2805b6888dc | 755 | ipcb = udp_pcbs; |
robert_jw | 0:b2805b6888dc | 756 | while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) { |
robert_jw | 0:b2805b6888dc | 757 | if (ipcb->local_port == port) { |
robert_jw | 0:b2805b6888dc | 758 | /* port is already used by another udp_pcb */ |
robert_jw | 0:b2805b6888dc | 759 | port++; |
robert_jw | 0:b2805b6888dc | 760 | /* restart scanning all udp pcbs */ |
robert_jw | 0:b2805b6888dc | 761 | ipcb = udp_pcbs; |
robert_jw | 0:b2805b6888dc | 762 | } else { |
robert_jw | 0:b2805b6888dc | 763 | /* go on with next udp pcb */ |
robert_jw | 0:b2805b6888dc | 764 | ipcb = ipcb->next; |
robert_jw | 0:b2805b6888dc | 765 | } |
robert_jw | 0:b2805b6888dc | 766 | } |
robert_jw | 0:b2805b6888dc | 767 | if (ipcb != NULL) { |
robert_jw | 0:b2805b6888dc | 768 | /* no more ports available in local range */ |
robert_jw | 0:b2805b6888dc | 769 | LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n")); |
robert_jw | 0:b2805b6888dc | 770 | return ERR_USE; |
robert_jw | 0:b2805b6888dc | 771 | } |
robert_jw | 0:b2805b6888dc | 772 | } |
robert_jw | 0:b2805b6888dc | 773 | pcb->local_port = port; |
robert_jw | 0:b2805b6888dc | 774 | snmp_insert_udpidx_tree(pcb); |
robert_jw | 0:b2805b6888dc | 775 | /* pcb not active yet? */ |
robert_jw | 0:b2805b6888dc | 776 | if (rebind == 0) { |
robert_jw | 0:b2805b6888dc | 777 | /* place the PCB on the active list if not already there */ |
robert_jw | 0:b2805b6888dc | 778 | pcb->next = udp_pcbs; |
robert_jw | 0:b2805b6888dc | 779 | udp_pcbs = pcb; |
robert_jw | 0:b2805b6888dc | 780 | } |
robert_jw | 0:b2805b6888dc | 781 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
robert_jw | 0:b2805b6888dc | 782 | ("udp_bind: bound to %"U16_F".%"U16_F".%"U16_F".%"U16_F", port %"U16_F"\n", |
robert_jw | 0:b2805b6888dc | 783 | ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip), |
robert_jw | 0:b2805b6888dc | 784 | ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip), |
robert_jw | 0:b2805b6888dc | 785 | pcb->local_port)); |
robert_jw | 0:b2805b6888dc | 786 | return ERR_OK; |
robert_jw | 0:b2805b6888dc | 787 | } |
robert_jw | 0:b2805b6888dc | 788 | /** |
robert_jw | 0:b2805b6888dc | 789 | * Connect an UDP PCB. |
robert_jw | 0:b2805b6888dc | 790 | * |
robert_jw | 0:b2805b6888dc | 791 | * This will associate the UDP PCB with the remote address. |
robert_jw | 0:b2805b6888dc | 792 | * |
robert_jw | 0:b2805b6888dc | 793 | * @param pcb UDP PCB to be connected with remote address ipaddr and port. |
robert_jw | 0:b2805b6888dc | 794 | * @param ipaddr remote IP address to connect with. |
robert_jw | 0:b2805b6888dc | 795 | * @param port remote UDP port to connect with. |
robert_jw | 0:b2805b6888dc | 796 | * |
robert_jw | 0:b2805b6888dc | 797 | * @return lwIP error code |
robert_jw | 0:b2805b6888dc | 798 | * |
robert_jw | 0:b2805b6888dc | 799 | * ipaddr & port are expected to be in the same byte order as in the pcb. |
robert_jw | 0:b2805b6888dc | 800 | * |
robert_jw | 0:b2805b6888dc | 801 | * The udp pcb is bound to a random local port if not already bound. |
robert_jw | 0:b2805b6888dc | 802 | * |
robert_jw | 0:b2805b6888dc | 803 | * @see udp_disconnect() |
robert_jw | 0:b2805b6888dc | 804 | */ |
robert_jw | 0:b2805b6888dc | 805 | err_t |
robert_jw | 0:b2805b6888dc | 806 | udp_connect(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port) |
robert_jw | 0:b2805b6888dc | 807 | { |
robert_jw | 0:b2805b6888dc | 808 | struct udp_pcb *ipcb; |
robert_jw | 0:b2805b6888dc | 809 | |
robert_jw | 0:b2805b6888dc | 810 | if (pcb->local_port == 0) { |
robert_jw | 0:b2805b6888dc | 811 | err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port); |
robert_jw | 0:b2805b6888dc | 812 | if (err != ERR_OK) { |
robert_jw | 0:b2805b6888dc | 813 | return err; |
robert_jw | 0:b2805b6888dc | 814 | } |
robert_jw | 0:b2805b6888dc | 815 | } |
robert_jw | 0:b2805b6888dc | 816 | |
robert_jw | 0:b2805b6888dc | 817 | ip_addr_set(&pcb->remote_ip, ipaddr); |
robert_jw | 0:b2805b6888dc | 818 | pcb->remote_port = port; |
robert_jw | 0:b2805b6888dc | 819 | pcb->flags |= UDP_FLAGS_CONNECTED; |
robert_jw | 0:b2805b6888dc | 820 | /** TODO: this functionality belongs in upper layers */ |
robert_jw | 0:b2805b6888dc | 821 | #ifdef LWIP_UDP_TODO |
robert_jw | 0:b2805b6888dc | 822 | /* Nail down local IP for netconn_addr()/getsockname() */ |
robert_jw | 0:b2805b6888dc | 823 | if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) { |
robert_jw | 0:b2805b6888dc | 824 | struct netif *netif; |
robert_jw | 0:b2805b6888dc | 825 | |
robert_jw | 0:b2805b6888dc | 826 | if ((netif = ip_route(&(pcb->remote_ip))) == NULL) { |
robert_jw | 0:b2805b6888dc | 827 | LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n", pcb->remote_ip.addr)); |
robert_jw | 0:b2805b6888dc | 828 | UDP_STATS_INC(udp.rterr); |
robert_jw | 0:b2805b6888dc | 829 | return ERR_RTE; |
robert_jw | 0:b2805b6888dc | 830 | } |
robert_jw | 0:b2805b6888dc | 831 | /** TODO: this will bind the udp pcb locally, to the interface which |
robert_jw | 0:b2805b6888dc | 832 | is used to route output packets to the remote address. However, we |
robert_jw | 0:b2805b6888dc | 833 | might want to accept incoming packets on any interface! */ |
robert_jw | 0:b2805b6888dc | 834 | pcb->local_ip = netif->ip_addr; |
robert_jw | 0:b2805b6888dc | 835 | } else if (ip_addr_isany(&pcb->remote_ip)) { |
robert_jw | 0:b2805b6888dc | 836 | pcb->local_ip.addr = 0; |
robert_jw | 0:b2805b6888dc | 837 | } |
robert_jw | 0:b2805b6888dc | 838 | #endif |
robert_jw | 0:b2805b6888dc | 839 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
robert_jw | 0:b2805b6888dc | 840 | ("udp_connect: connected to %"U16_F".%"U16_F".%"U16_F".%"U16_F",port %"U16_F"\n", |
robert_jw | 0:b2805b6888dc | 841 | ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip), |
robert_jw | 0:b2805b6888dc | 842 | ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip), |
robert_jw | 0:b2805b6888dc | 843 | pcb->local_port)); |
robert_jw | 0:b2805b6888dc | 844 | |
robert_jw | 0:b2805b6888dc | 845 | /* Insert UDP PCB into the list of active UDP PCBs. */ |
robert_jw | 0:b2805b6888dc | 846 | for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) { |
robert_jw | 0:b2805b6888dc | 847 | if (pcb == ipcb) { |
robert_jw | 0:b2805b6888dc | 848 | /* already on the list, just return */ |
robert_jw | 0:b2805b6888dc | 849 | return ERR_OK; |
robert_jw | 0:b2805b6888dc | 850 | } |
robert_jw | 0:b2805b6888dc | 851 | } |
robert_jw | 0:b2805b6888dc | 852 | /* PCB not yet on the list, add PCB now */ |
robert_jw | 0:b2805b6888dc | 853 | pcb->next = udp_pcbs; |
robert_jw | 0:b2805b6888dc | 854 | udp_pcbs = pcb; |
robert_jw | 0:b2805b6888dc | 855 | return ERR_OK; |
robert_jw | 0:b2805b6888dc | 856 | } |
robert_jw | 0:b2805b6888dc | 857 | |
robert_jw | 0:b2805b6888dc | 858 | /** |
robert_jw | 0:b2805b6888dc | 859 | * Disconnect a UDP PCB |
robert_jw | 0:b2805b6888dc | 860 | * |
robert_jw | 0:b2805b6888dc | 861 | * @param pcb the udp pcb to disconnect. |
robert_jw | 0:b2805b6888dc | 862 | */ |
robert_jw | 0:b2805b6888dc | 863 | void |
robert_jw | 0:b2805b6888dc | 864 | udp_disconnect(struct udp_pcb *pcb) |
robert_jw | 0:b2805b6888dc | 865 | { |
robert_jw | 0:b2805b6888dc | 866 | /* reset remote address association */ |
robert_jw | 0:b2805b6888dc | 867 | ip_addr_set_any(&pcb->remote_ip); |
robert_jw | 0:b2805b6888dc | 868 | pcb->remote_port = 0; |
robert_jw | 0:b2805b6888dc | 869 | /* mark PCB as unconnected */ |
robert_jw | 0:b2805b6888dc | 870 | pcb->flags &= ~UDP_FLAGS_CONNECTED; |
robert_jw | 0:b2805b6888dc | 871 | } |
robert_jw | 0:b2805b6888dc | 872 | |
robert_jw | 0:b2805b6888dc | 873 | /** |
robert_jw | 0:b2805b6888dc | 874 | * Set a receive callback for a UDP PCB |
robert_jw | 0:b2805b6888dc | 875 | * |
robert_jw | 0:b2805b6888dc | 876 | * This callback will be called when receiving a datagram for the pcb. |
robert_jw | 0:b2805b6888dc | 877 | * |
robert_jw | 0:b2805b6888dc | 878 | * @param pcb the pcb for wich to set the recv callback |
robert_jw | 0:b2805b6888dc | 879 | * @param recv function pointer of the callback function |
robert_jw | 0:b2805b6888dc | 880 | * @param recv_arg additional argument to pass to the callback function |
robert_jw | 0:b2805b6888dc | 881 | */ |
robert_jw | 0:b2805b6888dc | 882 | void |
robert_jw | 0:b2805b6888dc | 883 | udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg) |
robert_jw | 0:b2805b6888dc | 884 | { |
robert_jw | 0:b2805b6888dc | 885 | /* remember recv() callback and user data */ |
robert_jw | 0:b2805b6888dc | 886 | pcb->recv = recv; |
robert_jw | 0:b2805b6888dc | 887 | pcb->recv_arg = recv_arg; |
robert_jw | 0:b2805b6888dc | 888 | } |
robert_jw | 0:b2805b6888dc | 889 | |
robert_jw | 0:b2805b6888dc | 890 | /** |
robert_jw | 0:b2805b6888dc | 891 | * Remove an UDP PCB. |
robert_jw | 0:b2805b6888dc | 892 | * |
robert_jw | 0:b2805b6888dc | 893 | * @param pcb UDP PCB to be removed. The PCB is removed from the list of |
robert_jw | 0:b2805b6888dc | 894 | * UDP PCB's and the data structure is freed from memory. |
robert_jw | 0:b2805b6888dc | 895 | * |
robert_jw | 0:b2805b6888dc | 896 | * @see udp_new() |
robert_jw | 0:b2805b6888dc | 897 | */ |
robert_jw | 0:b2805b6888dc | 898 | void |
robert_jw | 0:b2805b6888dc | 899 | udp_remove(struct udp_pcb *pcb) |
robert_jw | 0:b2805b6888dc | 900 | { |
robert_jw | 0:b2805b6888dc | 901 | struct udp_pcb *pcb2; |
robert_jw | 0:b2805b6888dc | 902 | |
robert_jw | 0:b2805b6888dc | 903 | snmp_delete_udpidx_tree(pcb); |
robert_jw | 0:b2805b6888dc | 904 | /* pcb to be removed is first in list? */ |
robert_jw | 0:b2805b6888dc | 905 | if (udp_pcbs == pcb) { |
robert_jw | 0:b2805b6888dc | 906 | /* make list start at 2nd pcb */ |
robert_jw | 0:b2805b6888dc | 907 | udp_pcbs = udp_pcbs->next; |
robert_jw | 0:b2805b6888dc | 908 | /* pcb not 1st in list */ |
robert_jw | 0:b2805b6888dc | 909 | } else { |
robert_jw | 0:b2805b6888dc | 910 | for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) { |
robert_jw | 0:b2805b6888dc | 911 | /* find pcb in udp_pcbs list */ |
robert_jw | 0:b2805b6888dc | 912 | if (pcb2->next != NULL && pcb2->next == pcb) { |
robert_jw | 0:b2805b6888dc | 913 | /* remove pcb from list */ |
robert_jw | 0:b2805b6888dc | 914 | pcb2->next = pcb->next; |
robert_jw | 0:b2805b6888dc | 915 | } |
robert_jw | 0:b2805b6888dc | 916 | } |
robert_jw | 0:b2805b6888dc | 917 | } |
robert_jw | 0:b2805b6888dc | 918 | memp_free(MEMP_UDP_PCB, pcb); |
robert_jw | 0:b2805b6888dc | 919 | } |
robert_jw | 0:b2805b6888dc | 920 | |
robert_jw | 0:b2805b6888dc | 921 | /** |
robert_jw | 0:b2805b6888dc | 922 | * Create a UDP PCB. |
robert_jw | 0:b2805b6888dc | 923 | * |
robert_jw | 0:b2805b6888dc | 924 | * @return The UDP PCB which was created. NULL if the PCB data structure |
robert_jw | 0:b2805b6888dc | 925 | * could not be allocated. |
robert_jw | 0:b2805b6888dc | 926 | * |
robert_jw | 0:b2805b6888dc | 927 | * @see udp_remove() |
robert_jw | 0:b2805b6888dc | 928 | */ |
robert_jw | 0:b2805b6888dc | 929 | struct udp_pcb * |
robert_jw | 0:b2805b6888dc | 930 | udp_new(void) |
robert_jw | 0:b2805b6888dc | 931 | { |
robert_jw | 0:b2805b6888dc | 932 | struct udp_pcb *pcb; |
robert_jw | 0:b2805b6888dc | 933 | pcb = (struct udp_pcb *)memp_malloc(MEMP_UDP_PCB); |
robert_jw | 0:b2805b6888dc | 934 | /* could allocate UDP PCB? */ |
robert_jw | 0:b2805b6888dc | 935 | if (pcb != NULL) { |
robert_jw | 0:b2805b6888dc | 936 | /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0 |
robert_jw | 0:b2805b6888dc | 937 | * which means checksum is generated over the whole datagram per default |
robert_jw | 0:b2805b6888dc | 938 | * (recommended as default by RFC 3828). */ |
robert_jw | 0:b2805b6888dc | 939 | /* initialize PCB to all zeroes */ |
robert_jw | 0:b2805b6888dc | 940 | memset(pcb, 0, sizeof(struct udp_pcb)); |
robert_jw | 0:b2805b6888dc | 941 | pcb->ttl = UDP_TTL; |
robert_jw | 0:b2805b6888dc | 942 | } |
robert_jw | 0:b2805b6888dc | 943 | return pcb; |
robert_jw | 0:b2805b6888dc | 944 | } |
robert_jw | 0:b2805b6888dc | 945 | |
robert_jw | 0:b2805b6888dc | 946 | #if UDP_DEBUG |
robert_jw | 0:b2805b6888dc | 947 | /** |
robert_jw | 0:b2805b6888dc | 948 | * Print UDP header information for debug purposes. |
robert_jw | 0:b2805b6888dc | 949 | * |
robert_jw | 0:b2805b6888dc | 950 | * @param udphdr pointer to the udp header in memory. |
robert_jw | 0:b2805b6888dc | 951 | */ |
robert_jw | 0:b2805b6888dc | 952 | void |
robert_jw | 0:b2805b6888dc | 953 | udp_debug_print(struct udp_hdr *udphdr) |
robert_jw | 0:b2805b6888dc | 954 | { |
robert_jw | 0:b2805b6888dc | 955 | LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n")); |
robert_jw | 0:b2805b6888dc | 956 | LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); |
robert_jw | 0:b2805b6888dc | 957 | LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n", |
robert_jw | 0:b2805b6888dc | 958 | ntohs(udphdr->src), ntohs(udphdr->dest))); |
robert_jw | 0:b2805b6888dc | 959 | LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); |
robert_jw | 0:b2805b6888dc | 960 | LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n", |
robert_jw | 0:b2805b6888dc | 961 | ntohs(udphdr->len), ntohs(udphdr->chksum))); |
robert_jw | 0:b2805b6888dc | 962 | LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); |
robert_jw | 0:b2805b6888dc | 963 | } |
robert_jw | 0:b2805b6888dc | 964 | #endif /* UDP_DEBUG */ |
robert_jw | 0:b2805b6888dc | 965 | |
robert_jw | 0:b2805b6888dc | 966 | #endif /* LWIP_UDP */ |