| 30 | namespace phxsql { |
| 31 | |
| 32 | char *PhxBaseConfig::InitInnerIP() { |
| 33 | int sock_fd; |
| 34 | static char addr[32] = { "" }; |
| 35 | char tmp_addr[32] = { "" }; |
| 36 | |
| 37 | struct in_addr my_addr[2]; |
| 38 | struct ifreq stIfreq[10]; |
| 39 | const char *pInnerList[] = { "10.0.0.0", "10.255.255.255", "172.16.0.0", "172.31.255.255", "192.168.0.0", |
| 40 | "192.168.255.255", "169.254.0.0", "169.254.255.255" }; |
| 41 | |
| 42 | if (-1 == (sock_fd = socket(PF_INET, SOCK_DGRAM, 0))) { |
| 43 | return addr; |
| 44 | } |
| 45 | |
| 46 | struct ifconf ifc; |
| 47 | memset(&ifc, 0, sizeof(ifc)); |
| 48 | ifc.ifc_len = sizeof(stIfreq); |
| 49 | ifc.ifc_req = stIfreq; |
| 50 | ioctl(sock_fd, SIOCGIFCONF, &ifc); |
| 51 | |
| 52 | bool is_eth1 = false; |
| 53 | bool is_get_innerip = false; |
| 54 | |
| 55 | for (size_t i = 0; i < ifc.ifc_len / sizeof(ifreq); i++) { |
| 56 | sockaddr_in staddr; |
| 57 | memcpy(&staddr, &ifc.ifc_req[i].ifr_addr, sizeof(staddr)); |
| 58 | |
| 59 | if (0 == strncasecmp(ifc.ifc_req[i].ifr_name, "eth1", strlen("eth1"))) { |
| 60 | is_eth1 = true; |
| 61 | } |
| 62 | |
| 63 | if (!is_eth1 && is_get_innerip) { |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | for (size_t j = 0; j < sizeof(pInnerList) / sizeof(pInnerList[0]) / 2; j++) { |
| 68 | inet_aton(pInnerList[j * 2], &my_addr[0]); |
| 69 | my_addr[0].s_addr = htonl(my_addr[0].s_addr); |
| 70 | inet_aton(pInnerList[j * 2 + 1], &my_addr[1]); |
| 71 | my_addr[1].s_addr = htonl(my_addr[1].s_addr); |
| 72 | |
| 73 | unsigned long lTmp = htonl(staddr.sin_addr.s_addr); |
| 74 | if (lTmp >= my_addr[0].s_addr && lTmp <= my_addr[1].s_addr) { |
| 75 | if (is_eth1) { |
| 76 | inet_ntop(AF_INET, &staddr.sin_addr, addr, sizeof(addr)); |
| 77 | close(sock_fd); |
| 78 | return addr; |
| 79 | } |
| 80 | is_get_innerip = true; |
| 81 | memset(tmp_addr, 0x0, sizeof(tmp_addr)); |
| 82 | inet_ntop(AF_INET, &staddr.sin_addr, tmp_addr, sizeof(tmp_addr)); |
| 83 | } |
| 84 | } |
| 85 | is_eth1 = false; |
| 86 | } |
| 87 | |
| 88 | memcpy(addr, tmp_addr, sizeof(tmp_addr)); |
| 89 | close(sock_fd); |
nothing calls this directly
no outgoing calls
no test coverage detected