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
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
|
// -*- C++ -*-
//
// $Id$
#include "ace/Handle_Set.h"
#include "ace/Log_Msg.h"
#include <openssl/err.h>
#include "SSL_SOCK_Stream.h"
#if defined (ACE_LACKS_INLINE_FUNCTIONS)
#include "SSL_SOCK_Stream.i"
#endif
ACE_RCSID (ACE_SSL,
SSL_SOCK_Stream,
"$Id$")
ACE_ALLOC_HOOK_DEFINE(ACE_SSL_SOCK_Stream)
ACE_SSL_SOCK_Stream::ACE_SSL_SOCK_Stream (ACE_SSL_Context *context)
: ssl_ (0),
stream_ ()
{
ACE_TRACE ("ACE_SSL_SOCK_Stream::ACE_SSL_SOCK_Stream");
ACE_SSL_Context * ctx =
(context == 0 ? ACE_SSL_Context::instance () : context);
this->ssl_ = ::SSL_new (ctx->context ());
if (this->ssl_ != 0)
{
::SSL_set_verify (this->ssl_,
ctx->default_verify_mode (),
0);
}
else
{
ACE_ERROR ((LM_ERROR,
"(%P|%t) ACE_SSL_SOCK_Stream "
"- cannot allocate new SSL structure %p\n",
ACE_TEXT ("")));
}
}
ACE_SSL_SOCK_Stream::~ACE_SSL_SOCK_Stream (void)
{
ACE_TRACE ("ACE_SSL_SOCK_Stream::~ACE_SSL_SOCK_Stream");
::SSL_free (this->ssl_);
this->ssl_ = 0;
// @@ Question: should we reference count the Context object or
// leave that to the application developer? We do not reference
// count reactors (for example) and following some simple rules
// seems to work fine!
}
ssize_t
ACE_SSL_SOCK_Stream::sendv (const iovec iov[],
size_t n,
const ACE_Time_Value *max_wait_time) const
{
ACE_TRACE ("ACE_SSL_SOCK_Stream::sendv");
// There is subtle problem in this method that occurs when using
// non-blocking IO. The semantics of a non-blocking scatter write
// (sendv()) are not possible to retain with the emulation in this
// method.
ssize_t bytes_sent = 0;
ACE_Time_Value t;
ACE_Time_Value *timeout =
ACE_const_cast (ACE_Time_Value *, max_wait_time);
if (max_wait_time != 0)
{
// Make a copy since ACE_Countdown_Time modifies the
// ACE_Time_Value.
t = *max_wait_time;
timeout = &t;
}
// Take into account the time between each send.
ACE_Countdown_Time countdown (timeout);
for (size_t i = 0; i < n; ++i)
{
const ssize_t result = this->send (iov[i].iov_base,
iov[i].iov_len,
timeout);
if (result == -1)
{
// There is a subtle difference in behaviour depending on
// whether or not any data was sent. If no data was sent,
// then always return -1. Otherwise return bytes_sent.
// This gives the caller an opportunity to keep track of
if (bytes_sent > 0)
break;
else
return -1;
}
else
{
bytes_sent += result;
// Do not continue on to the next loop iteration if the
// amount of data sent was less than the amount data given.
// This avoids a subtle problem where "holes" in the data
// stream would occur if partial sends of a given buffer in
// the iovec array occured.
if (ACE_static_cast (size_t, result) < iov[i].iov_len)
break;
}
(void) countdown.update ();
}
return bytes_sent;
}
ssize_t
ACE_SSL_SOCK_Stream::recvv (iovec *io_vec,
const ACE_Time_Value *timeout) const
{
ACE_TRACE ("ACE_SSL_SOCK_Stream::recvv");
// From ACE_SOCK_IO::recvv().
#if defined (FIONREAD)
ACE_Handle_Set handle_set;
handle_set.reset ();
handle_set.set_bit (this->get_handle ());
io_vec->iov_base = 0;
// Check the status of the current socket.
switch (ACE_OS::select (int (this->get_handle ()) + 1,
handle_set,
0, 0,
timeout))
{
case -1:
return -1;
/* NOTREACHED */
case 0:
errno = ETIME;
return -1;
/* NOTREACHED */
default:
// Goes fine, fallthrough to get data
break;
}
u_long inlen;
if (ACE_OS::ioctl (this->get_handle (),
FIONREAD,
(u_long *) &inlen) == -1)
return -1;
else if (inlen > 0)
{
ACE_NEW_RETURN (io_vec->iov_base,
char[inlen],
-1);
io_vec->iov_len = this->recv (io_vec->iov_base,
inlen);
return io_vec->iov_len;
}
else
return 0;
#else
ACE_UNUSED_ARG (io_vec);
ACE_UNUSED_ARG (timeout);
ACE_NOTSUP_RETURN (-1);
#endif /* FIONREAD */
}
ssize_t
ACE_SSL_SOCK_Stream::send (const void *buf,
size_t len,
int flags,
const ACE_Time_Value *timeout) const
{
ACE_TRACE ("ACE_SSL_SOCK_Stream::send");
// If SSL has data in the buffer, i.e. SSL_pending() returns a
// non-zero value, then don't block on select().
if (timeout == 0 || ::SSL_pending (this->ssl_))
return this->send (buf, len, flags);
int val = 0;
if (ACE::enter_send_timedwait (this->get_handle (),
timeout,
val) == -1)
return -1;
ssize_t bytes_transferred = this->send (buf, len, flags);
ACE::restore_non_blocking_mode (this->get_handle (), val);
return bytes_transferred;
}
ssize_t
ACE_SSL_SOCK_Stream::recv (void *buf,
size_t n,
int flags,
const ACE_Time_Value *timeout) const
{
ACE_TRACE ("ACE_SSL_SOCK_Stream::recv");
return this->recv_i (buf, n, flags, timeout);
}
ssize_t
ACE_SSL_SOCK_Stream::send (size_t n, ...) const
{
ACE_TRACE ("ACE_SSL_SOCK_Stream::send");
const size_t total_tuples = n / 2;
va_list argp;
va_start (argp, n);
ssize_t bytes_sent = 0;
// NOTE: This method used to fill an IO vector (e.g. iovec) and then
// send it using a scatter write (sendv()). However, it is
// not possible to emulate a non-blocking scatter write over
// SSL. As such, there is no point in attempting to use
// scatter writes over SSL.
for (size_t i = 0; i < total_tuples; ++i)
{
const ssize_t data_len = va_arg (argp, ssize_t);
const ssize_t result = this->send (va_arg (argp, char *),
data_len);
if (result == -1)
{
// There is a subtle difference in behaviour depending on
// whether or not any data was sent. If no data was sent,
// then always return -1. Otherwise return bytes_sent.
// This gives the caller an opportunity to keep track of
// which data was actually sent.
if (bytes_sent > 0)
break;
else
{
va_end (argp);
return -1;
}
}
else
{
bytes_sent += result;
// Do not continue on to the next loop iteration if the
// amount of data sent was less than the amount of data
// given. This avoids a subtle problem where "holes" in the
// data stream would occur if partial sends of a given
// buffer in the varargs occured.
if (result < data_len)
break;
}
}
va_end (argp);
return bytes_sent;
}
ssize_t
ACE_SSL_SOCK_Stream::recv (size_t n, ...) const
{
ACE_TRACE ("ACE_SSL_SOCK_Stream::recv");
const size_t total_tuples = n / 2;
va_list argp;
va_start (argp, n);
ssize_t bytes_recv = 0;
for (size_t i = 0; i < total_tuples; ++i)
{
const ssize_t data_len = va_arg (argp, ssize_t);
const ssize_t result = this->recv (va_arg (argp, char *),
data_len);
if (result == -1)
{
// There is a subtle difference in behaviour depending on
// whether or not any data was received. If no data was
// received, then always return -1. Otherwise return
// bytes_received. This gives the caller an opportunity to
// keep track of which data was actually received.
if (bytes_recv > 0)
break;
else
{
va_end (argp);
return -1;
}
}
else
{
bytes_recv += result;
// Do not continue on to the next loop iteration if the
// amount of data received was less than the amount of data
// desired. This avoids a subtle problem where "holes" in
// the data stream would occur if partial receives of a
// given buffer in the varargs occured.
if (result < data_len)
break;
}
}
va_end (argp);
return bytes_recv;
}
ssize_t
ACE_SSL_SOCK_Stream::send_n (const void *buf,
size_t len,
int flags,
const ACE_Time_Value *timeout,
size_t *bt) const
{
ACE_TRACE ("ACE_SSL_SOCK_Stream::send_n");
// No support for send flags in SSL.
if (flags != 0)
ACE_NOTSUP_RETURN (-1);
/* This code mimics ACE::send_n */
// Total number of bytes written.
size_t temp = 0;
size_t &bytes_transferred = ((bt == 0) ? temp : *bt);
// Actual number of bytes written in each <send> attempt
ssize_t n = 0;
for (bytes_transferred = 0;
bytes_transferred < len;
bytes_transferred += n)
{
n = this->send ((const char*) buf + bytes_transferred,
len - bytes_transferred,
flags,
timeout);
if (n < 0)
{
if (errno == EWOULDBLOCK)
{
// If blocked, try again.
n = 0;
continue;
}
else
return -1;
}
else if (n == 0)
break;
}
return bytes_transferred;
}
ssize_t
ACE_SSL_SOCK_Stream::recv_n (void *buf,
size_t len,
int flags,
const ACE_Time_Value *timeout,
size_t *bt) const
{
ACE_TRACE ("ACE_SSL_SOCK_Stream::recv_n");
if (flags != 0)
{
if ((flags | MSG_PEEK) != MSG_PEEK)
ACE_NOTSUP_RETURN (-1);
}
size_t temp = 0;
size_t &bytes_transferred = ((bt == 0) ? temp : *bt);
ssize_t n = 0;
for (bytes_transferred = 0;
bytes_transferred < len;
bytes_transferred += n)
{
n = this->recv ((char*) buf + bytes_transferred,
len - bytes_transferred,
flags,
timeout);
if (n < 0)
{
if (errno == EWOULDBLOCK)
{
// If blocked, try again.
n = 0;
continue;
}
else
return -1;
}
else if (n == 0)
break;
}
return bytes_transferred;
}
ssize_t
ACE_SSL_SOCK_Stream::recv_n (void *buf, int len, int flags) const
{
ACE_TRACE ("ACE_SSL_SOCK_Stream::recv_n");
if (flags != 0)
{
if ((flags | MSG_PEEK) != MSG_PEEK)
ACE_NOTSUP_RETURN (-1);
}
ssize_t bytes_transferred = 0;
ssize_t n = 0;
for (bytes_transferred = 0;
bytes_transferred < len;
bytes_transferred += n)
{
n = this->recv ((char*) buf + bytes_transferred,
len - bytes_transferred,
flags);
if (n < 0)
{
if (errno == EWOULDBLOCK)
{
// If blocked, try again.
n = 0;
continue;
}
else
return -1;
}
else if (n == 0)
break;
}
return bytes_transferred;
}
ssize_t
ACE_SSL_SOCK_Stream::send_n (const void *buf, int len, int flags) const
{
ACE_TRACE ("ACE_SSL_SOCK_Stream::send_n");
// Send flags are unsupported in SSL
if (flags != 0)
ACE_NOTSUP_RETURN (-1);
/* The following code mimics <ACE::send_n> */
size_t bytes_transferred = 0;
ssize_t n = 0;
for (bytes_transferred = 0;
bytes_transferred < (size_t) len;
bytes_transferred += n)
{
n = this->send ((const char*) buf + bytes_transferred,
len - bytes_transferred,
flags);
if (n < 0)
{
if (errno == EWOULDBLOCK)
{
// If blocked, try again.
n = 0;
continue;
}
else
return -1;
}
else if (n == 0)
break;
}
return bytes_transferred;
}
ssize_t
ACE_SSL_SOCK_Stream::sendv_n (const iovec iov[], size_t iovcnt) const
{
ACE_TRACE ("ACE_SSL_SOCK_Stream::sendv_n");
ssize_t bytes_sent = 0;
for (size_t i = 0; i < iovcnt; ++i)
{
ssize_t result = this->send_n (iov[i].iov_base,
iov[i].iov_len);
if (result == -1)
{
// There is a subtle difference in behaviour depending on
// whether or not any data was sent. If no data was sent,
// then always return -1. Otherwise return bytes_sent.
// This gives the caller an opportunity to keep track of
// which data was actually sent.
if (bytes_sent > 0)
break;
else
return -1;
}
else
bytes_sent += result;
}
return bytes_sent;
}
ssize_t
ACE_SSL_SOCK_Stream::recvv_n (iovec iov[], size_t iovcnt) const
{
ACE_TRACE ("ACE_SSL_SOCK_Stream::recvv_n");
ssize_t bytes_read = 0;
for (size_t i = 0; i < iovcnt; ++i)
{
ssize_t result = this->recv_n (iov[i].iov_base,
iov[i].iov_len);
if (result == -1)
{
// There is a subtle difference in behaviour depending on
// whether or not any data was read. If no data was read,
// then always return -1. Otherwise return bytes_read.
// This gives the caller an opportunity to keep track of
// which data was actually read.
if (bytes_read > 0)
break;
else
return -1;
}
else
bytes_read += result;
}
return bytes_read;
}
int
ACE_SSL_SOCK_Stream::get_remote_addr (ACE_Addr &addr) const
{
// Some applications use get_remote_addr() as a way of determining
// whether or not a connection has been established. In SSL's case,
// the remote addr will be available once the TCP handshake has been
// complete. Despite that fact, the SSL connection may not have
// been completed. In such a case, a successful return from
// get_remote_addr() would be misleading.
if (SSL_is_init_finished (this->ssl_))
return this->ACE_SSL_SOCK::get_remote_addr (addr);
if (this->get_handle () == ACE_INVALID_HANDLE)
errno = EBADF;
else
errno = ENOTCONN;
return -1;
}
|