Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

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