EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Fri Jun 11 16:05:15 2010 +0000
Revision:
0:632c9925f013
Child:
5:dd63a1e02b1b

        

Who changed what in which revision?

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