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