diff --git a/230113-civ6hooksendto/hookdll.cpp b/230113-civ6hooksendto/hookdll.cpp index 70dcada364c0b59ed782cd634eb194749e287aa6..3a0eebf69c338be44f5d9bc28fd88c376c942888 100644 --- a/230113-civ6hooksendto/hookdll.cpp +++ b/230113-civ6hooksendto/hookdll.cpp @@ -12,23 +12,25 @@ Hook *sendto_hook; SockQueue sockqueue; // 枚举当前所有可用网卡的IPv4地址 -const std::vector &enum_addr() +static const std::vector &enum_addr() { static std::vector list; hostent *phost = gethostbyname(""); // 获取本机网卡 if (phost) { - if (phost->h_length == list.size()) // 数量相同直接返回 - return list; char **ppc = phost->h_addr_list; // 获取地址列表 - if (ppc) { - list.clear(); - // 遍历列表添加到容器 - while (*ppc) { - in_addr addr; - memcpy(&addr, *ppc, sizeof(in_addr)); - list.push_back(addr); - ppc++; - } + int addr_num = 0; + while (*ppc) + addr_num++, ppc++; // 获取地址个数 + if (addr_num == list.size()) // 数量相同直接返回 + return list; + ppc = phost->h_addr_list; + list.clear(); + // 遍历列表添加到容器 + while (*ppc) { + in_addr addr; + memcpy(&addr, *ppc, sizeof(in_addr)); + list.push_back(addr); + ppc++; } } return list; diff --git a/230130-hookgamesendto/src/fksendto.cpp b/230130-hookgamesendto/src/fksendto.cpp index 52056178d2d0ac10fb75ba9423012aac4cccec79..5c36e94ad153e0a855ac2fec84a102676e2a8b97 100644 --- a/230130-hookgamesendto/src/fksendto.cpp +++ b/230130-hookgamesendto/src/fksendto.cpp @@ -22,18 +22,20 @@ static const std::vector &enum_addr() static std::vector list; hostent *phost = gethostbyname(""); // 获取本机网卡 if (phost) { - if (phost->h_length == list.size()) // 数量相同直接返回 - return list; char **ppc = phost->h_addr_list; // 获取地址列表 - if (ppc) { - list.clear(); - // 遍历列表添加到容器 - while (*ppc) { - in_addr addr; - memcpy(&addr, *ppc, sizeof(in_addr)); - list.push_back(addr); - ppc++; - } + int addr_num = 0; + while (*ppc) + addr_num++, ppc++; // 获取地址个数 + if (addr_num == list.size()) // 数量相同直接返回 + return list; + ppc = phost->h_addr_list; + list.clear(); + // 遍历列表添加到容器 + while (*ppc) { + in_addr addr; + memcpy(&addr, *ppc, sizeof(in_addr)); + list.push_back(addr); + ppc++; } } return list;