Committer:
mbed714
Date:
Wed Sep 29 19:48:03 2010 +0000
Revision:
0:98aadd37a5c5

        

Who changed what in which revision?

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