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.

254 lines
6.5 KiB

  1. // Test DsAddressToSiteNames API
  2. #include <nt.h>
  3. #include <ntrtl.h>
  4. #include <nturtl.h>
  5. #include <ntddbrow.h>
  6. #include <windows.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <lmcons.h>
  10. // #include <nlsite.h>
  11. #include <winsock2.h>
  12. #include <dsgetdc.h>
  13. #include <lmapibuf.h>
  14. #define AllocSplMem( _x) LocalAlloc( 0, _x)
  15. #define DBGMSG( _x, _y) printf
  16. VOID
  17. AllocSplSockets(
  18. struct hostent *pHostEnt,
  19. PSOCKET_ADDRESS *ppSocketAddress,
  20. DWORD *nSocketAddresses
  21. )
  22. {
  23. DWORD i;
  24. PSOCKET_ADDRESS pSocket;
  25. // Allocate Sockaddr element for each SOCKET_ADDRESS
  26. // If we fail partway through, just use partial list
  27. for ( i= 0 ; pHostEnt->h_addr_list[i] ; i++) {
  28. pSocket = &((*ppSocketAddress)[*nSocketAddresses]);
  29. if (!(pSocket->lpSockaddr = (struct sockaddr *) AllocSplMem(sizeof(struct sockaddr_in)))) {
  30. break;
  31. }
  32. ((struct sockaddr_in *) pSocket->lpSockaddr)->sin_family = AF_INET;
  33. ((struct sockaddr_in *) pSocket->lpSockaddr)->sin_addr = *(struct in_addr *) pHostEnt->h_addr_list[i];
  34. pSocket->iSockaddrLength = sizeof(struct sockaddr_in);
  35. *nSocketAddresses += 1;
  36. }
  37. }
  38. VOID
  39. GetSocketAddressesFromMachineName(
  40. PSTR pszAnsiMachineName, // Machine
  41. PSOCKET_ADDRESS *ppSocketAddress,
  42. DWORD *nSocketAddresses
  43. )
  44. /*++
  45. Routine Description:
  46. This routine builds list of names other than the machine name that
  47. can be used to call spooler APIs.
  48. --*/
  49. {
  50. struct hostent *HostEnt;
  51. DWORD iWsaError;
  52. if (HostEnt = gethostbyname(pszAnsiMachineName)) {
  53. AllocSplSockets(HostEnt, ppSocketAddress, nSocketAddresses);
  54. } else {
  55. iWsaError = WSAGetLastError();
  56. printf("gethostbyname failed: %d\n", iWsaError);
  57. }
  58. }
  59. __cdecl main (int argc, char *argv[])
  60. {
  61. NET_API_STATUS NetStatus;
  62. SOCKET_ADDRESS* SocketAddresses;
  63. ULONG EntryCount;
  64. LPWSTR *SiteNames;
  65. LPSTR *SiteNamesA;
  66. LPWSTR *SubnetNames;
  67. LPSTR *SubnetNamesA;
  68. ULONG i;
  69. int j;
  70. char *end;
  71. BOOLEAN Verbose = FALSE;
  72. if ( argc < 2 ) {
  73. Usage:
  74. printf("Usage: %s [-s:<ServerName>] [-v] <HostNames>\n", argv[0] );
  75. return 1;
  76. }
  77. {
  78. WORD wVersionRequested;
  79. WSADATA wsaData;
  80. int err;
  81. //
  82. // Initialize winsock.
  83. //
  84. wVersionRequested = MAKEWORD( 1, 1 );
  85. NetStatus = WSAStartup( wVersionRequested, &wsaData );
  86. if ( NetStatus != 0 ) {
  87. printf("NETAPI32.DLL: Cannot initialize winsock %ld.\n", NetStatus );
  88. return NetStatus;
  89. }
  90. if ( LOBYTE( wsaData.wVersion ) != 1 ||
  91. HIBYTE( wsaData.wVersion ) != 1 ) {
  92. WSACleanup();
  93. printf("NETAPI32.DLL: Wrong winsock version %ld.\n", NetStatus );
  94. return WSANOTINITIALISED;
  95. }
  96. }
  97. //
  98. // Build an array of socket addresses
  99. //
  100. SocketAddresses = (PSOCKET_ADDRESS) AllocSplMem(5000*sizeof(SOCKET_ADDRESS));
  101. EntryCount = 0;
  102. for ( j=1; j<argc; j++ ) {
  103. if ( strcmp(argv[j], "-v") == 0 ) {
  104. Verbose = TRUE;
  105. } else if ( strncmp( argv[j], "-s:", 3 ) {
  106. AnsiServerName = &argv[j][3];
  107. } else {
  108. GetSocketAddressesFromMachineName(
  109. argv[j],
  110. &SocketAddresses,
  111. &EntryCount );
  112. }
  113. }
  114. //
  115. // Only do all of the API of verbose
  116. //
  117. if ( Verbose ) {
  118. //
  119. // Do it in unicode
  120. //
  121. printf( "\nUse DsAddressToSiteNamesW API:\n" );
  122. NetStatus = DsAddressToSiteNamesW(
  123. NULL,
  124. EntryCount,
  125. SocketAddresses,
  126. &SiteNames );
  127. if ( NetStatus != NO_ERROR ) {
  128. printf( "Translation Failed %ld\n", NetStatus );
  129. return 0;
  130. }
  131. for ( i=0; i<EntryCount; i++ ) {
  132. if ( SiteNames[i] == NULL ) {
  133. printf( "SiteName %ld doesn't map.\n", i );
  134. } else {
  135. printf( "SiteName %ld: %ws\n", i, SiteNames[i] );
  136. }
  137. }
  138. NetApiBufferFree( SiteNames );
  139. //
  140. // Do it in ANSI
  141. //
  142. printf( "\nUse DsAddressToSiteNamesA API:\n" );
  143. NetStatus = DsAddressToSiteNamesA(
  144. NULL,
  145. EntryCount,
  146. SocketAddresses,
  147. &SiteNamesA );
  148. if ( NetStatus != NO_ERROR ) {
  149. printf( "Translation Failed %ld\n", NetStatus );
  150. return 0;
  151. }
  152. for ( i=0; i<EntryCount; i++ ) {
  153. if ( SiteNamesA[i] == NULL ) {
  154. printf( "SiteName %ld doesn't map.\n", i );
  155. } else {
  156. printf( "SiteName %ld: %s\n", i, SiteNamesA[i] );
  157. }
  158. }
  159. NetApiBufferFree( SiteNamesA );
  160. //
  161. // Do it in unicode (with subnet)
  162. //
  163. printf( "\nUse DsAddressToSiteNamesExW API:\n" );
  164. NetStatus = DsAddressToSiteNamesExW(
  165. NULL,
  166. EntryCount,
  167. SocketAddresses,
  168. &SiteNames,
  169. &SubnetNames );
  170. if ( NetStatus != NO_ERROR ) {
  171. printf( "Translation Failed %ld\n", NetStatus );
  172. return 0;
  173. }
  174. for ( i=0; i<EntryCount; i++ ) {
  175. printf( "SiteName %ld: %ws %ws\n", i, SiteNames[i], SubnetNames[i] );
  176. }
  177. NetApiBufferFree( SiteNames );
  178. NetApiBufferFree( SubnetNames );
  179. printf( "\nUse DsAddressToSiteNamesExA API:\n" );
  180. }
  181. //
  182. // Do it in ANSI
  183. //
  184. NetStatus = DsAddressToSiteNamesExA(
  185. NULL,
  186. EntryCount,
  187. SocketAddresses,
  188. &SiteNamesA,
  189. &SubnetNamesA );
  190. if ( NetStatus != NO_ERROR ) {
  191. printf( "Translation Failed %ld\n", NetStatus );
  192. return 0;
  193. }
  194. for ( i=0; i<EntryCount; i++ ) {
  195. printf( "SiteName %ld: %s %s\n", i, SiteNamesA[i], SubnetNamesA[i] );
  196. }
  197. NetApiBufferFree( SiteNamesA );
  198. NetApiBufferFree( SubnetNamesA );
  199. printf( "Done\n" );
  200. return 0;
  201. }