mirror of
https://github.com/sogou/workflow.git
synced 2026-10-26 01:30:47 +08:00
add server's SSL wrapper
This commit is contained in:
+32
-15
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user