]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/ethernet/lwIP/core/udp.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / Common / ethernet / lwIP / 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 /* exported in udp.h (was static) */\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 (p->tot_len < (IPH_HL(iphdr) * 4 + UDP_HLEN)) {\r
102     /* drop short packets */\r
103 //    LWIP_DEBUGF(UDP_DEBUG,\r
104 //                ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len));\r
105 \r
106     LWIP_DEBUGF(UDP_DEBUG,\r
107                 ("udp_input: short UDP datagram (%u bytes) discarded\n", p->tot_len));\r
108 \r
109     UDP_STATS_INC(udp.lenerr);\r
110     UDP_STATS_INC(udp.drop);\r
111     snmp_inc_udpinerrors();\r
112     pbuf_free(p);\r
113     goto end;\r
114   }\r
115 \r
116   pbuf_header(p, -((s16_t)(IPH_HL(iphdr) * 4)));\r
117 \r
118   udphdr = (struct udp_hdr *)p->payload;\r
119 \r
120   LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len));\r
121 \r
122   src = ntohs(udphdr->src);\r
123   dest = ntohs(udphdr->dest);\r
124 \r
125   udp_debug_print(udphdr);\r
126 \r
127   /* print the UDP source and destination */\r
128   LWIP_DEBUGF(UDP_DEBUG,\r
129               ("udp (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") <-- "\r
130                "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",\r
131                ip4_addr1(&iphdr->dest), ip4_addr2(&iphdr->dest),\r
132                ip4_addr3(&iphdr->dest), ip4_addr4(&iphdr->dest), ntohs(udphdr->dest),\r
133                ip4_addr1(&iphdr->src), ip4_addr2(&iphdr->src),\r
134                ip4_addr3(&iphdr->src), ip4_addr4(&iphdr->src), ntohs(udphdr->src)));\r
135 \r
136   local_match = 0;\r
137   uncon_pcb = NULL;\r
138   /* Iterate through the UDP pcb list for a matching pcb */\r
139   for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {\r
140     /* print the PCB local and remote address */\r
141     LWIP_DEBUGF(UDP_DEBUG,\r
142                 ("pcb (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") --- "\r
143                  "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",\r
144                  ip4_addr1(&pcb->local_ip), ip4_addr2(&pcb->local_ip),\r
145                  ip4_addr3(&pcb->local_ip), ip4_addr4(&pcb->local_ip), pcb->local_port,\r
146                  ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),\r
147                  ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip), pcb->remote_port));\r
148 \r
149     /* compare PCB local addr+port to UDP destination addr+port */\r
150     if ((pcb->local_port == dest) &&\r
151         (ip_addr_isany(&pcb->local_ip) ||\r
152          ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest)) || \r
153          ip_addr_isbroadcast(&(iphdr->dest), inp))) {\r
154       local_match = 1;\r
155       if ((uncon_pcb == NULL) && \r
156           ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) {\r
157         /* the first unconnected matching PCB */     \r
158         uncon_pcb = pcb;\r
159       }\r
160     }\r
161     /* compare PCB remote addr+port to UDP source addr+port */\r
162     if ((local_match != 0) &&\r
163         (pcb->remote_port == src) &&\r
164         (ip_addr_isany(&pcb->remote_ip) ||\r
165          ip_addr_cmp(&(pcb->remote_ip), &(iphdr->src)))) {\r
166       /* the first fully matching PCB */\r
167       break;\r
168     }\r
169   }\r
170   /* no fully matching pcb found? then look for an unconnected pcb */\r
171   if (pcb == NULL) {\r
172     pcb = uncon_pcb;\r
173   }\r
174 \r
175   /* Check checksum if this is a match or if it was directed at us. */\r
176   if (pcb != NULL || ip_addr_cmp(&inp->ip_addr, &iphdr->dest)) {\r
177     LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE, ("udp_input: calculating checksum\n"));\r
178 #ifdef IPv6\r
179     if (iphdr->nexthdr == IP_PROTO_UDPLITE) {\r
180 #else\r
181     if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) {\r
182 #endif /* IPv4 */\r
183       /* Do the UDP Lite checksum */\r
184 #if CHECKSUM_CHECK_UDP\r
185       if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),\r
186                              (struct ip_addr *)&(iphdr->dest),\r
187                              IP_PROTO_UDPLITE, ntohs(udphdr->len)) != 0) {\r
188         LWIP_DEBUGF(UDP_DEBUG | 2,\r
189                     ("udp_input: UDP Lite datagram discarded due to failing checksum\n"));\r
190         UDP_STATS_INC(udp.chkerr);\r
191         UDP_STATS_INC(udp.drop);\r
192         snmp_inc_udpinerrors();\r
193         pbuf_free(p);\r
194         goto end;\r
195       }\r
196 #endif\r
197     } else {\r
198 #if CHECKSUM_CHECK_UDP\r
199       if (udphdr->chksum != 0) {\r
200         if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),\r
201                                (struct ip_addr *)&(iphdr->dest),\r
202                                IP_PROTO_UDP, p->tot_len) != 0) {\r
203           LWIP_DEBUGF(UDP_DEBUG | 2,\r
204                       ("udp_input: UDP datagram discarded due to failing checksum\n"));\r
205           UDP_STATS_INC(udp.chkerr);\r
206           UDP_STATS_INC(udp.drop);\r
207           snmp_inc_udpinerrors();\r
208           pbuf_free(p);\r
209           goto end;\r
210         }\r
211       }\r
212 #endif\r
213     }\r
214     pbuf_header(p, -UDP_HLEN);\r
215     if (pcb != NULL) {\r
216       snmp_inc_udpindatagrams();\r
217       /* callback */\r
218       if (pcb->recv != NULL)\r
219         pcb->recv(pcb->recv_arg, pcb, p, &(iphdr->src), src);\r
220     } else {\r
221       LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE, ("udp_input: not for us.\n"));\r
222 \r
223       /* No match was found, send ICMP destination port unreachable unless\r
224          destination address was broadcast/multicast. */\r
225 \r
226       if (!ip_addr_isbroadcast(&iphdr->dest, inp) &&\r
227           !ip_addr_ismulticast(&iphdr->dest)) {\r
228 \r
229         /* restore pbuf pointer */\r
230         p->payload = iphdr;\r
231         icmp_dest_unreach(p, ICMP_DUR_PORT);\r
232       }\r
233       UDP_STATS_INC(udp.proterr);\r
234       UDP_STATS_INC(udp.drop);\r
235       snmp_inc_udpnoports();\r
236       pbuf_free(p);\r
237     }\r
238   } else {\r
239     pbuf_free(p);\r
240   }\r
241 end:\r
242   PERF_STOP("udp_input");\r
243 }\r
244 \r
245 /**\r
246  * Send data to a specified address using UDP.\r
247  *\r
248  * @param pcb UDP PCB used to send the data.\r
249  * @param pbuf chain of pbuf's to be sent.\r
250  * @param dst_ip Destination IP address.\r
251  * @param dst_port Destination UDP port.\r
252  *\r
253  * If the PCB already has a remote address association, it will\r
254  * be restored after the data is sent.\r
255  * \r
256  * @return lwIP error code.\r
257  * - ERR_OK. Successful. No error occured.\r
258  * - ERR_MEM. Out of memory.\r
259  * - ERR_RTE. Could not find route to destination address.\r
260  *\r
261  * @see udp_disconnect() udp_send()\r
262  */\r
263 err_t\r
264 udp_sendto(struct udp_pcb *pcb, struct pbuf *p,\r
265   struct ip_addr *dst_ip, u16_t dst_port)\r
266 {\r
267   err_t err;\r
268   /* temporary space for current PCB remote address */\r
269   struct ip_addr pcb_remote_ip;\r
270   u16_t pcb_remote_port;\r
271   /* remember current remote peer address of PCB */\r
272   pcb_remote_ip.addr = pcb->remote_ip.addr;\r
273   pcb_remote_port = pcb->remote_port;\r
274   /* copy packet destination address to PCB remote peer address */\r
275   pcb->remote_ip.addr = dst_ip->addr;\r
276   pcb->remote_port = dst_port;\r
277   /* send to the packet destination address */\r
278   err = udp_send(pcb, p);\r
279   /* restore PCB remote peer address */\r
280   pcb->remote_ip.addr = pcb_remote_ip.addr;\r
281   pcb->remote_port = pcb_remote_port;\r
282   return err;\r
283 }\r
284 \r
285 /**\r
286  * Send data using UDP.\r
287  *\r
288  * @param pcb UDP PCB used to send the data.\r
289  * @param pbuf chain of pbuf's to be sent.\r
290  *\r
291  * @return lwIP error code.\r
292  * - ERR_OK. Successful. No error occured.\r
293  * - ERR_MEM. Out of memory.\r
294  * - ERR_RTE. Could not find route to destination address.\r
295  *\r
296  * @see udp_disconnect() udp_sendto()\r
297  */\r
298 err_t\r
299 udp_send(struct udp_pcb *pcb, struct pbuf *p)\r
300 {\r
301   struct udp_hdr *udphdr;\r
302   struct netif *netif;\r
303   struct ip_addr *src_ip;\r
304   err_t err;\r
305   struct pbuf *q; /* q will be sent down the stack */\r
306 \r
307   LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("udp_send\n"));\r
308 \r
309   /* if the PCB is not yet bound to a port, bind it here */\r
310   if (pcb->local_port == 0) {\r
311     LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: not yet bound to a port, binding now\n"));\r
312     err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);\r
313     if (err != ERR_OK) {\r
314       LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: forced port bind failed\n"));\r
315       return err;\r
316     }\r
317   }\r
318   /* find the outgoing network interface for this packet */\r
319   netif = ip_route(&(pcb->remote_ip));\r
320   /* no outgoing network interface could be found? */\r
321   if (netif == NULL) {\r
322     LWIP_DEBUGF(UDP_DEBUG | 1, ("udp_send: No route to 0x%"X32_F"\n", pcb->remote_ip.addr));\r
323     UDP_STATS_INC(udp.rterr);\r
324     return ERR_RTE;\r
325   }\r
326 \r
327   /* not enough space to add an UDP header to first pbuf in given p chain? */\r
328   if (pbuf_header(p, UDP_HLEN)) {\r
329     /* allocate header in a seperate new pbuf */\r
330     q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);\r
331     /* new header pbuf could not be allocated? */\r
332     if (q == NULL) {\r
333       LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: could not allocate header\n"));\r
334       return ERR_MEM;\r
335     }\r
336     /* chain header q in front of given pbuf p */\r
337     pbuf_chain(q, p);\r
338     /* first pbuf q points to header pbuf */\r
339     LWIP_DEBUGF(UDP_DEBUG,\r
340                 ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));\r
341     /* adding a header within p succeeded */\r
342   } else {\r
343     /* first pbuf q equals given pbuf */\r
344     q = p;\r
345     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));\r
346   }\r
347   /* q now represents the packet to be sent */\r
348   udphdr = q->payload;\r
349   udphdr->src = htons(pcb->local_port);\r
350   udphdr->dest = htons(pcb->remote_port);\r
351   /* in UDP, 0 checksum means 'no checksum' */\r
352   udphdr->chksum = 0x0000; \r
353 \r
354   /* PCB local address is IP_ANY_ADDR? */\r
355   if (ip_addr_isany(&pcb->local_ip)) {\r
356     /* use outgoing network interface IP address as source address */\r
357     src_ip = &(netif->ip_addr);\r
358   } else {\r
359     /* use UDP PCB local IP address as source address */\r
360     src_ip = &(pcb->local_ip);\r
361   }\r
362 \r
363   LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));\r
364 \r
365   /* UDP Lite protocol? */\r
366   if (pcb->flags & UDP_FLAGS_UDPLITE) {\r
367     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));\r
368     /* set UDP message length in UDP header */\r
369     udphdr->len = htons(pcb->chksum_len);\r
370     /* calculate checksum */\r
371 #if CHECKSUM_GEN_UDP\r
372     udphdr->chksum = inet_chksum_pseudo(q, src_ip, &(pcb->remote_ip),\r
373                                         IP_PROTO_UDP, pcb->chksum_len);\r
374     /* chksum zero must become 0xffff, as zero means 'no checksum' */\r
375     if (udphdr->chksum == 0x0000)\r
376       udphdr->chksum = 0xffff;\r
377 #else\r
378     udphdr->chksum = 0x0000;\r
379 #endif\r
380     /* output to IP */\r
381     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDPLITE,)\n"));\r
382     err = ip_output_if(q, src_ip, &pcb->remote_ip, pcb->ttl, pcb->tos, IP_PROTO_UDPLITE, netif);    \r
383   } else {      /* UDP */\r
384     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));\r
385     udphdr->len = htons(q->tot_len);\r
386     /* calculate checksum */\r
387 #if CHECKSUM_GEN_UDP\r
388     if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {\r
389       udphdr->chksum = inet_chksum_pseudo(q, src_ip, &pcb->remote_ip, IP_PROTO_UDP, q->tot_len);\r
390       /* chksum zero must become 0xffff, as zero means 'no checksum' */\r
391       if (udphdr->chksum == 0x0000) udphdr->chksum = 0xffff;\r
392     }\r
393 #else\r
394     udphdr->chksum = 0x0000;\r
395 #endif\r
396     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));\r
397     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n"));\r
398     /* output to IP */\r
399     err = ip_output_if(q, src_ip, &pcb->remote_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif);    \r
400   }\r
401   /* TODO: must this be increased even if error occured? */\r
402   snmp_inc_udpoutdatagrams();\r
403 \r
404   /* did we chain a seperate header pbuf earlier? */\r
405   if (q != p) {\r
406     /* free the header pbuf */\r
407     pbuf_free(q);\r
408     q = NULL;\r
409     /* p is still referenced by the caller, and will live on */\r
410   }\r
411 \r
412   UDP_STATS_INC(udp.xmit);\r
413   return err;\r
414 }\r
415 \r
416 /**\r
417  * Bind an UDP PCB.\r
418  *\r
419  * @param pcb UDP PCB to be bound with a local address ipaddr and port.\r
420  * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to\r
421  * bind to all local interfaces.\r
422  * @param port local UDP port to bind with.\r
423  *\r
424  * @return lwIP error code.\r
425  * - ERR_OK. Successful. No error occured.\r
426  * - ERR_USE. The specified ipaddr and port are already bound to by\r
427  * another UDP PCB.\r
428  *\r
429  * @see udp_disconnect()\r
430  */\r
431 err_t\r
432 udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)\r
433 {\r
434   struct udp_pcb *ipcb;\r
435   u8_t rebind;\r
436 \r
437   LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("udp_bind(ipaddr = "));\r
438   ip_addr_debug_print(UDP_DEBUG, ipaddr);\r
439   LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, (", port = %"U16_F")\n", port));\r
440 \r
441   rebind = 0;\r
442   /* Check for double bind and rebind of the same pcb */\r
443   for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {\r
444     /* is this UDP PCB already on active list? */\r
445     if (pcb == ipcb) {\r
446       /* pcb may occur at most once in active list */\r
447       LWIP_ASSERT("rebind == 0", rebind == 0);\r
448       /* pcb already in list, just rebind */\r
449       rebind = 1;\r
450     }\r
451 \r
452     /* this code does not allow upper layer to share a UDP port for\r
453        listening to broadcast or multicast traffic (See SO_REUSE_ADDR and\r
454        SO_REUSE_PORT under *BSD). TODO: See where it fits instead, OR\r
455        combine with implementation of UDP PCB flags. Leon Woestenberg. */\r
456 #ifdef LWIP_UDP_TODO\r
457     /* port matches that of PCB in list? */\r
458     else\r
459       if ((ipcb->local_port == port) &&\r
460           /* IP address matches, or one is IP_ADDR_ANY? */\r
461           (ip_addr_isany(&(ipcb->local_ip)) ||\r
462            ip_addr_isany(ipaddr) ||\r
463            ip_addr_cmp(&(ipcb->local_ip), ipaddr))) {\r
464         /* other PCB already binds to this local IP and port */\r
465         LWIP_DEBUGF(UDP_DEBUG,\r
466                     ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));\r
467         return ERR_USE;\r
468       }\r
469 #endif\r
470   }\r
471 \r
472   ip_addr_set(&pcb->local_ip, ipaddr);\r
473 \r
474   /* no port specified? */\r
475   if (port == 0) {\r
476 #ifndef UDP_LOCAL_PORT_RANGE_START\r
477 #define UDP_LOCAL_PORT_RANGE_START 4096\r
478 #define UDP_LOCAL_PORT_RANGE_END   0x7fff\r
479 #endif\r
480     port = UDP_LOCAL_PORT_RANGE_START;\r
481     ipcb = udp_pcbs;\r
482     while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) {\r
483       if (ipcb->local_port == port) {\r
484         port++;\r
485         ipcb = udp_pcbs;\r
486       } else\r
487         ipcb = ipcb->next;\r
488     }\r
489     if (ipcb != NULL) {\r
490       /* no more ports available in local range */\r
491       LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));\r
492       return ERR_USE;\r
493     }\r
494   }\r
495   pcb->local_port = port;\r
496   snmp_insert_udpidx_tree(pcb);\r
497   /* pcb not active yet? */\r
498   if (rebind == 0) {\r
499     /* place the PCB on the active list if not already there */\r
500     pcb->next = udp_pcbs;\r
501     udp_pcbs = pcb;\r
502   }\r
503   LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | DBG_STATE,\r
504               ("udp_bind: bound to %"U16_F".%"U16_F".%"U16_F".%"U16_F", port %"U16_F"\n",\r
505                (u16_t)(ntohl(pcb->local_ip.addr) >> 24 & 0xff),\r
506                (u16_t)(ntohl(pcb->local_ip.addr) >> 16 & 0xff),\r
507                (u16_t)(ntohl(pcb->local_ip.addr) >> 8 & 0xff),\r
508                (u16_t)(ntohl(pcb->local_ip.addr) & 0xff), pcb->local_port));\r
509   return ERR_OK;\r
510 }\r
511 /**\r
512  * Connect an UDP PCB.\r
513  *\r
514  * This will associate the UDP PCB with the remote address.\r
515  *\r
516  * @param pcb UDP PCB to be connected with remote address ipaddr and port.\r
517  * @param ipaddr remote IP address to connect with.\r
518  * @param port remote UDP port to connect with.\r
519  *\r
520  * @return lwIP error code\r
521  *\r
522  * @see udp_disconnect()\r
523  */\r
524 err_t\r
525 udp_connect(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)\r
526 {\r
527   struct udp_pcb *ipcb;\r
528 \r
529   if (pcb->local_port == 0) {\r
530     err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);\r
531     if (err != ERR_OK)\r
532       return err;\r
533   }\r
534 \r
535   ip_addr_set(&pcb->remote_ip, ipaddr);\r
536   pcb->remote_port = port;\r
537   pcb->flags |= UDP_FLAGS_CONNECTED;\r
538 /** TODO: this functionality belongs in upper layers */\r
539 #ifdef LWIP_UDP_TODO\r
540   /* Nail down local IP for netconn_addr()/getsockname() */\r
541   if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) {\r
542     struct netif *netif;\r
543 \r
544     if ((netif = ip_route(&(pcb->remote_ip))) == NULL) {\r
545       LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n", pcb->remote_ip.addr));\r
546       UDP_STATS_INC(udp.rterr);\r
547       return ERR_RTE;\r
548     }\r
549     /** TODO: this will bind the udp pcb locally, to the interface which\r
550         is used to route output packets to the remote address. However, we\r
551         might want to accept incoming packets on any interface! */\r
552     pcb->local_ip = netif->ip_addr;\r
553   } else if (ip_addr_isany(&pcb->remote_ip)) {\r
554     pcb->local_ip.addr = 0;\r
555   }\r
556 #endif\r
557   LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | DBG_STATE,\r
558               ("udp_connect: connected to %"U16_F".%"U16_F".%"U16_F".%"U16_F",port %"U16_F"\n",\r
559                (u16_t)(ntohl(pcb->remote_ip.addr) >> 24 & 0xff),\r
560                (u16_t)(ntohl(pcb->remote_ip.addr) >> 16 & 0xff),\r
561                (u16_t)(ntohl(pcb->remote_ip.addr) >> 8 & 0xff),\r
562                (u16_t)(ntohl(pcb->remote_ip.addr) & 0xff), pcb->remote_port));\r
563 \r
564   /* Insert UDP PCB into the list of active UDP PCBs. */\r
565   for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {\r
566     if (pcb == ipcb) {\r
567       /* already on the list, just return */\r
568       return ERR_OK;\r
569     }\r
570   }\r
571   /* PCB not yet on the list, add PCB now */\r
572   pcb->next = udp_pcbs;\r
573   udp_pcbs = pcb;\r
574   return ERR_OK;\r
575 }\r
576 \r
577 void\r
578 udp_disconnect(struct udp_pcb *pcb)\r
579 {\r
580   /* reset remote address association */\r
581   ip_addr_set(&pcb->remote_ip, IP_ADDR_ANY);\r
582   pcb->remote_port = 0;\r
583   /* mark PCB as unconnected */\r
584   pcb->flags &= ~UDP_FLAGS_CONNECTED;\r
585 }\r
586 \r
587 void\r
588 udp_recv(struct udp_pcb *pcb,\r
589          void (* recv)(void *arg, struct udp_pcb *upcb, struct pbuf *p,\r
590                        struct ip_addr *addr, u16_t port),\r
591          void *recv_arg)\r
592 {\r
593   /* remember recv() callback and user data */\r
594   pcb->recv = recv;\r
595   pcb->recv_arg = recv_arg;\r
596 }\r
597 \r
598 /**\r
599  * Remove an UDP PCB.\r
600  *\r
601  * @param pcb UDP PCB to be removed. The PCB is removed from the list of\r
602  * UDP PCB's and the data structure is freed from memory.\r
603  *\r
604  * @see udp_new()\r
605  */\r
606 void\r
607 udp_remove(struct udp_pcb *pcb)\r
608 {\r
609   struct udp_pcb *pcb2;\r
610 \r
611   snmp_delete_udpidx_tree(pcb);\r
612   /* pcb to be removed is first in list? */\r
613   if (udp_pcbs == pcb) {\r
614     /* make list start at 2nd pcb */\r
615     udp_pcbs = udp_pcbs->next;\r
616     /* pcb not 1st in list */\r
617   } else\r
618     for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {\r
619       /* find pcb in udp_pcbs list */\r
620       if (pcb2->next != NULL && pcb2->next == pcb) {\r
621         /* remove pcb from list */\r
622         pcb2->next = pcb->next;\r
623       }\r
624     }\r
625   memp_free(MEMP_UDP_PCB, pcb);\r
626 }\r
627 \r
628 /**\r
629  * Create a UDP PCB.\r
630  *\r
631  * @return The UDP PCB which was created. NULL if the PCB data structure\r
632  * could not be allocated.\r
633  *\r
634  * @see udp_remove()\r
635  */\r
636 struct udp_pcb *\r
637 udp_new(void)\r
638 {\r
639   struct udp_pcb *pcb;\r
640   pcb = memp_malloc(MEMP_UDP_PCB);\r
641   /* could allocate UDP PCB? */\r
642   if (pcb != NULL) {\r
643     /* initialize PCB to all zeroes */\r
644     memset(pcb, 0, sizeof(struct udp_pcb));\r
645     pcb->ttl = UDP_TTL;\r
646   }\r
647   return pcb;\r
648 }\r
649 \r
650 #if UDP_DEBUG\r
651 void\r
652 udp_debug_print(struct udp_hdr *udphdr)\r
653 {\r
654   LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));\r
655   LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));\r
656   LWIP_DEBUGF(UDP_DEBUG, ("|     %5"U16_F"     |     %5"U16_F"     | (src port, dest port)\n",\r
657                           ntohs(udphdr->src), ntohs(udphdr->dest)));\r
658   LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));\r
659   LWIP_DEBUGF(UDP_DEBUG, ("|     %5"U16_F"     |     0x%04"X16_F"    | (len, chksum)\n",\r
660                           ntohs(udphdr->len), ntohs(udphdr->chksum)));\r
661   LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));\r
662 }\r
663 #endif /* UDP_DEBUG */\r
664 \r
665 #endif /* LWIP_UDP */\r