1
0

ifconfig.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. char macaddr[32] = {'\0'};
  38. for (int i = 0; i < cnt; ++i) {
  39. // name
  40. strcpy(ifr.ifr_name, ifc.ifc_req[i].ifr_name);
  41. //printf("name: %s\n", ifr.ifr_name);
  42. strncpy(tmp.name, ifr.ifr_name, sizeof(tmp.name));
  43. // flags
  44. //iRet = ioctl(sock, SIOCGIFFLAGS, &ifr);
  45. //short flags = ifr.ifr_flags;
  46. // addr
  47. iRet = ioctl(sock, SIOCGIFADDR, &ifr);
  48. struct sockaddr_in* addr = (struct sockaddr_in*)&ifr.ifr_addr;
  49. char* ip = inet_ntoa(addr->sin_addr);
  50. //printf("ip: %s\n", ip);
  51. strncpy(tmp.ip, ip, sizeof(tmp.ip));
  52. // netmask
  53. iRet = ioctl(sock, SIOCGIFNETMASK, &ifr);
  54. addr = (struct sockaddr_in*)&ifr.ifr_netmask;
  55. char* netmask = inet_ntoa(addr->sin_addr);
  56. //printf("netmask: %s\n", netmask);
  57. strncpy(tmp.mask, netmask, sizeof(tmp.mask));
  58. // broadaddr
  59. //iRet = ioctl(sock, SIOCGIFBRDADDR, &ifr);
  60. //addr = (struct sockaddr_in*)&ifr.ifr_broadaddr;
  61. //char* broadaddr = inet_ntoa(addr->sin_addr);
  62. //printf("broadaddr: %s\n", broadaddr);
  63. // hwaddr
  64. iRet = ioctl(sock, SIOCGIFHWADDR, &ifr);
  65. snprintf(macaddr, sizeof(macaddr), "%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", macaddr);
  73. strncpy(tmp.mac, macaddr, sizeof(tmp.mac));
  74. //printf("\n");
  75. if (strcmp(tmp.ip, "0.0.0.0") == 0 ||
  76. strcmp(tmp.ip, "127.0.0.1") == 0 ||
  77. strcmp(tmp.mac, "00:00:00:00:00:00") == 0) {
  78. continue;
  79. }
  80. ifcs.push_back(tmp);
  81. }
  82. close(sock);
  83. return 0;
  84. }
  85. #elif defined(OS_WIN)
  86. #include <winsock2.h>
  87. #include <windows.h>
  88. #include <ws2ipdef.h>
  89. #include <iphlpapi.h>
  90. #ifdef _MSC_VER
  91. #pragma comment(lib, "iphlpapi.lib")
  92. #pragma comment(lib, "ws2_32.lib")
  93. #endif
  94. int ifconfig(std::vector<ifconfig_t>& ifcs) {
  95. PIP_ADAPTER_ADDRESSES pAddrs = NULL;
  96. ULONG buflen = 0;
  97. GetAdaptersAddresses(AF_INET, 0, NULL, pAddrs, &buflen);
  98. pAddrs = (PIP_ADAPTER_ADDRESSES)malloc(buflen);
  99. GetAdaptersAddresses(AF_INET, 0, NULL, pAddrs, &buflen);
  100. PIP_ADAPTER_INFO pInfos = NULL;
  101. buflen = 0;
  102. GetAdaptersInfo(pInfos, &buflen);
  103. pInfos = (PIP_ADAPTER_INFO)malloc(buflen);
  104. GetAdaptersInfo(pInfos, &buflen);
  105. ifconfig_t ifc;
  106. std::vector<ifconfig_t> tmp_ifcs;
  107. PIP_ADAPTER_ADDRESSES pAddr = pAddrs;
  108. char mac[32] = {'\0'};
  109. while (pAddr) {
  110. snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x",
  111. pAddr->PhysicalAddress[0],
  112. pAddr->PhysicalAddress[1],
  113. pAddr->PhysicalAddress[2],
  114. pAddr->PhysicalAddress[3],
  115. pAddr->PhysicalAddress[4],
  116. pAddr->PhysicalAddress[5]);
  117. memset(&ifc, 0, sizeof(ifc));
  118. strncpy(ifc.name, pAddr->AdapterName, sizeof(ifc.name));
  119. strncpy(ifc.ip, "0.0.0.0", sizeof(ifc.ip));
  120. strncpy(ifc.mask, "255.255.255.255", sizeof(ifc.mask));
  121. strncpy(ifc.mac, mac, sizeof(ifc.mac));
  122. tmp_ifcs.push_back(ifc);
  123. pAddr = pAddr->Next;
  124. }
  125. PIP_ADAPTER_INFO pInfo = pInfos;
  126. while (pInfo) {
  127. for (auto& item : tmp_ifcs) {
  128. if (strcmp(item.name, pInfo->AdapterName) == 0) {
  129. // description more friendly
  130. strncpy(item.name, pInfo->Description, sizeof(item.name));
  131. strncpy(item.ip, pInfo->IpAddressList.IpAddress.String, sizeof(item.ip));
  132. strncpy(item.mask, pInfo->IpAddressList.IpMask.String, sizeof(item.mask));
  133. }
  134. }
  135. pInfo = pInfo->Next;
  136. }
  137. free(pAddrs);
  138. free(pInfos);
  139. // filter
  140. ifcs.clear();
  141. for (auto& item : tmp_ifcs) {
  142. if (strcmp(item.ip, "0.0.0.0") == 0 ||
  143. strcmp(item.ip, "127.0.0.1") == 0 ||
  144. strcmp(item.mac, "00:00:00:00:00:00") == 0) {
  145. continue;
  146. }
  147. ifcs.push_back(item);
  148. }
  149. return 0;
  150. }
  151. #else
  152. int ifconfig(std::vector<ifconfig_t>& ifcs) {
  153. return -10; // unimplemented
  154. }
  155. #endif