home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / xtli1.exe / FILERX.C next >
Text File  |  1994-09-13  |  10KB  |  389 lines

  1. /****************************************************************************
  2. **    File:    FILERX.C
  3. **
  4. **    Desc:
  5. **
  6. **        This is an example of TLI communication for a DOS client.  It compiles 
  7. **        fine using MSC 6.0., just make sure you link the appropriate libraries:
  8. **
  9. **            nwcalls+sTLI+sNWIPXSP  (for small memory model).
  10. **
  11. **        It recives a file from the server endpoint.  It's companion program
  12. **        is called FILETX (FILETX.C & FILETX.NLM).
  13. **
  14. **    Disclaimer:
  15. **
  16. **        Novell, Inc. makes no representations or warranties with respect to
  17. **        any NetWare software, and specifically disclaims any express or
  18. **        implied warranties of merchantability, title, or fitness for a
  19. **        particular purpose.  
  20. **
  21. **        Distribution of any NetWare software is forbidden without the
  22. **        express written consent of Novell, Inc.  Further, Novell reserves
  23. **        the right to discontinue distribution of any NetWare software.
  24. **
  25. **        Novell is not responsible for lost profits or revenue, loss of use
  26. **        of the software, loss of data, costs of re-creating lost data, the
  27. **        cost of any substitute equipment or program, or claims by any party
  28. **        other than you.  Novell strongly recommends a backup be made before
  29. **        any software is installed.   Technical support for this software
  30. **        may be provided at the discretion of Novell.
  31. **
  32. **    Programmers:
  33. **
  34. **        Ini    Who                        Firm
  35. **        --------------------------------------------------------------------------
  36. **        ABJ    Adam B. Jerome            Novell Developer Support.
  37. **
  38. **    History:
  39. **
  40. **        When        Who    What
  41. **        --------------------------------------------------------------------------
  42. **        06-29-94    ABJ    First code.
  43. */
  44.  
  45. /****************************************************************************
  46. **    Include headers, macros, etc.
  47. */
  48.     /*------------------------------------------------------------------------
  49.     **    Standard Library.
  50.     */
  51.     #include <stdlib.h>  /* exit()     */
  52.     #include <stdio.h>     /* printf() */
  53.     #include <string.h>    /*    memset(), strupr(), memcpy() */
  54.  
  55.     /*------------------------------------------------------------------------
  56.     **    NetWare.
  57.     */
  58.     #define  NWDOS
  59.     #define     O_RDWR 0x02 
  60.     #include <nwcalls.h>    /*    NWReadPropertyValue(), NWWordSwap()    */ 
  61.     #include    <tiuser.h>    /* t_open(), t_info, t_errno, TSYSERR, t_error(),... */
  62.  
  63.     /*------------------------------------------------------------------------
  64.     **    Module
  65.     */
  66.    #define SERVER_TYPE    0x0275    /* Novell Developer Support test server type */
  67.  
  68. /****************************************************************************
  69. **    Get the Network:node:socket address of a server.
  70. **
  71. ** NODE: This method assumes that we are attached to a NetWare fileserver.
  72. */
  73. int GetIPXServerAddress(void *serverAddress, char *serverName, unsigned short serverType)
  74.     {
  75.     BYTE     propValue[128];
  76.     int      cCode;
  77.     BYTE    moreSegments;
  78.     BYTE    propertyFlags;
  79.  
  80.     if(serverAddress != NULL)
  81.         memset(serverAddress, 0x00, 4+6+2);
  82.  
  83.     cCode=NWReadPropertyValue(
  84.         /* connection        */ 0,                            /* default connection */
  85.          /*    objectName        */    strupr(serverName),
  86.         /* objectType        */    NWWordSwap(serverType),
  87.         /*    propertyName    */    "NET_ADDRESS",
  88.         /*    dataSetIndex    */    1,
  89.         /*    dataBuffer        */    propValue,
  90.         /* moreFlag            */    &moreSegments,
  91.         /*    propertyFlags    */    &propertyFlags
  92.         );
  93.  
  94.     if(serverAddress != NULL)
  95.         if(!cCode) memcpy(serverAddress, propValue, 4+6+2);
  96.  
  97.     return(cCode);
  98.     }
  99.  
  100. /****************************************************************************
  101. **    Program start
  102. */
  103. void main(int argc, char **argv)
  104.     {
  105.     struct t_info  tInfo;
  106.     struct t_call *tCall         = NULL;
  107.     int                 flags         = 0;
  108.     int                bindFlag     = (-1);
  109.     int                connectFlag    = (-1);
  110.     int                i;
  111.     int                fh;
  112.     int                cCode;
  113.     char              buf[47+1];
  114.     int                keepLooping = 1;
  115.  
  116.     /*------------------------------------------------------------------------
  117.     **    Check command line args.
  118.     */
  119.    if(argc != 2)
  120.       {
  121.       printf("Usage: %s <server's name>\n", argv[0]);
  122.       goto ERR_DONE;
  123.       }
  124.  
  125.     /*------------------------------------------------------------------------
  126.     ** Initialize a TLI endpoint.    
  127.     */
  128.    fh=t_open(
  129.             /* I-    path    */    "/dev/nspx",
  130.             /*    I-    oflag    */    O_RDWR,
  131.             /*    -O    info    */    &tInfo
  132.             );
  133.    if(fh == -1)
  134.       {
  135.         switch(t_errno)
  136.             {
  137.             case TSYSERR:
  138.             case TBADFLAG:
  139.             case TBADNAME:
  140.             default:
  141.               t_error("ERROR");
  142.                 break;
  143.             }
  144.       goto ERR_DONE;
  145.       }
  146.  
  147.     /*------------------------------------------------------------------------
  148.     ** Have TLI assign an endpoint address (IPX socket Number). 
  149.     */
  150.     bindFlag=t_bind(fh, NULL, NULL);
  151.     if(bindFlag == -1)
  152.         {
  153.       t_error("ERROR: t_bind");
  154.       goto ERR_DONE;
  155.         }
  156.  
  157.     /*------------------------------------------------------------------------
  158.     ** Allocate and initialize TLI structures.
  159.     */
  160.     tCall = (struct t_call *)t_alloc(
  161.         /* fd             */    fh,
  162.         /* struct_type    */    T_CALL,
  163.         /* fields        */    T_ADDR
  164.         );
  165.     if(tCall == NULL)
  166.         {
  167.         switch(t_errno)
  168.             {
  169.             case TBADF:
  170.             case TSYSERR:
  171.             case TNOTSUPPORT:
  172.             case TNOSTRUCTYPE:
  173.             default:
  174.                t_error("ERROR:");
  175.             }
  176.       goto ERR_DONE;
  177.         }
  178.  
  179.     /*------------------------------------------------------------------------
  180.     ** Get the server's Net:node:socket via BINDERY query.
  181.     */
  182.     tCall->addr.len = sizeof(IPX_ADDR);
  183.     cCode=GetIPXServerAddress(tCall->addr.buf, argv[1], SERVER_TYPE);
  184.     switch(cCode)
  185.         {
  186.         case 0:
  187.             break;
  188.  
  189.         case 252:
  190.             printf("ERROR:  No such object. [%s:%04hX]\n", argv[1], SERVER_TYPE);
  191.             goto ERR_DONE;
  192.  
  193.         default:
  194.             printf("ERROR:  GetServerAddress() returned 0x%04X\n", cCode);
  195.             goto ERR_DONE;
  196.         }
  197.  
  198.     /*------------------------------------------------------------------------
  199.     ** Establish connection.
  200.     */
  201.     connectFlag=t_connect(
  202.         /*    I-    fd            */    fh,
  203.         /* I-    tCall        */    tCall,
  204.         /* -O    rcvcall    */    NULL
  205.         );                                 
  206.     if(connectFlag == -1) switch(t_errno)
  207.         {
  208.       case TBADF:
  209.         case TOUTSTATE:
  210.         case TNODATA:
  211.         case TBADADDR:
  212.         case TBADOPT:
  213.         case TBADDATA:
  214.         case TBUFOVFLW:
  215.         case TLOOK:
  216.         case TNOTSUPPORT:
  217.         case TSYSERR:
  218.         default:
  219.             t_error("ERROR");
  220.           goto ERR_DONE;
  221.         }
  222.  
  223.     /*------------------------------------------------------------------------
  224.     ** Recieve file.
  225.     */
  226.     while(keepLooping)
  227.         {
  228.         memset(buf, 0x00, sizeof(buf));
  229.        cCode=t_rcv(
  230.             /* I-    fd            */    fh,
  231.             /* -O    buf        */    buf,
  232.             /* I-    nbytes    */    sizeof(buf)-1,
  233.             /* -O    flags        */    &flags
  234.             );
  235.        if(cCode == (-1))
  236.             {
  237.             switch(t_errno)
  238.                 {
  239.                 case TLOOK:
  240.                     /*------------------------------------------------------------
  241.                     **    WHAT DO YOU WANT NOW!
  242.                     */    
  243.                    cCode = t_look(fh);
  244.                     switch(cCode)
  245.                         {
  246.                         /*------------------------------------------------------------
  247.                         **    Disconnect request recieved?
  248.                         */
  249.                         case T_DISCONNECT:
  250.                             keepLooping=0;
  251.                             continue; 
  252.  
  253.                         /*------------------------------------------------------------
  254.                         ** Nothing happened.  Try again.
  255.                         */
  256.                         case 0:
  257.                             break;
  258.  
  259.                         /*------------------------------------------------------------
  260.                         **    We don't expect these things to happen.
  261.                         */
  262.                         case (-1):
  263.                         case T_LISTEN:
  264.                         case T_CONNECT:
  265.                         case T_DATA:
  266.                         case T_EXDATA:
  267.                         case T_UDERR:
  268.                         case T_ORDREL:
  269.                         case T_GODATA:
  270.                         case T_GOEXDATA:
  271.                         default:
  272.                            printf("ERROR: #%d\n", cCode);
  273.                             keepLooping=0;
  274.                             continue;
  275.                         }
  276.                     break;
  277.  
  278.                 /*---------------------------------------------------------------
  279.                 **    We don't expect these things to happen.
  280.                 */
  281.                 case TBADF:
  282.                 case TNODATA:
  283.                 case TNOTSUPPORT:
  284.                 case TOUTSTATE:
  285.                 case TSYSERR:
  286.                 default:
  287.                     t_error("ERROR: ");
  288.                     keepLooping=0;
  289.                     continue;
  290.                 }
  291.             }
  292.         else
  293.            printf("%s", buf);
  294.         }
  295.  
  296.  
  297. ERR_DONE:
  298.     /*------------------------------------------------------------------------
  299.     ** Destroy connection.
  300.     */
  301.     if(connectFlag != (-1))
  302.         {
  303.         
  304.         /*------------------------------------------------------------------
  305.         **    Get connection status.
  306.         */
  307.         cCode = !(T_DISCONNECT);
  308.         for(i=0; (i<60) && (cCode != T_DISCONNECT); i++)
  309.             {
  310.            cCode = t_look(fh);
  311.             switch(cCode)
  312.                 {
  313.                 /*------------------------------------------------------------
  314.                 **    Disconnect yet?
  315.                 */
  316.                 case T_DISCONNECT:
  317.                     continue; /* to top of (timmer) for loop */
  318.  
  319.                 /*------------------------------------------------------------
  320.                 ** Nothing happened.  Try again.
  321.                 */
  322.                 case 0:
  323.                     break;
  324.  
  325.                 /*------------------------------------------------------------
  326.                 **    We don't expect these things to happen.
  327.                 */
  328.                 case (-1):
  329.                 case T_LISTEN:
  330.                 case T_CONNECT:
  331.                 case T_DATA:
  332.                 case T_EXDATA:
  333.                 case T_UDERR:
  334.                 case T_ORDREL:
  335.                 case T_GODATA:
  336.                 case T_GOEXDATA:
  337.                 default:
  338.                    printf("ERROR: #%d\n", cCode);
  339.                     i=1000;
  340.                     continue;
  341.                 }
  342.  
  343.             }
  344.  
  345.         if(i == 60) printf("ERROR: Discconnect timeout\n");
  346.     
  347.         /*---------------------------------------------------------------------
  348.         **    Disconnect.
  349.         */
  350.         cCode=t_rcvdis(fh, NULL);
  351.         if(cCode == -1) switch(t_errno)
  352.             {
  353.             /*------------------------------------------------------------------
  354.             **    We expect none of these.
  355.             */
  356.             case TBADF:
  357.             case TNODIS:
  358.             case TBUFOVFLW:
  359.             case TNOTSUPPORT:
  360.             case TOUTSTATE:
  361.             case TSYSERR:
  362.             default:
  363.                 t_error("ERROR");
  364.                 break;
  365.             }
  366.         }
  367.  
  368.     /*------------------------------------------------------------------------
  369.     ** Release address. (SPX socket).
  370.     */
  371.     if(bindFlag != (-1))
  372.         if(t_unbind(fh) == (-1)) t_error("ERROR: t_unbind");
  373.     
  374.     /*------------------------------------------------------------------------
  375.     ** Free up memory as needed.
  376.     */
  377.     if(tCall != NULL)
  378.         if(t_free((char *)tCall, T_CALL) == -1) t_error("ERROR: t_free");
  379.  
  380.     /*------------------------------------------------------------------------
  381.     ** Close the TLI endpoint.
  382.     */
  383.    if(fh != -1)
  384.         if(t_close(fh) == -1) t_error("ERROR: t_close");
  385.  
  386.     exit(0);
  387.     }
  388.  
  389.