source: azure_iot_hub_f767zi/trunk/asp_baseplatform/lwip/lwip-2.1.2/src/core/udp.c@ 457

Last change on this file since 457 was 457, checked in by coas-nagasima, 4 years ago

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 43.0 KB
Line 
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 api
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) & (u16_t)~UDP_LOCAL_PORT_RANGE_START) + UDP_LOCAL_PORT_RANGE_START))
74#endif
75
76/* last local UDP port */
77static u16_t udp_port = UDP_LOCAL_PORT_RANGE_START;
78
79/* The list of UDP PCBs */
80/* exported in udp.h (was static) */
81struct udp_pcb *udp_pcbs;
82
83/**
84 * Initialize this module.
85 */
86void
87udp_init(void)
88{
89#ifdef LWIP_RAND
90 udp_port = UDP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND());
91#endif /* LWIP_RAND */
92}
93
94/**
95 * Allocate a new local UDP port.
96 *
97 * @return a new (free) local UDP port number
98 */
99static u16_t
100udp_new_port(void)
101{
102 u16_t n = 0;
103 struct udp_pcb *pcb;
104
105again:
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}
120
121/** Common code to see if the current input packet matches the pcb
122 * (current input packet is accessed via ip(4/6)_current_* macros)
123 *
124 * @param pcb pcb to check
125 * @param inp network interface on which the datagram was received (only used for IPv4)
126 * @param broadcast 1 if his is an IPv4 broadcast (global or subnet-only), 0 otherwise (only used for IPv4)
127 * @return 1 on match, 0 otherwise
128 */
129static u8_t
130udp_input_local_match(struct udp_pcb *pcb, struct netif *inp, u8_t broadcast)
131{
132 LWIP_UNUSED_ARG(inp); /* in IPv6 only case */
133 LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */
134
135 LWIP_ASSERT("udp_input_local_match: invalid pcb", pcb != NULL);
136 LWIP_ASSERT("udp_input_local_match: invalid netif", inp != NULL);
137
138 /* check if PCB is bound to specific netif */
139 if ((pcb->netif_idx != NETIF_NO_INDEX) &&
140 (pcb->netif_idx != netif_get_index(ip_data.current_input_netif))) {
141 return 0;
142 }
143
144 /* Dual-stack: PCBs listening to any IP type also listen to any IP address */
145 if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
146#if LWIP_IPV4 && IP_SOF_BROADCAST_RECV
147 if ((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) {
148 return 0;
149 }
150#endif /* LWIP_IPV4 && IP_SOF_BROADCAST_RECV */
151 return 1;
152 }
153
154 /* Only need to check PCB if incoming IP version matches PCB IP version */
155 if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) {
156#if LWIP_IPV4
157 /* Special case: IPv4 broadcast: all or broadcasts in my subnet
158 * Note: broadcast variable can only be 1 if it is an IPv4 broadcast */
159 if (broadcast != 0) {
160#if IP_SOF_BROADCAST_RECV
161 if (ip_get_option(pcb, SOF_BROADCAST))
162#endif /* IP_SOF_BROADCAST_RECV */
163 {
164 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) ||
165 ((ip4_current_dest_addr()->addr == IPADDR_BROADCAST)) ||
166 ip4_addr_netcmp(ip_2_ip4(&pcb->local_ip), ip4_current_dest_addr(), netif_ip4_netmask(inp))) {
167 return 1;
168 }
169 }
170 } else
171#endif /* LWIP_IPV4 */
172 /* Handle IPv4 and IPv6: all or exact match */
173 if (ip_addr_isany(&pcb->local_ip) || ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
174 return 1;
175 }
176 }
177
178 return 0;
179}
180
181/**
182 * Process an incoming UDP datagram.
183 *
184 * Given an incoming UDP datagram (as a chain of pbufs) this function
185 * finds a corresponding UDP PCB and hands over the pbuf to the pcbs
186 * recv function. If no pcb is found or the datagram is incorrect, the
187 * pbuf is freed.
188 *
189 * @param p pbuf to be demultiplexed to a UDP PCB (p->payload pointing to the UDP header)
190 * @param inp network interface on which the datagram was received.
191 *
192 */
193void
194udp_input(struct pbuf *p, struct netif *inp)
195{
196 struct udp_hdr *udphdr;
197 struct udp_pcb *pcb, *prev;
198 struct udp_pcb *uncon_pcb;
199 u16_t src, dest;
200 u8_t broadcast;
201 u8_t for_us = 0;
202
203 LWIP_UNUSED_ARG(inp);
204
205 LWIP_ASSERT_CORE_LOCKED();
206
207 LWIP_ASSERT("udp_input: invalid pbuf", p != NULL);
208 LWIP_ASSERT("udp_input: invalid netif", inp != NULL);
209
210 PERF_START;
211
212 UDP_STATS_INC(udp.recv);
213
214 /* Check minimum length (UDP header) */
215 if (p->len < UDP_HLEN) {
216 /* drop short packets */
217 LWIP_DEBUGF(UDP_DEBUG,
218 ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len));
219 UDP_STATS_INC(udp.lenerr);
220 UDP_STATS_INC(udp.drop);
221 MIB2_STATS_INC(mib2.udpinerrors);
222 pbuf_free(p);
223 goto end;
224 }
225
226 udphdr = (struct udp_hdr *)p->payload;
227
228 /* is broadcast packet ? */
229 broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif());
230
231 LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len));
232
233 /* convert src and dest ports to host byte order */
234 src = lwip_ntohs(udphdr->src);
235 dest = lwip_ntohs(udphdr->dest);
236
237 udp_debug_print(udphdr);
238
239 /* print the UDP source and destination */
240 LWIP_DEBUGF(UDP_DEBUG, ("udp ("));
241 ip_addr_debug_print_val(UDP_DEBUG, *ip_current_dest_addr());
242 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", lwip_ntohs(udphdr->dest)));
243 ip_addr_debug_print_val(UDP_DEBUG, *ip_current_src_addr());
244 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", lwip_ntohs(udphdr->src)));
245
246 pcb = NULL;
247 prev = NULL;
248 uncon_pcb = NULL;
249 /* Iterate through the UDP pcb list for a matching pcb.
250 * 'Perfect match' pcbs (connected to the remote port & ip address) are
251 * preferred. If no perfect match is found, the first unconnected pcb that
252 * matches the local port and ip address gets the datagram. */
253 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
254 /* print the PCB local and remote address */
255 LWIP_DEBUGF(UDP_DEBUG, ("pcb ("));
256 ip_addr_debug_print_val(UDP_DEBUG, pcb->local_ip);
257 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", pcb->local_port));
258 ip_addr_debug_print_val(UDP_DEBUG, pcb->remote_ip);
259 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", pcb->remote_port));
260
261 /* compare PCB local addr+port to UDP destination addr+port */
262 if ((pcb->local_port == dest) &&
263 (udp_input_local_match(pcb, inp, broadcast) != 0)) {
264 if ((pcb->flags & UDP_FLAGS_CONNECTED) == 0) {
265 if (uncon_pcb == NULL) {
266 /* the first unconnected matching PCB */
267 uncon_pcb = pcb;
268#if LWIP_IPV4
269 } else if (broadcast && ip4_current_dest_addr()->addr == IPADDR_BROADCAST) {
270 /* global broadcast address (only valid for IPv4; match was checked before) */
271 if (!IP_IS_V4_VAL(uncon_pcb->local_ip) || !ip4_addr_cmp(ip_2_ip4(&uncon_pcb->local_ip), netif_ip4_addr(inp))) {
272 /* uncon_pcb does not match the input netif, check this pcb */
273 if (IP_IS_V4_VAL(pcb->local_ip) && ip4_addr_cmp(ip_2_ip4(&pcb->local_ip), netif_ip4_addr(inp))) {
274 /* better match */
275 uncon_pcb = pcb;
276 }
277 }
278#endif /* LWIP_IPV4 */
279 }
280#if SO_REUSE
281 else if (!ip_addr_isany(&pcb->local_ip)) {
282 /* prefer specific IPs over catch-all */
283 uncon_pcb = pcb;
284 }
285#endif /* SO_REUSE */
286 }
287
288 /* compare PCB remote addr+port to UDP source addr+port */
289 if ((pcb->remote_port == src) &&
290 (ip_addr_isany_val(pcb->remote_ip) ||
291 ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()))) {
292 /* the first fully matching PCB */
293 if (prev != NULL) {
294 /* move the pcb to the front of udp_pcbs so that is
295 found faster next time */
296 prev->next = pcb->next;
297 pcb->next = udp_pcbs;
298 udp_pcbs = pcb;
299 } else {
300 UDP_STATS_INC(udp.cachehit);
301 }
302 break;
303 }
304 }
305
306 prev = pcb;
307 }
308 /* no fully matching pcb found? then look for an unconnected pcb */
309 if (pcb == NULL) {
310 pcb = uncon_pcb;
311 }
312
313 /* Check checksum if this is a match or if it was directed at us. */
314 if (pcb != NULL) {
315 for_us = 1;
316 } else {
317#if LWIP_IPV6
318 if (ip_current_is_v6()) {
319 for_us = netif_get_ip6_addr_match(inp, ip6_current_dest_addr()) >= 0;
320 }
321#endif /* LWIP_IPV6 */
322#if LWIP_IPV4
323 if (!ip_current_is_v6()) {
324 for_us = ip4_addr_cmp(netif_ip4_addr(inp), ip4_current_dest_addr());
325 }
326#endif /* LWIP_IPV4 */
327 }
328
329 if (for_us) {
330 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n"));
331#if CHECKSUM_CHECK_UDP
332 IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_UDP) {
333#if LWIP_UDPLITE
334 if (ip_current_header_proto() == IP_PROTO_UDPLITE) {
335 /* Do the UDP Lite checksum */
336 u16_t chklen = lwip_ntohs(udphdr->len);
337 if (chklen < sizeof(struct udp_hdr)) {
338 if (chklen == 0) {
339 /* For UDP-Lite, checksum length of 0 means checksum
340 over the complete packet (See RFC 3828 chap. 3.1) */
341 chklen = p->tot_len;
342 } else {
343 /* At least the UDP-Lite header must be covered by the
344 checksum! (Again, see RFC 3828 chap. 3.1) */
345 goto chkerr;
346 }
347 }
348 if (ip_chksum_pseudo_partial(p, IP_PROTO_UDPLITE,
349 p->tot_len, chklen,
350 ip_current_src_addr(), ip_current_dest_addr()) != 0) {
351 goto chkerr;
352 }
353 } else
354#endif /* LWIP_UDPLITE */
355 {
356 if (udphdr->chksum != 0) {
357 if (ip_chksum_pseudo(p, IP_PROTO_UDP, p->tot_len,
358 ip_current_src_addr(),
359 ip_current_dest_addr()) != 0) {
360 goto chkerr;
361 }
362 }
363 }
364 }
365#endif /* CHECKSUM_CHECK_UDP */
366 if (pbuf_remove_header(p, UDP_HLEN)) {
367 /* Can we cope with this failing? Just assert for now */
368 LWIP_ASSERT("pbuf_remove_header failed\n", 0);
369 UDP_STATS_INC(udp.drop);
370 MIB2_STATS_INC(mib2.udpinerrors);
371 pbuf_free(p);
372 goto end;
373 }
374
375 if (pcb != NULL) {
376 MIB2_STATS_INC(mib2.udpindatagrams);
377#if SO_REUSE && SO_REUSE_RXTOALL
378 if (ip_get_option(pcb, SOF_REUSEADDR) &&
379 (broadcast || ip_addr_ismulticast(ip_current_dest_addr()))) {
380 /* pass broadcast- or multicast packets to all multicast pcbs
381 if SOF_REUSEADDR is set on the first match */
382 struct udp_pcb *mpcb;
383 for (mpcb = udp_pcbs; mpcb != NULL; mpcb = mpcb->next) {
384 if (mpcb != pcb) {
385 /* compare PCB local addr+port to UDP destination addr+port */
386 if ((mpcb->local_port == dest) &&
387 (udp_input_local_match(mpcb, inp, broadcast) != 0)) {
388 /* pass a copy of the packet to all local matches */
389 if (mpcb->recv != NULL) {
390 struct pbuf *q;
391 q = pbuf_clone(PBUF_RAW, PBUF_POOL, p);
392 if (q != NULL) {
393 mpcb->recv(mpcb->recv_arg, mpcb, q, ip_current_src_addr(), src);
394 }
395 }
396 }
397 }
398 }
399 }
400#endif /* SO_REUSE && SO_REUSE_RXTOALL */
401 /* callback */
402 if (pcb->recv != NULL) {
403 /* now the recv function is responsible for freeing p */
404 pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr(), src);
405 } else {
406 /* no recv function registered? then we have to free the pbuf! */
407 pbuf_free(p);
408 goto end;
409 }
410 } else {
411 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n"));
412
413#if LWIP_ICMP || LWIP_ICMP6
414 /* No match was found, send ICMP destination port unreachable unless
415 destination address was broadcast/multicast. */
416 if (!broadcast && !ip_addr_ismulticast(ip_current_dest_addr())) {
417 /* move payload pointer back to ip header */
418 pbuf_header_force(p, (s16_t)(ip_current_header_tot_len() + UDP_HLEN));
419 icmp_port_unreach(ip_current_is_v6(), p);
420 }
421#endif /* LWIP_ICMP || LWIP_ICMP6 */
422 UDP_STATS_INC(udp.proterr);
423 UDP_STATS_INC(udp.drop);
424 MIB2_STATS_INC(mib2.udpnoports);
425 pbuf_free(p);
426 }
427 } else {
428 pbuf_free(p);
429 }
430end:
431 PERF_STOP("udp_input");
432 return;
433#if CHECKSUM_CHECK_UDP
434chkerr:
435 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
436 ("udp_input: UDP (or UDP Lite) datagram discarded due to failing checksum\n"));
437 UDP_STATS_INC(udp.chkerr);
438 UDP_STATS_INC(udp.drop);
439 MIB2_STATS_INC(mib2.udpinerrors);
440 pbuf_free(p);
441 PERF_STOP("udp_input");
442#endif /* CHECKSUM_CHECK_UDP */
443}
444
445/**
446 * @ingroup udp_raw
447 * Sends the pbuf p using UDP. The pbuf is not deallocated.
448 *
449 *
450 * @param pcb UDP PCB used to send the data.
451 * @param p chain of pbuf's to be sent.
452 *
453 * The datagram will be sent to the current remote_ip & remote_port
454 * stored in pcb. If the pcb is not bound to a port, it will
455 * automatically be bound to a random port.
456 *
457 * @return lwIP error code.
458 * - ERR_OK. Successful. No error occurred.
459 * - ERR_MEM. Out of memory.
460 * - ERR_RTE. Could not find route to destination address.
461 * - ERR_VAL. No PCB or PCB is dual-stack
462 * - More errors could be returned by lower protocol layers.
463 *
464 * @see udp_disconnect() udp_sendto()
465 */
466err_t
467udp_send(struct udp_pcb *pcb, struct pbuf *p)
468{
469 LWIP_ERROR("udp_send: invalid pcb", pcb != NULL, return ERR_ARG);
470 LWIP_ERROR("udp_send: invalid pbuf", p != NULL, return ERR_ARG);
471
472 if (IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) {
473 return ERR_VAL;
474 }
475
476 /* send to the packet using remote ip and port stored in the pcb */
477 return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port);
478}
479
480#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
481/** @ingroup udp_raw
482 * Same as udp_send() but with checksum
483 */
484err_t
485udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p,
486 u8_t have_chksum, u16_t chksum)
487{
488 LWIP_ERROR("udp_send_chksum: invalid pcb", pcb != NULL, return ERR_ARG);
489 LWIP_ERROR("udp_send_chksum: invalid pbuf", p != NULL, return ERR_ARG);
490
491 if (IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) {
492 return ERR_VAL;
493 }
494
495 /* send to the packet using remote ip and port stored in the pcb */
496 return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port,
497 have_chksum, chksum);
498}
499#endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
500
501/**
502 * @ingroup udp_raw
503 * Send data to a specified address using UDP.
504 *
505 * @param pcb UDP PCB used to send the data.
506 * @param p chain of pbuf's to be sent.
507 * @param dst_ip Destination IP address.
508 * @param dst_port Destination UDP port.
509 *
510 * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
511 *
512 * If the PCB already has a remote address association, it will
513 * be restored after the data is sent.
514 *
515 * @return lwIP error code (@see udp_send for possible error codes)
516 *
517 * @see udp_disconnect() udp_send()
518 */
519err_t
520udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
521 const ip_addr_t *dst_ip, u16_t dst_port)
522{
523#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
524 return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0);
525}
526
527/** @ingroup udp_raw
528 * Same as udp_sendto(), but with checksum */
529err_t
530udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
531 u16_t dst_port, u8_t have_chksum, u16_t chksum)
532{
533#endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
534 struct netif *netif;
535
536 LWIP_ERROR("udp_sendto: invalid pcb", pcb != NULL, return ERR_ARG);
537 LWIP_ERROR("udp_sendto: invalid pbuf", p != NULL, return ERR_ARG);
538 LWIP_ERROR("udp_sendto: invalid dst_ip", dst_ip != NULL, return ERR_ARG);
539
540 if (!IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
541 return ERR_VAL;
542 }
543
544 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send\n"));
545
546 if (pcb->netif_idx != NETIF_NO_INDEX) {
547 netif = netif_get_by_index(pcb->netif_idx);
548 } else {
549#if LWIP_MULTICAST_TX_OPTIONS
550 netif = NULL;
551 if (ip_addr_ismulticast(dst_ip)) {
552 /* For IPv6, the interface to use for packets with a multicast destination
553 * is specified using an interface index. The same approach may be used for
554 * IPv4 as well, in which case it overrides the IPv4 multicast override
555 * address below. Here we have to look up the netif by going through the
556 * list, but by doing so we skip a route lookup. If the interface index has
557 * gone stale, we fall through and do the regular route lookup after all. */
558 if (pcb->mcast_ifindex != NETIF_NO_INDEX) {
559 netif = netif_get_by_index(pcb->mcast_ifindex);
560 }
561#if LWIP_IPV4
562 else
563#if LWIP_IPV6
564 if (IP_IS_V4(dst_ip))
565#endif /* LWIP_IPV6 */
566 {
567 /* IPv4 does not use source-based routing by default, so we use an
568 administratively selected interface for multicast by default.
569 However, this can be overridden by setting an interface address
570 in pcb->mcast_ip4 that is used for routing. If this routing lookup
571 fails, we try regular routing as though no override was set. */
572 if (!ip4_addr_isany_val(pcb->mcast_ip4) &&
573 !ip4_addr_cmp(&pcb->mcast_ip4, IP4_ADDR_BROADCAST)) {
574 netif = ip4_route_src(ip_2_ip4(&pcb->local_ip), &pcb->mcast_ip4);
575 }
576 }
577#endif /* LWIP_IPV4 */
578 }
579
580 if (netif == NULL)
581#endif /* LWIP_MULTICAST_TX_OPTIONS */
582 {
583 /* find the outgoing network interface for this packet */
584 netif = ip_route(&pcb->local_ip, dst_ip);
585 }
586 }
587
588 /* no outgoing network interface could be found? */
589 if (netif == NULL) {
590 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: No route to "));
591 ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, dst_ip);
592 LWIP_DEBUGF(UDP_DEBUG, ("\n"));
593 UDP_STATS_INC(udp.rterr);
594 return ERR_RTE;
595 }
596#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
597 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum);
598#else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
599 return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
600#endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
601}
602
603/**
604 * @ingroup udp_raw
605 * Send data to a specified address using UDP.
606 * The netif used for sending can be specified.
607 *
608 * This function exists mainly for DHCP, to be able to send UDP packets
609 * on a netif that is still down.
610 *
611 * @param pcb UDP PCB used to send the data.
612 * @param p chain of pbuf's to be sent.
613 * @param dst_ip Destination IP address.
614 * @param dst_port Destination UDP port.
615 * @param netif the netif used for sending.
616 *
617 * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
618 *
619 * @return lwIP error code (@see udp_send for possible error codes)
620 *
621 * @see udp_disconnect() udp_send()
622 */
623err_t
624udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p,
625 const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif)
626{
627#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
628 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0);
629}
630
631/** Same as udp_sendto_if(), but with checksum */
632err_t
633udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
634 u16_t dst_port, struct netif *netif, u8_t have_chksum,
635 u16_t chksum)
636{
637#endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
638 const ip_addr_t *src_ip;
639
640 LWIP_ERROR("udp_sendto_if: invalid pcb", pcb != NULL, return ERR_ARG);
641 LWIP_ERROR("udp_sendto_if: invalid pbuf", p != NULL, return ERR_ARG);
642 LWIP_ERROR("udp_sendto_if: invalid dst_ip", dst_ip != NULL, return ERR_ARG);
643 LWIP_ERROR("udp_sendto_if: invalid netif", netif != NULL, return ERR_ARG);
644
645 if (!IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
646 return ERR_VAL;
647 }
648
649 /* PCB local address is IP_ANY_ADDR or multicast? */
650#if LWIP_IPV6
651 if (IP_IS_V6(dst_ip)) {
652 if (ip6_addr_isany(ip_2_ip6(&pcb->local_ip)) ||
653 ip6_addr_ismulticast(ip_2_ip6(&pcb->local_ip))) {
654 src_ip = ip6_select_source_address(netif, ip_2_ip6(dst_ip));
655 if (src_ip == NULL) {
656 /* No suitable source address was found. */
657 return ERR_RTE;
658 }
659 } else {
660 /* use UDP PCB local IPv6 address as source address, if still valid. */
661 if (netif_get_ip6_addr_match(netif, ip_2_ip6(&pcb->local_ip)) < 0) {
662 /* Address isn't valid anymore. */
663 return ERR_RTE;
664 }
665 src_ip = &pcb->local_ip;
666 }
667 }
668#endif /* LWIP_IPV6 */
669#if LWIP_IPV4 && LWIP_IPV6
670 else
671#endif /* LWIP_IPV4 && LWIP_IPV6 */
672#if LWIP_IPV4
673 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) ||
674 ip4_addr_ismulticast(ip_2_ip4(&pcb->local_ip))) {
675 /* if the local_ip is any or multicast
676 * use the outgoing network interface IP address as source address */
677 src_ip = netif_ip_addr4(netif);
678 } else {
679 /* check if UDP PCB local IP address is correct
680 * this could be an old address if netif->ip_addr has changed */
681 if (!ip4_addr_cmp(ip_2_ip4(&(pcb->local_ip)), netif_ip4_addr(netif))) {
682 /* local_ip doesn't match, drop the packet */
683 return ERR_RTE;
684 }
685 /* use UDP PCB local IP address as source address */
686 src_ip = &pcb->local_ip;
687 }
688#endif /* LWIP_IPV4 */
689#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
690 return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum, src_ip);
691#else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
692 return udp_sendto_if_src(pcb, p, dst_ip, dst_port, netif, src_ip);
693#endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
694}
695
696/** @ingroup udp_raw
697 * Same as @ref udp_sendto_if, but with source address */
698err_t
699udp_sendto_if_src(struct udp_pcb *pcb, struct pbuf *p,
700 const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif, const ip_addr_t *src_ip)
701{
702#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
703 return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0, src_ip);
704}
705
706/** Same as udp_sendto_if_src(), but with checksum */
707err_t
708udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
709 u16_t dst_port, struct netif *netif, u8_t have_chksum,
710 u16_t chksum, const ip_addr_t *src_ip)
711{
712#endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
713 struct udp_hdr *udphdr;
714 err_t err;
715 struct pbuf *q; /* q will be sent down the stack */
716 u8_t ip_proto;
717 u8_t ttl;
718
719 LWIP_ASSERT_CORE_LOCKED();
720
721 LWIP_ERROR("udp_sendto_if_src: invalid pcb", pcb != NULL, return ERR_ARG);
722 LWIP_ERROR("udp_sendto_if_src: invalid pbuf", p != NULL, return ERR_ARG);
723 LWIP_ERROR("udp_sendto_if_src: invalid dst_ip", dst_ip != NULL, return ERR_ARG);
724 LWIP_ERROR("udp_sendto_if_src: invalid src_ip", src_ip != NULL, return ERR_ARG);
725 LWIP_ERROR("udp_sendto_if_src: invalid netif", netif != NULL, return ERR_ARG);
726
727 if (!IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) ||
728 !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
729 return ERR_VAL;
730 }
731
732#if LWIP_IPV4 && IP_SOF_BROADCAST
733 /* broadcast filter? */
734 if (!ip_get_option(pcb, SOF_BROADCAST) &&
735#if LWIP_IPV6
736 IP_IS_V4(dst_ip) &&
737#endif /* LWIP_IPV6 */
738 ip_addr_isbroadcast(dst_ip, netif)) {
739 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
740 ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
741 return ERR_VAL;
742 }
743#endif /* LWIP_IPV4 && IP_SOF_BROADCAST */
744
745 /* if the PCB is not yet bound to a port, bind it here */
746 if (pcb->local_port == 0) {
747 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n"));
748 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
749 if (err != ERR_OK) {
750 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n"));
751 return err;
752 }
753 }
754
755 /* packet too large to add a UDP header without causing an overflow? */
756 if ((u16_t)(p->tot_len + UDP_HLEN) < p->tot_len) {
757 return ERR_MEM;
758 }
759 /* not enough space to add an UDP header to first pbuf in given p chain? */
760 if (pbuf_add_header(p, UDP_HLEN)) {
761 /* allocate header in a separate new pbuf */
762 q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
763 /* new header pbuf could not be allocated? */
764 if (q == NULL) {
765 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n"));
766 return ERR_MEM;
767 }
768 if (p->tot_len != 0) {
769 /* chain header q in front of given pbuf p (only if p contains data) */
770 pbuf_chain(q, p);
771 }
772 /* first pbuf q points to header pbuf */
773 LWIP_DEBUGF(UDP_DEBUG,
774 ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
775 } else {
776 /* adding space for header within p succeeded */
777 /* first pbuf q equals given pbuf */
778 q = p;
779 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
780 }
781 LWIP_ASSERT("check that first pbuf can hold struct udp_hdr",
782 (q->len >= sizeof(struct udp_hdr)));
783 /* q now represents the packet to be sent */
784 udphdr = (struct udp_hdr *)q->payload;
785 udphdr->src = lwip_htons(pcb->local_port);
786 udphdr->dest = lwip_htons(dst_port);
787 /* in UDP, 0 checksum means 'no checksum' */
788 udphdr->chksum = 0x0000;
789
790 /* Multicast Loop? */
791#if LWIP_MULTICAST_TX_OPTIONS
792 if (((pcb->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) {
793 q->flags |= PBUF_FLAG_MCASTLOOP;
794 }
795#endif /* LWIP_MULTICAST_TX_OPTIONS */
796
797 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));
798
799#if LWIP_UDPLITE
800 /* UDP Lite protocol? */
801 if (pcb->flags & UDP_FLAGS_UDPLITE) {
802 u16_t chklen, chklen_hdr;
803 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));
804 /* set UDP message length in UDP header */
805 chklen_hdr = chklen = pcb->chksum_len_tx;
806 if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) {
807 if (chklen != 0) {
808 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen));
809 }
810 /* For UDP-Lite, checksum length of 0 means checksum
811 over the complete packet. (See RFC 3828 chap. 3.1)
812 At least the UDP-Lite header must be covered by the
813 checksum, therefore, if chksum_len has an illegal
814 value, we generate the checksum over the complete
815 packet to be safe. */
816 chklen_hdr = 0;
817 chklen = q->tot_len;
818 }
819 udphdr->len = lwip_htons(chklen_hdr);
820 /* calculate checksum */
821#if CHECKSUM_GEN_UDP
822 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) {
823#if LWIP_CHECKSUM_ON_COPY
824 if (have_chksum) {
825 chklen = UDP_HLEN;
826 }
827#endif /* LWIP_CHECKSUM_ON_COPY */
828 udphdr->chksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDPLITE,
829 q->tot_len, chklen, src_ip, dst_ip);
830#if LWIP_CHECKSUM_ON_COPY
831 if (have_chksum) {
832 u32_t acc;
833 acc = udphdr->chksum + (u16_t)~(chksum);
834 udphdr->chksum = FOLD_U32T(acc);
835 }
836#endif /* LWIP_CHECKSUM_ON_COPY */
837
838 /* chksum zero must become 0xffff, as zero means 'no checksum' */
839 if (udphdr->chksum == 0x0000) {
840 udphdr->chksum = 0xffff;
841 }
842 }
843#endif /* CHECKSUM_GEN_UDP */
844
845 ip_proto = IP_PROTO_UDPLITE;
846 } else
847#endif /* LWIP_UDPLITE */
848 { /* UDP */
849 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));
850 udphdr->len = lwip_htons(q->tot_len);
851 /* calculate checksum */
852#if CHECKSUM_GEN_UDP
853 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) {
854 /* Checksum is mandatory over IPv6. */
855 if (IP_IS_V6(dst_ip) || (pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
856 u16_t udpchksum;
857#if LWIP_CHECKSUM_ON_COPY
858 if (have_chksum) {
859 u32_t acc;
860 udpchksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDP,
861 q->tot_len, UDP_HLEN, src_ip, dst_ip);
862 acc = udpchksum + (u16_t)~(chksum);
863 udpchksum = FOLD_U32T(acc);
864 } else
865#endif /* LWIP_CHECKSUM_ON_COPY */
866 {
867 udpchksum = ip_chksum_pseudo(q, IP_PROTO_UDP, q->tot_len,
868 src_ip, dst_ip);
869 }
870
871 /* chksum zero must become 0xffff, as zero means 'no checksum' */
872 if (udpchksum == 0x0000) {
873 udpchksum = 0xffff;
874 }
875 udphdr->chksum = udpchksum;
876 }
877 }
878#endif /* CHECKSUM_GEN_UDP */
879 ip_proto = IP_PROTO_UDP;
880 }
881
882 /* Determine TTL to use */
883#if LWIP_MULTICAST_TX_OPTIONS
884 ttl = (ip_addr_ismulticast(dst_ip) ? udp_get_multicast_ttl(pcb) : pcb->ttl);
885#else /* LWIP_MULTICAST_TX_OPTIONS */
886 ttl = pcb->ttl;
887#endif /* LWIP_MULTICAST_TX_OPTIONS */
888
889 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));
890 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,0x%02"X16_F",)\n", (u16_t)ip_proto));
891 /* output to IP */
892 NETIF_SET_HINTS(netif, &(pcb->netif_hints));
893 err = ip_output_if_src(q, src_ip, dst_ip, ttl, pcb->tos, ip_proto, netif);
894 NETIF_RESET_HINTS(netif);
895
896 /* @todo: must this be increased even if error occurred? */
897 MIB2_STATS_INC(mib2.udpoutdatagrams);
898
899 /* did we chain a separate header pbuf earlier? */
900 if (q != p) {
901 /* free the header pbuf */
902 pbuf_free(q);
903 q = NULL;
904 /* p is still referenced by the caller, and will live on */
905 }
906
907 UDP_STATS_INC(udp.xmit);
908 return err;
909}
910
911/**
912 * @ingroup udp_raw
913 * Bind an UDP PCB.
914 *
915 * @param pcb UDP PCB to be bound with a local address ipaddr and port.
916 * @param ipaddr local IP address to bind with. Use IP_ANY_TYPE to
917 * bind to all local interfaces.
918 * @param port local UDP port to bind with. Use 0 to automatically bind
919 * to a random port between UDP_LOCAL_PORT_RANGE_START and
920 * UDP_LOCAL_PORT_RANGE_END.
921 *
922 * ipaddr & port are expected to be in the same byte order as in the pcb.
923 *
924 * @return lwIP error code.
925 * - ERR_OK. Successful. No error occurred.
926 * - ERR_USE. The specified ipaddr and port are already bound to by
927 * another UDP PCB.
928 *
929 * @see udp_disconnect()
930 */
931err_t
932udp_bind(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
933{
934 struct udp_pcb *ipcb;
935 u8_t rebind;
936#if LWIP_IPV6 && LWIP_IPV6_SCOPES
937 ip_addr_t zoned_ipaddr;
938#endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
939
940 LWIP_ASSERT_CORE_LOCKED();
941
942#if LWIP_IPV4
943 /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
944 if (ipaddr == NULL) {
945 ipaddr = IP4_ADDR_ANY;
946 }
947#else /* LWIP_IPV4 */
948 LWIP_ERROR("udp_bind: invalid ipaddr", ipaddr != NULL, return ERR_ARG);
949#endif /* LWIP_IPV4 */
950
951 LWIP_ERROR("udp_bind: invalid pcb", pcb != NULL, return ERR_ARG);
952
953 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = "));
954 ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE, ipaddr);
955 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port));
956
957 rebind = 0;
958 /* Check for double bind and rebind of the same pcb */
959 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
960 /* is this UDP PCB already on active list? */
961 if (pcb == ipcb) {
962 rebind = 1;
963 break;
964 }
965 }
966
967#if LWIP_IPV6 && LWIP_IPV6_SCOPES
968 /* If the given IP address should have a zone but doesn't, assign one now.
969 * This is legacy support: scope-aware callers should always provide properly
970 * zoned source addresses. Do the zone selection before the address-in-use
971 * check below; as such we have to make a temporary copy of the address. */
972 if (IP_IS_V6(ipaddr) && ip6_addr_lacks_zone(ip_2_ip6(ipaddr), IP6_UNKNOWN)) {
973 ip_addr_copy(zoned_ipaddr, *ipaddr);
974 ip6_addr_select_zone(ip_2_ip6(&zoned_ipaddr), ip_2_ip6(&zoned_ipaddr));
975 ipaddr = &zoned_ipaddr;
976 }
977#endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
978
979 /* no port specified? */
980 if (port == 0) {
981 port = udp_new_port();
982 if (port == 0) {
983 /* no more ports available in local range */
984 LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
985 return ERR_USE;
986 }
987 } else {
988 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
989 if (pcb != ipcb) {
990 /* By default, we don't allow to bind to a port that any other udp
991 PCB is already bound to, unless *all* PCBs with that port have tha
992 REUSEADDR flag set. */
993#if SO_REUSE
994 if (!ip_get_option(pcb, SOF_REUSEADDR) ||
995 !ip_get_option(ipcb, SOF_REUSEADDR))
996#endif /* SO_REUSE */
997 {
998 /* port matches that of PCB in list and REUSEADDR not set -> reject */
999 if ((ipcb->local_port == port) &&
1000 /* IP address matches or any IP used? */
1001 (ip_addr_cmp(&ipcb->local_ip, ipaddr) || ip_addr_isany(ipaddr) ||
1002 ip_addr_isany(&ipcb->local_ip))) {
1003 /* other PCB already binds to this local IP and port */
1004 LWIP_DEBUGF(UDP_DEBUG,
1005 ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));
1006 return ERR_USE;
1007 }
1008 }
1009 }
1010 }
1011 }
1012
1013 ip_addr_set_ipaddr(&pcb->local_ip, ipaddr);
1014
1015 pcb->local_port = port;
1016 mib2_udp_bind(pcb);
1017 /* pcb not active yet? */
1018 if (rebind == 0) {
1019 /* place the PCB on the active list if not already there */
1020 pcb->next = udp_pcbs;
1021 udp_pcbs = pcb;
1022 }
1023 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_bind: bound to "));
1024 ip_addr_debug_print_val(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, pcb->local_ip);
1025 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->local_port));
1026 return ERR_OK;
1027}
1028
1029/**
1030 * @ingroup udp_raw
1031 * Bind an UDP PCB to a specific netif.
1032 * After calling this function, all packets received via this PCB
1033 * are guaranteed to have come in via the specified netif, and all
1034 * outgoing packets will go out via the specified netif.
1035 *
1036 * @param pcb UDP PCB to be bound.
1037 * @param netif netif to bind udp pcb to. Can be NULL.
1038 *
1039 * @see udp_disconnect()
1040 */
1041void
1042udp_bind_netif(struct udp_pcb *pcb, const struct netif *netif)
1043{
1044 LWIP_ASSERT_CORE_LOCKED();
1045
1046 if (netif != NULL) {
1047 pcb->netif_idx = netif_get_index(netif);
1048 } else {
1049 pcb->netif_idx = NETIF_NO_INDEX;
1050 }
1051}
1052
1053/**
1054 * @ingroup udp_raw
1055 * Sets the remote end of the pcb. This function does not generate any
1056 * network traffic, but only sets the remote address of the pcb.
1057 *
1058 * @param pcb UDP PCB to be connected with remote address ipaddr and port.
1059 * @param ipaddr remote IP address to connect with.
1060 * @param port remote UDP port to connect with.
1061 *
1062 * @return lwIP error code
1063 *
1064 * ipaddr & port are expected to be in the same byte order as in the pcb.
1065 *
1066 * The udp pcb is bound to a random local port if not already bound.
1067 *
1068 * @see udp_disconnect()
1069 */
1070err_t
1071udp_connect(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
1072{
1073 struct udp_pcb *ipcb;
1074
1075 LWIP_ASSERT_CORE_LOCKED();
1076
1077 LWIP_ERROR("udp_connect: invalid pcb", pcb != NULL, return ERR_ARG);
1078 LWIP_ERROR("udp_connect: invalid ipaddr", ipaddr != NULL, return ERR_ARG);
1079
1080 if (pcb->local_port == 0) {
1081 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
1082 if (err != ERR_OK) {
1083 return err;
1084 }
1085 }
1086
1087 ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr);
1088#if LWIP_IPV6 && LWIP_IPV6_SCOPES
1089 /* If the given IP address should have a zone but doesn't, assign one now,
1090 * using the bound address to make a more informed decision when possible. */
1091 if (IP_IS_V6(&pcb->remote_ip) &&
1092 ip6_addr_lacks_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNKNOWN)) {
1093 ip6_addr_select_zone(ip_2_ip6(&pcb->remote_ip), ip_2_ip6(&pcb->local_ip));
1094 }
1095#endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
1096
1097 pcb->remote_port = port;
1098 pcb->flags |= UDP_FLAGS_CONNECTED;
1099
1100 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_connect: connected to "));
1101 ip_addr_debug_print_val(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
1102 pcb->remote_ip);
1103 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->remote_port));
1104
1105 /* Insert UDP PCB into the list of active UDP PCBs. */
1106 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
1107 if (pcb == ipcb) {
1108 /* already on the list, just return */
1109 return ERR_OK;
1110 }
1111 }
1112 /* PCB not yet on the list, add PCB now */
1113 pcb->next = udp_pcbs;
1114 udp_pcbs = pcb;
1115 return ERR_OK;
1116}
1117
1118/**
1119 * @ingroup udp_raw
1120 * Remove the remote end of the pcb. This function does not generate
1121 * any network traffic, but only removes the remote address of the pcb.
1122 *
1123 * @param pcb the udp pcb to disconnect.
1124 */
1125void
1126udp_disconnect(struct udp_pcb *pcb)
1127{
1128 LWIP_ASSERT_CORE_LOCKED();
1129
1130 LWIP_ERROR("udp_disconnect: invalid pcb", pcb != NULL, return);
1131
1132 /* reset remote address association */
1133#if LWIP_IPV4 && LWIP_IPV6
1134 if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
1135 ip_addr_copy(pcb->remote_ip, *IP_ANY_TYPE);
1136 } else {
1137#endif
1138 ip_addr_set_any(IP_IS_V6_VAL(pcb->remote_ip), &pcb->remote_ip);
1139#if LWIP_IPV4 && LWIP_IPV6
1140 }
1141#endif
1142 pcb->remote_port = 0;
1143 pcb->netif_idx = NETIF_NO_INDEX;
1144 /* mark PCB as unconnected */
1145 udp_clear_flags(pcb, UDP_FLAGS_CONNECTED);
1146}
1147
1148/**
1149 * @ingroup udp_raw
1150 * Set a receive callback for a UDP PCB.
1151 * This callback will be called when receiving a datagram for the pcb.
1152 *
1153 * @param pcb the pcb for which to set the recv callback
1154 * @param recv function pointer of the callback function
1155 * @param recv_arg additional argument to pass to the callback function
1156 */
1157void
1158udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg)
1159{
1160 LWIP_ASSERT_CORE_LOCKED();
1161
1162 LWIP_ERROR("udp_recv: invalid pcb", pcb != NULL, return);
1163
1164 /* remember recv() callback and user data */
1165 pcb->recv = recv;
1166 pcb->recv_arg = recv_arg;
1167}
1168
1169/**
1170 * @ingroup udp_raw
1171 * Removes and deallocates the pcb.
1172 *
1173 * @param pcb UDP PCB to be removed. The PCB is removed from the list of
1174 * UDP PCB's and the data structure is freed from memory.
1175 *
1176 * @see udp_new()
1177 */
1178void
1179udp_remove(struct udp_pcb *pcb)
1180{
1181 struct udp_pcb *pcb2;
1182
1183 LWIP_ASSERT_CORE_LOCKED();
1184
1185 LWIP_ERROR("udp_remove: invalid pcb", pcb != NULL, return);
1186
1187 mib2_udp_unbind(pcb);
1188 /* pcb to be removed is first in list? */
1189 if (udp_pcbs == pcb) {
1190 /* make list start at 2nd pcb */
1191 udp_pcbs = udp_pcbs->next;
1192 /* pcb not 1st in list */
1193 } else {
1194 for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
1195 /* find pcb in udp_pcbs list */
1196 if (pcb2->next != NULL && pcb2->next == pcb) {
1197 /* remove pcb from list */
1198 pcb2->next = pcb->next;
1199 break;
1200 }
1201 }
1202 }
1203 memp_free(MEMP_UDP_PCB, pcb);
1204}
1205
1206/**
1207 * @ingroup udp_raw
1208 * Creates a new UDP pcb which can be used for UDP communication. The
1209 * pcb is not active until it has either been bound to a local address
1210 * or connected to a remote address.
1211 *
1212 * @return The UDP PCB which was created. NULL if the PCB data structure
1213 * could not be allocated.
1214 *
1215 * @see udp_remove()
1216 */
1217struct udp_pcb *
1218udp_new(void)
1219{
1220 struct udp_pcb *pcb;
1221
1222 LWIP_ASSERT_CORE_LOCKED();
1223
1224 pcb = (struct udp_pcb *)memp_malloc(MEMP_UDP_PCB);
1225 /* could allocate UDP PCB? */
1226 if (pcb != NULL) {
1227 /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0
1228 * which means checksum is generated over the whole datagram per default
1229 * (recommended as default by RFC 3828). */
1230 /* initialize PCB to all zeroes */
1231 memset(pcb, 0, sizeof(struct udp_pcb));
1232 pcb->ttl = UDP_TTL;
1233#if LWIP_MULTICAST_TX_OPTIONS
1234 udp_set_multicast_ttl(pcb, UDP_TTL);
1235#endif /* LWIP_MULTICAST_TX_OPTIONS */
1236 }
1237 return pcb;
1238}
1239
1240/**
1241 * @ingroup udp_raw
1242 * Create a UDP PCB for specific IP type.
1243 * The pcb is not active until it has either been bound to a local address
1244 * or connected to a remote address.
1245 *
1246 * @param type IP address type, see @ref lwip_ip_addr_type definitions.
1247 * If you want to listen to IPv4 and IPv6 (dual-stack) packets,
1248 * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE.
1249 * @return The UDP PCB which was created. NULL if the PCB data structure
1250 * could not be allocated.
1251 *
1252 * @see udp_remove()
1253 */
1254struct udp_pcb *
1255udp_new_ip_type(u8_t type)
1256{
1257 struct udp_pcb *pcb;
1258
1259 LWIP_ASSERT_CORE_LOCKED();
1260
1261 pcb = udp_new();
1262#if LWIP_IPV4 && LWIP_IPV6
1263 if (pcb != NULL) {
1264 IP_SET_TYPE_VAL(pcb->local_ip, type);
1265 IP_SET_TYPE_VAL(pcb->remote_ip, type);
1266 }
1267#else
1268 LWIP_UNUSED_ARG(type);
1269#endif /* LWIP_IPV4 && LWIP_IPV6 */
1270 return pcb;
1271}
1272
1273/** This function is called from netif.c when address is changed
1274 *
1275 * @param old_addr IP address of the netif before change
1276 * @param new_addr IP address of the netif after change
1277 */
1278void udp_netif_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr)
1279{
1280 struct udp_pcb *upcb;
1281
1282 if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) {
1283 for (upcb = udp_pcbs; upcb != NULL; upcb = upcb->next) {
1284 /* PCB bound to current local interface address? */
1285 if (ip_addr_cmp(&upcb->local_ip, old_addr)) {
1286 /* The PCB is bound to the old ipaddr and
1287 * is set to bound to the new one instead */
1288 ip_addr_copy(upcb->local_ip, *new_addr);
1289 }
1290 }
1291 }
1292}
1293
1294#if UDP_DEBUG
1295/**
1296 * Print UDP header information for debug purposes.
1297 *
1298 * @param udphdr pointer to the udp header in memory.
1299 */
1300void
1301udp_debug_print(struct udp_hdr *udphdr)
1302{
1303 LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
1304 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
1305 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
1306 lwip_ntohs(udphdr->src), lwip_ntohs(udphdr->dest)));
1307 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
1308 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n",
1309 lwip_ntohs(udphdr->len), lwip_ntohs(udphdr->chksum)));
1310 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
1311}
1312#endif /* UDP_DEBUG */
1313
1314#endif /* LWIP_UDP */
Note: See TracBrowser for help on using the repository browser.