Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
86 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:
92 andreas 30
        TSocket();
86 andreas 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);
112 andreas 36
        size_t receive(char *buffer, size_t size, bool doPoll=true);
88 andreas 37
        size_t readAbsolut(char *buffer, size_t size);
86 andreas 38
        size_t send(char *buffer, size_t size);
39
        bool close();
40
        bool isConnected() { return mConnected; }
41
        std::string& getMyIP() { return mMyIP; }
88 andreas 42
        int retrieveSSLerror(int rcode) { return SSL_get_error(mSsl, rcode); }
86 andreas 43
 
94 andreas 44
        static const size_t npos = static_cast<size_t>(-1);
45
 
86 andreas 46
    protected:
47
        struct addrinfo *lookup_host (const std::string& host, int port);
48
        void initSSL();
49
        SSL_CTX *initCTX();
50
        void log_ssl_error();
93 andreas 51
        bool isSockValid();
86 andreas 52
 
53
    private:
54
        std::string mHost;
55
        int mPort{0};
56
        int mSockfd{-1};
57
        bool mConnected{false};
58
        bool mEncrypted{false};
59
        bool mSSLInitialized{false};
60
        SSL_CTX *mCtx{nullptr};
61
        SSL *mSsl{nullptr};
62
        std::string mUser;
63
        std::string mPassword;
64
        std::string mMyIP;
65
};
66
 
67
#endif