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