Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.3 KiB

  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. #include <precomp.h>
  25. typedef char BUF[1024];
  26. int
  27. __cdecl
  28. main(
  29. int argc,
  30. char **argv
  31. )
  32. {
  33. char *pszReceiveMailslot = "\\\\.\\MAILSLOT\\REMOTE\\DEBUGGERS";
  34. HANDLE hReceiveMailslot;
  35. HANDLE hSendMailslot;
  36. BOOL b;
  37. DWORD dwErr;
  38. int nReceived = 0;
  39. int nRelayed = 0;
  40. int iBuf;
  41. DWORD cbWritten;
  42. DWORD rgcbBuf[2];
  43. char szSendMailslot[128];
  44. BUF rgBuf[2];
  45. if (argc != 2) {
  46. printf("Usage: \n"
  47. "rdsrelay <targetdomain>\n");
  48. return 1;
  49. }
  50. sprintf(szSendMailslot, "\\\\%s\\MAILSLOT\\REMOTE\\DEBUGGERS", argv[1]);
  51. printf("Relaying remote.exe broadcasts to %s.\n", szSendMailslot);
  52. hReceiveMailslot =
  53. CreateMailslot(
  54. pszReceiveMailslot,
  55. 0,
  56. MAILSLOT_WAIT_FOREVER,
  57. NULL
  58. );
  59. if (INVALID_HANDLE_VALUE == hReceiveMailslot) {
  60. dwErr = GetLastError();
  61. if (ERROR_ALREADY_EXISTS == dwErr) {
  62. printf("Cannot receive on %s,\n"
  63. "is rdsrelay or remoteds already running on this machine?\n",
  64. pszReceiveMailslot);
  65. } else {
  66. printf("CreateMailslot(%s) failed error %d\n",
  67. pszReceiveMailslot,
  68. dwErr);
  69. }
  70. return 2;
  71. }
  72. hSendMailslot =
  73. CreateFile(
  74. szSendMailslot,
  75. GENERIC_WRITE,
  76. FILE_SHARE_WRITE,
  77. NULL,
  78. OPEN_EXISTING,
  79. 0,
  80. NULL
  81. );
  82. if (INVALID_HANDLE_VALUE == hSendMailslot) {
  83. printf("CreateFile(%s) failed error %d\n",
  84. pszReceiveMailslot,
  85. GetLastError());
  86. return 3;
  87. }
  88. iBuf = 0;
  89. ZeroMemory(rgcbBuf, sizeof(rgcbBuf));
  90. ZeroMemory(rgBuf, sizeof(rgBuf));
  91. while(TRUE)
  92. {
  93. printf("\r%d received, %d relayed", nReceived, nRelayed);
  94. //
  95. // Multiple transports mean we get duplicates for
  96. // each transport shared by us and the sender.
  97. // Meanwhile when we relay on we generate duplicates
  98. // for each transport shared by this machine and
  99. // the remoteds.exe receiver(s) on the domain we're
  100. // relaying to. They will eliminate duplicates, but
  101. // to avoid exponential effects we should eliminate
  102. // duplicates before relaying. Thus the two buffers
  103. // in rgBuf, we alternate between them, and compare
  104. // the two to see if the last and this are dupes.
  105. //
  106. b = ReadFile(
  107. hReceiveMailslot,
  108. rgBuf[ iBuf ],
  109. sizeof(rgBuf[ iBuf ]),
  110. &rgcbBuf[ iBuf ],
  111. NULL
  112. );
  113. if (! b) {
  114. printf("ReadFile(hReceiveMailslot) failed error %d\n", GetLastError());
  115. return 4;
  116. }
  117. nReceived++;
  118. if ( rgcbBuf[0] == rgcbBuf[1] &&
  119. ! memcmp(rgBuf[0], rgBuf[1], rgcbBuf[0])) {
  120. continue; // duplicate
  121. }
  122. b = WriteFile(
  123. hSendMailslot,
  124. rgBuf[ iBuf ],
  125. rgcbBuf[ iBuf ],
  126. &cbWritten,
  127. NULL
  128. );
  129. if (! b) {
  130. printf("WriteFile(hSendMailslot) failed error %d\n", GetLastError());
  131. return 5;
  132. }
  133. if (cbWritten != rgcbBuf[ iBuf ]) {
  134. printf("WriteFile(hSendMailslot) wrote %d instead of %d.\n", cbWritten, rgcbBuf[ iBuf ]);
  135. return 6;
  136. }
  137. nRelayed++;
  138. iBuf = !iBuf;
  139. }
  140. return 0; // never executed
  141. }