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