home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / ipxpng.exe / IPXPING.C next >
Text File  |  1995-01-13  |  6KB  |  241 lines

  1. /****************************************************************************
  2. **    File:    IPXPING.C
  3. **
  4. **    Desc:    Sample IPX ping program.
  5. **            
  6. **            This sample code demonstrates how to send a diagnostic
  7. **            configuration packet to a node and process the response
  8. **            that comes back. Since any node that has IPXODI loaded
  9. **            by default has the Diagnostic responder loaded, this code
  10. **            can be used to determine if a node is active. This will also
  11. **            work with Servers (they also have a diagnostic responder).
  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. **        01-12-95    klb    First code.
  44. */
  45.  
  46. /****************************************************************************
  47. **    Include headers, macros, function prototypes, etc.
  48. */
  49.  
  50.     /*------------------------------------------------------------------------
  51.     **    ANSI
  52.     */
  53.     #include <stdio.h>
  54.     #include <string.h>
  55.     #include <time.h>
  56.     #include <ctype.h>
  57.  
  58.     /*------------------------------------------------------------------------
  59.     **    NetWare
  60.     */
  61.     #define NWDOS
  62.     #include <nwipxspx.h>
  63.     #include <nwcalls.h>
  64.     #include "ipxping.h"
  65.  
  66.  
  67.     /*------------------------------------------------------------------------
  68.     **    Globals
  69.     */
  70.     int IpxPing(IPXAddress *netAddress);
  71.     int PutAddress (char *string, char *buf, int hexBytes);
  72.  
  73. /****************************************************************************
  74. **    Function main(int argc, char *argv[])
  75. */
  76. int main(int argc, char *argv[])
  77. {
  78.     NWCCODE    ccode;
  79.     int            result, i;
  80.     IPXAddress    iPXAddress;
  81.  
  82.     char network[9], node[13];
  83.  
  84.     if ((strlen (argv[1]) > 8) || (strlen (argv[2]) > 12) || (argc != 3))
  85.     {
  86.         printf ("Usage:  NWPING <Network address> <Node address>\n");
  87.         return (3);
  88.     }
  89.  
  90.     ccode = NWCallsInit (NULL,NULL);
  91.     if (ccode) return 2;
  92.  
  93.     sprintf (network, "%08s", argv[1]);
  94.     strupr (network);
  95.     for (i = 0; i < 8; i++) if (network[i] == ' ') network[i] = '0';
  96.     sprintf (node, "%012s", argv[2]);
  97.     strupr (node);
  98.     for (i = 0; i < 12; i++) if (node[i] == ' ') node[i] = '0';
  99.  
  100.     /*** Set up address of target ***/
  101.     result = PutAddress (network, iPXAddress.network, 4);
  102.     if (result != 1)
  103.     {
  104.         printf ("Invalid network address.\n");
  105.         return 4;
  106.     }
  107.     result = PutAddress (node, iPXAddress.node, 6);
  108.     if (result != 1)
  109.     {
  110.         printf ("Invalid node address.\n");
  111.         return 5;
  112.     }
  113.     iPXAddress.socket[0] = 0x04;
  114.     iPXAddress.socket[1] = 0x56;
  115.  
  116.     result = IpxPing (&iPXAddress);
  117.  
  118.     if (result == 0)
  119.     {
  120.         printf ("%s:%s is alive\n", network, node);
  121.         return 0;
  122.     }
  123.     else
  124.     {
  125.         printf ("%s:%s does not respond\n", network, node);
  126.     }
  127.  
  128.     return 1;
  129. }
  130.  
  131. /****************************************************************************
  132. **    Function : IpxPing(IPXAddress *netAddress);
  133. **               This function sends the IPX Diag. Config. Packet
  134. **                  and waits for the response.
  135. */
  136. int IpxPing(IPXAddress *netAddress)
  137. {
  138.     WORD             Socket=0x0000;
  139.     WORD            diagSocket = 0x5604;
  140.     IPXHeader    sHeader;
  141.     ECB            sECB;
  142.     ECB            rECB;
  143.     IPXHeader    rHeader;
  144.     cReqPack        cReq;
  145.     cRepPack        cRep;
  146.     int            ccode, garbage;
  147.     time_t        baseTime;
  148.     time_t        currTime;
  149.  
  150.  
  151.     IPXInitialize();
  152.  
  153.     IPXOpenSocket((BYTE *)&Socket, (BYTE)0);
  154.  
  155.     rHeader.packetType = (char)4;
  156.     rHeader.length = NWWordSwap(sizeof(IPXHeader));
  157.  
  158.     rECB.ESRAddress = (void far *)NULL;
  159.     rECB.socketNumber= Socket;
  160.     rECB.fragmentCount = 2;
  161.     rECB.fragmentDescriptor[0].address = &rHeader;
  162.     rECB.fragmentDescriptor[0].size = sizeof(IPXHeader);
  163.     rECB.fragmentDescriptor[1].address = &cRep;
  164.     rECB.fragmentDescriptor[1].size = sizeof(cRep);
  165.  
  166.     IPXListenForPacket(&rECB);
  167.  
  168.  
  169.     memcpy(&sHeader.destination, netAddress, 12);
  170.     memcpy(sHeader.destination.socket, &diagSocket, 2);
  171.     cReq.eac = 0;
  172.     memcpy(cReq.ea, 0, 6);
  173.     sHeader.length = NWWordSwap(sizeof(IPXHeader));
  174.     sHeader.packetType = 4;
  175.  
  176.     sECB.ESRAddress = (void far *)NULL;
  177.     sECB.inUseFlag = 0;
  178.     sECB.fragmentCount = 2;
  179.     sECB.socketNumber = Socket;
  180.     IPXGetLocalTarget(netAddress, sECB.immediateAddress, &garbage);
  181.     sECB.fragmentDescriptor[0].address = (&sHeader);
  182.     sECB.fragmentDescriptor[0].size = sizeof(IPXHeader);
  183.     sECB.fragmentDescriptor[1].address = (&cReq);
  184.     sECB.fragmentDescriptor[1].size = sizeof(cReq);
  185.  
  186.     IPXSendPacket(&sECB);
  187.     while(sECB.inUseFlag)
  188.         IPXRelinquishControl();
  189.  
  190.     baseTime = time(NULL);
  191.     baseTime += 5;
  192.  
  193.         while(rECB.inUseFlag)
  194.             {
  195.             currTime = time(NULL);
  196.  
  197.             if(currTime >= baseTime)
  198.                 {
  199.                 IPXCancelEvent(&rECB);
  200.                 IPXCloseSocket(Socket);
  201.                 return(1);
  202.                 }
  203.             
  204.             IPXRelinquishControl();
  205.             }
  206.  
  207.     IPXCloseSocket(Socket);
  208.     return(0);
  209.  
  210. }
  211.  
  212.  
  213. /****************************************************************************
  214. **    Function : PutAddress()
  215. **               This function processes the netaddress passed in on the 
  216. **                  command line to a format usable by the IPXPing() function.
  217. */
  218. int PutAddress (char *string, char *buf, int hexBytes)
  219. {
  220.     int i, j, value;
  221.     char    c;
  222.  
  223.     for (i = 0; i < hexBytes; i++)
  224.     {
  225.         value = 0;
  226.         for (j = 0; j < 2; j++)
  227.         {
  228.             value <<= 4;
  229.             if ((c = toupper (*string)) >= '0' && c <='9')
  230.                 value += c - '0';
  231.             else if (c >= 'A' && c <= 'F')
  232.                 value += c - 'A' + 10;
  233.             else
  234.                 return 0;
  235.             string ++;
  236.         }
  237.         *buf++ = value;
  238.     }
  239.     return 1;
  240. }
  241.