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