Newer
Older
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
/*
This file is part of SLL.
Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
SLL 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; version 2 of the License.
SLL 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 SLL; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "slm.hh"
#include "connection.hh"
#include "error.hh"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <poll.h>
/*******************************************************************************
** Class method
******************************************************************************/
unsigned long int Connection::getconnid() {
static unsigned long int id = 1;
return id++;
}
/*******************************************************************************
** Public method
******************************************************************************/
/**
* Constructor
*
* @param fd socket of the connection (-1) is not exist
*/
Connection::Connection(int fd, bool bufferized)
: socket_fd_(fd), local_port_(-1), remote_port_(-1), id_(0), bufferized_(bufferized) {
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
pthread_mutex_init(&c_mutex_, 0);
pthread_mutex_init(&r_mutex_, 0);
pthread_mutex_init(&w_mutex_, 0);
if (socket_fd_ >= 0)
id_ = getconnid();
}
/**
* Destructor
*/
Connection::~Connection() {
pthread_mutex_destroy(&c_mutex_);
pthread_mutex_destroy(&r_mutex_);
pthread_mutex_destroy(&w_mutex_);
}
/**
* Disconnect socket
*/
void Connection::disconnect() {
// lock connection mutex
pthread_mutex_lock(&c_mutex_);
if (socket_fd_ < 0) {
pthread_mutex_unlock(&c_mutex_);
throw Error(ERR_NET, "No connection established but trying to disconnect");
}
disconnect_();
pthread_mutex_unlock(&c_mutex_);
}
/**
* Create connection with @param addr on port @param port
*/
void Connection::connect(const char *addr, int port) {
// check args
if (addr == 0 || *addr == 0)
throw Error(ERR_NET, "Trying to connect on a bad address");
if (port <= 0 || port >= 65536)
throw Error(ERR_NET, "Trying to connect on a bad port number");
// take connection mutex
pthread_mutex_lock(&c_mutex_);
// check already existant connection
if (socket_fd_ >= 0) {
pthread_mutex_unlock(&c_mutex_);
throw Error(ERR_NET, "Connection already established but trying to connect");
}
// take read and write mutex
pthread_mutex_lock(&w_mutex_);
// retrieve remote host info
struct hostent *h = gethostbyname(addr);
if (h == 0) {
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&c_mutex_);
throw Error(ERR_NET, (string) "Unable to resolve: " + addr + ": " + hstrerror(h_errno));
}
// create socket
try { socket_(); }
catch (...) {
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&c_mutex_);
throw;
}
// fill sockaddr
struct sockaddr_in daddr;
daddr.sin_family = AF_INET;
daddr.sin_port = htons(port);
daddr.sin_addr = *((struct in_addr *) h->h_addr);
memset(daddr.sin_zero, '\0', sizeof daddr.sin_zero);
// connect
if (::connect(socket_fd_, (struct sockaddr *) &daddr, sizeof daddr) == -1) {
int errno__ = errno;
disconnect_();
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&c_mutex_);
throw Error(ERR_NET, (string) "Unable to connect to " + addr + ": " + strerror(errno__));
}
// set infos
try {
setlocalip_();
setlocalport_();
setremoteip_();
setremoteport_();
}
catch (...) {
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&c_mutex_);
throw;
}
pthread_mutex_unlock(&r_mutex_);
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
pthread_mutex_unlock(&c_mutex_);
}
/**
* Listen on @param port with queue size of max @param max
*/
void Connection::listen(int port, int max) {
// check arg
if (port <= 0 || port >= 65536)
throw Error(ERR_NET, "Trying to listen on a bad port number");
if (max <= 0)
throw Error(ERR_NET, "Trying to listen with bad wait queue size");
// take connection mutex
pthread_mutex_lock(&c_mutex_);
if (socket_fd_ >= 0) {
pthread_mutex_unlock(&c_mutex_);
throw Error(ERR_NET, "Connection already established but trying to listen");
}
// lock read and write mutex
pthread_mutex_lock(&r_mutex_);
pthread_mutex_lock(&w_mutex_);
// create socket
try { socket_(); }
catch (...) {
pthread_mutex_unlock(&w_mutex_);
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&c_mutex_);
throw;
}
// fill sockaddr
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port);
saddr.sin_addr.s_addr = INADDR_ANY;
memset(saddr.sin_zero, '\0', sizeof saddr.sin_zero);
// bind on socket
if (bind(socket_fd_, (struct sockaddr *)&saddr, sizeof saddr) == -1) {
disconnect_();
pthread_mutex_unlock(&w_mutex_);
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&c_mutex_);
throw Error(ERR_NET, (string) "Unable to bind: " + strerror(errno));
}
// listen on socket
if (::listen(socket_fd_, max) == -1) {
disconnect_();
pthread_mutex_unlock(&w_mutex_);
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&c_mutex_);
throw Error(ERR_NET, (string) "Unable to listen: " + strerror(errno));
}
// set all infos
try {
setlocalip_();
setlocalport_();
}
catch (...) {
pthread_mutex_unlock(&w_mutex_);
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&c_mutex_);
throw;
}
pthread_mutex_unlock(&w_mutex_);
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&c_mutex_);
}
/**
* Accept new connection on a listening socket
*
* @return null on error, else a new connection
*/
Connection *Connection::accept() {
pthread_mutex_lock(&c_mutex_);
pthread_mutex_lock(&r_mutex_);
pthread_mutex_lock(&w_mutex_);
// get the socket fd
int socket_fd = socket_fd_;
pthread_mutex_unlock(&c_mutex_);
// test socket validity
if (socket_fd < 0) {
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&w_mutex_);
throw Error(ERR_NET, "No connection established but trying to accept");
struct sockaddr_in r_addr;
socklen_t r_sin_size = sizeof r_addr;
int r_fd;
if ((r_fd = ::accept(socket_fd, (struct sockaddr *) &r_addr, &r_sin_size)) == -1) {
int errno__ = errno;
pthread_mutex_lock(&c_mutex_);
disconnect_();
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&w_mutex_);
pthread_mutex_unlock(&c_mutex_);
throw Error(ERR_NET, (string) "accept: " + strerror(errno__));
}
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&w_mutex_);
return new Connection(r_fd);
}
/**
* Send data on @param buf of size @param len on socket
*/
void Connection::send(const void* buf, size_t len) {
// lock mutex for operation
pthread_mutex_lock(&c_mutex_);
pthread_mutex_lock(&w_mutex_);
// retreive socket_fd and free conn mutex
int socket_fd = socket_fd_;
pthread_mutex_unlock(&c_mutex_);
// test socket validity
if (socket_fd < 0) {
pthread_mutex_unlock(&w_mutex_);
throw Error(ERR_NET, "No connection established but trying to send data");
}
// call send
try { send_(socket_fd, buf, len); }
catch (...) {
pthread_mutex_lock(&c_mutex_);
pthread_mutex_lock(&r_mutex_);
disconnect_();
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&w_mutex_);
pthread_mutex_unlock(&c_mutex_);
throw;
}
// release the mutex
pthread_mutex_unlock(&w_mutex_);
}
/**
* Free read buffer and send write buffer if connected
*
*/
void Connection::flush() {
if (!bufferized_)
return;
// lock mutex for operation
pthread_mutex_lock(&c_mutex_);
pthread_mutex_lock(&w_mutex_);
// retreive socket_fd and free conn mutex
int socket_fd = socket_fd_;
pthread_mutex_unlock(&c_mutex_);
// test socket validity
if (socket_fd < 0) {
pthread_mutex_unlock(&w_mutex_);
throw Error(ERR_NET, "No connection established but trying to flush data");
}
// call send
try { send_(socket_fd, rbuf_.c_str(), rbuf_.length()); }
catch (...) {
pthread_mutex_lock(&c_mutex_);
pthread_mutex_lock(&r_mutex_);
disconnect_();
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&w_mutex_);
// release the mutex
pthread_mutex_unlock(&w_mutex_);
}
/**
* Receive raw data
*/
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
int Connection::recv(void *buf, size_t size) {
// lock mutex for operation
pthread_mutex_lock(&c_mutex_);
pthread_mutex_lock(&r_mutex_);
int socket_fd = socket_fd_;
pthread_mutex_unlock(&c_mutex_);
// test socket validity
if (socket_fd < 0) {
pthread_mutex_unlock(&r_mutex_);
throw Error(ERR_NET, "No connection established but trying to receive line");
}
int ret = 0;
if (rbuf_.size() > 0) {
size_t msize = (rbuf.length() > size) ? size : rbuf.length();
memcpy(buf, rbuf_.c_str(), msize);
ret += msize;
rbuf_.remove(0, msize);
}
if (ret < size) {
static char local_buf[MAX_LINE_SIZE];
try { ret = recv_(socket_fd, local_buf, MAX_LINE_SIZE); }
catch (...) {
pthread_mutex_lock(&c_mutex_);
pthread_mutex_lock(&w_mutex_);
*local_buf = 0;
disconnect_();
pthread_mutex_unlock(&w_mutex_);
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&c_mutex_);
throw;
}
local_buf[ret] = 0;
}
// add read data in buffer
rbuf_ += local_buf;
} while (1);
return ret;
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
}
/**
* Receive a line
*/
string Connection::recvln() {
// lock mutex for operation
pthread_mutex_lock(&c_mutex_);
pthread_mutex_lock(&r_mutex_);
int socket_fd = socket_fd_;
pthread_mutex_unlock(&c_mutex_);
// test socket validity
if (socket_fd < 0) {
pthread_mutex_unlock(&r_mutex_);
throw Error(ERR_NET, "No connection established but trying to receive line");
}
do {
// check EOL char
size_t offset = rbuf_.find('\n');
if (offset != string::npos) {
string s = rbuf_.substr(0, offset);
rbuf_.erase(0, offset + 1);
pthread_mutex_unlock(&r_mutex_);
return s;
}
// read data
static char local_buf[MAX_LINE_SIZE];
try { recv_(socket_fd, local_buf, MAX_LINE_SIZE); }
catch (...) {
pthread_mutex_lock(&c_mutex_);
pthread_mutex_lock(&w_mutex_);
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&c_mutex_);
}
local_buf[ret] = 0;
// add read data in buffer
rbuf_ += local_buf;
} while (1);
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
}
string Connection::getlocalip() {
// get local ip is a conn op
pthread_mutex_lock(&c_mutex_);
if (socket_fd_ < 0) {
pthread_mutex_unlock(&c_mutex_);
throw Error(ERR_NET, "No connection established but trying to get local ip");
}
if (local_ip_.empty())
try { setlocalip_(); }
catch (...) {
pthread_mutex_unlock(&c_mutex_);
throw;
}
string ip = local_ip_;
pthread_mutex_unlock(&c_mutex_);
return ip;
}
string Connection::getlocalhostname() {
pthread_mutex_lock(&c_mutex_);
if (local_hostname_.empty()) {
char buf[256];
if (::gethostname(buf, 256) != 0) {
pthread_mutex_unlock(&c_mutex_);
throw Error(ERR_NET, (string) "Unable to get local hostname: " + strerror(errno));
}
try { local_hostname_ = buf; }
catch (...) {
pthread_mutex_unlock(&c_mutex_);
throw;
}
}
pthread_mutex_unlock(&c_mutex_);
return local_hostname_;
}
int Connection::getlocalport() {
// get local port is a conn op
pthread_mutex_lock(&c_mutex_);
if (socket_fd_ < 0) {
pthread_mutex_unlock(&c_mutex_);
throw Error(ERR_NET, "No connection established but trying to get local port");
}
if (local_port_ == -1)
try { setlocalport_(); }
catch (...) {
pthread_mutex_unlock(&c_mutex_);
throw;
}
int port = local_port_;
pthread_mutex_unlock(&c_mutex_);
return port;
}
string Connection::getremoteip() {
// get remote hostname is a conn op
pthread_mutex_lock(&c_mutex_);
if (socket_fd_ < 0) {
pthread_mutex_unlock(&c_mutex_);
throw Error(ERR_NET, "No connection established but trying to get remote ip");
}
if (remote_ip_.empty())
try { setremoteip_(); }
catch (...) {
pthread_mutex_unlock(&c_mutex_);
throw;
}
string ip = remote_ip_;
pthread_mutex_unlock(&c_mutex_);
return ip;
}
string Connection::getremotehostname() {
// get remote ip is a conn op
pthread_mutex_lock(&c_mutex_);
if (socket_fd_ < 0) {
pthread_mutex_unlock(&c_mutex_);
throw Error(ERR_NET, "No connection established but trying to get remote ip");
}
if (remote_hostname_.empty())
try {setremotehostname_(); }
catch (...) {
pthread_mutex_unlock(&c_mutex_);
throw;
}
string hostname = remote_hostname_;
pthread_mutex_unlock(&c_mutex_);
return hostname;
}
int Connection::getremoteport() {
// get remote port is a conn op
pthread_mutex_lock(&c_mutex_);
if (socket_fd_ < 0) {
pthread_mutex_unlock(&c_mutex_);
throw Error(ERR_NET, "No connection established but trying to get remote port");
}
if (remote_port_ == -1)
try {setremoteport_(); }
catch (...) {
pthread_mutex_unlock(&c_mutex_);
throw;
}
int port = remote_port_;
pthread_mutex_unlock(&c_mutex_);
return port;
}
int Connection::getsocket() {
// get socket is a conn op
pthread_mutex_lock(&c_mutex_);
int socket_fd = socket_fd_;
pthread_mutex_unlock(&c_mutex_);
if (socket_fd < 0)
throw Error(ERR_NET, "No connection established but trying to get socket fd");
return socket_fd;
}
unsigned long int Connection::getid() const {
return id_;
}
bool Connection::isbufferized() const {
return bufferized_;
}
/*******************************************************************************
** Protected method
******************************************************************************/