Ethernet for Nucleo and Disco board STM32F746 works with gcc and arm. IAC is untested

Dependents:   STM32F746_iothub_client_sample_mqtt DISCO-F746NG_Ethernet Nucleo_F746ZG_Ethernet thethingsiO-DISCO_F746NG-mqtt ... more

Committer:
DieterGraef
Date:
Thu Jun 23 09:04:23 2016 +0000
Revision:
1:28ba13dd96f7
Parent:
0:d26c1b55cfca
corrected MAC issue. The MAC is now 02:00:00:xx:xx:xx where xx is the sum over the unique device register

Who changed what in which revision?

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