ifconfig.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "ifconfig.h"
  2. #include "hplatform.h"
  3. #ifdef OS_LINUX
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <sys/ioctl.h>
  8. #include <netinet/in.h>
  9. #include <net/if.h>
  10. #include <arpa/inet.h>
  11. int ifconfig(std::vector<ifconfig_t>& ifcs) {
  12. int sock = socket(AF_INET, SOCK_DGRAM, 0);
  13. if (sock < 0) {
  14. return -10;
  15. }
  16. struct ifconf ifc;
  17. char buf[1024];
  18. ifc.ifc_len = sizeof(buf);
  19. ifc.ifc_buf = buf;
  20. int iRet = ioctl(sock, SIOCGIFCONF, &ifc);
  21. if (iRet != 0) {
  22. close(sock);
  23. return iRet;
  24. }
  25. int cnt = ifc.ifc_len / sizeof(struct ifreq);
  26. //printf("ifc.size=%d\n", cnt);
  27. if (cnt == 0) {
  28. close(sock);
  29. return -20;
  30. }
  31. struct ifreq ifr;
  32. ifcs.clear();
  33. ifconfig_t tmp;
  34. for (int i = 0; i < cnt; ++i) {
  35. // name
  36. strcpy(ifr.ifr_name, ifc.ifc_req[i].ifr_name);
  37. //printf("name: %s\n", ifr.ifr_name);
  38. strncpy(tmp.name, ifr.ifr_name, sizeof(tmp.name));
  39. // flags
  40. //iRet = ioctl(sock, SIOCGIFFLAGS, &ifr);
  41. //short flags = ifr.ifr_flags;
  42. // addr
  43. iRet = ioctl(sock, SIOCGIFADDR, &ifr);
  44. struct sockaddr_in* addr = (struct sockaddr_in*)&ifr.ifr_addr;
  45. char* ip = inet_ntoa(addr->sin_addr);
  46. //printf("ip: %s\n", ip);
  47. strncpy(tmp.ip, ip, sizeof(tmp.ip));
  48. // netmask
  49. iRet = ioctl(sock, SIOCGIFNETMASK, &ifr);
  50. addr = (struct sockaddr_in*)&ifr.ifr_netmask;
  51. char* netmask = inet_ntoa(addr->sin_addr);
  52. //printf("netmask: %s\n", netmask);
  53. strncpy(tmp.mask, netmask, sizeof(tmp.mask));
  54. // broadaddr
  55. iRet = ioctl(sock, SIOCGIFBRDADDR, &ifr);
  56. addr = (struct sockaddr_in*)&ifr.ifr_broadaddr;
  57. char* broadaddr = inet_ntoa(addr->sin_addr);
  58. //printf("broadaddr: %s\n", broadaddr);
  59. strncpy(tmp.broadcast, broadaddr, sizeof(tmp.broadcast));
  60. // hwaddr
  61. iRet = ioctl(sock, SIOCGIFHWADDR, &ifr);
  62. snprintf(tmp.mac, sizeof(tmp.mac), "%02x:%02x:%02x:%02x:%02x:%02x",
  63. (unsigned char)ifr.ifr_hwaddr.sa_data[0],
  64. (unsigned char)ifr.ifr_hwaddr.sa_data[1],
  65. (unsigned char)ifr.ifr_hwaddr.sa_data[2],
  66. (unsigned char)ifr.ifr_hwaddr.sa_data[3],
  67. (unsigned char)ifr.ifr_hwaddr.sa_data[4],
  68. (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
  69. //printf("mac: %s\n", tmp.mac);
  70. //printf("\n");
  71. if (strcmp(tmp.ip, "0.0.0.0") == 0 ||
  72. strcmp(tmp.ip, "127.0.0.1") == 0 ||
  73. strcmp(tmp.mac, "00:00:00:00:00:00") == 0) {
  74. continue;
  75. }
  76. ifcs.push_back(tmp);
  77. }
  78. close(sock);
  79. return 0;
  80. }
  81. #elif defined(OS_WIN)
  82. #include <winsock2.h>
  83. #include <windows.h>
  84. #include <ws2ipdef.h>
  85. #include <iphlpapi.h>
  86. int ifconfig(std::vector<ifconfig_t>& ifcs) {
  87. PIP_ADAPTER_ADDRESSES pAddrs = NULL;
  88. ULONG buflen = 0;
  89. GetAdaptersAddresses(AF_INET, 0, NULL, pAddrs, &buflen);
  90. if (buflen <= 0) return -20;
  91. pAddrs = (PIP_ADAPTER_ADDRESSES)malloc(buflen);
  92. GetAdaptersAddresses(AF_INET, 0, NULL, pAddrs, &buflen);
  93. PIP_ADAPTER_INFO pInfos = NULL;
  94. buflen = 0;
  95. GetAdaptersInfo(pInfos, &buflen);
  96. if (buflen <= 0) {
  97. free(pAddrs);
  98. return -20;
  99. }
  100. pInfos = (PIP_ADAPTER_INFO)malloc(buflen);
  101. GetAdaptersInfo(pInfos, &buflen);
  102. ifconfig_t ifc;
  103. std::vector<ifconfig_t> tmp_ifcs;
  104. PIP_ADAPTER_ADDRESSES pAddr = pAddrs;
  105. char mac[32] = {'\0'};
  106. while (pAddr) {
  107. snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x",
  108. pAddr->PhysicalAddress[0],
  109. pAddr->PhysicalAddress[1],
  110. pAddr->PhysicalAddress[2],
  111. pAddr->PhysicalAddress[3],
  112. pAddr->PhysicalAddress[4],
  113. pAddr->PhysicalAddress[5]);
  114. memset(&ifc, 0, sizeof(ifc));
  115. strncpy(ifc.name, pAddr->AdapterName, sizeof(ifc.name));
  116. strncpy(ifc.ip, "0.0.0.0", sizeof(ifc.ip));
  117. strncpy(ifc.mask, "255.255.255.255", sizeof(ifc.mask));
  118. strncpy(ifc.mac, mac, sizeof(ifc.mac));
  119. tmp_ifcs.push_back(ifc);
  120. pAddr = pAddr->Next;
  121. }
  122. PIP_ADAPTER_INFO pInfo = pInfos;
  123. while (pInfo) {
  124. for (auto& item : tmp_ifcs) {
  125. if (strcmp(item.name, pInfo->AdapterName) == 0) {
  126. // description more friendly
  127. strncpy(item.name, pInfo->Description, sizeof(item.name));
  128. strncpy(item.ip, pInfo->IpAddressList.IpAddress.String, sizeof(item.ip));
  129. strncpy(item.mask, pInfo->IpAddressList.IpMask.String, sizeof(item.mask));
  130. }
  131. }
  132. pInfo = pInfo->Next;
  133. }
  134. free(pAddrs);
  135. free(pInfos);
  136. // filter
  137. ifcs.clear();
  138. for (auto& item : tmp_ifcs) {
  139. if (strcmp(item.ip, "0.0.0.0") == 0 ||
  140. strcmp(item.ip, "127.0.0.1") == 0 ||
  141. strcmp(item.mac, "00:00:00:00:00:00") == 0) {
  142. continue;
  143. }
  144. ifcs.push_back(item);
  145. }
  146. return 0;
  147. }
  148. #elif defined(OS_MAC)
  149. #include <ifaddrs.h>
  150. #include <net/if_dl.h>
  151. int ifconfig(std::vector<ifconfig_t>& ifcs) {
  152. struct ifaddrs *ifas, *ifap;
  153. int ret = getifaddrs(&ifas);
  154. if (ret != 0) return ret;
  155. ifconfig_s tmp;
  156. for (ifap = ifas; ifap != NULL; ifap = ifap->ifa_next) {
  157. if (ifap->ifa_addr->sa_family == AF_INET) {
  158. // ipv4
  159. struct sockaddr_in* addr = (struct sockaddr_in*)ifap->ifa_addr;
  160. char* ip = inet_ntoa(addr->sin_addr);
  161. // filter
  162. if (strcmp(ip, "0.0.0.0") == 0 || strcmp(ip, "127.0.0.1") == 0) {
  163. continue;
  164. }
  165. memset(&tmp, 0, sizeof(tmp));
  166. strncpy(tmp.name, ifap->ifa_name, sizeof(tmp.name));
  167. strncpy(tmp.ip, ip, sizeof(tmp.ip));
  168. // netmask
  169. addr = (struct sockaddr_in*)ifap->ifa_netmask;
  170. char* netmask = inet_ntoa(addr->sin_addr);
  171. strncpy(tmp.mask, netmask, sizeof(tmp.mask));
  172. // broadaddr
  173. addr = (struct sockaddr_in*)ifap->ifa_broadaddr;
  174. char* broadaddr = inet_ntoa(addr->sin_addr);
  175. strncpy(tmp.broadcast, broadaddr, sizeof(tmp.broadcast));
  176. // push_back
  177. ifcs.push_back(tmp);
  178. }
  179. }
  180. for (ifap = ifas; ifap != NULL; ifap = ifap->ifa_next) {
  181. if (ifap->ifa_addr->sa_family == AF_LINK) {
  182. // hwaddr
  183. for (auto iter = ifcs.begin(); iter != ifcs.end(); ++iter) {
  184. if (strcmp(iter->name, ifap->ifa_name) == 0) {
  185. struct sockaddr_dl* addr = (struct sockaddr_dl*)ifap->ifa_addr;
  186. unsigned char* pmac = (unsigned char*)LLADDR(addr);
  187. snprintf(iter->mac, sizeof(iter->mac), "%02x:%02x:%02x:%02x:%02x:%02x",
  188. pmac[0], pmac[1], pmac[2], pmac[3], pmac[4], pmac[5]);
  189. // filter
  190. if (strcmp(iter->mac, "00:00:00:00:00:00") == 0) {
  191. ifcs.erase(iter);
  192. }
  193. break;
  194. }
  195. }
  196. }
  197. }
  198. freeifaddrs(ifas);
  199. return 0;
  200. }
  201. #else
  202. int ifconfig(std::vector<ifconfig_t>& ifcs) {
  203. return -10; // unimplemented
  204. }
  205. #endif