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;
97 UDP_STATS_INC(udp.recv);
101 /* Check minimum length (IP header + UDP header)
102 * and move payload pointer to UDP header */
103 if (p->tot_len < (IPH_HL(iphdr) * 4 + UDP_HLEN) || pbuf_header(p, -(s16_t)(IPH_HL(iphdr) * 4))) {
104 /* drop short packets */
105 LWIP_DEBUGF(UDP_DEBUG,
106 ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len));
107 UDP_STATS_INC(udp.lenerr);
108 UDP_STATS_INC(udp.drop);
109 snmp_inc_udpinerrors();
114 udphdr = (struct udp_hdr *)p->payload;
116 /* is broadcast packet ? */
117 broadcast = ip_addr_isbroadcast(&(iphdr->dest), inp);
119 LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len));
121 /* convert src and dest ports to host byte order */
122 src = ntohs(udphdr->src);
123 dest = ntohs(udphdr->dest);
125 udp_debug_print(udphdr);
127 /* print the UDP source and destination */
128 LWIP_DEBUGF(UDP_DEBUG,
129 ("udp (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") <-- "
130 "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
131 ip4_addr1(&iphdr->dest), ip4_addr2(&iphdr->dest),
132 ip4_addr3(&iphdr->dest), ip4_addr4(&iphdr->dest), ntohs(udphdr->dest),
133 ip4_addr1(&iphdr->src), ip4_addr2(&iphdr->src),
134 ip4_addr3(&iphdr->src), ip4_addr4(&iphdr->src), ntohs(udphdr->src)));
138 /* when LWIP_DHCP is active, packets to DHCP_CLIENT_PORT may only be processed by
139 the dhcp module, no other UDP pcb may use the local UDP port DHCP_CLIENT_PORT */
140 if (dest == DHCP_CLIENT_PORT) {
141 /* all packets for DHCP_CLIENT_PORT not coming from DHCP_SERVER_PORT are dropped! */
142 if (src == DHCP_SERVER_PORT) {
143 if ((inp->dhcp != NULL) && (inp->dhcp->pcb != NULL)) {
144 /* accept the packe if
145 (- broadcast or directed to us) -> DHCP is link-layer-addressed, local ip is always ANY!
146 - inp->dhcp->pcb->remote == ANY or iphdr->src */
147 if ((ip_addr_isany(&inp->dhcp->pcb->remote_ip) ||
148 ip_addr_cmp(&(inp->dhcp->pcb->remote_ip), &(iphdr->src)))) {
149 pcb = inp->dhcp->pcb;
154 #endif /* LWIP_DHCP */
159 /* Iterate through the UDP pcb list for a matching pcb.
160 * 'Perfect match' pcbs (connected to the remote port & ip address) are
161 * preferred. If no perfect match is found, the first unconnected pcb that
162 * matches the local port and ip address gets the datagram. */
163 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
165 /* print the PCB local and remote address */
166 LWIP_DEBUGF(UDP_DEBUG,
167 ("pcb (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") --- "
168 "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
169 ip4_addr1(&pcb->local_ip), ip4_addr2(&pcb->local_ip),
170 ip4_addr3(&pcb->local_ip), ip4_addr4(&pcb->local_ip), pcb->local_port,
171 ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
172 ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip), pcb->remote_port));
174 /* compare PCB local addr+port to UDP destination addr+port */
175 if ((pcb->local_port == dest) &&
176 ((!broadcast && ip_addr_isany(&pcb->local_ip)) ||
177 ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest)) ||
179 ip_addr_ismulticast(&(iphdr->dest)) ||
180 #endif /* LWIP_IGMP */
181 #if IP_SOF_BROADCAST_RECV
182 (broadcast && (pcb->so_options & SOF_BROADCAST)))) {
183 #else /* IP_SOF_BROADCAST_RECV */
185 #endif /* IP_SOF_BROADCAST_RECV */
187 if ((uncon_pcb == NULL) &&
188 ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) {
189 /* the first unconnected matching PCB */
193 /* compare PCB remote addr+port to UDP source addr+port */
194 if ((local_match != 0) &&
195 (pcb->remote_port == src) &&
196 (ip_addr_isany(&pcb->remote_ip) ||
197 ip_addr_cmp(&(pcb->remote_ip), &(iphdr->src)))) {
198 /* the first fully matching PCB */
200 /* move the pcb to the front of udp_pcbs so that is
201 found faster next time */
202 prev->next = pcb->next;
203 pcb->next = udp_pcbs;
206 UDP_STATS_INC(udp.cachehit);
212 /* no fully matching pcb found? then look for an unconnected pcb */
218 /* Check checksum if this is a match or if it was directed at us. */
219 if (pcb != NULL || ip_addr_cmp(&inp->ip_addr, &iphdr->dest)) {
220 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n"));
222 if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) {
223 /* Do the UDP Lite checksum */
224 #if CHECKSUM_CHECK_UDP
225 u16_t chklen = ntohs(udphdr->len);
226 if (chklen < sizeof(struct udp_hdr)) {
228 /* For UDP-Lite, checksum length of 0 means checksum
229 over the complete packet (See RFC 3828 chap. 3.1) */
232 /* At least the UDP-Lite header must be covered by the
233 checksum! (Again, see RFC 3828 chap. 3.1) */
234 UDP_STATS_INC(udp.chkerr);
235 UDP_STATS_INC(udp.drop);
236 snmp_inc_udpinerrors();
241 if (inet_chksum_pseudo_partial(p, (struct ip_addr *)&(iphdr->src),
242 (struct ip_addr *)&(iphdr->dest),
243 IP_PROTO_UDPLITE, p->tot_len, chklen) != 0) {
244 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
245 ("udp_input: UDP Lite datagram discarded due to failing checksum\n"));
246 UDP_STATS_INC(udp.chkerr);
247 UDP_STATS_INC(udp.drop);
248 snmp_inc_udpinerrors();
252 #endif /* CHECKSUM_CHECK_UDP */
254 #endif /* LWIP_UDPLITE */
256 #if CHECKSUM_CHECK_UDP
257 if (udphdr->chksum != 0) {
258 if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
259 (struct ip_addr *)&(iphdr->dest),
260 IP_PROTO_UDP, p->tot_len) != 0) {
261 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
262 ("udp_input: UDP datagram discarded due to failing checksum\n"));
263 UDP_STATS_INC(udp.chkerr);
264 UDP_STATS_INC(udp.drop);
265 snmp_inc_udpinerrors();
270 #endif /* CHECKSUM_CHECK_UDP */
272 if(pbuf_header(p, -UDP_HLEN)) {
273 /* Can we cope with this failing? Just assert for now */
274 LWIP_ASSERT("pbuf_header failed\n", 0);
275 UDP_STATS_INC(udp.drop);
276 snmp_inc_udpinerrors();
281 snmp_inc_udpindatagrams();
283 if (pcb->recv != NULL) {
284 /* now the recv function is responsible for freeing p */
285 pcb->recv(pcb->recv_arg, pcb, p, &iphdr->src, src);
287 /* no recv function registered? then we have to free the pbuf! */
292 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n"));
295 /* No match was found, send ICMP destination port unreachable unless
296 destination address was broadcast/multicast. */
298 !ip_addr_ismulticast(&iphdr->dest)) {
299 /* move payload pointer back to ip header */
300 pbuf_header(p, (IPH_HL(iphdr) * 4) + UDP_HLEN);
301 LWIP_ASSERT("p->payload == iphdr", (p->payload == iphdr));
302 icmp_dest_unreach(p, ICMP_DUR_PORT);
304 #endif /* LWIP_ICMP */
305 UDP_STATS_INC(udp.proterr);
306 UDP_STATS_INC(udp.drop);
307 snmp_inc_udpnoports();
314 PERF_STOP("udp_input");
318 * Send data using UDP.
320 * @param pcb UDP PCB used to send the data.
321 * @param p chain of pbuf's to be sent.
323 * The datagram will be sent to the current remote_ip & remote_port
324 * stored in pcb. If the pcb is not bound to a port, it will
325 * automatically be bound to a random port.
327 * @return lwIP error code.
328 * - ERR_OK. Successful. No error occured.
329 * - ERR_MEM. Out of memory.
330 * - ERR_RTE. Could not find route to destination address.
331 * - More errors could be returned by lower protocol layers.
333 * @see udp_disconnect() udp_sendto()
336 udp_send(struct udp_pcb *pcb, struct pbuf *p)
338 /* send to the packet using remote ip and port stored in the pcb */
339 return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port);
343 * Send data to a specified address using UDP.
345 * @param pcb UDP PCB used to send the data.
346 * @param p chain of pbuf's to be sent.
347 * @param dst_ip Destination IP address.
348 * @param dst_port Destination UDP port.
350 * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
352 * If the PCB already has a remote address association, it will
353 * be restored after the data is sent.
355 * @return lwIP error code (@see udp_send for possible error codes)
357 * @see udp_disconnect() udp_send()
360 udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
361 struct ip_addr *dst_ip, u16_t dst_port)
365 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send\n"));
367 /* find the outgoing network interface for this packet */
369 netif = ip_route((ip_addr_ismulticast(dst_ip))?(&(pcb->multicast_ip)):(dst_ip));
371 netif = ip_route(dst_ip);
372 #endif /* LWIP_IGMP */
374 /* no outgoing network interface could be found? */
376 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: No route to 0x%"X32_F"\n", dst_ip->addr));
377 UDP_STATS_INC(udp.rterr);
380 return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
384 * Send data to a specified address using UDP.
385 * The netif used for sending can be specified.
387 * This function exists mainly for DHCP, to be able to send UDP packets
388 * on a netif that is still down.
390 * @param pcb UDP PCB used to send the data.
391 * @param p chain of pbuf's to be sent.
392 * @param dst_ip Destination IP address.
393 * @param dst_port Destination UDP port.
394 * @param netif the netif used for sending.
396 * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
398 * @return lwIP error code (@see udp_send for possible error codes)
400 * @see udp_disconnect() udp_send()
403 udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p,
404 struct ip_addr *dst_ip, u16_t dst_port, struct netif *netif)
406 struct udp_hdr *udphdr;
407 struct ip_addr *src_ip;
409 struct pbuf *q; /* q will be sent down the stack */
412 /* broadcast filter? */
413 if ( ((pcb->so_options & SOF_BROADCAST) == 0) && ip_addr_isbroadcast(dst_ip, netif) ) {
414 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
415 ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
418 #endif /* IP_SOF_BROADCAST */
420 /* if the PCB is not yet bound to a port, bind it here */
421 if (pcb->local_port == 0) {
422 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n"));
423 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
425 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n"));
430 /* not enough space to add an UDP header to first pbuf in given p chain? */
431 if (pbuf_header(p, UDP_HLEN)) {
432 /* allocate header in a separate new pbuf */
433 q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
434 /* new header pbuf could not be allocated? */
436 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n"));
439 /* chain header q in front of given pbuf p */
441 /* first pbuf q points to header pbuf */
442 LWIP_DEBUGF(UDP_DEBUG,
443 ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
445 /* adding space for header within p succeeded */
446 /* first pbuf q equals given pbuf */
448 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
450 LWIP_ASSERT("check that first pbuf can hold struct udp_hdr",
451 (q->len >= sizeof(struct udp_hdr)));
452 /* q now represents the packet to be sent */
454 udphdr->src = htons(pcb->local_port);
455 udphdr->dest = htons(dst_port);
456 /* in UDP, 0 checksum means 'no checksum' */
457 udphdr->chksum = 0x0000;
459 /* PCB local address is IP_ANY_ADDR? */
460 if (ip_addr_isany(&pcb->local_ip)) {
461 /* use outgoing network interface IP address as source address */
462 src_ip = &(netif->ip_addr);
464 /* check if UDP PCB local IP address is correct
465 * this could be an old address if netif->ip_addr has changed */
466 if (!ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) {
467 /* local_ip doesn't match, drop the packet */
469 /* free the header pbuf */
472 /* p is still referenced by the caller, and will live on */
476 /* use UDP PCB local IP address as source address */
477 src_ip = &(pcb->local_ip);
480 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));
483 /* UDP Lite protocol? */
484 if (pcb->flags & UDP_FLAGS_UDPLITE) {
485 u16_t chklen, chklen_hdr;
486 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));
487 /* set UDP message length in UDP header */
488 chklen_hdr = chklen = pcb->chksum_len_tx;
489 if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) {
491 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen));
493 /* For UDP-Lite, checksum length of 0 means checksum
494 over the complete packet. (See RFC 3828 chap. 3.1)
495 At least the UDP-Lite header must be covered by the
496 checksum, therefore, if chksum_len has an illegal
497 value, we generate the checksum over the complete
498 packet to be safe. */
502 udphdr->len = htons(chklen_hdr);
503 /* calculate checksum */
505 udphdr->chksum = inet_chksum_pseudo_partial(q, src_ip, dst_ip,
506 IP_PROTO_UDPLITE, q->tot_len, chklen);
507 /* chksum zero must become 0xffff, as zero means 'no checksum' */
508 if (udphdr->chksum == 0x0000)
509 udphdr->chksum = 0xffff;
510 #endif /* CHECKSUM_CHECK_UDP */
512 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDPLITE,)\n"));
513 #if LWIP_NETIF_HWADDRHINT
514 netif->addr_hint = &(pcb->addr_hint);
515 #endif /* LWIP_NETIF_HWADDRHINT*/
516 err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDPLITE, netif);
517 #if LWIP_NETIF_HWADDRHINT
518 netif->addr_hint = NULL;
519 #endif /* LWIP_NETIF_HWADDRHINT*/
521 #endif /* LWIP_UDPLITE */
523 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));
524 udphdr->len = htons(q->tot_len);
525 /* calculate checksum */
527 if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
528 udphdr->chksum = inet_chksum_pseudo(q, src_ip, dst_ip, IP_PROTO_UDP, q->tot_len);
529 /* chksum zero must become 0xffff, as zero means 'no checksum' */
530 if (udphdr->chksum == 0x0000) udphdr->chksum = 0xffff;
532 #endif /* CHECKSUM_CHECK_UDP */
533 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));
534 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n"));
536 #if LWIP_NETIF_HWADDRHINT
537 netif->addr_hint = &(pcb->addr_hint);
538 #endif /* LWIP_NETIF_HWADDRHINT*/
539 err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif);
540 #if LWIP_NETIF_HWADDRHINT
541 netif->addr_hint = NULL;
542 #endif /* LWIP_NETIF_HWADDRHINT*/
544 /* TODO: must this be increased even if error occured? */
545 snmp_inc_udpoutdatagrams();
547 /* did we chain a separate header pbuf earlier? */
549 /* free the header pbuf */
552 /* p is still referenced by the caller, and will live on */
555 UDP_STATS_INC(udp.xmit);
562 * @param pcb UDP PCB to be bound with a local address ipaddr and port.
563 * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
564 * bind to all local interfaces.
565 * @param port local UDP port to bind with. Use 0 to automatically bind
566 * to a random port between UDP_LOCAL_PORT_RANGE_START and
567 * UDP_LOCAL_PORT_RANGE_END.
569 * ipaddr & port are expected to be in the same byte order as in the pcb.
571 * @return lwIP error code.
572 * - ERR_OK. Successful. No error occured.
573 * - ERR_USE. The specified ipaddr and port are already bound to by
576 * @see udp_disconnect()
579 udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
581 struct udp_pcb *ipcb;
584 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = "));
585 ip_addr_debug_print(UDP_DEBUG, ipaddr);
586 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port));
589 /* Check for double bind and rebind of the same pcb */
590 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
591 /* is this UDP PCB already on active list? */
593 /* pcb may occur at most once in active list */
594 LWIP_ASSERT("rebind == 0", rebind == 0);
595 /* pcb already in list, just rebind */
599 /* this code does not allow upper layer to share a UDP port for
600 listening to broadcast or multicast traffic (See SO_REUSE_ADDR and
601 SO_REUSE_PORT under *BSD). TODO: See where it fits instead, OR
602 combine with implementation of UDP PCB flags. Leon Woestenberg. */
604 /* port matches that of PCB in list? */
606 if ((ipcb->local_port == port) &&
607 /* IP address matches, or one is IP_ADDR_ANY? */
608 (ip_addr_isany(&(ipcb->local_ip)) ||
609 ip_addr_isany(ipaddr) ||
610 ip_addr_cmp(&(ipcb->local_ip), ipaddr))) {
611 /* other PCB already binds to this local IP and port */
612 LWIP_DEBUGF(UDP_DEBUG,
613 ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));
619 ip_addr_set(&pcb->local_ip, ipaddr);
621 /* no port specified? */
623 #ifndef UDP_LOCAL_PORT_RANGE_START
624 #define UDP_LOCAL_PORT_RANGE_START 4096
625 #define UDP_LOCAL_PORT_RANGE_END 0x7fff
627 port = UDP_LOCAL_PORT_RANGE_START;
629 while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) {
630 if (ipcb->local_port == port) {
631 /* port is already used by another udp_pcb */
633 /* restart scanning all udp pcbs */
636 /* go on with next udp pcb */
640 /* no more ports available in local range */
641 LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
645 pcb->local_port = port;
646 snmp_insert_udpidx_tree(pcb);
647 /* pcb not active yet? */
649 /* place the PCB on the active list if not already there */
650 pcb->next = udp_pcbs;
653 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
654 ("udp_bind: bound to %"U16_F".%"U16_F".%"U16_F".%"U16_F", port %"U16_F"\n",
655 (u16_t)((ntohl(pcb->local_ip.addr) >> 24) & 0xff),
656 (u16_t)((ntohl(pcb->local_ip.addr) >> 16) & 0xff),
657 (u16_t)((ntohl(pcb->local_ip.addr) >> 8) & 0xff),
658 (u16_t)(ntohl(pcb->local_ip.addr) & 0xff), pcb->local_port));
662 * Connect an UDP PCB.
664 * This will associate the UDP PCB with the remote address.
666 * @param pcb UDP PCB to be connected with remote address ipaddr and port.
667 * @param ipaddr remote IP address to connect with.
668 * @param port remote UDP port to connect with.
670 * @return lwIP error code
672 * ipaddr & port are expected to be in the same byte order as in the pcb.
674 * The udp pcb is bound to a random local port if not already bound.
676 * @see udp_disconnect()
679 udp_connect(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
681 struct udp_pcb *ipcb;
683 if (pcb->local_port == 0) {
684 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
689 ip_addr_set(&pcb->remote_ip, ipaddr);
690 pcb->remote_port = port;
691 pcb->flags |= UDP_FLAGS_CONNECTED;
692 /** TODO: this functionality belongs in upper layers */
694 /* Nail down local IP for netconn_addr()/getsockname() */
695 if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) {
698 if ((netif = ip_route(&(pcb->remote_ip))) == NULL) {
699 LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n", pcb->remote_ip.addr));
700 UDP_STATS_INC(udp.rterr);
703 /** TODO: this will bind the udp pcb locally, to the interface which
704 is used to route output packets to the remote address. However, we
705 might want to accept incoming packets on any interface! */
706 pcb->local_ip = netif->ip_addr;
707 } else if (ip_addr_isany(&pcb->remote_ip)) {
708 pcb->local_ip.addr = 0;
711 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
712 ("udp_connect: connected to %"U16_F".%"U16_F".%"U16_F".%"U16_F",port %"U16_F"\n",
713 (u16_t)((ntohl(pcb->remote_ip.addr) >> 24) & 0xff),
714 (u16_t)((ntohl(pcb->remote_ip.addr) >> 16) & 0xff),
715 (u16_t)((ntohl(pcb->remote_ip.addr) >> 8) & 0xff),
716 (u16_t)(ntohl(pcb->remote_ip.addr) & 0xff), pcb->remote_port));
718 /* Insert UDP PCB into the list of active UDP PCBs. */
719 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
721 /* already on the list, just return */
725 /* PCB not yet on the list, add PCB now */
726 pcb->next = udp_pcbs;
732 * Disconnect a UDP PCB
734 * @param pcb the udp pcb to disconnect.
737 udp_disconnect(struct udp_pcb *pcb)
739 /* reset remote address association */
740 ip_addr_set(&pcb->remote_ip, IP_ADDR_ANY);
741 pcb->remote_port = 0;
742 /* mark PCB as unconnected */
743 pcb->flags &= ~UDP_FLAGS_CONNECTED;
747 * Set a receive callback for a UDP PCB
749 * This callback will be called when receiving a datagram for the pcb.
751 * @param pcb the pcb for wich to set the recv callback
752 * @param recv function pointer of the callback function
753 * @param recv_arg additional argument to pass to the callback function
756 udp_recv(struct udp_pcb *pcb,
757 void (* recv)(void *arg, struct udp_pcb *upcb, struct pbuf *p,
758 struct ip_addr *addr, u16_t port),
761 /* remember recv() callback and user data */
763 pcb->recv_arg = recv_arg;
769 * @param pcb UDP PCB to be removed. The PCB is removed from the list of
770 * UDP PCB's and the data structure is freed from memory.
775 udp_remove(struct udp_pcb *pcb)
777 struct udp_pcb *pcb2;
779 snmp_delete_udpidx_tree(pcb);
780 /* pcb to be removed is first in list? */
781 if (udp_pcbs == pcb) {
782 /* make list start at 2nd pcb */
783 udp_pcbs = udp_pcbs->next;
784 /* pcb not 1st in list */
786 for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
787 /* find pcb in udp_pcbs list */
788 if (pcb2->next != NULL && pcb2->next == pcb) {
789 /* remove pcb from list */
790 pcb2->next = pcb->next;
793 memp_free(MEMP_UDP_PCB, pcb);
799 * @return The UDP PCB which was created. NULL if the PCB data structure
800 * could not be allocated.
808 pcb = memp_malloc(MEMP_UDP_PCB);
809 /* could allocate UDP PCB? */
811 /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0
812 * which means checksum is generated over the whole datagram per default
813 * (recommended as default by RFC 3828). */
814 /* initialize PCB to all zeroes */
815 memset(pcb, 0, sizeof(struct udp_pcb));
823 * Print UDP header information for debug purposes.
825 * @param udphdr pointer to the udp header in memory.
828 udp_debug_print(struct udp_hdr *udphdr)
830 LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
831 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
832 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
833 ntohs(udphdr->src), ntohs(udphdr->dest)));
834 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
835 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n",
836 ntohs(udphdr->len), ntohs(udphdr->chksum)));
837 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
839 #endif /* UDP_DEBUG */
841 #endif /* LWIP_UDP */