NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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