Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

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