1 /**
2 * @file
3 * User Datagram Protocol module\n
4 * The code for the User Datagram Protocol UDP & UDPLite (RFC 3828).\n
5 * See also @ref udp_raw
6 *
7 * @defgroup udp_raw UDP
8 * @ingroup callbackstyle_api
9 * User Datagram Protocol module\n
10 * @see @ref raw_api and @ref netconn
11 */
12
13 /*
14 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without modification,
18 * are permitted provided that the following conditions are met:
19 *
20 * 1. Redistributions of source code must retain the above copyright notice,
21 * this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright notice,
23 * this list of conditions and the following disclaimer in the documentation
24 * and/or other materials provided with the distribution.
25 * 3. The name of the author may not be used to endorse or promote products
26 * derived from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
31 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
33 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
37 * OF SUCH DAMAGE.
38 *
39 * This file is part of the lwIP TCP/IP stack.
40 *
41 * Author: Adam Dunkels <adam@sics.se>
42 *
43 */
44
45 /* @todo Check the use of '(struct udp_pcb).chksum_len_rx'!
46 */
47
48 #include "lwip/opt.h"
49
50 #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */
51
52 #include "lwip/udp.h"
53 #include "lwip/def.h"
54 #include "lwip/memp.h"
55 #include "lwip/inet_chksum.h"
56 #include "lwip/ip_addr.h"
57 #include "lwip/ip6.h"
58 #include "lwip/ip6_addr.h"
59 #include "lwip/netif.h"
60 #include "lwip/icmp.h"
61 #include "lwip/icmp6.h"
62 #include "lwip/stats.h"
63 #include "lwip/snmp.h"
64 #include "lwip/dhcp.h"
65
66 #include <string.h>
67
68 #ifndef UDP_LOCAL_PORT_RANGE_START
69 /* From http://www.iana.org/assignments/port-numbers:
70 "The Dynamic and/or Private Ports are those from 49152 through 65535" */
71 #define UDP_LOCAL_PORT_RANGE_START 0xc000
72 #define UDP_LOCAL_PORT_RANGE_END 0xffff
73 #define UDP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & ~UDP_LOCAL_PORT_RANGE_START) + UDP_LOCAL_PORT_RANGE_START))
74 #endif
75
76 /* last local UDP port */
77 static u16_t udp_port = UDP_LOCAL_PORT_RANGE_START;
78
79 /* The list of UDP PCBs */
80 /* exported in udp.h (was static) */
81 struct udp_pcb *udp_pcbs;
82
83 /**
84 * Initialize this module.
85 */
86 void
udp_init(void)87 udp_init(void)
88 {
89 #if LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND)
90 udp_port = UDP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND());
91 #endif /* LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) */
92 }
93
94 /**
95 * Allocate a new local UDP port.
96 *
97 * @return a new (free) local UDP port number
98 */
99 static u16_t
udp_new_port(void)100 udp_new_port(void)
101 {
102 u16_t n = 0;
103 struct udp_pcb *pcb;
104
105 again:
106 if (udp_port++ == UDP_LOCAL_PORT_RANGE_END) {
107 udp_port = UDP_LOCAL_PORT_RANGE_START;
108 }
109 /* Check all PCBs. */
110 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
111 if (pcb->local_port == udp_port) {
112 if (++n > (UDP_LOCAL_PORT_RANGE_END - UDP_LOCAL_PORT_RANGE_START)) {
113 return 0;
114 }
115 goto again;
116 }
117 }
118 return udp_port;
119 #if 0
120 struct udp_pcb *ipcb = udp_pcbs;
121 while ((ipcb != NULL) && (udp_port != UDP_LOCAL_PORT_RANGE_END)) {
122 if (ipcb->local_port == udp_port) {
123 /* port is already used by another udp_pcb */
124 udp_port++;
125 /* restart scanning all udp pcbs */
126 ipcb = udp_pcbs;
127 } else {
128 /* go on with next udp pcb */
129 ipcb = ipcb->next;
130 }
131 }
132 if (ipcb != NULL) {
133 return 0;
134 }
135 return udp_port;
136 #endif
137 }
138
139 /** Common code to see if the current input packet matches the pcb
140 * (current input packet is accessed via ip(4/6)_current_* macros)
141 *
142 * @param pcb pcb to check
143 * @param inp network interface on which the datagram was received (only used for IPv4)
144 * @param broadcast 1 if his is an IPv4 broadcast (global or subnet-only), 0 otherwise (only used for IPv4)
145 * @return 1 on match, 0 otherwise
146 */
147 static u8_t
udp_input_local_match(struct udp_pcb * pcb,struct netif * inp,u8_t broadcast)148 udp_input_local_match(struct udp_pcb *pcb, struct netif *inp, u8_t broadcast)
149 {
150 LWIP_UNUSED_ARG(inp); /* in IPv6 only case */
151 LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */
152
153 /* Dual-stack: PCBs listening to any IP type also listen to any IP address */
154 if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
155 #if LWIP_IPV4 && IP_SOF_BROADCAST_RECV
156 if ((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) {
157 return 0;
158 }
159 #endif /* LWIP_IPV4 && IP_SOF_BROADCAST_RECV */
160 return 1;
161 }
162
163 /* Only need to check PCB if incoming IP version matches PCB IP version */
164 if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) {
165 #if LWIP_IPV4
166 /* Special case: IPv4 broadcast: all or broadcasts in my subnet
167 * Note: broadcast variable can only be 1 if it is an IPv4 broadcast */
168 if (broadcast != 0) {
169 #if IP_SOF_BROADCAST_RECV
170 if (ip_get_option(pcb, SOF_BROADCAST))
171 #endif /* IP_SOF_BROADCAST_RECV */
172 {
173 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) ||
174 ((ip4_current_dest_addr()->addr == IPADDR_BROADCAST)) ||
175 ip4_addr_netcmp(ip_2_ip4(&pcb->local_ip), ip4_current_dest_addr(), netif_ip4_netmask(inp))) {
176 return 1;
177 }
178 }
179 } else
180 #endif /* LWIP_IPV4 */
181 /* Handle IPv4 and IPv6: all, multicast or exact match */
182 if (ip_addr_isany(&pcb->local_ip) ||
183 #if LWIP_IPV6_MLD
184 (ip_current_is_v6() && ip6_addr_ismulticast(ip6_current_dest_addr())) ||
185 #endif /* LWIP_IPV6_MLD */
186 #if LWIP_IGMP
187 (!ip_current_is_v6() && ip4_addr_ismulticast(ip4_current_dest_addr())) ||
188 #endif /* LWIP_IGMP */
189 ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
190 return 1;
191 }
192 }
193
194 return 0;
195 }
196
197 /**
198 * Process an incoming UDP datagram.
199 *
200 * Given an incoming UDP datagram (as a chain of pbufs) this function
201 * finds a corresponding UDP PCB and hands over the pbuf to the pcbs
202 * recv function. If no pcb is found or the datagram is incorrect, the
203 * pbuf is freed.
204 *
205 * @param p pbuf to be demultiplexed to a UDP PCB (p->payload pointing to the UDP header)
206 * @param inp network interface on which the datagram was received.
207 *
208 */
209 void
udp_input(struct pbuf * p,struct netif * inp)210 udp_input(struct pbuf *p, struct netif *inp)
211 {
212 struct udp_hdr *udphdr;
213 struct udp_pcb *pcb, *prev;
214 struct udp_pcb *uncon_pcb;
215 u16_t src, dest;
216 u8_t broadcast;
217 u8_t for_us = 0;
218
219 LWIP_UNUSED_ARG(inp);
220
221 PERF_START;
222
223 UDP_STATS_INC(udp.recv);
224
225 /* Check minimum length (UDP header) */
226 if (p->len < UDP_HLEN) {
227 /* drop short packets */
228 LWIP_DEBUGF(UDP_DEBUG,
229 ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len));
230 UDP_STATS_INC(udp.lenerr);
231 UDP_STATS_INC(udp.drop);
232 MIB2_STATS_INC(mib2.udpinerrors);
233 pbuf_free(p);
234 goto end;
235 }
236
237 udphdr = (struct udp_hdr *)p->payload;
238
239 /* is broadcast packet ? */
240 broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif());
241
242 LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len));
243
244 /* convert src and dest ports to host byte order */
245 src = lwip_ntohs(udphdr->src);
246 dest = lwip_ntohs(udphdr->dest);
247
248 udp_debug_print(udphdr);
249
250 /* print the UDP source and destination */
251 LWIP_DEBUGF(UDP_DEBUG, ("udp ("));
252 ip_addr_debug_print(UDP_DEBUG, ip_current_dest_addr());
253 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", lwip_ntohs(udphdr->dest)));
254 ip_addr_debug_print(UDP_DEBUG, ip_current_src_addr());
255 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", lwip_ntohs(udphdr->src)));
256
257 pcb = NULL;
258 prev = NULL;
259 uncon_pcb = NULL;
260 /* Iterate through the UDP pcb list for a matching pcb.
261 * 'Perfect match' pcbs (connected to the remote port & ip address) are
262 * preferred. If no perfect match is found, the first unconnected pcb that
263 * matches the local port and ip address gets the datagram. */
264 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
265 /* print the PCB local and remote address */
266 LWIP_DEBUGF(UDP_DEBUG, ("pcb ("));
267 ip_addr_debug_print(UDP_DEBUG, &pcb->local_ip);
268 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", pcb->local_port));
269 ip_addr_debug_print(UDP_DEBUG, &pcb->remote_ip);
270 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", pcb->remote_port));
271
272 /* compare PCB local addr+port to UDP destination addr+port */
273 if ((pcb->local_port == dest) &&
274 (udp_input_local_match(pcb, inp, broadcast) != 0)) {
275 if (((pcb->flags & UDP_FLAGS_CONNECTED) == 0) &&
276 ((uncon_pcb == NULL)
277 #if SO_REUSE
278 /* prefer specific IPs over cath-all */
279 || !ip_addr_isany(&pcb->local_ip)
280 #endif /* SO_REUSE */
281 )) {
282 /* the first unconnected matching PCB */
283 uncon_pcb = pcb;
284 }
285
286 /* compare PCB remote addr+port to UDP source addr+port */
287 if ((pcb->remote_port == src) &&
288 (ip_addr_isany_val(pcb->remote_ip) ||
289 ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()))) {
290 /* the first fully matching PCB */
291 if (prev != NULL) {
292 /* move the pcb to the front of udp_pcbs so that is
293 found faster next time */
294 prev->next = pcb->next;
295 pcb->next = udp_pcbs;
296 udp_pcbs = pcb;
297 } else {
298 UDP_STATS_INC(udp.cachehit);
299 }
300 break;
301 }
302 }
303
304 prev = pcb;
305 }
306 /* no fully matching pcb found? then look for an unconnected pcb */
307 if (pcb == NULL) {
308 pcb = uncon_pcb;
309 }
310
311 /* Check checksum if this is a match or if it was directed at us. */
312 if (pcb != NULL) {
313 for_us = 1;
314 } else {
315 #if LWIP_IPV6
316 if (ip_current_is_v6()) {
317 for_us = netif_get_ip6_addr_match(inp, ip6_current_dest_addr()) >= 0;
318 }
319 #endif /* LWIP_IPV6 */
320 #if LWIP_IPV4
321 if (!ip_current_is_v6()) {
322 for_us = ip4_addr_cmp(netif_ip4_addr(inp), ip4_current_dest_addr());
323 }
324 #endif /* LWIP_IPV4 */
325 }
326
327 if (for_us) {
328 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n"));
329 #if CHECKSUM_CHECK_UDP
330 IF__NETIF_CHECKSUM_ENABLED(inp, CHECKSUM_CHECK_UDP) {
331 #if LWIP_UDPLITE
332 if (ip_current_header_proto() == IP_PROTO_UDPLITE) {
333 /* Do the UDP Lite checksum */
334 u16_t chklen = lwip_ntohs(udphdr->len);
335 if (chklen < sizeof(struct udp_hdr)) {
336 if (chklen == 0) {
337 /* For UDP-Lite, checksum length of 0 means checksum
338 over the complete packet (See RFC 3828 chap. 3.1) */
339 chklen = p->tot_len;
340 } else {
341 /* At least the UDP-Lite header must be covered by the
342 checksum! (Again, see RFC 3828 chap. 3.1) */
343 goto chkerr;
344 }
345 }
346 if (ip_chksum_pseudo_partial(p, IP_PROTO_UDPLITE,
347 p->tot_len, chklen,
348 ip_current_src_addr(), ip_current_dest_addr()) != 0) {
349 goto chkerr;
350 }
351 } else
352 #endif /* LWIP_UDPLITE */
353 {
354 #ifndef DNS_SERVER_PORT
355 #define DNS_SERVER_PORT 53
356 #endif
357 if (lwip_ntohs(udphdr->src) != DNS_SERVER_PORT && udphdr->chksum != 0) {
358 if (ip_chksum_pseudo(p, IP_PROTO_UDP, p->tot_len,
359 ip_current_src_addr(),
360 ip_current_dest_addr()) != 0) {
361 goto chkerr;
362 }
363 }
364 }
365 }
366 #endif /* CHECKSUM_CHECK_UDP */
367 if (pbuf_header(p, -UDP_HLEN)) {
368 /* Can we cope with this failing? Just assert for now */
369 LWIP_ASSERT("pbuf_header failed\n", 0);
370 UDP_STATS_INC(udp.drop);
371 MIB2_STATS_INC(mib2.udpinerrors);
372 pbuf_free(p);
373 goto end;
374 }
375
376 if (pcb != NULL) {
377 MIB2_STATS_INC(mib2.udpindatagrams);
378 #if SO_REUSE && SO_REUSE_RXTOALL
379 if (ip_get_option(pcb, SOF_REUSEADDR) &&
380 (broadcast || ip_addr_ismulticast(ip_current_dest_addr()))) {
381 /* pass broadcast- or multicast packets to all multicast pcbs
382 if SOF_REUSEADDR is set on the first match */
383 struct udp_pcb *mpcb;
384 u8_t p_header_changed = 0;
385 s16_t hdrs_len = (s16_t)(ip_current_header_tot_len() + UDP_HLEN);
386 for (mpcb = udp_pcbs; mpcb != NULL; mpcb = mpcb->next) {
387 if (mpcb != pcb) {
388 /* compare PCB local addr+port to UDP destination addr+port */
389 if ((mpcb->local_port == dest) &&
390 (udp_input_local_match(mpcb, inp, broadcast) != 0)) {
391 /* pass a copy of the packet to all local matches */
392 if (mpcb->recv != NULL) {
393 struct pbuf *q;
394 /* for that, move payload to IP header again */
395 if (p_header_changed == 0) {
396 pbuf_header_force(p, hdrs_len);
397 p_header_changed = 1;
398 }
399 q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
400 if (q != NULL) {
401 err_t err = pbuf_copy(q, p);
402 if (err == ERR_OK) {
403 /* move payload to UDP data */
404 pbuf_header(q, -hdrs_len);
405 mpcb->recv(mpcb->recv_arg, mpcb, q, ip_current_src_addr(), src);
406 }
407 }
408 }
409 }
410 }
411 }
412 if (p_header_changed) {
413 /* and move payload to UDP data again */
414 pbuf_header(p, -hdrs_len);
415 }
416 }
417 #endif /* SO_REUSE && SO_REUSE_RXTOALL */
418 /* callback */
419 if (pcb->recv != NULL) {
420 /* now the recv function is responsible for freeing p */
421 pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr(), src);
422 } else {
423 /* no recv function registered? then we have to free the pbuf! */
424 pbuf_free(p);
425 goto end;
426 }
427 } else {
428 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n"));
429
430 #if LWIP_ICMP || LWIP_ICMP6
431 /* No match was found, send ICMP destination port unreachable unless
432 destination address was broadcast/multicast. */
433 if (!broadcast && !ip_addr_ismulticast(ip_current_dest_addr())) {
434 /* move payload pointer back to ip header */
435 pbuf_header_force(p, ip_current_header_tot_len() + UDP_HLEN);
436 icmp_port_unreach(ip_current_is_v6(), p);
437 }
438 #endif /* LWIP_ICMP || LWIP_ICMP6 */
439 UDP_STATS_INC(udp.proterr);
440 UDP_STATS_INC(udp.drop);
441 MIB2_STATS_INC(mib2.udpnoports);
442 pbuf_free(p);
443 }
444 } else {
445 pbuf_free(p);
446 }
447 end:
448 PERF_STOP("udp_input");
449 return;
450 #if CHECKSUM_CHECK_UDP
451 chkerr:
452 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
453 ("udp_input: UDP (or UDP Lite) datagram discarded due to failing checksum\n"));
454 UDP_STATS_INC(udp.chkerr);
455 UDP_STATS_INC(udp.drop);
456 MIB2_STATS_INC(mib2.udpinerrors);
457 pbuf_free(p);
458 PERF_STOP("udp_input");
459 #endif /* CHECKSUM_CHECK_UDP */
460 }
461
462 /**
463 * @ingroup udp_raw
464 * Send data using UDP.
465 *
466 * @param pcb UDP PCB used to send the data.
467 * @param p chain of pbuf's to be sent.
468 *
469 * The datagram will be sent to the current remote_ip & remote_port
470 * stored in pcb. If the pcb is not bound to a port, it will
471 * automatically be bound to a random port.
472 *
473 * @return lwIP error code.
474 * - ERR_OK. Successful. No error occurred.
475 * - ERR_MEM. Out of memory.
476 * - ERR_RTE. Could not find route to destination address.
477 * - ERR_VAL. No PCB or PCB is dual-stack
478 * - More errors could be returned by lower protocol layers.
479 *
480 * @see udp_disconnect() udp_sendto()
481 */
482 err_t
udp_send(struct udp_pcb * pcb,struct pbuf * p)483 udp_send(struct udp_pcb *pcb, struct pbuf *p)
484 {
485 if ((pcb == NULL) || IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) {
486 return ERR_VAL;
487 }
488
489 /* send to the packet using remote ip and port stored in the pcb */
490 return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port);
491 }
492
493 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
494 /** @ingroup udp_raw
495 * Same as udp_send() but with checksum
496 */
497 err_t
udp_send_chksum(struct udp_pcb * pcb,struct pbuf * p,u8_t have_chksum,u16_t chksum)498 udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p,
499 u8_t have_chksum, u16_t chksum)
500 {
501 if ((pcb == NULL) || IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) {
502 return ERR_VAL;
503 }
504
505 /* send to the packet using remote ip and port stored in the pcb */
506 return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port,
507 have_chksum, chksum);
508 }
509 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
510
511 /**
512 * @ingroup udp_raw
513 * Send data to a specified address using UDP.
514 *
515 * @param pcb UDP PCB used to send the data.
516 * @param p chain of pbuf's to be sent.
517 * @param dst_ip Destination IP address.
518 * @param dst_port Destination UDP port.
519 *
520 * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
521 *
522 * If the PCB already has a remote address association, it will
523 * be restored after the data is sent.
524 *
525 * @return lwIP error code (@see udp_send for possible error codes)
526 *
527 * @see udp_disconnect() udp_send()
528 */
529 err_t
udp_sendto(struct udp_pcb * pcb,struct pbuf * p,const ip_addr_t * dst_ip,u16_t dst_port)530 udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
531 const ip_addr_t *dst_ip, u16_t dst_port)
532 {
533 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
534 return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0);
535 }
536
537 /** @ingroup udp_raw
538 * Same as udp_sendto(), but with checksum */
539 err_t
udp_sendto_chksum(struct udp_pcb * pcb,struct pbuf * p,const ip_addr_t * dst_ip,u16_t dst_port,u8_t have_chksum,u16_t chksum)540 udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
541 u16_t dst_port, u8_t have_chksum, u16_t chksum)
542 {
543 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
544 struct netif *netif;
545 const ip_addr_t *dst_ip_route = dst_ip;
546
547 if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
548 return ERR_VAL;
549 }
550
551 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send\n"));
552
553 #if LWIP_IPV6 || (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS)
554 if (ip_addr_ismulticast(dst_ip_route)) {
555 #if LWIP_IPV6
556 if (IP_IS_V6(dst_ip)) {
557 /* For multicast, find a netif based on source address. */
558 /* if src address is ANY ADDR, based on dest address. */
559 dst_ip_route = IP_IS_ANY_TYPE_VAL(pcb->local_ip) ? dst_ip_route : &pcb->local_ip;
560 } else
561 #endif /* LWIP_IPV6 */
562 {
563 #if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS
564 /* IPv4 does not use source-based routing by default, so we use an
565 administratively selected interface for multicast by default.
566 However, this can be overridden by setting an interface address
567 in pcb->multicast_ip that is used for routing. */
568 if (!ip_addr_isany_val(pcb->multicast_ip) &&
569 !ip4_addr_cmp(ip_2_ip4(&pcb->multicast_ip), IP4_ADDR_BROADCAST)) {
570 dst_ip_route = &pcb->multicast_ip;
571 }
572 #endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS */
573 }
574 }
575 #endif /* LWIP_IPV6 || (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) */
576
577 /* find the outgoing network interface for this packet */
578 netif = ip_route(&pcb->local_ip, dst_ip_route);
579
580 /* no outgoing network interface could be found? */
581 if (netif == NULL) {
582 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: No route to "));
583 ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, dst_ip);
584 LWIP_DEBUGF(UDP_DEBUG, ("\n"));
585 UDP_STATS_INC(udp.rterr);
586 return ERR_RTE;
587 }
588 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
589 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum);
590 #else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
591 return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
592 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
593 }
594
595 /**
596 * @ingroup udp_raw
597 * Send data to a specified address using UDP.
598 * The netif used for sending can be specified.
599 *
600 * This function exists mainly for DHCP, to be able to send UDP packets
601 * on a netif that is still down.
602 *
603 * @param pcb UDP PCB used to send the data.
604 * @param p chain of pbuf's to be sent.
605 * @param dst_ip Destination IP address.
606 * @param dst_port Destination UDP port.
607 * @param netif the netif used for sending.
608 *
609 * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
610 *
611 * @return lwIP error code (@see udp_send for possible error codes)
612 *
613 * @see udp_disconnect() udp_send()
614 */
615 err_t
udp_sendto_if(struct udp_pcb * pcb,struct pbuf * p,const ip_addr_t * dst_ip,u16_t dst_port,struct netif * netif)616 udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p,
617 const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif)
618 {
619 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
620 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0);
621 }
622
623 /** Same as udp_sendto_if(), but with checksum */
624 err_t
udp_sendto_if_chksum(struct udp_pcb * pcb,struct pbuf * p,const ip_addr_t * dst_ip,u16_t dst_port,struct netif * netif,u8_t have_chksum,u16_t chksum)625 udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
626 u16_t dst_port, struct netif *netif, u8_t have_chksum,
627 u16_t chksum)
628 {
629 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
630 const ip_addr_t *src_ip;
631
632 if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
633 return ERR_VAL;
634 }
635
636 /* PCB local address is IP_ANY_ADDR? */
637 #if LWIP_IPV6
638 if (IP_IS_V6(dst_ip)) {
639 if (ip6_addr_isany(ip_2_ip6(&pcb->local_ip))) {
640 src_ip = ip6_select_source_address(netif, ip_2_ip6(dst_ip));
641 if (src_ip == NULL) {
642 /* No suitable source address was found. */
643 return ERR_RTE;
644 }
645 } else {
646 /* use UDP PCB local IPv6 address as source address, if still valid. */
647 if (netif_get_ip6_addr_match(netif, ip_2_ip6(&pcb->local_ip)) < 0) {
648 /* Address isn't valid anymore. */
649 return ERR_RTE;
650 }
651 src_ip = &pcb->local_ip;
652 }
653 }
654 #endif /* LWIP_IPV6 */
655 #if LWIP_IPV4 && LWIP_IPV6
656 else
657 #endif /* LWIP_IPV4 && LWIP_IPV6 */
658 #if LWIP_IPV4
659 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) ||
660 ip4_addr_ismulticast(ip_2_ip4(&pcb->local_ip))) {
661 /* if the local_ip is any or multicast
662 * use the outgoing network interface IP address as source address */
663 src_ip = netif_ip_addr4(netif);
664 } else {
665 /* check if UDP PCB local IP address is correct
666 * this could be an old address if netif->ip_addr has changed */
667 if (!ip4_addr_cmp(ip_2_ip4(&(pcb->local_ip)), netif_ip4_addr(netif))) {
668 /* local_ip doesn't match, drop the packet */
669 return ERR_VAL;
670 }
671 /* use UDP PCB local IP address as source address */
672 src_ip = &pcb->local_ip;
673 }
674 #endif /* LWIP_IPV4 */
675 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
676 return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum, src_ip);
677 #else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
678 return udp_sendto_if_src(pcb, p, dst_ip, dst_port, netif, src_ip);
679 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
680 }
681
682 /** @ingroup udp_raw
683 * Same as @ref udp_sendto_if, but with source address */
684 err_t
udp_sendto_if_src(struct udp_pcb * pcb,struct pbuf * p,const ip_addr_t * dst_ip,u16_t dst_port,struct netif * netif,const ip_addr_t * src_ip)685 udp_sendto_if_src(struct udp_pcb *pcb, struct pbuf *p,
686 const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif, const ip_addr_t *src_ip)
687 {
688 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
689 return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0, src_ip);
690 }
691
692 /** Same as udp_sendto_if_src(), but with checksum */
693 err_t
udp_sendto_if_src_chksum(struct udp_pcb * pcb,struct pbuf * p,const ip_addr_t * dst_ip,u16_t dst_port,struct netif * netif,u8_t have_chksum,u16_t chksum,const ip_addr_t * src_ip)694 udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
695 u16_t dst_port, struct netif *netif, u8_t have_chksum,
696 u16_t chksum, const ip_addr_t *src_ip)
697 {
698 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
699 struct udp_hdr *udphdr;
700 err_t err;
701 struct pbuf *q; /* q will be sent down the stack */
702 u8_t ip_proto;
703 u8_t ttl;
704
705 if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) ||
706 !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
707 return ERR_VAL;
708 }
709
710 #if LWIP_IPV4 && IP_SOF_BROADCAST
711 /* broadcast filter? */
712 if (!ip_get_option(pcb, SOF_BROADCAST) &&
713 #if LWIP_IPV6
714 IP_IS_V4(dst_ip) &&
715 #endif /* LWIP_IPV6 */
716 ip_addr_isbroadcast(dst_ip, netif)) {
717 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
718 ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
719 return ERR_VAL;
720 }
721 #endif /* LWIP_IPV4 && IP_SOF_BROADCAST */
722
723 /* if the PCB is not yet bound to a port, bind it here */
724 if (pcb->local_port == 0) {
725 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n"));
726 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
727 if (err != ERR_OK) {
728 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n"));
729 return err;
730 }
731 }
732
733 /* not enough space to add an UDP header to first pbuf in given p chain? */
734 if (pbuf_header(p, UDP_HLEN)) {
735 /* allocate header in a separate new pbuf */
736 q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
737 /* new header pbuf could not be allocated? */
738 if (q == NULL) {
739 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n"));
740 return ERR_MEM;
741 }
742 if (p->tot_len != 0) {
743 /* chain header q in front of given pbuf p (only if p contains data) */
744 pbuf_chain(q, p);
745 }
746 /* first pbuf q points to header pbuf */
747 LWIP_DEBUGF(UDP_DEBUG,
748 ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
749 } else {
750 /* adding space for header within p succeeded */
751 /* first pbuf q equals given pbuf */
752 q = p;
753 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
754 }
755 LWIP_ASSERT("check that first pbuf can hold struct udp_hdr",
756 (q->len >= sizeof(struct udp_hdr)));
757 /* q now represents the packet to be sent */
758 udphdr = (struct udp_hdr *)q->payload;
759 udphdr->src = lwip_htons(pcb->local_port);
760 udphdr->dest = lwip_htons(dst_port);
761 /* in UDP, 0 checksum means 'no checksum' */
762 udphdr->chksum = 0x0000;
763
764 /* Multicast Loop? */
765 #if (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) || (LWIP_IPV6 && LWIP_IPV6_MLD)
766 if (((pcb->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) {
767 q->flags |= PBUF_FLAG_MCASTLOOP;
768 }
769 #endif /* (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) || (LWIP_IPV6 && LWIP_IPV6_MLD) */
770
771 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));
772
773 #if LWIP_UDPLITE
774 /* UDP Lite protocol? */
775 if (pcb->flags & UDP_FLAGS_UDPLITE) {
776 u16_t chklen, chklen_hdr;
777 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));
778 /* set UDP message length in UDP header */
779 chklen_hdr = chklen = pcb->chksum_len_tx;
780 if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) {
781 if (chklen != 0) {
782 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen));
783 }
784 /* For UDP-Lite, checksum length of 0 means checksum
785 over the complete packet. (See RFC 3828 chap. 3.1)
786 At least the UDP-Lite header must be covered by the
787 checksum, therefore, if chksum_len has an illegal
788 value, we generate the checksum over the complete
789 packet to be safe. */
790 chklen_hdr = 0;
791 chklen = q->tot_len;
792 }
793 udphdr->len = lwip_htons(chklen_hdr);
794 /* calculate checksum */
795 #if CHECKSUM_GEN_UDP
796 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) {
797 #if LWIP_CHECKSUM_ON_COPY
798 if (have_chksum) {
799 chklen = UDP_HLEN;
800 }
801 #endif /* LWIP_CHECKSUM_ON_COPY */
802 udphdr->chksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDPLITE,
803 q->tot_len, chklen, src_ip, dst_ip);
804 #if LWIP_CHECKSUM_ON_COPY
805 if (have_chksum) {
806 u32_t acc;
807 acc = udphdr->chksum + (u16_t)~(chksum);
808 udphdr->chksum = FOLD_U32T(acc);
809 }
810 #endif /* LWIP_CHECKSUM_ON_COPY */
811
812 /* chksum zero must become 0xffff, as zero means 'no checksum' */
813 if (udphdr->chksum == 0x0000) {
814 udphdr->chksum = 0xffff;
815 }
816 }
817 #endif /* CHECKSUM_GEN_UDP */
818
819 ip_proto = IP_PROTO_UDPLITE;
820 } else
821 #endif /* LWIP_UDPLITE */
822 { /* UDP */
823 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));
824 udphdr->len = lwip_htons(q->tot_len);
825 /* calculate checksum */
826 #if CHECKSUM_GEN_UDP
827 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) {
828 /* Checksum is mandatory over IPv6. */
829 if (IP_IS_V6(dst_ip) || (pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
830 u16_t udpchksum;
831 #if LWIP_CHECKSUM_ON_COPY
832 if (have_chksum) {
833 u32_t acc;
834 udpchksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDP,
835 q->tot_len, UDP_HLEN, src_ip, dst_ip);
836 acc = udpchksum + (u16_t)~(chksum);
837 udpchksum = FOLD_U32T(acc);
838 } else
839 #endif /* LWIP_CHECKSUM_ON_COPY */
840 {
841 udpchksum = ip_chksum_pseudo(q, IP_PROTO_UDP, q->tot_len,
842 src_ip, dst_ip);
843 }
844
845 /* chksum zero must become 0xffff, as zero means 'no checksum' */
846 if (udpchksum == 0x0000) {
847 udpchksum = 0xffff;
848 }
849 udphdr->chksum = udpchksum;
850 }
851 }
852 #endif /* CHECKSUM_GEN_UDP */
853 ip_proto = IP_PROTO_UDP;
854 }
855
856 /* Determine TTL to use */
857 #if LWIP_MULTICAST_TX_OPTIONS
858 ttl = (ip_addr_ismulticast(dst_ip) ? udp_get_multicast_ttl(pcb) : pcb->ttl);
859 #else /* LWIP_MULTICAST_TX_OPTIONS */
860 ttl = pcb->ttl;
861 #endif /* LWIP_MULTICAST_TX_OPTIONS */
862
863 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));
864 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,0x%02"X16_F",)\n", (u16_t)ip_proto));
865 /* output to IP */
866 NETIF_SET_HWADDRHINT(netif, &(pcb->addr_hint));
867 err = ip_output_if_src(q, src_ip, dst_ip, ttl, pcb->tos, ip_proto, netif);
868 NETIF_SET_HWADDRHINT(netif, NULL);
869
870 /* @todo: must this be increased even if error occurred? */
871 MIB2_STATS_INC(mib2.udpoutdatagrams);
872
873 /* did we chain a separate header pbuf earlier? */
874 if (q != p) {
875 /* free the header pbuf */
876 pbuf_free(q);
877 q = NULL;
878 /* p is still referenced by the caller, and will live on */
879 }
880
881 UDP_STATS_INC(udp.xmit);
882 return err;
883 }
884
885 /**
886 * @ingroup udp_raw
887 * Bind an UDP PCB.
888 *
889 * @param pcb UDP PCB to be bound with a local address ipaddr and port.
890 * @param ipaddr local IP address to bind with. Use IP4_ADDR_ANY to
891 * bind to all local interfaces.
892 * @param port local UDP port to bind with. Use 0 to automatically bind
893 * to a random port between UDP_LOCAL_PORT_RANGE_START and
894 * UDP_LOCAL_PORT_RANGE_END.
895 *
896 * ipaddr & port are expected to be in the same byte order as in the pcb.
897 *
898 * @return lwIP error code.
899 * - ERR_OK. Successful. No error occurred.
900 * - ERR_USE. The specified ipaddr and port are already bound to by
901 * another UDP PCB.
902 *
903 * @see udp_disconnect()
904 */
905 err_t
udp_bind(struct udp_pcb * pcb,const ip_addr_t * ipaddr,u16_t port)906 udp_bind(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
907 {
908 struct udp_pcb *ipcb;
909 u8_t rebind;
910
911 #if LWIP_IPV4
912 /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
913 if (ipaddr == NULL) {
914 ipaddr = IP4_ADDR_ANY;
915 }
916 #endif /* LWIP_IPV4 */
917
918 /* still need to check for ipaddr == NULL in IPv6 only case */
919 if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ipaddr)) {
920 return ERR_VAL;
921 }
922
923 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = "));
924 ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE, ipaddr);
925 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port));
926
927 rebind = 0;
928 /* Check for double bind and rebind of the same pcb */
929 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
930 /* is this UDP PCB already on active list? */
931 if (pcb == ipcb) {
932 rebind = 1;
933 break;
934 }
935 }
936
937 /* no port specified? */
938 if (port == 0) {
939 port = udp_new_port();
940 if (port == 0) {
941 /* no more ports available in local range */
942 LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
943 return ERR_USE;
944 }
945 } else {
946 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
947 if (pcb != ipcb) {
948 /* By default, we don't allow to bind to a port that any other udp
949 PCB is already bound to, unless *all* PCBs with that port have tha
950 REUSEADDR flag set. */
951 #if SO_REUSE
952 if (!ip_get_option(pcb, SOF_REUSEADDR) ||
953 !ip_get_option(ipcb, SOF_REUSEADDR))
954 #endif /* SO_REUSE */
955 {
956 /* port matches that of PCB in list and REUSEADDR not set -> reject */
957 if ((ipcb->local_port == port) &&
958 /* IP address matches? */
959 ip_addr_cmp(&ipcb->local_ip, ipaddr)) {
960 /* other PCB already binds to this local IP and port */
961 LWIP_DEBUGF(UDP_DEBUG,
962 ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));
963 return ERR_USE;
964 }
965 }
966 }
967 }
968 }
969
970 ip_addr_set_ipaddr(&pcb->local_ip, ipaddr);
971
972 pcb->local_port = port;
973 mib2_udp_bind(pcb);
974 /* pcb not active yet? */
975 if (rebind == 0) {
976 /* place the PCB on the active list if not already there */
977 pcb->next = udp_pcbs;
978 udp_pcbs = pcb;
979 }
980 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_bind: bound to "));
981 ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, &pcb->local_ip);
982 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->local_port));
983 return ERR_OK;
984 }
985
986 /**
987 * @ingroup udp_raw
988 * Connect an UDP PCB.
989 *
990 * This will associate the UDP PCB with the remote address.
991 *
992 * @param pcb UDP PCB to be connected with remote address ipaddr and port.
993 * @param ipaddr remote IP address to connect with.
994 * @param port remote UDP port to connect with.
995 *
996 * @return lwIP error code
997 *
998 * ipaddr & port are expected to be in the same byte order as in the pcb.
999 *
1000 * The udp pcb is bound to a random local port if not already bound.
1001 *
1002 * @see udp_disconnect()
1003 */
1004 err_t
udp_connect(struct udp_pcb * pcb,const ip_addr_t * ipaddr,u16_t port)1005 udp_connect(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
1006 {
1007 struct udp_pcb *ipcb;
1008
1009 if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) {
1010 return ERR_VAL;
1011 }
1012
1013 if (pcb->local_port == 0) {
1014 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
1015 if (err != ERR_OK) {
1016 return err;
1017 }
1018 }
1019
1020 ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr);
1021 pcb->remote_port = port;
1022 pcb->flags |= UDP_FLAGS_CONNECTED;
1023
1024 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_connect: connected to "));
1025 ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
1026 &pcb->remote_ip);
1027 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->remote_port));
1028
1029 /* Insert UDP PCB into the list of active UDP PCBs. */
1030 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
1031 if (pcb == ipcb) {
1032 /* already on the list, just return */
1033 return ERR_OK;
1034 }
1035 }
1036 /* PCB not yet on the list, add PCB now */
1037 pcb->next = udp_pcbs;
1038 udp_pcbs = pcb;
1039 return ERR_OK;
1040 }
1041
1042 /**
1043 * @ingroup udp_raw
1044 * Disconnect a UDP PCB
1045 *
1046 * @param pcb the udp pcb to disconnect.
1047 */
1048 void
udp_disconnect(struct udp_pcb * pcb)1049 udp_disconnect(struct udp_pcb *pcb)
1050 {
1051 /* reset remote address association */
1052 #if LWIP_IPV4 && LWIP_IPV6
1053 if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
1054 ip_addr_copy(pcb->remote_ip, *IP_ANY_TYPE);
1055 } else {
1056 #endif
1057 ip_addr_set_any(IP_IS_V6_VAL(pcb->remote_ip), &pcb->remote_ip);
1058 #if LWIP_IPV4 && LWIP_IPV6
1059 }
1060 #endif
1061 pcb->remote_port = 0;
1062 /* mark PCB as unconnected */
1063 pcb->flags &= ~UDP_FLAGS_CONNECTED;
1064 }
1065
1066 /**
1067 * @ingroup udp_raw
1068 * Set a receive callback for a UDP PCB
1069 *
1070 * This callback will be called when receiving a datagram for the pcb.
1071 *
1072 * @param pcb the pcb for which to set the recv callback
1073 * @param recv function pointer of the callback function
1074 * @param recv_arg additional argument to pass to the callback function
1075 */
1076 void
udp_recv(struct udp_pcb * pcb,udp_recv_fn recv,void * recv_arg)1077 udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg)
1078 {
1079 /* remember recv() callback and user data */
1080 pcb->recv = recv;
1081 pcb->recv_arg = recv_arg;
1082 }
1083
1084 /**
1085 * @ingroup udp_raw
1086 * Remove an UDP PCB.
1087 *
1088 * @param pcb UDP PCB to be removed. The PCB is removed from the list of
1089 * UDP PCB's and the data structure is freed from memory.
1090 *
1091 * @see udp_new()
1092 */
1093 void
udp_remove(struct udp_pcb * pcb)1094 udp_remove(struct udp_pcb *pcb)
1095 {
1096 struct udp_pcb *pcb2;
1097
1098 mib2_udp_unbind(pcb);
1099 /* pcb to be removed is first in list? */
1100 if (udp_pcbs == pcb) {
1101 /* make list start at 2nd pcb */
1102 udp_pcbs = udp_pcbs->next;
1103 /* pcb not 1st in list */
1104 } else {
1105 for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
1106 /* find pcb in udp_pcbs list */
1107 if (pcb2->next != NULL && pcb2->next == pcb) {
1108 /* remove pcb from list */
1109 pcb2->next = pcb->next;
1110 break;
1111 }
1112 }
1113 }
1114 memp_free(MEMP_UDP_PCB, pcb);
1115 }
1116
1117 /**
1118 * @ingroup udp_raw
1119 * Create a UDP PCB.
1120 *
1121 * @return The UDP PCB which was created. NULL if the PCB data structure
1122 * could not be allocated.
1123 *
1124 * @see udp_remove()
1125 */
1126 struct udp_pcb *
udp_new(void)1127 udp_new(void)
1128 {
1129 struct udp_pcb *pcb;
1130 pcb = (struct udp_pcb *)memp_malloc(MEMP_UDP_PCB);
1131 /* could allocate UDP PCB? */
1132 if (pcb != NULL) {
1133 /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0
1134 * which means checksum is generated over the whole datagram per default
1135 * (recommended as default by RFC 3828). */
1136 /* initialize PCB to all zeroes */
1137 memset(pcb, 0, sizeof(struct udp_pcb));
1138 pcb->ttl = UDP_TTL;
1139 #if LWIP_MULTICAST_TX_OPTIONS
1140 udp_set_multicast_ttl(pcb, UDP_TTL);
1141 #endif /* LWIP_MULTICAST_TX_OPTIONS */
1142 }
1143 return pcb;
1144 }
1145
1146 /**
1147 * @ingroup udp_raw
1148 * Create a UDP PCB for specific IP type.
1149 *
1150 * @param type IP address type, see @ref lwip_ip_addr_type definitions.
1151 * If you want to listen to IPv4 and IPv6 (dual-stack) packets,
1152 * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE.
1153 * @return The UDP PCB which was created. NULL if the PCB data structure
1154 * could not be allocated.
1155 *
1156 * @see udp_remove()
1157 */
1158 struct udp_pcb *
udp_new_ip_type(u8_t type)1159 udp_new_ip_type(u8_t type)
1160 {
1161 struct udp_pcb *pcb;
1162 pcb = udp_new();
1163 #if LWIP_IPV4 && LWIP_IPV6
1164 if (pcb != NULL) {
1165 IP_SET_TYPE_VAL(pcb->local_ip, type);
1166 IP_SET_TYPE_VAL(pcb->remote_ip, type);
1167 }
1168 #else
1169 LWIP_UNUSED_ARG(type);
1170 #endif /* LWIP_IPV4 && LWIP_IPV6 */
1171 return pcb;
1172 }
1173
1174 /** This function is called from netif.c when address is changed
1175 *
1176 * @param old_addr IP address of the netif before change
1177 * @param new_addr IP address of the netif after change
1178 */
udp_netif_ip_addr_changed(const ip_addr_t * old_addr,const ip_addr_t * new_addr)1179 void udp_netif_ip_addr_changed(const ip_addr_t* old_addr, const ip_addr_t* new_addr)
1180 {
1181 struct udp_pcb* upcb;
1182
1183 if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) {
1184 for (upcb = udp_pcbs; upcb != NULL; upcb = upcb->next) {
1185 /* PCB bound to current local interface address? */
1186 if (ip_addr_cmp(&upcb->local_ip, old_addr)) {
1187 /* The PCB is bound to the old ipaddr and
1188 * is set to bound to the new one instead */
1189 ip_addr_copy(upcb->local_ip, *new_addr);
1190 }
1191 }
1192 }
1193 }
1194
1195 #if UDP_DEBUG
1196 /**
1197 * Print UDP header information for debug purposes.
1198 *
1199 * @param udphdr pointer to the udp header in memory.
1200 */
1201 void
udp_debug_print(struct udp_hdr * udphdr)1202 udp_debug_print(struct udp_hdr *udphdr)
1203 {
1204 LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
1205 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
1206 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
1207 lwip_ntohs(udphdr->src), lwip_ntohs(udphdr->dest)));
1208 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
1209 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n",
1210 lwip_ntohs(udphdr->len), lwip_ntohs(udphdr->chksum)));
1211 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
1212 }
1213 #endif /* UDP_DEBUG */
1214
1215 #endif /* LWIP_UDP */
1216