From c4ffab658f422e84259a0a9769f0003b33a013bd Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sat, 7 May 2022 15:00:02 +0300 Subject: Netlib_OpenConnection - finally old stupid structure NETLIBOPENCONNECTION died and converted into a set of parameters + code cleaning --- protocols/Facebook/src/server.cpp | 6 +-- protocols/IRCG/src/irclib.cpp | 19 ++------ protocols/JabberG/jabber.vcxproj | 1 - protocols/JabberG/jabber.vcxproj.filters | 3 -- protocols/JabberG/src/jabber_byte.cpp | 16 +------ protocols/JabberG/src/jabber_file.cpp | 7 +-- protocols/JabberG/src/jabber_proto.cpp | 8 +++- protocols/JabberG/src/jabber_proto.h | 13 ++---- protocols/JabberG/src/jabber_thread.cpp | 48 ++++++++++--------- protocols/JabberG/src/jabber_ws.cpp | 72 ----------------------------- protocols/JabberG/src/jabber_zstream.cpp | 8 ++-- protocols/JabberG/src/stdafx.h | 4 +- protocols/Sametime/src/sametime_session.cpp | 14 +----- protocols/YAMN/src/proto/netlib.cpp | 5 +- 14 files changed, 55 insertions(+), 169 deletions(-) delete mode 100644 protocols/JabberG/src/jabber_ws.cpp (limited to 'protocols') diff --git a/protocols/Facebook/src/server.cpp b/protocols/Facebook/src/server.cpp index caffa475a7..5b0a15ee13 100644 --- a/protocols/Facebook/src/server.cpp +++ b/protocols/Facebook/src/server.cpp @@ -402,11 +402,7 @@ LBL_Begin: } // connect to MQTT server - NETLIBOPENCONNECTION nloc = {}; - nloc.szHost = "mqtt.facebook.com"; - nloc.wPort = 443; - nloc.flags = NLOCF_SSL | NLOCF_V2; - m_mqttConn = Netlib_OpenConnection(m_hNetlibUser, &nloc); + m_mqttConn = Netlib_OpenConnection(m_hNetlibUser, "mqtt.facebook.com", 443, 0, NLOCF_SSL); if (m_mqttConn == nullptr) { debugLogA("connection failed, exiting"); ConnectionFailed(); diff --git a/protocols/IRCG/src/irclib.cpp b/protocols/IRCG/src/irclib.cpp index 4e17358632..b36e1b37e7 100644 --- a/protocols/IRCG/src/irclib.cpp +++ b/protocols/IRCG/src/irclib.cpp @@ -194,10 +194,7 @@ bool CIrcProto::Connect(const CIrcSessionInfo& info) { codepage = m_codepage; - NETLIBOPENCONNECTION ncon = {}; - ncon.szHost = info.sServer.c_str(); - ncon.wPort = info.iPort; - con = Netlib_OpenConnection(m_hNetlibUser, &ncon); + con = Netlib_OpenConnection(m_hNetlibUser, info.sServer, info.iPort); if (con == nullptr) { wchar_t szTemp[300]; mir_snwprintf(szTemp, L"%c5%s %c%s%c (%S: %u).", irc::COLOR, TranslateT("Failed to connect to"), irc::BOLD, m_tszUserName, irc::BOLD, m_sessionInfo.sServer.c_str(), m_sessionInfo.iPort); @@ -856,12 +853,8 @@ int CDccSession::SetupConnection() // If a remote computer initiates a chat session this is used to connect to the remote computer (user already accepted at this point). // also used for connecting to a remote computer for remote file transfers - if (di->iType == DCC_CHAT && !di->bSender || di->iType == DCC_SEND && di->bSender && di->bReverse) { - NETLIBOPENCONNECTION ncon = {}; - ncon.szHost = ConvertIntegerToIP(di->dwAdr); - ncon.wPort = (uint16_t)di->iPort; - con = Netlib_OpenConnection(m_proto->hNetlibDCC, &ncon); - } + if (di->iType == DCC_CHAT && !di->bSender || di->iType == DCC_SEND && di->bSender && di->bReverse) + con = Netlib_OpenConnection(m_proto->hNetlibDCC, ConvertIntegerToIP(di->dwAdr), di->iPort); // If a remote computer initiates a filetransfer this is used to connect to that computer (the user has chosen to accept but it is possible the file exists/needs to be resumed etc still) if (di->iType == DCC_SEND && !di->bSender) { @@ -951,11 +944,7 @@ int CDccSession::SetupConnection() } // connect to the remote computer from which you are receiving the file (now all actions to take (resume/overwrite etc) have been decided - NETLIBOPENCONNECTION ncon = {}; - ncon.szHost = ConvertIntegerToIP(di->dwAdr); - ncon.wPort = (uint16_t)di->iPort; - - con = Netlib_OpenConnection(m_proto->hNetlibDCC, &ncon); + con = Netlib_OpenConnection(m_proto->hNetlibDCC, ConvertIntegerToIP(di->dwAdr), di->iPort); } // if for some reason the plugin has failed to connect to the remote computer the object is destroyed. diff --git a/protocols/JabberG/jabber.vcxproj b/protocols/JabberG/jabber.vcxproj index 7dc563d45a..632ea83ce6 100644 --- a/protocols/JabberG/jabber.vcxproj +++ b/protocols/JabberG/jabber.vcxproj @@ -76,7 +76,6 @@ - diff --git a/protocols/JabberG/jabber.vcxproj.filters b/protocols/JabberG/jabber.vcxproj.filters index b7205778b0..8f8f5a9e4e 100644 --- a/protocols/JabberG/jabber.vcxproj.filters +++ b/protocols/JabberG/jabber.vcxproj.filters @@ -149,9 +149,6 @@ Source Files - - Source Files - Source Files diff --git a/protocols/JabberG/src/jabber_byte.cpp b/protocols/JabberG/src/jabber_byte.cpp index 48c3c02463..685174347d 100644 --- a/protocols/JabberG/src/jabber_byte.cpp +++ b/protocols/JabberG/src/jabber_byte.cpp @@ -438,15 +438,9 @@ void CJabberProto::ByteSendViaProxy(JABBER_BYTE_TRANSFER *jbt) char *szPort = jbt->szProxyPort; char *szHost = jbt->szProxyHost; - uint16_t port = (uint16_t)atoi(szPort); replaceStr(jbt->streamhostJID, jbt->szProxyJid); - NETLIBOPENCONNECTION nloc = {}; - nloc.szHost = szHost; - nloc.wPort = port; - - HNETLIBCONN hConn = Netlib_OpenConnection(m_hNetlibUser, &nloc); - mir_free((void*)nloc.szHost); + HNETLIBCONN hConn = Netlib_OpenConnection(m_hNetlibUser, szHost, atoi(szPort)); if (hConn != nullptr) { jbt->hConn = hConn; @@ -610,13 +604,7 @@ void __cdecl CJabberProto::ByteReceiveThread(JABBER_BYTE_TRANSFER *jbt) replaceStr(jbt->streamhostJID, str); debugLogA("bytestream_recv connecting to %s:%d", szHost, port); - NETLIBOPENCONNECTION nloc = { 0 }; - nloc.szHost = mir_strdup(szHost); - nloc.wPort = port; - - HNETLIBCONN hConn = Netlib_OpenConnection(m_hNetlibUser, &nloc); - mir_free((void*)nloc.szHost); - + HNETLIBCONN hConn = Netlib_OpenConnection(m_hNetlibUser, szHost, port); if (hConn == nullptr) { debugLogA("bytestream_recv_connection connection failed (%d), try next streamhost", WSAGetLastError()); continue; diff --git a/protocols/JabberG/src/jabber_file.cpp b/protocols/JabberG/src/jabber_file.cpp index f7d0cd0aac..b01b32f32a 100644 --- a/protocols/JabberG/src/jabber_file.cpp +++ b/protocols/JabberG/src/jabber_file.cpp @@ -36,10 +36,7 @@ void __cdecl CJabberProto::FileReceiveThread(filetransfer *ft) ft->type = FT_OOB; - NETLIBOPENCONNECTION nloc = {}; - nloc.szHost = ft->httpHostName; - nloc.wPort = ft->httpPort; - info.s = Netlib_OpenConnection(m_hNetlibUser, &nloc); + info.s = Netlib_OpenConnection(m_hNetlibUser, ft->httpHostName, ft->httpPort); if (info.s == nullptr) { debugLogA("Connection failed (%d), thread ended", WSAGetLastError()); ProtoBroadcastAck(ft->std.hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft); @@ -401,7 +398,7 @@ int CJabberProto::FileSendParse(HNETLIBCONN s, filetransfer *ft, char* buffer, i char fileBuffer[2048]; int bytes = mir_snprintf(fileBuffer, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n", _filelength(fileId)); - WsSend(s, fileBuffer, bytes, MSG_DUMPASTEXT); + Netlib_Send(s, fileBuffer, bytes, MSG_DUMPASTEXT); ft->std.flags |= PFTS_SENDING; ft->std.currentFileProgress = 0; diff --git a/protocols/JabberG/src/jabber_proto.cpp b/protocols/JabberG/src/jabber_proto.cpp index c2dc2d50e5..fb6fee3ce1 100644 --- a/protocols/JabberG/src/jabber_proto.cpp +++ b/protocols/JabberG/src/jabber_proto.cpp @@ -204,7 +204,6 @@ CJabberProto::CJabberProto(const char *aProtoName, const wchar_t *aUserName) : IconsInit(); InitPopups(); GlobalMenuInit(); - WsInit(); m_pepServices.insert(new CPepMood(this)); m_pepServices.insert(new CPepActivity(this)); @@ -214,6 +213,13 @@ CJabberProto::CJabberProto(const char *aProtoName, const wchar_t *aUserName) : OmemoInitDevice(); } + // network initialization + NETLIBUSER nlu = {}; + nlu.flags = NUF_OUTGOING | NUF_INCOMING | NUF_HTTPCONNS | NUF_UNICODE; + nlu.szDescriptiveName.w = m_tszUserName; + nlu.szSettingsModule = m_szModuleName; + m_hNetlibUser = Netlib_RegisterUser(&nlu); + // group chats GCREGISTER gcr = {}; gcr.dwFlags = GC_TYPNOTIF | GC_CHANMGR; diff --git a/protocols/JabberG/src/jabber_proto.h b/protocols/JabberG/src/jabber_proto.h index 823f17feff..9d87dbc775 100644 --- a/protocols/JabberG/src/jabber_proto.h +++ b/protocols/JabberG/src/jabber_proto.h @@ -76,6 +76,8 @@ struct CChatMark struct CJabberProto : public PROTO, public IJabberInterface { + friend struct ThreadData; + class CJabberProtoImpl { friend struct CJabberProto; @@ -240,6 +242,7 @@ struct CJabberProto : public PROTO, public IJabberInterface CMOption m_iConnectionKeepAliveTimeout; PVOID m_sslCtx; + mir_cs m_csSocket; // protects i/o operations HANDLE m_hThreadHandle; @@ -883,14 +886,6 @@ struct CJabberProto : public PROTO, public IJabberInterface void SetServerVcard(BOOL bPhotoChanged, wchar_t* szPhotoFileName); void SaveVcardToDB(HWND hwndPage, int iPage); - //---- jabber_ws.c ------------------------------------------------- - - HNETLIBCONN WsConnect(char* host, uint16_t port); - - bool WsInit(void); - int WsSend(HNETLIBCONN s, char* data, int datalen, int flags); - int WsRecv(HNETLIBCONN s, char* data, long datalen, int flags); - //---- jabber_xml.c ------------------------------------------------------------------ void OnConsoleProcessXml(const TiXmlElement *node, uint32_t flags); @@ -929,8 +924,6 @@ struct CJabberProto : public PROTO, public IJabberInterface private: char *m_szXmlStreamToBeInitialized; - uint32_t m_lastTicks; - HANDLE m_hPopupClass; LONG m_nSerial; diff --git a/protocols/JabberG/src/jabber_thread.cpp b/protocols/JabberG/src/jabber_thread.cpp index dcd5e5d6b7..0dbf3a1ba9 100644 --- a/protocols/JabberG/src/jabber_thread.cpp +++ b/protocols/JabberG/src/jabber_thread.cpp @@ -141,16 +141,15 @@ void CJabberProto::JLoginFailed(int errorCode) void CJabberProto::CheckKeepAlive() { - if (GetTickCount() - m_lastTicks < m_iConnectionKeepAliveInterval) - return; - - if (m_bKeepAlive && m_ThreadInfo) { - if (m_ThreadInfo->jabberServerCaps & JABBER_CAPS_PING) { - CJabberIqInfo *pInfo = AddIQ(&CJabberProto::OnPingReply, JABBER_IQ_TYPE_GET, nullptr, this); - pInfo->SetTimeout(m_iConnectionKeepAliveTimeout); - m_ThreadInfo->send(XmlNodeIq(pInfo) << XATTR("from", m_ThreadInfo->fullJID) << XCHILDNS("ping", JABBER_FEAT_PING)); + if (m_ThreadInfo) { + if (m_bKeepAlive && ::GetTickCount() - m_ThreadInfo->lastWriteTime < m_iConnectionKeepAliveInterval) { + if (m_ThreadInfo->jabberServerCaps & JABBER_CAPS_PING) { + CJabberIqInfo *pInfo = AddIQ(&CJabberProto::OnPingReply, JABBER_IQ_TYPE_GET, nullptr, this); + pInfo->SetTimeout(m_iConnectionKeepAliveTimeout); + m_ThreadInfo->send(XmlNodeIq(pInfo) << XATTR("from", m_ThreadInfo->fullJID) << XCHILDNS("ping", JABBER_FEAT_PING)); + } + else m_ThreadInfo->send(" \t "); } - else m_ThreadInfo->send(" \t "); } if (m_bEnableStreamMgmt) @@ -192,7 +191,7 @@ void ThreadData::xmpp_client_query(void) char* dnsHost = it->pNameTarget; proto->debugLogA("%s%s resolved to %s:%d", "_xmpp-client._tcp.", conn.server, dnsHost, dnsPort); - s = proto->WsConnect(dnsHost, dnsPort); + s = Netlib_OpenConnection(proto->m_hNetlibUser, dnsHost, dnsPort); if (s) { strncpy_s(conn.manualHost, dnsHost, _TRUNCATE); conn.port = dnsPort; @@ -375,10 +374,10 @@ LBL_FatalError: info.xmpp_client_query(); if (info.s == nullptr) { strncpy_s(info.conn.manualHost, info.conn.server, _TRUNCATE); - info.s = WsConnect(info.conn.manualHost, info.conn.port); + info.s = Netlib_OpenConnection(m_hNetlibUser, info.conn.manualHost, info.conn.port); } } - else info.s = WsConnect(info.conn.manualHost, info.conn.port); + else info.s = Netlib_OpenConnection(m_hNetlibUser, info.conn.manualHost, info.conn.port); debugLogA("Thread type=%d server='%s' port='%d'", info.bIsReg, info.conn.manualHost, info.conn.port); if (info.s == nullptr) { @@ -424,7 +423,7 @@ LBL_FatalError: debugLogA("Entering main recv loop"); int datalen = 0; - // cache values + // main socket reading cycle for (;;) { int recvResult = info.recv(info.buffer + datalen, jabberNetworkBufferSize - datalen); debugLogA("recvResult = %d", recvResult); @@ -1958,7 +1957,7 @@ ThreadData::ThreadData(CJabberProto *_pro, JABBER_CONN_DATA *param) resolveID = -1; proto = _pro; - iomutex = CreateMutex(nullptr, FALSE, nullptr); + lastWriteTime = ::GetTickCount(); if (param != nullptr) { bIsReg = true; @@ -1974,8 +1973,6 @@ ThreadData::~ThreadData() mir_free(gssapiHostName); mir_free(zRecvData); mir_free(buffer); - - CloseHandle(iomutex); } void ThreadData::close(void) @@ -1997,10 +1994,20 @@ int ThreadData::recv(char* buf, size_t len) if (this == nullptr) return 0; + // this select() is still required because shitty openssl is not thread safe + if (zRecvReady) { + NETLIBSELECT nls = {}; + nls.dwTimeout = INFINITE; + nls.hReadConns[0] = s; + int nSelRes = Netlib_Select(&nls); + if (nSelRes == SOCKET_ERROR) // error + return SOCKET_ERROR; + } + if (useZlib) return zlibRecv(buf, (long)len); - return proto->WsRecv(s, buf, (long)len, MSG_DUMPASTEXT); + return Netlib_Recv(s, buf, (long)len, MSG_DUMPASTEXT); } int ThreadData::send(char* buf, int bufsize) @@ -2008,22 +2015,21 @@ int ThreadData::send(char* buf, int bufsize) if (this == nullptr) return 0; + lastWriteTime = ::GetTickCount(); if (bufsize == -1) bufsize = (int)mir_strlen(buf); - WaitForSingleObject(iomutex, 6000); + mir_cslock lck(proto->m_csSocket); int result; if (useZlib) result = zlibSend(buf, bufsize); else - result = proto->WsSend(s, buf, bufsize, MSG_DUMPASTEXT); + result = Netlib_Send(s, buf, bufsize, MSG_DUMPASTEXT); if (result == SOCKET_ERROR) close(); - ReleaseMutex(iomutex); - return result; } diff --git a/protocols/JabberG/src/jabber_ws.cpp b/protocols/JabberG/src/jabber_ws.cpp deleted file mode 100644 index 3f70f1d6c1..0000000000 --- a/protocols/JabberG/src/jabber_ws.cpp +++ /dev/null @@ -1,72 +0,0 @@ -/* - -Jabber Protocol Plugin for Miranda NG - -Copyright (c) 2002-04 Santithorn Bunchua -Copyright (c) 2005-12 George Hazan -Copyright (C) 2012-22 Miranda NG team - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -*/ - -#include "stdafx.h" - -bool CJabberProto::WsInit(void) -{ - m_lastTicks = ::GetTickCount(); - - - NETLIBUSER nlu = {}; - nlu.flags = NUF_OUTGOING | NUF_INCOMING | NUF_HTTPCONNS | NUF_UNICODE; - nlu.szDescriptiveName.w = m_tszUserName; - nlu.szSettingsModule = m_szModuleName; - m_hNetlibUser = Netlib_RegisterUser(&nlu); - return m_hNetlibUser != nullptr; -} - -HNETLIBCONN CJabberProto::WsConnect(char* host, uint16_t port) -{ - NETLIBOPENCONNECTION nloc = {}; - nloc.szHost = host; - nloc.wPort = port; - nloc.timeout = 6; - return Netlib_OpenConnection(m_hNetlibUser, &nloc); -} - -int CJabberProto::WsSend(HNETLIBCONN hConn, char* data, int datalen, int flags) -{ - m_lastTicks = ::GetTickCount(); - int ret = Netlib_Send(hConn, data, datalen, flags); - if (ret == SOCKET_ERROR || ret != datalen) { - debugLogA("Netlib_Send() failed, error=%d", WSAGetLastError()); - return SOCKET_ERROR; - } - return ret; -} - -int CJabberProto::WsRecv(HNETLIBCONN hConn, char* data, long datalen, int flags) -{ - int ret = Netlib_Recv(hConn, data, datalen, flags); - if (ret == SOCKET_ERROR) { - debugLogA("Netlib_Recv() failed, error=%d", WSAGetLastError()); - return 0; - } - if (ret == 0) { - debugLogA("Connection closed gracefully"); - return 0; - } - return ret; -} diff --git a/protocols/JabberG/src/jabber_zstream.cpp b/protocols/JabberG/src/jabber_zstream.cpp index ec3a7ca3b7..0931e4fc34 100644 --- a/protocols/JabberG/src/jabber_zstream.cpp +++ b/protocols/JabberG/src/jabber_zstream.cpp @@ -72,7 +72,7 @@ int ThreadData::zlibSend(char* data, int datalen) } int send_datalen = ZLIB_CHUNK_SIZE - zStreamOut.avail_out; - int len = proto->WsSend(s, send_data, send_datalen, MSG_NODUMP); + int len = Netlib_Send(s, send_data, send_datalen, MSG_NODUMP); if (len == SOCKET_ERROR || len != send_datalen) { proto->debugLogA("Netlib_Send() failed, error=%d", WSAGetLastError()); return SOCKET_ERROR; @@ -92,13 +92,15 @@ int ThreadData::zlibRecv(char* data, long datalen) { if (zRecvReady) { retry: - zRecvDatalen = proto->WsRecv(s, zRecvData, ZLIB_CHUNK_SIZE, MSG_NODUMP); + zRecvDatalen = Netlib_Recv(s, zRecvData, ZLIB_CHUNK_SIZE, MSG_NODUMP); if (zRecvDatalen == SOCKET_ERROR) { proto->debugLogA("Netlib_Recv() failed, error=%d", WSAGetLastError()); return SOCKET_ERROR; } - if (zRecvDatalen == 0) + if (zRecvDatalen == 0) { + proto->debugLogA("Connection closed gracefully"); return 0; + } zStreamIn.avail_in = zRecvDatalen; zStreamIn.next_in = (Bytef*)zRecvData; diff --git a/protocols/JabberG/src/stdafx.h b/protocols/JabberG/src/stdafx.h index 8d56126b59..cf41737088 100644 --- a/protocols/JabberG/src/stdafx.h +++ b/protocols/JabberG/src/stdafx.h @@ -339,10 +339,10 @@ struct ThreadData ptrA szStreamId; char* buffer; + uint32_t lastWriteTime; // in ticks // network support HNETLIBCONN s; - HANDLE iomutex; // protects i/o operations CJabberProto *proto; // XEP-0138 (Compression support) @@ -367,7 +367,7 @@ struct ThreadData bool bIsReg; bool reg_done, bIsSessionAvailable; bool bBookmarksLoaded; - uint32_t dwLoginRqs; + uint32_t dwLoginRqs; // connection & login data JABBER_CONN_DATA conn; diff --git a/protocols/Sametime/src/sametime_session.cpp b/protocols/Sametime/src/sametime_session.cpp index 84d8ed15d2..18475f0ea8 100644 --- a/protocols/Sametime/src/sametime_session.cpp +++ b/protocols/Sametime/src/sametime_session.cpp @@ -358,11 +358,6 @@ void __cdecl CSametimeProto::KeepAliveThread(void*) return; } -int waitcallback(unsigned int*) -{ - return continue_connect ? 1 : 0; -} - void __cdecl CSametimeProto::SessionThread(LPVOID) { debugLogW(L"SessionThread() start"); @@ -372,14 +367,7 @@ void __cdecl CSametimeProto::SessionThread(LPVOID) BroadcastNewStatus(ID_STATUS_CONNECTING); // setup - NETLIBOPENCONNECTION conn_data = { 0 }; - conn_data.flags = NLOCF_V2; - conn_data.szHost = options.server_name; - conn_data.wPort = options.port; - conn_data.timeout = 20; - conn_data.waitcallback = waitcallback; - server_connection = Netlib_OpenConnection(m_hNetlibUser, &conn_data); - + server_connection = Netlib_OpenConnection(m_hNetlibUser, options.server_name, options.port, 20); if (!server_connection) { BroadcastNewStatus(ID_STATUS_OFFLINE); diff --git a/protocols/YAMN/src/proto/netlib.cpp b/protocols/YAMN/src/proto/netlib.cpp index 34366fa734..29222dd980 100644 --- a/protocols/YAMN/src/proto/netlib.cpp +++ b/protocols/YAMN/src/proto/netlib.cpp @@ -88,10 +88,7 @@ void CNLClient::Connect(const char* servername, const int port) throw(DWORD) DebugLog(CommFile, "\n"); #endif try { - NETLIBOPENCONNECTION nloc = {}; - nloc.szHost = servername; - nloc.wPort = port; - if (nullptr == (hConnection = Netlib_OpenConnection(hNetlibUser, &nloc))) { + if (nullptr == (hConnection = Netlib_OpenConnection(hNetlibUser, servername, port))) { SystemError = WSAGetLastError(); throw NetworkError = (uint32_t)ENL_CONNECT; } -- cgit v1.2.3