Compare commits
2 Commits
5e80c41538
...
f30eeffc90
| Author | SHA1 | Date | |
|---|---|---|---|
| f30eeffc90 | |||
| fd62679c25 |
@ -17,15 +17,34 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#ifdef LIN
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <openssl/err.h>
|
||||||
|
#include <openssl/ssl.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
class Socket
|
class Socket
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
#ifdef LIN
|
||||||
|
SSL *ssl;
|
||||||
|
int sock;
|
||||||
|
#endif
|
||||||
char lastPath[513] = "";
|
char lastPath[513] = "";
|
||||||
char lastHash[2 * MD5LEN + 1] = "";
|
char lastHash[2 * MD5LEN + 1] = "";
|
||||||
std::mutex wsLock;
|
std::mutex wsLock;
|
||||||
std::string url;
|
std::string url;
|
||||||
std::function<void(std::string)> toLog;
|
std::function<void(std::string)> toLog;
|
||||||
|
|
||||||
|
#ifdef LIN
|
||||||
|
void logSSL();
|
||||||
|
void *getSinAddr(addrinfo *addr);
|
||||||
|
in_addr_t resolveHost(const char *host);
|
||||||
|
#endif
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Socket(std::string url,
|
explicit Socket(std::string url,
|
||||||
std::function<void(const std::string)> toLog);
|
std::function<void(const std::string)> toLog);
|
||||||
|
|||||||
@ -6,17 +6,67 @@ Socket::Socket(std::string url, std::function<void(const std::string)> toLog)
|
|||||||
#ifdef IBM
|
#ifdef IBM
|
||||||
// WSA INIT
|
// WSA INIT
|
||||||
int a = 0;
|
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 =
|
||||||
|
resolveHost("german-airlines.de"); // 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
|
#endif
|
||||||
// PLATFORM AGNOSTIC
|
// PLATFORM AGNOSTIC
|
||||||
}
|
}
|
||||||
|
|
||||||
Socket::~Socket()
|
Socket::~Socket()
|
||||||
{
|
{
|
||||||
// PLATFORM AGNOSTIC
|
|
||||||
#ifdef IBM
|
#ifdef IBM
|
||||||
// WSA DEINIT
|
// WSA DEINIT
|
||||||
int a = 0;
|
int a = 0;
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef LIN
|
||||||
|
close(sock);
|
||||||
|
SSL_shutdown(ssl);
|
||||||
|
SSL_free(ssl);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::sendData(data d)
|
void Socket::sendData(data d)
|
||||||
@ -44,3 +94,65 @@ void Socket::sendData(data d)
|
|||||||
// SEND
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void *Socket::getSinAddr(addrinfo *addr)
|
||||||
|
{
|
||||||
|
switch (addr->ai_family) {
|
||||||
|
case AF_INET:
|
||||||
|
return &(reinterpret_cast<sockaddr_in *>(addr->ai_addr)->sin_addr);
|
||||||
|
case AF_INET6:
|
||||||
|
return &(reinterpret_cast<sockaddr_in6 *>(addr->ai_addr)->sin6_addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
in_addr_t Socket::resolveHost(const char *host)
|
||||||
|
{
|
||||||
|
addrinfo hints = {};
|
||||||
|
hints.ai_flags = AI_CANONNAME;
|
||||||
|
hints.ai_family = AF_INET;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
hints.ai_protocol = IPPROTO_TCP;
|
||||||
|
|
||||||
|
addrinfo *res;
|
||||||
|
|
||||||
|
int ret = getaddrinfo(host, NULL, &hints, &res);
|
||||||
|
if (ret != 0) {
|
||||||
|
std::ostringstream msg;
|
||||||
|
msg << "getaddrinfo() failed: " << gai_strerror(ret);
|
||||||
|
toLog(msg.str());
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
std::ostringstream msg;
|
||||||
|
msg << res->ai_canonname;
|
||||||
|
toLog(msg.str());
|
||||||
|
|
||||||
|
in_addr_t retVal = 0;
|
||||||
|
|
||||||
|
for (addrinfo *addr = res; addr != NULL; addr = addr->ai_next) {
|
||||||
|
if (addr->ai_family == AF_INET) {
|
||||||
|
retVal =
|
||||||
|
(reinterpret_cast<sockaddr_in *>(addr->ai_addr))->sin_addr.s_addr;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
freeaddrinfo(res);
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
Loading…
x
Reference in New Issue
Block a user