home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 239.lha / Unix2src.shar / dnet / dnet.h < prev    next >
C/C++ Source or Header  |  1989-05-02  |  6KB  |  234 lines

  1.  
  2. /*
  3.  *  DNET.H
  4.  *
  5.  *    DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved
  6.  */
  7.  
  8. #include <sys/types.h>
  9. #include <sys/ioctl.h>
  10. #include <sys/socket.h>
  11. #include <sys/time.h>
  12. #include <sys/file.h>
  13.  
  14. /* V2.01 */
  15. #include <sys/param.h>
  16. #include <pwd.h>
  17.  
  18. #include <signal.h>
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #ifndef FD_SETSIZE
  22. #define FD_SETSIZE (sizeof(struct fd_set) * 8)
  23. #endif
  24. #ifndef NFDBITS
  25. #define NFDBITS 32
  26. #define    FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
  27. #define    FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
  28. #define    FD_ISSET(n, p)    ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
  29. #define FD_ZERO(p)    bzero((char *)(p), sizeof(*(p)))
  30. #endif
  31. #ifndef sigmask
  32. #define sigmask(m) (1 << ((m)-1))
  33. #endif
  34.  
  35. #ifndef LASTTRYDNETSERVERS
  36. #define LASTTRYDNETSERVERS "/usr/local/lib/dnet/dnet.servers"
  37. #endif
  38.  
  39. struct Node {
  40.     struct Node *ln_Succ;
  41.     struct Node *ln_Pred;
  42. };
  43.  
  44. struct List {
  45.     struct Node *lh_Head;
  46.     struct Node *lh_Tail;
  47.     struct Node *lh_TailPred;
  48. };
  49.  
  50.  
  51. typedef unsigned char    ubyte;
  52. typedef unsigned short    uword;
  53. typedef unsigned long    ulong;
  54.  
  55. typedef struct List        LIST;
  56. typedef struct Node        NODE;
  57.  
  58. #include "channel.h"
  59.  
  60. #define PKT struct _PKT
  61. #define XIOR struct _XIOR
  62.  
  63. #define EMPTY    0    /*  empty (sent)                    */
  64. #define READY    1    /*  data ready (not sent yet)       */
  65. #define RCVBUF  4096
  66.  
  67. #define MAXCHAN 128    /*  Max # of channels supported     */
  68. #define SYNC    0xFF    /*  SYNC character            */
  69. #define WTIME    2000000 /*  in micro seconds (expect ack)   */
  70. #define RTIME    1000000 /*  in micro seconds (read to)      */
  71. #define MAXPKT    200    /*  maximum packet size  (data area)              */
  72. #define MINPKT  32    /*  minimum packet size  (data area) for purposes */
  73.             /*  of determining the dynamic maximum packet size*/
  74.             /*  only.  Actual minimum is, of course, 1 byte   */
  75.  
  76. #define OVERHEAD    7    /*  for packets with data        */
  77.  
  78. XIOR {
  79.     NODE    io_Node;
  80.     ubyte   *io_Data;
  81.     ulong   io_Length;
  82.     ulong   io_Actual;
  83.     uword   io_Channel;
  84.     ubyte   io_Command;
  85.     ubyte   io_Error;
  86.     char    io_Pri;
  87. };
  88.  
  89. PKT {
  90.     uword   iolength;    /*  send: length of packet, recv: length of data    */
  91.     ubyte   state;    /*  EMPTY, READY                    */
  92.  
  93.     ubyte   sync;    /*  THE PACKET        */
  94.     ubyte   ctl;
  95.     ubyte   cchk;
  96.     ubyte   lenh;
  97.     ubyte   lenl;
  98.     ubyte   data[MAXPKT+2];
  99. };
  100.  
  101.     /*
  102.      *    In receiving a packet the receiver can be in one of these
  103.      *    states.
  104.      */
  105.  
  106. #define RS_SYNC 0        /*    Waiting for sync        */
  107. #define RS_CTL    1        /*    Waiting for command        */
  108. #define RS_CCHK 2        /*    Waiting for check byte        */
  109. #define RS_LEN1 3        /*    Waiting for MSB length byte    */
  110. #define RS_LEN2 4        /*    Waiting for LSB length byte    */
  111. #define RS_DATA 5        /*    Waiting for data & checksum    */
  112.  
  113.     /*
  114.      *    The low level protocol generates packets.   This is used 
  115.      *    for error checking, data sequencing, retries, restart, etc...
  116.      *    The packet format is:
  117.      *
  118.      *    SYNC sss0xccc CHK
  119.      *    SYNC sss1xccc CHK nnnnnnnn nnnnnnnn [DATA] CHK2
  120.      *                msb       lsb
  121.      *
  122.      *    sss = sequence #
  123.      *    B4  = packet contains data
  124.      *    B3  = reserved (may be used to extend the command set)
  125.      *    ccc = PKF_?????
  126.      *
  127.      *    NOTE that the data length nnn..nn is not checked with either
  128.      *    CHK or CHK2 .  The protocol will determine if the length
  129.      *    is reasonable (< MAXPKT) and then try to read that many
  130.      *    bytes.  The CHK will obviously fail if the length was 
  131.      *    incorrect.
  132.      */
  133.  
  134. #define PKF_SEQUENCE    0xE0    /*  Sequence #            */
  135. #define PKF_DATA    0x10    /*  1-65535 bytes        */
  136. #define PKF_RESERVED    0x08    /*  reserved bit        */
  137. #define PKF_MASK    0x07    /*  command mask        */
  138.  
  139. #define PKCMD_WRITE    1    /*  A DATA packet        */
  140. #define PKCMD_CHECK    2    /*  Request ACK or NAK for win    */
  141. #define PKCMD_ACK    3    /*  ACK a window        */
  142. #define PKCMD_NAK    4    /*  NAK a window        */
  143. #define PKCMD_RESTART     5    /*  RESTART dnet.  (new)    */
  144. #define PKCMD_ACKRSTART    6    /*  ACK a restart  (new)    */
  145. #define PKCMD_RESERVE3    7
  146.  
  147.     /*
  148.      *  All channel multiplexing, channel commands, etc... is encoded
  149.      *  within a PKCMD_WRITE packet.
  150.      *
  151.      *  Channel commands are characterized by a one byte control
  152.      *  field and up to 7 bytes of data.  The control field format
  153.      *  is:        10cccnnn [DATA]        ccc = SCMD_??
  154.      *                    nnn = # additional data bytes
  155.      */
  156.  
  157. #define SCMD_SWITCH    0x00    /*  switch active channel #    */
  158. #define SCMD_OPEN    0x01    /*  open a channel        */
  159. #define SCMD_CLOSE    0x02    /*  close a channel        */
  160. #define SCMD_ACKCMD    0x03    /*  ack an open/close request    */
  161. #define SCMD_EOFCMD    0x04    /*  Reof or Weof        */
  162. #define SCMD_QUIT    0x05    /*  QUIT dnet            */
  163. #define SCMD_IOCTL    0x06    /*  channel ioctl (new),PTY sup    */
  164. #define SCMD_RESERVE1    0x07
  165.  
  166.     /*
  167.      *  Stream data is characterized by the following format:
  168.      *
  169.      *           msb      lsb    
  170.      *        11nnnnnn nnnnnnnn [128-16383 bytes DATA]
  171.      *             0nnnnnnn [0-127 bytes DATA]
  172.      *
  173.      *  NOTE:  11000000 0ccccccc nnnnnnnn   reserved for furture ext. of
  174.      *                    SCMD commands.
  175.      */
  176.  
  177. #define SCMD_DATA    0x08    /*  stream command, DATA (dummy ID)    */
  178.  
  179.     /*
  180.      *  Each channel can be in one of several states.
  181.      */
  182.  
  183. #define CHAN_FREE    0x01    /*  free channel        */
  184. #define CHAN_ROPEN    0x02    /*  remote open, wait port msg    */
  185. #define CHAN_LOPEN    0x03    /*  local open, wait reply    */
  186. #define CHAN_OPEN    0x04    /*  channel open        */
  187. #define CHAN_CLOSE    0x05    /*  one side of channel closed  */
  188.  
  189. #define CHANF_ROK    0x01    /*  remote hasn't EOF'd us yet  */
  190. #define CHANF_WOK    0x02    /*  remote will accept data    */
  191. #define CHANF_LCLOSE    0x04     /*  channel closed on our side  */
  192. #define CHANF_RCLOSE    0x08    /*  channel closed on rem side  */
  193.  
  194. extern ubyte *RemHead();
  195. extern ubyte *malloc();
  196. extern char *getenv();
  197.  
  198. #ifndef NOEXT
  199. extern long USecPerByte;
  200. extern int DNet_fd;
  201. extern PKT Pkts[9];
  202. extern ubyte WCBuf[64];
  203. extern PKT *RPak[4];
  204. extern PKT *WPak[4];
  205. extern CHAN Chan[MAXCHAN];
  206. extern LIST TxList;           /*  For pending DNCMD_WRITE reqs.   */
  207. extern fd_set Fdread;
  208. extern fd_set Fdwrite;
  209. extern fd_set Fdexcept;
  210. extern void (*Fdstate[FD_SETSIZE])();
  211. extern ubyte Fdperm[FD_SETSIZE];
  212. extern uword FdChan[FD_SETSIZE];
  213. extern ubyte RcvBuf[RCVBUF];
  214. extern ubyte RTimedout, WTimedout;
  215. extern uword Rto_act, Wto_act;
  216. extern uword RcvData;
  217. extern uword RExpect;
  218. extern uword RChan;
  219. extern uword WChan;
  220. extern uword RPStart;
  221. extern uword WPStart;
  222. extern uword WPUsed;
  223. extern uword RState;
  224. extern ubyte DDebug;
  225. extern ubyte DidWrite;
  226. extern ubyte Restart;
  227. extern ubyte DeldQuit;
  228. extern ulong NumCon; 
  229.  
  230. extern int errno;
  231.  
  232. #endif
  233.  
  234.