home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / winnt / remote / rdsrelay.c < prev    next >
C/C++ Source or Header  |  1997-08-30  |  5KB  |  172 lines

  1. //
  2. // rdsrelay.c
  3. //
  4. // Relays recieved remote.exe broadcasts (for remoteds.exe)
  5. // to another domain/workgroup.
  6. //
  7. // WARNING:  There are no checks in this program for looping
  8. //           relays, only one copy should be run per network.
  9. //           I wrote this to relay ntdev remote.exe broadcasts
  10. //           to ntwksta so that remoteds.exe running on \\ntstress
  11. //           can see remote servers in both ntdev and ntwksta.
  12. //           \\ntstress is in ntwksta.
  13. //
  14. // Usage:
  15. //
  16. //    rdsrelay <targetdomain>
  17. //
  18. //
  19. // Dave Hart (davehart) written Aug 29, 1997.
  20. //
  21. // Copyright 1997 Microsoft Corp.
  22. //
  23. //
  24.  
  25. #include <windows.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <process.h>
  30.  
  31. typedef char BUF[1024];
  32.  
  33. int
  34. main(
  35.     int argc,
  36.     char **argv
  37.     )
  38. {
  39.     char   *pszReceiveMailslot = "\\\\.\\MAILSLOT\\REMOTE\\DEBUGGERS";
  40.     HANDLE  hReceiveMailslot;
  41.     HANDLE  hSendMailslot;
  42.     BOOL    b;
  43.     DWORD   dwErr;
  44.     int     nReceived = 0;
  45.     int     nRelayed = 0;
  46.     int     iBuf;
  47.     DWORD   cbWritten;
  48.     DWORD   rgcbBuf[2];
  49.     char    szSendMailslot[128];
  50.     BUF     rgBuf[2];
  51.  
  52.     if (argc != 2) {
  53.         printf("Usage: \n"
  54.                "rdsrelay <targetdomain>\n");
  55.         return 1;
  56.     }
  57.  
  58.     sprintf(szSendMailslot, "\\\\%s\\MAILSLOT\\REMOTE\\DEBUGGERS", argv[1]);
  59.     printf("Relaying remote.exe broadcasts to %s.\n", szSendMailslot);
  60.  
  61.     hReceiveMailslot =
  62.         CreateMailslot(
  63.             pszReceiveMailslot,
  64.             0,
  65.             MAILSLOT_WAIT_FOREVER,
  66.             NULL
  67.             );
  68.  
  69.     if (INVALID_HANDLE_VALUE == hReceiveMailslot) {
  70.  
  71.         dwErr = GetLastError();
  72.  
  73.         if (ERROR_ALREADY_EXISTS == dwErr) {
  74.             printf("Cannot receive on %s,\n"
  75.                    "is rdsrelay or remoteds already running on this machine?\n",
  76.                    pszReceiveMailslot);
  77.         } else {
  78.             printf("CreateMailslot(%s) failed error %d\n",
  79.                     pszReceiveMailslot,
  80.                     dwErr);
  81.         }
  82.         return 2;
  83.     }
  84.  
  85.     hSendMailslot =
  86.         CreateFile(
  87.             szSendMailslot,
  88.             GENERIC_WRITE,
  89.             FILE_SHARE_WRITE,
  90.             NULL,
  91.             OPEN_EXISTING,
  92.             0,
  93.             NULL
  94.             );
  95.  
  96.     if (INVALID_HANDLE_VALUE == hSendMailslot) {
  97.  
  98.         printf("CreateFile(%s) failed error %d\n",
  99.                 pszReceiveMailslot,
  100.                 GetLastError());
  101.         return 3;
  102.     }
  103.  
  104.     iBuf = 0;
  105.     ZeroMemory(rgcbBuf, sizeof(rgcbBuf));
  106.     ZeroMemory(rgBuf, sizeof(rgBuf));
  107.  
  108.     while(TRUE)
  109.     {
  110.         printf("\r%d received, %d relayed", nReceived, nRelayed);
  111.  
  112.         //
  113.         // Multiple transports mean we get duplicates for
  114.         // each transport shared by us and the sender.
  115.         // Meanwhile when we relay on we generate duplicates
  116.         // for each transport shared by this machine and
  117.         // the remoteds.exe receiver(s) on the domain we're
  118.         // relaying to.  They will eliminate duplicates, but
  119.         // to avoid exponential effects we should eliminate
  120.         // duplicates before relaying.  Thus the two buffers
  121.         // in rgBuf, we alternate between them, and compare
  122.         // the two to see if the last and this are dupes.
  123.         //
  124.  
  125.         b = ReadFile(
  126.                 hReceiveMailslot,
  127.                 rgBuf[ iBuf ],
  128.                 sizeof(rgBuf[ iBuf ]),
  129.                 &rgcbBuf[ iBuf ],
  130.                 NULL
  131.                 );
  132.  
  133.         if (! b) {
  134.             printf("ReadFile(hReceiveMailslot) failed error %d\n", GetLastError());
  135.             return 4;
  136.         }
  137.  
  138.         nReceived++;
  139.  
  140.         if ( rgcbBuf[0] == rgcbBuf[1] &&
  141.              ! memcmp(rgBuf[0], rgBuf[1], rgcbBuf[0])) {
  142.  
  143.             continue;               // duplicate
  144.         }
  145.  
  146.         b = WriteFile(
  147.                 hSendMailslot,
  148.                 rgBuf[ iBuf ],
  149.                 rgcbBuf[ iBuf ],
  150.                 &cbWritten,
  151.                 NULL
  152.                 );
  153.  
  154.         if (! b) {
  155.             printf("WriteFile(hSendMailslot) failed error %d\n", GetLastError());
  156.             return 5;
  157.         }
  158.  
  159.         if (cbWritten != rgcbBuf[ iBuf ]) {
  160.             printf("WriteFile(hSendMailslot) wrote %d instead of %d.\n", cbWritten, rgcbBuf[ iBuf ]);
  161.             return 6;
  162.         }
  163.  
  164.         nRelayed++;
  165.  
  166.         iBuf = !iBuf;
  167.  
  168.     }
  169.  
  170.     return 0;    // never executed
  171. }
  172.