home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1 / HamRadio.cdr / misc / tcpipsrc / tcp.h < prev    next >
C/C++ Source or Header  |  1990-12-09  |  10KB  |  294 lines

  1. #ifndef    _TCP_H
  2. #define    _TCP_H
  3.  
  4. /* TCP implementation. Follows RFC 793 as closely as possible */
  5. #ifndef    _GLOBAL_H
  6. #include "global.h"
  7. #endif
  8.  
  9. #ifndef    _MBUF_H
  10. #include "mbuf.h"
  11. #endif
  12.  
  13. #ifndef    _IFACE_H
  14. #include "iface.h"
  15. #endif
  16.  
  17. #ifndef    _INTERNET_H
  18. #include "internet.h"
  19. #endif
  20.  
  21. #ifndef _IP_H
  22. #include "ip.h"
  23. #endif
  24.  
  25. #ifndef    _NETUSER_H
  26. #include "netuser.h"
  27. #endif
  28.  
  29. #ifndef    _TIMER_H
  30. #include "timer.h"
  31. #endif
  32.  
  33. #define    DEF_WND    2048    /* Default receiver window */
  34. #define    NTCB    19    /* # TCB hash table headers */
  35. #define    RTTCACHE 16    /* # of TCP round-trip-time cache entries */
  36. #define    DEF_MSS    512    /* Default maximum segment size */
  37. #define    DEF_RTT    5000    /* Initial guess at round trip time (5 sec) */
  38. #define    MSL2    30    /* Guess at two maximum-segment lifetimes */
  39. #define    TCP_MAXOPT    40    /* Largest option field, bytes */
  40.  
  41. #define    geniss()    ((int32)msclock() << 12) /* Increment clock at 4 MB/sec */
  42.  
  43. /* Number of consecutive duplicate acks to trigger fast recovery */
  44. #define    TCPDUPACKS    3
  45.  
  46. /* Round trip timing parameters */
  47. #define    AGAIN    8    /* Average RTT gain = 1/8 */
  48. #define    LAGAIN    3    /* Log2(AGAIN) */
  49. #define    DGAIN    4    /* Mean deviation gain = 1/4 */
  50. #define    LDGAIN    2    /* log2(DGAIN) */
  51.  
  52. /* TCP segment header -- internal representation
  53.  * Note that this structure is NOT the actual header as it appears on the
  54.  * network (in particular, the offset and checksum fields are missing).
  55.  * All that knowledge is in the functions ntohtcp() and htontcp() in tcpsubr.c
  56.  */
  57. struct tcp {
  58.     int16 source;    /* Source port */
  59.     int16 dest;    /* Destination port */
  60.     int32 seq;    /* Sequence number */
  61.     int32 ack;    /* Acknowledgment number */
  62.     struct {
  63.         char urg;
  64.         char ack;
  65.         char psh;
  66.         char rst;
  67.         char syn;
  68.         char fin;
  69.     } flags;
  70.     int16 wnd;            /* Receiver flow control window */
  71.     int16 checksum;            /* Checksum */
  72.     int16 up;            /* Urgent pointer */
  73.     int16 mss;            /* Optional max seg size */
  74.     char options[TCP_MAXOPT];    /* Options field */
  75.     int optlen;            /* Length of options field, bytes */
  76. };
  77. /* TCP options */
  78. #define    EOL_KIND    0
  79. #define    NOOP_KIND    1
  80. #define    MSS_KIND    2
  81.  
  82. #define    TCPLEN        20
  83. #define    MSS_LENGTH    4
  84. /* Resequencing queue entry */
  85. struct reseq {
  86.     struct reseq *next;    /* Linked-list pointer */
  87.     struct tcp seg;        /* TCP header */
  88.     struct mbuf *bp;    /* data */
  89.     int16 length;        /* data length */
  90.     char tos;        /* Type of service */
  91. };
  92. #define    NULLRESEQ    (struct reseq *)0
  93.  
  94. /* TCP connection control block */
  95. struct tcb {
  96.     struct tcb *prev;    /* Linked list pointers for hash table */
  97.     struct tcb *next;
  98.  
  99.     struct connection conn;
  100.  
  101.     char state;    /* Connection state */
  102.  
  103. /* These numbers match those defined in the MIB for TCP connection state */
  104. #define    TCP_CLOSED        1
  105. #define    TCP_LISTEN        2
  106. #define    TCP_SYN_SENT        3
  107. #define    TCP_SYN_RECEIVED    4
  108. #define    TCP_ESTABLISHED        5
  109. #define    TCP_FINWAIT1        6
  110. #define    TCP_FINWAIT2        7
  111. #define    TCP_CLOSE_WAIT        8
  112. #define    TCP_LAST_ACK        9
  113. #define    TCP_CLOSING        10
  114. #define    TCP_TIME_WAIT        11
  115.  
  116.     char reason;        /* Reason for closing */
  117. #define    NORMAL        0    /* Normal close */
  118. #define    RESET        1    /* Reset by other end */
  119. #define    TIMEOUT        2    /* Excessive retransmissions */
  120. #define    NETWORK        3    /* Network problem (ICMP message) */
  121.  
  122. /* If reason == NETWORK, the ICMP type and code values are stored here */
  123.     char type;
  124.     char code;
  125.  
  126.     /* Send sequence variables */
  127.     struct {
  128.         int32 una;    /* First unacknowledged sequence number */
  129.         int32 nxt;    /* Next sequence num to be sent for the first time */
  130.         int32 ptr;    /* Working transmission pointer */
  131.         int32 wl1;    /* Sequence number used for last window update */
  132.         int32 wl2;    /* Ack number used for last window update */
  133.         int16 wnd;    /* Other end's offered receive window */
  134.         int16 up;    /* Send urgent pointer */
  135.     } snd;
  136.     int32 iss;        /* Initial send sequence number */
  137.     int32 resent;        /* Count of bytes retransmitted */
  138.     int16 cwind;        /* Congestion window */
  139.     int16 ssthresh;        /* Slow-start threshold */
  140.     int dupacks;        /* Count of duplicate (do-nothing) ACKs */
  141.  
  142.     /* Receive sequence variables */
  143.     struct {
  144.         int32 nxt;    /* Incoming sequence number expected next */
  145.         int16 wnd;    /* Our offered receive window */
  146.         int16 up;    /* Receive urgent pointer */
  147.     } rcv;
  148.     int32 irs;        /* Initial receive sequence number */
  149.     int32 rerecv;        /* Count of duplicate bytes received */
  150.     int16 mss;        /* Maximum segment size */
  151.  
  152.     int16 window;        /* Receiver window and send queue limit */
  153.  
  154.     void (*r_upcall) __ARGS((struct tcb *tcb,int cnt));
  155.         /* Call when "significant" amount of data arrives */
  156.     void (*t_upcall) __ARGS((struct tcb *tcb,int cnt));
  157.         /* Call when ok to send more data */
  158.     void (*s_upcall) __ARGS((struct tcb *tcb,int old,int new));
  159.         /* Call when connection state changes */
  160.     struct {        /* Control flags */
  161.         char force;    /* We owe the other end an ACK or window update */
  162.         char clone;    /* Server-type TCB, cloned on incoming SYN */
  163.         char retran;    /* A retransmission has occurred */
  164.         char active;    /* TCB created with an active open */
  165.         char synack;    /* Our SYN has been acked */
  166.         char rtt_run;    /* We're timing a segment */
  167.     } flags;
  168.     char tos;        /* Type of service (for IP) */
  169.     int backoff;        /* Backoff interval */
  170.  
  171.     struct mbuf *rcvq;    /* Receive queue */
  172.     struct mbuf *sndq;    /* Send queue */
  173.     int16 rcvcnt;        /* Count of items on rcvq */
  174.     int16 sndcnt;        /* Number of unacknowledged sequence numbers on
  175.                  * sndq. NB: includes SYN and FIN, which don't
  176.                  * actually appear on sndq!
  177.                  */
  178.  
  179.     struct reseq *reseq;    /* Out-of-order segment queue */
  180.     struct timer timer;    /* Retransmission timer */
  181.     int32 rtt_time;        /* Stored clock values for RTT */
  182.     int32 rttseq;        /* Sequence number being timed */
  183.     int32 srtt;        /* Smoothed round trip time, milliseconds */
  184.     int32 mdev;        /* Mean deviation, milliseconds */
  185.     int32 lastactive;    /* Clock time when xmtr last active */
  186.  
  187.     int user;        /* User parameter (e.g., for mapping to an
  188.                  * application control block
  189.                  */
  190. };
  191. #define    NULLTCB    (struct tcb *)0
  192. /* TCP round-trip time cache */
  193. struct tcp_rtt {
  194.     int32 addr;        /* Destination IP address */
  195.     int32 srtt;        /* Most recent SRTT */
  196.     int32 mdev;        /* Most recent mean deviation */
  197. };
  198. #define    NULLRTT    (struct tcp_rtt *)0
  199. extern struct tcp_rtt Tcp_rtt[];
  200.  
  201. /* TCP statistics counters */
  202. struct tcp_stat {
  203.     int16 runt;        /* Smaller than minimum size */
  204.     int16 checksum;        /* TCP header checksum errors */
  205.     int16 conout;        /* Outgoing connection attempts */
  206.     int16 conin;        /* Incoming connection attempts */
  207.     int16 resets;        /* Resets generated */
  208.     int16 bdcsts;        /* Bogus broadcast packets */
  209. };
  210. extern struct mib_entry Tcp_mib[];
  211. #define    tcpRtoAlgorithm    Tcp_mib[1].value.integer
  212. #define    tcpRtoMin    Tcp_mib[2].value.integer
  213. #define    tcpRtoMax    Tcp_mib[3].value.integer
  214. #define    tcpMaxConn    Tcp_mib[4].value.integer
  215. #define    tcpActiveOpens    Tcp_mib[5].value.integer
  216. #define tcpPassiveOpens    Tcp_mib[6].value.integer
  217. #define    tcpAttemptFails    Tcp_mib[7].value.integer
  218. #define    tcpEstabResets    Tcp_mib[8].value.integer
  219. #define    tcpCurrEstab    Tcp_mib[9].value.integer
  220. #define    tcpInSegs    Tcp_mib[10].value.integer
  221. #define    tcpOutSegs    Tcp_mib[11].value.integer
  222. #define    tcpRetransSegs    Tcp_mib[12].value.integer
  223. #define    tcpInErrs    Tcp_mib[14].value.integer
  224. #define    tcpOutRsts    Tcp_mib[15].value.integer
  225. #define    NUMTCPMIB    15
  226.  
  227. extern struct tcb *Tcbs[];
  228. extern int16 Tcp_mss;
  229. extern int16 Tcp_window;
  230. extern int32 Tcp_irtt;
  231. extern int Tcp_trace;
  232. extern int Tcp_syndata;
  233. extern char *Tcpstates[];
  234. extern char *Tcpreasons[];
  235.  
  236. /* In tcpcmd.c: */
  237. void st_tcp __ARGS((struct tcb *tcb));
  238.  
  239. /* In tcphdr.c: */
  240. struct mbuf *htontcp __ARGS((struct tcp *tcph,struct mbuf *data,
  241.     struct pseudo_header *ph));
  242. int ntohtcp __ARGS((struct tcp *tcph,struct mbuf **bpp));
  243.  
  244. /* In tcpin.c: */
  245. void reset __ARGS((struct ip *ip,struct tcp *seg));
  246. void send_syn __ARGS((struct tcb *tcb));
  247. void tcp_input __ARGS((struct iface *iface,struct ip *ip,struct mbuf *bp,
  248.     int rxbroadcast));
  249. void tcp_icmp __ARGS((int32 icsource,int32 source,int32 dest,
  250.     char type,char code,struct mbuf **bpp));
  251.  
  252. /* In tcpsubr.c: */
  253. void close_self __ARGS((struct tcb *tcb,int reason));
  254. struct tcb *create_tcb __ARGS((struct connection *conn));
  255. void link_tcb __ARGS((struct tcb *tcb));
  256. struct tcb *lookup_tcb __ARGS((struct connection *conn));
  257. void rtt_add __ARGS((int32 addr,int32 rtt));
  258. struct tcp_rtt *rtt_get __ARGS((int32 addr));
  259. int seq_ge __ARGS((int32 x,int32 y));
  260. int seq_gt __ARGS((int32 x,int32 y));
  261. int seq_le __ARGS((int32 x,int32 y));
  262. int seq_lt __ARGS((int32 x,int32 y));
  263. int seq_within __ARGS((int32 x,int32 low,int32 high));
  264. void setstate __ARGS((struct tcb *tcb,int newstate));
  265. void unlink_tcb __ARGS((struct tcb *tcb));
  266. void tcp_garbage __ARGS((int red));
  267.  
  268. /* In tcpout.c: */
  269. void tcp_output __ARGS((struct tcb *tcb));
  270.  
  271. /* In tcptimer.c: */
  272. int32 backoff __ARGS((int n));
  273. void tcp_timeout __ARGS((void *p));
  274.  
  275. /* In tcpuser.c: */
  276. int close_tcp __ARGS((struct tcb *tcb));
  277. int del_tcp __ARGS((struct tcb *tcb));
  278. int kick __ARGS((int32 addr));
  279. int kick_tcp __ARGS((struct tcb *tcb));
  280. struct tcb *open_tcp __ARGS((struct socket *lsocket,struct socket *fsocket,
  281.     int mode,int16 window,
  282.     void (*r_upcall) __ARGS((struct tcb *tcb,int cnt)),
  283.     void (*t_upcall) __ARGS((struct tcb *tcb,int cnt)),
  284.     void (*s_upcall) __ARGS((struct tcb *tcb,int old,int new)),
  285.     int tos,int user));
  286. int recv_tcp __ARGS((struct tcb *tcb,struct mbuf **bpp,int16 cnt));
  287. void reset_all __ARGS((void));
  288. void reset_tcp __ARGS((struct tcb *tcb));
  289. int send_tcp __ARGS((struct tcb *tcb,struct mbuf *bp));
  290. char *tcp_port __ARGS((int16 n));
  291. int tcpval __ARGS((struct tcb *tcb));
  292.  
  293. #endif    /* _TCP_H */
  294.