diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml new file mode 100644 index 0000000..5e6dd34 --- /dev/null +++ b/.idea/codeStyleSettings.xml @@ -0,0 +1,35 @@ + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index ca60024..c769c5f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ find_package(Qt5Widgets REQUIRED) include_directories(${NSPR_INCLUDE_DIRS} ${NSS_INCLUDE_DIRS}) -set(SOURCE_FILES main.cpp dns.cpp) +set(SOURCE_FILES main.cpp dns.cpp ui.cpp) add_executable(oksirc ${SOURCE_FILES}) target_link_libraries(oksirc ${NSPR_LIBRARIES} ${NSS_LIBRARIES} Qt5::Widgets) \ No newline at end of file diff --git a/dns.cpp b/dns.cpp index c11df04..3bd8a89 100644 --- a/dns.cpp +++ b/dns.cpp @@ -8,14 +8,14 @@ PRErrorCode LookupName(char *hostname, PRNetAddr *result) { // PRNetAddr tempAddr; // do { iter = PR_EnumerateAddrInfo(iter, addrInfos, 0, result); - char ipString[80]; - PRStatus status = PR_NetAddrToString(result, ipString, 75); - if(status == PR_FAILURE) { - const PRErrorCode err = PR_GetError(); - fprintf(stderr, "Error: Converting PRNetAddr to string %d: %s\n", - err, PR_ErrorToName(err)); - return err; - } + char ipString[80]; + PRStatus status = PR_NetAddrToString(result, ipString, 75); + if (status == PR_FAILURE) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Error: Converting PRNetAddr to string %d: %s\n", + err, PR_ErrorToName(err)); + return err; + } printf("Found IP Address: %s\n", ipString); return 0; // break; diff --git a/main.cpp b/main.cpp index 2d85496..20505f2 100644 --- a/main.cpp +++ b/main.cpp @@ -1,10 +1,12 @@ #include #include "dns.h" + // NSPR include files #include #include #include +#include #include // NSS include files @@ -15,125 +17,108 @@ #include #include -// Qt Include Files, should come after NSPR / NSS -#include -#include +// Qt-dependent Include Files, should come after NSPR / NSS +#include "ui.h" NSSInitContext *nssContext; SECMODModule *builtInRootsMod; // PK11 Password Function typedef typedef char *(*PK11PasswordFunc)( - PK11SlotInfo *slot, - PRBool retry, - void *arg); + PK11SlotInfo *slot, + PRBool retry, + void *arg); void init() { - // NSPR Init - PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); + // NSPR Init + PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); - // NSS Init - nssContext = - NSS_InitContext("sql:/etc/pki/nssdb", "", "", "", NULL, NSS_INIT_READONLY | NSS_INIT_PK11RELOAD); + // NSS Init + nssContext = + NSS_InitContext("sql:/etc/pki/nssdb", "", "", "", NULL, NSS_INIT_READONLY | NSS_INIT_PK11RELOAD); - if(nssContext == NULL) { - const PRErrorCode err = PR_GetError(); - fprintf(stderr, "NSSInitContext failed: Error code %d: %s\n", err, PR_ErrorToName(err)); - exit(1); - } + if (nssContext == NULL) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "NSSInitContext failed: Error code %d: %s\n", err, PR_ErrorToName(err)); + exit(1); + } - // Ciphers to enable - static const PRUint16 goodCiphers[] = { - TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, - TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, - SSL_NULL_WITH_NULL_NULL // Sentinel value - }; + // Ciphers to enable + static const PRUint16 goodCiphers[] = { + TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, + TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, + SSL_NULL_WITH_NULL_NULL // Sentinel value + }; - PRBool foundGoodCipher = PR_FALSE; - for(const PRUint16 *p = goodCiphers; *p != SSL_NULL_WITH_NULL_NULL; ++p) { - PRInt32 policy; - if(SSL_CipherPolicyGet(*p, &policy) != SECSuccess) { - const PRErrorCode err = PR_GetError(); - fprintf(stderr, "Error: Policy for cipher %u: error %d: %s\n", - (unsigned)*p, err, PR_ErrorToName(err)); - exit(1); - } - if(policy == SSL_ALLOWED) { - // Policy allowed this cipher choice - fprintf(stderr, "Info: found cipher %x\n", (unsigned) *p); - foundGoodCipher = PR_TRUE; - break; - } - } + PRBool foundGoodCipher = PR_FALSE; + for (const PRUint16 *p = goodCiphers; *p != SSL_NULL_WITH_NULL_NULL; ++p) { + PRInt32 policy; + if (SSL_CipherPolicyGet(*p, &policy) != SECSuccess) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Error: Policy for cipher %u: error %d: %s\n", + (unsigned) *p, err, PR_ErrorToName(err)); + exit(1); + } + if (policy == SSL_ALLOWED) { + // Policy allowed this cipher choice + fprintf(stderr, "Info: found cipher %x\n", (unsigned) *p); + foundGoodCipher = PR_TRUE; + break; + } + } - if(!foundGoodCipher) { - if(NSS_SetDomesticPolicy() != SECSuccess) { - const PRErrorCode err = PR_GetError(); - fprintf(stderr, "Error: NSS_SetDomesticPolicy: error %d: %s\n", - err, PR_ErrorToName(err)); - exit(1); - } - } + if (!foundGoodCipher) { + if (NSS_SetDomesticPolicy() != SECSuccess) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Error: NSS_SetDomesticPolicy: error %d: %s\n", + err, PR_ErrorToName(err)); + exit(1); + } + } - // Initialize trusted certificate store - char module_name[] = "library=libnssckbi.so name=\"Root Certs\""; - builtInRootsMod = SECMOD_LoadUserModule(module_name, NULL, PR_FALSE); - if(builtInRootsMod == NULL || !builtInRootsMod->loaded) { - const PRErrorCode err = PR_GetError(); - fprintf(stderr, "Error: Loading built in roots module failed, code %d: %s\n", - err, PR_ErrorToName(err)); - exit(1); - } + // Initialize trusted certificate store + char module_name[] = "library=libnssckbi.so name=\"Root Certs\""; + builtInRootsMod = SECMOD_LoadUserModule(module_name, NULL, PR_FALSE); + if (builtInRootsMod == NULL || !builtInRootsMod->loaded) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Error: Loading built in roots module failed, code %d: %s\n", + err, PR_ErrorToName(err)); + exit(1); + } } -int main(int argc, char* argv[]) { - init(); - PRNetAddr theAddr; - LookupName((char *)"buyvm.theoks.net", &theAddr); +int main(int argc, char *argv[]) { + init(); - char ipString[80]; - PRStatus status = PR_NetAddrToString(&theAddr, ipString, 75); - if(status == PR_FAILURE) { - const PRErrorCode err = PR_GetError(); - fprintf(stderr, "Error: Converting PRNetAddr to string %d: %s\n", - err, PR_ErrorToName(err)); - exit(1); - } + PRNetAddr theAddr; + LookupName((char *) "buyvm.theoks.net", &theAddr); + + char ipString[80]; + PRStatus status = PR_NetAddrToString(&theAddr, ipString, 75); + if (status == PR_FAILURE) { + const PRErrorCode err = PR_GetError(); + fprintf(stderr, "Error: Converting PRNetAddr to string %d: %s\n", + err, PR_ErrorToName(err)); + exit(1); + } - PRFileDesc *tcpSocket = PR_NewTCPSocket(); - theAddr.inet.port = 443; - PRFileDesc *nssSocket; - SSL_ImportFD(NULL, tcpSocket); + PRFileDesc *tcpSocket = PR_NewTCPSocket(); + theAddr.inet.port = 443; + PRFileDesc *nssSocket; + SSL_ImportFD(NULL, tcpSocket); - QApplication app(argc, argv); - QWidget mainWindow; - mainWindow.resize(1280, 720); - mainWindow.setWindowTitle("oksirc - early Qt test"); - QPlainTextEdit *mainLog = new QPlainTextEdit(); - mainLog->setReadOnly(true); - QLineEdit *textEntry = new QLineEdit(); - QPushButton *sendButton = new QPushButton(QApplication::translate("send", "Send")); - QHBoxLayout *entryLineLayout = new QHBoxLayout(); - entryLineLayout->addWidget(textEntry); - entryLineLayout->addWidget(sendButton); + ui *uiInstance = new ui(argc, argv); + int qAppRetCode = uiInstance->exec(); - QVBoxLayout *mainLogAndLineLayout = new QVBoxLayout(); - mainLogAndLineLayout->addWidget(mainLog); - mainLogAndLineLayout->addLayout(entryLineLayout); - mainWindow.setLayout(mainLogAndLineLayout); - mainWindow.show(); - textEntry->setFocus(); + char messageBuf[1024]; + sprintf(messageBuf, "Found IP Address: %s", ipString); + uiInstance->mainLog->appendPlainText(messageBuf); + SECMOD_DestroyModule(builtInRootsMod); + NSS_ShutdownContext(nssContext); - char messageBuf[1024]; - sprintf(messageBuf, "Found IP Address: %s", ipString); - mainLog->appendPlainText(messageBuf); - - SECMOD_DestroyModule(builtInRootsMod); - NSS_ShutdownContext(nssContext); - - return app.exec(); + return qAppRetCode; } \ No newline at end of file diff --git a/ui.cpp b/ui.cpp new file mode 100644 index 0000000..7d245e7 --- /dev/null +++ b/ui.cpp @@ -0,0 +1,35 @@ +#include "ui.h" + +ui::ui(int argc, char **argv) { + app = new QApplication(argc, argv); + mainWindow = new QWidget(); + mainWindow->resize(1280, 720); + mainWindow->setWindowTitle("oksirc - early Qt test"); + + mainLog = new QPlainTextEdit(); + mainLog->setReadOnly(true); + textEntry = new QLineEdit(); + QPushButton *sendButton = new QPushButton(QApplication::translate("send", "Send")); + QObject::connect(sendButton, &QPushButton::clicked, this, &ui::sendButtonClicked); + + QHBoxLayout *entryLineLayout = new QHBoxLayout(); + entryLineLayout->addWidget(textEntry); + entryLineLayout->addWidget(sendButton); + + QVBoxLayout *mainLogAndLineLayout = new QVBoxLayout(); + mainLogAndLineLayout->addWidget(mainLog); + mainLogAndLineLayout->addLayout(entryLineLayout); + mainWindow->setLayout(mainLogAndLineLayout); + mainWindow->show(); + textEntry->setFocus(); +} + +int ui::exec() { + return app->exec(); +} + +void ui::sendButtonClicked(bool checked) { + this->mainLog->appendPlainText("Send button clicked!"); + this->mainLog->appendPlainText(textEntry->text()); + textEntry->clear(); +} \ No newline at end of file diff --git a/ui.h b/ui.h new file mode 100644 index 0000000..f74b9aa --- /dev/null +++ b/ui.h @@ -0,0 +1,24 @@ +#ifndef OKSIRC_UI_H +#define OKSIRC_UI_H + +#include + +class ui : public QObject { +public: + ui(int, char **); + + int exec(); + void sendButtonClicked(bool); + + + QPlainTextEdit *mainLog; + +private: + QApplication *app; + QWidget *mainWindow; + QLineEdit *textEntry; + +}; + + +#endif //OKSIRC_UI_H