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