Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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