Skip to content

Commit 11c7f7f

Browse files
committed
Merge pull request #27 from hanumantmk/GITHUB_26
fix mongoc-stream-tls
2 parents 366f9b4 + 698870e commit 11c7f7f

File tree

2 files changed

+77
-27
lines changed

2 files changed

+77
-27
lines changed

src/mongoc/mongoc-stream-tls.c

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -470,22 +470,56 @@ _mongoc_stream_tls_writev (mongoc_stream_t *stream,
470470
mongoc_stream_tls_t *tls = (mongoc_stream_tls_t *)stream;
471471
ssize_t ret = 0;
472472
size_t i;
473+
size_t iov_pos = 0;
473474
int write_ret;
474475

476+
int64_t now;
477+
int64_t expire = 0;
478+
475479
BSON_ASSERT (tls);
476480
BSON_ASSERT (iov);
477481
BSON_ASSERT (iovcnt);
478482

479483
tls->timeout = timeout_msec;
480484

485+
if (timeout_msec >= 0) {
486+
expire = bson_get_monotonic_time () + (timeout_msec * 1000UL);
487+
}
488+
481489
for (i = 0; i < iovcnt; i++) {
482-
write_ret = BIO_write (tls->bio, iov[i].iov_base, (int)iov[i].iov_len);
490+
iov_pos = 0;
483491

484-
if (write_ret != iov[i].iov_len) {
485-
return write_ret;
486-
}
492+
while (iov_pos < iov[i].iov_len) {
493+
write_ret = BIO_write (tls->bio, (char *)iov[i].iov_base + iov_pos,
494+
(int)(iov[i].iov_len - iov_pos));
495+
496+
if (write_ret < 0) {
497+
return write_ret;
498+
}
499+
500+
if (expire) {
501+
now = bson_get_monotonic_time ();
502+
503+
if ((expire - now) < 0) {
504+
if (write_ret == 0) {
505+
mongoc_counter_streams_timeout_inc();
506+
#ifdef _WIN32
507+
errno = WSAETIMEDOUT;
508+
#else
509+
errno = ETIMEDOUT;
510+
#endif
511+
return -1;
512+
}
487513

488-
ret += write_ret;
514+
tls->timeout = 0;
515+
} else {
516+
tls->timeout = expire - now;
517+
}
518+
}
519+
520+
ret += write_ret;
521+
iov_pos += write_ret;
522+
}
489523
}
490524

491525
if (ret >= 0) {
@@ -527,48 +561,57 @@ _mongoc_stream_tls_readv (mongoc_stream_t *stream,
527561
int read_ret;
528562
size_t iov_pos = 0;
529563
int64_t now;
530-
int64_t expire;
564+
int64_t expire = 0;
531565

532566
BSON_ASSERT (tls);
533567
BSON_ASSERT (iov);
534568
BSON_ASSERT (iovcnt);
535569

536570
tls->timeout = timeout_msec;
537571

538-
expire = bson_get_monotonic_time () + (timeout_msec * 1000UL);
572+
if (timeout_msec >= 0) {
573+
expire = bson_get_monotonic_time () + (timeout_msec * 1000UL);
574+
}
539575

540576
for (i = 0; i < iovcnt; i++) {
541577
iov_pos = 0;
542578

543-
while (iov_pos < iov[i].iov_len - 1) {
579+
while (iov_pos < iov[i].iov_len) {
544580
read_ret = BIO_read (tls->bio, (char *)iov[i].iov_base + iov_pos,
545581
(int)(iov[i].iov_len - iov_pos));
546582

547-
now = bson_get_monotonic_time ();
583+
if (read_ret < 0) {
584+
return read_ret;
585+
}
586+
587+
if (expire) {
588+
now = bson_get_monotonic_time ();
548589

549-
if (((expire - now) < 0) && (read_ret == 0)) {
550-
mongoc_counter_streams_timeout_inc();
590+
if ((expire - now) < 0) {
591+
if (read_ret == 0) {
592+
mongoc_counter_streams_timeout_inc();
551593
#ifdef _WIN32
552-
errno = WSAETIMEDOUT;
594+
errno = WSAETIMEDOUT;
553595
#else
554-
errno = ETIMEDOUT;
596+
errno = ETIMEDOUT;
555597
#endif
556-
return -1;
557-
}
598+
return -1;
599+
}
558600

559-
if (read_ret == -1) {
560-
return read_ret;
601+
tls->timeout = 0;
602+
} else {
603+
tls->timeout = expire - now;
604+
}
561605
}
562606

563607
ret += read_ret;
564608

565-
if (read_ret != iov[i].iov_len) {
566-
if ((size_t)read_ret >= min_bytes) {
567-
return read_ret;
568-
}
569-
570-
iov_pos += read_ret;
609+
if ((size_t)ret >= min_bytes) {
610+
mongoc_counter_streams_ingress_add(ret);
611+
return ret;
571612
}
613+
614+
iov_pos += read_ret;
572615
}
573616
}
574617

tests/ssl-test.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,17 @@ ssl_test_client (void * ptr)
248248
r = mongoc_stream_writev(ssl_stream, &wiov, 1, TIMEOUT);
249249
assert(r == wiov.iov_len);
250250

251-
r = mongoc_stream_readv(ssl_stream, &riov, 1, 4, TIMEOUT);
252-
assert(r > 0);
253-
assert(r == wiov.iov_len);
254-
assert(strcmp(riov.iov_base, wiov.iov_base) == 0);
251+
riov.iov_len = 1;
252+
253+
r = mongoc_stream_readv(ssl_stream, &riov, 1, 1, TIMEOUT);
254+
assert(r == 1);
255+
assert(memcmp(riov.iov_base, "f", 1) == 0);
256+
257+
riov.iov_len = 3;
258+
259+
r = mongoc_stream_readv(ssl_stream, &riov, 1, 3, TIMEOUT);
260+
assert(r == 3);
261+
assert(memcmp(riov.iov_base, "oo", 3) == 0);
255262

256263
mongoc_stream_destroy(ssl_stream);
257264

0 commit comments

Comments
 (0)