Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
lwip_udp.c
00001 /** 00002 * @file 00003 * User Datagram Protocol module\n 00004 * The code for the User Datagram Protocol UDP & UDPLite (RFC 3828).\n 00005 * See also @ref udp_raw 00006 * 00007 * @defgroup udp_raw UDP 00008 * @ingroup callbackstyle_api 00009 * User Datagram Protocol module\n 00010 * @see @ref api 00011 */ 00012 00013 /* 00014 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 00015 * All rights reserved. 00016 * 00017 * Redistribution and use in source and binary forms, with or without modification, 00018 * are permitted provided that the following conditions are met: 00019 * 00020 * 1. Redistributions of source code must retain the above copyright notice, 00021 * this list of conditions and the following disclaimer. 00022 * 2. Redistributions in binary form must reproduce the above copyright notice, 00023 * this list of conditions and the following disclaimer in the documentation 00024 * and/or other materials provided with the distribution. 00025 * 3. The name of the author may not be used to endorse or promote products 00026 * derived from this software without specific prior written permission. 00027 * 00028 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00029 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00030 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00031 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00032 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00033 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00034 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00035 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00036 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00037 * OF SUCH DAMAGE. 00038 * 00039 * This file is part of the lwIP TCP/IP stack. 00040 * 00041 * Author: Adam Dunkels <adam@sics.se> 00042 * 00043 */ 00044 00045 /* @todo Check the use of '(struct udp_pcb).chksum_len_rx'! 00046 */ 00047 00048 #include "lwip/opt.h" 00049 00050 #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */ 00051 00052 #include "lwip/udp.h" 00053 #include "lwip/def.h" 00054 #include "lwip/memp.h" 00055 #include "lwip/inet_chksum.h" 00056 #include "lwip/ip_addr.h" 00057 #include "lwip/ip6.h" 00058 #include "lwip/ip6_addr.h" 00059 #include "lwip/netif.h" 00060 #include "lwip/icmp.h" 00061 #include "lwip/icmp6.h" 00062 #include "lwip/stats.h" 00063 #include "lwip/snmp.h" 00064 #include "lwip/dhcp.h" 00065 00066 #include <string.h> 00067 00068 #ifndef UDP_LOCAL_PORT_RANGE_START 00069 /* From http://www.iana.org/assignments/port-numbers: 00070 "The Dynamic and/or Private Ports are those from 49152 through 65535" */ 00071 #define UDP_LOCAL_PORT_RANGE_START 0xc000 00072 #define UDP_LOCAL_PORT_RANGE_END 0xffff 00073 #define UDP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & (u16_t)~UDP_LOCAL_PORT_RANGE_START) + UDP_LOCAL_PORT_RANGE_START)) 00074 #endif 00075 00076 /* last local UDP port */ 00077 static u16_t udp_port = UDP_LOCAL_PORT_RANGE_START; 00078 00079 /* The list of UDP PCBs */ 00080 /* exported in udp.h (was static) */ 00081 struct udp_pcb *udp_pcbs; 00082 00083 /** 00084 * Initialize this module. 00085 */ 00086 void 00087 udp_init(void) 00088 { 00089 #ifdef LWIP_RAND 00090 udp_port = UDP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND()); 00091 #endif /* LWIP_RAND */ 00092 } 00093 00094 /** 00095 * Allocate a new local UDP port. 00096 * 00097 * @return a new (free) local UDP port number 00098 */ 00099 static u16_t 00100 udp_new_port(void) 00101 { 00102 u16_t n = 0; 00103 struct udp_pcb *pcb; 00104 00105 again: 00106 if (udp_port++ == UDP_LOCAL_PORT_RANGE_END) { 00107 udp_port = UDP_LOCAL_PORT_RANGE_START; 00108 } 00109 /* Check all PCBs. */ 00110 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) { 00111 if (pcb->local_port == udp_port) { 00112 if (++n > (UDP_LOCAL_PORT_RANGE_END - UDP_LOCAL_PORT_RANGE_START)) { 00113 return 0; 00114 } 00115 goto again; 00116 } 00117 } 00118 return udp_port; 00119 } 00120 00121 /** Common code to see if the current input packet matches the pcb 00122 * (current input packet is accessed via ip(4/6)_current_* macros) 00123 * 00124 * @param pcb pcb to check 00125 * @param inp network interface on which the datagram was received (only used for IPv4) 00126 * @param broadcast 1 if his is an IPv4 broadcast (global or subnet-only), 0 otherwise (only used for IPv4) 00127 * @return 1 on match, 0 otherwise 00128 */ 00129 static u8_t 00130 udp_input_local_match(struct udp_pcb *pcb, struct netif *inp, u8_t broadcast) 00131 { 00132 LWIP_UNUSED_ARG(inp); /* in IPv6 only case */ 00133 LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */ 00134 00135 LWIP_ASSERT("udp_input_local_match: invalid pcb", pcb != NULL); 00136 LWIP_ASSERT("udp_input_local_match: invalid netif", inp != NULL); 00137 00138 /* check if PCB is bound to specific netif */ 00139 if ((pcb->netif_idx != NETIF_NO_INDEX) && 00140 (pcb->netif_idx != netif_get_index(ip_data.current_input_netif))) { 00141 return 0; 00142 } 00143 00144 /* Dual-stack: PCBs listening to any IP type also listen to any IP address */ 00145 if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) { 00146 #if LWIP_IPV4 && IP_SOF_BROADCAST_RECV 00147 if ((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) { 00148 return 0; 00149 } 00150 #endif /* LWIP_IPV4 && IP_SOF_BROADCAST_RECV */ 00151 return 1; 00152 } 00153 00154 /* Only need to check PCB if incoming IP version matches PCB IP version */ 00155 if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) { 00156 #if LWIP_IPV4 00157 /* Special case: IPv4 broadcast: all or broadcasts in my subnet 00158 * Note: broadcast variable can only be 1 if it is an IPv4 broadcast */ 00159 if (broadcast != 0) { 00160 #if IP_SOF_BROADCAST_RECV 00161 if (ip_get_option(pcb, SOF_BROADCAST)) 00162 #endif /* IP_SOF_BROADCAST_RECV */ 00163 { 00164 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) || 00165 ((ip4_current_dest_addr()->addr == IPADDR_BROADCAST)) || 00166 ip4_addr_netcmp(ip_2_ip4(&pcb->local_ip), ip4_current_dest_addr(), netif_ip4_netmask(inp))) { 00167 return 1; 00168 } 00169 } 00170 } else 00171 #endif /* LWIP_IPV4 */ 00172 /* Handle IPv4 and IPv6: all or exact match */ 00173 if (ip_addr_isany(&pcb->local_ip) || ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) { 00174 return 1; 00175 } 00176 } 00177 00178 return 0; 00179 } 00180 00181 /** 00182 * Process an incoming UDP datagram. 00183 * 00184 * Given an incoming UDP datagram (as a chain of pbufs) this function 00185 * finds a corresponding UDP PCB and hands over the pbuf to the pcbs 00186 * recv function. If no pcb is found or the datagram is incorrect, the 00187 * pbuf is freed. 00188 * 00189 * @param p pbuf to be demultiplexed to a UDP PCB (p->payload pointing to the UDP header) 00190 * @param inp network interface on which the datagram was received. 00191 * 00192 */ 00193 void 00194 udp_input(struct pbuf *p, struct netif *inp) 00195 { 00196 struct udp_hdr *udphdr; 00197 struct udp_pcb *pcb, *prev; 00198 struct udp_pcb *uncon_pcb; 00199 u16_t src, dest; 00200 u8_t broadcast; 00201 u8_t for_us = 0; 00202 00203 LWIP_UNUSED_ARG(inp); 00204 00205 LWIP_ASSERT_CORE_LOCKED(); 00206 00207 LWIP_ASSERT("udp_input: invalid pbuf", p != NULL); 00208 LWIP_ASSERT("udp_input: invalid netif", inp != NULL); 00209 00210 PERF_START; 00211 00212 UDP_STATS_INC(udp.recv); 00213 00214 /* Check minimum length (UDP header) */ 00215 if (p->len < UDP_HLEN) { 00216 /* drop short packets */ 00217 LWIP_DEBUGF(UDP_DEBUG, 00218 ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len)); 00219 UDP_STATS_INC(udp.lenerr); 00220 UDP_STATS_INC(udp.drop); 00221 MIB2_STATS_INC(mib2.udpinerrors); 00222 pbuf_free(p); 00223 goto end; 00224 } 00225 00226 udphdr = (struct udp_hdr *)p->payload; 00227 00228 /* is broadcast packet ? */ 00229 broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif()); 00230 00231 LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len)); 00232 00233 /* convert src and dest ports to host byte order */ 00234 src = lwip_ntohs(udphdr->src); 00235 dest = lwip_ntohs(udphdr->dest); 00236 00237 udp_debug_print(udphdr); 00238 00239 /* print the UDP source and destination */ 00240 LWIP_DEBUGF(UDP_DEBUG, ("udp (")); 00241 ip_addr_debug_print_val(UDP_DEBUG, *ip_current_dest_addr()); 00242 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", lwip_ntohs(udphdr->dest))); 00243 ip_addr_debug_print_val(UDP_DEBUG, *ip_current_src_addr()); 00244 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", lwip_ntohs(udphdr->src))); 00245 00246 pcb = NULL; 00247 prev = NULL; 00248 uncon_pcb = NULL; 00249 /* Iterate through the UDP pcb list for a matching pcb. 00250 * 'Perfect match' pcbs (connected to the remote port & ip address) are 00251 * preferred. If no perfect match is found, the first unconnected pcb that 00252 * matches the local port and ip address gets the datagram. */ 00253 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) { 00254 /* print the PCB local and remote address */ 00255 LWIP_DEBUGF(UDP_DEBUG, ("pcb (")); 00256 ip_addr_debug_print_val(UDP_DEBUG, pcb->local_ip); 00257 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", pcb->local_port)); 00258 ip_addr_debug_print_val(UDP_DEBUG, pcb->remote_ip); 00259 LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", pcb->remote_port)); 00260 00261 /* compare PCB local addr+port to UDP destination addr+port */ 00262 if ((pcb->local_port == dest) && 00263 (udp_input_local_match(pcb, inp, broadcast) != 0)) { 00264 if ((pcb->flags & UDP_FLAGS_CONNECTED) == 0) { 00265 if (uncon_pcb == NULL) { 00266 /* the first unconnected matching PCB */ 00267 uncon_pcb = pcb; 00268 #if LWIP_IPV4 00269 } else if (broadcast && ip4_current_dest_addr()->addr == IPADDR_BROADCAST) { 00270 /* global broadcast address (only valid for IPv4; match was checked before) */ 00271 if (!IP_IS_V4_VAL(uncon_pcb->local_ip) || !ip4_addr_cmp(ip_2_ip4(&uncon_pcb->local_ip), netif_ip4_addr(inp))) { 00272 /* uncon_pcb does not match the input netif, check this pcb */ 00273 if (IP_IS_V4_VAL(pcb->local_ip) && ip4_addr_cmp(ip_2_ip4(&pcb->local_ip), netif_ip4_addr(inp))) { 00274 /* better match */ 00275 uncon_pcb = pcb; 00276 } 00277 } 00278 #endif /* LWIP_IPV4 */ 00279 } 00280 #if SO_REUSE 00281 else if (!ip_addr_isany(&pcb->local_ip)) { 00282 /* prefer specific IPs over catch-all */ 00283 uncon_pcb = pcb; 00284 } 00285 #endif /* SO_REUSE */ 00286 } 00287 00288 /* compare PCB remote addr+port to UDP source addr+port */ 00289 if ((pcb->remote_port == src) && 00290 (ip_addr_isany_val(pcb->remote_ip) || 00291 ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()))) { 00292 /* the first fully matching PCB */ 00293 if (prev != NULL) { 00294 /* move the pcb to the front of udp_pcbs so that is 00295 found faster next time */ 00296 prev->next = pcb->next; 00297 pcb->next = udp_pcbs; 00298 udp_pcbs = pcb; 00299 } else { 00300 UDP_STATS_INC(udp.cachehit); 00301 } 00302 break; 00303 } 00304 } 00305 00306 prev = pcb; 00307 } 00308 /* no fully matching pcb found? then look for an unconnected pcb */ 00309 if (pcb == NULL) { 00310 pcb = uncon_pcb; 00311 } 00312 00313 /* Check checksum if this is a match or if it was directed at us. */ 00314 if (pcb != NULL) { 00315 for_us = 1; 00316 } else { 00317 #if LWIP_IPV6 00318 if (ip_current_is_v6()) { 00319 for_us = netif_get_ip6_addr_match(inp, ip6_current_dest_addr()) >= 0; 00320 } 00321 #endif /* LWIP_IPV6 */ 00322 #if LWIP_IPV4 00323 if (!ip_current_is_v6()) { 00324 for_us = ip4_addr_cmp(netif_ip4_addr(inp), ip4_current_dest_addr()); 00325 } 00326 #endif /* LWIP_IPV4 */ 00327 } 00328 00329 if (for_us) { 00330 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n")); 00331 #if CHECKSUM_CHECK_UDP 00332 IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_UDP) { 00333 #if LWIP_UDPLITE 00334 if (ip_current_header_proto() == IP_PROTO_UDPLITE) { 00335 /* Do the UDP Lite checksum */ 00336 u16_t chklen = lwip_ntohs(udphdr->len); 00337 if (chklen < sizeof(struct udp_hdr)) { 00338 if (chklen == 0) { 00339 /* For UDP-Lite, checksum length of 0 means checksum 00340 over the complete packet (See RFC 3828 chap. 3.1) */ 00341 chklen = p->tot_len; 00342 } else { 00343 /* At least the UDP-Lite header must be covered by the 00344 checksum! (Again, see RFC 3828 chap. 3.1) */ 00345 goto chkerr; 00346 } 00347 } 00348 if (ip_chksum_pseudo_partial(p, IP_PROTO_UDPLITE, 00349 p->tot_len, chklen, 00350 ip_current_src_addr(), ip_current_dest_addr()) != 0) { 00351 goto chkerr; 00352 } 00353 } else 00354 #endif /* LWIP_UDPLITE */ 00355 { 00356 if (udphdr->chksum != 0) { 00357 if (ip_chksum_pseudo(p, IP_PROTO_UDP, p->tot_len, 00358 ip_current_src_addr(), 00359 ip_current_dest_addr()) != 0) { 00360 goto chkerr; 00361 } 00362 } 00363 } 00364 } 00365 #endif /* CHECKSUM_CHECK_UDP */ 00366 if (pbuf_remove_header(p, UDP_HLEN)) { 00367 /* Can we cope with this failing? Just assert for now */ 00368 LWIP_ASSERT("pbuf_remove_header failed\n", 0); 00369 UDP_STATS_INC(udp.drop); 00370 MIB2_STATS_INC(mib2.udpinerrors); 00371 pbuf_free(p); 00372 goto end; 00373 } 00374 00375 if (pcb != NULL) { 00376 MIB2_STATS_INC(mib2.udpindatagrams); 00377 #if SO_REUSE && SO_REUSE_RXTOALL 00378 if (ip_get_option(pcb, SOF_REUSEADDR) && 00379 (broadcast || ip_addr_ismulticast(ip_current_dest_addr()))) { 00380 /* pass broadcast- or multicast packets to all multicast pcbs 00381 if SOF_REUSEADDR is set on the first match */ 00382 struct udp_pcb *mpcb; 00383 for (mpcb = udp_pcbs; mpcb != NULL; mpcb = mpcb->next) { 00384 if (mpcb != pcb) { 00385 /* compare PCB local addr+port to UDP destination addr+port */ 00386 if ((mpcb->local_port == dest) && 00387 (udp_input_local_match(mpcb, inp, broadcast) != 0)) { 00388 /* pass a copy of the packet to all local matches */ 00389 if (mpcb->recv != NULL) { 00390 struct pbuf *q; 00391 q = pbuf_clone(PBUF_RAW, PBUF_POOL, p); 00392 if (q != NULL) { 00393 mpcb->recv(mpcb->recv_arg, mpcb, q, ip_current_src_addr(), src); 00394 } 00395 } 00396 } 00397 } 00398 } 00399 } 00400 #endif /* SO_REUSE && SO_REUSE_RXTOALL */ 00401 /* callback */ 00402 if (pcb->recv != NULL) { 00403 /* now the recv function is responsible for freeing p */ 00404 pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr(), src); 00405 } else { 00406 /* no recv function registered? then we have to free the pbuf! */ 00407 pbuf_free(p); 00408 goto end; 00409 } 00410 } else { 00411 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n")); 00412 00413 #if LWIP_ICMP || LWIP_ICMP6 00414 /* No match was found, send ICMP destination port unreachable unless 00415 destination address was broadcast/multicast. */ 00416 if (!broadcast && !ip_addr_ismulticast(ip_current_dest_addr())) { 00417 /* move payload pointer back to ip header */ 00418 pbuf_header_force(p, (s16_t)(ip_current_header_tot_len() + UDP_HLEN)); 00419 icmp_port_unreach(ip_current_is_v6(), p); 00420 } 00421 #endif /* LWIP_ICMP || LWIP_ICMP6 */ 00422 UDP_STATS_INC(udp.proterr); 00423 UDP_STATS_INC(udp.drop); 00424 MIB2_STATS_INC(mib2.udpnoports); 00425 pbuf_free(p); 00426 } 00427 } else { 00428 pbuf_free(p); 00429 } 00430 end: 00431 PERF_STOP("udp_input"); 00432 return; 00433 #if CHECKSUM_CHECK_UDP 00434 chkerr: 00435 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, 00436 ("udp_input: UDP (or UDP Lite) datagram discarded due to failing checksum\n")); 00437 UDP_STATS_INC(udp.chkerr); 00438 UDP_STATS_INC(udp.drop); 00439 MIB2_STATS_INC(mib2.udpinerrors); 00440 pbuf_free(p); 00441 PERF_STOP("udp_input"); 00442 #endif /* CHECKSUM_CHECK_UDP */ 00443 } 00444 00445 /** 00446 * @ingroup udp_raw 00447 * Sends the pbuf p using UDP. The pbuf is not deallocated. 00448 * 00449 * 00450 * @param pcb UDP PCB used to send the data. 00451 * @param p chain of pbuf's to be sent. 00452 * 00453 * The datagram will be sent to the current remote_ip & remote_port 00454 * stored in pcb. If the pcb is not bound to a port, it will 00455 * automatically be bound to a random port. 00456 * 00457 * @return lwIP error code. 00458 * - ERR_OK. Successful. No error occurred. 00459 * - ERR_MEM. Out of memory. 00460 * - ERR_RTE. Could not find route to destination address. 00461 * - ERR_VAL. No PCB or PCB is dual-stack 00462 * - More errors could be returned by lower protocol layers. 00463 * 00464 * @see udp_disconnect() udp_sendto() 00465 */ 00466 err_t 00467 udp_send(struct udp_pcb *pcb, struct pbuf *p) 00468 { 00469 LWIP_ERROR("udp_send: invalid pcb", pcb != NULL, return ERR_ARG); 00470 LWIP_ERROR("udp_send: invalid pbuf", p != NULL, return ERR_ARG); 00471 00472 if (IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) { 00473 return ERR_VAL; 00474 } 00475 00476 /* send to the packet using remote ip and port stored in the pcb */ 00477 return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port); 00478 } 00479 00480 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 00481 /** @ingroup udp_raw 00482 * Same as udp_send() but with checksum 00483 */ 00484 err_t 00485 udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p, 00486 u8_t have_chksum, u16_t chksum) 00487 { 00488 LWIP_ERROR("udp_send_chksum: invalid pcb", pcb != NULL, return ERR_ARG); 00489 LWIP_ERROR("udp_send_chksum: invalid pbuf", p != NULL, return ERR_ARG); 00490 00491 if (IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) { 00492 return ERR_VAL; 00493 } 00494 00495 /* send to the packet using remote ip and port stored in the pcb */ 00496 return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port, 00497 have_chksum, chksum); 00498 } 00499 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ 00500 00501 /** 00502 * @ingroup udp_raw 00503 * Send data to a specified address using UDP. 00504 * 00505 * @param pcb UDP PCB used to send the data. 00506 * @param p chain of pbuf's to be sent. 00507 * @param dst_ip Destination IP address. 00508 * @param dst_port Destination UDP port. 00509 * 00510 * dst_ip & dst_port are expected to be in the same byte order as in the pcb. 00511 * 00512 * If the PCB already has a remote address association, it will 00513 * be restored after the data is sent. 00514 * 00515 * @return lwIP error code (@see udp_send for possible error codes) 00516 * 00517 * @see udp_disconnect() udp_send() 00518 */ 00519 err_t 00520 udp_sendto(struct udp_pcb *pcb, struct pbuf *p, 00521 const ip_addr_t *dst_ip, u16_t dst_port) 00522 { 00523 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 00524 return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0); 00525 } 00526 00527 /** @ingroup udp_raw 00528 * Same as udp_sendto(), but with checksum */ 00529 err_t 00530 udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, 00531 u16_t dst_port, u8_t have_chksum, u16_t chksum) 00532 { 00533 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ 00534 struct netif *netif; 00535 00536 LWIP_ERROR("udp_sendto: invalid pcb", pcb != NULL, return ERR_ARG); 00537 LWIP_ERROR("udp_sendto: invalid pbuf", p != NULL, return ERR_ARG); 00538 LWIP_ERROR("udp_sendto: invalid dst_ip", dst_ip != NULL, return ERR_ARG); 00539 00540 if (!IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) { 00541 return ERR_VAL; 00542 } 00543 00544 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send\n")); 00545 00546 if (pcb->netif_idx != NETIF_NO_INDEX) { 00547 netif = netif_get_by_index(pcb->netif_idx); 00548 } else { 00549 #if LWIP_MULTICAST_TX_OPTIONS 00550 netif = NULL; 00551 if (ip_addr_ismulticast(dst_ip)) { 00552 /* For IPv6, the interface to use for packets with a multicast destination 00553 * is specified using an interface index. The same approach may be used for 00554 * IPv4 as well, in which case it overrides the IPv4 multicast override 00555 * address below. Here we have to look up the netif by going through the 00556 * list, but by doing so we skip a route lookup. If the interface index has 00557 * gone stale, we fall through and do the regular route lookup after all. */ 00558 if (pcb->mcast_ifindex != NETIF_NO_INDEX) { 00559 netif = netif_get_by_index(pcb->mcast_ifindex); 00560 } 00561 #if LWIP_IPV4 00562 else 00563 #if LWIP_IPV6 00564 if (IP_IS_V4(dst_ip)) 00565 #endif /* LWIP_IPV6 */ 00566 { 00567 /* IPv4 does not use source-based routing by default, so we use an 00568 administratively selected interface for multicast by default. 00569 However, this can be overridden by setting an interface address 00570 in pcb->mcast_ip4 that is used for routing. If this routing lookup 00571 fails, we try regular routing as though no override was set. */ 00572 if (!ip4_addr_isany_val(pcb->mcast_ip4) && 00573 !ip4_addr_cmp(&pcb->mcast_ip4, IP4_ADDR_BROADCAST)) { 00574 netif = ip4_route_src(ip_2_ip4(&pcb->local_ip), &pcb->mcast_ip4); 00575 } 00576 } 00577 #endif /* LWIP_IPV4 */ 00578 } 00579 00580 if (netif == NULL) 00581 #endif /* LWIP_MULTICAST_TX_OPTIONS */ 00582 { 00583 /* find the outgoing network interface for this packet */ 00584 netif = ip_route(&pcb->local_ip, dst_ip); 00585 } 00586 } 00587 00588 /* no outgoing network interface could be found? */ 00589 if (netif == NULL) { 00590 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: No route to ")); 00591 ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, dst_ip); 00592 LWIP_DEBUGF(UDP_DEBUG, ("\n")); 00593 UDP_STATS_INC(udp.rterr); 00594 return ERR_RTE; 00595 } 00596 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 00597 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum); 00598 #else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ 00599 return udp_sendto_if(pcb, p, dst_ip, dst_port, netif); 00600 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ 00601 } 00602 00603 /** 00604 * @ingroup udp_raw 00605 * Send data to a specified address using UDP. 00606 * The netif used for sending can be specified. 00607 * 00608 * This function exists mainly for DHCP, to be able to send UDP packets 00609 * on a netif that is still down. 00610 * 00611 * @param pcb UDP PCB used to send the data. 00612 * @param p chain of pbuf's to be sent. 00613 * @param dst_ip Destination IP address. 00614 * @param dst_port Destination UDP port. 00615 * @param netif the netif used for sending. 00616 * 00617 * dst_ip & dst_port are expected to be in the same byte order as in the pcb. 00618 * 00619 * @return lwIP error code (@see udp_send for possible error codes) 00620 * 00621 * @see udp_disconnect() udp_send() 00622 */ 00623 err_t 00624 udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p, 00625 const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif) 00626 { 00627 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 00628 return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0); 00629 } 00630 00631 /** Same as udp_sendto_if(), but with checksum */ 00632 err_t 00633 udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, 00634 u16_t dst_port, struct netif *netif, u8_t have_chksum, 00635 u16_t chksum) 00636 { 00637 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ 00638 const ip_addr_t *src_ip; 00639 00640 LWIP_ERROR("udp_sendto_if: invalid pcb", pcb != NULL, return ERR_ARG); 00641 LWIP_ERROR("udp_sendto_if: invalid pbuf", p != NULL, return ERR_ARG); 00642 LWIP_ERROR("udp_sendto_if: invalid dst_ip", dst_ip != NULL, return ERR_ARG); 00643 LWIP_ERROR("udp_sendto_if: invalid netif", netif != NULL, return ERR_ARG); 00644 00645 if (!IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) { 00646 return ERR_VAL; 00647 } 00648 00649 /* PCB local address is IP_ANY_ADDR or multicast? */ 00650 #if LWIP_IPV6 00651 if (IP_IS_V6(dst_ip)) { 00652 if (ip6_addr_isany(ip_2_ip6(&pcb->local_ip)) || 00653 ip6_addr_ismulticast(ip_2_ip6(&pcb->local_ip))) { 00654 src_ip = ip6_select_source_address(netif, ip_2_ip6(dst_ip)); 00655 if (src_ip == NULL) { 00656 /* No suitable source address was found. */ 00657 return ERR_RTE; 00658 } 00659 } else { 00660 /* use UDP PCB local IPv6 address as source address, if still valid. */ 00661 if (netif_get_ip6_addr_match(netif, ip_2_ip6(&pcb->local_ip)) < 0) { 00662 /* Address isn't valid anymore. */ 00663 return ERR_RTE; 00664 } 00665 src_ip = &pcb->local_ip; 00666 } 00667 } 00668 #endif /* LWIP_IPV6 */ 00669 #if LWIP_IPV4 && LWIP_IPV6 00670 else 00671 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 00672 #if LWIP_IPV4 00673 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) || 00674 ip4_addr_ismulticast(ip_2_ip4(&pcb->local_ip))) { 00675 /* if the local_ip is any or multicast 00676 * use the outgoing network interface IP address as source address */ 00677 src_ip = netif_ip_addr4(netif); 00678 } else { 00679 /* check if UDP PCB local IP address is correct 00680 * this could be an old address if netif->ip_addr has changed */ 00681 if (!ip4_addr_cmp(ip_2_ip4(&(pcb->local_ip)), netif_ip4_addr(netif))) { 00682 /* local_ip doesn't match, drop the packet */ 00683 return ERR_RTE; 00684 } 00685 /* use UDP PCB local IP address as source address */ 00686 src_ip = &pcb->local_ip; 00687 } 00688 #endif /* LWIP_IPV4 */ 00689 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 00690 return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum, src_ip); 00691 #else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ 00692 return udp_sendto_if_src(pcb, p, dst_ip, dst_port, netif, src_ip); 00693 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ 00694 } 00695 00696 /** @ingroup udp_raw 00697 * Same as @ref udp_sendto_if, but with source address */ 00698 err_t 00699 udp_sendto_if_src(struct udp_pcb *pcb, struct pbuf *p, 00700 const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif, const ip_addr_t *src_ip) 00701 { 00702 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 00703 return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0, src_ip); 00704 } 00705 00706 /** Same as udp_sendto_if_src(), but with checksum */ 00707 err_t 00708 udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, 00709 u16_t dst_port, struct netif *netif, u8_t have_chksum, 00710 u16_t chksum, const ip_addr_t *src_ip) 00711 { 00712 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ 00713 struct udp_hdr *udphdr; 00714 err_t err; 00715 struct pbuf *q; /* q will be sent down the stack */ 00716 u8_t ip_proto; 00717 u8_t ttl; 00718 00719 LWIP_ASSERT_CORE_LOCKED(); 00720 00721 LWIP_ERROR("udp_sendto_if_src: invalid pcb", pcb != NULL, return ERR_ARG); 00722 LWIP_ERROR("udp_sendto_if_src: invalid pbuf", p != NULL, return ERR_ARG); 00723 LWIP_ERROR("udp_sendto_if_src: invalid dst_ip", dst_ip != NULL, return ERR_ARG); 00724 LWIP_ERROR("udp_sendto_if_src: invalid src_ip", src_ip != NULL, return ERR_ARG); 00725 LWIP_ERROR("udp_sendto_if_src: invalid netif", netif != NULL, return ERR_ARG); 00726 00727 if (!IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) || 00728 !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) { 00729 return ERR_VAL; 00730 } 00731 00732 #if LWIP_IPV4 && IP_SOF_BROADCAST 00733 /* broadcast filter? */ 00734 if (!ip_get_option(pcb, SOF_BROADCAST) && 00735 #if LWIP_IPV6 00736 IP_IS_V4(dst_ip) && 00737 #endif /* LWIP_IPV6 */ 00738 ip_addr_isbroadcast(dst_ip, netif)) { 00739 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, 00740 ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb)); 00741 return ERR_VAL; 00742 } 00743 #endif /* LWIP_IPV4 && IP_SOF_BROADCAST */ 00744 00745 /* if the PCB is not yet bound to a port, bind it here */ 00746 if (pcb->local_port == 0) { 00747 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n")); 00748 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port); 00749 if (err != ERR_OK) { 00750 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n")); 00751 return err; 00752 } 00753 } 00754 00755 /* packet too large to add a UDP header without causing an overflow? */ 00756 if ((u16_t)(p->tot_len + UDP_HLEN) < p->tot_len) { 00757 return ERR_MEM; 00758 } 00759 /* not enough space to add an UDP header to first pbuf in given p chain? */ 00760 if (pbuf_add_header(p, UDP_HLEN)) { 00761 /* allocate header in a separate new pbuf */ 00762 q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM); 00763 /* new header pbuf could not be allocated? */ 00764 if (q == NULL) { 00765 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n")); 00766 return ERR_MEM; 00767 } 00768 if (p->tot_len != 0) { 00769 /* chain header q in front of given pbuf p (only if p contains data) */ 00770 pbuf_chain(q, p); 00771 } 00772 /* first pbuf q points to header pbuf */ 00773 LWIP_DEBUGF(UDP_DEBUG, 00774 ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p)); 00775 } else { 00776 /* adding space for header within p succeeded */ 00777 /* first pbuf q equals given pbuf */ 00778 q = p; 00779 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p)); 00780 } 00781 LWIP_ASSERT("check that first pbuf can hold struct udp_hdr", 00782 (q->len >= sizeof(struct udp_hdr))); 00783 /* q now represents the packet to be sent */ 00784 udphdr = (struct udp_hdr *)q->payload; 00785 udphdr->src = lwip_htons(pcb->local_port); 00786 udphdr->dest = lwip_htons(dst_port); 00787 /* in UDP, 0 checksum means 'no checksum' */ 00788 udphdr->chksum = 0x0000; 00789 00790 /* Multicast Loop? */ 00791 #if LWIP_MULTICAST_TX_OPTIONS 00792 if (((pcb->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) { 00793 q->flags |= PBUF_FLAG_MCASTLOOP; 00794 } 00795 #endif /* LWIP_MULTICAST_TX_OPTIONS */ 00796 00797 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len)); 00798 00799 #if LWIP_UDPLITE 00800 /* UDP Lite protocol? */ 00801 if (pcb->flags & UDP_FLAGS_UDPLITE) { 00802 u16_t chklen, chklen_hdr; 00803 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len)); 00804 /* set UDP message length in UDP header */ 00805 chklen_hdr = chklen = pcb->chksum_len_tx; 00806 if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) { 00807 if (chklen != 0) { 00808 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen)); 00809 } 00810 /* For UDP-Lite, checksum length of 0 means checksum 00811 over the complete packet. (See RFC 3828 chap. 3.1) 00812 At least the UDP-Lite header must be covered by the 00813 checksum, therefore, if chksum_len has an illegal 00814 value, we generate the checksum over the complete 00815 packet to be safe. */ 00816 chklen_hdr = 0; 00817 chklen = q->tot_len; 00818 } 00819 udphdr->len = lwip_htons(chklen_hdr); 00820 /* calculate checksum */ 00821 #if CHECKSUM_GEN_UDP 00822 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) { 00823 #if LWIP_CHECKSUM_ON_COPY 00824 if (have_chksum) { 00825 chklen = UDP_HLEN; 00826 } 00827 #endif /* LWIP_CHECKSUM_ON_COPY */ 00828 udphdr->chksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDPLITE, 00829 q->tot_len, chklen, src_ip, dst_ip); 00830 #if LWIP_CHECKSUM_ON_COPY 00831 if (have_chksum) { 00832 u32_t acc; 00833 acc = udphdr->chksum + (u16_t)~(chksum); 00834 udphdr->chksum = FOLD_U32T(acc); 00835 } 00836 #endif /* LWIP_CHECKSUM_ON_COPY */ 00837 00838 /* chksum zero must become 0xffff, as zero means 'no checksum' */ 00839 if (udphdr->chksum == 0x0000) { 00840 udphdr->chksum = 0xffff; 00841 } 00842 } 00843 #endif /* CHECKSUM_GEN_UDP */ 00844 00845 ip_proto = IP_PROTO_UDPLITE; 00846 } else 00847 #endif /* LWIP_UDPLITE */ 00848 { /* UDP */ 00849 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len)); 00850 udphdr->len = lwip_htons(q->tot_len); 00851 /* calculate checksum */ 00852 #if CHECKSUM_GEN_UDP 00853 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) { 00854 /* Checksum is mandatory over IPv6. */ 00855 if (IP_IS_V6(dst_ip) || (pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) { 00856 u16_t udpchksum; 00857 #if LWIP_CHECKSUM_ON_COPY 00858 if (have_chksum) { 00859 u32_t acc; 00860 udpchksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDP, 00861 q->tot_len, UDP_HLEN, src_ip, dst_ip); 00862 acc = udpchksum + (u16_t)~(chksum); 00863 udpchksum = FOLD_U32T(acc); 00864 } else 00865 #endif /* LWIP_CHECKSUM_ON_COPY */ 00866 { 00867 udpchksum = ip_chksum_pseudo(q, IP_PROTO_UDP, q->tot_len, 00868 src_ip, dst_ip); 00869 } 00870 00871 /* chksum zero must become 0xffff, as zero means 'no checksum' */ 00872 if (udpchksum == 0x0000) { 00873 udpchksum = 0xffff; 00874 } 00875 udphdr->chksum = udpchksum; 00876 } 00877 } 00878 #endif /* CHECKSUM_GEN_UDP */ 00879 ip_proto = IP_PROTO_UDP; 00880 } 00881 00882 /* Determine TTL to use */ 00883 #if LWIP_MULTICAST_TX_OPTIONS 00884 ttl = (ip_addr_ismulticast(dst_ip) ? udp_get_multicast_ttl(pcb) : pcb->ttl); 00885 #else /* LWIP_MULTICAST_TX_OPTIONS */ 00886 ttl = pcb->ttl; 00887 #endif /* LWIP_MULTICAST_TX_OPTIONS */ 00888 00889 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum)); 00890 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,0x%02"X16_F",)\n", (u16_t)ip_proto)); 00891 /* output to IP */ 00892 NETIF_SET_HINTS(netif, &(pcb->netif_hints)); 00893 err = ip_output_if_src(q, src_ip, dst_ip, ttl, pcb->tos, ip_proto, netif); 00894 NETIF_RESET_HINTS(netif); 00895 00896 /* @todo: must this be increased even if error occurred? */ 00897 MIB2_STATS_INC(mib2.udpoutdatagrams); 00898 00899 /* did we chain a separate header pbuf earlier? */ 00900 if (q != p) { 00901 /* free the header pbuf */ 00902 pbuf_free(q); 00903 q = NULL; 00904 /* p is still referenced by the caller, and will live on */ 00905 } 00906 00907 UDP_STATS_INC(udp.xmit); 00908 return err; 00909 } 00910 00911 /** 00912 * @ingroup udp_raw 00913 * Bind an UDP PCB. 00914 * 00915 * @param pcb UDP PCB to be bound with a local address ipaddr and port. 00916 * @param ipaddr local IP address to bind with. Use IP_ANY_TYPE to 00917 * bind to all local interfaces. 00918 * @param port local UDP port to bind with. Use 0 to automatically bind 00919 * to a random port between UDP_LOCAL_PORT_RANGE_START and 00920 * UDP_LOCAL_PORT_RANGE_END. 00921 * 00922 * ipaddr & port are expected to be in the same byte order as in the pcb. 00923 * 00924 * @return lwIP error code. 00925 * - ERR_OK. Successful. No error occurred. 00926 * - ERR_USE. The specified ipaddr and port are already bound to by 00927 * another UDP PCB. 00928 * 00929 * @see udp_disconnect() 00930 */ 00931 err_t 00932 udp_bind(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port) 00933 { 00934 struct udp_pcb *ipcb; 00935 u8_t rebind; 00936 #if LWIP_IPV6 && LWIP_IPV6_SCOPES 00937 ip_addr_t zoned_ipaddr; 00938 #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */ 00939 00940 LWIP_ASSERT_CORE_LOCKED(); 00941 00942 #if LWIP_IPV4 00943 /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */ 00944 if (ipaddr == NULL) { 00945 ipaddr = IP4_ADDR_ANY; 00946 } 00947 #else /* LWIP_IPV4 */ 00948 LWIP_ERROR("udp_bind: invalid ipaddr", ipaddr != NULL, return ERR_ARG); 00949 #endif /* LWIP_IPV4 */ 00950 00951 LWIP_ERROR("udp_bind: invalid pcb", pcb != NULL, return ERR_ARG); 00952 00953 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = ")); 00954 ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE, ipaddr); 00955 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port)); 00956 00957 rebind = 0; 00958 /* Check for double bind and rebind of the same pcb */ 00959 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) { 00960 /* is this UDP PCB already on active list? */ 00961 if (pcb == ipcb) { 00962 rebind = 1; 00963 break; 00964 } 00965 } 00966 00967 #if LWIP_IPV6 && LWIP_IPV6_SCOPES 00968 /* If the given IP address should have a zone but doesn't, assign one now. 00969 * This is legacy support: scope-aware callers should always provide properly 00970 * zoned source addresses. Do the zone selection before the address-in-use 00971 * check below; as such we have to make a temporary copy of the address. */ 00972 if (IP_IS_V6(ipaddr) && ip6_addr_lacks_zone(ip_2_ip6(ipaddr), IP6_UNKNOWN)) { 00973 ip_addr_copy(zoned_ipaddr, *ipaddr); 00974 ip6_addr_select_zone(ip_2_ip6(&zoned_ipaddr), ip_2_ip6(&zoned_ipaddr)); 00975 ipaddr = &zoned_ipaddr; 00976 } 00977 #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */ 00978 00979 /* no port specified? */ 00980 if (port == 0) { 00981 port = udp_new_port(); 00982 if (port == 0) { 00983 /* no more ports available in local range */ 00984 LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n")); 00985 return ERR_USE; 00986 } 00987 } else { 00988 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) { 00989 if (pcb != ipcb) { 00990 /* By default, we don't allow to bind to a port that any other udp 00991 PCB is already bound to, unless *all* PCBs with that port have tha 00992 REUSEADDR flag set. */ 00993 #if SO_REUSE 00994 if (!ip_get_option(pcb, SOF_REUSEADDR) || 00995 !ip_get_option(ipcb, SOF_REUSEADDR)) 00996 #endif /* SO_REUSE */ 00997 { 00998 /* port matches that of PCB in list and REUSEADDR not set -> reject */ 00999 if ((ipcb->local_port == port) && 01000 /* IP address matches or any IP used? */ 01001 (ip_addr_cmp(&ipcb->local_ip, ipaddr) || ip_addr_isany(ipaddr) || 01002 ip_addr_isany(&ipcb->local_ip))) { 01003 /* other PCB already binds to this local IP and port */ 01004 LWIP_DEBUGF(UDP_DEBUG, 01005 ("udp_bind: local port %"U16_F" already bound by another pcb\n", port)); 01006 return ERR_USE; 01007 } 01008 } 01009 } 01010 } 01011 } 01012 01013 ip_addr_set_ipaddr(&pcb->local_ip, ipaddr); 01014 01015 pcb->local_port = port; 01016 mib2_udp_bind(pcb); 01017 /* pcb not active yet? */ 01018 if (rebind == 0) { 01019 /* place the PCB on the active list if not already there */ 01020 pcb->next = udp_pcbs; 01021 udp_pcbs = pcb; 01022 } 01023 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_bind: bound to ")); 01024 ip_addr_debug_print_val(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, pcb->local_ip); 01025 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->local_port)); 01026 return ERR_OK; 01027 } 01028 01029 /** 01030 * @ingroup udp_raw 01031 * Bind an UDP PCB to a specific netif. 01032 * After calling this function, all packets received via this PCB 01033 * are guaranteed to have come in via the specified netif, and all 01034 * outgoing packets will go out via the specified netif. 01035 * 01036 * @param pcb UDP PCB to be bound. 01037 * @param netif netif to bind udp pcb to. Can be NULL. 01038 * 01039 * @see udp_disconnect() 01040 */ 01041 void 01042 udp_bind_netif(struct udp_pcb *pcb, const struct netif *netif) 01043 { 01044 LWIP_ASSERT_CORE_LOCKED(); 01045 01046 if (netif != NULL) { 01047 pcb->netif_idx = netif_get_index(netif); 01048 } else { 01049 pcb->netif_idx = NETIF_NO_INDEX; 01050 } 01051 } 01052 01053 /** 01054 * @ingroup udp_raw 01055 * Sets the remote end of the pcb. This function does not generate any 01056 * network traffic, but only sets the remote address of the pcb. 01057 * 01058 * @param pcb UDP PCB to be connected with remote address ipaddr and port. 01059 * @param ipaddr remote IP address to connect with. 01060 * @param port remote UDP port to connect with. 01061 * 01062 * @return lwIP error code 01063 * 01064 * ipaddr & port are expected to be in the same byte order as in the pcb. 01065 * 01066 * The udp pcb is bound to a random local port if not already bound. 01067 * 01068 * @see udp_disconnect() 01069 */ 01070 err_t 01071 udp_connect(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port) 01072 { 01073 struct udp_pcb *ipcb; 01074 01075 LWIP_ASSERT_CORE_LOCKED(); 01076 01077 LWIP_ERROR("udp_connect: invalid pcb", pcb != NULL, return ERR_ARG); 01078 LWIP_ERROR("udp_connect: invalid ipaddr", ipaddr != NULL, return ERR_ARG); 01079 01080 if (pcb->local_port == 0) { 01081 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port); 01082 if (err != ERR_OK) { 01083 return err; 01084 } 01085 } 01086 01087 ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr); 01088 #if LWIP_IPV6 && LWIP_IPV6_SCOPES 01089 /* If the given IP address should have a zone but doesn't, assign one now, 01090 * using the bound address to make a more informed decision when possible. */ 01091 if (IP_IS_V6(&pcb->remote_ip) && 01092 ip6_addr_lacks_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNKNOWN)) { 01093 ip6_addr_select_zone(ip_2_ip6(&pcb->remote_ip), ip_2_ip6(&pcb->local_ip)); 01094 } 01095 #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */ 01096 01097 pcb->remote_port = port; 01098 pcb->flags |= UDP_FLAGS_CONNECTED; 01099 01100 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_connect: connected to ")); 01101 ip_addr_debug_print_val(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, 01102 pcb->remote_ip); 01103 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->remote_port)); 01104 01105 /* Insert UDP PCB into the list of active UDP PCBs. */ 01106 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) { 01107 if (pcb == ipcb) { 01108 /* already on the list, just return */ 01109 return ERR_OK; 01110 } 01111 } 01112 /* PCB not yet on the list, add PCB now */ 01113 pcb->next = udp_pcbs; 01114 udp_pcbs = pcb; 01115 return ERR_OK; 01116 } 01117 01118 /** 01119 * @ingroup udp_raw 01120 * Remove the remote end of the pcb. This function does not generate 01121 * any network traffic, but only removes the remote address of the pcb. 01122 * 01123 * @param pcb the udp pcb to disconnect. 01124 */ 01125 void 01126 udp_disconnect(struct udp_pcb *pcb) 01127 { 01128 LWIP_ASSERT_CORE_LOCKED(); 01129 01130 LWIP_ERROR("udp_disconnect: invalid pcb", pcb != NULL, return); 01131 01132 /* reset remote address association */ 01133 #if LWIP_IPV4 && LWIP_IPV6 01134 if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) { 01135 ip_addr_copy(pcb->remote_ip, *IP_ANY_TYPE); 01136 } else { 01137 #endif 01138 ip_addr_set_any(IP_IS_V6_VAL(pcb->remote_ip), &pcb->remote_ip); 01139 #if LWIP_IPV4 && LWIP_IPV6 01140 } 01141 #endif 01142 pcb->remote_port = 0; 01143 pcb->netif_idx = NETIF_NO_INDEX; 01144 /* mark PCB as unconnected */ 01145 udp_clear_flags(pcb, UDP_FLAGS_CONNECTED); 01146 } 01147 01148 /** 01149 * @ingroup udp_raw 01150 * Set a receive callback for a UDP PCB. 01151 * This callback will be called when receiving a datagram for the pcb. 01152 * 01153 * @param pcb the pcb for which to set the recv callback 01154 * @param recv function pointer of the callback function 01155 * @param recv_arg additional argument to pass to the callback function 01156 */ 01157 void 01158 udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg) 01159 { 01160 LWIP_ASSERT_CORE_LOCKED(); 01161 01162 LWIP_ERROR("udp_recv: invalid pcb", pcb != NULL, return); 01163 01164 /* remember recv() callback and user data */ 01165 pcb->recv = recv; 01166 pcb->recv_arg = recv_arg; 01167 } 01168 01169 /** 01170 * @ingroup udp_raw 01171 * Removes and deallocates the pcb. 01172 * 01173 * @param pcb UDP PCB to be removed. The PCB is removed from the list of 01174 * UDP PCB's and the data structure is freed from memory. 01175 * 01176 * @see udp_new() 01177 */ 01178 void 01179 udp_remove(struct udp_pcb *pcb) 01180 { 01181 struct udp_pcb *pcb2; 01182 01183 LWIP_ASSERT_CORE_LOCKED(); 01184 01185 LWIP_ERROR("udp_remove: invalid pcb", pcb != NULL, return); 01186 01187 mib2_udp_unbind(pcb); 01188 /* pcb to be removed is first in list? */ 01189 if (udp_pcbs == pcb) { 01190 /* make list start at 2nd pcb */ 01191 udp_pcbs = udp_pcbs->next; 01192 /* pcb not 1st in list */ 01193 } else { 01194 for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) { 01195 /* find pcb in udp_pcbs list */ 01196 if (pcb2->next != NULL && pcb2->next == pcb) { 01197 /* remove pcb from list */ 01198 pcb2->next = pcb->next; 01199 break; 01200 } 01201 } 01202 } 01203 memp_free(MEMP_UDP_PCB, pcb); 01204 } 01205 01206 /** 01207 * @ingroup udp_raw 01208 * Creates a new UDP pcb which can be used for UDP communication. The 01209 * pcb is not active until it has either been bound to a local address 01210 * or connected to a remote address. 01211 * 01212 * @return The UDP PCB which was created. NULL if the PCB data structure 01213 * could not be allocated. 01214 * 01215 * @see udp_remove() 01216 */ 01217 struct udp_pcb * 01218 udp_new(void) 01219 { 01220 struct udp_pcb *pcb; 01221 01222 LWIP_ASSERT_CORE_LOCKED(); 01223 01224 pcb = (struct udp_pcb *)memp_malloc(MEMP_UDP_PCB); 01225 /* could allocate UDP PCB? */ 01226 if (pcb != NULL) { 01227 /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0 01228 * which means checksum is generated over the whole datagram per default 01229 * (recommended as default by RFC 3828). */ 01230 /* initialize PCB to all zeroes */ 01231 memset(pcb, 0, sizeof(struct udp_pcb)); 01232 pcb->ttl = UDP_TTL; 01233 #if LWIP_MULTICAST_TX_OPTIONS 01234 udp_set_multicast_ttl(pcb, UDP_TTL); 01235 #endif /* LWIP_MULTICAST_TX_OPTIONS */ 01236 } 01237 return pcb; 01238 } 01239 01240 /** 01241 * @ingroup udp_raw 01242 * Create a UDP PCB for specific IP type. 01243 * The pcb is not active until it has either been bound to a local address 01244 * or connected to a remote address. 01245 * 01246 * @param type IP address type, see @ref lwip_ip_addr_type definitions. 01247 * If you want to listen to IPv4 and IPv6 (dual-stack) packets, 01248 * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE. 01249 * @return The UDP PCB which was created. NULL if the PCB data structure 01250 * could not be allocated. 01251 * 01252 * @see udp_remove() 01253 */ 01254 struct udp_pcb * 01255 udp_new_ip_type(u8_t type) 01256 { 01257 struct udp_pcb *pcb; 01258 01259 LWIP_ASSERT_CORE_LOCKED(); 01260 01261 pcb = udp_new(); 01262 #if LWIP_IPV4 && LWIP_IPV6 01263 if (pcb != NULL) { 01264 IP_SET_TYPE_VAL(pcb->local_ip, type); 01265 IP_SET_TYPE_VAL(pcb->remote_ip, type); 01266 } 01267 #else 01268 LWIP_UNUSED_ARG(type); 01269 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 01270 return pcb; 01271 } 01272 01273 /** This function is called from netif.c when address is changed 01274 * 01275 * @param old_addr IP address of the netif before change 01276 * @param new_addr IP address of the netif after change 01277 */ 01278 void udp_netif_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr) 01279 { 01280 struct udp_pcb *upcb; 01281 01282 if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) { 01283 for (upcb = udp_pcbs; upcb != NULL; upcb = upcb->next) { 01284 /* PCB bound to current local interface address? */ 01285 if (ip_addr_cmp(&upcb->local_ip, old_addr)) { 01286 /* The PCB is bound to the old ipaddr and 01287 * is set to bound to the new one instead */ 01288 ip_addr_copy(upcb->local_ip, *new_addr); 01289 } 01290 } 01291 } 01292 } 01293 01294 #if UDP_DEBUG 01295 /** 01296 * Print UDP header information for debug purposes. 01297 * 01298 * @param udphdr pointer to the udp header in memory. 01299 */ 01300 void 01301 udp_debug_print(struct udp_hdr *udphdr) 01302 { 01303 LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n")); 01304 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); 01305 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n", 01306 lwip_ntohs(udphdr->src), lwip_ntohs(udphdr->dest))); 01307 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); 01308 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n", 01309 lwip_ntohs(udphdr->len), lwip_ntohs(udphdr->chksum))); 01310 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n")); 01311 } 01312 #endif /* UDP_DEBUG */ 01313 01314 #endif /* LWIP_UDP */
Generated on Tue Jul 12 2022 13:54:30 by
