modded version Dirk-Willem van Gulik's Bonjour/Zerconf library http://mbed.org/users/dirkx/code/Bonjour/

Dependents:   OSCtoCVConverter

Fork of Bonjour by Dirk-Willem van Gulik (NXP/mbed)

Committer:
casiotone401
Date:
Thu Oct 16 14:13:21 2014 +0000
Revision:
8:275256b5d807
Parent:
7:105191e07767
minor change

Who changed what in which revision?

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