# tcp.h

Go to the documentation of this file.

    00001 /*
    00002  * Copyright (c) 2001, Swedish Institute of Computer Science.
    00003  * All rights reserved.
    00004  *
    00005  * Redistribution and use in source and binary forms, with or without
    00006  * modification, are permitted provided that the following conditions
    00007  * are met:
    00008  * 1. Redistributions of source code must retain the above copyright
    00009  *    notice, this list of conditions and the following disclaimer.
    00010  * 2. Redistributions in binary form must reproduce the above copyright
    00011  *    notice, this list of conditions and the following disclaimer in the
    00012  *    documentation and/or other materials provided with the distribution.
    00013  * 3. Neither the name of the Institute nor the names of its contributors
    00014  *    may be used to endorse or promote products derived from this software
    00015  *    without specific prior written permission.
    00016  *
    00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
    00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
    00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    00027  * SUCH DAMAGE.
    00028  *
    00029  * This file is part of the lwIP TCP/IP stack.
    00030  *
    00031  * Author: Adam Dunkels <adam@sics.se>
    00032  *
    00033  * $Id: tcp_8h-source.html 88 2016-01-12 00:11:29Z reddawg $
    00034  */
    00035 #ifndef __LWIP_TCP_H__
    00036 #define __LWIP_TCP_H__
    00037
    00038 #include "net/sys.h"
    00039 #include "net/mem.h"
    00040
    00041 #include "net/pbuf.h"
    00042 #include "net/opt.h"
    00043 //UBU
    00044 #include "net/ipv4/ip.h"
    00045 //UBU
    00046 #include "net/ipv4/icmp.h"
    00047
    00048 #include "net/sys.h"
    00049
    00050 #include "net/err.h"
    00051
    00052 struct tcp_pcb;
    00053
    00054 /* Functions for interfacing with TCP: */
    00055
    00056 /* Lower layer interface to TCP: */
    00057 void             tcp_init    (void);  /* Must be called first to
    00058                                          initialize TCP. */
    00059 void             tcp_tmr     (void);  /* Must be called every
    00060                                          TCP_TMR_INTERVAL
    00061                                          ms. (Typically 100 ms). */
    00062 /* Application program's interface: */
    00063 struct tcp_pcb * tcp_new     (void);
    00064
    00065 void             tcp_arg     (struct tcp_pcb *pcb, void *arg);
    00066 void             tcp_accept  (struct tcp_pcb *pcb,
    00067                               err_t (* accept)(void *arg, struct tcp_pcb *newpcb,
    00068                                                err_t err));
    00069 void             tcp_recv    (struct tcp_pcb *pcb,
    00070                               err_t (* recv)(void *arg, struct tcp_pcb *tpcb,
    00071                                   struct pbuf *p, err_t err));
    00072 void             tcp_sent    (struct tcp_pcb *pcb,
    00073                               err_t (* sent)(void *arg, struct tcp_pcb *tpcb,
    00074                                              uInt16 len));
    00075 void             tcp_poll    (struct tcp_pcb *pcb,
    00076                               err_t (* poll)(void *arg, struct tcp_pcb *tpcb),
    00077                               uInt8 interval);
    00078 void             tcp_err     (struct tcp_pcb *pcb,
    00079                               void (* err)(void *arg, err_t err));
    00080
    00081 #define          tcp_sndbuf(pcb)   ((pcb)->snd_buf)
    00082
    00083 void             tcp_recved  (struct tcp_pcb *pcb, uInt16 len);
    00084 err_t            tcp_bind    (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
    00085                               uInt16 port);
    00086 err_t            tcp_connect (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
    00087                               uInt16 port, err_t (* connected)(void *arg,
    00088                                                               struct tcp_pcb *tpcb,
    00089                                                               err_t err));
    00090 struct tcp_pcb * tcp_listen  (struct tcp_pcb *pcb);
    00091 void             tcp_abort   (struct tcp_pcb *pcb);
    00092 err_t            tcp_close   (struct tcp_pcb *pcb);
    00093 err_t            tcp_write   (struct tcp_pcb *pcb, const void *dataptr, uInt16 len,
    00094                               uInt8 copy);
    00095
    00096 /* It is also possible to call these two functions at the right
    00097    intervals (instead of calling tcp_tmr()). */
    00098 void             tcp_slowtmr (void);
    00099 void             tcp_fasttmr (void);
    00100
    00101
    00102 /* Only used by IP to pass a TCP segment to TCP: */
    00103 void             tcp_input   (struct pbuf *p, struct netif *inp);
    00104 /* Used within the TCP code only: */
    00105 err_t            tcp_output  (struct tcp_pcb *pcb);
    00106
    00107
    00108
    00109
    00110 #define TCP_SEQ_LT(a,b)     ((Int32)((a)-(b)) < 0)
    00111 #define TCP_SEQ_LEQ(a,b)    ((Int32)((a)-(b)) <= 0)
    00112 #define TCP_SEQ_GT(a,b)     ((Int32)((a)-(b)) > 0)
    00113 #define TCP_SEQ_GEQ(a,b)    ((Int32)((a)-(b)) >= 0)
    00114
    00115 #define TCP_FIN 0x01
    00116 #define TCP_SYN 0x02
    00117 #define TCP_RST 0x04
    00118 #define TCP_PSH 0x08
    00119 #define TCP_ACK 0x10
    00120 #define TCP_URG 0x20
    00121
    00122 /* Length of the TCP header, excluding options. */
    00123 #define TCP_HLEN 20
    00124
    00125 #define TCP_TMR_INTERVAL       100  /* The TCP timer interval in
    00126                                        milliseconds. */
    00127
    00128 #define TCP_FAST_INTERVAL      200  /* the fine grained timeout in
    00129                                        milliseconds */
    00130 #define TCP_SLOW_INTERVAL      500  /* the coarse grained timeout in
    00131                                        milliseconds */
    00132 #define TCP_FIN_WAIT_TIMEOUT 20000 /* milliseconds */
    00133 #define TCP_SYN_RCVD_TIMEOUT 20000 /* milliseconds */
    00134
    00135 #define TCP_OOSEQ_TIMEOUT        6 /* x RTO */
    00136
    00137 #define TCP_MSL 60000  /* The maximum segment lifetime in microseconds */
    00138
    00139 struct tcp_hdr {
    00140   PACK_STRUCT_FIELD(uInt16 src);
    00141   PACK_STRUCT_FIELD(uInt16 dest);
    00142   PACK_STRUCT_FIELD(uInt32 seqno);
    00143   PACK_STRUCT_FIELD(uInt32 ackno);
    00144   PACK_STRUCT_FIELD(uInt16 _offset_flags);
    00145   PACK_STRUCT_FIELD(uInt16 wnd);
    00146   PACK_STRUCT_FIELD(uInt16 chksum);
    00147   PACK_STRUCT_FIELD(uInt16 urgp);
    00148 } PACK_STRUCT_STRUCT;
    00149
    00150 #define TCPH_OFFSET(hdr) (NTOHS((hdr)->_offset_flags) >> 8)
    00151 #define TCPH_FLAGS(hdr) (NTOHS((hdr)->_offset_flags) & 0xff)
    00152
    00153 #define TCPH_OFFSET_SET(hdr, offset) (hdr)->_offset_flags = HTONS(((offset) << 8) | TCPH_FLAGS(hdr))
    00154 #define TCPH_FLAGS_SET(hdr, flags) (hdr)->_offset_flags = HTONS((TCPH_OFFSET(hdr) << 8) | (flags))
    00155
    00156 #define TCP_TCPLEN(seg) ((seg)->len + ((TCPH_FLAGS((seg)->tcphdr) & TCP_FIN || \
    00157                                         TCPH_FLAGS((seg)->tcphdr) & TCP_SYN)? 1: 0))
    00158
    00159 enum tcp_state {
    00160   CLOSED      = 0,
    00161   LISTEN      = 1,
    00162   SYN_SENT    = 2,
    00163   SYN_RCVD    = 3,
    00164   ESTABLISHED = 4,
    00165   FIN_WAIT_1  = 5,
    00166   FIN_WAIT_2  = 6,
    00167   CLOSE_WAIT  = 7,
    00168   CLOSING     = 8,
    00169   LAST_ACK    = 9,
    00170   TIME_WAIT   = 10
    00171 };
    00172
    00173
    00174 /* the TCP protocol control block */
    00175 struct tcp_pcb {
    00176   struct tcp_pcb *next;   /* for the linked list */
    00177
    00178   enum tcp_state state;   /* TCP state */
    00179
    00180   void *callback_arg;
    00181
    00182   /* Function to call when a listener has been connected. */
    00183   err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
    00184
    00185   struct ip_addr local_ip;
    00186   uInt16 local_port;
    00187
    00188   struct ip_addr remote_ip;
    00189   uInt16 remote_port;
    00190
    00191   /* receiver varables */
    00192   uInt32 rcv_nxt;   /* next seqno expected */
    00193   uInt16 rcv_wnd;   /* receiver window */
    00194
    00195   /* Timers */
    00196   uInt16 tmr;
    00197
    00198   /* Retransmission timer. */
    00199   uInt8 rtime;
    00200
    00201   uInt16 mss;   /* maximum segment size */
    00202
    00203   uInt8 flags;
    00204 #define TF_ACK_DELAY 0x01   /* Delayed ACK. */
    00205 #define TF_ACK_NOW   0x02   /* Immediate ACK. */
    00206 #define TF_INFR      0x04   /* In fast recovery. */
    00207 #define TF_RESET     0x08   /* Connection was reset. */
    00208 #define TF_CLOSED    0x10   /* Connection was sucessfully closed. */
    00209 #define TF_GOT_FIN   0x20   /* Connection was closed by the remote end. */
    00210
    00211   /* RTT estimation variables. */
    00212   uInt16 rttest; /* RTT estimate in 500ms ticks */
    00213   uInt32 rtseq;  /* sequence number being timed */
    00214   Int32 sa, sv;
    00215
    00216   uInt16 rto;    /* retransmission time-out */
    00217   uInt8 nrtx;    /* number of retransmissions */
    00218
    00219   /* fast retransmit/recovery */
    00220   uInt32 lastack; /* Highest acknowledged seqno. */
    00221   uInt8 dupacks;
    00222
    00223   /* congestion avoidance/control variables */
    00224   uInt16 cwnd;
    00225   uInt16 ssthresh;
    00226
    00227   /* sender variables */
    00228   uInt32 snd_nxt,       /* next seqno to be sent */
    00229     snd_max,       /* Highest seqno sent. */
    00230     snd_wnd,       /* sender window */
    00231     snd_wl1, snd_wl2,
    00232     snd_lbb;
    00233
    00234   uInt16 snd_buf;   /* Avaliable buffer space for sending. */
    00235   uInt8 snd_queuelen;
    00236
    00237   /* Function to be called when more send buffer space is avaliable. */
    00238   err_t (* sent)(void *arg, struct tcp_pcb *pcb, uInt16 space);
    00239   uInt16 acked;
    00240
    00241   /* Function to be called when (in-sequence) data has arrived. */
    00242   err_t (* recv)(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
    00243   struct pbuf *recv_data;
    00244
    00245   /* Function to be called when a connection has been set up. */
    00246   err_t (* connected)(void *arg, struct tcp_pcb *pcb, err_t err);
    00247
    00248   /* Function which is called periodically. */
    00249   err_t (* poll)(void *arg, struct tcp_pcb *pcb);
    00250
    00251   /* Function to be called whenever a fatal error occurs. */
    00252   void (* errf)(void *arg, err_t err);
    00253
    00254   uInt8 polltmr, pollinterval;
    00255
    00256   /* These are ordered by sequence number: */
    00257   struct tcp_seg *unsent;   /* Unsent (queued) segments. */
    00258   struct tcp_seg *unacked;  /* Sent but unacknowledged segments. */
    00259 #if TCP_QUEUE_OOSEQ
    00260   struct tcp_seg *ooseq;    /* Received out of sequence segments. */
    00261 #endif /* TCP_QUEUE_OOSEQ */
    00262
    00263 };
    00264
    00265 struct tcp_pcb_listen {
    00266   struct tcp_pcb_listen *next;   /* for the linked list */
    00267
    00268   enum tcp_state state;   /* TCP state */
    00269
    00270   void *callback_arg;
    00271
    00272   /* Function to call when a listener has been connected. */
    00273   void (* accept)(void *arg, struct tcp_pcb *newpcb);
    00274
    00275   struct ip_addr local_ip;
    00276   uInt16 local_port;
    00277 };
    00278
    00279 /* This structure is used to repressent TCP segments. */
    00280 struct tcp_seg {
    00281   struct tcp_seg *next;    /* used when putting segements on a queue */
    00282   struct pbuf *p;          /* buffer containing data + TCP header */
    00283   void *dataptr;           /* pointer to the TCP data in the pbuf */
    00284   uInt16 len;               /* the TCP length of this segment */
    00285   struct tcp_hdr *tcphdr;  /* the TCP header */
    00286 };
    00287
    00288 /* Internal functions and global variables: */
    00289 struct tcp_pcb *tcp_pcb_copy(struct tcp_pcb *pcb);
    00290 void tcp_pcb_purge(struct tcp_pcb *pcb);
    00291 void tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb);
    00292
    00293 uInt8 tcp_segs_free(struct tcp_seg *seg);
    00294 uInt8 tcp_seg_free(struct tcp_seg *seg);
    00295 struct tcp_seg *tcp_seg_copy(struct tcp_seg *seg);
    00296
    00297 #define tcp_ack(pcb)     if((pcb)->flags & TF_ACK_DELAY) { \
    00298                             (pcb)->flags |= TF_ACK_NOW; \
    00299                             tcp_output(pcb); \
    00300                          } else { \
    00301                             (pcb)->flags |= TF_ACK_DELAY; \
    00302                          }
    00303
    00304 #define tcp_ack_now(pcb) (pcb)->flags |= TF_ACK_NOW; \
    00305                          tcp_output(pcb)
    00306
    00307 err_t tcp_send_ctrl(struct tcp_pcb *pcb, uInt8 flags);
    00308 err_t tcp_enqueue(struct tcp_pcb *pcb, void *dataptr, uInt16 len,
    00309                 uInt8 flags, uInt8 copy,
    00310                 uInt8 *optdata, uInt8 optlen);
    00311
    00312 void tcp_rexmit_seg(struct tcp_pcb *pcb, struct tcp_seg *seg);
    00313
    00314 void tcp_rst(uInt32 seqno, uInt32 ackno,
    00315              struct ip_addr *local_ip, struct ip_addr *remote_ip,
    00316              uInt16 local_port, uInt16 remote_port);
    00317
    00318 uInt32 tcp_next_iss(void);
    00319
    00320 extern uInt32 tcp_ticks;
    00321
    00322 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
    00323 void tcp_debug_print(struct tcp_hdr *tcphdr);
    00324 void tcp_debug_print_flags(uInt8 flags);
    00325 void tcp_debug_print_state(enum tcp_state s);
    00326 void tcp_debug_print_pcbs(void);
    00327 int tcp_pcbs_sane(void);
    00328 #else
    00329 #define tcp_pcbs_sane() 1
    00330 #endif /* TCP_DEBUG */
    00331
    00332
    00333 /* The TCP PCB lists. */
    00334 extern struct tcp_pcb_listen *tcp_listen_pcbs;  /* List of all TCP PCBs in LISTEN state. */
    00335 extern struct tcp_pcb *tcp_active_pcbs;  /* List of all TCP PCBs that are in a
    00336                                             state in which they accept or send
    00337                                             data. */
    00338 extern struct tcp_pcb *tcp_tw_pcbs;      /* List of all TCP PCBs in TIME-WAIT. */
    00339
    00340 extern struct tcp_pcb *tcp_tmp_pcb;      /* Only used for temporary storage. */
    00341
    00342 /* Axoims about the above lists:
    00343    1) Every TCP PCB that is not CLOSED is in one of the lists.
    00344    2) A PCB is only in one of the lists.
    00345    3) All PCBs in the tcp_listen_pcbs list is in LISTEN state.
    00346    4) All PCBs in the tcp_tw_pcbs list is in TIME-WAIT state.
    00347 */
    00348
    00349 /* Define two macros, TCP_REG and TCP_RMV that registers a TCP PCB
    00350    with a PCB list or removes a PCB from a list, respectively. */
    00351 #ifdef LWIP_DEBUG
    00352 #define TCP_REG(pcbs, npcb) do {\
    00353                             DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %d\n", npcb, npcb->local_port)); \
    00354                             for(tcp_tmp_pcb = *pcbs; \
    00355                                   tcp_tmp_pcb != NULL; \
    00356                                 tcp_tmp_pcb = tcp_tmp_pcb->next) { \
    00357                                 ASSERT("TCP_REG: already registered\n", tcp_tmp_pcb != npcb); \
    00358                             } \
    00359                             ASSERT("TCP_REG: pcb->state != CLOSED", npcb->state != CLOSED); \
    00360                             npcb->next = *pcbs; \
    00361                             ASSERT("TCP_REG: npcb->next != npcb", npcb->next != npcb); \
    00362                             *pcbs = npcb; \
    00363                             ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
    00364                             } while(0)
    00365 #define TCP_RMV(pcbs, npcb) do { \
    00366                             ASSERT("TCP_RMV: pcbs != NULL", *pcbs != NULL); \
    00367                             DEBUGF(TCP_DEBUG, ("TCP_RMV: removing %p from %p\n", npcb, *pcbs)); \
    00368                             if(*pcbs == npcb) { \
    00369                                *pcbs = (*pcbs)->next; \
    00370                             } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
    00371                                if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \
    00372                                   tcp_tmp_pcb->next = npcb->next; \
    00373                                   break; \
    00374                                } \
    00375                             } \
    00376                             npcb->next = NULL; \
    00377                             ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
    00378                             DEBUGF(TCP_DEBUG, ("TCP_RMV: removed %p from %p\n", npcb, *pcbs)); \
    00379                             } while(0)
    00380
    00381 #else /* LWIP_DEBUG */
    00382 #define TCP_REG(pcbs, npcb) do { \
    00383                             npcb->next = *pcbs; \
    00384                             *pcbs = npcb; \
    00385                             } while(0)
    00386 #define TCP_RMV(pcbs, npcb) do { \
    00387                             if(*pcbs == npcb) { \
    00388                                *pcbs = (*pcbs)->next; \
    00389                             } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
    00390                                if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \
    00391                                   tcp_tmp_pcb->next = npcb->next; \
    00392                                   break; \
    00393                                } \
    00394                             } \
    00395                             npcb->next = NULL; \
    00396                             } while(0)
    00397 #endif /* LWIP_DEBUG */
    00398 #endif /* __LWIP_TCP_H__ */
    00399
    00400
    00401