Web server based weather station using Sparkfun Weather Meters.

Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
AdamGreen
Date:
Sat Feb 25 03:28:05 2012 +0000
Revision:
1:c7958aa34fa1
Parent:
0:616601bde9fb
Use published libraries where possible.

Who changed what in which revision?

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