home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / sapgen.exe / SAPGEN.C next >
Text File  |  1994-08-08  |  8KB  |  268 lines

  1. /****************************************************************************
  2. **    File:    SAPGEN.C
  3. **
  4. **    Desc:    Sample General Service Query Program
  5. **
  6. **        Run SAPGEN from the command line. This program sends out
  7. **        a general service query for File Servers (type 4), and 
  8. **        drops in to a loop that receives the response packets and
  9. **        prints the results on the screen. 
  10. **        
  11. **        
  12. **        
  13. **        
  14. **
  15. **        DISCLAIMER  
  16. **  
  17. **    Novell, Inc. makes no representations or warranties with respect to
  18. **    any NetWare software, and specifically disclaims any express or
  19. **    implied warranties of merchantability, title, or fitness for a
  20. **    particular purpose.  
  21. **
  22. **    Distribution of any NetWare software is forbidden without the
  23. **    express written consent of Novell, Inc.  Further, Novell reserves
  24. **    the right to discontinue distribution of any NetWare software.
  25. **    
  26. **    Novell is not responsible for lost profits or revenue, loss of use
  27. **    of the software, loss of data, costs of re-creating lost data, the
  28. **    cost of any substitute equipment or program, or claims by any party
  29. **    other than you.  Novell strongly recommends a backup be made before
  30. **    any software is installed.   Technical support for this software
  31. **    may be provided at the discretion of Novell.
  32. **
  33. **    Programmers:
  34. **
  35. **        Ini    Who                        Firm
  36. **        -----------------------------------------------------------------------
  37. **        KLB    Karl Bunnell                Novell Developer Support.
  38. **
  39. **    History:
  40. **
  41. **        When        Who    What
  42. **        -----------------------------------------------------------------------
  43. **        08-08-94    klb    First code.
  44. */
  45.  
  46. /****************************************************************************
  47. **    Include headers, macros, function prototypes, etc.
  48. */
  49.  
  50.     /*------------------------------------------------------------------------
  51.     **    ANSI
  52.     */
  53.     #include <stdio.h>
  54.  
  55.  
  56.     /*------------------------------------------------------------------------
  57.     **    NetWare
  58.     */
  59.     #define    NWDOS
  60.     #include <nwcalls.h>
  61.  
  62.  
  63. /****************************************************************************
  64. **    Global Structure Definitions
  65. */
  66.  
  67.     /*------------------------------------------------------------------------
  68.     **    NetWare IPX
  69.     */
  70.  
  71.     typedef struct IPXAddress
  72.         {
  73.         BYTE    network[4];             /* high-low */
  74.         BYTE    node[6];                /* high-low */
  75.         BYTE    socket[2];              /* high-low */
  76.         }IPXAddress;
  77.  
  78.  
  79.     typedef struct IPXHeader
  80.         {
  81.         WORD        checkSum;           /* high-low */
  82.         WORD        length;             /* high-low */
  83.         BYTE        transportControl;
  84.         BYTE        packetType;
  85.         IPXAddress  destination;
  86.         IPXAddress  source;
  87.         }IPXHeader;
  88.  
  89.     typedef struct ECBFragment
  90.         {
  91.         void far *address;
  92.         WORD    size;                   /* low-high */
  93.         }ECBFragment;
  94.  
  95.     typedef struct ECB
  96.         {
  97.         void far *linkAddress;
  98.         void (far *ESRAddress)();
  99.         BYTE        inUseFlag;
  100.         BYTE        completionCode;
  101.         WORD        socketNumber;               /* high-low */
  102.         BYTE        IPXWorkspace[4];            /* N/A */
  103.         BYTE        driverWorkspace[12];        /* N/A */
  104.         BYTE        immediateAddress[6];        /* high-low */
  105.         WORD        fragmentCount;              /* low-high */
  106.         ECBFragment fragmentDescriptor[5];
  107.         }ECB;
  108.  
  109.     typedef struct
  110.         {
  111.         WORD            responseType;
  112.         WORD            serverType;
  113.         BYTE            serverName[48];
  114.         BYTE            network[4];
  115.         BYTE            node[6];
  116.        WORD            sock;
  117.         WORD            intNetworks;
  118.         } SAP_RECORD;
  119.  
  120.     typedef struct
  121.         {
  122.         WORD            queryType;
  123.         WORD            serverType;
  124.         } QUERY_PACKET;
  125.  
  126. /****************************************************************************
  127. **    This function is the entire program, as it is very simple.
  128. **    Prompt user for input, open a stream, make a call, close the stream.
  129. */
  130.  
  131. void main(void)
  132. {
  133.     WORD                 Socket=0x0000;
  134.     SAP_RECORD        sapInfo;
  135.     QUERY_PACKET    qPacket;
  136.     int                ccode, i, cnt;
  137.     IPXHeader        sHeader;
  138.     ECB                sECB;
  139.     ECB                rECB;
  140.     IPXHeader        rHeader;
  141.     char                ch;
  142.     WORD        sapSocket;
  143.     
  144.     ccode = IPXInitialize();
  145.  
  146.  
  147.     /*------------------------------------------------------------------------
  148.     **    Open a dynamic socket to listen on.
  149.     */
  150.  
  151.     IPXOpenSocket((BYTE *)&Socket, (BYTE)0);
  152.  
  153.     printf("\nIPXOpenSocket returned : %04X\n", NWWordSwap(Socket));
  154.  
  155.     /*------------------------------------------------------------------------
  156.     **    Initialize fields for Receive IPX Header and Receive ECB
  157.     */
  158.  
  159.     rHeader.packetType = (char)4;
  160.     rHeader.length = NWWordSwap(sizeof(IPXHeader));
  161.  
  162.     rECB.ESRAddress = (void far *)NULL;
  163.     rECB.socketNumber= Socket;
  164.     rECB.fragmentCount = 2;
  165.     rECB.fragmentDescriptor[0].address = &rHeader;
  166.     rECB.fragmentDescriptor[0].size = sizeof(IPXHeader);
  167.     rECB.fragmentDescriptor[1].address = &sapInfo;
  168.     rECB.fragmentDescriptor[1].size = sizeof(sapInfo);
  169.  
  170.     /*------------------------------------------------------------------------
  171.     **    Start listening before you send!
  172.     */
  173.  
  174.     IPXListenForPacket(&rECB);
  175.  
  176.     /*------------------------------------------------------------------------
  177.     **    Initialize Send IPX Header, Query Packet and Send ECB
  178.     */
  179.  
  180.  
  181.     for (i=0; i<4; ++i)
  182.     sHeader.destination.network[i] = (BYTE)0x00;
  183.     for (i=0; i<6; ++i)
  184.     sHeader.destination.node[i] = (BYTE)0xFF;
  185.     sapSocket=NWWordSwap(0x452);
  186.     memcpy(sHeader.destination.socket, &sapSocket, 2);
  187.  
  188.     /*------------------------------------------------------------------------
  189.     **    Query Services Packet
  190.     */
  191.  
  192.     qPacket.queryType = NWWordSwap(0x01);             // General Service Query
  193.     qPacket.serverType = NWWordSwap(0x0004);        // File server type
  194.  
  195.     sHeader.length = NWWordSwap(sizeof(IPXHeader));
  196.     sHeader.packetType = 4;
  197.  
  198.     sECB.ESRAddress = (void far *)NULL;
  199.     sECB.inUseFlag = 0;
  200.     sECB.fragmentCount = 2;
  201.     sECB.socketNumber = Socket;
  202.     for (i=0; i<6; ++i)
  203.         sECB.immediateAddress[i] = 0xFF;
  204.     sECB.fragmentDescriptor[0].address = (&sHeader);
  205.     sECB.fragmentDescriptor[0].size = sizeof(IPXHeader);
  206.     sECB.fragmentDescriptor[1].address = (&qPacket);
  207.     sECB.fragmentDescriptor[1].size = sizeof(QUERY_PACKET);
  208.  
  209.     /*------------------------------------------------------------------------
  210.     **    Send out the Query Packet!
  211.     */
  212.  
  213.     IPXSendPacket(&sECB);
  214.     while(sECB.inUseFlag)
  215.         IPXRelinquishControl();
  216.  
  217.     printf("\n Packet is sent!! \n");
  218.  
  219.     printf("\n Waiting for packet: \n");
  220.  
  221.     /*------------------------------------------------------------------------
  222.     **    Drop into this loop until a key is pressed. The results are printed
  223.     ** to the screen. The data from these packets could be extracted and used
  224.     ** for other purposes. Also note that only one receive ECB is being posted
  225.     ** to listen for packets. Depending on the circumstances this may not be
  226.     ** sufficient to receive all the responses sent from servers on the LAN. 
  227.     */
  228.  
  229.  
  230.     while(!kbhit())
  231.         {
  232.         while(rECB.inUseFlag)
  233.             {
  234.             if(kbhit())
  235.                 {
  236.                 while(kbhit())
  237.                     {
  238.                     IPXCancelEvent(&rECB);             // close shop 
  239.                     IPXCloseSocket(Socket);
  240.                     printf("\n...Receiving of SAP responses aborted\n");
  241.                     exit(1);
  242.                     }
  243.                 }
  244.             IPXRelinquishControl();
  245.             }
  246.  
  247.                 printf("\n");
  248.                 printf("   Server : %-47.47s", sapInfo.serverName);
  249.                 printf("   Type.. : %04X\n", NWWordSwap(sapInfo.serverType));
  250.                 printf("   Address: %02X%02X%02X%02X:%02X%02X%02X%02X%02X%02X:%04X",
  251.                     sapInfo.network[0],
  252.                     sapInfo.network[1],
  253.                     sapInfo.network[2],
  254.                     sapInfo.network[3],
  255.                    sapInfo.node[0],
  256.                     sapInfo.node[1],
  257.                     sapInfo.node[2],
  258.                     sapInfo.node[3],
  259.                     sapInfo.node[4],
  260.                     sapInfo.node[5],
  261.                     NWWordSwap(sapInfo.sock));
  262.                 printf("   Hops: %d\n", NWWordSwap(sapInfo.intNetworks));
  263.         IPXListenForPacket(&rECB);
  264.         }
  265.     IPXCloseSocket(Socket);                             // remember to close the socket!
  266. }
  267.  
  268.