-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_client.cpp
355 lines (278 loc) · 8.57 KB
/
tcp_client.cpp
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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* BigPlg
* Copyright (C) Aaron Hebert 2012 - Present <[email protected]>
*
* BigPlg is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BigPlg 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <netdb.h>
#include <syslog.h>
#include <cstring>
#include <unistd.h>
#include <netinet/tcp.h>
#include <sys/ioctl.h>
#include "tcp_client.h"
enum peer_buffer {
DEFAULT_TCP_BUFFER_SIZE = 14600,
DEFAULT_BGP_BUFFER_SIZE = 4096,
MAX_PEER_BUFFER = 131072
};
tcp_client::tcp_client() :
tcp_socket(0),
tcp_rx_socket_buffer_size(DEFAULT_TCP_BUFFER_SIZE),
tcp_tx_socket_buffer_size(DEFAULT_TCP_BUFFER_SIZE),
bgp_tx_buffer_size(DEFAULT_BGP_BUFFER_SIZE),
bgp_rx_buffer_size(DEFAULT_BGP_BUFFER_SIZE) {
rp = nullptr;
results = nullptr;
tx = nullptr;
rx = nullptr;
tcp_client_reset();
}
tcp_client::tcp_client(const tcp_client& orig) {
}
tcp_client::~tcp_client() {
tcp_client_reset();
}
void tcp_client::tcp_client_reset(void) {
if (tcp_socket)close(tcp_socket);
memset(&hints, 0, sizeof (addrinfo));
}
/*! tcp_client::get_addr_info - sets server info.
*
* sets server info for the server we are connecting to.
* this must be called before anything.
*/
bool tcp_client::get_addr_info(const char *host, const char *port) {
hints.ai_family = AF_UNSPEC; // ipv4 or ipv6
hints.ai_socktype = SOCK_STREAM; // tcp
hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
results = 0;
int s = getaddrinfo(host, port, &hints, &results);
if (s != 0) {
syslog(LOG_DEBUG, "get_addr_info: %s", gai_strerror(s));
return false;
}
return true;
}
/*! tcp_client::connect - creates socket and connects.
*
* creates socket and initiates tcp connection.
*/
bool tcp_client::connect(const char *host, const char *port) {
if (!get_addr_info(host, port)) return false;
if (!this->results) return false;
bool rc(false);
for (rp = results; rp != NULL; rp = rp->ai_next) {
tcp_socket = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (tcp_socket == -1)
continue;
if (::connect(tcp_socket, rp->ai_addr, rp->ai_addrlen) != -1) {
int option(1);
setsockopt(tcp_socket, SOL_SOCKET, SO_REUSEADDR,
(char *) &option, sizeof (option));
rc = true;
syslog(LOG_DEBUG, "connection to %s OK", host);
break;
}
close(tcp_socket);
}
if (rp == NULL) {
syslog(LOG_DEBUG, "unable to allocate interface to destination host");
return false;
}
freeaddrinfo(results);
if (NULL == (this->tx = fdopen(tcp_socket, "w"))) {
close(tcp_socket);
syslog(LOG_DEBUG, "unable to open TX stream");
} else rc = true;
if (NULL == (this->rx = fdopen(tcp_socket, "r"))) {
close(tcp_socket);
syslog(LOG_DEBUG, "unable to open RX stream");
} else rc = true;
return rc;
}
void tcp_client::disconnect(void) {
close(this->tcp_socket);
if (tx)fclose(tx);
if (rx)fclose(rx);
rx = NULL;
tx = NULL;
this->tcp_socket = 0;
}
/*! tcp_client::set_tx_buffer_size - increase tx buffer size.
*
* size - the amount of data in bytes the buffer will store
* before flush().
*/
bool tcp_client::set_bgp_tx_buffer_size(const size_t &size) {
bgp_tx_buffer_size = size;
return !setvbuf(this->tx, NULL, _IOFBF, size);
}
/*! tcp_client::set_rx_buffer_size - increase tx buffer size.
*
* size - the amount of data in bytes the buffer will store
* before flush().
*/
bool tcp_client::set_bgp_rx_buffer_size(const size_t &size) {
bgp_rx_buffer_size = size;
return !setvbuf(this->rx, NULL, _IOFBF, size);
}
bool tcp_client::set_tcp_tx_buffer_size(const size_t &size) {
tcp_tx_socket_buffer_size = size;
bool ret_val(false);
int option(size);
ret_val = !setsockopt(tcp_socket, SOL_SOCKET, SO_SNDBUF,
(char *) &option, sizeof (option));
return ret_val;
}
bool tcp_client::set_tcp_rx_buffer_size(const size_t &size) {
tcp_rx_socket_buffer_size = size;
bool ret_val(false);
int option(size);
ret_val = !setsockopt(tcp_socket, SOL_SOCKET, SO_RCVBUF,
(char *) &option, sizeof (option));
return ret_val;
}
/*!tcp_client::set_rx_buffer_size - resize bgp and tcp socket buffers
*/
bool tcp_client::set_tcp_bgp_rx_buffer_size(const size_t &tcp_buffer_size,
const size_t &bgp_buffer_size) {
tcp_rx_socket_buffer_size = tcp_buffer_size;
bgp_rx_buffer_size = bgp_buffer_size;
bool ret_val(false);
int option(tcp_buffer_size);
ret_val = !setsockopt(tcp_socket, SOL_SOCKET, SO_RCVBUF,
(char *) &option, sizeof (option));
ret_val = !setvbuf(this->rx, NULL, _IOFBF, bgp_buffer_size);
return ret_val;
}
/*!tcp_client::set_tx_buffer_size - resize bgp and tcp socket buffers
*/
bool tcp_client::set_tcp_bgp_tx_buffer_size(const size_t &tcp_buffer_size,
const size_t &bgp_buffer_size) {
tcp_tx_socket_buffer_size = tcp_buffer_size;
bgp_tx_buffer_size = bgp_buffer_size;
bool ret_val(false);
int option(tcp_buffer_size);
ret_val = !setsockopt(tcp_socket, SOL_SOCKET, SO_SNDBUF,
(char *) &option, sizeof (option));
ret_val = !setvbuf(this->tx, NULL, _IOFBF, bgp_buffer_size);
return ret_val;
}
int tcp_client::get_tcp_rx_buffer_size(void) {
int sockbufsize(0);
socklen_t size(sizeof (int));
int ret_val = getsockopt(this->tcp_socket, SOL_SOCKET, SO_RCVBUF,
(char *) &sockbufsize, &size);
if (ret_val < 0)
perror("getsockopt");
return sockbufsize;
}
int tcp_client::get_tcp_tx_buffer_size(void) {
int sockbufsize(0);
socklen_t size(sizeof (int));
int ret_val = getsockopt(this->tcp_socket, SOL_SOCKET, SO_SNDBUF,
(char *) &sockbufsize, &size);
if (ret_val < 0)
perror("getsockopt");
return sockbufsize;
}
/*! tcp_client::connected - test eof on streams.
*
* if either FILE stream at eof returns true.
*/
bool tcp_client::connected(void) {
if (rx == NULL) return false;
if (tx == NULL) return false;
this->lock_feof();
bool connected_((!feof(rx) && !feof(tx)));
this->unlock_feof();
return connected_;
}
/*! tcp_client::read - read from rx stream.
*
* calls fread() on rx stream.
*/
size_t tcp_client::read(void *data, const size_t &size,
const size_t &count) {
if (!connected()) {
syslog(LOG_DEBUG, "can not read, no stream available");
return -1;
}
size_t read_(fread(data, size, count, this->rx));
return read_;
}
int tcp_client::read_char(void) {
if (!connected()) {
syslog(LOG_DEBUG, "can not read, no stream available");
return -1;
}
int read_(fgetc(this->rx));
return read_;
}
/*! tcp_client::write - write tx stream.
*
* calls fwrite() on tx stream.
*/
size_t tcp_client::write(void *data, const size_t &size,
const size_t &count) {
if (!connected()) {
syslog(LOG_DEBUG, "can not write, no stream available");
return -1;
}
this->lock_write();
size_t write_(fwrite(data, size, count, this->tx));
this->unlock_write();
return write_;
}
/*! tcp_client::write_formated - format data to stream.
*
* calls fprintf() on tx stream.
*/
int tcp_client::write_formated(const char *format, const int &a,
const int &b) {
if (!connected()) {
syslog(LOG_DEBUG, "can not write_formated, no stream available");
return -1;
}
this->lock_write();
int write_(fprintf(this->tx, format, a, b));
this->unlock_write();
return write_;
}
/*! tcp_client::tx_flush - flush tx stream.
*
* calls fflush() on tx stream to send data
* and empty buffer.
*/
int tcp_client::tx_flush(void) {
this->lock_write();
int rc(fflush(this->tx));
this->unlock_write();
return rc;
}
/*! tcp_client::rx_flush - flush rx stream.
*
* calls fflush() on rx stream to send data
* and empty buffer.
*/
int tcp_client::rx_flush(void) {
int rc(fflush(this->rx));
return rc;
}