ifconfig.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. int ifconfig(std::vector<ifconfig_t>& ifcs) {
  90. PIP_ADAPTER_ADDRESSES pAddrs = NULL;
  91. ULONG buflen = 0;
  92. GetAdaptersAddresses(AF_INET, 0, NULL, pAddrs, &buflen);
  93. pAddrs = (PIP_ADAPTER_ADDRESSES)malloc(buflen);
  94. GetAdaptersAddresses(AF_INET, 0, NULL, pAddrs, &buflen);
  95. PIP_ADAPTER_INFO pInfos = NULL;
  96. buflen = 0;
  97. GetAdaptersInfo(pInfos, &buflen);
  98. pInfos = (PIP_ADAPTER_INFO)malloc(buflen);
  99. GetAdaptersInfo(pInfos, &buflen);
  100. ifconfig_t ifc;
  101. std::vector<ifconfig_t> tmp_ifcs;
  102. PIP_ADAPTER_ADDRESSES pAddr = pAddrs;
  103. char mac[32] = {'\0'};
  104. while (pAddr) {
  105. snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x",
  106. pAddr->PhysicalAddress[0],
  107. pAddr->PhysicalAddress[1],
  108. pAddr->PhysicalAddress[2],
  109. pAddr->PhysicalAddress[3],
  110. pAddr->PhysicalAddress[4],
  111. pAddr->PhysicalAddress[5]);
  112. memset(&ifc, 0, sizeof(ifc));
  113. strncpy(ifc.name, pAddr->AdapterName, sizeof(ifc.name));
  114. strncpy(ifc.ip, "0.0.0.0", sizeof(ifc.ip));
  115. strncpy(ifc.mask, "255.255.255.255", sizeof(ifc.mask));
  116. strncpy(ifc.mac, mac, sizeof(ifc.mac));
  117. tmp_ifcs.push_back(ifc);
  118. pAddr = pAddr->Next;
  119. }
  120. PIP_ADAPTER_INFO pInfo = pInfos;
  121. while (pInfo) {
  122. for (auto& item : tmp_ifcs) {
  123. if (strcmp(item.name, pInfo->AdapterName) == 0) {
  124. // description more friendly
  125. strncpy(item.name, pInfo->Description, sizeof(item.name));
  126. strncpy(item.ip, pInfo->IpAddressList.IpAddress.String, sizeof(item.ip));
  127. strncpy(item.mask, pInfo->IpAddressList.IpMask.String, sizeof(item.mask));
  128. }
  129. }
  130. pInfo = pInfo->Next;
  131. }
  132. free(pAddrs);
  133. free(pInfos);
  134. // filter
  135. ifcs.clear();
  136. for (auto& item : tmp_ifcs) {
  137. if (strcmp(item.ip, "0.0.0.0") == 0 ||
  138. strcmp(item.ip, "127.0.0.1") == 0 ||
  139. strcmp(item.mac, "00:00:00:00:00:00") == 0) {
  140. continue;
  141. }
  142. ifcs.push_back(item);
  143. }
  144. return 0;
  145. }
  146. #elif defined(OS_MAC)
  147. #include <ifaddrs.h>
  148. #include <net/if_dl.h>
  149. int ifconfig(std::vector<ifconfig_t>& ifcs) {
  150. struct ifaddrs *ifas, *ifap;
  151. int ret = getifaddrs(&ifas);
  152. if (ret != 0) return ret;
  153. ifconfig_s tmp;
  154. for (ifap = ifas; ifap != NULL; ifap = ifap->ifa_next) {
  155. if (ifap->ifa_addr->sa_family == AF_INET) {
  156. // ipv4
  157. struct sockaddr_in* addr = (struct sockaddr_in*)ifap->ifa_addr;
  158. char* ip = inet_ntoa(addr->sin_addr);
  159. // filter
  160. if (strcmp(ip, "0.0.0.0") == 0 || strcmp(ip, "127.0.0.1") == 0) {
  161. continue;
  162. }
  163. memset(&tmp, 0, sizeof(tmp));
  164. strncpy(tmp.name, ifap->ifa_name, sizeof(tmp.name));
  165. strncpy(tmp.ip, ip, sizeof(tmp.ip));
  166. // netmask
  167. addr = (struct sockaddr_in*)ifap->ifa_netmask;
  168. char* netmask = inet_ntoa(addr->sin_addr);
  169. strncpy(tmp.mask, netmask, sizeof(tmp.mask));
  170. // broadaddr
  171. addr = (struct sockaddr_in*)ifap->ifa_broadaddr;
  172. char* broadaddr = inet_ntoa(addr->sin_addr);
  173. strncpy(tmp.broadcast, broadaddr, sizeof(tmp.broadcast));
  174. // push_back
  175. ifcs.push_back(tmp);
  176. }
  177. }
  178. for (ifap = ifas; ifap != NULL; ifap = ifap->ifa_next) {
  179. if (ifap->ifa_addr->sa_family == AF_LINK) {
  180. // hwaddr
  181. for (auto iter = ifcs.begin(); iter != ifcs.end(); ++iter) {
  182. if (strcmp(iter->name, ifap->ifa_name) == 0) {
  183. struct sockaddr_dl* addr = (struct sockaddr_dl*)ifap->ifa_addr;
  184. unsigned char* pmac = (unsigned char*)LLADDR(addr);
  185. snprintf(iter->mac, sizeof(iter->mac), "%02x:%02x:%02x:%02x:%02x:%02x",
  186. pmac[0], pmac[1], pmac[2], pmac[3], pmac[4], pmac[5]);
  187. // filter
  188. if (strcmp(iter->mac, "00:00:00:00:00:00") == 0) {
  189. ifcs.erase(iter);
  190. }
  191. break;
  192. }
  193. }
  194. }
  195. }
  196. freeifaddrs(ifas);
  197. return 0;
  198. }
  199. #else
  200. int ifconfig(std::vector<ifconfig_t>& ifcs) {
  201. return -10; // unimplemented
  202. }
  203. #endif