NetServices Stack source
Dependents: HelloWorld ServoInterfaceBoardExample1 4180_Lab4
udp.c
00001 /** 00002 * @file 00003 * User Datagram Protocol module 00004 * 00005 */ 00006 00007 /* 00008 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 00009 * All rights reserved. 00010 * 00011 * Redistribution and use in source and binary forms, with or without modification, 00012 * are permitted provided that the following conditions are met: 00013 * 00014 * 1. Redistributions of source code must retain the above copyright notice, 00015 * this list of conditions and the following disclaimer. 00016 * 2. Redistributions in binary form must reproduce the above copyright notice, 00017 * this list of conditions and the following disclaimer in the documentation 00018 * and/or other materials provided with the distribution. 00019 * 3. The name of the author may not be used to endorse or promote products 00020 * derived from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00023 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00025 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00026 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00027 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00028 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00029 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00030 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00031 * OF SUCH DAMAGE. 00032 * 00033 * This file is part of the lwIP TCP/IP stack. 00034 * 00035 * Author: Adam Dunkels <adam@sics.se> 00036 * 00037 */ 00038 00039 00040 /* udp.c 00041 * 00042 * The code for the User Datagram Protocol UDP & UDPLite (RFC 3828). 00043 * 00044 */ 00045 00046 /* @todo Check the use of '(struct udp_pcb).chksum_len_rx'! 00047 */ 00048 00049 #include "lwip/opt.h" 00050 00051 #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */ 00052 00053 #include "lwip/udp.h" 00054 #include "lwip/def.h" 00055 #include "lwip/memp.h" 00056 #include "lwip/inet_chksum.h" 00057 #include "lwip/ip_addr.h" 00058 #include "lwip/netif.h" 00059 #include "lwip/icmp.h" 00060 #include "lwip/stats.h" 00061 #include "lwip/snmp.h" 00062 #include "arch/perf.h" 00063 #include "lwip/dhcp.h " 00064 00065 #include <string.h> 00066 00067 /* The list of UDP PCBs */ 00068 /* exported in udp.h (was static) */ 00069 struct udp_pcb *udp_pcbs; 00070 00071 /** 00072 * Process an incoming UDP datagram. 00073 * 00074 * Given an incoming UDP datagram (as a chain of pbufs) this function 00075 * finds a corresponding UDP PCB and hands over the pbuf to the pcbs 00076 * recv function. If no pcb is found or the datagram is incorrect, the 00077 * pbuf is freed. 00078 * 00079 * @param p pbuf to be demultiplexed to a UDP PCB. 00080 * @param inp network interface on which the datagram was received. 00081 * 00082 */ 00083 void 00084 udp_input(struct pbuf *p, struct netif *inp) 00085 { 00086 struct udp_hdr *udphdr; 00087 struct udp_pcb *pcb, *prev; 00088 struct udp_pcb *uncon_pcb; 00089 struct ip_hdr *iphdr; 00090 u16_t src, dest; 00091 u8_t local_match; 00092 u8_t broadcast; 00093 00094 PERF_START; 00095 00096 UDP_STATS_INC(udp.recv); 00097 00098 iphdr = (struct ip_hdr *)p->payload; 00099 00100 /* Check minimum length (IP header + UDP header) 00101 * and move payload pointer to UDP header */ 00102 if (p->tot_len < (IPH_HL(iphdr) * 4 + UDP_HLEN) || pbuf_header(p, -(s16_t)(IPH_HL(iphdr) * 4))) { 00103 /* drop short packets */ 00104 LWIP_DEBUGF(UDP_DEBUG, 00105 ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len)); 00106 UDP_STATS_INC(udp.lenerr); 00107 UDP_STATS_INC(udp.drop); 00108 snmp_inc_udpinerrors(); 00109 pbuf_free(p); 00110 goto end; 00111 } 00112 00113 udphdr = (struct udp_hdr *)p->payload; 00114 00115 /* is broadcast packet ? */ 00116 broadcast = ip_addr_isbroadcast(¤t_iphdr_dest, inp); 00117 00118 LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len)); 00119 00120 /* convert src and dest ports to host byte order */ 00121 src = ntohs(udphdr->src); 00122 dest = ntohs(udphdr->dest); 00123 00124 udp_debug_print(udphdr); 00125 00126 /* print the UDP source and destination */ 00127 LWIP_DEBUGF(UDP_DEBUG, 00128 ("udp (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") <-- " 00129 "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n", 00130 ip4_addr1_16(&iphdr->dest), ip4_addr2_16(&iphdr->dest), 00131 ip4_addr3_16(&iphdr->dest), ip4_addr4_16(&iphdr->dest), ntohs(udphdr->dest), 00132 ip4_addr1_16(&iphdr->src), ip4_addr2_16(&iphdr->src), 00133 ip4_addr3_16(&iphdr->src), ip4_addr4_16(&iphdr->src), ntohs(udphdr->src))); 00134 00135 #if LWIP_DHCP 00136 pcb = NULL; 00137 /* when LWIP_DHCP is active, packets to DHCP_CLIENT_PORT may only be processed by 00138 the dhcp module, no other UDP pcb may use the local UDP port DHCP_CLIENT_PORT */ 00139 if (dest == DHCP_CLIENT_PORT) { 00140 /* all packets for DHCP_CLIENT_PORT not coming from DHCP_SERVER_PORT are dropped! */ 00141 if (src == DHCP_SERVER_PORT) { 00142 if ((inp->dhcp != NULL) && (inp->dhcp->pcb != NULL)) { 00143 /* accept the packe if 00144 (- broadcast or directed to us) -> DHCP is link-layer-addressed, local ip is always ANY! 00145 - inp->dhcp->pcb->remote == ANY or iphdr->src */ 00146 if ((ip_addr_isany(&inp->dhcp->pcb->remote_ip) || 00147 ip_addr_cmp(&(inp->dhcp->pcb->remote_ip), ¤t_iphdr_src))) { 00148 pcb = inp->dhcp->pcb; 00149 } 00150 } 00151 } 00152 } else 00153 #endif /* LWIP_DHCP */ 00154 { 00155 prev = NULL; 00156 local_match = 0; 00157 uncon_pcb = NULL; 00158 /* Iterate through the UDP pcb list for a matching pcb. 00159 * 'Perfect match' pcbs (connected to the remote port & ip address) are 00160 * preferred. If no perfect match is found, the first unconnected pcb that 00161 * matches the local port and ip address gets the datagram. */ 00162 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) { 00163 local_match = 0; 00164 /* print the PCB local and remote address */ 00165 LWIP_DEBUGF(UDP_DEBUG, 00166 ("pcb (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") --- " 00167 "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n", 00168 ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip), 00169 ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip), pcb->local_port, 00170 ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip), 00171 ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip), pcb->remote_port)); 00172 00173 /* compare PCB local addr+port to UDP destination addr+port */ 00174 if ((pcb->local_port == dest) && 00175 ((!broadcast && ip_addr_isany(&pcb->local_ip)) || 00176 ip_addr_cmp(&(pcb->local_ip), ¤t_iphdr_dest) || 00177 #if LWIP_IGMP 00178 ip_addr_ismulticast(¤t_iphdr_dest) || 00179 #endif /* LWIP_IGMP */ 00180 #if IP_SOF_BROADCAST_RECV 00181 (broadcast && (pcb->so_options & SOF_BROADCAST)))) { 00182 #else /* IP_SOF_BROADCAST_RECV */ 00183 (broadcast))) { 00184 #endif /* IP_SOF_BROADCAST_RECV */ 00185 local_match = 1; 00186 if ((uncon_pcb == NULL) && 00187 ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) { 00188 /* the first unconnected matching PCB */ 00189 uncon_pcb = pcb; 00190 } 00191 } 00192 /* compare PCB remote addr+port to UDP source addr+port */ 00193 if ((local_match != 0) && 00194 (pcb->remote_port == src) && 00195 (ip_addr_isany(&pcb->remote_ip) || 00196 ip_addr_cmp(&(pcb->remote_ip), ¤t_iphdr_src))) { 00197 /* the first fully matching PCB */ 00198 if (prev != NULL) { 00199 /* move the pcb to the front of udp_pcbs so that is 00200 found faster next time */ 00201 prev->next = pcb->next; 00202 pcb->next = udp_pcbs; 00203 udp_pcbs = pcb; 00204 } else { 00205 UDP_STATS_INC(udp.cachehit); 00206 } 00207 break; 00208 } 00209 prev = pcb; 00210 } 00211 /* no fully matching pcb found? then look for an unconnected pcb */ 00212 if (pcb == NULL) { 00213 pcb = uncon_pcb; 00214 } 00215 } 00216 00217 /* Check checksum if this is a match or if it was directed at us. */ 00218 if (pcb != NULL || ip_addr_cmp(&inp->ip_addr, ¤t_iphdr_dest)) { 00219 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n")); 00220 #if LWIP_UDPLITE 00221 if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) { 00222 /* Do the UDP Lite checksum */ 00223 #if CHECKSUM_CHECK_UDP 00224 u16_t chklen = ntohs(udphdr->len); 00225 if (chklen < sizeof(struct udp_hdr)) { 00226 if (chklen == 0) { 00227 /* For UDP-Lite, checksum length of 0 means checksum 00228 over the complete packet (See RFC 3828 chap. 3.1) */ 00229 chklen = p->tot_len; 00230 } else { 00231 /* At least the UDP-Lite header must be covered by the 00232 checksum! (Again, see RFC 3828 chap. 3.1) */ 00233 UDP_STATS_INC(udp.chkerr); 00234 UDP_STATS_INC(udp.drop); 00235 snmp_inc_udpinerrors(); 00236 pbuf_free(p); 00237 goto end; 00238 } 00239 } 00240 if (inet_chksum_pseudo_partial(p, ¤t_iphdr_src, ¤t_iphdr_dest, 00241 IP_PROTO_UDPLITE, p->tot_len, chklen) != 0) { 00242 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, 00243 ("udp_input: UDP Lite datagram discarded due to failing checksum\n")); 00244 UDP_STATS_INC(udp.chkerr); 00245 UDP_STATS_INC(udp.drop); 00246 snmp_inc_udpinerrors(); 00247 pbuf_free(p); 00248 goto end; 00249 } 00250 #endif /* CHECKSUM_CHECK_UDP */ 00251 } else 00252 #endif /* LWIP_UDPLITE */ 00253 { 00254 #if CHECKSUM_CHECK_UDP 00255 if (udphdr->chksum != 0) { 00256 if (inet_chksum_pseudo(p, ip_current_src_addr(), ip_current_dest_addr(), 00257 IP_PROTO_UDP, p->tot_len) != 0) { 00258 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, 00259 ("udp_input: UDP datagram discarded due to failing checksum\n")); 00260 UDP_STATS_INC(udp.chkerr); 00261 UDP_STATS_INC(udp.drop); 00262 snmp_inc_udpinerrors(); 00263 pbuf_free(p); 00264 goto end; 00265 } 00266 } 00267 #endif /* CHECKSUM_CHECK_UDP */ 00268 } 00269 if(pbuf_header(p, -UDP_HLEN)) { 00270 /* Can we cope with this failing? Just assert for now */ 00271 LWIP_ASSERT("pbuf_header failed\n", 0); 00272 UDP_STATS_INC(udp.drop); 00273 snmp_inc_udpinerrors(); 00274 pbuf_free(p); 00275 goto end; 00276 } 00277 if (pcb != NULL) { 00278 snmp_inc_udpindatagrams(); 00279 #if SO_REUSE && SO_REUSE_RXTOALL 00280 if ((broadcast || ip_addr_ismulticast(¤t_iphdr_dest)) && 00281 ((pcb->so_options & SOF_REUSEADDR) != 0)) { 00282 /* pass broadcast- or multicast packets to all multicast pcbs 00283 if SOF_REUSEADDR is set on the first match */ 00284 struct udp_pcb *mpcb; 00285 u8_t p_header_changed = 0; 00286 for (mpcb = udp_pcbs; mpcb != NULL; mpcb = mpcb->next) { 00287 if (mpcb != pcb) { 00288 /* compare PCB local addr+port to UDP destination addr+port */ 00289 if ((mpcb->local_port == dest) && 00290 ((!broadcast && ip_addr_isany(&mpcb->local_ip)) || 00291 ip_addr_cmp(&(mpcb->local_ip), ¤t_iphdr_dest) || 00292 #if LWIP_IGMP 00293 ip_addr_ismulticast(¤t_iphdr_dest) || 00294 #endif /* LWIP_IGMP */ 00295 #if IP_SOF_BROADCAST_RECV 00296 (broadcast && (mpcb->so_options & SOF_BROADCAST)))) { 00297 #else /* IP_SOF_BROADCAST_RECV */ 00298 (broadcast))) { 00299 #endif /* IP_SOF_BROADCAST_RECV */ 00300 /* pass a copy of the packet to all local matches */ 00301 if (mpcb->recv != NULL) { 00302 struct pbuf *q; 00303 /* for that, move payload to IP header again */ 00304 if (p_header_changed == 0) { 00305 pbuf_header(p, (s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN)); 00306 p_header_changed = 1; 00307 } 00308 q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM); 00309 if (q != NULL) { 00310 err_t err = pbuf_copy(q, p); 00311 if (err == ERR_OK) { 00312 /* move payload to UDP data */ 00313 pbuf_header(q, -(s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN)); 00314 mpcb->recv(mpcb->recv_arg, mpcb, q, ip_current_src_addr(), src); 00315 } 00316 } 00317 } 00318 } 00319 } 00320 } 00321 if (p_header_changed) { 00322 /* and move payload to UDP data again */ 00323 pbuf_header(p, -(s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN)); 00324 } 00325 } 00326 #endif /* SO_REUSE && SO_REUSE_RXTOALL */ 00327 /* callback */ 00328 if (pcb->recv != NULL) { 00329 /* now the recv function is responsible for freeing p */ 00330 pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr(), src); 00331 } else { 00332 /* no recv function registered? then we have to free the pbuf! */ 00333 pbuf_free(p); 00334 goto end; 00335 } 00336 } else { 00337 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n")); 00338 00339 #if LWIP_ICMP 00340 /* No match was found, send ICMP destination port unreachable unless 00341 destination address was broadcast/multicast. */ 00342 if (!broadcast && 00343 !ip_addr_ismulticast(¤t_iphdr_dest)) { 00344 /* move payload pointer back to ip header */ 00345 pbuf_header(p, (IPH_HL(iphdr) * 4) + UDP_HLEN); 00346 LWIP_ASSERT("p->payload == iphdr", (p->payload == iphdr)); 00347 icmp_dest_unreach(p, ICMP_DUR_PORT); 00348 } 00349 #endif /* LWIP_ICMP */ 00350 UDP_STATS_INC(udp.proterr); 00351 UDP_STATS_INC(udp.drop); 00352 snmp_inc_udpnoports(); 00353 pbuf_free(p); 00354 } 00355 } else { 00356 pbuf_free(p); 00357 } 00358 end: 00359 PERF_STOP("udp_input"); 00360 } 00361 00362 /** 00363 * Send data using UDP. 00364 * 00365 * @param pcb UDP PCB used to send the data. 00366 * @param p chain of pbuf's to be sent. 00367 * 00368 * The datagram will be sent to the current remote_ip & remote_port 00369 * stored in pcb. If the pcb is not bound to a port, it will 00370 * automatically be bound to a random port. 00371 * 00372 * @return lwIP error code. 00373 * - ERR_OK. Successful. No error occured. 00374 * - ERR_MEM. Out of memory. 00375 * - ERR_RTE. Could not find route to destination address. 00376 * - More errors could be returned by lower protocol layers. 00377 * 00378 * @see udp_disconnect() udp_sendto() 00379 */ 00380 err_t 00381 udp_send(struct udp_pcb *pcb, struct pbuf *p) 00382 { 00383 /* send to the packet using remote ip and port stored in the pcb */ 00384 return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port); 00385 } 00386 00387 #if LWIP_CHECKSUM_ON_COPY 00388 /** Same as udp_send() but with checksum 00389 */ 00390 err_t 00391 udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p, 00392 u8_t have_chksum, u16_t chksum) 00393 { 00394 /* send to the packet using remote ip and port stored in the pcb */ 00395 return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port, 00396 have_chksum, chksum); 00397 } 00398 #endif /* LWIP_CHECKSUM_ON_COPY */ 00399 00400 /** 00401 * Send data to a specified address using UDP. 00402 * 00403 * @param pcb UDP PCB used to send the data. 00404 * @param p chain of pbuf's to be sent. 00405 * @param dst_ip Destination IP address. 00406 * @param dst_port Destination UDP port. 00407 * 00408 * dst_ip & dst_port are expected to be in the same byte order as in the pcb. 00409 * 00410 * If the PCB already has a remote address association, it will 00411 * be restored after the data is sent. 00412 * 00413 * @return lwIP error code (@see udp_send for possible error codes) 00414 * 00415 * @see udp_disconnect() udp_send() 00416 */ 00417 err_t 00418 udp_sendto(struct udp_pcb *pcb, struct pbuf *p, 00419 ip_addr_t *dst_ip, u16_t dst_port) 00420 { 00421 #if LWIP_CHECKSUM_ON_COPY 00422 return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0); 00423 } 00424 00425 /** Same as udp_sendto(), but with checksum */ 00426 err_t 00427 udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip, 00428 u16_t dst_port, u8_t have_chksum, u16_t chksum) 00429 { 00430 #endif /* LWIP_CHECKSUM_ON_COPY */ 00431 struct netif *netif; 00432 00433 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send\n")); 00434 00435 /* find the outgoing network interface for this packet */ 00436 #if LWIP_IGMP 00437 netif = ip_route((ip_addr_ismulticast(dst_ip))?(&(pcb->multicast_ip)):(dst_ip)); 00438 #else 00439 netif = ip_route(dst_ip); 00440 #endif /* LWIP_IGMP */ 00441 00442 /* no outgoing network interface could be found? */ 00443 if (netif == NULL) { 00444 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", 00445 ip4_addr1_16(dst_ip), ip4_addr2_16(dst_ip), ip4_addr3_16(dst_ip), ip4_addr4_16(dst_ip))); 00446 UDP_STATS_INC(udp.rterr); 00447 return ERR_RTE; 00448 } 00449 #if LWIP_CHECKSUM_ON_COPY 00450 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum); 00451 #else /* LWIP_CHECKSUM_ON_COPY */ 00452 return udp_sendto_if(pcb, p, dst_ip, dst_port, netif); 00453 #endif /* LWIP_CHECKSUM_ON_COPY */ 00454 } 00455 00456 /** 00457 * Send data to a specified address using UDP. 00458 * The netif used for sending can be specified. 00459 * 00460 * This function exists mainly for DHCP, to be able to send UDP packets 00461 * on a netif that is still down. 00462 * 00463 * @param pcb UDP PCB used to send the data. 00464 * @param p chain of pbuf's to be sent. 00465 * @param dst_ip Destination IP address. 00466 * @param dst_port Destination UDP port. 00467 * @param netif the netif used for sending. 00468 * 00469 * dst_ip & dst_port are expected to be in the same byte order as in the pcb. 00470 * 00471 * @return lwIP error code (@see udp_send for possible error codes) 00472 * 00473 * @see udp_disconnect() udp_send() 00474 */ 00475 err_t 00476 udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p, 00477 ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif) 00478 { 00479 #if LWIP_CHECKSUM_ON_COPY 00480 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0); 00481 } 00482 00483 /** Same as udp_sendto_if(), but with checksum */ 00484 err_t 00485 udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip, 00486 u16_t dst_port, struct netif *netif, u8_t have_chksum, 00487 u16_t chksum) 00488 { 00489 #endif /* LWIP_CHECKSUM_ON_COPY */ 00490 struct udp_hdr *udphdr; 00491 ip_addr_t *src_ip; 00492 err_t err; 00493 struct pbuf *q; /* q will be sent down the stack */ 00494 00495 #if IP_SOF_BROADCAST 00496 /* broadcast filter? */ 00497 if ( ((pcb->so_options & SOF_BROADCAST) == 0) && ip_addr_isbroadcast(dst_ip, netif) ) { 00498 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, 00499 ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb)); 00500 return ERR_VAL; 00501 } 00502 #endif /* IP_SOF_BROADCAST */ 00503 00504 /* if the PCB is not yet bound to a port, bind it here */ 00505 if (pcb->local_port == 0) { 00506 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n")); 00507 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port); 00508 if (err != ERR_OK) { 00509 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n")); 00510 return err; 00511 } 00512 } 00513 00514 /* not enough space to add an UDP header to first pbuf in given p chain? */ 00515 if (pbuf_header(p, UDP_HLEN)) { 00516 /* allocate header in a separate new pbuf */ 00517 q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM); 00518 /* new header pbuf could not be allocated? */ 00519 if (q == NULL) { 00520 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n")); 00521 return ERR_MEM; 00522 } 00523 /* chain header q in front of given pbuf p */ 00524 pbuf_chain(q, p); 00525 /* first pbuf q points to header pbuf */ 00526 LWIP_DEBUGF(UDP_DEBUG, 00527 ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p)); 00528 } else { 00529 /* adding space for header within p succeeded */ 00530 /* first pbuf q equals given pbuf */ 00531 q = p; 00532 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p)); 00533 } 00534 LWIP_ASSERT("check that first pbuf can hold struct udp_hdr", 00535 (q->len >= sizeof(struct udp_hdr))); 00536 /* q now represents the packet to be sent */ 00537 udphdr = (struct udp_hdr *)q->payload; 00538 udphdr->src = htons(pcb->local_port); 00539 udphdr->dest = htons(dst_port); 00540 /* in UDP, 0 checksum means 'no checksum' */ 00541 udphdr->chksum = 0x0000; 00542 00543 /* Multicast Loop? */ 00544 #if LWIP_IGMP 00545 if (ip_addr_ismulticast(dst_ip) && ((pcb->flags & UDP_FLAGS_MULTICAST_LOOP) != 0)) { 00546 q->flags |= PBUF_FLAG_MCASTLOOP; 00547 } 00548 #endif /* LWIP_IGMP */ 00549 00550 00551 /* PCB local address is IP_ANY_ADDR? */ 00552 if (ip_addr_isany(&pcb->local_ip)) { 00553 /* use outgoing network interface IP address as source address */ 00554 src_ip = &(netif->ip_addr); 00555 } else { 00556 /* check if UDP PCB local IP address is correct 00557 * this could be an old address if netif->ip_addr has changed */ 00558 if (!ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) { 00559 /* local_ip doesn't match, drop the packet */ 00560 if (q != p) { 00561 /* free the header pbuf */ 00562 pbuf_free(q); 00563 q = NULL; 00564 /* p is still referenced by the caller, and will live on */ 00565 } 00566 return ERR_VAL; 00567 } 00568 /* use UDP PCB local IP address as source address */ 00569 src_ip = &(pcb->local_ip); 00570 } 00571 00572 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len)); 00573 00574 #if LWIP_UDPLITE 00575 /* UDP Lite protocol? */ 00576 if (pcb->flags & UDP_FLAGS_UDPLITE) { 00577 u16_t chklen, chklen_hdr; 00578 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len)); 00579 /* set UDP message length in UDP header */ 00580 chklen_hdr = chklen = pcb->chksum_len_tx; 00581 if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) { 00582 if (chklen != 0) { 00583 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen)); 00584 } 00585 /* For UDP-Lite, checksum length of 0 means checksum 00586 over the complete packet. (See RFC 3828 chap. 3.1) 00587 At least the UDP-Lite header must be covered by the 00588 checksum, therefore, if chksum_len has an illegal 00589 value, we generate the checksum over the complete 00590 packet to be safe. */ 00591 chklen_hdr = 0; 00592 chklen = q->tot_len; 00593 } 00594 udphdr->len = htons(chklen_hdr); 00595 /* calculate checksum */ 00596 #if CHECKSUM_GEN_UDP 00597 udphdr->chksum = inet_chksum_pseudo_partial(q, src_ip, dst_ip, 00598 IP_PROTO_UDPLITE, q->tot_len, 00599 #if !LWIP_CHECKSUM_ON_COPY 00600 chklen); 00601 #else /* !LWIP_CHECKSUM_ON_COPY */ 00602 (have_chksum ? UDP_HLEN : chklen)); 00603 if (have_chksum) { 00604 u32_t acc; 00605 acc = udphdr->chksum + (u16_t)~(chksum); 00606 udphdr->chksum = FOLD_U32T(acc); 00607 } 00608 #endif /* !LWIP_CHECKSUM_ON_COPY */ 00609 00610 /* chksum zero must become 0xffff, as zero means 'no checksum' */ 00611 if (udphdr->chksum == 0x0000) { 00612 udphdr->chksum = 0xffff; 00613 } 00614 #endif /* CHECKSUM_GEN_UDP */ 00615 /* output to IP */ 00616 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDPLITE,)\n")); 00617 #if LWIP_NETIF_HWADDRHINT 00618 netif->addr_hint = &(pcb->addr_hint); 00619 #endif /* LWIP_NETIF_HWADDRHINT*/ 00620 err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDPLITE, netif); 00621 #if LWIP_NETIF_HWADDRHINT 00622 netif->addr_hint = NULL; 00623 #endif /* LWIP_NETIF_HWADDRHINT*/ 00624 } else 00625 #endif /* LWIP_UDPLITE */ 00626 { /* UDP */ 00627 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len)); 00628 udphdr->len = htons(q->tot_len); 00629 /* calculate checksum */ 00630 #if CHECKSUM_GEN_UDP 00631 if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) { 00632 u16_t udpchksum; 00633 #if LWIP_CHECKSUM_ON_COPY 00634 if (have_chksum) { 00635 u32_t acc; 00636 udpchksum = inet_chksum_pseudo_partial(q, src_ip, dst_ip, IP_PROTO_UDP, 00637 q->tot_len, UDP_HLEN); 00638 acc = udpchksum + (u16_t)~(chksum); 00639 udpchksum = FOLD_U32T(acc); 00640 } else 00641 #endif /* LWIP_CHECKSUM_ON_COPY */ 00642 { 00643 udpchksum = inet_chksum_pseudo(q, src_ip, dst_ip, IP_PROTO_UDP, q->tot_len); 00644 } 00645 00646 /* chksum zero must become 0xffff, as zero means 'no checksum' */ 00647 if (udpchksum == 0x0000) { 00648 udpchksum = 0xffff; 00649 } 00650 udphdr->chksum = udpchksum; 00651 } 00652 #endif /* CHECKSUM_GEN_UDP */ 00653 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum)); 00654 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n")); 00655 /* output to IP */ 00656 #if LWIP_NETIF_HWADDRHINT 00657 netif->addr_hint = &(pcb->addr_hint); 00658 #endif /* LWIP_NETIF_HWADDRHINT*/ 00659 err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif); 00660 #if LWIP_NETIF_HWADDRHINT 00661 netif->addr_hint = NULL; 00662 #endif /* LWIP_NETIF_HWADDRHINT*/ 00663 } 00664 /* TODO: must this be increased even if error occured? */ 00665 snmp_inc_udpoutdatagrams(); 00666 00667 /* did we chain a separate header pbuf earlier? */ 00668 if (q != p) { 00669 /* free the header pbuf */ 00670 pbuf_free(q); 00671 q = NULL; 00672 /* p is still referenced by the caller, and will live on */ 00673 } 00674 00675 UDP_STATS_INC(udp.xmit); 00676 return err; 00677 } 00678 00679 /** 00680 * Bind an UDP PCB. 00681 * 00682 * @param pcb UDP PCB to be bound with a local address ipaddr and port. 00683 * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to 00684 * bind to all local interfaces. 00685 * @param port local UDP port to bind with. Use 0 to automatically bind 00686 * to a random port between UDP_LOCAL_PORT_RANGE_START and 00687 * UDP_LOCAL_PORT_RANGE_END. 00688 * 00689 * ipaddr & port are expected to be in the same byte order as in the pcb. 00690 * 00691 * @return lwIP error code. 00692 * - ERR_OK. Successful. No error occured. 00693 * - ERR_USE. The specified ipaddr and port are already bound to by 00694 * another UDP PCB. 00695 * 00696 * @see udp_disconnect() 00697 */ 00698 err_t 00699 udp_bind(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port) 00700 { 00701 struct udp_pcb *ipcb; 00702 u8_t rebind; 00703 00704 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = ")); 00705 ip_addr_debug_print(UDP_DEBUG, ipaddr); 00706 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port)); 00707 00708 rebind = 0; 00709 /* Check for double bind and rebind of the same pcb */ 00710 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) { 00711 /* is this UDP PCB already on active list? */ 00712 if (pcb == ipcb) { 00713 /* pcb may occur at most once in active list */ 00714 LWIP_ASSERT("rebind == 0", rebind == 0); 00715 /* pcb already in list, just rebind */ 00716 rebind = 1; 00717 } 00718 00719 /* By default, we don't allow to bind to a port that any other udp 00720 PCB is alread bound to, unless *all* PCBs with that port have tha 00721 REUSEADDR flag set. */ 00722 #if SO_REUSE 00723 else if (((pcb->so_options & SOF_REUSEADDR) == 0) && 00724 ((ipcb->so_options & SOF_REUSEADDR) == 0)) { 00725 #else /* SO_REUSE */ 00726 /* port matches that of PCB in list and REUSEADDR not set -> reject */ 00727 else { 00728 #endif /* SO_REUSE */ 00729 if ((ipcb->local_port == port) && 00730 /* IP address matches, or one is IP_ADDR_ANY? */ 00731 (ip_addr_isany(&(ipcb->local_ip)) || 00732 ip_addr_isany(ipaddr) || 00733 ip_addr_cmp(&(ipcb->local_ip), ipaddr))) { 00734 /* other PCB already binds to this local IP and port */ 00735 LWIP_DEBUGF(UDP_DEBUG, 00736 ("udp_bind: local port %"U16_F" already bound by another pcb\n", port)); 00737 return ERR_USE; 00738 } 00739 } 00740 } 00741 00742 ip_addr_set(&pcb->local_ip, ipaddr); 00743 00744 /* no port specified? */ 00745 if (port == 0) { 00746 #ifndef UDP_LOCAL_PORT_RANGE_START 00747 #define UDP_LOCAL_PORT_RANGE_START 4096 00748 #define UDP_LOCAL_PORT_RANGE_END 0x7fff 00749 #endif 00750 port = UDP_LOCAL_PORT_RANGE_START; 00751 ipcb = udp_pcbs; 00752 while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) { 00753 if (ipcb->local_port == port) { 00754 /* port is already used by another udp_pcb */ 00755 port++; 00756 /* restart scanning all udp pcbs */ 00757 ipcb = udp_pcbs; 00758 } else { 00759 /* go on with next udp pcb */ 00760 ipcb = ipcb->next; 00761 } 00762 } 00763 if (ipcb != NULL) { 00764 /* no more ports available in local range */ 00765 LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n")); 00766 return ERR_USE; 00767 } 00768 } 00769 pcb->local_port = port; 00770 snmp_insert_udpidx_tree(pcb); 00771 /* pcb not active yet? */ 00772 if (rebind == 0) { 00773 /* place the PCB on the active list if not already there */ 00774 pcb->next = udp_pcbs; 00775 udp_pcbs = pcb; 00776 } 00777 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, 00778 ("udp_bind: bound to %"U16_F".%"U16_F".%"U16_F".%"U16_F", port %"U16_F"\n", 00779 ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip), 00780 ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip), 00781 pcb->local_port)); 00782 return ERR_OK; 00783 } 00784 /** 00785 * Connect an UDP PCB. 00786 * 00787 * This will associate the UDP PCB with the remote address. 00788 * 00789 * @param pcb UDP PCB to be connected with remote address ipaddr and port. 00790 * @param ipaddr remote IP address to connect with. 00791 * @param port remote UDP port to connect with. 00792 * 00793 * @return lwIP error code 00794 * 00795 * ipaddr & port are expected to be in the same byte order as in the pcb. 00796 * 00797 * The udp pcb is bound to a random local port if not already bound. 00798 * 00799 * @see udp_disconnect() 00800 */ 00801 err_t 00802 udp_connect(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port) 00803 { 00804 struct udp_pcb *ipcb; 00805 00806 if (pcb->local_port == 0) { 00807 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port); 00808 if (err != ERR_OK) { 00809 return err; 00810 } 00811 } 00812 00813 ip_addr_set(&pcb->remote_ip, ipaddr); 00814 pcb->remote_port = port; 00815 pcb->flags |= UDP_FLAGS_CONNECTED; 00816 /** TODO: this functionality belongs in upper layers */ 00817 #ifdef LWIP_UDP_TODO 00818 /* Nail down local IP for netconn_addr()/getsockname() */ 00819 if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) { 00820 struct netif *netif; 00821 00822 if ((netif = ip_route(&(pcb->remote_ip))) == NULL) { 00823 LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n", pcb->remote_ip.addr)); 00824 UDP_STATS_INC(udp.rterr); 00825 return ERR_RTE; 00826 } 00827 /** TODO: this will bind the udp pcb locally, to the interface which 00828 is used to route output packets to the remote address. However, we 00829 might want to accept incoming packets on any interface! */ 00830 pcb->local_ip = netif->ip_addr; 00831 } else if (ip_addr_isany(&pcb->remote_ip)) { 00832 pcb->local_ip.addr = 0; 00833 } 00834 #endif 00835 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, 00836 ("udp_connect: connected to %"U16_F".%"U16_F".%"U16_F".%"U16_F",port %"U16_F"\n", 00837 ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip), 00838 ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip), 00839 pcb->local_port)); 00840 00841 /* Insert UDP PCB into the list of active UDP PCBs. */ 00842 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) { 00843 if (pcb == ipcb) { 00844 /* already on the list, just return */ 00845 return ERR_OK; 00846 } 00847 } 00848 /* PCB not yet on the list, add PCB now */ 00849 pcb->next = udp_pcbs; 00850 udp_pcbs = pcb; 00851 return ERR_OK; 00852 } 00853 00854 /** 00855 * Disconnect a UDP PCB 00856 * 00857 * @param pcb the udp pcb to disconnect. 00858 */ 00859 void 00860 udp_disconnect(struct udp_pcb *pcb) 00861 { 00862 /* reset remote address association */ 00863 ip_addr_set_any(&pcb->remote_ip); 00864 pcb->remote_port = 0; 00865 /* mark PCB as unconnected */ 00866 pcb->flags &= ~UDP_FLAGS_CONNECTED; 00867 } 00868 00869 /** 00870 * Set a receive callback for a UDP PCB 00871 * 00872 * This callback will be called when receiving a datagram for the pcb. 00873 * 00874 * @param pcb the pcb for wich to set the recv callback 00875 * @param recv function pointer of the callback function 00876 * @param recv_arg additional argument to pass to the callback function 00877 */ 00878 void 00879 udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg) 00880 { 00881 /* remember recv() callback and user data */ 00882 pcb->recv = recv; 00883 pcb->recv_arg = recv_arg; 00884 } 00885 00886 /** 00887 * Remove an UDP PCB. 00888 * 00889 * @param pcb UDP PCB to be removed. The PCB is removed from the list of 00890 * UDP PCB's and the data structure is freed from memory. 00891 * 00892 * @see udp_new() 00893 */ 00894 void 00895 udp_remove(struct udp_pcb *pcb) 00896 { 00897 struct udp_pcb *pcb2; 00898 00899 snmp_delete_udpidx_tree(pcb); 00900 /* pcb to be removed is first in list? */ 00901 if (udp_pcbs == pcb) { 00902 /* make list start at 2nd pcb */ 00903 udp_pcbs = udp_pcbs->next; 00904 /* pcb not 1st in list */ 00905 } else { 00906 for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) { 00907 /* find pcb in udp_pcbs list */ 00908 if (pcb2->next != NULL && pcb2->next == pcb) { 00909 /* remove pcb from list */ 00910 pcb2->next = pcb->next; 00911 } 00912 } 00913 } 00914 memp_free(MEMP_UDP_PCB, pcb); 00915 } 00916 00917 /** 00918 * Create a UDP PCB. 00919 * 00920 * @return The UDP PCB which was created. NULL if the PCB data structure 00921 * could not be allocated. 00922 * 00923 * @see udp_remove() 00924 */ 00925 struct udp_pcb * 00926 udp_new(void) 00927 { 00928 struct udp_pcb *pcb; 00929 pcb = (struct udp_pcb *)memp_malloc(MEMP_UDP_PCB); 00930 /* could allocate UDP PCB? */ 00931 if (pcb != NULL) { 00932 /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0 00933 * which means checksum is generated over the whole datagram per default 00934 * (recommended as default by RFC 3828). */ 00935 /* initialize PCB to all zeroes */ 00936 memset(pcb, 0, sizeof(struct udp_pcb)); 00937 pcb->ttl = UDP_TTL; 00938 } 00939 return pcb; 00940 } 00941 00942 #if UDP_DEBUG 00943 /** 00944 * Print UDP header information for debug purposes. 00945 * 00946 * @param udphdr pointer to the udp header in memory. 00947 */ 00948 void 00949 udp_debug_print(struct udp_hdr *udphdr) 00950 { 00951 LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n")); 00952 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); 00953 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n", 00954 ntohs(udphdr->src), ntohs(udphdr->dest))); 00955 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); 00956 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n", 00957 ntohs(udphdr->len), ntohs(udphdr->chksum))); 00958 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); 00959 } 00960 #endif /* UDP_DEBUG */ 00961 00962 #endif /* LWIP_UDP */
Generated on Tue Jul 12 2022 11:52:58 by 1.7.2