Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame^] | 1 | /** |
| 2 | * @file |
| 3 | * |
| 4 | * Transmission Control Protocol for IP |
| 5 | * |
| 6 | * This file contains common functions for the TCP implementation, such as functinos |
| 7 | * for manipulating the data structures and the TCP timer functions. TCP functions |
| 8 | * related to input and output is found in tcp_in.c and tcp_out.c respectively. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
| 14 | * All rights reserved. |
| 15 | * |
| 16 | * Redistribution and use in source and binary forms, with or without modification, |
| 17 | * are permitted provided that the following conditions are met: |
| 18 | * |
| 19 | * 1. Redistributions of source code must retain the above copyright notice, |
| 20 | * this list of conditions and the following disclaimer. |
| 21 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 22 | * this list of conditions and the following disclaimer in the documentation |
| 23 | * and/or other materials provided with the distribution. |
| 24 | * 3. The name of the author may not be used to endorse or promote products |
| 25 | * derived from this software without specific prior written permission. |
| 26 | * |
| 27 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 28 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 29 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 30 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 31 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 32 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 33 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 34 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 35 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
| 36 | * OF SUCH DAMAGE. |
| 37 | * |
| 38 | * This file is part of the lwIP TCP/IP stack. |
| 39 | * |
| 40 | * Author: Adam Dunkels <adam@sics.se> |
| 41 | * |
| 42 | */ |
| 43 | |
| 44 | #include <string.h> |
| 45 | |
| 46 | #include "lwip/opt.h" |
| 47 | #include "lwip/def.h" |
| 48 | #include "lwip/mem.h" |
| 49 | #include "lwip/memp.h" |
| 50 | |
| 51 | #include "lwip/tcp.h" |
| 52 | #if LWIP_TCP |
| 53 | |
| 54 | /* Incremented every coarse grained timer shot |
| 55 | (typically every 500 ms, determined by TCP_COARSE_TIMEOUT). */ |
| 56 | u32_t tcp_ticks; |
| 57 | const u8_t tcp_backoff[13] = |
| 58 | { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7}; |
| 59 | |
| 60 | /* The TCP PCB lists. */ |
| 61 | |
| 62 | /** List of all TCP PCBs in LISTEN state */ |
| 63 | union tcp_listen_pcbs_t tcp_listen_pcbs; |
| 64 | /** List of all TCP PCBs that are in a state in which |
| 65 | * they accept or send data. */ |
| 66 | struct tcp_pcb *tcp_active_pcbs; |
| 67 | /** List of all TCP PCBs in TIME-WAIT state */ |
| 68 | struct tcp_pcb *tcp_tw_pcbs; |
| 69 | |
| 70 | struct tcp_pcb *tcp_tmp_pcb; |
| 71 | |
| 72 | static u8_t tcp_timer; |
| 73 | static u16_t tcp_new_port(void); |
| 74 | |
| 75 | /** |
| 76 | * Initializes the TCP layer. |
| 77 | */ |
| 78 | void |
| 79 | tcp_init(void) |
| 80 | { |
| 81 | /* Clear globals. */ |
| 82 | tcp_listen_pcbs.listen_pcbs = NULL; |
| 83 | tcp_active_pcbs = NULL; |
| 84 | tcp_tw_pcbs = NULL; |
| 85 | tcp_tmp_pcb = NULL; |
| 86 | |
| 87 | /* initialize timer */ |
| 88 | tcp_ticks = 0; |
| 89 | tcp_timer = 0; |
| 90 | |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Called periodically to dispatch TCP timers. |
| 95 | * |
| 96 | */ |
| 97 | void |
| 98 | tcp_tmr(void) |
| 99 | { |
| 100 | /* Call tcp_fasttmr() every 250 ms */ |
| 101 | tcp_fasttmr(); |
| 102 | |
| 103 | if (++tcp_timer & 1) { |
| 104 | /* Call tcp_tmr() every 500 ms, i.e., every other timer |
| 105 | tcp_tmr() is called. */ |
| 106 | tcp_slowtmr(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Closes the connection held by the PCB. |
| 112 | * |
| 113 | */ |
| 114 | err_t |
| 115 | tcp_close(struct tcp_pcb *pcb) |
| 116 | { |
| 117 | err_t err; |
| 118 | |
| 119 | #if TCP_DEBUG |
| 120 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in state ")); |
| 121 | tcp_debug_print_state(pcb->state); |
| 122 | LWIP_DEBUGF(TCP_DEBUG, ("\n")); |
| 123 | #endif /* TCP_DEBUG */ |
| 124 | switch (pcb->state) { |
| 125 | case CLOSED: |
| 126 | /* Closing a pcb in the CLOSED state might seem erroneous, |
| 127 | * however, it is in this state once allocated and as yet unused |
| 128 | * and the user needs some way to free it should the need arise. |
| 129 | * Calling tcp_close() with a pcb that has already been closed, (i.e. twice) |
| 130 | * or for a pcb that has been used and then entered the CLOSED state |
| 131 | * is erroneous, but this should never happen as the pcb has in those cases |
| 132 | * been freed, and so any remaining handles are bogus. */ |
| 133 | err = ERR_OK; |
| 134 | memp_free(MEMP_TCP_PCB, pcb); |
| 135 | pcb = NULL; |
| 136 | break; |
| 137 | case LISTEN: |
| 138 | err = ERR_OK; |
| 139 | tcp_pcb_remove((struct tcp_pcb **)&tcp_listen_pcbs.pcbs, pcb); |
| 140 | memp_free(MEMP_TCP_PCB_LISTEN, pcb); |
| 141 | pcb = NULL; |
| 142 | break; |
| 143 | case SYN_SENT: |
| 144 | err = ERR_OK; |
| 145 | tcp_pcb_remove(&tcp_active_pcbs, pcb); |
| 146 | memp_free(MEMP_TCP_PCB, pcb); |
| 147 | pcb = NULL; |
| 148 | break; |
| 149 | case SYN_RCVD: |
| 150 | case ESTABLISHED: |
| 151 | err = tcp_send_ctrl(pcb, TCP_FIN); |
| 152 | if (err == ERR_OK) { |
| 153 | pcb->state = FIN_WAIT_1; |
| 154 | } |
| 155 | break; |
| 156 | case CLOSE_WAIT: |
| 157 | err = tcp_send_ctrl(pcb, TCP_FIN); |
| 158 | if (err == ERR_OK) { |
| 159 | pcb->state = LAST_ACK; |
| 160 | } |
| 161 | break; |
| 162 | default: |
| 163 | /* Has already been closed, do nothing. */ |
| 164 | err = ERR_OK; |
| 165 | pcb = NULL; |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | if (pcb != NULL && err == ERR_OK) { |
| 170 | err = tcp_output(pcb); |
| 171 | } |
| 172 | return err; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Aborts a connection by sending a RST to the remote host and deletes |
| 177 | * the local protocol control block. This is done when a connection is |
| 178 | * killed because of shortage of memory. |
| 179 | * |
| 180 | */ |
| 181 | void |
| 182 | tcp_abort(struct tcp_pcb *pcb) |
| 183 | { |
| 184 | u32_t seqno, ackno; |
| 185 | u16_t remote_port, local_port; |
| 186 | struct ip_addr remote_ip, local_ip; |
| 187 | #if LWIP_CALLBACK_API |
| 188 | void (* errf)(void *arg, err_t err); |
| 189 | #endif /* LWIP_CALLBACK_API */ |
| 190 | void *errf_arg; |
| 191 | |
| 192 | |
| 193 | /* Figure out on which TCP PCB list we are, and remove us. If we |
| 194 | are in an active state, call the receive function associated with |
| 195 | the PCB with a NULL argument, and send an RST to the remote end. */ |
| 196 | if (pcb->state == TIME_WAIT) { |
| 197 | tcp_pcb_remove(&tcp_tw_pcbs, pcb); |
| 198 | memp_free(MEMP_TCP_PCB, pcb); |
| 199 | } else { |
| 200 | seqno = pcb->snd_nxt; |
| 201 | ackno = pcb->rcv_nxt; |
| 202 | ip_addr_set(&local_ip, &(pcb->local_ip)); |
| 203 | ip_addr_set(&remote_ip, &(pcb->remote_ip)); |
| 204 | local_port = pcb->local_port; |
| 205 | remote_port = pcb->remote_port; |
| 206 | #if LWIP_CALLBACK_API |
| 207 | errf = pcb->errf; |
| 208 | #endif /* LWIP_CALLBACK_API */ |
| 209 | errf_arg = pcb->callback_arg; |
| 210 | tcp_pcb_remove(&tcp_active_pcbs, pcb); |
| 211 | if (pcb->unacked != NULL) { |
| 212 | tcp_segs_free(pcb->unacked); |
| 213 | } |
| 214 | if (pcb->unsent != NULL) { |
| 215 | tcp_segs_free(pcb->unsent); |
| 216 | } |
| 217 | #if TCP_QUEUE_OOSEQ |
| 218 | if (pcb->ooseq != NULL) { |
| 219 | tcp_segs_free(pcb->ooseq); |
| 220 | } |
| 221 | #endif /* TCP_QUEUE_OOSEQ */ |
| 222 | memp_free(MEMP_TCP_PCB, pcb); |
| 223 | TCP_EVENT_ERR(errf, errf_arg, ERR_ABRT); |
| 224 | LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abort: sending RST\n")); |
| 225 | tcp_rst(seqno, ackno, &local_ip, &remote_ip, local_port, remote_port); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Binds the connection to a local portnumber and IP address. If the |
| 231 | * IP address is not given (i.e., ipaddr == NULL), the IP address of |
| 232 | * the outgoing network interface is used instead. |
| 233 | * |
| 234 | */ |
| 235 | |
| 236 | err_t |
| 237 | tcp_bind(struct tcp_pcb *pcb, struct ip_addr *ipaddr, u16_t port) |
| 238 | { |
| 239 | struct tcp_pcb *cpcb; |
| 240 | |
| 241 | if (port == 0) { |
| 242 | port = tcp_new_port(); |
| 243 | } |
| 244 | /* Check if the address already is in use. */ |
| 245 | for(cpcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; |
| 246 | cpcb != NULL; cpcb = cpcb->next) { |
| 247 | if (cpcb->local_port == port) { |
| 248 | if (ip_addr_isany(&(cpcb->local_ip)) || |
| 249 | ip_addr_isany(ipaddr) || |
| 250 | ip_addr_cmp(&(cpcb->local_ip), ipaddr)) { |
| 251 | return ERR_USE; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | for(cpcb = tcp_active_pcbs; |
| 256 | cpcb != NULL; cpcb = cpcb->next) { |
| 257 | if (cpcb->local_port == port) { |
| 258 | if (ip_addr_isany(&(cpcb->local_ip)) || |
| 259 | ip_addr_isany(ipaddr) || |
| 260 | ip_addr_cmp(&(cpcb->local_ip), ipaddr)) { |
| 261 | return ERR_USE; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (!ip_addr_isany(ipaddr)) { |
| 267 | pcb->local_ip = *ipaddr; |
| 268 | } |
| 269 | pcb->local_port = port; |
| 270 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %"U16_F"\n", port)); |
| 271 | return ERR_OK; |
| 272 | } |
| 273 | #if LWIP_CALLBACK_API |
| 274 | static err_t |
| 275 | tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err) |
| 276 | { |
| 277 | (void)arg; |
| 278 | (void)pcb; |
| 279 | (void)err; |
| 280 | |
| 281 | return ERR_ABRT; |
| 282 | } |
| 283 | #endif /* LWIP_CALLBACK_API */ |
| 284 | |
| 285 | /** |
| 286 | * Set the state of the connection to be LISTEN, which means that it |
| 287 | * is able to accept incoming connections. The protocol control block |
| 288 | * is reallocated in order to consume less memory. Setting the |
| 289 | * connection to LISTEN is an irreversible process. |
| 290 | * |
| 291 | */ |
| 292 | struct tcp_pcb * |
| 293 | tcp_listen(struct tcp_pcb *pcb) |
| 294 | { |
| 295 | struct tcp_pcb_listen *lpcb; |
| 296 | |
| 297 | /* already listening? */ |
| 298 | if (pcb->state == LISTEN) { |
| 299 | return pcb; |
| 300 | } |
| 301 | lpcb = memp_malloc(MEMP_TCP_PCB_LISTEN); |
| 302 | if (lpcb == NULL) { |
| 303 | return NULL; |
| 304 | } |
| 305 | lpcb->callback_arg = pcb->callback_arg; |
| 306 | lpcb->local_port = pcb->local_port; |
| 307 | lpcb->state = LISTEN; |
| 308 | lpcb->so_options = pcb->so_options; |
| 309 | lpcb->so_options |= SOF_ACCEPTCONN; |
| 310 | lpcb->ttl = pcb->ttl; |
| 311 | lpcb->tos = pcb->tos; |
| 312 | ip_addr_set(&lpcb->local_ip, &pcb->local_ip); |
| 313 | memp_free(MEMP_TCP_PCB, pcb); |
| 314 | #if LWIP_CALLBACK_API |
| 315 | lpcb->accept = tcp_accept_null; |
| 316 | #endif /* LWIP_CALLBACK_API */ |
| 317 | TCP_REG(&tcp_listen_pcbs.listen_pcbs, lpcb); |
| 318 | return (struct tcp_pcb *)lpcb; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * This function should be called by the application when it has |
| 323 | * processed the data. The purpose is to advertise a larger window |
| 324 | * when the data has been processed. |
| 325 | * |
| 326 | */ |
| 327 | void |
| 328 | tcp_recved(struct tcp_pcb *pcb, u16_t len) |
| 329 | { |
| 330 | if ((u32_t)pcb->rcv_wnd + len > TCP_WND) { |
| 331 | pcb->rcv_wnd = TCP_WND; |
| 332 | } else { |
| 333 | pcb->rcv_wnd += len; |
| 334 | } |
| 335 | if (!(pcb->flags & TF_ACK_DELAY) && |
| 336 | !(pcb->flags & TF_ACK_NOW)) { |
| 337 | /* |
| 338 | * We send an ACK here (if one is not already pending, hence |
| 339 | * the above tests) as tcp_recved() implies that the application |
| 340 | * has processed some data, and so we can open the receiver's |
| 341 | * window to allow more to be transmitted. This could result in |
| 342 | * two ACKs being sent for each received packet in some limited cases |
| 343 | * (where the application is only receiving data, and is slow to |
| 344 | * process it) but it is necessary to guarantee that the sender can |
| 345 | * continue to transmit. |
| 346 | */ |
| 347 | tcp_ack(pcb); |
| 348 | } |
| 349 | else if (pcb->flags & TF_ACK_DELAY && pcb->rcv_wnd >= TCP_WND/2) { |
| 350 | /* If we can send a window update such that there is a full |
| 351 | * segment available in the window, do so now. This is sort of |
| 352 | * nagle-like in its goals, and tries to hit a compromise between |
| 353 | * sending acks each time the window is updated, and only sending |
| 354 | * window updates when a timer expires. The "threshold" used |
| 355 | * above (currently TCP_WND/2) can be tuned to be more or less |
| 356 | * aggressive */ |
| 357 | tcp_ack_now(pcb); |
| 358 | } |
| 359 | |
| 360 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: recveived %"U16_F" bytes, wnd %"U16_F" (%"U16_F").\n", |
| 361 | len, pcb->rcv_wnd, TCP_WND - pcb->rcv_wnd)); |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * A nastly hack featuring 'goto' statements that allocates a |
| 366 | * new TCP local port. |
| 367 | */ |
| 368 | static u16_t |
| 369 | tcp_new_port(void) |
| 370 | { |
| 371 | struct tcp_pcb *pcb; |
| 372 | #ifndef TCP_LOCAL_PORT_RANGE_START |
| 373 | #define TCP_LOCAL_PORT_RANGE_START 4096 |
| 374 | #define TCP_LOCAL_PORT_RANGE_END 0x7fff |
| 375 | #endif |
| 376 | static u16_t port = TCP_LOCAL_PORT_RANGE_START; |
| 377 | |
| 378 | again: |
| 379 | if (++port > TCP_LOCAL_PORT_RANGE_END) { |
| 380 | port = TCP_LOCAL_PORT_RANGE_START; |
| 381 | } |
| 382 | |
| 383 | for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { |
| 384 | if (pcb->local_port == port) { |
| 385 | goto again; |
| 386 | } |
| 387 | } |
| 388 | for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { |
| 389 | if (pcb->local_port == port) { |
| 390 | goto again; |
| 391 | } |
| 392 | } |
| 393 | for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next) { |
| 394 | if (pcb->local_port == port) { |
| 395 | goto again; |
| 396 | } |
| 397 | } |
| 398 | return port; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Connects to another host. The function given as the "connected" |
| 403 | * argument will be called when the connection has been established. |
| 404 | * |
| 405 | */ |
| 406 | err_t |
| 407 | tcp_connect(struct tcp_pcb *pcb, struct ip_addr *ipaddr, u16_t port, |
| 408 | err_t (* connected)(void *arg, struct tcp_pcb *tpcb, err_t err)) |
| 409 | { |
| 410 | u32_t optdata; |
| 411 | err_t ret; |
| 412 | u32_t iss; |
| 413 | |
| 414 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port)); |
| 415 | if (ipaddr != NULL) { |
| 416 | pcb->remote_ip = *ipaddr; |
| 417 | } else { |
| 418 | return ERR_VAL; |
| 419 | } |
| 420 | pcb->remote_port = port; |
| 421 | if (pcb->local_port == 0) { |
| 422 | pcb->local_port = tcp_new_port(); |
| 423 | } |
| 424 | iss = tcp_next_iss(); |
| 425 | pcb->rcv_nxt = 0; |
| 426 | pcb->snd_nxt = iss; |
| 427 | pcb->lastack = iss - 1; |
| 428 | pcb->snd_lbb = iss - 1; |
| 429 | pcb->rcv_wnd = TCP_WND; |
| 430 | pcb->snd_wnd = TCP_WND; |
| 431 | pcb->mss = TCP_MSS; |
| 432 | pcb->cwnd = 1; |
| 433 | pcb->ssthresh = pcb->mss * 10; |
| 434 | pcb->state = SYN_SENT; |
| 435 | #if LWIP_CALLBACK_API |
| 436 | pcb->connected = connected; |
| 437 | #endif /* LWIP_CALLBACK_API */ |
| 438 | TCP_REG(&tcp_active_pcbs, pcb); |
| 439 | |
| 440 | /* Build an MSS option */ |
| 441 | optdata = htonl(((u32_t)2 << 24) | |
| 442 | ((u32_t)4 << 16) | |
| 443 | (((u32_t)pcb->mss / 256) << 8) | |
| 444 | (pcb->mss & 255)); |
| 445 | |
| 446 | ret = tcp_enqueue(pcb, NULL, 0, TCP_SYN, 0, (u8_t *)&optdata, 4); |
| 447 | if (ret == ERR_OK) { |
| 448 | tcp_output(pcb); |
| 449 | } |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Called every 500 ms and implements the retransmission timer and the timer that |
| 455 | * removes PCBs that have been in TIME-WAIT for enough time. It also increments |
| 456 | * various timers such as the inactivity timer in each PCB. |
| 457 | */ |
| 458 | void |
| 459 | tcp_slowtmr(void) |
| 460 | { |
| 461 | struct tcp_pcb *pcb, *pcb2, *prev; |
| 462 | u32_t eff_wnd; |
| 463 | u8_t pcb_remove; /* flag if a PCB should be removed */ |
| 464 | err_t err; |
| 465 | |
| 466 | err = ERR_OK; |
| 467 | |
| 468 | ++tcp_ticks; |
| 469 | |
| 470 | /* Steps through all of the active PCBs. */ |
| 471 | prev = NULL; |
| 472 | pcb = tcp_active_pcbs; |
| 473 | if (pcb == NULL) { |
| 474 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: no active pcbs\n")); |
| 475 | } |
| 476 | while (pcb != NULL) { |
| 477 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: processing active pcb\n")); |
| 478 | LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED); |
| 479 | LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN); |
| 480 | LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT); |
| 481 | |
| 482 | pcb_remove = 0; |
| 483 | |
| 484 | if (pcb->state == SYN_SENT && pcb->nrtx == TCP_SYNMAXRTX) { |
| 485 | ++pcb_remove; |
| 486 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max SYN retries reached\n")); |
| 487 | } |
| 488 | else if (pcb->nrtx == TCP_MAXRTX) { |
| 489 | ++pcb_remove; |
| 490 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max DATA retries reached\n")); |
| 491 | } else { |
| 492 | ++pcb->rtime; |
| 493 | if (pcb->unacked != NULL && pcb->rtime >= pcb->rto) { |
| 494 | |
| 495 | /* Time for a retransmission. */ |
| 496 | LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_slowtmr: rtime %"U16_F" pcb->rto %"U16_F"\n", |
| 497 | pcb->rtime, pcb->rto)); |
| 498 | |
| 499 | /* Double retransmission time-out unless we are trying to |
| 500 | * connect to somebody (i.e., we are in SYN_SENT). */ |
| 501 | if (pcb->state != SYN_SENT) { |
| 502 | pcb->rto = ((pcb->sa >> 3) + pcb->sv) << tcp_backoff[pcb->nrtx]; |
| 503 | } |
| 504 | /* Reduce congestion window and ssthresh. */ |
| 505 | eff_wnd = LWIP_MIN(pcb->cwnd, pcb->snd_wnd); |
| 506 | pcb->ssthresh = eff_wnd >> 1; |
| 507 | if (pcb->ssthresh < pcb->mss) { |
| 508 | pcb->ssthresh = pcb->mss * 2; |
| 509 | } |
| 510 | pcb->cwnd = pcb->mss; |
| 511 | LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: cwnd %"U16_F" ssthresh %"U16_F"\n", |
| 512 | pcb->cwnd, pcb->ssthresh)); |
| 513 | |
| 514 | /* The following needs to be called AFTER cwnd is set to one mss - STJ */ |
| 515 | tcp_rexmit_rto(pcb); |
| 516 | } |
| 517 | } |
| 518 | /* Check if this PCB has stayed too long in FIN-WAIT-2 */ |
| 519 | if (pcb->state == FIN_WAIT_2) { |
| 520 | if ((u32_t)(tcp_ticks - pcb->tmr) > |
| 521 | TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) { |
| 522 | ++pcb_remove; |
| 523 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2\n")); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /* Check if KEEPALIVE should be sent */ |
| 528 | if((pcb->so_options & SOF_KEEPALIVE) && ((pcb->state == ESTABLISHED) || (pcb->state == CLOSE_WAIT))) { |
| 529 | if((u32_t)(tcp_ticks - pcb->tmr) > (pcb->keepalive + TCP_MAXIDLE) / TCP_SLOW_INTERVAL) { |
| 530 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: KEEPALIVE timeout. Aborting connection to %"U16_F".%"U16_F".%"U16_F".%"U16_F".\n", |
| 531 | ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip), |
| 532 | ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip))); |
| 533 | |
| 534 | tcp_abort(pcb); |
| 535 | } |
| 536 | else if((u32_t)(tcp_ticks - pcb->tmr) > (pcb->keepalive + pcb->keep_cnt * TCP_KEEPINTVL) / TCP_SLOW_INTERVAL) { |
| 537 | tcp_keepalive(pcb); |
| 538 | pcb->keep_cnt++; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /* If this PCB has queued out of sequence data, but has been |
| 543 | inactive for too long, will drop the data (it will eventually |
| 544 | be retransmitted). */ |
| 545 | #if TCP_QUEUE_OOSEQ |
| 546 | if (pcb->ooseq != NULL && |
| 547 | (u32_t)tcp_ticks - pcb->tmr >= |
| 548 | pcb->rto * TCP_OOSEQ_TIMEOUT) { |
| 549 | tcp_segs_free(pcb->ooseq); |
| 550 | pcb->ooseq = NULL; |
| 551 | LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: dropping OOSEQ queued data\n")); |
| 552 | } |
| 553 | #endif /* TCP_QUEUE_OOSEQ */ |
| 554 | |
| 555 | /* Check if this PCB has stayed too long in SYN-RCVD */ |
| 556 | if (pcb->state == SYN_RCVD) { |
| 557 | if ((u32_t)(tcp_ticks - pcb->tmr) > |
| 558 | TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) { |
| 559 | ++pcb_remove; |
| 560 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD\n")); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | /* Check if this PCB has stayed too long in LAST-ACK */ |
| 565 | if (pcb->state == LAST_ACK) { |
| 566 | if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) { |
| 567 | ++pcb_remove; |
| 568 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in LAST-ACK\n")); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | /* If the PCB should be removed, do it. */ |
| 573 | if (pcb_remove) { |
| 574 | tcp_pcb_purge(pcb); |
| 575 | /* Remove PCB from tcp_active_pcbs list. */ |
| 576 | if (prev != NULL) { |
| 577 | LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs); |
| 578 | prev->next = pcb->next; |
| 579 | } else { |
| 580 | /* This PCB was the first. */ |
| 581 | LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb); |
| 582 | tcp_active_pcbs = pcb->next; |
| 583 | } |
| 584 | |
| 585 | TCP_EVENT_ERR(pcb->errf, pcb->callback_arg, ERR_ABRT); |
| 586 | |
| 587 | pcb2 = pcb->next; |
| 588 | memp_free(MEMP_TCP_PCB, pcb); |
| 589 | pcb = pcb2; |
| 590 | } else { |
| 591 | |
| 592 | /* We check if we should poll the connection. */ |
| 593 | ++pcb->polltmr; |
| 594 | if (pcb->polltmr >= pcb->pollinterval) { |
| 595 | pcb->polltmr = 0; |
| 596 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: polling application\n")); |
| 597 | TCP_EVENT_POLL(pcb, err); |
| 598 | if (err == ERR_OK) { |
| 599 | tcp_output(pcb); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | prev = pcb; |
| 604 | pcb = pcb->next; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | |
| 609 | /* Steps through all of the TIME-WAIT PCBs. */ |
| 610 | prev = NULL; |
| 611 | pcb = tcp_tw_pcbs; |
| 612 | while (pcb != NULL) { |
| 613 | LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT); |
| 614 | pcb_remove = 0; |
| 615 | |
| 616 | /* Check if this PCB has stayed long enough in TIME-WAIT */ |
| 617 | if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) { |
| 618 | ++pcb_remove; |
| 619 | } |
| 620 | |
| 621 | |
| 622 | |
| 623 | /* If the PCB should be removed, do it. */ |
| 624 | if (pcb_remove) { |
| 625 | tcp_pcb_purge(pcb); |
| 626 | /* Remove PCB from tcp_tw_pcbs list. */ |
| 627 | if (prev != NULL) { |
| 628 | LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs); |
| 629 | prev->next = pcb->next; |
| 630 | } else { |
| 631 | /* This PCB was the first. */ |
| 632 | LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb); |
| 633 | tcp_tw_pcbs = pcb->next; |
| 634 | } |
| 635 | pcb2 = pcb->next; |
| 636 | memp_free(MEMP_TCP_PCB, pcb); |
| 637 | pcb = pcb2; |
| 638 | } else { |
| 639 | prev = pcb; |
| 640 | pcb = pcb->next; |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * Is called every TCP_FAST_INTERVAL (250 ms) and sends delayed ACKs. |
| 647 | */ |
| 648 | void |
| 649 | tcp_fasttmr(void) |
| 650 | { |
| 651 | struct tcp_pcb *pcb; |
| 652 | |
| 653 | /* send delayed ACKs */ |
| 654 | for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { |
| 655 | if (pcb->flags & TF_ACK_DELAY) { |
| 656 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n")); |
| 657 | tcp_ack_now(pcb); |
| 658 | pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * Deallocates a list of TCP segments (tcp_seg structures). |
| 665 | * |
| 666 | */ |
| 667 | u8_t |
| 668 | tcp_segs_free(struct tcp_seg *seg) |
| 669 | { |
| 670 | u8_t count = 0; |
| 671 | struct tcp_seg *next; |
| 672 | while (seg != NULL) { |
| 673 | next = seg->next; |
| 674 | count += tcp_seg_free(seg); |
| 675 | seg = next; |
| 676 | } |
| 677 | return count; |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Frees a TCP segment. |
| 682 | * |
| 683 | */ |
| 684 | u8_t |
| 685 | tcp_seg_free(struct tcp_seg *seg) |
| 686 | { |
| 687 | u8_t count = 0; |
| 688 | |
| 689 | if (seg != NULL) { |
| 690 | if (seg->p != NULL) { |
| 691 | count = pbuf_free(seg->p); |
| 692 | #if TCP_DEBUG |
| 693 | seg->p = NULL; |
| 694 | #endif /* TCP_DEBUG */ |
| 695 | } |
| 696 | memp_free(MEMP_TCP_SEG, seg); |
| 697 | } |
| 698 | return count; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * Sets the priority of a connection. |
| 703 | * |
| 704 | */ |
| 705 | void |
| 706 | tcp_setprio(struct tcp_pcb *pcb, u8_t prio) |
| 707 | { |
| 708 | pcb->prio = prio; |
| 709 | } |
| 710 | #if TCP_QUEUE_OOSEQ |
| 711 | |
| 712 | /** |
| 713 | * Returns a copy of the given TCP segment. |
| 714 | * |
| 715 | */ |
| 716 | struct tcp_seg * |
| 717 | tcp_seg_copy(struct tcp_seg *seg) |
| 718 | { |
| 719 | struct tcp_seg *cseg; |
| 720 | |
| 721 | cseg = memp_malloc(MEMP_TCP_SEG); |
| 722 | if (cseg == NULL) { |
| 723 | return NULL; |
| 724 | } |
| 725 | memcpy((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg)); |
| 726 | pbuf_ref(cseg->p); |
| 727 | return cseg; |
| 728 | } |
| 729 | #endif |
| 730 | |
| 731 | #if LWIP_CALLBACK_API |
| 732 | static err_t |
| 733 | tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) |
| 734 | { |
| 735 | arg = arg; |
| 736 | if (p != NULL) { |
| 737 | pbuf_free(p); |
| 738 | } else if (err == ERR_OK) { |
| 739 | return tcp_close(pcb); |
| 740 | } |
| 741 | return ERR_OK; |
| 742 | } |
| 743 | #endif /* LWIP_CALLBACK_API */ |
| 744 | |
| 745 | static void |
| 746 | tcp_kill_prio(u8_t prio) |
| 747 | { |
| 748 | struct tcp_pcb *pcb, *inactive; |
| 749 | u32_t inactivity; |
| 750 | u8_t mprio; |
| 751 | |
| 752 | |
| 753 | mprio = TCP_PRIO_MAX; |
| 754 | |
| 755 | /* We kill the oldest active connection that has lower priority than |
| 756 | prio. */ |
| 757 | inactivity = 0; |
| 758 | inactive = NULL; |
| 759 | for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { |
| 760 | if (pcb->prio <= prio && |
| 761 | pcb->prio <= mprio && |
| 762 | (u32_t)(tcp_ticks - pcb->tmr) >= inactivity) { |
| 763 | inactivity = tcp_ticks - pcb->tmr; |
| 764 | inactive = pcb; |
| 765 | mprio = pcb->prio; |
| 766 | } |
| 767 | } |
| 768 | if (inactive != NULL) { |
| 769 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F")\n", |
| 770 | (void *)inactive, inactivity)); |
| 771 | tcp_abort(inactive); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | |
| 776 | static void |
| 777 | tcp_kill_timewait(void) |
| 778 | { |
| 779 | struct tcp_pcb *pcb, *inactive; |
| 780 | u32_t inactivity; |
| 781 | |
| 782 | inactivity = 0; |
| 783 | inactive = NULL; |
| 784 | for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { |
| 785 | if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) { |
| 786 | inactivity = tcp_ticks - pcb->tmr; |
| 787 | inactive = pcb; |
| 788 | } |
| 789 | } |
| 790 | if (inactive != NULL) { |
| 791 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F")\n", |
| 792 | (void *)inactive, inactivity)); |
| 793 | tcp_abort(inactive); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | |
| 798 | |
| 799 | struct tcp_pcb * |
| 800 | tcp_alloc(u8_t prio) |
| 801 | { |
| 802 | struct tcp_pcb *pcb; |
| 803 | u32_t iss; |
| 804 | |
| 805 | pcb = memp_malloc(MEMP_TCP_PCB); |
| 806 | if (pcb == NULL) { |
| 807 | /* Try killing oldest connection in TIME-WAIT. */ |
| 808 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest TIME-WAIT connection\n")); |
| 809 | tcp_kill_timewait(); |
| 810 | pcb = memp_malloc(MEMP_TCP_PCB); |
| 811 | if (pcb == NULL) { |
| 812 | tcp_kill_prio(prio); |
| 813 | pcb = memp_malloc(MEMP_TCP_PCB); |
| 814 | } |
| 815 | } |
| 816 | if (pcb != NULL) { |
| 817 | memset(pcb, 0, sizeof(struct tcp_pcb)); |
| 818 | pcb->prio = TCP_PRIO_NORMAL; |
| 819 | pcb->snd_buf = TCP_SND_BUF; |
| 820 | pcb->snd_queuelen = 0; |
| 821 | pcb->rcv_wnd = TCP_WND; |
| 822 | pcb->tos = 0; |
| 823 | pcb->ttl = TCP_TTL; |
| 824 | pcb->mss = TCP_MSS; |
| 825 | pcb->rto = 3000 / TCP_SLOW_INTERVAL; |
| 826 | pcb->sa = 0; |
| 827 | pcb->sv = 3000 / TCP_SLOW_INTERVAL; |
| 828 | pcb->rtime = 0; |
| 829 | pcb->cwnd = 1; |
| 830 | iss = tcp_next_iss(); |
| 831 | pcb->snd_wl2 = iss; |
| 832 | pcb->snd_nxt = iss; |
| 833 | pcb->snd_max = iss; |
| 834 | pcb->lastack = iss; |
| 835 | pcb->snd_lbb = iss; |
| 836 | pcb->tmr = tcp_ticks; |
| 837 | |
| 838 | pcb->polltmr = 0; |
| 839 | |
| 840 | #if LWIP_CALLBACK_API |
| 841 | pcb->recv = tcp_recv_null; |
| 842 | #endif /* LWIP_CALLBACK_API */ |
| 843 | |
| 844 | /* Init KEEPALIVE timer */ |
| 845 | pcb->keepalive = TCP_KEEPDEFAULT; |
| 846 | pcb->keep_cnt = 0; |
| 847 | } |
| 848 | return pcb; |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * Creates a new TCP protocol control block but doesn't place it on |
| 853 | * any of the TCP PCB lists. |
| 854 | * |
| 855 | * @internal: Maybe there should be a idle TCP PCB list where these |
| 856 | * PCBs are put on. We can then implement port reservation using |
| 857 | * tcp_bind(). Currently, we lack this (BSD socket type of) feature. |
| 858 | */ |
| 859 | |
| 860 | struct tcp_pcb * |
| 861 | tcp_new(void) |
| 862 | { |
| 863 | return tcp_alloc(TCP_PRIO_NORMAL); |
| 864 | } |
| 865 | |
| 866 | /* |
| 867 | * tcp_arg(): |
| 868 | * |
| 869 | * Used to specify the argument that should be passed callback |
| 870 | * functions. |
| 871 | * |
| 872 | */ |
| 873 | |
| 874 | void |
| 875 | tcp_arg(struct tcp_pcb *pcb, void *arg) |
| 876 | { |
| 877 | pcb->callback_arg = arg; |
| 878 | } |
| 879 | #if LWIP_CALLBACK_API |
| 880 | |
| 881 | /** |
| 882 | * Used to specify the function that should be called when a TCP |
| 883 | * connection receives data. |
| 884 | * |
| 885 | */ |
| 886 | void |
| 887 | tcp_recv(struct tcp_pcb *pcb, |
| 888 | err_t (* recv)(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)) |
| 889 | { |
| 890 | pcb->recv = recv; |
| 891 | } |
| 892 | |
| 893 | /** |
| 894 | * Used to specify the function that should be called when TCP data |
| 895 | * has been successfully delivered to the remote host. |
| 896 | * |
| 897 | */ |
| 898 | |
| 899 | void |
| 900 | tcp_sent(struct tcp_pcb *pcb, |
| 901 | err_t (* sent)(void *arg, struct tcp_pcb *tpcb, u16_t len)) |
| 902 | { |
| 903 | pcb->sent = sent; |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * Used to specify the function that should be called when a fatal error |
| 908 | * has occured on the connection. |
| 909 | * |
| 910 | */ |
| 911 | void |
| 912 | tcp_err(struct tcp_pcb *pcb, |
| 913 | void (* errf)(void *arg, err_t err)) |
| 914 | { |
| 915 | pcb->errf = errf; |
| 916 | } |
| 917 | |
| 918 | /** |
| 919 | * Used for specifying the function that should be called when a |
| 920 | * LISTENing connection has been connected to another host. |
| 921 | * |
| 922 | */ |
| 923 | void |
| 924 | tcp_accept(struct tcp_pcb *pcb, |
| 925 | err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err)) |
| 926 | { |
| 927 | ((struct tcp_pcb_listen *)pcb)->accept = accept; |
| 928 | } |
| 929 | #endif /* LWIP_CALLBACK_API */ |
| 930 | |
| 931 | |
| 932 | /** |
| 933 | * Used to specify the function that should be called periodically |
| 934 | * from TCP. The interval is specified in terms of the TCP coarse |
| 935 | * timer interval, which is called twice a second. |
| 936 | * |
| 937 | */ |
| 938 | void |
| 939 | tcp_poll(struct tcp_pcb *pcb, |
| 940 | err_t (* poll)(void *arg, struct tcp_pcb *tpcb), u8_t interval) |
| 941 | { |
| 942 | #if LWIP_CALLBACK_API |
| 943 | pcb->poll = poll; |
| 944 | #endif /* LWIP_CALLBACK_API */ |
| 945 | pcb->pollinterval = interval; |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * Purges a TCP PCB. Removes any buffered data and frees the buffer memory. |
| 950 | * |
| 951 | */ |
| 952 | void |
| 953 | tcp_pcb_purge(struct tcp_pcb *pcb) |
| 954 | { |
| 955 | if (pcb->state != CLOSED && |
| 956 | pcb->state != TIME_WAIT && |
| 957 | pcb->state != LISTEN) { |
| 958 | |
| 959 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge\n")); |
| 960 | |
| 961 | if (pcb->unsent != NULL) { |
| 962 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: not all data sent\n")); |
| 963 | } |
| 964 | if (pcb->unacked != NULL) { |
| 965 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->unacked\n")); |
| 966 | } |
| 967 | #if TCP_QUEUE_OOSEQ /* LW */ |
| 968 | if (pcb->ooseq != NULL) { |
| 969 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->ooseq\n")); |
| 970 | } |
| 971 | |
| 972 | tcp_segs_free(pcb->ooseq); |
| 973 | pcb->ooseq = NULL; |
| 974 | #endif /* TCP_QUEUE_OOSEQ */ |
| 975 | tcp_segs_free(pcb->unsent); |
| 976 | tcp_segs_free(pcb->unacked); |
| 977 | pcb->unacked = pcb->unsent = NULL; |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | /** |
| 982 | * Purges the PCB and removes it from a PCB list. Any delayed ACKs are sent first. |
| 983 | * |
| 984 | */ |
| 985 | void |
| 986 | tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb) |
| 987 | { |
| 988 | TCP_RMV(pcblist, pcb); |
| 989 | |
| 990 | tcp_pcb_purge(pcb); |
| 991 | |
| 992 | /* if there is an outstanding delayed ACKs, send it */ |
| 993 | if (pcb->state != TIME_WAIT && |
| 994 | pcb->state != LISTEN && |
| 995 | pcb->flags & TF_ACK_DELAY) { |
| 996 | pcb->flags |= TF_ACK_NOW; |
| 997 | tcp_output(pcb); |
| 998 | } |
| 999 | pcb->state = CLOSED; |
| 1000 | |
| 1001 | LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane()); |
| 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * Calculates a new initial sequence number for new connections. |
| 1006 | * |
| 1007 | */ |
| 1008 | u32_t |
| 1009 | tcp_next_iss(void) |
| 1010 | { |
| 1011 | static u32_t iss = 6510; |
| 1012 | |
| 1013 | iss += tcp_ticks; /* XXX */ |
| 1014 | return iss; |
| 1015 | } |
| 1016 | |
| 1017 | #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG |
| 1018 | void |
| 1019 | tcp_debug_print(struct tcp_hdr *tcphdr) |
| 1020 | { |
| 1021 | LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n")); |
| 1022 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); |
| 1023 | LWIP_DEBUGF(TCP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n", |
| 1024 | ntohs(tcphdr->src), ntohs(tcphdr->dest))); |
| 1025 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); |
| 1026 | LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (seq no)\n", |
| 1027 | ntohl(tcphdr->seqno))); |
| 1028 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); |
| 1029 | LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (ack no)\n", |
| 1030 | ntohl(tcphdr->ackno))); |
| 1031 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); |
| 1032 | LWIP_DEBUGF(TCP_DEBUG, ("| %2"U16_F" | |%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"| %5"U16_F" | (hdrlen, flags (", |
| 1033 | TCPH_HDRLEN(tcphdr), |
| 1034 | TCPH_FLAGS(tcphdr) >> 5 & 1, |
| 1035 | TCPH_FLAGS(tcphdr) >> 4 & 1, |
| 1036 | TCPH_FLAGS(tcphdr) >> 3 & 1, |
| 1037 | TCPH_FLAGS(tcphdr) >> 2 & 1, |
| 1038 | TCPH_FLAGS(tcphdr) >> 1 & 1, |
| 1039 | TCPH_FLAGS(tcphdr) & 1, |
| 1040 | ntohs(tcphdr->wnd))); |
| 1041 | tcp_debug_print_flags(TCPH_FLAGS(tcphdr)); |
| 1042 | LWIP_DEBUGF(TCP_DEBUG, ("), win)\n")); |
| 1043 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); |
| 1044 | LWIP_DEBUGF(TCP_DEBUG, ("| 0x%04"X16_F" | %5"U16_F" | (chksum, urgp)\n", |
| 1045 | ntohs(tcphdr->chksum), ntohs(tcphdr->urgp))); |
| 1046 | LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n")); |
| 1047 | } |
| 1048 | |
| 1049 | void |
| 1050 | tcp_debug_print_state(enum tcp_state s) |
| 1051 | { |
| 1052 | LWIP_DEBUGF(TCP_DEBUG, ("State: ")); |
| 1053 | switch (s) { |
| 1054 | case CLOSED: |
| 1055 | LWIP_DEBUGF(TCP_DEBUG, ("CLOSED\n")); |
| 1056 | break; |
| 1057 | case LISTEN: |
| 1058 | LWIP_DEBUGF(TCP_DEBUG, ("LISTEN\n")); |
| 1059 | break; |
| 1060 | case SYN_SENT: |
| 1061 | LWIP_DEBUGF(TCP_DEBUG, ("SYN_SENT\n")); |
| 1062 | break; |
| 1063 | case SYN_RCVD: |
| 1064 | LWIP_DEBUGF(TCP_DEBUG, ("SYN_RCVD\n")); |
| 1065 | break; |
| 1066 | case ESTABLISHED: |
| 1067 | LWIP_DEBUGF(TCP_DEBUG, ("ESTABLISHED\n")); |
| 1068 | break; |
| 1069 | case FIN_WAIT_1: |
| 1070 | LWIP_DEBUGF(TCP_DEBUG, ("FIN_WAIT_1\n")); |
| 1071 | break; |
| 1072 | case FIN_WAIT_2: |
| 1073 | LWIP_DEBUGF(TCP_DEBUG, ("FIN_WAIT_2\n")); |
| 1074 | break; |
| 1075 | case CLOSE_WAIT: |
| 1076 | LWIP_DEBUGF(TCP_DEBUG, ("CLOSE_WAIT\n")); |
| 1077 | break; |
| 1078 | case CLOSING: |
| 1079 | LWIP_DEBUGF(TCP_DEBUG, ("CLOSING\n")); |
| 1080 | break; |
| 1081 | case LAST_ACK: |
| 1082 | LWIP_DEBUGF(TCP_DEBUG, ("LAST_ACK\n")); |
| 1083 | break; |
| 1084 | case TIME_WAIT: |
| 1085 | LWIP_DEBUGF(TCP_DEBUG, ("TIME_WAIT\n")); |
| 1086 | break; |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | void |
| 1091 | tcp_debug_print_flags(u8_t flags) |
| 1092 | { |
| 1093 | if (flags & TCP_FIN) { |
| 1094 | LWIP_DEBUGF(TCP_DEBUG, ("FIN ")); |
| 1095 | } |
| 1096 | if (flags & TCP_SYN) { |
| 1097 | LWIP_DEBUGF(TCP_DEBUG, ("SYN ")); |
| 1098 | } |
| 1099 | if (flags & TCP_RST) { |
| 1100 | LWIP_DEBUGF(TCP_DEBUG, ("RST ")); |
| 1101 | } |
| 1102 | if (flags & TCP_PSH) { |
| 1103 | LWIP_DEBUGF(TCP_DEBUG, ("PSH ")); |
| 1104 | } |
| 1105 | if (flags & TCP_ACK) { |
| 1106 | LWIP_DEBUGF(TCP_DEBUG, ("ACK ")); |
| 1107 | } |
| 1108 | if (flags & TCP_URG) { |
| 1109 | LWIP_DEBUGF(TCP_DEBUG, ("URG ")); |
| 1110 | } |
| 1111 | if (flags & TCP_ECE) { |
| 1112 | LWIP_DEBUGF(TCP_DEBUG, ("ECE ")); |
| 1113 | } |
| 1114 | if (flags & TCP_CWR) { |
| 1115 | LWIP_DEBUGF(TCP_DEBUG, ("CWR ")); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | void |
| 1120 | tcp_debug_print_pcbs(void) |
| 1121 | { |
| 1122 | struct tcp_pcb *pcb; |
| 1123 | LWIP_DEBUGF(TCP_DEBUG, ("Active PCB states:\n")); |
| 1124 | for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { |
| 1125 | LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ", |
| 1126 | pcb->local_port, pcb->remote_port, |
| 1127 | pcb->snd_nxt, pcb->rcv_nxt)); |
| 1128 | tcp_debug_print_state(pcb->state); |
| 1129 | } |
| 1130 | LWIP_DEBUGF(TCP_DEBUG, ("Listen PCB states:\n")); |
| 1131 | for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next) { |
| 1132 | LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ", |
| 1133 | pcb->local_port, pcb->remote_port, |
| 1134 | pcb->snd_nxt, pcb->rcv_nxt)); |
| 1135 | tcp_debug_print_state(pcb->state); |
| 1136 | } |
| 1137 | LWIP_DEBUGF(TCP_DEBUG, ("TIME-WAIT PCB states:\n")); |
| 1138 | for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { |
| 1139 | LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ", |
| 1140 | pcb->local_port, pcb->remote_port, |
| 1141 | pcb->snd_nxt, pcb->rcv_nxt)); |
| 1142 | tcp_debug_print_state(pcb->state); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | s16_t |
| 1147 | tcp_pcbs_sane(void) |
| 1148 | { |
| 1149 | struct tcp_pcb *pcb; |
| 1150 | for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { |
| 1151 | LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED); |
| 1152 | LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN); |
| 1153 | LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT); |
| 1154 | } |
| 1155 | for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { |
| 1156 | LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT); |
| 1157 | } |
| 1158 | return 1; |
| 1159 | } |
| 1160 | #endif /* TCP_DEBUG */ |
| 1161 | #endif /* LWIP_TCP */ |
| 1162 | |
| 1163 | |
| 1164 | |
| 1165 | |
| 1166 | |
| 1167 | |
| 1168 | |
| 1169 | |
| 1170 | |
| 1171 | |