I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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