Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: blinky_max32630fthr
features/FEATURE_LWIP/lwip-interface/lwip/src/core/lwip_udp.c@0:5c4d7b2438d3, 2016-11-11 (annotated)
- Committer:
- switches
- Date:
- Fri Nov 11 20:59:50 2016 +0000
- Revision:
- 0:5c4d7b2438d3
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:5c4d7b2438d3 | 1 | /** |
switches | 0:5c4d7b2438d3 | 2 | * @file |
switches | 0:5c4d7b2438d3 | 3 | * User Datagram Protocol module\n |
switches | 0:5c4d7b2438d3 | 4 | * See also @ref udp_raw |
switches | 0:5c4d7b2438d3 | 5 | */ |
switches | 0:5c4d7b2438d3 | 6 | |
switches | 0:5c4d7b2438d3 | 7 | /* |
switches | 0:5c4d7b2438d3 | 8 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
switches | 0:5c4d7b2438d3 | 9 | * All rights reserved. |
switches | 0:5c4d7b2438d3 | 10 | * |
switches | 0:5c4d7b2438d3 | 11 | * Redistribution and use in source and binary forms, with or without modification, |
switches | 0:5c4d7b2438d3 | 12 | * are permitted provided that the following conditions are met: |
switches | 0:5c4d7b2438d3 | 13 | * |
switches | 0:5c4d7b2438d3 | 14 | * 1. Redistributions of source code must retain the above copyright notice, |
switches | 0:5c4d7b2438d3 | 15 | * this list of conditions and the following disclaimer. |
switches | 0:5c4d7b2438d3 | 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
switches | 0:5c4d7b2438d3 | 17 | * this list of conditions and the following disclaimer in the documentation |
switches | 0:5c4d7b2438d3 | 18 | * and/or other materials provided with the distribution. |
switches | 0:5c4d7b2438d3 | 19 | * 3. The name of the author may not be used to endorse or promote products |
switches | 0:5c4d7b2438d3 | 20 | * derived from this software without specific prior written permission. |
switches | 0:5c4d7b2438d3 | 21 | * |
switches | 0:5c4d7b2438d3 | 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
switches | 0:5c4d7b2438d3 | 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
switches | 0:5c4d7b2438d3 | 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
switches | 0:5c4d7b2438d3 | 25 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
switches | 0:5c4d7b2438d3 | 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
switches | 0:5c4d7b2438d3 | 27 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
switches | 0:5c4d7b2438d3 | 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
switches | 0:5c4d7b2438d3 | 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
switches | 0:5c4d7b2438d3 | 30 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
switches | 0:5c4d7b2438d3 | 31 | * OF SUCH DAMAGE. |
switches | 0:5c4d7b2438d3 | 32 | * |
switches | 0:5c4d7b2438d3 | 33 | * This file is part of the lwIP TCP/IP stack. |
switches | 0:5c4d7b2438d3 | 34 | * |
switches | 0:5c4d7b2438d3 | 35 | * Author: Adam Dunkels <adam@sics.se> |
switches | 0:5c4d7b2438d3 | 36 | * |
switches | 0:5c4d7b2438d3 | 37 | */ |
switches | 0:5c4d7b2438d3 | 38 | |
switches | 0:5c4d7b2438d3 | 39 | /** |
switches | 0:5c4d7b2438d3 | 40 | * @defgroup udp_raw UDP |
switches | 0:5c4d7b2438d3 | 41 | * @ingroup raw_api |
switches | 0:5c4d7b2438d3 | 42 | * User Datagram Protocol module\n |
switches | 0:5c4d7b2438d3 | 43 | * @see @ref raw_api and @ref netconn |
switches | 0:5c4d7b2438d3 | 44 | */ |
switches | 0:5c4d7b2438d3 | 45 | |
switches | 0:5c4d7b2438d3 | 46 | /* udp.c |
switches | 0:5c4d7b2438d3 | 47 | * |
switches | 0:5c4d7b2438d3 | 48 | * The code for the User Datagram Protocol UDP & UDPLite (RFC 3828). |
switches | 0:5c4d7b2438d3 | 49 | * |
switches | 0:5c4d7b2438d3 | 50 | */ |
switches | 0:5c4d7b2438d3 | 51 | |
switches | 0:5c4d7b2438d3 | 52 | /* @todo Check the use of '(struct udp_pcb).chksum_len_rx'! |
switches | 0:5c4d7b2438d3 | 53 | */ |
switches | 0:5c4d7b2438d3 | 54 | |
switches | 0:5c4d7b2438d3 | 55 | #include "lwip/opt.h" |
switches | 0:5c4d7b2438d3 | 56 | |
switches | 0:5c4d7b2438d3 | 57 | #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */ |
switches | 0:5c4d7b2438d3 | 58 | |
switches | 0:5c4d7b2438d3 | 59 | #include "lwip/udp.h" |
switches | 0:5c4d7b2438d3 | 60 | #include "lwip/def.h" |
switches | 0:5c4d7b2438d3 | 61 | #include "lwip/memp.h" |
switches | 0:5c4d7b2438d3 | 62 | #include "lwip/inet_chksum.h" |
switches | 0:5c4d7b2438d3 | 63 | #include "lwip/ip_addr.h" |
switches | 0:5c4d7b2438d3 | 64 | #include "lwip/ip6.h" |
switches | 0:5c4d7b2438d3 | 65 | #include "lwip/ip6_addr.h" |
switches | 0:5c4d7b2438d3 | 66 | #include "lwip/inet_chksum.h" |
switches | 0:5c4d7b2438d3 | 67 | #include "lwip/netif.h" |
switches | 0:5c4d7b2438d3 | 68 | #include "lwip/icmp.h" |
switches | 0:5c4d7b2438d3 | 69 | #include "lwip/icmp6.h" |
switches | 0:5c4d7b2438d3 | 70 | #include "lwip/stats.h" |
switches | 0:5c4d7b2438d3 | 71 | #include "lwip/snmp.h" |
switches | 0:5c4d7b2438d3 | 72 | #include "lwip/dhcp.h" |
switches | 0:5c4d7b2438d3 | 73 | |
switches | 0:5c4d7b2438d3 | 74 | #include <string.h> |
switches | 0:5c4d7b2438d3 | 75 | |
switches | 0:5c4d7b2438d3 | 76 | #ifndef UDP_LOCAL_PORT_RANGE_START |
switches | 0:5c4d7b2438d3 | 77 | /* From http://www.iana.org/assignments/port-numbers: |
switches | 0:5c4d7b2438d3 | 78 | "The Dynamic and/or Private Ports are those from 49152 through 65535" */ |
switches | 0:5c4d7b2438d3 | 79 | #define UDP_LOCAL_PORT_RANGE_START 0xc000 |
switches | 0:5c4d7b2438d3 | 80 | #define UDP_LOCAL_PORT_RANGE_END 0xffff |
switches | 0:5c4d7b2438d3 | 81 | #define UDP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & ~UDP_LOCAL_PORT_RANGE_START) + UDP_LOCAL_PORT_RANGE_START)) |
switches | 0:5c4d7b2438d3 | 82 | #endif |
switches | 0:5c4d7b2438d3 | 83 | |
switches | 0:5c4d7b2438d3 | 84 | /* last local UDP port */ |
switches | 0:5c4d7b2438d3 | 85 | static u16_t udp_port = UDP_LOCAL_PORT_RANGE_START; |
switches | 0:5c4d7b2438d3 | 86 | |
switches | 0:5c4d7b2438d3 | 87 | /* The list of UDP PCBs */ |
switches | 0:5c4d7b2438d3 | 88 | /* exported in udp.h (was static) */ |
switches | 0:5c4d7b2438d3 | 89 | struct udp_pcb *udp_pcbs; |
switches | 0:5c4d7b2438d3 | 90 | |
switches | 0:5c4d7b2438d3 | 91 | /** |
switches | 0:5c4d7b2438d3 | 92 | * Initialize this module. |
switches | 0:5c4d7b2438d3 | 93 | */ |
switches | 0:5c4d7b2438d3 | 94 | void |
switches | 0:5c4d7b2438d3 | 95 | udp_init(void) |
switches | 0:5c4d7b2438d3 | 96 | { |
switches | 0:5c4d7b2438d3 | 97 | #if LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) |
switches | 0:5c4d7b2438d3 | 98 | udp_port = UDP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND()); |
switches | 0:5c4d7b2438d3 | 99 | #endif /* LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) */ |
switches | 0:5c4d7b2438d3 | 100 | } |
switches | 0:5c4d7b2438d3 | 101 | |
switches | 0:5c4d7b2438d3 | 102 | /** |
switches | 0:5c4d7b2438d3 | 103 | * Allocate a new local UDP port. |
switches | 0:5c4d7b2438d3 | 104 | * |
switches | 0:5c4d7b2438d3 | 105 | * @return a new (free) local UDP port number |
switches | 0:5c4d7b2438d3 | 106 | */ |
switches | 0:5c4d7b2438d3 | 107 | static u16_t |
switches | 0:5c4d7b2438d3 | 108 | udp_new_port(void) |
switches | 0:5c4d7b2438d3 | 109 | { |
switches | 0:5c4d7b2438d3 | 110 | u16_t n = 0; |
switches | 0:5c4d7b2438d3 | 111 | struct udp_pcb *pcb; |
switches | 0:5c4d7b2438d3 | 112 | |
switches | 0:5c4d7b2438d3 | 113 | again: |
switches | 0:5c4d7b2438d3 | 114 | if (udp_port++ == UDP_LOCAL_PORT_RANGE_END) { |
switches | 0:5c4d7b2438d3 | 115 | udp_port = UDP_LOCAL_PORT_RANGE_START; |
switches | 0:5c4d7b2438d3 | 116 | } |
switches | 0:5c4d7b2438d3 | 117 | /* Check all PCBs. */ |
switches | 0:5c4d7b2438d3 | 118 | for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) { |
switches | 0:5c4d7b2438d3 | 119 | if (pcb->local_port == udp_port) { |
switches | 0:5c4d7b2438d3 | 120 | if (++n > (UDP_LOCAL_PORT_RANGE_END - UDP_LOCAL_PORT_RANGE_START)) { |
switches | 0:5c4d7b2438d3 | 121 | return 0; |
switches | 0:5c4d7b2438d3 | 122 | } |
switches | 0:5c4d7b2438d3 | 123 | goto again; |
switches | 0:5c4d7b2438d3 | 124 | } |
switches | 0:5c4d7b2438d3 | 125 | } |
switches | 0:5c4d7b2438d3 | 126 | return udp_port; |
switches | 0:5c4d7b2438d3 | 127 | #if 0 |
switches | 0:5c4d7b2438d3 | 128 | struct udp_pcb *ipcb = udp_pcbs; |
switches | 0:5c4d7b2438d3 | 129 | while ((ipcb != NULL) && (udp_port != UDP_LOCAL_PORT_RANGE_END)) { |
switches | 0:5c4d7b2438d3 | 130 | if (ipcb->local_port == udp_port) { |
switches | 0:5c4d7b2438d3 | 131 | /* port is already used by another udp_pcb */ |
switches | 0:5c4d7b2438d3 | 132 | udp_port++; |
switches | 0:5c4d7b2438d3 | 133 | /* restart scanning all udp pcbs */ |
switches | 0:5c4d7b2438d3 | 134 | ipcb = udp_pcbs; |
switches | 0:5c4d7b2438d3 | 135 | } else { |
switches | 0:5c4d7b2438d3 | 136 | /* go on with next udp pcb */ |
switches | 0:5c4d7b2438d3 | 137 | ipcb = ipcb->next; |
switches | 0:5c4d7b2438d3 | 138 | } |
switches | 0:5c4d7b2438d3 | 139 | } |
switches | 0:5c4d7b2438d3 | 140 | if (ipcb != NULL) { |
switches | 0:5c4d7b2438d3 | 141 | return 0; |
switches | 0:5c4d7b2438d3 | 142 | } |
switches | 0:5c4d7b2438d3 | 143 | return udp_port; |
switches | 0:5c4d7b2438d3 | 144 | #endif |
switches | 0:5c4d7b2438d3 | 145 | } |
switches | 0:5c4d7b2438d3 | 146 | |
switches | 0:5c4d7b2438d3 | 147 | /** Common code to see if the current input packet matches the pcb |
switches | 0:5c4d7b2438d3 | 148 | * (current input packet is accessed via ip(4/6)_current_* macros) |
switches | 0:5c4d7b2438d3 | 149 | * |
switches | 0:5c4d7b2438d3 | 150 | * @param pcb pcb to check |
switches | 0:5c4d7b2438d3 | 151 | * @param inp network interface on which the datagram was received (only used for IPv4) |
switches | 0:5c4d7b2438d3 | 152 | * @param broadcast 1 if his is an IPv4 broadcast (global or subnet-only), 0 otherwise (only used for IPv4) |
switches | 0:5c4d7b2438d3 | 153 | * @return 1 on match, 0 otherwise |
switches | 0:5c4d7b2438d3 | 154 | */ |
switches | 0:5c4d7b2438d3 | 155 | static u8_t |
switches | 0:5c4d7b2438d3 | 156 | udp_input_local_match(struct udp_pcb *pcb, struct netif *inp, u8_t broadcast) |
switches | 0:5c4d7b2438d3 | 157 | { |
switches | 0:5c4d7b2438d3 | 158 | LWIP_UNUSED_ARG(inp); /* in IPv6 only case */ |
switches | 0:5c4d7b2438d3 | 159 | LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */ |
switches | 0:5c4d7b2438d3 | 160 | |
switches | 0:5c4d7b2438d3 | 161 | /* Dual-stack: PCBs listening to any IP type also listen to any IP address */ |
switches | 0:5c4d7b2438d3 | 162 | if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) { |
switches | 0:5c4d7b2438d3 | 163 | #if LWIP_IPV4 && IP_SOF_BROADCAST_RECV |
switches | 0:5c4d7b2438d3 | 164 | if ((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) { |
switches | 0:5c4d7b2438d3 | 165 | return 0; |
switches | 0:5c4d7b2438d3 | 166 | } |
switches | 0:5c4d7b2438d3 | 167 | #endif /* LWIP_IPV4 && IP_SOF_BROADCAST_RECV */ |
switches | 0:5c4d7b2438d3 | 168 | return 1; |
switches | 0:5c4d7b2438d3 | 169 | } |
switches | 0:5c4d7b2438d3 | 170 | |
switches | 0:5c4d7b2438d3 | 171 | /* Only need to check PCB if incoming IP version matches PCB IP version */ |
switches | 0:5c4d7b2438d3 | 172 | if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) { |
switches | 0:5c4d7b2438d3 | 173 | #if LWIP_IPV4 |
switches | 0:5c4d7b2438d3 | 174 | /* Special case: IPv4 broadcast: all or broadcasts in my subnet |
switches | 0:5c4d7b2438d3 | 175 | * Note: broadcast variable can only be 1 if it is an IPv4 broadcast */ |
switches | 0:5c4d7b2438d3 | 176 | if (broadcast != 0) { |
switches | 0:5c4d7b2438d3 | 177 | #if IP_SOF_BROADCAST_RECV |
switches | 0:5c4d7b2438d3 | 178 | if (ip_get_option(pcb, SOF_BROADCAST)) |
switches | 0:5c4d7b2438d3 | 179 | #endif /* IP_SOF_BROADCAST_RECV */ |
switches | 0:5c4d7b2438d3 | 180 | { |
switches | 0:5c4d7b2438d3 | 181 | if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) || |
switches | 0:5c4d7b2438d3 | 182 | ((ip4_current_dest_addr()->addr == IPADDR_BROADCAST)) || |
switches | 0:5c4d7b2438d3 | 183 | ip4_addr_netcmp(ip_2_ip4(&pcb->local_ip), ip4_current_dest_addr(), netif_ip4_netmask(inp))) { |
switches | 0:5c4d7b2438d3 | 184 | return 1; |
switches | 0:5c4d7b2438d3 | 185 | } |
switches | 0:5c4d7b2438d3 | 186 | } |
switches | 0:5c4d7b2438d3 | 187 | } else |
switches | 0:5c4d7b2438d3 | 188 | #endif /* LWIP_IPV4 */ |
switches | 0:5c4d7b2438d3 | 189 | /* Handle IPv4 and IPv6: all, multicast or exact match */ |
switches | 0:5c4d7b2438d3 | 190 | if (ip_addr_isany(&pcb->local_ip) || |
switches | 0:5c4d7b2438d3 | 191 | #if LWIP_IPV6_MLD |
switches | 0:5c4d7b2438d3 | 192 | (ip_current_is_v6() && ip6_addr_ismulticast(ip6_current_dest_addr())) || |
switches | 0:5c4d7b2438d3 | 193 | #endif /* LWIP_IPV6_MLD */ |
switches | 0:5c4d7b2438d3 | 194 | #if LWIP_IGMP |
switches | 0:5c4d7b2438d3 | 195 | (!ip_current_is_v6() && ip4_addr_ismulticast(ip4_current_dest_addr())) || |
switches | 0:5c4d7b2438d3 | 196 | #endif /* LWIP_IGMP */ |
switches | 0:5c4d7b2438d3 | 197 | ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) { |
switches | 0:5c4d7b2438d3 | 198 | return 1; |
switches | 0:5c4d7b2438d3 | 199 | } |
switches | 0:5c4d7b2438d3 | 200 | } |
switches | 0:5c4d7b2438d3 | 201 | |
switches | 0:5c4d7b2438d3 | 202 | return 0; |
switches | 0:5c4d7b2438d3 | 203 | } |
switches | 0:5c4d7b2438d3 | 204 | |
switches | 0:5c4d7b2438d3 | 205 | /** |
switches | 0:5c4d7b2438d3 | 206 | * Process an incoming UDP datagram. |
switches | 0:5c4d7b2438d3 | 207 | * |
switches | 0:5c4d7b2438d3 | 208 | * Given an incoming UDP datagram (as a chain of pbufs) this function |
switches | 0:5c4d7b2438d3 | 209 | * finds a corresponding UDP PCB and hands over the pbuf to the pcbs |
switches | 0:5c4d7b2438d3 | 210 | * recv function. If no pcb is found or the datagram is incorrect, the |
switches | 0:5c4d7b2438d3 | 211 | * pbuf is freed. |
switches | 0:5c4d7b2438d3 | 212 | * |
switches | 0:5c4d7b2438d3 | 213 | * @param p pbuf to be demultiplexed to a UDP PCB (p->payload pointing to the UDP header) |
switches | 0:5c4d7b2438d3 | 214 | * @param inp network interface on which the datagram was received. |
switches | 0:5c4d7b2438d3 | 215 | * |
switches | 0:5c4d7b2438d3 | 216 | */ |
switches | 0:5c4d7b2438d3 | 217 | void |
switches | 0:5c4d7b2438d3 | 218 | udp_input(struct pbuf *p, struct netif *inp) |
switches | 0:5c4d7b2438d3 | 219 | { |
switches | 0:5c4d7b2438d3 | 220 | struct udp_hdr *udphdr; |
switches | 0:5c4d7b2438d3 | 221 | struct udp_pcb *pcb, *prev; |
switches | 0:5c4d7b2438d3 | 222 | struct udp_pcb *uncon_pcb; |
switches | 0:5c4d7b2438d3 | 223 | u16_t src, dest; |
switches | 0:5c4d7b2438d3 | 224 | u8_t broadcast; |
switches | 0:5c4d7b2438d3 | 225 | u8_t for_us = 0; |
switches | 0:5c4d7b2438d3 | 226 | |
switches | 0:5c4d7b2438d3 | 227 | LWIP_UNUSED_ARG(inp); |
switches | 0:5c4d7b2438d3 | 228 | |
switches | 0:5c4d7b2438d3 | 229 | PERF_START; |
switches | 0:5c4d7b2438d3 | 230 | |
switches | 0:5c4d7b2438d3 | 231 | UDP_STATS_INC(udp.recv); |
switches | 0:5c4d7b2438d3 | 232 | |
switches | 0:5c4d7b2438d3 | 233 | /* Check minimum length (UDP header) */ |
switches | 0:5c4d7b2438d3 | 234 | if (p->len < UDP_HLEN) { |
switches | 0:5c4d7b2438d3 | 235 | /* drop short packets */ |
switches | 0:5c4d7b2438d3 | 236 | LWIP_DEBUGF(UDP_DEBUG, |
switches | 0:5c4d7b2438d3 | 237 | ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len)); |
switches | 0:5c4d7b2438d3 | 238 | UDP_STATS_INC(udp.lenerr); |
switches | 0:5c4d7b2438d3 | 239 | UDP_STATS_INC(udp.drop); |
switches | 0:5c4d7b2438d3 | 240 | MIB2_STATS_INC(mib2.udpinerrors); |
switches | 0:5c4d7b2438d3 | 241 | pbuf_free(p); |
switches | 0:5c4d7b2438d3 | 242 | goto end; |
switches | 0:5c4d7b2438d3 | 243 | } |
switches | 0:5c4d7b2438d3 | 244 | |
switches | 0:5c4d7b2438d3 | 245 | udphdr = (struct udp_hdr *)p->payload; |
switches | 0:5c4d7b2438d3 | 246 | |
switches | 0:5c4d7b2438d3 | 247 | /* is broadcast packet ? */ |
switches | 0:5c4d7b2438d3 | 248 | broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif()); |
switches | 0:5c4d7b2438d3 | 249 | |
switches | 0:5c4d7b2438d3 | 250 | LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len)); |
switches | 0:5c4d7b2438d3 | 251 | |
switches | 0:5c4d7b2438d3 | 252 | /* convert src and dest ports to host byte order */ |
switches | 0:5c4d7b2438d3 | 253 | src = ntohs(udphdr->src); |
switches | 0:5c4d7b2438d3 | 254 | dest = ntohs(udphdr->dest); |
switches | 0:5c4d7b2438d3 | 255 | |
switches | 0:5c4d7b2438d3 | 256 | udp_debug_print(udphdr); |
switches | 0:5c4d7b2438d3 | 257 | |
switches | 0:5c4d7b2438d3 | 258 | /* print the UDP source and destination */ |
switches | 0:5c4d7b2438d3 | 259 | LWIP_DEBUGF(UDP_DEBUG, ("udp (")); |
switches | 0:5c4d7b2438d3 | 260 | ip_addr_debug_print(UDP_DEBUG, ip_current_dest_addr()); |
switches | 0:5c4d7b2438d3 | 261 | LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", ntohs(udphdr->dest))); |
switches | 0:5c4d7b2438d3 | 262 | ip_addr_debug_print(UDP_DEBUG, ip_current_src_addr()); |
switches | 0:5c4d7b2438d3 | 263 | LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", ntohs(udphdr->src))); |
switches | 0:5c4d7b2438d3 | 264 | |
switches | 0:5c4d7b2438d3 | 265 | pcb = NULL; |
switches | 0:5c4d7b2438d3 | 266 | prev = NULL; |
switches | 0:5c4d7b2438d3 | 267 | uncon_pcb = NULL; |
switches | 0:5c4d7b2438d3 | 268 | /* Iterate through the UDP pcb list for a matching pcb. |
switches | 0:5c4d7b2438d3 | 269 | * 'Perfect match' pcbs (connected to the remote port & ip address) are |
switches | 0:5c4d7b2438d3 | 270 | * preferred. If no perfect match is found, the first unconnected pcb that |
switches | 0:5c4d7b2438d3 | 271 | * matches the local port and ip address gets the datagram. */ |
switches | 0:5c4d7b2438d3 | 272 | for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) { |
switches | 0:5c4d7b2438d3 | 273 | /* print the PCB local and remote address */ |
switches | 0:5c4d7b2438d3 | 274 | LWIP_DEBUGF(UDP_DEBUG, ("pcb (")); |
switches | 0:5c4d7b2438d3 | 275 | ip_addr_debug_print(UDP_DEBUG, &pcb->local_ip); |
switches | 0:5c4d7b2438d3 | 276 | LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", pcb->local_port)); |
switches | 0:5c4d7b2438d3 | 277 | ip_addr_debug_print(UDP_DEBUG, &pcb->remote_ip); |
switches | 0:5c4d7b2438d3 | 278 | LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", pcb->remote_port)); |
switches | 0:5c4d7b2438d3 | 279 | |
switches | 0:5c4d7b2438d3 | 280 | /* compare PCB local addr+port to UDP destination addr+port */ |
switches | 0:5c4d7b2438d3 | 281 | if ((pcb->local_port == dest) && |
switches | 0:5c4d7b2438d3 | 282 | (udp_input_local_match(pcb, inp, broadcast) != 0)) { |
switches | 0:5c4d7b2438d3 | 283 | if (((pcb->flags & UDP_FLAGS_CONNECTED) == 0) && |
switches | 0:5c4d7b2438d3 | 284 | ((uncon_pcb == NULL) |
switches | 0:5c4d7b2438d3 | 285 | #if SO_REUSE |
switches | 0:5c4d7b2438d3 | 286 | /* prefer specific IPs over cath-all */ |
switches | 0:5c4d7b2438d3 | 287 | || !ip_addr_isany(&pcb->local_ip) |
switches | 0:5c4d7b2438d3 | 288 | #endif /* SO_REUSE */ |
switches | 0:5c4d7b2438d3 | 289 | )) { |
switches | 0:5c4d7b2438d3 | 290 | /* the first unconnected matching PCB */ |
switches | 0:5c4d7b2438d3 | 291 | uncon_pcb = pcb; |
switches | 0:5c4d7b2438d3 | 292 | } |
switches | 0:5c4d7b2438d3 | 293 | |
switches | 0:5c4d7b2438d3 | 294 | /* compare PCB remote addr+port to UDP source addr+port */ |
switches | 0:5c4d7b2438d3 | 295 | if ((pcb->remote_port == src) && |
switches | 0:5c4d7b2438d3 | 296 | (ip_addr_isany_val(pcb->remote_ip) || |
switches | 0:5c4d7b2438d3 | 297 | ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()))) { |
switches | 0:5c4d7b2438d3 | 298 | /* the first fully matching PCB */ |
switches | 0:5c4d7b2438d3 | 299 | if (prev != NULL) { |
switches | 0:5c4d7b2438d3 | 300 | /* move the pcb to the front of udp_pcbs so that is |
switches | 0:5c4d7b2438d3 | 301 | found faster next time */ |
switches | 0:5c4d7b2438d3 | 302 | prev->next = pcb->next; |
switches | 0:5c4d7b2438d3 | 303 | pcb->next = udp_pcbs; |
switches | 0:5c4d7b2438d3 | 304 | udp_pcbs = pcb; |
switches | 0:5c4d7b2438d3 | 305 | } else { |
switches | 0:5c4d7b2438d3 | 306 | UDP_STATS_INC(udp.cachehit); |
switches | 0:5c4d7b2438d3 | 307 | } |
switches | 0:5c4d7b2438d3 | 308 | break; |
switches | 0:5c4d7b2438d3 | 309 | } |
switches | 0:5c4d7b2438d3 | 310 | } |
switches | 0:5c4d7b2438d3 | 311 | |
switches | 0:5c4d7b2438d3 | 312 | prev = pcb; |
switches | 0:5c4d7b2438d3 | 313 | } |
switches | 0:5c4d7b2438d3 | 314 | /* no fully matching pcb found? then look for an unconnected pcb */ |
switches | 0:5c4d7b2438d3 | 315 | if (pcb == NULL) { |
switches | 0:5c4d7b2438d3 | 316 | pcb = uncon_pcb; |
switches | 0:5c4d7b2438d3 | 317 | } |
switches | 0:5c4d7b2438d3 | 318 | |
switches | 0:5c4d7b2438d3 | 319 | /* Check checksum if this is a match or if it was directed at us. */ |
switches | 0:5c4d7b2438d3 | 320 | if (pcb != NULL) { |
switches | 0:5c4d7b2438d3 | 321 | for_us = 1; |
switches | 0:5c4d7b2438d3 | 322 | } else { |
switches | 0:5c4d7b2438d3 | 323 | #if LWIP_IPV6 |
switches | 0:5c4d7b2438d3 | 324 | if (ip_current_is_v6()) { |
switches | 0:5c4d7b2438d3 | 325 | for_us = netif_get_ip6_addr_match(inp, ip6_current_dest_addr()) >= 0; |
switches | 0:5c4d7b2438d3 | 326 | } |
switches | 0:5c4d7b2438d3 | 327 | #endif /* LWIP_IPV6 */ |
switches | 0:5c4d7b2438d3 | 328 | #if LWIP_IPV4 |
switches | 0:5c4d7b2438d3 | 329 | if (!ip_current_is_v6()) { |
switches | 0:5c4d7b2438d3 | 330 | for_us = ip4_addr_cmp(netif_ip4_addr(inp), ip4_current_dest_addr()); |
switches | 0:5c4d7b2438d3 | 331 | } |
switches | 0:5c4d7b2438d3 | 332 | #endif /* LWIP_IPV4 */ |
switches | 0:5c4d7b2438d3 | 333 | } |
switches | 0:5c4d7b2438d3 | 334 | |
switches | 0:5c4d7b2438d3 | 335 | if (for_us) { |
switches | 0:5c4d7b2438d3 | 336 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n")); |
switches | 0:5c4d7b2438d3 | 337 | #if CHECKSUM_CHECK_UDP |
switches | 0:5c4d7b2438d3 | 338 | IF__NETIF_CHECKSUM_ENABLED(inp, CHECKSUM_CHECK_UDP) { |
switches | 0:5c4d7b2438d3 | 339 | #if LWIP_UDPLITE |
switches | 0:5c4d7b2438d3 | 340 | if (ip_current_header_proto() == IP_PROTO_UDPLITE) { |
switches | 0:5c4d7b2438d3 | 341 | /* Do the UDP Lite checksum */ |
switches | 0:5c4d7b2438d3 | 342 | u16_t chklen = ntohs(udphdr->len); |
switches | 0:5c4d7b2438d3 | 343 | if (chklen < sizeof(struct udp_hdr)) { |
switches | 0:5c4d7b2438d3 | 344 | if (chklen == 0) { |
switches | 0:5c4d7b2438d3 | 345 | /* For UDP-Lite, checksum length of 0 means checksum |
switches | 0:5c4d7b2438d3 | 346 | over the complete packet (See RFC 3828 chap. 3.1) */ |
switches | 0:5c4d7b2438d3 | 347 | chklen = p->tot_len; |
switches | 0:5c4d7b2438d3 | 348 | } else { |
switches | 0:5c4d7b2438d3 | 349 | /* At least the UDP-Lite header must be covered by the |
switches | 0:5c4d7b2438d3 | 350 | checksum! (Again, see RFC 3828 chap. 3.1) */ |
switches | 0:5c4d7b2438d3 | 351 | goto chkerr; |
switches | 0:5c4d7b2438d3 | 352 | } |
switches | 0:5c4d7b2438d3 | 353 | } |
switches | 0:5c4d7b2438d3 | 354 | if (ip_chksum_pseudo_partial(p, IP_PROTO_UDPLITE, |
switches | 0:5c4d7b2438d3 | 355 | p->tot_len, chklen, |
switches | 0:5c4d7b2438d3 | 356 | ip_current_src_addr(), ip_current_dest_addr()) != 0) { |
switches | 0:5c4d7b2438d3 | 357 | goto chkerr; |
switches | 0:5c4d7b2438d3 | 358 | } |
switches | 0:5c4d7b2438d3 | 359 | } else |
switches | 0:5c4d7b2438d3 | 360 | #endif /* LWIP_UDPLITE */ |
switches | 0:5c4d7b2438d3 | 361 | { |
switches | 0:5c4d7b2438d3 | 362 | if (udphdr->chksum != 0) { |
switches | 0:5c4d7b2438d3 | 363 | if (ip_chksum_pseudo(p, IP_PROTO_UDP, p->tot_len, |
switches | 0:5c4d7b2438d3 | 364 | ip_current_src_addr(), |
switches | 0:5c4d7b2438d3 | 365 | ip_current_dest_addr()) != 0) { |
switches | 0:5c4d7b2438d3 | 366 | goto chkerr; |
switches | 0:5c4d7b2438d3 | 367 | } |
switches | 0:5c4d7b2438d3 | 368 | } |
switches | 0:5c4d7b2438d3 | 369 | } |
switches | 0:5c4d7b2438d3 | 370 | } |
switches | 0:5c4d7b2438d3 | 371 | #endif /* CHECKSUM_CHECK_UDP */ |
switches | 0:5c4d7b2438d3 | 372 | if (pbuf_header(p, -UDP_HLEN)) { |
switches | 0:5c4d7b2438d3 | 373 | /* Can we cope with this failing? Just assert for now */ |
switches | 0:5c4d7b2438d3 | 374 | LWIP_ASSERT("pbuf_header failed\n", 0); |
switches | 0:5c4d7b2438d3 | 375 | UDP_STATS_INC(udp.drop); |
switches | 0:5c4d7b2438d3 | 376 | MIB2_STATS_INC(mib2.udpinerrors); |
switches | 0:5c4d7b2438d3 | 377 | pbuf_free(p); |
switches | 0:5c4d7b2438d3 | 378 | goto end; |
switches | 0:5c4d7b2438d3 | 379 | } |
switches | 0:5c4d7b2438d3 | 380 | |
switches | 0:5c4d7b2438d3 | 381 | if (pcb != NULL) { |
switches | 0:5c4d7b2438d3 | 382 | MIB2_STATS_INC(mib2.udpindatagrams); |
switches | 0:5c4d7b2438d3 | 383 | #if SO_REUSE && SO_REUSE_RXTOALL |
switches | 0:5c4d7b2438d3 | 384 | if (ip_get_option(pcb, SOF_REUSEADDR) && |
switches | 0:5c4d7b2438d3 | 385 | (broadcast || ip_addr_ismulticast(ip_current_dest_addr()))) { |
switches | 0:5c4d7b2438d3 | 386 | /* pass broadcast- or multicast packets to all multicast pcbs |
switches | 0:5c4d7b2438d3 | 387 | if SOF_REUSEADDR is set on the first match */ |
switches | 0:5c4d7b2438d3 | 388 | struct udp_pcb *mpcb; |
switches | 0:5c4d7b2438d3 | 389 | u8_t p_header_changed = 0; |
switches | 0:5c4d7b2438d3 | 390 | s16_t hdrs_len = (s16_t)(ip_current_header_tot_len() + UDP_HLEN); |
switches | 0:5c4d7b2438d3 | 391 | for (mpcb = udp_pcbs; mpcb != NULL; mpcb = mpcb->next) { |
switches | 0:5c4d7b2438d3 | 392 | if (mpcb != pcb) { |
switches | 0:5c4d7b2438d3 | 393 | /* compare PCB local addr+port to UDP destination addr+port */ |
switches | 0:5c4d7b2438d3 | 394 | if ((mpcb->local_port == dest) && |
switches | 0:5c4d7b2438d3 | 395 | (udp_input_local_match(mpcb, inp, broadcast) != 0)) { |
switches | 0:5c4d7b2438d3 | 396 | /* pass a copy of the packet to all local matches */ |
switches | 0:5c4d7b2438d3 | 397 | if (mpcb->recv != NULL) { |
switches | 0:5c4d7b2438d3 | 398 | struct pbuf *q; |
switches | 0:5c4d7b2438d3 | 399 | /* for that, move payload to IP header again */ |
switches | 0:5c4d7b2438d3 | 400 | if (p_header_changed == 0) { |
switches | 0:5c4d7b2438d3 | 401 | pbuf_header_force(p, hdrs_len); |
switches | 0:5c4d7b2438d3 | 402 | p_header_changed = 1; |
switches | 0:5c4d7b2438d3 | 403 | } |
switches | 0:5c4d7b2438d3 | 404 | q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM); |
switches | 0:5c4d7b2438d3 | 405 | if (q != NULL) { |
switches | 0:5c4d7b2438d3 | 406 | err_t err = pbuf_copy(q, p); |
switches | 0:5c4d7b2438d3 | 407 | if (err == ERR_OK) { |
switches | 0:5c4d7b2438d3 | 408 | /* move payload to UDP data */ |
switches | 0:5c4d7b2438d3 | 409 | pbuf_header(q, -hdrs_len); |
switches | 0:5c4d7b2438d3 | 410 | mpcb->recv(mpcb->recv_arg, mpcb, q, ip_current_src_addr(), src); |
switches | 0:5c4d7b2438d3 | 411 | } |
switches | 0:5c4d7b2438d3 | 412 | } |
switches | 0:5c4d7b2438d3 | 413 | } |
switches | 0:5c4d7b2438d3 | 414 | } |
switches | 0:5c4d7b2438d3 | 415 | } |
switches | 0:5c4d7b2438d3 | 416 | } |
switches | 0:5c4d7b2438d3 | 417 | if (p_header_changed) { |
switches | 0:5c4d7b2438d3 | 418 | /* and move payload to UDP data again */ |
switches | 0:5c4d7b2438d3 | 419 | pbuf_header(p, -hdrs_len); |
switches | 0:5c4d7b2438d3 | 420 | } |
switches | 0:5c4d7b2438d3 | 421 | } |
switches | 0:5c4d7b2438d3 | 422 | #endif /* SO_REUSE && SO_REUSE_RXTOALL */ |
switches | 0:5c4d7b2438d3 | 423 | /* callback */ |
switches | 0:5c4d7b2438d3 | 424 | if (pcb->recv != NULL) { |
switches | 0:5c4d7b2438d3 | 425 | /* now the recv function is responsible for freeing p */ |
switches | 0:5c4d7b2438d3 | 426 | pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr(), src); |
switches | 0:5c4d7b2438d3 | 427 | } else { |
switches | 0:5c4d7b2438d3 | 428 | /* no recv function registered? then we have to free the pbuf! */ |
switches | 0:5c4d7b2438d3 | 429 | pbuf_free(p); |
switches | 0:5c4d7b2438d3 | 430 | goto end; |
switches | 0:5c4d7b2438d3 | 431 | } |
switches | 0:5c4d7b2438d3 | 432 | } else { |
switches | 0:5c4d7b2438d3 | 433 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n")); |
switches | 0:5c4d7b2438d3 | 434 | |
switches | 0:5c4d7b2438d3 | 435 | #if LWIP_ICMP || LWIP_ICMP6 |
switches | 0:5c4d7b2438d3 | 436 | /* No match was found, send ICMP destination port unreachable unless |
switches | 0:5c4d7b2438d3 | 437 | destination address was broadcast/multicast. */ |
switches | 0:5c4d7b2438d3 | 438 | if (!broadcast && !ip_addr_ismulticast(ip_current_dest_addr())) { |
switches | 0:5c4d7b2438d3 | 439 | /* move payload pointer back to ip header */ |
switches | 0:5c4d7b2438d3 | 440 | pbuf_header_force(p, ip_current_header_tot_len() + UDP_HLEN); |
switches | 0:5c4d7b2438d3 | 441 | icmp_port_unreach(ip_current_is_v6(), p); |
switches | 0:5c4d7b2438d3 | 442 | } |
switches | 0:5c4d7b2438d3 | 443 | #endif /* LWIP_ICMP || LWIP_ICMP6 */ |
switches | 0:5c4d7b2438d3 | 444 | UDP_STATS_INC(udp.proterr); |
switches | 0:5c4d7b2438d3 | 445 | UDP_STATS_INC(udp.drop); |
switches | 0:5c4d7b2438d3 | 446 | MIB2_STATS_INC(mib2.udpnoports); |
switches | 0:5c4d7b2438d3 | 447 | pbuf_free(p); |
switches | 0:5c4d7b2438d3 | 448 | } |
switches | 0:5c4d7b2438d3 | 449 | } else { |
switches | 0:5c4d7b2438d3 | 450 | pbuf_free(p); |
switches | 0:5c4d7b2438d3 | 451 | } |
switches | 0:5c4d7b2438d3 | 452 | end: |
switches | 0:5c4d7b2438d3 | 453 | PERF_STOP("udp_input"); |
switches | 0:5c4d7b2438d3 | 454 | return; |
switches | 0:5c4d7b2438d3 | 455 | #if CHECKSUM_CHECK_UDP |
switches | 0:5c4d7b2438d3 | 456 | chkerr: |
switches | 0:5c4d7b2438d3 | 457 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
switches | 0:5c4d7b2438d3 | 458 | ("udp_input: UDP (or UDP Lite) datagram discarded due to failing checksum\n")); |
switches | 0:5c4d7b2438d3 | 459 | UDP_STATS_INC(udp.chkerr); |
switches | 0:5c4d7b2438d3 | 460 | UDP_STATS_INC(udp.drop); |
switches | 0:5c4d7b2438d3 | 461 | MIB2_STATS_INC(mib2.udpinerrors); |
switches | 0:5c4d7b2438d3 | 462 | pbuf_free(p); |
switches | 0:5c4d7b2438d3 | 463 | PERF_STOP("udp_input"); |
switches | 0:5c4d7b2438d3 | 464 | #endif /* CHECKSUM_CHECK_UDP */ |
switches | 0:5c4d7b2438d3 | 465 | } |
switches | 0:5c4d7b2438d3 | 466 | |
switches | 0:5c4d7b2438d3 | 467 | /** |
switches | 0:5c4d7b2438d3 | 468 | * @ingroup udp_raw |
switches | 0:5c4d7b2438d3 | 469 | * Send data using UDP. |
switches | 0:5c4d7b2438d3 | 470 | * |
switches | 0:5c4d7b2438d3 | 471 | * @param pcb UDP PCB used to send the data. |
switches | 0:5c4d7b2438d3 | 472 | * @param p chain of pbuf's to be sent. |
switches | 0:5c4d7b2438d3 | 473 | * |
switches | 0:5c4d7b2438d3 | 474 | * The datagram will be sent to the current remote_ip & remote_port |
switches | 0:5c4d7b2438d3 | 475 | * stored in pcb. If the pcb is not bound to a port, it will |
switches | 0:5c4d7b2438d3 | 476 | * automatically be bound to a random port. |
switches | 0:5c4d7b2438d3 | 477 | * |
switches | 0:5c4d7b2438d3 | 478 | * @return lwIP error code. |
switches | 0:5c4d7b2438d3 | 479 | * - ERR_OK. Successful. No error occurred. |
switches | 0:5c4d7b2438d3 | 480 | * - ERR_MEM. Out of memory. |
switches | 0:5c4d7b2438d3 | 481 | * - ERR_RTE. Could not find route to destination address. |
switches | 0:5c4d7b2438d3 | 482 | * - ERR_VAL. No PCB or PCB is dual-stack |
switches | 0:5c4d7b2438d3 | 483 | * - More errors could be returned by lower protocol layers. |
switches | 0:5c4d7b2438d3 | 484 | * |
switches | 0:5c4d7b2438d3 | 485 | * @see udp_disconnect() udp_sendto() |
switches | 0:5c4d7b2438d3 | 486 | */ |
switches | 0:5c4d7b2438d3 | 487 | err_t |
switches | 0:5c4d7b2438d3 | 488 | udp_send(struct udp_pcb *pcb, struct pbuf *p) |
switches | 0:5c4d7b2438d3 | 489 | { |
switches | 0:5c4d7b2438d3 | 490 | if ((pcb == NULL) || IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) { |
switches | 0:5c4d7b2438d3 | 491 | return ERR_VAL; |
switches | 0:5c4d7b2438d3 | 492 | } |
switches | 0:5c4d7b2438d3 | 493 | |
switches | 0:5c4d7b2438d3 | 494 | /* send to the packet using remote ip and port stored in the pcb */ |
switches | 0:5c4d7b2438d3 | 495 | return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port); |
switches | 0:5c4d7b2438d3 | 496 | } |
switches | 0:5c4d7b2438d3 | 497 | |
switches | 0:5c4d7b2438d3 | 498 | #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP |
switches | 0:5c4d7b2438d3 | 499 | /** @ingroup udp_raw |
switches | 0:5c4d7b2438d3 | 500 | * Same as udp_send() but with checksum |
switches | 0:5c4d7b2438d3 | 501 | */ |
switches | 0:5c4d7b2438d3 | 502 | err_t |
switches | 0:5c4d7b2438d3 | 503 | udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p, |
switches | 0:5c4d7b2438d3 | 504 | u8_t have_chksum, u16_t chksum) |
switches | 0:5c4d7b2438d3 | 505 | { |
switches | 0:5c4d7b2438d3 | 506 | if ((pcb == NULL) || IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) { |
switches | 0:5c4d7b2438d3 | 507 | return ERR_VAL; |
switches | 0:5c4d7b2438d3 | 508 | } |
switches | 0:5c4d7b2438d3 | 509 | |
switches | 0:5c4d7b2438d3 | 510 | /* send to the packet using remote ip and port stored in the pcb */ |
switches | 0:5c4d7b2438d3 | 511 | return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port, |
switches | 0:5c4d7b2438d3 | 512 | have_chksum, chksum); |
switches | 0:5c4d7b2438d3 | 513 | } |
switches | 0:5c4d7b2438d3 | 514 | #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ |
switches | 0:5c4d7b2438d3 | 515 | |
switches | 0:5c4d7b2438d3 | 516 | /** |
switches | 0:5c4d7b2438d3 | 517 | * @ingroup udp_raw |
switches | 0:5c4d7b2438d3 | 518 | * Send data to a specified address using UDP. |
switches | 0:5c4d7b2438d3 | 519 | * |
switches | 0:5c4d7b2438d3 | 520 | * @param pcb UDP PCB used to send the data. |
switches | 0:5c4d7b2438d3 | 521 | * @param p chain of pbuf's to be sent. |
switches | 0:5c4d7b2438d3 | 522 | * @param dst_ip Destination IP address. |
switches | 0:5c4d7b2438d3 | 523 | * @param dst_port Destination UDP port. |
switches | 0:5c4d7b2438d3 | 524 | * |
switches | 0:5c4d7b2438d3 | 525 | * dst_ip & dst_port are expected to be in the same byte order as in the pcb. |
switches | 0:5c4d7b2438d3 | 526 | * |
switches | 0:5c4d7b2438d3 | 527 | * If the PCB already has a remote address association, it will |
switches | 0:5c4d7b2438d3 | 528 | * be restored after the data is sent. |
switches | 0:5c4d7b2438d3 | 529 | * |
switches | 0:5c4d7b2438d3 | 530 | * @return lwIP error code (@see udp_send for possible error codes) |
switches | 0:5c4d7b2438d3 | 531 | * |
switches | 0:5c4d7b2438d3 | 532 | * @see udp_disconnect() udp_send() |
switches | 0:5c4d7b2438d3 | 533 | */ |
switches | 0:5c4d7b2438d3 | 534 | err_t |
switches | 0:5c4d7b2438d3 | 535 | udp_sendto(struct udp_pcb *pcb, struct pbuf *p, |
switches | 0:5c4d7b2438d3 | 536 | const ip_addr_t *dst_ip, u16_t dst_port) |
switches | 0:5c4d7b2438d3 | 537 | { |
switches | 0:5c4d7b2438d3 | 538 | #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP |
switches | 0:5c4d7b2438d3 | 539 | return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0); |
switches | 0:5c4d7b2438d3 | 540 | } |
switches | 0:5c4d7b2438d3 | 541 | |
switches | 0:5c4d7b2438d3 | 542 | /** @ingroup udp_raw |
switches | 0:5c4d7b2438d3 | 543 | * Same as udp_sendto(), but with checksum */ |
switches | 0:5c4d7b2438d3 | 544 | err_t |
switches | 0:5c4d7b2438d3 | 545 | udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, |
switches | 0:5c4d7b2438d3 | 546 | u16_t dst_port, u8_t have_chksum, u16_t chksum) |
switches | 0:5c4d7b2438d3 | 547 | { |
switches | 0:5c4d7b2438d3 | 548 | #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ |
switches | 0:5c4d7b2438d3 | 549 | struct netif *netif; |
switches | 0:5c4d7b2438d3 | 550 | const ip_addr_t *dst_ip_route = dst_ip; |
switches | 0:5c4d7b2438d3 | 551 | |
switches | 0:5c4d7b2438d3 | 552 | if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) { |
switches | 0:5c4d7b2438d3 | 553 | return ERR_VAL; |
switches | 0:5c4d7b2438d3 | 554 | } |
switches | 0:5c4d7b2438d3 | 555 | |
switches | 0:5c4d7b2438d3 | 556 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send\n")); |
switches | 0:5c4d7b2438d3 | 557 | |
switches | 0:5c4d7b2438d3 | 558 | #if LWIP_IPV6 || (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) |
switches | 0:5c4d7b2438d3 | 559 | if (ip_addr_ismulticast(dst_ip_route)) { |
switches | 0:5c4d7b2438d3 | 560 | #if LWIP_IPV6 |
switches | 0:5c4d7b2438d3 | 561 | if (IP_IS_V6(dst_ip)) { |
switches | 0:5c4d7b2438d3 | 562 | /* For multicast, find a netif based on source address. */ |
switches | 0:5c4d7b2438d3 | 563 | dst_ip_route = &pcb->local_ip; |
switches | 0:5c4d7b2438d3 | 564 | } else |
switches | 0:5c4d7b2438d3 | 565 | #endif /* LWIP_IPV6 */ |
switches | 0:5c4d7b2438d3 | 566 | { |
switches | 0:5c4d7b2438d3 | 567 | #if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS |
switches | 0:5c4d7b2438d3 | 568 | /* IPv4 does not use source-based routing by default, so we use an |
switches | 0:5c4d7b2438d3 | 569 | administratively selected interface for multicast by default. |
switches | 0:5c4d7b2438d3 | 570 | However, this can be overridden by setting an interface address |
switches | 0:5c4d7b2438d3 | 571 | in pcb->multicast_ip that is used for routing. */ |
switches | 0:5c4d7b2438d3 | 572 | if (!ip_addr_isany_val(pcb->multicast_ip) && |
switches | 0:5c4d7b2438d3 | 573 | !ip4_addr_cmp(ip_2_ip4(&pcb->multicast_ip), IP4_ADDR_BROADCAST)) { |
switches | 0:5c4d7b2438d3 | 574 | dst_ip_route = &pcb->multicast_ip; |
switches | 0:5c4d7b2438d3 | 575 | } |
switches | 0:5c4d7b2438d3 | 576 | #endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS */ |
switches | 0:5c4d7b2438d3 | 577 | } |
switches | 0:5c4d7b2438d3 | 578 | } |
switches | 0:5c4d7b2438d3 | 579 | #endif /* LWIP_IPV6 || (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) */ |
switches | 0:5c4d7b2438d3 | 580 | |
switches | 0:5c4d7b2438d3 | 581 | /* find the outgoing network interface for this packet */ |
switches | 0:5c4d7b2438d3 | 582 | netif = ip_route(&pcb->local_ip, dst_ip_route); |
switches | 0:5c4d7b2438d3 | 583 | |
switches | 0:5c4d7b2438d3 | 584 | /* no outgoing network interface could be found? */ |
switches | 0:5c4d7b2438d3 | 585 | if (netif == NULL) { |
switches | 0:5c4d7b2438d3 | 586 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: No route to ")); |
switches | 0:5c4d7b2438d3 | 587 | ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, dst_ip); |
switches | 0:5c4d7b2438d3 | 588 | LWIP_DEBUGF(UDP_DEBUG, ("\n")); |
switches | 0:5c4d7b2438d3 | 589 | UDP_STATS_INC(udp.rterr); |
switches | 0:5c4d7b2438d3 | 590 | return ERR_RTE; |
switches | 0:5c4d7b2438d3 | 591 | } |
switches | 0:5c4d7b2438d3 | 592 | #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP |
switches | 0:5c4d7b2438d3 | 593 | return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum); |
switches | 0:5c4d7b2438d3 | 594 | #else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ |
switches | 0:5c4d7b2438d3 | 595 | return udp_sendto_if(pcb, p, dst_ip, dst_port, netif); |
switches | 0:5c4d7b2438d3 | 596 | #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ |
switches | 0:5c4d7b2438d3 | 597 | } |
switches | 0:5c4d7b2438d3 | 598 | |
switches | 0:5c4d7b2438d3 | 599 | /** |
switches | 0:5c4d7b2438d3 | 600 | * @ingroup udp_raw |
switches | 0:5c4d7b2438d3 | 601 | * Send data to a specified address using UDP. |
switches | 0:5c4d7b2438d3 | 602 | * The netif used for sending can be specified. |
switches | 0:5c4d7b2438d3 | 603 | * |
switches | 0:5c4d7b2438d3 | 604 | * This function exists mainly for DHCP, to be able to send UDP packets |
switches | 0:5c4d7b2438d3 | 605 | * on a netif that is still down. |
switches | 0:5c4d7b2438d3 | 606 | * |
switches | 0:5c4d7b2438d3 | 607 | * @param pcb UDP PCB used to send the data. |
switches | 0:5c4d7b2438d3 | 608 | * @param p chain of pbuf's to be sent. |
switches | 0:5c4d7b2438d3 | 609 | * @param dst_ip Destination IP address. |
switches | 0:5c4d7b2438d3 | 610 | * @param dst_port Destination UDP port. |
switches | 0:5c4d7b2438d3 | 611 | * @param netif the netif used for sending. |
switches | 0:5c4d7b2438d3 | 612 | * |
switches | 0:5c4d7b2438d3 | 613 | * dst_ip & dst_port are expected to be in the same byte order as in the pcb. |
switches | 0:5c4d7b2438d3 | 614 | * |
switches | 0:5c4d7b2438d3 | 615 | * @return lwIP error code (@see udp_send for possible error codes) |
switches | 0:5c4d7b2438d3 | 616 | * |
switches | 0:5c4d7b2438d3 | 617 | * @see udp_disconnect() udp_send() |
switches | 0:5c4d7b2438d3 | 618 | */ |
switches | 0:5c4d7b2438d3 | 619 | err_t |
switches | 0:5c4d7b2438d3 | 620 | udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p, |
switches | 0:5c4d7b2438d3 | 621 | const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif) |
switches | 0:5c4d7b2438d3 | 622 | { |
switches | 0:5c4d7b2438d3 | 623 | #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP |
switches | 0:5c4d7b2438d3 | 624 | return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0); |
switches | 0:5c4d7b2438d3 | 625 | } |
switches | 0:5c4d7b2438d3 | 626 | |
switches | 0:5c4d7b2438d3 | 627 | /** Same as udp_sendto_if(), but with checksum */ |
switches | 0:5c4d7b2438d3 | 628 | err_t |
switches | 0:5c4d7b2438d3 | 629 | udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, |
switches | 0:5c4d7b2438d3 | 630 | u16_t dst_port, struct netif *netif, u8_t have_chksum, |
switches | 0:5c4d7b2438d3 | 631 | u16_t chksum) |
switches | 0:5c4d7b2438d3 | 632 | { |
switches | 0:5c4d7b2438d3 | 633 | #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ |
switches | 0:5c4d7b2438d3 | 634 | const ip_addr_t *src_ip; |
switches | 0:5c4d7b2438d3 | 635 | |
switches | 0:5c4d7b2438d3 | 636 | if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) { |
switches | 0:5c4d7b2438d3 | 637 | return ERR_VAL; |
switches | 0:5c4d7b2438d3 | 638 | } |
switches | 0:5c4d7b2438d3 | 639 | |
switches | 0:5c4d7b2438d3 | 640 | /* PCB local address is IP_ANY_ADDR? */ |
switches | 0:5c4d7b2438d3 | 641 | #if LWIP_IPV6 |
switches | 0:5c4d7b2438d3 | 642 | if (IP_IS_V6(dst_ip)) { |
switches | 0:5c4d7b2438d3 | 643 | if (ip6_addr_isany(ip_2_ip6(&pcb->local_ip))) { |
switches | 0:5c4d7b2438d3 | 644 | src_ip = ip6_select_source_address(netif, ip_2_ip6(dst_ip)); |
switches | 0:5c4d7b2438d3 | 645 | if (src_ip == NULL) { |
switches | 0:5c4d7b2438d3 | 646 | /* No suitable source address was found. */ |
switches | 0:5c4d7b2438d3 | 647 | return ERR_RTE; |
switches | 0:5c4d7b2438d3 | 648 | } |
switches | 0:5c4d7b2438d3 | 649 | } else { |
switches | 0:5c4d7b2438d3 | 650 | /* use UDP PCB local IPv6 address as source address, if still valid. */ |
switches | 0:5c4d7b2438d3 | 651 | if (netif_get_ip6_addr_match(netif, ip_2_ip6(&pcb->local_ip)) < 0) { |
switches | 0:5c4d7b2438d3 | 652 | /* Address isn't valid anymore. */ |
switches | 0:5c4d7b2438d3 | 653 | return ERR_RTE; |
switches | 0:5c4d7b2438d3 | 654 | } |
switches | 0:5c4d7b2438d3 | 655 | src_ip = &pcb->local_ip; |
switches | 0:5c4d7b2438d3 | 656 | } |
switches | 0:5c4d7b2438d3 | 657 | } |
switches | 0:5c4d7b2438d3 | 658 | #endif /* LWIP_IPV6 */ |
switches | 0:5c4d7b2438d3 | 659 | #if LWIP_IPV4 && LWIP_IPV6 |
switches | 0:5c4d7b2438d3 | 660 | else |
switches | 0:5c4d7b2438d3 | 661 | #endif /* LWIP_IPV4 && LWIP_IPV6 */ |
switches | 0:5c4d7b2438d3 | 662 | #if LWIP_IPV4 |
switches | 0:5c4d7b2438d3 | 663 | if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) || |
switches | 0:5c4d7b2438d3 | 664 | ip4_addr_ismulticast(ip_2_ip4(&pcb->local_ip))) { |
switches | 0:5c4d7b2438d3 | 665 | /* if the local_ip is any or multicast |
switches | 0:5c4d7b2438d3 | 666 | * use the outgoing network interface IP address as source address */ |
switches | 0:5c4d7b2438d3 | 667 | src_ip = netif_ip_addr4(netif); |
switches | 0:5c4d7b2438d3 | 668 | } else { |
switches | 0:5c4d7b2438d3 | 669 | /* check if UDP PCB local IP address is correct |
switches | 0:5c4d7b2438d3 | 670 | * this could be an old address if netif->ip_addr has changed */ |
switches | 0:5c4d7b2438d3 | 671 | if (!ip4_addr_cmp(ip_2_ip4(&(pcb->local_ip)), netif_ip4_addr(netif))) { |
switches | 0:5c4d7b2438d3 | 672 | /* local_ip doesn't match, drop the packet */ |
switches | 0:5c4d7b2438d3 | 673 | return ERR_VAL; |
switches | 0:5c4d7b2438d3 | 674 | } |
switches | 0:5c4d7b2438d3 | 675 | /* use UDP PCB local IP address as source address */ |
switches | 0:5c4d7b2438d3 | 676 | src_ip = &pcb->local_ip; |
switches | 0:5c4d7b2438d3 | 677 | } |
switches | 0:5c4d7b2438d3 | 678 | #endif /* LWIP_IPV4 */ |
switches | 0:5c4d7b2438d3 | 679 | #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP |
switches | 0:5c4d7b2438d3 | 680 | return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum, src_ip); |
switches | 0:5c4d7b2438d3 | 681 | #else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ |
switches | 0:5c4d7b2438d3 | 682 | return udp_sendto_if_src(pcb, p, dst_ip, dst_port, netif, src_ip); |
switches | 0:5c4d7b2438d3 | 683 | #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ |
switches | 0:5c4d7b2438d3 | 684 | } |
switches | 0:5c4d7b2438d3 | 685 | |
switches | 0:5c4d7b2438d3 | 686 | /** @ingroup udp_raw |
switches | 0:5c4d7b2438d3 | 687 | * Same as @ref udp_sendto_if, but with source address */ |
switches | 0:5c4d7b2438d3 | 688 | err_t |
switches | 0:5c4d7b2438d3 | 689 | udp_sendto_if_src(struct udp_pcb *pcb, struct pbuf *p, |
switches | 0:5c4d7b2438d3 | 690 | const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif, const ip_addr_t *src_ip) |
switches | 0:5c4d7b2438d3 | 691 | { |
switches | 0:5c4d7b2438d3 | 692 | #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP |
switches | 0:5c4d7b2438d3 | 693 | return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0, src_ip); |
switches | 0:5c4d7b2438d3 | 694 | } |
switches | 0:5c4d7b2438d3 | 695 | |
switches | 0:5c4d7b2438d3 | 696 | /** Same as udp_sendto_if_src(), but with checksum */ |
switches | 0:5c4d7b2438d3 | 697 | err_t |
switches | 0:5c4d7b2438d3 | 698 | udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, |
switches | 0:5c4d7b2438d3 | 699 | u16_t dst_port, struct netif *netif, u8_t have_chksum, |
switches | 0:5c4d7b2438d3 | 700 | u16_t chksum, const ip_addr_t *src_ip) |
switches | 0:5c4d7b2438d3 | 701 | { |
switches | 0:5c4d7b2438d3 | 702 | #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ |
switches | 0:5c4d7b2438d3 | 703 | struct udp_hdr *udphdr; |
switches | 0:5c4d7b2438d3 | 704 | err_t err; |
switches | 0:5c4d7b2438d3 | 705 | struct pbuf *q; /* q will be sent down the stack */ |
switches | 0:5c4d7b2438d3 | 706 | u8_t ip_proto; |
switches | 0:5c4d7b2438d3 | 707 | u8_t ttl; |
switches | 0:5c4d7b2438d3 | 708 | |
switches | 0:5c4d7b2438d3 | 709 | if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) || |
switches | 0:5c4d7b2438d3 | 710 | !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) { |
switches | 0:5c4d7b2438d3 | 711 | return ERR_VAL; |
switches | 0:5c4d7b2438d3 | 712 | } |
switches | 0:5c4d7b2438d3 | 713 | |
switches | 0:5c4d7b2438d3 | 714 | #if LWIP_IPV4 && IP_SOF_BROADCAST |
switches | 0:5c4d7b2438d3 | 715 | /* broadcast filter? */ |
switches | 0:5c4d7b2438d3 | 716 | if (!ip_get_option(pcb, SOF_BROADCAST) && |
switches | 0:5c4d7b2438d3 | 717 | #if LWIP_IPV6 |
switches | 0:5c4d7b2438d3 | 718 | IP_IS_V4(dst_ip) && |
switches | 0:5c4d7b2438d3 | 719 | #endif /* LWIP_IPV6 */ |
switches | 0:5c4d7b2438d3 | 720 | ip_addr_isbroadcast(dst_ip, netif)) { |
switches | 0:5c4d7b2438d3 | 721 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
switches | 0:5c4d7b2438d3 | 722 | ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb)); |
switches | 0:5c4d7b2438d3 | 723 | return ERR_VAL; |
switches | 0:5c4d7b2438d3 | 724 | } |
switches | 0:5c4d7b2438d3 | 725 | #endif /* LWIP_IPV4 && IP_SOF_BROADCAST */ |
switches | 0:5c4d7b2438d3 | 726 | |
switches | 0:5c4d7b2438d3 | 727 | /* if the PCB is not yet bound to a port, bind it here */ |
switches | 0:5c4d7b2438d3 | 728 | if (pcb->local_port == 0) { |
switches | 0:5c4d7b2438d3 | 729 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n")); |
switches | 0:5c4d7b2438d3 | 730 | err = udp_bind(pcb, &pcb->local_ip, pcb->local_port); |
switches | 0:5c4d7b2438d3 | 731 | if (err != ERR_OK) { |
switches | 0:5c4d7b2438d3 | 732 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n")); |
switches | 0:5c4d7b2438d3 | 733 | return err; |
switches | 0:5c4d7b2438d3 | 734 | } |
switches | 0:5c4d7b2438d3 | 735 | } |
switches | 0:5c4d7b2438d3 | 736 | |
switches | 0:5c4d7b2438d3 | 737 | /* not enough space to add an UDP header to first pbuf in given p chain? */ |
switches | 0:5c4d7b2438d3 | 738 | if (pbuf_header(p, UDP_HLEN)) { |
switches | 0:5c4d7b2438d3 | 739 | /* allocate header in a separate new pbuf */ |
switches | 0:5c4d7b2438d3 | 740 | q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM); |
switches | 0:5c4d7b2438d3 | 741 | /* new header pbuf could not be allocated? */ |
switches | 0:5c4d7b2438d3 | 742 | if (q == NULL) { |
switches | 0:5c4d7b2438d3 | 743 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n")); |
switches | 0:5c4d7b2438d3 | 744 | return ERR_MEM; |
switches | 0:5c4d7b2438d3 | 745 | } |
switches | 0:5c4d7b2438d3 | 746 | if (p->tot_len != 0) { |
switches | 0:5c4d7b2438d3 | 747 | /* chain header q in front of given pbuf p (only if p contains data) */ |
switches | 0:5c4d7b2438d3 | 748 | pbuf_chain(q, p); |
switches | 0:5c4d7b2438d3 | 749 | } |
switches | 0:5c4d7b2438d3 | 750 | /* first pbuf q points to header pbuf */ |
switches | 0:5c4d7b2438d3 | 751 | LWIP_DEBUGF(UDP_DEBUG, |
switches | 0:5c4d7b2438d3 | 752 | ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p)); |
switches | 0:5c4d7b2438d3 | 753 | } else { |
switches | 0:5c4d7b2438d3 | 754 | /* adding space for header within p succeeded */ |
switches | 0:5c4d7b2438d3 | 755 | /* first pbuf q equals given pbuf */ |
switches | 0:5c4d7b2438d3 | 756 | q = p; |
switches | 0:5c4d7b2438d3 | 757 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p)); |
switches | 0:5c4d7b2438d3 | 758 | } |
switches | 0:5c4d7b2438d3 | 759 | LWIP_ASSERT("check that first pbuf can hold struct udp_hdr", |
switches | 0:5c4d7b2438d3 | 760 | (q->len >= sizeof(struct udp_hdr))); |
switches | 0:5c4d7b2438d3 | 761 | /* q now represents the packet to be sent */ |
switches | 0:5c4d7b2438d3 | 762 | udphdr = (struct udp_hdr *)q->payload; |
switches | 0:5c4d7b2438d3 | 763 | udphdr->src = htons(pcb->local_port); |
switches | 0:5c4d7b2438d3 | 764 | udphdr->dest = htons(dst_port); |
switches | 0:5c4d7b2438d3 | 765 | /* in UDP, 0 checksum means 'no checksum' */ |
switches | 0:5c4d7b2438d3 | 766 | udphdr->chksum = 0x0000; |
switches | 0:5c4d7b2438d3 | 767 | |
switches | 0:5c4d7b2438d3 | 768 | /* Multicast Loop? */ |
switches | 0:5c4d7b2438d3 | 769 | #if (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) || (LWIP_IPV6 && LWIP_IPV6_MLD) |
switches | 0:5c4d7b2438d3 | 770 | if (((pcb->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) { |
switches | 0:5c4d7b2438d3 | 771 | q->flags |= PBUF_FLAG_MCASTLOOP; |
switches | 0:5c4d7b2438d3 | 772 | } |
switches | 0:5c4d7b2438d3 | 773 | #endif /* (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) || (LWIP_IPV6 && LWIP_IPV6_MLD) */ |
switches | 0:5c4d7b2438d3 | 774 | |
switches | 0:5c4d7b2438d3 | 775 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len)); |
switches | 0:5c4d7b2438d3 | 776 | |
switches | 0:5c4d7b2438d3 | 777 | #if LWIP_UDPLITE |
switches | 0:5c4d7b2438d3 | 778 | /* UDP Lite protocol? */ |
switches | 0:5c4d7b2438d3 | 779 | if (pcb->flags & UDP_FLAGS_UDPLITE) { |
switches | 0:5c4d7b2438d3 | 780 | u16_t chklen, chklen_hdr; |
switches | 0:5c4d7b2438d3 | 781 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len)); |
switches | 0:5c4d7b2438d3 | 782 | /* set UDP message length in UDP header */ |
switches | 0:5c4d7b2438d3 | 783 | chklen_hdr = chklen = pcb->chksum_len_tx; |
switches | 0:5c4d7b2438d3 | 784 | if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) { |
switches | 0:5c4d7b2438d3 | 785 | if (chklen != 0) { |
switches | 0:5c4d7b2438d3 | 786 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen)); |
switches | 0:5c4d7b2438d3 | 787 | } |
switches | 0:5c4d7b2438d3 | 788 | /* For UDP-Lite, checksum length of 0 means checksum |
switches | 0:5c4d7b2438d3 | 789 | over the complete packet. (See RFC 3828 chap. 3.1) |
switches | 0:5c4d7b2438d3 | 790 | At least the UDP-Lite header must be covered by the |
switches | 0:5c4d7b2438d3 | 791 | checksum, therefore, if chksum_len has an illegal |
switches | 0:5c4d7b2438d3 | 792 | value, we generate the checksum over the complete |
switches | 0:5c4d7b2438d3 | 793 | packet to be safe. */ |
switches | 0:5c4d7b2438d3 | 794 | chklen_hdr = 0; |
switches | 0:5c4d7b2438d3 | 795 | chklen = q->tot_len; |
switches | 0:5c4d7b2438d3 | 796 | } |
switches | 0:5c4d7b2438d3 | 797 | udphdr->len = htons(chklen_hdr); |
switches | 0:5c4d7b2438d3 | 798 | /* calculate checksum */ |
switches | 0:5c4d7b2438d3 | 799 | #if CHECKSUM_GEN_UDP |
switches | 0:5c4d7b2438d3 | 800 | IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) { |
switches | 0:5c4d7b2438d3 | 801 | #if LWIP_CHECKSUM_ON_COPY |
switches | 0:5c4d7b2438d3 | 802 | if (have_chksum) { |
switches | 0:5c4d7b2438d3 | 803 | chklen = UDP_HLEN; |
switches | 0:5c4d7b2438d3 | 804 | } |
switches | 0:5c4d7b2438d3 | 805 | #endif /* LWIP_CHECKSUM_ON_COPY */ |
switches | 0:5c4d7b2438d3 | 806 | udphdr->chksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDPLITE, |
switches | 0:5c4d7b2438d3 | 807 | q->tot_len, chklen, src_ip, dst_ip); |
switches | 0:5c4d7b2438d3 | 808 | #if LWIP_CHECKSUM_ON_COPY |
switches | 0:5c4d7b2438d3 | 809 | if (have_chksum) { |
switches | 0:5c4d7b2438d3 | 810 | u32_t acc; |
switches | 0:5c4d7b2438d3 | 811 | acc = udphdr->chksum + (u16_t)~(chksum); |
switches | 0:5c4d7b2438d3 | 812 | udphdr->chksum = FOLD_U32T(acc); |
switches | 0:5c4d7b2438d3 | 813 | } |
switches | 0:5c4d7b2438d3 | 814 | #endif /* LWIP_CHECKSUM_ON_COPY */ |
switches | 0:5c4d7b2438d3 | 815 | |
switches | 0:5c4d7b2438d3 | 816 | /* chksum zero must become 0xffff, as zero means 'no checksum' */ |
switches | 0:5c4d7b2438d3 | 817 | if (udphdr->chksum == 0x0000) { |
switches | 0:5c4d7b2438d3 | 818 | udphdr->chksum = 0xffff; |
switches | 0:5c4d7b2438d3 | 819 | } |
switches | 0:5c4d7b2438d3 | 820 | } |
switches | 0:5c4d7b2438d3 | 821 | #endif /* CHECKSUM_GEN_UDP */ |
switches | 0:5c4d7b2438d3 | 822 | |
switches | 0:5c4d7b2438d3 | 823 | ip_proto = IP_PROTO_UDPLITE; |
switches | 0:5c4d7b2438d3 | 824 | } else |
switches | 0:5c4d7b2438d3 | 825 | #endif /* LWIP_UDPLITE */ |
switches | 0:5c4d7b2438d3 | 826 | { /* UDP */ |
switches | 0:5c4d7b2438d3 | 827 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len)); |
switches | 0:5c4d7b2438d3 | 828 | udphdr->len = htons(q->tot_len); |
switches | 0:5c4d7b2438d3 | 829 | /* calculate checksum */ |
switches | 0:5c4d7b2438d3 | 830 | #if CHECKSUM_GEN_UDP |
switches | 0:5c4d7b2438d3 | 831 | IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) { |
switches | 0:5c4d7b2438d3 | 832 | /* Checksum is mandatory over IPv6. */ |
switches | 0:5c4d7b2438d3 | 833 | if (IP_IS_V6(dst_ip) || (pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) { |
switches | 0:5c4d7b2438d3 | 834 | u16_t udpchksum; |
switches | 0:5c4d7b2438d3 | 835 | #if LWIP_CHECKSUM_ON_COPY |
switches | 0:5c4d7b2438d3 | 836 | if (have_chksum) { |
switches | 0:5c4d7b2438d3 | 837 | u32_t acc; |
switches | 0:5c4d7b2438d3 | 838 | udpchksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDP, |
switches | 0:5c4d7b2438d3 | 839 | q->tot_len, UDP_HLEN, src_ip, dst_ip); |
switches | 0:5c4d7b2438d3 | 840 | acc = udpchksum + (u16_t)~(chksum); |
switches | 0:5c4d7b2438d3 | 841 | udpchksum = FOLD_U32T(acc); |
switches | 0:5c4d7b2438d3 | 842 | } else |
switches | 0:5c4d7b2438d3 | 843 | #endif /* LWIP_CHECKSUM_ON_COPY */ |
switches | 0:5c4d7b2438d3 | 844 | { |
switches | 0:5c4d7b2438d3 | 845 | udpchksum = ip_chksum_pseudo(q, IP_PROTO_UDP, q->tot_len, |
switches | 0:5c4d7b2438d3 | 846 | src_ip, dst_ip); |
switches | 0:5c4d7b2438d3 | 847 | } |
switches | 0:5c4d7b2438d3 | 848 | |
switches | 0:5c4d7b2438d3 | 849 | /* chksum zero must become 0xffff, as zero means 'no checksum' */ |
switches | 0:5c4d7b2438d3 | 850 | if (udpchksum == 0x0000) { |
switches | 0:5c4d7b2438d3 | 851 | udpchksum = 0xffff; |
switches | 0:5c4d7b2438d3 | 852 | } |
switches | 0:5c4d7b2438d3 | 853 | udphdr->chksum = udpchksum; |
switches | 0:5c4d7b2438d3 | 854 | } |
switches | 0:5c4d7b2438d3 | 855 | } |
switches | 0:5c4d7b2438d3 | 856 | #endif /* CHECKSUM_GEN_UDP */ |
switches | 0:5c4d7b2438d3 | 857 | ip_proto = IP_PROTO_UDP; |
switches | 0:5c4d7b2438d3 | 858 | } |
switches | 0:5c4d7b2438d3 | 859 | |
switches | 0:5c4d7b2438d3 | 860 | /* Determine TTL to use */ |
switches | 0:5c4d7b2438d3 | 861 | #if LWIP_MULTICAST_TX_OPTIONS |
switches | 0:5c4d7b2438d3 | 862 | ttl = (ip_addr_ismulticast(dst_ip) ? pcb->mcast_ttl : pcb->ttl); |
switches | 0:5c4d7b2438d3 | 863 | #else /* LWIP_MULTICAST_TX_OPTIONS */ |
switches | 0:5c4d7b2438d3 | 864 | ttl = pcb->ttl; |
switches | 0:5c4d7b2438d3 | 865 | #endif /* LWIP_MULTICAST_TX_OPTIONS */ |
switches | 0:5c4d7b2438d3 | 866 | |
switches | 0:5c4d7b2438d3 | 867 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum)); |
switches | 0:5c4d7b2438d3 | 868 | LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,0x%02"X16_F",)\n", (u16_t)ip_proto)); |
switches | 0:5c4d7b2438d3 | 869 | /* output to IP */ |
switches | 0:5c4d7b2438d3 | 870 | NETIF_SET_HWADDRHINT(netif, &(pcb->addr_hint)); |
switches | 0:5c4d7b2438d3 | 871 | err = ip_output_if_src(q, src_ip, dst_ip, ttl, pcb->tos, ip_proto, netif); |
switches | 0:5c4d7b2438d3 | 872 | NETIF_SET_HWADDRHINT(netif, NULL); |
switches | 0:5c4d7b2438d3 | 873 | |
switches | 0:5c4d7b2438d3 | 874 | /* @todo: must this be increased even if error occurred? */ |
switches | 0:5c4d7b2438d3 | 875 | MIB2_STATS_INC(mib2.udpoutdatagrams); |
switches | 0:5c4d7b2438d3 | 876 | |
switches | 0:5c4d7b2438d3 | 877 | /* did we chain a separate header pbuf earlier? */ |
switches | 0:5c4d7b2438d3 | 878 | if (q != p) { |
switches | 0:5c4d7b2438d3 | 879 | /* free the header pbuf */ |
switches | 0:5c4d7b2438d3 | 880 | pbuf_free(q); |
switches | 0:5c4d7b2438d3 | 881 | q = NULL; |
switches | 0:5c4d7b2438d3 | 882 | /* p is still referenced by the caller, and will live on */ |
switches | 0:5c4d7b2438d3 | 883 | } |
switches | 0:5c4d7b2438d3 | 884 | |
switches | 0:5c4d7b2438d3 | 885 | UDP_STATS_INC(udp.xmit); |
switches | 0:5c4d7b2438d3 | 886 | return err; |
switches | 0:5c4d7b2438d3 | 887 | } |
switches | 0:5c4d7b2438d3 | 888 | |
switches | 0:5c4d7b2438d3 | 889 | /** |
switches | 0:5c4d7b2438d3 | 890 | * @ingroup udp_raw |
switches | 0:5c4d7b2438d3 | 891 | * Bind an UDP PCB. |
switches | 0:5c4d7b2438d3 | 892 | * |
switches | 0:5c4d7b2438d3 | 893 | * @param pcb UDP PCB to be bound with a local address ipaddr and port. |
switches | 0:5c4d7b2438d3 | 894 | * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to |
switches | 0:5c4d7b2438d3 | 895 | * bind to all local interfaces. |
switches | 0:5c4d7b2438d3 | 896 | * @param port local UDP port to bind with. Use 0 to automatically bind |
switches | 0:5c4d7b2438d3 | 897 | * to a random port between UDP_LOCAL_PORT_RANGE_START and |
switches | 0:5c4d7b2438d3 | 898 | * UDP_LOCAL_PORT_RANGE_END. |
switches | 0:5c4d7b2438d3 | 899 | * |
switches | 0:5c4d7b2438d3 | 900 | * ipaddr & port are expected to be in the same byte order as in the pcb. |
switches | 0:5c4d7b2438d3 | 901 | * |
switches | 0:5c4d7b2438d3 | 902 | * @return lwIP error code. |
switches | 0:5c4d7b2438d3 | 903 | * - ERR_OK. Successful. No error occurred. |
switches | 0:5c4d7b2438d3 | 904 | * - ERR_USE. The specified ipaddr and port are already bound to by |
switches | 0:5c4d7b2438d3 | 905 | * another UDP PCB. |
switches | 0:5c4d7b2438d3 | 906 | * |
switches | 0:5c4d7b2438d3 | 907 | * @see udp_disconnect() |
switches | 0:5c4d7b2438d3 | 908 | */ |
switches | 0:5c4d7b2438d3 | 909 | err_t |
switches | 0:5c4d7b2438d3 | 910 | udp_bind(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port) |
switches | 0:5c4d7b2438d3 | 911 | { |
switches | 0:5c4d7b2438d3 | 912 | struct udp_pcb *ipcb; |
switches | 0:5c4d7b2438d3 | 913 | u8_t rebind; |
switches | 0:5c4d7b2438d3 | 914 | |
switches | 0:5c4d7b2438d3 | 915 | #if LWIP_IPV4 |
switches | 0:5c4d7b2438d3 | 916 | /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */ |
switches | 0:5c4d7b2438d3 | 917 | if (ipaddr == NULL) { |
switches | 0:5c4d7b2438d3 | 918 | ipaddr = IP_ADDR_ANY; |
switches | 0:5c4d7b2438d3 | 919 | } |
switches | 0:5c4d7b2438d3 | 920 | #endif /* LWIP_IPV4 */ |
switches | 0:5c4d7b2438d3 | 921 | |
switches | 0:5c4d7b2438d3 | 922 | /* still need to check for ipaddr == NULL in IPv6 only case */ |
switches | 0:5c4d7b2438d3 | 923 | if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ipaddr)) { |
switches | 0:5c4d7b2438d3 | 924 | return ERR_VAL; |
switches | 0:5c4d7b2438d3 | 925 | } |
switches | 0:5c4d7b2438d3 | 926 | |
switches | 0:5c4d7b2438d3 | 927 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = ")); |
switches | 0:5c4d7b2438d3 | 928 | ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE, ipaddr); |
switches | 0:5c4d7b2438d3 | 929 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port)); |
switches | 0:5c4d7b2438d3 | 930 | |
switches | 0:5c4d7b2438d3 | 931 | rebind = 0; |
switches | 0:5c4d7b2438d3 | 932 | /* Check for double bind and rebind of the same pcb */ |
switches | 0:5c4d7b2438d3 | 933 | for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) { |
switches | 0:5c4d7b2438d3 | 934 | /* is this UDP PCB already on active list? */ |
switches | 0:5c4d7b2438d3 | 935 | if (pcb == ipcb) { |
switches | 0:5c4d7b2438d3 | 936 | rebind = 1; |
switches | 0:5c4d7b2438d3 | 937 | break; |
switches | 0:5c4d7b2438d3 | 938 | } |
switches | 0:5c4d7b2438d3 | 939 | } |
switches | 0:5c4d7b2438d3 | 940 | |
switches | 0:5c4d7b2438d3 | 941 | /* no port specified? */ |
switches | 0:5c4d7b2438d3 | 942 | if (port == 0) { |
switches | 0:5c4d7b2438d3 | 943 | port = udp_new_port(); |
switches | 0:5c4d7b2438d3 | 944 | if (port == 0) { |
switches | 0:5c4d7b2438d3 | 945 | /* no more ports available in local range */ |
switches | 0:5c4d7b2438d3 | 946 | LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n")); |
switches | 0:5c4d7b2438d3 | 947 | return ERR_USE; |
switches | 0:5c4d7b2438d3 | 948 | } |
switches | 0:5c4d7b2438d3 | 949 | } else { |
switches | 0:5c4d7b2438d3 | 950 | for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) { |
switches | 0:5c4d7b2438d3 | 951 | if (pcb != ipcb) { |
switches | 0:5c4d7b2438d3 | 952 | /* By default, we don't allow to bind to a port that any other udp |
switches | 0:5c4d7b2438d3 | 953 | PCB is already bound to, unless *all* PCBs with that port have tha |
switches | 0:5c4d7b2438d3 | 954 | REUSEADDR flag set. */ |
switches | 0:5c4d7b2438d3 | 955 | #if SO_REUSE |
switches | 0:5c4d7b2438d3 | 956 | if (!ip_get_option(pcb, SOF_REUSEADDR) || |
switches | 0:5c4d7b2438d3 | 957 | !ip_get_option(ipcb, SOF_REUSEADDR)) |
switches | 0:5c4d7b2438d3 | 958 | #endif /* SO_REUSE */ |
switches | 0:5c4d7b2438d3 | 959 | { |
switches | 0:5c4d7b2438d3 | 960 | /* port matches that of PCB in list and REUSEADDR not set -> reject */ |
switches | 0:5c4d7b2438d3 | 961 | if ((ipcb->local_port == port) && |
switches | 0:5c4d7b2438d3 | 962 | /* IP address matches? */ |
switches | 0:5c4d7b2438d3 | 963 | ip_addr_cmp(&ipcb->local_ip, ipaddr)) { |
switches | 0:5c4d7b2438d3 | 964 | /* other PCB already binds to this local IP and port */ |
switches | 0:5c4d7b2438d3 | 965 | LWIP_DEBUGF(UDP_DEBUG, |
switches | 0:5c4d7b2438d3 | 966 | ("udp_bind: local port %"U16_F" already bound by another pcb\n", port)); |
switches | 0:5c4d7b2438d3 | 967 | return ERR_USE; |
switches | 0:5c4d7b2438d3 | 968 | } |
switches | 0:5c4d7b2438d3 | 969 | } |
switches | 0:5c4d7b2438d3 | 970 | } |
switches | 0:5c4d7b2438d3 | 971 | } |
switches | 0:5c4d7b2438d3 | 972 | } |
switches | 0:5c4d7b2438d3 | 973 | |
switches | 0:5c4d7b2438d3 | 974 | ip_addr_set_ipaddr(&pcb->local_ip, ipaddr); |
switches | 0:5c4d7b2438d3 | 975 | |
switches | 0:5c4d7b2438d3 | 976 | pcb->local_port = port; |
switches | 0:5c4d7b2438d3 | 977 | mib2_udp_bind(pcb); |
switches | 0:5c4d7b2438d3 | 978 | /* pcb not active yet? */ |
switches | 0:5c4d7b2438d3 | 979 | if (rebind == 0) { |
switches | 0:5c4d7b2438d3 | 980 | /* place the PCB on the active list if not already there */ |
switches | 0:5c4d7b2438d3 | 981 | pcb->next = udp_pcbs; |
switches | 0:5c4d7b2438d3 | 982 | udp_pcbs = pcb; |
switches | 0:5c4d7b2438d3 | 983 | } |
switches | 0:5c4d7b2438d3 | 984 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_bind: bound to ")); |
switches | 0:5c4d7b2438d3 | 985 | ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, &pcb->local_ip); |
switches | 0:5c4d7b2438d3 | 986 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->local_port)); |
switches | 0:5c4d7b2438d3 | 987 | return ERR_OK; |
switches | 0:5c4d7b2438d3 | 988 | } |
switches | 0:5c4d7b2438d3 | 989 | |
switches | 0:5c4d7b2438d3 | 990 | /** |
switches | 0:5c4d7b2438d3 | 991 | * @ingroup udp_raw |
switches | 0:5c4d7b2438d3 | 992 | * Connect an UDP PCB. |
switches | 0:5c4d7b2438d3 | 993 | * |
switches | 0:5c4d7b2438d3 | 994 | * This will associate the UDP PCB with the remote address. |
switches | 0:5c4d7b2438d3 | 995 | * |
switches | 0:5c4d7b2438d3 | 996 | * @param pcb UDP PCB to be connected with remote address ipaddr and port. |
switches | 0:5c4d7b2438d3 | 997 | * @param ipaddr remote IP address to connect with. |
switches | 0:5c4d7b2438d3 | 998 | * @param port remote UDP port to connect with. |
switches | 0:5c4d7b2438d3 | 999 | * |
switches | 0:5c4d7b2438d3 | 1000 | * @return lwIP error code |
switches | 0:5c4d7b2438d3 | 1001 | * |
switches | 0:5c4d7b2438d3 | 1002 | * ipaddr & port are expected to be in the same byte order as in the pcb. |
switches | 0:5c4d7b2438d3 | 1003 | * |
switches | 0:5c4d7b2438d3 | 1004 | * The udp pcb is bound to a random local port if not already bound. |
switches | 0:5c4d7b2438d3 | 1005 | * |
switches | 0:5c4d7b2438d3 | 1006 | * @see udp_disconnect() |
switches | 0:5c4d7b2438d3 | 1007 | */ |
switches | 0:5c4d7b2438d3 | 1008 | err_t |
switches | 0:5c4d7b2438d3 | 1009 | udp_connect(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port) |
switches | 0:5c4d7b2438d3 | 1010 | { |
switches | 0:5c4d7b2438d3 | 1011 | struct udp_pcb *ipcb; |
switches | 0:5c4d7b2438d3 | 1012 | |
switches | 0:5c4d7b2438d3 | 1013 | if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) { |
switches | 0:5c4d7b2438d3 | 1014 | return ERR_VAL; |
switches | 0:5c4d7b2438d3 | 1015 | } |
switches | 0:5c4d7b2438d3 | 1016 | |
switches | 0:5c4d7b2438d3 | 1017 | if (pcb->local_port == 0) { |
switches | 0:5c4d7b2438d3 | 1018 | err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port); |
switches | 0:5c4d7b2438d3 | 1019 | if (err != ERR_OK) { |
switches | 0:5c4d7b2438d3 | 1020 | return err; |
switches | 0:5c4d7b2438d3 | 1021 | } |
switches | 0:5c4d7b2438d3 | 1022 | } |
switches | 0:5c4d7b2438d3 | 1023 | |
switches | 0:5c4d7b2438d3 | 1024 | ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr); |
switches | 0:5c4d7b2438d3 | 1025 | pcb->remote_port = port; |
switches | 0:5c4d7b2438d3 | 1026 | pcb->flags |= UDP_FLAGS_CONNECTED; |
switches | 0:5c4d7b2438d3 | 1027 | |
switches | 0:5c4d7b2438d3 | 1028 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_connect: connected to ")); |
switches | 0:5c4d7b2438d3 | 1029 | ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
switches | 0:5c4d7b2438d3 | 1030 | &pcb->remote_ip); |
switches | 0:5c4d7b2438d3 | 1031 | LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->remote_port)); |
switches | 0:5c4d7b2438d3 | 1032 | |
switches | 0:5c4d7b2438d3 | 1033 | /* Insert UDP PCB into the list of active UDP PCBs. */ |
switches | 0:5c4d7b2438d3 | 1034 | for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) { |
switches | 0:5c4d7b2438d3 | 1035 | if (pcb == ipcb) { |
switches | 0:5c4d7b2438d3 | 1036 | /* already on the list, just return */ |
switches | 0:5c4d7b2438d3 | 1037 | return ERR_OK; |
switches | 0:5c4d7b2438d3 | 1038 | } |
switches | 0:5c4d7b2438d3 | 1039 | } |
switches | 0:5c4d7b2438d3 | 1040 | /* PCB not yet on the list, add PCB now */ |
switches | 0:5c4d7b2438d3 | 1041 | pcb->next = udp_pcbs; |
switches | 0:5c4d7b2438d3 | 1042 | udp_pcbs = pcb; |
switches | 0:5c4d7b2438d3 | 1043 | return ERR_OK; |
switches | 0:5c4d7b2438d3 | 1044 | } |
switches | 0:5c4d7b2438d3 | 1045 | |
switches | 0:5c4d7b2438d3 | 1046 | /** |
switches | 0:5c4d7b2438d3 | 1047 | * @ingroup udp_raw |
switches | 0:5c4d7b2438d3 | 1048 | * Disconnect a UDP PCB |
switches | 0:5c4d7b2438d3 | 1049 | * |
switches | 0:5c4d7b2438d3 | 1050 | * @param pcb the udp pcb to disconnect. |
switches | 0:5c4d7b2438d3 | 1051 | */ |
switches | 0:5c4d7b2438d3 | 1052 | void |
switches | 0:5c4d7b2438d3 | 1053 | udp_disconnect(struct udp_pcb *pcb) |
switches | 0:5c4d7b2438d3 | 1054 | { |
switches | 0:5c4d7b2438d3 | 1055 | /* reset remote address association */ |
switches | 0:5c4d7b2438d3 | 1056 | #if LWIP_IPV4 && LWIP_IPV6 |
switches | 0:5c4d7b2438d3 | 1057 | if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) { |
switches | 0:5c4d7b2438d3 | 1058 | ip_addr_copy(pcb->remote_ip, *IP_ANY_TYPE); |
switches | 0:5c4d7b2438d3 | 1059 | } else { |
switches | 0:5c4d7b2438d3 | 1060 | #endif |
switches | 0:5c4d7b2438d3 | 1061 | ip_addr_set_any(IP_IS_V6_VAL(pcb->remote_ip), &pcb->remote_ip); |
switches | 0:5c4d7b2438d3 | 1062 | #if LWIP_IPV4 && LWIP_IPV6 |
switches | 0:5c4d7b2438d3 | 1063 | } |
switches | 0:5c4d7b2438d3 | 1064 | #endif |
switches | 0:5c4d7b2438d3 | 1065 | pcb->remote_port = 0; |
switches | 0:5c4d7b2438d3 | 1066 | /* mark PCB as unconnected */ |
switches | 0:5c4d7b2438d3 | 1067 | pcb->flags &= ~UDP_FLAGS_CONNECTED; |
switches | 0:5c4d7b2438d3 | 1068 | } |
switches | 0:5c4d7b2438d3 | 1069 | |
switches | 0:5c4d7b2438d3 | 1070 | /** |
switches | 0:5c4d7b2438d3 | 1071 | * @ingroup udp_raw |
switches | 0:5c4d7b2438d3 | 1072 | * Set a receive callback for a UDP PCB |
switches | 0:5c4d7b2438d3 | 1073 | * |
switches | 0:5c4d7b2438d3 | 1074 | * This callback will be called when receiving a datagram for the pcb. |
switches | 0:5c4d7b2438d3 | 1075 | * |
switches | 0:5c4d7b2438d3 | 1076 | * @param pcb the pcb for which to set the recv callback |
switches | 0:5c4d7b2438d3 | 1077 | * @param recv function pointer of the callback function |
switches | 0:5c4d7b2438d3 | 1078 | * @param recv_arg additional argument to pass to the callback function |
switches | 0:5c4d7b2438d3 | 1079 | */ |
switches | 0:5c4d7b2438d3 | 1080 | void |
switches | 0:5c4d7b2438d3 | 1081 | udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg) |
switches | 0:5c4d7b2438d3 | 1082 | { |
switches | 0:5c4d7b2438d3 | 1083 | /* remember recv() callback and user data */ |
switches | 0:5c4d7b2438d3 | 1084 | pcb->recv = recv; |
switches | 0:5c4d7b2438d3 | 1085 | pcb->recv_arg = recv_arg; |
switches | 0:5c4d7b2438d3 | 1086 | } |
switches | 0:5c4d7b2438d3 | 1087 | |
switches | 0:5c4d7b2438d3 | 1088 | /** |
switches | 0:5c4d7b2438d3 | 1089 | * @ingroup udp_raw |
switches | 0:5c4d7b2438d3 | 1090 | * Remove an UDP PCB. |
switches | 0:5c4d7b2438d3 | 1091 | * |
switches | 0:5c4d7b2438d3 | 1092 | * @param pcb UDP PCB to be removed. The PCB is removed from the list of |
switches | 0:5c4d7b2438d3 | 1093 | * UDP PCB's and the data structure is freed from memory. |
switches | 0:5c4d7b2438d3 | 1094 | * |
switches | 0:5c4d7b2438d3 | 1095 | * @see udp_new() |
switches | 0:5c4d7b2438d3 | 1096 | */ |
switches | 0:5c4d7b2438d3 | 1097 | void |
switches | 0:5c4d7b2438d3 | 1098 | udp_remove(struct udp_pcb *pcb) |
switches | 0:5c4d7b2438d3 | 1099 | { |
switches | 0:5c4d7b2438d3 | 1100 | struct udp_pcb *pcb2; |
switches | 0:5c4d7b2438d3 | 1101 | |
switches | 0:5c4d7b2438d3 | 1102 | mib2_udp_unbind(pcb); |
switches | 0:5c4d7b2438d3 | 1103 | /* pcb to be removed is first in list? */ |
switches | 0:5c4d7b2438d3 | 1104 | if (udp_pcbs == pcb) { |
switches | 0:5c4d7b2438d3 | 1105 | /* make list start at 2nd pcb */ |
switches | 0:5c4d7b2438d3 | 1106 | udp_pcbs = udp_pcbs->next; |
switches | 0:5c4d7b2438d3 | 1107 | /* pcb not 1st in list */ |
switches | 0:5c4d7b2438d3 | 1108 | } else { |
switches | 0:5c4d7b2438d3 | 1109 | for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) { |
switches | 0:5c4d7b2438d3 | 1110 | /* find pcb in udp_pcbs list */ |
switches | 0:5c4d7b2438d3 | 1111 | if (pcb2->next != NULL && pcb2->next == pcb) { |
switches | 0:5c4d7b2438d3 | 1112 | /* remove pcb from list */ |
switches | 0:5c4d7b2438d3 | 1113 | pcb2->next = pcb->next; |
switches | 0:5c4d7b2438d3 | 1114 | break; |
switches | 0:5c4d7b2438d3 | 1115 | } |
switches | 0:5c4d7b2438d3 | 1116 | } |
switches | 0:5c4d7b2438d3 | 1117 | } |
switches | 0:5c4d7b2438d3 | 1118 | memp_free(MEMP_UDP_PCB, pcb); |
switches | 0:5c4d7b2438d3 | 1119 | } |
switches | 0:5c4d7b2438d3 | 1120 | |
switches | 0:5c4d7b2438d3 | 1121 | /** |
switches | 0:5c4d7b2438d3 | 1122 | * @ingroup udp_raw |
switches | 0:5c4d7b2438d3 | 1123 | * Create a UDP PCB. |
switches | 0:5c4d7b2438d3 | 1124 | * |
switches | 0:5c4d7b2438d3 | 1125 | * @return The UDP PCB which was created. NULL if the PCB data structure |
switches | 0:5c4d7b2438d3 | 1126 | * could not be allocated. |
switches | 0:5c4d7b2438d3 | 1127 | * |
switches | 0:5c4d7b2438d3 | 1128 | * @see udp_remove() |
switches | 0:5c4d7b2438d3 | 1129 | */ |
switches | 0:5c4d7b2438d3 | 1130 | struct udp_pcb * |
switches | 0:5c4d7b2438d3 | 1131 | udp_new(void) |
switches | 0:5c4d7b2438d3 | 1132 | { |
switches | 0:5c4d7b2438d3 | 1133 | struct udp_pcb *pcb; |
switches | 0:5c4d7b2438d3 | 1134 | pcb = (struct udp_pcb *)memp_malloc(MEMP_UDP_PCB); |
switches | 0:5c4d7b2438d3 | 1135 | /* could allocate UDP PCB? */ |
switches | 0:5c4d7b2438d3 | 1136 | if (pcb != NULL) { |
switches | 0:5c4d7b2438d3 | 1137 | /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0 |
switches | 0:5c4d7b2438d3 | 1138 | * which means checksum is generated over the whole datagram per default |
switches | 0:5c4d7b2438d3 | 1139 | * (recommended as default by RFC 3828). */ |
switches | 0:5c4d7b2438d3 | 1140 | /* initialize PCB to all zeroes */ |
switches | 0:5c4d7b2438d3 | 1141 | memset(pcb, 0, sizeof(struct udp_pcb)); |
switches | 0:5c4d7b2438d3 | 1142 | pcb->ttl = UDP_TTL; |
switches | 0:5c4d7b2438d3 | 1143 | #if LWIP_MULTICAST_TX_OPTIONS |
switches | 0:5c4d7b2438d3 | 1144 | pcb->mcast_ttl = UDP_TTL; |
switches | 0:5c4d7b2438d3 | 1145 | #endif /* LWIP_MULTICAST_TX_OPTIONS */ |
switches | 0:5c4d7b2438d3 | 1146 | } |
switches | 0:5c4d7b2438d3 | 1147 | return pcb; |
switches | 0:5c4d7b2438d3 | 1148 | } |
switches | 0:5c4d7b2438d3 | 1149 | |
switches | 0:5c4d7b2438d3 | 1150 | /** |
switches | 0:5c4d7b2438d3 | 1151 | * @ingroup udp_raw |
switches | 0:5c4d7b2438d3 | 1152 | * Create a UDP PCB for specific IP type. |
switches | 0:5c4d7b2438d3 | 1153 | * |
switches | 0:5c4d7b2438d3 | 1154 | * @param type IP address type, see IPADDR_TYPE_XX definitions. |
switches | 0:5c4d7b2438d3 | 1155 | * @return The UDP PCB which was created. NULL if the PCB data structure |
switches | 0:5c4d7b2438d3 | 1156 | * could not be allocated. |
switches | 0:5c4d7b2438d3 | 1157 | * |
switches | 0:5c4d7b2438d3 | 1158 | * @see udp_remove() |
switches | 0:5c4d7b2438d3 | 1159 | */ |
switches | 0:5c4d7b2438d3 | 1160 | struct udp_pcb * |
switches | 0:5c4d7b2438d3 | 1161 | udp_new_ip_type(u8_t type) |
switches | 0:5c4d7b2438d3 | 1162 | { |
switches | 0:5c4d7b2438d3 | 1163 | struct udp_pcb *pcb; |
switches | 0:5c4d7b2438d3 | 1164 | pcb = udp_new(); |
switches | 0:5c4d7b2438d3 | 1165 | #if LWIP_IPV4 && LWIP_IPV6 |
switches | 0:5c4d7b2438d3 | 1166 | if (pcb != NULL) { |
switches | 0:5c4d7b2438d3 | 1167 | IP_SET_TYPE_VAL(pcb->local_ip, type); |
switches | 0:5c4d7b2438d3 | 1168 | IP_SET_TYPE_VAL(pcb->remote_ip, type); |
switches | 0:5c4d7b2438d3 | 1169 | } |
switches | 0:5c4d7b2438d3 | 1170 | #else |
switches | 0:5c4d7b2438d3 | 1171 | LWIP_UNUSED_ARG(type); |
switches | 0:5c4d7b2438d3 | 1172 | #endif /* LWIP_IPV4 && LWIP_IPV6 */ |
switches | 0:5c4d7b2438d3 | 1173 | return pcb; |
switches | 0:5c4d7b2438d3 | 1174 | } |
switches | 0:5c4d7b2438d3 | 1175 | |
switches | 0:5c4d7b2438d3 | 1176 | #if LWIP_IPV4 |
switches | 0:5c4d7b2438d3 | 1177 | /** This function is called from netif.c when address is changed |
switches | 0:5c4d7b2438d3 | 1178 | * |
switches | 0:5c4d7b2438d3 | 1179 | * @param old_addr IPv4 address of the netif before change |
switches | 0:5c4d7b2438d3 | 1180 | * @param new_addr IPv4 address of the netif after change |
switches | 0:5c4d7b2438d3 | 1181 | */ |
switches | 0:5c4d7b2438d3 | 1182 | void udp_netif_ipv4_addr_changed(const ip4_addr_t* old_addr, const ip4_addr_t* new_addr) |
switches | 0:5c4d7b2438d3 | 1183 | { |
switches | 0:5c4d7b2438d3 | 1184 | struct udp_pcb* upcb; |
switches | 0:5c4d7b2438d3 | 1185 | |
switches | 0:5c4d7b2438d3 | 1186 | if (!ip4_addr_isany(new_addr)) { |
switches | 0:5c4d7b2438d3 | 1187 | for (upcb = udp_pcbs; upcb != NULL; upcb = upcb->next) { |
switches | 0:5c4d7b2438d3 | 1188 | /* Is this an IPv4 pcb? */ |
switches | 0:5c4d7b2438d3 | 1189 | if (IP_IS_V4_VAL(upcb->local_ip)) { |
switches | 0:5c4d7b2438d3 | 1190 | /* PCB bound to current local interface address? */ |
switches | 0:5c4d7b2438d3 | 1191 | if (!ip4_addr_isany(ip_2_ip4(&upcb->local_ip)) && |
switches | 0:5c4d7b2438d3 | 1192 | ip4_addr_cmp(ip_2_ip4(&upcb->local_ip), old_addr)) { |
switches | 0:5c4d7b2438d3 | 1193 | /* The PCB is bound to the old ipaddr and |
switches | 0:5c4d7b2438d3 | 1194 | * is set to bound to the new one instead */ |
switches | 0:5c4d7b2438d3 | 1195 | ip_addr_copy_from_ip4(upcb->local_ip, *new_addr); |
switches | 0:5c4d7b2438d3 | 1196 | } |
switches | 0:5c4d7b2438d3 | 1197 | } |
switches | 0:5c4d7b2438d3 | 1198 | } |
switches | 0:5c4d7b2438d3 | 1199 | } |
switches | 0:5c4d7b2438d3 | 1200 | } |
switches | 0:5c4d7b2438d3 | 1201 | #endif /* LWIP_IPV4 */ |
switches | 0:5c4d7b2438d3 | 1202 | |
switches | 0:5c4d7b2438d3 | 1203 | #if UDP_DEBUG |
switches | 0:5c4d7b2438d3 | 1204 | /** |
switches | 0:5c4d7b2438d3 | 1205 | * Print UDP header information for debug purposes. |
switches | 0:5c4d7b2438d3 | 1206 | * |
switches | 0:5c4d7b2438d3 | 1207 | * @param udphdr pointer to the udp header in memory. |
switches | 0:5c4d7b2438d3 | 1208 | */ |
switches | 0:5c4d7b2438d3 | 1209 | void |
switches | 0:5c4d7b2438d3 | 1210 | udp_debug_print(struct udp_hdr *udphdr) |
switches | 0:5c4d7b2438d3 | 1211 | { |
switches | 0:5c4d7b2438d3 | 1212 | LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n")); |
switches | 0:5c4d7b2438d3 | 1213 | LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); |
switches | 0:5c4d7b2438d3 | 1214 | LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n", |
switches | 0:5c4d7b2438d3 | 1215 | ntohs(udphdr->src), ntohs(udphdr->dest))); |
switches | 0:5c4d7b2438d3 | 1216 | LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); |
switches | 0:5c4d7b2438d3 | 1217 | LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n", |
switches | 0:5c4d7b2438d3 | 1218 | ntohs(udphdr->len), ntohs(udphdr->chksum))); |
switches | 0:5c4d7b2438d3 | 1219 | LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); |
switches | 0:5c4d7b2438d3 | 1220 | } |
switches | 0:5c4d7b2438d3 | 1221 | #endif /* UDP_DEBUG */ |
switches | 0:5c4d7b2438d3 | 1222 | |
switches | 0:5c4d7b2438d3 | 1223 | #endif /* LWIP_UDP */ |