Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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