From 4ff4d4d93daa59fbd68e3370d99bd0cdb75222be Mon Sep 17 00:00:00 2001 From: Darren VanBuren Date: Sun, 23 Jul 2017 02:07:58 -0700 Subject: [PATCH] Get closer to actually connecting, add hitting enter on text entry --- main.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- ui.cpp | 11 +++++-- ui.h | 2 +- 3 files changed, 105 insertions(+), 7 deletions(-) diff --git a/main.cpp b/main.cpp index 41f43b5..45d3c93 100644 --- a/main.cpp +++ b/main.cpp @@ -103,15 +103,106 @@ int main(int argc, char *argv[]) { exit(1); } - - PRFileDesc *tcpSocket = PR_NewTCPSocket(); - theAddr.inet.port = 443; + PRFileDesc *tcpSocket = PR_OpenTCPSocket(PR_AF_INET6); + PRFileDesc *model = PR_NewTCPSocket(); + theAddr.ipv6.port = 443; + theAddr.raw.family = PR_AF_INET6; PRFileDesc *nssSocket; - SSL_ImportFD(NULL, tcpSocket); + PRFileDesc *sslModel = SSL_ImportFD(NULL, model); + // Set SSL options + if(SSL_OptionSet(sslModel, SSL_ENABLE_SSL2, PR_FALSE) != SECSuccess) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Error disabling SSLv2, code %d: %s\n", err, PR_ErrorToName(err)); + exit(1); + } + + if(SSL_OptionSet(sslModel, SSL_V2_COMPATIBLE_HELLO, PR_FALSE) != SECSuccess) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Error disabling SSLv2 compatible hello, code %d: %s\n", err, PR_ErrorToName(err)); + exit(1); + } + + if(SSL_OptionSet(sslModel, SSL_ENABLE_SSL3, PR_FALSE) != SECSuccess) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Error disabling SSLv3, code %d: %s\n", err, PR_ErrorToName(err)); + exit(1); + } + + if(SSL_OptionSet(sslModel, SSL_ENABLE_TLS, PR_TRUE) != SECSuccess) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Error enabling TLS, code %d: %s\n", err, PR_ErrorToName(err)); + exit(1); + } + + // Open base TCP connection + if(PR_Connect(tcpSocket, &theAddr, PR_INTERVAL_NO_TIMEOUT) != PR_SUCCESS) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Error opening TCP connection, code %d: %s\n", err, PR_ErrorToName(err)); + exit(1); + } + + // Wrap TCP connection with SSL layer + nssSocket = SSL_ImportFD(sslModel, tcpSocket); + if(nssSocket == nullptr) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Error importing TCP Socket to NSS, code %d: %s\n", err, PR_ErrorToName(err)); + exit(1); + } + + PR_Close(model); + + // Do the handshake + if(SSL_ResetHandshake(nssSocket, PR_FALSE) != SECSuccess) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Failed resetting handshake, code %d: %s\n", err, PR_ErrorToName(err)); + exit(1); + } + + if(SSL_SetURL(nssSocket, "buyvm.theoks.net") != SECSuccess) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Error setting domain for connection, code %d: %s\n", err, PR_ErrorToName(err)); + exit(1); + } + + if(SSL_ForceHandshake(nssSocket) != SECSuccess) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Error forcing handshake, code %d: %s\n", err, PR_ErrorToName(err)); + exit(1); + } ui *uiInstance = new ui(argc, argv); + char netBuf[8192]; + + // TEMP: Retreiving test file from HTTP daemon w/ TLS + // Send the request + snprintf(netBuf, sizeof(netBuf), "GET /junk/testfile.txt HTTP/1.0\r\nHost: buyvm.theoks.net\r\nUser-Agent: oksirc-testing\r\n\r\n"); + PRInt32 ret = PR_Write(nssSocket, netBuf, strlen(netBuf)); + if(ret < 0) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Error writing data to socket, code %d: %s\n", err, PR_ErrorToName(err)); + uiInstance->mainLog->appendPlainText("Error writing data to socket, see stderr."); + } + + // Get the response + ret = PR_Read(nssSocket, netBuf, sizeof(netBuf)); + if(ret < 0) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Error reading data from socket, code %d: %s\n", err, PR_ErrorToName(err)); + uiInstance->mainLog->appendPlainText("Error writing data from socket, see stderr."); + } else { + uiInstance->mainLog->appendPlainText(netBuf); + } + + if(PR_Shutdown(nssSocket, PR_SHUTDOWN_BOTH) != PR_SUCCESS) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Error shutting down socket, code %d: %s\n", err, PR_ErrorToName(err)); + exit(1); + } + + PR_Close(nssSocket); + char messageBuf[1024]; sprintf(messageBuf, "Found IP Address: %s", ipString); uiInstance->mainLog->appendPlainText(messageBuf); diff --git a/ui.cpp b/ui.cpp index 7d245e7..e14bdf0 100644 --- a/ui.cpp +++ b/ui.cpp @@ -11,6 +11,7 @@ ui::ui(int argc, char **argv) { textEntry = new QLineEdit(); QPushButton *sendButton = new QPushButton(QApplication::translate("send", "Send")); QObject::connect(sendButton, &QPushButton::clicked, this, &ui::sendButtonClicked); + QObject::connect(textEntry, &QLineEdit::returnPressed, this, &ui::returnOnTextEntry); QHBoxLayout *entryLineLayout = new QHBoxLayout(); entryLineLayout->addWidget(textEntry); @@ -28,8 +29,14 @@ int ui::exec() { return app->exec(); } +void ui::returnOnTextEntry() { + mainLog->appendPlainText("Return key pressed!"); + mainLog->appendPlainText(textEntry->text()); + textEntry->clear(); +} + void ui::sendButtonClicked(bool checked) { - this->mainLog->appendPlainText("Send button clicked!"); - this->mainLog->appendPlainText(textEntry->text()); + mainLog->appendPlainText("Send button clicked!"); + mainLog->appendPlainText(textEntry->text()); textEntry->clear(); } \ No newline at end of file diff --git a/ui.h b/ui.h index f74b9aa..bcab122 100644 --- a/ui.h +++ b/ui.h @@ -9,7 +9,7 @@ public: int exec(); void sendButtonClicked(bool); - + void returnOnTextEntry(); QPlainTextEdit *mainLog;