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