Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwip_tcp.c Source File

lwip_tcp.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * Transmission Control Protocol for IP
00004  * See also @ref tcp_raw
00005  *
00006  * @defgroup tcp_raw TCP
00007  * @ingroup callbackstyle_api
00008  * Transmission Control Protocol for IP\n
00009  * @see @ref api
00010  *
00011  * Common functions for the TCP implementation, such as functions
00012  * for manipulating the data structures and the TCP timer functions. TCP functions
00013  * related to input and output is found in tcp_in.c and tcp_out.c respectively.\n
00014  * 
00015  * TCP connection setup
00016  * --------------------
00017  * The functions used for setting up connections is similar to that of
00018  * the sequential API and of the BSD socket API. A new TCP connection
00019  * identifier (i.e., a protocol control block - PCB) is created with the
00020  * tcp_new() function. This PCB can then be either set to listen for new
00021  * incoming connections or be explicitly connected to another host.
00022  * - tcp_new()
00023  * - tcp_bind()
00024  * - tcp_listen() and tcp_listen_with_backlog()
00025  * - tcp_accept()
00026  * - tcp_connect()
00027  * 
00028  * Sending TCP data
00029  * ----------------
00030  * TCP data is sent by enqueueing the data with a call to tcp_write() and
00031  * triggering to send by calling tcp_output(). When the data is successfully
00032  * transmitted to the remote host, the application will be notified with a
00033  * call to a specified callback function.
00034  * - tcp_write()
00035  * - tcp_output()
00036  * - tcp_sent()
00037  * 
00038  * Receiving TCP data
00039  * ------------------
00040  * TCP data reception is callback based - an application specified
00041  * callback function is called when new data arrives. When the
00042  * application has taken the data, it has to call the tcp_recved()
00043  * function to indicate that TCP can advertise increase the receive
00044  * window.
00045  * - tcp_recv()
00046  * - tcp_recved()
00047  * 
00048  * Application polling
00049  * -------------------
00050  * When a connection is idle (i.e., no data is either transmitted or
00051  * received), lwIP will repeatedly poll the application by calling a
00052  * specified callback function. This can be used either as a watchdog
00053  * timer for killing connections that have stayed idle for too long, or
00054  * as a method of waiting for memory to become available. For instance,
00055  * if a call to tcp_write() has failed because memory wasn't available,
00056  * the application may use the polling functionality to call tcp_write()
00057  * again when the connection has been idle for a while.
00058  * - tcp_poll()
00059  *
00060  * Closing and aborting connections
00061  * --------------------------------
00062  * - tcp_close()
00063  * - tcp_abort()
00064  * - tcp_err()
00065  * 
00066  */
00067 
00068 /*
00069  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00070  * All rights reserved.
00071  *
00072  * Redistribution and use in source and binary forms, with or without modification,
00073  * are permitted provided that the following conditions are met:
00074  *
00075  * 1. Redistributions of source code must retain the above copyright notice,
00076  *    this list of conditions and the following disclaimer.
00077  * 2. Redistributions in binary form must reproduce the above copyright notice,
00078  *    this list of conditions and the following disclaimer in the documentation
00079  *    and/or other materials provided with the distribution.
00080  * 3. The name of the author may not be used to endorse or promote products
00081  *    derived from this software without specific prior written permission.
00082  *
00083  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00084  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00085  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00086  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00087  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00088  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00089  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00090  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00091  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00092  * OF SUCH DAMAGE.
00093  *
00094  * This file is part of the lwIP TCP/IP stack.
00095  *
00096  * Author: Adam Dunkels <adam@sics.se>
00097  *
00098  */
00099 
00100 #include "lwip/opt.h"
00101 
00102 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
00103 
00104 #include "lwip/def.h"
00105 #include "lwip/mem.h"
00106 #include "lwip/memp.h"
00107 #include "lwip/tcp.h"
00108 #include "lwip/priv/tcp_priv.h"
00109 #include "lwip/debug.h"
00110 #include "lwip/stats.h"
00111 #include "lwip/ip6.h"
00112 #include "lwip/ip6_addr.h"
00113 #include "lwip/nd6.h"
00114 
00115 #include <string.h>
00116 
00117 #ifdef LWIP_HOOK_FILENAME
00118 #include LWIP_HOOK_FILENAME
00119 #endif
00120 
00121 #ifndef TCP_LOCAL_PORT_RANGE_START
00122 /* From http://www.iana.org/assignments/port-numbers:
00123    "The Dynamic and/or Private Ports are those from 49152 through 65535" */
00124 #define TCP_LOCAL_PORT_RANGE_START        0xc000
00125 #define TCP_LOCAL_PORT_RANGE_END          0xffff
00126 #define TCP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & (u16_t)~TCP_LOCAL_PORT_RANGE_START) + TCP_LOCAL_PORT_RANGE_START))
00127 #endif
00128 
00129 #if LWIP_TCP_KEEPALIVE
00130 #define TCP_KEEP_DUR(pcb)   ((pcb)->keep_cnt * (pcb)->keep_intvl)
00131 #define TCP_KEEP_INTVL(pcb) ((pcb)->keep_intvl)
00132 #else /* LWIP_TCP_KEEPALIVE */
00133 #define TCP_KEEP_DUR(pcb)   TCP_MAXIDLE
00134 #define TCP_KEEP_INTVL(pcb) TCP_KEEPINTVL_DEFAULT
00135 #endif /* LWIP_TCP_KEEPALIVE */
00136 
00137 /* As initial send MSS, we use TCP_MSS but limit it to 536. */
00138 #if TCP_MSS > 536
00139 #define INITIAL_MSS 536
00140 #else
00141 #define INITIAL_MSS TCP_MSS
00142 #endif
00143 
00144 static const char *const tcp_state_str[] = {
00145   "CLOSED",
00146   "LISTEN",
00147   "SYN_SENT",
00148   "SYN_RCVD",
00149   "ESTABLISHED",
00150   "FIN_WAIT_1",
00151   "FIN_WAIT_2",
00152   "CLOSE_WAIT",
00153   "CLOSING",
00154   "LAST_ACK",
00155   "TIME_WAIT"
00156 };
00157 
00158 /* last local TCP port */
00159 static u16_t tcp_port = TCP_LOCAL_PORT_RANGE_START;
00160 
00161 /* Incremented every coarse grained timer shot (typically every 500 ms). */
00162 u32_t tcp_ticks;
00163 static const u8_t tcp_backoff[13] =
00164 { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7};
00165 /* Times per slowtmr hits */
00166 static const u8_t tcp_persist_backoff[7] = { 3, 6, 12, 24, 48, 96, 120 };
00167 
00168 /* The TCP PCB lists. */
00169 
00170 /** List of all TCP PCBs bound but not yet (connected || listening) */
00171 struct tcp_pcb *tcp_bound_pcbs;
00172 /** List of all TCP PCBs in LISTEN state */
00173 union tcp_listen_pcbs_t tcp_listen_pcbs;
00174 /** List of all TCP PCBs that are in a state in which
00175  * they accept or send data. */
00176 struct tcp_pcb *tcp_active_pcbs;
00177 /** List of all TCP PCBs in TIME-WAIT state */
00178 struct tcp_pcb *tcp_tw_pcbs;
00179 
00180 /** An array with all (non-temporary) PCB lists, mainly used for smaller code size */
00181 struct tcp_pcb **const tcp_pcb_lists[] = {&tcp_listen_pcbs.pcbs, &tcp_bound_pcbs,
00182          &tcp_active_pcbs, &tcp_tw_pcbs
00183 };
00184 
00185 u8_t tcp_active_pcbs_changed;
00186 
00187 /** Timer counter to handle calling slow-timer from tcp_tmr() */
00188 static u8_t tcp_timer;
00189 static u8_t tcp_timer_ctr;
00190 static u16_t tcp_new_port(void);
00191 
00192 static err_t tcp_close_shutdown_fin(struct tcp_pcb *pcb);
00193 #if LWIP_TCP_PCB_NUM_EXT_ARGS
00194 static void tcp_ext_arg_invoke_callbacks_destroyed(struct tcp_pcb_ext_args *ext_args);
00195 #endif
00196 
00197 /**
00198  * Initialize this module.
00199  */
00200 void
00201 tcp_init(void)
00202 {
00203 #ifdef LWIP_RAND
00204   tcp_port = TCP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND());
00205 #endif /* LWIP_RAND */
00206 }
00207 
00208 /** Free a tcp pcb */
00209 void
00210 tcp_free(struct tcp_pcb *pcb)
00211 {
00212   LWIP_ASSERT("tcp_free: LISTEN", pcb->state != LISTEN);
00213 #if LWIP_TCP_PCB_NUM_EXT_ARGS
00214   tcp_ext_arg_invoke_callbacks_destroyed(pcb->ext_args);
00215 #endif
00216   memp_free(MEMP_TCP_PCB, pcb);
00217 }
00218 
00219 /** Free a tcp listen pcb */
00220 static void
00221 tcp_free_listen(struct tcp_pcb *pcb)
00222 {
00223   LWIP_ASSERT("tcp_free_listen: !LISTEN", pcb->state != LISTEN);
00224 #if LWIP_TCP_PCB_NUM_EXT_ARGS
00225   tcp_ext_arg_invoke_callbacks_destroyed(pcb->ext_args);
00226 #endif
00227   memp_free(MEMP_TCP_PCB_LISTEN, pcb);
00228 }
00229 
00230 /**
00231  * Called periodically to dispatch TCP timers.
00232  */
00233 void
00234 tcp_tmr(void)
00235 {
00236   /* Call tcp_fasttmr() every 250 ms */
00237   tcp_fasttmr();
00238 
00239   if (++tcp_timer & 1) {
00240     /* Call tcp_slowtmr() every 500 ms, i.e., every other timer
00241        tcp_tmr() is called. */
00242     tcp_slowtmr();
00243   }
00244 }
00245 
00246 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
00247 /** Called when a listen pcb is closed. Iterates one pcb list and removes the
00248  * closed listener pcb from pcb->listener if matching.
00249  */
00250 static void
00251 tcp_remove_listener(struct tcp_pcb *list, struct tcp_pcb_listen *lpcb)
00252 {
00253   struct tcp_pcb *pcb;
00254 
00255   LWIP_ASSERT("tcp_remove_listener: invalid listener", lpcb != NULL);
00256 
00257   for (pcb = list; pcb != NULL; pcb = pcb->next) {
00258     if (pcb->listener == lpcb) {
00259       pcb->listener = NULL;
00260     }
00261   }
00262 }
00263 #endif
00264 
00265 /** Called when a listen pcb is closed. Iterates all pcb lists and removes the
00266  * closed listener pcb from pcb->listener if matching.
00267  */
00268 static void
00269 tcp_listen_closed(struct tcp_pcb *pcb)
00270 {
00271 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
00272   size_t i;
00273   LWIP_ASSERT("pcb != NULL", pcb != NULL);
00274   LWIP_ASSERT("pcb->state == LISTEN", pcb->state == LISTEN);
00275   for (i = 1; i < LWIP_ARRAYSIZE(tcp_pcb_lists); i++) {
00276     tcp_remove_listener(*tcp_pcb_lists[i], (struct tcp_pcb_listen *)pcb);
00277   }
00278 #endif
00279   LWIP_UNUSED_ARG(pcb);
00280 }
00281 
00282 #if TCP_LISTEN_BACKLOG
00283 /** @ingroup tcp_raw
00284  * Delay accepting a connection in respect to the listen backlog:
00285  * the number of outstanding connections is increased until
00286  * tcp_backlog_accepted() is called.
00287  *
00288  * ATTENTION: the caller is responsible for calling tcp_backlog_accepted()
00289  * or else the backlog feature will get out of sync!
00290  *
00291  * @param pcb the connection pcb which is not fully accepted yet
00292  */
00293 void
00294 tcp_backlog_delayed(struct tcp_pcb *pcb)
00295 {
00296   LWIP_ASSERT("pcb != NULL", pcb != NULL);
00297   LWIP_ASSERT_CORE_LOCKED();
00298   if ((pcb->flags & TF_BACKLOGPEND) == 0) {
00299     if (pcb->listener != NULL) {
00300       pcb->listener->accepts_pending++;
00301       LWIP_ASSERT("accepts_pending != 0", pcb->listener->accepts_pending != 0);
00302       tcp_set_flags(pcb, TF_BACKLOGPEND);
00303     }
00304   }
00305 }
00306 
00307 /** @ingroup tcp_raw
00308  * A delayed-accept a connection is accepted (or closed/aborted): decreases
00309  * the number of outstanding connections after calling tcp_backlog_delayed().
00310  *
00311  * ATTENTION: the caller is responsible for calling tcp_backlog_accepted()
00312  * or else the backlog feature will get out of sync!
00313  *
00314  * @param pcb the connection pcb which is now fully accepted (or closed/aborted)
00315  */
00316 void
00317 tcp_backlog_accepted(struct tcp_pcb *pcb)
00318 {
00319   LWIP_ASSERT("pcb != NULL", pcb != NULL);
00320   LWIP_ASSERT_CORE_LOCKED();
00321   if ((pcb->flags & TF_BACKLOGPEND) != 0) {
00322     if (pcb->listener != NULL) {
00323       LWIP_ASSERT("accepts_pending != 0", pcb->listener->accepts_pending != 0);
00324       pcb->listener->accepts_pending--;
00325       tcp_clear_flags(pcb, TF_BACKLOGPEND);
00326     }
00327   }
00328 }
00329 #endif /* TCP_LISTEN_BACKLOG */
00330 
00331 /**
00332  * Closes the TX side of a connection held by the PCB.
00333  * For tcp_close(), a RST is sent if the application didn't receive all data
00334  * (tcp_recved() not called for all data passed to recv callback).
00335  *
00336  * Listening pcbs are freed and may not be referenced any more.
00337  * Connection pcbs are freed if not yet connected and may not be referenced
00338  * any more. If a connection is established (at least SYN received or in
00339  * a closing state), the connection is closed, and put in a closing state.
00340  * The pcb is then automatically freed in tcp_slowtmr(). It is therefore
00341  * unsafe to reference it.
00342  *
00343  * @param pcb the tcp_pcb to close
00344  * @return ERR_OK if connection has been closed
00345  *         another err_t if closing failed and pcb is not freed
00346  */
00347 static err_t
00348 tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data)
00349 {
00350   LWIP_ASSERT("tcp_close_shutdown: invalid pcb", pcb != NULL);
00351 
00352   if (rst_on_unacked_data && ((pcb->state == ESTABLISHED) || (pcb->state == CLOSE_WAIT))) {
00353     if ((pcb->refused_data != NULL) || (pcb->rcv_wnd != TCP_WND_MAX(pcb))) {
00354       /* Not all data received by application, send RST to tell the remote
00355          side about this. */
00356       LWIP_ASSERT("pcb->flags & TF_RXCLOSED", pcb->flags & TF_RXCLOSED);
00357 
00358       /* don't call tcp_abort here: we must not deallocate the pcb since
00359          that might not be expected when calling tcp_close */
00360       tcp_rst(pcb, pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
00361               pcb->local_port, pcb->remote_port);
00362 
00363       tcp_pcb_purge(pcb);
00364       TCP_RMV_ACTIVE(pcb);
00365       /* Deallocate the pcb since we already sent a RST for it */
00366       if (tcp_input_pcb == pcb) {
00367         /* prevent using a deallocated pcb: free it from tcp_input later */
00368         tcp_trigger_input_pcb_close();
00369       } else {
00370         tcp_free(pcb);
00371       }
00372       return ERR_OK;
00373     }
00374   }
00375 
00376   /* - states which free the pcb are handled here,
00377      - states which send FIN and change state are handled in tcp_close_shutdown_fin() */
00378   switch (pcb->state) {
00379     case CLOSED:
00380       /* Closing a pcb in the CLOSED state might seem erroneous,
00381        * however, it is in this state once allocated and as yet unused
00382        * and the user needs some way to free it should the need arise.
00383        * Calling tcp_close() with a pcb that has already been closed, (i.e. twice)
00384        * or for a pcb that has been used and then entered the CLOSED state
00385        * is erroneous, but this should never happen as the pcb has in those cases
00386        * been freed, and so any remaining handles are bogus. */
00387       if (pcb->local_port != 0) {
00388         TCP_RMV(&tcp_bound_pcbs, pcb);
00389       }
00390       tcp_free(pcb);
00391       break;
00392     case LISTEN:
00393       tcp_listen_closed(pcb);
00394       tcp_pcb_remove(&tcp_listen_pcbs.pcbs, pcb);
00395       tcp_free_listen(pcb);
00396       break;
00397     case SYN_SENT:
00398       TCP_PCB_REMOVE_ACTIVE(pcb);
00399       tcp_free(pcb);
00400       MIB2_STATS_INC(mib2.tcpattemptfails);
00401       break;
00402     default:
00403       return tcp_close_shutdown_fin(pcb);
00404   }
00405   return ERR_OK;
00406 }
00407 
00408 static err_t
00409 tcp_close_shutdown_fin(struct tcp_pcb *pcb)
00410 {
00411   err_t err;
00412   LWIP_ASSERT("pcb != NULL", pcb != NULL);
00413 
00414   switch (pcb->state) {
00415     case SYN_RCVD:
00416       err = tcp_send_fin(pcb);
00417       if (err == ERR_OK) {
00418         tcp_backlog_accepted(pcb);
00419         MIB2_STATS_INC(mib2.tcpattemptfails);
00420         pcb->state = FIN_WAIT_1;
00421       }
00422       break;
00423     case ESTABLISHED:
00424       err = tcp_send_fin(pcb);
00425       if (err == ERR_OK) {
00426         MIB2_STATS_INC(mib2.tcpestabresets);
00427         pcb->state = FIN_WAIT_1;
00428       }
00429       break;
00430     case CLOSE_WAIT:
00431       err = tcp_send_fin(pcb);
00432       if (err == ERR_OK) {
00433         MIB2_STATS_INC(mib2.tcpestabresets);
00434         pcb->state = LAST_ACK;
00435       }
00436       break;
00437     default:
00438       /* Has already been closed, do nothing. */
00439       return ERR_OK;
00440   }
00441 
00442   if (err == ERR_OK) {
00443     /* To ensure all data has been sent when tcp_close returns, we have
00444        to make sure tcp_output doesn't fail.
00445        Since we don't really have to ensure all data has been sent when tcp_close
00446        returns (unsent data is sent from tcp timer functions, also), we don't care
00447        for the return value of tcp_output for now. */
00448     tcp_output(pcb);
00449   } else if (err == ERR_MEM) {
00450     /* Mark this pcb for closing. Closing is retried from tcp_tmr. */
00451     tcp_set_flags(pcb, TF_CLOSEPEND);
00452     /* We have to return ERR_OK from here to indicate to the callers that this
00453        pcb should not be used any more as it will be freed soon via tcp_tmr.
00454        This is OK here since sending FIN does not guarantee a time frime for
00455        actually freeing the pcb, either (it is left in closure states for
00456        remote ACK or timeout) */
00457     return ERR_OK;
00458   }
00459   return err;
00460 }
00461 
00462 /**
00463  * @ingroup tcp_raw
00464  * Closes the connection held by the PCB.
00465  *
00466  * Listening pcbs are freed and may not be referenced any more.
00467  * Connection pcbs are freed if not yet connected and may not be referenced
00468  * any more. If a connection is established (at least SYN received or in
00469  * a closing state), the connection is closed, and put in a closing state.
00470  * The pcb is then automatically freed in tcp_slowtmr(). It is therefore
00471  * unsafe to reference it (unless an error is returned).
00472  * 
00473  * The function may return ERR_MEM if no memory
00474  * was available for closing the connection. If so, the application
00475  * should wait and try again either by using the acknowledgment
00476  * callback or the polling functionality. If the close succeeds, the
00477  * function returns ERR_OK.
00478  *
00479  * @param pcb the tcp_pcb to close
00480  * @return ERR_OK if connection has been closed
00481  *         another err_t if closing failed and pcb is not freed
00482  */
00483 err_t
00484 tcp_close(struct tcp_pcb *pcb)
00485 {
00486   LWIP_ASSERT_CORE_LOCKED();
00487 
00488   LWIP_ERROR("tcp_close: invalid pcb", pcb != NULL, return ERR_ARG);
00489   LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in "));
00490 
00491   tcp_debug_print_state(pcb->state);
00492 
00493   if (pcb->state != LISTEN) {
00494     /* Set a flag not to receive any more data... */
00495     tcp_set_flags(pcb, TF_RXCLOSED);
00496   }
00497   /* ... and close */
00498   return tcp_close_shutdown(pcb, 1);
00499 }
00500 
00501 /**
00502  * @ingroup tcp_raw
00503  * Causes all or part of a full-duplex connection of this PCB to be shut down.
00504  * This doesn't deallocate the PCB unless shutting down both sides!
00505  * Shutting down both sides is the same as calling tcp_close, so if it succeds
00506  * (i.e. returns ER_OK), the PCB must not be referenced any more!
00507  *
00508  * @param pcb PCB to shutdown
00509  * @param shut_rx shut down receive side if this is != 0
00510  * @param shut_tx shut down send side if this is != 0
00511  * @return ERR_OK if shutdown succeeded (or the PCB has already been shut down)
00512  *         another err_t on error.
00513  */
00514 err_t
00515 tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx)
00516 {
00517   LWIP_ASSERT_CORE_LOCKED();
00518 
00519   LWIP_ERROR("tcp_shutdown: invalid pcb", pcb != NULL, return ERR_ARG);
00520 
00521   if (pcb->state == LISTEN) {
00522     return ERR_CONN;
00523   }
00524   if (shut_rx) {
00525     /* shut down the receive side: set a flag not to receive any more data... */
00526     tcp_set_flags(pcb, TF_RXCLOSED);
00527     if (shut_tx) {
00528       /* shutting down the tx AND rx side is the same as closing for the raw API */
00529       return tcp_close_shutdown(pcb, 1);
00530     }
00531     /* ... and free buffered data */
00532     if (pcb->refused_data != NULL) {
00533       pbuf_free(pcb->refused_data);
00534       pcb->refused_data = NULL;
00535     }
00536   }
00537   if (shut_tx) {
00538     /* This can't happen twice since if it succeeds, the pcb's state is changed.
00539        Only close in these states as the others directly deallocate the PCB */
00540     switch (pcb->state) {
00541       case SYN_RCVD:
00542       case ESTABLISHED:
00543       case CLOSE_WAIT:
00544         return tcp_close_shutdown(pcb, (u8_t)shut_rx);
00545       default:
00546         /* Not (yet?) connected, cannot shutdown the TX side as that would bring us
00547           into CLOSED state, where the PCB is deallocated. */
00548         return ERR_CONN;
00549     }
00550   }
00551   return ERR_OK;
00552 }
00553 
00554 /**
00555  * Abandons a connection and optionally sends a RST to the remote
00556  * host.  Deletes the local protocol control block. This is done when
00557  * a connection is killed because of shortage of memory.
00558  *
00559  * @param pcb the tcp_pcb to abort
00560  * @param reset boolean to indicate whether a reset should be sent
00561  */
00562 void
00563 tcp_abandon(struct tcp_pcb *pcb, int reset)
00564 {
00565   u32_t seqno, ackno;
00566 #if LWIP_CALLBACK_API
00567   tcp_err_fn errf;
00568 #endif /* LWIP_CALLBACK_API */
00569   void *errf_arg;
00570 
00571   LWIP_ASSERT_CORE_LOCKED();
00572 
00573   LWIP_ERROR("tcp_abandon: invalid pcb", pcb != NULL, return);
00574 
00575   /* pcb->state LISTEN not allowed here */
00576   LWIP_ASSERT("don't call tcp_abort/tcp_abandon for listen-pcbs",
00577               pcb->state != LISTEN);
00578   /* Figure out on which TCP PCB list we are, and remove us. If we
00579      are in an active state, call the receive function associated with
00580      the PCB with a NULL argument, and send an RST to the remote end. */
00581   if (pcb->state == TIME_WAIT) {
00582     tcp_pcb_remove(&tcp_tw_pcbs, pcb);
00583     tcp_free(pcb);
00584   } else {
00585     int send_rst = 0;
00586     u16_t local_port = 0;
00587     enum tcp_state last_state;
00588     seqno = pcb->snd_nxt;
00589     ackno = pcb->rcv_nxt;
00590 #if LWIP_CALLBACK_API
00591     errf = pcb->errf;
00592 #endif /* LWIP_CALLBACK_API */
00593     errf_arg = pcb->callback_arg;
00594     if (pcb->state == CLOSED) {
00595       if (pcb->local_port != 0) {
00596         /* bound, not yet opened */
00597         TCP_RMV(&tcp_bound_pcbs, pcb);
00598       }
00599     } else {
00600       send_rst = reset;
00601       local_port = pcb->local_port;
00602       TCP_PCB_REMOVE_ACTIVE(pcb);
00603     }
00604     if (pcb->unacked != NULL) {
00605       tcp_segs_free(pcb->unacked);
00606     }
00607     if (pcb->unsent != NULL) {
00608       tcp_segs_free(pcb->unsent);
00609     }
00610 #if TCP_QUEUE_OOSEQ
00611     if (pcb->ooseq != NULL) {
00612       tcp_segs_free(pcb->ooseq);
00613     }
00614 #endif /* TCP_QUEUE_OOSEQ */
00615     tcp_backlog_accepted(pcb);
00616     if (send_rst) {
00617       LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abandon: sending RST\n"));
00618       tcp_rst(pcb, seqno, ackno, &pcb->local_ip, &pcb->remote_ip, local_port, pcb->remote_port);
00619     }
00620     last_state = pcb->state;
00621     tcp_free(pcb);
00622     TCP_EVENT_ERR(last_state, errf, errf_arg, ERR_ABRT);
00623   }
00624 }
00625 
00626 /**
00627  * @ingroup tcp_raw
00628  * Aborts the connection by sending a RST (reset) segment to the remote
00629  * host. The pcb is deallocated. This function never fails.
00630  *
00631  * ATTENTION: When calling this from one of the TCP callbacks, make
00632  * sure you always return ERR_ABRT (and never return ERR_ABRT otherwise
00633  * or you will risk accessing deallocated memory or memory leaks!
00634  *
00635  * @param pcb the tcp pcb to abort
00636  */
00637 void
00638 tcp_abort(struct tcp_pcb *pcb)
00639 {
00640   tcp_abandon(pcb, 1);
00641 }
00642 
00643 /**
00644  * @ingroup tcp_raw
00645  * Binds the connection to a local port number and IP address. If the
00646  * IP address is not given (i.e., ipaddr == IP_ANY_TYPE), the connection is
00647  * bound to all local IP addresses.
00648  * If another connection is bound to the same port, the function will
00649  * return ERR_USE, otherwise ERR_OK is returned.
00650  *
00651  * @param pcb the tcp_pcb to bind (no check is done whether this pcb is
00652  *        already bound!)
00653  * @param ipaddr the local ip address to bind to (use IPx_ADDR_ANY to bind
00654  *        to any local address
00655  * @param port the local port to bind to
00656  * @return ERR_USE if the port is already in use
00657  *         ERR_VAL if bind failed because the PCB is not in a valid state
00658  *         ERR_OK if bound
00659  */
00660 err_t
00661 tcp_bind(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
00662 {
00663   int i;
00664   int max_pcb_list = NUM_TCP_PCB_LISTS;
00665   struct tcp_pcb *cpcb;
00666 #if LWIP_IPV6 && LWIP_IPV6_SCOPES
00667   ip_addr_t zoned_ipaddr;
00668 #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
00669 
00670   LWIP_ASSERT_CORE_LOCKED();
00671 
00672 #if LWIP_IPV4
00673   /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
00674   if (ipaddr == NULL) {
00675     ipaddr = IP4_ADDR_ANY;
00676   }
00677 #else /* LWIP_IPV4 */
00678   LWIP_ERROR("tcp_bind: invalid ipaddr", ipaddr != NULL, return ERR_ARG);
00679 #endif /* LWIP_IPV4 */
00680 
00681   LWIP_ERROR("tcp_bind: invalid pcb", pcb != NULL, return ERR_ARG);
00682 
00683   LWIP_ERROR("tcp_bind: can only bind in state CLOSED", pcb->state == CLOSED, return ERR_VAL);
00684 
00685 #if SO_REUSE
00686   /* Unless the REUSEADDR flag is set,
00687      we have to check the pcbs in TIME-WAIT state, also.
00688      We do not dump TIME_WAIT pcb's; they can still be matched by incoming
00689      packets using both local and remote IP addresses and ports to distinguish.
00690    */
00691   if (ip_get_option(pcb, SOF_REUSEADDR)) {
00692     max_pcb_list = NUM_TCP_PCB_LISTS_NO_TIME_WAIT;
00693   }
00694 #endif /* SO_REUSE */
00695 
00696 #if LWIP_IPV6 && LWIP_IPV6_SCOPES
00697   /* If the given IP address should have a zone but doesn't, assign one now.
00698    * This is legacy support: scope-aware callers should always provide properly
00699    * zoned source addresses. Do the zone selection before the address-in-use
00700    * check below; as such we have to make a temporary copy of the address. */
00701   if (IP_IS_V6(ipaddr) && ip6_addr_lacks_zone(ip_2_ip6(ipaddr), IP6_UNICAST)) {
00702     ip_addr_copy(zoned_ipaddr, *ipaddr);
00703     ip6_addr_select_zone(ip_2_ip6(&zoned_ipaddr), ip_2_ip6(&zoned_ipaddr));
00704     ipaddr = &zoned_ipaddr;
00705   }
00706 #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
00707 
00708   if (port == 0) {
00709     port = tcp_new_port();
00710     if (port == 0) {
00711       return ERR_BUF;
00712     }
00713   } else {
00714     /* Check if the address already is in use (on all lists) */
00715     for (i = 0; i < max_pcb_list; i++) {
00716       for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
00717         if (cpcb->local_port == port) {
00718 #if SO_REUSE
00719           /* Omit checking for the same port if both pcbs have REUSEADDR set.
00720              For SO_REUSEADDR, the duplicate-check for a 5-tuple is done in
00721              tcp_connect. */
00722           if (!ip_get_option(pcb, SOF_REUSEADDR) ||
00723               !ip_get_option(cpcb, SOF_REUSEADDR))
00724 #endif /* SO_REUSE */
00725           {
00726             /* @todo: check accept_any_ip_version */
00727             if ((IP_IS_V6(ipaddr) == IP_IS_V6_VAL(cpcb->local_ip)) &&
00728                 (ip_addr_isany(&cpcb->local_ip) ||
00729                  ip_addr_isany(ipaddr) ||
00730                  ip_addr_cmp(&cpcb->local_ip, ipaddr))) {
00731               return ERR_USE;
00732             }
00733           }
00734         }
00735       }
00736     }
00737   }
00738 
00739   if (!ip_addr_isany(ipaddr)
00740 #if LWIP_IPV4 && LWIP_IPV6
00741       || (IP_GET_TYPE(ipaddr) != IP_GET_TYPE(&pcb->local_ip))
00742 #endif /* LWIP_IPV4 && LWIP_IPV6 */
00743      ) {
00744     ip_addr_set(&pcb->local_ip, ipaddr);
00745   }
00746   pcb->local_port = port;
00747   TCP_REG(&tcp_bound_pcbs, pcb);
00748   LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %"U16_F"\n", port));
00749   return ERR_OK;
00750 }
00751 
00752 /**
00753  * @ingroup tcp_raw
00754  * Binds the connection to a netif and IP address.
00755  * After calling this function, all packets received via this PCB
00756  * are guaranteed to have come in via the specified netif, and all
00757  * outgoing packets will go out via the specified netif.
00758  *
00759  * @param pcb the tcp_pcb to bind.
00760  * @param netif the netif to bind to. Can be NULL.
00761  */
00762 void
00763 tcp_bind_netif(struct tcp_pcb *pcb, const struct netif *netif)
00764 {
00765   LWIP_ASSERT_CORE_LOCKED();
00766   if (netif != NULL) {
00767     pcb->netif_idx = netif_get_index(netif);
00768   } else {
00769     pcb->netif_idx = NETIF_NO_INDEX;
00770   }
00771 }
00772 
00773 #if LWIP_CALLBACK_API
00774 /**
00775  * Default accept callback if no accept callback is specified by the user.
00776  */
00777 static err_t
00778 tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err)
00779 {
00780   LWIP_UNUSED_ARG(arg);
00781   LWIP_UNUSED_ARG(err);
00782 
00783   LWIP_ASSERT("tcp_accept_null: invalid pcb", pcb != NULL);
00784 
00785   tcp_abort(pcb);
00786 
00787   return ERR_ABRT;
00788 }
00789 #endif /* LWIP_CALLBACK_API */
00790 
00791 /**
00792  * @ingroup tcp_raw
00793  * Set the state of the connection to be LISTEN, which means that it
00794  * is able to accept incoming connections. The protocol control block
00795  * is reallocated in order to consume less memory. Setting the
00796  * connection to LISTEN is an irreversible process.
00797  * When an incoming connection is accepted, the function specified with
00798  * the tcp_accept() function will be called. The pcb has to be bound
00799  * to a local port with the tcp_bind() function.
00800  * 
00801  * The tcp_listen() function returns a new connection identifier, and
00802  * the one passed as an argument to the function will be
00803  * deallocated. The reason for this behavior is that less memory is
00804  * needed for a connection that is listening, so tcp_listen() will
00805  * reclaim the memory needed for the original connection and allocate a
00806  * new smaller memory block for the listening connection.
00807  *
00808  * tcp_listen() may return NULL if no memory was available for the
00809  * listening connection. If so, the memory associated with the pcb
00810  * passed as an argument to tcp_listen() will not be deallocated.
00811  *
00812  * The backlog limits the number of outstanding connections
00813  * in the listen queue to the value specified by the backlog argument.
00814  * To use it, your need to set TCP_LISTEN_BACKLOG=1 in your lwipopts.h.
00815  * 
00816  * @param pcb the original tcp_pcb
00817  * @param backlog the incoming connections queue limit
00818  * @return tcp_pcb used for listening, consumes less memory.
00819  *
00820  * @note The original tcp_pcb is freed. This function therefore has to be
00821  *       called like this:
00822  *             tpcb = tcp_listen_with_backlog(tpcb, backlog);
00823  */
00824 struct tcp_pcb *
00825 tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
00826 {
00827   LWIP_ASSERT_CORE_LOCKED();
00828   return tcp_listen_with_backlog_and_err(pcb, backlog, NULL);
00829 }
00830 
00831 /**
00832  * @ingroup tcp_raw
00833  * Set the state of the connection to be LISTEN, which means that it
00834  * is able to accept incoming connections. The protocol control block
00835  * is reallocated in order to consume less memory. Setting the
00836  * connection to LISTEN is an irreversible process.
00837  *
00838  * @param pcb the original tcp_pcb
00839  * @param backlog the incoming connections queue limit
00840  * @param err when NULL is returned, this contains the error reason
00841  * @return tcp_pcb used for listening, consumes less memory.
00842  *
00843  * @note The original tcp_pcb is freed. This function therefore has to be
00844  *       called like this:
00845  *             tpcb = tcp_listen_with_backlog_and_err(tpcb, backlog, &err);
00846  */
00847 struct tcp_pcb *
00848 tcp_listen_with_backlog_and_err(struct tcp_pcb *pcb, u8_t backlog, err_t *err)
00849 {
00850   struct tcp_pcb_listen *lpcb = NULL;
00851   err_t res;
00852 
00853   LWIP_UNUSED_ARG(backlog);
00854 
00855   LWIP_ASSERT_CORE_LOCKED();
00856 
00857   LWIP_ERROR("tcp_listen_with_backlog_and_err: invalid pcb", pcb != NULL, res = ERR_ARG; goto done);
00858   LWIP_ERROR("tcp_listen_with_backlog_and_err: pcb already connected", pcb->state == CLOSED, res = ERR_CLSD; goto done);
00859 
00860   /* already listening? */
00861   if (pcb->state == LISTEN) {
00862     lpcb = (struct tcp_pcb_listen *)pcb;
00863     res = ERR_ALREADY;
00864     goto done;
00865   }
00866 #if SO_REUSE
00867   if (ip_get_option(pcb, SOF_REUSEADDR)) {
00868     /* Since SOF_REUSEADDR allows reusing a local address before the pcb's usage
00869        is declared (listen-/connection-pcb), we have to make sure now that
00870        this port is only used once for every local IP. */
00871     for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
00872       if ((lpcb->local_port == pcb->local_port) &&
00873           ip_addr_cmp(&lpcb->local_ip, &pcb->local_ip)) {
00874         /* this address/port is already used */
00875         lpcb = NULL;
00876         res = ERR_USE;
00877         goto done;
00878       }
00879     }
00880   }
00881 #endif /* SO_REUSE */
00882   lpcb = (struct tcp_pcb_listen *)memp_malloc(MEMP_TCP_PCB_LISTEN);
00883   if (lpcb == NULL) {
00884     res = ERR_MEM;
00885     goto done;
00886   }
00887   lpcb->callback_arg = pcb->callback_arg;
00888   lpcb->local_port = pcb->local_port;
00889   lpcb->state = LISTEN;
00890   lpcb->prio = pcb->prio;
00891   lpcb->so_options = pcb->so_options;
00892   lpcb->netif_idx = NETIF_NO_INDEX;
00893   lpcb->ttl = pcb->ttl;
00894   lpcb->tos = pcb->tos;
00895 #if LWIP_IPV4 && LWIP_IPV6
00896   IP_SET_TYPE_VAL(lpcb->remote_ip, pcb->local_ip.type);
00897 #endif /* LWIP_IPV4 && LWIP_IPV6 */
00898   ip_addr_copy(lpcb->local_ip, pcb->local_ip);
00899   if (pcb->local_port != 0) {
00900     TCP_RMV(&tcp_bound_pcbs, pcb);
00901   }
00902 #if LWIP_TCP_PCB_NUM_EXT_ARGS
00903   /* copy over ext_args to listening pcb  */
00904   memcpy(&lpcb->ext_args, &pcb->ext_args, sizeof(pcb->ext_args));
00905 #endif
00906   tcp_free(pcb);
00907 #if LWIP_CALLBACK_API
00908   lpcb->accept = tcp_accept_null;
00909 #endif /* LWIP_CALLBACK_API */
00910 #if TCP_LISTEN_BACKLOG
00911   lpcb->accepts_pending = 0;
00912   tcp_backlog_set(lpcb, backlog);
00913 #endif /* TCP_LISTEN_BACKLOG */
00914   TCP_REG(&tcp_listen_pcbs.pcbs, (struct tcp_pcb *)lpcb);
00915   res = ERR_OK;
00916 done:
00917   if (err != NULL) {
00918     *err = res;
00919   }
00920   return (struct tcp_pcb *)lpcb;
00921 }
00922 
00923 /**
00924  * Update the state that tracks the available window space to advertise.
00925  *
00926  * Returns how much extra window would be advertised if we sent an
00927  * update now.
00928  */
00929 u32_t
00930 tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb)
00931 {
00932   u32_t new_right_edge;
00933 
00934   LWIP_ASSERT("tcp_update_rcv_ann_wnd: invalid pcb", pcb != NULL);
00935   new_right_edge = pcb->rcv_nxt + pcb->rcv_wnd;
00936 
00937   if (TCP_SEQ_GEQ(new_right_edge, pcb->rcv_ann_right_edge + LWIP_MIN((TCP_WND / 2), pcb->mss))) {
00938     /* we can advertise more window */
00939     pcb->rcv_ann_wnd = pcb->rcv_wnd;
00940     return new_right_edge - pcb->rcv_ann_right_edge;
00941   } else {
00942     if (TCP_SEQ_GT(pcb->rcv_nxt, pcb->rcv_ann_right_edge)) {
00943       /* Can happen due to other end sending out of advertised window,
00944        * but within actual available (but not yet advertised) window */
00945       pcb->rcv_ann_wnd = 0;
00946     } else {
00947       /* keep the right edge of window constant */
00948       u32_t new_rcv_ann_wnd = pcb->rcv_ann_right_edge - pcb->rcv_nxt;
00949 #if !LWIP_WND_SCALE
00950       LWIP_ASSERT("new_rcv_ann_wnd <= 0xffff", new_rcv_ann_wnd <= 0xffff);
00951 #endif
00952       pcb->rcv_ann_wnd = (tcpwnd_size_t)new_rcv_ann_wnd;
00953     }
00954     return 0;
00955   }
00956 }
00957 
00958 /**
00959  * @ingroup tcp_raw
00960  * This function should be called by the application when it has
00961  * processed the data. The purpose is to advertise a larger window
00962  * when the data has been processed.
00963  *
00964  * @param pcb the tcp_pcb for which data is read
00965  * @param len the amount of bytes that have been read by the application
00966  */
00967 void
00968 tcp_recved(struct tcp_pcb *pcb, u16_t len)
00969 {
00970   u32_t wnd_inflation;
00971   tcpwnd_size_t rcv_wnd;
00972 
00973   LWIP_ASSERT_CORE_LOCKED();
00974 
00975   LWIP_ERROR("tcp_recved: invalid pcb", pcb != NULL, return);
00976 
00977   /* pcb->state LISTEN not allowed here */
00978   LWIP_ASSERT("don't call tcp_recved for listen-pcbs",
00979               pcb->state != LISTEN);
00980 
00981   rcv_wnd = (tcpwnd_size_t)(pcb->rcv_wnd + len);
00982   if ((rcv_wnd > TCP_WND_MAX(pcb)) || (rcv_wnd < pcb->rcv_wnd)) {
00983     /* window got too big or tcpwnd_size_t overflow */
00984     LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: window got too big or tcpwnd_size_t overflow\n"));
00985     pcb->rcv_wnd = TCP_WND_MAX(pcb);
00986   } else  {
00987     pcb->rcv_wnd = rcv_wnd;
00988   }
00989 
00990   wnd_inflation = tcp_update_rcv_ann_wnd(pcb);
00991 
00992   /* If the change in the right edge of window is significant (default
00993    * watermark is TCP_WND/4), then send an explicit update now.
00994    * Otherwise wait for a packet to be sent in the normal course of
00995    * events (or more window to be available later) */
00996   if (wnd_inflation >= TCP_WND_UPDATE_THRESHOLD) {
00997     tcp_ack_now(pcb);
00998     tcp_output(pcb);
00999   }
01000 
01001   LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: received %"U16_F" bytes, wnd %"TCPWNDSIZE_F" (%"TCPWNDSIZE_F").\n",
01002                           len, pcb->rcv_wnd, (u16_t)(TCP_WND_MAX(pcb) - pcb->rcv_wnd)));
01003 }
01004 
01005 /**
01006  * Allocate a new local TCP port.
01007  *
01008  * @return a new (free) local TCP port number
01009  */
01010 static u16_t
01011 tcp_new_port(void)
01012 {
01013   u8_t i;
01014   u16_t n = 0;
01015   struct tcp_pcb *pcb;
01016 
01017 again:
01018   tcp_port++;
01019   if (tcp_port == TCP_LOCAL_PORT_RANGE_END) {
01020     tcp_port = TCP_LOCAL_PORT_RANGE_START;
01021   }
01022   /* Check all PCB lists. */
01023   for (i = 0; i < NUM_TCP_PCB_LISTS; i++) {
01024     for (pcb = *tcp_pcb_lists[i]; pcb != NULL; pcb = pcb->next) {
01025       if (pcb->local_port == tcp_port) {
01026         n++;
01027         if (n > (TCP_LOCAL_PORT_RANGE_END - TCP_LOCAL_PORT_RANGE_START)) {
01028           return 0;
01029         }
01030         goto again;
01031       }
01032     }
01033   }
01034   return tcp_port;
01035 }
01036 
01037 /**
01038  * @ingroup tcp_raw
01039  * Connects to another host. The function given as the "connected"
01040  * argument will be called when the connection has been established.
01041  *  Sets up the pcb to connect to the remote host and sends the
01042  * initial SYN segment which opens the connection. 
01043  *
01044  * The tcp_connect() function returns immediately; it does not wait for
01045  * the connection to be properly setup. Instead, it will call the
01046  * function specified as the fourth argument (the "connected" argument)
01047  * when the connection is established. If the connection could not be
01048  * properly established, either because the other host refused the
01049  * connection or because the other host didn't answer, the "err"
01050  * callback function of this pcb (registered with tcp_err, see below)
01051  * will be called.
01052  *
01053  * The tcp_connect() function can return ERR_MEM if no memory is
01054  * available for enqueueing the SYN segment. If the SYN indeed was
01055  * enqueued successfully, the tcp_connect() function returns ERR_OK.
01056  *
01057  * @param pcb the tcp_pcb used to establish the connection
01058  * @param ipaddr the remote ip address to connect to
01059  * @param port the remote tcp port to connect to
01060  * @param connected callback function to call when connected (on error,
01061                     the err calback will be called)
01062  * @return ERR_VAL if invalid arguments are given
01063  *         ERR_OK if connect request has been sent
01064  *         other err_t values if connect request couldn't be sent
01065  */
01066 err_t
01067 tcp_connect(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port,
01068             tcp_connected_fn connected)
01069 {
01070   struct netif *netif = NULL;
01071   err_t ret;
01072   u32_t iss;
01073   u16_t old_local_port;
01074 
01075   LWIP_ASSERT_CORE_LOCKED();
01076 
01077   LWIP_ERROR("tcp_connect: invalid pcb", pcb != NULL, return ERR_ARG);
01078   LWIP_ERROR("tcp_connect: invalid ipaddr", ipaddr != NULL, return ERR_ARG);
01079 
01080   LWIP_ERROR("tcp_connect: can only connect from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
01081 
01082   LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port));
01083   ip_addr_set(&pcb->remote_ip, ipaddr);
01084   pcb->remote_port = port;
01085 
01086   if (pcb->netif_idx != NETIF_NO_INDEX) {
01087     netif = netif_get_by_index(pcb->netif_idx);
01088   } else {
01089     /* check if we have a route to the remote host */
01090     netif = ip_route(&pcb->local_ip, &pcb->remote_ip);
01091   }
01092   if (netif == NULL) {
01093     /* Don't even try to send a SYN packet if we have no route since that will fail. */
01094     return ERR_RTE;
01095   }
01096 
01097   /* check if local IP has been assigned to pcb, if not, get one */
01098   if (ip_addr_isany(&pcb->local_ip)) {
01099     const ip_addr_t *local_ip = ip_netif_get_local_ip(netif, ipaddr);
01100     if (local_ip == NULL) {
01101       return ERR_RTE;
01102     }
01103     ip_addr_copy(pcb->local_ip, *local_ip);
01104   }
01105 
01106 #if LWIP_IPV6 && LWIP_IPV6_SCOPES
01107   /* If the given IP address should have a zone but doesn't, assign one now.
01108    * Given that we already have the target netif, this is easy and cheap. */
01109   if (IP_IS_V6(&pcb->remote_ip) &&
01110       ip6_addr_lacks_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNICAST)) {
01111     ip6_addr_assign_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNICAST, netif);
01112   }
01113 #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
01114 
01115   old_local_port = pcb->local_port;
01116   if (pcb->local_port == 0) {
01117     pcb->local_port = tcp_new_port();
01118     if (pcb->local_port == 0) {
01119       return ERR_BUF;
01120     }
01121   } else {
01122 #if SO_REUSE
01123     if (ip_get_option(pcb, SOF_REUSEADDR)) {
01124       /* Since SOF_REUSEADDR allows reusing a local address, we have to make sure
01125          now that the 5-tuple is unique. */
01126       struct tcp_pcb *cpcb;
01127       int i;
01128       /* Don't check listen- and bound-PCBs, check active- and TIME-WAIT PCBs. */
01129       for (i = 2; i < NUM_TCP_PCB_LISTS; i++) {
01130         for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
01131           if ((cpcb->local_port == pcb->local_port) &&
01132               (cpcb->remote_port == port) &&
01133               ip_addr_cmp(&cpcb->local_ip, &pcb->local_ip) &&
01134               ip_addr_cmp(&cpcb->remote_ip, ipaddr)) {
01135             /* linux returns EISCONN here, but ERR_USE should be OK for us */
01136             return ERR_USE;
01137           }
01138         }
01139       }
01140     }
01141 #endif /* SO_REUSE */
01142   }
01143 
01144   iss = tcp_next_iss(pcb);
01145   pcb->rcv_nxt = 0;
01146   pcb->snd_nxt = iss;
01147   pcb->lastack = iss - 1;
01148   pcb->snd_wl2 = iss - 1;
01149   pcb->snd_lbb = iss - 1;
01150   /* Start with a window that does not need scaling. When window scaling is
01151      enabled and used, the window is enlarged when both sides agree on scaling. */
01152   pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_WND);
01153   pcb->rcv_ann_right_edge = pcb->rcv_nxt;
01154   pcb->snd_wnd = TCP_WND;
01155   /* As initial send MSS, we use TCP_MSS but limit it to 536.
01156      The send MSS is updated when an MSS option is received. */
01157   pcb->mss = INITIAL_MSS;
01158 #if TCP_CALCULATE_EFF_SEND_MSS
01159   pcb->mss = tcp_eff_send_mss_netif(pcb->mss, netif, &pcb->remote_ip);
01160 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
01161   pcb->cwnd = 1;
01162 #if LWIP_CALLBACK_API
01163   pcb->connected = connected;
01164 #else /* LWIP_CALLBACK_API */
01165   LWIP_UNUSED_ARG(connected);
01166 #endif /* LWIP_CALLBACK_API */
01167 
01168   /* Send a SYN together with the MSS option. */
01169   ret = tcp_enqueue_flags(pcb, TCP_SYN);
01170   if (ret == ERR_OK) {
01171     /* SYN segment was enqueued, changed the pcbs state now */
01172     pcb->state = SYN_SENT;
01173     if (old_local_port != 0) {
01174       TCP_RMV(&tcp_bound_pcbs, pcb);
01175     }
01176     TCP_REG_ACTIVE(pcb);
01177     MIB2_STATS_INC(mib2.tcpactiveopens);
01178 
01179     tcp_output(pcb);
01180   }
01181   return ret;
01182 }
01183 
01184 /**
01185  * Called every 500 ms and implements the retransmission timer and the timer that
01186  * removes PCBs that have been in TIME-WAIT for enough time. It also increments
01187  * various timers such as the inactivity timer in each PCB.
01188  *
01189  * Automatically called from tcp_tmr().
01190  */
01191 void
01192 tcp_slowtmr(void)
01193 {
01194   struct tcp_pcb *pcb, *prev;
01195   tcpwnd_size_t eff_wnd;
01196   u8_t pcb_remove;      /* flag if a PCB should be removed */
01197   u8_t pcb_reset;       /* flag if a RST should be sent when removing */
01198   err_t err;
01199 
01200   err = ERR_OK;
01201 
01202   ++tcp_ticks;
01203   ++tcp_timer_ctr;
01204 
01205 tcp_slowtmr_start:
01206   /* Steps through all of the active PCBs. */
01207   prev = NULL;
01208   pcb = tcp_active_pcbs;
01209   if (pcb == NULL) {
01210     LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: no active pcbs\n"));
01211   }
01212   while (pcb != NULL) {
01213     LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: processing active pcb\n"));
01214     LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED);
01215     LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN);
01216     LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT);
01217     if (pcb->last_timer == tcp_timer_ctr) {
01218       /* skip this pcb, we have already processed it */
01219       prev = pcb;
01220       pcb = pcb->next;
01221       continue;
01222     }
01223     pcb->last_timer = tcp_timer_ctr;
01224 
01225     pcb_remove = 0;
01226     pcb_reset = 0;
01227 
01228     if (pcb->state == SYN_SENT && pcb->nrtx >= TCP_SYNMAXRTX) {
01229       ++pcb_remove;
01230       LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max SYN retries reached\n"));
01231     } else if (pcb->nrtx >= TCP_MAXRTX) {
01232       ++pcb_remove;
01233       LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max DATA retries reached\n"));
01234     } else {
01235       if (pcb->persist_backoff > 0) {
01236         LWIP_ASSERT("tcp_slowtimr: persist ticking with in-flight data", pcb->unacked == NULL);
01237         LWIP_ASSERT("tcp_slowtimr: persist ticking with empty send buffer", pcb->unsent != NULL);
01238         if (pcb->persist_probe >= TCP_MAXRTX) {
01239           ++pcb_remove; /* max probes reached */
01240         } else {
01241           u8_t backoff_cnt = tcp_persist_backoff[pcb->persist_backoff - 1];
01242           if (pcb->persist_cnt < backoff_cnt) {
01243             pcb->persist_cnt++;
01244           }
01245           if (pcb->persist_cnt >= backoff_cnt) {
01246             int next_slot = 1; /* increment timer to next slot */
01247             /* If snd_wnd is zero, send 1 byte probes */
01248             if (pcb->snd_wnd == 0) {
01249               if (tcp_zero_window_probe(pcb) != ERR_OK) {
01250                 next_slot = 0; /* try probe again with current slot */
01251               }
01252               /* snd_wnd not fully closed, split unsent head and fill window */
01253             } else {
01254               if (tcp_split_unsent_seg(pcb, (u16_t)pcb->snd_wnd) == ERR_OK) {
01255                 if (tcp_output(pcb) == ERR_OK) {
01256                   /* sending will cancel persist timer, else retry with current slot */
01257                   next_slot = 0;
01258                 }
01259               }
01260             }
01261             if (next_slot) {
01262               pcb->persist_cnt = 0;
01263               if (pcb->persist_backoff < sizeof(tcp_persist_backoff)) {
01264                 pcb->persist_backoff++;
01265               }
01266             }
01267           }
01268         }
01269       } else {
01270         /* Increase the retransmission timer if it is running */
01271         if ((pcb->rtime >= 0) && (pcb->rtime < 0x7FFF)) {
01272           ++pcb->rtime;
01273         }
01274 
01275         if (pcb->rtime >= pcb->rto) {
01276           /* Time for a retransmission. */
01277           LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_slowtmr: rtime %"S16_F
01278                                       " pcb->rto %"S16_F"\n",
01279                                       pcb->rtime, pcb->rto));
01280           /* If prepare phase fails but we have unsent data but no unacked data,
01281              still execute the backoff calculations below, as this means we somehow
01282              failed to send segment. */
01283           if ((tcp_rexmit_rto_prepare(pcb) == ERR_OK) || ((pcb->unacked == NULL) && (pcb->unsent != NULL))) {
01284             /* Double retransmission time-out unless we are trying to
01285              * connect to somebody (i.e., we are in SYN_SENT). */
01286             if (pcb->state != SYN_SENT) {
01287               u8_t backoff_idx = LWIP_MIN(pcb->nrtx, sizeof(tcp_backoff) - 1);
01288               int calc_rto = ((pcb->sa >> 3) + pcb->sv) << tcp_backoff[backoff_idx];
01289               pcb->rto = (s16_t)LWIP_MIN(calc_rto, 0x7FFF);
01290             }
01291 
01292             /* Reset the retransmission timer. */
01293             pcb->rtime = 0;
01294 
01295             /* Reduce congestion window and ssthresh. */
01296             eff_wnd = LWIP_MIN(pcb->cwnd, pcb->snd_wnd);
01297             pcb->ssthresh = eff_wnd >> 1;
01298             if (pcb->ssthresh < (tcpwnd_size_t)(pcb->mss << 1)) {
01299               pcb->ssthresh = (tcpwnd_size_t)(pcb->mss << 1);
01300             }
01301             pcb->cwnd = pcb->mss;
01302             LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: cwnd %"TCPWNDSIZE_F
01303                                          " ssthresh %"TCPWNDSIZE_F"\n",
01304                                          pcb->cwnd, pcb->ssthresh));
01305             pcb->bytes_acked = 0;
01306 
01307             /* The following needs to be called AFTER cwnd is set to one
01308                mss - STJ */
01309             tcp_rexmit_rto_commit(pcb);
01310           }
01311         }
01312       }
01313     }
01314     /* Check if this PCB has stayed too long in FIN-WAIT-2 */
01315     if (pcb->state == FIN_WAIT_2) {
01316       /* If this PCB is in FIN_WAIT_2 because of SHUT_WR don't let it time out. */
01317       if (pcb->flags & TF_RXCLOSED) {
01318         /* PCB was fully closed (either through close() or SHUT_RDWR):
01319            normal FIN-WAIT timeout handling. */
01320         if ((u32_t)(tcp_ticks - pcb->tmr) >
01321             TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) {
01322           ++pcb_remove;
01323           LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2\n"));
01324         }
01325       }
01326     }
01327 
01328     /* Check if KEEPALIVE should be sent */
01329     if (ip_get_option(pcb, SOF_KEEPALIVE) &&
01330         ((pcb->state == ESTABLISHED) ||
01331          (pcb->state == CLOSE_WAIT))) {
01332       if ((u32_t)(tcp_ticks - pcb->tmr) >
01333           (pcb->keep_idle + TCP_KEEP_DUR(pcb)) / TCP_SLOW_INTERVAL) {
01334         LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: KEEPALIVE timeout. Aborting connection to "));
01335         ip_addr_debug_print_val(TCP_DEBUG, pcb->remote_ip);
01336         LWIP_DEBUGF(TCP_DEBUG, ("\n"));
01337 
01338         ++pcb_remove;
01339         ++pcb_reset;
01340       } else if ((u32_t)(tcp_ticks - pcb->tmr) >
01341                  (pcb->keep_idle + pcb->keep_cnt_sent * TCP_KEEP_INTVL(pcb))
01342                  / TCP_SLOW_INTERVAL) {
01343         err = tcp_keepalive(pcb);
01344         if (err == ERR_OK) {
01345           pcb->keep_cnt_sent++;
01346         }
01347       }
01348     }
01349 
01350     /* If this PCB has queued out of sequence data, but has been
01351        inactive for too long, will drop the data (it will eventually
01352        be retransmitted). */
01353 #if TCP_QUEUE_OOSEQ
01354     if (pcb->ooseq != NULL &&
01355         (tcp_ticks - pcb->tmr >= (u32_t)pcb->rto * TCP_OOSEQ_TIMEOUT)) {
01356       LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: dropping OOSEQ queued data\n"));
01357       tcp_free_ooseq(pcb);
01358     }
01359 #endif /* TCP_QUEUE_OOSEQ */
01360 
01361     /* Check if this PCB has stayed too long in SYN-RCVD */
01362     if (pcb->state == SYN_RCVD) {
01363       if ((u32_t)(tcp_ticks - pcb->tmr) >
01364           TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) {
01365         ++pcb_remove;
01366         LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD\n"));
01367       }
01368     }
01369 
01370     /* Check if this PCB has stayed too long in LAST-ACK */
01371     if (pcb->state == LAST_ACK) {
01372       if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
01373         ++pcb_remove;
01374         LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in LAST-ACK\n"));
01375       }
01376     }
01377 
01378     /* If the PCB should be removed, do it. */
01379     if (pcb_remove) {
01380       struct tcp_pcb *pcb2;
01381 #if LWIP_CALLBACK_API
01382       tcp_err_fn err_fn = pcb->errf;
01383 #endif /* LWIP_CALLBACK_API */
01384       void *err_arg;
01385       enum tcp_state last_state;
01386       tcp_pcb_purge(pcb);
01387       /* Remove PCB from tcp_active_pcbs list. */
01388       if (prev != NULL) {
01389         LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs);
01390         prev->next = pcb->next;
01391       } else {
01392         /* This PCB was the first. */
01393         LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb);
01394         tcp_active_pcbs = pcb->next;
01395       }
01396 
01397       if (pcb_reset) {
01398         tcp_rst(pcb, pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
01399                 pcb->local_port, pcb->remote_port);
01400       }
01401 
01402       err_arg = pcb->callback_arg;
01403       last_state = pcb->state;
01404       pcb2 = pcb;
01405       pcb = pcb->next;
01406       tcp_free(pcb2);
01407 
01408       tcp_active_pcbs_changed = 0;
01409       TCP_EVENT_ERR(last_state, err_fn, err_arg, ERR_ABRT);
01410       if (tcp_active_pcbs_changed) {
01411         goto tcp_slowtmr_start;
01412       }
01413     } else {
01414       /* get the 'next' element now and work with 'prev' below (in case of abort) */
01415       prev = pcb;
01416       pcb = pcb->next;
01417 
01418       /* We check if we should poll the connection. */
01419       ++prev->polltmr;
01420       if (prev->polltmr >= prev->pollinterval) {
01421         prev->polltmr = 0;
01422         LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: polling application\n"));
01423         tcp_active_pcbs_changed = 0;
01424         TCP_EVENT_POLL(prev, err);
01425         if (tcp_active_pcbs_changed) {
01426           goto tcp_slowtmr_start;
01427         }
01428         /* if err == ERR_ABRT, 'prev' is already deallocated */
01429         if (err == ERR_OK) {
01430           tcp_output(prev);
01431         }
01432       }
01433     }
01434   }
01435 
01436 
01437   /* Steps through all of the TIME-WAIT PCBs. */
01438   prev = NULL;
01439   pcb = tcp_tw_pcbs;
01440   while (pcb != NULL) {
01441     LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
01442     pcb_remove = 0;
01443 
01444     /* Check if this PCB has stayed long enough in TIME-WAIT */
01445     if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
01446       ++pcb_remove;
01447     }
01448 
01449     /* If the PCB should be removed, do it. */
01450     if (pcb_remove) {
01451       struct tcp_pcb *pcb2;
01452       tcp_pcb_purge(pcb);
01453       /* Remove PCB from tcp_tw_pcbs list. */
01454       if (prev != NULL) {
01455         LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs);
01456         prev->next = pcb->next;
01457       } else {
01458         /* This PCB was the first. */
01459         LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb);
01460         tcp_tw_pcbs = pcb->next;
01461       }
01462       pcb2 = pcb;
01463       pcb = pcb->next;
01464       tcp_free(pcb2);
01465     } else {
01466       prev = pcb;
01467       pcb = pcb->next;
01468     }
01469   }
01470 }
01471 
01472 /**
01473  * Is called every TCP_FAST_INTERVAL (250 ms) and process data previously
01474  * "refused" by upper layer (application) and sends delayed ACKs or pending FINs.
01475  *
01476  * Automatically called from tcp_tmr().
01477  */
01478 void
01479 tcp_fasttmr(void)
01480 {
01481   struct tcp_pcb *pcb;
01482 
01483   ++tcp_timer_ctr;
01484 
01485 tcp_fasttmr_start:
01486   pcb = tcp_active_pcbs;
01487 
01488   while (pcb != NULL) {
01489     if (pcb->last_timer != tcp_timer_ctr) {
01490       struct tcp_pcb *next;
01491       pcb->last_timer = tcp_timer_ctr;
01492       /* send delayed ACKs */
01493       if (pcb->flags & TF_ACK_DELAY) {
01494         LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n"));
01495         tcp_ack_now(pcb);
01496         tcp_output(pcb);
01497         tcp_clear_flags(pcb, TF_ACK_DELAY | TF_ACK_NOW);
01498       }
01499       /* send pending FIN */
01500       if (pcb->flags & TF_CLOSEPEND) {
01501         LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: pending FIN\n"));
01502         tcp_clear_flags(pcb, TF_CLOSEPEND);
01503         tcp_close_shutdown_fin(pcb);
01504       }
01505 
01506       next = pcb->next;
01507 
01508       /* If there is data which was previously "refused" by upper layer */
01509       if (pcb->refused_data != NULL) {
01510         tcp_active_pcbs_changed = 0;
01511         tcp_process_refused_data(pcb);
01512         if (tcp_active_pcbs_changed) {
01513           /* application callback has changed the pcb list: restart the loop */
01514           goto tcp_fasttmr_start;
01515         }
01516       }
01517       pcb = next;
01518     } else {
01519       pcb = pcb->next;
01520     }
01521   }
01522 }
01523 
01524 /** Call tcp_output for all active pcbs that have TF_NAGLEMEMERR set */
01525 void
01526 tcp_txnow(void)
01527 {
01528   struct tcp_pcb *pcb;
01529 
01530   for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
01531     if (pcb->flags & TF_NAGLEMEMERR) {
01532       tcp_output(pcb);
01533     }
01534   }
01535 }
01536 
01537 /** Pass pcb->refused_data to the recv callback */
01538 err_t
01539 tcp_process_refused_data(struct tcp_pcb *pcb)
01540 {
01541 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
01542   struct pbuf *rest;
01543 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
01544 
01545   LWIP_ERROR("tcp_process_refused_data: invalid pcb", pcb != NULL, return ERR_ARG);
01546 
01547 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
01548   while (pcb->refused_data != NULL)
01549 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
01550   {
01551     err_t err;
01552     u8_t refused_flags = pcb->refused_data->flags;
01553     /* set pcb->refused_data to NULL in case the callback frees it and then
01554        closes the pcb */
01555     struct pbuf *refused_data = pcb->refused_data;
01556 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
01557     pbuf_split_64k(refused_data, &rest);
01558     pcb->refused_data = rest;
01559 #else /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
01560     pcb->refused_data = NULL;
01561 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
01562     /* Notify again application with data previously received. */
01563     LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: notify kept packet\n"));
01564     TCP_EVENT_RECV(pcb, refused_data, ERR_OK, err);
01565     if (err == ERR_OK) {
01566       /* did refused_data include a FIN? */
01567       if ((refused_flags & PBUF_FLAG_TCP_FIN)
01568 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
01569           && (rest == NULL)
01570 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
01571          ) {
01572         /* correct rcv_wnd as the application won't call tcp_recved()
01573            for the FIN's seqno */
01574         if (pcb->rcv_wnd != TCP_WND_MAX(pcb)) {
01575           pcb->rcv_wnd++;
01576         }
01577         TCP_EVENT_CLOSED(pcb, err);
01578         if (err == ERR_ABRT) {
01579           return ERR_ABRT;
01580         }
01581       }
01582     } else if (err == ERR_ABRT) {
01583       /* if err == ERR_ABRT, 'pcb' is already deallocated */
01584       /* Drop incoming packets because pcb is "full" (only if the incoming
01585          segment contains data). */
01586       LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: drop incoming packets, because pcb is \"full\"\n"));
01587       return ERR_ABRT;
01588     } else {
01589       /* data is still refused, pbuf is still valid (go on for ACK-only packets) */
01590 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
01591       if (rest != NULL) {
01592         pbuf_cat(refused_data, rest);
01593       }
01594 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
01595       pcb->refused_data = refused_data;
01596       return ERR_INPROGRESS;
01597     }
01598   }
01599   return ERR_OK;
01600 }
01601 
01602 /**
01603  * Deallocates a list of TCP segments (tcp_seg structures).
01604  *
01605  * @param seg tcp_seg list of TCP segments to free
01606  */
01607 void
01608 tcp_segs_free(struct tcp_seg *seg)
01609 {
01610   while (seg != NULL) {
01611     struct tcp_seg *next = seg->next;
01612     tcp_seg_free(seg);
01613     seg = next;
01614   }
01615 }
01616 
01617 /**
01618  * Frees a TCP segment (tcp_seg structure).
01619  *
01620  * @param seg single tcp_seg to free
01621  */
01622 void
01623 tcp_seg_free(struct tcp_seg *seg)
01624 {
01625   if (seg != NULL) {
01626     if (seg->p != NULL) {
01627       pbuf_free(seg->p);
01628 #if TCP_DEBUG
01629       seg->p = NULL;
01630 #endif /* TCP_DEBUG */
01631     }
01632     memp_free(MEMP_TCP_SEG, seg);
01633   }
01634 }
01635 
01636 /**
01637  * @ingroup tcp
01638  * Sets the priority of a connection.
01639  *
01640  * @param pcb the tcp_pcb to manipulate
01641  * @param prio new priority
01642  */
01643 void
01644 tcp_setprio(struct tcp_pcb *pcb, u8_t prio)
01645 {
01646   LWIP_ASSERT_CORE_LOCKED();
01647 
01648   LWIP_ERROR("tcp_setprio: invalid pcb", pcb != NULL, return);
01649 
01650   pcb->prio = prio;
01651 }
01652 
01653 #if TCP_QUEUE_OOSEQ
01654 /**
01655  * Returns a copy of the given TCP segment.
01656  * The pbuf and data are not copied, only the pointers
01657  *
01658  * @param seg the old tcp_seg
01659  * @return a copy of seg
01660  */
01661 struct tcp_seg *
01662 tcp_seg_copy(struct tcp_seg *seg)
01663 {
01664   struct tcp_seg *cseg;
01665 
01666   LWIP_ASSERT("tcp_seg_copy: invalid seg", seg != NULL);
01667 
01668   cseg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG);
01669   if (cseg == NULL) {
01670     return NULL;
01671   }
01672   SMEMCPY((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg));
01673   pbuf_ref(cseg->p);
01674   return cseg;
01675 }
01676 #endif /* TCP_QUEUE_OOSEQ */
01677 
01678 #if LWIP_CALLBACK_API
01679 /**
01680  * Default receive callback that is called if the user didn't register
01681  * a recv callback for the pcb.
01682  */
01683 err_t
01684 tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
01685 {
01686   LWIP_UNUSED_ARG(arg);
01687 
01688   LWIP_ERROR("tcp_recv_null: invalid pcb", pcb != NULL, return ERR_ARG);
01689 
01690   if (p != NULL) {
01691     tcp_recved(pcb, p->tot_len);
01692     pbuf_free(p);
01693   } else if (err == ERR_OK) {
01694     return tcp_close(pcb);
01695   }
01696   return ERR_OK;
01697 }
01698 #endif /* LWIP_CALLBACK_API */
01699 
01700 /**
01701  * Kills the oldest active connection that has a lower priority than 'prio'.
01702  *
01703  * @param prio minimum priority
01704  */
01705 static void
01706 tcp_kill_prio(u8_t prio)
01707 {
01708   struct tcp_pcb *pcb, *inactive;
01709   u32_t inactivity;
01710   u8_t mprio;
01711 
01712   mprio = LWIP_MIN(TCP_PRIO_MAX, prio);
01713 
01714   /* We want to kill connections with a lower prio, so bail out if 
01715    * supplied prio is 0 - there can never be a lower prio
01716    */
01717   if (mprio == 0) {
01718     return;
01719   }
01720 
01721   /* We only want kill connections with a lower prio, so decrement prio by one 
01722    * and start searching for oldest connection with same or lower priority than mprio.
01723    * We want to find the connections with the lowest possible prio, and among
01724    * these the one with the longest inactivity time.
01725    */
01726    mprio--;
01727 
01728   inactivity = 0;
01729   inactive = NULL;
01730   for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
01731         /* lower prio is always a kill candidate */
01732     if ((pcb->prio < mprio) ||
01733         /* longer inactivity is also a kill candidate */
01734         ((pcb->prio == mprio) && ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity))) {
01735       inactivity = tcp_ticks - pcb->tmr;
01736       inactive   = pcb;
01737       mprio      = pcb->prio;
01738     }
01739   }
01740   if (inactive != NULL) {
01741     LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F")\n",
01742                             (void *)inactive, inactivity));
01743     tcp_abort(inactive);
01744   }
01745 }
01746 
01747 /**
01748  * Kills the oldest connection that is in specific state.
01749  * Called from tcp_alloc() for LAST_ACK and CLOSING if no more connections are available.
01750  */
01751 static void
01752 tcp_kill_state(enum tcp_state state)
01753 {
01754   struct tcp_pcb *pcb, *inactive;
01755   u32_t inactivity;
01756 
01757   LWIP_ASSERT("invalid state", (state == CLOSING) || (state == LAST_ACK));
01758 
01759   inactivity = 0;
01760   inactive = NULL;
01761   /* Go through the list of active pcbs and get the oldest pcb that is in state
01762      CLOSING/LAST_ACK. */
01763   for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
01764     if (pcb->state == state) {
01765       if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
01766         inactivity = tcp_ticks - pcb->tmr;
01767         inactive = pcb;
01768       }
01769     }
01770   }
01771   if (inactive != NULL) {
01772     LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_closing: killing oldest %s PCB %p (%"S32_F")\n",
01773                             tcp_state_str[state], (void *)inactive, inactivity));
01774     /* Don't send a RST, since no data is lost. */
01775     tcp_abandon(inactive, 0);
01776   }
01777 }
01778 
01779 /**
01780  * Kills the oldest connection that is in TIME_WAIT state.
01781  * Called from tcp_alloc() if no more connections are available.
01782  */
01783 static void
01784 tcp_kill_timewait(void)
01785 {
01786   struct tcp_pcb *pcb, *inactive;
01787   u32_t inactivity;
01788 
01789   inactivity = 0;
01790   inactive = NULL;
01791   /* Go through the list of TIME_WAIT pcbs and get the oldest pcb. */
01792   for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
01793     if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
01794       inactivity = tcp_ticks - pcb->tmr;
01795       inactive = pcb;
01796     }
01797   }
01798   if (inactive != NULL) {
01799     LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F")\n",
01800                             (void *)inactive, inactivity));
01801     tcp_abort(inactive);
01802   }
01803 }
01804 
01805 /* Called when allocating a pcb fails.
01806  * In this case, we want to handle all pcbs that want to close first: if we can
01807  * now send the FIN (which failed before), the pcb might be in a state that is
01808  * OK for us to now free it.
01809  */
01810 static void
01811 tcp_handle_closepend(void)
01812 {
01813   struct tcp_pcb *pcb = tcp_active_pcbs;
01814 
01815   while (pcb != NULL) {
01816     struct tcp_pcb *next = pcb->next;
01817     /* send pending FIN */
01818     if (pcb->flags & TF_CLOSEPEND) {
01819       LWIP_DEBUGF(TCP_DEBUG, ("tcp_handle_closepend: pending FIN\n"));
01820       tcp_clear_flags(pcb, TF_CLOSEPEND);
01821       tcp_close_shutdown_fin(pcb);
01822     }
01823     pcb = next;
01824   }
01825 }
01826 
01827 /**
01828  * Allocate a new tcp_pcb structure.
01829  *
01830  * @param prio priority for the new pcb
01831  * @return a new tcp_pcb that initially is in state CLOSED
01832  */
01833 struct tcp_pcb *
01834 tcp_alloc(u8_t prio)
01835 {
01836   struct tcp_pcb *pcb;
01837 
01838   LWIP_ASSERT_CORE_LOCKED();
01839 
01840   pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
01841   if (pcb == NULL) {
01842     /* Try to send FIN for all pcbs stuck in TF_CLOSEPEND first */
01843     tcp_handle_closepend();
01844 
01845     /* Try killing oldest connection in TIME-WAIT. */
01846     LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest TIME-WAIT connection\n"));
01847     tcp_kill_timewait();
01848     /* Try to allocate a tcp_pcb again. */
01849     pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
01850     if (pcb == NULL) {
01851       /* Try killing oldest connection in LAST-ACK (these wouldn't go to TIME-WAIT). */
01852       LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest LAST-ACK connection\n"));
01853       tcp_kill_state(LAST_ACK);
01854       /* Try to allocate a tcp_pcb again. */
01855       pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
01856       if (pcb == NULL) {
01857         /* Try killing oldest connection in CLOSING. */
01858         LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest CLOSING connection\n"));
01859         tcp_kill_state(CLOSING);
01860         /* Try to allocate a tcp_pcb again. */
01861         pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
01862         if (pcb == NULL) {
01863           /* Try killing oldest active connection with lower priority than the new one. */
01864           LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing oldest connection with prio lower than %d\n", prio));
01865           tcp_kill_prio(prio);
01866           /* Try to allocate a tcp_pcb again. */
01867           pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
01868           if (pcb != NULL) {
01869             /* adjust err stats: memp_malloc failed multiple times before */
01870             MEMP_STATS_DEC(err, MEMP_TCP_PCB);
01871           }
01872         }
01873         if (pcb != NULL) {
01874           /* adjust err stats: memp_malloc failed multiple times before */
01875           MEMP_STATS_DEC(err, MEMP_TCP_PCB);
01876         }
01877       }
01878       if (pcb != NULL) {
01879         /* adjust err stats: memp_malloc failed multiple times before */
01880         MEMP_STATS_DEC(err, MEMP_TCP_PCB);
01881       }
01882     }
01883     if (pcb != NULL) {
01884       /* adjust err stats: memp_malloc failed above */
01885       MEMP_STATS_DEC(err, MEMP_TCP_PCB);
01886     }
01887   }
01888   if (pcb != NULL) {
01889     /* zero out the whole pcb, so there is no need to initialize members to zero */
01890     memset(pcb, 0, sizeof(struct tcp_pcb));
01891     pcb->prio = prio;
01892     pcb->snd_buf = TCP_SND_BUF;
01893     /* Start with a window that does not need scaling. When window scaling is
01894        enabled and used, the window is enlarged when both sides agree on scaling. */
01895     pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_WND);
01896     pcb->ttl = TCP_TTL;
01897     /* As initial send MSS, we use TCP_MSS but limit it to 536.
01898        The send MSS is updated when an MSS option is received. */
01899     pcb->mss = INITIAL_MSS;
01900     pcb->rto = 3000 / TCP_SLOW_INTERVAL;
01901     pcb->sv = 3000 / TCP_SLOW_INTERVAL;
01902     pcb->rtime = -1;
01903     pcb->cwnd = 1;
01904     pcb->tmr = tcp_ticks;
01905     pcb->last_timer = tcp_timer_ctr;
01906 
01907     /* RFC 5681 recommends setting ssthresh abritrarily high and gives an example
01908     of using the largest advertised receive window.  We've seen complications with
01909     receiving TCPs that use window scaling and/or window auto-tuning where the
01910     initial advertised window is very small and then grows rapidly once the
01911     connection is established. To avoid these complications, we set ssthresh to the
01912     largest effective cwnd (amount of in-flight data) that the sender can have. */
01913     pcb->ssthresh = TCP_SND_BUF;
01914 
01915 #if LWIP_CALLBACK_API
01916     pcb->recv = tcp_recv_null;
01917 #endif /* LWIP_CALLBACK_API */
01918 
01919     /* Init KEEPALIVE timer */
01920     pcb->keep_idle  = TCP_KEEPIDLE_DEFAULT;
01921 
01922 #if LWIP_TCP_KEEPALIVE
01923     pcb->keep_intvl = TCP_KEEPINTVL_DEFAULT;
01924     pcb->keep_cnt   = TCP_KEEPCNT_DEFAULT;
01925 #endif /* LWIP_TCP_KEEPALIVE */
01926   }
01927   return pcb;
01928 }
01929 
01930 /**
01931  * @ingroup tcp_raw
01932  * Creates a new TCP protocol control block but doesn't place it on
01933  * any of the TCP PCB lists.
01934  * The pcb is not put on any list until binding using tcp_bind().
01935  * If memory is not available for creating the new pcb, NULL is returned.
01936  *
01937  * @internal: Maybe there should be a idle TCP PCB list where these
01938  * PCBs are put on. Port reservation using tcp_bind() is implemented but
01939  * allocated pcbs that are not bound can't be killed automatically if wanting
01940  * to allocate a pcb with higher prio (@see tcp_kill_prio())
01941  *
01942  * @return a new tcp_pcb that initially is in state CLOSED
01943  */
01944 struct tcp_pcb *
01945 tcp_new(void)
01946 {
01947   return tcp_alloc(TCP_PRIO_NORMAL);
01948 }
01949 
01950 /**
01951  * @ingroup tcp_raw
01952  * Creates a new TCP protocol control block but doesn't
01953  * place it on any of the TCP PCB lists.
01954  * The pcb is not put on any list until binding using tcp_bind().
01955  *
01956  * @param type IP address type, see @ref lwip_ip_addr_type definitions.
01957  * If you want to listen to IPv4 and IPv6 (dual-stack) connections,
01958  * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE.
01959  * @return a new tcp_pcb that initially is in state CLOSED
01960  */
01961 struct tcp_pcb *
01962 tcp_new_ip_type(u8_t type)
01963 {
01964   struct tcp_pcb *pcb;
01965   pcb = tcp_alloc(TCP_PRIO_NORMAL);
01966 #if LWIP_IPV4 && LWIP_IPV6
01967   if (pcb != NULL) {
01968     IP_SET_TYPE_VAL(pcb->local_ip, type);
01969     IP_SET_TYPE_VAL(pcb->remote_ip, type);
01970   }
01971 #else
01972   LWIP_UNUSED_ARG(type);
01973 #endif /* LWIP_IPV4 && LWIP_IPV6 */
01974   return pcb;
01975 }
01976 
01977 /**
01978  * @ingroup tcp_raw
01979  * Specifies the program specific state that should be passed to all
01980  * other callback functions. The "pcb" argument is the current TCP
01981  * connection control block, and the "arg" argument is the argument
01982  * that will be passed to the callbacks.
01983  *
01984  * @param pcb tcp_pcb to set the callback argument
01985  * @param arg void pointer argument to pass to callback functions
01986  */
01987 void
01988 tcp_arg(struct tcp_pcb *pcb, void *arg)
01989 {
01990   LWIP_ASSERT_CORE_LOCKED();
01991   /* This function is allowed to be called for both listen pcbs and
01992      connection pcbs. */
01993   if (pcb != NULL) {
01994     pcb->callback_arg = arg;
01995   }
01996 }
01997 #if LWIP_CALLBACK_API
01998 
01999 /**
02000  * @ingroup tcp_raw
02001  * Sets the callback function that will be called when new data
02002  * arrives. The callback function will be passed a NULL pbuf to
02003  * indicate that the remote host has closed the connection. If the
02004  * callback function returns ERR_OK or ERR_ABRT it must have
02005  * freed the pbuf, otherwise it must not have freed it.
02006  *
02007  * @param pcb tcp_pcb to set the recv callback
02008  * @param recv callback function to call for this pcb when data is received
02009  */
02010 void
02011 tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv)
02012 {
02013   LWIP_ASSERT_CORE_LOCKED();
02014   if (pcb != NULL) {
02015     LWIP_ASSERT("invalid socket state for recv callback", pcb->state != LISTEN);
02016     pcb->recv = recv;
02017   }
02018 }
02019 
02020 /**
02021  * @ingroup tcp_raw
02022  * Specifies the callback function that should be called when data has
02023  * successfully been received (i.e., acknowledged) by the remote
02024  * host. The len argument passed to the callback function gives the
02025  * amount bytes that was acknowledged by the last acknowledgment.
02026  *
02027  * @param pcb tcp_pcb to set the sent callback
02028  * @param sent callback function to call for this pcb when data is successfully sent
02029  */
02030 void
02031 tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent)
02032 {
02033   LWIP_ASSERT_CORE_LOCKED();
02034   if (pcb != NULL) {
02035     LWIP_ASSERT("invalid socket state for sent callback", pcb->state != LISTEN);
02036     pcb->sent = sent;
02037   }
02038 }
02039 
02040 /**
02041  * @ingroup tcp_raw
02042  * Used to specify the function that should be called when a fatal error
02043  * has occurred on the connection.
02044  * 
02045  * If a connection is aborted because of an error, the application is
02046  * alerted of this event by the err callback. Errors that might abort a
02047  * connection are when there is a shortage of memory. The callback
02048  * function to be called is set using the tcp_err() function.
02049  *
02050  * @note The corresponding pcb is already freed when this callback is called!
02051  *
02052  * @param pcb tcp_pcb to set the err callback
02053  * @param err callback function to call for this pcb when a fatal error
02054  *        has occurred on the connection
02055  */
02056 void
02057 tcp_err(struct tcp_pcb *pcb, tcp_err_fn err)
02058 {
02059   LWIP_ASSERT_CORE_LOCKED();
02060   if (pcb != NULL) {
02061     LWIP_ASSERT("invalid socket state for err callback", pcb->state != LISTEN);
02062     pcb->errf = err;
02063   }
02064 }
02065 
02066 /**
02067  * @ingroup tcp_raw
02068  * Used for specifying the function that should be called when a
02069  * LISTENing connection has been connected to another host.
02070  *
02071  * @param pcb tcp_pcb to set the accept callback
02072  * @param accept callback function to call for this pcb when LISTENing
02073  *        connection has been connected to another host
02074  */
02075 void
02076 tcp_accept(struct tcp_pcb *pcb, tcp_accept_fn accept)
02077 {
02078   LWIP_ASSERT_CORE_LOCKED();
02079   if ((pcb != NULL) && (pcb->state == LISTEN)) {
02080     struct tcp_pcb_listen *lpcb = (struct tcp_pcb_listen *)pcb;
02081     lpcb->accept = accept;
02082   }
02083 }
02084 #endif /* LWIP_CALLBACK_API */
02085 
02086 
02087 /**
02088  * @ingroup tcp_raw
02089  * Specifies the polling interval and the callback function that should
02090  * be called to poll the application. The interval is specified in
02091  * number of TCP coarse grained timer shots, which typically occurs
02092  * twice a second. An interval of 10 means that the application would
02093  * be polled every 5 seconds.
02094  * 
02095  * When a connection is idle (i.e., no data is either transmitted or
02096  * received), lwIP will repeatedly poll the application by calling a
02097  * specified callback function. This can be used either as a watchdog
02098  * timer for killing connections that have stayed idle for too long, or
02099  * as a method of waiting for memory to become available. For instance,
02100  * if a call to tcp_write() has failed because memory wasn't available,
02101  * the application may use the polling functionality to call tcp_write()
02102  * again when the connection has been idle for a while.
02103  */
02104 void
02105 tcp_poll(struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval)
02106 {
02107   LWIP_ASSERT_CORE_LOCKED();
02108 
02109   LWIP_ERROR("tcp_poll: invalid pcb", pcb != NULL, return);
02110   LWIP_ASSERT("invalid socket state for poll", pcb->state != LISTEN);
02111 
02112 #if LWIP_CALLBACK_API
02113   pcb->poll = poll;
02114 #else /* LWIP_CALLBACK_API */
02115   LWIP_UNUSED_ARG(poll);
02116 #endif /* LWIP_CALLBACK_API */
02117   pcb->pollinterval = interval;
02118 }
02119 
02120 /**
02121  * Purges a TCP PCB. Removes any buffered data and frees the buffer memory
02122  * (pcb->ooseq, pcb->unsent and pcb->unacked are freed).
02123  *
02124  * @param pcb tcp_pcb to purge. The pcb itself is not deallocated!
02125  */
02126 void
02127 tcp_pcb_purge(struct tcp_pcb *pcb)
02128 {
02129   LWIP_ERROR("tcp_pcb_purge: invalid pcb", pcb != NULL, return);
02130 
02131   if (pcb->state != CLOSED &&
02132       pcb->state != TIME_WAIT &&
02133       pcb->state != LISTEN) {
02134 
02135     LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge\n"));
02136 
02137     tcp_backlog_accepted(pcb);
02138 
02139     if (pcb->refused_data != NULL) {
02140       LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->refused_data\n"));
02141       pbuf_free(pcb->refused_data);
02142       pcb->refused_data = NULL;
02143     }
02144     if (pcb->unsent != NULL) {
02145       LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: not all data sent\n"));
02146     }
02147     if (pcb->unacked != NULL) {
02148       LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->unacked\n"));
02149     }
02150 #if TCP_QUEUE_OOSEQ
02151     if (pcb->ooseq != NULL) {
02152       LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->ooseq\n"));
02153       tcp_free_ooseq(pcb);
02154     }
02155 #endif /* TCP_QUEUE_OOSEQ */
02156 
02157     /* Stop the retransmission timer as it will expect data on unacked
02158        queue if it fires */
02159     pcb->rtime = -1;
02160 
02161     tcp_segs_free(pcb->unsent);
02162     tcp_segs_free(pcb->unacked);
02163     pcb->unacked = pcb->unsent = NULL;
02164 #if TCP_OVERSIZE
02165     pcb->unsent_oversize = 0;
02166 #endif /* TCP_OVERSIZE */
02167   }
02168 }
02169 
02170 /**
02171  * Purges the PCB and removes it from a PCB list. Any delayed ACKs are sent first.
02172  *
02173  * @param pcblist PCB list to purge.
02174  * @param pcb tcp_pcb to purge. The pcb itself is NOT deallocated!
02175  */
02176 void
02177 tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb)
02178 {
02179   LWIP_ASSERT("tcp_pcb_remove: invalid pcb", pcb != NULL);
02180   LWIP_ASSERT("tcp_pcb_remove: invalid pcblist", pcblist != NULL);
02181 
02182   TCP_RMV(pcblist, pcb);
02183 
02184   tcp_pcb_purge(pcb);
02185 
02186   /* if there is an outstanding delayed ACKs, send it */
02187   if ((pcb->state != TIME_WAIT) &&
02188       (pcb->state != LISTEN) &&
02189       (pcb->flags & TF_ACK_DELAY)) {
02190     tcp_ack_now(pcb);
02191     tcp_output(pcb);
02192   }
02193 
02194   if (pcb->state != LISTEN) {
02195     LWIP_ASSERT("unsent segments leaking", pcb->unsent == NULL);
02196     LWIP_ASSERT("unacked segments leaking", pcb->unacked == NULL);
02197 #if TCP_QUEUE_OOSEQ
02198     LWIP_ASSERT("ooseq segments leaking", pcb->ooseq == NULL);
02199 #endif /* TCP_QUEUE_OOSEQ */
02200   }
02201 
02202   pcb->state = CLOSED;
02203   /* reset the local port to prevent the pcb from being 'bound' */
02204   pcb->local_port = 0;
02205 
02206   LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane());
02207 }
02208 
02209 /**
02210  * Calculates a new initial sequence number for new connections.
02211  *
02212  * @return u32_t pseudo random sequence number
02213  */
02214 u32_t
02215 tcp_next_iss(struct tcp_pcb *pcb)
02216 {
02217 #ifdef LWIP_HOOK_TCP_ISN
02218   LWIP_ASSERT("tcp_next_iss: invalid pcb", pcb != NULL);
02219   return LWIP_HOOK_TCP_ISN(&pcb->local_ip, pcb->local_port, &pcb->remote_ip, pcb->remote_port);
02220 #else /* LWIP_HOOK_TCP_ISN */
02221   static u32_t iss = 6510;
02222 
02223   LWIP_ASSERT("tcp_next_iss: invalid pcb", pcb != NULL);
02224   LWIP_UNUSED_ARG(pcb);
02225 
02226   iss += tcp_ticks;       /* XXX */
02227   return iss;
02228 #endif /* LWIP_HOOK_TCP_ISN */
02229 }
02230 
02231 #if TCP_CALCULATE_EFF_SEND_MSS
02232 /**
02233  * Calculates the effective send mss that can be used for a specific IP address
02234  * by calculating the minimum of TCP_MSS and the mtu (if set) of the target
02235  * netif (if not NULL).
02236  */
02237 u16_t
02238 tcp_eff_send_mss_netif(u16_t sendmss, struct netif *outif, const ip_addr_t *dest)
02239 {
02240   u16_t mss_s;
02241   u16_t mtu;
02242 
02243   LWIP_UNUSED_ARG(dest); /* in case IPv6 is disabled */
02244 
02245   LWIP_ASSERT("tcp_eff_send_mss_netif: invalid dst_ip", dest != NULL);
02246 
02247 #if LWIP_IPV6
02248 #if LWIP_IPV4
02249   if (IP_IS_V6(dest))
02250 #endif /* LWIP_IPV4 */
02251   {
02252     /* First look in destination cache, to see if there is a Path MTU. */
02253     mtu = nd6_get_destination_mtu(ip_2_ip6(dest), outif);
02254   }
02255 #if LWIP_IPV4
02256   else
02257 #endif /* LWIP_IPV4 */
02258 #endif /* LWIP_IPV6 */
02259 #if LWIP_IPV4
02260   {
02261     if (outif == NULL) {
02262       return sendmss;
02263     }
02264     mtu = outif->mtu;
02265   }
02266 #endif /* LWIP_IPV4 */
02267 
02268   if (mtu != 0) {
02269     u16_t offset;
02270 #if LWIP_IPV6
02271 #if LWIP_IPV4
02272     if (IP_IS_V6(dest))
02273 #endif /* LWIP_IPV4 */
02274     {
02275       offset = IP6_HLEN + TCP_HLEN;
02276     }
02277 #if LWIP_IPV4
02278     else
02279 #endif /* LWIP_IPV4 */
02280 #endif /* LWIP_IPV6 */
02281 #if LWIP_IPV4
02282     {
02283       offset = IP_HLEN + TCP_HLEN;
02284     }
02285 #endif /* LWIP_IPV4 */
02286     mss_s = (mtu > offset) ? (u16_t)(mtu - offset) : 0;
02287     /* RFC 1122, chap 4.2.2.6:
02288      * Eff.snd.MSS = min(SendMSS+20, MMS_S) - TCPhdrsize - IPoptionsize
02289      * We correct for TCP options in tcp_write(), and don't support IP options.
02290      */
02291     sendmss = LWIP_MIN(sendmss, mss_s);
02292   }
02293   return sendmss;
02294 }
02295 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
02296 
02297 /** Helper function for tcp_netif_ip_addr_changed() that iterates a pcb list */
02298 static void
02299 tcp_netif_ip_addr_changed_pcblist(const ip_addr_t *old_addr, struct tcp_pcb *pcb_list)
02300 {
02301   struct tcp_pcb *pcb;
02302   pcb = pcb_list;
02303 
02304   LWIP_ASSERT("tcp_netif_ip_addr_changed_pcblist: invalid old_addr", old_addr != NULL);
02305 
02306   while (pcb != NULL) {
02307     /* PCB bound to current local interface address? */
02308     if (ip_addr_cmp(&pcb->local_ip, old_addr)
02309 #if LWIP_AUTOIP
02310         /* connections to link-local addresses must persist (RFC3927 ch. 1.9) */
02311         && (!IP_IS_V4_VAL(pcb->local_ip) || !ip4_addr_islinklocal(ip_2_ip4(&pcb->local_ip)))
02312 #endif /* LWIP_AUTOIP */
02313        ) {
02314       /* this connection must be aborted */
02315       struct tcp_pcb *next = pcb->next;
02316       LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
02317       tcp_abort(pcb);
02318       pcb = next;
02319     } else {
02320       pcb = pcb->next;
02321     }
02322   }
02323 }
02324 
02325 /** This function is called from netif.c when address is changed or netif is removed
02326  *
02327  * @param old_addr IP address of the netif before change
02328  * @param new_addr IP address of the netif after change or NULL if netif has been removed
02329  */
02330 void
02331 tcp_netif_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr)
02332 {
02333   struct tcp_pcb_listen *lpcb;
02334 
02335   if (!ip_addr_isany(old_addr)) {
02336     tcp_netif_ip_addr_changed_pcblist(old_addr, tcp_active_pcbs);
02337     tcp_netif_ip_addr_changed_pcblist(old_addr, tcp_bound_pcbs);
02338 
02339     if (!ip_addr_isany(new_addr)) {
02340       /* PCB bound to current local interface address? */
02341       for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
02342         /* PCB bound to current local interface address? */
02343         if (ip_addr_cmp(&lpcb->local_ip, old_addr)) {
02344           /* The PCB is listening to the old ipaddr and
02345             * is set to listen to the new one instead */
02346           ip_addr_copy(lpcb->local_ip, *new_addr);
02347         }
02348       }
02349     }
02350   }
02351 }
02352 
02353 const char *
02354 tcp_debug_state_str(enum tcp_state s)
02355 {
02356   return tcp_state_str[s];
02357 }
02358 
02359 err_t
02360 tcp_tcp_get_tcp_addrinfo(struct tcp_pcb *pcb, int local, ip_addr_t *addr, u16_t *port)
02361 {
02362   if (pcb) {
02363     if (local) {
02364       if (addr) {
02365         *addr = pcb->local_ip;
02366       }
02367       if (port) {
02368         *port = pcb->local_port;
02369       }
02370     } else {
02371       if (addr) {
02372         *addr = pcb->remote_ip;
02373       }
02374       if (port) {
02375         *port = pcb->remote_port;
02376       }
02377     }
02378     return ERR_OK;
02379   }
02380   return ERR_VAL;
02381 }
02382 
02383 #if TCP_QUEUE_OOSEQ
02384 /* Free all ooseq pbufs (and possibly reset SACK state) */
02385 void
02386 tcp_free_ooseq(struct tcp_pcb *pcb)
02387 {
02388   if (pcb->ooseq) {
02389     tcp_segs_free(pcb->ooseq);
02390     pcb->ooseq = NULL;
02391 #if LWIP_TCP_SACK_OUT
02392     memset(pcb->rcv_sacks, 0, sizeof(pcb->rcv_sacks));
02393 #endif /* LWIP_TCP_SACK_OUT */
02394   }
02395 }
02396 #endif /* TCP_QUEUE_OOSEQ */
02397 
02398 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
02399 /**
02400  * Print a tcp header for debugging purposes.
02401  *
02402  * @param tcphdr pointer to a struct tcp_hdr
02403  */
02404 void
02405 tcp_debug_print(struct tcp_hdr *tcphdr)
02406 {
02407   LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n"));
02408   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
02409   LWIP_DEBUGF(TCP_DEBUG, ("|    %5"U16_F"      |    %5"U16_F"      | (src port, dest port)\n",
02410                           lwip_ntohs(tcphdr->src), lwip_ntohs(tcphdr->dest)));
02411   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
02412   LWIP_DEBUGF(TCP_DEBUG, ("|           %010"U32_F"          | (seq no)\n",
02413                           lwip_ntohl(tcphdr->seqno)));
02414   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
02415   LWIP_DEBUGF(TCP_DEBUG, ("|           %010"U32_F"          | (ack no)\n",
02416                           lwip_ntohl(tcphdr->ackno)));
02417   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
02418   LWIP_DEBUGF(TCP_DEBUG, ("| %2"U16_F" |   |%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"|     %5"U16_F"     | (hdrlen, flags (",
02419                           TCPH_HDRLEN(tcphdr),
02420                           (u16_t)(TCPH_FLAGS(tcphdr) >> 5 & 1),
02421                           (u16_t)(TCPH_FLAGS(tcphdr) >> 4 & 1),
02422                           (u16_t)(TCPH_FLAGS(tcphdr) >> 3 & 1),
02423                           (u16_t)(TCPH_FLAGS(tcphdr) >> 2 & 1),
02424                           (u16_t)(TCPH_FLAGS(tcphdr) >> 1 & 1),
02425                           (u16_t)(TCPH_FLAGS(tcphdr)      & 1),
02426                           lwip_ntohs(tcphdr->wnd)));
02427   tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
02428   LWIP_DEBUGF(TCP_DEBUG, ("), win)\n"));
02429   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
02430   LWIP_DEBUGF(TCP_DEBUG, ("|    0x%04"X16_F"     |     %5"U16_F"     | (chksum, urgp)\n",
02431                           lwip_ntohs(tcphdr->chksum), lwip_ntohs(tcphdr->urgp)));
02432   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
02433 }
02434 
02435 /**
02436  * Print a tcp state for debugging purposes.
02437  *
02438  * @param s enum tcp_state to print
02439  */
02440 void
02441 tcp_debug_print_state(enum tcp_state s)
02442 {
02443   LWIP_DEBUGF(TCP_DEBUG, ("State: %s\n", tcp_state_str[s]));
02444 }
02445 
02446 /**
02447  * Print tcp flags for debugging purposes.
02448  *
02449  * @param flags tcp flags, all active flags are printed
02450  */
02451 void
02452 tcp_debug_print_flags(u8_t flags)
02453 {
02454   if (flags & TCP_FIN) {
02455     LWIP_DEBUGF(TCP_DEBUG, ("FIN "));
02456   }
02457   if (flags & TCP_SYN) {
02458     LWIP_DEBUGF(TCP_DEBUG, ("SYN "));
02459   }
02460   if (flags & TCP_RST) {
02461     LWIP_DEBUGF(TCP_DEBUG, ("RST "));
02462   }
02463   if (flags & TCP_PSH) {
02464     LWIP_DEBUGF(TCP_DEBUG, ("PSH "));
02465   }
02466   if (flags & TCP_ACK) {
02467     LWIP_DEBUGF(TCP_DEBUG, ("ACK "));
02468   }
02469   if (flags & TCP_URG) {
02470     LWIP_DEBUGF(TCP_DEBUG, ("URG "));
02471   }
02472   if (flags & TCP_ECE) {
02473     LWIP_DEBUGF(TCP_DEBUG, ("ECE "));
02474   }
02475   if (flags & TCP_CWR) {
02476     LWIP_DEBUGF(TCP_DEBUG, ("CWR "));
02477   }
02478   LWIP_DEBUGF(TCP_DEBUG, ("\n"));
02479 }
02480 
02481 /**
02482  * Print all tcp_pcbs in every list for debugging purposes.
02483  */
02484 void
02485 tcp_debug_print_pcbs(void)
02486 {
02487   struct tcp_pcb *pcb;
02488   struct tcp_pcb_listen *pcbl;
02489 
02490   LWIP_DEBUGF(TCP_DEBUG, ("Active PCB states:\n"));
02491   for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
02492     LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
02493                             pcb->local_port, pcb->remote_port,
02494                             pcb->snd_nxt, pcb->rcv_nxt));
02495     tcp_debug_print_state(pcb->state);
02496   }
02497 
02498   LWIP_DEBUGF(TCP_DEBUG, ("Listen PCB states:\n"));
02499   for (pcbl = tcp_listen_pcbs.listen_pcbs; pcbl != NULL; pcbl = pcbl->next) {
02500     LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F" ", pcbl->local_port));
02501     tcp_debug_print_state(pcbl->state);
02502   }
02503 
02504   LWIP_DEBUGF(TCP_DEBUG, ("TIME-WAIT PCB states:\n"));
02505   for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
02506     LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
02507                             pcb->local_port, pcb->remote_port,
02508                             pcb->snd_nxt, pcb->rcv_nxt));
02509     tcp_debug_print_state(pcb->state);
02510   }
02511 }
02512 
02513 /**
02514  * Check state consistency of the tcp_pcb lists.
02515  */
02516 s16_t
02517 tcp_pcbs_sane(void)
02518 {
02519   struct tcp_pcb *pcb;
02520   for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
02521     LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED);
02522     LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN);
02523     LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
02524   }
02525   for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
02526     LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
02527   }
02528   return 1;
02529 }
02530 #endif /* TCP_DEBUG */
02531 
02532 #if LWIP_TCP_PCB_NUM_EXT_ARGS
02533 /**
02534  * @defgroup tcp_raw_extargs ext arguments
02535  * @ingroup tcp_raw
02536  * Additional data storage per tcp pcb\n
02537  * @see @ref tcp_raw
02538  *
02539  * When LWIP_TCP_PCB_NUM_EXT_ARGS is > 0, every tcp pcb (including listen pcb)
02540  * includes a number of additional argument entries in an array.
02541  *
02542  * To support memory management, in addition to a 'void *', callbacks can be
02543  * provided to manage transition from listening pcbs to connections and to
02544  * deallocate memory when a pcb is deallocated (see struct @ref tcp_ext_arg_callbacks).
02545  *
02546  * After allocating this index, use @ref tcp_ext_arg_set and @ref tcp_ext_arg_get
02547  * to store and load arguments from this index for a given pcb.
02548  */
02549 
02550 static u8_t tcp_ext_arg_id;
02551 
02552 /**
02553  * @ingroup tcp_raw_extargs
02554  * Allocate an index to store data in ext_args member of struct tcp_pcb.
02555  * Returned value is an index in mentioned array.
02556  * The index is *global* over all pcbs!
02557  *
02558  * When @ref LWIP_TCP_PCB_NUM_EXT_ARGS is > 0, every tcp pcb (including listen pcb)
02559  * includes a number of additional argument entries in an array.
02560  *
02561  * To support memory management, in addition to a 'void *', callbacks can be
02562  * provided to manage transition from listening pcbs to connections and to
02563  * deallocate memory when a pcb is deallocated (see struct @ref tcp_ext_arg_callbacks).
02564  *
02565  * After allocating this index, use @ref tcp_ext_arg_set and @ref tcp_ext_arg_get
02566  * to store and load arguments from this index for a given pcb.
02567  *
02568  * @return a unique index into struct tcp_pcb.ext_args
02569  */
02570 u8_t
02571 tcp_ext_arg_alloc_id(void)
02572 {
02573   u8_t result = tcp_ext_arg_id;
02574   tcp_ext_arg_id++;
02575 
02576   LWIP_ASSERT_CORE_LOCKED();
02577 
02578 #if LWIP_TCP_PCB_NUM_EXT_ARGS >= 255
02579 #error LWIP_TCP_PCB_NUM_EXT_ARGS
02580 #endif
02581   LWIP_ASSERT("Increase LWIP_TCP_PCB_NUM_EXT_ARGS in lwipopts.h", result < LWIP_TCP_PCB_NUM_EXT_ARGS);
02582   return result;
02583 }
02584 
02585 /**
02586  * @ingroup tcp_raw_extargs
02587  * Set callbacks for a given index of ext_args on the specified pcb.
02588  *
02589  * @param pcb tcp_pcb for which to set the callback
02590  * @param id ext_args index to set (allocated via @ref tcp_ext_arg_alloc_id)
02591  * @param callbacks callback table (const since it is referenced, not copied!)
02592  */
02593 void
02594 tcp_ext_arg_set_callbacks(struct tcp_pcb *pcb, uint8_t id, const struct tcp_ext_arg_callbacks * const callbacks)
02595 {
02596   LWIP_ASSERT("pcb != NULL", pcb != NULL);
02597   LWIP_ASSERT("id < LWIP_TCP_PCB_NUM_EXT_ARGS", id < LWIP_TCP_PCB_NUM_EXT_ARGS);
02598   LWIP_ASSERT("callbacks != NULL", callbacks != NULL);
02599 
02600   LWIP_ASSERT_CORE_LOCKED();
02601 
02602   pcb->ext_args[id].callbacks = callbacks;
02603 }
02604 
02605 /**
02606  * @ingroup tcp_raw_extargs
02607  * Set data for a given index of ext_args on the specified pcb.
02608  *
02609  * @param pcb tcp_pcb for which to set the data
02610  * @param id ext_args index to set (allocated via @ref tcp_ext_arg_alloc_id)
02611  * @param arg data pointer to set
02612  */
02613 void tcp_ext_arg_set(struct tcp_pcb *pcb, uint8_t id, void *arg)
02614 {
02615   LWIP_ASSERT("pcb != NULL", pcb != NULL);
02616   LWIP_ASSERT("id < LWIP_TCP_PCB_NUM_EXT_ARGS", id < LWIP_TCP_PCB_NUM_EXT_ARGS);
02617 
02618   LWIP_ASSERT_CORE_LOCKED();
02619 
02620   pcb->ext_args[id].data = arg;
02621 }
02622 
02623 /**
02624  * @ingroup tcp_raw_extargs
02625  * Set data for a given index of ext_args on the specified pcb.
02626  *
02627  * @param pcb tcp_pcb for which to set the data
02628  * @param id ext_args index to set (allocated via @ref tcp_ext_arg_alloc_id)
02629  * @return data pointer at the given index
02630  */
02631 void *tcp_ext_arg_get(const struct tcp_pcb *pcb, uint8_t id)
02632 {
02633   LWIP_ASSERT("pcb != NULL", pcb != NULL);
02634   LWIP_ASSERT("id < LWIP_TCP_PCB_NUM_EXT_ARGS", id < LWIP_TCP_PCB_NUM_EXT_ARGS);
02635 
02636   LWIP_ASSERT_CORE_LOCKED();
02637 
02638   return pcb->ext_args[id].data;
02639 }
02640 
02641 /** This function calls the "destroy" callback for all ext_args once a pcb is
02642  * freed.
02643  */
02644 static void
02645 tcp_ext_arg_invoke_callbacks_destroyed(struct tcp_pcb_ext_args *ext_args)
02646 {
02647   int i;
02648   LWIP_ASSERT("ext_args != NULL", ext_args != NULL);
02649 
02650   for (i = 0; i < LWIP_TCP_PCB_NUM_EXT_ARGS; i++) {
02651     if (ext_args[i].callbacks != NULL) {
02652       if (ext_args[i].callbacks->destroy != NULL) {
02653         ext_args[i].callbacks->destroy((u8_t)i, ext_args[i].data);
02654       }
02655     }
02656   }
02657 }
02658 
02659 /** This function calls the "passive_open" callback for all ext_args if a connection
02660  * is in the process of being accepted. This is called just after the SYN is
02661  * received and before a SYN/ACK is sent, to allow to modify the very first
02662  * segment sent even on passive open. Naturally, the "accepted" callback of the
02663  * pcb has not been called yet!
02664  */
02665 err_t
02666 tcp_ext_arg_invoke_callbacks_passive_open(struct tcp_pcb_listen *lpcb, struct tcp_pcb *cpcb)
02667 {
02668   int i;
02669   LWIP_ASSERT("lpcb != NULL", lpcb != NULL);
02670   LWIP_ASSERT("cpcb != NULL", cpcb != NULL);
02671 
02672   for (i = 0; i < LWIP_TCP_PCB_NUM_EXT_ARGS; i++) {
02673     if (lpcb->ext_args[i].callbacks != NULL) {
02674       if (lpcb->ext_args[i].callbacks->passive_open != NULL) {
02675         err_t err = lpcb->ext_args[i].callbacks->passive_open((u8_t)i, lpcb, cpcb);
02676         if (err != ERR_OK) {
02677           return err;
02678         }
02679       }
02680     }
02681   }
02682   return ERR_OK;
02683 }
02684 #endif /* LWIP_TCP_PCB_NUM_EXT_ARGS */
02685 
02686 #endif /* LWIP_TCP */