Official mbed lwIP library (version 1.4.0)

Dependents:   LwIPNetworking NetServicesMin EthernetInterface EthernetInterface_RSF ... more

Legacy Networking Libraries

This is an mbed 2 networking library. For mbed OS 5, lwip has been integrated with built-in networking interfaces. The networking libraries have been revised to better support additional network stacks and thread safety here.

This library is based on the code of lwIP v1.4.0

Copyright (c) 2001, 2002 Swedish Institute of Computer Science.
All rights reserved. 

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
Committer:
mbed_official
Date:
Mon Mar 14 16:15:36 2016 +0000
Revision:
20:08f08bfc3f3d
Parent:
0:51ac1d130fd4
Synchronized with git revision fec574a5ed6db26aca1b13992ff271bf527d4a0d

Full URL: https://github.com/mbedmicro/mbed/commit/fec574a5ed6db26aca1b13992ff271bf527d4a0d/

Increased allocated netbufs to handle DTLS handshakes

Who changed what in which revision?

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