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.h"
57 #include "lwip/inet_chksum.h"
58 #include "lwip/ip_addr.h"
59 #include "lwip/netif.h"
60 #include "lwip/icmp.h"
61 #include "lwip/stats.h"
62 #include "lwip/snmp.h"
63 #include "arch/perf.h"
64 #include "lwip/dhcp.h"
68 /* The list of UDP PCBs */
69 /* exported in udp.h (was static) */
70 struct udp_pcb *udp_pcbs;
73 * Process an incoming UDP datagram.
75 * Given an incoming UDP datagram (as a chain of pbufs) this function
76 * finds a corresponding UDP PCB and hands over the pbuf to the pcbs
77 * recv function. If no pcb is found or the datagram is incorrect, the
80 * @param p pbuf to be demultiplexed to a UDP PCB.
81 * @param inp network interface on which the datagram was received.
85 udp_input(struct pbuf *p, struct netif *inp)
87 struct udp_hdr *udphdr;
88 struct udp_pcb *pcb, *prev;
89 struct udp_pcb *uncon_pcb;
96 UDP_STATS_INC(udp.recv);
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 LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len));
117 /* convert src and dest ports to host byte order */
118 src = ntohs(udphdr->src);
119 dest = ntohs(udphdr->dest);
121 udp_debug_print(udphdr);
123 /* print the UDP source and destination */
124 LWIP_DEBUGF(UDP_DEBUG,
125 ("udp (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") <-- "
126 "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
127 ip4_addr1(&iphdr->dest), ip4_addr2(&iphdr->dest),
128 ip4_addr3(&iphdr->dest), ip4_addr4(&iphdr->dest), ntohs(udphdr->dest),
129 ip4_addr1(&iphdr->src), ip4_addr2(&iphdr->src),
130 ip4_addr3(&iphdr->src), ip4_addr4(&iphdr->src), ntohs(udphdr->src)));
134 /* when LWIP_DHCP is active, packets to DHCP_CLIENT_PORT may only be processed by
135 the dhcp module, no other UDP pcb may use the local UDP port DHCP_CLIENT_PORT */
136 if (dest == DHCP_CLIENT_PORT) {
137 /* all packets for DHCP_CLIENT_PORT not coming from DHCP_SERVER_PORT are dropped! */
138 if (src == DHCP_SERVER_PORT) {
139 if ((inp->dhcp != NULL) && (inp->dhcp->pcb != NULL)) {
140 /* accept the packe if
141 (- broadcast or directed to us) -> DHCP is link-layer-addressed, local ip is always ANY!
142 - inp->dhcp->pcb->remote == ANY or iphdr->src */
143 if ((ip_addr_isany(&inp->dhcp->pcb->remote_ip) ||
144 ip_addr_cmp(&(inp->dhcp->pcb->remote_ip), &(iphdr->src)))) {
145 pcb = inp->dhcp->pcb;
150 #endif /* LWIP_DHCP */
155 /* Iterate through the UDP pcb list for a matching pcb.
156 * 'Perfect match' pcbs (connected to the remote port & ip address) are
157 * preferred. If no perfect match is found, the first unconnected pcb that
158 * matches the local port and ip address gets the datagram. */
159 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
161 /* print the PCB local and remote address */
162 LWIP_DEBUGF(UDP_DEBUG,
163 ("pcb (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") --- "
164 "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
165 ip4_addr1(&pcb->local_ip), ip4_addr2(&pcb->local_ip),
166 ip4_addr3(&pcb->local_ip), ip4_addr4(&pcb->local_ip), pcb->local_port,
167 ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
168 ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip), pcb->remote_port));
170 /* compare PCB local addr+port to UDP destination addr+port */
171 if ((pcb->local_port == dest) &&
172 (ip_addr_isany(&pcb->local_ip) ||
173 ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest)) ||
175 ip_addr_ismulticast(&(iphdr->dest)) ||
176 #endif /* LWIP_IGMP */
177 ip_addr_isbroadcast(&(iphdr->dest), inp))) {
179 if ((uncon_pcb == NULL) &&
180 ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) {
181 /* the first unconnected matching PCB */
185 /* compare PCB remote addr+port to UDP source addr+port */
186 if ((local_match != 0) &&
187 (pcb->remote_port == src) &&
188 (ip_addr_isany(&pcb->remote_ip) ||
189 ip_addr_cmp(&(pcb->remote_ip), &(iphdr->src)))) {
190 /* the first fully matching PCB */
192 /* move the pcb to the front of udp_pcbs so that is
193 found faster next time */
194 prev->next = pcb->next;
195 pcb->next = udp_pcbs;
198 UDP_STATS_INC(udp.cachehit);
204 /* no fully matching pcb found? then look for an unconnected pcb */
210 /* Check checksum if this is a match or if it was directed at us. */
211 if (pcb != NULL || ip_addr_cmp(&inp->ip_addr, &iphdr->dest)) {
212 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n"));
214 if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) {
215 /* Do the UDP Lite checksum */
216 #if CHECKSUM_CHECK_UDP
217 u16_t chklen = ntohs(udphdr->len);
218 if (chklen < sizeof(struct udp_hdr)) {
220 /* For UDP-Lite, checksum length of 0 means checksum
221 over the complete packet (See RFC 3828 chap. 3.1) */
224 /* At least the UDP-Lite header must be covered by the
225 checksum! (Again, see RFC 3828 chap. 3.1) */
226 UDP_STATS_INC(udp.chkerr);
227 UDP_STATS_INC(udp.drop);
228 snmp_inc_udpinerrors();
233 if (inet_chksum_pseudo_partial(p, (struct ip_addr *)&(iphdr->src),
234 (struct ip_addr *)&(iphdr->dest),
235 IP_PROTO_UDPLITE, p->tot_len, chklen) != 0) {
236 LWIP_DEBUGF(UDP_DEBUG | 2,
237 ("udp_input: UDP Lite datagram discarded due to failing checksum\n"));
238 UDP_STATS_INC(udp.chkerr);
239 UDP_STATS_INC(udp.drop);
240 snmp_inc_udpinerrors();
244 #endif /* CHECKSUM_CHECK_UDP */
246 #endif /* LWIP_UDPLITE */
248 #if CHECKSUM_CHECK_UDP
249 if (udphdr->chksum != 0) {
250 if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
251 (struct ip_addr *)&(iphdr->dest),
252 IP_PROTO_UDP, p->tot_len) != 0) {
253 LWIP_DEBUGF(UDP_DEBUG | 2,
254 ("udp_input: UDP datagram discarded due to failing checksum\n"));
255 UDP_STATS_INC(udp.chkerr);
256 UDP_STATS_INC(udp.drop);
257 snmp_inc_udpinerrors();
262 #endif /* CHECKSUM_CHECK_UDP */
264 if(pbuf_header(p, -UDP_HLEN)) {
265 /* Can we cope with this failing? Just assert for now */
266 LWIP_ASSERT("pbuf_header failed\n", 0);
267 UDP_STATS_INC(udp.drop);
268 snmp_inc_udpinerrors();
273 snmp_inc_udpindatagrams();
275 if (pcb->recv != NULL) {
276 /* now the recv function is responsible for freeing p */
277 pcb->recv(pcb->recv_arg, pcb, p, &(iphdr->src), src);
279 /* no recv function registered? then we have to free the pbuf! */
284 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n"));
287 /* No match was found, send ICMP destination port unreachable unless
288 destination address was broadcast/multicast. */
289 if (!ip_addr_isbroadcast(&iphdr->dest, inp) &&
290 !ip_addr_ismulticast(&iphdr->dest)) {
291 /* move payload pointer back to ip header */
292 pbuf_header(p, (IPH_HL(iphdr) * 4) + UDP_HLEN);
293 LWIP_ASSERT("p->payload == iphdr", (p->payload == iphdr));
294 icmp_dest_unreach(p, ICMP_DUR_PORT);
296 #endif /* LWIP_ICMP */
297 UDP_STATS_INC(udp.proterr);
298 UDP_STATS_INC(udp.drop);
299 snmp_inc_udpnoports();
306 PERF_STOP("udp_input");
310 * Send data using UDP.
312 * @param pcb UDP PCB used to send the data.
313 * @param p chain of pbuf's to be sent.
315 * The datagram will be sent to the current remote_ip & remote_port
316 * stored in pcb. If the pcb is not bound to a port, it will
317 * automatically be bound to a random port.
319 * @return lwIP error code.
320 * - ERR_OK. Successful. No error occured.
321 * - ERR_MEM. Out of memory.
322 * - ERR_RTE. Could not find route to destination address.
323 * - More errors could be returned by lower protocol layers.
325 * @see udp_disconnect() udp_sendto()
328 udp_send(struct udp_pcb *pcb, struct pbuf *p)
330 /* send to the packet using remote ip and port stored in the pcb */
331 return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port);
335 * Send data to a specified address using UDP.
337 * @param pcb UDP PCB used to send the data.
338 * @param p chain of pbuf's to be sent.
339 * @param dst_ip Destination IP address.
340 * @param dst_port Destination UDP port.
342 * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
344 * If the PCB already has a remote address association, it will
345 * be restored after the data is sent.
347 * @return lwIP error code (@see udp_send for possible error codes)
349 * @see udp_disconnect() udp_send()
352 udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
353 struct ip_addr *dst_ip, u16_t dst_port)
357 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | 3, ("udp_send\n"));
359 /* find the outgoing network interface for this packet */
361 netif = ip_route((ip_addr_ismulticast(dst_ip))?(&(pcb->multicast_ip)):(dst_ip));
363 netif = ip_route(dst_ip);
364 #endif /* LWIP_IGMP */
366 /* no outgoing network interface could be found? */
368 LWIP_DEBUGF(UDP_DEBUG | 1, ("udp_send: No route to 0x%"X32_F"\n", dst_ip->addr));
369 UDP_STATS_INC(udp.rterr);
372 return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
376 * Send data to a specified address using UDP.
377 * The netif used for sending can be specified.
379 * This function exists mainly for DHCP, to be able to send UDP packets
380 * on a netif that is still down.
382 * @param pcb UDP PCB used to send the data.
383 * @param p chain of pbuf's to be sent.
384 * @param dst_ip Destination IP address.
385 * @param dst_port Destination UDP port.
386 * @param netif the netif used for sending.
388 * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
390 * @return lwIP error code (@see udp_send for possible error codes)
392 * @see udp_disconnect() udp_send()
395 udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p,
396 struct ip_addr *dst_ip, u16_t dst_port, struct netif *netif)
398 struct udp_hdr *udphdr;
399 struct ip_addr *src_ip;
401 struct pbuf *q; /* q will be sent down the stack */
403 /* if the PCB is not yet bound to a port, bind it here */
404 if (pcb->local_port == 0) {
405 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | 2, ("udp_send: not yet bound to a port, binding now\n"));
406 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
408 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | 2, ("udp_send: forced port bind failed\n"));
413 /* not enough space to add an UDP header to first pbuf in given p chain? */
414 if (pbuf_header(p, UDP_HLEN)) {
415 /* allocate header in a separate new pbuf */
416 q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
417 /* new header pbuf could not be allocated? */
419 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | 2, ("udp_send: could not allocate header\n"));
422 /* chain header q in front of given pbuf p */
424 /* first pbuf q points to header pbuf */
425 LWIP_DEBUGF(UDP_DEBUG,
426 ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
428 /* adding space for header within p succeeded */
429 /* first pbuf q equals given pbuf */
431 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
433 LWIP_ASSERT("check that first pbuf can hold struct udp_hdr",
434 (q->len >= sizeof(struct udp_hdr)));
435 /* q now represents the packet to be sent */
437 udphdr->src = htons(pcb->local_port);
438 udphdr->dest = htons(dst_port);
439 /* in UDP, 0 checksum means 'no checksum' */
440 udphdr->chksum = 0x0000;
442 /* PCB local address is IP_ANY_ADDR? */
443 if (ip_addr_isany(&pcb->local_ip)) {
444 /* use outgoing network interface IP address as source address */
445 src_ip = &(netif->ip_addr);
447 /* check if UDP PCB local IP address is correct
448 * this could be an old address if netif->ip_addr has changed */
449 if (!ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) {
450 /* local_ip doesn't match, drop the packet */
452 /* free the header pbuf */
455 /* p is still referenced by the caller, and will live on */
459 /* use UDP PCB local IP address as source address */
460 src_ip = &(pcb->local_ip);
463 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));
466 /* UDP Lite protocol? */
467 if (pcb->flags & UDP_FLAGS_UDPLITE) {
468 u16_t chklen, chklen_hdr;
469 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));
470 /* set UDP message length in UDP header */
471 chklen_hdr = chklen = pcb->chksum_len_tx;
472 if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) {
474 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen));
476 /* For UDP-Lite, checksum length of 0 means checksum
477 over the complete packet. (See RFC 3828 chap. 3.1)
478 At least the UDP-Lite header must be covered by the
479 checksum, therefore, if chksum_len has an illegal
480 value, we generate the checksum over the complete
481 packet to be safe. */
485 udphdr->len = htons(chklen_hdr);
486 /* calculate checksum */
488 udphdr->chksum = inet_chksum_pseudo_partial(q, src_ip, dst_ip,
489 IP_PROTO_UDPLITE, q->tot_len, chklen);
490 /* chksum zero must become 0xffff, as zero means 'no checksum' */
491 if (udphdr->chksum == 0x0000)
492 udphdr->chksum = 0xffff;
493 #endif /* CHECKSUM_CHECK_UDP */
495 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDPLITE,)\n"));
496 #if LWIP_NETIF_HWADDRHINT
497 netif->addr_hint = &(pcb->addr_hint);
498 #endif /* LWIP_NETIF_HWADDRHINT*/
499 err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDPLITE, netif);
500 #if LWIP_NETIF_HWADDRHINT
501 netif->addr_hint = NULL;
502 #endif /* LWIP_NETIF_HWADDRHINT*/
504 #endif /* LWIP_UDPLITE */
506 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));
507 udphdr->len = htons(q->tot_len);
508 /* calculate checksum */
510 if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
511 udphdr->chksum = inet_chksum_pseudo(q, src_ip, dst_ip, IP_PROTO_UDP, q->tot_len);
512 /* chksum zero must become 0xffff, as zero means 'no checksum' */
513 if (udphdr->chksum == 0x0000) udphdr->chksum = 0xffff;
515 #endif /* CHECKSUM_CHECK_UDP */
516 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));
517 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n"));
519 #if LWIP_NETIF_HWADDRHINT
520 netif->addr_hint = &(pcb->addr_hint);
521 #endif /* LWIP_NETIF_HWADDRHINT*/
522 err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif);
523 #if LWIP_NETIF_HWADDRHINT
524 netif->addr_hint = NULL;
525 #endif /* LWIP_NETIF_HWADDRHINT*/
527 /* TODO: must this be increased even if error occured? */
528 snmp_inc_udpoutdatagrams();
530 /* did we chain a separate header pbuf earlier? */
532 /* free the header pbuf */
535 /* p is still referenced by the caller, and will live on */
538 UDP_STATS_INC(udp.xmit);
545 * @param pcb UDP PCB to be bound with a local address ipaddr and port.
546 * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
547 * bind to all local interfaces.
548 * @param port local UDP port to bind with. Use 0 to automatically bind
549 * to a random port between UDP_LOCAL_PORT_RANGE_START and
550 * UDP_LOCAL_PORT_RANGE_END.
552 * ipaddr & port are expected to be in the same byte order as in the pcb.
554 * @return lwIP error code.
555 * - ERR_OK. Successful. No error occured.
556 * - ERR_USE. The specified ipaddr and port are already bound to by
559 * @see udp_disconnect()
562 udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
564 struct udp_pcb *ipcb;
567 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | 3, ("udp_bind(ipaddr = "));
568 ip_addr_debug_print(UDP_DEBUG, ipaddr);
569 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | 3, (", port = %"U16_F")\n", port));
572 /* Check for double bind and rebind of the same pcb */
573 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
574 /* is this UDP PCB already on active list? */
576 /* pcb may occur at most once in active list */
577 LWIP_ASSERT("rebind == 0", rebind == 0);
578 /* pcb already in list, just rebind */
582 /* this code does not allow upper layer to share a UDP port for
583 listening to broadcast or multicast traffic (See SO_REUSE_ADDR and
584 SO_REUSE_PORT under *BSD). TODO: See where it fits instead, OR
585 combine with implementation of UDP PCB flags. Leon Woestenberg. */
587 /* port matches that of PCB in list? */
589 if ((ipcb->local_port == port) &&
590 /* IP address matches, or one is IP_ADDR_ANY? */
591 (ip_addr_isany(&(ipcb->local_ip)) ||
592 ip_addr_isany(ipaddr) ||
593 ip_addr_cmp(&(ipcb->local_ip), ipaddr))) {
594 /* other PCB already binds to this local IP and port */
595 LWIP_DEBUGF(UDP_DEBUG,
596 ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));
602 ip_addr_set(&pcb->local_ip, ipaddr);
604 /* no port specified? */
606 #ifndef UDP_LOCAL_PORT_RANGE_START
607 #define UDP_LOCAL_PORT_RANGE_START 4096
608 #define UDP_LOCAL_PORT_RANGE_END 0x7fff
610 port = UDP_LOCAL_PORT_RANGE_START;
612 while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) {
613 if (ipcb->local_port == port) {
614 /* port is already used by another udp_pcb */
616 /* restart scanning all udp pcbs */
619 /* go on with next udp pcb */
623 /* no more ports available in local range */
624 LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
628 pcb->local_port = port;
629 snmp_insert_udpidx_tree(pcb);
630 /* pcb not active yet? */
632 /* place the PCB on the active list if not already there */
633 pcb->next = udp_pcbs;
636 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
637 ("udp_bind: bound to %"U16_F".%"U16_F".%"U16_F".%"U16_F", port %"U16_F"\n",
638 (u16_t)(ntohl(pcb->local_ip.addr) >> 24 & 0xff),
639 (u16_t)(ntohl(pcb->local_ip.addr) >> 16 & 0xff),
640 (u16_t)(ntohl(pcb->local_ip.addr) >> 8 & 0xff),
641 (u16_t)(ntohl(pcb->local_ip.addr) & 0xff), pcb->local_port));
645 * Connect an UDP PCB.
647 * This will associate the UDP PCB with the remote address.
649 * @param pcb UDP PCB to be connected with remote address ipaddr and port.
650 * @param ipaddr remote IP address to connect with.
651 * @param port remote UDP port to connect with.
653 * @return lwIP error code
655 * ipaddr & port are expected to be in the same byte order as in the pcb.
657 * The udp pcb is bound to a random local port if not already bound.
659 * @see udp_disconnect()
662 udp_connect(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
664 struct udp_pcb *ipcb;
666 if (pcb->local_port == 0) {
667 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
672 ip_addr_set(&pcb->remote_ip, ipaddr);
673 pcb->remote_port = port;
674 pcb->flags |= UDP_FLAGS_CONNECTED;
675 /** TODO: this functionality belongs in upper layers */
677 /* Nail down local IP for netconn_addr()/getsockname() */
678 if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) {
681 if ((netif = ip_route(&(pcb->remote_ip))) == NULL) {
682 LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n", pcb->remote_ip.addr));
683 UDP_STATS_INC(udp.rterr);
686 /** TODO: this will bind the udp pcb locally, to the interface which
687 is used to route output packets to the remote address. However, we
688 might want to accept incoming packets on any interface! */
689 pcb->local_ip = netif->ip_addr;
690 } else if (ip_addr_isany(&pcb->remote_ip)) {
691 pcb->local_ip.addr = 0;
694 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
695 ("udp_connect: connected to %"U16_F".%"U16_F".%"U16_F".%"U16_F",port %"U16_F"\n",
696 (u16_t)(ntohl(pcb->remote_ip.addr) >> 24 & 0xff),
697 (u16_t)(ntohl(pcb->remote_ip.addr) >> 16 & 0xff),
698 (u16_t)(ntohl(pcb->remote_ip.addr) >> 8 & 0xff),
699 (u16_t)(ntohl(pcb->remote_ip.addr) & 0xff), pcb->remote_port));
701 /* Insert UDP PCB into the list of active UDP PCBs. */
702 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
704 /* already on the list, just return */
708 /* PCB not yet on the list, add PCB now */
709 pcb->next = udp_pcbs;
715 * Disconnect a UDP PCB
717 * @param pcb the udp pcb to disconnect.
720 udp_disconnect(struct udp_pcb *pcb)
722 /* reset remote address association */
723 ip_addr_set(&pcb->remote_ip, IP_ADDR_ANY);
724 pcb->remote_port = 0;
725 /* mark PCB as unconnected */
726 pcb->flags &= ~UDP_FLAGS_CONNECTED;
730 * Set a receive callback for a UDP PCB
732 * This callback will be called when receiving a datagram for the pcb.
734 * @param pcb the pcb for wich to set the recv callback
735 * @param recv function pointer of the callback function
736 * @param recv_arg additional argument to pass to the callback function
739 udp_recv(struct udp_pcb *pcb,
740 void (* recv)(void *arg, struct udp_pcb *upcb, struct pbuf *p,
741 struct ip_addr *addr, u16_t port),
744 /* remember recv() callback and user data */
746 pcb->recv_arg = recv_arg;
752 * @param pcb UDP PCB to be removed. The PCB is removed from the list of
753 * UDP PCB's and the data structure is freed from memory.
758 udp_remove(struct udp_pcb *pcb)
760 struct udp_pcb *pcb2;
762 snmp_delete_udpidx_tree(pcb);
763 /* pcb to be removed is first in list? */
764 if (udp_pcbs == pcb) {
765 /* make list start at 2nd pcb */
766 udp_pcbs = udp_pcbs->next;
767 /* pcb not 1st in list */
769 for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
770 /* find pcb in udp_pcbs list */
771 if (pcb2->next != NULL && pcb2->next == pcb) {
772 /* remove pcb from list */
773 pcb2->next = pcb->next;
776 memp_free(MEMP_UDP_PCB, pcb);
782 * @return The UDP PCB which was created. NULL if the PCB data structure
783 * could not be allocated.
791 pcb = memp_malloc(MEMP_UDP_PCB);
792 /* could allocate UDP PCB? */
794 /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0
795 * which means checksum is generated over the whole datagram per default
796 * (recommended as default by RFC 3828). */
797 /* initialize PCB to all zeroes */
798 memset(pcb, 0, sizeof(struct udp_pcb));
806 * Print UDP header information for debug purposes.
808 * @param udphdr pointer to the udp header in memory.
811 udp_debug_print(struct udp_hdr *udphdr)
813 LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
814 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
815 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
816 ntohs(udphdr->src), ntohs(udphdr->dest)));
817 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
818 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n",
819 ntohs(udphdr->len), ntohs(udphdr->chksum)));
820 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
822 #endif /* UDP_DEBUG */
824 #endif /* LWIP_UDP */