3 * User Datagram Protocol module
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
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.
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
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
42 * The code for the User Datagram Protocol UDP & UDPLite (RFC 3828).
46 /* @todo Check the use of '(struct udp_pcb).chksum_len_rx'!
51 #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */
55 #include "lwip/memp.h"
56 #include "lwip/inet_chksum.h"
57 #include "lwip/ip_addr.h"
58 #include "lwip/netif.h"
59 #include "lwip/icmp.h"
60 #include "lwip/stats.h"
61 #include "lwip/snmp.h"
62 #include "arch/perf.h"
63 #include "lwip/dhcp.h"
67 /* The list of UDP PCBs */
68 /* exported in udp.h (was static) */
69 struct udp_pcb *udp_pcbs;
72 * Process an incoming UDP datagram.
74 * Given an incoming UDP datagram (as a chain of pbufs) this function
75 * finds a corresponding UDP PCB and hands over the pbuf to the pcbs
76 * recv function. If no pcb is found or the datagram is incorrect, the
79 * @param p pbuf to be demultiplexed to a UDP PCB.
80 * @param inp network interface on which the datagram was received.
84 udp_input(struct pbuf *p, struct netif *inp)
86 struct udp_hdr *udphdr;
87 struct udp_pcb *pcb, *prev;
88 struct udp_pcb *uncon_pcb;
96 UDP_STATS_INC(udp.recv);
98 iphdr = (struct ip_hdr *)p->payload;
100 /* Check minimum length (IP header + UDP header)
101 * and move payload pointer to UDP header */
102 if (p->tot_len < (IPH_HL(iphdr) * 4 + UDP_HLEN) || pbuf_header(p, -(s16_t)(IPH_HL(iphdr) * 4))) {
103 /* drop short packets */
104 LWIP_DEBUGF(UDP_DEBUG,
105 ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len));
106 UDP_STATS_INC(udp.lenerr);
107 UDP_STATS_INC(udp.drop);
108 snmp_inc_udpinerrors();
113 udphdr = (struct udp_hdr *)p->payload;
115 /* is broadcast packet ? */
116 broadcast = ip_addr_isbroadcast(&(iphdr->dest), inp);
118 LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len));
120 /* convert src and dest ports to host byte order */
121 src = ntohs(udphdr->src);
122 dest = ntohs(udphdr->dest);
124 udp_debug_print(udphdr);
126 /* print the UDP source and destination */
127 LWIP_DEBUGF(UDP_DEBUG,
128 ("udp (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") <-- "
129 "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
130 ip4_addr1_16(&iphdr->dest), ip4_addr2_16(&iphdr->dest),
131 ip4_addr3_16(&iphdr->dest), ip4_addr4_16(&iphdr->dest), ntohs(udphdr->dest),
132 ip4_addr1_16(&iphdr->src), ip4_addr2_16(&iphdr->src),
133 ip4_addr3_16(&iphdr->src), ip4_addr4_16(&iphdr->src), ntohs(udphdr->src)));
137 /* when LWIP_DHCP is active, packets to DHCP_CLIENT_PORT may only be processed by
138 the dhcp module, no other UDP pcb may use the local UDP port DHCP_CLIENT_PORT */
139 if (dest == DHCP_CLIENT_PORT) {
140 /* all packets for DHCP_CLIENT_PORT not coming from DHCP_SERVER_PORT are dropped! */
141 if (src == DHCP_SERVER_PORT) {
142 if ((inp->dhcp != NULL) && (inp->dhcp->pcb != NULL)) {
143 /* accept the packe if
144 (- broadcast or directed to us) -> DHCP is link-layer-addressed, local ip is always ANY!
145 - inp->dhcp->pcb->remote == ANY or iphdr->src */
146 if ((ip_addr_isany(&inp->dhcp->pcb->remote_ip) ||
147 ip_addr_cmp(&(inp->dhcp->pcb->remote_ip), &(iphdr->src)))) {
148 pcb = inp->dhcp->pcb;
153 #endif /* LWIP_DHCP */
158 /* Iterate through the UDP pcb list for a matching pcb.
159 * 'Perfect match' pcbs (connected to the remote port & ip address) are
160 * preferred. If no perfect match is found, the first unconnected pcb that
161 * matches the local port and ip address gets the datagram. */
162 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
164 /* print the PCB local and remote address */
165 LWIP_DEBUGF(UDP_DEBUG,
166 ("pcb (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") --- "
167 "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
168 ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip),
169 ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip), pcb->local_port,
170 ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip),
171 ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip), pcb->remote_port));
173 /* compare PCB local addr+port to UDP destination addr+port */
174 if ((pcb->local_port == dest) &&
175 ((!broadcast && ip_addr_isany(&pcb->local_ip)) ||
176 ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest)) ||
178 ip_addr_ismulticast(&(iphdr->dest)) ||
179 #endif /* LWIP_IGMP */
180 #if IP_SOF_BROADCAST_RECV
181 (broadcast && (pcb->so_options & SOF_BROADCAST)))) {
182 #else /* IP_SOF_BROADCAST_RECV */
184 #endif /* IP_SOF_BROADCAST_RECV */
186 if ((uncon_pcb == NULL) &&
187 ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) {
188 /* the first unconnected matching PCB */
192 /* compare PCB remote addr+port to UDP source addr+port */
193 if ((local_match != 0) &&
194 (pcb->remote_port == src) &&
195 (ip_addr_isany(&pcb->remote_ip) ||
196 ip_addr_cmp(&(pcb->remote_ip), &(iphdr->src)))) {
197 /* the first fully matching PCB */
199 /* move the pcb to the front of udp_pcbs so that is
200 found faster next time */
201 prev->next = pcb->next;
202 pcb->next = udp_pcbs;
205 UDP_STATS_INC(udp.cachehit);
211 /* no fully matching pcb found? then look for an unconnected pcb */
217 /* Check checksum if this is a match or if it was directed at us. */
218 if (pcb != NULL || ip_addr_cmp(&inp->ip_addr, &iphdr->dest)) {
219 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n"));
221 if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) {
222 /* Do the UDP Lite checksum */
223 #if CHECKSUM_CHECK_UDP
224 u16_t chklen = ntohs(udphdr->len);
225 if (chklen < sizeof(struct udp_hdr)) {
227 /* For UDP-Lite, checksum length of 0 means checksum
228 over the complete packet (See RFC 3828 chap. 3.1) */
231 /* At least the UDP-Lite header must be covered by the
232 checksum! (Again, see RFC 3828 chap. 3.1) */
233 UDP_STATS_INC(udp.chkerr);
234 UDP_STATS_INC(udp.drop);
235 snmp_inc_udpinerrors();
240 if (inet_chksum_pseudo_partial(p, &iphdr->src, &iphdr->dest,
241 IP_PROTO_UDPLITE, p->tot_len, chklen) != 0) {
242 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
243 ("udp_input: UDP Lite datagram discarded due to failing checksum\n"));
244 UDP_STATS_INC(udp.chkerr);
245 UDP_STATS_INC(udp.drop);
246 snmp_inc_udpinerrors();
250 #endif /* CHECKSUM_CHECK_UDP */
252 #endif /* LWIP_UDPLITE */
254 #if CHECKSUM_CHECK_UDP
255 if (udphdr->chksum != 0) {
256 if (inet_chksum_pseudo(p, &iphdr->src, &iphdr->dest,
257 IP_PROTO_UDP, p->tot_len) != 0) {
258 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
259 ("udp_input: UDP datagram discarded due to failing checksum\n"));
260 UDP_STATS_INC(udp.chkerr);
261 UDP_STATS_INC(udp.drop);
262 snmp_inc_udpinerrors();
267 #endif /* CHECKSUM_CHECK_UDP */
269 if(pbuf_header(p, -UDP_HLEN)) {
270 /* Can we cope with this failing? Just assert for now */
271 LWIP_ASSERT("pbuf_header failed\n", 0);
272 UDP_STATS_INC(udp.drop);
273 snmp_inc_udpinerrors();
278 snmp_inc_udpindatagrams();
279 #if SO_REUSE && SO_REUSE_RXTOALL
280 if ((broadcast || ip_addr_ismulticast(&iphdr->dest)) &&
281 ((pcb->so_options & SOF_REUSEADDR) != 0)) {
282 /* pass broadcast- or multicast packets to all multicast pcbs
283 if SOF_REUSEADDR is set on the first match */
284 struct udp_pcb *mpcb;
285 u8_t p_header_changed = 0;
286 for (mpcb = udp_pcbs; mpcb != NULL; mpcb = mpcb->next) {
288 /* compare PCB local addr+port to UDP destination addr+port */
289 if ((mpcb->local_port == dest) &&
290 ((!broadcast && ip_addr_isany(&mpcb->local_ip)) ||
291 ip_addr_cmp(&(mpcb->local_ip), &(iphdr->dest)) ||
293 ip_addr_ismulticast(&(iphdr->dest)) ||
294 #endif /* LWIP_IGMP */
295 #if IP_SOF_BROADCAST_RECV
296 (broadcast && (mpcb->so_options & SOF_BROADCAST)))) {
297 #else /* IP_SOF_BROADCAST_RECV */
299 #endif /* IP_SOF_BROADCAST_RECV */
300 /* pass a copy of the packet to all local matches */
301 if (mpcb->recv != NULL) {
303 /* for that, move payload to IP header again */
304 if (p_header_changed == 0) {
305 pbuf_header(p, (s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN));
306 p_header_changed = 1;
308 q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
310 err_t err = pbuf_copy(q, p);
312 /* move payload to UDP data */
313 struct ip_hdr *q_iphdr = (struct ip_hdr *)q->payload;
314 pbuf_header(q, -(s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN));
315 mpcb->recv(mpcb->recv_arg, mpcb, q, &q_iphdr->src, src);
322 if (p_header_changed) {
323 /* and move payload to UDP data again */
324 pbuf_header(p, -(s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN));
327 #endif /* SO_REUSE && SO_REUSE_RXTOALL */
329 if (pcb->recv != NULL) {
330 /* now the recv function is responsible for freeing p */
331 pcb->recv(pcb->recv_arg, pcb, p, &iphdr->src, src);
333 /* no recv function registered? then we have to free the pbuf! */
338 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n"));
341 /* No match was found, send ICMP destination port unreachable unless
342 destination address was broadcast/multicast. */
344 !ip_addr_ismulticast(&iphdr->dest)) {
345 /* move payload pointer back to ip header */
346 pbuf_header(p, (IPH_HL(iphdr) * 4) + UDP_HLEN);
347 LWIP_ASSERT("p->payload == iphdr", (p->payload == iphdr));
348 icmp_dest_unreach(p, ICMP_DUR_PORT);
350 #endif /* LWIP_ICMP */
351 UDP_STATS_INC(udp.proterr);
352 UDP_STATS_INC(udp.drop);
353 snmp_inc_udpnoports();
360 PERF_STOP("udp_input");
364 * Send data using UDP.
366 * @param pcb UDP PCB used to send the data.
367 * @param p chain of pbuf's to be sent.
369 * The datagram will be sent to the current remote_ip & remote_port
370 * stored in pcb. If the pcb is not bound to a port, it will
371 * automatically be bound to a random port.
373 * @return lwIP error code.
374 * - ERR_OK. Successful. No error occured.
375 * - ERR_MEM. Out of memory.
376 * - ERR_RTE. Could not find route to destination address.
377 * - More errors could be returned by lower protocol layers.
379 * @see udp_disconnect() udp_sendto()
382 udp_send(struct udp_pcb *pcb, struct pbuf *p)
384 /* send to the packet using remote ip and port stored in the pcb */
385 return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port);
388 #if LWIP_CHECKSUM_ON_COPY
389 /** Same as udp_send() but with checksum
392 udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p,
393 u8_t have_chksum, u16_t chksum)
395 /* send to the packet using remote ip and port stored in the pcb */
396 return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port,
397 have_chksum, chksum);
399 #endif /* LWIP_CHECKSUM_ON_COPY */
402 * Send data to a specified address using UDP.
404 * @param pcb UDP PCB used to send the data.
405 * @param p chain of pbuf's to be sent.
406 * @param dst_ip Destination IP address.
407 * @param dst_port Destination UDP port.
409 * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
411 * If the PCB already has a remote address association, it will
412 * be restored after the data is sent.
414 * @return lwIP error code (@see udp_send for possible error codes)
416 * @see udp_disconnect() udp_send()
419 udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
420 ip_addr_t *dst_ip, u16_t dst_port)
422 #if LWIP_CHECKSUM_ON_COPY
423 return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0);
426 /** Same as udp_sendto(), but with checksum */
428 udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip,
429 u16_t dst_port, u8_t have_chksum, u16_t chksum)
431 #endif /* LWIP_CHECKSUM_ON_COPY */
434 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send\n"));
436 /* find the outgoing network interface for this packet */
438 netif = ip_route((ip_addr_ismulticast(dst_ip))?(&(pcb->multicast_ip)):(dst_ip));
440 netif = ip_route(dst_ip);
441 #endif /* LWIP_IGMP */
443 /* no outgoing network interface could be found? */
445 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
446 ip4_addr1_16(dst_ip), ip4_addr2_16(dst_ip), ip4_addr3_16(dst_ip), ip4_addr4_16(dst_ip)));
447 UDP_STATS_INC(udp.rterr);
450 #if LWIP_CHECKSUM_ON_COPY
451 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum);
452 #else /* LWIP_CHECKSUM_ON_COPY */
453 return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
454 #endif /* LWIP_CHECKSUM_ON_COPY */
458 * Send data to a specified address using UDP.
459 * The netif used for sending can be specified.
461 * This function exists mainly for DHCP, to be able to send UDP packets
462 * on a netif that is still down.
464 * @param pcb UDP PCB used to send the data.
465 * @param p chain of pbuf's to be sent.
466 * @param dst_ip Destination IP address.
467 * @param dst_port Destination UDP port.
468 * @param netif the netif used for sending.
470 * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
472 * @return lwIP error code (@see udp_send for possible error codes)
474 * @see udp_disconnect() udp_send()
477 udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p,
478 ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif)
480 #if LWIP_CHECKSUM_ON_COPY
481 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0);
484 /** Same as udp_sendto_if(), but with checksum */
486 udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip,
487 u16_t dst_port, struct netif *netif, u8_t have_chksum,
490 #endif /* LWIP_CHECKSUM_ON_COPY */
491 struct udp_hdr *udphdr;
494 struct pbuf *q; /* q will be sent down the stack */
497 /* broadcast filter? */
498 if ( ((pcb->so_options & SOF_BROADCAST) == 0) && ip_addr_isbroadcast(dst_ip, netif) ) {
499 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
500 ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
503 #endif /* IP_SOF_BROADCAST */
505 /* if the PCB is not yet bound to a port, bind it here */
506 if (pcb->local_port == 0) {
507 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n"));
508 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
510 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n"));
515 /* not enough space to add an UDP header to first pbuf in given p chain? */
516 if (pbuf_header(p, UDP_HLEN)) {
517 /* allocate header in a separate new pbuf */
518 q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
519 /* new header pbuf could not be allocated? */
521 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n"));
524 /* chain header q in front of given pbuf p */
526 /* first pbuf q points to header pbuf */
527 LWIP_DEBUGF(UDP_DEBUG,
528 ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
530 /* adding space for header within p succeeded */
531 /* first pbuf q equals given pbuf */
533 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
535 LWIP_ASSERT("check that first pbuf can hold struct udp_hdr",
536 (q->len >= sizeof(struct udp_hdr)));
537 /* q now represents the packet to be sent */
538 udphdr = (struct udp_hdr *)q->payload;
539 udphdr->src = htons(pcb->local_port);
540 udphdr->dest = htons(dst_port);
541 /* in UDP, 0 checksum means 'no checksum' */
542 udphdr->chksum = 0x0000;
544 /* PCB local address is IP_ANY_ADDR? */
545 if (ip_addr_isany(&pcb->local_ip)) {
546 /* use outgoing network interface IP address as source address */
547 src_ip = &(netif->ip_addr);
549 /* check if UDP PCB local IP address is correct
550 * this could be an old address if netif->ip_addr has changed */
551 if (!ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) {
552 /* local_ip doesn't match, drop the packet */
554 /* free the header pbuf */
557 /* p is still referenced by the caller, and will live on */
561 /* use UDP PCB local IP address as source address */
562 src_ip = &(pcb->local_ip);
565 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));
568 /* UDP Lite protocol? */
569 if (pcb->flags & UDP_FLAGS_UDPLITE) {
570 u16_t chklen, chklen_hdr;
571 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));
572 /* set UDP message length in UDP header */
573 chklen_hdr = chklen = pcb->chksum_len_tx;
574 if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) {
576 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen));
578 /* For UDP-Lite, checksum length of 0 means checksum
579 over the complete packet. (See RFC 3828 chap. 3.1)
580 At least the UDP-Lite header must be covered by the
581 checksum, therefore, if chksum_len has an illegal
582 value, we generate the checksum over the complete
583 packet to be safe. */
587 udphdr->len = htons(chklen_hdr);
588 /* calculate checksum */
590 udphdr->chksum = inet_chksum_pseudo_partial(q, src_ip, dst_ip,
591 IP_PROTO_UDPLITE, q->tot_len,
592 #if !LWIP_CHECKSUM_ON_COPY
594 #else /* !LWIP_CHECKSUM_ON_COPY */
595 (have_chksum ? UDP_HLEN : chklen));
598 acc = udphdr->chksum + (u16_t)~(chksum);
599 udphdr->chksum = FOLD_U32T(acc);
601 #endif /* !LWIP_CHECKSUM_ON_COPY */
603 /* chksum zero must become 0xffff, as zero means 'no checksum' */
604 if (udphdr->chksum == 0x0000) {
605 udphdr->chksum = 0xffff;
607 #endif /* CHECKSUM_GEN_UDP */
609 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDPLITE,)\n"));
610 #if LWIP_NETIF_HWADDRHINT
611 netif->addr_hint = &(pcb->addr_hint);
612 #endif /* LWIP_NETIF_HWADDRHINT*/
613 err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDPLITE, netif);
614 #if LWIP_NETIF_HWADDRHINT
615 netif->addr_hint = NULL;
616 #endif /* LWIP_NETIF_HWADDRHINT*/
618 #endif /* LWIP_UDPLITE */
620 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));
621 udphdr->len = htons(q->tot_len);
622 /* calculate checksum */
624 if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
626 #if LWIP_CHECKSUM_ON_COPY
629 udpchksum = inet_chksum_pseudo_partial(q, src_ip, dst_ip, IP_PROTO_UDP,
630 q->tot_len, UDP_HLEN);
631 acc = udpchksum + (u16_t)~(chksum);
632 udpchksum = FOLD_U32T(acc);
634 #endif /* LWIP_CHECKSUM_ON_COPY */
636 udpchksum = inet_chksum_pseudo(q, src_ip, dst_ip, IP_PROTO_UDP, q->tot_len);
639 /* chksum zero must become 0xffff, as zero means 'no checksum' */
640 if (udpchksum == 0x0000) {
643 udphdr->chksum = udpchksum;
645 #endif /* CHECKSUM_GEN_UDP */
646 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));
647 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n"));
649 #if LWIP_NETIF_HWADDRHINT
650 netif->addr_hint = &(pcb->addr_hint);
651 #endif /* LWIP_NETIF_HWADDRHINT*/
652 err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif);
653 #if LWIP_NETIF_HWADDRHINT
654 netif->addr_hint = NULL;
655 #endif /* LWIP_NETIF_HWADDRHINT*/
657 /* TODO: must this be increased even if error occured? */
658 snmp_inc_udpoutdatagrams();
660 /* did we chain a separate header pbuf earlier? */
662 /* free the header pbuf */
665 /* p is still referenced by the caller, and will live on */
668 UDP_STATS_INC(udp.xmit);
675 * @param pcb UDP PCB to be bound with a local address ipaddr and port.
676 * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
677 * bind to all local interfaces.
678 * @param port local UDP port to bind with. Use 0 to automatically bind
679 * to a random port between UDP_LOCAL_PORT_RANGE_START and
680 * UDP_LOCAL_PORT_RANGE_END.
682 * ipaddr & port are expected to be in the same byte order as in the pcb.
684 * @return lwIP error code.
685 * - ERR_OK. Successful. No error occured.
686 * - ERR_USE. The specified ipaddr and port are already bound to by
689 * @see udp_disconnect()
692 udp_bind(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port)
694 struct udp_pcb *ipcb;
697 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = "));
698 ip_addr_debug_print(UDP_DEBUG, ipaddr);
699 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port));
702 /* Check for double bind and rebind of the same pcb */
703 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
704 /* is this UDP PCB already on active list? */
706 /* pcb may occur at most once in active list */
707 LWIP_ASSERT("rebind == 0", rebind == 0);
708 /* pcb already in list, just rebind */
712 /* By default, we don't allow to bind to a port that any other udp
713 PCB is alread bound to, unless *all* PCBs with that port have tha
714 REUSEADDR flag set. */
716 else if (((pcb->so_options & SOF_REUSEADDR) == 0) &&
717 ((ipcb->so_options & SOF_REUSEADDR) == 0)) {
719 /* port matches that of PCB in list and REUSEADDR not set -> reject */
721 #endif /* SO_REUSE */
722 if ((ipcb->local_port == port) &&
723 /* IP address matches, or one is IP_ADDR_ANY? */
724 (ip_addr_isany(&(ipcb->local_ip)) ||
725 ip_addr_isany(ipaddr) ||
726 ip_addr_cmp(&(ipcb->local_ip), ipaddr))) {
727 /* other PCB already binds to this local IP and port */
728 LWIP_DEBUGF(UDP_DEBUG,
729 ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));
735 ip_addr_set(&pcb->local_ip, ipaddr);
737 /* no port specified? */
739 #ifndef UDP_LOCAL_PORT_RANGE_START
740 #define UDP_LOCAL_PORT_RANGE_START 4096
741 #define UDP_LOCAL_PORT_RANGE_END 0x7fff
743 port = UDP_LOCAL_PORT_RANGE_START;
745 while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) {
746 if (ipcb->local_port == port) {
747 /* port is already used by another udp_pcb */
749 /* restart scanning all udp pcbs */
752 /* go on with next udp pcb */
757 /* no more ports available in local range */
758 LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
762 pcb->local_port = port;
763 snmp_insert_udpidx_tree(pcb);
764 /* pcb not active yet? */
766 /* place the PCB on the active list if not already there */
767 pcb->next = udp_pcbs;
770 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
771 ("udp_bind: bound to %"U16_F".%"U16_F".%"U16_F".%"U16_F", port %"U16_F"\n",
772 ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip),
773 ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip),
778 * Connect an UDP PCB.
780 * This will associate the UDP PCB with the remote address.
782 * @param pcb UDP PCB to be connected with remote address ipaddr and port.
783 * @param ipaddr remote IP address to connect with.
784 * @param port remote UDP port to connect with.
786 * @return lwIP error code
788 * ipaddr & port are expected to be in the same byte order as in the pcb.
790 * The udp pcb is bound to a random local port if not already bound.
792 * @see udp_disconnect()
795 udp_connect(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port)
797 struct udp_pcb *ipcb;
799 if (pcb->local_port == 0) {
800 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
806 ip_addr_set(&pcb->remote_ip, ipaddr);
807 pcb->remote_port = port;
808 pcb->flags |= UDP_FLAGS_CONNECTED;
809 /** TODO: this functionality belongs in upper layers */
811 /* Nail down local IP for netconn_addr()/getsockname() */
812 if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) {
815 if ((netif = ip_route(&(pcb->remote_ip))) == NULL) {
816 LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n", pcb->remote_ip.addr));
817 UDP_STATS_INC(udp.rterr);
820 /** TODO: this will bind the udp pcb locally, to the interface which
821 is used to route output packets to the remote address. However, we
822 might want to accept incoming packets on any interface! */
823 pcb->local_ip = netif->ip_addr;
824 } else if (ip_addr_isany(&pcb->remote_ip)) {
825 pcb->local_ip.addr = 0;
828 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
829 ("udp_connect: connected to %"U16_F".%"U16_F".%"U16_F".%"U16_F",port %"U16_F"\n",
830 ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip),
831 ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip),
834 /* Insert UDP PCB into the list of active UDP PCBs. */
835 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
837 /* already on the list, just return */
841 /* PCB not yet on the list, add PCB now */
842 pcb->next = udp_pcbs;
848 * Disconnect a UDP PCB
850 * @param pcb the udp pcb to disconnect.
853 udp_disconnect(struct udp_pcb *pcb)
855 /* reset remote address association */
856 ip_addr_set_any(&pcb->remote_ip);
857 pcb->remote_port = 0;
858 /* mark PCB as unconnected */
859 pcb->flags &= ~UDP_FLAGS_CONNECTED;
863 * Set a receive callback for a UDP PCB
865 * This callback will be called when receiving a datagram for the pcb.
867 * @param pcb the pcb for wich to set the recv callback
868 * @param recv function pointer of the callback function
869 * @param recv_arg additional argument to pass to the callback function
872 udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg)
874 /* remember recv() callback and user data */
876 pcb->recv_arg = recv_arg;
882 * @param pcb UDP PCB to be removed. The PCB is removed from the list of
883 * UDP PCB's and the data structure is freed from memory.
888 udp_remove(struct udp_pcb *pcb)
890 struct udp_pcb *pcb2;
892 snmp_delete_udpidx_tree(pcb);
893 /* pcb to be removed is first in list? */
894 if (udp_pcbs == pcb) {
895 /* make list start at 2nd pcb */
896 udp_pcbs = udp_pcbs->next;
897 /* pcb not 1st in list */
899 for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
900 /* find pcb in udp_pcbs list */
901 if (pcb2->next != NULL && pcb2->next == pcb) {
902 /* remove pcb from list */
903 pcb2->next = pcb->next;
907 memp_free(MEMP_UDP_PCB, pcb);
913 * @return The UDP PCB which was created. NULL if the PCB data structure
914 * could not be allocated.
922 pcb = (struct udp_pcb *)memp_malloc(MEMP_UDP_PCB);
923 /* could allocate UDP PCB? */
925 /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0
926 * which means checksum is generated over the whole datagram per default
927 * (recommended as default by RFC 3828). */
928 /* initialize PCB to all zeroes */
929 memset(pcb, 0, sizeof(struct udp_pcb));
937 * Print UDP header information for debug purposes.
939 * @param udphdr pointer to the udp header in memory.
942 udp_debug_print(struct udp_hdr *udphdr)
944 LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
945 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
946 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
947 ntohs(udphdr->src), ntohs(udphdr->dest)));
948 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
949 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n",
950 ntohs(udphdr->len), ntohs(udphdr->chksum)));
951 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
953 #endif /* UDP_DEBUG */
955 #endif /* LWIP_UDP */