home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / security / winnt / sockauth / client.c next >
Encoding:
C/C++ Source or Header  |  1997-10-09  |  4.2 KB  |  223 lines

  1. /*++
  2.  
  3. Copyright 1996-1997 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     client.c
  8.  
  9. Abstract:
  10.  
  11.     A command line app that establishes an authenticated connection
  12.     with a server.
  13.  
  14. Revision History:
  15.  
  16. --*/
  17.  
  18. #include <windows.h>
  19. #include <winsock.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include "security.h"
  23. #include "comm.h"
  24.  
  25. BOOL ConnectAuthSocket (LPCSTR szServer, SOCKET *s);
  26. BOOL CloseAuthSocket (SOCKET s);
  27. BOOL DoAuthentication (SOCKET s);
  28.  
  29. static PBYTE g_pInBuf = NULL;
  30. static PBYTE g_pOutBuf = NULL;
  31. static DWORD g_cbMaxMessage = 0;
  32. static unsigned short g_usPort = 2000;
  33.  
  34. void main(int argc, char *argv[])
  35. {
  36.     SOCKET s;
  37.     int cbRead;
  38.     char achData[BUFSIZ];
  39.  
  40.     if (argc < 2)  {
  41.         fprintf (stderr, "usage: %s <server>\n", argv[0]);
  42.         fprintf (stderr, " example: %s winbase\n", argv[0]);
  43.         exit (EXIT_FAILURE);
  44.     }
  45.  
  46.     // initialize
  47.     //
  48.     if (!InitWinsock ())
  49.         exit (EXIT_FAILURE);
  50.  
  51.     if (!InitPackage (&g_cbMaxMessage))
  52.         exit (EXIT_FAILURE);
  53.  
  54.     g_pInBuf = (PBYTE) malloc (g_cbMaxMessage);
  55.     g_pOutBuf = (PBYTE) malloc (g_cbMaxMessage);
  56.     if (NULL == g_pInBuf || NULL == g_pOutBuf)
  57.         exit (EXIT_FAILURE);
  58.  
  59.     // connect to server
  60.     //
  61.     if (!ConnectAuthSocket (argv[1], &s))
  62.         exit (EXIT_FAILURE);
  63.  
  64.     while (1) {
  65.         // read from the socket
  66.         //
  67.         if (!ReceiveBytes (s, achData, BUFSIZ, &cbRead))
  68.             break;
  69.  
  70.         if (0 == cbRead)
  71.             break;
  72.  
  73.         // display the results
  74.         //
  75.         printf ("%.*s", cbRead, achData);
  76.     }
  77.  
  78.     printf ("\n");
  79.  
  80.     // terminate
  81.     //
  82.     CloseAuthSocket (s);
  83.  
  84.     TermPackage ();
  85.  
  86.     TermWinsock ();
  87.  
  88.     free (g_pInBuf);
  89.     free (g_pOutBuf);
  90.  
  91.     exit (EXIT_SUCCESS);
  92. }
  93.  
  94. BOOL ConnectAuthSocket (LPCSTR szServer, SOCKET *s)
  95. /*++
  96.  
  97.  Routine Description:
  98.  
  99.     Establishes an authenticated socket connection with a server and
  100.     initializes any needed security package resources.
  101.  
  102.  Return Value:
  103.  
  104.     Returns TRUE is successful; otherwise FALSE is returned.
  105.  
  106. --*/
  107. {
  108.     SOCKET sockServer;
  109.     unsigned long ulAddress;
  110.     struct hostent *pHost;
  111.     SOCKADDR_IN sin;
  112.     DWORD dwRes;
  113.  
  114.     // lookup the address for the server name
  115.     //
  116.     ulAddress = inet_addr (szServer);
  117.     if (INADDR_NONE == ulAddress) {
  118.         pHost = gethostbyname (szServer);
  119.         if (NULL == pHost) {
  120.             dwRes = GetLastError ();
  121.             fprintf (stderr, "Unable to resolve host name: %u\n", dwRes);
  122.             return(FALSE);
  123.         }
  124.  
  125.         memcpy((char FAR *)&ulAddress, pHost->h_addr, pHost->h_length);
  126.     }
  127.  
  128.     // create the socket
  129.     //
  130.     sockServer = socket (PF_INET, SOCK_STREAM, 0);
  131.     if (INVALID_SOCKET == sockServer) {
  132.         fprintf (stderr, "Unable to create socket: %u\n", GetLastError ());
  133.         return(FALSE);
  134.     }
  135.  
  136.     sin.sin_family = AF_INET;
  137.     sin.sin_addr.s_addr = ulAddress;
  138.     sin.sin_port = htons (g_usPort);
  139.  
  140.     // connect to remote endpoint
  141.     //
  142.     if (connect (sockServer, (LPSOCKADDR) &sin, sizeof (sin))) {
  143.         fprintf (stderr, "connect failed: %u\n", GetLastError ());
  144.         closesocket (sockServer);
  145.         return(FALSE);
  146.     }
  147.  
  148.     // Make this an authenticated connection
  149.     //
  150.     if (!InitSession (sockServer)) {
  151.         closesocket (sockServer);
  152.         return(FALSE);
  153.     }
  154.  
  155.     if (!DoAuthentication (sockServer)) {
  156.         closesocket (sockServer);
  157.         return(FALSE);
  158.     }
  159.  
  160.     *s = sockServer;
  161.  
  162.     return(TRUE);
  163. }
  164.  
  165. BOOL CloseAuthSocket (SOCKET s)
  166. /*++
  167.  
  168.  Routine Description:
  169.  
  170.     Closes a socket and releases security resources associated with
  171.     the socket
  172.  
  173.  Return Value:
  174.  
  175.     Returns TRUE is successful; otherwise FALSE is returned.
  176.  
  177. --*/
  178. {
  179.     TermSession (s);
  180.     shutdown (s, 2);
  181.     closesocket (s);
  182.     return(TRUE);
  183. }    
  184.  
  185. BOOL DoAuthentication (SOCKET s)
  186. /*++
  187.  
  188.  Routine Description:
  189.  
  190.     Manges the authentication conversation with the server via the
  191.     supplied socket handle.
  192.  
  193.  Return Value:
  194.  
  195.     Returns TRUE is successful; otherwise FALSE is returned.
  196.  
  197. --*/
  198. {
  199.     BOOL done = FALSE;
  200.     DWORD cbOut, cbIn;
  201.  
  202.     cbOut = g_cbMaxMessage;
  203.     if (!GenClientContext (s, NULL, 0, g_pOutBuf, &cbOut, &done))
  204.         return(FALSE);
  205.     
  206.     if (!SendMsg (s, g_pOutBuf, cbOut))
  207.         return(FALSE);
  208.  
  209.     while (!done) {
  210.         if (!ReceiveMsg (s, g_pInBuf, g_cbMaxMessage, &cbIn))
  211.             return(FALSE);
  212.  
  213.         cbOut = g_cbMaxMessage;
  214.         if (!GenClientContext (s, g_pInBuf, cbIn, g_pOutBuf, &cbOut, &done))
  215.             return(FALSE);
  216.  
  217.         if (!SendMsg (s, g_pOutBuf, cbOut))
  218.             return(FALSE);
  219.     }
  220.  
  221.     return(TRUE);
  222. }
  223.