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