EthernetNetIf Compatibility.
Dependents: XBeeWiFi_SPI_example
Fork of NetServicesSource by
lwip/core/tcp.c@5:dd63a1e02b1b, 2010-07-27 (annotated)
- Committer:
- donatien
- Date:
- Tue Jul 27 15:59:42 2010 +0000
- Revision:
- 5:dd63a1e02b1b
- Parent:
- 0:632c9925f013
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:632c9925f013 | 1 | /** |
donatien | 0:632c9925f013 | 2 | * @file |
donatien | 0:632c9925f013 | 3 | * Transmission Control Protocol for IP |
donatien | 0:632c9925f013 | 4 | * |
donatien | 0:632c9925f013 | 5 | * This file contains common functions for the TCP implementation, such as functinos |
donatien | 0:632c9925f013 | 6 | * for manipulating the data structures and the TCP timer functions. TCP functions |
donatien | 0:632c9925f013 | 7 | * related to input and output is found in tcp_in.c and tcp_out.c respectively. |
donatien | 0:632c9925f013 | 8 | * |
donatien | 0:632c9925f013 | 9 | */ |
donatien | 0:632c9925f013 | 10 | |
donatien | 0:632c9925f013 | 11 | /* |
donatien | 0:632c9925f013 | 12 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
donatien | 0:632c9925f013 | 13 | * All rights reserved. |
donatien | 0:632c9925f013 | 14 | * |
donatien | 0:632c9925f013 | 15 | * Redistribution and use in source and binary forms, with or without modification, |
donatien | 0:632c9925f013 | 16 | * are permitted provided that the following conditions are met: |
donatien | 0:632c9925f013 | 17 | * |
donatien | 0:632c9925f013 | 18 | * 1. Redistributions of source code must retain the above copyright notice, |
donatien | 0:632c9925f013 | 19 | * this list of conditions and the following disclaimer. |
donatien | 0:632c9925f013 | 20 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
donatien | 0:632c9925f013 | 21 | * this list of conditions and the following disclaimer in the documentation |
donatien | 0:632c9925f013 | 22 | * and/or other materials provided with the distribution. |
donatien | 0:632c9925f013 | 23 | * 3. The name of the author may not be used to endorse or promote products |
donatien | 0:632c9925f013 | 24 | * derived from this software without specific prior written permission. |
donatien | 0:632c9925f013 | 25 | * |
donatien | 0:632c9925f013 | 26 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
donatien | 0:632c9925f013 | 27 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
donatien | 0:632c9925f013 | 28 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
donatien | 0:632c9925f013 | 29 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
donatien | 0:632c9925f013 | 30 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
donatien | 0:632c9925f013 | 31 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
donatien | 0:632c9925f013 | 32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
donatien | 0:632c9925f013 | 33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
donatien | 0:632c9925f013 | 34 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
donatien | 0:632c9925f013 | 35 | * OF SUCH DAMAGE. |
donatien | 0:632c9925f013 | 36 | * |
donatien | 0:632c9925f013 | 37 | * This file is part of the lwIP TCP/IP stack. |
donatien | 0:632c9925f013 | 38 | * |
donatien | 0:632c9925f013 | 39 | * Author: Adam Dunkels <adam@sics.se> |
donatien | 0:632c9925f013 | 40 | * |
donatien | 0:632c9925f013 | 41 | */ |
donatien | 0:632c9925f013 | 42 | |
donatien | 0:632c9925f013 | 43 | #include "lwip/opt.h" |
donatien | 0:632c9925f013 | 44 | |
donatien | 0:632c9925f013 | 45 | #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */ |
donatien | 0:632c9925f013 | 46 | |
donatien | 0:632c9925f013 | 47 | #include "lwip/def.h" |
donatien | 0:632c9925f013 | 48 | #include "lwip/mem.h" |
donatien | 0:632c9925f013 | 49 | #include "lwip/memp.h" |
donatien | 0:632c9925f013 | 50 | #include "lwip/snmp.h" |
donatien | 0:632c9925f013 | 51 | #include "lwip/tcp.h" |
donatien | 0:632c9925f013 | 52 | #include "lwip/tcp_impl.h" |
donatien | 0:632c9925f013 | 53 | #include "lwip/debug.h" |
donatien | 0:632c9925f013 | 54 | #include "lwip/stats.h" |
donatien | 0:632c9925f013 | 55 | |
donatien | 0:632c9925f013 | 56 | #include <string.h> |
donatien | 0:632c9925f013 | 57 | |
donatien | 5:dd63a1e02b1b | 58 | const char * const tcp_state_str[] = { |
donatien | 0:632c9925f013 | 59 | "CLOSED", |
donatien | 0:632c9925f013 | 60 | "LISTEN", |
donatien | 0:632c9925f013 | 61 | "SYN_SENT", |
donatien | 0:632c9925f013 | 62 | "SYN_RCVD", |
donatien | 0:632c9925f013 | 63 | "ESTABLISHED", |
donatien | 0:632c9925f013 | 64 | "FIN_WAIT_1", |
donatien | 0:632c9925f013 | 65 | "FIN_WAIT_2", |
donatien | 0:632c9925f013 | 66 | "CLOSE_WAIT", |
donatien | 0:632c9925f013 | 67 | "CLOSING", |
donatien | 0:632c9925f013 | 68 | "LAST_ACK", |
donatien | 0:632c9925f013 | 69 | "TIME_WAIT" |
donatien | 0:632c9925f013 | 70 | }; |
donatien | 0:632c9925f013 | 71 | |
donatien | 0:632c9925f013 | 72 | /* Incremented every coarse grained timer shot (typically every 500 ms). */ |
donatien | 0:632c9925f013 | 73 | u32_t tcp_ticks; |
donatien | 0:632c9925f013 | 74 | const u8_t tcp_backoff[13] = |
donatien | 0:632c9925f013 | 75 | { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7}; |
donatien | 0:632c9925f013 | 76 | /* Times per slowtmr hits */ |
donatien | 0:632c9925f013 | 77 | const u8_t tcp_persist_backoff[7] = { 3, 6, 12, 24, 48, 96, 120 }; |
donatien | 0:632c9925f013 | 78 | |
donatien | 0:632c9925f013 | 79 | /* The TCP PCB lists. */ |
donatien | 0:632c9925f013 | 80 | |
donatien | 0:632c9925f013 | 81 | /** List of all TCP PCBs bound but not yet (connected || listening) */ |
donatien | 0:632c9925f013 | 82 | struct tcp_pcb *tcp_bound_pcbs; |
donatien | 0:632c9925f013 | 83 | /** List of all TCP PCBs in LISTEN state */ |
donatien | 0:632c9925f013 | 84 | union tcp_listen_pcbs_t tcp_listen_pcbs; |
donatien | 0:632c9925f013 | 85 | /** List of all TCP PCBs that are in a state in which |
donatien | 0:632c9925f013 | 86 | * they accept or send data. */ |
donatien | 0:632c9925f013 | 87 | struct tcp_pcb *tcp_active_pcbs; |
donatien | 0:632c9925f013 | 88 | /** List of all TCP PCBs in TIME-WAIT state */ |
donatien | 0:632c9925f013 | 89 | struct tcp_pcb *tcp_tw_pcbs; |
donatien | 0:632c9925f013 | 90 | |
donatien | 5:dd63a1e02b1b | 91 | #define NUM_TCP_PCB_LISTS 4 |
donatien | 5:dd63a1e02b1b | 92 | #define NUM_TCP_PCB_LISTS_NO_TIME_WAIT 3 |
donatien | 5:dd63a1e02b1b | 93 | /** An array with all (non-temporary) PCB lists, mainly used for smaller code size */ |
donatien | 5:dd63a1e02b1b | 94 | struct tcp_pcb **tcp_pcb_lists[] = {&tcp_listen_pcbs.pcbs, &tcp_bound_pcbs, |
donatien | 5:dd63a1e02b1b | 95 | &tcp_active_pcbs, &tcp_tw_pcbs}; |
donatien | 5:dd63a1e02b1b | 96 | |
donatien | 0:632c9925f013 | 97 | /** Only used for temporary storage. */ |
donatien | 0:632c9925f013 | 98 | struct tcp_pcb *tcp_tmp_pcb; |
donatien | 0:632c9925f013 | 99 | |
donatien | 0:632c9925f013 | 100 | /** Timer counter to handle calling slow-timer from tcp_tmr() */ |
donatien | 0:632c9925f013 | 101 | static u8_t tcp_timer; |
donatien | 0:632c9925f013 | 102 | static u16_t tcp_new_port(void); |
donatien | 0:632c9925f013 | 103 | |
donatien | 0:632c9925f013 | 104 | /** |
donatien | 0:632c9925f013 | 105 | * Called periodically to dispatch TCP timers. |
donatien | 0:632c9925f013 | 106 | * |
donatien | 0:632c9925f013 | 107 | */ |
donatien | 0:632c9925f013 | 108 | void |
donatien | 0:632c9925f013 | 109 | tcp_tmr(void) |
donatien | 0:632c9925f013 | 110 | { |
donatien | 0:632c9925f013 | 111 | /* Call tcp_fasttmr() every 250 ms */ |
donatien | 0:632c9925f013 | 112 | tcp_fasttmr(); |
donatien | 0:632c9925f013 | 113 | |
donatien | 0:632c9925f013 | 114 | if (++tcp_timer & 1) { |
donatien | 0:632c9925f013 | 115 | /* Call tcp_tmr() every 500 ms, i.e., every other timer |
donatien | 0:632c9925f013 | 116 | tcp_tmr() is called. */ |
donatien | 0:632c9925f013 | 117 | tcp_slowtmr(); |
donatien | 0:632c9925f013 | 118 | } |
donatien | 0:632c9925f013 | 119 | } |
donatien | 0:632c9925f013 | 120 | |
donatien | 0:632c9925f013 | 121 | /** |
donatien | 0:632c9925f013 | 122 | * Closes the TX side of a connection held by the PCB. |
donatien | 0:632c9925f013 | 123 | * For tcp_close(), a RST is sent if the application didn't receive all data |
donatien | 0:632c9925f013 | 124 | * (tcp_recved() not called for all data passed to recv callback). |
donatien | 0:632c9925f013 | 125 | * |
donatien | 0:632c9925f013 | 126 | * Listening pcbs are freed and may not be referenced any more. |
donatien | 0:632c9925f013 | 127 | * Connection pcbs are freed if not yet connected and may not be referenced |
donatien | 0:632c9925f013 | 128 | * any more. If a connection is established (at least SYN received or in |
donatien | 0:632c9925f013 | 129 | * a closing state), the connection is closed, and put in a closing state. |
donatien | 0:632c9925f013 | 130 | * The pcb is then automatically freed in tcp_slowtmr(). It is therefore |
donatien | 0:632c9925f013 | 131 | * unsafe to reference it. |
donatien | 0:632c9925f013 | 132 | * |
donatien | 0:632c9925f013 | 133 | * @param pcb the tcp_pcb to close |
donatien | 0:632c9925f013 | 134 | * @return ERR_OK if connection has been closed |
donatien | 0:632c9925f013 | 135 | * another err_t if closing failed and pcb is not freed |
donatien | 0:632c9925f013 | 136 | */ |
donatien | 0:632c9925f013 | 137 | static err_t |
donatien | 0:632c9925f013 | 138 | tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data) |
donatien | 0:632c9925f013 | 139 | { |
donatien | 0:632c9925f013 | 140 | err_t err; |
donatien | 0:632c9925f013 | 141 | |
donatien | 0:632c9925f013 | 142 | if (rst_on_unacked_data && (pcb->state != LISTEN)) { |
donatien | 5:dd63a1e02b1b | 143 | if ((pcb->refused_data != NULL) || (pcb->rcv_wnd != TCP_WND)) { |
donatien | 0:632c9925f013 | 144 | /* Not all data received by application, send RST to tell the remote |
donatien | 0:632c9925f013 | 145 | side about this. */ |
donatien | 5:dd63a1e02b1b | 146 | LWIP_ASSERT("pcb->flags & TF_RXCLOSED", pcb->flags & TF_RXCLOSED); |
donatien | 5:dd63a1e02b1b | 147 | |
donatien | 5:dd63a1e02b1b | 148 | /* don't call tcp_abort here: we must not deallocate the pcb since |
donatien | 5:dd63a1e02b1b | 149 | that might not be expected when calling tcp_close */ |
donatien | 0:632c9925f013 | 150 | tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip, |
donatien | 0:632c9925f013 | 151 | pcb->local_port, pcb->remote_port); |
donatien | 5:dd63a1e02b1b | 152 | |
donatien | 5:dd63a1e02b1b | 153 | tcp_pcb_purge(pcb); |
donatien | 5:dd63a1e02b1b | 154 | |
donatien | 5:dd63a1e02b1b | 155 | /* TODO: to which state do we move now? */ |
donatien | 5:dd63a1e02b1b | 156 | |
donatien | 5:dd63a1e02b1b | 157 | /* move to TIME_WAIT since we close actively */ |
donatien | 5:dd63a1e02b1b | 158 | TCP_RMV(&tcp_active_pcbs, pcb); |
donatien | 5:dd63a1e02b1b | 159 | pcb->state = TIME_WAIT; |
donatien | 5:dd63a1e02b1b | 160 | TCP_REG(&tcp_tw_pcbs, pcb); |
donatien | 5:dd63a1e02b1b | 161 | |
donatien | 5:dd63a1e02b1b | 162 | return ERR_OK; |
donatien | 0:632c9925f013 | 163 | } |
donatien | 0:632c9925f013 | 164 | } |
donatien | 0:632c9925f013 | 165 | |
donatien | 0:632c9925f013 | 166 | switch (pcb->state) { |
donatien | 0:632c9925f013 | 167 | case CLOSED: |
donatien | 0:632c9925f013 | 168 | /* Closing a pcb in the CLOSED state might seem erroneous, |
donatien | 0:632c9925f013 | 169 | * however, it is in this state once allocated and as yet unused |
donatien | 0:632c9925f013 | 170 | * and the user needs some way to free it should the need arise. |
donatien | 0:632c9925f013 | 171 | * Calling tcp_close() with a pcb that has already been closed, (i.e. twice) |
donatien | 0:632c9925f013 | 172 | * or for a pcb that has been used and then entered the CLOSED state |
donatien | 0:632c9925f013 | 173 | * is erroneous, but this should never happen as the pcb has in those cases |
donatien | 0:632c9925f013 | 174 | * been freed, and so any remaining handles are bogus. */ |
donatien | 0:632c9925f013 | 175 | err = ERR_OK; |
donatien | 0:632c9925f013 | 176 | TCP_RMV(&tcp_bound_pcbs, pcb); |
donatien | 0:632c9925f013 | 177 | memp_free(MEMP_TCP_PCB, pcb); |
donatien | 0:632c9925f013 | 178 | pcb = NULL; |
donatien | 0:632c9925f013 | 179 | break; |
donatien | 0:632c9925f013 | 180 | case LISTEN: |
donatien | 0:632c9925f013 | 181 | err = ERR_OK; |
donatien | 5:dd63a1e02b1b | 182 | tcp_pcb_remove(&tcp_listen_pcbs.pcbs, pcb); |
donatien | 0:632c9925f013 | 183 | memp_free(MEMP_TCP_PCB_LISTEN, pcb); |
donatien | 0:632c9925f013 | 184 | pcb = NULL; |
donatien | 0:632c9925f013 | 185 | break; |
donatien | 0:632c9925f013 | 186 | case SYN_SENT: |
donatien | 0:632c9925f013 | 187 | err = ERR_OK; |
donatien | 0:632c9925f013 | 188 | tcp_pcb_remove(&tcp_active_pcbs, pcb); |
donatien | 0:632c9925f013 | 189 | memp_free(MEMP_TCP_PCB, pcb); |
donatien | 0:632c9925f013 | 190 | pcb = NULL; |
donatien | 0:632c9925f013 | 191 | snmp_inc_tcpattemptfails(); |
donatien | 0:632c9925f013 | 192 | break; |
donatien | 0:632c9925f013 | 193 | case SYN_RCVD: |
donatien | 0:632c9925f013 | 194 | err = tcp_send_fin(pcb); |
donatien | 0:632c9925f013 | 195 | if (err == ERR_OK) { |
donatien | 0:632c9925f013 | 196 | snmp_inc_tcpattemptfails(); |
donatien | 0:632c9925f013 | 197 | pcb->state = FIN_WAIT_1; |
donatien | 0:632c9925f013 | 198 | } |
donatien | 0:632c9925f013 | 199 | break; |
donatien | 0:632c9925f013 | 200 | case ESTABLISHED: |
donatien | 0:632c9925f013 | 201 | err = tcp_send_fin(pcb); |
donatien | 0:632c9925f013 | 202 | if (err == ERR_OK) { |
donatien | 0:632c9925f013 | 203 | snmp_inc_tcpestabresets(); |
donatien | 0:632c9925f013 | 204 | pcb->state = FIN_WAIT_1; |
donatien | 0:632c9925f013 | 205 | } |
donatien | 0:632c9925f013 | 206 | break; |
donatien | 0:632c9925f013 | 207 | case CLOSE_WAIT: |
donatien | 0:632c9925f013 | 208 | err = tcp_send_fin(pcb); |
donatien | 0:632c9925f013 | 209 | if (err == ERR_OK) { |
donatien | 0:632c9925f013 | 210 | snmp_inc_tcpestabresets(); |
donatien | 0:632c9925f013 | 211 | pcb->state = LAST_ACK; |
donatien | 0:632c9925f013 | 212 | } |
donatien | 0:632c9925f013 | 213 | break; |
donatien | 0:632c9925f013 | 214 | default: |
donatien | 0:632c9925f013 | 215 | /* Has already been closed, do nothing. */ |
donatien | 0:632c9925f013 | 216 | err = ERR_OK; |
donatien | 0:632c9925f013 | 217 | pcb = NULL; |
donatien | 0:632c9925f013 | 218 | break; |
donatien | 0:632c9925f013 | 219 | } |
donatien | 0:632c9925f013 | 220 | |
donatien | 0:632c9925f013 | 221 | if (pcb != NULL && err == ERR_OK) { |
donatien | 0:632c9925f013 | 222 | /* To ensure all data has been sent when tcp_close returns, we have |
donatien | 0:632c9925f013 | 223 | to make sure tcp_output doesn't fail. |
donatien | 0:632c9925f013 | 224 | Since we don't really have to ensure all data has been sent when tcp_close |
donatien | 0:632c9925f013 | 225 | returns (unsent data is sent from tcp timer functions, also), we don't care |
donatien | 0:632c9925f013 | 226 | for the return value of tcp_output for now. */ |
donatien | 0:632c9925f013 | 227 | /* @todo: When implementing SO_LINGER, this must be changed somehow: |
donatien | 0:632c9925f013 | 228 | If SOF_LINGER is set, the data should be sent and acked before close returns. |
donatien | 0:632c9925f013 | 229 | This can only be valid for sequential APIs, not for the raw API. */ |
donatien | 0:632c9925f013 | 230 | tcp_output(pcb); |
donatien | 0:632c9925f013 | 231 | } |
donatien | 0:632c9925f013 | 232 | return err; |
donatien | 0:632c9925f013 | 233 | } |
donatien | 0:632c9925f013 | 234 | |
donatien | 0:632c9925f013 | 235 | /** |
donatien | 0:632c9925f013 | 236 | * Closes the connection held by the PCB. |
donatien | 0:632c9925f013 | 237 | * |
donatien | 0:632c9925f013 | 238 | * Listening pcbs are freed and may not be referenced any more. |
donatien | 0:632c9925f013 | 239 | * Connection pcbs are freed if not yet connected and may not be referenced |
donatien | 0:632c9925f013 | 240 | * any more. If a connection is established (at least SYN received or in |
donatien | 0:632c9925f013 | 241 | * a closing state), the connection is closed, and put in a closing state. |
donatien | 0:632c9925f013 | 242 | * The pcb is then automatically freed in tcp_slowtmr(). It is therefore |
donatien | 0:632c9925f013 | 243 | * unsafe to reference it (unless an error is returned). |
donatien | 0:632c9925f013 | 244 | * |
donatien | 0:632c9925f013 | 245 | * @param pcb the tcp_pcb to close |
donatien | 0:632c9925f013 | 246 | * @return ERR_OK if connection has been closed |
donatien | 0:632c9925f013 | 247 | * another err_t if closing failed and pcb is not freed |
donatien | 0:632c9925f013 | 248 | */ |
donatien | 0:632c9925f013 | 249 | err_t |
donatien | 0:632c9925f013 | 250 | tcp_close(struct tcp_pcb *pcb) |
donatien | 0:632c9925f013 | 251 | { |
donatien | 0:632c9925f013 | 252 | #if TCP_DEBUG |
donatien | 0:632c9925f013 | 253 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in ")); |
donatien | 0:632c9925f013 | 254 | tcp_debug_print_state(pcb->state); |
donatien | 0:632c9925f013 | 255 | #endif /* TCP_DEBUG */ |
donatien | 0:632c9925f013 | 256 | |
donatien | 0:632c9925f013 | 257 | if (pcb->state != LISTEN) { |
donatien | 0:632c9925f013 | 258 | /* Set a flag not to receive any more data... */ |
donatien | 0:632c9925f013 | 259 | pcb->flags |= TF_RXCLOSED; |
donatien | 0:632c9925f013 | 260 | } |
donatien | 0:632c9925f013 | 261 | /* ... and close */ |
donatien | 0:632c9925f013 | 262 | return tcp_close_shutdown(pcb, 1); |
donatien | 0:632c9925f013 | 263 | } |
donatien | 0:632c9925f013 | 264 | |
donatien | 0:632c9925f013 | 265 | /** |
donatien | 0:632c9925f013 | 266 | * Causes all or part of a full-duplex connection of this PCB to be shut down. |
donatien | 0:632c9925f013 | 267 | * This doesn't deallocate the PCB! |
donatien | 0:632c9925f013 | 268 | * |
donatien | 0:632c9925f013 | 269 | * @param pcb PCB to shutdown |
donatien | 0:632c9925f013 | 270 | * @param shut_rx shut down receive side if this is != 0 |
donatien | 0:632c9925f013 | 271 | * @param shut_tx shut down send side if this is != 0 |
donatien | 0:632c9925f013 | 272 | * @return ERR_OK if shutdown succeeded (or the PCB has already been shut down) |
donatien | 0:632c9925f013 | 273 | * another err_t on error. |
donatien | 0:632c9925f013 | 274 | */ |
donatien | 0:632c9925f013 | 275 | err_t |
donatien | 0:632c9925f013 | 276 | tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx) |
donatien | 0:632c9925f013 | 277 | { |
donatien | 0:632c9925f013 | 278 | if (pcb->state == LISTEN) { |
donatien | 0:632c9925f013 | 279 | return ERR_CONN; |
donatien | 0:632c9925f013 | 280 | } |
donatien | 0:632c9925f013 | 281 | if (shut_rx) { |
donatien | 0:632c9925f013 | 282 | /* shut down the receive side: free buffered data... */ |
donatien | 0:632c9925f013 | 283 | if (pcb->refused_data != NULL) { |
donatien | 0:632c9925f013 | 284 | pbuf_free(pcb->refused_data); |
donatien | 0:632c9925f013 | 285 | pcb->refused_data = NULL; |
donatien | 0:632c9925f013 | 286 | } |
donatien | 0:632c9925f013 | 287 | /* ... and set a flag not to receive any more data */ |
donatien | 0:632c9925f013 | 288 | pcb->flags |= TF_RXCLOSED; |
donatien | 0:632c9925f013 | 289 | } |
donatien | 0:632c9925f013 | 290 | if (shut_tx) { |
donatien | 0:632c9925f013 | 291 | /* This can't happen twice since if it succeeds, the pcb's state is changed. |
donatien | 0:632c9925f013 | 292 | Only close in these states as the others directly deallocate the PCB */ |
donatien | 0:632c9925f013 | 293 | switch (pcb->state) { |
donatien | 0:632c9925f013 | 294 | case SYN_RCVD: |
donatien | 0:632c9925f013 | 295 | case ESTABLISHED: |
donatien | 0:632c9925f013 | 296 | case CLOSE_WAIT: |
donatien | 0:632c9925f013 | 297 | return tcp_close_shutdown(pcb, 0); |
donatien | 0:632c9925f013 | 298 | default: |
donatien | 0:632c9925f013 | 299 | /* don't shut down other states */ |
donatien | 0:632c9925f013 | 300 | break; |
donatien | 0:632c9925f013 | 301 | } |
donatien | 0:632c9925f013 | 302 | } |
donatien | 0:632c9925f013 | 303 | /* @todo: return another err_t if not in correct state or already shut? */ |
donatien | 0:632c9925f013 | 304 | return ERR_OK; |
donatien | 0:632c9925f013 | 305 | } |
donatien | 0:632c9925f013 | 306 | |
donatien | 0:632c9925f013 | 307 | /** |
donatien | 0:632c9925f013 | 308 | * Abandons a connection and optionally sends a RST to the remote |
donatien | 0:632c9925f013 | 309 | * host. Deletes the local protocol control block. This is done when |
donatien | 0:632c9925f013 | 310 | * a connection is killed because of shortage of memory. |
donatien | 0:632c9925f013 | 311 | * |
donatien | 0:632c9925f013 | 312 | * @param pcb the tcp_pcb to abort |
donatien | 0:632c9925f013 | 313 | * @param reset boolean to indicate whether a reset should be sent |
donatien | 0:632c9925f013 | 314 | */ |
donatien | 0:632c9925f013 | 315 | void |
donatien | 0:632c9925f013 | 316 | tcp_abandon(struct tcp_pcb *pcb, int reset) |
donatien | 0:632c9925f013 | 317 | { |
donatien | 0:632c9925f013 | 318 | u32_t seqno, ackno; |
donatien | 0:632c9925f013 | 319 | u16_t remote_port, local_port; |
donatien | 0:632c9925f013 | 320 | ip_addr_t remote_ip, local_ip; |
donatien | 0:632c9925f013 | 321 | #if LWIP_CALLBACK_API |
donatien | 0:632c9925f013 | 322 | tcp_err_fn errf; |
donatien | 0:632c9925f013 | 323 | #endif /* LWIP_CALLBACK_API */ |
donatien | 0:632c9925f013 | 324 | void *errf_arg; |
donatien | 0:632c9925f013 | 325 | |
donatien | 5:dd63a1e02b1b | 326 | /* pcb->state LISTEN not allowed here */ |
donatien | 5:dd63a1e02b1b | 327 | LWIP_ASSERT("don't call tcp_abort/tcp_abandon for listen-pcbs", |
donatien | 5:dd63a1e02b1b | 328 | pcb->state != LISTEN); |
donatien | 0:632c9925f013 | 329 | /* Figure out on which TCP PCB list we are, and remove us. If we |
donatien | 0:632c9925f013 | 330 | are in an active state, call the receive function associated with |
donatien | 0:632c9925f013 | 331 | the PCB with a NULL argument, and send an RST to the remote end. */ |
donatien | 0:632c9925f013 | 332 | if (pcb->state == TIME_WAIT) { |
donatien | 0:632c9925f013 | 333 | tcp_pcb_remove(&tcp_tw_pcbs, pcb); |
donatien | 0:632c9925f013 | 334 | memp_free(MEMP_TCP_PCB, pcb); |
donatien | 0:632c9925f013 | 335 | } else { |
donatien | 0:632c9925f013 | 336 | seqno = pcb->snd_nxt; |
donatien | 0:632c9925f013 | 337 | ackno = pcb->rcv_nxt; |
donatien | 0:632c9925f013 | 338 | ip_addr_copy(local_ip, pcb->local_ip); |
donatien | 0:632c9925f013 | 339 | ip_addr_copy(remote_ip, pcb->remote_ip); |
donatien | 0:632c9925f013 | 340 | local_port = pcb->local_port; |
donatien | 0:632c9925f013 | 341 | remote_port = pcb->remote_port; |
donatien | 0:632c9925f013 | 342 | #if LWIP_CALLBACK_API |
donatien | 0:632c9925f013 | 343 | errf = pcb->errf; |
donatien | 0:632c9925f013 | 344 | #endif /* LWIP_CALLBACK_API */ |
donatien | 0:632c9925f013 | 345 | errf_arg = pcb->callback_arg; |
donatien | 0:632c9925f013 | 346 | tcp_pcb_remove(&tcp_active_pcbs, pcb); |
donatien | 0:632c9925f013 | 347 | if (pcb->unacked != NULL) { |
donatien | 0:632c9925f013 | 348 | tcp_segs_free(pcb->unacked); |
donatien | 0:632c9925f013 | 349 | } |
donatien | 0:632c9925f013 | 350 | if (pcb->unsent != NULL) { |
donatien | 0:632c9925f013 | 351 | tcp_segs_free(pcb->unsent); |
donatien | 0:632c9925f013 | 352 | } |
donatien | 0:632c9925f013 | 353 | #if TCP_QUEUE_OOSEQ |
donatien | 0:632c9925f013 | 354 | if (pcb->ooseq != NULL) { |
donatien | 0:632c9925f013 | 355 | tcp_segs_free(pcb->ooseq); |
donatien | 0:632c9925f013 | 356 | } |
donatien | 0:632c9925f013 | 357 | #endif /* TCP_QUEUE_OOSEQ */ |
donatien | 0:632c9925f013 | 358 | memp_free(MEMP_TCP_PCB, pcb); |
donatien | 0:632c9925f013 | 359 | TCP_EVENT_ERR(errf, errf_arg, ERR_ABRT); |
donatien | 0:632c9925f013 | 360 | if (reset) { |
donatien | 0:632c9925f013 | 361 | LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abandon: sending RST\n")); |
donatien | 0:632c9925f013 | 362 | tcp_rst(seqno, ackno, &local_ip, &remote_ip, local_port, remote_port); |
donatien | 0:632c9925f013 | 363 | } |
donatien | 0:632c9925f013 | 364 | } |
donatien | 0:632c9925f013 | 365 | } |
donatien | 0:632c9925f013 | 366 | |
donatien | 0:632c9925f013 | 367 | /** |
donatien | 0:632c9925f013 | 368 | * Aborts the connection by sending a RST (reset) segment to the remote |
donatien | 0:632c9925f013 | 369 | * host. The pcb is deallocated. This function never fails. |
donatien | 0:632c9925f013 | 370 | * |
donatien | 0:632c9925f013 | 371 | * ATTENTION: When calling this from one of the TCP callbacks, make |
donatien | 0:632c9925f013 | 372 | * sure you always return ERR_ABRT (and never return ERR_ABRT otherwise |
donatien | 0:632c9925f013 | 373 | * or you will risk accessing deallocated memory or memory leaks! |
donatien | 0:632c9925f013 | 374 | * |
donatien | 0:632c9925f013 | 375 | * @param pcb the tcp pcb to abort |
donatien | 0:632c9925f013 | 376 | */ |
donatien | 0:632c9925f013 | 377 | void |
donatien | 0:632c9925f013 | 378 | tcp_abort(struct tcp_pcb *pcb) |
donatien | 0:632c9925f013 | 379 | { |
donatien | 0:632c9925f013 | 380 | tcp_abandon(pcb, 1); |
donatien | 0:632c9925f013 | 381 | } |
donatien | 0:632c9925f013 | 382 | |
donatien | 0:632c9925f013 | 383 | /** |
donatien | 0:632c9925f013 | 384 | * Binds the connection to a local portnumber and IP address. If the |
donatien | 0:632c9925f013 | 385 | * IP address is not given (i.e., ipaddr == NULL), the IP address of |
donatien | 0:632c9925f013 | 386 | * the outgoing network interface is used instead. |
donatien | 0:632c9925f013 | 387 | * |
donatien | 0:632c9925f013 | 388 | * @param pcb the tcp_pcb to bind (no check is done whether this pcb is |
donatien | 0:632c9925f013 | 389 | * already bound!) |
donatien | 0:632c9925f013 | 390 | * @param ipaddr the local ip address to bind to (use IP_ADDR_ANY to bind |
donatien | 0:632c9925f013 | 391 | * to any local address |
donatien | 0:632c9925f013 | 392 | * @param port the local port to bind to |
donatien | 0:632c9925f013 | 393 | * @return ERR_USE if the port is already in use |
donatien | 0:632c9925f013 | 394 | * ERR_OK if bound |
donatien | 0:632c9925f013 | 395 | */ |
donatien | 0:632c9925f013 | 396 | err_t |
donatien | 0:632c9925f013 | 397 | tcp_bind(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port) |
donatien | 0:632c9925f013 | 398 | { |
donatien | 5:dd63a1e02b1b | 399 | int i; |
donatien | 5:dd63a1e02b1b | 400 | int max_pcb_list = NUM_TCP_PCB_LISTS; |
donatien | 0:632c9925f013 | 401 | struct tcp_pcb *cpcb; |
donatien | 0:632c9925f013 | 402 | |
donatien | 0:632c9925f013 | 403 | LWIP_ERROR("tcp_bind: can only bind in state CLOSED", pcb->state == CLOSED, return ERR_ISCONN); |
donatien | 0:632c9925f013 | 404 | |
donatien | 5:dd63a1e02b1b | 405 | #if SO_REUSE |
donatien | 5:dd63a1e02b1b | 406 | /* Unless the REUSEADDR flag is set, |
donatien | 5:dd63a1e02b1b | 407 | we have to check the pcbs in TIME-WAIT state, also. |
donatien | 5:dd63a1e02b1b | 408 | We do not dump TIME_WAIT pcb's; they can still be matched by incoming |
donatien | 5:dd63a1e02b1b | 409 | packets using both local and remote IP addresses and ports to distinguish. |
donatien | 5:dd63a1e02b1b | 410 | */ |
donatien | 5:dd63a1e02b1b | 411 | #if SO_REUSE |
donatien | 5:dd63a1e02b1b | 412 | if ((pcb->so_options & SOF_REUSEADDR) != 0) { |
donatien | 5:dd63a1e02b1b | 413 | max_pcb_list = NUM_TCP_PCB_LISTS_NO_TIME_WAIT; |
donatien | 5:dd63a1e02b1b | 414 | } |
donatien | 5:dd63a1e02b1b | 415 | #endif /* SO_REUSE */ |
donatien | 5:dd63a1e02b1b | 416 | #endif /* SO_REUSE */ |
donatien | 5:dd63a1e02b1b | 417 | |
donatien | 0:632c9925f013 | 418 | if (port == 0) { |
donatien | 0:632c9925f013 | 419 | port = tcp_new_port(); |
donatien | 0:632c9925f013 | 420 | } |
donatien | 5:dd63a1e02b1b | 421 | |
donatien | 5:dd63a1e02b1b | 422 | /* Check if the address already is in use (on all lists) */ |
donatien | 5:dd63a1e02b1b | 423 | for (i = 0; i < max_pcb_list; i++) { |
donatien | 5:dd63a1e02b1b | 424 | for(cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) { |
donatien | 0:632c9925f013 | 425 | if (cpcb->local_port == port) { |
donatien | 5:dd63a1e02b1b | 426 | #if SO_REUSE |
donatien | 5:dd63a1e02b1b | 427 | /* Omit checking for the same port if both pcbs have REUSEADDR set. |
donatien | 5:dd63a1e02b1b | 428 | For SO_REUSEADDR, the duplicate-check for a 5-tuple is done in |
donatien | 5:dd63a1e02b1b | 429 | tcp_connect. */ |
donatien | 5:dd63a1e02b1b | 430 | if (((pcb->so_options & SOF_REUSEADDR) == 0) || |
donatien | 5:dd63a1e02b1b | 431 | ((cpcb->so_options & SOF_REUSEADDR) == 0)) |
donatien | 5:dd63a1e02b1b | 432 | #endif /* SO_REUSE */ |
donatien | 5:dd63a1e02b1b | 433 | { |
donatien | 5:dd63a1e02b1b | 434 | if (ip_addr_isany(&(cpcb->local_ip)) || |
donatien | 5:dd63a1e02b1b | 435 | ip_addr_isany(ipaddr) || |
donatien | 5:dd63a1e02b1b | 436 | ip_addr_cmp(&(cpcb->local_ip), ipaddr)) { |
donatien | 5:dd63a1e02b1b | 437 | return ERR_USE; |
donatien | 5:dd63a1e02b1b | 438 | } |
donatien | 0:632c9925f013 | 439 | } |
donatien | 0:632c9925f013 | 440 | } |
donatien | 0:632c9925f013 | 441 | } |
donatien | 0:632c9925f013 | 442 | } |
donatien | 0:632c9925f013 | 443 | |
donatien | 0:632c9925f013 | 444 | if (!ip_addr_isany(ipaddr)) { |
donatien | 0:632c9925f013 | 445 | pcb->local_ip = *ipaddr; |
donatien | 0:632c9925f013 | 446 | } |
donatien | 0:632c9925f013 | 447 | pcb->local_port = port; |
donatien | 0:632c9925f013 | 448 | TCP_REG(&tcp_bound_pcbs, pcb); |
donatien | 0:632c9925f013 | 449 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %"U16_F"\n", port)); |
donatien | 0:632c9925f013 | 450 | return ERR_OK; |
donatien | 0:632c9925f013 | 451 | } |
donatien | 0:632c9925f013 | 452 | #if LWIP_CALLBACK_API |
donatien | 0:632c9925f013 | 453 | /** |
donatien | 0:632c9925f013 | 454 | * Default accept callback if no accept callback is specified by the user. |
donatien | 0:632c9925f013 | 455 | */ |
donatien | 0:632c9925f013 | 456 | static err_t |
donatien | 0:632c9925f013 | 457 | tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err) |
donatien | 0:632c9925f013 | 458 | { |
donatien | 0:632c9925f013 | 459 | LWIP_UNUSED_ARG(arg); |
donatien | 0:632c9925f013 | 460 | LWIP_UNUSED_ARG(pcb); |
donatien | 0:632c9925f013 | 461 | LWIP_UNUSED_ARG(err); |
donatien | 0:632c9925f013 | 462 | |
donatien | 0:632c9925f013 | 463 | return ERR_ABRT; |
donatien | 0:632c9925f013 | 464 | } |
donatien | 0:632c9925f013 | 465 | #endif /* LWIP_CALLBACK_API */ |
donatien | 0:632c9925f013 | 466 | |
donatien | 0:632c9925f013 | 467 | /** |
donatien | 0:632c9925f013 | 468 | * Set the state of the connection to be LISTEN, which means that it |
donatien | 0:632c9925f013 | 469 | * is able to accept incoming connections. The protocol control block |
donatien | 0:632c9925f013 | 470 | * is reallocated in order to consume less memory. Setting the |
donatien | 0:632c9925f013 | 471 | * connection to LISTEN is an irreversible process. |
donatien | 0:632c9925f013 | 472 | * |
donatien | 0:632c9925f013 | 473 | * @param pcb the original tcp_pcb |
donatien | 0:632c9925f013 | 474 | * @param backlog the incoming connections queue limit |
donatien | 0:632c9925f013 | 475 | * @return tcp_pcb used for listening, consumes less memory. |
donatien | 0:632c9925f013 | 476 | * |
donatien | 0:632c9925f013 | 477 | * @note The original tcp_pcb is freed. This function therefore has to be |
donatien | 0:632c9925f013 | 478 | * called like this: |
donatien | 0:632c9925f013 | 479 | * tpcb = tcp_listen(tpcb); |
donatien | 0:632c9925f013 | 480 | */ |
donatien | 0:632c9925f013 | 481 | struct tcp_pcb * |
donatien | 0:632c9925f013 | 482 | tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog) |
donatien | 0:632c9925f013 | 483 | { |
donatien | 0:632c9925f013 | 484 | struct tcp_pcb_listen *lpcb; |
donatien | 0:632c9925f013 | 485 | |
donatien | 0:632c9925f013 | 486 | LWIP_UNUSED_ARG(backlog); |
donatien | 0:632c9925f013 | 487 | LWIP_ERROR("tcp_listen: pcb already connected", pcb->state == CLOSED, return NULL); |
donatien | 0:632c9925f013 | 488 | |
donatien | 0:632c9925f013 | 489 | /* already listening? */ |
donatien | 0:632c9925f013 | 490 | if (pcb->state == LISTEN) { |
donatien | 0:632c9925f013 | 491 | return pcb; |
donatien | 0:632c9925f013 | 492 | } |
donatien | 5:dd63a1e02b1b | 493 | #if SO_REUSE |
donatien | 5:dd63a1e02b1b | 494 | if ((pcb->so_options & SOF_REUSEADDR) != 0) { |
donatien | 5:dd63a1e02b1b | 495 | /* Since SOF_REUSEADDR allows reusing a local address before the pcb's usage |
donatien | 5:dd63a1e02b1b | 496 | is declared (listen-/connection-pcb), we have to make sure now that |
donatien | 5:dd63a1e02b1b | 497 | this port is only used once for every local IP. */ |
donatien | 5:dd63a1e02b1b | 498 | for(lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) { |
donatien | 5:dd63a1e02b1b | 499 | if (lpcb->local_port == pcb->local_port) { |
donatien | 5:dd63a1e02b1b | 500 | if (ip_addr_cmp(&lpcb->local_ip, &pcb->local_ip)) { |
donatien | 5:dd63a1e02b1b | 501 | /* this address/port is already used */ |
donatien | 5:dd63a1e02b1b | 502 | return NULL; |
donatien | 5:dd63a1e02b1b | 503 | } |
donatien | 5:dd63a1e02b1b | 504 | } |
donatien | 5:dd63a1e02b1b | 505 | } |
donatien | 5:dd63a1e02b1b | 506 | } |
donatien | 5:dd63a1e02b1b | 507 | #endif /* SO_REUSE */ |
donatien | 0:632c9925f013 | 508 | lpcb = (struct tcp_pcb_listen *)memp_malloc(MEMP_TCP_PCB_LISTEN); |
donatien | 0:632c9925f013 | 509 | if (lpcb == NULL) { |
donatien | 0:632c9925f013 | 510 | return NULL; |
donatien | 0:632c9925f013 | 511 | } |
donatien | 0:632c9925f013 | 512 | lpcb->callback_arg = pcb->callback_arg; |
donatien | 0:632c9925f013 | 513 | lpcb->local_port = pcb->local_port; |
donatien | 0:632c9925f013 | 514 | lpcb->state = LISTEN; |
donatien | 5:dd63a1e02b1b | 515 | lpcb->prio = pcb->prio; |
donatien | 0:632c9925f013 | 516 | lpcb->so_options = pcb->so_options; |
donatien | 0:632c9925f013 | 517 | lpcb->so_options |= SOF_ACCEPTCONN; |
donatien | 0:632c9925f013 | 518 | lpcb->ttl = pcb->ttl; |
donatien | 0:632c9925f013 | 519 | lpcb->tos = pcb->tos; |
donatien | 0:632c9925f013 | 520 | ip_addr_copy(lpcb->local_ip, pcb->local_ip); |
donatien | 0:632c9925f013 | 521 | TCP_RMV(&tcp_bound_pcbs, pcb); |
donatien | 0:632c9925f013 | 522 | memp_free(MEMP_TCP_PCB, pcb); |
donatien | 0:632c9925f013 | 523 | #if LWIP_CALLBACK_API |
donatien | 0:632c9925f013 | 524 | lpcb->accept = tcp_accept_null; |
donatien | 0:632c9925f013 | 525 | #endif /* LWIP_CALLBACK_API */ |
donatien | 0:632c9925f013 | 526 | #if TCP_LISTEN_BACKLOG |
donatien | 0:632c9925f013 | 527 | lpcb->accepts_pending = 0; |
donatien | 0:632c9925f013 | 528 | lpcb->backlog = (backlog ? backlog : 1); |
donatien | 0:632c9925f013 | 529 | #endif /* TCP_LISTEN_BACKLOG */ |
donatien | 5:dd63a1e02b1b | 530 | TCP_REG(&tcp_listen_pcbs.pcbs, (struct tcp_pcb *)lpcb); |
donatien | 0:632c9925f013 | 531 | return (struct tcp_pcb *)lpcb; |
donatien | 0:632c9925f013 | 532 | } |
donatien | 0:632c9925f013 | 533 | |
donatien | 0:632c9925f013 | 534 | /** |
donatien | 0:632c9925f013 | 535 | * Update the state that tracks the available window space to advertise. |
donatien | 0:632c9925f013 | 536 | * |
donatien | 0:632c9925f013 | 537 | * Returns how much extra window would be advertised if we sent an |
donatien | 0:632c9925f013 | 538 | * update now. |
donatien | 0:632c9925f013 | 539 | */ |
donatien | 0:632c9925f013 | 540 | u32_t tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb) |
donatien | 0:632c9925f013 | 541 | { |
donatien | 0:632c9925f013 | 542 | u32_t new_right_edge = pcb->rcv_nxt + pcb->rcv_wnd; |
donatien | 0:632c9925f013 | 543 | |
donatien | 0:632c9925f013 | 544 | if (TCP_SEQ_GEQ(new_right_edge, pcb->rcv_ann_right_edge + LWIP_MIN((TCP_WND / 2), pcb->mss))) { |
donatien | 0:632c9925f013 | 545 | /* we can advertise more window */ |
donatien | 0:632c9925f013 | 546 | pcb->rcv_ann_wnd = pcb->rcv_wnd; |
donatien | 0:632c9925f013 | 547 | return new_right_edge - pcb->rcv_ann_right_edge; |
donatien | 0:632c9925f013 | 548 | } else { |
donatien | 0:632c9925f013 | 549 | if (TCP_SEQ_GT(pcb->rcv_nxt, pcb->rcv_ann_right_edge)) { |
donatien | 0:632c9925f013 | 550 | /* Can happen due to other end sending out of advertised window, |
donatien | 0:632c9925f013 | 551 | * but within actual available (but not yet advertised) window */ |
donatien | 0:632c9925f013 | 552 | pcb->rcv_ann_wnd = 0; |
donatien | 0:632c9925f013 | 553 | } else { |
donatien | 0:632c9925f013 | 554 | /* keep the right edge of window constant */ |
donatien | 0:632c9925f013 | 555 | u32_t new_rcv_ann_wnd = pcb->rcv_ann_right_edge - pcb->rcv_nxt; |
donatien | 0:632c9925f013 | 556 | LWIP_ASSERT("new_rcv_ann_wnd <= 0xffff", new_rcv_ann_wnd <= 0xffff); |
donatien | 0:632c9925f013 | 557 | pcb->rcv_ann_wnd = (u16_t)new_rcv_ann_wnd; |
donatien | 0:632c9925f013 | 558 | } |
donatien | 0:632c9925f013 | 559 | return 0; |
donatien | 0:632c9925f013 | 560 | } |
donatien | 0:632c9925f013 | 561 | } |
donatien | 0:632c9925f013 | 562 | |
donatien | 0:632c9925f013 | 563 | /** |
donatien | 0:632c9925f013 | 564 | * This function should be called by the application when it has |
donatien | 0:632c9925f013 | 565 | * processed the data. The purpose is to advertise a larger window |
donatien | 0:632c9925f013 | 566 | * when the data has been processed. |
donatien | 0:632c9925f013 | 567 | * |
donatien | 0:632c9925f013 | 568 | * @param pcb the tcp_pcb for which data is read |
donatien | 0:632c9925f013 | 569 | * @param len the amount of bytes that have been read by the application |
donatien | 0:632c9925f013 | 570 | */ |
donatien | 0:632c9925f013 | 571 | void |
donatien | 0:632c9925f013 | 572 | tcp_recved(struct tcp_pcb *pcb, u16_t len) |
donatien | 0:632c9925f013 | 573 | { |
donatien | 0:632c9925f013 | 574 | int wnd_inflation; |
donatien | 0:632c9925f013 | 575 | |
donatien | 0:632c9925f013 | 576 | LWIP_ASSERT("tcp_recved: len would wrap rcv_wnd\n", |
donatien | 0:632c9925f013 | 577 | len <= 0xffff - pcb->rcv_wnd ); |
donatien | 0:632c9925f013 | 578 | |
donatien | 0:632c9925f013 | 579 | pcb->rcv_wnd += len; |
donatien | 0:632c9925f013 | 580 | if (pcb->rcv_wnd > TCP_WND) { |
donatien | 0:632c9925f013 | 581 | pcb->rcv_wnd = TCP_WND; |
donatien | 0:632c9925f013 | 582 | } |
donatien | 0:632c9925f013 | 583 | |
donatien | 0:632c9925f013 | 584 | wnd_inflation = tcp_update_rcv_ann_wnd(pcb); |
donatien | 0:632c9925f013 | 585 | |
donatien | 0:632c9925f013 | 586 | /* If the change in the right edge of window is significant (default |
donatien | 0:632c9925f013 | 587 | * watermark is TCP_WND/4), then send an explicit update now. |
donatien | 0:632c9925f013 | 588 | * Otherwise wait for a packet to be sent in the normal course of |
donatien | 0:632c9925f013 | 589 | * events (or more window to be available later) */ |
donatien | 0:632c9925f013 | 590 | if (wnd_inflation >= TCP_WND_UPDATE_THRESHOLD) { |
donatien | 0:632c9925f013 | 591 | tcp_ack_now(pcb); |
donatien | 0:632c9925f013 | 592 | tcp_output(pcb); |
donatien | 0:632c9925f013 | 593 | } |
donatien | 0:632c9925f013 | 594 | |
donatien | 0:632c9925f013 | 595 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: recveived %"U16_F" bytes, wnd %"U16_F" (%"U16_F").\n", |
donatien | 0:632c9925f013 | 596 | len, pcb->rcv_wnd, TCP_WND - pcb->rcv_wnd)); |
donatien | 0:632c9925f013 | 597 | } |
donatien | 0:632c9925f013 | 598 | |
donatien | 0:632c9925f013 | 599 | /** |
donatien | 0:632c9925f013 | 600 | * A nastly hack featuring 'goto' statements that allocates a |
donatien | 0:632c9925f013 | 601 | * new TCP local port. |
donatien | 0:632c9925f013 | 602 | * |
donatien | 0:632c9925f013 | 603 | * @return a new (free) local TCP port number |
donatien | 0:632c9925f013 | 604 | */ |
donatien | 0:632c9925f013 | 605 | static u16_t |
donatien | 0:632c9925f013 | 606 | tcp_new_port(void) |
donatien | 0:632c9925f013 | 607 | { |
donatien | 5:dd63a1e02b1b | 608 | int i; |
donatien | 0:632c9925f013 | 609 | struct tcp_pcb *pcb; |
donatien | 0:632c9925f013 | 610 | #ifndef TCP_LOCAL_PORT_RANGE_START |
donatien | 0:632c9925f013 | 611 | #define TCP_LOCAL_PORT_RANGE_START 4096 |
donatien | 0:632c9925f013 | 612 | #define TCP_LOCAL_PORT_RANGE_END 0x7fff |
donatien | 0:632c9925f013 | 613 | #endif |
donatien | 0:632c9925f013 | 614 | static u16_t port = TCP_LOCAL_PORT_RANGE_START; |
donatien | 0:632c9925f013 | 615 | |
donatien | 0:632c9925f013 | 616 | again: |
donatien | 0:632c9925f013 | 617 | if (++port > TCP_LOCAL_PORT_RANGE_END) { |
donatien | 0:632c9925f013 | 618 | port = TCP_LOCAL_PORT_RANGE_START; |
donatien | 0:632c9925f013 | 619 | } |
donatien | 5:dd63a1e02b1b | 620 | /* Check all PCB lists. */ |
donatien | 5:dd63a1e02b1b | 621 | for (i = 1; i < NUM_TCP_PCB_LISTS; i++) { |
donatien | 5:dd63a1e02b1b | 622 | for(pcb = *tcp_pcb_lists[i]; pcb != NULL; pcb = pcb->next) { |
donatien | 5:dd63a1e02b1b | 623 | if (pcb->local_port == port) { |
donatien | 5:dd63a1e02b1b | 624 | goto again; |
donatien | 5:dd63a1e02b1b | 625 | } |
donatien | 0:632c9925f013 | 626 | } |
donatien | 0:632c9925f013 | 627 | } |
donatien | 0:632c9925f013 | 628 | return port; |
donatien | 0:632c9925f013 | 629 | } |
donatien | 0:632c9925f013 | 630 | |
donatien | 0:632c9925f013 | 631 | /** |
donatien | 0:632c9925f013 | 632 | * Connects to another host. The function given as the "connected" |
donatien | 0:632c9925f013 | 633 | * argument will be called when the connection has been established. |
donatien | 0:632c9925f013 | 634 | * |
donatien | 0:632c9925f013 | 635 | * @param pcb the tcp_pcb used to establish the connection |
donatien | 0:632c9925f013 | 636 | * @param ipaddr the remote ip address to connect to |
donatien | 0:632c9925f013 | 637 | * @param port the remote tcp port to connect to |
donatien | 0:632c9925f013 | 638 | * @param connected callback function to call when connected (or on error) |
donatien | 0:632c9925f013 | 639 | * @return ERR_VAL if invalid arguments are given |
donatien | 0:632c9925f013 | 640 | * ERR_OK if connect request has been sent |
donatien | 0:632c9925f013 | 641 | * other err_t values if connect request couldn't be sent |
donatien | 0:632c9925f013 | 642 | */ |
donatien | 0:632c9925f013 | 643 | err_t |
donatien | 0:632c9925f013 | 644 | tcp_connect(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port, |
donatien | 0:632c9925f013 | 645 | tcp_connected_fn connected) |
donatien | 0:632c9925f013 | 646 | { |
donatien | 0:632c9925f013 | 647 | err_t ret; |
donatien | 0:632c9925f013 | 648 | u32_t iss; |
donatien | 0:632c9925f013 | 649 | |
donatien | 0:632c9925f013 | 650 | LWIP_ERROR("tcp_connect: can only connected from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN); |
donatien | 0:632c9925f013 | 651 | |
donatien | 0:632c9925f013 | 652 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port)); |
donatien | 0:632c9925f013 | 653 | if (ipaddr != NULL) { |
donatien | 0:632c9925f013 | 654 | pcb->remote_ip = *ipaddr; |
donatien | 0:632c9925f013 | 655 | } else { |
donatien | 0:632c9925f013 | 656 | return ERR_VAL; |
donatien | 0:632c9925f013 | 657 | } |
donatien | 0:632c9925f013 | 658 | pcb->remote_port = port; |
donatien | 5:dd63a1e02b1b | 659 | |
donatien | 5:dd63a1e02b1b | 660 | /* check if we have a route to the remote host */ |
donatien | 5:dd63a1e02b1b | 661 | if (ip_addr_isany(&(pcb->local_ip))) { |
donatien | 5:dd63a1e02b1b | 662 | /* no local IP address set, yet. */ |
donatien | 5:dd63a1e02b1b | 663 | struct netif *netif = ip_route(&(pcb->remote_ip)); |
donatien | 5:dd63a1e02b1b | 664 | if (netif == NULL) { |
donatien | 5:dd63a1e02b1b | 665 | /* Don't even try to send a SYN packet if we have no route |
donatien | 5:dd63a1e02b1b | 666 | since that will fail. */ |
donatien | 5:dd63a1e02b1b | 667 | return ERR_RTE; |
donatien | 5:dd63a1e02b1b | 668 | } |
donatien | 5:dd63a1e02b1b | 669 | /* Use the netif's IP address as local address. */ |
donatien | 5:dd63a1e02b1b | 670 | ip_addr_copy(pcb->local_ip, netif->ip_addr); |
donatien | 5:dd63a1e02b1b | 671 | } |
donatien | 5:dd63a1e02b1b | 672 | |
donatien | 0:632c9925f013 | 673 | if (pcb->local_port == 0) { |
donatien | 0:632c9925f013 | 674 | pcb->local_port = tcp_new_port(); |
donatien | 0:632c9925f013 | 675 | } |
donatien | 5:dd63a1e02b1b | 676 | #if SO_REUSE |
donatien | 5:dd63a1e02b1b | 677 | if ((pcb->so_options & SOF_REUSEADDR) != 0) { |
donatien | 5:dd63a1e02b1b | 678 | /* Since SOF_REUSEADDR allows reusing a local address, we have to make sure |
donatien | 5:dd63a1e02b1b | 679 | now that the 5-tuple is unique. */ |
donatien | 5:dd63a1e02b1b | 680 | struct tcp_pcb *cpcb; |
donatien | 5:dd63a1e02b1b | 681 | int i; |
donatien | 5:dd63a1e02b1b | 682 | /* Don't check listen PCBs, check bound-, active- and TIME-WAIT PCBs. */ |
donatien | 5:dd63a1e02b1b | 683 | for (i = 1; i < NUM_TCP_PCB_LISTS; i++) { |
donatien | 5:dd63a1e02b1b | 684 | for(cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) { |
donatien | 5:dd63a1e02b1b | 685 | if ((cpcb->local_port == pcb->local_port) && |
donatien | 5:dd63a1e02b1b | 686 | (cpcb->remote_port == port) && |
donatien | 5:dd63a1e02b1b | 687 | ip_addr_cmp(&cpcb->local_ip, &pcb->local_ip) && |
donatien | 5:dd63a1e02b1b | 688 | ip_addr_cmp(&cpcb->remote_ip, ipaddr)) { |
donatien | 5:dd63a1e02b1b | 689 | /* linux returns EISCONN here, but ERR_USE should be OK for us */ |
donatien | 5:dd63a1e02b1b | 690 | return ERR_USE; |
donatien | 5:dd63a1e02b1b | 691 | } |
donatien | 5:dd63a1e02b1b | 692 | } |
donatien | 5:dd63a1e02b1b | 693 | } |
donatien | 5:dd63a1e02b1b | 694 | } |
donatien | 5:dd63a1e02b1b | 695 | #endif /* SO_REUSE */ |
donatien | 0:632c9925f013 | 696 | iss = tcp_next_iss(); |
donatien | 0:632c9925f013 | 697 | pcb->rcv_nxt = 0; |
donatien | 0:632c9925f013 | 698 | pcb->snd_nxt = iss; |
donatien | 0:632c9925f013 | 699 | pcb->lastack = iss - 1; |
donatien | 0:632c9925f013 | 700 | pcb->snd_lbb = iss - 1; |
donatien | 0:632c9925f013 | 701 | pcb->rcv_wnd = TCP_WND; |
donatien | 0:632c9925f013 | 702 | pcb->rcv_ann_wnd = TCP_WND; |
donatien | 0:632c9925f013 | 703 | pcb->rcv_ann_right_edge = pcb->rcv_nxt; |
donatien | 0:632c9925f013 | 704 | pcb->snd_wnd = TCP_WND; |
donatien | 0:632c9925f013 | 705 | /* As initial send MSS, we use TCP_MSS but limit it to 536. |
donatien | 0:632c9925f013 | 706 | The send MSS is updated when an MSS option is received. */ |
donatien | 0:632c9925f013 | 707 | pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS; |
donatien | 0:632c9925f013 | 708 | #if TCP_CALCULATE_EFF_SEND_MSS |
donatien | 0:632c9925f013 | 709 | pcb->mss = tcp_eff_send_mss(pcb->mss, ipaddr); |
donatien | 0:632c9925f013 | 710 | #endif /* TCP_CALCULATE_EFF_SEND_MSS */ |
donatien | 0:632c9925f013 | 711 | pcb->cwnd = 1; |
donatien | 0:632c9925f013 | 712 | pcb->ssthresh = pcb->mss * 10; |
donatien | 0:632c9925f013 | 713 | #if LWIP_CALLBACK_API |
donatien | 0:632c9925f013 | 714 | pcb->connected = connected; |
donatien | 5:dd63a1e02b1b | 715 | #else /* LWIP_CALLBACK_API */ |
donatien | 5:dd63a1e02b1b | 716 | LWIP_UNUSED_ARG(connected); |
donatien | 0:632c9925f013 | 717 | #endif /* LWIP_CALLBACK_API */ |
donatien | 0:632c9925f013 | 718 | |
donatien | 5:dd63a1e02b1b | 719 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect: tcp_ticks=%"U32_F" iss=%"U32_F"\n", tcp_ticks, iss)); |
donatien | 5:dd63a1e02b1b | 720 | |
donatien | 0:632c9925f013 | 721 | /* Send a SYN together with the MSS option. */ |
donatien | 0:632c9925f013 | 722 | ret = tcp_enqueue_flags(pcb, TCP_SYN); |
donatien | 0:632c9925f013 | 723 | if (ret == ERR_OK) { |
donatien | 0:632c9925f013 | 724 | /* SYN segment was enqueued, changed the pcbs state now */ |
donatien | 0:632c9925f013 | 725 | pcb->state = SYN_SENT; |
donatien | 0:632c9925f013 | 726 | TCP_RMV(&tcp_bound_pcbs, pcb); |
donatien | 0:632c9925f013 | 727 | TCP_REG(&tcp_active_pcbs, pcb); |
donatien | 0:632c9925f013 | 728 | snmp_inc_tcpactiveopens(); |
donatien | 0:632c9925f013 | 729 | |
donatien | 0:632c9925f013 | 730 | tcp_output(pcb); |
donatien | 0:632c9925f013 | 731 | } |
donatien | 0:632c9925f013 | 732 | return ret; |
donatien | 0:632c9925f013 | 733 | } |
donatien | 0:632c9925f013 | 734 | |
donatien | 0:632c9925f013 | 735 | /** |
donatien | 0:632c9925f013 | 736 | * Called every 500 ms and implements the retransmission timer and the timer that |
donatien | 0:632c9925f013 | 737 | * removes PCBs that have been in TIME-WAIT for enough time. It also increments |
donatien | 0:632c9925f013 | 738 | * various timers such as the inactivity timer in each PCB. |
donatien | 0:632c9925f013 | 739 | * |
donatien | 0:632c9925f013 | 740 | * Automatically called from tcp_tmr(). |
donatien | 0:632c9925f013 | 741 | */ |
donatien | 0:632c9925f013 | 742 | void |
donatien | 0:632c9925f013 | 743 | tcp_slowtmr(void) |
donatien | 0:632c9925f013 | 744 | { |
donatien | 0:632c9925f013 | 745 | struct tcp_pcb *pcb, *pcb2, *prev; |
donatien | 0:632c9925f013 | 746 | u16_t eff_wnd; |
donatien | 0:632c9925f013 | 747 | u8_t pcb_remove; /* flag if a PCB should be removed */ |
donatien | 0:632c9925f013 | 748 | u8_t pcb_reset; /* flag if a RST should be sent when removing */ |
donatien | 0:632c9925f013 | 749 | err_t err; |
donatien | 5:dd63a1e02b1b | 750 | |
donatien | 5:dd63a1e02b1b | 751 | tcp_debug_print_pcbs(); //DG |
donatien | 0:632c9925f013 | 752 | |
donatien | 0:632c9925f013 | 753 | err = ERR_OK; |
donatien | 0:632c9925f013 | 754 | |
donatien | 0:632c9925f013 | 755 | ++tcp_ticks; |
donatien | 5:dd63a1e02b1b | 756 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: tcp_ticks=%"U32_F"\n", tcp_ticks)); |
donatien | 0:632c9925f013 | 757 | |
donatien | 0:632c9925f013 | 758 | /* Steps through all of the active PCBs. */ |
donatien | 0:632c9925f013 | 759 | prev = NULL; |
donatien | 0:632c9925f013 | 760 | pcb = tcp_active_pcbs; |
donatien | 0:632c9925f013 | 761 | if (pcb == NULL) { |
donatien | 0:632c9925f013 | 762 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: no active pcbs\n")); |
donatien | 0:632c9925f013 | 763 | } |
donatien | 0:632c9925f013 | 764 | while (pcb != NULL) { |
donatien | 0:632c9925f013 | 765 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: processing active pcb\n")); |
donatien | 0:632c9925f013 | 766 | LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED); |
donatien | 0:632c9925f013 | 767 | LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN); |
donatien | 0:632c9925f013 | 768 | LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT); |
donatien | 0:632c9925f013 | 769 | |
donatien | 0:632c9925f013 | 770 | pcb_remove = 0; |
donatien | 0:632c9925f013 | 771 | pcb_reset = 0; |
donatien | 0:632c9925f013 | 772 | |
donatien | 0:632c9925f013 | 773 | if (pcb->state == SYN_SENT && pcb->nrtx == TCP_SYNMAXRTX) { |
donatien | 0:632c9925f013 | 774 | ++pcb_remove; |
donatien | 0:632c9925f013 | 775 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max SYN retries reached\n")); |
donatien | 0:632c9925f013 | 776 | } |
donatien | 0:632c9925f013 | 777 | else if (pcb->nrtx == TCP_MAXRTX) { |
donatien | 0:632c9925f013 | 778 | ++pcb_remove; |
donatien | 0:632c9925f013 | 779 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max DATA retries reached\n")); |
donatien | 0:632c9925f013 | 780 | } else { |
donatien | 0:632c9925f013 | 781 | if (pcb->persist_backoff > 0) { |
donatien | 0:632c9925f013 | 782 | /* If snd_wnd is zero, use persist timer to send 1 byte probes |
donatien | 0:632c9925f013 | 783 | * instead of using the standard retransmission mechanism. */ |
donatien | 0:632c9925f013 | 784 | pcb->persist_cnt++; |
donatien | 0:632c9925f013 | 785 | if (pcb->persist_cnt >= tcp_persist_backoff[pcb->persist_backoff-1]) { |
donatien | 0:632c9925f013 | 786 | pcb->persist_cnt = 0; |
donatien | 0:632c9925f013 | 787 | if (pcb->persist_backoff < sizeof(tcp_persist_backoff)) { |
donatien | 0:632c9925f013 | 788 | pcb->persist_backoff++; |
donatien | 0:632c9925f013 | 789 | } |
donatien | 0:632c9925f013 | 790 | tcp_zero_window_probe(pcb); |
donatien | 0:632c9925f013 | 791 | } |
donatien | 0:632c9925f013 | 792 | } else { |
donatien | 0:632c9925f013 | 793 | /* Increase the retransmission timer if it is running */ |
donatien | 0:632c9925f013 | 794 | if(pcb->rtime >= 0) |
donatien | 0:632c9925f013 | 795 | ++pcb->rtime; |
donatien | 0:632c9925f013 | 796 | |
donatien | 0:632c9925f013 | 797 | if (pcb->unacked != NULL && pcb->rtime >= pcb->rto) { |
donatien | 0:632c9925f013 | 798 | /* Time for a retransmission. */ |
donatien | 0:632c9925f013 | 799 | LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_slowtmr: rtime %"S16_F |
donatien | 0:632c9925f013 | 800 | " pcb->rto %"S16_F"\n", |
donatien | 0:632c9925f013 | 801 | pcb->rtime, pcb->rto)); |
donatien | 0:632c9925f013 | 802 | |
donatien | 0:632c9925f013 | 803 | /* Double retransmission time-out unless we are trying to |
donatien | 0:632c9925f013 | 804 | * connect to somebody (i.e., we are in SYN_SENT). */ |
donatien | 0:632c9925f013 | 805 | if (pcb->state != SYN_SENT) { |
donatien | 0:632c9925f013 | 806 | pcb->rto = ((pcb->sa >> 3) + pcb->sv) << tcp_backoff[pcb->nrtx]; |
donatien | 0:632c9925f013 | 807 | } |
donatien | 0:632c9925f013 | 808 | |
donatien | 0:632c9925f013 | 809 | /* Reset the retransmission timer. */ |
donatien | 0:632c9925f013 | 810 | pcb->rtime = 0; |
donatien | 0:632c9925f013 | 811 | |
donatien | 0:632c9925f013 | 812 | /* Reduce congestion window and ssthresh. */ |
donatien | 0:632c9925f013 | 813 | eff_wnd = LWIP_MIN(pcb->cwnd, pcb->snd_wnd); |
donatien | 0:632c9925f013 | 814 | pcb->ssthresh = eff_wnd >> 1; |
donatien | 0:632c9925f013 | 815 | if (pcb->ssthresh < (pcb->mss << 1)) { |
donatien | 0:632c9925f013 | 816 | pcb->ssthresh = (pcb->mss << 1); |
donatien | 0:632c9925f013 | 817 | } |
donatien | 0:632c9925f013 | 818 | pcb->cwnd = pcb->mss; |
donatien | 0:632c9925f013 | 819 | LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: cwnd %"U16_F |
donatien | 0:632c9925f013 | 820 | " ssthresh %"U16_F"\n", |
donatien | 0:632c9925f013 | 821 | pcb->cwnd, pcb->ssthresh)); |
donatien | 0:632c9925f013 | 822 | |
donatien | 0:632c9925f013 | 823 | /* The following needs to be called AFTER cwnd is set to one |
donatien | 0:632c9925f013 | 824 | mss - STJ */ |
donatien | 0:632c9925f013 | 825 | tcp_rexmit_rto(pcb); |
donatien | 0:632c9925f013 | 826 | } |
donatien | 0:632c9925f013 | 827 | } |
donatien | 0:632c9925f013 | 828 | } |
donatien | 0:632c9925f013 | 829 | /* Check if this PCB has stayed too long in FIN-WAIT-2 */ |
donatien | 0:632c9925f013 | 830 | if (pcb->state == FIN_WAIT_2) { |
donatien | 0:632c9925f013 | 831 | if ((u32_t)(tcp_ticks - pcb->tmr) > |
donatien | 0:632c9925f013 | 832 | TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) { |
donatien | 0:632c9925f013 | 833 | ++pcb_remove; |
donatien | 0:632c9925f013 | 834 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2\n")); |
donatien | 0:632c9925f013 | 835 | } |
donatien | 0:632c9925f013 | 836 | } |
donatien | 0:632c9925f013 | 837 | |
donatien | 0:632c9925f013 | 838 | /* Check if KEEPALIVE should be sent */ |
donatien | 0:632c9925f013 | 839 | if((pcb->so_options & SOF_KEEPALIVE) && |
donatien | 0:632c9925f013 | 840 | ((pcb->state == ESTABLISHED) || |
donatien | 0:632c9925f013 | 841 | (pcb->state == CLOSE_WAIT))) { |
donatien | 0:632c9925f013 | 842 | #if LWIP_TCP_KEEPALIVE |
donatien | 0:632c9925f013 | 843 | if((u32_t)(tcp_ticks - pcb->tmr) > |
donatien | 0:632c9925f013 | 844 | (pcb->keep_idle + (pcb->keep_cnt*pcb->keep_intvl)) |
donatien | 0:632c9925f013 | 845 | / TCP_SLOW_INTERVAL) |
donatien | 0:632c9925f013 | 846 | #else |
donatien | 0:632c9925f013 | 847 | if((u32_t)(tcp_ticks - pcb->tmr) > |
donatien | 0:632c9925f013 | 848 | (pcb->keep_idle + TCP_MAXIDLE) / TCP_SLOW_INTERVAL) |
donatien | 0:632c9925f013 | 849 | #endif /* LWIP_TCP_KEEPALIVE */ |
donatien | 0:632c9925f013 | 850 | { |
donatien | 0:632c9925f013 | 851 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: KEEPALIVE timeout. Aborting connection to %"U16_F".%"U16_F".%"U16_F".%"U16_F".\n", |
donatien | 0:632c9925f013 | 852 | ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip), |
donatien | 0:632c9925f013 | 853 | ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip))); |
donatien | 0:632c9925f013 | 854 | |
donatien | 0:632c9925f013 | 855 | ++pcb_remove; |
donatien | 0:632c9925f013 | 856 | ++pcb_reset; |
donatien | 0:632c9925f013 | 857 | } |
donatien | 0:632c9925f013 | 858 | #if LWIP_TCP_KEEPALIVE |
donatien | 0:632c9925f013 | 859 | else if((u32_t)(tcp_ticks - pcb->tmr) > |
donatien | 0:632c9925f013 | 860 | (pcb->keep_idle + pcb->keep_cnt_sent * pcb->keep_intvl) |
donatien | 0:632c9925f013 | 861 | / TCP_SLOW_INTERVAL) |
donatien | 0:632c9925f013 | 862 | #else |
donatien | 0:632c9925f013 | 863 | else if((u32_t)(tcp_ticks - pcb->tmr) > |
donatien | 0:632c9925f013 | 864 | (pcb->keep_idle + pcb->keep_cnt_sent * TCP_KEEPINTVL_DEFAULT) |
donatien | 0:632c9925f013 | 865 | / TCP_SLOW_INTERVAL) |
donatien | 0:632c9925f013 | 866 | #endif /* LWIP_TCP_KEEPALIVE */ |
donatien | 0:632c9925f013 | 867 | { |
donatien | 0:632c9925f013 | 868 | tcp_keepalive(pcb); |
donatien | 0:632c9925f013 | 869 | pcb->keep_cnt_sent++; |
donatien | 0:632c9925f013 | 870 | } |
donatien | 0:632c9925f013 | 871 | } |
donatien | 0:632c9925f013 | 872 | |
donatien | 0:632c9925f013 | 873 | /* If this PCB has queued out of sequence data, but has been |
donatien | 0:632c9925f013 | 874 | inactive for too long, will drop the data (it will eventually |
donatien | 0:632c9925f013 | 875 | be retransmitted). */ |
donatien | 0:632c9925f013 | 876 | #if TCP_QUEUE_OOSEQ |
donatien | 0:632c9925f013 | 877 | if (pcb->ooseq != NULL && |
donatien | 0:632c9925f013 | 878 | (u32_t)tcp_ticks - pcb->tmr >= pcb->rto * TCP_OOSEQ_TIMEOUT) { |
donatien | 0:632c9925f013 | 879 | tcp_segs_free(pcb->ooseq); |
donatien | 0:632c9925f013 | 880 | pcb->ooseq = NULL; |
donatien | 0:632c9925f013 | 881 | LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: dropping OOSEQ queued data\n")); |
donatien | 0:632c9925f013 | 882 | } |
donatien | 0:632c9925f013 | 883 | #endif /* TCP_QUEUE_OOSEQ */ |
donatien | 0:632c9925f013 | 884 | |
donatien | 0:632c9925f013 | 885 | /* Check if this PCB has stayed too long in SYN-RCVD */ |
donatien | 0:632c9925f013 | 886 | if (pcb->state == SYN_RCVD) { |
donatien | 0:632c9925f013 | 887 | if ((u32_t)(tcp_ticks - pcb->tmr) > |
donatien | 0:632c9925f013 | 888 | TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) { |
donatien | 0:632c9925f013 | 889 | ++pcb_remove; |
donatien | 0:632c9925f013 | 890 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD\n")); |
donatien | 0:632c9925f013 | 891 | } |
donatien | 0:632c9925f013 | 892 | } |
donatien | 0:632c9925f013 | 893 | |
donatien | 0:632c9925f013 | 894 | /* Check if this PCB has stayed too long in LAST-ACK */ |
donatien | 0:632c9925f013 | 895 | if (pcb->state == LAST_ACK) { |
donatien | 0:632c9925f013 | 896 | if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) { |
donatien | 0:632c9925f013 | 897 | ++pcb_remove; |
donatien | 0:632c9925f013 | 898 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in LAST-ACK\n")); |
donatien | 0:632c9925f013 | 899 | } |
donatien | 0:632c9925f013 | 900 | } |
donatien | 0:632c9925f013 | 901 | |
donatien | 0:632c9925f013 | 902 | /* If the PCB should be removed, do it. */ |
donatien | 0:632c9925f013 | 903 | if (pcb_remove) { |
donatien | 0:632c9925f013 | 904 | tcp_pcb_purge(pcb); |
donatien | 0:632c9925f013 | 905 | /* Remove PCB from tcp_active_pcbs list. */ |
donatien | 0:632c9925f013 | 906 | if (prev != NULL) { |
donatien | 0:632c9925f013 | 907 | LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs); |
donatien | 0:632c9925f013 | 908 | prev->next = pcb->next; |
donatien | 0:632c9925f013 | 909 | } else { |
donatien | 0:632c9925f013 | 910 | /* This PCB was the first. */ |
donatien | 0:632c9925f013 | 911 | LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb); |
donatien | 0:632c9925f013 | 912 | tcp_active_pcbs = pcb->next; |
donatien | 0:632c9925f013 | 913 | } |
donatien | 0:632c9925f013 | 914 | |
donatien | 0:632c9925f013 | 915 | TCP_EVENT_ERR(pcb->errf, pcb->callback_arg, ERR_ABRT); |
donatien | 0:632c9925f013 | 916 | if (pcb_reset) { |
donatien | 0:632c9925f013 | 917 | tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip, |
donatien | 0:632c9925f013 | 918 | pcb->local_port, pcb->remote_port); |
donatien | 0:632c9925f013 | 919 | } |
donatien | 0:632c9925f013 | 920 | |
donatien | 0:632c9925f013 | 921 | pcb2 = pcb->next; |
donatien | 0:632c9925f013 | 922 | memp_free(MEMP_TCP_PCB, pcb); |
donatien | 0:632c9925f013 | 923 | pcb = pcb2; |
donatien | 0:632c9925f013 | 924 | } else { |
donatien | 0:632c9925f013 | 925 | /* get the 'next' element now and work with 'prev' below (in case of abort) */ |
donatien | 0:632c9925f013 | 926 | prev = pcb; |
donatien | 0:632c9925f013 | 927 | pcb = pcb->next; |
donatien | 0:632c9925f013 | 928 | |
donatien | 0:632c9925f013 | 929 | /* We check if we should poll the connection. */ |
donatien | 0:632c9925f013 | 930 | ++prev->polltmr; |
donatien | 0:632c9925f013 | 931 | if (prev->polltmr >= prev->pollinterval) { |
donatien | 0:632c9925f013 | 932 | prev->polltmr = 0; |
donatien | 0:632c9925f013 | 933 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: polling application\n")); |
donatien | 0:632c9925f013 | 934 | TCP_EVENT_POLL(prev, err); |
donatien | 0:632c9925f013 | 935 | /* if err == ERR_ABRT, 'prev' is already deallocated */ |
donatien | 0:632c9925f013 | 936 | if (err == ERR_OK) { |
donatien | 0:632c9925f013 | 937 | tcp_output(prev); |
donatien | 0:632c9925f013 | 938 | } |
donatien | 0:632c9925f013 | 939 | } |
donatien | 0:632c9925f013 | 940 | } |
donatien | 0:632c9925f013 | 941 | } |
donatien | 0:632c9925f013 | 942 | |
donatien | 0:632c9925f013 | 943 | |
donatien | 0:632c9925f013 | 944 | /* Steps through all of the TIME-WAIT PCBs. */ |
donatien | 0:632c9925f013 | 945 | prev = NULL; |
donatien | 0:632c9925f013 | 946 | pcb = tcp_tw_pcbs; |
donatien | 0:632c9925f013 | 947 | while (pcb != NULL) { |
donatien | 0:632c9925f013 | 948 | LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT); |
donatien | 0:632c9925f013 | 949 | pcb_remove = 0; |
donatien | 0:632c9925f013 | 950 | |
donatien | 0:632c9925f013 | 951 | /* Check if this PCB has stayed long enough in TIME-WAIT */ |
donatien | 0:632c9925f013 | 952 | if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) { |
donatien | 0:632c9925f013 | 953 | ++pcb_remove; |
donatien | 0:632c9925f013 | 954 | } |
donatien | 0:632c9925f013 | 955 | |
donatien | 0:632c9925f013 | 956 | |
donatien | 0:632c9925f013 | 957 | |
donatien | 0:632c9925f013 | 958 | /* If the PCB should be removed, do it. */ |
donatien | 0:632c9925f013 | 959 | if (pcb_remove) { |
donatien | 0:632c9925f013 | 960 | tcp_pcb_purge(pcb); |
donatien | 0:632c9925f013 | 961 | /* Remove PCB from tcp_tw_pcbs list. */ |
donatien | 0:632c9925f013 | 962 | if (prev != NULL) { |
donatien | 0:632c9925f013 | 963 | LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs); |
donatien | 0:632c9925f013 | 964 | prev->next = pcb->next; |
donatien | 0:632c9925f013 | 965 | } else { |
donatien | 0:632c9925f013 | 966 | /* This PCB was the first. */ |
donatien | 0:632c9925f013 | 967 | LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb); |
donatien | 0:632c9925f013 | 968 | tcp_tw_pcbs = pcb->next; |
donatien | 0:632c9925f013 | 969 | } |
donatien | 0:632c9925f013 | 970 | pcb2 = pcb->next; |
donatien | 0:632c9925f013 | 971 | memp_free(MEMP_TCP_PCB, pcb); |
donatien | 0:632c9925f013 | 972 | pcb = pcb2; |
donatien | 0:632c9925f013 | 973 | } else { |
donatien | 0:632c9925f013 | 974 | prev = pcb; |
donatien | 0:632c9925f013 | 975 | pcb = pcb->next; |
donatien | 0:632c9925f013 | 976 | } |
donatien | 0:632c9925f013 | 977 | } |
donatien | 0:632c9925f013 | 978 | } |
donatien | 0:632c9925f013 | 979 | |
donatien | 0:632c9925f013 | 980 | /** |
donatien | 0:632c9925f013 | 981 | * Is called every TCP_FAST_INTERVAL (250 ms) and process data previously |
donatien | 0:632c9925f013 | 982 | * "refused" by upper layer (application) and sends delayed ACKs. |
donatien | 0:632c9925f013 | 983 | * |
donatien | 0:632c9925f013 | 984 | * Automatically called from tcp_tmr(). |
donatien | 0:632c9925f013 | 985 | */ |
donatien | 0:632c9925f013 | 986 | void |
donatien | 0:632c9925f013 | 987 | tcp_fasttmr(void) |
donatien | 0:632c9925f013 | 988 | { |
donatien | 0:632c9925f013 | 989 | struct tcp_pcb *pcb = tcp_active_pcbs; |
donatien | 0:632c9925f013 | 990 | |
donatien | 0:632c9925f013 | 991 | while(pcb != NULL) { |
donatien | 0:632c9925f013 | 992 | struct tcp_pcb *next = pcb->next; |
donatien | 0:632c9925f013 | 993 | /* If there is data which was previously "refused" by upper layer */ |
donatien | 0:632c9925f013 | 994 | if (pcb->refused_data != NULL) { |
donatien | 0:632c9925f013 | 995 | /* Notify again application with data previously received. */ |
donatien | 0:632c9925f013 | 996 | err_t err; |
donatien | 0:632c9925f013 | 997 | LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_fasttmr: notify kept packet\n")); |
donatien | 0:632c9925f013 | 998 | TCP_EVENT_RECV(pcb, pcb->refused_data, ERR_OK, err); |
donatien | 0:632c9925f013 | 999 | if (err == ERR_OK) { |
donatien | 0:632c9925f013 | 1000 | pcb->refused_data = NULL; |
donatien | 0:632c9925f013 | 1001 | } else if (err == ERR_ABRT) { |
donatien | 0:632c9925f013 | 1002 | /* if err == ERR_ABRT, 'pcb' is already deallocated */ |
donatien | 0:632c9925f013 | 1003 | pcb = NULL; |
donatien | 0:632c9925f013 | 1004 | } |
donatien | 0:632c9925f013 | 1005 | } |
donatien | 0:632c9925f013 | 1006 | |
donatien | 0:632c9925f013 | 1007 | /* send delayed ACKs */ |
donatien | 0:632c9925f013 | 1008 | if (pcb && (pcb->flags & TF_ACK_DELAY)) { |
donatien | 0:632c9925f013 | 1009 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n")); |
donatien | 0:632c9925f013 | 1010 | tcp_ack_now(pcb); |
donatien | 0:632c9925f013 | 1011 | tcp_output(pcb); |
donatien | 0:632c9925f013 | 1012 | pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW); |
donatien | 0:632c9925f013 | 1013 | } |
donatien | 0:632c9925f013 | 1014 | |
donatien | 0:632c9925f013 | 1015 | pcb = next; |
donatien | 0:632c9925f013 | 1016 | } |
donatien | 0:632c9925f013 | 1017 | } |
donatien | 0:632c9925f013 | 1018 | |
donatien | 0:632c9925f013 | 1019 | /** |
donatien | 0:632c9925f013 | 1020 | * Deallocates a list of TCP segments (tcp_seg structures). |
donatien | 0:632c9925f013 | 1021 | * |
donatien | 0:632c9925f013 | 1022 | * @param seg tcp_seg list of TCP segments to free |
donatien | 0:632c9925f013 | 1023 | */ |
donatien | 0:632c9925f013 | 1024 | void |
donatien | 0:632c9925f013 | 1025 | tcp_segs_free(struct tcp_seg *seg) |
donatien | 0:632c9925f013 | 1026 | { |
donatien | 0:632c9925f013 | 1027 | while (seg != NULL) { |
donatien | 0:632c9925f013 | 1028 | struct tcp_seg *next = seg->next; |
donatien | 0:632c9925f013 | 1029 | tcp_seg_free(seg); |
donatien | 0:632c9925f013 | 1030 | seg = next; |
donatien | 0:632c9925f013 | 1031 | } |
donatien | 0:632c9925f013 | 1032 | } |
donatien | 0:632c9925f013 | 1033 | |
donatien | 0:632c9925f013 | 1034 | /** |
donatien | 0:632c9925f013 | 1035 | * Frees a TCP segment (tcp_seg structure). |
donatien | 0:632c9925f013 | 1036 | * |
donatien | 0:632c9925f013 | 1037 | * @param seg single tcp_seg to free |
donatien | 0:632c9925f013 | 1038 | */ |
donatien | 0:632c9925f013 | 1039 | void |
donatien | 0:632c9925f013 | 1040 | tcp_seg_free(struct tcp_seg *seg) |
donatien | 0:632c9925f013 | 1041 | { |
donatien | 0:632c9925f013 | 1042 | if (seg != NULL) { |
donatien | 0:632c9925f013 | 1043 | if (seg->p != NULL) { |
donatien | 0:632c9925f013 | 1044 | pbuf_free(seg->p); |
donatien | 0:632c9925f013 | 1045 | #if TCP_DEBUG |
donatien | 0:632c9925f013 | 1046 | seg->p = NULL; |
donatien | 0:632c9925f013 | 1047 | #endif /* TCP_DEBUG */ |
donatien | 0:632c9925f013 | 1048 | } |
donatien | 0:632c9925f013 | 1049 | memp_free(MEMP_TCP_SEG, seg); |
donatien | 0:632c9925f013 | 1050 | } |
donatien | 0:632c9925f013 | 1051 | } |
donatien | 0:632c9925f013 | 1052 | |
donatien | 0:632c9925f013 | 1053 | /** |
donatien | 0:632c9925f013 | 1054 | * Sets the priority of a connection. |
donatien | 0:632c9925f013 | 1055 | * |
donatien | 0:632c9925f013 | 1056 | * @param pcb the tcp_pcb to manipulate |
donatien | 0:632c9925f013 | 1057 | * @param prio new priority |
donatien | 0:632c9925f013 | 1058 | */ |
donatien | 0:632c9925f013 | 1059 | void |
donatien | 0:632c9925f013 | 1060 | tcp_setprio(struct tcp_pcb *pcb, u8_t prio) |
donatien | 0:632c9925f013 | 1061 | { |
donatien | 0:632c9925f013 | 1062 | pcb->prio = prio; |
donatien | 0:632c9925f013 | 1063 | } |
donatien | 5:dd63a1e02b1b | 1064 | |
donatien | 0:632c9925f013 | 1065 | #if TCP_QUEUE_OOSEQ |
donatien | 0:632c9925f013 | 1066 | /** |
donatien | 0:632c9925f013 | 1067 | * Returns a copy of the given TCP segment. |
donatien | 0:632c9925f013 | 1068 | * The pbuf and data are not copied, only the pointers |
donatien | 0:632c9925f013 | 1069 | * |
donatien | 0:632c9925f013 | 1070 | * @param seg the old tcp_seg |
donatien | 0:632c9925f013 | 1071 | * @return a copy of seg |
donatien | 0:632c9925f013 | 1072 | */ |
donatien | 0:632c9925f013 | 1073 | struct tcp_seg * |
donatien | 0:632c9925f013 | 1074 | tcp_seg_copy(struct tcp_seg *seg) |
donatien | 0:632c9925f013 | 1075 | { |
donatien | 0:632c9925f013 | 1076 | struct tcp_seg *cseg; |
donatien | 0:632c9925f013 | 1077 | |
donatien | 0:632c9925f013 | 1078 | cseg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG); |
donatien | 0:632c9925f013 | 1079 | if (cseg == NULL) { |
donatien | 0:632c9925f013 | 1080 | return NULL; |
donatien | 0:632c9925f013 | 1081 | } |
donatien | 0:632c9925f013 | 1082 | SMEMCPY((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg)); |
donatien | 0:632c9925f013 | 1083 | pbuf_ref(cseg->p); |
donatien | 0:632c9925f013 | 1084 | return cseg; |
donatien | 0:632c9925f013 | 1085 | } |
donatien | 5:dd63a1e02b1b | 1086 | #endif /* TCP_QUEUE_OOSEQ */ |
donatien | 0:632c9925f013 | 1087 | |
donatien | 0:632c9925f013 | 1088 | #if LWIP_CALLBACK_API |
donatien | 0:632c9925f013 | 1089 | /** |
donatien | 0:632c9925f013 | 1090 | * Default receive callback that is called if the user didn't register |
donatien | 0:632c9925f013 | 1091 | * a recv callback for the pcb. |
donatien | 0:632c9925f013 | 1092 | */ |
donatien | 0:632c9925f013 | 1093 | err_t |
donatien | 0:632c9925f013 | 1094 | tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) |
donatien | 0:632c9925f013 | 1095 | { |
donatien | 0:632c9925f013 | 1096 | LWIP_UNUSED_ARG(arg); |
donatien | 0:632c9925f013 | 1097 | if (p != NULL) { |
donatien | 0:632c9925f013 | 1098 | tcp_recved(pcb, p->tot_len); |
donatien | 0:632c9925f013 | 1099 | pbuf_free(p); |
donatien | 0:632c9925f013 | 1100 | } else if (err == ERR_OK) { |
donatien | 0:632c9925f013 | 1101 | return tcp_close(pcb); |
donatien | 0:632c9925f013 | 1102 | } |
donatien | 0:632c9925f013 | 1103 | return ERR_OK; |
donatien | 0:632c9925f013 | 1104 | } |
donatien | 0:632c9925f013 | 1105 | #endif /* LWIP_CALLBACK_API */ |
donatien | 0:632c9925f013 | 1106 | |
donatien | 0:632c9925f013 | 1107 | /** |
donatien | 0:632c9925f013 | 1108 | * Kills the oldest active connection that has lower priority than prio. |
donatien | 0:632c9925f013 | 1109 | * |
donatien | 0:632c9925f013 | 1110 | * @param prio minimum priority |
donatien | 0:632c9925f013 | 1111 | */ |
donatien | 0:632c9925f013 | 1112 | static void |
donatien | 0:632c9925f013 | 1113 | tcp_kill_prio(u8_t prio) |
donatien | 0:632c9925f013 | 1114 | { |
donatien | 0:632c9925f013 | 1115 | struct tcp_pcb *pcb, *inactive; |
donatien | 0:632c9925f013 | 1116 | u32_t inactivity; |
donatien | 0:632c9925f013 | 1117 | u8_t mprio; |
donatien | 0:632c9925f013 | 1118 | |
donatien | 0:632c9925f013 | 1119 | |
donatien | 0:632c9925f013 | 1120 | mprio = TCP_PRIO_MAX; |
donatien | 0:632c9925f013 | 1121 | |
donatien | 0:632c9925f013 | 1122 | /* We kill the oldest active connection that has lower priority than prio. */ |
donatien | 0:632c9925f013 | 1123 | inactivity = 0; |
donatien | 0:632c9925f013 | 1124 | inactive = NULL; |
donatien | 0:632c9925f013 | 1125 | for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { |
donatien | 0:632c9925f013 | 1126 | if (pcb->prio <= prio && |
donatien | 0:632c9925f013 | 1127 | pcb->prio <= mprio && |
donatien | 0:632c9925f013 | 1128 | (u32_t)(tcp_ticks - pcb->tmr) >= inactivity) { |
donatien | 0:632c9925f013 | 1129 | inactivity = tcp_ticks - pcb->tmr; |
donatien | 0:632c9925f013 | 1130 | inactive = pcb; |
donatien | 0:632c9925f013 | 1131 | mprio = pcb->prio; |
donatien | 0:632c9925f013 | 1132 | } |
donatien | 0:632c9925f013 | 1133 | } |
donatien | 0:632c9925f013 | 1134 | if (inactive != NULL) { |
donatien | 0:632c9925f013 | 1135 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F")\n", |
donatien | 0:632c9925f013 | 1136 | (void *)inactive, inactivity)); |
donatien | 0:632c9925f013 | 1137 | tcp_abort(inactive); |
donatien | 5:dd63a1e02b1b | 1138 | } |
donatien | 0:632c9925f013 | 1139 | } |
donatien | 0:632c9925f013 | 1140 | |
donatien | 0:632c9925f013 | 1141 | /** |
donatien | 0:632c9925f013 | 1142 | * Kills the oldest connection that is in TIME_WAIT state. |
donatien | 0:632c9925f013 | 1143 | * Called from tcp_alloc() if no more connections are available. |
donatien | 0:632c9925f013 | 1144 | */ |
donatien | 0:632c9925f013 | 1145 | static void |
donatien | 0:632c9925f013 | 1146 | tcp_kill_timewait(void) |
donatien | 0:632c9925f013 | 1147 | { |
donatien | 0:632c9925f013 | 1148 | struct tcp_pcb *pcb, *inactive; |
donatien | 0:632c9925f013 | 1149 | u32_t inactivity; |
donatien | 0:632c9925f013 | 1150 | |
donatien | 0:632c9925f013 | 1151 | inactivity = 0; |
donatien | 0:632c9925f013 | 1152 | inactive = NULL; |
donatien | 0:632c9925f013 | 1153 | /* Go through the list of TIME_WAIT pcbs and get the oldest pcb. */ |
donatien | 0:632c9925f013 | 1154 | for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { |
donatien | 0:632c9925f013 | 1155 | if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) { |
donatien | 0:632c9925f013 | 1156 | inactivity = tcp_ticks - pcb->tmr; |
donatien | 0:632c9925f013 | 1157 | inactive = pcb; |
donatien | 0:632c9925f013 | 1158 | } |
donatien | 0:632c9925f013 | 1159 | } |
donatien | 0:632c9925f013 | 1160 | if (inactive != NULL) { |
donatien | 0:632c9925f013 | 1161 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F")\n", |
donatien | 0:632c9925f013 | 1162 | (void *)inactive, inactivity)); |
donatien | 0:632c9925f013 | 1163 | tcp_abort(inactive); |
donatien | 5:dd63a1e02b1b | 1164 | } |
donatien | 0:632c9925f013 | 1165 | } |
donatien | 0:632c9925f013 | 1166 | |
donatien | 0:632c9925f013 | 1167 | /** |
donatien | 0:632c9925f013 | 1168 | * Allocate a new tcp_pcb structure. |
donatien | 0:632c9925f013 | 1169 | * |
donatien | 0:632c9925f013 | 1170 | * @param prio priority for the new pcb |
donatien | 0:632c9925f013 | 1171 | * @return a new tcp_pcb that initially is in state CLOSED |
donatien | 0:632c9925f013 | 1172 | */ |
donatien | 0:632c9925f013 | 1173 | struct tcp_pcb * |
donatien | 0:632c9925f013 | 1174 | tcp_alloc(u8_t prio) |
donatien | 0:632c9925f013 | 1175 | { |
donatien | 0:632c9925f013 | 1176 | struct tcp_pcb *pcb; |
donatien | 0:632c9925f013 | 1177 | u32_t iss; |
donatien | 0:632c9925f013 | 1178 | |
donatien | 0:632c9925f013 | 1179 | pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB); |
donatien | 0:632c9925f013 | 1180 | if (pcb == NULL) { |
donatien | 0:632c9925f013 | 1181 | /* Try killing oldest connection in TIME-WAIT. */ |
donatien | 0:632c9925f013 | 1182 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest TIME-WAIT connection\n")); |
donatien | 0:632c9925f013 | 1183 | tcp_kill_timewait(); |
donatien | 0:632c9925f013 | 1184 | /* Try to allocate a tcp_pcb again. */ |
donatien | 0:632c9925f013 | 1185 | pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB); |
donatien | 0:632c9925f013 | 1186 | if (pcb == NULL) { |
donatien | 0:632c9925f013 | 1187 | /* Try killing active connections with lower priority than the new one. */ |
donatien | 0:632c9925f013 | 1188 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing connection with prio lower than %d\n", prio)); |
donatien | 0:632c9925f013 | 1189 | tcp_kill_prio(prio); |
donatien | 0:632c9925f013 | 1190 | /* Try to allocate a tcp_pcb again. */ |
donatien | 0:632c9925f013 | 1191 | pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB); |
donatien | 0:632c9925f013 | 1192 | if (pcb != NULL) { |
donatien | 0:632c9925f013 | 1193 | /* adjust err stats: memp_malloc failed twice before */ |
donatien | 0:632c9925f013 | 1194 | MEMP_STATS_DEC(err, MEMP_TCP_PCB); |
donatien | 0:632c9925f013 | 1195 | } |
donatien | 0:632c9925f013 | 1196 | } |
donatien | 0:632c9925f013 | 1197 | if (pcb != NULL) { |
donatien | 0:632c9925f013 | 1198 | /* adjust err stats: timewait PCB was freed above */ |
donatien | 0:632c9925f013 | 1199 | MEMP_STATS_DEC(err, MEMP_TCP_PCB); |
donatien | 0:632c9925f013 | 1200 | } |
donatien | 0:632c9925f013 | 1201 | } |
donatien | 0:632c9925f013 | 1202 | if (pcb != NULL) { |
donatien | 0:632c9925f013 | 1203 | memset(pcb, 0, sizeof(struct tcp_pcb)); |
donatien | 0:632c9925f013 | 1204 | pcb->prio = prio; |
donatien | 0:632c9925f013 | 1205 | pcb->snd_buf = TCP_SND_BUF; |
donatien | 0:632c9925f013 | 1206 | pcb->snd_queuelen = 0; |
donatien | 0:632c9925f013 | 1207 | pcb->rcv_wnd = TCP_WND; |
donatien | 0:632c9925f013 | 1208 | pcb->rcv_ann_wnd = TCP_WND; |
donatien | 0:632c9925f013 | 1209 | pcb->tos = 0; |
donatien | 0:632c9925f013 | 1210 | pcb->ttl = TCP_TTL; |
donatien | 0:632c9925f013 | 1211 | /* As initial send MSS, we use TCP_MSS but limit it to 536. |
donatien | 0:632c9925f013 | 1212 | The send MSS is updated when an MSS option is received. */ |
donatien | 0:632c9925f013 | 1213 | pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS; |
donatien | 0:632c9925f013 | 1214 | pcb->rto = 3000 / TCP_SLOW_INTERVAL; |
donatien | 0:632c9925f013 | 1215 | pcb->sa = 0; |
donatien | 0:632c9925f013 | 1216 | pcb->sv = 3000 / TCP_SLOW_INTERVAL; |
donatien | 0:632c9925f013 | 1217 | pcb->rtime = -1; |
donatien | 0:632c9925f013 | 1218 | pcb->cwnd = 1; |
donatien | 0:632c9925f013 | 1219 | iss = tcp_next_iss(); |
donatien | 0:632c9925f013 | 1220 | pcb->snd_wl2 = iss; |
donatien | 0:632c9925f013 | 1221 | pcb->snd_nxt = iss; |
donatien | 0:632c9925f013 | 1222 | pcb->lastack = iss; |
donatien | 0:632c9925f013 | 1223 | pcb->snd_lbb = iss; |
donatien | 0:632c9925f013 | 1224 | pcb->tmr = tcp_ticks; |
donatien | 5:dd63a1e02b1b | 1225 | |
donatien | 5:dd63a1e02b1b | 1226 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: tcp_ticks=%"U32_F" iss=%"U32_F"\n", tcp_ticks, iss)); |
donatien | 0:632c9925f013 | 1227 | |
donatien | 0:632c9925f013 | 1228 | pcb->polltmr = 0; |
donatien | 0:632c9925f013 | 1229 | |
donatien | 0:632c9925f013 | 1230 | #if LWIP_CALLBACK_API |
donatien | 0:632c9925f013 | 1231 | pcb->recv = tcp_recv_null; |
donatien | 0:632c9925f013 | 1232 | #endif /* LWIP_CALLBACK_API */ |
donatien | 0:632c9925f013 | 1233 | |
donatien | 0:632c9925f013 | 1234 | /* Init KEEPALIVE timer */ |
donatien | 0:632c9925f013 | 1235 | pcb->keep_idle = TCP_KEEPIDLE_DEFAULT; |
donatien | 0:632c9925f013 | 1236 | |
donatien | 0:632c9925f013 | 1237 | #if LWIP_TCP_KEEPALIVE |
donatien | 0:632c9925f013 | 1238 | pcb->keep_intvl = TCP_KEEPINTVL_DEFAULT; |
donatien | 0:632c9925f013 | 1239 | pcb->keep_cnt = TCP_KEEPCNT_DEFAULT; |
donatien | 0:632c9925f013 | 1240 | #endif /* LWIP_TCP_KEEPALIVE */ |
donatien | 0:632c9925f013 | 1241 | |
donatien | 0:632c9925f013 | 1242 | pcb->keep_cnt_sent = 0; |
donatien | 0:632c9925f013 | 1243 | } |
donatien | 0:632c9925f013 | 1244 | return pcb; |
donatien | 0:632c9925f013 | 1245 | } |
donatien | 0:632c9925f013 | 1246 | |
donatien | 0:632c9925f013 | 1247 | /** |
donatien | 0:632c9925f013 | 1248 | * Creates a new TCP protocol control block but doesn't place it on |
donatien | 0:632c9925f013 | 1249 | * any of the TCP PCB lists. |
donatien | 0:632c9925f013 | 1250 | * The pcb is not put on any list until binding using tcp_bind(). |
donatien | 0:632c9925f013 | 1251 | * |
donatien | 0:632c9925f013 | 1252 | * @internal: Maybe there should be a idle TCP PCB list where these |
donatien | 0:632c9925f013 | 1253 | * PCBs are put on. Port reservation using tcp_bind() is implemented but |
donatien | 0:632c9925f013 | 1254 | * allocated pcbs that are not bound can't be killed automatically if wanting |
donatien | 0:632c9925f013 | 1255 | * to allocate a pcb with higher prio (@see tcp_kill_prio()) |
donatien | 0:632c9925f013 | 1256 | * |
donatien | 0:632c9925f013 | 1257 | * @return a new tcp_pcb that initially is in state CLOSED |
donatien | 0:632c9925f013 | 1258 | */ |
donatien | 0:632c9925f013 | 1259 | struct tcp_pcb * |
donatien | 0:632c9925f013 | 1260 | tcp_new(void) |
donatien | 0:632c9925f013 | 1261 | { |
donatien | 0:632c9925f013 | 1262 | return tcp_alloc(TCP_PRIO_NORMAL); |
donatien | 0:632c9925f013 | 1263 | } |
donatien | 0:632c9925f013 | 1264 | |
donatien | 0:632c9925f013 | 1265 | /** |
donatien | 0:632c9925f013 | 1266 | * Used to specify the argument that should be passed callback |
donatien | 0:632c9925f013 | 1267 | * functions. |
donatien | 0:632c9925f013 | 1268 | * |
donatien | 0:632c9925f013 | 1269 | * @param pcb tcp_pcb to set the callback argument |
donatien | 0:632c9925f013 | 1270 | * @param arg void pointer argument to pass to callback functions |
donatien | 0:632c9925f013 | 1271 | */ |
donatien | 0:632c9925f013 | 1272 | void |
donatien | 0:632c9925f013 | 1273 | tcp_arg(struct tcp_pcb *pcb, void *arg) |
donatien | 0:632c9925f013 | 1274 | { |
donatien | 0:632c9925f013 | 1275 | pcb->callback_arg = arg; |
donatien | 0:632c9925f013 | 1276 | } |
donatien | 0:632c9925f013 | 1277 | #if LWIP_CALLBACK_API |
donatien | 0:632c9925f013 | 1278 | |
donatien | 0:632c9925f013 | 1279 | /** |
donatien | 0:632c9925f013 | 1280 | * Used to specify the function that should be called when a TCP |
donatien | 0:632c9925f013 | 1281 | * connection receives data. |
donatien | 0:632c9925f013 | 1282 | * |
donatien | 0:632c9925f013 | 1283 | * @param pcb tcp_pcb to set the recv callback |
donatien | 0:632c9925f013 | 1284 | * @param recv callback function to call for this pcb when data is received |
donatien | 0:632c9925f013 | 1285 | */ |
donatien | 0:632c9925f013 | 1286 | void |
donatien | 0:632c9925f013 | 1287 | tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv) |
donatien | 0:632c9925f013 | 1288 | { |
donatien | 0:632c9925f013 | 1289 | pcb->recv = recv; |
donatien | 0:632c9925f013 | 1290 | } |
donatien | 0:632c9925f013 | 1291 | |
donatien | 0:632c9925f013 | 1292 | /** |
donatien | 0:632c9925f013 | 1293 | * Used to specify the function that should be called when TCP data |
donatien | 0:632c9925f013 | 1294 | * has been successfully delivered to the remote host. |
donatien | 0:632c9925f013 | 1295 | * |
donatien | 0:632c9925f013 | 1296 | * @param pcb tcp_pcb to set the sent callback |
donatien | 0:632c9925f013 | 1297 | * @param sent callback function to call for this pcb when data is successfully sent |
donatien | 0:632c9925f013 | 1298 | */ |
donatien | 0:632c9925f013 | 1299 | void |
donatien | 0:632c9925f013 | 1300 | tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent) |
donatien | 0:632c9925f013 | 1301 | { |
donatien | 0:632c9925f013 | 1302 | pcb->sent = sent; |
donatien | 0:632c9925f013 | 1303 | } |
donatien | 0:632c9925f013 | 1304 | |
donatien | 0:632c9925f013 | 1305 | /** |
donatien | 0:632c9925f013 | 1306 | * Used to specify the function that should be called when a fatal error |
donatien | 0:632c9925f013 | 1307 | * has occured on the connection. |
donatien | 0:632c9925f013 | 1308 | * |
donatien | 0:632c9925f013 | 1309 | * @param pcb tcp_pcb to set the err callback |
donatien | 0:632c9925f013 | 1310 | * @param err callback function to call for this pcb when a fatal error |
donatien | 0:632c9925f013 | 1311 | * has occured on the connection |
donatien | 0:632c9925f013 | 1312 | */ |
donatien | 0:632c9925f013 | 1313 | void |
donatien | 0:632c9925f013 | 1314 | tcp_err(struct tcp_pcb *pcb, tcp_err_fn err) |
donatien | 0:632c9925f013 | 1315 | { |
donatien | 0:632c9925f013 | 1316 | pcb->errf = err; |
donatien | 0:632c9925f013 | 1317 | } |
donatien | 0:632c9925f013 | 1318 | |
donatien | 0:632c9925f013 | 1319 | /** |
donatien | 0:632c9925f013 | 1320 | * Used for specifying the function that should be called when a |
donatien | 0:632c9925f013 | 1321 | * LISTENing connection has been connected to another host. |
donatien | 0:632c9925f013 | 1322 | * |
donatien | 0:632c9925f013 | 1323 | * @param pcb tcp_pcb to set the accept callback |
donatien | 0:632c9925f013 | 1324 | * @param accept callback function to call for this pcb when LISTENing |
donatien | 0:632c9925f013 | 1325 | * connection has been connected to another host |
donatien | 0:632c9925f013 | 1326 | */ |
donatien | 0:632c9925f013 | 1327 | void |
donatien | 0:632c9925f013 | 1328 | tcp_accept(struct tcp_pcb *pcb, tcp_accept_fn accept) |
donatien | 0:632c9925f013 | 1329 | { |
donatien | 0:632c9925f013 | 1330 | pcb->accept = accept; |
donatien | 0:632c9925f013 | 1331 | } |
donatien | 0:632c9925f013 | 1332 | #endif /* LWIP_CALLBACK_API */ |
donatien | 0:632c9925f013 | 1333 | |
donatien | 0:632c9925f013 | 1334 | |
donatien | 0:632c9925f013 | 1335 | /** |
donatien | 0:632c9925f013 | 1336 | * Used to specify the function that should be called periodically |
donatien | 0:632c9925f013 | 1337 | * from TCP. The interval is specified in terms of the TCP coarse |
donatien | 0:632c9925f013 | 1338 | * timer interval, which is called twice a second. |
donatien | 0:632c9925f013 | 1339 | * |
donatien | 0:632c9925f013 | 1340 | */ |
donatien | 0:632c9925f013 | 1341 | void |
donatien | 0:632c9925f013 | 1342 | tcp_poll(struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval) |
donatien | 0:632c9925f013 | 1343 | { |
donatien | 0:632c9925f013 | 1344 | #if LWIP_CALLBACK_API |
donatien | 0:632c9925f013 | 1345 | pcb->poll = poll; |
donatien | 5:dd63a1e02b1b | 1346 | #else /* LWIP_CALLBACK_API */ |
donatien | 5:dd63a1e02b1b | 1347 | LWIP_UNUSED_ARG(poll); |
donatien | 0:632c9925f013 | 1348 | #endif /* LWIP_CALLBACK_API */ |
donatien | 0:632c9925f013 | 1349 | pcb->pollinterval = interval; |
donatien | 0:632c9925f013 | 1350 | } |
donatien | 0:632c9925f013 | 1351 | |
donatien | 0:632c9925f013 | 1352 | /** |
donatien | 0:632c9925f013 | 1353 | * Purges a TCP PCB. Removes any buffered data and frees the buffer memory |
donatien | 0:632c9925f013 | 1354 | * (pcb->ooseq, pcb->unsent and pcb->unacked are freed). |
donatien | 0:632c9925f013 | 1355 | * |
donatien | 0:632c9925f013 | 1356 | * @param pcb tcp_pcb to purge. The pcb itself is not deallocated! |
donatien | 0:632c9925f013 | 1357 | */ |
donatien | 0:632c9925f013 | 1358 | void |
donatien | 0:632c9925f013 | 1359 | tcp_pcb_purge(struct tcp_pcb *pcb) |
donatien | 0:632c9925f013 | 1360 | { |
donatien | 0:632c9925f013 | 1361 | if (pcb->state != CLOSED && |
donatien | 0:632c9925f013 | 1362 | pcb->state != TIME_WAIT && |
donatien | 0:632c9925f013 | 1363 | pcb->state != LISTEN) { |
donatien | 0:632c9925f013 | 1364 | |
donatien | 0:632c9925f013 | 1365 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge\n")); |
donatien | 0:632c9925f013 | 1366 | |
donatien | 0:632c9925f013 | 1367 | #if TCP_LISTEN_BACKLOG |
donatien | 0:632c9925f013 | 1368 | if (pcb->state == SYN_RCVD) { |
donatien | 0:632c9925f013 | 1369 | /* Need to find the corresponding listen_pcb and decrease its accepts_pending */ |
donatien | 0:632c9925f013 | 1370 | struct tcp_pcb_listen *lpcb; |
donatien | 0:632c9925f013 | 1371 | LWIP_ASSERT("tcp_pcb_purge: pcb->state == SYN_RCVD but tcp_listen_pcbs is NULL", |
donatien | 0:632c9925f013 | 1372 | tcp_listen_pcbs.listen_pcbs != NULL); |
donatien | 0:632c9925f013 | 1373 | for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) { |
donatien | 0:632c9925f013 | 1374 | if ((lpcb->local_port == pcb->local_port) && |
donatien | 0:632c9925f013 | 1375 | (ip_addr_isany(&lpcb->local_ip) || |
donatien | 0:632c9925f013 | 1376 | ip_addr_cmp(&pcb->local_ip, &lpcb->local_ip))) { |
donatien | 0:632c9925f013 | 1377 | /* port and address of the listen pcb match the timed-out pcb */ |
donatien | 0:632c9925f013 | 1378 | LWIP_ASSERT("tcp_pcb_purge: listen pcb does not have accepts pending", |
donatien | 0:632c9925f013 | 1379 | lpcb->accepts_pending > 0); |
donatien | 0:632c9925f013 | 1380 | lpcb->accepts_pending--; |
donatien | 0:632c9925f013 | 1381 | break; |
donatien | 0:632c9925f013 | 1382 | } |
donatien | 0:632c9925f013 | 1383 | } |
donatien | 0:632c9925f013 | 1384 | } |
donatien | 0:632c9925f013 | 1385 | #endif /* TCP_LISTEN_BACKLOG */ |
donatien | 0:632c9925f013 | 1386 | |
donatien | 0:632c9925f013 | 1387 | |
donatien | 0:632c9925f013 | 1388 | if (pcb->refused_data != NULL) { |
donatien | 0:632c9925f013 | 1389 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->refused_data\n")); |
donatien | 0:632c9925f013 | 1390 | pbuf_free(pcb->refused_data); |
donatien | 0:632c9925f013 | 1391 | pcb->refused_data = NULL; |
donatien | 0:632c9925f013 | 1392 | } |
donatien | 0:632c9925f013 | 1393 | if (pcb->unsent != NULL) { |
donatien | 0:632c9925f013 | 1394 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: not all data sent\n")); |
donatien | 0:632c9925f013 | 1395 | } |
donatien | 0:632c9925f013 | 1396 | if (pcb->unacked != NULL) { |
donatien | 0:632c9925f013 | 1397 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->unacked\n")); |
donatien | 0:632c9925f013 | 1398 | } |
donatien | 5:dd63a1e02b1b | 1399 | #if TCP_QUEUE_OOSEQ |
donatien | 0:632c9925f013 | 1400 | if (pcb->ooseq != NULL) { |
donatien | 0:632c9925f013 | 1401 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->ooseq\n")); |
donatien | 0:632c9925f013 | 1402 | } |
donatien | 0:632c9925f013 | 1403 | tcp_segs_free(pcb->ooseq); |
donatien | 0:632c9925f013 | 1404 | pcb->ooseq = NULL; |
donatien | 0:632c9925f013 | 1405 | #endif /* TCP_QUEUE_OOSEQ */ |
donatien | 0:632c9925f013 | 1406 | |
donatien | 0:632c9925f013 | 1407 | /* Stop the retransmission timer as it will expect data on unacked |
donatien | 0:632c9925f013 | 1408 | queue if it fires */ |
donatien | 0:632c9925f013 | 1409 | pcb->rtime = -1; |
donatien | 0:632c9925f013 | 1410 | |
donatien | 0:632c9925f013 | 1411 | tcp_segs_free(pcb->unsent); |
donatien | 0:632c9925f013 | 1412 | tcp_segs_free(pcb->unacked); |
donatien | 0:632c9925f013 | 1413 | pcb->unacked = pcb->unsent = NULL; |
donatien | 0:632c9925f013 | 1414 | #if TCP_OVERSIZE |
donatien | 0:632c9925f013 | 1415 | pcb->unsent_oversize = 0; |
donatien | 0:632c9925f013 | 1416 | #endif /* TCP_OVERSIZE */ |
donatien | 0:632c9925f013 | 1417 | } |
donatien | 0:632c9925f013 | 1418 | } |
donatien | 0:632c9925f013 | 1419 | |
donatien | 0:632c9925f013 | 1420 | /** |
donatien | 0:632c9925f013 | 1421 | * Purges the PCB and removes it from a PCB list. Any delayed ACKs are sent first. |
donatien | 0:632c9925f013 | 1422 | * |
donatien | 0:632c9925f013 | 1423 | * @param pcblist PCB list to purge. |
donatien | 0:632c9925f013 | 1424 | * @param pcb tcp_pcb to purge. The pcb itself is NOT deallocated! |
donatien | 0:632c9925f013 | 1425 | */ |
donatien | 0:632c9925f013 | 1426 | void |
donatien | 0:632c9925f013 | 1427 | tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb) |
donatien | 0:632c9925f013 | 1428 | { |
donatien | 0:632c9925f013 | 1429 | TCP_RMV(pcblist, pcb); |
donatien | 0:632c9925f013 | 1430 | |
donatien | 0:632c9925f013 | 1431 | tcp_pcb_purge(pcb); |
donatien | 0:632c9925f013 | 1432 | |
donatien | 0:632c9925f013 | 1433 | /* if there is an outstanding delayed ACKs, send it */ |
donatien | 0:632c9925f013 | 1434 | if (pcb->state != TIME_WAIT && |
donatien | 0:632c9925f013 | 1435 | pcb->state != LISTEN && |
donatien | 0:632c9925f013 | 1436 | pcb->flags & TF_ACK_DELAY) { |
donatien | 0:632c9925f013 | 1437 | pcb->flags |= TF_ACK_NOW; |
donatien | 0:632c9925f013 | 1438 | tcp_output(pcb); |
donatien | 0:632c9925f013 | 1439 | } |
donatien | 0:632c9925f013 | 1440 | |
donatien | 0:632c9925f013 | 1441 | if (pcb->state != LISTEN) { |
donatien | 0:632c9925f013 | 1442 | LWIP_ASSERT("unsent segments leaking", pcb->unsent == NULL); |
donatien | 0:632c9925f013 | 1443 | LWIP_ASSERT("unacked segments leaking", pcb->unacked == NULL); |
donatien | 0:632c9925f013 | 1444 | #if TCP_QUEUE_OOSEQ |
donatien | 0:632c9925f013 | 1445 | LWIP_ASSERT("ooseq segments leaking", pcb->ooseq == NULL); |
donatien | 0:632c9925f013 | 1446 | #endif /* TCP_QUEUE_OOSEQ */ |
donatien | 0:632c9925f013 | 1447 | } |
donatien | 0:632c9925f013 | 1448 | |
donatien | 0:632c9925f013 | 1449 | pcb->state = CLOSED; |
donatien | 0:632c9925f013 | 1450 | |
donatien | 0:632c9925f013 | 1451 | LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane()); |
donatien | 0:632c9925f013 | 1452 | } |
donatien | 0:632c9925f013 | 1453 | |
donatien | 0:632c9925f013 | 1454 | /** |
donatien | 0:632c9925f013 | 1455 | * Calculates a new initial sequence number for new connections. |
donatien | 0:632c9925f013 | 1456 | * |
donatien | 0:632c9925f013 | 1457 | * @return u32_t pseudo random sequence number |
donatien | 0:632c9925f013 | 1458 | */ |
donatien | 0:632c9925f013 | 1459 | u32_t |
donatien | 0:632c9925f013 | 1460 | tcp_next_iss(void) |
donatien | 0:632c9925f013 | 1461 | { |
donatien | 0:632c9925f013 | 1462 | static u32_t iss = 6510; |
donatien | 0:632c9925f013 | 1463 | |
donatien | 0:632c9925f013 | 1464 | iss += tcp_ticks; /* XXX */ |
donatien | 0:632c9925f013 | 1465 | return iss; |
donatien | 0:632c9925f013 | 1466 | } |
donatien | 0:632c9925f013 | 1467 | |
donatien | 0:632c9925f013 | 1468 | #if TCP_CALCULATE_EFF_SEND_MSS |
donatien | 0:632c9925f013 | 1469 | /** |
donatien | 0:632c9925f013 | 1470 | * Calcluates the effective send mss that can be used for a specific IP address |
donatien | 0:632c9925f013 | 1471 | * by using ip_route to determin the netif used to send to the address and |
donatien | 0:632c9925f013 | 1472 | * calculating the minimum of TCP_MSS and that netif's mtu (if set). |
donatien | 0:632c9925f013 | 1473 | */ |
donatien | 0:632c9925f013 | 1474 | u16_t |
donatien | 0:632c9925f013 | 1475 | tcp_eff_send_mss(u16_t sendmss, ip_addr_t *addr) |
donatien | 0:632c9925f013 | 1476 | { |
donatien | 0:632c9925f013 | 1477 | u16_t mss_s; |
donatien | 0:632c9925f013 | 1478 | struct netif *outif; |
donatien | 0:632c9925f013 | 1479 | |
donatien | 0:632c9925f013 | 1480 | outif = ip_route(addr); |
donatien | 0:632c9925f013 | 1481 | if ((outif != NULL) && (outif->mtu != 0)) { |
donatien | 0:632c9925f013 | 1482 | mss_s = outif->mtu - IP_HLEN - TCP_HLEN; |
donatien | 0:632c9925f013 | 1483 | /* RFC 1122, chap 4.2.2.6: |
donatien | 0:632c9925f013 | 1484 | * Eff.snd.MSS = min(SendMSS+20, MMS_S) - TCPhdrsize - IPoptionsize |
donatien | 0:632c9925f013 | 1485 | * We correct for TCP options in tcp_write(), and don't support IP options. |
donatien | 0:632c9925f013 | 1486 | */ |
donatien | 0:632c9925f013 | 1487 | sendmss = LWIP_MIN(sendmss, mss_s); |
donatien | 0:632c9925f013 | 1488 | } |
donatien | 0:632c9925f013 | 1489 | return sendmss; |
donatien | 0:632c9925f013 | 1490 | } |
donatien | 0:632c9925f013 | 1491 | #endif /* TCP_CALCULATE_EFF_SEND_MSS */ |
donatien | 0:632c9925f013 | 1492 | |
donatien | 0:632c9925f013 | 1493 | const char* |
donatien | 0:632c9925f013 | 1494 | tcp_debug_state_str(enum tcp_state s) |
donatien | 0:632c9925f013 | 1495 | { |
donatien | 0:632c9925f013 | 1496 | return tcp_state_str[s]; |
donatien | 0:632c9925f013 | 1497 | } |
donatien | 0:632c9925f013 | 1498 | |
donatien | 0:632c9925f013 | 1499 | #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG |
donatien | 0:632c9925f013 | 1500 | /** |
donatien | 0:632c9925f013 | 1501 | * Print a tcp header for debugging purposes. |
donatien | 0:632c9925f013 | 1502 | * |
donatien | 0:632c9925f013 | 1503 | * @param tcphdr pointer to a struct tcp_hdr |
donatien | 0:632c9925f013 | 1504 | */ |
donatien | 0:632c9925f013 | 1505 | void |
donatien | 0:632c9925f013 | 1506 | tcp_debug_print(struct tcp_hdr *tcphdr) |
donatien | 0:632c9925f013 | 1507 | { |
donatien | 0:632c9925f013 | 1508 | LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n")); |
donatien | 0:632c9925f013 | 1509 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); |
donatien | 0:632c9925f013 | 1510 | LWIP_DEBUGF(TCP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n", |
donatien | 0:632c9925f013 | 1511 | ntohs(tcphdr->src), ntohs(tcphdr->dest))); |
donatien | 0:632c9925f013 | 1512 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); |
donatien | 0:632c9925f013 | 1513 | LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (seq no)\n", |
donatien | 0:632c9925f013 | 1514 | ntohl(tcphdr->seqno))); |
donatien | 0:632c9925f013 | 1515 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); |
donatien | 0:632c9925f013 | 1516 | LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (ack no)\n", |
donatien | 0:632c9925f013 | 1517 | ntohl(tcphdr->ackno))); |
donatien | 0:632c9925f013 | 1518 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); |
donatien | 0:632c9925f013 | 1519 | LWIP_DEBUGF(TCP_DEBUG, ("| %2"U16_F" | |%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"| %5"U16_F" | (hdrlen, flags (", |
donatien | 0:632c9925f013 | 1520 | TCPH_HDRLEN(tcphdr), |
donatien | 0:632c9925f013 | 1521 | TCPH_FLAGS(tcphdr) >> 5 & 1, |
donatien | 0:632c9925f013 | 1522 | TCPH_FLAGS(tcphdr) >> 4 & 1, |
donatien | 0:632c9925f013 | 1523 | TCPH_FLAGS(tcphdr) >> 3 & 1, |
donatien | 0:632c9925f013 | 1524 | TCPH_FLAGS(tcphdr) >> 2 & 1, |
donatien | 0:632c9925f013 | 1525 | TCPH_FLAGS(tcphdr) >> 1 & 1, |
donatien | 0:632c9925f013 | 1526 | TCPH_FLAGS(tcphdr) & 1, |
donatien | 0:632c9925f013 | 1527 | ntohs(tcphdr->wnd))); |
donatien | 0:632c9925f013 | 1528 | tcp_debug_print_flags(TCPH_FLAGS(tcphdr)); |
donatien | 0:632c9925f013 | 1529 | LWIP_DEBUGF(TCP_DEBUG, ("), win)\n")); |
donatien | 0:632c9925f013 | 1530 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); |
donatien | 0:632c9925f013 | 1531 | LWIP_DEBUGF(TCP_DEBUG, ("| 0x%04"X16_F" | %5"U16_F" | (chksum, urgp)\n", |
donatien | 0:632c9925f013 | 1532 | ntohs(tcphdr->chksum), ntohs(tcphdr->urgp))); |
donatien | 0:632c9925f013 | 1533 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); |
donatien | 0:632c9925f013 | 1534 | } |
donatien | 0:632c9925f013 | 1535 | |
donatien | 0:632c9925f013 | 1536 | /** |
donatien | 0:632c9925f013 | 1537 | * Print a tcp state for debugging purposes. |
donatien | 0:632c9925f013 | 1538 | * |
donatien | 0:632c9925f013 | 1539 | * @param s enum tcp_state to print |
donatien | 0:632c9925f013 | 1540 | */ |
donatien | 0:632c9925f013 | 1541 | void |
donatien | 0:632c9925f013 | 1542 | tcp_debug_print_state(enum tcp_state s) |
donatien | 0:632c9925f013 | 1543 | { |
donatien | 0:632c9925f013 | 1544 | LWIP_DEBUGF(TCP_DEBUG, ("State: %s\n", tcp_state_str[s])); |
donatien | 0:632c9925f013 | 1545 | } |
donatien | 0:632c9925f013 | 1546 | |
donatien | 0:632c9925f013 | 1547 | /** |
donatien | 0:632c9925f013 | 1548 | * Print tcp flags for debugging purposes. |
donatien | 0:632c9925f013 | 1549 | * |
donatien | 0:632c9925f013 | 1550 | * @param flags tcp flags, all active flags are printed |
donatien | 0:632c9925f013 | 1551 | */ |
donatien | 0:632c9925f013 | 1552 | void |
donatien | 0:632c9925f013 | 1553 | tcp_debug_print_flags(u8_t flags) |
donatien | 0:632c9925f013 | 1554 | { |
donatien | 0:632c9925f013 | 1555 | if (flags & TCP_FIN) { |
donatien | 0:632c9925f013 | 1556 | LWIP_DEBUGF(TCP_DEBUG, ("FIN ")); |
donatien | 0:632c9925f013 | 1557 | } |
donatien | 0:632c9925f013 | 1558 | if (flags & TCP_SYN) { |
donatien | 0:632c9925f013 | 1559 | LWIP_DEBUGF(TCP_DEBUG, ("SYN ")); |
donatien | 0:632c9925f013 | 1560 | } |
donatien | 0:632c9925f013 | 1561 | if (flags & TCP_RST) { |
donatien | 0:632c9925f013 | 1562 | LWIP_DEBUGF(TCP_DEBUG, ("RST ")); |
donatien | 0:632c9925f013 | 1563 | } |
donatien | 0:632c9925f013 | 1564 | if (flags & TCP_PSH) { |
donatien | 0:632c9925f013 | 1565 | LWIP_DEBUGF(TCP_DEBUG, ("PSH ")); |
donatien | 0:632c9925f013 | 1566 | } |
donatien | 0:632c9925f013 | 1567 | if (flags & TCP_ACK) { |
donatien | 0:632c9925f013 | 1568 | LWIP_DEBUGF(TCP_DEBUG, ("ACK ")); |
donatien | 0:632c9925f013 | 1569 | } |
donatien | 0:632c9925f013 | 1570 | if (flags & TCP_URG) { |
donatien | 0:632c9925f013 | 1571 | LWIP_DEBUGF(TCP_DEBUG, ("URG ")); |
donatien | 0:632c9925f013 | 1572 | } |
donatien | 0:632c9925f013 | 1573 | if (flags & TCP_ECE) { |
donatien | 0:632c9925f013 | 1574 | LWIP_DEBUGF(TCP_DEBUG, ("ECE ")); |
donatien | 0:632c9925f013 | 1575 | } |
donatien | 0:632c9925f013 | 1576 | if (flags & TCP_CWR) { |
donatien | 0:632c9925f013 | 1577 | LWIP_DEBUGF(TCP_DEBUG, ("CWR ")); |
donatien | 0:632c9925f013 | 1578 | } |
donatien | 0:632c9925f013 | 1579 | LWIP_DEBUGF(TCP_DEBUG, ("\n")); |
donatien | 0:632c9925f013 | 1580 | } |
donatien | 0:632c9925f013 | 1581 | |
donatien | 0:632c9925f013 | 1582 | /** |
donatien | 0:632c9925f013 | 1583 | * Print all tcp_pcbs in every list for debugging purposes. |
donatien | 0:632c9925f013 | 1584 | */ |
donatien | 0:632c9925f013 | 1585 | void |
donatien | 0:632c9925f013 | 1586 | tcp_debug_print_pcbs(void) |
donatien | 0:632c9925f013 | 1587 | { |
donatien | 0:632c9925f013 | 1588 | struct tcp_pcb *pcb; |
donatien | 0:632c9925f013 | 1589 | LWIP_DEBUGF(TCP_DEBUG, ("Active PCB states:\n")); |
donatien | 0:632c9925f013 | 1590 | for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { |
donatien | 0:632c9925f013 | 1591 | LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ", |
donatien | 0:632c9925f013 | 1592 | pcb->local_port, pcb->remote_port, |
donatien | 0:632c9925f013 | 1593 | pcb->snd_nxt, pcb->rcv_nxt)); |
donatien | 0:632c9925f013 | 1594 | tcp_debug_print_state(pcb->state); |
donatien | 0:632c9925f013 | 1595 | } |
donatien | 0:632c9925f013 | 1596 | LWIP_DEBUGF(TCP_DEBUG, ("Listen PCB states:\n")); |
donatien | 0:632c9925f013 | 1597 | for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next) { |
donatien | 0:632c9925f013 | 1598 | LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ", |
donatien | 0:632c9925f013 | 1599 | pcb->local_port, pcb->remote_port, |
donatien | 0:632c9925f013 | 1600 | pcb->snd_nxt, pcb->rcv_nxt)); |
donatien | 0:632c9925f013 | 1601 | tcp_debug_print_state(pcb->state); |
donatien | 0:632c9925f013 | 1602 | } |
donatien | 0:632c9925f013 | 1603 | LWIP_DEBUGF(TCP_DEBUG, ("TIME-WAIT PCB states:\n")); |
donatien | 0:632c9925f013 | 1604 | for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { |
donatien | 0:632c9925f013 | 1605 | LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ", |
donatien | 0:632c9925f013 | 1606 | pcb->local_port, pcb->remote_port, |
donatien | 0:632c9925f013 | 1607 | pcb->snd_nxt, pcb->rcv_nxt)); |
donatien | 0:632c9925f013 | 1608 | tcp_debug_print_state(pcb->state); |
donatien | 0:632c9925f013 | 1609 | } |
donatien | 0:632c9925f013 | 1610 | } |
donatien | 0:632c9925f013 | 1611 | |
donatien | 0:632c9925f013 | 1612 | /** |
donatien | 0:632c9925f013 | 1613 | * Check state consistency of the tcp_pcb lists. |
donatien | 0:632c9925f013 | 1614 | */ |
donatien | 0:632c9925f013 | 1615 | s16_t |
donatien | 0:632c9925f013 | 1616 | tcp_pcbs_sane(void) |
donatien | 0:632c9925f013 | 1617 | { |
donatien | 0:632c9925f013 | 1618 | struct tcp_pcb *pcb; |
donatien | 0:632c9925f013 | 1619 | for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { |
donatien | 0:632c9925f013 | 1620 | LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED); |
donatien | 0:632c9925f013 | 1621 | LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN); |
donatien | 0:632c9925f013 | 1622 | LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT); |
donatien | 0:632c9925f013 | 1623 | } |
donatien | 0:632c9925f013 | 1624 | for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { |
donatien | 0:632c9925f013 | 1625 | LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT); |
donatien | 0:632c9925f013 | 1626 | } |
donatien | 0:632c9925f013 | 1627 | return 1; |
donatien | 0:632c9925f013 | 1628 | } |
donatien | 0:632c9925f013 | 1629 | #endif /* TCP_DEBUG */ |
donatien | 0:632c9925f013 | 1630 | |
donatien | 0:632c9925f013 | 1631 | #endif /* LWIP_TCP */ |