GetMacAddress.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
  3. * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the Politecnico di Torino, CACE Technologies
  16. * nor the names of its contributors may be used to endorse or promote
  17. * products derived from this software without specific prior written
  18. * permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <stdio.h>
  34. #include <conio.h>
  35. #include <Packet32.h>
  36. #include <ntddndis.h>
  37. #define Max_Num_Adapter 10
  38. char AdapterList[Max_Num_Adapter][1024];
  39. #include <tchar.h>
  40. BOOL LoadNpcapDlls()
  41. {
  42. TCHAR npcap_dir[512];
  43. UINT len;
  44. len = GetSystemDirectory(npcap_dir, 480);
  45. if (!len) {
  46. fprintf(stderr, "Error in GetSystemDirectory: %x", GetLastError());
  47. return FALSE;
  48. }
  49. _tcscat_s(npcap_dir, 512, TEXT("\\Npcap"));
  50. if (SetDllDirectory(npcap_dir) == 0) {
  51. fprintf(stderr, "Error in SetDllDirectory: %x", GetLastError());
  52. return FALSE;
  53. }
  54. return TRUE;
  55. }
  56. int main()
  57. {
  58. LPADAPTER lpAdapter = 0;
  59. int i;
  60. DWORD dwErrorCode;
  61. char AdapterName[8192];
  62. char *temp,*temp1;
  63. int AdapterNum=0,Open;
  64. ULONG AdapterLength;
  65. PPACKET_OID_DATA OidData;
  66. BOOLEAN Status;
  67. /* Load Npcap and its functions. */
  68. if (!LoadNpcapDlls())
  69. {
  70. fprintf(stderr, "Couldn't load Npcap\n");
  71. exit(1);
  72. }
  73. //
  74. // Obtain the name of the adapters installed on this machine
  75. //
  76. printf("Packet.dll test application. Library version:%s\n", PacketGetVersion());
  77. printf("Adapters installed:\n");
  78. i=0;
  79. AdapterLength = sizeof(AdapterName);
  80. if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE){
  81. printf("Unable to retrieve the list of the adapters!\n");
  82. return -1;
  83. }
  84. temp=AdapterName;
  85. temp1=AdapterName;
  86. while ((*temp!='\0')||(*(temp-1)!='\0'))
  87. {
  88. if (*temp=='\0')
  89. {
  90. memcpy(AdapterList[i],temp1,temp-temp1);
  91. temp1=temp+1;
  92. i++;
  93. }
  94. temp++;
  95. }
  96. AdapterNum=i;
  97. for (i=0;i<AdapterNum;i++)
  98. printf("\n%d- %s\n",i+1,AdapterList[i]);
  99. printf("\n");
  100. do
  101. {
  102. printf("Select the number of the adapter to open : ");
  103. scanf_s("%d",&Open);
  104. if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum);
  105. } while (Open>AdapterNum);
  106. //
  107. // Open the selected adapter
  108. //
  109. lpAdapter = PacketOpenAdapter(AdapterList[Open-1]);
  110. if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
  111. {
  112. dwErrorCode=GetLastError();
  113. printf("Unable to open the adapter, Error Code : %lx\n",dwErrorCode);
  114. return -1;
  115. }
  116. //
  117. // Allocate a buffer to get the MAC adress
  118. //
  119. OidData = malloc(6 + sizeof(PACKET_OID_DATA));
  120. if (OidData == NULL)
  121. {
  122. printf("error allocating memory!\n");
  123. PacketCloseAdapter(lpAdapter);
  124. return -1;
  125. }
  126. //
  127. // Retrieve the adapter MAC querying the NIC driver
  128. //
  129. OidData->Oid = OID_802_3_CURRENT_ADDRESS;
  130. OidData->Length = 6;
  131. ZeroMemory(OidData->Data, 6);
  132. Status = PacketRequest(lpAdapter, FALSE, OidData);
  133. if(Status)
  134. {
  135. printf("The MAC address of the adapter is %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
  136. (OidData->Data)[0],
  137. (OidData->Data)[1],
  138. (OidData->Data)[2],
  139. (OidData->Data)[3],
  140. (OidData->Data)[4],
  141. (OidData->Data)[5]);
  142. }
  143. else
  144. {
  145. printf("error retrieving the MAC address of the adapter!\n");
  146. }
  147. free(OidData);
  148. PacketCloseAdapter(lpAdapter);
  149. return (0);
  150. }