]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/net/lwip_tcpip/v2_0/src/core/udp.c
Initial revision
[karo-tx-redboot.git] / packages / net / lwip_tcpip / v2_0 / src / core / udp.c
1 /**
2  * @file
3  * User Datagram Protocol module
4  *
5  */
6 /*
7  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30  * OF SUCH DAMAGE.
31  *
32  * This file is part of the lwIP TCP/IP stack.
33  *
34  * Author: Adam Dunkels <adam@sics.se>
35  *
36  */
37
38
39 /* udp.c
40  *
41  * The code for the User Datagram Protocol UDP.
42  *
43  */
44
45 #include "lwip/opt.h"
46
47 #include "lwip/def.h"
48 #include "lwip/memp.h"
49 #include "lwip/inet.h"
50 #include "lwip/ip_addr.h"
51 #include "lwip/netif.h"
52 #include "lwip/udp.h"
53 #include "lwip/icmp.h"
54
55 #include "lwip/stats.h"
56
57 #include "arch/perf.h"
58 #include "lwip/snmp.h"
59
60 /* The list of UDP PCBs */
61 #if LWIP_UDP
62 /* was static, but we may want to access this from a socket layer */
63 struct udp_pcb *udp_pcbs = NULL;
64
65 static struct udp_pcb *pcb_cache = NULL;
66
67
68 void
69 udp_init(void)
70 {
71   udp_pcbs = pcb_cache = NULL;
72 }
73
74 /**
75  * Process an incoming UDP datagram.
76  *
77  * Given an incoming UDP datagram (as a chain of pbufs) this function
78  * finds a corresponding UDP PCB and
79  *
80  * @param pbuf pbuf to be demultiplexed to a UDP PCB.
81  * @param netif network interface on which the datagram was received.
82  *
83  */
84 void
85 udp_input(struct pbuf *p, struct netif *inp)
86 {
87   struct udp_hdr *udphdr;
88   struct udp_pcb *pcb;
89   struct ip_hdr *iphdr;
90   u16_t src, dest;
91
92 #if SO_REUSE
93   struct udp_pcb *pcb_temp;
94   int reuse = 0;
95   int reuse_port_1 = 0;
96   int reuse_port_2 = 0;
97 #endif /* SO_REUSE */
98   
99   PERF_START;
100
101   UDP_STATS_INC(udp.recv);
102
103   iphdr = p->payload;
104
105   if (pbuf_header(p, -((s16_t)(UDP_HLEN + IPH_HL(iphdr) * 4)))) {
106     /* drop short packets */
107     LWIP_DEBUGF(UDP_DEBUG, ("udp_input: short UDP datagram (%u bytes) discarded\n", p->tot_len));
108     UDP_STATS_INC(udp.lenerr);
109     UDP_STATS_INC(udp.drop);
110     snmp_inc_udpinerrors();
111     pbuf_free(p);
112     goto end;
113   }
114
115   udphdr = (struct udp_hdr *)((u8_t *)p->payload - UDP_HLEN);
116
117   LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %u\n", p->tot_len));
118
119   src = ntohs(udphdr->src);
120   dest = ntohs(udphdr->dest);
121
122   udp_debug_print(udphdr);
123
124   /* print the UDP source and destination */
125   LWIP_DEBUGF(UDP_DEBUG, ("udp (%u.%u.%u.%u, %u) <-- (%u.%u.%u.%u, %u)\n",
126     ip4_addr1(&iphdr->dest), ip4_addr2(&iphdr->dest),
127     ip4_addr3(&iphdr->dest), ip4_addr4(&iphdr->dest), ntohs(udphdr->dest),
128     ip4_addr1(&iphdr->src), ip4_addr2(&iphdr->src),
129     ip4_addr3(&iphdr->src), ip4_addr4(&iphdr->src), ntohs(udphdr->src)));
130
131 #if SO_REUSE
132   pcb_temp = udp_pcbs;
133   
134  again_1:
135   
136   /* Iterate through the UDP pcb list for a fully matching pcb */
137   for(pcb = pcb_temp; pcb != NULL; pcb = pcb->next) {
138 #else  /* SO_REUSE */ 
139   /* Iterate through the UDP pcb list for a fully matching pcb */
140   for(pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
141 #endif  /* SO_REUSE */ 
142     /* print the PCB local and remote address */
143     LWIP_DEBUGF(UDP_DEBUG, ("pcb (%u.%u.%u.%u, %u) --- (%u.%u.%u.%u, %u)\n",
144       ip4_addr1(&pcb->local_ip), ip4_addr2(&pcb->local_ip),
145       ip4_addr3(&pcb->local_ip), ip4_addr4(&pcb->local_ip), pcb->local_port,
146       ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
147       ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip), pcb->remote_port));
148
149        /* PCB remote port matches UDP source port? */
150     if ((pcb->remote_port == src) &&
151        /* PCB local port matches UDP destination port? */
152        (pcb->local_port == dest) &&
153        /* accepting from any remote (source) IP address? or... */
154        (ip_addr_isany(&pcb->remote_ip) ||
155        /* PCB remote IP address matches UDP source IP address? */
156         ip_addr_cmp(&(pcb->remote_ip), &(iphdr->src))) &&
157        /* accepting on any local (netif) IP address? or... */
158        (ip_addr_isany(&pcb->local_ip) ||
159        /* PCB local IP address matches UDP destination IP address? */
160         ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest)))) {
161 #if SO_REUSE
162       if(pcb->so_options & SOF_REUSEPORT) {
163         if(reuse) {
164           /* We processed one PCB already */
165           LWIP_DEBUGF(UDP_DEBUG, ("udp_input: second or later PCB and SOF_REUSEPORT set.\n"));
166         } else {
167           /* First PCB with this address */
168           LWIP_DEBUGF(UDP_DEBUG, ("udp_input: first PCB and SOF_REUSEPORT set.\n"));
169           reuse = 1;
170         }
171         
172         reuse_port_1 = 1; 
173         p->ref++;
174         LWIP_DEBUGF(UDP_DEBUG, ("udp_input: reference counter on PBUF set to %i\n", p->ref));
175       } else {
176         if(reuse) {
177           /* We processed one PCB already */
178           LWIP_DEBUGF(UDP_DEBUG, ("udp_input: second or later PCB but SOF_REUSEPORT not set !\n"));
179         }
180       }
181 #endif /* SO_REUSE */
182       break;
183     }
184   }
185   /* no fully matching pcb found? then look for an unconnected pcb */
186   if (pcb == NULL) {
187     /* Iterate through the UDP PCB list for a pcb that matches
188        the local address. */
189
190 #if SO_REUSE
191     pcb_temp = udp_pcbs;
192     
193   again_2:
194
195     for(pcb = pcb_temp; pcb != NULL; pcb = pcb->next) {
196 #else  /* SO_REUSE */ 
197     for(pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
198 #endif  /* SO_REUSE */ 
199       LWIP_DEBUGF(UDP_DEBUG, ("pcb (%u.%u.%u.%u, %u) --- (%u.%u.%u.%u, %u)\n",
200         ip4_addr1(&pcb->local_ip), ip4_addr2(&pcb->local_ip),
201         ip4_addr3(&pcb->local_ip), ip4_addr4(&pcb->local_ip), pcb->local_port,
202         ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
203         ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip), pcb->remote_port));
204       /* unconnected? */
205       if (((pcb->flags & UDP_FLAGS_CONNECTED) == 0) &&
206          /* destination port matches? */
207         (pcb->local_port == dest) &&
208         /* not bound to a specific (local) interface address? or... */
209         (ip_addr_isany(&pcb->local_ip) ||
210         /* ...matching interface address? */
211         ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest)))) {
212 #if SO_REUSE
213         if(pcb->so_options & SOF_REUSEPORT) {
214           if(reuse) {
215             /* We processed one PCB already */
216             LWIP_DEBUGF(UDP_DEBUG, ("udp_input: second or later PCB and SOF_REUSEPORT set.\n"));
217           } else {
218             /* First PCB with this address */
219             LWIP_DEBUGF(UDP_DEBUG, ("udp_input: first PCB and SOF_REUSEPORT set.\n"));
220             reuse = 1;
221           }
222           
223           reuse_port_2 = 1; 
224           p->ref++;
225           LWIP_DEBUGF(UDP_DEBUG, ("udp_input: reference counter on PBUF set to %i\n", p->ref));
226         } else {
227           if(reuse) {
228             /* We processed one PCB already */
229             LWIP_DEBUGF(UDP_DEBUG, ("udp_input: second or later PCB but SOF_REUSEPORT not set !\n"));
230           }
231         }
232 #endif /* SO_REUSE */
233         break;
234       }
235     }
236   }
237
238   /* Check checksum if this is a match or if it was directed at us. */
239   if (pcb != NULL  || ip_addr_cmp(&inp->ip_addr, &iphdr->dest))
240     {
241     LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE, ("udp_input: calculating checksum\n"));
242     pbuf_header(p, UDP_HLEN);
243 #ifdef IPv6
244     if (iphdr->nexthdr == IP_PROTO_UDPLITE) {
245 #else
246     if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) {
247 #endif /* IPv4 */
248       /* Do the UDP Lite checksum */
249 #if CHECKSUM_CHECK_UDP
250       if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
251          (struct ip_addr *)&(iphdr->dest),
252          IP_PROTO_UDPLITE, ntohs(udphdr->len)) != 0) {
253   LWIP_DEBUGF(UDP_DEBUG | 2, ("udp_input: UDP Lite datagram discarded due to failing checksum\n"));
254   UDP_STATS_INC(udp.chkerr);
255   UDP_STATS_INC(udp.drop);
256   snmp_inc_udpinerrors();
257   pbuf_free(p);
258   goto end;
259       }
260 #endif
261     } else {
262 #if CHECKSUM_CHECK_UDP
263       if (udphdr->chksum != 0) {
264   if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
265        (struct ip_addr *)&(iphdr->dest),
266         IP_PROTO_UDP, p->tot_len) != 0) {
267     LWIP_DEBUGF(UDP_DEBUG | 2, ("udp_input: UDP datagram discarded due to failing checksum\n"));
268
269     UDP_STATS_INC(udp.chkerr);
270     UDP_STATS_INC(udp.drop);
271     snmp_inc_udpinerrors();
272     pbuf_free(p);
273     goto end;
274   }
275       }
276 #endif
277     }
278     pbuf_header(p, -UDP_HLEN);
279     if (pcb != NULL) {
280       snmp_inc_udpindatagrams();
281       pcb->recv(pcb->recv_arg, pcb, p, &(iphdr->src), src);
282 #if SO_REUSE
283       /* First socket should receive now */
284       if(reuse_port_1 || reuse_port_2) {
285         /* We want to search on next socket after receiving */
286         pcb_temp = pcb->next;
287         
288         if(reuse_port_1) {
289           /* We are searching connected sockets */
290           reuse_port_1 = 0;
291           reuse_port_2 = 0;
292           goto again_1;
293         } else {
294           /* We are searching unconnected sockets */
295           reuse_port_1 = 0;
296           reuse_port_2 = 0;
297           goto again_2;
298         }
299       }
300 #endif /* SO_REUSE */ 
301     } else {
302 #if SO_REUSE
303       if(reuse) {
304         LWIP_DEBUGF(UDP_DEBUG, ("udp_input: freeing PBUF with reference counter set to %i\n", p->ref));
305         pbuf_free(p);
306         goto end;
307       }
308 #endif /* SO_REUSE */
309       LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE, ("udp_input: not for us.\n"));
310
311       /* No match was found, send ICMP destination port unreachable unless
312       destination address was broadcast/multicast. */
313
314       if (!ip_addr_isbroadcast(&iphdr->dest, inp) &&
315           !ip_addr_ismulticast(&iphdr->dest)) {
316
317   /* adjust pbuf pointer */
318   p->payload = iphdr;
319   icmp_dest_unreach(p, ICMP_DUR_PORT);
320       }
321       UDP_STATS_INC(udp.proterr);
322       UDP_STATS_INC(udp.drop);
323     snmp_inc_udpnoports();
324       pbuf_free(p);
325     }
326   } else {
327     pbuf_free(p);
328   }
329   end:
330
331   PERF_STOP("udp_input");
332 }
333
334 /**
335  * Send data to a specified address using UDP.
336  *
337  * @param pcb UDP PCB used to send the data.
338  * @param pbuf chain of pbuf's to be sent.
339  * @param dst_ip Destination IP address.
340  * @param dst_port Destination UDP port.
341  *
342  * If the PCB already has a remote address association, it will
343  * be restored after the data is sent.
344  * 
345  * @return lwIP error code.
346  * - ERR_OK. Successful. No error occured.
347  * - ERR_MEM. Out of memory.
348  * - ERR_RTE. Could not find route to destination address.
349  *
350  * @see udp_disconnect() udp_send()
351  */
352 err_t
353 udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
354   struct ip_addr *dst_ip, u16_t dst_port)
355 {
356   err_t err;
357   struct ip_addr pcb_remote_ip;
358   u16_t pcb_remote_port;
359   /* remember remote peer address of PCB */
360   pcb_remote_ip.addr = pcb->remote_ip.addr;
361   pcb_remote_port = pcb->remote_port;
362   /* copy packet destination address to PCB remote peer address */
363   pcb->remote_ip.addr = dst_ip->addr;
364   pcb->remote_port = dst_port;
365   /* send to the packet destination address */
366   err = udp_send(pcb, p);
367   /* reset PCB remote peer address */
368   pcb->remote_ip.addr = pcb_remote_ip.addr;
369   pcb->remote_port = pcb_remote_port;
370   return err;
371 }
372
373 /**
374  * Send data using UDP.
375  *
376  * @param pcb UDP PCB used to send the data.
377  * @param pbuf chain of pbuf's to be sent.
378  *
379  * @return lwIP error code.
380  * - ERR_OK. Successful. No error occured.
381  * - ERR_MEM. Out of memory.
382  * - ERR_RTE. Could not find route to destination address.
383  *
384  * @see udp_disconnect() udp_sendto()
385  */
386 err_t
387 udp_send(struct udp_pcb *pcb, struct pbuf *p)
388 {
389   struct udp_hdr *udphdr;
390   struct netif *netif;
391   struct ip_addr *src_ip;
392   err_t err;
393   struct pbuf *q; /* q will be sent down the stack */
394
395   LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("udp_send\n"));
396
397   /* if the PCB is not yet bound to a port, bind it here */
398   if (pcb->local_port == 0) {
399     LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: not yet bound to a port, binding now\n"));
400     err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
401     if (err != ERR_OK) {
402       LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: forced port bind failed\n"));
403       return err;
404     }
405   }
406
407   /* not enough space to add an UDP header to first pbuf in given p chain? */
408   if (pbuf_header(p, UDP_HLEN)) {
409     /* allocate header in a seperate new pbuf */
410     q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
411     /* new header pbuf could not be allocated? */
412     if (q == NULL) {
413       LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: could not allocate header\n"));
414       return ERR_MEM;
415     }
416     /* chain header q in front of given pbuf p */
417     pbuf_chain(q, p);
418     /* { first pbuf q points to header pbuf } */
419     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
420   /* adding a header within p succeeded */
421   } else {
422     /* first pbuf q equals given pbuf */
423     q = p;
424     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
425   }
426   /* { q now represents the packet to be sent } */
427   udphdr = q->payload;
428   udphdr->src = htons(pcb->local_port);
429   udphdr->dest = htons(pcb->remote_port);
430   /* in UDP, 0 checksum means 'no checksum' */
431   udphdr->chksum = 0x0000; 
432
433   /* find the outgoing network interface for this packet */
434   if ((netif = ip_route(&(pcb->remote_ip))) == NULL) {
435     LWIP_DEBUGF(UDP_DEBUG | 1, ("udp_send: No route to 0x%lx\n", pcb->remote_ip.addr));
436     UDP_STATS_INC(udp.rterr);
437     return ERR_RTE;
438   }
439   /* PCB local address is IP_ANY_ADDR? */
440   if (ip_addr_isany(&pcb->local_ip)) {
441     /* use outgoing network interface IP address as source address */
442     src_ip = &(netif->ip_addr);
443   } else {
444     /* use UDP PCB local IP address as source address */
445     src_ip = &(pcb->local_ip);
446   }
447
448   LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %u\n", q->tot_len));
449
450   /* UDP Lite protocol? */
451   if (pcb->flags & UDP_FLAGS_UDPLITE) {
452     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %u\n", q->tot_len));
453     /* set UDP message length in UDP header */
454     udphdr->len = htons(pcb->chksum_len);
455     /* calculate checksum */
456 #if CHECKSUM_GEN_UDP
457     udphdr->chksum = inet_chksum_pseudo(q, src_ip, &(pcb->remote_ip),
458           IP_PROTO_UDP, pcb->chksum_len);
459     /* chksum zero must become 0xffff, as zero means 'no checksum' */
460     if (udphdr->chksum == 0x0000) udphdr->chksum = 0xffff;
461 #else
462     udphdr->chksum = 0x0000;
463 #endif
464     /* output to IP */
465     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDPLITE,)\n"));
466     err = ip_output_if (q, src_ip, &pcb->remote_ip, pcb->ttl, pcb->tos, IP_PROTO_UDPLITE, netif);    
467   /* UDP */
468   } else {
469     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %u\n", q->tot_len));
470     udphdr->len = htons(q->tot_len);
471     /* calculate checksum */
472 #if CHECKSUM_GEN_UDP
473     if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
474       udphdr->chksum = inet_chksum_pseudo(q, src_ip, &pcb->remote_ip, IP_PROTO_UDP, q->tot_len);
475       /* chksum zero must become 0xffff, as zero means 'no checksum' */
476       if (udphdr->chksum == 0x0000) udphdr->chksum = 0xffff;
477     }
478 #else
479     udphdr->chksum = 0x0000;
480 #endif
481     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04x\n", udphdr->chksum));
482     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n"));
483     /* output to IP */
484     err = ip_output_if(q, src_ip, &pcb->remote_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif);    
485   }
486   /* TODO: must this be increased even if error occured? */
487   snmp_inc_udpoutdatagrams();
488
489   /* did we chain a seperate header pbuf earlier? */
490   if (q != p) {
491     /* free the header pbuf */
492     pbuf_free(q); q = NULL;
493     /* { p is still referenced by the caller, and will live on } */
494   }
495
496   UDP_STATS_INC(udp.xmit);
497   return err;
498 }
499
500 /**
501  * Bind an UDP PCB.
502  *
503  * @param pcb UDP PCB to be bound with a local address ipaddr and port.
504  * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
505  * bind to all local interfaces.
506  * @param port local UDP port to bind with.
507  *
508  * @return lwIP error code.
509  * - ERR_OK. Successful. No error occured.
510  * - ERR_USE. The specified ipaddr and port are already bound to by
511  * another UDP PCB.
512  *
513  * @see udp_disconnect()
514  */
515 err_t
516 udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
517 {
518   struct udp_pcb *ipcb;
519   u8_t rebind;
520 #if SO_REUSE
521   int reuse_port_all_set = 1;
522 #endif /* SO_REUSE */
523   LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("udp_bind(ipaddr = "));
524   ip_addr_debug_print(UDP_DEBUG, ipaddr);
525   LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, (", port = %u)\n", port));
526
527   rebind = 0;
528   /* Check for double bind and rebind of the same pcb */
529   for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
530     /* is this UDP PCB already on active list? */
531     if (pcb == ipcb) {
532       /* pcb may occur at most once in active list */
533       LWIP_ASSERT("rebind == 0", rebind == 0);
534       /* pcb already in list, just rebind */
535       rebind = 1;
536     }
537
538 #if SO_REUSE == 0
539 /* this code does not allow upper layer to share a UDP port for
540    listening to broadcast or multicast traffic (See SO_REUSE_ADDR and
541    SO_REUSE_PORT under *BSD). TODO: See where it fits instead, OR
542    combine with implementation of UDP PCB flags. Leon Woestenberg. */
543 #ifdef LWIP_UDP_TODO
544     /* port matches that of PCB in list? */
545     else if ((ipcb->local_port == port) &&
546        /* IP address matches, or one is IP_ADDR_ANY? */
547        (ip_addr_isany(&(ipcb->local_ip)) ||
548        ip_addr_isany(ipaddr) ||
549        ip_addr_cmp(&(ipcb->local_ip), ipaddr))) {
550       /* other PCB already binds to this local IP and port */
551       LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: local port %u already bound by another pcb\n", port));
552       return ERR_USE;
553     }
554 #endif
555
556 #else /* SO_REUSE */
557       /* Search through list of PCB's. 
558          
559       If there is a PCB bound to specified port and IP_ADDR_ANY another PCB can be bound to the interface IP
560       or to the loopback address on the same port if SOF_REUSEADDR is set. Any combination of PCB's bound to 
561       the same local port, but to one address out of {IP_ADDR_ANY, 127.0.0.1, interface IP} at a time is valid.
562       But no two PCB's bound to same local port and same local address is valid.
563       
564       If SOF_REUSEPORT is set several PCB's can be bound to same local port and same local address also. But then 
565       all PCB's must have the SOF_REUSEPORT option set.
566       
567       When the two options aren't set and specified port is already bound, ERR_USE is returned saying that 
568       address is already in use. */
569     else if (ipcb->local_port == port) {
570       if(ip_addr_cmp(&(ipcb->local_ip), ipaddr)) {
571         if(pcb->so_options & SOF_REUSEPORT) {
572           LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: in UDP PCB's SO_REUSEPORT set and same address.\n"));
573           reuse_port_all_set = (reuse_port_all_set && (ipcb->so_options & SOF_REUSEPORT));
574         }
575         else {
576           LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: in UDP PCB's SO_REUSEPORT not set and same address.\n"));
577           return ERR_USE;
578         }
579       }
580       else if((ip_addr_isany(ipaddr) && !ip_addr_isany(&(ipcb->local_ip))) ||
581               (!ip_addr_isany(ipaddr) && ip_addr_isany(&(ipcb->local_ip)))) {
582         if(!(pcb->so_options & SOF_REUSEADDR) && !(pcb->so_options & SOF_REUSEPORT)) {
583           LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: in UDP PCB's SO_REUSEPORT or SO_REUSEADDR not set and not the same address.\n"));
584           return ERR_USE;
585         }           
586       }
587     }
588 #endif /* SO_REUSE */
589
590   }
591
592 #if SO_REUSE
593   /* If SOF_REUSEPORT isn't set in all PCB's bound to specified port and local address specified then 
594      {IP, port} can't be reused. */
595   if(!reuse_port_all_set) {
596     LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: not all sockets have SO_REUSEPORT set.\n"));
597     return ERR_USE;
598   }
599 #endif /* SO_REUSE */
600
601   ip_addr_set(&pcb->local_ip, ipaddr);
602   /* no port specified? */
603   if (port == 0) {
604 #ifndef UDP_LOCAL_PORT_RANGE_START
605 #define UDP_LOCAL_PORT_RANGE_START 4096
606 #define UDP_LOCAL_PORT_RANGE_END   0x7fff
607 #endif
608     port = UDP_LOCAL_PORT_RANGE_START;
609     ipcb = udp_pcbs;
610     while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) {
611       if (ipcb->local_port == port) {
612         port++;
613         ipcb = udp_pcbs;
614       } else
615         ipcb = ipcb->next;
616     }
617     if (ipcb != NULL) {
618       /* no more ports available in local range */
619       LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
620       return ERR_USE;
621     }
622   }
623   pcb->local_port = port;
624   /* pcb not active yet? */
625   if (rebind == 0) {
626     /* place the PCB on the active list if not already there */
627     pcb->next = udp_pcbs;
628     udp_pcbs = pcb;
629   }
630   LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | DBG_STATE, ("udp_bind: bound to %u.%u.%u.%u, port %u\n",
631    (unsigned int)(ntohl(pcb->local_ip.addr) >> 24 & 0xff),
632    (unsigned int)(ntohl(pcb->local_ip.addr) >> 16 & 0xff),
633    (unsigned int)(ntohl(pcb->local_ip.addr) >> 8 & 0xff),
634    (unsigned int)(ntohl(pcb->local_ip.addr) & 0xff), pcb->local_port));
635   return ERR_OK;
636 }
637 /**
638  * Connect an UDP PCB.
639  *
640  * This will associate the UDP PCB with the remote address.
641  *
642  * @param pcb UDP PCB to be connected with remote address ipaddr and port.
643  * @param ipaddr remote IP address to connect with.
644  * @param port remote UDP port to connect with.
645  *
646  * @return lwIP error code
647  *
648  * @see udp_disconnect()
649  */
650 err_t
651 udp_connect(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
652 {
653   struct udp_pcb *ipcb;
654
655   if (pcb->local_port == 0) {
656     err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
657     if (err != ERR_OK)
658       return err;
659   }
660
661   ip_addr_set(&pcb->remote_ip, ipaddr);
662   pcb->remote_port = port;
663   pcb->flags |= UDP_FLAGS_CONNECTED;
664 /** TODO: this functionality belongs in upper layers */
665 #ifdef LWIP_UDP_TODO
666   /* Nail down local IP for netconn_addr()/getsockname() */
667   if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) {
668     struct netif *netif;
669
670     if ((netif = ip_route(&(pcb->remote_ip))) == NULL) {
671       LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n", pcb->remote_ip.addr));
672         UDP_STATS_INC(udp.rterr);
673       return ERR_RTE;
674     }
675     /** TODO: this will bind the udp pcb locally, to the interface which
676         is used to route output packets to the remote address. However, we
677         might want to accept incoming packets on any interface! */
678     pcb->local_ip = netif->ip_addr;
679   } else if (ip_addr_isany(&pcb->remote_ip)) {
680     pcb->local_ip.addr = 0;
681   }
682 #endif
683   LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | DBG_STATE, ("udp_connect: connected to %u.%u.%u.%u, port %u\n",
684    (unsigned int)(ntohl(pcb->remote_ip.addr) >> 24 & 0xff),
685    (unsigned int)(ntohl(pcb->remote_ip.addr) >> 16 & 0xff),
686    (unsigned int)(ntohl(pcb->remote_ip.addr) >> 8 & 0xff),
687    (unsigned int)(ntohl(pcb->remote_ip.addr) & 0xff), pcb->remote_port));
688
689   /* Insert UDP PCB into the list of active UDP PCBs. */
690   for(ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
691     if (pcb == ipcb) {
692       /* already on the list, just return */
693       return ERR_OK;
694     }
695   }
696   /* PCB not yet on the list, add PCB now */
697   pcb->next = udp_pcbs;
698   udp_pcbs = pcb;
699   return ERR_OK;
700 }
701
702 void
703 udp_disconnect(struct udp_pcb *pcb)
704 {
705   /* reset remote address association */
706   ip_addr_set(&pcb->remote_ip, IP_ADDR_ANY);
707   pcb->remote_port = 0;
708   /* mark PCB as unconnected */
709   pcb->flags &= ~UDP_FLAGS_CONNECTED;
710 }
711
712 void
713 udp_recv(struct udp_pcb *pcb,
714    void (* recv)(void *arg, struct udp_pcb *upcb, struct pbuf *p,
715            struct ip_addr *addr, u16_t port),
716    void *recv_arg)
717 {
718   /* remember recv() callback and user data */
719   pcb->recv = recv;
720   pcb->recv_arg = recv_arg;
721 }
722 /**
723  * Remove an UDP PCB.
724  *
725  * @param pcb UDP PCB to be removed. The PCB is removed from the list of
726  * UDP PCB's and the data structure is freed from memory.
727  *
728  * @see udp_new()
729  */
730 void
731 udp_remove(struct udp_pcb *pcb)
732 {
733   struct udp_pcb *pcb2;
734   /* pcb to be removed is first in list? */
735   if (udp_pcbs == pcb) {
736     /* make list start at 2nd pcb */
737     udp_pcbs = udp_pcbs->next;
738   /* pcb not 1st in list */
739   } else for(pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
740     /* find pcb in udp_pcbs list */
741     if (pcb2->next != NULL && pcb2->next == pcb) {
742       /* remove pcb from list */
743       pcb2->next = pcb->next;
744     }
745   }
746   memp_free(MEMP_UDP_PCB, pcb);
747 }
748 /**
749  * Create a UDP PCB.
750  *
751  * @return The UDP PCB which was created. NULL if the PCB data structure
752  * could not be allocated.
753  *
754  * @see udp_remove()
755  */
756 struct udp_pcb *
757 udp_new(void) {
758   struct udp_pcb *pcb;
759   pcb = memp_malloc(MEMP_UDP_PCB);
760   /* could allocate UDP PCB? */
761   if (pcb != NULL) {
762     /* initialize PCB to all zeroes */
763     memset(pcb, 0, sizeof(struct udp_pcb));
764     pcb->ttl = UDP_TTL;
765   }
766   
767   
768   return pcb;
769 }
770
771 #if UDP_DEBUG
772 int
773 udp_debug_print(struct udp_hdr *udphdr)
774 {
775   LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
776   LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
777   LWIP_DEBUGF(UDP_DEBUG, ("|     %5u     |     %5u     | (src port, dest port)\n",
778          ntohs(udphdr->src), ntohs(udphdr->dest)));
779   LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
780   LWIP_DEBUGF(UDP_DEBUG, ("|     %5u     |     0x%04x    | (len, chksum)\n",
781          ntohs(udphdr->len), ntohs(udphdr->chksum)));
782   LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
783   return 0;
784 }
785 #endif /* UDP_DEBUG */
786
787 #endif /* LWIP_UDP */
788
789
790
791
792
793
794
795
796