client.h
4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, version 3.
FlashMQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with FlashMQ. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef CLIENT_H
#define CLIENT_H
#include <fcntl.h>
#include <unistd.h>
#include <vector>
#include <mutex>
#include <iostream>
#include <time.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "forward_declarations.h"
#include "threaddata.h"
#include "mqttpacket.h"
#include "exceptions.h"
#include "cirbuf.h"
#include "types.h"
#include "iowrapper.h"
#define MQTT_HEADER_LENGH 2
class Client
{
friend class IoWrapper;
int fd;
bool fuzzMode = false;
ProtocolVersion protocolVersion = ProtocolVersion::None;
const size_t initialBufferSize = 0;
const size_t maxPacketSize = 0;
IoWrapper ioWrapper;
std::string transportStr;
std::string address;
CirBuf readbuf;
CirBuf writebuf;
bool authenticated = false;
bool connectPacketSeen = false;
bool readyForWriting = false;
bool readyForReading = true;
bool disconnectWhenBytesWritten = false;
bool disconnecting = false;
std::string disconnectReason;
std::chrono::time_point<std::chrono::steady_clock> lastActivity;
std::string clientid;
std::string username;
uint16_t keepalive = 0;
bool cleanSession = false;
std::string will_topic;
std::string will_payload;
bool will_retain = false;
char will_qos = 0;
std::shared_ptr<ThreadData> threadData;
std::mutex writeBufMutex;
std::shared_ptr<Session> session;
Logger *logger = Logger::getInstance();
void setReadyForWriting(bool val);
void setReadyForReading(bool val);
public:
Client(int fd, std::shared_ptr<ThreadData> threadData, SSL *ssl, bool websocket, struct sockaddr *addr, std::shared_ptr<Settings> settings, bool fuzzMode=false);
Client(const Client &other) = delete;
Client(Client &&other) = delete;
~Client();
int getFd() { return fd;}
bool isSslAccepted() const;
bool isSsl() const;
bool getSslReadWantsWrite() const;
bool getSslWriteWantsRead() const;
ProtocolVersion getProtocolVersion() const;
void startOrContinueSslAccept();
void markAsDisconnecting();
bool readFdIntoBuffer();
bool bufferToMqttPackets(std::vector<MqttPacket> &packetQueueIn, std::shared_ptr<Client> &sender);
void setClientProperties(ProtocolVersion protocolVersion, const std::string &clientId, const std::string username, bool connectPacketSeen, uint16_t keepalive, bool cleanSession);
void setWill(const std::string &topic, const std::string &payload, bool retain, char qos);
void clearWill();
void setAuthenticated(bool value) { authenticated = value;}
bool getAuthenticated() { return authenticated; }
bool hasConnectPacketSeen() { return connectPacketSeen; }
std::shared_ptr<ThreadData> getThreadData() { return threadData; }
std::string &getClientId() { return this->clientid; }
const std::string &getUsername() const { return this->username; }
bool getCleanSession() { return cleanSession; }
void assignSession(std::shared_ptr<Session> &session);
std::shared_ptr<Session> getSession();
void setDisconnectReason(const std::string &reason);
void writeText(const std::string &text);
void writePingResp();
void writeMqttPacket(const MqttPacket &packet, const char qos = 0);
void writeMqttPacketAndBlameThisClient(const MqttPacket &packet, const char qos);
bool writeBufIntoFd();
bool readyForDisconnecting() const { return disconnectWhenBytesWritten && writebuf.usedBytes() == 0; }
// Do this before calling an action that makes this client ready for writing, so that the EPOLLOUT will handle it.
void setReadyForDisconnect() { disconnectWhenBytesWritten = true; }
std::string repr();
bool keepAliveExpired();
std::string getKeepAliveInfoString() const;
void resetBuffersIfEligible();
#ifndef NDEBUG
void setFakeUpgraded();
#endif
};
#endif // CLIENT_H