home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / TCP.H < prev    next >
C/C++ Source or Header  |  1994-04-17  |  11KB  |  327 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_MSS 512 /* Default maximum segment size */
  34. #define DEF_WND 2048    /* Default receiver window */
  35. #define RTTCACHE 16 /* # of TCP round-trip-time cache entries */
  36. #define DEF_RETRIES 10   /* default number of retries before resetting tcb */
  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 MIN_RTO 500L    /* Minimum timeout, milliseconds */
  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 field is missing).
  55.  * All that knowledge is in the functions ntohtcp() and htontcp() in tcpsubr.c
  56.  */
  57. #define TCPLEN      20  /* Minimum Header length, bytes */
  58. #define TCP_MAXOPT  40  /* Largest option field, bytes */
  59. struct tcp {
  60.     int16 source;   /* Source port */
  61.     int16 dest; /* Destination port */
  62.     int32 seq;  /* Sequence number */
  63.     int32 ack;  /* Acknowledgment number */
  64.     int16 wnd;          /* Receiver flow control window */
  65.     int16 checksum;         /* Checksum */
  66.     int16 up;           /* Urgent pointer */
  67.     int16 mss;          /* Optional max seg size */
  68.     struct {
  69.         char congest;   /* Echoed IP congestion experienced bit */
  70.         char urg;
  71.         char ack;
  72.         char psh;
  73.         char rst;
  74.         char syn;
  75.         char fin;
  76.     } flags;
  77.     char optlen;            /* Length of options field, bytes */
  78.     char options[TCP_MAXOPT];   /* Options field */
  79. };
  80. /* TCP options */
  81. #define EOL_KIND    0
  82. #define NOOP_KIND   1
  83. #define MSS_KIND    2
  84. #define MSS_LENGTH  4
  85.   
  86. /* Resequencing queue entry */
  87. struct reseq {
  88.     struct reseq *next; /* Linked-list pointer */
  89.     struct tcp seg;     /* TCP header */
  90.     struct mbuf *bp;    /* data */
  91.     int16 length;       /* data length */
  92.     char tos;       /* Type of service */
  93. };
  94. #define NULLRESEQ   (struct reseq *)0
  95.   
  96. /* interface dependent tcp parameters */
  97. struct iftcp {
  98.     int32 irtt;
  99.     int32 maxwait;
  100.     int16 window;
  101.     int16 mss;
  102.     int blimit;
  103.     int retries;
  104.     int timertype;
  105.     int syndata;
  106. };
  107. extern struct iftcp def_iftcp;
  108.   
  109. /* TCP connection control block */
  110. struct tcb {
  111.     struct tcb *next;   /* Linked list pointer */
  112.   
  113.     struct connection conn;
  114.   
  115.     char state; /* Connection state */
  116.   
  117. /* These numbers match those defined in the MIB for TCP connection state */
  118. #define TCP_CLOSED      1
  119. #define TCP_LISTEN      2
  120. #define TCP_SYN_SENT        3
  121. #define TCP_SYN_RECEIVED    4
  122. #define TCP_ESTABLISHED     5
  123. #define TCP_FINWAIT1        6
  124. #define TCP_FINWAIT2        7
  125. #define TCP_CLOSE_WAIT      8
  126. #define TCP_LAST_ACK        9
  127. #define TCP_CLOSING     10
  128. #define TCP_TIME_WAIT       11
  129.   
  130.     char reason;        /* Reason for closing */
  131. #define NORMAL      0   /* Normal close */
  132. #define RESET       1   /* Reset by other end */
  133. #define TIMEOUT     2   /* Excessive retransmissions */
  134. #define NETWORK     3   /* Network problem (ICMP message) */
  135.   
  136. /* If reason == NETWORK, the ICMP type and code values are stored here */
  137.     char type;
  138.     char code;
  139.   
  140.     /* Send sequence variables */
  141.     struct {
  142.         int32 una;  /* First unacknowledged sequence number */
  143.         int32 nxt;  /* Next sequence num to be sent for the first time */
  144.         int32 ptr;  /* Working transmission pointer */
  145.         int32 wl1;  /* Sequence number used for last window update */
  146.         int32 wl2;  /* Ack number used for last window update */
  147.         int16 wnd;  /* Other end's offered receive window */
  148.         int16 up;   /* Send urgent pointer */
  149.     } snd;
  150.     int32 iss;      /* Initial send sequence number */
  151.     int32 resent;       /* Count of bytes retransmitted */
  152.     int16 cwind;        /* Congestion window */
  153.     int16 ssthresh;     /* Slow-start threshold */
  154.     int dupacks;        /* Count of duplicate (do-nothing) ACKs */
  155.   
  156.     /* Receive sequence variables */
  157.     struct {
  158.         int32 nxt;  /* Incoming sequence number expected next */
  159.         int16 wnd;  /* Our offered receive window */
  160.         int16 up;   /* Receive urgent pointer */
  161.     } rcv;
  162.     int32 irs;      /* Initial receive sequence number */
  163.     int32 rerecv;       /* Count of duplicate bytes received */
  164.     int16 mss;      /* Maximum segment size */
  165.   
  166.     int16 window;       /* Receiver window and send queue limit */
  167.     int16 limit;        /* Send queue limit */
  168.   
  169.     void (*r_upcall) __ARGS((struct tcb *tcb,int cnt));
  170.         /* Call when "significant" amount of data arrives */
  171.     void (*t_upcall) __ARGS((struct tcb *tcb,int cnt));
  172.         /* Call when ok to send more data */
  173.     void (*s_upcall) __ARGS((struct tcb *tcb,int old,int new));
  174.         /* Call when connection state changes */
  175.     struct {        /* Control flags */
  176.         char force; /* We owe the other end an ACK or window update */
  177.         char clone; /* Server-type TCB, cloned on incoming SYN */
  178.         char retran;    /* A retransmission has occurred */
  179.         char active;    /* TCB created with an active open */
  180.         char synack;    /* Our SYN has been acked */
  181.         char rtt_run;   /* We're timing a segment */
  182.         char congest;   /* Copy of last IP congest bit received */
  183.     } flags;
  184.     char tos;       /* Type of service (for IP) */
  185.     int backoff;        /* Backoff interval */
  186.   
  187.     struct mbuf *rcvq;  /* Receive queue */
  188.     struct mbuf *sndq;  /* Send queue */
  189.     int16 rcvcnt;       /* Count of items on rcvq */
  190.     int16 sndcnt;       /* Number of unacknowledged sequence numbers on
  191.                  * sndq. NB: includes SYN and FIN, which don't
  192.                  * actually appear on sndq!
  193.                  */
  194.   
  195.     struct reseq *reseq;    /* Out-of-order segment queue */
  196.     struct timer timer; /* Retransmission timer */
  197.     int32 rtt_time;     /* Stored clock values for RTT */
  198.     int32 rttseq;       /* Sequence number being timed */
  199.     int32 srtt;     /* Smoothed round trip time, milliseconds */
  200.     int32 mdev;     /* Mean deviation, milliseconds */
  201.     int32 lastactive;   /* Clock time when xmtr last active */
  202.   
  203.     int user;       /* User parameter (e.g., for mapping to an
  204.                  * application control block
  205.                  */
  206.     struct iftcp *parms; /* Connection/Interface specific parameters */
  207. };
  208. #define NULLTCB (struct tcb *)0
  209. /* TCP round-trip time cache */
  210. struct tcp_rtt {
  211.     int32 addr;     /* Destination IP address */
  212.     int32 srtt;     /* Most recent SRTT */
  213.     int32 mdev;     /* Most recent mean deviation */
  214. };
  215. #define NULLRTT (struct tcp_rtt *)0
  216. extern struct tcp_rtt Tcp_rtt[];
  217.   
  218. /* TCP statistics counters */
  219. struct tcp_stat {
  220.     int16 runt;     /* Smaller than minimum size */
  221.     int16 checksum;     /* TCP header checksum errors */
  222.     int16 conout;       /* Outgoing connection attempts */
  223.     int16 conin;        /* Incoming connection attempts */
  224.     int16 resets;       /* Resets generated */
  225.     int16 bdcsts;       /* Bogus broadcast packets */
  226. };
  227. extern struct mib_entry Tcp_mib[];
  228. #define tcpRtoAlgorithm Tcp_mib[1].value.integer
  229. #define tcpRtoMin   Tcp_mib[2].value.integer
  230. #define tcpRtoMax   Tcp_mib[3].value.integer
  231. #define tcpMaxConn  Tcp_mib[4].value.integer
  232. #define tcpActiveOpens  Tcp_mib[5].value.integer
  233. #define tcpPassiveOpens Tcp_mib[6].value.integer
  234. #define tcpAttemptFails Tcp_mib[7].value.integer
  235. #define tcpEstabResets  Tcp_mib[8].value.integer
  236. #define tcpCurrEstab    Tcp_mib[9].value.integer
  237. #define tcpInSegs   Tcp_mib[10].value.integer
  238. #define tcpOutSegs  Tcp_mib[11].value.integer
  239. #define tcpRetransSegs  Tcp_mib[12].value.integer
  240. #define tcpInErrs   Tcp_mib[14].value.integer
  241. #define tcpOutRsts  Tcp_mib[15].value.integer
  242. #define NUMTCPMIB   15
  243.   
  244. extern struct tcb *Tcbs;
  245. extern char *Tcpstates[];
  246. extern char *Tcpreasons[];
  247.   
  248. extern struct rtaccess *TCPaccess;
  249.   
  250. /* In tcpcmd.c: */
  251. extern int32 Tcp_irtt;
  252. extern int16 Tcp_limit;
  253. extern int16 Tcp_mss;
  254. extern int Tcp_retries;
  255. extern int Tcp_syndata;
  256. extern int Tcp_trace;
  257. extern int16 Tcp_window;
  258.   
  259. void st_tcp __ARGS((struct tcb *tcb));
  260. void init_iftcp(struct iftcp *);
  261. void init_tcbparms(struct iface *,struct iftcp *);
  262. int doiftcp __ARGS((int argc,char *argv[],void *p));
  263.   
  264. /* In tcphdr.c: */
  265. struct mbuf *htontcp __ARGS((struct tcp *tcph,struct mbuf *data,
  266. struct pseudo_header *ph));
  267. int ntohtcp __ARGS((struct tcp *tcph,struct mbuf **bpp));
  268.   
  269. /* In tcpin.c: */
  270. void reset __ARGS((struct ip *ip,struct tcp *seg));
  271. void send_syn __ARGS((struct tcb *tcb));
  272. void tcp_input __ARGS((struct iface *iface,struct ip *ip,struct mbuf *bp,
  273. int rxbroadcast));
  274. void tcp_icmp __ARGS((int32 icsource,int32 source,int32 dest,
  275. char type,char code,struct mbuf **bpp));
  276.   
  277. /* In tcpsubr.c: */
  278. void close_self __ARGS((struct tcb *tcb,int reason));
  279. struct tcb *create_tcb __ARGS((struct connection *conn));
  280. struct tcb *lookup_tcb __ARGS((struct connection *conn));
  281. void rtt_add __ARGS((int32 addr,int32 rtt));
  282. struct tcp_rtt *rtt_get __ARGS((int32 addr));
  283. int seq_ge __ARGS((int32 x,int32 y));
  284. int seq_gt __ARGS((int32 x,int32 y));
  285. int seq_le __ARGS((int32 x,int32 y));
  286. int seq_lt __ARGS((int32 x,int32 y));
  287. int seq_within __ARGS((int32 x,int32 low,int32 high));
  288. #ifdef LINUX
  289. #ifdef setstate
  290. #undef setstate
  291. #endif
  292. #define setstate j_setstate
  293. #endif
  294. void setstate __ARGS((struct tcb *tcb,int newstate));
  295. void tcp_garbage __ARGS((int red));
  296.   
  297. /* In tcpout.c: */
  298. void tcp_output __ARGS((struct tcb *tcb));
  299.   
  300. /* In tcptimer.c: */
  301. int32 backoff __ARGS((struct tcb *tcb));
  302. void tcp_timeout __ARGS((void *p));
  303.   
  304. /* In tcpuser.c: */
  305. void set_irtt __ARGS((struct tcb *tcb));
  306. int close_tcp __ARGS((struct tcb *tcb));
  307. int del_tcp __ARGS((struct tcb *tcb));
  308. int kick __ARGS((int32 addr));
  309. int kick_tcp __ARGS((struct tcb *tcb));
  310. struct tcb *open_tcp __ARGS((struct socket *lsocket,struct socket *fsocket,
  311. int mode,int16 window,
  312. void (*r_upcall) __ARGS((struct tcb *tcb,int cnt)),
  313. void (*t_upcall) __ARGS((struct tcb *tcb,int cnt)),
  314. void (*s_upcall) __ARGS((struct tcb *tcb,int old,int new)),
  315. int tos,int user));
  316. int recv_tcp __ARGS((struct tcb *tcb,struct mbuf **bpp,int16 cnt));
  317. void reset_all __ARGS((void));
  318. void reset_tcp __ARGS((struct tcb *tcb));
  319. int send_tcp __ARGS((struct tcb *tcb,struct mbuf *bp));
  320. char *tcp_port __ARGS((int16 n));
  321. int tcpval __ARGS((struct tcb *tcb));
  322.   
  323. /* In tcpcmd.c */
  324. int tcp_check __ARGS((struct rtaccess *,int32,int16 port));
  325.   
  326. #endif  /* _TCP_H */
  327.