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