SAMV71 Xplained Ultra Software Package 1.4

tcp.c

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