Port of LwIP performed by Ralf in 2010. Not recommended for use with recent mbed libraries, but good demos of raw LwIP possible

Dependents:   LwIP_raw_API_serverExample tiny-dtls

Committer:
RodColeman
Date:
Tue Sep 18 14:41:24 2012 +0000
Revision:
0:0791c1fece8e
[mbed] converted /Eth_TCP_Wei_Server/lwip

Who changed what in which revision?

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