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.

115 lines
2.8 KiB

  1. /*++
  2. Copyright (c) 2000, Microsoft Corporation
  3. Module Name:
  4. sendarp.c
  5. Abstract:
  6. The module implements a utility program to resolve an IP address to
  7. a hardware address using the SendARP() API routine.
  8. Author:
  9. Abolade Gbadegesin (aboladeg) 6-October-1999
  10. Revision History:
  11. --*/
  12. #include <windows.h>
  13. #include <winsock.h>
  14. #include <iphlpapi.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. MIB_IPNETROW IpNetRow;
  18. int __cdecl
  19. main(
  20. int argc,
  21. char* argv[]
  22. )
  23. {
  24. ULONG Error;
  25. UCHAR HardwareAddress[6];
  26. ULONG HardwareAddressLength;
  27. ULONG InterfaceIndex;
  28. ULONG Length;
  29. SOCKADDR_IN SockAddrIn;
  30. SOCKET Socket;
  31. ULONG SourceAddress;
  32. ULONG TargetAddress;
  33. HANDLE ThreadHandle;
  34. WSADATA wd;
  35. if (argc != 2) {
  36. printf("Usage: %s <IP address>\n", argv[0]);
  37. return 0;
  38. }
  39. WSAStartup(0x202, &wd);
  40. TargetAddress = inet_addr(argv[1]);
  41. //
  42. // Retrieve the best interface for the target IP address,
  43. // and also perform a UDP-connect to determine the 'closest'
  44. // local IP address to the target IP address.
  45. //
  46. Error = GetBestInterface(TargetAddress, &InterfaceIndex);
  47. if (Error != NO_ERROR) {
  48. printf("GetBestInterfaceFromStack: %d\n", Error);
  49. return 0;
  50. }
  51. Length = sizeof(SockAddrIn);
  52. SockAddrIn.sin_family = AF_INET;
  53. SockAddrIn.sin_port = 0;
  54. SockAddrIn.sin_addr.s_addr = TargetAddress;
  55. if ((Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))
  56. == INVALID_SOCKET ||
  57. connect(Socket, (PSOCKADDR)&SockAddrIn, sizeof(SockAddrIn))
  58. == SOCKET_ERROR ||
  59. getsockname(Socket, (PSOCKADDR)&SockAddrIn, &Length)
  60. == SOCKET_ERROR) {
  61. printf("socket/connect/getsockname: %d\n", WSAGetLastError());
  62. }
  63. SourceAddress = SockAddrIn.sin_addr.s_addr;
  64. //
  65. // Make sure the target IP address isn't already cached,
  66. // by removing it from the ARP cache if present using the interface index
  67. // determined above.
  68. //
  69. ZeroMemory(&IpNetRow, sizeof(IpNetRow));
  70. IpNetRow.dwIndex = InterfaceIndex;
  71. IpNetRow.dwPhysAddrLen = 6;
  72. IpNetRow.dwAddr = TargetAddress;
  73. IpNetRow.dwType = MIB_IPNET_TYPE_INVALID;
  74. DeleteIpNetEntry(&IpNetRow);
  75. HardwareAddressLength = sizeof(HardwareAddress);
  76. Error =
  77. SendARP(
  78. TargetAddress,
  79. SourceAddress,
  80. (PULONG)HardwareAddress,
  81. &HardwareAddressLength
  82. );
  83. if (Error) {
  84. printf("SendARP: %d\n", Error);
  85. } else {
  86. ULONG i;
  87. printf("%s\t", argv[1]);
  88. for (i = 0; i < HardwareAddressLength-1; i++) {
  89. printf("%02x-", HardwareAddress[i]);
  90. }
  91. printf("%02x\n", HardwareAddress[i]);
  92. }
  93. return 0;
  94. }