-
Notifications
You must be signed in to change notification settings - Fork 1
/
Connection.cpp
520 lines (449 loc) · 14.9 KB
/
Connection.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
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
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
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
#include "Connection.h"
#include "Message.h"
#include "PeerID.h"
#include "CredentialManager.h"
#include "MessageCore.h"
#include "CryptoHelper.h"
#include "BValue.h"
#include "BDecoder.h"
#include "RainSandboxApp.h"
#include <openssl/err.h>
using namespace RAIN;
extern MessageCore *globalMessageCore;
/* a connection is deemed unconnectable if it fails to connected more
* than MAX_FAILURE_COUNT times */
#define MAX_FAILURE_COUNT 2
/** constructs a connection for server usage */
Connection::Connection(SSL *ssl)
: failureCount(0)
{
this->ssl = ssl;
this->ctx = NULL;
this->pid = new PeerID(false);
this->client = false;
this->isConnected = true;
if (this->pid->address.Length() == 0)
{
/* thanks to 'Joseph Bruni' (brunij@earthlink.net)
* Subject: Re: determining incoming connection address using BIOs */
int sk;
#ifdef __WINDOWS__
int skaddrlen;
#else
socklen_t skaddrlen;
#endif
sockaddr_in skaddr;
skaddrlen = sizeof(skaddr);
BIO_get_fd(SSL_get_wbio(this->ssl), &sk);
getpeername(sk, (sockaddr*) &skaddr, &skaddrlen);
this->pid->address = wxString::Format("%s", inet_ntoa(skaddr.sin_addr));
}
wxLogVerbose("Connection::Connection(server): Connection accepted from %s", this->pid->address.c_str());
this->ImportCertificate();
}
/** constructs a connection for client usage */
Connection::Connection(SSL_CTX *ctx, const wxString& addr, bool connectNow)
: failureCount(0)
{
this->ssl = NULL;
this->ctx = ctx;
this->pid = new PeerID(false);
this->client = true;
this->isConnected = false;
this->pid->address = wxString(addr);
if (connectNow)
{
this->Connect();
if (this->isConnected)
wxLogVerbose("Connection::Connection(client): Connection made to %s", this->pid->address.c_str());
else
return;
}
}
Connection::~Connection()
{
this->Disconnect(true);
delete this->pid;
}
/** sets the connection's messagecore */
void Connection::SetMessageCore(MessageCore *core)
{
this->core = core;
}
void Connection::ImportCertificate()
{
if (this->ssl)
{
X509 *cert = SSL_get_peer_certificate(this->ssl);
if (!cert)
{
wxLogError("Connection::ImportCertificate(): No Peer Certificate");
return;
}
this->pid->SetCertificate(cert);
X509_free(cert);
}
}
/** tries to create an SSL connection to the peer
*
* only valid for client connections */
bool Connection::Connect()
{
BIO *client_bio = NULL;
if (this->ssl != NULL)
{
this->Disconnect();
}
wxLogStatus("Trying to connect to %s", this->pid->address.c_str());
client_bio = BIO_new_connect((char *)this->pid->address.c_str());
BIO_set_nbio(client_bio, 1);
// TODO: add timeout here
int err;
while ((err = BIO_do_connect(client_bio)) <= 0)
{
int shouldretry = BIO_should_retry(client_bio);
if (shouldretry == 0)
{
wxLogError("Connection::Connect(): BIO_do_connect() failed for '%s' errorcode %d", this->pid->address.c_str(), err);
this->isConnected = false;
BIO_free_all(client_bio);
this->failureCount++;
return false;
} else {
wxThread::Sleep(100);
}
}
if (!(this->ssl = SSL_new(this->ctx)))
{
wxLogError("Connection::Connect(): SSL_new() failed for '%s'", this->pid->address.c_str());
this->isConnected = false;
BIO_free_all(client_bio);
this->failureCount++;
return false;
}
SSL_set_bio(this->ssl, client_bio, client_bio);
while ((err = SSL_connect(this->ssl)) <= 0)
{
if (err != -1)
{
wxLogError("Connection::Connect(): SSL_connect() failed for '%s'", this->pid->address.c_str());
this->isConnected = false;
/* SSL_free also frees the bio which we successfully attached
* to it above */
SSL_free(this->ssl);
this->ssl = NULL;
this->failureCount++;
return false;
} else {
int shouldretry_ssl = SSL_get_error(this->ssl, err);
if (shouldretry_ssl == SSL_ERROR_WANT_READ || shouldretry_ssl == SSL_ERROR_WANT_WRITE)
{
wxThread::Sleep(100);
} else {
//wxLogError("SSL_connect() failed; wanted something other than read or write (!!!)");
this->isConnected = false;
this->failureCount++;
return false;
}
}
}
//wxLogVerbose("SSL_connect() succeeded to '%s' with cipher %s", this->pid->address.c_str(), SSL_get_cipher(this->ssl));
this->isConnected = true;
this->since = wxDateTime::Now();
this->ImportCertificate();
wxASSERT(globalMessageCore);
Message *msg = new Message();
msg->status = MSG_STATUS_LOCAL;
msg->subsystem = -1;
msg->headers["Destination"] = new BEnc::String("Broadcast");
msg->headers["Event"] = new BEnc::String("Client-Connection-Made");
msg->headers["From"] = this->pid->AsBValue();
globalMessageCore->PushMessage(msg);
wxLogStatus("Connected to %s", this->pid->address.c_str());
return true;
}
bool Connection::IsConnectable()
{
return (this->isConnected || this->failureCount < MAX_FAILURE_COUNT);
}
/** cleanly disconnects the connection and frees the underlying ssl connection */
void Connection::Disconnect(bool silent)
{
if (this->ssl == NULL)
{
this->isConnected = false;
return;
} else {
int shutdown_s = SSL_shutdown(this->ssl);
/* if ssl_shutdown returns 0, it means the connection is not
* yet shutdown and ssl_shutdown should be called again to complete it */
if (shutdown_s == 0)
{
SSL_shutdown(this->ssl);
} else if (shutdown_s == -1) {
wxLogError("Connection::Disconnect(): SSL_shutdown() failed: fatal connection error");
}
this->isConnected = false;
// could do something with SSL_clear here in the future
SSL_free(ssl);
this->ssl = NULL;
}
if (!silent)
{
Message *msg = new Message();
msg->status = MSG_STATUS_LOCAL;
msg->subsystem = -1;
msg->headers["Destination"] = new BEnc::String("Broadcast");
msg->headers["Event"] = new BEnc::String("Peer-Disconnected");
msg->headers["From"] = this->pid->AsBValue();
globalMessageCore->PushMessage(msg);
}
}
/** polls the connection for incoming messages */
void Connection::Poll()
{
const int sizeReadLength = 16384;
bool readWantsWrite = false;
ERR_clear_error();
wxMutexLocker ml(this->incomingBuffer);
/* check read */
wxString readBuffer;
char *buffer = readBuffer.GetWriteBuf(sizeReadLength);
int read = SSL_read(this->ssl, buffer, sizeReadLength);
readBuffer.UngetWriteBuf(sizeReadLength);
if (read == 0)
{
/* possible disconnection, let's call disconnect to finish the
* disconnection cleanly and/or tidy up the connection */
//wxLogVerbose("Poll::Read calling disconnect - SSL_read returned 0");
this->Disconnect();
return;
} else if (read < 0) {
/* the ssl_read failed, but we need to do something to get it
* to work - let's see why it failed first */
int rFailureCode = SSL_get_error(this->ssl, read);
switch (rFailureCode)
{
case SSL_ERROR_ZERO_RETURN:
/* connection cleanly closed :)
* as above, we'll tidy */
//wxLogVerbose("Poll::Read calling disconnect - SSL_write returned SSL_ERROR_ZERO_RETURN");
this->Disconnect();
break;
case SSL_ERROR_WANT_WRITE:
readWantsWrite = true;
case SSL_ERROR_WANT_READ:
/* these are not fatal errors, we just need to wait for
* whatever renegotiation is occuring to finish, or for
* the underlying tcp socket to get more data
*
* the next poll will try again */
//wxLogVerbose("Poll:Read returned SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE");
break;
default:
/* this is probably SSL_ERROR_SSL or SSL_ERROR_SYSCALL or ...
* we'll force a disconnect this end and tidy */
wxLogVerbose("Connection::Poll::Read calling disconnect - %d = SSL_write returned SSL_ERROR_SSL or SSL_ERROR_SYSCALL: %s", rFailureCode, ERR_error_string(ERR_get_error(), NULL));
this->Disconnect();
break;
}
/* if the reason for ssl_read's failure was fatal, we're going
* be disconnected now
*
* if it wasn't fatal, we'll try reading again next poll */
} else { // (read > 0)
/* read is now the number of bytes we read successfully :)
*
* hooray!
*
* we append the bytes we read to the incoming message buffer,
* then test the message buffer for validity! it will be cleared
* by CheckMessageBuffer if it is unrecoverably malformed or
* correct - a Message will be added to the application's
* message queue if this is the case! */
wxLogVerbose("Connection::Poll(): read %d bytes from %s", read, this->pid->address.c_str());
readBuffer.Truncate(read);
this->incomingMessageBuffer.Append(readBuffer);
this->CheckMessageBuffer();
}
wxMutexLocker locker(outgoingBuffer);
/* check write */
if (this->outgoingMessageBuffer.Length() > 0 || readWantsWrite)
{
if (!this->ssl)
return;
/* we have stuff to write */
int write = SSL_write(this->ssl, this->outgoingMessageBuffer.c_str(), this->outgoingMessageBuffer.Length());
if (write == 0)
{
/* possible disconnection, let's call disconnect to finish the
* disconnection cleanly and/or tidy up the connection */
//wxLogVerbose("Poll::Write calling disconnect - SSL_write returned 0");
this->Disconnect();
return;
} else if (write < 0) {
/* the ssl_read failed, but we need to do something to get it
* to work - let's see why it failed first */
int wFailureCode = SSL_get_error(this->ssl, write);
switch (wFailureCode)
{
case SSL_ERROR_ZERO_RETURN:
/* connection cleanly closed :)
* as above, we'll tidy */
//wxLogVerbose("Poll::Write calling disconnect - SSL_write returned SSL_ERROR_ZERO_RETURN");
this->Disconnect();
break;
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
/* these are not fatal errors, we just need to wait for
* whatever renegotiation is occuring to finish, or for
* the underlying tcp socket to get more data
*
* the next poll will try again */
break;
default:
/* this is probably SSL_ERROR_SSL or SSL_ERROR_SYSCALL or ...
* we'll force a disconnect this end and tidy */
wxLogVerbose("Connection::Poll::Write calling disconnect - %d = SSL_write returned SSL_ERROR_SSL or SSL_ERROR_SYSCALL: %s", wFailureCode, ERR_error_string(ERR_get_error(), NULL));
this->Disconnect();
break;
}
/* if the reason for ssl_write's failure was fatal, we're going
* be disconnected now
*
* if it wasn't fatal, we'll try writing again next poll */
} else { // (write > 0)
/* write is now the number of bytes we read successfully :)
*
* hooray!
*
* we remove 'write' bytes from the front of the
* outgoingMessageBuffer */
wxLogVerbose("Connection::Poll(): wrote %d/%d bytes from %s", write, this->outgoingMessageBuffer.Length(), this->pid->address.c_str());
this->outgoingMessageBuffer = this->outgoingMessageBuffer.Right(this->outgoingMessageBuffer.Length() - write);
/* then flush the underlying bio */
wxLogVerbose("Connection::Poll(): flushed after write: %d", BIO_flush(SSL_get_wbio(this->ssl)));
}
}
}
/** checks the incoming message buffer, and dispatch()es a message if it
* is valid */
void Connection::CheckMessageBuffer()
{
/* this is called every time Poll() gets some valid data, there must
* be a better way - but it'll do for now */
long length = this->incomingMessageBuffer.Length();
int i = 0;
if (length == 0)
{
/* this should never happen */
return;
} else {
/* possibly, the incoming buffer has a valid message for us!
*
* let's see how bdecoder handles what we have */
BEnc::Decoder decoder;
wxLogVerbose("Connection::CheckMessageBuffer(): trying to bdecode buffer");
/* note that verify will not mess up our buffer it doesn't contain
* a full valid message */
decoder.Verify(this->incomingMessageBuffer);
bool gotMessage = false;
switch (decoder.errorState)
{
case BENC_NOERROR:
wxLogVerbose("Connection::CheckMessageBuffer(): BENC_NOERROR");
/* no error */
gotMessage = true;
break;
case BENC_NOERROR_TOOMUCHDATA:
wxLogVerbose("Connection::CheckMessageBuffer(): BENC_NOERROR_TOOMUCHDATA");
/* more than one message in our buffer is no problem,
* and we can safely extract and push a message into our messagecore */
gotMessage = true;
break;
case BENC_BAD_TOKEN:
wxLogVerbose("Connection::CheckMessageBuffer(): bad token");
case BENC_UNKNOWN_ERROR:
wxLogVerbose("Connection::CheckMessageBuffer(): fatal error");
/* this basically means we have some fragment of bad data before any
* possible good data. we try to find any good data at all, else throw
* the whole thing out */
while (this->incomingMessageBuffer.Length() > 0)
{
decoder.Verify(this->incomingMessageBuffer);
if (decoder.errorState >= BENC_NOERROR)
break;
else
this->incomingMessageBuffer = this->incomingMessageBuffer.Mid(1);
}
/* now we either have a trimmed valid buffer, or no buffer whatsoever */
gotMessage = (decoder.errorState >= BENC_NOERROR);
break;
case BENC_TRUNCATED_STREAM:
/* no problems, we need to do more reads probably */
wxLogVerbose("Connection::CheckMessageBuffer(): truncated stream");
gotMessage = false;
break;
}
if (gotMessage)
{
decoder.Decode(this->incomingMessageBuffer);
Message *m = new Message();
m->status = MSG_STATUS_INCOMING;
decoder.PopulateHash(m->headers);
m->ImportHeaders();
this->core->PushMessage(m);
}
/* if we still have stuff to parse, do it again */
if (gotMessage && this->incomingMessageBuffer.Length() > 0)
this->CheckMessageBuffer();
}
}
/** adds the message to the outgoing message queue, which will be flushed the next
* time Poll() is called, if the connection is open */
bool Connection::SendMessage(Message *msg)
{
if (!this->isConnected)
return false;
// TODO: this isn't threadsafe yet
wxString msgstr = msg->CanonicalForm();
wxMutexLocker locker(outgoingBuffer);
this->outgoingMessageBuffer.Append(msgstr);
//wxLogVerbose("adding %d bytes to connection buffer", msgstr.Length());
return true;
}
/** don't use this - debugging only */
bool Connection::SendRawString(const wxString& what)
{
wxASSERT(0);
if (this->isConnected)
{
SSL_write(this->ssl, what.c_str(), what.Length());
wxLogVerbose("wrote %d bytes to connection %08x", what.Length(), this->ssl);
return true;
} else {
wxLogError("tried writing %d bytes to a closed connection", what.Length());
return false;
}
}
/** gets the number of bytes sent over the underlying connection */
unsigned long Connection::GetBytesSent()
{
if (this->ssl)
return BIO_number_written(this->ssl->rbio);
else
return 0;
}
/** gets the number of bytes received from the underlying connection */
unsigned long Connection::GetBytesRecv()
{
if (this->ssl)
return BIO_number_read(this->ssl->rbio);
else
return 0;
}
/** gets the underlying openssl SSL structure */
SSL * Connection::GetSSL()
{
return this->ssl;
}