hifconf.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "hifconf.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #ifdef __unix__
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <sys/ioctl.h>
  10. #include <netinet/in.h>
  11. #include <linux/if.h>
  12. #include <arpa/inet.h>
  13. int ifconfig(std::vector<ifconfig_t>& ifcs) {
  14. int sock = socket(AF_INET, SOCK_DGRAM, 0);
  15. if (sock < 0) {
  16. return -10;
  17. }
  18. struct ifconf ifc;
  19. char buf[1024];
  20. ifc.ifc_len = sizeof(buf);
  21. ifc.ifc_buf = buf;
  22. int iRet = ioctl(sock, SIOCGIFCONF, &ifc);
  23. if (iRet != 0) {
  24. close(sock);
  25. return iRet;
  26. }
  27. int cnt = ifc.ifc_len / sizeof(struct ifreq);
  28. //printf("ifc.size=%d\n", cnt);
  29. if (cnt == 0) {
  30. close(sock);
  31. return -20;
  32. }
  33. struct ifreq ifr;
  34. ifcs.clear();
  35. ifconfig_t tmp;
  36. char macaddr[32] = {'\0'};
  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. // hwaddr
  63. iRet = ioctl(sock, SIOCGIFHWADDR, &ifr);
  64. snprintf(macaddr, sizeof(macaddr), "%02x:%02x:%02x:%02x:%02x:%02x",
  65. (unsigned char)ifr.ifr_hwaddr.sa_data[0],
  66. (unsigned char)ifr.ifr_hwaddr.sa_data[1],
  67. (unsigned char)ifr.ifr_hwaddr.sa_data[2],
  68. (unsigned char)ifr.ifr_hwaddr.sa_data[3],
  69. (unsigned char)ifr.ifr_hwaddr.sa_data[4],
  70. (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
  71. //printf("mac: %s\n", macaddr);
  72. strncpy(tmp.mac, macaddr, sizeof(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(_WIN32)
  85. #include <winsock2.h>
  86. #include <windows.h>
  87. #include <IphlpApi.h>
  88. #ifdef _MSC_VER
  89. #pragma comment(lib, "Iphlpapi.lib")
  90. #pragma comment(lib, "ws2_32.lib")
  91. #endif
  92. int ifconfig(std::vector<ifconfig_t>& ifcs) {
  93. PIP_ADAPTER_ADDRESSES pAddrs = NULL;
  94. ULONG buflen = 0;
  95. GetAdaptersAddresses(AF_INET, 0, NULL, pAddrs, &buflen);
  96. pAddrs = (PIP_ADAPTER_ADDRESSES)malloc(buflen);
  97. GetAdaptersAddresses(AF_INET, 0, NULL, pAddrs, &buflen);
  98. PIP_ADAPTER_INFO pInfos = NULL;
  99. buflen = 0;
  100. GetAdaptersInfo(pInfos, &buflen);
  101. pInfos = (PIP_ADAPTER_INFO)malloc(buflen);
  102. GetAdaptersInfo(pInfos, &buflen);
  103. ifconfig_t ifc;
  104. std::vector<ifconfig_t> tmp_ifcs;
  105. PIP_ADAPTER_ADDRESSES pAddr = pAddrs;
  106. char mac[32] = {'\0'};
  107. while (pAddr) {
  108. snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x",
  109. pAddr->PhysicalAddress[0],
  110. pAddr->PhysicalAddress[1],
  111. pAddr->PhysicalAddress[2],
  112. pAddr->PhysicalAddress[3],
  113. pAddr->PhysicalAddress[4],
  114. pAddr->PhysicalAddress[5]);
  115. memset(&ifc, 0, sizeof(ifc));
  116. strncpy(ifc.name, pAddr->AdapterName, sizeof(ifc.name));
  117. strncpy(ifc.ip, "0.0.0.0", sizeof(ifc.ip));
  118. strncpy(ifc.mask, "0.0.0.0", sizeof(ifc.ip));
  119. strncpy(ifc.mac, mac, sizeof(ifc.mac));
  120. tmp_ifcs.push_back(ifc);
  121. pAddr = pAddr->Next;
  122. }
  123. PIP_ADAPTER_INFO pInfo = pInfos;
  124. while (pInfo) {
  125. for (auto& item : tmp_ifcs) {
  126. if (strcmp(item.name, pInfo->AdapterName) == 0) {
  127. // description more friendly
  128. strncpy(item.name, pInfo->Description, sizeof(item.name));
  129. strncpy(item.ip, pInfo->IpAddressList.IpAddress.String, sizeof(item.ip));
  130. strncpy(item.mask, pInfo->IpAddressList.IpMask.String, sizeof(item.mask));
  131. }
  132. }
  133. pInfo = pInfo->Next;
  134. }
  135. free(pAddrs);
  136. free(pInfos);
  137. // filter
  138. ifcs.clear();
  139. for (auto& item : tmp_ifcs) {
  140. if (strcmp(item.ip, "0.0.0.0") == 0 ||
  141. strcmp(item.ip, "127.0.0.1") == 0 ||
  142. strcmp(item.mac, "00:00:00:00:00:00") == 0) {
  143. continue;
  144. }
  145. ifcs.push_back(item);
  146. }
  147. return 0;
  148. }
  149. #else
  150. int ifconfig(std::vector<ifconfig_t>& ifcs) {
  151. return -10; // unimplemented
  152. }
  153. #endif