105 lines
2.1 KiB
C++
105 lines
2.1 KiB
C++
#include "include/socket.h"
|
|
|
|
Socket::Socket(std::string url, std::function<void(const std::string)> toLog)
|
|
: url(url), toLog(std::move(toLog))
|
|
{
|
|
#ifdef IBM
|
|
// WSA INIT
|
|
int a = 0;
|
|
#endif
|
|
#ifdef APL
|
|
int a = 0;
|
|
#endif
|
|
#ifdef LIN
|
|
int s;
|
|
s = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (s < 0) {
|
|
toLog("Error creating socket.");
|
|
return;
|
|
}
|
|
struct sockaddr_in sa;
|
|
memset(&sa, 0, sizeof(sa));
|
|
sa.sin_family = AF_INET;
|
|
sa.sin_addr.s_addr =
|
|
inet_addr("136.243.123.153"); // address of german-airlines.de
|
|
sa.sin_port = htons(443);
|
|
socklen_t socklen = sizeof(sa);
|
|
if (connect(s, (struct sockaddr *)&sa, socklen)) {
|
|
toLog("Error connecting to server.");
|
|
return;
|
|
}
|
|
SSL_library_init();
|
|
SSLeay_add_ssl_algorithms();
|
|
SSL_load_error_strings();
|
|
const SSL_METHOD *meth = TLSv1_2_client_method();
|
|
SSL_CTX *ctx = SSL_CTX_new(meth);
|
|
ssl = SSL_new(ctx);
|
|
if (!ssl) {
|
|
toLog("Error creating SSL.");
|
|
logSSL();
|
|
return;
|
|
}
|
|
sock = SSL_get_fd(ssl);
|
|
SSL_set_fd(ssl, s);
|
|
int err = SSL_connect(ssl);
|
|
if (err <= 0) {
|
|
std::ostringstream msg;
|
|
msg << "Error creating SSL connection. err=" << err;
|
|
toLog(msg.str());
|
|
logSSL();
|
|
return;
|
|
}
|
|
std::ostringstream msg;
|
|
msg << "SSL connection using " << SSL_get_cipher(ssl);
|
|
toLog(msg.str());
|
|
#endif
|
|
// PLATFORM AGNOSTIC
|
|
}
|
|
|
|
Socket::~Socket()
|
|
{
|
|
// PLATFORM AGNOSTIC
|
|
#ifdef IBM
|
|
// WSA DEINIT
|
|
int a = 0;
|
|
#endif
|
|
}
|
|
|
|
void Socket::sendData(data d)
|
|
{
|
|
if (strcmp(d.path, lastPath) != 0) {
|
|
strcpy(lastPath, d.path);
|
|
if (util::generateMD5(d.path, lastHash, toLog)) {
|
|
strcpy(lastHash, "NOT SET");
|
|
}
|
|
}
|
|
|
|
nlohmann::json json = {
|
|
{"altitude", d.alt},
|
|
{"vs", d.vs},
|
|
{"ias", d.ias},
|
|
{"magHdg", d.magHeading},
|
|
{"truHdg", d.truHdg},
|
|
{"totFuel", d.totFuelKg},
|
|
{"fuelFlow", d.ff},
|
|
{"hash", lastHash},
|
|
};
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(wsLock);
|
|
// SEND
|
|
}
|
|
}
|
|
|
|
#ifdef LIN
|
|
void Socket::logSSL()
|
|
{
|
|
int err;
|
|
while ((err = ERR_get_error())) {
|
|
char *str = ERR_error_string(err, 0);
|
|
if (!str)
|
|
return;
|
|
toLog(str);
|
|
}
|
|
}
|
|
#endif |