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.
Fork of lwip by
tcp.c
00001 /** 00002 * @file 00003 * Transmission Control Protocol for IP 00004 * 00005 * This file contains common functions for the TCP implementation, such as functinos 00006 * for manipulating the data structures and the TCP timer functions. TCP functions 00007 * related to input and output is found in tcp_in.c and tcp_out.c respectively. 00008 * 00009 */ 00010 00011 /* 00012 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 00013 * All rights reserved. 00014 * 00015 * Redistribution and use in source and binary forms, with or without modification, 00016 * are permitted provided that the following conditions are met: 00017 * 00018 * 1. Redistributions of source code must retain the above copyright notice, 00019 * this list of conditions and the following disclaimer. 00020 * 2. Redistributions in binary form must reproduce the above copyright notice, 00021 * this list of conditions and the following disclaimer in the documentation 00022 * and/or other materials provided with the distribution. 00023 * 3. The name of the author may not be used to endorse or promote products 00024 * derived from this software without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00027 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00028 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00029 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00030 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00031 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00032 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00033 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00034 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00035 * OF SUCH DAMAGE. 00036 * 00037 * This file is part of the lwIP TCP/IP stack. 00038 * 00039 * Author: Adam Dunkels <adam@sics.se> 00040 * 00041 */ 00042 00043 #include "lwip/opt.h" 00044 00045 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */ 00046 00047 #include "lwip/def.h" 00048 #include "lwip/mem.h" 00049 #include "lwip/memp.h" 00050 #include "lwip/snmp.h" 00051 #include "lwip/tcp.h" 00052 #include "lwip/tcp_impl.h" 00053 #include "lwip/debug.h" 00054 #include "lwip/stats.h" 00055 00056 #include <string.h> 00057 00058 const char * const tcp_state_str[] = { 00059 "CLOSED", 00060 "LISTEN", 00061 "SYN_SENT", 00062 "SYN_RCVD", 00063 "ESTABLISHED", 00064 "FIN_WAIT_1", 00065 "FIN_WAIT_2", 00066 "CLOSE_WAIT", 00067 "CLOSING", 00068 "LAST_ACK", 00069 "TIME_WAIT" 00070 }; 00071 00072 /* Incremented every coarse grained timer shot (typically every 500 ms). */ 00073 u32_t tcp_ticks; 00074 const u8_t tcp_backoff[13] = 00075 { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7}; 00076 /* Times per slowtmr hits */ 00077 const u8_t tcp_persist_backoff[7] = { 3, 6, 12, 24, 48, 96, 120 }; 00078 00079 /* The TCP PCB lists. */ 00080 00081 /** List of all TCP PCBs bound but not yet (connected || listening) */ 00082 struct tcp_pcb *tcp_bound_pcbs; 00083 /** List of all TCP PCBs in LISTEN state */ 00084 union tcp_listen_pcbs_t tcp_listen_pcbs; 00085 /** List of all TCP PCBs that are in a state in which 00086 * they accept or send data. */ 00087 struct tcp_pcb *tcp_active_pcbs; 00088 /** List of all TCP PCBs in TIME-WAIT state */ 00089 struct tcp_pcb *tcp_tw_pcbs; 00090 00091 #define NUM_TCP_PCB_LISTS 4 00092 #define NUM_TCP_PCB_LISTS_NO_TIME_WAIT 3 00093 /** An array with all (non-temporary) PCB lists, mainly used for smaller code size */ 00094 struct tcp_pcb ** const tcp_pcb_lists[] = {&tcp_listen_pcbs.pcbs, &tcp_bound_pcbs, 00095 &tcp_active_pcbs, &tcp_tw_pcbs}; 00096 00097 /** Only used for temporary storage. */ 00098 struct tcp_pcb *tcp_tmp_pcb; 00099 00100 /** Timer counter to handle calling slow-timer from tcp_tmr() */ 00101 static u8_t tcp_timer; 00102 static u16_t tcp_new_port(void); 00103 00104 /** 00105 * Called periodically to dispatch TCP timers. 00106 * 00107 */ 00108 void 00109 tcp_tmr(void) 00110 { 00111 /* Call tcp_fasttmr() every 250 ms */ 00112 tcp_fasttmr(); 00113 00114 if (++tcp_timer & 1) { 00115 /* Call tcp_tmr() every 500 ms, i.e., every other timer 00116 tcp_tmr() is called. */ 00117 tcp_slowtmr(); 00118 } 00119 } 00120 00121 /** 00122 * Closes the TX side of a connection held by the PCB. 00123 * For tcp_close(), a RST is sent if the application didn't receive all data 00124 * (tcp_recved() not called for all data passed to recv callback). 00125 * 00126 * Listening pcbs are freed and may not be referenced any more. 00127 * Connection pcbs are freed if not yet connected and may not be referenced 00128 * any more. If a connection is established (at least SYN received or in 00129 * a closing state), the connection is closed, and put in a closing state. 00130 * The pcb is then automatically freed in tcp_slowtmr(). It is therefore 00131 * unsafe to reference it. 00132 * 00133 * @param pcb the tcp_pcb to close 00134 * @return ERR_OK if connection has been closed 00135 * another err_t if closing failed and pcb is not freed 00136 */ 00137 static err_t 00138 tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data) 00139 { 00140 err_t err; 00141 00142 if (rst_on_unacked_data && (pcb->state != LISTEN)) { 00143 if ((pcb->refused_data != NULL) || (pcb->rcv_wnd != TCP_WND)) { 00144 /* Not all data received by application, send RST to tell the remote 00145 side about this. */ 00146 LWIP_ASSERT("pcb->flags & TF_RXCLOSED", pcb->flags & TF_RXCLOSED); 00147 00148 /* don't call tcp_abort here: we must not deallocate the pcb since 00149 that might not be expected when calling tcp_close */ 00150 tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip, 00151 pcb->local_port, pcb->remote_port); 00152 00153 tcp_pcb_purge(pcb); 00154 00155 /* TODO: to which state do we move now? */ 00156 00157 /* move to TIME_WAIT since we close actively */ 00158 TCP_RMV(&tcp_active_pcbs, pcb); 00159 pcb->state = TIME_WAIT; 00160 TCP_REG(&tcp_tw_pcbs, pcb); 00161 00162 return ERR_OK; 00163 } 00164 } 00165 00166 switch (pcb->state) { 00167 case CLOSED: 00168 /* Closing a pcb in the CLOSED state might seem erroneous, 00169 * however, it is in this state once allocated and as yet unused 00170 * and the user needs some way to free it should the need arise. 00171 * Calling tcp_close() with a pcb that has already been closed, (i.e. twice) 00172 * or for a pcb that has been used and then entered the CLOSED state 00173 * is erroneous, but this should never happen as the pcb has in those cases 00174 * been freed, and so any remaining handles are bogus. */ 00175 err = ERR_OK; 00176 if (pcb->local_port != 0) { 00177 TCP_RMV(&tcp_bound_pcbs, pcb); 00178 } 00179 memp_free(MEMP_TCP_PCB, pcb); 00180 pcb = NULL; 00181 break; 00182 case LISTEN: 00183 err = ERR_OK; 00184 tcp_pcb_remove(&tcp_listen_pcbs.pcbs, pcb); 00185 memp_free(MEMP_TCP_PCB_LISTEN, pcb); 00186 pcb = NULL; 00187 break; 00188 case SYN_SENT: 00189 err = ERR_OK; 00190 tcp_pcb_remove(&tcp_active_pcbs, pcb); 00191 memp_free(MEMP_TCP_PCB, pcb); 00192 pcb = NULL; 00193 snmp_inc_tcpattemptfails(); 00194 break; 00195 case SYN_RCVD: 00196 err = tcp_send_fin(pcb); 00197 if (err == ERR_OK) { 00198 snmp_inc_tcpattemptfails(); 00199 pcb->state = FIN_WAIT_1; 00200 } 00201 break; 00202 case ESTABLISHED: 00203 err = tcp_send_fin(pcb); 00204 if (err == ERR_OK) { 00205 snmp_inc_tcpestabresets(); 00206 pcb->state = FIN_WAIT_1; 00207 } 00208 break; 00209 case CLOSE_WAIT: 00210 err = tcp_send_fin(pcb); 00211 if (err == ERR_OK) { 00212 snmp_inc_tcpestabresets(); 00213 pcb->state = LAST_ACK; 00214 } 00215 break; 00216 default: 00217 /* Has already been closed, do nothing. */ 00218 err = ERR_OK; 00219 pcb = NULL; 00220 break; 00221 } 00222 00223 if (pcb != NULL && err == ERR_OK) { 00224 /* To ensure all data has been sent when tcp_close returns, we have 00225 to make sure tcp_output doesn't fail. 00226 Since we don't really have to ensure all data has been sent when tcp_close 00227 returns (unsent data is sent from tcp timer functions, also), we don't care 00228 for the return value of tcp_output for now. */ 00229 /* @todo: When implementing SO_LINGER, this must be changed somehow: 00230 If SOF_LINGER is set, the data should be sent and acked before close returns. 00231 This can only be valid for sequential APIs, not for the raw API. */ 00232 tcp_output(pcb); 00233 } 00234 return err; 00235 } 00236 00237 /** 00238 * Closes the connection held by the PCB. 00239 * 00240 * Listening pcbs are freed and may not be referenced any more. 00241 * Connection pcbs are freed if not yet connected and may not be referenced 00242 * any more. If a connection is established (at least SYN received or in 00243 * a closing state), the connection is closed, and put in a closing state. 00244 * The pcb is then automatically freed in tcp_slowtmr(). It is therefore 00245 * unsafe to reference it (unless an error is returned). 00246 * 00247 * @param pcb the tcp_pcb to close 00248 * @return ERR_OK if connection has been closed 00249 * another err_t if closing failed and pcb is not freed 00250 */ 00251 err_t 00252 tcp_close(struct tcp_pcb *pcb) 00253 { 00254 #if TCP_DEBUG 00255 LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in ")); 00256 tcp_debug_print_state(pcb->state); 00257 #endif /* TCP_DEBUG */ 00258 00259 if (pcb->state != LISTEN) { 00260 /* Set a flag not to receive any more data... */ 00261 pcb->flags |= TF_RXCLOSED; 00262 } 00263 /* ... and close */ 00264 return tcp_close_shutdown(pcb, 1); 00265 } 00266 00267 /** 00268 * Causes all or part of a full-duplex connection of this PCB to be shut down. 00269 * This doesn't deallocate the PCB! 00270 * 00271 * @param pcb PCB to shutdown 00272 * @param shut_rx shut down receive side if this is != 0 00273 * @param shut_tx shut down send side if this is != 0 00274 * @return ERR_OK if shutdown succeeded (or the PCB has already been shut down) 00275 * another err_t on error. 00276 */ 00277 err_t 00278 tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx) 00279 { 00280 if (pcb->state == LISTEN) { 00281 return ERR_CONN; 00282 } 00283 if (shut_rx) { 00284 /* shut down the receive side: free buffered data... */ 00285 if (pcb->refused_data != NULL) { 00286 pbuf_free(pcb->refused_data); 00287 pcb->refused_data = NULL; 00288 } 00289 /* ... and set a flag not to receive any more data */ 00290 pcb->flags |= TF_RXCLOSED; 00291 } 00292 if (shut_tx) { 00293 /* This can't happen twice since if it succeeds, the pcb's state is changed. 00294 Only close in these states as the others directly deallocate the PCB */ 00295 switch (pcb->state) { 00296 case SYN_RCVD: 00297 case ESTABLISHED: 00298 case CLOSE_WAIT: 00299 return tcp_close_shutdown(pcb, 0); 00300 default: 00301 /* don't shut down other states */ 00302 break; 00303 } 00304 } 00305 /* @todo: return another err_t if not in correct state or already shut? */ 00306 return ERR_OK; 00307 } 00308 00309 /** 00310 * Abandons a connection and optionally sends a RST to the remote 00311 * host. Deletes the local protocol control block. This is done when 00312 * a connection is killed because of shortage of memory. 00313 * 00314 * @param pcb the tcp_pcb to abort 00315 * @param reset boolean to indicate whether a reset should be sent 00316 */ 00317 void 00318 tcp_abandon(struct tcp_pcb *pcb, int reset) 00319 { 00320 u32_t seqno, ackno; 00321 u16_t remote_port, local_port; 00322 ip_addr_t remote_ip, local_ip; 00323 #if LWIP_CALLBACK_API 00324 tcp_err_fn errf; 00325 #endif /* LWIP_CALLBACK_API */ 00326 void *errf_arg; 00327 00328 /* pcb->state LISTEN not allowed here */ 00329 LWIP_ASSERT("don't call tcp_abort/tcp_abandon for listen-pcbs", 00330 pcb->state != LISTEN); 00331 /* Figure out on which TCP PCB list we are, and remove us. If we 00332 are in an active state, call the receive function associated with 00333 the PCB with a NULL argument, and send an RST to the remote end. */ 00334 if (pcb->state == TIME_WAIT) { 00335 tcp_pcb_remove(&tcp_tw_pcbs, pcb); 00336 memp_free(MEMP_TCP_PCB, pcb); 00337 } else { 00338 seqno = pcb->snd_nxt; 00339 ackno = pcb->rcv_nxt; 00340 ip_addr_copy(local_ip, pcb->local_ip); 00341 ip_addr_copy(remote_ip, pcb->remote_ip); 00342 local_port = pcb->local_port; 00343 remote_port = pcb->remote_port; 00344 #if LWIP_CALLBACK_API 00345 errf = pcb->errf; 00346 #endif /* LWIP_CALLBACK_API */ 00347 errf_arg = pcb->callback_arg; 00348 tcp_pcb_remove(&tcp_active_pcbs, pcb); 00349 if (pcb->unacked != NULL) { 00350 tcp_segs_free(pcb->unacked); 00351 } 00352 if (pcb->unsent != NULL) { 00353 tcp_segs_free(pcb->unsent); 00354 } 00355 #if TCP_QUEUE_OOSEQ 00356 if (pcb->ooseq != NULL) { 00357 tcp_segs_free(pcb->ooseq); 00358 } 00359 #endif /* TCP_QUEUE_OOSEQ */ 00360 memp_free(MEMP_TCP_PCB, pcb); 00361 TCP_EVENT_ERR(errf, errf_arg, ERR_ABRT); 00362 if (reset) { 00363 LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abandon: sending RST\n")); 00364 tcp_rst(seqno, ackno, &local_ip, &remote_ip, local_port, remote_port); 00365 } 00366 } 00367 } 00368 00369 /** 00370 * Aborts the connection by sending a RST (reset) segment to the remote 00371 * host. The pcb is deallocated. This function never fails. 00372 * 00373 * ATTENTION: When calling this from one of the TCP callbacks, make 00374 * sure you always return ERR_ABRT (and never return ERR_ABRT otherwise 00375 * or you will risk accessing deallocated memory or memory leaks! 00376 * 00377 * @param pcb the tcp pcb to abort 00378 */ 00379 void 00380 tcp_abort(struct tcp_pcb *pcb) 00381 { 00382 tcp_abandon(pcb, 1); 00383 } 00384 00385 /** 00386 * Binds the connection to a local portnumber and IP address. If the 00387 * IP address is not given (i.e., ipaddr == NULL), the IP address of 00388 * the outgoing network interface is used instead. 00389 * 00390 * @param pcb the tcp_pcb to bind (no check is done whether this pcb is 00391 * already bound!) 00392 * @param ipaddr the local ip address to bind to (use IP_ADDR_ANY to bind 00393 * to any local address 00394 * @param port the local port to bind to 00395 * @return ERR_USE if the port is already in use 00396 * ERR_VAL if bind failed because the PCB is not in a valid state 00397 * ERR_OK if bound 00398 */ 00399 err_t 00400 tcp_bind(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port) 00401 { 00402 int i; 00403 int max_pcb_list = NUM_TCP_PCB_LISTS; 00404 struct tcp_pcb *cpcb; 00405 00406 LWIP_ERROR("tcp_bind: can only bind in state CLOSED", pcb->state == CLOSED, return ERR_VAL); 00407 00408 #if SO_REUSE 00409 /* Unless the REUSEADDR flag is set, 00410 we have to check the pcbs in TIME-WAIT state, also. 00411 We do not dump TIME_WAIT pcb's; they can still be matched by incoming 00412 packets using both local and remote IP addresses and ports to distinguish. 00413 */ 00414 if ((pcb->so_options & SOF_REUSEADDR) != 0) { 00415 max_pcb_list = NUM_TCP_PCB_LISTS_NO_TIME_WAIT; 00416 } 00417 #endif /* SO_REUSE */ 00418 00419 if (port == 0) { 00420 port = tcp_new_port(); 00421 } 00422 00423 /* Check if the address already is in use (on all lists) */ 00424 for (i = 0; i < max_pcb_list; i++) { 00425 for(cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) { 00426 if (cpcb->local_port == port) { 00427 #if SO_REUSE 00428 /* Omit checking for the same port if both pcbs have REUSEADDR set. 00429 For SO_REUSEADDR, the duplicate-check for a 5-tuple is done in 00430 tcp_connect. */ 00431 if (((pcb->so_options & SOF_REUSEADDR) == 0) || 00432 ((cpcb->so_options & SOF_REUSEADDR) == 0)) 00433 #endif /* SO_REUSE */ 00434 { 00435 if (ip_addr_isany(&(cpcb->local_ip)) || 00436 ip_addr_isany(ipaddr) || 00437 ip_addr_cmp(&(cpcb->local_ip), ipaddr)) { 00438 return ERR_USE; 00439 } 00440 } 00441 } 00442 } 00443 } 00444 00445 if (!ip_addr_isany(ipaddr)) { 00446 pcb->local_ip = *ipaddr; 00447 } 00448 pcb->local_port = port; 00449 TCP_REG(&tcp_bound_pcbs, pcb); 00450 LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %"U16_F"\n", port)); 00451 return ERR_OK; 00452 } 00453 #if LWIP_CALLBACK_API 00454 /** 00455 * Default accept callback if no accept callback is specified by the user. 00456 */ 00457 static err_t 00458 tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err) 00459 { 00460 LWIP_UNUSED_ARG(arg); 00461 LWIP_UNUSED_ARG(pcb); 00462 LWIP_UNUSED_ARG(err); 00463 00464 return ERR_ABRT; 00465 } 00466 #endif /* LWIP_CALLBACK_API */ 00467 00468 /** 00469 * Set the state of the connection to be LISTEN, which means that it 00470 * is able to accept incoming connections. The protocol control block 00471 * is reallocated in order to consume less memory. Setting the 00472 * connection to LISTEN is an irreversible process. 00473 * 00474 * @param pcb the original tcp_pcb 00475 * @param backlog the incoming connections queue limit 00476 * @return tcp_pcb used for listening, consumes less memory. 00477 * 00478 * @note The original tcp_pcb is freed. This function therefore has to be 00479 * called like this: 00480 * tpcb = tcp_listen(tpcb); 00481 */ 00482 struct tcp_pcb * 00483 tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog) 00484 { 00485 struct tcp_pcb_listen *lpcb; 00486 00487 LWIP_UNUSED_ARG(backlog); 00488 LWIP_ERROR("tcp_listen: pcb already connected", pcb->state == CLOSED, return NULL); 00489 00490 /* already listening? */ 00491 if (pcb->state == LISTEN) { 00492 return pcb; 00493 } 00494 #if SO_REUSE 00495 if ((pcb->so_options & SOF_REUSEADDR) != 0) { 00496 /* Since SOF_REUSEADDR allows reusing a local address before the pcb's usage 00497 is declared (listen-/connection-pcb), we have to make sure now that 00498 this port is only used once for every local IP. */ 00499 for(lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) { 00500 if (lpcb->local_port == pcb->local_port) { 00501 if (ip_addr_cmp(&lpcb->local_ip, &pcb->local_ip)) { 00502 /* this address/port is already used */ 00503 return NULL; 00504 } 00505 } 00506 } 00507 } 00508 #endif /* SO_REUSE */ 00509 lpcb = (struct tcp_pcb_listen *)memp_malloc(MEMP_TCP_PCB_LISTEN); 00510 if (lpcb == NULL) { 00511 return NULL; 00512 } 00513 lpcb->callback_arg = pcb->callback_arg; 00514 lpcb->local_port = pcb->local_port; 00515 lpcb->state = LISTEN; 00516 lpcb->prio = pcb->prio; 00517 lpcb->so_options = pcb->so_options; 00518 lpcb->so_options |= SOF_ACCEPTCONN; 00519 lpcb->ttl = pcb->ttl; 00520 lpcb->tos = pcb->tos; 00521 ip_addr_copy(lpcb->local_ip, pcb->local_ip); 00522 if (pcb->local_port != 0) { 00523 TCP_RMV(&tcp_bound_pcbs, pcb); 00524 } 00525 memp_free(MEMP_TCP_PCB, pcb); 00526 #if LWIP_CALLBACK_API 00527 lpcb->accept = tcp_accept_null; 00528 #endif /* LWIP_CALLBACK_API */ 00529 #if TCP_LISTEN_BACKLOG 00530 lpcb->accepts_pending = 0; 00531 lpcb->backlog = (backlog ? backlog : 1); 00532 #endif /* TCP_LISTEN_BACKLOG */ 00533 TCP_REG(&tcp_listen_pcbs.pcbs, (struct tcp_pcb *)lpcb); 00534 return (struct tcp_pcb *)lpcb; 00535 } 00536 00537 /** 00538 * Update the state that tracks the available window space to advertise. 00539 * 00540 * Returns how much extra window would be advertised if we sent an 00541 * update now. 00542 */ 00543 u32_t tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb) 00544 { 00545 u32_t new_right_edge = pcb->rcv_nxt + pcb->rcv_wnd; 00546 00547 if (TCP_SEQ_GEQ(new_right_edge, pcb->rcv_ann_right_edge + LWIP_MIN((TCP_WND / 2), pcb->mss))) { 00548 /* we can advertise more window */ 00549 pcb->rcv_ann_wnd = pcb->rcv_wnd; 00550 return new_right_edge - pcb->rcv_ann_right_edge; 00551 } else { 00552 if (TCP_SEQ_GT(pcb->rcv_nxt, pcb->rcv_ann_right_edge)) { 00553 /* Can happen due to other end sending out of advertised window, 00554 * but within actual available (but not yet advertised) window */ 00555 pcb->rcv_ann_wnd = 0; 00556 } else { 00557 /* keep the right edge of window constant */ 00558 u32_t new_rcv_ann_wnd = pcb->rcv_ann_right_edge - pcb->rcv_nxt; 00559 LWIP_ASSERT("new_rcv_ann_wnd <= 0xffff", new_rcv_ann_wnd <= 0xffff); 00560 pcb->rcv_ann_wnd = (u16_t)new_rcv_ann_wnd; 00561 } 00562 return 0; 00563 } 00564 } 00565 00566 /** 00567 * This function should be called by the application when it has 00568 * processed the data. The purpose is to advertise a larger window 00569 * when the data has been processed. 00570 * 00571 * @param pcb the tcp_pcb for which data is read 00572 * @param len the amount of bytes that have been read by the application 00573 */ 00574 void 00575 tcp_recved(struct tcp_pcb *pcb, u16_t len) 00576 { 00577 int wnd_inflation; 00578 00579 LWIP_ASSERT("tcp_recved: len would wrap rcv_wnd\n", 00580 len <= 0xffff - pcb->rcv_wnd ); 00581 00582 pcb->rcv_wnd += len; 00583 if (pcb->rcv_wnd > TCP_WND) { 00584 pcb->rcv_wnd = TCP_WND; 00585 } 00586 00587 wnd_inflation = tcp_update_rcv_ann_wnd(pcb); 00588 00589 /* If the change in the right edge of window is significant (default 00590 * watermark is TCP_WND/4), then send an explicit update now. 00591 * Otherwise wait for a packet to be sent in the normal course of 00592 * events (or more window to be available later) */ 00593 if (wnd_inflation >= TCP_WND_UPDATE_THRESHOLD) { 00594 tcp_ack_now(pcb); 00595 tcp_output(pcb); 00596 } 00597 00598 LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: recveived %"U16_F" bytes, wnd %"U16_F" (%"U16_F").\n", 00599 len, pcb->rcv_wnd, TCP_WND - pcb->rcv_wnd)); 00600 } 00601 00602 /** 00603 * A nastly hack featuring 'goto' statements that allocates a 00604 * new TCP local port. 00605 * 00606 * @return a new (free) local TCP port number 00607 */ 00608 static u16_t 00609 tcp_new_port(void) 00610 { 00611 int i; 00612 struct tcp_pcb *pcb; 00613 #ifndef TCP_LOCAL_PORT_RANGE_START 00614 /* From http://www.iana.org/assignments/port-numbers: 00615 "The Dynamic and/or Private Ports are those from 49152 through 65535" */ 00616 #define TCP_LOCAL_PORT_RANGE_START 0xc000 00617 #define TCP_LOCAL_PORT_RANGE_END 0xffff 00618 #endif 00619 static u16_t port = TCP_LOCAL_PORT_RANGE_START; 00620 00621 again: 00622 if (port++ >= TCP_LOCAL_PORT_RANGE_END) { 00623 port = TCP_LOCAL_PORT_RANGE_START; 00624 } 00625 /* Check all PCB lists. */ 00626 for (i = 0; i < NUM_TCP_PCB_LISTS; i++) { 00627 for(pcb = *tcp_pcb_lists[i]; pcb != NULL; pcb = pcb->next) { 00628 if (pcb->local_port == port) { 00629 goto again; 00630 } 00631 } 00632 } 00633 return port; 00634 } 00635 00636 /** 00637 * Connects to another host. The function given as the "connected" 00638 * argument will be called when the connection has been established. 00639 * 00640 * @param pcb the tcp_pcb used to establish the connection 00641 * @param ipaddr the remote ip address to connect to 00642 * @param port the remote tcp port to connect to 00643 * @param connected callback function to call when connected (or on error) 00644 * @return ERR_VAL if invalid arguments are given 00645 * ERR_OK if connect request has been sent 00646 * other err_t values if connect request couldn't be sent 00647 */ 00648 err_t 00649 tcp_connect(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port, 00650 tcp_connected_fn connected) 00651 { 00652 err_t ret; 00653 u32_t iss; 00654 u16_t old_local_port; 00655 00656 LWIP_ERROR("tcp_connect: can only connect from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN); 00657 00658 LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port)); 00659 if (ipaddr != NULL) { 00660 pcb->remote_ip = *ipaddr; 00661 } else { 00662 return ERR_VAL; 00663 } 00664 pcb->remote_port = port; 00665 00666 /* check if we have a route to the remote host */ 00667 if (ip_addr_isany(&(pcb->local_ip))) { 00668 /* no local IP address set, yet. */ 00669 struct netif *netif = ip_route(&(pcb->remote_ip)); 00670 if (netif == NULL) { 00671 /* Don't even try to send a SYN packet if we have no route 00672 since that will fail. */ 00673 return ERR_RTE; 00674 } 00675 /* Use the netif's IP address as local address. */ 00676 ip_addr_copy(pcb->local_ip, netif->ip_addr); 00677 } 00678 00679 old_local_port = pcb->local_port; 00680 if (pcb->local_port == 0) { 00681 pcb->local_port = tcp_new_port(); 00682 } 00683 #if SO_REUSE 00684 if ((pcb->so_options & SOF_REUSEADDR) != 0) { 00685 /* Since SOF_REUSEADDR allows reusing a local address, we have to make sure 00686 now that the 5-tuple is unique. */ 00687 struct tcp_pcb *cpcb; 00688 int i; 00689 /* Don't check listen- and bound-PCBs, check active- and TIME-WAIT PCBs. */ 00690 for (i = 2; i < NUM_TCP_PCB_LISTS; i++) { 00691 for(cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) { 00692 if ((cpcb->local_port == pcb->local_port) && 00693 (cpcb->remote_port == port) && 00694 ip_addr_cmp(&cpcb->local_ip, &pcb->local_ip) && 00695 ip_addr_cmp(&cpcb->remote_ip, ipaddr)) { 00696 /* linux returns EISCONN here, but ERR_USE should be OK for us */ 00697 return ERR_USE; 00698 } 00699 } 00700 } 00701 } 00702 #endif /* SO_REUSE */ 00703 iss = tcp_next_iss(); 00704 pcb->rcv_nxt = 0; 00705 pcb->snd_nxt = iss; 00706 pcb->lastack = iss - 1; 00707 pcb->snd_lbb = iss - 1; 00708 pcb->rcv_wnd = TCP_WND; 00709 pcb->rcv_ann_wnd = TCP_WND; 00710 pcb->rcv_ann_right_edge = pcb->rcv_nxt; 00711 pcb->snd_wnd = TCP_WND; 00712 /* As initial send MSS, we use TCP_MSS but limit it to 536. 00713 The send MSS is updated when an MSS option is received. */ 00714 pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS; 00715 #if TCP_CALCULATE_EFF_SEND_MSS 00716 pcb->mss = tcp_eff_send_mss(pcb->mss, ipaddr); 00717 #endif /* TCP_CALCULATE_EFF_SEND_MSS */ 00718 pcb->cwnd = 1; 00719 pcb->ssthresh = pcb->mss * 10; 00720 #if LWIP_CALLBACK_API 00721 pcb->connected = connected; 00722 #else /* LWIP_CALLBACK_API */ 00723 LWIP_UNUSED_ARG(connected); 00724 #endif /* LWIP_CALLBACK_API */ 00725 00726 /* Send a SYN together with the MSS option. */ 00727 ret = tcp_enqueue_flags(pcb, TCP_SYN); 00728 if (ret == ERR_OK) { 00729 /* SYN segment was enqueued, changed the pcbs state now */ 00730 pcb->state = SYN_SENT; 00731 if (old_local_port != 0) { 00732 TCP_RMV(&tcp_bound_pcbs, pcb); 00733 } 00734 TCP_REG(&tcp_active_pcbs, pcb); 00735 snmp_inc_tcpactiveopens(); 00736 //printf("VAi para output\n"); 00737 tcp_output(pcb); 00738 //printf("Veio para output\n"); 00739 } 00740 return ret; 00741 } 00742 00743 /** 00744 * Called every 500 ms and implements the retransmission timer and the timer that 00745 * removes PCBs that have been in TIME-WAIT for enough time. It also increments 00746 * various timers such as the inactivity timer in each PCB. 00747 * 00748 * Automatically called from tcp_tmr(). 00749 */ 00750 void 00751 tcp_slowtmr(void) 00752 { 00753 struct tcp_pcb *pcb, *prev; 00754 u16_t eff_wnd; 00755 u8_t pcb_remove; /* flag if a PCB should be removed */ 00756 u8_t pcb_reset; /* flag if a RST should be sent when removing */ 00757 err_t err; 00758 00759 err = ERR_OK; 00760 00761 ++tcp_ticks; 00762 00763 /* Steps through all of the active PCBs. */ 00764 prev = NULL; 00765 pcb = tcp_active_pcbs; 00766 if (pcb == NULL) { 00767 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: no active pcbs\n")); 00768 } 00769 while (pcb != NULL) { 00770 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: processing active pcb\n")); 00771 LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED); 00772 LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN); 00773 LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT); 00774 00775 pcb_remove = 0; 00776 pcb_reset = 0; 00777 00778 if (pcb->state == SYN_SENT && pcb->nrtx == TCP_SYNMAXRTX) { 00779 ++pcb_remove; 00780 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max SYN retries reached\n")); 00781 } 00782 else if (pcb->nrtx == TCP_MAXRTX) { 00783 ++pcb_remove; 00784 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max DATA retries reached\n")); 00785 } else { 00786 if (pcb->persist_backoff > 0) { 00787 /* If snd_wnd is zero, use persist timer to send 1 byte probes 00788 * instead of using the standard retransmission mechanism. */ 00789 pcb->persist_cnt++; 00790 if (pcb->persist_cnt >= tcp_persist_backoff[pcb->persist_backoff-1]) { 00791 pcb->persist_cnt = 0; 00792 if (pcb->persist_backoff < sizeof(tcp_persist_backoff)) { 00793 pcb->persist_backoff++; 00794 } 00795 tcp_zero_window_probe(pcb); 00796 } 00797 } else { 00798 /* Increase the retransmission timer if it is running */ 00799 if(pcb->rtime >= 0) 00800 ++pcb->rtime; 00801 00802 if (pcb->unacked != NULL && pcb->rtime >= pcb->rto) { 00803 /* Time for a retransmission. */ 00804 LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_slowtmr: rtime %"S16_F 00805 " pcb->rto %"S16_F"\n", 00806 pcb->rtime, pcb->rto)); 00807 00808 /* Double retransmission time-out unless we are trying to 00809 * connect to somebody (i.e., we are in SYN_SENT). */ 00810 if (pcb->state != SYN_SENT) { 00811 pcb->rto = ((pcb->sa >> 3) + pcb->sv) << tcp_backoff[pcb->nrtx]; 00812 } 00813 00814 /* Reset the retransmission timer. */ 00815 pcb->rtime = 0; 00816 00817 /* Reduce congestion window and ssthresh. */ 00818 eff_wnd = LWIP_MIN(pcb->cwnd, pcb->snd_wnd); 00819 pcb->ssthresh = eff_wnd >> 1; 00820 if (pcb->ssthresh < (pcb->mss << 1)) { 00821 pcb->ssthresh = (pcb->mss << 1); 00822 } 00823 pcb->cwnd = pcb->mss; 00824 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: cwnd %"U16_F 00825 " ssthresh %"U16_F"\n", 00826 pcb->cwnd, pcb->ssthresh)); 00827 00828 /* The following needs to be called AFTER cwnd is set to one 00829 mss - STJ */ 00830 tcp_rexmit_rto(pcb); 00831 } 00832 } 00833 } 00834 /* Check if this PCB has stayed too long in FIN-WAIT-2 */ 00835 if (pcb->state == FIN_WAIT_2) { 00836 if ((u32_t)(tcp_ticks - pcb->tmr) > 00837 TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) { 00838 ++pcb_remove; 00839 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2\n")); 00840 } 00841 } 00842 00843 /* Check if KEEPALIVE should be sent */ 00844 if((pcb->so_options & SOF_KEEPALIVE) && 00845 ((pcb->state == ESTABLISHED) || 00846 (pcb->state == CLOSE_WAIT))) { 00847 #if LWIP_TCP_KEEPALIVE 00848 if((u32_t)(tcp_ticks - pcb->tmr) > 00849 (pcb->keep_idle + (pcb->keep_cnt*pcb->keep_intvl)) 00850 / TCP_SLOW_INTERVAL) 00851 #else 00852 if((u32_t)(tcp_ticks - pcb->tmr) > 00853 (pcb->keep_idle + TCP_MAXIDLE) / TCP_SLOW_INTERVAL) 00854 #endif /* LWIP_TCP_KEEPALIVE */ 00855 { 00856 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: KEEPALIVE timeout. Aborting connection to %"U16_F".%"U16_F".%"U16_F".%"U16_F".\n", 00857 ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip), 00858 ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip))); 00859 00860 ++pcb_remove; 00861 ++pcb_reset; 00862 } 00863 #if LWIP_TCP_KEEPALIVE 00864 else if((u32_t)(tcp_ticks - pcb->tmr) > 00865 (pcb->keep_idle + pcb->keep_cnt_sent * pcb->keep_intvl) 00866 / TCP_SLOW_INTERVAL) 00867 #else 00868 else if((u32_t)(tcp_ticks - pcb->tmr) > 00869 (pcb->keep_idle + pcb->keep_cnt_sent * TCP_KEEPINTVL_DEFAULT) 00870 / TCP_SLOW_INTERVAL) 00871 #endif /* LWIP_TCP_KEEPALIVE */ 00872 { 00873 tcp_keepalive(pcb); 00874 pcb->keep_cnt_sent++; 00875 } 00876 } 00877 00878 /* If this PCB has queued out of sequence data, but has been 00879 inactive for too long, will drop the data (it will eventually 00880 be retransmitted). */ 00881 #if TCP_QUEUE_OOSEQ 00882 if (pcb->ooseq != NULL && 00883 (u32_t)tcp_ticks - pcb->tmr >= pcb->rto * TCP_OOSEQ_TIMEOUT) { 00884 tcp_segs_free(pcb->ooseq); 00885 pcb->ooseq = NULL; 00886 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: dropping OOSEQ queued data\n")); 00887 } 00888 #endif /* TCP_QUEUE_OOSEQ */ 00889 00890 /* Check if this PCB has stayed too long in SYN-RCVD */ 00891 if (pcb->state == SYN_RCVD) { 00892 if ((u32_t)(tcp_ticks - pcb->tmr) > 00893 TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) { 00894 ++pcb_remove; 00895 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD\n")); 00896 } 00897 } 00898 00899 /* Check if this PCB has stayed too long in LAST-ACK */ 00900 if (pcb->state == LAST_ACK) { 00901 if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) { 00902 ++pcb_remove; 00903 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in LAST-ACK\n")); 00904 } 00905 } 00906 00907 /* If the PCB should be removed, do it. */ 00908 if (pcb_remove) { 00909 struct tcp_pcb *pcb2; 00910 tcp_pcb_purge(pcb); 00911 /* Remove PCB from tcp_active_pcbs list. */ 00912 if (prev != NULL) { 00913 LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs); 00914 prev->next = pcb->next; 00915 } else { 00916 /* This PCB was the first. */ 00917 LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb); 00918 tcp_active_pcbs = pcb->next; 00919 } 00920 00921 TCP_EVENT_ERR(pcb->errf, pcb->callback_arg, ERR_ABRT); 00922 if (pcb_reset) { 00923 tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip, 00924 pcb->local_port, pcb->remote_port); 00925 } 00926 00927 pcb2 = pcb; 00928 pcb = pcb->next; 00929 memp_free(MEMP_TCP_PCB, pcb2); 00930 } else { 00931 /* get the 'next' element now and work with 'prev' below (in case of abort) */ 00932 prev = pcb; 00933 pcb = pcb->next; 00934 00935 /* We check if we should poll the connection. */ 00936 ++prev->polltmr; 00937 if (prev->polltmr >= prev->pollinterval) { 00938 prev->polltmr = 0; 00939 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: polling application\n")); 00940 TCP_EVENT_POLL(prev, err); 00941 /* if err == ERR_ABRT, 'prev' is already deallocated */ 00942 if (err == ERR_OK) { 00943 tcp_output(prev); 00944 } 00945 } 00946 } 00947 } 00948 00949 00950 /* Steps through all of the TIME-WAIT PCBs. */ 00951 prev = NULL; 00952 pcb = tcp_tw_pcbs; 00953 while (pcb != NULL) { 00954 LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT); 00955 pcb_remove = 0; 00956 00957 /* Check if this PCB has stayed long enough in TIME-WAIT */ 00958 if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) { 00959 ++pcb_remove; 00960 } 00961 00962 00963 00964 /* If the PCB should be removed, do it. */ 00965 if (pcb_remove) { 00966 struct tcp_pcb *pcb2; 00967 tcp_pcb_purge(pcb); 00968 /* Remove PCB from tcp_tw_pcbs list. */ 00969 if (prev != NULL) { 00970 LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs); 00971 prev->next = pcb->next; 00972 } else { 00973 /* This PCB was the first. */ 00974 LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb); 00975 tcp_tw_pcbs = pcb->next; 00976 } 00977 pcb2 = pcb; 00978 pcb = pcb->next; 00979 memp_free(MEMP_TCP_PCB, pcb2); 00980 } else { 00981 prev = pcb; 00982 pcb = pcb->next; 00983 } 00984 } 00985 } 00986 00987 /** 00988 * Is called every TCP_FAST_INTERVAL (250 ms) and process data previously 00989 * "refused" by upper layer (application) and sends delayed ACKs. 00990 * 00991 * Automatically called from tcp_tmr(). 00992 */ 00993 void 00994 tcp_fasttmr(void) 00995 { 00996 struct tcp_pcb *pcb = tcp_active_pcbs; 00997 00998 while(pcb != NULL) { 00999 struct tcp_pcb *next = pcb->next; 01000 /* If there is data which was previously "refused" by upper layer */ 01001 if (pcb->refused_data != NULL) { 01002 /* Notify again application with data previously received. */ 01003 err_t err; 01004 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_fasttmr: notify kept packet\n")); 01005 TCP_EVENT_RECV(pcb, pcb->refused_data, ERR_OK, err); 01006 if (err == ERR_OK) { 01007 pcb->refused_data = NULL; 01008 } else if (err == ERR_ABRT) { 01009 /* if err == ERR_ABRT, 'pcb' is already deallocated */ 01010 pcb = NULL; 01011 } 01012 } 01013 01014 /* send delayed ACKs */ 01015 if (pcb && (pcb->flags & TF_ACK_DELAY)) { 01016 LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n")); 01017 tcp_ack_now(pcb); 01018 tcp_output(pcb); 01019 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW); 01020 } 01021 01022 pcb = next; 01023 } 01024 } 01025 01026 /** 01027 * Deallocates a list of TCP segments (tcp_seg structures). 01028 * 01029 * @param seg tcp_seg list of TCP segments to free 01030 */ 01031 void 01032 tcp_segs_free(struct tcp_seg *seg) 01033 { 01034 while (seg != NULL) { 01035 struct tcp_seg *next = seg->next; 01036 tcp_seg_free(seg); 01037 seg = next; 01038 } 01039 } 01040 01041 /** 01042 * Frees a TCP segment (tcp_seg structure). 01043 * 01044 * @param seg single tcp_seg to free 01045 */ 01046 void 01047 tcp_seg_free(struct tcp_seg *seg) 01048 { 01049 if (seg != NULL) { 01050 if (seg->p != NULL) { 01051 pbuf_free(seg->p); 01052 #if TCP_DEBUG 01053 seg->p = NULL; 01054 #endif /* TCP_DEBUG */ 01055 } 01056 memp_free(MEMP_TCP_SEG, seg); 01057 } 01058 } 01059 01060 /** 01061 * Sets the priority of a connection. 01062 * 01063 * @param pcb the tcp_pcb to manipulate 01064 * @param prio new priority 01065 */ 01066 void 01067 tcp_setprio(struct tcp_pcb *pcb, u8_t prio) 01068 { 01069 pcb->prio = prio; 01070 } 01071 01072 #if TCP_QUEUE_OOSEQ 01073 /** 01074 * Returns a copy of the given TCP segment. 01075 * The pbuf and data are not copied, only the pointers 01076 * 01077 * @param seg the old tcp_seg 01078 * @return a copy of seg 01079 */ 01080 struct tcp_seg * 01081 tcp_seg_copy(struct tcp_seg *seg) 01082 { 01083 struct tcp_seg *cseg; 01084 01085 cseg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG); 01086 if (cseg == NULL) { 01087 return NULL; 01088 } 01089 SMEMCPY((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg)); 01090 pbuf_ref(cseg->p); 01091 return cseg; 01092 } 01093 #endif /* TCP_QUEUE_OOSEQ */ 01094 01095 #if LWIP_CALLBACK_API 01096 /** 01097 * Default receive callback that is called if the user didn't register 01098 * a recv callback for the pcb. 01099 */ 01100 err_t 01101 tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) 01102 { 01103 LWIP_UNUSED_ARG(arg); 01104 if (p != NULL) { 01105 tcp_recved(pcb, p->tot_len); 01106 pbuf_free(p); 01107 } else if (err == ERR_OK) { 01108 return tcp_close(pcb); 01109 } 01110 return ERR_OK; 01111 } 01112 #endif /* LWIP_CALLBACK_API */ 01113 01114 /** 01115 * Kills the oldest active connection that has lower priority than prio. 01116 * 01117 * @param prio minimum priority 01118 */ 01119 static void 01120 tcp_kill_prio(u8_t prio) 01121 { 01122 struct tcp_pcb *pcb, *inactive; 01123 u32_t inactivity; 01124 u8_t mprio; 01125 01126 01127 mprio = TCP_PRIO_MAX; 01128 01129 /* We kill the oldest active connection that has lower priority than prio. */ 01130 inactivity = 0; 01131 inactive = NULL; 01132 for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { 01133 if (pcb->prio <= prio && 01134 pcb->prio <= mprio && 01135 (u32_t)(tcp_ticks - pcb->tmr) >= inactivity) { 01136 inactivity = tcp_ticks - pcb->tmr; 01137 inactive = pcb; 01138 mprio = pcb->prio; 01139 } 01140 } 01141 if (inactive != NULL) { 01142 LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F")\n", 01143 (void *)inactive, inactivity)); 01144 tcp_abort(inactive); 01145 } 01146 } 01147 01148 /** 01149 * Kills the oldest connection that is in TIME_WAIT state. 01150 * Called from tcp_alloc() if no more connections are available. 01151 */ 01152 static void 01153 tcp_kill_timewait(void) 01154 { 01155 struct tcp_pcb *pcb, *inactive; 01156 u32_t inactivity; 01157 01158 inactivity = 0; 01159 inactive = NULL; 01160 /* Go through the list of TIME_WAIT pcbs and get the oldest pcb. */ 01161 for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { 01162 if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) { 01163 inactivity = tcp_ticks - pcb->tmr; 01164 inactive = pcb; 01165 } 01166 } 01167 if (inactive != NULL) { 01168 LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F")\n", 01169 (void *)inactive, inactivity)); 01170 tcp_abort(inactive); 01171 } 01172 } 01173 01174 /** 01175 * Allocate a new tcp_pcb structure. 01176 * 01177 * @param prio priority for the new pcb 01178 * @return a new tcp_pcb that initially is in state CLOSED 01179 */ 01180 struct tcp_pcb * 01181 tcp_alloc(u8_t prio) 01182 { 01183 struct tcp_pcb *pcb; 01184 u32_t iss; 01185 01186 pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB); 01187 if (pcb == NULL) { 01188 /* Try killing oldest connection in TIME-WAIT. */ 01189 LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest TIME-WAIT connection\n")); 01190 tcp_kill_timewait(); 01191 /* Try to allocate a tcp_pcb again. */ 01192 pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB); 01193 if (pcb == NULL) { 01194 /* Try killing active connections with lower priority than the new one. */ 01195 LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing connection with prio lower than %d\n", prio)); 01196 tcp_kill_prio(prio); 01197 /* Try to allocate a tcp_pcb again. */ 01198 pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB); 01199 if (pcb != NULL) { 01200 /* adjust err stats: memp_malloc failed twice before */ 01201 MEMP_STATS_DEC(err, MEMP_TCP_PCB); 01202 } 01203 } 01204 if (pcb != NULL) { 01205 /* adjust err stats: timewait PCB was freed above */ 01206 MEMP_STATS_DEC(err, MEMP_TCP_PCB); 01207 } 01208 } 01209 if (pcb != NULL) { 01210 memset(pcb, 0, sizeof(struct tcp_pcb)); 01211 pcb->prio = prio; 01212 pcb->snd_buf = TCP_SND_BUF; 01213 pcb->snd_queuelen = 0; 01214 pcb->rcv_wnd = TCP_WND; 01215 pcb->rcv_ann_wnd = TCP_WND; 01216 pcb->tos = 0; 01217 pcb->ttl = TCP_TTL; 01218 /* As initial send MSS, we use TCP_MSS but limit it to 536. 01219 The send MSS is updated when an MSS option is received. */ 01220 pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS; 01221 pcb->rto = 3000 / TCP_SLOW_INTERVAL; 01222 pcb->sa = 0; 01223 pcb->sv = 3000 / TCP_SLOW_INTERVAL; 01224 pcb->rtime = -1; 01225 pcb->cwnd = 1; 01226 iss = tcp_next_iss(); 01227 pcb->snd_wl2 = iss; 01228 pcb->snd_nxt = iss; 01229 pcb->lastack = iss; 01230 pcb->snd_lbb = iss; 01231 pcb->tmr = tcp_ticks; 01232 01233 pcb->polltmr = 0; 01234 01235 #if LWIP_CALLBACK_API 01236 pcb->recv = tcp_recv_null; 01237 #endif /* LWIP_CALLBACK_API */ 01238 01239 /* Init KEEPALIVE timer */ 01240 pcb->keep_idle = TCP_KEEPIDLE_DEFAULT; 01241 01242 #if LWIP_TCP_KEEPALIVE 01243 pcb->keep_intvl = TCP_KEEPINTVL_DEFAULT; 01244 pcb->keep_cnt = TCP_KEEPCNT_DEFAULT; 01245 #endif /* LWIP_TCP_KEEPALIVE */ 01246 01247 pcb->keep_cnt_sent = 0; 01248 } 01249 return pcb; 01250 } 01251 01252 /** 01253 * Creates a new TCP protocol control block but doesn't place it on 01254 * any of the TCP PCB lists. 01255 * The pcb is not put on any list until binding using tcp_bind(). 01256 * 01257 * @internal: Maybe there should be a idle TCP PCB list where these 01258 * PCBs are put on. Port reservation using tcp_bind() is implemented but 01259 * allocated pcbs that are not bound can't be killed automatically if wanting 01260 * to allocate a pcb with higher prio (@see tcp_kill_prio()) 01261 * 01262 * @return a new tcp_pcb that initially is in state CLOSED 01263 */ 01264 struct tcp_pcb * 01265 tcp_new(void) 01266 { 01267 return tcp_alloc(TCP_PRIO_NORMAL); 01268 } 01269 01270 /** 01271 * Used to specify the argument that should be passed callback 01272 * functions. 01273 * 01274 * @param pcb tcp_pcb to set the callback argument 01275 * @param arg void pointer argument to pass to callback functions 01276 */ 01277 void 01278 tcp_arg(struct tcp_pcb *pcb, void *arg) 01279 { 01280 pcb->callback_arg = arg; 01281 } 01282 #if LWIP_CALLBACK_API 01283 01284 /** 01285 * Used to specify the function that should be called when a TCP 01286 * connection receives data. 01287 * 01288 * @param pcb tcp_pcb to set the recv callback 01289 * @param recv callback function to call for this pcb when data is received 01290 */ 01291 void 01292 tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv) 01293 { 01294 pcb->recv = recv; 01295 } 01296 01297 /** 01298 * Used to specify the function that should be called when TCP data 01299 * has been successfully delivered to the remote host. 01300 * 01301 * @param pcb tcp_pcb to set the sent callback 01302 * @param sent callback function to call for this pcb when data is successfully sent 01303 */ 01304 void 01305 tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent) 01306 { 01307 pcb->sent = sent; 01308 } 01309 01310 /** 01311 * Used to specify the function that should be called when a fatal error 01312 * has occured on the connection. 01313 * 01314 * @param pcb tcp_pcb to set the err callback 01315 * @param err callback function to call for this pcb when a fatal error 01316 * has occured on the connection 01317 */ 01318 void 01319 tcp_err(struct tcp_pcb *pcb, tcp_err_fn err) 01320 { 01321 pcb->errf = err; 01322 } 01323 01324 /** 01325 * Used for specifying the function that should be called when a 01326 * LISTENing connection has been connected to another host. 01327 * 01328 * @param pcb tcp_pcb to set the accept callback 01329 * @param accept callback function to call for this pcb when LISTENing 01330 * connection has been connected to another host 01331 */ 01332 void 01333 tcp_accept(struct tcp_pcb *pcb, tcp_accept_fn accept) 01334 { 01335 pcb->accept = accept; 01336 } 01337 #endif /* LWIP_CALLBACK_API */ 01338 01339 01340 /** 01341 * Used to specify the function that should be called periodically 01342 * from TCP. The interval is specified in terms of the TCP coarse 01343 * timer interval, which is called twice a second. 01344 * 01345 */ 01346 void 01347 tcp_poll(struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval) 01348 { 01349 #if LWIP_CALLBACK_API 01350 pcb->poll = poll; 01351 #else /* LWIP_CALLBACK_API */ 01352 LWIP_UNUSED_ARG(poll); 01353 #endif /* LWIP_CALLBACK_API */ 01354 pcb->pollinterval = interval; 01355 } 01356 01357 /** 01358 * Purges a TCP PCB. Removes any buffered data and frees the buffer memory 01359 * (pcb->ooseq, pcb->unsent and pcb->unacked are freed). 01360 * 01361 * @param pcb tcp_pcb to purge. The pcb itself is not deallocated! 01362 */ 01363 void 01364 tcp_pcb_purge(struct tcp_pcb *pcb) 01365 { 01366 if (pcb->state != CLOSED && 01367 pcb->state != TIME_WAIT && 01368 pcb->state != LISTEN) { 01369 01370 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge\n")); 01371 01372 #if TCP_LISTEN_BACKLOG 01373 if (pcb->state == SYN_RCVD) { 01374 /* Need to find the corresponding listen_pcb and decrease its accepts_pending */ 01375 struct tcp_pcb_listen *lpcb; 01376 LWIP_ASSERT("tcp_pcb_purge: pcb->state == SYN_RCVD but tcp_listen_pcbs is NULL", 01377 tcp_listen_pcbs.listen_pcbs != NULL); 01378 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) { 01379 if ((lpcb->local_port == pcb->local_port) && 01380 (ip_addr_isany(&lpcb->local_ip) || 01381 ip_addr_cmp(&pcb->local_ip, &lpcb->local_ip))) { 01382 /* port and address of the listen pcb match the timed-out pcb */ 01383 LWIP_ASSERT("tcp_pcb_purge: listen pcb does not have accepts pending", 01384 lpcb->accepts_pending > 0); 01385 lpcb->accepts_pending--; 01386 break; 01387 } 01388 } 01389 } 01390 #endif /* TCP_LISTEN_BACKLOG */ 01391 01392 01393 if (pcb->refused_data != NULL) { 01394 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->refused_data\n")); 01395 pbuf_free(pcb->refused_data); 01396 pcb->refused_data = NULL; 01397 } 01398 if (pcb->unsent != NULL) { 01399 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: not all data sent\n")); 01400 } 01401 if (pcb->unacked != NULL) { 01402 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->unacked\n")); 01403 } 01404 #if TCP_QUEUE_OOSEQ 01405 if (pcb->ooseq != NULL) { 01406 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->ooseq\n")); 01407 } 01408 tcp_segs_free(pcb->ooseq); 01409 pcb->ooseq = NULL; 01410 #endif /* TCP_QUEUE_OOSEQ */ 01411 01412 /* Stop the retransmission timer as it will expect data on unacked 01413 queue if it fires */ 01414 pcb->rtime = -1; 01415 01416 tcp_segs_free(pcb->unsent); 01417 tcp_segs_free(pcb->unacked); 01418 pcb->unacked = pcb->unsent = NULL; 01419 #if TCP_OVERSIZE 01420 pcb->unsent_oversize = 0; 01421 #endif /* TCP_OVERSIZE */ 01422 } 01423 } 01424 01425 /** 01426 * Purges the PCB and removes it from a PCB list. Any delayed ACKs are sent first. 01427 * 01428 * @param pcblist PCB list to purge. 01429 * @param pcb tcp_pcb to purge. The pcb itself is NOT deallocated! 01430 */ 01431 void 01432 tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb) 01433 { 01434 TCP_RMV(pcblist, pcb); 01435 01436 tcp_pcb_purge(pcb); 01437 01438 /* if there is an outstanding delayed ACKs, send it */ 01439 if (pcb->state != TIME_WAIT && 01440 pcb->state != LISTEN && 01441 pcb->flags & TF_ACK_DELAY) { 01442 pcb->flags |= TF_ACK_NOW; 01443 tcp_output(pcb); 01444 } 01445 01446 if (pcb->state != LISTEN) { 01447 LWIP_ASSERT("unsent segments leaking", pcb->unsent == NULL); 01448 LWIP_ASSERT("unacked segments leaking", pcb->unacked == NULL); 01449 #if TCP_QUEUE_OOSEQ 01450 LWIP_ASSERT("ooseq segments leaking", pcb->ooseq == NULL); 01451 #endif /* TCP_QUEUE_OOSEQ */ 01452 } 01453 01454 pcb->state = CLOSED; 01455 01456 LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane()); 01457 } 01458 01459 /** 01460 * Calculates a new initial sequence number for new connections. 01461 * 01462 * @return u32_t pseudo random sequence number 01463 */ 01464 u32_t 01465 tcp_next_iss(void) 01466 { 01467 static u32_t iss = 6510; 01468 01469 iss += tcp_ticks; /* XXX */ 01470 return iss; 01471 } 01472 01473 #if TCP_CALCULATE_EFF_SEND_MSS 01474 /** 01475 * Calcluates the effective send mss that can be used for a specific IP address 01476 * by using ip_route to determin the netif used to send to the address and 01477 * calculating the minimum of TCP_MSS and that netif's mtu (if set). 01478 */ 01479 u16_t 01480 tcp_eff_send_mss(u16_t sendmss, ip_addr_t *addr) 01481 { 01482 u16_t mss_s; 01483 struct netif *outif; 01484 01485 outif = ip_route(addr); 01486 if ((outif != NULL) && (outif->mtu != 0)) { 01487 mss_s = outif->mtu - IP_HLEN - TCP_HLEN; 01488 /* RFC 1122, chap 4.2.2.6: 01489 * Eff.snd.MSS = min(SendMSS+20, MMS_S) - TCPhdrsize - IPoptionsize 01490 * We correct for TCP options in tcp_write(), and don't support IP options. 01491 */ 01492 sendmss = LWIP_MIN(sendmss, mss_s); 01493 } 01494 return sendmss; 01495 } 01496 #endif /* TCP_CALCULATE_EFF_SEND_MSS */ 01497 01498 const char* 01499 tcp_debug_state_str(enum tcp_state s) 01500 { 01501 return tcp_state_str[s]; 01502 } 01503 01504 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG 01505 /** 01506 * Print a tcp header for debugging purposes. 01507 * 01508 * @param tcphdr pointer to a struct tcp_hdr 01509 */ 01510 void 01511 tcp_debug_print(struct tcp_hdr *tcphdr) 01512 { 01513 LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n")); 01514 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); 01515 LWIP_DEBUGF(TCP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n", 01516 ntohs(tcphdr->src), ntohs(tcphdr->dest))); 01517 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); 01518 LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (seq no)\n", 01519 ntohl(tcphdr->seqno))); 01520 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); 01521 LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (ack no)\n", 01522 ntohl(tcphdr->ackno))); 01523 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); 01524 LWIP_DEBUGF(TCP_DEBUG, ("| %2"U16_F" | |%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"| %5"U16_F" | (hdrlen, flags (", 01525 TCPH_HDRLEN(tcphdr), 01526 TCPH_FLAGS(tcphdr) >> 5 & 1, 01527 TCPH_FLAGS(tcphdr) >> 4 & 1, 01528 TCPH_FLAGS(tcphdr) >> 3 & 1, 01529 TCPH_FLAGS(tcphdr) >> 2 & 1, 01530 TCPH_FLAGS(tcphdr) >> 1 & 1, 01531 TCPH_FLAGS(tcphdr) & 1, 01532 ntohs(tcphdr->wnd))); 01533 tcp_debug_print_flags(TCPH_FLAGS(tcphdr)); 01534 LWIP_DEBUGF(TCP_DEBUG, ("), win)\n")); 01535 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); 01536 LWIP_DEBUGF(TCP_DEBUG, ("| 0x%04"X16_F" | %5"U16_F" | (chksum, urgp)\n", 01537 ntohs(tcphdr->chksum), ntohs(tcphdr->urgp))); 01538 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); 01539 } 01540 01541 /** 01542 * Print a tcp state for debugging purposes. 01543 * 01544 * @param s enum tcp_state to print 01545 */ 01546 void 01547 tcp_debug_print_state(enum tcp_state s) 01548 { 01549 LWIP_DEBUGF(TCP_DEBUG, ("State: %s\n", tcp_state_str[s])); 01550 } 01551 01552 /** 01553 * Print tcp flags for debugging purposes. 01554 * 01555 * @param flags tcp flags, all active flags are printed 01556 */ 01557 void 01558 tcp_debug_print_flags(u8_t flags) 01559 { 01560 if (flags & TCP_FIN) { 01561 LWIP_DEBUGF(TCP_DEBUG, ("FIN ")); 01562 } 01563 if (flags & TCP_SYN) { 01564 LWIP_DEBUGF(TCP_DEBUG, ("SYN ")); 01565 } 01566 if (flags & TCP_RST) { 01567 LWIP_DEBUGF(TCP_DEBUG, ("RST ")); 01568 } 01569 if (flags & TCP_PSH) { 01570 LWIP_DEBUGF(TCP_DEBUG, ("PSH ")); 01571 } 01572 if (flags & TCP_ACK) { 01573 LWIP_DEBUGF(TCP_DEBUG, ("ACK ")); 01574 } 01575 if (flags & TCP_URG) { 01576 LWIP_DEBUGF(TCP_DEBUG, ("URG ")); 01577 } 01578 if (flags & TCP_ECE) { 01579 LWIP_DEBUGF(TCP_DEBUG, ("ECE ")); 01580 } 01581 if (flags & TCP_CWR) { 01582 LWIP_DEBUGF(TCP_DEBUG, ("CWR ")); 01583 } 01584 LWIP_DEBUGF(TCP_DEBUG, ("\n")); 01585 } 01586 01587 /** 01588 * Print all tcp_pcbs in every list for debugging purposes. 01589 */ 01590 void 01591 tcp_debug_print_pcbs(void) 01592 { 01593 struct tcp_pcb *pcb; 01594 LWIP_DEBUGF(TCP_DEBUG, ("Active PCB states:\n")); 01595 for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { 01596 LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ", 01597 pcb->local_port, pcb->remote_port, 01598 pcb->snd_nxt, pcb->rcv_nxt)); 01599 tcp_debug_print_state(pcb->state); 01600 } 01601 LWIP_DEBUGF(TCP_DEBUG, ("Listen PCB states:\n")); 01602 for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next) { 01603 LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ", 01604 pcb->local_port, pcb->remote_port, 01605 pcb->snd_nxt, pcb->rcv_nxt)); 01606 tcp_debug_print_state(pcb->state); 01607 } 01608 LWIP_DEBUGF(TCP_DEBUG, ("TIME-WAIT PCB states:\n")); 01609 for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { 01610 LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ", 01611 pcb->local_port, pcb->remote_port, 01612 pcb->snd_nxt, pcb->rcv_nxt)); 01613 tcp_debug_print_state(pcb->state); 01614 } 01615 } 01616 01617 /** 01618 * Check state consistency of the tcp_pcb lists. 01619 */ 01620 s16_t 01621 tcp_pcbs_sane(void) 01622 { 01623 struct tcp_pcb *pcb; 01624 for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { 01625 LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED); 01626 LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN); 01627 LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT); 01628 } 01629 for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { 01630 LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT); 01631 } 01632 return 1; 01633 } 01634 #endif /* TCP_DEBUG */ 01635 01636 #endif /* LWIP_TCP */
Generated on Tue Jul 12 2022 21:59:36 by
1.7.2
