home *** CD-ROM | disk | FTP | other *** search
/ CD Loisirs 18 / cd.iso / PLANETE / FINGER31 / NETWRKB.C < prev    next >
C/C++ Source or Header  |  1993-05-18  |  5KB  |  173 lines

  1. //
  2. // Finger Version 3.1, a Windows Sockets Finger Client
  3. //
  4. // Copyright 1992, 1993 Network Research Corporation
  5. //
  6. // Permission to use, modify, and distribute this software and its
  7. // documentation for any purpose and without fee is hereby granted, provided
  8. // that the above copyright notice appears in all copies and that both
  9. // that copyright notice and this permission notice appear in supporting
  10. // documentation.  NRC makes no claims as to the suitability of this software
  11. // for any purpose.
  12. //
  13. // Module NETWRKB uses Windows Sockets synchronous (blocking) calls to
  14. // query a remote finger server for a list of currently logged in users.
  15. // Module FINGER initiates the operation by calling FingerStart(), and
  16. // NETWRKB signals completion by calling FingerFinish(). NETWRKB uses DSPLIST
  17. // functions to send the retrieved data to the FINGER user interface module.
  18. //
  19. // 02/12/92 Lee Murach       Created.
  20. // 10/20/92 Lee Murach       Adapted to use display lists.
  21. // 11/02/92 Lee Murach       Don't assume gethostbyname() works with dotted
  22. //               decimal internet addr string.  Prepare for
  23. //               inet_addr() change in future windows sockets.
  24. // 12/02/92 Lee Murach       Restructured for Finger 3.0 integrated release
  25. // 03/25/93 Lee Murach       Added per-user finger support.
  26. // 05/18/93 F. Whiteside   Fixed a simple bug in the getservbyname() code
  27. //
  28.  
  29. #include <memory.h>
  30. #include <string.h>
  31. #include <winsock.h>
  32. #include "finger.h"
  33.  
  34. BOOL Finger(IPA ipa, UINT port);
  35. IPA GetHostAddr(char *szHostName);
  36.  
  37. //
  38. // InitNetApp -- no application wide initialization needed; provided for
  39. // compatability with NETWRKM.
  40. //
  41. VOID InitNetApp(VOID)
  42. {
  43. }
  44.  
  45. //
  46. // InitNetInst -- no instance initialization needed; provided for
  47. // compatability with NETWRKM.
  48. //
  49. VOID InitNetInst(HWND hWnd)
  50. {
  51. }
  52.  
  53. //
  54. // FingerStart -- called by FINGER module to initiate a conversation with
  55. // the remote finger server.  We start by resolving the finger tcp service
  56. // to a port number, and then we resolve the host specifier to an IP address.
  57. // Finger() then establishes the connection, submits query, and retrieves
  58. // results.  Calls FINGER module FingerFinish() to signal completion.
  59. //
  60. VOID FingerStart(VOID)
  61. {
  62.    int err = FE_ERROR;    // assume failure
  63.    SERVENT FAR *pse;    // pts to "service" data (internally allocated)
  64.  
  65.    // looking for finger tcp port
  66.    if (pse = getservbyname("finger", NULL))
  67.    {
  68.       UINT fingerport;       // hold the data returned by getservbyname
  69.                // because the standard says you *must* copy this
  70.                // data out before calling any winsock API function
  71.       IPA ipa;                   // holds internet protocol address
  72.  
  73.       fingerport = pse->s_port;
  74.       if ((ipa = GetHostAddr(szHostName)) != INADDR_NONE)
  75.       {
  76.      if (Finger(ipa, fingerport))
  77.         err = 0;               // it worked!
  78.       }
  79.       else                   // don't recognize host name
  80.      ReportFingerErr(FE_NOHOST);
  81.    }
  82.    else                    // can't find finger port
  83.       ReportFingerErr(FE_NOPORT);
  84.  
  85.    FingerFinish(err);
  86. }
  87.  
  88. //
  89. // GetHostAddr -- gets host's internet address. Interprets argument first
  90. // as dotted internet address string, and failing that, as a DNS host
  91. // name.
  92. //
  93. IPA GetHostAddr(char *szHostName)
  94. {
  95.    IPA ipa;
  96.    HOSTENT FAR *phe; // pts to host info struct (internally allocated)
  97.  
  98.    if ((ipa = INET_ADDR(szHostName)) != INADDR_NONE)
  99.       return ipa;
  100.  
  101.    if (phe = gethostbyname(szHostName))
  102.    {
  103.       ipa = *(IPA FAR *) *(phe->h_addr_list);
  104.       return ipa;
  105.    }
  106.  
  107.    return INADDR_NONE;
  108. }
  109.  
  110. //
  111. // Finger -- queries remote finger server for list of users currently
  112. // logged in.  Returns the data as a display list by using DSPLIST functions.
  113. //
  114. BOOL Finger(IPA ipa, UINT port)
  115. {
  116.    BOOL ret = TRUE;  // assume success
  117.    SOCKET sock;      // communications end point
  118.  
  119.    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) >= 0)
  120.    {
  121.       SOCKADDR_IN server;
  122.  
  123.       // address of the finger server
  124.       memset(&server, 0, sizeof(server));
  125.       server.sin_family = AF_INET;
  126.       server.sin_port = port;
  127.       server.sin_addr.s_addr = ipa;
  128.  
  129.       // establish a stream connection to server
  130.       if (!connect(sock, (SOCKADDR *)&server, sizeof(server)))
  131.       {
  132.      char msg[MAXUSER+3];
  133.      int msglen;
  134.  
  135.      strcpy(msg, szUser);
  136.      strcat(msg, "\r\n");
  137.      msglen = strlen(msg);
  138.  
  139.      if (send(sock, msg, msglen, 0) == msglen)
  140.      {
  141.         static char buf[500];      // receive buffer
  142.         int nchars;            // n chars just received, or error
  143.  
  144.         /* open a new display list and push characters
  145.            until either no more received or an error occurs */
  146.  
  147.         OpenDisplayList();
  148.         while (( nchars = recv(sock, (char FAR *)&buf,
  149.              sizeof(buf), 0)) > 0)
  150.            PushChars(buf, nchars);         // add chars to display list
  151.         CloseDisplayList();
  152.  
  153.         if (nchars < 0)
  154.         {
  155.            FreeDisplayList();         // list is invalid
  156.            ReportFingerErr(FE_NORECV);   // error during receive
  157.            ret = FALSE;
  158.         }
  159.      }
  160.      else
  161.         ReportFingerErr(FE_NOSEND);      // didn't send it (all)
  162.       }
  163.       else
  164.      ReportFingerErr(FE_NOCONN);         // cannot connect to server
  165.  
  166.       closesocket(sock);             // don't need this anymore
  167.    }
  168.    else
  169.       ReportFingerErr(FE_NOSOCK);         // cannot get socket
  170.  
  171.    return(ret);
  172. }
  173.