From 2127cb654fcff1674de06dfbf8ec43574ce69cf1 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 22 Oct 2020 21:39:39 +0300 Subject: EMLanProto: - fixes #2599 (Em-LAN: binds to localhost); - code cleaning; - version bump; --- protocols/EmLanProto/src/lan.cpp | 59 +++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 22 deletions(-) (limited to 'protocols/EmLanProto/src/lan.cpp') diff --git a/protocols/EmLanProto/src/lan.cpp b/protocols/EmLanProto/src/lan.cpp index d7737b2479..157470f2b0 100644 --- a/protocols/EmLanProto/src/lan.cpp +++ b/protocols/EmLanProto/src/lan.cpp @@ -5,12 +5,9 @@ CLan::CLan() { - m_income = INVALID_SOCKET; - m_filesoc = INVALID_SOCKET; - m_status = LS_OK; - m_mode = LM_OFF; - m_hListenThread = nullptr; - m_hAcceptTCPThread = nullptr; + memset(&m_curAddr, 0, sizeof(m_curAddr)); + memset(&m_hostAddr, 0, sizeof(m_hostAddr)); + Startup(); } @@ -19,6 +16,36 @@ CLan::~CLan() Shutdown(); } +bool CLan::ResetInterfaces() +{ + char hostname[256]; + if (gethostname(hostname, 256) != 0) { + m_status = LS_CANT_GET_HOSTADDR; + return false; + } + + int hostAddrCount = 0; + in_addr hostAddr[MAX_INTERNAL_IP]; + + hostent *host = gethostbyname(hostname); + char **pAddr = host->h_addr_list; + + while (*pAddr && hostAddrCount < MAX_INTERNAL_IP) { + in_addr addr; + addr.S_un.S_addr = *((u_long *)(*pAddr)); + hostAddr[hostAddrCount++] = addr; + pAddr++; + } + + // nothing changed? return false + if (m_hostAddrCount == hostAddrCount && !memcmp(m_hostAddr, hostAddr, sizeof(hostAddr))) + return false; + + m_hostAddrCount = hostAddrCount; + memcpy(m_hostAddr, hostAddr, sizeof(hostAddr)); + return true; +} + void CLan::Startup() { WSADATA wsa; @@ -26,22 +53,7 @@ void CLan::Startup() m_status = LS_OK; m_mode = LM_ON; - char hostname[256]; - if (gethostname(hostname, 256) == 0) { - hostent* host = gethostbyname(hostname); - char** pAddr = host->h_addr_list; - m_hostAddrCount = 0; - while (*pAddr && m_hostAddrCount < MAX_INTERNAL_IP) { - in_addr addr; - addr.S_un.S_addr = *((u_long*)(*pAddr)); - m_hostAddr[m_hostAddrCount++] = addr; - pAddr++; - } - m_curAddr = m_hostAddr[0]; - } - else { - m_status = LS_CANT_GET_HOSTADDR; - } + ResetInterfaces(); } else { m_status = LS_OK; @@ -91,6 +103,9 @@ void CLan::StartListen() if (m_mode != LM_ON) return; + if (m_curAddr.S_un.S_addr == 0) + return; + m_income = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); m_filesoc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (m_income == INVALID_SOCKET || m_filesoc == INVALID_SOCKET) { -- cgit v1.2.3