Skip to content

Commit

Permalink
RT3873 Add traffic counters
Browse files Browse the repository at this point in the history
Add data counters to SSL structure bytes_written and bytes_read

(cherry picked from commit 7709c18)

Conflicts:
	include/openssl/ssl.h
	ssl/record/rec_layer_s3.c
  • Loading branch information
Dancer Vesperman authored and tmshort committed Jun 18, 2015
1 parent 1f26946 commit 956053b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
28 changes: 28 additions & 0 deletions doc/ssl/SSL_get_byte_counters.pod
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=pod

=head1 NAME

SSL_get_byte_counters - get read and write byte counters for a connection

=head1 SYNOPSIS

#include <openssl/ssl.h>

int SSL_get_byte_counters(SSL *ssl, size_t *write, size_t *read);

=head1 DESCRIPTION

SSL_get_byte_counters() returns the number of bytes read or written on the SSL connection.
Non-NULL B<write> or B<read> arguments are filled with the appropriate byte count.

=head1 RETURN VALUES

The number of bytes read are returned in B<*read>.

The number of bytes written are returned in B<*write>.

=head1 SEE ALSO

L<ssl(3)|ssl(3)>

=cut
1 change: 1 addition & 0 deletions include/openssl/ssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,7 @@ void SSL_set_not_resumable_session_callback(SSL *ssl,
void SSL_set_debug(SSL *s, int debug);
__owur int SSL_cache_hit(SSL *s);
__owur int SSL_is_server(SSL *s);
void SSL_get_byte_counters(SSL *s, size_t *w, size_t *r);

__owur __owur SSL_CONF_CTX *SSL_CONF_CTX_new(void);
int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx);
Expand Down
1 change: 1 addition & 0 deletions ssl/record/rec_layer_s3.c
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ int ssl3_writev_bytes(SSL *s, int type, const ssl_bucket *buckets,
s->rlayer.wnum = tot;
return i;
}
s->bytes_written += i;

if ((i == (int)n) ||
(type == SSL3_RT_APPLICATION_DATA &&
Expand Down
4 changes: 4 additions & 0 deletions ssl/record/ssl3_record.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ int ssl3_get_record(SSL *s)
SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0);
if (n <= 0)
return (n); /* error or non-blocking */
s->bytes_read += n;
RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);

p = RECORD_LAYER_get_packet(&s->rlayer);
Expand Down Expand Up @@ -320,6 +321,7 @@ int ssl3_get_record(SSL *s)
n = ssl3_read_n(s, i, i, 1);
if (n <= 0)
return (n); /* error or non-blocking io */
s->bytes_read += n;
}

/* set state for later operations */
Expand Down Expand Up @@ -1413,6 +1415,7 @@ int dtls1_get_record(SSL *s)
/* read timeout is handled by dtls1_read_bytes */
if (n <= 0)
return (n); /* error or non-blocking */
s->bytes_read += n;

/* this packet contained a partial record, dump it */
if (RECORD_LAYER_get_packet_length(&s->rlayer) != DTLS1_RT_HEADER_LENGTH) {
Expand Down Expand Up @@ -1482,6 +1485,7 @@ int dtls1_get_record(SSL *s)
RECORD_LAYER_reset_packet_length(&s->rlayer);
goto again;
}
s->bytes_read += n;

/*
* now n == rr->length, and s->packet_length ==
Expand Down
10 changes: 10 additions & 0 deletions ssl/ssl_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ int SSL_clear(SSL *s)
s->session = NULL;
}

s->bytes_read = 0;
s->bytes_written = 0;
s->error = 0;
s->hit = 0;
s->shutdown = 0;
Expand Down Expand Up @@ -3540,4 +3542,12 @@ void SSL_CTX_share_session_cache(SSL_CTX *a, SSL_CTX *b)
CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
}

void SSL_get_byte_counters(SSL *s, size_t *w, size_t *r)
{
if (w)
*w = s->bytes_written;
if (r)
*r = s->bytes_read;
}

IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id);
4 changes: 4 additions & 0 deletions ssl/ssl_locl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,10 @@ struct ssl_st {
SSL_key_exch_prep_ctx kx_sign;
} ctx; /* context/closure handed out to task */
} task;

/* Keep track of bytes passed through SSL */
size_t bytes_written;
size_t bytes_read;
};


Expand Down

0 comments on commit 956053b

Please sign in to comment.