Newer
Older
/*
This file is part of SLS.
Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
SLS 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.
SLS 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 SLS; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "sls.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) : socket_fd_(fd), local_port_(-1), remote_port_(-1), id_(0) {
pthread_mutex_init(&c_mutex_, 0);
pthread_mutex_init(&r_mutex_, 0);
pthread_mutex_init(&w_mutex_, 0);
if (socket_fd_ >= 0) {
id_ = getconnid();
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
356
357
358
359
360
361
362
363
364
}
/**
* Destructor
*/
Connection::~Connection() {
pthread_mutex_destroy(&c_mutex_);
pthread_mutex_destroy(&r_mutex_);
pthread_mutex_destroy(&w_mutex_);
}
/**
* Create connection with @param addr on port @param port
*/
void Connection::connect(const char *addr, int port) {
if (socket_fd_ >= 0)
throw Error(ERR_NET, "Connection already established but trying to connect");
if (port <= 0)
throw Error(ERR_NET, "Trying to Connect on a bad port number");
pthread_mutex_lock(&c_mutex_);
pthread_mutex_lock(&r_mutex_);
pthread_mutex_lock(&w_mutex_);
// retrieve remote host info
struct hostent *h = gethostbyname(addr);
if (h == 0) {
pthread_mutex_unlock(&w_mutex_);
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(&w_mutex_);
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) {
disconnect_();
pthread_mutex_unlock(&w_mutex_);
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&c_mutex_);
throw Error(ERR_NET, (string) "Unable to connect to " + addr + ": " + hstrerror(h_errno));
}
// set infos
setallinfo_();
pthread_mutex_unlock(&w_mutex_);
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&c_mutex_);
}
/**
* Listen on @param port with queue size of max @param max
*/
void Connection::listen(int port, int max) {
if (socket_fd_ >= 0)
throw Error(ERR_NET, "Connection already established but trying to listen");
// lock
pthread_mutex_lock(&c_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
setallinfo_();
pthread_mutex_unlock(&w_mutex_);
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&c_mutex_);
}
/**
* Disconnect socket
*/
void Connection::disconnect() {
if (socket < 0)
throw Error(ERR_NET, "No connection established but trying to disconnect");
// lock all mutex
pthread_mutex_lock(&c_mutex_);
pthread_mutex_lock(&r_mutex_);
pthread_mutex_lock(&w_mutex_);
disconnect_();
pthread_mutex_unlock(&w_mutex_);
pthread_mutex_unlock(&r_mutex_);
pthread_mutex_unlock(&c_mutex_);
}
/**
* Return the connection sate
* Cannot be const, du to mutex locking
*
* @return see description
*/
bool Connection::connected() {
// get connection mutex
pthread_mutex_lock(&c_mutex_);
bool ret = (socket_fd_ >= 0);
pthread_mutex_unlock(&c_mutex_);
return ret;
}
/**
* Accept new connection on a listening socket
*
* @return null on error, else a new connection
*/
Connection *Connection::accept() {
if (socket < 0)
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;
pthread_mutex_lock(&r_mutex_);
pthread_mutex_lock(&w_mutex_);
if ((r_fd = ::accept(socket_fd_, (struct sockaddr *) &r_addr, &r_sin_size)) == -1) {
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 char* buf, size_t len) {
if (socket < 0)
throw Error(ERR_NET, "No connection established but trying to send data");
pthread_mutex_lock(&w_mutex_);
int ret = ::send(socket_fd_, buf, len, 0);
pthread_mutex_unlock(&w_mutex_);
if (ret == -1 && errno == ECONNRESET)
throw Error(ERR_NET, "Connection reset by peer");
if (ret == -1)
throw Error(ERR_NET, "send: " + (string) strerror(errno));
}
/**
* Send data on @param buf of size @param len followed by '\n' on socket
*/
void Connection::sendln(const char* buf, size_t len) {
if (socket < 0)
throw Error(ERR_NET, "No connection established but trying to send line");
pthread_mutex_lock(&w_mutex_);
// write data
int ret = ::send(socket_fd_, buf, len, 0);
if (ret == -1) {
pthread_mutex_unlock(&w_mutex_);
if (errno == ECONNRESET)
throw Error(ERR_NET, "Connection reset by peer");
throw Error(ERR_NET, "sendln: " + (string) strerror(errno));
}
// write '\n'
ret = ::send(socket_fd_, "\n", 1, 0);
pthread_mutex_unlock(&w_mutex_);
if (ret == -1)
throw Error(ERR_NET, "sendln: " + (string) strerror(errno));
}
/**
* Receive a line
*/
string Connection::recvln() {
if (socket < 0)
throw Error(ERR_NET, "No connection established but trying to receive line");
pthread_mutex_lock(&r_mutex_);
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;
}
// do poll fd here ?
// read data
static char local_buf[MAX_LINE_SIZE];
int ret = ::recv(socket_fd_, local_buf, MAX_LINE_SIZE, 0);
if (ret == 0) {
pthread_mutex_unlock(&r_mutex_);
throw Error(ERR_NET, "Connection reset by peer");
}
if (ret == -1) {
*local_buf = 0;
// unlock before error
pthread_mutex_unlock(&r_mutex_);
throw Error(ERR_NET, (string) "recvln: " + strerror(errno));
}
else local_buf[ret] = 0;
// add read data in buffer
rbuf_ += local_buf;
} while (1);
assert(1);
}
string Connection::getlocalip() {
pthread_mutex_lock(&c_mutex_);
if (local_ip_.empty())
setlocalip_();
string ip = local_ip_;
pthread_mutex_unlock(&c_mutex_);
return ip;
}
int Connection::getlocalport() {
pthread_mutex_lock(&c_mutex_);
if (local_port_ == -1)
setlocalport_();
int port = local_port_;
pthread_mutex_unlock(&c_mutex_);
return port;
}
string Connection::getremoteip() {
pthread_mutex_lock(&c_mutex_);
if (remote_ip_.empty())
setremoteip_();
string ip = remote_ip_;
pthread_mutex_unlock(&c_mutex_);
return ip;
}
int Connection::getremoteport() {
pthread_mutex_lock(&c_mutex_);
if (remote_port_ == -1)
setremoteport_();
int port = remote_port_;
pthread_mutex_unlock(&c_mutex_);
return port;
}
unsigned long int Connection::getid() {
return id_;
}
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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
int Connection::getsocket() {
pthread_mutex_lock(&c_mutex_);
int ret = socket_fd_;
pthread_mutex_unlock(&c_mutex_);
return ret;
}
/*******************************************************************************
** Protected method
******************************************************************************/
/**
* Reserve a socket
* No mutex used !
*/
void Connection::socket_() {
assert(socket_fd_ == -1);
int yes = 1;
if ((socket_fd_ = ::socket(AF_INET, SOCK_STREAM, 0)) == -1)
throw Error(ERR_NET, (string) "Unable to open socket: " + strerror(errno));
if (::setsockopt(socket_fd_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
throw Error(ERR_NET, (string) "Unable to set socket options: " + strerror(errno));
}
/**
* Free a socket
* No mutex used !
*/
void Connection::disconnect_() {
assert(socket_fd_ >= 0);
close(socket_fd_);
socket_fd_ = -1;
}
/**
* Set local ip
* No mutex used !
*/
void Connection::setlocalip_() {
struct sockaddr_in addr;
socklen_t len = sizeof addr;
::getsockname(socket_fd_, (struct sockaddr*) &addr, &len);
local_ip_ = inet_ntoa(addr.sin_addr);
}
/**
* Set local port
* No mutex used !
*/
void Connection::setlocalport_() {
struct sockaddr_in addr;
socklen_t len = sizeof addr;
::getsockname(socket_fd_, (struct sockaddr*) &addr, &len);
local_port_ = ntohs(addr.sin_port);
}
/**
* Set remote ip
* No mutex used !
*/
void Connection::setremoteip_() {
struct sockaddr_in addr;
socklen_t len = sizeof addr;
::getpeername(socket_fd_, (struct sockaddr*) &addr, &len);
remote_ip_ = inet_ntoa(addr.sin_addr);
}
/**
* Set remote port
* No mutex used !
*/
void Connection::setremoteport_() {
struct sockaddr_in addr;
socklen_t len = sizeof addr;
::getpeername(socket_fd_, (struct sockaddr*) &addr, &len);
remote_port_ = ntohs(addr.sin_port);
}