Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
dcoban
Date:
Tue Apr 03 18:43:13 2012 +0000
Revision:
0:1a61c61d0845

        

Who changed what in which revision?

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