home *** CD-ROM | disk | FTP | other *** search
/ GRIPS 2: Government Rast…rocessing Software & Data / GRIPS_2.cdr / dos / ncsa_tel / tel_2_2_ / source / protocol.h < prev    next >
C/C++ Source or Header  |  1988-07-15  |  11KB  |  397 lines

  1. /*
  2. *    Protocol structures for network communication
  3. *
  4. ****************************************************************************
  5. *                                                                          *
  6. *      part of:                                                            *
  7. *      TCP/IP kernel for NCSA Telnet                                       *
  8. *      by Tim Krauskopf                                                    *
  9. *                                                                          *
  10. *      National Center for Supercomputing Applications                     *
  11. *      152 Computing Applications Building                                 *
  12. *      605 E. Springfield Ave.                                             *
  13. *      Champaign, IL  61820                                                *
  14. *                                                                          *
  15. ****************************************************************************
  16. *
  17. *  This file contains the structure definitions for each type of 
  18. *  protocol that this program wishes to handle.  A companion file,
  19. *  'protinit.c' initializes sample versions of each type of header, 
  20. *  improving the efficiency of sending packets with constants in most
  21. *  of the fields.
  22. */
  23.  
  24. #include "whatami.h"
  25.  
  26. /************************************************************************/
  27. /*  Ethernet frames
  28. *      All Ethernet transmissions should use this Ethernet header which
  29. *   denotes what type of packet is being sent.
  30. *
  31. *   The header is 14 bytes.  The first 6 bytes are the target's hardware
  32. *   Ethernet address, the second 6 are the sender's hardware address and
  33. *   the last two bytes are the packet type.  Some packet type definitions
  34. *   are included here.
  35. *
  36. *   the two-byte packet type is byte-swapped, PC is lo-hi, Ether is hi-lo
  37. */
  38.  
  39.  
  40. #ifdef PC
  41. #define  EXNS  0x0006           /* probably need swapping */
  42. #define  EIP   0x0008
  43. #define  EARP  0x0608
  44. #define  ERARP    0x3580            /* I guess this is RARP */
  45. #define  ECHAOS  0x0408
  46. #else
  47. #define  EXNS  0x0600           /* these don't need swapping */
  48. #define  EIP   0x0800
  49. #define  EARP  0x0806
  50. #define  ERARP    0x8035
  51. #define  ECHAOS  0x0804
  52. #endif
  53.  
  54. struct ether {
  55.     uint8 
  56.         dest[DADDLEN],                /* where the packet is going */
  57.         me[DADDLEN];                /* who am i to send this packet */
  58.  
  59.     uint16 
  60.         type;                        /* Ethernet packet type  */
  61. };
  62.  
  63. typedef struct ether DLAYER;
  64.  
  65.  
  66. /*************************************************************************/
  67. /*  Dave Plummer's  Address Resolution Protocol (ARP) (RFC-826) and 
  68. *   Finlayson, Mann, Mogul and Theimer's Reverse ARP packets.
  69. *
  70. *   Note that the 2 byte ints are byte-swapped.  The protocols calls for
  71. *   in-order bytes, and the PC is lo-hi ordered.
  72. *   
  73. */
  74.  
  75. #define RARPR    0x0004          /*  RARP reply, from host, needs swap */
  76. #define RARPQ    0x0003            /*  RARP request, needs swapping */
  77. #define ARPREP  0x0002          /*  reply, byte swapped when used */
  78. #define ARPREQ  0x0001          /*  request, byte-swapped when used */
  79. #define ARPPRO    0x0800            /*  IP protocol, needs swapping */
  80.  
  81. #define HTYPE     0x0001            /*  Ethernet hardware type, needs swapping */
  82.  
  83. struct plummer {
  84.     DLAYER d;                 /* data link layer packet header */
  85.  
  86.     uint16
  87.             hrd,            /* hardware type, Ethernet = 1 */
  88.             pro;            /* protocol type to resolve for */
  89.     uint8    
  90.             hln,            /* byte length of hardware addr = 6 for ETNET */
  91.             pln;            /* byte length of protocol = 4 for IP */
  92.     uint16
  93.             op;            /* opcode, request = 1, reply = 2, RARP = 3,4 */
  94.     uint8
  95.             sha[DADDLEN],
  96.             spa[4],
  97.             tha[DADDLEN],
  98.             tpa[4];
  99. /*
  100. *   the final four fields (contained in 'rest') are:
  101. *      sender hardware address:   sha       hln bytes
  102. *      sender protocol address:   spa       pln bytes
  103. *      target hardware address:   tha       hln bytes
  104. *      target protocol address:   tpa       pln bytes
  105. */
  106. };
  107.  
  108. typedef struct plummer ARPKT;
  109.  
  110. /***********************************************************************/
  111. /*  ARP cache
  112. *   Data structure for saving low-level information until needed
  113. */
  114. struct acache {
  115.     uint8
  116.         hrd[DADDLEN],            /* hardware address for this IP address */
  117.         ip[4],                    /* the IP # in question */
  118.         gate;                    /* is this a gateway? */
  119.     int32 
  120.         tm;                        /* time information */
  121. };
  122.  
  123. /***********************************************************************/
  124. /*   Internet protocol
  125. *
  126. */
  127. struct iph {
  128.  
  129.     uint8
  130.         versionandhdrlen;
  131.                             /* I prefer to OR them myself */
  132.                             /* each half is four bits */
  133.     uint8 
  134.         service;            /* type of service for IP */
  135.     uint16
  136.         tlen,                /* total length of IP packet */
  137.         ident,                /* these are all BYTE-SWAPPED! */
  138.         frags;                /* combination of flags and value */
  139.  
  140.     uint8
  141.         ttl,                /* time to live */
  142.         protocol;            /* higher level protocol type */
  143.  
  144.     uint16
  145.         check;                /* header checksum, byte-swapped */
  146.  
  147.     uint8 
  148.         ipsource[4],        /* IP addresses */
  149.         ipdest[4];
  150.  
  151. };
  152.  
  153. typedef struct iph IPLAYER;
  154.  
  155. /*  
  156. *  full IP packet, with data and ip header
  157. */
  158.  
  159. struct ip {
  160.     DLAYER d;
  161.     IPLAYER i;
  162.  
  163.     union {
  164.         uint8 
  165.             data[536];            /* largest recommended, may include options */
  166.         uint8
  167.             options[40];
  168.     } x;
  169. };
  170.  
  171. typedef struct ip IPKT;
  172.  
  173. #define PROTUDP        17
  174. #define PROTTCP        6        /* standard protocol types for IP */
  175. #define PROTICMP    1
  176. /************************************************************************/
  177. /* ICMP packet
  178. *  all of them are of a similar form, some generic fields are spec'd here.
  179. */
  180. struct icmph {
  181.     uint8
  182.         type,                /* ICMP type field */
  183.         code;                /* ICMP code field */
  184.     uint16 
  185.         check,              /* checksum */
  186.         part1,part2;        /* depends on type and code */
  187. };
  188.  
  189. typedef struct icmph ICMPLAYER;
  190.  
  191. struct icmp {
  192.     DLAYER d;
  193.     IPLAYER i;
  194.     ICMPLAYER c;
  195.     uint8 
  196.         data[ICMPMAX];
  197. };
  198.  
  199. typedef struct icmp ICMPKT;
  200.  
  201. /**************************************************************************/
  202. /*  TCP protocol
  203. *      define both headers required and create a data type for a typical
  204. *      outgoing TCP packet (with IP header)
  205. *   
  206. *  Note:  So far, there is no way to handle IP options fields
  207. *    which are associated with a TCP packet.  They are mutually exclusive
  208. *    for both receiving and sending.  Support may be added later.
  209. *
  210. *   The tcph and iph structures can be included in many different types of
  211. *   arbitrary data structures and will be the basis for generic send and
  212. *   receive subroutines later.  For now, the packet structures are optimized 
  213. *   for packets with no options fields.  (seems to be almost all of them from
  214. *   what I've observed.
  215. */
  216.  
  217. struct tcph {
  218.     uint16 
  219.         source,dest;            /* TCP port numbers, all byte-swapped */
  220.     uint32 
  221.         seq,ack;                /* sequence, ACK numbers */
  222.     uint8
  223.         hlen,                        /* length of TCP header in 4 byte words */
  224.         flags;                    /* flag fields */
  225.     uint16
  226.         window,                    /* advertised window, byte-swapped */
  227.         check,                    /* TCP checksum of whole packet */
  228.         urgent;                    /* urgent pointer, when flag is set */
  229. };
  230.  
  231. typedef struct tcph TCPLAYER;
  232.  
  233. /*
  234. *  used for computing checksums in TCP
  235. */
  236. struct pseudotcp {
  237.     uint8 
  238.         source[4],dest[4],        /* IP #'s for source,dest */
  239.         z,proto;                /* zero and protocol number */
  240.     uint16 
  241.         tcplen;                    /* byte-swapped length field */
  242. };
  243.  
  244. struct tcp {
  245.     DLAYER d;
  246.     IPLAYER i;
  247.     TCPLAYER t;
  248.  
  249.     union {
  250.         uint8 
  251.             options[40];        /* not very likely, except on SYN */
  252.         uint8 
  253.             data[TMAXSIZE];    /* largest TCP data we will use */
  254.     } x;
  255. };
  256.  
  257. typedef struct tcp TCPKT;
  258.  
  259. /* 
  260. *  flag field definitions, first two bits undefined
  261. */
  262.  
  263. #define TURG    0x20
  264. #define TACK    0x10
  265. #define TPUSH    0x08
  266. #define TRESET    0x04
  267. #define TSYN    0x02
  268. #define TFIN    0x01
  269.  
  270. /*************************************************************************/
  271. /*   TCP queuing
  272. *   data types for all TCP queuing operations
  273. *   Each open port will have one of these structures assigned to it.
  274. */
  275.  
  276. struct window {
  277.     uint32    
  278.         nxt,                /* sequence number, not byte-swapped */
  279.         ack;                /* what the other machine acked */
  280.     int32
  281.         lasttime;            /* (signed) used for timeout checking */
  282.     uint8
  283.         where[WINDOWSIZE],    /* storage for queue */
  284.         *endbuf,            /* set to end of queue */
  285.         *base,                /* where useful data is in queue */
  286.         *endlim,            /* first spot in queue to add more data */
  287.         push;                /* flag for TCP push */
  288.     uint
  289.         size,                /* size of window advertised */
  290.         port,                /* port numbers from one host or another */
  291.         contain;            /* how many bytes in queue? */
  292. };
  293.  
  294. struct port {
  295.     struct window in,out;
  296.     TCPKT tcpout;                /* pre-initialized as much as possible */
  297.     uint8 state;                /* connection state */
  298.     struct pseudotcp tcps;        /* pseudo-tcp for checksumming */
  299.     int
  300.         credit,                    /* choked-down window for fast hosts */
  301.         sendsize,                /* MTU value for this connection */
  302.         rto;                    /* retrans timeout */
  303. };
  304.  
  305.  
  306. /*************************************************************************/
  307. /*  TCP states
  308. *     each connection has an associated state in the connection flow.
  309. *     the order of the states now matters, those less than a certain
  310. *     number are the "inactive" states.
  311. */
  312. #define SCLOSED 1
  313. #define SLISTEN 2
  314. #define STWAIT    3
  315. #define SSYNR   4
  316. #define SSYNS    5
  317. #define SEST    6
  318. #define SCWAIT    10
  319. #define SFW1    7
  320. #define SFW2    8
  321. #define SCLOSING 9
  322. #define SLAST    11
  323.  
  324. /*
  325. *     services which we will want to use
  326. */
  327. #define HFTP    21
  328. #define HTELNET    23
  329. #define HNAME    42
  330. #define HSUNRPC    111
  331. #define HPRINTER 515
  332.  
  333. /*************************************************************************/
  334. /*  UDP
  335. *   User Datagram Protocol
  336. *   Each packet is an independent datagram, no sequencing information
  337. *
  338. *   UDP uses the identical checksum to TCP
  339. */
  340.  
  341. struct udph {
  342.     uint16
  343.         source,dest;        /* port numbers, all byte-swapped */
  344.     uint16
  345.         length,                /* length of packet, including hdr */
  346.         check;                /* TCP checksum of whole packet */
  347. };
  348.  
  349. typedef struct udph UDPLAYER;
  350.  
  351. struct udp {
  352.     DLAYER d;
  353.     IPLAYER i;
  354.     UDPLAYER u;
  355.     uint8 
  356.         data[UMAXLEN];      /* largest UDP data we will use */
  357. };
  358.  
  359. typedef struct udp UDPKT;
  360.  
  361. struct uport {
  362.     UDPKT udpout;
  363.     struct pseudotcp tcps;        /* pseudo-tcp for checksumming */
  364.     uint16
  365.         listen,                    /* what port should this one listen to? */
  366.         length;                    /* how much data arrived in last packet? */
  367.     uint8 
  368.         data[UMAXLEN],            /* incoming, last datagram of that type */
  369.         who[4],                    /* who sent it to me ? */
  370.         stale;                    /* have we read this packet yet? */
  371. };
  372.  
  373. /*************************************************************************/
  374. /*  event queue
  375. *   records what happens, especially errors, and keeps them for any
  376. *   routines that poll, looking for what happened.
  377. *   Eight event classes are masked in the event class byte.
  378. *    There can be 256 event types per class.
  379. *   The data field is handled differently by each event type.
  380. */
  381. struct eq {
  382.     uint8
  383.         eclass,            /* class, defined in netevent.h */
  384.         event;            /* which event */
  385.     int 
  386.         next,            /* ordering for events in queue  */
  387.         idata;            /* integer data, if you feel like it */
  388. };
  389.  
  390. /*  
  391. *  events which can occur and be placed into the event queue
  392. */
  393. #define NEVENTS 50
  394.  
  395. /*  classes defined in netevent.h   */
  396.