home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1996 October / PCO_10.ISO / filesbbs / bsrc_260.arj / SRC.ZIP / sendsync.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-23  |  18.3 KB  |  523 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-96, Bit Bucket Software Co.              */
  11. /*                                                                          */
  12. /*               This module was written by Vince Perriello                 */
  13. /*                                                                          */
  14. /*          FidoNet(R) Mail Session Calling System Synchronization          */
  15. /*                                                                          */
  16. /*                                                                          */
  17. /*    For complete  details  of the licensing restrictions, please refer    */
  18. /*    to the License  agreement,  which  is published in its entirety in    */
  19. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.260.    */
  20. /*                                                                          */
  21. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  22. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  23. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  24. /*    NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET    */
  25. /*    SOFTWARE CO.  AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT    */
  26. /*    SHOULD YOU  PROCEED TO USE THIS FILE  WITHOUT HAVING  ACCEPTED THE    */
  27. /*    TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER    */
  28. /*    AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.      */
  29. /*                                                                          */
  30. /*                                                                          */
  31. /* You can contact Bit Bucket Software Co. at any one of the following      */
  32. /* addresses:                                                               */
  33. /*                                                                          */
  34. /* Bit Bucket Software Co.        FidoNet  1:104/501, 1:343/491             */
  35. /* P.O. Box 460398                AlterNet 7:42/1491                        */
  36. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  37. /*                                Internet f491.n343.z1.fidonet.org         */
  38. /*                                                                          */
  39. /* Please feel free to contact us at any time to share your comments about  */
  40. /* our software and/or licensing policies.                                  */
  41. /*                                                                          */
  42. /*--------------------------------------------------------------------------*/
  43.  
  44. /* Include this file before any other includes or defines! */
  45.  
  46. #include "includes.h"
  47.  
  48. /*
  49.     Sender Synchronization state table.
  50.  
  51.     This logic is used by the calling system. It will determine the
  52.     type of mail transfer which can be used in communicating with
  53.     the called system.
  54.  
  55.     This stuff was copied almost verbatim from a file sent to us by TJ.
  56.     (Of course, then we hacked the heck out of it!!!)
  57.  
  58.     Thanks, Tom.
  59.  
  60.  
  61.  .-----+----------+-------------------------+-------------------------+-----.
  62.  | SS0 | SyncInit |                         | Prepare 3 sec Sync timer|     |
  63.  |     |          |                         | Prepare .5 sec NAK tmr  |     |
  64.  |     |          |                         | Init NAK Count          |     |
  65.  |     |          |                         | Start 60 sec master tmr | SS1 |
  66.  |-----+----------+-------------------------+-------------------------+-----|
  67.  | SS1 | SendSync | 1. Over 60 seconds      |                         |     |
  68.  |     |          |    or carrier lost      | no response             | exit|
  69.  |     |          +-------------------------+-------------------------+-----|
  70.  |     |          | 2. 3 sec elapsed        | Send YOOHOO, then TSYNC |     |
  71.  |     |          |    or timer not started | Start 3 sec Sync timer  | SS2 |
  72.  |     |          +-------------------------+-------------------------+-----|
  73.  |     |          | 3. not elapsed          |                         | SS2 |
  74.  |-----+----------+-------------------------+-------------------------+-----|
  75.  | SS2 | WaitResp | 1. Nothing received     | require a response      | SS1 |
  76.  |     |          +-------------------------+-------------------------+-----|
  77.  |     |          | 2. ENQ received         | WaZOO Protocol selected | exit|
  78.  |     |          +-------------------------+-------------------------+-----|
  79.  |     |          | 3. '*' received         | probable EMSI           | SS5 |
  80.  |     |          +-------------------------+-------------------------+-----|
  81.  |     |          | 4. 'C' received         | probable FSC001         | SS3 |
  82.  |     |          +-------------------------+-------------------------+-----|
  83.  |     |          | 5. NAK received         | probable FSC001         | SS3 |
  84.  |     |          +-------------------------+-------------------------+-----|
  85.  |     |          | 6. 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.  | SS5 | EMSI     | 1. String matched       | Check for sync chars    | SS6 |
  98.  |     |          +-------------------------+-------------------------+-----|
  99.  |     |          | 2. No match             | Get next input character| SS1 |
  100.  |-----+----------+-------------------------+-------------------------+-----|
  101.  | SS6 | TestSync | 1. No TSYNC or YOOHOO   | Call EMSI handshake     | exit|
  102.  |     |          | sent or 3 sec of quiet  |                         |     |
  103.  |     |          +-------------------------+-------------------------+-----|
  104.  |     |          | 2. ENQ, 'C' or NAK seen | Toss EMSI, try again    | SS2 |
  105.  `-----+----------+-------------------------+-------------------------+-----'
  106.  
  107.  
  108.  */
  109.  
  110. /*
  111.  * Data structure used by all SendSync state machine functions.
  112.  * Contains all data which needs to be passed between various states.
  113.  *
  114.  */
  115.  
  116. typedef struct
  117. {
  118.     long control;                /* Must always start with a long!   */
  119.     long NAK_Timer;                /* 1/2 second NAK interval timer    */
  120.     long SendSyncTimer;            /* 3 second SendSync Timer          */
  121.     long Master_Timer;            /* 60 second master timeout         */
  122.     int NAK_Count;                /* Count of NAK's received          */
  123.     char *emsi_ptr;                /* pointer for EMSI_INQ             */
  124.     int emsi_flag;
  125.     int sync_flag;
  126.     int    CR_count;
  127.     int result;                    /* Result we want to send out       */
  128. } SSARGS, *SSARGSP;
  129.  
  130. int SSSyncInit (SSARGSP);        /* Called by state machine at start */
  131. int SSExit (SSARGSP);            /* Called by state machine at end   */
  132. int SSSendSync (SSARGSP);        /* SS1 state processing function    */
  133. int SSWaitResp (SSARGSP);        /* SS2 state processing function    */
  134. int SSNAKTmr (SSARGSP);            /* SS3 state processing function    */
  135. int SSNAKCount (SSARGSP);        /* SS4 state processing function    */
  136. int SSTestEMSI (SSARGSP);        /* SS5 state processing function    */
  137. int SSTestSync (SSARGSP);        /* SS6 state processing function    */
  138.  
  139. #define SS0    0                /* Reserved value of 0 for init     */
  140. #define SSexit 0                /* Slot 1 is exit, but called by 0  */
  141. #define SS1    2                /* First "user" slot is 2.          */
  142. #define SS2    3                /* After that, it all maps n : n+1  */
  143. #define SS3    4
  144. #define SS4    5
  145. #define SS5    6
  146. #define SS6    7
  147.  
  148. typedef struct
  149. {
  150.     char *state_name;
  151.     int (*state_func) (SSARGSP);
  152. } SSTATES, *SSTATEP;
  153.  
  154. SSTATES Send_Sync[] =
  155. {                                /* Table used by state machine      */
  156.     {"SSSyncInit", SSSyncInit},    /* And referred to by 'SSn' defines */
  157.     {"SSExit", SSExit},            /* listed above ...                 */
  158.     {"SSSendSync", SSSendSync},
  159.     {"SSWaitResp", SSWaitResp},
  160.     {"SSNAKTmr", SSNAKTmr},
  161.     {"SSNAKCount", SSNAKCount},
  162.     {"SSTestEMSI", SSTestEMSI},
  163.     {"SSTestSync", SSTestSync}
  164. };
  165.  
  166. /*
  167.  * CallerSendSync
  168.  * Determine whether we are talking to an FTS-0001 mailer or an FTS-0006
  169.  * mailer. Use the general state machine driver.
  170.  *
  171.  * This is the only external entry point into this module.
  172.  *
  173.  */
  174.  
  175. int 
  176. CallerSendSync (void)
  177. {
  178.     SSARGS args;
  179.     int res;
  180.  
  181.     args.result = 0;
  182.  
  183.     res = state_machine ((STATEP) Send_Sync, &args, 2);
  184.     return (res);
  185. }
  186.  
  187. /*
  188.  * This routine is called by the state machine when the 'SSexit'
  189.  * state is seen. Its return value is what the state machine
  190.  * will return to its caller as the result of the function.
  191.  *
  192.  */
  193.  
  194. int 
  195. SSExit (SSARGSP args)
  196. {
  197.     return (args->result);
  198. }
  199.  
  200. /*
  201.  
  202.  .-----+----------+-------------------------+-------------------------+-----.
  203.  | SS0 | SyncInit |                         | Prepare 3 sec Sync timer|     |
  204.  |     |          |                         | Prepare .5 sec NAK tmr  |     |
  205.  |     |          |                         | Init NAK Count          |     |
  206.  |     |          |                         | Start 60 sec master tmr | SS1 |
  207.  `-----+----------+-------------------------+-------------------------+-----'
  208.  
  209.   */
  210.  
  211. int 
  212. SSSyncInit (SSARGSP args)
  213. {
  214.     args->NAK_Timer = args->SendSyncTimer = 0L;
  215.     args->NAK_Count = 0;
  216.     args->emsi_ptr = emsistr[EMSI_REQ];
  217.     args->emsi_flag = FALSE;
  218.     args->sync_flag = FALSE;
  219.     args->CR_count = 0;
  220.     if (!no_EMSI_Session)
  221.         args->SendSyncTimer = timerset (200);
  222.     args->Master_Timer = timerset (6000);
  223.     return (SS1);
  224. }
  225.  
  226. /*
  227.  
  228.  .-----+----------+-------------------------+-------------------------+-----.
  229.  | SS1 | SendSync | 1. Over 60 seconds      |                         |     |
  230.  |     |          |    or carrier lost      | no response             | exit|
  231.  |     |          +-------------------------+-------------------------+-----|
  232.  |     |          | 2. 3 sec elapsed        | Send YOOHOO, then TSYNC |     |
  233.  |     |          |    or timer not started | Start 3 sec Sync timer  | SS2 |
  234.  |     |          +-------------------------+-------------------------+-----|
  235.  |     |          | 3. not elapsed          |                         | SS2 |
  236.  `-----+----------+-------------------------+-------------------------+-----'
  237.  
  238.   */
  239.  
  240. int 
  241. SSSendSync (SSARGSP args)
  242. {
  243.     if (!(CARRIER) || (timeup (args->Master_Timer)))
  244.     {
  245.         args->result = 0;        /* Failure */
  246.         return (SSexit);
  247.     }
  248.  
  249.     if (got_ESC ())
  250.     {
  251.         status_line (MSG_TXT(M_CONNECT_ABORTED));
  252.         mdm_hangup ();
  253.         return (SSexit);
  254.     }
  255.  
  256.     if (!no_EMSI_Session && CHAR_AVAIL ())
  257.         return (SS2);
  258.  
  259.     if (!(args->SendSyncTimer) || timeup (args->SendSyncTimer))
  260.     {
  261.         CLEAR_INBOUND ();
  262.  
  263. #ifdef NEVER                    /* We don't intend to ever transmit this! */
  264.         if (!no_EMSI_Session && !args->emsi_flag)
  265.         {                        /* If we're EMSI,   */
  266.             SENDCHARS (emsistr[EMSI_INQ], strlen (emsistr[EMSI_INQ]), 1);
  267.             SENDCHARS (emsistr[EMSI_INQ], strlen (emsistr[EMSI_INQ]), 1);
  268.         }
  269. #endif                            /* EMSI doesn't need to send and this fouls up BBSes */
  270.  
  271.         if (!no_WaZOO_Session)    /* If we're WaZOO,   */
  272.             SENDBYTE (YOOHOO);
  273.         SENDBYTE (TSYNC);
  274.         args->sync_flag = TRUE;
  275.         args->SendSyncTimer = timerset (300);
  276.     }
  277.     return (SS2);
  278. }
  279.  
  280. /*
  281.  
  282.  .-----+----------+-------------------------+-------------------------+-----.
  283.  | SS2 | WaitResp | 1. Nothing received     | require a response      | SS1 |
  284.  |     |          +-------------------------+-------------------------+-----|
  285.  |     |          | 2. ENQ received         | Wazoo Protocol selected | exit|
  286.  |     |          +-------------------------+-------------------------+-----|
  287.  |     |          | 3. 'C' received         | probable FSC001         | SS3 |
  288.  |     |          +-------------------------+-------------------------+-----|
  289.  |     |          | 4. NAK received         | probable FSC001         | SS3 |
  290.  |     |          +-------------------------+-------------------------+-----|
  291.  |     |          | 5. Debris (might include| Reset NAK timer         |     |
  292.  |     |          |    (YOOHOO|TSYNC) & 127)| if started              | SS1 |
  293.  `-----+----------+-------------------------+-------------------------+-----'
  294.  
  295.   */
  296.  
  297. int 
  298. SSWaitResp (SSARGSP args)
  299. {
  300.     short i;
  301.     int exit_code;
  302.  
  303.     if (!CHAR_AVAIL ())
  304.     {
  305.         time_release ();        /*PLF Sun  12-01-1991  04:46:58 */
  306.         return (SS1);
  307.     }
  308.  
  309.     if (args->emsi_flag)
  310.         return (SS5);
  311.  
  312.     i = PEEKBYTE ();
  313.  
  314.     switch (i)
  315.     {
  316.  
  317.     case 'C':
  318.     case NAK:
  319.         exit_code = SS3;
  320.         break;
  321.  
  322.     case '*':
  323.         if (!no_EMSI_Session)
  324.         {                        /* If we're EMSI,   */
  325.             exit_code = SS5;
  326.             break;
  327.         }
  328.         goto unknown;
  329.  
  330.     case 0x0d:                    /* Return */
  331.         /* If we get two of these with no sync, give up on EMSI. */
  332.         if ((++args->CR_count) == 2 && !args->sync_flag)
  333.         {
  334.             args->SendSyncTimer = 0;
  335.             (void) TIMED_READ (0);
  336.             exit_code = SS1;
  337.             break;
  338.         }
  339.         goto unknown;
  340.  
  341.     case ENQ:
  342.  
  343.         /* If we get this before a YooHoo, send one. */
  344.         if (!args->sync_flag)
  345.             args->SendSyncTimer = 0;
  346.         else
  347.         if (!no_WaZOO_Session)
  348.         {                        /* If we're WaZOO,   */
  349.             args->result = 2;    /* WaZOO */
  350.             exit_code = SSexit;
  351.             break;
  352.         }
  353.  
  354.         /* Deliberately fall through from ENQ if we're not doing WaZOO */
  355.  
  356.     default:
  357.  
  358. unknown:
  359.         if (i == -1)
  360.             time_release ();
  361.         else
  362.             (void) TIMED_READ (0);
  363.  
  364.         if (args->NAK_Timer)
  365.         {
  366.             args->NAK_Timer = timerset (50);
  367.         }
  368.         exit_code = SS1;
  369.         break;
  370.     }
  371.  
  372.     return (exit_code);
  373. }
  374.  
  375. /*
  376.  
  377.  .-----+----------+-------------------------+-------------------------+-----.
  378.  | SS3 | NAKTmr   | 1. Timer not expired    | Zero NAK count          |     |
  379.  |     |          |    or timer not started | Start .5 sec NAK timer  | SS1 |
  380.  |     |          +-------------------------+-------------------------+-----|
  381.  |     |          | 2. Timer expired        | Bump NAK count          | SS4 |
  382.  `-----+----------+-------------------------+-------------------------+-----'
  383.  
  384.   */
  385.  
  386. int 
  387. SSNAKTmr (SSARGSP args)
  388. {
  389.     if (!(args->NAK_Timer) || !timeup (args->NAK_Timer))
  390.     {
  391.         args->NAK_Count = 0;
  392.         args->NAK_Timer = timerset (50);
  393.         (void) TIMED_READ (0);
  394.         return (SS1);
  395.     }
  396.     else
  397.     {
  398.         (args->NAK_Count)++;
  399.         return (SS4);
  400.     }
  401. }
  402.  
  403. /*
  404.  
  405.  .-----+----------+-------------------------+-------------------------+-----.
  406.  | SS4 | NAKCount | 1. Count >= 2?          | assume FSC001           | exit|
  407.  |     |          +-------------------------+-------------------------+-----|
  408.  |     |          | 2. Count < 2            | Keep looking            | SS1 |
  409.  `-----+----------+-------------------------+-------------------------+-----'
  410.  
  411.   */
  412.  
  413. int 
  414. SSNAKCount (SSARGSP args)
  415. {
  416.     if (no_WaZOO_Session || args->NAK_Count >= 2)
  417.     {
  418.         args->result = 1;        /* FSC001 */
  419.         return (SSexit);
  420.     }
  421.     else
  422.     {
  423.         (void) TIMED_READ (0);
  424.         return (SS1);
  425.     }
  426. }
  427.  
  428. /*
  429.  
  430.  .-----+----------+-------------------------+-------------------------+-----.
  431.  | SS5 | EMSI     | 1. String matched       | Check for sync chars    | SS6 |
  432.  |     |          +-------------------------+-------------------------+-----|
  433.  |     |          | 2. No match             | Get next input character| SS2 |
  434.  `-----+----------+-------------------------+-------------------------+-----'
  435.  
  436.  */
  437.  
  438. int 
  439. SSTestEMSI (SSARGSP args)
  440. {
  441.     short i;
  442.  
  443.     i = PEEKBYTE ();                        /* Get the character     */
  444.  
  445.     if ((int) toupper (i & 0xff) != (int) (*(args->emsi_ptr++)))
  446.     {                                        /* Does this match next? */
  447.         args->emsi_ptr = emsistr[EMSI_REQ];    /* No, reset pointer */
  448.         args->emsi_flag = FALSE;
  449.         args->SendSyncTimer = timerset (300);
  450.         return (SS2);
  451.     }
  452.     else
  453.     {
  454.         TIMED_READ (0);            /* Eat the character     */
  455.         if (!*args->emsi_ptr)    /* Was this the last one?*/
  456.             return (SS6);
  457.         args->emsi_flag = TRUE;
  458.         args->SendSyncTimer = timerset (300);
  459.     }
  460.     return (SS2);
  461. }
  462.  
  463. /*
  464.  .-----+----------+-------------------------+-------------------------+-----.
  465.  | SS6 | TestSync | 1. No TSYNC or YOOHOO   | Call EMSI handshake     | exit|
  466.  |     |          | sent or 3 sec of quiet  |                         |     |
  467.  |     |          +-------------------------+-------------------------+-----|
  468.  |     |          | 2. ENQ, 'C' or NAK seen | Toss EMSI, try again    | SS2 |
  469.  `-----+----------+-------------------------+-------------------------+-----'
  470. */
  471.  
  472. int
  473. SSTestSync (SSARGSP args)
  474. {
  475.     /* Wait for 3 secs of quiet if we ever sent TSYNC or YOOHOO. During
  476.        that period, if we see something that might be a response to the
  477.        TSYNC or YOOHOO, toss the EMSI stuff and try again. */
  478.  
  479.     if (args->sync_flag)
  480.     {
  481.         short i;
  482.         long t = timerset (300);
  483.  
  484.         status_line (">Waiting for called system to quiet.");
  485.         while (!timeup (t))
  486.         {
  487.             i = PEEKBYTE ();
  488.  
  489.             /* Is anything there right now? */
  490.             if (i == -1)
  491.             {
  492.                 time_release ();
  493.                 continue;
  494.             }
  495.  
  496.             /* Was it a session startup? */
  497.             if (i == 'C' || i == NAK || i == ENQ || i == '*')
  498.             {
  499.                 status_line (">Found called sync, try again.");
  500.                 args->emsi_ptr = emsistr[EMSI_REQ];
  501.                 args->emsi_flag = FALSE;
  502.                 args->SendSyncTimer = timerset (300);
  503.                 return (SS2);
  504.             }
  505.             else
  506.             {
  507.                 /* No. Eat it and reset the timer. */
  508.                 TIMED_READ (0);
  509.                 t = timerset (300);
  510.             }
  511.  
  512.         }
  513.         status_line (">All quiet, let's do EMSI.");
  514.     }
  515.  
  516.     /* We're going with EMSI. Send EMSI_INQ and go with it. */
  517.  
  518.     SENDCHARS (emsistr[EMSI_INQ], strlen (emsistr[EMSI_INQ]), 1);
  519.     SENDBYTE ('\r');
  520.     args->result = 4;    /* 4 = EMSI */
  521.     return (SSexit);
  522. }
  523.