446 |
andreas |
1 |
/*
|
|
|
2 |
* Copyright (C) 2022 by Andreas Theofilu <andreas@theosys.at>
|
|
|
3 |
*
|
|
|
4 |
* This program is free software; you can redistribute it and/or modify
|
|
|
5 |
* it under the terms of the GNU General Public License as published by
|
|
|
6 |
* the Free Software Foundation; either version 3 of the License, or
|
|
|
7 |
* (at your option) any later version.
|
|
|
8 |
*
|
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
* GNU General Public License for more details.
|
|
|
13 |
*
|
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
|
15 |
* along with this program; if not, write to the Free Software Foundation,
|
|
|
16 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
#ifndef __TSOCKET_H__
|
|
|
20 |
#define __TSOCKET_H__
|
|
|
21 |
|
|
|
22 |
#include <string>
|
|
|
23 |
|
|
|
24 |
#include <netdb.h>
|
|
|
25 |
#include <openssl/ssl.h>
|
|
|
26 |
|
|
|
27 |
class TSocket
|
|
|
28 |
{
|
|
|
29 |
public:
|
|
|
30 |
TSocket();
|
|
|
31 |
TSocket(const std::string& host, int port);
|
|
|
32 |
~TSocket();
|
|
|
33 |
|
|
|
34 |
bool connect(bool encrypt=false);
|
|
|
35 |
bool connect(const std::string& host, int port, bool encrypt=false);
|
|
|
36 |
size_t receive(char *buffer, size_t size, bool doPoll=true);
|
|
|
37 |
size_t readAbsolut(char *buffer, size_t size);
|
|
|
38 |
size_t send(char *buffer, size_t size);
|
|
|
39 |
bool close();
|
|
|
40 |
bool isConnected() { return mConnected; }
|
|
|
41 |
std::string& getMyIP() { return mMyIP; }
|
464 |
andreas |
42 |
std::string& getMyHostName() { return mMyHostName; }
|
|
|
43 |
std::string& getMyNetmask() { return mMyNetmask; }
|
446 |
andreas |
44 |
int retrieveSSLerror(int rcode) { return SSL_get_error(mSsl, rcode); }
|
|
|
45 |
int getSocket() { return mSockfd; }
|
|
|
46 |
|
|
|
47 |
static const size_t npos = static_cast<size_t>(-1);
|
|
|
48 |
|
|
|
49 |
protected:
|
|
|
50 |
struct addrinfo *lookup_host (const std::string& host, int port);
|
|
|
51 |
void initSSL();
|
|
|
52 |
SSL_CTX *initCTX();
|
|
|
53 |
void log_ssl_error();
|
|
|
54 |
bool isSockValid();
|
464 |
andreas |
55 |
bool getHost();
|
465 |
andreas |
56 |
bool determineNetmask(int socket);
|
446 |
andreas |
57 |
|
|
|
58 |
private:
|
|
|
59 |
std::string mHost;
|
|
|
60 |
int mPort{0};
|
|
|
61 |
int mSockfd{-1};
|
|
|
62 |
bool mConnected{false};
|
|
|
63 |
bool mEncrypted{false};
|
|
|
64 |
bool mSSLInitialized{false};
|
|
|
65 |
SSL_CTX *mCtx{nullptr};
|
|
|
66 |
SSL *mSsl{nullptr};
|
|
|
67 |
std::string mUser;
|
|
|
68 |
std::string mPassword;
|
|
|
69 |
std::string mMyIP;
|
464 |
andreas |
70 |
std::string mMyHostName;
|
|
|
71 |
std::string mMyNetmask;
|
446 |
andreas |
72 |
};
|
|
|
73 |
|
|
|
74 |
#endif
|