aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2013-03-01 23:30:48 +0000
committerlloyd <[email protected]>2013-03-01 23:30:48 +0000
commitbe38dbbdd58614c887123237219d21df6ea54433 (patch)
treed237c0355075bfd506dfab34d5ed609b04fafb28
parentba20b8f3a49ceb61307e3afea0d8f713d2315353 (diff)
Add sync handshake function to Blocking_Client
-rw-r--r--src/tls/tls_blocking.cpp24
-rw-r--r--src/tls/tls_blocking.h16
2 files changed, 32 insertions, 8 deletions
diff --git a/src/tls/tls_blocking.cpp b/src/tls/tls_blocking.cpp
index ee94f086e..7b773cd81 100644
--- a/src/tls/tls_blocking.cpp
+++ b/src/tls/tls_blocking.cpp
@@ -6,6 +6,7 @@
*/
#include <botan/tls_blocking.h>
+#include <botan/internal/assert.h>
namespace Botan {
@@ -50,16 +51,24 @@ void Blocking_Client::process_data(const byte data[], size_t data_len,
alert_notification(alert);
}
-size_t Blocking_Client::read(byte buf[], size_t buf_len)
+void Blocking_Client::do_handshake()
{
- secure_vector<byte> readbuf(4096);
+ std::vector<byte> readbuf(4096);
- while(m_plaintext.empty())
+ while(!m_channel.is_closed() && !m_channel.is_active())
{
- const size_t readbuf_size = 4096;
- byte readbuf[readbuf_size] = { 0 };
+ const size_t from_socket = m_read_fn(&readbuf[0], readbuf.size());
+ m_channel.received_data(&readbuf[0], from_socket);
+ }
+ }
- const size_t from_socket = m_read_fn(&readbuf[0], readbuf_size);
+size_t Blocking_Client::read(byte buf[], size_t buf_len)
+ {
+ std::vector<byte> readbuf(4096);
+
+ while(m_plaintext.empty() && !m_channel.is_closed())
+ {
+ const size_t from_socket = m_read_fn(&readbuf[0], readbuf.size());
m_channel.received_data(&readbuf[0], from_socket);
}
@@ -69,6 +78,9 @@ size_t Blocking_Client::read(byte buf[], size_t buf_len)
buf[i] = m_plaintext[i];
m_plaintext.erase(m_plaintext.begin(), m_plaintext.begin() + returned);
+ BOTAN_ASSERT_IMPLICATION(returned == 0, m_channel.is_closed(),
+ "Only return zero if channel is closed");
+
return returned;
}
diff --git a/src/tls/tls_blocking.h b/src/tls/tls_blocking.h
index 955413be1..917032164 100644
--- a/src/tls/tls_blocking.h
+++ b/src/tls/tls_blocking.h
@@ -36,9 +36,21 @@ class BOTAN_DLL Blocking_Client
std::function<std::string (std::vector<std::string>)> next_protocol =
std::function<std::string (std::vector<std::string>)>());
- size_t currently_readable() const { return m_plaintext.size(); }
+ /**
+ * Completes full handshake then returns
+ */
+ void do_handshake();
- size_t read(byte buf[], size_t buf_len); // blocking read
+ /**
+ * Number of bytes pending read in the plaintext buffer (bytes
+ * readable without blocking)
+ */
+ size_t pending() const { return m_plaintext.size(); }
+
+ /**
+ * Blocking read, will return at least 1 byte or 0 on connection close
+ */
+ size_t read(byte buf[], size_t buf_len);
void write(const byte buf[], size_t buf_len) { m_channel.send(buf, buf_len); }