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.

113 lines
2.0 KiB

  1. /*++
  2. Copyright (C) 1999 Microsoft Corporation
  3. Module Name:
  4. isdhcp.c
  5. Abstract:
  6. test program to see if a DHCP server is around or not.
  7. Environment:
  8. Win2K+
  9. --*/
  10. #include <nt.h>
  11. #include <ntrtl.h>
  12. #include <nturtl.h>
  13. #include <windows.h>
  14. #include <dhcpcapi.h>
  15. #include <iprtrmib.h>
  16. #include <iphlpapi.h>
  17. #include <stdio.h>
  18. #include <winsock2.h>
  19. BOOL
  20. IsDHCPAvailableOnInterface(DWORD ipaddress)
  21. /*++
  22. Routine Description:
  23. This routine attempts to check if a dhcp
  24. server is around by trying to get a dhcp lease.
  25. If that fails, then it assume that no dhcp server
  26. is around.
  27. Return Values:
  28. TRUE -- DHCP server is around
  29. FALSE -- DHCP server not around
  30. In case of internal failures, it will return FALSE
  31. --*/
  32. {
  33. DWORD Error = 0;
  34. DHCP_CLIENT_UID DhcpClientUID =
  35. {
  36. (BYTE*)"ISDHCP",
  37. 6
  38. };
  39. DHCP_OPTION_LIST DummyOptList;
  40. LPDHCP_LEASE_INFO LeaseInfo = 0;
  41. LPDHCP_OPTION_INFO DummyOptionInfo = 0;
  42. BOOL found = FALSE;
  43. if( ipaddress == INADDR_ANY ||
  44. ipaddress == INADDR_LOOPBACK ||
  45. ipaddress == 0x0100007f)
  46. {
  47. //
  48. // oops. not a usable address
  49. //
  50. return FALSE;
  51. }
  52. LeaseInfo = NULL;
  53. Error = DhcpLeaseIpAddress(
  54. RtlUlongByteSwap(ipaddress),
  55. &DhcpClientUID,
  56. 0,
  57. &DummyOptList,
  58. &LeaseInfo,
  59. &DummyOptionInfo);
  60. if( NO_ERROR != Error )
  61. {
  62. //
  63. // lease request failed.
  64. //
  65. if( ERROR_ACCESS_DENIED == Error )
  66. {
  67. //
  68. // We only get access denied if the dhcp server
  69. // is around to NAK it. So we have found a dhcp
  70. // server
  71. //
  72. found = TRUE;
  73. }
  74. return found;
  75. }
  76. if( LeaseInfo->DhcpServerAddress != INADDR_ANY &&
  77. LeaseInfo->DhcpServerAddress != INADDR_NONE )
  78. {
  79. //
  80. // Valid address, so dhcp is there.
  81. //
  82. DhcpReleaseIpAddressLease(
  83. RtlUlongByteSwap(ipaddress),
  84. LeaseInfo);
  85. found = TRUE;
  86. }
  87. return found;
  88. }