add server's SSL wrapper

This commit is contained in:
XieHan
2021-05-17 02:32:35 +08:00
parent 6a03669b91
commit d101edb5a7
2 changed files with 52 additions and 16 deletions
+32 -15
View File
@@ -154,31 +154,25 @@ int SSLWrapper::encode(struct iovec vectors[], int max)
#define BUFSIZE 8192
int SSLWrapper::append(const void *buf, size_t *size)
int SSLWrapper::append_message()
{
BIO *rbio = SSL_get_rbio(this->ssl);
char rbuf[BUFSIZE];
size_t nleft;
size_t n;
char buf[BUFSIZE];
int ret;
ret = BIO_write(rbio, buf, *size);
if (ret <= 0)
return -1;
*size = ret;
while ((ret = SSL_read(this->ssl, rbuf, BUFSIZE)) > 0)
while ((ret = SSL_read(this->ssl, buf, BUFSIZE)) > 0)
{
buf = rbuf;
nleft = ret;
size_t nleft = ret;
char *p = buf;
size_t n;
do
{
n = nleft;
ret = this->msg->append(buf, &n);
ret = this->msg->append(p, &n);
if (ret == 0)
{
buf = (char *)buf + n;
nleft -= n;
p += n;
}
else
return ret;
@@ -201,5 +195,28 @@ int SSLWrapper::append(const void *buf, size_t *size)
return 0;
}
int SSLWrapper::append(const void *buf, size_t *size)
{
BIO *rbio = SSL_get_rbio(this->ssl);
int ret;
ret = BIO_write(rbio, buf, *size);
if (ret <= 0)
return -1;
*size = ret;
return this->append_message();
}
int ServerSSLWrapper::append(const void *buf, size_t *size)
{
int ret = this->handshaker.append(buf, size);
if (ret > 0)
ret = this->append_message();
return ret;
}
}
+20 -1
View File
@@ -27,7 +27,7 @@ namespace protocol
class SSLHandshaker : public ProtocolMessage
{
protected:
public:
virtual int encode(struct iovec vectors[], int max);
virtual int append(const void *buf, size_t *size);
@@ -44,6 +44,9 @@ protected:
virtual int encode(struct iovec vectors[], int max);
virtual int append(const void *buf, size_t *size);
protected:
int append_message();
protected:
ProtocolMessage *msg;
SSL *ssl;
@@ -56,6 +59,22 @@ public:
}
};
class ServerSSLWrapper : public SSLWrapper
{
protected:
virtual int append(const void *buf, size_t *size);
protected:
SSLHandshaker handshaker;
public:
ServerSSLWrapper(ProtocolMessage *msg, SSL *ssl) :
SSLWrapper(msg, ssl),
handshaker(ssl)
{
}
};
}
#endif