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.

1580 lines
34 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. common.c
  5. Abstract:
  6. This module contains miscellaneous utility routines used by the
  7. DHCP server service. Code is hacked from convert.c
  8. Author:
  9. Shubho Bhattacharya (a-sbhatt) 11/17/98
  10. Revision History:
  11. --*/
  12. #include <precomp.h>
  13. WCHAR *messages[] = {
  14. L"success",
  15. L"name not found",
  16. L"no response",
  17. L"out of memory",
  18. L"bad ip address",
  19. L"host not found",
  20. L"host address not verified",
  21. L"invalid argument",
  22. L"failed to open NBT driver"
  23. };
  24. SOCKET sd;
  25. WSADATA WsaData;
  26. u_long NonBlocking = 1;
  27. int NumWinServers=0;
  28. int NumNBNames=0;
  29. LPWSTR
  30. WinsAnsiToUnicode(
  31. IN LPCSTR Ansi,
  32. IN OUT LPWSTR Unicode
  33. )
  34. {
  35. ANSI_STRING AnsiString;
  36. UNICODE_STRING UnicodeString;
  37. RtlInitString( &AnsiString, Ansi );
  38. UnicodeString.MaximumLength =
  39. (USHORT)RtlAnsiStringToUnicodeSize( &AnsiString );
  40. if( Unicode == NULL ) {
  41. UnicodeString.Buffer =
  42. WinsAllocateMemory( UnicodeString.MaximumLength );
  43. }
  44. else {
  45. UnicodeString.Buffer = Unicode;
  46. }
  47. if ( UnicodeString.Buffer == NULL ) {
  48. return NULL;
  49. }
  50. if(!NT_SUCCESS( RtlAnsiStringToUnicodeString( &UnicodeString,
  51. &AnsiString,
  52. FALSE))){
  53. if( Unicode == NULL ) {
  54. WinsFreeMemory( UnicodeString.Buffer );
  55. UnicodeString.Buffer = NULL;
  56. }
  57. return NULL;
  58. }
  59. return UnicodeString.Buffer;
  60. }
  61. LPSTR
  62. WinsUnicodeToAnsi(
  63. IN LPCWSTR Unicode,
  64. IN OUT LPSTR Ansi
  65. )
  66. {
  67. ANSI_STRING AnsiString;
  68. UNICODE_STRING UnicodeString;
  69. RtlInitUnicodeString( &UnicodeString, Unicode );
  70. AnsiString.MaximumLength =
  71. (USHORT) RtlUnicodeStringToAnsiSize( &UnicodeString );
  72. if( Ansi == NULL )
  73. {
  74. AnsiString.Buffer = WinsAllocateMemory( AnsiString.MaximumLength );
  75. }
  76. else
  77. {
  78. AnsiString.Buffer = Ansi;
  79. }
  80. if ( AnsiString.Buffer == NULL )
  81. {
  82. return NULL;
  83. }
  84. if(!NT_SUCCESS( RtlUnicodeStringToAnsiString( &AnsiString,
  85. &UnicodeString,
  86. FALSE))){
  87. if( Ansi == NULL ) {
  88. WinsFreeMemory( AnsiString.Buffer );
  89. AnsiString.Buffer = NULL;
  90. }
  91. return NULL;
  92. }
  93. return AnsiString.Buffer;
  94. }
  95. LPSTR
  96. WinsAnsiToOem(
  97. IN LPCSTR Ansi
  98. )
  99. {
  100. LPSTR Oem = NULL;
  101. LPWSTR Unicode = NULL;
  102. Unicode = WinsAnsiToUnicode(Ansi, NULL);
  103. if( Unicode is NULL )
  104. return NULL;
  105. Oem = WinsUnicodeToOem(Unicode, NULL);
  106. WinsFreeMemory(Unicode);
  107. Unicode = NULL;
  108. return Oem;
  109. }
  110. LPWSTR
  111. WinsOemToUnicodeN(
  112. IN LPCSTR Ansi,
  113. IN OUT LPWSTR Unicode,
  114. IN USHORT cChars
  115. )
  116. {
  117. OEM_STRING AnsiString;
  118. UNICODE_STRING UnicodeString;
  119. RtlInitString( &AnsiString, Ansi );
  120. UnicodeString.MaximumLength =
  121. cChars;
  122. if( Unicode == NULL ) {
  123. UnicodeString.Buffer =
  124. WinsAllocateMemory( UnicodeString.MaximumLength );
  125. }
  126. else {
  127. UnicodeString.Buffer = Unicode;
  128. }
  129. if ( UnicodeString.Buffer == NULL ) {
  130. return NULL;
  131. }
  132. if(!NT_SUCCESS( RtlOemStringToUnicodeString( &UnicodeString,
  133. &AnsiString,
  134. FALSE))){
  135. if( Unicode == NULL ) {
  136. WinsFreeMemory( UnicodeString.Buffer );
  137. UnicodeString.Buffer = NULL;
  138. }
  139. return NULL;
  140. }
  141. return UnicodeString.Buffer;
  142. }
  143. LPWSTR
  144. WinsOemToUnicode(
  145. IN LPCSTR Ansi,
  146. IN OUT LPWSTR Unicode
  147. )
  148. {
  149. OEM_STRING AnsiString;
  150. RtlInitString( &AnsiString, Ansi );
  151. return WinsOemToUnicodeN(
  152. Ansi,
  153. Unicode,
  154. (USHORT) RtlOemStringToUnicodeSize( &AnsiString )
  155. );
  156. }
  157. /*++
  158. Routine Description:
  159. Convert an OEM (zero terminated) string to the corresponding UNICODE
  160. string.
  161. Arguments:
  162. Ansi - Specifies the ASCII zero terminated string to convert.
  163. Unicode - Specifies the pointer to the unicode buffer. If this
  164. pointer is NULL then this routine allocates buffer using
  165. DhcpAllocateMemory and returns. The caller should freeup this
  166. memory after use by calling DhcpFreeMemory.
  167. Return Value:
  168. NULL - There was some error in the conversion.
  169. Otherwise, it returns a pointer to the zero terminated UNICODE string in
  170. an allocated buffer. The buffer can be freed using DhcpFreeMemory.
  171. --*/
  172. LPSTR
  173. WinsUnicodeToOem(
  174. IN LPCWSTR Unicode,
  175. IN OUT LPSTR Ansi
  176. )
  177. /*++
  178. Routine Description:
  179. Convert an UNICODE (zero terminated) string to the corresponding OEM
  180. string.
  181. Arguments:
  182. Ansi - Specifies the UNICODE zero terminated string to convert.
  183. Ansi - Specifies the pointer to the oem buffer. If this
  184. pointer is NULL then this routine allocates buffer using
  185. DhcpAllocateMemory and returns. The caller should freeup this
  186. memory after use by calling DhcpFreeMemory.
  187. Return Value:
  188. NULL - There was some error in the conversion.
  189. Otherwise, it returns a pointer to the zero terminated OEM string in
  190. an allocated buffer. The buffer can be freed using DhcpFreeMemory.
  191. --*/
  192. {
  193. OEM_STRING AnsiString;
  194. UNICODE_STRING UnicodeString;
  195. RtlInitUnicodeString( &UnicodeString, Unicode );
  196. AnsiString.MaximumLength =
  197. (USHORT) RtlUnicodeStringToOemSize( &UnicodeString );
  198. if( Ansi == NULL )
  199. {
  200. AnsiString.Buffer = WinsAllocateMemory( AnsiString.MaximumLength );
  201. }
  202. else
  203. {
  204. AnsiString.Buffer = Ansi;
  205. }
  206. if ( AnsiString.Buffer == NULL )
  207. {
  208. return NULL;
  209. }
  210. if(!NT_SUCCESS( RtlUnicodeStringToOemString( &AnsiString,
  211. &UnicodeString,
  212. FALSE))){
  213. if( Ansi == NULL ) {
  214. WinsFreeMemory( AnsiString.Buffer );
  215. AnsiString.Buffer = NULL;
  216. }
  217. return NULL;
  218. }
  219. return AnsiString.Buffer;
  220. }
  221. VOID
  222. WinsHexToString(
  223. OUT LPWSTR Buffer,
  224. IN const BYTE * HexNumber,
  225. IN DWORD Length
  226. )
  227. /*++
  228. Routine Description:
  229. This functions converts are arbitrary length hex number to a Unicode
  230. string. The string is not NUL terminated.
  231. Arguments:
  232. Buffer - A pointer to a buffer for the resultant Unicode string.
  233. The buffer must be at least Length * 2 characters in size.
  234. HexNumber - The hex number to convert.
  235. Length - The length of HexNumber, in bytes.
  236. Return Value:
  237. None.
  238. --*/
  239. {
  240. DWORD i;
  241. int j;
  242. for (i = 0; i < Length * 2; i+=2 ) {
  243. j = *HexNumber & 0xF;
  244. if ( j <= 9 ) {
  245. Buffer[i+1] = j + L'0';
  246. } else {
  247. Buffer[i+1] = j + L'A' - 10;
  248. }
  249. j = *HexNumber >> 4;
  250. if ( j <= 9 ) {
  251. Buffer[i] = j + L'0';
  252. } else {
  253. Buffer[i] = j + L'A' - 10;
  254. }
  255. HexNumber++;
  256. }
  257. return;
  258. }
  259. VOID
  260. WinsHexToAscii(
  261. OUT LPSTR Buffer,
  262. IN const BYTE * HexNumber,
  263. IN DWORD Length
  264. )
  265. /*++
  266. Routine Description:
  267. This functions converts are arbitrary length hex number to an ASCII
  268. string. The string is not NUL terminated.
  269. Arguments:
  270. Buffer - A pointer to a buffer for the resultant Unicode string.
  271. The buffer must be at least Length * 2 characters in size.
  272. HexNumber - The hex number to convert.
  273. Length - The length of HexNumber, in bytes.
  274. Return Value:
  275. None.
  276. --*/
  277. {
  278. DWORD i;
  279. int j;
  280. for (i = 0; i < Length; i+=1 ) {
  281. j = *HexNumber & 0xF;
  282. if ( j <= 9 ) {
  283. Buffer[i+1] = j + '0';
  284. } else {
  285. Buffer[i+1] = j + 'A' - 10;
  286. }
  287. j = *HexNumber >> 4;
  288. if ( j <= 9 ) {
  289. Buffer[i] = j + '0';
  290. } else {
  291. Buffer[i] = j + 'A' - 10;
  292. }
  293. HexNumber++;
  294. }
  295. return;
  296. }
  297. VOID
  298. WinsDecimalToString(
  299. OUT LPWSTR Buffer,
  300. IN BYTE Number
  301. )
  302. /*++
  303. Routine Description:
  304. This functions converts a single byte decimal digit to a 3 character
  305. Unicode string. The string not NUL terminated.
  306. Arguments:
  307. Buffer - A pointer to a buffer for the resultant Unicode string.
  308. The buffer must be at least 3 characters in size.
  309. Number - The number to convert.
  310. Return Value:
  311. None.
  312. --*/
  313. {
  314. Buffer[2] = Number % 10 + L'0';
  315. Number /= 10;
  316. Buffer[1] = Number % 10 + L'0';
  317. Number /= 10;
  318. Buffer[0] = Number + L'0';
  319. return;
  320. }
  321. DWORD
  322. WinsDottedStringToIpAddress(
  323. IN LPCSTR String
  324. )
  325. /*++
  326. Routine Description:
  327. This functions converts a dotted decimal form ASCII string to a
  328. Host order IP address.
  329. Arguments:
  330. String - The address to convert.
  331. Return Value:
  332. The corresponding IP address.
  333. --*/
  334. {
  335. struct in_addr addr;
  336. addr.s_addr = inet_addr( String );
  337. return( ntohl(*(LPDWORD)&addr) );
  338. }
  339. LPSTR
  340. WinsIpAddressToDottedString(
  341. IN DWORD IpAddress
  342. )
  343. /*++
  344. Routine Description:
  345. This functions converts a Host order IP address to a dotted decimal
  346. form ASCII string.
  347. Arguments:
  348. IpAddress - Host order IP Address.
  349. Return Value:
  350. String for IP Address.
  351. --*/
  352. {
  353. DWORD NetworkOrderIpAddress;
  354. NetworkOrderIpAddress = htonl(IpAddress);
  355. return(inet_ntoa( *(struct in_addr *)&NetworkOrderIpAddress));
  356. }
  357. #if DBG
  358. VOID
  359. WinsAssertFailed(
  360. IN LPCSTR FailedAssertion,
  361. IN LPCSTR FileName,
  362. IN DWORD LineNumber,
  363. IN LPCSTR Message
  364. )
  365. /*++
  366. Routine Description:
  367. Assertion failed.
  368. Arguments:
  369. FailedAssertion :
  370. FileName :
  371. LineNumber :
  372. Message :
  373. Return Value:
  374. none.
  375. --*/
  376. {
  377. #ifndef DHCP_NOASSERT
  378. RtlAssert(
  379. (LPVOID)FailedAssertion,
  380. (LPVOID)FileName,
  381. (ULONG) LineNumber,
  382. (PCHAR) Message);
  383. #endif
  384. WinsPrint(( 0, "Assert @ %s \n", FailedAssertion ));
  385. WinsPrint(( 0, "Assert Filename, %s \n", FileName ));
  386. WinsPrint(( 0, "Line Num. = %ld.\n", LineNumber ));
  387. WinsPrint(( 0, "Message is %s\n", Message ));
  388. }
  389. VOID
  390. WinsPrintRoutine(
  391. IN DWORD DebugFlag,
  392. IN LPCSTR Format,
  393. ...
  394. )
  395. {
  396. #define WSTRSIZE( wsz ) ( ( wcslen( wsz ) + 1 ) * sizeof( WCHAR ) )
  397. #define MAX_PRINTF_LEN 1024 // Arbitrary.
  398. va_list arglist;
  399. char OutputBuffer[MAX_PRINTF_LEN];
  400. ULONG length = 0;
  401. //
  402. // Put a the information requested by the caller onto the line
  403. //
  404. va_start(arglist, Format);
  405. length += (ULONG) vsprintf(&OutputBuffer[length], Format, arglist);
  406. va_end(arglist);
  407. #if DBG
  408. WinsAssert(length <= MAX_PRINTF_LEN);
  409. #endif //DBG
  410. //
  411. // Output to the debug terminal,
  412. //
  413. DbgPrint( "%s", OutputBuffer);
  414. }
  415. #endif // DBG
  416. DWORD
  417. CreateDumpFile(
  418. IN LPCWSTR pwszName,
  419. OUT PHANDLE phFile
  420. )
  421. {
  422. HANDLE hFile;
  423. *phFile = NULL;
  424. hFile = CreateFileW(pwszName,
  425. GENERIC_WRITE,
  426. FILE_SHARE_READ | FILE_SHARE_DELETE,
  427. NULL,
  428. OPEN_ALWAYS,
  429. FILE_ATTRIBUTE_NORMAL,
  430. NULL);
  431. if(hFile == INVALID_HANDLE_VALUE)
  432. return GetLastError();
  433. *phFile = hFile;
  434. return NO_ERROR;
  435. }
  436. VOID
  437. CloseDumpFile(
  438. HANDLE hFile
  439. )
  440. {
  441. if( hFile )
  442. CloseHandle(hFile);
  443. }
  444. DWORD
  445. WinsDottedStringToIpAddressW(
  446. IN LPCWSTR pwszString
  447. )
  448. {
  449. DWORD dwStrlen = 0;
  450. DWORD dwLen = 0;
  451. DWORD dwRes = INADDR_NONE;
  452. LPSTR pszString = NULL;
  453. if( pwszString == NULL )
  454. return dwRes;
  455. pszString = WinsUnicodeToOem(pwszString, NULL);
  456. if( pszString )
  457. {
  458. dwRes = WinsDottedStringToIpAddress(pszString);
  459. WinsFreeMemory(pszString);
  460. pszString = NULL;
  461. }
  462. return dwRes;
  463. }
  464. LPWSTR
  465. WinsIpAddressToDottedStringW(
  466. DWORD IpAddress
  467. )
  468. {
  469. DWORD dwStrlen = 0;
  470. DWORD dwLen = 0;
  471. DWORD dwRes = 0;
  472. LPWSTR pwszString = NULL;
  473. LPSTR pszString = NULL;
  474. pszString = WinsIpAddressToDottedString(IpAddress);
  475. pwszString = WinsOemToUnicode(pszString, NULL);
  476. return pwszString;
  477. }
  478. BOOL
  479. IsIpAddress(
  480. IN LPCWSTR pwszAddress
  481. )
  482. {
  483. LPSTR pszAdd = NULL;
  484. LPSTR pszTemp = NULL;
  485. if( IsBadStringPtr(pwszAddress, MAX_IP_STRING_LEN+1) is TRUE )
  486. return FALSE;
  487. if( wcslen(pwszAddress) < 3 )
  488. return FALSE;
  489. if( wcslen(pwszAddress) > 16 )
  490. return FALSE;
  491. pszAdd = WinsUnicodeToOem(pwszAddress, NULL);
  492. if( pszAdd is NULL )
  493. {
  494. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  495. return FALSE;
  496. }
  497. pszTemp = strtok(pszAdd, ".");
  498. while(pszTemp isnot NULL )
  499. {
  500. DWORD i=0;
  501. for(i=0; i<strlen(pszTemp); i++)
  502. {
  503. if( tolower(pszTemp[i]) < L'0' or
  504. tolower(pszTemp[i]) > L'9' )
  505. return FALSE;
  506. }
  507. if( atol(pszTemp) < 0 or
  508. atol(pszTemp) > 255 )
  509. {
  510. return FALSE;
  511. }
  512. pszTemp = strtok(NULL, ".");
  513. }
  514. if( INADDR_NONE is inet_addr(pszAdd) )
  515. {
  516. WinsFreeMemory(pszAdd);
  517. pszAdd = NULL;
  518. return FALSE;
  519. }
  520. else
  521. {
  522. WinsFreeMemory(pszAdd);
  523. pszAdd = NULL;
  524. return TRUE;
  525. }
  526. }
  527. BOOL
  528. IsValidServer(
  529. IN LPCWSTR pwszServer
  530. )
  531. {
  532. struct hostent * lpHostEnt = NULL;
  533. DWORD dwIpAddress = 0;
  534. LPSTR pszServer = NULL;
  535. if( pwszServer is NULL )
  536. return FALSE;
  537. pszServer = WinsUnicodeToAnsi(pwszServer, NULL);
  538. if( pszServer is NULL )
  539. {
  540. DisplayMessage(g_hModule,
  541. EMSG_WINS_OUT_OF_MEMORY);
  542. return FALSE;
  543. }
  544. //Is it an IPAddress?
  545. if( IsIpAddress(pwszServer) )
  546. {
  547. dwIpAddress = inet_addr(pszServer);
  548. lpHostEnt = gethostbyaddr((char *)&dwIpAddress, 4, AF_INET);
  549. }
  550. else if( wcslen(pwszServer) > 2 and
  551. _wcsnicmp(pwszServer, L"\\\\", 2) is 0 )
  552. {
  553. lpHostEnt = gethostbyname(pszServer+2);
  554. }
  555. WinsFreeMemory(pszServer);
  556. return (lpHostEnt != NULL);
  557. }
  558. LPWSTR
  559. MakeTimeString(
  560. DWORD dwTime
  561. )
  562. {
  563. LPWSTR pwszTime = NULL;
  564. WCHAR wcHr[3] = {L'\0'},
  565. wcMt[3] = {L'\0'},
  566. wcSc[3] = {L'\0'};
  567. DWORD dwHr = 0,
  568. dwMt = 0,
  569. dwSc = 0,
  570. dw = 0;
  571. pwszTime = WinsAllocateMemory(9*sizeof(WCHAR));
  572. if( pwszTime )
  573. {
  574. for( dw=0; dw<8; dw++ )
  575. pwszTime[dw] = L'0';
  576. pwszTime[2] = pwszTime[5] = L':';
  577. dwHr = dwTime/(60*60);
  578. dwTime = dwTime - dwHr*60*60;
  579. dwMt = dwTime/60;
  580. dwTime = dwTime - dwMt*60;
  581. dwSc = dwTime;
  582. _itow((int)dwHr, wcHr, 10);
  583. _itow((int)dwMt, wcMt, 10);
  584. _itow((int)dwSc, wcSc, 10);
  585. if( dwHr isnot 0 )
  586. {
  587. wcsncpy(pwszTime+2-wcslen(wcHr), wcHr, wcslen(wcHr));
  588. }
  589. if( dwMt isnot 0 )
  590. {
  591. wcsncpy(pwszTime+5-wcslen(wcMt), wcMt, wcslen(wcMt));
  592. }
  593. wcsncpy(pwszTime+8-wcslen(wcSc), wcSc, wcslen(wcSc));
  594. }
  595. return pwszTime;
  596. }
  597. LPWSTR
  598. MakeDayTimeString(
  599. DWORD dwTime
  600. )
  601. {
  602. LPWSTR pwszTime = NULL;
  603. WCHAR wcDay[4] = {L'\0'},
  604. wcHr[3] = {L'\0'},
  605. wcMt[3] = {L'\0'};
  606. DWORD dwDay = 0,
  607. dwHr = 0,
  608. dwMt = 0,
  609. dw = 0;
  610. pwszTime = WinsAllocateMemory(10*sizeof(WCHAR));
  611. if( pwszTime )
  612. {
  613. for( dw=0; dw < 10; dw++ )
  614. pwszTime[dw] = L'0';
  615. pwszTime[3] = L':';
  616. pwszTime[6] = L':';
  617. pwszTime[9] = L'\0';
  618. dwDay = dwTime/(24*60*60);
  619. dwTime = dwTime - dwDay*24*60*60;
  620. dwHr = dwTime/(60*60);
  621. dwTime = dwTime - dwHr*60*60;
  622. dwMt = dwTime/60;
  623. dwTime = dwTime - dwMt*60;
  624. _itow(dwDay, wcDay,10);
  625. _itow(dwHr, wcHr, 10);
  626. _itow(dwMt, wcMt, 10);
  627. if( dwDay isnot 0 )
  628. {
  629. wcsncpy(pwszTime+3-wcslen(wcDay), wcDay, wcslen(wcDay));
  630. }
  631. if( dwHr isnot 0 )
  632. {
  633. wcsncpy(pwszTime+6-wcslen(wcHr), wcHr, wcslen(wcHr));
  634. }
  635. if( dwMt isnot 0 )
  636. {
  637. wcsncpy(pwszTime+9-wcslen(wcMt), wcMt, wcslen(wcMt));
  638. }
  639. }
  640. return pwszTime;
  641. }
  642. DWORD
  643. ImportStaticMappingsFile(LPWSTR strFile,
  644. BOOL fDelete
  645. )
  646. {
  647. DWORD Status = NO_ERROR;
  648. WCHAR ws[256];
  649. Status = WinsDoStaticInit(g_hBind,
  650. strFile,
  651. fDelete);
  652. return Status;
  653. }
  654. BOOL
  655. IsLocalServer(VOID)
  656. {
  657. BOOL fReturn = TRUE;
  658. WCHAR wcName[MAX_COMPUTER_NAME_LEN+1] = {L'\0'};
  659. DWORD dwLen = MAX_COMPUTER_NAME_LEN;
  660. fReturn = GetComputerNameEx(ComputerNameNetBIOS,
  661. wcName,
  662. &dwLen);
  663. if( fReturn is TRUE )
  664. {
  665. if( _wcsnicmp(wcName, g_ServerNameUnicode, dwLen ) is 0 )
  666. {
  667. return TRUE;
  668. }
  669. else
  670. return FALSE;
  671. }
  672. else
  673. {
  674. return FALSE;
  675. }
  676. }
  677. BOOL
  678. IsPureNumeric(IN LPCWSTR pwszStr)
  679. {
  680. DWORD dwLen = 0,
  681. i;
  682. if( pwszStr is NULL )
  683. return FALSE;
  684. dwLen = wcslen(pwszStr);
  685. for(i=0; i<dwLen; i++ )
  686. {
  687. if( pwszStr[i] >= L'0' and
  688. pwszStr[i] <= L'9' )
  689. {
  690. continue;
  691. }
  692. else
  693. return FALSE;
  694. }
  695. return TRUE;
  696. }
  697. DWORD
  698. DisplayErrorMessage(
  699. DWORD dwMsgID,
  700. DWORD dwErrID,
  701. ...
  702. )
  703. {
  704. LPWSTR pwszErrorMsg = NULL;
  705. WCHAR rgwcInput[MAX_MSG_LENGTH + 1] = {L'\0'};
  706. DWORD dwMsgLen = 0;
  707. HANDLE hWinsEvnt = NULL;
  708. va_list arglist;
  709. va_start(arglist, dwErrID);
  710. switch(dwErrID)
  711. {
  712. case ERROR_INVALID_PARAMETER:
  713. {
  714. DisplayMessage(g_hModule, EMSG_WINS_INVALID_PARAMETER);
  715. return dwErrID;
  716. }
  717. case ERROR_NOT_ENOUGH_MEMORY:
  718. {
  719. DisplayMessage(g_hModule, EMSG_WINS_OUT_OF_MEMORY);
  720. return dwErrID;
  721. }
  722. case ERROR_NO_MORE_ITEMS:
  723. {
  724. DisplayMessage(g_hModule, EMSG_WINS_NO_MORE_ITEMS);
  725. return dwErrID;
  726. }
  727. case ERROR_MORE_DATA:
  728. {
  729. DisplayMessage(g_hModule, EMSG_WINS_MORE_DATA);
  730. return dwErrID;
  731. }
  732. case ERROR_ACCESS_DENIED:
  733. {
  734. DisplayMessage(g_hModule, EMSG_WINS_ACCESS_DENIED);
  735. return dwErrID;
  736. }
  737. case ERROR_INVALID_DB_VERSION:
  738. {
  739. DisplayMessage(g_hModule, EMSG_INVALID_DB_VERSION);
  740. return dwErrID;
  741. }
  742. case ERROR_INVALID_IPADDRESS:
  743. {
  744. DisplayMessage(g_hModule, EMSG_INVALID_IPADDRESS);
  745. return dwErrID;
  746. }
  747. case ERROR_INVALID_PARTNER_NAME:
  748. {
  749. DisplayMessage(g_hModule, EMSG_INVALID_PARTNER_NAME);
  750. return dwErrID;
  751. }
  752. case ERROR_NO_PARTNER_EXIST:
  753. {
  754. DisplayMessage(g_hModule, EMSG_NO_PARTNER_EXIST);
  755. return dwErrID;
  756. }
  757. case ERROR_WINS_BIND_FAILED:
  758. {
  759. DisplayMessage(g_hModule, EMSG_WINS_BIND_FAILED, arglist);
  760. return dwErrID;
  761. }
  762. case ERROR_INVALID_PARAMETER_SPECIFICATION:
  763. {
  764. DisplayMessage(g_hModule, EMSG_INVALID_PARAMETER_SPECIFICATION);
  765. return dwErrID;
  766. }
  767. default:
  768. break;
  769. }
  770. //Is it Wins specific message ?
  771. hWinsEvnt = LoadLibrary(TEXT("WinsEvnt.dll"));
  772. if( hWinsEvnt is NULL )
  773. {
  774. DisplayMessage(g_hModule, MSG_DLL_LOAD_FAILED, TEXT("WinsEvnt.dll"));
  775. goto System;
  776. }
  777. if( !LoadStringW(hWinsEvnt,
  778. dwErrID,
  779. rgwcInput,
  780. MAX_MSG_LENGTH) )
  781. {
  782. goto System;
  783. }
  784. dwMsgLen = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_STRING,
  785. rgwcInput,
  786. 0,
  787. 0L, // Default country ID.
  788. (LPWSTR)&pwszErrorMsg,
  789. 0,
  790. NULL);
  791. if( dwMsgLen isnot 0)
  792. {
  793. DisplayMessage(g_hModule, dwMsgID, pwszErrorMsg);
  794. goto Cleanup;
  795. }
  796. //Might be a system error returned by GetLastError();
  797. System:
  798. dwMsgLen = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  799. NULL,
  800. dwErrID,
  801. 0L,
  802. (LPWSTR)&pwszErrorMsg,
  803. 0,
  804. NULL);
  805. if( dwMsgLen isnot 0)
  806. {
  807. DisplayMessage(g_hModule, dwMsgID, pwszErrorMsg);
  808. goto Cleanup;
  809. }
  810. _itow(dwErrID, rgwcInput, 10);
  811. DisplayMessage(g_hModule, dwMsgID, rgwcInput);
  812. Cleanup:
  813. if (hWinsEvnt)
  814. FreeLibrary(hWinsEvnt);
  815. if (pwszErrorMsg)
  816. LocalFree(pwszErrorMsg);
  817. return dwMsgLen;
  818. }
  819. /*---------------------------------------------------------------------------
  820. ControlWINSService(LPCTSTR pszName, BOOL bStop)
  821. Stops ot starts the WINS service on the local machine
  822. //Code hacked from the WINS MMC code
  823. ---------------------------------------------------------------------------*/
  824. DWORD
  825. ControlWINSService(BOOL bStop)
  826. {
  827. DWORD dwState = bStop ? SERVICE_STOPPED : SERVICE_RUNNING;
  828. DWORD dwPending = bStop ? SERVICE_STOP_PENDING : SERVICE_START_PENDING;
  829. DWORD err = ERROR_SUCCESS;
  830. int i;
  831. SERVICE_STATUS ss;
  832. DWORD dwControl;
  833. BOOL fSuccess;
  834. SC_HANDLE hService = NULL;
  835. SC_HANDLE hScManager = NULL;
  836. // oepmnt he service control manager
  837. hScManager = OpenSCManager(g_ServerNameUnicode, NULL, SC_MANAGER_ALL_ACCESS);
  838. if (hScManager == NULL)
  839. {
  840. err = GetLastError();
  841. goto Error;
  842. }
  843. // get the handle to the WINS service
  844. hService = OpenService(hScManager, _T("WINS"), SERVICE_ALL_ACCESS);
  845. if (hService == NULL)
  846. {
  847. err = GetLastError();
  848. goto Error;
  849. }
  850. // if stop requested
  851. if (bStop)
  852. {
  853. dwControl = SERVICE_CONTROL_STOP;
  854. fSuccess = ControlService(hService, dwControl, &ss);
  855. if (!fSuccess)
  856. {
  857. err = GetLastError();
  858. goto Error;
  859. }
  860. }
  861. // otherwise start the service
  862. else
  863. {
  864. fSuccess = StartService(hService, 0, NULL);
  865. if (!fSuccess)
  866. {
  867. err = GetLastError();
  868. goto Error;
  869. }
  870. }
  871. #define LOOP_TIME 5000
  872. #define NUM_LOOPS 600
  873. // wait for the service to start/stop.
  874. for (i = 0; i < NUM_LOOPS; i++)
  875. {
  876. QueryServiceStatus(hService, &ss);
  877. // check to see if we are done6
  878. if (ss.dwCurrentState == dwState)
  879. {
  880. int time = LOOP_TIME * i;
  881. DisplayMessage(g_hModule, MSG_WINS_SERVICE_TIME, time);
  882. break;
  883. }
  884. // now see if something bad happened
  885. if (ss.dwCurrentState != dwPending)
  886. {
  887. int time = LOOP_TIME * i;
  888. DisplayMessage(g_hModule, MSG_WINS_SERVICE_TIME, time);
  889. break;
  890. }
  891. Sleep(LOOP_TIME);
  892. }
  893. if (i == NUM_LOOPS)
  894. DisplayMessage(g_hModule, EMSG_WINS_SERVICE_FAILED);
  895. if (ss.dwCurrentState != dwState)
  896. err = ERROR_SERVICE_REQUEST_TIMEOUT;
  897. Error:
  898. // close the respective handles
  899. if (hService)
  900. CloseServiceHandle(hService);
  901. if (hScManager)
  902. CloseServiceHandle(hScManager);
  903. return err;
  904. }
  905. VOID
  906. OEMprintf(
  907. FILE * pFile,
  908. IN PWCHAR pwszUnicode
  909. )
  910. {
  911. PCHAR achOem;
  912. DWORD dwLen;
  913. dwLen = WideCharToMultiByte( CP_OEMCP,
  914. 0,
  915. pwszUnicode,
  916. -1,
  917. NULL,
  918. 0,
  919. NULL,
  920. NULL );
  921. achOem = malloc(dwLen);
  922. if (achOem)
  923. {
  924. WideCharToMultiByte( CP_OEMCP,
  925. 0,
  926. pwszUnicode,
  927. -1,
  928. achOem,
  929. dwLen,
  930. NULL,
  931. NULL );
  932. fprintf( stdout, "%hs", achOem );
  933. free(achOem);
  934. }
  935. }
  936. VOID
  937. MyDisplayMessage(
  938. IN FILE *pFile,
  939. IN PWCHAR pwszFormat,
  940. IN va_list *parglist
  941. )
  942. {
  943. DWORD dwMsgLen = 0;
  944. PWCHAR pwszOutput = NULL;
  945. LPSTR pszOutput = NULL;
  946. do
  947. {
  948. dwMsgLen = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER
  949. |FORMAT_MESSAGE_FROM_STRING,
  950. pwszFormat,
  951. 0,
  952. 0L, // Default country ID.
  953. (LPWSTR)&pwszOutput,
  954. 0,
  955. parglist);
  956. if(dwMsgLen == 0)
  957. {
  958. fwprintf( pFile, L"Error %d in FormatMessageW()\n", GetLastError());
  959. ASSERT(pwszOutput == NULL);
  960. break;
  961. }
  962. pszOutput = WinsUnicodeToOem(pwszOutput, NULL );
  963. if( pszOutput is NULL )
  964. {
  965. fwprintf(pFile, pwszOutput);
  966. }
  967. else
  968. {
  969. fprintf(pFile, pszOutput);
  970. WinsFreeMemory(pszOutput);
  971. pszOutput = NULL;
  972. }
  973. } while ( FALSE );
  974. if ( pwszOutput) { LocalFree( pwszOutput ); }
  975. return;
  976. }
  977. VOID
  978. DumpMessage(
  979. HANDLE hModule,
  980. FILE * pFile,
  981. DWORD dwMsgId,
  982. ...
  983. )
  984. {
  985. DWORD dwMsgLen = 0;
  986. PWCHAR pwszOutput = NULL;
  987. WCHAR rgwcInput[MAX_MSG_LENGTH + 1];
  988. va_list arglist;
  989. if ( !LoadStringW(hModule,
  990. dwMsgId,
  991. rgwcInput,
  992. MAX_MSG_LENGTH) )
  993. {
  994. return;
  995. }
  996. va_start(arglist, dwMsgId);
  997. MyDisplayMessage(pFile,
  998. rgwcInput,
  999. &arglist);
  1000. return;
  1001. }
  1002. VOID
  1003. TimeToFileTime(time_t time,
  1004. LPFILETIME pftTime)
  1005. {
  1006. LONGLONG longval = (LONGLONG)0;
  1007. longval = Int32x32To64(time, 10000000) + 116444736000000000;
  1008. pftTime->dwLowDateTime = (DWORD)longval;
  1009. longval = (longval & 0xFFFFFFFF00000000);
  1010. longval >>= 32;
  1011. pftTime->dwHighDateTime = (DWORD)longval;
  1012. }
  1013. DWORD
  1014. GetDateTimeInfo(LCTYPE lcType,
  1015. LPSYSTEMTIME lpSystemTime,
  1016. LPWSTR pwszBuffer,
  1017. DWORD *pdwBufferLen)
  1018. {
  1019. DWORD dwError = NO_ERROR;
  1020. BOOL fQueryLen = FALSE;
  1021. int cchFormat = 0,
  1022. cchData = 0;
  1023. PVOID pfnPtr = NULL;
  1024. DWORD dwBuff = 0,
  1025. dwInputBuff = 0;
  1026. LPWSTR pwszFormat = NULL,
  1027. pwszData = NULL;
  1028. if( pdwBufferLen is NULL )
  1029. {
  1030. return ERROR_INVALID_PARAMETER;
  1031. }
  1032. dwInputBuff = *pdwBufferLen;
  1033. *pdwBufferLen = 0;
  1034. if( pwszBuffer is NULL or
  1035. dwInputBuff is 0 )
  1036. {
  1037. fQueryLen = TRUE;
  1038. }
  1039. cchFormat = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT,
  1040. lcType,
  1041. NULL,
  1042. 0);
  1043. if( cchFormat is 0 )
  1044. {
  1045. dwError = GetLastError();
  1046. goto RETURN;
  1047. }
  1048. pwszFormat = WinsAllocateMemory(cchFormat*sizeof(WCHAR));
  1049. if( pwszFormat is NULL )
  1050. {
  1051. dwError = ERROR_NOT_ENOUGH_MEMORY;
  1052. goto RETURN;
  1053. }
  1054. cchFormat = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT,
  1055. lcType,
  1056. pwszFormat,
  1057. cchFormat);
  1058. if( cchFormat is 0 )
  1059. {
  1060. dwError = GetLastError();
  1061. goto RETURN;
  1062. }
  1063. if( lcType isnot LOCALE_STIMEFORMAT )
  1064. {
  1065. cchData = GetDateFormat(LOCALE_SYSTEM_DEFAULT,
  1066. 0,
  1067. lpSystemTime,
  1068. pwszFormat,
  1069. NULL,
  1070. 0);
  1071. if( cchData is 0 )
  1072. {
  1073. dwError = GetLastError();
  1074. goto RETURN;
  1075. }
  1076. if( fQueryLen is FALSE )
  1077. {
  1078. if( cchData > (int)dwInputBuff )
  1079. {
  1080. dwError = ERROR_INSUFFICIENT_BUFFER;
  1081. goto RETURN;
  1082. }
  1083. cchData = GetDateFormat(LOCALE_SYSTEM_DEFAULT,
  1084. 0,
  1085. lpSystemTime,
  1086. pwszFormat,
  1087. pwszBuffer,
  1088. (int)dwInputBuff);
  1089. if( cchData is 0 )
  1090. {
  1091. dwError = GetLastError();
  1092. goto RETURN;
  1093. }
  1094. }
  1095. }
  1096. else
  1097. {
  1098. cchData = GetTimeFormat(LOCALE_SYSTEM_DEFAULT,
  1099. 0,
  1100. lpSystemTime,
  1101. pwszFormat,
  1102. NULL,
  1103. 0);
  1104. if( cchData is 0 )
  1105. {
  1106. dwError = GetLastError();
  1107. goto RETURN;
  1108. }
  1109. if( fQueryLen is FALSE )
  1110. {
  1111. if( cchData > (int)dwInputBuff )
  1112. {
  1113. dwError = ERROR_INSUFFICIENT_BUFFER;
  1114. goto RETURN;
  1115. }
  1116. cchData = GetTimeFormat(LOCALE_SYSTEM_DEFAULT,
  1117. 0,
  1118. lpSystemTime,
  1119. pwszFormat,
  1120. pwszBuffer,
  1121. (int)dwInputBuff);
  1122. if( cchData is 0 )
  1123. {
  1124. dwError = GetLastError();
  1125. goto RETURN;
  1126. }
  1127. }
  1128. }
  1129. dwBuff += cchData;
  1130. *pdwBufferLen = dwBuff;
  1131. RETURN:
  1132. if( pwszFormat )
  1133. {
  1134. WinsFreeMemory(pwszFormat);
  1135. pwszFormat = NULL;
  1136. }
  1137. return dwError;
  1138. }
  1139. DWORD
  1140. FormatDateTimeString( time_t time,
  1141. BOOL fShort,
  1142. LPWSTR pwszBuffer,
  1143. DWORD *pdwBuffLen)
  1144. {
  1145. BOOL fQueryLen = FALSE;
  1146. DWORD dwError = NO_ERROR,
  1147. dwBufferLen = 0;
  1148. DWORD dwBuff = 0,
  1149. dwInputBuff = 0;
  1150. FILETIME ftTime = {0},
  1151. ftLocalTime = {0};
  1152. SYSTEMTIME stTime = {0};
  1153. if( pdwBuffLen is NULL )
  1154. {
  1155. return ERROR_INVALID_PARAMETER;
  1156. }
  1157. dwInputBuff = *pdwBuffLen;
  1158. if( pwszBuffer is NULL or
  1159. dwInputBuff is 0 )
  1160. {
  1161. fQueryLen = TRUE;
  1162. }
  1163. TimeToFileTime(time, &ftTime);
  1164. if( !FileTimeToLocalFileTime(&ftTime, &ftLocalTime) )
  1165. {
  1166. dwError = GetLastError();
  1167. goto RETURN;
  1168. }
  1169. if( !FileTimeToSystemTime(&ftLocalTime, &stTime) )
  1170. {
  1171. dwError = GetLastError();
  1172. goto RETURN;
  1173. }
  1174. if( fQueryLen is TRUE )
  1175. {
  1176. dwError = GetDateTimeInfo(fShort ? LOCALE_SSHORTDATE : LOCALE_SLONGDATE,
  1177. &stTime,
  1178. NULL,
  1179. &dwBuff);
  1180. if( dwError isnot NO_ERROR )
  1181. goto RETURN;
  1182. }
  1183. else
  1184. {
  1185. dwBuff = dwInputBuff;
  1186. dwError = GetDateTimeInfo(fShort ? LOCALE_SSHORTDATE : LOCALE_SLONGDATE,
  1187. &stTime,
  1188. pwszBuffer,
  1189. &dwBuff);
  1190. if( dwError isnot NO_ERROR )
  1191. goto RETURN;
  1192. }
  1193. dwBufferLen += dwBuff;
  1194. //Increment to add a space between date and time
  1195. dwBufferLen ++;
  1196. if( fQueryLen is TRUE )
  1197. {
  1198. dwBuff = 0;
  1199. dwError = GetDateTimeInfo(LOCALE_STIMEFORMAT,
  1200. &stTime,
  1201. NULL,
  1202. &dwBuff);
  1203. if( dwError isnot NO_ERROR )
  1204. goto RETURN;
  1205. }
  1206. else
  1207. {
  1208. if( dwBufferLen > dwInputBuff )
  1209. {
  1210. dwError = ERROR_INSUFFICIENT_BUFFER;
  1211. goto RETURN;
  1212. }
  1213. wcscat( pwszBuffer, L" ");
  1214. dwBuff = dwInputBuff - dwBufferLen;
  1215. dwError = GetDateTimeInfo(LOCALE_STIMEFORMAT,
  1216. &stTime,
  1217. pwszBuffer + dwBufferLen - 1,
  1218. &dwBuff);
  1219. if( dwError isnot NO_ERROR )
  1220. goto RETURN;
  1221. }
  1222. dwBufferLen += dwBuff;
  1223. *pdwBuffLen = dwBufferLen;
  1224. RETURN:
  1225. return dwError;
  1226. }