Skip to content
Permalink
Browse files

Add new socket type.

  • Loading branch information...
john-preston committed Jul 8, 2019
1 parent 3cda267 commit 69b6b487382c12efc43d52f472cab5954ab850e2
@@ -19,6 +19,7 @@ extern "C" {
#include <openssl/modes.h>
#include <openssl/crypto.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
} // extern "C"

#ifdef small
@@ -445,6 +446,24 @@ inline bytes::vector Pbkdf2Sha512(
EVP_sha512());
}

inline bytes::vector HmacSha256(
bytes::const_span key,
bytes::const_span data) {
auto result = bytes::vector(kSha256Size);
auto length = unsigned int(kSha256Size);

HMAC(
EVP_sha256(),
key.data(),
key.size(),
reinterpret_cast<const unsigned char*>(data.data()),
data.size(),
reinterpret_cast<unsigned char*>(result.data()),
&length);

return result;
}

} // namespace openssl

namespace bytes {
@@ -224,7 +224,9 @@ bytes::const_span TcpConnection::Protocol::VersionD::readPacket(

auto TcpConnection::Protocol::Create(bytes::vector &&secret)
-> std::unique_ptr<Protocol> {
if (secret.size() == 17 && static_cast<uchar>(secret[0]) == 0xDD) {
if (secret.size() == 17
&& (static_cast<uchar>(secret[0]) == 0xDD
|| static_cast<uchar>(secret[0]) == 0xEE)) {

This comment has been minimized.

Copy link
@p1ratrulezzz
return std::make_unique<VersionD>(
bytes::make_vector(bytes::make_span(secret).subspan(1)));
} else if (secret.size() == 16) {
@@ -363,7 +365,7 @@ void TcpConnection::socketRead() {
TCP_LOG(("TCP Info: no bytes read, but bytes available was true..."));
break;
}
} while (_socket->isConnected() && _socket->bytesAvailable());
} while (_socket->isConnected() && _socket->hasBytesAvailable());
}

mtpBuffer TcpConnection::parsePacket(bytes::const_span bytes) {
@@ -8,6 +8,7 @@ For license and copyright information please follow this link:
#include "mtproto/mtp_abstract_socket.h"

#include "mtproto/mtp_tcp_socket.h"
#include "mtproto/mtp_tls_socket.h"

namespace MTP {
namespace internal {
@@ -16,7 +17,15 @@ std::unique_ptr<AbstractSocket> AbstractSocket::Create(
not_null<QThread*> thread,
const bytes::vector &secret,
const ProxyData &proxy) {
return std::make_unique<TcpSocket>(thread, proxy);
const auto proxySecret = (proxy.type == ProxyData::Type::Mtproto)
? proxy.secretFromMtprotoPassword()
: bytes::vector();
const auto &usingSecret = proxySecret.empty() ? secret : proxySecret;
if (!usingSecret.empty() && usingSecret[0] == bytes::type(0xEE)) {
return std::make_unique<TlsSocket>(thread, secret, proxy);
} else {
return std::make_unique<TcpSocket>(thread, proxy);
}
}

} // namespace internal
@@ -37,7 +37,7 @@ class AbstractSocket : protected QObject {

virtual void connectToHost(const QString &address, int port) = 0;
[[nodiscard]] virtual bool isConnected() = 0;
[[nodiscard]] virtual int bytesAvailable() = 0;
[[nodiscard]] virtual bool hasBytesAvailable() = 0;
[[nodiscard]] virtual int64 read(char *buffer, int64 maxLength) = 0;
virtual int64 write(const char *buffer, int64 length) = 0;

@@ -9,12 +9,6 @@ For license and copyright information please follow this link:

namespace MTP {
namespace internal {
namespace {

using ErrorSignal = void(QTcpSocket::*)(QAbstractSocket::SocketError);
const auto QTcpSocket_error = ErrorSignal(&QAbstractSocket::error);

} // namespace

TcpSocket::TcpSocket(not_null<QThread*> thread, const ProxyData &proxy)
: AbstractSocket(thread) {
@@ -38,14 +32,13 @@ TcpSocket::TcpSocket(not_null<QThread*> thread, const ProxyData &proxy)
&_socket,
&QTcpSocket::readyRead,
wrap([=] { _readyRead.fire({}); }));

using ErrorSignal = void(QTcpSocket::*)(QAbstractSocket::SocketError);
const auto QTcpSocket_error = ErrorSignal(&QAbstractSocket::error);
connect(
&_socket,
QTcpSocket_error,
wrap([=](Error e) { logError(e); _error.fire({}); }));
}

TcpSocket::~TcpSocket() {
_socket.close();
wrap([=](Error e) { handleError(e); }));
}

void TcpSocket::connectToHost(const QString &address, int port) {
@@ -56,8 +49,8 @@ bool TcpSocket::isConnected() {
return (_socket.state() == QAbstractSocket::ConnectedState);
}

int TcpSocket::bytesAvailable() {
return _socket.bytesAvailable();
bool TcpSocket::hasBytesAvailable() {
return _socket.bytesAvailable() > 0;
}

int64 TcpSocket::read(char *buffer, int64 maxLength) {
@@ -114,8 +107,9 @@ void TcpSocket::LogError(int errorCode, const QString &errorText) {
).arg(errorText));
}

void TcpSocket::logError(int errorCode) {
void TcpSocket::handleError(int errorCode) {
LogError(errorCode, _socket.errorString());
_error.fire({});
}

} // namespace internal
@@ -15,11 +15,10 @@ namespace internal {
class TcpSocket : public AbstractSocket {
public:
TcpSocket(not_null<QThread*> thread, const ProxyData &proxy);
~TcpSocket();

void connectToHost(const QString &address, int port) override;
bool isConnected() override;
int bytesAvailable() override;
bool hasBytesAvailable() override;
int64 read(char *buffer, int64 maxLength) override;
int64 write(const char *buffer, int64 length) override;

@@ -28,7 +27,7 @@ class TcpSocket : public AbstractSocket {
static void LogError(int errorCode, const QString &errorText);

private:
void logError(int errorCode);
void handleError(int errorCode);

QTcpSocket _socket;

0 comments on commit 69b6b48

Please sign in to comment.
You can’t perform that action at this time.