inoutmac.h
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:4k
源码类别:

模拟服务器

开发平台:

C/C++

  1. // -------------------------------------------------------------------------
  2. // 文件名 : inoutmac.h
  3. // 创建者 : 谢茂培 (Hsie)
  4. // 创建时间 : 2003-07-20 14:47:18
  5. // 功能描述 :
  6. //
  7. // -------------------------------------------------------------------------
  8. #ifndef __INOUTMAC_H__
  9. #define __INOUTMAC_H__
  10. #include "malloc.h"
  11. #define MAX_ADAPTER_DESCRIPTION_LENGTH  128 // arb.
  12. #define MAX_ADAPTER_NAME_LENGTH         256 // arb.
  13. #define MAX_ADAPTER_ADDRESS_LENGTH      8   // arb.
  14. typedef struct {
  15.     char String[4 * 4];
  16. } K_IP_ADDRESS_STRING, *K_PIP_ADDRESS_STRING, K_IP_MASK_STRING, *K_PIP_MASK_STRING;
  17. typedef struct _K_IP_ADDR_STRING {
  18.     struct _K_IP_ADDR_STRING* Next;
  19.     K_IP_ADDRESS_STRING IpAddress;
  20.     K_IP_MASK_STRING IpMask;
  21.     DWORD Context;
  22. } K_IP_ADDR_STRING, *K_PIP_ADDR_STRING;
  23. typedef struct _K_IP_ADAPTER_INFO {
  24.     struct _K_IP_ADAPTER_INFO* Next;
  25.     DWORD ComboIndex;
  26.     char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];
  27.     char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];
  28.     UINT AddressLength;
  29.     BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
  30.     DWORD Index;
  31.     UINT Type;
  32.     UINT DhcpEnabled;
  33.     K_PIP_ADDR_STRING CurrentIpAddress;
  34.     K_IP_ADDR_STRING IpAddressList;
  35.     K_IP_ADDR_STRING GatewayList;
  36.     K_IP_ADDR_STRING DhcpServer;
  37.     BOOL HaveWins;
  38.     K_IP_ADDR_STRING PrimaryWinsServer;
  39.     K_IP_ADDR_STRING SecondaryWinsServer;
  40.     time_t LeaseObtained;
  41.     time_t LeaseExpires;
  42. } K_IP_ADAPTER_INFO, *K_PIP_ADAPTER_INFO;
  43. //此函数会确保两个地址均有
  44. //返回值为网卡数量
  45. inline int gGetMacAndIPAddress(BYTE* pMacAddressIn, DWORD* pIPAddressIn, BYTE* pMacAddressOut, DWORD* pIPAddressOut, DWORD nMask = 0x0000a8c0, DWORD nMacLength = 6)
  46. {
  47. typedef DWORD(CALLBACK * PGAINFO)(K_PIP_ADAPTER_INFO, PULONG);//GetAdaptersInfo
  48.     //加载IP Helper API 所需的库文件
  49.     HINSTANCE hInst = LoadLibrary("iphlpapi.dll");
  50.     if(!hInst)
  51. return -1;
  52.     PGAINFO pGAInfo = (PGAINFO)GetProcAddress(hInst,"GetAdaptersInfo");
  53. if (!pGAInfo)
  54. return -1;
  55. K_PIP_ADAPTER_INFO pInfo = NULL;
  56. ULONG ulSize = 0;
  57. pGAInfo(pInfo, &ulSize);//第一次调用,获取缓冲区大小
  58. pInfo = (K_PIP_ADAPTER_INFO)alloca(ulSize);
  59. pGAInfo(pInfo, &ulSize);
  60. bool bIn = false;
  61. char* pMacAddressInX = (char*)alloca(nMacLength + 1);
  62. DWORD nIPAddressInX = 0;
  63. bool bOut = false;
  64. char* pMacAddressOutX = (char*)alloca(nMacLength + 1);
  65. DWORD nIPAddressOutX = 0;
  66. DWORD nMAcNum = 0;
  67.     //遍历每一张网卡
  68.     while (pInfo && (!bIn || !bOut))
  69.     {
  70. K_PIP_ADDR_STRING pAddTemp = &(pInfo->IpAddressList);       
  71. while (pAddTemp)/*遍历IP列表中的每一个元素*/
  72. {
  73. DWORD nAddress = inet_addr(pAddTemp->IpAddress.String);
  74. if (!bIn &&
  75. ((nAddress & 0x0000FFFF) == nMask)
  76. )
  77. {
  78. bIn = true;
  79. nIPAddressInX = nAddress;
  80. //物理地址的长度
  81. if (pInfo->AddressLength == nMacLength)
  82. memcpy(pMacAddressInX, pInfo->Address, nMacLength);
  83. else
  84. memset(pMacAddressInX, 0, nMacLength);
  85. }
  86. if (!bOut &&
  87. ((nAddress & 0x0000FFFF) != nMask)
  88. )
  89. {
  90. bOut = true;
  91. nIPAddressOutX = nAddress;
  92. //物理地址的长度
  93. if (pInfo->AddressLength == nMacLength)
  94. memcpy(pMacAddressOutX, pInfo->Address, nMacLength);
  95. else
  96. memset(pMacAddressOutX, 0, nMacLength);
  97. }
  98. pAddTemp = pAddTemp->Next;
  99. }
  100. //将当前指针移向下一个
  101. pInfo = pInfo->Next;
  102. nMAcNum++;
  103. }
  104. if (bIn)
  105. {
  106. if (pMacAddressIn)
  107. memcpy(pMacAddressIn, pMacAddressInX, nMacLength);
  108. if (pIPAddressIn)
  109. *pIPAddressIn = nIPAddressInX;
  110. if (!bOut)
  111. {
  112. if (pMacAddressOut)
  113. memcpy(pMacAddressOut, pMacAddressInX, nMacLength);
  114. if (pIPAddressOut)
  115. *pIPAddressOut = nIPAddressInX;
  116. }
  117. }
  118. if (bOut)
  119. {
  120. if (pMacAddressOut)
  121. memcpy(pMacAddressOut, pMacAddressOutX, nMacLength);
  122. if (pIPAddressOut)
  123. *pIPAddressOut = nIPAddressOutX;
  124. if (!bIn)
  125. {
  126. if (pMacAddressIn)
  127. memcpy(pMacAddressIn, pMacAddressOutX, nMacLength);
  128. if (pIPAddressIn)
  129. *pIPAddressIn = nIPAddressOutX;
  130. }
  131. }
  132. FreeLibrary(hInst);
  133. return nMAcNum;
  134. }
  135. #endif // __INOUTMAC_H__