Ethernet test for ECE 4180 and others to find your IP address and do a simple HTTP GET request over port 80.

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Committer:
mkersh3
Date:
Thu Apr 04 05:26:09 2013 +0000
Revision:
0:e7ca326e76ee
Ethernet Test for ECE4180 and others to find their IP Address and do a simple HTTP GET request over port 80.

Who changed what in which revision?

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