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.

870 lines
28 KiB

  1. /******************************************************************************
  2. Copyright (C) Microsoft Corporation
  3. Module Name:
  4. Query.CPP
  5. Abstract:
  6. This module deals with Query functionality of OpenFiles.exe
  7. NT command line utility.
  8. Author:
  9. Akhil Gokhale (akhil.gokhale@wipro.com) 1-Nov-2000
  10. Revision History:
  11. Akhil Gokhale (akhil.gokhale@wipro.com) 1-Nov-2000 : Created It.
  12. *****************************************************************************/
  13. #include "pch.h"
  14. #include "query.h"
  15. GUID HostnameGuid = SVCID_INET_HOSTADDRBYNAME;
  16. GUID AddressGuid = SVCID_INET_HOSTADDRBYINETSTRING;
  17. AFPROTOCOLS afp[2] = {
  18. {AF_INET, IPPROTO_UDP},
  19. {AF_INET, IPPROTO_TCP}
  20. };
  21. BOOL
  22. DoQuery(
  23. IN PTCHAR pszServer,
  24. IN BOOL bShowNoHeader,
  25. IN PTCHAR pszFormat,
  26. IN BOOL bVerbose
  27. )
  28. /*++
  29. Routine Description:
  30. This function Queries for all open files in for the server and display them.
  31. Arguments:
  32. [in] pszServer : Will have the server name.
  33. [in] bShowNoHeader : Will have the value whether to show header or not.
  34. [in] pszFormat : Will have the format required to show the result.
  35. [in] bVerbose : Will have the value whether to show verbose
  36. result.
  37. Return Value:
  38. TRUE if query successful
  39. else FALSE
  40. --*/
  41. {
  42. CHString szCHString = L"\0";
  43. // Receives the count of elements actually enumerated by "NetFileEnum"
  44. // function
  45. DWORD dwEntriesRead = 0;
  46. // Receives the total number of entries that could have been enumerated
  47. // from the current resume position by "NetFileEnum" function
  48. DWORD dwTotalEntries = 0;
  49. // Contains a resume handle which is used to continue an existing file
  50. // search. The handle should be zero on the first call and left unchanged
  51. // for subsequent calls. If resume_handle is NULL, then no resume handle
  52. // is stored. This variable used in calling "NetFileEnum" function.
  53. DWORD dwResumeHandle = 0;
  54. // Contains the state whether at least one record found for this query.
  55. BOOL bAtLeastOne = FALSE;
  56. // Stores format flag required to show result on console. Default format
  57. // is TABLE
  58. DWORD dwFormat = SR_FORMAT_TABLE;
  59. // LPFILE_INFO_3 structure contains the identification number and other
  60. // pertinent information about files, devices, and pipes.
  61. LPFILE_INFO_3 pFileInfo3_1 = NULL;
  62. LPFILE_INFO_3 pFileInfo3Org_1 = NULL;
  63. // Contains return value for "NetFileEnum" function.
  64. DWORD dwError = 0;
  65. // Stores server type information.
  66. LPTSTR pszServerType = new TCHAR[MIN_MEMORY_REQUIRED+1];
  67. AFP_FILE_INFO* pFileInfo = NULL;
  68. AFP_FILE_INFO* pFileInfoOrg = NULL;
  69. DWORD hEnumHandle = 0;
  70. AFP_SERVER_HANDLE ulSFMServerConnection = 0;
  71. typedef DWORD (*FILEENUMPROC)(AFP_SERVER_HANDLE,LPBYTE*,DWORD,LPDWORD,
  72. LPDWORD,LPDWORD);
  73. typedef DWORD (*CONNECTPROC) (LPWSTR,PAFP_SERVER_HANDLE);
  74. typedef DWORD (*MEMFREEPROC) (LPVOID);
  75. // buffer for Windows directory path.
  76. TCHAR szDllPath[MAX_PATH+1];
  77. // AfpAdminConnect and AfpAdminFileEnum functions are undocumented function
  78. // and used only for MAC client.
  79. CONNECTPROC AfpAdminConnect = NULL; // Function Pointer
  80. FILEENUMPROC AfpAdminFileEnum = NULL;// Function Pointer
  81. MEMFREEPROC AfpAdminBufferFree = NULL; // Function Pointer
  82. // To store retval for LoadLibrary
  83. HMODULE hModule = 0;
  84. DWORD dwIndx = 0; // Indx variable
  85. DWORD dwRow = 0; // Row No indx.
  86. //server name to be shown
  87. LPTSTR pszServerNameToShow = new TCHAR[MIN_MEMORY_REQUIRED+ 1];
  88. LPTSTR pszTemp = new TCHAR[MIN_MEMORY_REQUIRED+ 1];
  89. //Some column required to hide in non verbose mode query
  90. DWORD dwMask = bVerbose?SR_TYPE_STRING:SR_HIDECOLUMN|SR_TYPE_STRING;
  91. TCOLUMNS pMainCols[]=
  92. {
  93. {L"\0",COL_WIDTH_HOSTNAME,dwMask,L"\0",NULL,NULL},
  94. {L"\0",COL_WIDTH_ID,SR_TYPE_STRING,L"\0",NULL,NULL},
  95. {L"\0",COL_WIDTH_ACCESSED_BY,SR_TYPE_STRING,L"\0",NULL,NULL},
  96. {L"\0",COL_WIDTH_TYPE,SR_TYPE_STRING,L"\0",NULL,NULL},
  97. {L"\0",COL_WIDTH_LOCK,dwMask,L"\0",NULL,NULL},
  98. {L"\0",COL_WIDTH_OPEN_MODE,dwMask,L"\0",NULL,NULL},
  99. {L"\0",COL_WIDTH_OPEN_FILE,SR_TYPE_STRING|
  100. (SR_NO_TRUNCATION&~(SR_WORDWRAP)),L"\0",NULL,NULL}
  101. };
  102. TARRAY pColData = CreateDynamicArray();//array to stores
  103. //result
  104. if(( NULL == pszServerNameToShow)||
  105. ( NULL == pszServerType)||
  106. ( NULL == pColData)||
  107. ( NULL == pszTemp))
  108. {
  109. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  110. ShowMessage(stderr,GetResString(IDS_ID_SHOW_ERROR));
  111. ShowLastError(stderr);
  112. SAFEDELETE(pszServerNameToShow);
  113. SAFEDELETE(pszServerType);
  114. SAFERELDYNARRAY(pColData);
  115. SAFEDELETE(pszTemp);
  116. return FALSE;
  117. }
  118. // Initialize allocated arrays
  119. SecureZeroMemory(pszServerNameToShow,MIN_MEMORY_REQUIRED*sizeof(TCHAR));
  120. SecureZeroMemory(pszServerType,MIN_MEMORY_REQUIRED*sizeof(TCHAR));
  121. SecureZeroMemory(szDllPath, SIZE_OF_ARRAY(szDllPath));
  122. // Fill column headers with TEXT.
  123. StringCopy(pMainCols[0].szColumn, COL_HOSTNAME, SIZE_OF_ARRAY(pMainCols[0].szColumn));
  124. StringCopy(pMainCols[1].szColumn, COL_ID, SIZE_OF_ARRAY(pMainCols[1].szColumn));
  125. StringCopy(pMainCols[2].szColumn, COL_ACCESSED_BY, SIZE_OF_ARRAY(pMainCols[2].szColumn));
  126. StringCopy(pMainCols[3].szColumn, COL_TYPE, SIZE_OF_ARRAY(pMainCols[3].szColumn));
  127. StringCopy(pMainCols[4].szColumn, COL_LOCK, SIZE_OF_ARRAY(pMainCols[4].szColumn));
  128. StringCopy(pMainCols[5].szColumn, COL_OPEN_MODE, SIZE_OF_ARRAY(pMainCols[5].szColumn));
  129. StringCopy(pMainCols[6].szColumn, COL_OPEN_FILE, SIZE_OF_ARRAY(pMainCols[6].szColumn));
  130. if( NULL != pszFormat)
  131. {
  132. if( 0 == StringCompare(pszFormat,GetResString(IDS_LIST),TRUE, 0))
  133. {
  134. dwFormat = SR_FORMAT_LIST;
  135. }
  136. else if( 0 == StringCompare(pszFormat,GetResString(IDS_CSV),TRUE,0))
  137. {
  138. dwFormat = SR_FORMAT_CSV;
  139. }
  140. }
  141. // Check if local machine
  142. if(( NULL == pszServer)||
  143. ( TRUE == IsLocalSystem(pszServer)))
  144. {
  145. DWORD dwBuffLength;
  146. dwBuffLength = RNR_BUFFER_SIZE + 1 ;
  147. // Gets the name of computer for local machine.
  148. GetComputerName(pszServerNameToShow,&dwBuffLength);
  149. // Show Local Open files
  150. DoLocalOpenFiles (dwFormat,bShowNoHeader,bVerbose,L"\0");
  151. ShowMessage(stdout,GetResString(IDS_SHARED_OPEN_FILES));
  152. ShowMessage(stdout,GetResString(IDS_LOCAL_OPEN_FILES_SP2));
  153. }
  154. else
  155. {
  156. // pszServername can be changed in GetHostName function
  157. // so a copy of pszServer is passed.
  158. StringCopy(pszServerNameToShow, pszServer, MIN_MEMORY_REQUIRED);
  159. if( FALSE == GetHostName(pszServerNameToShow))
  160. {
  161. SAFEDELETE(pszServerNameToShow);
  162. SAFEDELETE(pszServerType);
  163. SAFERELDYNARRAY(pColData);
  164. SAFEDELETE(pszTemp);
  165. return FALSE;
  166. }
  167. }
  168. // Server type is "Windows" as NetFileEnum enumerates file only for
  169. // files opened for windows client
  170. StringCopy(pszServerType,GetResString(IDS_STRING_WINDOWS),
  171. MIN_MEMORY_REQUIRED);
  172. do
  173. {
  174. //The NetFileEnum function returns information about some
  175. // or all open files (from Windows client) on a server
  176. dwError = NetFileEnum( pszServer, NULL, NULL, 3,
  177. (LPBYTE*)&pFileInfo3_1,
  178. MAX_PREFERRED_LENGTH,
  179. &dwEntriesRead,
  180. &dwTotalEntries,
  181. (PDWORD_PTR)&dwResumeHandle );
  182. if( ERROR_ACCESS_DENIED == dwError)
  183. {
  184. SetLastError(E_ACCESSDENIED);
  185. SaveLastError();
  186. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  187. SAFEDELETE(pszServerNameToShow);
  188. SAFEDELETE(pszServerType);
  189. SAFERELDYNARRAY(pColData);
  190. SAFEDELETE(pszTemp);
  191. return FALSE;
  192. }
  193. pFileInfo3Org_1 = pFileInfo3_1;
  194. if( NERR_Success == dwError || ERROR_MORE_DATA == dwError)
  195. {
  196. for ( dwIndx = 0; dwIndx < dwEntriesRead;
  197. dwIndx++, pFileInfo3_1++ )
  198. {
  199. // Now fill the result to dynamic array "pColData"
  200. DynArrayAppendRow( pColData, 0 );
  201. // Hostname
  202. DynArrayAppendString2(pColData,dwRow,pszServerNameToShow,0);
  203. // id
  204. StringCchPrintfW(pszTemp, MIN_MEMORY_REQUIRED-1,_T("%lu"),
  205. pFileInfo3_1->fi3_id);
  206. DynArrayAppendString2(pColData ,dwRow,pszTemp,0);
  207. // Accessed By
  208. if(StringLength(pFileInfo3_1->fi3_username, 0)<=0)
  209. {
  210. DynArrayAppendString2(pColData,dwRow,
  211. GetResString(IDS_NA),0);
  212. }
  213. else
  214. {
  215. DynArrayAppendString2(pColData,dwRow,
  216. pFileInfo3_1->fi3_username,0);
  217. }
  218. // Type
  219. DynArrayAppendString2(pColData,dwRow,pszServerType,0);
  220. // Locks
  221. StringCchPrintfW(pszTemp,MIN_MEMORY_REQUIRED-1,
  222. _T("%ld"),pFileInfo3_1->fi3_num_locks);
  223. DynArrayAppendString2(pColData ,dwRow,pszTemp,0);
  224. // Checks for open file mode
  225. if((pFileInfo3_1->fi3_permissions & PERM_FILE_READ)&&
  226. (pFileInfo3_1->fi3_permissions & PERM_FILE_WRITE ))
  227. {
  228. DynArrayAppendString2(pColData,dwRow,
  229. GetResString(IDS_READ_WRITE),0);
  230. }
  231. else if(pFileInfo3_1->fi3_permissions & PERM_FILE_WRITE )
  232. {
  233. DynArrayAppendString2(pColData,dwRow,
  234. GetResString(IDS_WRITE),0);
  235. }
  236. else if(pFileInfo3_1->fi3_permissions & PERM_FILE_READ )
  237. {
  238. DynArrayAppendString2(pColData,dwRow,
  239. GetResString(IDS_READ),0);
  240. }
  241. else
  242. {
  243. DynArrayAppendString2(pColData,dwRow,
  244. GetResString(IDS_NOACCESS),0);
  245. }
  246. // If show result is table mode and if
  247. // open file length is gerater than
  248. // column with, Open File string cut from right
  249. // by COL_WIDTH_OPEN_FILE-5 and "..." will be
  250. // inserted before the string.
  251. // Example o/p: ...notepad.exe
  252. szCHString = pFileInfo3_1->fi3_pathname;
  253. if( FALSE == bVerbose)
  254. {
  255. if((szCHString.GetLength()>(COL_WIDTH_OPEN_FILE-5))&&
  256. ( SR_FORMAT_TABLE == dwFormat))
  257. {
  258. // If file path is too big to fit in column width
  259. // then it is cut like..
  260. // c:\...\rest_of_the_path.
  261. CHString szTemp =
  262. szCHString.Right(COL_WIDTH_OPEN_FILE-5);;
  263. DWORD dwTemp = szTemp.GetLength();
  264. szTemp = szTemp.Mid(szTemp.Find(SINGLE_SLASH),
  265. dwTemp);
  266. szCHString.Format(L"%s%s%s",szCHString.Mid(0,3),
  267. DOT_DOT,
  268. szTemp);
  269. pMainCols[6].dwWidth = COL_WIDTH_OPEN_FILE+1;
  270. }
  271. }
  272. else
  273. {
  274. pMainCols[6].dwWidth = 80;
  275. }
  276. // Open File name
  277. DynArrayAppendString2(pColData,dwRow,
  278. (LPCWSTR)szCHString,0);
  279. bAtLeastOne = TRUE;
  280. dwRow++;
  281. }// Enf for loop
  282. }
  283. // Free the block
  284. if( NULL != pFileInfo3Org_1 )
  285. {
  286. NetApiBufferFree( pFileInfo3Org_1 );
  287. pFileInfo3_1 = NULL;
  288. }
  289. } while ( ERROR_MORE_DATA == dwError );
  290. // Now Enumerate Open files for MAC Client..................
  291. // Server type is "Machintosh" as AfpAdminFileEnum enumerates file only for
  292. // files opened for Mac client
  293. StringCopy(pszServerType, MAC_OS, MIN_MEMORY_REQUIRED);
  294. // DLL required stored always in \windows\system32 directory....
  295. // so get windows directory first.
  296. if( 0 != GetSystemDirectory(szDllPath, MAX_PATH))
  297. {
  298. // appending dll file name
  299. StringConcat(szDllPath,MAC_DLL_FILE_NAME,MAX_PATH);
  300. hModule = ::LoadLibrary (szDllPath);
  301. if( NULL == hModule)
  302. {
  303. ShowMessage(stderr,GetResString(IDS_ID_SHOW_ERROR));
  304. // Shows the error string set by API function.
  305. ShowLastError(stderr);
  306. SAFEDELETE(pszServerNameToShow);
  307. SAFEDELETE(pszServerType);
  308. SAFERELDYNARRAY(pColData);
  309. SAFEDELETE(pszTemp);
  310. return FALSE;
  311. }
  312. }
  313. else
  314. {
  315. ShowMessage(stderr,GetResString(IDS_ID_SHOW_ERROR));
  316. // Shows the error string set by API function.
  317. ShowLastError(stderr);
  318. SAFEDELETE(pszServerNameToShow);
  319. SAFEDELETE(pszServerType);
  320. SAFERELDYNARRAY(pColData);
  321. SAFEDELETE(pszTemp);
  322. return FALSE;
  323. }
  324. AfpAdminConnect =
  325. (CONNECTPROC)::GetProcAddress (hModule,"AfpAdminConnect");
  326. AfpAdminFileEnum =
  327. (FILEENUMPROC)::GetProcAddress (hModule,"AfpAdminFileEnum");
  328. AfpAdminBufferFree =
  329. (MEMFREEPROC)::GetProcAddress (hModule,"AfpAdminBufferFree");
  330. if(( NULL == AfpAdminConnect) ||
  331. ( NULL == AfpAdminFileEnum) ||
  332. ( NULL == AfpAdminBufferFree))
  333. {
  334. ShowMessage(stderr,GetResString(IDS_ID_SHOW_ERROR));
  335. // Shows the error string set by API function.
  336. ShowLastError(stderr);
  337. SAFEDELETE(pszServerNameToShow);
  338. SAFEDELETE(pszServerType);
  339. SAFERELDYNARRAY(pColData);
  340. SAFEDELETE(pszTemp);
  341. FREE_LIBRARY(hModule);
  342. return FALSE;
  343. }
  344. // Connection ID is required for AfpAdminFileEnum function.
  345. // So connect to server to get connect id...
  346. DWORD retval_connect = AfpAdminConnect(const_cast<LPWSTR>(pszServer),
  347. &ulSFMServerConnection );
  348. if( 0 != retval_connect)
  349. {
  350. ShowMessage(stderr,GetResString(IDS_ID_SHOW_ERROR));
  351. // Shows the error string set by API function.
  352. ShowLastError(stderr);
  353. SAFEDELETE(pszServerNameToShow);
  354. SAFEDELETE(pszServerType);
  355. SAFERELDYNARRAY(pColData);
  356. SAFEDELETE(pszTemp);
  357. FREE_LIBRARY(hModule);
  358. return FALSE;
  359. }
  360. do
  361. {
  362. //The AfpAdminFileEnum function returns information about some
  363. // or all open files (from MAC client) on a server
  364. dwError = AfpAdminFileEnum( ulSFMServerConnection,
  365. (PBYTE*)&pFileInfo,
  366. (DWORD)-1L,
  367. &dwEntriesRead,
  368. &dwTotalEntries,
  369. &hEnumHandle );
  370. if( ERROR_ACCESS_DENIED == dwError)
  371. {
  372. SetLastError(E_ACCESSDENIED);
  373. SaveLastError();
  374. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  375. SAFEDELETE(pszServerNameToShow);
  376. SAFEDELETE(pszServerType);
  377. SAFERELDYNARRAY(pColData);
  378. SAFEDELETE(pszTemp);
  379. FREE_LIBRARY(hModule);
  380. return FALSE;
  381. }
  382. pFileInfoOrg = pFileInfo;
  383. if( NERR_Success == dwError || ERROR_MORE_DATA == dwError)
  384. {
  385. for ( dwIndx = 0 ; dwIndx < dwEntriesRead;
  386. dwIndx++, pFileInfo++ )
  387. {
  388. // Now fill the result to dynamic array "pColData"
  389. DynArrayAppendRow( pColData, 0 );
  390. // Hostname
  391. DynArrayAppendString2(pColData,dwRow,pszServerNameToShow,0);
  392. // id
  393. StringCchPrintfW(pszTemp,MIN_MEMORY_REQUIRED-1,
  394. _T("%lu"),pFileInfo->afpfile_id );
  395. DynArrayAppendString2(pColData ,dwRow,pszTemp,0);
  396. // Accessed By
  397. if(StringLength(pFileInfo->afpfile_username,0 )<=0)
  398. {
  399. DynArrayAppendString2(pColData,dwRow,
  400. GetResString(IDS_NA),0);
  401. }
  402. else
  403. {
  404. DynArrayAppendString2(pColData,dwRow,
  405. pFileInfo->afpfile_username,0);
  406. }
  407. // Server Type
  408. DynArrayAppendString2(pColData,dwRow,pszServerType,0);
  409. // Locks
  410. StringCchPrintfW(pszTemp,MIN_MEMORY_REQUIRED-1,
  411. _T("%ld"),pFileInfo->afpfile_num_locks );
  412. DynArrayAppendString2(pColData ,dwRow,pszTemp,0);
  413. // Checks for open file mode
  414. if((pFileInfo->afpfile_open_mode & AFP_OPEN_MODE_READ)&&
  415. (pFileInfo->afpfile_open_mode & AFP_OPEN_MODE_WRITE ))
  416. {
  417. DynArrayAppendString2(pColData,dwRow,
  418. GetResString(IDS_READ_WRITE),0);
  419. }
  420. else if(pFileInfo->afpfile_open_mode & AFP_OPEN_MODE_WRITE )
  421. {
  422. DynArrayAppendString2(pColData,dwRow,
  423. GetResString(IDS_WRITE),0);
  424. }
  425. else if(pFileInfo->afpfile_open_mode & AFP_OPEN_MODE_READ )
  426. {
  427. DynArrayAppendString2(pColData,dwRow,
  428. GetResString(IDS_READ),0);
  429. }
  430. else
  431. {
  432. DynArrayAppendString2(pColData,dwRow,
  433. GetResString(IDS_NOACCESS),0);
  434. }
  435. // If show result is table mode and if
  436. // open file length is gerater than
  437. // column with, Open File string cut from right
  438. // by COL_WIDTH_OPEN_FILE-5 and "..." will be
  439. // inserted before the string.
  440. // Example o/p: ...notepad.exe
  441. szCHString = pFileInfo->afpfile_path ;
  442. if( FALSE == bVerbose)
  443. {
  444. if((szCHString.GetLength()>(COL_WIDTH_OPEN_FILE-5))&&
  445. ( SR_FORMAT_TABLE == dwFormat))
  446. {
  447. // If file path is too big to fit in column width
  448. // then it is cut like..
  449. // c:\...\rest_of_the_path.
  450. CHString szTemp =
  451. szCHString.Right(COL_WIDTH_OPEN_FILE-5);
  452. DWORD dwTemp = szTemp.GetLength();
  453. szTemp = szTemp.Mid(szTemp.Find(SINGLE_SLASH),
  454. dwTemp);
  455. szCHString.Format(L"%s%s%s",szCHString.Mid(0,3),
  456. DOT_DOT,
  457. szTemp);
  458. pMainCols[6].dwWidth = COL_WIDTH_OPEN_FILE+1;
  459. }
  460. }
  461. else
  462. {
  463. pMainCols[6].dwWidth = 80;
  464. }
  465. // Open File name
  466. DynArrayAppendString2(pColData,dwRow,
  467. (LPCWSTR)szCHString,0);
  468. bAtLeastOne = TRUE;
  469. dwRow++;
  470. }// Enf for loop
  471. }
  472. // Free the block
  473. if( NULL != pFileInfoOrg )
  474. {
  475. AfpAdminBufferFree( pFileInfoOrg );
  476. pFileInfo = NULL;
  477. }
  478. } while ( ERROR_MORE_DATA == dwError);
  479. // if not a single open file found, show info
  480. // as - INFO: No open file found.
  481. if( FALSE == bAtLeastOne)
  482. {
  483. ShowMessage(stdout,GetResString(IDS_NO_OPENFILES));
  484. }
  485. else
  486. {
  487. //Output should start after one blank line
  488. if( SR_FORMAT_CSV != dwFormat)
  489. {
  490. ShowMessage(stdout,BLANK_LINE);
  491. }
  492. if( TRUE == bShowNoHeader)
  493. {
  494. dwFormat |=SR_NOHEADER;
  495. }
  496. ShowResults(MAX_OUTPUT_COLUMN,pMainCols,dwFormat,pColData);
  497. // Destroy dynamic array.
  498. SAFERELDYNARRAY(pColData);
  499. }
  500. SAFEDELETE(pszServerNameToShow);
  501. SAFEDELETE(pszServerType);
  502. SAFERELDYNARRAY(pColData);
  503. SAFEDELETE(pszTemp);
  504. FREE_LIBRARY(hModule);
  505. return TRUE;
  506. }
  507. BOOL
  508. GetHostName(
  509. IN OUT PTCHAR pszServer
  510. )
  511. /*++
  512. Routine Description:
  513. This routine gets Hostname of remote computer
  514. Arguments:
  515. [in/out] pszServer = ServerName
  516. Return Value:
  517. TRUE : if server name retured successfully
  518. FALSE: otherwise
  519. --*/
  520. {
  521. if(NULL == pszServer )
  522. {
  523. return FALSE;
  524. }
  525. BOOL bReturnValue = TRUE; // function return state.
  526. WORD wVersionRequested; //Variable used to store highest version
  527. //number that Windows Sockets can support.
  528. // The high-order byte specifies the minor
  529. // version (revision) number; the low-order
  530. // byte specifies the major version number
  531. WSADATA wsaData;//Variable receive details of the Windows Sockets
  532. //implementation
  533. DWORD dwErr; // strore error code
  534. wVersionRequested = MAKEWORD( 2, 2 );
  535. //WSAStartup function initiates use of
  536. //Ws2_32.dll by a process
  537. dwErr = WSAStartup( wVersionRequested, &wsaData );
  538. if( 0 != dwErr)
  539. {
  540. SetLastError(WSAGetLastError());
  541. ShowMessage(stderr,GetResString(IDS_ID_SHOW_ERROR));
  542. ShowLastError(stderr);
  543. return FALSE;
  544. }
  545. if(IsValidIPAddress(pszServer))
  546. {
  547. LPTSTR pszTemp = NULL;
  548. // gethostbyaddr function retrieves the host information
  549. //corresponding to a network address.
  550. pszTemp = GetHostByAddr(pszServer);
  551. if( NULL != pszTemp)
  552. {
  553. StringCopy(pszServer,pszTemp,MIN_MEMORY_REQUIRED);
  554. SAFEDELETE(pszTemp);
  555. }
  556. else
  557. {
  558. SetLastError(WSAGetLastError());
  559. ShowMessage(stderr,GetResString(IDS_ID_SHOW_ERROR));
  560. ShowLastError(stderr);
  561. }
  562. }
  563. // Check validity for the server name
  564. else if (IsValidServer(pszServer))
  565. {
  566. LPTSTR pszTemp = NULL;
  567. pszTemp = GetHostByName(pszServer);
  568. if( NULL != pszTemp)
  569. {
  570. StringCopy(pszServer, pszTemp,MIN_MEMORY_REQUIRED);
  571. SAFEDELETE(pszTemp);
  572. }
  573. else
  574. {
  575. SetLastError(WSAGetLastError());
  576. ShowMessage(stderr,GetResString(IDS_ID_SHOW_ERROR));
  577. ShowLastError(stderr);
  578. }
  579. }
  580. else
  581. {
  582. // server name is incorrect, show error message.
  583. ShowMessage(stderr,GetResString(IDS_ID_SHOW_ERROR ));
  584. ShowMessage(stderr,GetResString(IDS_INVALID_SERVER_NAME));
  585. }
  586. //WSACleanup function terminates use of the Ws2_32.dll.
  587. WSACleanup( );
  588. return bReturnValue;
  589. }
  590. LPTSTR
  591. GetHostByName(
  592. IN LPCTSTR pszName
  593. )
  594. /*++
  595. Routine Description:
  596. Get host information corresponding to a hostname.
  597. Arguments:
  598. [in ]pszName - A pointer to the null terminated name of the host.
  599. Returns:
  600. returns hostname.
  601. --*/
  602. {
  603. LPTSTR pszReturn = NULL;
  604. pszReturn = new TCHAR[RNR_BUFFER_SIZE];
  605. if(NULL == pszReturn)
  606. {
  607. SetLastError(WSA_NOT_ENOUGH_MEMORY);
  608. return NULL;
  609. }
  610. SecureZeroMemory(pszReturn,RNR_BUFFER_SIZE * sizeof(TCHAR));
  611. if(GetXYDataEnt( RNR_BUFFER_SIZE,
  612. (LPTSTR)pszName,
  613. &HostnameGuid,
  614. pszReturn))
  615. {
  616. return pszReturn;
  617. }
  618. else
  619. return NULL;
  620. }
  621. LPTSTR
  622. GetHostByAddr(
  623. IN LPCTSTR pszAddress
  624. )
  625. /*++
  626. Routine Description:
  627. Get host information corresponding to an address.
  628. Arguments:
  629. [in]pszAddress - A pointer to an address in network byte order.
  630. Returns:
  631. returns Hostname
  632. --*/
  633. {
  634. LPTSTR pszReturn = NULL;
  635. pszReturn = new TCHAR[RNR_BUFFER_SIZE];
  636. if(NULL == pszReturn)
  637. {
  638. SetLastError(WSA_NOT_ENOUGH_MEMORY);
  639. return NULL;
  640. }
  641. SecureZeroMemory(pszReturn,RNR_BUFFER_SIZE * sizeof(TCHAR));
  642. if(GetXYDataEnt(RNR_BUFFER_SIZE,
  643. (LPTSTR)pszAddress,
  644. &AddressGuid,
  645. pszReturn))
  646. {
  647. return pszReturn;
  648. }
  649. else
  650. return NULL;
  651. }
  652. LPBLOB
  653. GetXYDataEnt(
  654. IN DWORD dwLength,
  655. IN LPTSTR pszName,
  656. IN LPGUID lpType,
  657. OUT LPTSTR pszHostName
  658. )
  659. /*++
  660. Routine Description:
  661. This function will query system for given GUID.
  662. Arguments:
  663. [in] dwLength : length.
  664. [in] pszName : Hostname (hostname/ipaddress).
  665. [in] lpType : GUID of the class.
  666. [out] pszHostName : Hostname which is returned by query.
  667. Return Value:
  668. LPBLOB
  669. --*/
  670. {
  671. WSAQUERYSET *pwsaq = (WSAQUERYSET*)new TCHAR[RNR_BUFFER_SIZE];
  672. int err;
  673. HANDLE hRnR;
  674. LPBLOB pvRet = 0;
  675. if(NULL == pwsaq)
  676. {
  677. return NULL;
  678. }
  679. SecureZeroMemory(pwsaq,RNR_BUFFER_SIZE * sizeof(TCHAR));
  680. pwsaq->dwSize = sizeof(WSAQUERYSET);
  681. pwsaq->lpszServiceInstanceName = pszName;
  682. pwsaq->lpServiceClassId = lpType;
  683. pwsaq->dwNameSpace = NS_ALL;
  684. pwsaq->dwNumberOfProtocols = 2;
  685. pwsaq->lpafpProtocols = &afp[0];
  686. err = WSALookupServiceBegin( pwsaq,
  687. LUP_RETURN_BLOB | LUP_RETURN_NAME,
  688. &hRnR);
  689. if( NO_ERROR == err)
  690. {
  691. //
  692. // The query was accepted, so execute it via the Next call.
  693. //
  694. err = WSALookupServiceNext(
  695. hRnR,
  696. 0,
  697. &dwLength,
  698. pwsaq);
  699. //
  700. // if NO_ERROR was returned and a BLOB is present, this
  701. // worked, just return the requested information. Otherwise,
  702. // invent an error or capture the transmitted one.
  703. //
  704. if( NO_ERROR == err)
  705. {
  706. if(pvRet = pwsaq->lpBlob)
  707. {
  708. if(pszHostName)
  709. {
  710. StringCopy(pszHostName, pwsaq->lpszServiceInstanceName,
  711. MIN_MEMORY_REQUIRED);
  712. }
  713. }
  714. else
  715. {
  716. err = WSANO_DATA;
  717. }
  718. }
  719. else
  720. {
  721. //
  722. // WSALookupServiceEnd clobbers LastError so save
  723. // it before closing the handle.
  724. //
  725. err = GetLastError();
  726. }
  727. WSALookupServiceEnd(hRnR);
  728. //
  729. // if an error happened, stash the value in LastError
  730. //
  731. if(NO_ERROR != err)
  732. {
  733. SetLastError(err);
  734. }
  735. }
  736. SAFEDELETE(pwsaq);
  737. return(pvRet);
  738. }