Khan's Notebook GCC/GNU/Linux Delphi/Window Java/Anywhere

路漫漫,长修远,我们不能没有钱
随笔 - 172, 文章 - 0, 评论 - 257, 引用 - 0
数据加载中……

linux 和 windows下 获取主机网卡mac地址

  1 #ifndef __EHTERNET_HPP__
  2 #define __EHTERNET_HPP__
  3 
  4 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
  5 #define MY_WIN32
  6 #elif defined(linux) || defined(__linux) || defined(__linux__)
  7 #define MY_LINUX
  8 #endif
  9 
 10 #include<string>
 11 
 12 #if defined(MY_WIN32)
 13 #include <winsock2.h>
 14 #include <iphlpapi.h>
 15 #endif
 16 
 17 #if defined(MY_LINUX)
 18 #include <sys/socket.h>
 19 //#include <linux/if.h>
 20 //#include <net/if.h>
 21 #include <sys/ioctl.h>
 22 #endif
 23 
 24 #if defined(MY_WIN32)
 25 std::string operstatusToStr(IF_OPER_STATUS status){
 26     switch(status){
 27     case IfOperStatusUp:
 28         return "up";
 29     case IfOperStatusDown:
 30         return "down";
 31     case IfOperStatusTesting:
 32         return "testing";
 33     case IfOperStatusUnknown:
 34         return "unknown";
 35     case IfOperStatusDormant:
 36         return "dormant";
 37     case IfOperStatusNotPresent:
 38         return "notPresent";
 39     case IfOperStatusLowerLayerDown:
 40         return "sLowerLayerDown";
 41     }
 42     return "unknown";
 43 }
 44 
 45 bool GetMacByGetAdaptersAddresses(std::string& macOUT, bool all = false) {
 46     bool ret = false;
 47 
 48     ULONG family = AF_UNSPEC;//返回包括 IPV4 和 IPV6 地址
 49     ULONG flags = GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_INCLUDE_GATEWAYS;//包括 IPV4 ,IPV6 閘道器
 50     ULONG outBufLen = sizeof(IP_ADAPTER_ADDRESSES) * 1;
 51     PIP_ADAPTER_ADDRESSES pAddresses = NULL;
 52 
 53     PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
 54 
 55     DWORD dwRetVal = 0;
 56     do {
 57         pAddresses = (IP_ADAPTER_ADDRESSES *)malloc(outBufLen);
 58         if (pAddresses == NULL)
 59             return ret;
 60         dwRetVal = GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen);
 61         if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
 62             free(pAddresses);
 63             pAddresses = NULL;
 64         } else {
 65             break;
 66         }
 67     } while (dwRetVal == ERROR_BUFFER_OVERFLOW);
 68 
 69     if (dwRetVal == NO_ERROR) {
 70         pCurrAddresses = pAddresses;
 71         while (pCurrAddresses) {
 72             if (pCurrAddresses->PhysicalAddressLength != 0) {
 73                 if(all || pCurrAddresses->OperStatus == IfOperStatusUp){
 74                     char type[8= {0};
 75                     switch (pCurrAddresses->IfType){ //网卡类型
 76                     case MIB_IF_TYPE_ETHERNET:
 77                         sprintf(type, "eth");
 78                         break;
 79                     case MIB_IF_TYPE_PPP:
 80                         sprintf(type, "ppp");
 81                         break;
 82                     case MIB_IF_TYPE_LOOPBACK:
 83                         sprintf(type, "loop");
 84                         break;
 85                     case MIB_IF_TYPE_SLIP:
 86                         sprintf(type, "atm");
 87                         break;
 88                     case IF_TYPE_IEEE80211:
 89                         sprintf(type, "wifi");
 90                         break;
 91                     }
 92 
 93                     char acMAC[32];
 94                     sprintf(acMAC, "%02X%02X%02X%02X%02X%02X", (unsigned char)(pCurrAddresses->PhysicalAddress[0]), (unsigned char)(pCurrAddresses->PhysicalAddress[1]),
 95                             (unsigned char)(pCurrAddresses->PhysicalAddress[2]), (unsigned char)(pCurrAddresses->PhysicalAddress[3]),
 96                             (unsigned char)(pCurrAddresses->PhysicalAddress[4]), (unsigned char)(pCurrAddresses->PhysicalAddress[5]));
 97                     macOUT = macOUT + acMAC + '\n';
 98                 }
 99             }
100 
101 
102             pCurrAddresses = pCurrAddresses->Next;
103         }
104         ret = true;
105     } else {
106 
107     }
108 
109     if (pAddresses) free(pAddresses);
110     return ret;
111 }
112 #endif
113 
114 #if defined(MY_LINUX)
115 bool GetMacByGetAdaptersAddresses(std::string& macOUT, bool all = false){
116     int sock, if_count, i;
117     struct ifconf ifc;
118     struct ifreq ifr[10];
119     char mac[32];
120 
121     memset(&ifc, 0sizeof(struct ifconf));
122     sock = socket(AF_INET, SOCK_DGRAM, 0);
123 
124     ifc.ifc_len = 10 * sizeof(struct ifreq);
125     ifc.ifc_buf = (char *)ifr;
126 
127     //获取所有网卡信息
128     ioctl(sock, SIOCGIFCONF, (char *)&ifc);
129     if_count = ifc.ifc_len / (sizeof(struct ifreq));
130     for (i = 0; i < if_count; i++) {
131         if (ioctl(sock, SIOCGIFHWADDR, &ifr[i]) == 0) {
132             if(all || (ifr[i].ifr_flags & IFF_RUNNING) ){
133                 memcpy(mac, ifr[i].ifr_hwaddr.sa_data, 6);
134                 sprintf(mac, "%02X%02X%02X%02X%02X%02X", (unsigned char)(mac[0]), (unsigned char)(mac[1]), (unsigned char)(mac[2]),
135                         (unsigned char)(mac[3]), (unsigned char)(mac[4]), (unsigned char)(mac[5]));
136                 macOUT = macOUT + mac + '\n';
137 //                printf("eth: %s, mac: %02x:%02x:%02x:%02x:%02x:%02x\n", ifr[i].ifr_name, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
138             }
139         }
140     }
141     return true;
142 }
143 #endif
144 
145 #endif
146 
147 

调用方式

    std::string macs;
    GetMacByGetAdaptersAddresses(macs, true);
    qDebug() << TIMESTAMP << macs.c_str();



posted on 2021-06-03 17:42 Khan 阅读(890) 评论(0)  编辑 收藏 引用 所属分类: GCC/G++跨平台开发


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理