Philippe Bazot / Mbed 2 deprecated DU4SmartCities

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
icraggs
Date:
Wed Oct 01 13:27:35 2014 +0000
Revision:
8:80d49dd91542
Parent:
6:37b6d0d56190
Remove conditional compilation for IBM IoT settings

Who changed what in which revision?

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