implement size header

This commit is contained in:
Lion Kortlepel
2020-11-05 23:54:41 +01:00
parent ad91c65b55
commit 6ca0740a98
+25 -12
View File
@@ -14,6 +14,8 @@
#include <string> #include <string>
#include <thread> #include <thread>
#include <vector> #include <vector>
#include <cstring>
#include <algorithm>
namespace fs = std::experimental::filesystem; namespace fs = std::experimental::filesystem;
std::string ListOfMods; std::string ListOfMods;
@@ -29,19 +31,17 @@ std::vector<std::string> Split(const std::string& String,const std::string& deli
if(!s.empty())Val.push_back(s); if(!s.empty())Val.push_back(s);
return Val; return Val;
} }
void STCPSendRaw(SOCKET socket, const std::vector<char>& Data) {
void STCPSend(SOCKET socket,const std::string&Data){ if (socket == -1) {
if(socket == -1){
Terminate = true; Terminate = true;
return; return;
} }
int BytesSent = send(socket, Data.c_str(), int(Data.length())+1, 0); int BytesSent = send(socket, Data.data(), int(Data.size()), 0);
if (BytesSent == 0){ if (BytesSent == 0) {
debug(Sec("(TCP) Connection closing...")); debug(Sec("(TCP) Connection closing..."));
Terminate = true; Terminate = true;
return; return;
} } else if (BytesSent < 0) {
else if (BytesSent < 0) {
debug(Sec("(TCP) send failed with error: ") + std::to_string(WSAGetLastError())); debug(Sec("(TCP) send failed with error: ") + std::to_string(WSAGetLastError()));
closesocket(socket); closesocket(socket);
Terminate = true; Terminate = true;
@@ -49,6 +49,10 @@ void STCPSend(SOCKET socket,const std::string&Data){
} }
std::this_thread::sleep_for(std::chrono::milliseconds(200)); std::this_thread::sleep_for(std::chrono::milliseconds(200));
} }
void STCPSend(SOCKET socket, const std::string& Data) {
STCPSendRaw(socket, std::vector<char>(Data.begin(), Data.begin() + Data.size() + 1));
}
std::pair<char*,size_t> STCPRecv(SOCKET socket){ std::pair<char*,size_t> STCPRecv(SOCKET socket){
char buf[64000]; char buf[64000];
int len = 64000; int len = 64000;
@@ -112,6 +116,15 @@ void Check(Hold* S){
} }
} }
} }
std::vector<char> PrependSize(const std::string& str) {
uint32_t Size = htonl(uint32_t(str.size()) + 1);
std::vector<char> result;
// +1 for \0, +4 for the size
result.resize(str.size() + 1 + 4);
memcpy(result.data(), &Size, 4);
memcpy(result.data() + 4, str.data(), str.size() + 1);
return result;
}
std::string HandShake(SOCKET Sock,Hold*S,RSA*LKey){ std::string HandShake(SOCKET Sock,Hold*S,RSA*LKey){
S->TCPSock = Sock; S->TCPSock = Sock;
std::thread Timeout(Check,S); std::thread Timeout(Check,S);
@@ -121,7 +134,7 @@ std::string HandShake(SOCKET Sock,Hold*S,RSA*LKey){
std::string msg(Res.first,Res.second); std::string msg(Res.first,Res.second);
Parse(msg); Parse(msg);
if(N != 0 && E != 0) { if(N != 0 && E != 0) {
STCPSend(Sock,GenerateM(LKey)); STCPSendRaw(Sock,PrependSize(GenerateM(LKey)));
Res = STCPRecv(Sock); Res = STCPRecv(Sock);
msg = std::string(Res.first,Res.second); msg = std::string(Res.first,Res.second);
if(RSA_D(msg,LKey->d,LKey->n) != "HC"){ if(RSA_D(msg,LKey->d,LKey->n) != "HC"){
@@ -137,8 +150,8 @@ std::string HandShake(SOCKET Sock,Hold*S,RSA*LKey){
} }
msg = RSA_E("NR" + GetDName() + ":" + GetDID(),E,N); msg = RSA_E("NR" + GetDName() + ":" + GetDID(),E,N);
if(!msg.empty()) { if(!msg.empty()) {
STCPSend(Sock,msg); STCPSendRaw(Sock, PrependSize(msg));
STCPSend(Sock, RSA_E("VC" + GetVer(),E,N)); STCPSendRaw(Sock, PrependSize(RSA_E("VC" + GetVer(),E,N)));
Res = STCPRecv(Sock); Res = STCPRecv(Sock);
msg = Res.first; msg = Res.first;
} }
@@ -150,12 +163,12 @@ std::string HandShake(SOCKET Sock,Hold*S,RSA*LKey){
info(Sec("Terminated!")); info(Sec("Terminated!"));
return ""; return "";
} }
STCPSend(Sock,Sec("SR")); STCPSend(Sock,PrependSize(Sec("SR")));
Res = STCPRecv(Sock); Res = STCPRecv(Sock);
if(strlen(Res.first) == 0 || std::string(Res.first) == "-"){ if(strlen(Res.first) == 0 || std::string(Res.first) == "-"){
info(Sec("Didn't Receive any mods...")); info(Sec("Didn't Receive any mods..."));
ListOfMods = "-"; ListOfMods = "-";
STCPSend(Sock,Sec("Done")); STCPSend(Sock,PrependSize(Sec("Done")));
info(Sec("Done!")); info(Sec("Done!"));
return ""; return "";
} }