First step: AutoIP compiled in and working

Dependencies:   mbed

Committer:
darran
Date:
Fri Jun 18 15:54:21 2010 +0000
Revision:
1:4218cacaf696
Parent:
0:55a05330f8cc

        

Who changed what in which revision?

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