From 68531876e937776b8a8a65339d4bb1df190b8b00 Mon Sep 17 00:00:00 2001 From: ruanshudong Date: Tue, 21 May 2024 13:34:36 +0800 Subject: [PATCH] fix tc_socket getLocalHosts bug in linux --- util/src/tc_socket.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/util/src/tc_socket.cpp b/util/src/tc_socket.cpp index 79fe975b..6a7d1c37 100755 --- a/util/src/tc_socket.cpp +++ b/util/src/tc_socket.cpp @@ -893,6 +893,86 @@ void TC_Socket::createPipe(int fds[2], bool bBlock) THROW_EXCEPTION_SYSCODE(TC_Socket_Exception, "[TC_Socket::createPipe] error"); } +#if TARGET_PLATFORM_LINUX||TARGET_PLATFORM_IOS + +vector TC_Socket::getLocalHosts(int domain, bool withLoopIp) +{ + vector result; + TC_Socket ts; + ts.createSocket(SOCK_STREAM, domain); + + int cmd = SIOCGIFCONF; + + struct ifconf ifc; + + int numaddrs = 10; + + int old_ifc_len = 0; + + while(true) + { + int bufsize = numaddrs * static_cast(sizeof(struct ifreq)); + ifc.ifc_len = bufsize; + ifc.ifc_buf = (char*)malloc(bufsize); + int rs = ioctl(ts.getfd(), cmd, &ifc); + + if(rs == -1) + { + free(ifc.ifc_buf); + throw TC_Socket_Exception("[TC_Socket::getLocalHosts] ioctl error", errno); + } + else if(ifc.ifc_len == old_ifc_len) + { + break; + } + else + { + old_ifc_len = ifc.ifc_len; + } + + numaddrs += 10; + free(ifc.ifc_buf); + } + + numaddrs = ifc.ifc_len / static_cast(sizeof(struct ifreq)); + struct ifreq* ifr = ifc.ifc_req; + for(int i = 0; i < numaddrs; ++i) + { + char sAddr[INET_ADDRSTRLEN] = "\0"; + + if(ifr[i].ifr_addr.sa_family == AF_INET) + { + struct sockaddr_in* addr = reinterpret_cast(&ifr[i].ifr_addr); + if(addr->sin_addr.s_addr != 0) + { + inet_ntop(AF_INET, &(*addr).sin_addr, sAddr, sizeof(sAddr)); + } + } + else if (ifr[i].ifr_addr.sa_family == AF_INET6) + { + struct sockaddr_in6* addr = reinterpret_cast(&ifr[i].ifr_addr); + if(!memcmp(&addr->sin6_addr, &in6addr_any, sizeof(addr->sin6_addr))) + { + inet_ntop(AF_INET6, &(*addr).sin6_addr, sAddr, sizeof(sAddr)); + } + } + else + { + continue; + } + + if(withLoopIp || (TC_Port::strcasecmp(sAddr,"127.0.0.1") != 0 && TC_Port::strcasecmp(sAddr, "::1") != 0)) + { + result.push_back(sAddr); + } + } + + free(ifc.ifc_buf); + + return result; +} +#else + vector TC_Socket::getLocalHosts(int domain, bool withLoopIp) { vector hosts; @@ -937,6 +1017,7 @@ vector TC_Socket::getLocalHosts(int domain, bool withLoopIp) return hosts; } +#endif bool TC_Socket::isPending() {