ifconfig.cpp 7.2 KB

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