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