2019-07-27 22:44:32 +08:00
|
|
|
|
#pragma once
|
2017-06-02 14:47:24 +08:00
|
|
|
|
#include "../acl_cpp_define.hpp"
|
|
|
|
|
#include "stream_hook.hpp"
|
2014-11-30 21:15:35 +08:00
|
|
|
|
|
|
|
|
|
struct ACL_VSTREAM;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
namespace acl {
|
|
|
|
|
|
|
|
|
|
class polarssl_conf;
|
2019-07-12 11:05:17 +08:00
|
|
|
|
class atomic_long;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* stream/aio_stream 流对象底层 IO 处理过程的处理类,该类对象中的读写的过程将会替代
|
|
|
|
|
* stream/aio_stream 流对象中 默认的底层 IO 过程;该类对象必须是动态创建的(即为堆对象),
|
|
|
|
|
* stream/aio_stream 流对象通过调用本类对象的 destroy() 方法释放本类对象
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
|
|
|
|
class ACL_CPP_API polarssl_io : public stream_hook
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 构造函数
|
|
|
|
|
* @param conf {polarssl_conf&} 对每一个 SSL 连接进行配置的类对象
|
|
|
|
|
* @param server_side {bool} 是否为服务端模式,因为客户端模式与服务端
|
|
|
|
|
* 模式的握手方法不同,所以通过此参数来进行区分
|
|
|
|
|
* @param nblock {bool} 是否为非阻塞模式
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2019-07-12 11:05:17 +08:00
|
|
|
|
polarssl_io(polarssl_conf& conf, bool server_side, bool nblock = false);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-12 11:05:17 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 销毁 SSL IO 对象
|
2019-07-12 11:05:17 +08:00
|
|
|
|
*/
|
2019-07-07 11:00:12 +08:00
|
|
|
|
virtual void destroy(void);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2014-11-30 21:15:35 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 调用此方法进行 SSL 握手,在非阻塞 IO 模式下该函数需要与 handshake_ok()
|
|
|
|
|
* 函数组合使用来判断 SSL 握手是否成功
|
2014-11-30 21:15:35 +08:00
|
|
|
|
* @return {bool}
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 1、返回 false 表示握手失败,需要关闭连接;
|
|
|
|
|
* 2、当返回 true 时:
|
|
|
|
|
* 2.1、如果为阻塞 IO 模式则表示 SSL 握手成功
|
|
|
|
|
* 2.2、在非阻塞 IO 模式下仅代表本次握手过程中 IO 是成功的,还需要调用
|
|
|
|
|
* handshake_ok() 函数判断 SSL 握手是否成功
|
2014-11-30 21:15:35 +08:00
|
|
|
|
*/
|
|
|
|
|
bool handshake(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 判断 SSL 握手是否成功
|
2014-11-30 21:15:35 +08:00
|
|
|
|
* @return {bool}
|
|
|
|
|
*/
|
|
|
|
|
bool handshake_ok(void)
|
|
|
|
|
{
|
|
|
|
|
return handshake_ok_;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-07 23:48:11 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 设置套接字为阻塞模式/非阻塞模式
|
|
|
|
|
* @param yes {bool} 当为 false 时则设为阻塞模式,否则设为非阻塞模式
|
2014-12-07 23:48:11 +08:00
|
|
|
|
* @return {polarssl_io&}
|
|
|
|
|
*/
|
|
|
|
|
polarssl_io& set_non_blocking(bool yes);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 判断当前设置的 SSL IO 是否阻塞模式还是非阻塞模式
|
|
|
|
|
* @return {bool} 返回 true 则表示为非阻塞模式,否则为阻塞模式
|
2014-12-07 23:48:11 +08:00
|
|
|
|
*/
|
|
|
|
|
bool is_non_blocking(void) const
|
|
|
|
|
{
|
2019-07-12 11:05:17 +08:00
|
|
|
|
return nblock_;
|
2014-12-07 23:48:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-11-30 21:15:35 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 检查对方证书是否有效(一般不必调用此函数)
|
2014-11-30 21:15:35 +08:00
|
|
|
|
* @return {bool}
|
|
|
|
|
*/
|
|
|
|
|
bool check_peer(void);
|
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
private:
|
2019-07-07 11:00:12 +08:00
|
|
|
|
~polarssl_io(void);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2014-11-30 21:15:35 +08:00
|
|
|
|
virtual bool open(ACL_VSTREAM* s);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
virtual bool on_close(bool alive);
|
|
|
|
|
virtual int read(void* buf, size_t len);
|
|
|
|
|
virtual int send(const void* buf, size_t len);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
polarssl_conf& conf_;
|
2019-07-12 11:05:17 +08:00
|
|
|
|
atomic_long* refers_;
|
2014-11-30 21:15:35 +08:00
|
|
|
|
bool server_side_;
|
2019-07-12 11:05:17 +08:00
|
|
|
|
bool nblock_;
|
2014-11-30 21:15:35 +08:00
|
|
|
|
bool handshake_ok_;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
void* ssl_;
|
|
|
|
|
void* ssn_;
|
|
|
|
|
void* rnd_;
|
2014-11-30 21:15:35 +08:00
|
|
|
|
ACL_VSTREAM* stream_;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
static int sock_read(void *ctx, unsigned char *buf, size_t len);
|
|
|
|
|
static int sock_send(void *ctx, const unsigned char *buf, size_t len);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace acl
|