home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / DataScope 2.0.3 / DataScope2l / TCPIncludes / protocol.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-04  |  10.2 KB  |  406 lines  |  [TEXT/MPS ]

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