Example self-announcing webserver which controls a servo through a smallHTML userinterface.

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:56:01 2010 +0000
Revision:
0:a259777c45a3

        

Who changed what in which revision?

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