Subversion Repositories tpanel

Rev

Rev 464 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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; }
42
        int retrieveSSLerror(int rcode) { return SSL_get_error(mSsl, rcode); }
43
        int getSocket() { return mSockfd; }
44
 
45
        static const size_t npos = static_cast<size_t>(-1);
46
 
47
    protected:
48
        struct addrinfo *lookup_host (const std::string& host, int port);
49
        void initSSL();
50
        SSL_CTX *initCTX();
51
        void log_ssl_error();
52
        bool isSockValid();
53
 
54
    private:
55
        std::string mHost;
56
        int mPort{0};
57
        int mSockfd{-1};
58
        bool mConnected{false};
59
        bool mEncrypted{false};
60
        bool mSSLInitialized{false};
61
        SSL_CTX *mCtx{nullptr};
62
        SSL *mSsl{nullptr};
63
        std::string mUser;
64
        std::string mPassword;
65
        std::string mMyIP;
66
};
67
 
68
#endif