home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / BSRC_250.LZH / SENDSYNC.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  15KB  |  342 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software, Co.                       */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          Freely Available<tm> Software.                 */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  (C) Copyright 1987-91, Bit Bucket Software Co., a Delaware Corporation. */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*               This module was written by Vince Perriello                 */
  14. /*                                                                          */
  15. /*                                                                          */
  16. /*          FidoNet(R) Mail Session Calling System Synchronization          */
  17. /*                                                                          */
  18. /*                                                                          */
  19. /*    For complete  details  of the licensing restrictions, please refer    */
  20. /*    to the License  agreement,  which  is published in its entirety in    */
  21. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.250.    */
  22. /*                                                                          */
  23. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  24. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  25. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  26. /*    NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET    */
  27. /*    SOFTWARE CO.  AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT    */
  28. /*    SHOULD YOU  PROCEED TO USE THIS FILE  WITHOUT HAVING  ACCEPTED THE    */
  29. /*    TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER    */
  30. /*    AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.      */
  31. /*                                                                          */
  32. /*                                                                          */
  33. /* You can contact Bit Bucket Software Co. at any one of the following      */
  34. /* addresses:                                                               */
  35. /*                                                                          */
  36. /* Bit Bucket Software Co.        FidoNet  1:104/501, 1:343/491             */
  37. /* P.O. Box 460398                AlterNet 7:491/0                          */
  38. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  39. /*                                Internet f491.n343.z1.fidonet.org         */
  40. /*                                                                          */
  41. /* Please feel free to contact us at any time to share your comments about  */
  42. /* our software and/or licensing policies.                                  */
  43. /*                                                                          */
  44. /*                                                                          */
  45. /*--------------------------------------------------------------------------*/
  46.  
  47.  
  48.  
  49.  /*
  50.     Sender Synchronization state table.
  51.    
  52.     This logic is used by the calling system. It will determine the
  53.     type of mail transfer which can be used in communicating with
  54.     the called system.
  55.    
  56.     This stuff was copied almost verbatim from a file sent to us by TJ.
  57.     (Of course, then we hacked the heck out of it!!!)
  58.   
  59.     Thanks, Tom.
  60.   
  61.  
  62.  .-----+----------+-------------------------+-------------------------+-----.
  63.  | SS0 | SyncInit |                         | Prepare 3 sec Sync timer|     |
  64.  |     |          |                         | Prepare .5 sec NAK tmr  |     |
  65.  |     |          |                         | Init NAK Count          |     |
  66.  |     |          |                         | Start 60 sec master tmr | SS1 |
  67.  |-----+----------+-------------------------+-------------------------+-----|
  68.  | SS1 | SendSync | 1. Over 60 seconds      |                         |     |
  69.  |     |          |    or carrier lost      | no response             | exit|
  70.  |     |          +-------------------------+-------------------------+-----|
  71.  |     |          | 2. 3 sec elapsed        | Clear Inbound buffer    |     |
  72.  |     |          |    or timer not started | Send YOOHOO, then TSYNC |     |
  73.  |     |          |                         | Start 3 sec Sync timer  | SS2 |
  74.  |     |          +-------------------------+-------------------------+-----|
  75.  |     |          | 3. not elapsed          |                         | SS2 |
  76.  |-----+----------+-------------------------+-------------------------+-----|
  77.  | SS2 | WaitResp | 1. Nothing received     | require a response      | SS1 |
  78.  |     |          +-------------------------+-------------------------+-----|
  79.  |     |          | 2. ENQ received         | WaZOO Protocol selected | exit|
  80.  |     |          +-------------------------+-------------------------+-----|
  81.  |     |          | 3. 'C' received         | probable FSC001         | SS3 |
  82.  |     |          +-------------------------+-------------------------+-----|
  83.  |     |          | 4. NAK received         | probable FSC001         | SS3 |
  84.  |     |          +-------------------------+-------------------------+-----|
  85.  |     |          | 5. Debris (might include| Reset NAK timer         |     |
  86.  |     |          |    (YOOHOO|TSYNC) & 127)| if started              | SS1 |
  87.  |-----+----------+-------------------------+-------------------------+-----|
  88.  | SS3 | NAKTmr   | 1. Timer not expired    | Zero NAK count          |     |
  89.  |     |          |    or timer not started | Start .5 sec NAK timer  | SS1 |
  90.  |     |          +-------------------------+-------------------------+-----|
  91.  |     |          | 2. Timer expired        | Bump NAK count          | SS4 |
  92.  |-----+----------+-------------------------+-------------------------+-----|
  93.  | SS4 | NAKCount | 1. Count >= 2?          | assume FSC001           | exit|
  94.  |     |          +-------------------------+-------------------------+-----|
  95.  |     |          | 2. Count < 2            | Keep looking            | SS1 |
  96.  `-----+----------+-------------------------+-------------------------+-----'
  97.  
  98.  
  99.  */
  100.  
  101. /* Include this file before any other includes or defines! */
  102.  
  103. #include "includes.h"
  104.  
  105. /*
  106.  * Data structure used by all SendSync state machine functions.
  107.  * Contains all data which needs to be passed between various states.
  108.  *
  109.  */
  110.  
  111. typedef struct {
  112.    long NAK_Timer;                      /* 1/2 second NAK interval timer    */
  113.    long SendSyncTimer;                  /* 3 second SendSync Timer          */
  114.    long Master_Timer;                   /* 60 second master timeout         */
  115.    int  NAK_Count;                      /* Count of NAK's received          */
  116.    int  result;                         /* Result we want to send out       */
  117. } SSARGS, *SSARGSP;
  118.  
  119. int SSSyncInit (SSARGSP, int);          /* Called by state machine at start */
  120. int SSExit     (SSARGSP, int);          /* Called by state machine at end   */
  121. int SSSendSync (SSARGSP);               /* SS1 state processing function    */
  122. int SSWaitResp (SSARGSP);               /* SS2 state processing function    */
  123. int SSNAKTmr   (SSARGSP);               /* SS3 state processing function    */
  124. int SSNAKCount (SSARGSP);               /* SS4 state processing function    */
  125.  
  126.  
  127. #define SS0    0                        /* Reserved value of 0 for init     */
  128. #define SSexit 0                        /* Slot 1 is exit, but called by 0  */
  129. #define SS1    2                        /* First "user" slot is 2.          */
  130. #define SS2    3                        /* After that, it all maps n : n+1  */
  131. #define SS3    4
  132. #define SS4    5
  133. #define SS5    6
  134.  
  135.  
  136. STATES Send_Sync [] = {                 /* Table used by state machine      */
  137.    { "SSSyncInit", SSSyncInit },        /* And referred to by 'SSn' defines */
  138.    { "SSExit",     SSExit },            /* listed above ...                 */
  139.    { "SSSendSync", SSSendSync },
  140.    { "SSWaitResp", SSWaitResp },
  141.    { "SSNAKTmr",   SSNAKTmr },
  142.    { "SSNAKCount", SSNAKCount },
  143. };
  144.  
  145.  
  146. /*
  147.  * CallerSendSync
  148.  * Determine whether we are talking to an FTS-0001 mailer or an FTS-0006
  149.  * mailer. Use the general state machine driver.
  150.  *
  151.  * This is the only external entry point into this module.
  152.  *
  153.  */
  154.  
  155. int CallerSendSync (void)
  156. {
  157.    SSARGS args;
  158.    int res;
  159.  
  160.    args.result = 0;
  161.  
  162.    res = state_machine (Send_Sync, &args, 2);
  163.    return (res);
  164. }
  165.  
  166.  
  167.  
  168. /*
  169.  * This routine is called by the state machine when the 'SSexit'
  170.  * state is seen. Its return value is what the state machine
  171.  * will return to its caller as the result of the function.
  172.  *
  173.  */
  174.  
  175. int SSExit (SSARGSP args, int cur_state)
  176. {
  177.    happy_compiler = cur_state;  /* Makes the compiler happy! */
  178.    return (args->result);
  179. }
  180.  
  181.  
  182.  
  183.  /*
  184.  
  185.  .-----+----------+-------------------------+-------------------------+-----.
  186.  | SS0 | SyncInit |                         | Prepare 3 sec Sync timer|     |
  187.  |     |          |                         | Prepare .5 sec NAK tmr  |     |
  188.  |     |          |                         | Init NAK Count          |     |
  189.  |     |          |                         | Start 60 sec master tmr | SS1 |
  190.  `-----+----------+-------------------------+-------------------------+-----'
  191.  
  192.   */
  193.  
  194.  
  195. int SSSyncInit (SSARGSP args, int start_state)
  196. {
  197.    args->NAK_Timer = args->SendSyncTimer = 0L;
  198.    args->NAK_Count = 0;
  199.    args->Master_Timer = timerset (6000);
  200.    happy_compiler = start_state;  /* Makes the compiler happy! */
  201.    return (SS1);
  202. }
  203.  
  204.  /*
  205.  
  206.  .-----+----------+-------------------------+-------------------------+-----.
  207.  | SS1 | SendSync | 1. Over 60 seconds      |                         |     |
  208.  |     |          |    or carrier lost      | no response             | exit|
  209.  |     |          +-------------------------+-------------------------+-----|
  210.  |     |          | 2. 3 sec elapsed        | Clear Inbound buffer    |     |
  211.  |     |          |    or timer not started | Send YOOHOO, then TSYNC |     |
  212.  |     |          |                         | Start 3 sec Sync timer  | SS2 |
  213.  |     |          +-------------------------+-------------------------+-----|
  214.  |     |          | 3. not elapsed          |                         | SS2 |
  215.  `-----+----------+-------------------------+-------------------------+-----'
  216.  
  217.   */
  218.  
  219.  
  220. int SSSendSync (SSARGSP args)
  221. {
  222.    if (!(CARRIER) || (timeup (args->Master_Timer))) {
  223.       args->result = 0;         /* Failure */
  224.       return (SSexit);
  225.    }
  226.    
  227.    if (!(args->SendSyncTimer) || timeup (args->SendSyncTimer)) {
  228.       CLEAR_INBOUND ();
  229.       if (!no_WaZOO_Session)    /* If we're WaZOO,   */
  230.          SENDBYTE (YOOHOO);
  231.       SENDBYTE (TSYNC);
  232.       args->SendSyncTimer = timerset (300);
  233.    }
  234.    return (SS2);
  235. }
  236.  
  237.  
  238.  /*
  239.  
  240.  .-----+----------+-------------------------+-------------------------+-----.
  241.  | SS2 | WaitResp | 1. Nothing received     | require a response      | SS1 |
  242.  |     |          +-------------------------+-------------------------+-----|
  243.  |     |          | 2. ENQ received         | Wazoo Protocol selected | exit|
  244.  |     |          +-------------------------+-------------------------+-----|
  245.  |     |          | 3. 'C' received         | probable FSC001         | SS3 |
  246.  |     |          +-------------------------+-------------------------+-----|
  247.  |     |          | 4. NAK received         | probable FSC001         | SS3 |
  248.  |     |          +-------------------------+-------------------------+-----|
  249.  |     |          | 5. Debris (might include| Reset NAK timer         |     |
  250.  |     |          |    (YOOHOO|TSYNC) & 127)| if started              | SS1 |
  251.  `-----+----------+-------------------------+-------------------------+-----'
  252.  
  253.   */
  254.  
  255.  
  256. int SSWaitResp (SSARGSP args)
  257. {
  258.    int i;
  259.    int exit_code;
  260.    
  261.    if (!CHAR_AVAIL ()) {
  262.       return (SS1);
  263.    }
  264.    
  265.    i = PEEKBYTE ();
  266.    
  267.    switch (i) {
  268.    
  269.       case 'C':
  270.       case NAK:
  271.          exit_code = SS3;
  272.          break;
  273.  
  274.       case ENQ:
  275.          if (!no_WaZOO_Session) {      /* If we're WaZOO,   */
  276.             args->result = 2;      /* WaZOO */
  277.             exit_code = SSexit;
  278.             break;
  279.          }         
  280.  
  281.       /* Deliberately fall through from ENQ if we're not doing WaZOO */
  282.  
  283.       default:
  284.          (void) TIMED_READ (0);
  285.          if (args->NAK_Timer) {
  286.             args->NAK_Timer = timerset (50);
  287.          }
  288.          exit_code = SS1;
  289.          break;
  290.     }
  291.  
  292.    return (exit_code);
  293. }
  294.  
  295.  /*
  296.  
  297.  .-----+----------+-------------------------+-------------------------+-----.
  298.  | SS3 | NAKTmr   | 1. Timer not expired    | Zero NAK count          |     |
  299.  |     |          |    or timer not started | Start .5 sec NAK timer  | SS1 |
  300.  |     |          +-------------------------+-------------------------+-----|
  301.  |     |          | 2. Timer expired        | Bump NAK count          | SS4 |
  302.  `-----+----------+-------------------------+-------------------------+-----'
  303.  
  304.   */
  305.  
  306.  
  307. int SSNAKTmr (SSARGSP args)
  308. {
  309.    if (!(args->NAK_Timer) || !timeup (args->NAK_Timer)) {
  310.       args->NAK_Count = 0;
  311.       args->NAK_Timer = timerset (50);
  312.       (void) TIMED_READ (0);
  313.       return (SS1);
  314.    } else {
  315.       (args->NAK_Count)++;
  316.       return (SS4);
  317.    }
  318. }
  319.  
  320.  /*
  321.  
  322.  .-----+----------+-------------------------+-------------------------+-----.
  323.  | SS4 | NAKCount | 1. Count >= 2?          | assume FSC001           | exit|
  324.  |     |          +-------------------------+-------------------------+-----|
  325.  |     |          | 2. Count < 2            | Keep looking            | SS1 |
  326.  `-----+----------+-------------------------+-------------------------+-----'
  327.  
  328.   */
  329.  
  330.  
  331. int SSNAKCount (SSARGSP args)
  332. {
  333.    if (no_WaZOO_Session || args->NAK_Count >= 2) {
  334.       args->result = 1;         /* FSC001 */
  335.       return (SSexit);
  336.    } else {
  337.       (void) TIMED_READ (0);
  338.       return (SS1);
  339.    }
  340. }
  341.  
  342.