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.

714 lines
27 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. /*****************************************************************************
  16. Routine Description:
  17. This function Queries for all open files in for the server and display them.
  18. Arguments:
  19. [in] pszServer : Will have the server name.
  20. [in] bShowNoHeader : Will have the value whether to show header or not.
  21. [in] pszFormat : Will have the format required to show the result.
  22. [in] bVerbose : Will have the value whether to show verbose
  23. result.
  24. Return Value:
  25. TRUE if query successful
  26. else FALSE
  27. ******************************************************************************/
  28. BOOL
  29. DoQuery(PTCHAR pszServer,
  30. BOOL bShowNoHeader,
  31. PTCHAR pszFormat,
  32. BOOL bVerbose)
  33. {
  34. CHString szCHString;
  35. DWORD dwEntriesRead = 0;// Receives the count of elements actually
  36. // enumerated by "NetFileEnum" function
  37. DWORD dwTotalEntries = 0;//Receives the total number of entries that could
  38. //have been enumerated from the current
  39. //resume position by "NetFileEnum" function
  40. DWORD dwResumeHandle = 0;//Contains a resume handle which is used to
  41. //continue an existing file search.
  42. //The handle should be zero on the first call and
  43. //left unchanged for subsequent calls.
  44. //If resume_handle is NULL,
  45. //then no resume handle is stored. This variable
  46. // used in calling "NetFileEnum" function.
  47. BOOL bAtLeastOne = FALSE;//Contains the state whether at least one record
  48. //found for this query
  49. DWORD dwFormat = SR_FORMAT_TABLE;//Stores format flag required to show
  50. // result on console. Default format
  51. //is TABLE
  52. LPFILE_INFO_3 pFileInfo3_1 = NULL;// LPFILE_INFO_3 structure contains the
  53. // identification number and other
  54. // pertinent information about files,
  55. // devices, and pipes.
  56. LPSERVER_INFO_100 pServerInfo = NULL;//The SERVER_INFO_100 structure
  57. //contains information about the
  58. //specified server, including the name
  59. //and platform
  60. DWORD dwError = 0;//Contains return value for "NetFileEnum" function
  61. LONG lRowNo = 0;// Row index variable
  62. LPTSTR pszServerType = new TCHAR[MIN_MEMORY_REQUIRED+1];//Stores server type
  63. // information
  64. AFP_FILE_INFO* pFileInfo = NULL;
  65. DWORD hEnumHandle = 0;
  66. HRESULT hr = S_OK;
  67. NET_API_STATUS retval = NERR_Success;
  68. AFP_SERVER_HANDLE ulSFMServerConnection = 0;
  69. typedef DWORD (*FILEENUMPROC)(AFP_SERVER_HANDLE,LPBYTE*,DWORD,LPDWORD,LPDWORD,LPDWORD);
  70. typedef DWORD (*CONNECTPROC) (LPWSTR,PAFP_SERVER_HANDLE);
  71. typedef DWORD (*MEMFREEPROC) (LPVOID);
  72. TCHAR szDllPath[MAX_PATH+1] = NULL_STRING;// buffer for Windows directory
  73. // AfpAdminConnect and AfpAdminFileEnum functions are undocumented function
  74. // and used only for MAC client.
  75. CONNECTPROC AfpAdminConnect = NULL; // Function Pointer
  76. FILEENUMPROC AfpAdminFileEnum = NULL;// Function Pointer
  77. MEMFREEPROC AfpAdminBufferFree = NULL; // Function Pointer
  78. HMODULE hModule = 0; // To store retval for LoadLibrary
  79. DWORD dwIndx = 0; // Indx variable
  80. DWORD dwRow = 0; // Row No indx.
  81. //server name to be shown
  82. LPTSTR pszServerNameToShow = new TCHAR[MIN_MEMORY_REQUIRED+ 1];
  83. LPTSTR pszTemp = new TCHAR[MIN_MEMORY_REQUIRED+ 1];
  84. //Some column required to hide in non verbose mode query
  85. DWORD dwMask = bVerbose?SR_TYPE_STRING:SR_HIDECOLUMN|SR_TYPE_STRING;
  86. TCOLUMNS pMainCols[]=
  87. {
  88. {NULL_STRING,COL_WIDTH_HOSTNAME,dwMask,NULL_STRING,NULL,NULL},
  89. {NULL_STRING,COL_WIDTH_ID,SR_TYPE_STRING,NULL_STRING,NULL,NULL},
  90. {NULL_STRING,COL_WIDTH_ACCESSED_BY,SR_TYPE_STRING,NULL_STRING,NULL,NULL},
  91. {NULL_STRING,COL_WIDTH_TYPE,SR_TYPE_STRING,NULL_STRING,NULL,NULL},
  92. {NULL_STRING,COL_WIDTH_LOCK,dwMask,NULL_STRING,NULL,NULL},
  93. {NULL_STRING,COL_WIDTH_OPEN_MODE,dwMask,NULL_STRING,NULL,NULL},
  94. {NULL_STRING,COL_WIDTH_OPEN_FILE,SR_TYPE_STRING|(SR_NO_TRUNCATION&~(SR_WORDWRAP)),NULL_STRING,NULL,NULL}
  95. };
  96. TARRAY pColData = CreateDynamicArray();//array to stores
  97. //result
  98. if((pszServerNameToShow == NULL)||
  99. (pszServerType == NULL)||
  100. (pColData == NULL)||
  101. (pszTemp == NULL))
  102. {
  103. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  104. DISPLAY_MESSAGE(stderr,GetResString(IDS_ID_SHOW_ERROR));
  105. ShowLastError(stderr);
  106. SAFEDELETE(pszServerNameToShow);
  107. SAFEDELETE(pszServerType);
  108. SAFERELDYNARRAY(pColData);
  109. SAFEDELETE(pszTemp);
  110. return FALSE;
  111. }
  112. // Initialize allocated arrays
  113. memset(pszServerNameToShow,0,MIN_MEMORY_REQUIRED*sizeof(TCHAR));
  114. memset(pszServerType,0,MIN_MEMORY_REQUIRED*sizeof(TCHAR));
  115. // Fill column headers with TEXT.
  116. lstrcpy(pMainCols[0].szColumn, COL_HOSTNAME);
  117. lstrcpy(pMainCols[1].szColumn, COL_ID);
  118. lstrcpy(pMainCols[2].szColumn, COL_ACCESSED_BY);
  119. lstrcpy(pMainCols[3].szColumn, COL_TYPE);
  120. lstrcpy(pMainCols[4].szColumn, COL_LOCK);
  121. lstrcpy(pMainCols[5].szColumn, COL_OPEN_MODE);
  122. lstrcpy(pMainCols[6].szColumn, COL_OPEN_FILE);
  123. if(pszFormat!=NULL)
  124. {
  125. if(lstrcmpi(pszFormat,GetResString(IDS_LIST))==0)
  126. {
  127. dwFormat = SR_FORMAT_LIST;
  128. }
  129. else if(lstrcmpi(pszFormat,GetResString(IDS_CSV))==0)
  130. {
  131. dwFormat = SR_FORMAT_CSV;
  132. }
  133. }
  134. // Check if local machine
  135. if((pszServer == NULL)||
  136. (IsLocalSystem(pszServer)==TRUE))
  137. {
  138. DWORD dwBuffLength;
  139. dwBuffLength = MAX_COMPUTERNAME_LENGTH + 1 ;
  140. // Gets the name of computer for local machine.
  141. GetComputerName(pszServerNameToShow,&dwBuffLength);
  142. // Show Local Open files
  143. DoLocalOpenFiles (dwFormat,bShowNoHeader,bVerbose,NULL_STRING);
  144. ShowMessage(stdout,GetResString(IDS_SHARED_OPEN_FILES));
  145. ShowMessage(stdout,GetResString(IDS_LOCAL_OPEN_FILES_SP2));
  146. }
  147. else
  148. {
  149. // pszServername can be changed in GetHostName function
  150. // so a copy of pszServer is passed.
  151. lstrcpy(pszServerNameToShow, pszServer);
  152. if(GetHostName(pszServerNameToShow)==FALSE)
  153. {
  154. SAFEDELETE(pszServerNameToShow);
  155. SAFEDELETE(pszServerType);
  156. SAFERELDYNARRAY(pColData);
  157. SAFEDELETE(pszTemp);
  158. return FALSE;
  159. }
  160. }
  161. // Server type is "Windows" as NetFileEnum enumerates file only for
  162. // files opened for windows client
  163. lstrcpy(pszServerType,GetResString(IDS_STRING_WINDOWS));
  164. do
  165. {
  166. //The NetFileEnum function returns information about some
  167. // or all open files (from Windows client) on a server
  168. dwError = NetFileEnum( pszServer, NULL, NULL, 3,
  169. (LPBYTE*)&pFileInfo3_1,
  170. MAX_PREFERRED_LENGTH,
  171. &dwEntriesRead,
  172. &dwTotalEntries,
  173. (PDWORD_PTR)&dwResumeHandle );
  174. if(dwError == ERROR_ACCESS_DENIED)
  175. {
  176. SetLastError(ERROR_ACCESS_DENIED);
  177. DISPLAY_MESSAGE(stderr,GetResString(IDS_ID_SHOW_ERROR));
  178. ShowLastError(stderr);
  179. SAFEDELETE(pszServerNameToShow);
  180. SAFEDELETE(pszServerType);
  181. SAFERELDYNARRAY(pColData);
  182. SAFEDELETE(pszTemp);
  183. return FALSE;
  184. }
  185. if( dwError == NERR_Success || dwError == ERROR_MORE_DATA )
  186. {
  187. for ( dwIndx = 0; dwIndx < dwEntriesRead;
  188. dwIndx++, pFileInfo3_1++ )
  189. {
  190. // Now fill the result to dynamic array "pColData"
  191. DynArrayAppendRow( pColData, 0 );
  192. // Hostname
  193. DynArrayAppendString2(pColData,dwRow,pszServerNameToShow,0);
  194. // id
  195. wsprintf(pszTemp,_T("%lu"),pFileInfo3_1->fi3_id);
  196. DynArrayAppendString2(pColData ,dwRow,pszTemp,0);
  197. // Accessed By
  198. if(lstrlen(pFileInfo3_1->fi3_username)<=0)
  199. {
  200. DynArrayAppendString2(pColData,dwRow,
  201. GetResString(IDS_NA),0);
  202. }
  203. else
  204. {
  205. DynArrayAppendString2(pColData,dwRow,
  206. pFileInfo3_1->fi3_username,0);
  207. }
  208. // Type
  209. DynArrayAppendString2(pColData,dwRow,pszServerType,0);
  210. // Locks
  211. wsprintf(pszTemp,_T("%ld"),pFileInfo3_1->fi3_num_locks);
  212. DynArrayAppendString2(pColData ,dwRow,pszTemp,0);
  213. // Checks for open file mode
  214. if((pFileInfo3_1->fi3_permissions & PERM_FILE_READ)&&
  215. (pFileInfo3_1->fi3_permissions & PERM_FILE_WRITE ))
  216. {
  217. DynArrayAppendString2(pColData,dwRow,
  218. GetResString(IDS_READ_WRITE),0);
  219. }
  220. else if(pFileInfo3_1->fi3_permissions & PERM_FILE_WRITE )
  221. {
  222. DynArrayAppendString2(pColData,dwRow,
  223. GetResString(IDS_WRITE),0);
  224. }
  225. else if(pFileInfo3_1->fi3_permissions & PERM_FILE_READ )
  226. {
  227. DynArrayAppendString2(pColData,dwRow,
  228. GetResString(IDS_READ),0);
  229. }
  230. else
  231. {
  232. DynArrayAppendString2(pColData,dwRow,
  233. GetResString(IDS_NOACCESS),0);
  234. }
  235. // If show result is table mode and if
  236. // open file length is gerater than
  237. // column with, Open File string cut from right
  238. // by COL_WIDTH_OPEN_FILE-5 and "..." will be
  239. // inserted before the string.
  240. // Example o/p: ...notepad.exe
  241. szCHString = pFileInfo3_1->fi3_pathname;
  242. if(bVerbose==FALSE)
  243. {
  244. if((szCHString.GetLength()>(COL_WIDTH_OPEN_FILE-5))&&
  245. (dwFormat == SR_FORMAT_TABLE))
  246. {
  247. // If file path is too big to fit in column width
  248. // then it is cut like..
  249. // c:\...\rest_of_the_path.
  250. CHString szTemp = szCHString.Right(COL_WIDTH_OPEN_FILE-5);;
  251. DWORD dwTemp = szTemp.GetLength();
  252. szTemp = szTemp.Mid(szTemp.Find(SINGLE_SLASH),
  253. dwTemp);
  254. szCHString.Format(L"%s%s%s",szCHString.Mid(0,3),
  255. DOT_DOT,
  256. szTemp);
  257. pMainCols[6].dwWidth = COL_WIDTH_OPEN_FILE+1;
  258. }
  259. }
  260. else
  261. {
  262. pMainCols[6].dwWidth = 80;
  263. }
  264. // Open File name
  265. DynArrayAppendString2(pColData,dwRow,
  266. (LPCWSTR)szCHString,0);
  267. bAtLeastOne = TRUE;
  268. dwRow++;
  269. }// Enf for loop
  270. }
  271. // Free the block
  272. if( pFileInfo3_1 !=NULL)
  273. {
  274. NetApiBufferFree( pFileInfo3_1 );
  275. pFileInfo3_1 = NULL;
  276. }
  277. } while ( dwError == ERROR_MORE_DATA );
  278. // Now Enumerate Open files for MAC Client..................
  279. // Server type is "Machintosh" as AfpAdminFileEnum enumerates file only for
  280. // files opened for Mac client
  281. lstrcpy(pszServerType,MAC_OS);
  282. do
  283. {
  284. // DLL required stored always in \windows\system32 directory....
  285. // so get windows directory first.
  286. if(GetSystemDirectory(szDllPath, MAX_PATH)!= 0)
  287. {
  288. lstrcat(szDllPath,MAC_DLL_FILE_NAME); // appending dll file name
  289. hModule = ::LoadLibrary (szDllPath);
  290. if(hModule==NULL)
  291. {
  292. DISPLAY_MESSAGE(stderr,GetResString(IDS_ID_SHOW_ERROR));
  293. ShowLastError(stderr); // Shows the error string set by API function.
  294. SAFEDELETE(pszServerNameToShow);
  295. SAFEDELETE(pszServerType);
  296. SAFERELDYNARRAY(pColData);
  297. SAFEDELETE(pszTemp);
  298. return FALSE;
  299. }
  300. }
  301. else
  302. {
  303. DISPLAY_MESSAGE(stderr,GetResString(IDS_ID_SHOW_ERROR));
  304. ShowLastError(stderr); // Shows the error string set by API function.
  305. SAFEDELETE(pszServerNameToShow);
  306. SAFEDELETE(pszServerType);
  307. SAFERELDYNARRAY(pColData);
  308. SAFEDELETE(pszTemp);
  309. return FALSE;
  310. }
  311. AfpAdminConnect = (CONNECTPROC)::GetProcAddress (hModule,"AfpAdminConnect");
  312. AfpAdminFileEnum = (FILEENUMPROC)::GetProcAddress (hModule,"AfpAdminFileEnum");
  313. AfpAdminBufferFree = (MEMFREEPROC)::GetProcAddress (hModule,"AfpAdminBufferFree");
  314. if((AfpAdminConnect == NULL) ||
  315. (AfpAdminFileEnum == NULL) ||
  316. (AfpAdminBufferFree == NULL))
  317. {
  318. DISPLAY_MESSAGE(stderr,GetResString(IDS_ID_SHOW_ERROR));
  319. ShowLastError(stderr); // Shows the error string set by API function.
  320. SAFEDELETE(pszServerNameToShow);
  321. SAFEDELETE(pszServerType);
  322. SAFERELDYNARRAY(pColData);
  323. SAFEDELETE(pszTemp);
  324. FREE_LIBRARY(hModule);
  325. return FALSE;
  326. }
  327. // Connection ID is required for AfpAdminFileEnum function.
  328. // So connect to server to get connect id...
  329. DWORD retval_connect = AfpAdminConnect(const_cast<LPWSTR>(pszServer),
  330. &ulSFMServerConnection );
  331. if(retval_connect!=0)
  332. {
  333. DISPLAY_MESSAGE(stderr,GetResString(IDS_ID_SHOW_ERROR));
  334. ShowLastError(stderr); // Shows the error string set by API function.
  335. SAFEDELETE(pszServerNameToShow);
  336. SAFEDELETE(pszServerType);
  337. SAFERELDYNARRAY(pColData);
  338. SAFEDELETE(pszTemp);
  339. FREE_LIBRARY(hModule);
  340. return FALSE;
  341. }
  342. //The AfpAdminFileEnum function returns information about some
  343. // or all open files (from MAC client) on a server
  344. dwError = AfpAdminFileEnum( ulSFMServerConnection,
  345. (PBYTE*)&pFileInfo,
  346. (DWORD)-1L,
  347. &dwEntriesRead,
  348. &dwTotalEntries,
  349. &hEnumHandle );
  350. if(dwError == ERROR_ACCESS_DENIED)
  351. {
  352. SetLastError(ERROR_ACCESS_DENIED);
  353. DISPLAY_MESSAGE(stderr,GetResString(IDS_ID_SHOW_ERROR));
  354. ShowLastError(stderr);
  355. SAFEDELETE(pszServerNameToShow);
  356. SAFEDELETE(pszServerType);
  357. SAFERELDYNARRAY(pColData);
  358. SAFEDELETE(pszTemp);
  359. FREE_LIBRARY(hModule);
  360. return FALSE;
  361. }
  362. if( dwError == NERR_Success || dwError == ERROR_MORE_DATA )
  363. {
  364. for ( dwIndx = 0 ; dwIndx < dwEntriesRead;
  365. dwIndx++, pFileInfo++ )
  366. {
  367. // Now fill the result to dynamic array "pColData"
  368. DynArrayAppendRow( pColData, 0 );
  369. // Hostname
  370. DynArrayAppendString2(pColData,dwRow,pszServerNameToShow,0);
  371. // id
  372. wsprintf(pszTemp,_T("%lu"),pFileInfo->afpfile_id );
  373. DynArrayAppendString2(pColData ,dwRow,pszTemp,0);
  374. // Accessed By
  375. if(lstrlen(pFileInfo->afpfile_username )<=0)
  376. {
  377. DynArrayAppendString2(pColData,dwRow,
  378. GetResString(IDS_NA),0);
  379. }
  380. else
  381. {
  382. DynArrayAppendString2(pColData,dwRow,
  383. pFileInfo->afpfile_username,0);
  384. }
  385. // Server Type
  386. DynArrayAppendString2(pColData,dwRow,pszServerType,0);
  387. // Locks
  388. wsprintf(pszTemp,_T("%ld"),pFileInfo->afpfile_num_locks );
  389. DynArrayAppendString2(pColData ,dwRow,pszTemp,0);
  390. // Checks for open file mode
  391. if((pFileInfo->afpfile_open_mode & AFP_OPEN_MODE_READ)&&
  392. (pFileInfo->afpfile_open_mode & AFP_OPEN_MODE_WRITE ))
  393. {
  394. DynArrayAppendString2(pColData,dwRow,
  395. GetResString(IDS_READ_WRITE),0);
  396. }
  397. else if(pFileInfo->afpfile_open_mode & AFP_OPEN_MODE_WRITE )
  398. {
  399. DynArrayAppendString2(pColData,dwRow,
  400. GetResString(IDS_WRITE),0);
  401. }
  402. else if(pFileInfo->afpfile_open_mode & AFP_OPEN_MODE_READ )
  403. {
  404. DynArrayAppendString2(pColData,dwRow,
  405. GetResString(IDS_READ),0);
  406. }
  407. else
  408. {
  409. DynArrayAppendString2(pColData,dwRow,
  410. GetResString(IDS_NOACCESS),0);
  411. }
  412. // If show result is table mode and if
  413. // open file length is gerater than
  414. // column with, Open File string cut from right
  415. // by COL_WIDTH_OPEN_FILE-5 and "..." will be
  416. // inserted before the string.
  417. // Example o/p: ...notepad.exe
  418. szCHString = pFileInfo->afpfile_path ;
  419. if(bVerbose==FALSE)
  420. {
  421. if((szCHString.GetLength()>(COL_WIDTH_OPEN_FILE-5))&&
  422. (dwFormat == SR_FORMAT_TABLE))
  423. {
  424. // If file path is too big to fit in column width
  425. // then it is cut like..
  426. // c:\...\rest_of_the_path.
  427. CHString szTemp = szCHString.Right(COL_WIDTH_OPEN_FILE-5);;
  428. DWORD dwTemp = szTemp.GetLength();
  429. szTemp = szTemp.Mid(szTemp.Find(SINGLE_SLASH),
  430. dwTemp);
  431. szCHString.Format(L"%s%s%s",szCHString.Mid(0,3),
  432. DOT_DOT,
  433. szTemp);
  434. pMainCols[6].dwWidth = COL_WIDTH_OPEN_FILE+1;
  435. }
  436. }
  437. else
  438. {
  439. pMainCols[6].dwWidth = 80;
  440. }
  441. // Open File name
  442. DynArrayAppendString2(pColData,dwRow,
  443. (LPCWSTR)szCHString,0);
  444. bAtLeastOne = TRUE;
  445. dwRow++;
  446. }// Enf for loop
  447. }
  448. // Free the block
  449. if( pFileInfo!=NULL)
  450. {
  451. AfpAdminBufferFree( pFileInfo );
  452. pFileInfo = NULL;
  453. }
  454. } while ( dwError == ERROR_MORE_DATA );
  455. if(bAtLeastOne==FALSE)// if not a single open file found, show info
  456. // as - INFO: No open file found.
  457. {
  458. ShowMessage(stdout,GetResString(IDS_NO_OPENFILES));
  459. }
  460. else
  461. {
  462. // Display output result.
  463. if(bShowNoHeader==TRUE)
  464. {
  465. dwFormat |=SR_NOHEADER;
  466. }
  467. //Output should start after one blank line
  468. ShowMessage(stdout,BLANK_LINE);
  469. ShowResults(MAX_OUTPUT_COLUMN,pMainCols,dwFormat,pColData);
  470. // Destroy dynamic array.
  471. SAFERELDYNARRAY(pColData);
  472. }
  473. SAFEDELETE(pszServerNameToShow);
  474. SAFEDELETE(pszServerType);
  475. SAFERELDYNARRAY(pColData);
  476. SAFEDELETE(pszTemp);
  477. FREE_LIBRARY(hModule);
  478. return TRUE;
  479. }
  480. // ***************************************************************************
  481. // Routine Description:
  482. // This routine gets Hostname of remote computer
  483. //
  484. // Arguments:
  485. // [in/out] pszServer = ServerName
  486. // Return Value:
  487. // TRUE : if server name retured successfully
  488. // FALSE: otherwise
  489. // ***************************************************************************
  490. BOOL GetHostName(PTCHAR pszServer)
  491. {
  492. if(pszServer == NULL)
  493. return FALSE;
  494. BOOL bReturnValue = FALSE; // function return state.
  495. char* pszServerName = new char[MIN_MEMORY_REQUIRED+1];//char is used as winsock
  496. // required these type.
  497. HOSTENT* hostEnt = NULL;//The Windows Sockets hostEnt structure is
  498. //used by functions to store information
  499. //about a given host.
  500. WORD wVersionRequested; //Variable used to store highest version
  501. //number that Windows Sockets can support.
  502. // The high-order byte specifies the minor
  503. // version (revision) number; the low-order
  504. // byte specifies the major version number
  505. WSADATA wsaData;//Variable receive details of the Windows Sockets
  506. //implementation
  507. DWORD dwErr; // strore error code
  508. if(pszServerName==NULL)
  509. {
  510. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  511. DISPLAY_MESSAGE(stderr,GetResString(IDS_ID_SHOW_ERROR));
  512. //Release allocated memory safely
  513. ShowLastError(stderr);
  514. return FALSE;
  515. }
  516. wVersionRequested = MAKEWORD( 2, 2 );
  517. //WSAStartup function initiates use of
  518. //Ws2_32.dll by a process
  519. dwErr = WSAStartup( wVersionRequested, &wsaData );
  520. if(dwErr!=0)
  521. {
  522. SetLastError(WSAGetLastError());
  523. DISPLAY_MESSAGE(stderr,GetResString(IDS_ID_SHOW_ERROR));
  524. ShowLastError(stderr);
  525. return FALSE;
  526. }
  527. // Initialize arrays...
  528. memset(pszServerName,0,MIN_MEMORY_REQUIRED);
  529. GetAsMultiByteString(pszServer,pszServerName,MIN_MEMORY_REQUIRED);
  530. if(IsValidIPAddress(pszServer))
  531. {
  532. unsigned long ulInetAddr = 0;//unsigned long is used as winsock
  533. // required this type. Variable
  534. // used
  535. // inet_addr function converts a string containing an
  536. //(Ipv4) Internet Protocol dotted address into a proper
  537. //address for the IN_ADDR structure.
  538. ulInetAddr = inet_addr( pszServerName );
  539. // gethostbyaddr function retrieves the host information
  540. //corresponding to a network address.
  541. hostEnt = gethostbyaddr( (char *) &ulInetAddr,
  542. sizeof( ulInetAddr ),
  543. PF_INET);
  544. if(hostEnt != NULL)
  545. {
  546. // As hostEnt->h_name is of type char so convert it
  547. // to unicode string
  548. GetAsUnicodeStringEx(hostEnt->h_name,pszServer,
  549. MAX_COMPUTERNAME_LENGTH);
  550. bReturnValue = GetHostNameFromFQDN( pszServer);
  551. }
  552. else
  553. {
  554. SetLastError(WSAGetLastError());
  555. DISPLAY_MESSAGE(stderr,GetResString(IDS_ID_SHOW_ERROR));
  556. ShowLastError(stderr);
  557. }
  558. }
  559. // Check validity for the server name
  560. else if (IsValidServer(pszServer))
  561. {
  562. hostEnt = gethostbyname(pszServerName);
  563. if(hostEnt != NULL)
  564. {
  565. // As hostEnt->h_name is of type char so convert it
  566. // to unicode string
  567. GetAsUnicodeStringEx(hostEnt->h_name,pszServer,
  568. MAX_COMPUTERNAME_LENGTH);
  569. bReturnValue = GetHostNameFromFQDN( pszServer);
  570. }
  571. else
  572. {
  573. SetLastError(WSAGetLastError());
  574. DISPLAY_MESSAGE(stderr,GetResString(IDS_ID_SHOW_ERROR));
  575. ShowLastError(stderr);
  576. }
  577. }
  578. else
  579. {
  580. // server name is incorrect, show error message.
  581. DISPLAY_MESSAGE(stderr,GetResString(IDS_ID_SHOW_ERROR ));
  582. DISPLAY_MESSAGE(stderr,GetResString(IDS_INVALID_SERVER_NAME));
  583. }
  584. //WSACleanup function terminates use of the Ws2_32.dll.
  585. WSACleanup( );
  586. SAFEDELETE(pszServerName);
  587. return bReturnValue;
  588. }
  589. // ***************************************************************************
  590. // Routine Description:
  591. // This routine gets Hostname from FQDN name.
  592. //
  593. // Arguments:
  594. // [in/out] pszServer = ServerName
  595. // Return Value:
  596. // TRUE : if server name retured successfully
  597. // FALSE: otherwise
  598. // ***************************************************************************
  599. BOOL
  600. GetHostNameFromFQDN(LPTSTR pszServer)
  601. {
  602. BOOL bReturnValue = FALSE;
  603. if(pszServer==NULL)
  604. return FALSE;
  605. LPTSTR pszTemp = new TCHAR[lstrlen(pszServer)+1];
  606. LPTSTR pszTempHeadPos;
  607. if(pszTemp==NULL)
  608. {
  609. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  610. DISPLAY_MESSAGE(stderr,GetResString(IDS_ID_SHOW_ERROR));
  611. ShowLastError(stderr);
  612. }
  613. else
  614. {
  615. // Following is done to chekc Fully Qualified Domain Name.
  616. // This will extract a string coming before "."
  617. // for example FQDN = akhil.wipro.com...
  618. // String of interest is akhil, which will be exracted
  619. // in following manner.
  620. pszTempHeadPos = pszTemp;
  621. pszTemp = _tcstok(pszServer,SINGLE_DOT);
  622. pszTemp = 0;// putting explicitly NULL character. at
  623. // the place where "." comes.
  624. bReturnValue = TRUE;
  625. }
  626. SAFEDELETE(pszTempHeadPos);
  627. return bReturnValue;
  628. }