Small Testprogram to have WebAccess via Webserver to a 433Mhz tranmitter to control remotly some devices from remote, with TFTP, NTP and RMF. This could be a base to develop applications.

Dependencies:   ChaNFSSD TFTPServer RMFWeb

Dependents:   RMFWeb

Committer:
ED7418
Date:
Fri Jun 06 09:06:03 2014 +0000
Revision:
0:51f1ef89ec7b
06062014

Who changed what in which revision?

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