Source code of Windows XP (NT5)
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.

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