home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / EXTRAS / UUCODE / UUPC / TEST / UPC12ES2.ZIP / UUCICO / commlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-20  |  16.8 KB  |  469 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       c o m m l i b . C                                            */
  3. /*                                                                    */
  4. /*       Generic communications library interface for UUPC/extended.  */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*    Changes Copyright (c) 1990-1993 by Kendra Electronic            */
  9. /*    Wonderworks.                                                    */
  10. /*                                                                    */
  11. /*    All rights reserved except those explicitly granted by the      */
  12. /*    UUPC/extended license agreement.                                */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: commlib.c 1.14 1993/11/20 14:48:53 ahd Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: commlib.c $
  24.  * Revision 1.14  1993/11/20  14:48:53  ahd
  25.  * Add support for passing port name/port handle/port speed/user id to child
  26.  *
  27.  * Revision 1.13  1993/11/16  05:37:01  ahd
  28.  * Up 16 bit buffer size
  29.  *
  30.  * Revision 1.12  1993/11/06  17:56:09  rhg
  31.  * Drive Drew nuts by submitting cosmetic changes mixed in with bug fixes
  32.  *
  33.  * Revision 1.11  1993/10/12  01:32:46  ahd
  34.  * Normalize comments to PL/I style
  35.  *
  36.  * Revision 1.10  1993/10/07  22:51:00  ahd
  37.  * Allocate communications input buffer for selected suites
  38.  *
  39.  * Revision 1.9  1993/10/02  23:13:29  ahd
  40.  * Allow suppressing TCPIP support
  41.  *
  42.  * Revision 1.8  1993/09/27  00:45:20  ahd
  43.  * Allow named pipes under OS/2 16 bit
  44.  *
  45.  * Revision 1.7  1993/09/24  03:43:27  ahd
  46.  * Add os/2 named pipes
  47.  *
  48.  * Revision 1.6  1993/09/20  04:46:34  ahd
  49.  * OS/2 2.x support (BC++ 1.0 support)
  50.  * TCP/IP support from Dave Watt
  51.  * 't' protocol support
  52.  *
  53.  * Revision 1.5  1993/07/22  23:22:27  ahd
  54.  * First pass at changes for Robert Denny's Windows 3.1 support
  55.  *
  56.  * Revision 1.4  1993/07/13  01:13:32  ahd
  57.  * Don't print NULL communications suite name!
  58.  *
  59.  * Revision 1.3  1993/07/11  14:38:32  ahd
  60.  * Display chosen suite
  61.  *
  62.  * Revision 1.2  1993/05/30  15:25:50  ahd
  63.  * Multiple driver support
  64.  *
  65.  * Revision 1.1  1993/05/30  00:01:47  ahd
  66.  * Initial revision
  67.  *
  68.  */
  69.  
  70. /*--------------------------------------------------------------------*/
  71. /*                        System include files                        */
  72. /*--------------------------------------------------------------------*/
  73.  
  74. #include <stdio.h>
  75. #include <stdlib.h>
  76. #include <string.h>
  77. #include <time.h>
  78.  
  79. /*--------------------------------------------------------------------*/
  80. /*                       UUPC/extended includes                       */
  81. /*--------------------------------------------------------------------*/
  82.  
  83. #include "lib.h"
  84. #include "hlib.h"
  85. #include "commlib.h"
  86. #include "dcp.h"
  87. #include "hostable.h"
  88. #include "usertabl.h"
  89. #include "security.h"
  90. #include "modem.h"
  91.  
  92. #include "ulib.h"             /* Native communications interface      */
  93.  
  94. #if !defined(BIT32ENV) && !defined(FAMILYAPI) && !defined(_Windows)
  95. #include "ulibfs.h"           /* DOS FOSSIL interface                 */
  96. #include "ulib14.h"           /* DOS ARTISOFT INT14 interface         */
  97. #endif
  98.  
  99. /*--------------------------------------------------------------------*/
  100. /*         Define table for looking up communications functions       */
  101. /*--------------------------------------------------------------------*/
  102.  
  103. typedef struct _COMMSUITE {
  104.         char     *type;
  105.         ref_activeopenline      activeopenline;
  106.         ref_passiveopenline     passiveopenline;
  107.         ref_sread               sread;
  108.         ref_swrite              swrite;
  109.         ref_ssendbrk            ssendbrk;
  110.         ref_closeline           closeline;
  111.         ref_SIOSpeed            SIOSpeed;
  112.         ref_flowcontrol         flowcontrol;
  113.         ref_hangup              hangup;
  114.         ref_GetSpeed            GetSpeed;
  115.         ref_CD                  CD;
  116.         ref_WaitForNetConnect   WaitForNetConnect;
  117.         ref_GetComHandle        GetComHandle;
  118.         boolean  network;
  119.         boolean  buffered;
  120.         char     *netDevice;           /* Network device name         */
  121. } COMMSUITE;
  122.  
  123. /*--------------------------------------------------------------------*/
  124. /*       Use the NOTCPIP to suppress the TCP/IP when you don't        */
  125. /*       have WINSOCK.H                                               */
  126. /*--------------------------------------------------------------------*/
  127.  
  128. #if defined(WIN32) || defined(_Windows)
  129. #ifndef NOTCPIP
  130. #include "ulibip.h"           /* Windows sockets on TCP/IP interface  */
  131. #define TCPIP
  132. #endif
  133. #endif
  134.  
  135. #if defined(__OS2__) || defined(FAMILYAPI)
  136. #include "ulibnmp.h"          /* OS/2 named pipes interface           */
  137. #endif
  138.  
  139. #define NATIVE "internal"
  140.  
  141. /*--------------------------------------------------------------------*/
  142. /*                          Global variables                          */
  143. /*--------------------------------------------------------------------*/
  144.  
  145. boolean portActive;         /* Port active flag for error handler   */
  146. boolean traceEnabled;        /* Trace active flag                     */
  147. size_t commBufferLength = 0;
  148. size_t commBufferUsed   = 0;
  149. char *commBuffer = NULL;
  150.  
  151. ref_activeopenline activeopenlinep;
  152. ref_passiveopenline passiveopenlinep;
  153. ref_sread sreadp;
  154. ref_swrite swritep;
  155. ref_ssendbrk ssendbrkp;
  156. ref_closeline closelinep;
  157. ref_SIOSpeed SIOSpeedp;
  158. ref_flowcontrol flowcontrolp;
  159. ref_hangup hangupp;
  160. ref_GetSpeed GetSpeedp;
  161. ref_CD CDp;
  162. ref_WaitForNetConnect WaitForNetConnectp;
  163. ref_GetComHandle GetComHandlep;
  164.  
  165. /*--------------------------------------------------------------------*/
  166. /*                          Local variables                           */
  167. /*--------------------------------------------------------------------*/
  168.  
  169. static FILE *traceStream;    /* Stream used for trace file            */
  170.  
  171. static short   traceMode;    /* Flag for last data (input/output)     */
  172.                              /* written to trace log                  */
  173.  
  174. static boolean network = FALSE;  /* Current communications suite is   */
  175.                                  /* network oriented                  */
  176.  
  177. currentfile();
  178.  
  179. int dummyGetComHandle( void );
  180.  
  181. /*--------------------------------------------------------------------*/
  182. /*       c h o o s e C o m m u n i c a t i o n s                      */
  183. /*                                                                    */
  184. /*       Choose communications suite to use                           */
  185. /*--------------------------------------------------------------------*/
  186.  
  187. boolean chooseCommunications( const char *name )
  188. {
  189.    static COMMSUITE suite[] =
  190.    {
  191.         { NATIVE,                      /* Default for any opsys        */
  192.           nopenline, nopenline, nsread, nswrite,
  193.           nssendbrk, ncloseline, nSIOSpeed, nflowcontrol, nhangup,
  194.           nGetSpeed,
  195.           nCD,
  196.           (ref_WaitForNetConnect) NULL,
  197. #if defined(BIT32ENV) || defined(FAMILYAPI)
  198.           nGetComHandle,
  199.           FALSE,                       /* Not network based           */
  200.           TRUE,                        /* Buffered under OS/2 and Windows NT  */
  201. #else
  202.           dummyGetComHandle,
  203.           FALSE,                       /* Not network based           */
  204.           TRUE,                        /* Unbuffered for DOS, Windows 3.x  */
  205. #endif
  206.           NULL                         /* No network device name      */
  207.         },
  208. #if !defined(BIT32ENV) && !defined(_Windows) && !defined(FAMILYAPI)
  209.         { "fossil",                    /* MS-DOS FOSSIL driver        */
  210.           fopenline, fopenline, fsread, fswrite,
  211.           fssendbrk, fcloseline, fSIOSpeed, fflowcontrol, fhangup,
  212.           fGetSpeed,
  213.           fCD,
  214.           (ref_WaitForNetConnect) NULL,
  215.           dummyGetComHandle,
  216.           FALSE,                       /* Not network oriented        */
  217.           FALSE,                       /* Not buffered                 */
  218.           NULL                         /* No network device name      */
  219.         },
  220.         { "articomm",                  /* MS-DOS ARTISOFT INT14 driver  */
  221.           iopenline, iopenline, isread, iswrite,
  222.           issendbrk, icloseline, iSIOSpeed, iflowcontrol, ihangup,
  223.           iGetSpeed,
  224.           iCD,
  225.           (ref_WaitForNetConnect) NULL,
  226.           dummyGetComHandle,
  227.           FALSE,                       /* Not network oriented        */
  228.           FALSE,                       /* Not buffered                 */
  229.           NULL                         /* No network device name      */
  230.         },
  231. #endif
  232.  
  233. #if defined(TCPIP)
  234.         { "tcp/ip",                    /* Win32 TCP/IP Winsock interface  */
  235.           tactiveopenline, tpassiveopenline, tsread, tswrite,
  236.           tssendbrk, tcloseline, tSIOSpeed, tflowcontrol, thangup,
  237.           tGetSpeed,
  238.           tCD,
  239.           tWaitForNetConnect,
  240.           dummyGetComHandle,
  241.           TRUE,                        /* Network oriented            */
  242.           TRUE,                        /* Uses internal buffer        */
  243.           "tcptty",                    /* Network device name         */
  244.         },
  245. #endif
  246.  
  247. #if defined(__OS2__) || defined(FAMILYAPI)
  248.         { "namedpipes",                /* OS/2 named pipes            */
  249.           pactiveopenline, ppassiveopenline, psread, pswrite,
  250.           pssendbrk, pcloseline, pSIOSpeed, pflowcontrol, phangup,
  251.           pGetSpeed,
  252.           pCD,
  253.           pWaitForNetConnect,
  254.           dummyGetComHandle,
  255.           TRUE,                        /* Network oriented            */
  256.           TRUE,                        /* Uses internal buffer        */
  257.           "pipe",                      /* Network device name         */
  258.         },
  259. #endif
  260.         { NULL }                       /* End of list                 */
  261.    };
  262.  
  263.    int subscript = 0;
  264.  
  265. /*--------------------------------------------------------------------*/
  266. /*                   Search for name in suite table                   */
  267. /*--------------------------------------------------------------------*/
  268.  
  269.    while (( name  != NULL ) && (suite[subscript].type != NULL ))
  270.    {
  271.       if ( equali(name,suite[subscript].type))
  272.          break;                           /* Success!                 */
  273.       else
  274.          subscript++;
  275.    } /* while */
  276.  
  277.    if ( suite[subscript].type == NULL )
  278.    {
  279.       printmsg(0,"chooseCommunications: Invalid suite name %s",
  280.                   name );
  281.       return FALSE;
  282.    }
  283.  
  284. /*--------------------------------------------------------------------*/
  285. /*       We have a valid suite, define the routines to use and        */
  286. /*       return to caller                                             */
  287. /*--------------------------------------------------------------------*/
  288.  
  289.    activeopenlinep    = suite[subscript].activeopenline;
  290.    passiveopenlinep   = suite[subscript].passiveopenline;
  291.    sreadp             = suite[subscript].sread;
  292.    swritep            = suite[subscript].swrite;
  293.    ssendbrkp          = suite[subscript].ssendbrk;
  294.    closelinep         = suite[subscript].closeline;
  295.    SIOSpeedp          = suite[subscript].SIOSpeed;
  296.    flowcontrolp       = suite[subscript].flowcontrol;
  297.    hangupp            = suite[subscript].hangup;
  298.    GetSpeedp          = suite[subscript].GetSpeed;
  299.    CDp                = suite[subscript].CD;
  300.    WaitForNetConnectp = suite[subscript].WaitForNetConnect;
  301.    GetComHandlep      = suite[subscript].GetComHandle;
  302.    network            = suite[subscript].network;
  303.  
  304. /*--------------------------------------------------------------------*/
  305. /*                  Override device name as required                  */
  306. /*--------------------------------------------------------------------*/
  307.  
  308.    if ( suite[subscript].netDevice != NULL )
  309.       M_device = suite[subscript].netDevice;
  310.  
  311.    if ( suite[subscript].buffered && ! commBufferLength)
  312.    {
  313.  
  314. #ifdef BIT32ENV
  315.       commBufferLength = (MAXPACK * 4);      /* Generous to reduce I/O's  */
  316. #else
  317.       commBufferLength = (MAXPACK * 2) + 20; /* 2 packet plus headers    */
  318. #endif
  319.  
  320.       commBuffer = malloc( commBufferLength );
  321.       checkref( commBuffer );
  322.  
  323.    } /* if */
  324.    else if ( (! suite[subscript].buffered) && commBufferLength )
  325.    {
  326.       commBufferLength = 0;
  327.       free( commBuffer );
  328.       commBuffer = NULL;
  329.    }
  330.    commBufferUsed = 0;
  331.  
  332.    printmsg(equal(suite[subscript].type, NATIVE) ? 5 : 4,
  333.             "chooseCommunications: Chose suite %s",
  334.             suite[subscript].type );
  335.  
  336.    return TRUE;
  337.  
  338. } /* chooseCommunications */
  339.  
  340. /*--------------------------------------------------------------------*/
  341. /*       t r a c e S t a r t                                          */
  342. /*                                                                    */
  343. /*       Begin communicatons line tracing                             */
  344. /*--------------------------------------------------------------------*/
  345.  
  346. boolean traceStart( const char *port )
  347. {
  348.    char *linelog;
  349.    time_t now;
  350.  
  351.    if ( ! traceEnabled )
  352.       return FALSE;
  353.  
  354.    linelog = normalize( "LineData.Log" );
  355.  
  356.    if ( traceStream != NULL )
  357.    {
  358.       printmsg(0,"traceOn: Trace file %s already open!", linelog);
  359.       panic();
  360.    }
  361.  
  362.    traceStream = FOPEN( linelog ,"a", BINARY_MODE);
  363.  
  364.    if ( traceStream == NULL )
  365.    {
  366.       printerr( linelog );
  367.       printmsg(0, "Unable to open trace file, tracing disabled");
  368.       traceEnabled = FALSE;
  369.       return FALSE;
  370.    }
  371.  
  372.    time( &now );
  373.  
  374.    fprintf(traceStream,"Trace begins for port %s at %s",
  375.            port, ctime( &now ));
  376.  
  377.    printmsg(4,"Tracing communications port %s in file %s",
  378.             port, linelog );
  379.  
  380.    traceMode  = 2;               /* Make sure first trace includes     */
  381.                                  /* prefix with direction              */
  382.  
  383.    return TRUE;                  /* Success to caller                 */
  384.  
  385. } /* traceStart */
  386.  
  387. /*--------------------------------------------------------------------*/
  388. /*       t r a c e S t o p                                            */
  389. /*                                                                    */
  390. /*       Terminate communications line tracing                        */
  391. /*--------------------------------------------------------------------*/
  392.  
  393. void traceStop( void )
  394. {
  395.    if ( traceStream != NULL )
  396.    {
  397.       time_t now = time( NULL );
  398.       fprintf(traceStream,"\nTrace complete at %s",  ctime( &now ));
  399.       fclose( traceStream );
  400.       traceStream = NULL;
  401.    }
  402.  
  403. } /* traceStop */
  404.  
  405. /*--------------------------------------------------------------------*/
  406. /*       t r a c e D a t a                                            */
  407. /*                                                                    */
  408. /*       Write traced data to the log                                 */
  409. /*--------------------------------------------------------------------*/
  410.  
  411. void traceData( const char *data,
  412.                 const unsigned len,
  413.                 const boolean output)
  414. {
  415. #ifdef VERBOSE
  416.    unsigned subscript;
  417. #endif
  418.  
  419.  
  420.    if ( ! traceEnabled || ! len )
  421.       return;
  422.  
  423.    printmsg(network ? 4 : 15, "traceData: %u bytes %s",
  424.                len,
  425.                output ? "written" : "read" );
  426.  
  427.    if ( traceMode != (short) output )
  428.    {
  429.       fputs(output ? "\nWrite: " : "\nRead:  ",traceStream );
  430.       traceMode = (short) output;
  431.    }
  432.  
  433. #ifdef VERBOSE
  434.    for (subscript = 0; subscript < len; subscript++)
  435.    {
  436.       fprintf( traceStream, "%2.2x", data[subscript] );
  437.    } /* for */
  438. #else
  439.  
  440.    fwrite(data, 1, len, traceStream ); /* Write out raw data           */
  441.  
  442. #endif
  443.  
  444. } /* traceData */
  445.  
  446. /*--------------------------------------------------------------------*/
  447. /*       I s N e t w o r k                                            */
  448. /*                                                                    */
  449. /*       Report if current communications suite is network oriented   */
  450. /*--------------------------------------------------------------------*/
  451.  
  452. boolean IsNetwork(void)
  453. {
  454.    return network;         /* Preset when suite initialized           */
  455. }
  456.  
  457.  
  458. /*--------------------------------------------------------------------*/
  459. /*       d u m m y G e t C o m H a n d l e                            */
  460. /*                                                                    */
  461. /*       Return invalid communications handle for unsupported         */
  462. /*       environments                                                 */
  463. /*--------------------------------------------------------------------*/
  464.  
  465. int dummyGetComHandle( void )
  466. {
  467.    return -1;
  468. }
  469.