diff options
author | George Hazan <ghazan@miranda.im> | 2020-10-22 21:39:39 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2020-10-22 21:39:39 +0300 |
commit | 2127cb654fcff1674de06dfbf8ec43574ce69cf1 (patch) | |
tree | 39a1dde91f34b98c20d6272844d3ecff58ada50e /protocols/EmLanProto/src | |
parent | 261ba202738ee2b9c049d12a26c3901e04d18e3f (diff) |
EMLanProto:
- fixes #2599 (Em-LAN: binds to localhost);
- code cleaning;
- version bump;
Diffstat (limited to 'protocols/EmLanProto/src')
-rw-r--r-- | protocols/EmLanProto/src/lan.cpp | 59 | ||||
-rw-r--r-- | protocols/EmLanProto/src/lan.h | 113 | ||||
-rw-r--r-- | protocols/EmLanProto/src/mlan.cpp | 101 | ||||
-rw-r--r-- | protocols/EmLanProto/src/mlan.h | 28 | ||||
-rw-r--r-- | protocols/EmLanProto/src/stdafx.h | 1 | ||||
-rw-r--r-- | protocols/EmLanProto/src/version.h | 2 |
6 files changed, 149 insertions, 155 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) {
diff --git a/protocols/EmLanProto/src/lan.h b/protocols/EmLanProto/src/lan.h index c6dc217c64..9083281619 100644 --- a/protocols/EmLanProto/src/lan.h +++ b/protocols/EmLanProto/src/lan.h @@ -9,82 +9,85 @@ #define MAX_INTERNAL_IP 32
#define PORT_NUMBER MAKE_PORT(34074)
-//! Class for operating with LAN
+// Class for operating with LAN
class CLan
{
public:
- //! constructor
+ // constructor
CLan();
- //! destructor
+ // destructor
~CLan();
- //! Helper function - returns status
+ // Helper function - returns status
int GetStatus() { return m_status; }
- //! Helper function - returns mode
+ // Helper function - returns mode
int GetMode() { return m_mode; }
- //! Getting host addresses count
+ // Getting host addresses count
int GetHostAddrCount() { return m_hostAddrCount; }
- //! Getting host addresses
+ // Getting host addresses
in_addr GetHostAddress(int ind) { return m_hostAddr[ind]; }
- //! Get current host address
+ // Get current host address
in_addr GetCurHostAddress() { return m_curAddr; }
protected:
- //! Lan status
+ // Lan status
enum enumStatus
{
- LS_OK, //!< no problems
- LS_WRONG_WINSOCK, //!< not found winsock of propper version
- LS_CANT_CREATE_SOCKET, //!< can not create income socket
- LS_CANT_GET_HOSTADDR, //!< can not find host address
- LS_CANT_TURN_ON_BROADCAST, //!< can not allow broadcast messages for socket
- LS_CANT_BIND_SOCKET, //!< can not bind socket to the address
- LS_CANT_START_LISTEN, //!< can not start listen on TCP socket
- LS_CANT_CREATE_THREADS, //!< can not create threads for listen and accept
+ LS_OK, //< no problems
+ LS_WRONG_WINSOCK, //< not found winsock of propper version
+ LS_CANT_CREATE_SOCKET, //< can not create income socket
+ LS_CANT_GET_HOSTADDR, //< can not find host address
+ LS_CANT_TURN_ON_BROADCAST, //< can not allow broadcast messages for socket
+ LS_CANT_BIND_SOCKET, //< can not bind socket to the address
+ LS_CANT_START_LISTEN, //< can not start listen on TCP socket
+ LS_CANT_CREATE_THREADS, //< can not create threads for listen and accept
};
- //! Lan mode
+ // Lan mode
enum enumMode
{
- LM_OFF, //!< Winsock is turned off
- LM_ON, //!< Winsock is on
- LM_LISTEN, //!< Listening for incoming messages
+ LM_OFF, //< Winsock is turned off
+ LM_ON, //< Winsock is on
+ LM_LISTEN, //< Listening for incoming messages
};
- //! Starts winsock
+ // Retrieve list of network interfaces
+ bool ResetInterfaces();
+
+ // Starts winsock
void Startup();
- //! Stops winsock
+ // Stops winsock
void Shutdown();
- //! Listen
+ // Listen
void StartListen();
- //! Stop Listen
+ // Stop Listen
void StopListen();
- //! Set current host address
+ // Set current host address
void SetCurHostAddress(in_addr addr);
- //! Send packet
+ // Send packet
void SendPacket(in_addr addr, const u_char* mes, int len);
- //! Send broadcast packet
+ // Send broadcast packet
void SendPacketBroadcast(const u_char* mes, int len);
- //! Event - called when packet is received
+ // Event - called when packet is received
virtual void OnRecvPacket(u_char*, int, in_addr) { };
- //! Event - called when new incoming tcp connection is created (new thread is created)
+ // Event - called when new incoming tcp connection is created (new thread is created)
virtual void OnInTCPConnection(u_long, SOCKET) { };
- //! Event - called when new outgoing tcp connection is created )new thread is created)
+ // Event - called when new outgoing tcp connection is created )new thread is created)
virtual void OnOutTCPConnection(u_long, SOCKET, LPVOID) {};
- //! Creates new outgoing TCP connection
+ // Creates new outgoing TCP connection
SOCKET CreateTCPConnection(u_long addr, LPVOID lpParameter);
private:
- //! Launches Listen procedure when in new thread
+ // Launches Listen procedure when in new thread
static void __cdecl ListenProc(void *lpParameter);
- //! Listnes for incoming messages
+ // Listnes for incoming messages
void Listen();
- //! Listen thread handle
- HANDLE m_hListenThread;
- //! Structure passed to new TCP connection thread
+ // Listen thread handle
+ HANDLE m_hListenThread = nullptr;
+ // Structure passed to new TCP connection thread
struct TTCPConnect
{
CLan* m_lan;
@@ -92,33 +95,33 @@ private: SOCKET m_socket;
LPVOID m_lpParameter;
};
- //! Launches accept procedure for TCP connections in new thread
+ // Launches accept procedure for TCP connections in new thread
static void __cdecl AcceptTCPProc(void *lpParameter);
- //! Accepts TCP connections
+ // Accepts TCP connections
void AcceptTCP();
- //! Accept TCP thread handle
- HANDLE m_hAcceptTCPThread;
+ // Accept TCP thread handle
+ HANDLE m_hAcceptTCPThread = nullptr;
- //! Called when new income TCP connection is created
+ // Called when new income TCP connection is created
static void __cdecl OnInTCPConnectionProc(void *lpParameter);
- //! Called when new ougoing TCP connectio is created
+ // Called when new ougoing TCP connectio is created
static void __cdecl OnOutTCPConnectionProc(void *lpParameter);
- //! Stores retrieved host addresses
+ // Stores retrieved host addresses
in_addr m_hostAddr[MAX_INTERNAL_IP];
- //! Current address count
- int m_hostAddrCount;
+ // Current address count
+ int m_hostAddrCount = 0;
- //! Stores current host address
+ // Stores current host address
in_addr m_curAddr;
- //! Socket for income messages
- SOCKET m_income;
- //! Socket for income files
- SOCKET m_filesoc;
- //! Current status
- int m_status;
- //! Current mode
- int m_mode;
+ // Socket for income messages
+ SOCKET m_income = INVALID_SOCKET;
+ // Socket for income files
+ SOCKET m_filesoc = INVALID_SOCKET;
+ // Current status
+ int m_status = LS_OK;
+ // Current mode
+ int m_mode = LM_OFF;
};
#endif //__lan_h__
diff --git a/protocols/EmLanProto/src/mlan.cpp b/protocols/EmLanProto/src/mlan.cpp index 2db580c26b..961d5c2124 100644 --- a/protocols/EmLanProto/src/mlan.cpp +++ b/protocols/EmLanProto/src/mlan.cpp @@ -26,27 +26,12 @@ enum enuLEXT LEXT_SENDURL, }; -CMLan::CMLan() +CMLan::CMLan() : + m_timer(Miranda_GetSystemWindow(), (UINT_PTR)this) { - m_RequiredIp = 0; - m_UseHostName = true; - - m_mirStatus = ID_STATUS_OFFLINE; - m_pRootContact = nullptr; - m_hCheckThread = nullptr; - - m_handleId = 1; - - m_amesAway = nullptr; - m_amesNa = nullptr; - m_amesOccupied = nullptr; - m_amesDnd = nullptr; - m_amesFfc = nullptr; - - m_pFileConnectionList = nullptr; + m_timer.OnEvent = Callback(this, &CMLan::OnTimer); LoadSettings(); - SetAllOffline(); } @@ -116,25 +101,27 @@ void CMLan::SetAllOffline() void CMLan::StartChecking() { - if (m_hCheckThread) + if (m_bChecking) return; for (TContact *cont = m_pRootContact; cont; cont = cont->m_prev) cont->m_time = MLAN_CHECK + MLAN_TIMEOUT; - m_hCheckThread = mir_forkthread(CheckProc, this); + if (ResetInterfaces()) + LoadSettings(); + + m_bChecking = true; + m_timer.Start(MLAN_SLEEP); + StartListen(); RequestStatus(true); } void CMLan::StopChecking() { - { - mir_cslock lck(m_csAccessClass); - if (m_hCheckThread) { - TerminateThread(m_hCheckThread, 0); - m_hCheckThread = nullptr; - } + if (m_bChecking) { + m_bChecking = false; + m_timer.Stop(); } m_mirStatus = ID_STATUS_OFFLINE; @@ -150,30 +137,21 @@ void CMLan::StopChecking() SetAllOffline(); } -void __cdecl CMLan::CheckProc(void *lpParameter) +void CMLan::OnTimer(CTimer*) { - CMLan *lan = (CMLan*)lpParameter; - lan->Check(); -} + mir_cslock lck(m_csAccessClass); -void CMLan::Check() -{ - while (true) { - Sleep(MLAN_SLEEP); - mir_cslock lck(m_csAccessClass); - - for (TContact *cont = m_pRootContact; cont; cont = cont->m_prev) { - if (cont->m_status != ID_STATUS_OFFLINE) { - if (cont->m_time) - cont->m_time--; - if (cont->m_time == MLAN_TIMEOUT) - RequestStatus(true, cont->m_addr.S_un.S_addr); - if (!cont->m_time) { - cont->m_status = ID_STATUS_OFFLINE; - MCONTACT hContact = FindContact(cont->m_addr, cont->m_nick, false, false, false); - if (hContact) - g_plugin.setWord(hContact, "Status", ID_STATUS_OFFLINE); - } + for (TContact *cont = m_pRootContact; cont; cont = cont->m_prev) { + if (cont->m_status != ID_STATUS_OFFLINE) { + if (cont->m_time) + cont->m_time--; + if (cont->m_time == MLAN_TIMEOUT) + RequestStatus(true, cont->m_addr.S_un.S_addr); + if (!cont->m_time) { + cont->m_status = ID_STATUS_OFFLINE; + MCONTACT hContact = FindContact(cont->m_addr, cont->m_nick, false, false, false); + if (hContact) + g_plugin.setWord(hContact, "Status", ID_STATUS_OFFLINE); } } } @@ -1186,7 +1164,7 @@ void CMLan::OnInTCPConnection(u_long addr, SOCKET in_sock) void CMLan::OnOutTCPConnection(u_long, SOCKET out_socket, LPVOID lpParameter) { EMLOG("Sending OUT TCP connection"); - TFileConnection *conn = (TFileConnection*)lpParameter; + TFileConnection *conn = (TFileConnection *)lpParameter; if (out_socket == INVALID_SOCKET) { EMLOG("Can't create OUT socket"); @@ -1224,20 +1202,20 @@ void CMLan::OnOutTCPConnection(u_long, SOCKET out_socket, LPVOID lpParameter) filecount++; CloseHandle(hFile); - wchar_t* filepart; + wchar_t *filepart; GetFullPathName(*pf, MAX_PATH, name, &filepart); free(*pf); *pf = mir_wstrdup(name); - mir_strcpy((char*)buf + len, _T2A(filepart)); + mir_strcpy((char *)buf + len, _T2A(filepart)); len += (int)mir_wstrlen(filepart) + 1; pf++; } - mir_strcpy((char*)buf + len, _T2A(conn->m_szDescription)); + mir_strcpy((char *)buf + len, _T2A(conn->m_szDescription)); len += (int)mir_wstrlen(conn->m_szDescription) + 1; - *((int*)(buf + 1)) = size; - *((int*)(buf + 1 + 4)) = filecount; + *((int *)(buf + 1)) = size; + *((int *)(buf + 1 + 4)) = filecount; GetCurrentDirectory(MAX_PATH, name); conn->m_szDir = mir_wstrdup(name); @@ -1287,7 +1265,7 @@ void CMLan::OnOutTCPConnection(u_long, SOCKET out_socket, LPVOID lpParameter) u_char snd_buf[5]; snd_buf[0] = FCODE_SND_NEXTFILE; int fsize = GetFileSize(hFile, nullptr); - *((int*)(snd_buf + 1)) = fsize; + *((int *)(snd_buf + 1)) = fsize; EMLOG("Sending file size"); if (conn->Send(snd_buf, 5)) { CloseHandle(hFile); @@ -1398,12 +1376,12 @@ int CMLan::SendFile(CCSDATA *ccs) conn->m_cid = cid; conn->m_hContact = ccs->hContact; - conn->m_szDescription = mir_wstrdup((wchar_t*)ccs->wParam); + conn->m_szDescription = mir_wstrdup((wchar_t *)ccs->wParam); int files = 0; - wchar_t** ppszFiles = (wchar_t**)ccs->lParam; + wchar_t **ppszFiles = (wchar_t **)ccs->lParam; while (ppszFiles[files]) files++; - conn->m_szFiles = new wchar_t*[files + 1]; + conn->m_szFiles = new wchar_t *[files + 1]; for (int i = 0; i < files; i++) conn->m_szFiles[i] = mir_wstrdup(ppszFiles[i]); conn->m_szFiles[files] = nullptr; @@ -1428,7 +1406,7 @@ int CMLan::FileAllow(CCSDATA *ccs) mir_cslock connLck(conn->m_csAccess); conn->m_state = TFileConnection::FCS_ALLOW; - conn->m_szDir = mir_wstrdup((wchar_t*)ccs->lParam); + conn->m_szDir = mir_wstrdup((wchar_t *)ccs->lParam); return cid; } @@ -1464,11 +1442,8 @@ int CMLan::FileCancel(CCSDATA *ccs) return 0; } -int CMLan::FileResume(int cid, PROTOFILERESUME* pfr) +int CMLan::FileResume(int cid, PROTOFILERESUME *pfr) { - //int cid = (int)ccs->wParam; - //PROTOFILERESUME* pfr = (PROTOFILERESUME*)ccs->lParam; - TFileConnection *conn = m_pFileConnectionList; while (conn) { if (conn->m_cid == cid) diff --git a/protocols/EmLanProto/src/mlan.h b/protocols/EmLanProto/src/mlan.h index 8396f5d105..9a2e257720 100644 --- a/protocols/EmLanProto/src/mlan.h +++ b/protocols/EmLanProto/src/mlan.h @@ -89,9 +89,8 @@ private: char *m_nick;
TContact *m_prev;
};
- u_int m_mirStatus;
- TContact *m_pRootContact;
- HANDLE m_hCheckThread;
+ u_int m_mirStatus = ID_STATUS_OFFLINE;
+ TContact *m_pRootContact = nullptr;
wchar_t m_name[MAX_HOSTNAME_LEN];
DWORD m_nameLen;
@@ -103,12 +102,13 @@ private: MCONTACT FindContact(in_addr addr, const char *nick, bool add_to_list, bool make_permanent, bool make_visible, u_int status = ID_STATUS_ONLINE);
void DeleteCache();
+ CTimer m_timer;
+ BOOL m_bChecking = false;
void StartChecking();
void StopChecking();
- static void __cdecl CheckProc(void *lpParameter);
- void Check();
+ void OnTimer(CTimer*);
- int m_handleId;
+ int m_handleId = 1;
int GetRandomProcId() { return m_handleId++; } // TODO: must create propper CRITICAL SECTION, cause there may be collisions
static void __cdecl LaunchExt(void *lpParameter);
@@ -133,15 +133,15 @@ private: void ParsePacket(TPacket& pak, u_char* buf, int len = 65536);
void SendPacketExt(TPacket& pak, u_long addr);
- bool m_UseHostName;
- u_long m_RequiredIp;
+ bool m_UseHostName = true;
+ u_long m_RequiredIp = 0;
HANDLE m_hookIcqMsgReq;
- char* m_amesAway;
- char* m_amesNa;
- char* m_amesOccupied;
- char* m_amesDnd;
- char* m_amesFfc;
+ char* m_amesAway = nullptr;
+ char* m_amesNa = nullptr;
+ char* m_amesOccupied = nullptr;
+ char* m_amesDnd = nullptr;
+ char* m_amesFfc = nullptr;
struct TFileConnection
{
@@ -187,7 +187,7 @@ private: void FileRemoveFromList(TFileConnection* conn);
mir_cs m_csFileConnectionList;
- TFileConnection* m_pFileConnectionList;
+ TFileConnection* m_pFileConnectionList = nullptr;
};
#endif //__mlan_h__
diff --git a/protocols/EmLanProto/src/stdafx.h b/protocols/EmLanProto/src/stdafx.h index 527a5b57c2..057dc15473 100644 --- a/protocols/EmLanProto/src/stdafx.h +++ b/protocols/EmLanProto/src/stdafx.h @@ -18,6 +18,7 @@ #include <m_database.h>
#include <m_langpack.h>
#include <m_clist.h>
+#include <m_gui.h>
#include "resource.h"
#include "version.h"
diff --git a/protocols/EmLanProto/src/version.h b/protocols/EmLanProto/src/version.h index 4a9cf125ce..4efa8d4214 100644 --- a/protocols/EmLanProto/src/version.h +++ b/protocols/EmLanProto/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 #define __MINOR_VERSION 1 #define __RELEASE_NUM 0 -#define __BUILD_NUM 2 +#define __BUILD_NUM 3 #include <stdver.h> #define __FILEVERSION_DWORD PLUGIN_MAKE_VERSION(__MAJOR_VERSION,__MINOR_VERSION,__RELEASE_NUM,__BUILD_NUM) |