summaryrefslogtreecommitdiff
path: root/protocols/Tlen/src/tlen_p2p_old.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'protocols/Tlen/src/tlen_p2p_old.cpp')
-rw-r--r--protocols/Tlen/src/tlen_p2p_old.cpp522
1 files changed, 0 insertions, 522 deletions
diff --git a/protocols/Tlen/src/tlen_p2p_old.cpp b/protocols/Tlen/src/tlen_p2p_old.cpp
deleted file mode 100644
index d5a71a204e..0000000000
--- a/protocols/Tlen/src/tlen_p2p_old.cpp
+++ /dev/null
@@ -1,522 +0,0 @@
-/*
-
-Tlen Protocol Plugin for Miranda NG
-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 "stdafx.h"
-#include "tlen_p2p_old.h"
-#include <io.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include "tlen_list.h"
-
-
-void TlenP2PFreeFileTransfer(TLEN_FILE_TRANSFER *ft)
-{
- if (ft->jid) mir_free(ft->jid);
- if (ft->iqId) mir_free(ft->iqId);
- if (ft->id2) mir_free(ft->id2);
- if (ft->hostName) mir_free(ft->hostName);
- if (ft->localName) mir_free(ft->localName);
- if (ft->szSavePath) mir_free(ft->szSavePath);
- if (ft->szDescription) mir_free(ft->szDescription);
- if (ft->filesSize) mir_free(ft->filesSize);
- if (ft->files) {
- for (int i=0; i<ft->fileCount; i++) {
- if (ft->files[i]) mir_free(ft->files[i]);
- }
- mir_free(ft->files);
- }
- mir_free(ft);
-}
-
-TLEN_FILE_PACKET *TlenP2PPacketCreate(int datalen)
-{
- TLEN_FILE_PACKET *packet=(TLEN_FILE_PACKET *) mir_alloc(sizeof(TLEN_FILE_PACKET));
- if (packet == nullptr)
- return nullptr;
- packet->packet = nullptr;
- if (datalen > 0) {
- if ((packet->packet=(char *) mir_alloc(datalen)) == nullptr) {
- mir_free(packet);
- return nullptr;
- }
- }
- packet->maxDataLen = datalen;
- packet->type=0;
- packet->len=0;
- return packet;
-}
-
-void TlenP2PPacketFree(TLEN_FILE_PACKET *packet)
-{
- if (packet != nullptr) {
- if (packet->packet != nullptr)
- mir_free(packet->packet);
- mir_free(packet);
- }
-}
-
-void TlenP2PPacketSetType(TLEN_FILE_PACKET *packet, DWORD type)
-{
- if (packet != nullptr) {
- packet->type = type;
- }
-}
-
-void TlenP2PPacketSetLen(TLEN_FILE_PACKET *packet, DWORD len)
-{
- if (packet != nullptr) {
- packet->len = len;
- }
-}
-
-void TlenP2PPacketPackDword(TLEN_FILE_PACKET *packet, DWORD data)
-{
- if (packet != nullptr && packet->packet != nullptr) {
- if (packet->len + sizeof(DWORD) <= packet->maxDataLen) {
- (*((DWORD*)((packet->packet)+(packet->len)))) = data;
- packet->len += sizeof(DWORD);
- }
- }
-}
-
-void TlenP2PPacketPackBuffer(TLEN_FILE_PACKET *packet, char *buffer, int len)
-{
- if (packet != nullptr && packet->packet != nullptr) {
- if (packet->len + len <= packet->maxDataLen) {
- memcpy((packet->packet)+(packet->len), buffer, len);
- packet->len += len;
- }
- }
-}
-
-void TlenP2PInit() {
-}
-
-void TlenP2PUninit() {
-}
-
-int TlenP2PPacketSend(HNETLIBCONN s, TLEN_FILE_PACKET *packet)
-{
- DWORD sendResult;
- if (packet != nullptr && packet->packet != nullptr) {
- Netlib_Send(s, (char *)&packet->type, 4, MSG_NODUMP);
- Netlib_Send(s, (char *)&packet->len, 4, MSG_NODUMP);
- sendResult=Netlib_Send(s, packet->packet, packet->len, MSG_NODUMP);
- if (sendResult == SOCKET_ERROR || sendResult != packet->len) return 0;
- }
- return 1;
-}
-
-TLEN_FILE_PACKET* TlenP2PPacketReceive(HNETLIBCONN s)
-{
- DWORD type, len, pos;
- DWORD recvResult = Netlib_Recv(s, (char *)&type, 4, MSG_NODUMP);
- if (recvResult == 0 || recvResult == SOCKET_ERROR) return nullptr;
- recvResult = Netlib_Recv(s, (char *)&len, 4, MSG_NODUMP);
- if (recvResult == 0 || recvResult == SOCKET_ERROR) return nullptr;
- TLEN_FILE_PACKET *packet = TlenP2PPacketCreate(len);
- TlenP2PPacketSetType(packet, type);
- TlenP2PPacketSetLen(packet, len);
- pos = 0;
- while (len > 0) {
- recvResult = Netlib_Recv(s, packet->packet+pos, len, MSG_NODUMP);
- if (recvResult == 0 || recvResult == SOCKET_ERROR) {
- TlenP2PPacketFree(packet);
- return nullptr;
- }
- len -= recvResult;
- pos += recvResult;
- }
- return packet;
-}
-
-void TlenP2PEstablishOutgoingConnection(TLEN_FILE_TRANSFER *ft, BOOL sendAck)
-{
- char str[300];
- TlenProtocol *proto = ft->proto;
-
- proto->debugLogA("Establishing outgoing connection.");
- ft->state = FT_ERROR;
- TLEN_FILE_PACKET *packet = TlenP2PPacketCreate(2*sizeof(DWORD) + 20);
- if (packet != nullptr) {
- TlenP2PPacketSetType(packet, TLEN_FILE_PACKET_CONNECTION_REQUEST);
- TlenP2PPacketPackDword(packet, 1);
- TlenP2PPacketPackDword(packet, (DWORD) atoi(ft->iqId));
- size_t srt_len = mir_snprintf(str, "%08X%s%d", atoi(ft->iqId), proto->threadData->username, atoi(ft->iqId));
- char *hash = TlenSha1(str, (int)srt_len);
- TlenP2PPacketPackBuffer(packet, hash, 20);
- mir_free(hash);
- TlenP2PPacketSend(ft->s, packet);
- TlenP2PPacketFree(packet);
- packet = TlenP2PPacketReceive(ft->s);
- if (packet != nullptr) {
- if (packet->type == TLEN_FILE_PACKET_CONNECTION_REQUEST_ACK) { // acknowledge
- if ((int)(*((DWORD*)packet->packet)) == atoi(ft->iqId)) {
- ft->state = FT_CONNECTING;
- if (sendAck) {
- ProtoBroadcastAck(proto->m_szModuleName, ft->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, ft, 0);
- }
- }
- }
- TlenP2PPacketFree(packet);
- }
- }
-}
-
-TLEN_FILE_TRANSFER* TlenP2PEstablishIncomingConnection(TlenProtocol *proto, HNETLIBCONN s, TLEN_LIST list, BOOL sendAck)
-{
- char str[300];
- DWORD iqId;
- // TYPE: 0x1
- // LEN:
- // (DWORD) 0x1
- // (DWORD) id
- // (BYTE) hash[20]
- TLEN_FILE_PACKET *packet = TlenP2PPacketReceive(s);
- if (packet == nullptr || packet->type != TLEN_FILE_PACKET_CONNECTION_REQUEST || packet->len<28) {
- if (packet != nullptr) {
- TlenP2PPacketFree(packet);
- }
- return nullptr;
- }
- iqId = *((DWORD *)(packet->packet+sizeof(DWORD)));
- int i = 0;
- TLEN_LIST_ITEM *item;
- while ((i=TlenListFindNext(proto, list, i)) >= 0) {
- item = TlenListGetItemPtrFromIndex(proto, i);
- if (item != nullptr) {
- mir_snprintf(str, "%d", iqId);
- if (!mir_strcmp(item->ft->iqId, str)) {
- char *hash, *nick;
- int j;
- nick = TlenNickFromJID(item->ft->jid);
- j = mir_snprintf(str, "%08X%s%d", iqId, nick, iqId);
- mir_free(nick);
- hash = TlenSha1(str, j);
- for (j=0;j<20;j++) {
- if (hash[j] != packet->packet[2*sizeof(DWORD)+j]) break;
- }
- mir_free(hash);
- if (j == 20) break;
- }
- }
- i++;
- }
- TlenP2PPacketFree(packet);
- if (i >=0) {
- if ((packet=TlenP2PPacketCreate(sizeof(DWORD))) != nullptr) {
- // Send connection establishment acknowledgement
- TlenP2PPacketSetType(packet, TLEN_FILE_PACKET_CONNECTION_REQUEST_ACK);
- TlenP2PPacketPackDword(packet, (DWORD) atoi(item->ft->iqId));
- TlenP2PPacketSend(s, packet);
- TlenP2PPacketFree(packet);
- item->ft->state = FT_CONNECTING;
- if (sendAck) {
- ProtoBroadcastAck(item->ft->proto->m_szModuleName, item->ft->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, item->ft, 0);
- }
- return item->ft;
- }
- }
- return nullptr;
-}
-
-static void __cdecl TlenFileBindSocks4Thread(TLEN_FILE_TRANSFER* ft)
-{
- BYTE buf[8];
-
-// TlenLog("Waiting for the file to be sent via SOCKS...");
- int status = Netlib_Recv(ft->s, (char*)buf, 8, MSG_NODUMP);
-// TlenLog("accepted connection !!!");
- if ( status == SOCKET_ERROR || status < 8 || buf[1] != 90) {
- status = 1;
- } else {
- status = 0;
- }
- if (!status) {
- ft->pfnNewConnectionV2(ft->s, 0, nullptr);
- } else {
- if (ft->state != FT_SWITCH) {
- ft->state = FT_ERROR;
- }
- }
- ft->proto->debugLogA("Closing connection for this file transfer...");
-// Netlib_CloseHandle(ft->s);
- if (ft->hFileEvent != nullptr)
- SetEvent(ft->hFileEvent);
-
-}
-static void __cdecl TlenFileBindSocks5Thread(TLEN_FILE_TRANSFER* ft)
-{
- BYTE buf[256];
-
-// TlenLog("Waiting for the file to be sent via SOCKS...");
- int status = Netlib_Recv(ft->s, (char*)buf, sizeof(buf), MSG_NODUMP);
-// TlenLog("accepted connection !!!");
- if ( status == SOCKET_ERROR || status < 7 || buf[1] != 0) {
- status = 1;
- } else {
- status = 0;
- }
- if (!status) {
- ft->pfnNewConnectionV2(ft->s, 0, nullptr);
- } else {
- if (ft->state != FT_SWITCH) {
- ft->state = FT_ERROR;
- }
- }
-// TlenLog("Closing connection for this file transfer...");
-// Netlib_CloseHandle(ft->s);
- if (ft->hFileEvent != nullptr)
- SetEvent(ft->hFileEvent);
-
-}
-
-static HNETLIBCONN TlenP2PBindSocks4(SOCKSBIND * sb, TLEN_FILE_TRANSFER *ft)
-{ //rfc1928
- int len;
- BYTE buf[256];
- int status;
- struct in_addr in;
-
- NETLIBOPENCONNECTION nloc = { sizeof(nloc) };
- nloc.szHost = sb->szHost;
- nloc.wPort = sb->wPort;
- HNETLIBCONN s = Netlib_OpenConnection(ft->proto->hFileNetlibUser, &nloc);
- if (s == nullptr) {
-// TlenLog("Connection failed (%d), thread ended", WSAGetLastError());
- return nullptr;
- }
- buf[0] = 4; //socks4
- buf[1] = 2; //2-bind, 1-connect
- *(PWORD)(buf+2) = htons(0); // port
- *(PDWORD)(buf+4) = INADDR_ANY;
- if (sb->useAuth) {
- mir_strcpy((char*)buf+8, sb->szUser);
- len = (int)mir_strlen(sb->szUser);
- } else {
- buf[8] = 0;
- len = 0;
- }
- len += 9;
- status = Netlib_Send(s, (char*)buf, len, MSG_NODUMP);
- if (status == SOCKET_ERROR || status < len) {
-// TlenLog("Send failed (%d), thread ended", WSAGetLastError());
- Netlib_CloseHandle(s);
- return nullptr;
- }
- status = Netlib_Recv(s, (char*)buf, 8, MSG_NODUMP);
- if (status == SOCKET_ERROR || status < 8 || buf[1] != 90) {
-// TlenLog("SOCKS4 negotiation failed");
- Netlib_CloseHandle(s);
- return nullptr;
- }
- status = Netlib_Recv(s, (char*)buf, sizeof(buf), MSG_NODUMP);
- if ( status == SOCKET_ERROR || status < 7 || buf[0] != 5 || buf[1] != 0) {
-// TlenLog("SOCKS5 request failed");
- Netlib_CloseHandle(s);
- return nullptr;
- }
- in.S_un.S_addr = *(PDWORD)(buf+4);
- strncpy(sb->szHost, inet_ntoa(in), sizeof(sb->szHost)-1);
- sb->wPort = htons(*(PWORD)(buf+2));
- ft->s = s;
- mir_forkthread(pThreadFunc(TlenFileBindSocks4Thread), ft);
- return s;
-}
-
-static HNETLIBCONN TlenP2PBindSocks5(SOCKSBIND * sb, TLEN_FILE_TRANSFER *ft)
-{ //rfc1928
- BYTE buf[512];
- int len, status;
- struct in_addr in;
-
- NETLIBOPENCONNECTION nloc = { sizeof(nloc) };
- nloc.szHost = sb->szHost;
- nloc.wPort = sb->wPort;
- HNETLIBCONN s = Netlib_OpenConnection(ft->proto->hFileNetlibUser, &nloc);
- if (s == nullptr) {
- ft->proto->debugLogA("Connection failed (%d), thread ended", WSAGetLastError());
- return nullptr;
- }
- buf[0] = 5; //yep, socks5
- buf[1] = 1; //one auth method
- buf[2] = sb->useAuth?2:0; // authorization
- status = Netlib_Send(s, (char*)buf, 3, MSG_NODUMP);
- if (status == SOCKET_ERROR || status < 3) {
- ft->proto->debugLogA("Send failed (%d), thread ended", WSAGetLastError());
- Netlib_CloseHandle(s);
- return nullptr;
- }
- status = Netlib_Recv(s, (char*)buf, 2, MSG_NODUMP);
- if (status == SOCKET_ERROR || status < 2 || (buf[1] != 0 && buf[1] != 2)) {
- ft->proto->debugLogA("SOCKS5 negotiation failed");
- Netlib_CloseHandle(s);
- return nullptr;
- }
- if (buf[1] == 2) { //rfc1929
- int nUserLen, nPassLen;
- PBYTE pAuthBuf;
-
- nUserLen = (int)mir_strlen(sb->szUser);
- nPassLen = (int)mir_strlen(sb->szPassword);
- pAuthBuf = (PBYTE)mir_alloc(3+nUserLen+nPassLen);
- pAuthBuf[0] = 1; //auth version
- pAuthBuf[1] = nUserLen;
- memcpy(pAuthBuf+2, sb->szUser, nUserLen);
- pAuthBuf[2+nUserLen]=nPassLen;
- memcpy(pAuthBuf+3+nUserLen,sb->szPassword,nPassLen);
- status = Netlib_Send(s, (char*)pAuthBuf, 3+nUserLen+nPassLen, MSG_NODUMP);
- mir_free(pAuthBuf);
- if (status == SOCKET_ERROR || status < 3 + nUserLen+nPassLen) {
- ft->proto->debugLogA("Send failed (%d), thread ended", WSAGetLastError());
- Netlib_CloseHandle(s);
- return nullptr;
- }
- status = Netlib_Recv(s, (char*)buf, sizeof(buf), MSG_NODUMP);
- if (status == SOCKET_ERROR || status < 2 || buf[1] != 0) {
- ft->proto->debugLogA("SOCKS5 sub-negotiation failed");
- Netlib_CloseHandle(s);
- return nullptr;
- }
- }
-
- { PBYTE pInit;
- int nHostLen=4;
- DWORD hostIP=INADDR_ANY;
- pInit=(PBYTE)mir_alloc(6+nHostLen);
- pInit[0]=5; //SOCKS5
- pInit[1]=2; //bind
- pInit[2]=0; //reserved
- pInit[3]=1;
- *(PDWORD)(pInit+4)=hostIP;
- *(PWORD)(pInit+4+nHostLen)=htons(0);
- status = Netlib_Send(s, (char*)pInit, 6+nHostLen, MSG_NODUMP);
- mir_free(pInit);
- if (status == SOCKET_ERROR || status < 6 + nHostLen) {
-// TlenLog("Send failed (%d), thread ended", WSAGetLastError());
- Netlib_CloseHandle(s);
- return nullptr;
- }
- }
- status = Netlib_Recv(s, (char*)buf, sizeof(buf), MSG_NODUMP);
- if ( status == SOCKET_ERROR || status < 7 || buf[0] != 5 || buf[1] != 0) {
-// TlenLog("SOCKS5 request failed");
- Netlib_CloseHandle(s);
- return nullptr;
- }
- if (buf[2] == 1) { // domain
- len = buf[4];
- memcpy(sb->szHost, buf+5, len);
- sb->szHost[len]=0;
- len += 4;
- } else { // ip address
- in.S_un.S_addr = *(PDWORD)(buf+4);
- mir_strcpy(sb->szHost, inet_ntoa(in));
- len = 8;
- }
- sb->wPort = htons(*(PWORD)(buf+len));
- ft->s = s;
-
- mir_forkthread(pThreadFunc(TlenFileBindSocks5Thread), ft);
- return s;
-}
-
-
-HNETLIBCONN TlenP2PListen(TLEN_FILE_TRANSFER *ft)
-{
- HNETLIBCONN s = nullptr;
- int useProxy;
- DBVARIANT dbv;
- SOCKSBIND sb;
- struct in_addr in;
- TlenProtocol *proto = ft->proto;
- useProxy=0;
- if (ft->localName != nullptr) mir_free(ft->localName);
- ft->localName = nullptr;
- ft->wPort = 0;
- if (db_get_b(NULL, proto->m_szModuleName, "UseFileProxy", FALSE)) {
- if (!db_get(NULL, proto->m_szModuleName, "FileProxyHost", &dbv)) {
- mir_strcpy(sb.szHost, dbv.pszVal);
- db_free(&dbv);
- sb.wPort = db_get_w(NULL, proto->m_szModuleName, "FileProxyPort", 0);
- sb.useAuth = FALSE;
- mir_strcpy(sb.szUser, "");
- mir_strcpy(sb.szPassword, "");
- if (db_get_b(NULL, proto->m_szModuleName, "FileProxyAuth", FALSE)) {
- sb.useAuth = TRUE;
- if (!db_get_s(NULL, proto->m_szModuleName, "FileProxyUsername", &dbv)) {
- strncpy(sb.szUser, dbv.pszVal, sizeof(sb.szUser)-1);
- db_free(&dbv);
- }
- if (!db_get_s(NULL, proto->m_szModuleName, "FileProxyPassword", &dbv)) {
- strncpy(sb.szPassword, dbv.pszVal, sizeof(sb.szPassword)-1);
- db_free(&dbv);
- }
- }
- switch (db_get_w(NULL, proto->m_szModuleName, "FileProxyType", 0)) {
- case 0: // forwarding
- useProxy = 1;
- break;
- case 1: // socks4
- s = TlenP2PBindSocks4(&sb, ft);
- useProxy = 2;
- break;
- case 2: // socks5
- s = TlenP2PBindSocks5(&sb, ft);
- useProxy = 2;
- break;
- }
- ft->localName = mir_strdup(sb.szHost);
- ft->wPort = sb.wPort;
- ft->wLocalPort = sb.wPort;
- }
- }
-
- NETLIBBIND nlb = {};
- if (useProxy<2) {
- nlb.pfnNewConnectionV2 = ft->pfnNewConnectionV2;
- nlb.wPort = 0; // Use user-specified incoming port ranges, if available
- nlb.pExtra = proto;
- ft->proto->debugLogA("Calling MS_NETLIB_BINDPORT");
- s = (HNETLIBCONN)Netlib_BindPort(ft->proto->m_hNetlibUser, &nlb);
- ft->proto->debugLogA("listening on %d",s);
- }
- if (useProxy == 0) {
- in.S_un.S_addr = htonl(nlb.dwExternalIP);
- ft->localName = mir_strdup(inet_ntoa(in));
- ft->wPort = nlb.wPort;
- ft->wLocalPort = nlb.wExPort;
- }
- if (s != nullptr) {
-// listenCount++;
- }
- return s;
-}
-
-void TlenP2PStopListening(HANDLE s) {
- if (s != nullptr) {
-// listenCount--;
-// if (listenCount <= 0) {
-// Netlib_CloseHandle(s);
-// }
- }
-}