From fd67a89688262e4e58337c33728488f5f29196a8 Mon Sep 17 00:00:00 2001 From: Szymon Tokarz Date: Wed, 17 Oct 2012 21:05:02 +0000 Subject: Tlen protocol adopted - code moved to trunk\protocols folder - update project files (based on rev: 27, 228, 204, 350, 279, 280, 1374, 278) - changed code organisation to c++ convention (based on rev: 1092, 503, 504) - changed code to Miranda NG convention (based on rev: 49, 54, 312, 401, 321, 358, 410, 441, 477, 483, 496, 507, 515, 644, 652, 743, 956, 1206, 667, 1040, 1590, 1857) - folders restructurization (based on rev: 1890) - code cleaning (based on rev: 270, 398, 409) - this commit includes adopted sources of tlen_czaty.dll (former mucc.dll) plugin witch is now deprecated and will be removed -- wsx22{at}o2.pl git-svn-id: http://svn.miranda-ng.org/main/trunk@1972 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tlen/src/jabber_ws.cpp | 160 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 protocols/Tlen/src/jabber_ws.cpp (limited to 'protocols/Tlen/src/jabber_ws.cpp') diff --git a/protocols/Tlen/src/jabber_ws.cpp b/protocols/Tlen/src/jabber_ws.cpp new file mode 100644 index 0000000000..c2cb853b02 --- /dev/null +++ b/protocols/Tlen/src/jabber_ws.cpp @@ -0,0 +1,160 @@ +/* + +Jabber Protocol Plugin for Miranda IM +Tlen Protocol Plugin for Miranda NG +Copyright (C) 2002-2004 Santithorn Bunchua +Copyright (C) 2004-2007 Piotr Piastucki + +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 "jabber.h" + +BOOL JabberWsInit(TlenProtocol *proto) +{ + NETLIBUSER nlu = {0}; + NETLIBUSERSETTINGS nlus = {0}; + TCHAR name[128]; + + + nlu.cbSize = sizeof(nlu); + nlu.flags = NUF_OUTGOING | NUF_INCOMING | NUF_HTTPCONNS | NUF_TCHAR; // | NUF_HTTPGATEWAY; + mir_sntprintf( name, SIZEOF(name), TranslateT("%s connection"), proto->m_tszUserName); + nlu.ptszDescriptiveName = name; + nlu.szSettingsModule = proto->m_szModuleName; + proto->hNetlibUser = (HANDLE) CallService(MS_NETLIB_REGISTERUSER, 0, (LPARAM) &nlu); + + nlu.flags = NUF_OUTGOING | NUF_INCOMING | NUF_NOOPTIONS | NUF_TCHAR; + mir_sntprintf( name, SIZEOF(name), TranslateT("%s SOCKS connection"), proto->m_tszUserName); + nlu.ptszDescriptiveName = name; + proto->hFileNetlibUser = (HANDLE) CallService(MS_NETLIB_REGISTERUSER, 0, (LPARAM) &nlu); + nlus.cbSize = sizeof(nlus); + nlus.useProxy = 0; + CallService(MS_NETLIB_SETUSERSETTINGS, (WPARAM) proto->hFileNetlibUser, (LPARAM) &nlus); + + return (proto->hNetlibUser != NULL)?TRUE:FALSE; +} + +void JabberWsUninit(TlenProtocol *proto) +{ + if (proto->hNetlibUser != NULL) Netlib_CloseHandle(proto->hNetlibUser); + if (proto->hFileNetlibUser != NULL) Netlib_CloseHandle(proto->hFileNetlibUser); + proto->hNetlibUser = NULL; + proto->hFileNetlibUser = NULL; +} + +JABBER_SOCKET JabberWsConnect(TlenProtocol *proto, char *host, WORD port) +{ + NETLIBOPENCONNECTION nloc = {0}; + + nloc.cbSize = sizeof(NETLIBOPENCONNECTION); //NETLIBOPENCONNECTION_V1_SIZE; + nloc.szHost = host; + nloc.wPort = port; + nloc.flags = 0; + nloc.timeout = 6; + return (HANDLE) CallService(MS_NETLIB_OPENCONNECTION, (WPARAM) proto->hNetlibUser, (LPARAM) &nloc); +} + + +int JabberWsSend(TlenProtocol *proto, JABBER_SOCKET s, char *data, int datalen) +{ + int len; + if ((len=Netlib_Send(s, data, datalen, /*MSG_NODUMP|*/MSG_DUMPASTEXT)) == SOCKET_ERROR || len != datalen) { + JabberLog(proto, "Netlib_Send() failed, error=%d", WSAGetLastError()); + return FALSE; + } + return TRUE; +} + +int JabberWsRecv(TlenProtocol *proto, JABBER_SOCKET s, char *data, long datalen) +{ + int ret; + ret = Netlib_Recv(s, data, datalen, /*MSG_NODUMP|*/MSG_DUMPASTEXT); + if (ret == SOCKET_ERROR) { + JabberLog(proto, "Netlib_Recv() failed, error=%d", WSAGetLastError()); + return 0; + } + if (ret == 0) { + JabberLog(proto, "Connection closed gracefully"); + return 0; + } + return ret; +} + + +int JabberWsSendAES(TlenProtocol *proto, char *data, int datalen, aes_context *aes_ctx, unsigned char *aes_iv) +{ + int len, sendlen; + unsigned char aes_input[16]; + unsigned char aes_output[256]; + if (proto->threadData == NULL) { + return FALSE; + } + while (datalen > 0) { + len = 0; + while (datalen > 0 && len < 256) { + int pad = datalen < 16 ? 16 - datalen : 0; + memcpy(aes_input, data, datalen < 16 ? datalen : 16); + memset(aes_input + 16 - pad, ' ', pad); + aes_crypt_cbc(aes_ctx, AES_ENCRYPT, 16, aes_iv, aes_input, aes_output + len); + datalen -= 16; + data += 16; + len += 16; + } + if (len > 0) { + JabberLog(proto, "Sending %d bytes", len); + if ((sendlen=Netlib_Send(proto->threadData->s, (char *)aes_output, len, MSG_NODUMP)) == SOCKET_ERROR || len != sendlen) { + JabberLog(proto, "Netlib_Send() failed, error=%d", WSAGetLastError()); + return FALSE; + } + } + } + return TRUE; +} + +int JabberWsRecvAES(TlenProtocol *proto, char *data, long datalen, aes_context *aes_ctx, unsigned char *aes_iv) +{ + int ret, len = 0, maxlen = datalen; + unsigned char aes_input[16]; + unsigned char *aes_output = (unsigned char *)data; + if (proto->threadData == NULL) { + return 0; + } + for (maxlen = maxlen & ~0xF; maxlen != 0; maxlen = maxlen & 0xF) { + ret = Netlib_Recv(proto->threadData->s, data, maxlen, MSG_NODUMP); + if (ret == SOCKET_ERROR) { + JabberLog(proto, "Netlib_Recv() failed, error=%d", WSAGetLastError()); + return 0; + } + if (ret == 0) { + JabberLog(proto, "Connection closed gracefully"); + return 0; + } + data += ret; + len += ret; + maxlen -= ret; + } + + ret = len; + while (len > 15) { + memcpy(aes_input, aes_output, 16); + aes_crypt_cbc(aes_ctx, AES_DECRYPT, 16, aes_iv, aes_input, aes_output); + aes_output += 16; + len -= 16; + } + return ret; +} + -- cgit v1.2.3