home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / fido / ftsc_all.z43 / FTS-0006.C < prev    next >
C/C++ Source or Header  |  1989-07-10  |  15KB  |  539 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*      ------------         Bit-Bucket Software <no-Inc>                   */
  4. /*      \ 10001101 /         Writers and Distributors of                    */
  5. /*       \ 011110 /          No-Cost<no-tm> Software.                       */
  6. /*        \ 1011 /                                                          */
  7. /*         ------                                                           */
  8. /*                                                                          */
  9. /*  Copyright (C) 1987, 1988, 1989 by Robert Hartman and Vincent Perriello  */
  10. /*                                                                          */
  11. /*                                                                          */
  12. /*               This module was written by Vince Perriello                 */
  13. /*                                                                          */
  14. /*                                                                          */
  15. /*                                                                          */
  16. /*  This file contains a sample YooHoo sender and receiver module. You may  */
  17. /*  use it in any program, provided you give credit to Bit-Bucket Software  */
  18. /*  in any informational screens or literature pertaining to your program   */
  19. /*  that contains other such information (such as your own copyrights).     */
  20. /*                                                                          */
  21. /*                                                                          */
  22. /*--------------------------------------------------------------------------*/
  23.  
  24. #include <stdio.h>
  25. #include <signal.h>
  26. #include <ctype.h>
  27. #include <conio.h>
  28. #include <string.h>
  29.  
  30. #define WAZOO_SECTION
  31. #define MATRIX_SECTION
  32. #define isBITBRAIN 0x1b
  33.  
  34. #include "com.h"
  35. #include "xfer.h"
  36. #include "zmodem.h"
  37. #include "keybd.h"
  38. #include "sbuf.h"
  39. #include "sched.h"
  40. #include "externs.h"
  41. #include "prototyp.h"
  42.  
  43. static int Send_Hello_Packet (int);
  44. static int Recv_Hello_Packet (void);
  45.  
  46. /*--------------------------------------------------------------------------*/
  47. /* YOOHOO SENDER    (used when I am the CALLING system)                     */
  48. /*--------------------------------------------------------------------------*/
  49. int YooHoo_Sender (void)
  50. {
  51.    char *sptr;
  52.  
  53.    if (un_attended && fullscreen)
  54.       {
  55.       sb_move (filewin, 2, 2);
  56.       sb_puts (filewin, "YooHoo");
  57.       sb_show ();
  58.       }
  59.    else
  60.       {
  61.       set_xy ("YooHoo ");
  62.       }
  63.  
  64.    if (!Send_Hello_Packet (1))
  65.       sptr = "No Send";
  66.  
  67.    else
  68.       {
  69.       if (TIMED_READ (30) == YOOHOO)
  70.          return Recv_Hello_Packet ();
  71.  
  72.       status_line ("!No YOOHOO/2U2");
  73.       sptr = IDUNNO_msg;
  74.       }
  75.  
  76.    message (sptr);
  77.    return (0);
  78. }
  79.  
  80. /*--------------------------------------------------------------------------*/
  81. /* YOOHOO RECEIVER  (Used when I am the CALLED system)                      */
  82. /*--------------------------------------------------------------------------*/
  83.  
  84. int YooHoo_Receiver (void)
  85. {
  86.    int i;
  87.    int c;
  88.  
  89.    if (un_attended && fullscreen)
  90.       {
  91.       sb_move (filewin, 2, 2);
  92.       sb_puts (filewin, "YooHoo");
  93.       sb_show ();
  94.       }
  95.    else
  96.       {
  97.       set_xy ("YooHoo ");
  98.       }
  99.  
  100.    if (!(i = Recv_Hello_Packet()))
  101.       return (0);
  102.  
  103.    for (i = 0; (CARRIER) && (i < 10); i++)
  104.       {
  105.       if ((c = TIMED_READ (10)) == ENQ)
  106.          return Send_Hello_Packet (0) ? 1 : (b_init (), 0);
  107.  
  108.       if (c > 0)
  109.          {
  110.          message (NULL);
  111.          cprintf ("[%x] ", c);
  112.          CLEAR_INBOUND ();
  113.          }
  114.  
  115.       SENDBYTE (YOOHOO);
  116.       }
  117.  
  118.    hup = 1;
  119.    message (FUBAR_msg);
  120.    b_init ();
  121.    return (0);
  122.  
  123. }                                                /* YooHoo Receiver */
  124.  
  125. /*--------------------------------------------------------------------------*/
  126. /* SEND HELLO PACKET                                                        */
  127. /*--------------------------------------------------------------------------*/
  128. static int Send_Hello_Packet (Sender)
  129. int Sender;
  130. {
  131.    int i;
  132.    struct _Hello Hello;
  133.    byte *sptr;
  134.    long response_timer;
  135.    word crc;
  136.    word num_errs = 0;
  137.  
  138.    /*--------------------------------------------------------------------*/
  139.    /* Setup HELLO structure                                              */
  140.    /*--------------------------------------------------------------------*/
  141.    memset ((char *) &Hello, 0, sizeof (struct _Hello));
  142.  
  143.    Hello.signal = 'o';
  144.    Hello.hello_version = 1;
  145.  
  146.    Hello.product = isBITBRAIN;
  147.    Hello.product_maj = BINK_MAJVERSION;
  148.    Hello.product_min = BINK_MINVERSION;
  149.  
  150.    strncpy (Hello.my_name, system_name, 58);
  151.    Hello.my_name[58] = '\0';
  152.  
  153.    strncpy (Hello.sysop, sysop, 19);
  154.    Hello.sysop[19] = '\0';
  155.  
  156.    Hello.my_zone = alias[assumed].Zone;
  157.    if ((pvtnet >= 0) && (Sender) &&
  158.        ((called_zone == alias[assumed].Zone) || (called_zone == 0)) &&
  159.        (called_net == boss_net) && (called_node == boss_node))
  160.       {
  161.       Hello.my_net = boss_net;
  162.       Hello.my_node = boss_node;
  163.       Hello.my_point = alias[assumed].Node;
  164.       }
  165.    else
  166.       {
  167.       Hello.my_net = alias[assumed].Net;
  168.       Hello.my_node = alias[assumed].Node;
  169.       }
  170.  
  171.    Hello.capabilities = (no_zapzed) ? 0 : ZED_ZAPPER;
  172.    Hello.capabilities |= Y_DIETIFNA;
  173. #ifdef JANUS
  174.    if (janus_baud >= cur_baud)
  175.       Hello.capabilities |= DOES_IANUS;
  176. #endif
  177.  
  178.    if (n_getpassword (remote_zone, remote_net, remote_node))
  179.       {
  180.       strncpy (Hello.my_password, remote_password, 8);
  181.       }
  182.  
  183.    if ((matrix_mask & TAKE_REQ) &&
  184.        ((!Sender) || (on_our_nickel)))
  185.       Hello.capabilities |= WZ_FREQ;
  186.  
  187.    /*--------------------------------------------------------------------*/
  188.    /* Disable handshaking and ^C/^K handling                             */
  189.    /*--------------------------------------------------------------------*/
  190.    XON_DISABLE ();
  191.  
  192.    /*--------------------------------------------------------------------*/
  193.    /* Send the packet.                                                   */
  194.    /* Load outbound buffer quickly, and get modem busy sending.          */
  195.    /*--------------------------------------------------------------------*/
  196.  
  197. xmit_packet:
  198.  
  199.    SENDBYTE (0x1f);
  200.  
  201.    sptr = (char *) (&Hello);
  202.    SENDCHARS (sptr, 128, 1);
  203.  
  204.    /*--------------------------------------------------------------------*/
  205.    /* Calculate CRC while modem is sending its buffer                    */
  206.    /*--------------------------------------------------------------------*/
  207.    for (crc = i = 0; i < 128; i++)
  208.       {
  209.       crc = xcrc (crc, (byte) sptr[i]);
  210.       }
  211.  
  212.    CLEAR_INBOUND ();
  213.  
  214.    SENDBYTE ((unsigned char) (crc >> 8));
  215.    SENDBYTE ((unsigned char) (crc & 0xff));
  216.  
  217.    response_timer = timerset (4000);
  218.  
  219.    while (!timeup(response_timer) && CARRIER)
  220.       {
  221.       if (!CHAR_AVAIL ())
  222.          {
  223.          if (got_ESC ())
  224.             {
  225.             DTR_OFF ();
  226.             sptr  = KBD_msg;
  227.             goto no_response;
  228.             }
  229.      time_release ();
  230.          continue;
  231.      }     
  232.  
  233.       switch (i = TIMED_READ (0))
  234.          {
  235.          case ACK:
  236.             return (1);
  237.  
  238.          case '?':
  239.             message ("drats");
  240.  
  241.          case ENQ:
  242.             if (++num_errs == 10)
  243.                 {
  244.                 sptr = FUBAR_msg;
  245.                 goto no_response;
  246.                 }
  247.             goto xmit_packet;
  248.  
  249.          default:
  250.             if (i > 0)                   /* Could just be line noise */
  251.                {
  252. /*             cprintf ("[%x] ", i);*/
  253.                }
  254.             break;
  255.          }
  256.       }
  257.  
  258.    if (!CARRIER)
  259.       sptr = CARRIER_msg;
  260.    else
  261.       sptr  = TIME_msg;
  262.  
  263. no_response:
  264.  
  265.    message (sptr);
  266.    return (0);
  267.  
  268. }                                                /* Send Hello */
  269.  
  270.  
  271. /*--------------------------------------------------------------------------*/
  272. /* RECEIVE HELLO PACKET                                                     */
  273. /*--------------------------------------------------------------------------*/
  274. static int Recv_Hello_Packet (void)
  275. {
  276.    int i;
  277.    int c;
  278.    struct _Hello Hello;
  279.    byte *sptr;
  280.    byte num_errs = 0;
  281.    word crc;
  282.    word lsb;
  283.    word msb;
  284.    long master_timeout, hello_timeout;
  285.    char junkbuff[128];
  286.  
  287.    hup = 0;
  288.    sptr = NULL;
  289.  
  290.    /*--------------------------------------------------------------------*/
  291.    /* Clean up any mess that may be around                               */
  292.    /*--------------------------------------------------------------------*/
  293.    CLEAR_OUTBOUND ();
  294.    CLEAR_INBOUND ();
  295.    XON_DISABLE ();
  296.  
  297.    /*--------------------------------------------------------------------*/
  298.    /* Get the Hello structure                                            */
  299.    /*--------------------------------------------------------------------*/
  300.  
  301. /* big_pause (1);  */                           /* Wait for quiet. */
  302.    
  303.    if (un_attended && fullscreen)
  304.       {
  305.       sb_move (filewin, 2, 2);
  306.       sb_puts (filewin, "YooHoo/2U2");
  307.       sb_show ();
  308.       }
  309.    else
  310.       {
  311.       set_xy ("YooHoo/2U2 ");
  312.       }
  313.  
  314.    SENDBYTE (ENQ);                               /* Let the other system know
  315.                                                   * we heard YooHoo. */
  316.  
  317.    master_timeout = timerset (12000);        /* No more than 2 mins! */
  318.  
  319. watch_for_header:
  320.  
  321.    while (1)
  322.       {
  323.       if (sptr)
  324.          {
  325.          message (sptr);
  326.          sptr = NULL;
  327.          }
  328.  
  329.       if ((c = TIMED_READ (20)) == 0x1f)
  330.          break;
  331.  
  332.       if (got_ESC ())
  333.          {
  334.          sptr = KBD_msg;
  335.          goto receive_failed;
  336.          }
  337.  
  338.       if (!CARRIER)
  339.          {
  340.          sptr = CARRIER_msg;
  341.          goto receive_failed;
  342.          }
  343.  
  344.       if (timeup(master_timeout))
  345.          goto timeout;
  346.  
  347.       if (c >= 0)                               /* noise? */
  348.          {
  349.          hello_timeout = timerset (1000);       /* Look for up to 10 secs  */
  350.          while (((c = PEEKBYTE ()) >= 0) && (c != 0x1f) && (CARRIER))
  351.             {
  352.             if (timeup(hello_timeout))
  353.                break;
  354.             MODEM_IN ();                        /* Eat non-YooHoo chars    */
  355.             }
  356.  
  357.          if (c != 0x1f)                         /* If we didn't get YooHoo */
  358.             {
  359.             CLEAR_INBOUND ();                   /* Throw out what we have  */
  360.             SENDBYTE (ENQ);                     /* Start over with ENQ     */
  361.             }
  362.          }
  363.  
  364.       }                         /* while */
  365.  
  366.  
  367. /*receive_packet:*/
  368.  
  369.    sptr = (char *) (&Hello);
  370.  
  371.    hello_timeout = timerset (3000);
  372.  
  373.    for (i = 0, crc = 0; i < 128; i++)
  374.       {
  375.  
  376.       while (PEEKBYTE () < 0)
  377.          {
  378.          if (timeup (master_timeout) || timeup (hello_timeout))
  379.             goto timeout;
  380.         
  381.          if (got_ESC ())
  382.             {
  383.             sptr = KBD_msg;
  384.             goto receive_failed;
  385.             }
  386.  
  387.          if (!CARRIER)
  388.             {
  389.             sptr = CARRIER_msg;
  390.             goto receive_failed;
  391.             }
  392.  
  393.          time_release ();
  394.      }
  395.  
  396.       c = TIMED_READ (0);
  397.  
  398.       sptr[i] = (char) c;
  399.       crc = xcrc (crc, (byte) c);
  400.       }
  401.  
  402.    if (!CARRIER)
  403.       {
  404.       sptr = CARRIER_msg;
  405.       goto receive_failed;
  406.       }
  407.  
  408.    if (((msb = TIMED_READ (10)) < 0) || ((lsb = TIMED_READ (10)) < 0))
  409.       {
  410.       sptr = SHRT_msg;
  411.       goto hello_error;
  412.       }
  413.  
  414.    if (((msb << 8) | lsb) == crc)
  415.       goto process_hello;
  416.  
  417.    sptr = CRC_msg;
  418.  
  419. hello_error:
  420.  
  421.    if (timeup(master_timeout))
  422.       goto timeout;
  423.  
  424.    if ((num_errs++) > 10)
  425.       {
  426.       sptr = FUBAR_msg;
  427.       goto receive_failed;
  428.       }
  429.  
  430.    CLEAR_INBOUND ();
  431.    SENDBYTE ('?');
  432.    goto watch_for_header;
  433.  
  434. process_hello:
  435.  
  436.    Hello.my_name[42] = '\0';
  437.    Hello.sysop[19] = '\0';
  438.    remote_zone = Hello.my_zone;
  439.    remote_net = Hello.my_net;
  440.    remote_node = Hello.my_node;
  441.    remote_point = Hello.my_point;
  442.  
  443.    remote_capabilities = (Hello.capabilities) | Y_DIETIFNA;
  444.  
  445.    if (nodefind (remote_zone, remote_net, remote_node, 0) && !remote_zone)
  446.       remote_zone = found_zone;
  447.  
  448.    sprintf (junkbuff, "*%s (%u:%u/%u.%u)",
  449.             Hello.my_name,
  450.             remote_zone,
  451.             remote_net,
  452.             remote_node,
  453.             remote_point);
  454.    status_line (junkbuff);
  455.  
  456.    if ((pvtnet >= 0) &&
  457.        ((remote_zone == alias[assumed].Zone) || (remote_zone == 0)) &&
  458.        (remote_net == boss_net) && (remote_node == boss_node) &&
  459.        (remote_point > 0))
  460.       {
  461.       remote_net = pvtnet;
  462.       remote_node = Hello.my_point;
  463.       remote_point = 0;
  464.       }
  465.  
  466.    switch (Hello.product)
  467.       {
  468.       case isBITBRAIN:
  469.       case isFIDO:
  470.       case isSPARK:
  471.       case isSEA:
  472.       case isHENK:
  473.       case isTABBIE:
  474.       case isWOLF:
  475.       case isQMM:
  476.       case isFD:
  477.       case isTERUS:
  478.       case isCYGNUS:
  479.       case isGSPOINT:
  480.       case isBGMAIL:
  481.       case isCROSSBOW:
  482.       case isDBRIDGE:
  483.       case isDAISY:
  484.       case isTHEBOX:
  485.          status_line ("*Remote uses %s Version %d.%02d",
  486.              prodcode[Hello.product], Hello.product_maj, Hello.product_min);
  487.          break;
  488.  
  489.       case isOPUS:
  490.          status_line ("*Remote Uses Opus Version %d.%02d", Hello.product_maj,
  491.                       (Hello.product_min == 48) ? 0 : Hello.product_min);
  492.          break;
  493.       default:
  494.          status_line ("*Remote Uses Program '%02x' Version %d.%02d",
  495.                       Hello.product, Hello.product_maj, Hello.product_min);
  496.          break;
  497.       }
  498.  
  499.    if (Hello.sysop[0])
  500.       status_line (":Sysop: %s", Hello.sysop);
  501.  
  502.    if (remote_point > 0)
  503.       {
  504.       remote_point = 0;
  505.       remote_node = -1;
  506.       }
  507.  
  508.    if (n_getpassword (remote_zone, remote_net, remote_node))
  509.       {
  510.       if (n_password (Hello.my_password, remote_password))
  511.          {
  512.          DTR_OFF ();                            /* He'll never get it right */
  513.          timer (2);                             /* Wait two secs */
  514.          sptr = NULL;                           /* Already logged something */
  515.          goto receive_failed;
  516.          }
  517.       }
  518.  
  519.    CLEAR_INBOUND ();
  520.  
  521.    SENDBYTE (ACK);
  522.    SENDBYTE (YOOHOO);
  523.  
  524.    return (1);
  525.  
  526. timeout:
  527.  
  528.    sptr = TIME_msg;
  529.  
  530. receive_failed:
  531.  
  532.    hup = 1;
  533.    message (sptr);
  534.    b_init ();
  535.    return (0);
  536. }                                               /* Recv Hello */
  537.  
  538. /* END OF FILE: yoohoo.c */
  539.