summaryrefslogtreecommitdiff
path: root/protocols/EmLanProto/src/lan.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2020-10-22 21:39:39 +0300
committerGeorge Hazan <ghazan@miranda.im>2020-10-22 21:39:39 +0300
commit2127cb654fcff1674de06dfbf8ec43574ce69cf1 (patch)
tree39a1dde91f34b98c20d6272844d3ecff58ada50e /protocols/EmLanProto/src/lan.cpp
parent261ba202738ee2b9c049d12a26c3901e04d18e3f (diff)
EMLanProto:
- fixes #2599 (Em-LAN: binds to localhost); - code cleaning; - version bump;
Diffstat (limited to 'protocols/EmLanProto/src/lan.cpp')
-rw-r--r--protocols/EmLanProto/src/lan.cpp59
1 files changed, 37 insertions, 22 deletions
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) {