diff --git a/sdk_core/src/base/network_util.cpp b/sdk_core/src/base/network_util.cpp index 6ffca23..c3a8f16 100644 --- a/sdk_core/src/base/network_util.cpp +++ b/sdk_core/src/base/network_util.cpp @@ -65,35 +65,56 @@ apr_socket_t *CreateBindSocket(uint16_t port, apr_pool_t *mem_pool, bool nonbloc return s; } + #ifdef WIN32 + +bool GetAdapterState(const IP_ADAPTER_INFO *pAdapter) +{ + if(pAdapter == NULL) { + return false; + } + MIB_IFROW info; + memset(&info ,0 ,sizeof(MIB_IFROW)); + info.dwIndex = pAdapter->Index; + if (GetIfEntry(&info) != NOERROR) { + return false; + } + if (info.dwOperStatus == IF_OPER_STATUS_NON_OPERATIONAL + || info.dwOperStatus == IF_OPER_STATUS_UNREACHABLE + || info.dwOperStatus == IF_OPER_STATUS_DISCONNECTED + || info.dwOperStatus == IF_OPER_STATUS_CONNECTING) + return false; + return true; +} + bool FindLocalIP(const struct sockaddr_in &client_addr, uint32_t &local_ip) { - bool found = false; - ULONG ulOutbufLen = sizeof(IP_ADAPTER_INFO); - boost::scoped_array pAdapterInfo(new uint8_t[ulOutbufLen]); - DWORD dlRetVal = GetAdaptersInfo(reinterpret_cast(pAdapterInfo.get()), &ulOutbufLen); - if (dlRetVal == ERROR_BUFFER_OVERFLOW) { - pAdapterInfo.reset(new uint8_t[ulOutbufLen]); - dlRetVal = GetAdaptersInfo(reinterpret_cast(pAdapterInfo.get()), &ulOutbufLen); - } + bool found = false; + ULONG ulOutbufLen = sizeof(IP_ADAPTER_INFO); + boost::scoped_array pAdapterInfo(new uint8_t[ulOutbufLen]); + DWORD dlRetVal = GetAdaptersInfo(reinterpret_cast(pAdapterInfo.get()), &ulOutbufLen); + if (dlRetVal == ERROR_BUFFER_OVERFLOW) { + pAdapterInfo.reset(new uint8_t[ulOutbufLen]); + dlRetVal = GetAdaptersInfo(reinterpret_cast(pAdapterInfo.get()), &ulOutbufLen); + } - IP_ADAPTER_INFO *pAdapter = reinterpret_cast(pAdapterInfo.get()); - if (NO_ERROR == dlRetVal && pAdapter != NULL) { - while (pAdapter) { - std::string str_ip = pAdapter->IpAddressList.IpAddress.String; - std::string str_mask = pAdapter->IpAddressList.IpMask.String; - ULONG host_ip = inet_addr(const_cast(str_ip.c_str())); - ULONG host_mask = inet_addr(const_cast(str_mask.c_str())); + IP_ADAPTER_INFO *pAdapter = reinterpret_cast(pAdapterInfo.get()); + if (NO_ERROR == dlRetVal && pAdapter != NULL) { + while (GetAdapterState(pAdapter)) { + std::string str_ip = pAdapter->IpAddressList.IpAddress.String; + std::string str_mask = pAdapter->IpAddressList.IpMask.String; + ULONG host_ip = inet_addr(const_cast(str_ip.c_str())); + ULONG host_mask = inet_addr(const_cast(str_mask.c_str())); - if ((host_ip & host_mask) == - (client_addr.sin_addr.S_un.S_addr & host_mask)) { - local_ip = host_ip; - found = true; - break; - } - pAdapter = pAdapter->Next; - } - } - return found; + if ((host_ip & host_mask) == + (client_addr.sin_addr.S_un.S_addr & host_mask)) { + local_ip = host_ip; + found = true; + break; + } + pAdapter = pAdapter->Next; + } + } + return found; } #else bool FindLocalIP(const struct sockaddr_in &client_addr, uint32_t &local_ip) { diff --git a/sdk_core/src/device_discovery.cpp b/sdk_core/src/device_discovery.cpp index 55f4fce..bf5e5cc 100644 --- a/sdk_core/src/device_discovery.cpp +++ b/sdk_core/src/device_discovery.cpp @@ -210,6 +210,9 @@ void DeviceDiscovery::OnBroadcast(const CommPacket &packet, apr_sockaddr_t *addr result = false; break; } + LOG_INFO(" LocalIP: {}", inet_ntoa(*(struct in_addr *)&local_ip)); + LOG_INFO("Command Port: {}", lidar_info.cmd_port); + LOG_INFO("Data Port: {}", lidar_info.data_port); CommPacket packet; memset(&packet, 0, sizeof(packet)); handshake_req.ip_addr = local_ip;