Newer
Older
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <chrono>
#include "tcp_layer_factory.hh"
#include "loggers.hh"
tcp_layer::tcp_layer(const std::string & p_type, const std::string & param) : layer(p_type), SSL_Socket(), PORT(p_type.c_str()), _params(), _client_id{-1}, _time_key("tcp_layer::Handle_Fd_Event_Readable"), _reconnect_on_send{false} {
loggers::get_instance().log(">>> tcp_layer::tcp_layer: %s, %s", to_string().c_str(), param.c_str());
// Setup parameters
params::convert(_params, param);
_params.log();
set_socket_debugging(false);
params::const_iterator it = _params.find(params::debug);
if (it == _params.cend()) {
_params.insert(std::pair<std::string, std::string>(std::string("debug"), "0"));
} else if (it->second.compare("1") == 0) {
set_socket_debugging(true);
}
bool server_mode = false;
it = _params.find(params::server_mode);
if (it != _params.cend()) {
server_mode = (1 == std::stoi(it->second));
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
}
it = _params.find(params::server);
if (it == _params.cend()) {
_params.insert(std::pair<std::string, std::string>(std::string("server"), "127.0.0.1")); // TODO Try using params::server instead of std::string("server")
}
if (!parameter_set(params::server.c_str(), _params[params::server].c_str())) {
loggers::get_instance().warning("tcp_layer::set_parameter: Unprocessed parameter: %s", params::server.c_str());
}
set_ssl_use_ssl(false);
it = _params.find(params::use_ssl);
if (it == _params.cend()) {
_params.insert(std::pair<std::string, std::string>(std::string("use_ssl"), "0"));
} else if (it->second.compare("1") == 0) {
set_ssl_use_ssl(true);
}
it = _params.find(params::port);
if (it == _params.cend()) {
if (_params[params::use_ssl].compare("0") == 0) { // Use standard HTTP port
_params.insert(std::pair<std::string, std::string>(std::string("port"), "80"));
} else { // Use standard HTTPS port
_params.insert(std::pair<std::string, std::string>(std::string("port"), "443"));
}
}
if (!parameter_set(params::port.c_str(), _params[params::port].c_str())) {
loggers::get_instance().warning("tcp_layer::set_parameter: Unprocessed parameter: %s", params::port.c_str());
}
it = _params.find(params::local_port);
if (it == _params.cend()) {
if (_params[params::use_ssl].compare("0") == 0) { // Use standard HTTP local_port
_params.insert(std::pair<std::string, std::string>(std::string("local_port"), "80"));
} else { // Use standard HTTPS local_port
_params.insert(std::pair<std::string, std::string>(std::string("local_port"), "443"));
}
}
if (!parameter_set(params::local_port.c_str(), _params[params::local_port].c_str())) {
loggers::get_instance().warning("tcp_layer::set_parameter: Unprocessed parameter: %s", params::local_port.c_str());
}
parameter_set(use_connection_ASPs_name(), (!server_mode) ? "yes" : "no");
parameter_set(server_backlog_name(), "1024");
loggers::get_instance().log("tcp_layer::tcp_layer: server_mode=%x", server_mode);
set_server_mode(server_mode);
if (server_mode) {
parameter_set("serverPort", _params[params::local_port].c_str());
}
set_ttcn_buffer_usercontrol(false);
set_handle_half_close(true);
map_user();
if (!_reconnect_on_send) {
open_client_connection(_params[params::server].c_str(), _params[params::port].c_str(), NULL, NULL);
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
tcp_layer::~tcp_layer() {
loggers::get_instance().log(">>> tcp_layer::~tcp_layer: %d", _client_id);
if (_client_id != -1) {
remove_client(_client_id);
}
unmap_user();
}
void tcp_layer::Handle_Fd_Event(int fd, boolean is_readable, boolean is_writable, boolean is_error)
{
loggers::get_instance().log(">>> tcp_layer::Handle_Fd_Event: %d", fd);
Handle_Socket_Event(fd, is_readable, is_writable, is_error);
log_debug("<<< tcp_layer::Handle_Fd_Event");
}
void tcp_layer::Handle_Timeout(double time_since_last_call)
{
loggers::get_instance().log(">>> tcp_layer::Handle_Timeout: %f", time_since_last_call);
Handle_Timeout_Event(time_since_last_call);
loggers::get_instance().log("<<< tcp_layer::Handle_Timeout");
}
void tcp_layer::send_data(OCTETSTRING& data, params& params) {
loggers::get_instance().log_msg(">>> tcp_layer::send_data: ", data);
loggers::get_instance().log("tcp_layer::send_data: SSL mode: %x", get_ssl_use_ssl());
send_outgoing(static_cast<const unsigned char*>(data), data.lengthof(), _client_id);
}
void tcp_layer::receive_data(OCTETSTRING& data, params& params) {
loggers::get_instance().log_msg(">>> tcp_layer::receive_data: ", data);
receive_to_all_layers(data, params);
}
void tcp_layer::message_incoming(const unsigned char* message_buffer, int length, int client_id) {
loggers::get_instance().log(">>> tcp_layer::message_incoming");
loggers::get_instance().log_to_hexa("tcp_layer::message_incoming: ", message_buffer, length);
float duration;
loggers::get_instance().set_start_time(_time_key);
OCTETSTRING data(length, message_buffer);
params params;
this->receive_data(data, params); // TODO Check execution time for decoding operation
loggers::get_instance().set_stop_time(_time_key, duration);
}
void tcp_layer::client_connection_opened(int p_client_id)
{
loggers::get_instance().log(">>> tcp_layer::client_connection_opened: %d", p_client_id);
_client_id = p_client_id;
}
bool tcp_layer::add_user_data(int p_client_id)
{
loggers::get_instance().log(">>> tcp_layer::add_user_data: %d", p_client_id);
if (_params[params::use_ssl].compare("0") == 0) {
loggers::get_instance().log("tcp_layer::add_user_data: Non secured mode");
return Abstract_Socket::add_user_data(p_client_id);
}
loggers::get_instance().log("tcp_layer::add_user_data: SSL mode");
return SSL_Socket::add_user_data(p_client_id);
}
int tcp_layer::send_message_on_fd(int p_client_id, const unsigned char * message_buffer, int length_of_message)
{
loggers::get_instance().log(">>> tcp_layer::send_message_on_fd: %d", p_client_id);
if(get_user_data(p_client_id))
{
loggers::get_instance().log("tcp_layer::send_message_on_fd: SSL mode");
return SSL_Socket::send_message_on_fd(p_client_id, message_buffer, length_of_message);
}
loggers::get_instance().log("tcp_layer::send_message_on_fd: Non secured mode");
return Abstract_Socket::send_message_on_fd(p_client_id, message_buffer, length_of_message);
}
int tcp_layer::send_message_on_nonblocking_fd(int p_client_id, const unsigned char * message_buffer, int length_of_message)
{
loggers::get_instance().log(">>> tcp_layer::send_message_on_nonblocking_fd: %d", p_client_id);
if(get_user_data(p_client_id))
{
loggers::get_instance().log("tcp_layer::send_message_on_nonblocking_fd: SSL mode");
return SSL_Socket::send_message_on_nonblocking_fd(p_client_id, message_buffer, length_of_message);
}
loggers::get_instance().log("tcp_layer::send_message_on_nonblocking_fd: Non secured mode");
return Abstract_Socket::send_message_on_nonblocking_fd(p_client_id, message_buffer, length_of_message);
}
int tcp_layer::receive_message_on_fd(int p_client_id)
{
loggers::get_instance().log(">>> tcp_layer::receive_message_on_fd: %d", p_client_id);
if(get_user_data(p_client_id)) {
// INFO: it is assumed that only SSL_Socket assigns user data to each peer
loggers::get_instance().log("tcp_layer::receive_message_on_fd: SSL mode");
return SSL_Socket::receive_message_on_fd(p_client_id);
}
loggers::get_instance().log("tcp_layer::receive_message_on_fd: Non secured mode");
return Abstract_Socket::receive_message_on_fd(p_client_id);
}
void tcp_layer::peer_disconnected(int p_client_id)
{
loggers::get_instance().log(">>> tcp_layer::peer_disconnected: %d", p_client_id);
Abstract_Socket::peer_disconnected(p_client_id);
_client_id = -1;
}
tcp_layer_factory tcp_layer_factory::_f;