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