1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
#pragma once
#include <exception>
#include <memory>
#include <cstdio>
#include "easylogging++.h"
#include "util.h"
extern "C"
{
#include <event2/bufferevent.h>
#include <evdns.h>
#include <unistd.h>
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// 对关键的Libevent资源实行智能指针的封装
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct EventDeleter
{
void operator()(struct evbuffer *evbuf)
{ evbuffer_free(evbuf); }
};
struct FileDescriptorDeleter
{
void operator()(int fd)
{ close(fd); }
void operator()(FILE *fp)
{ fclose(fp); }
};
using evbuffer_unique_ptr_t = std::unique_ptr<struct evbuffer, EventDeleter>;
using file_unique_ptr_t = std::unique_ptr<FILE, FileDescriptorDeleter>;
using fd_unique_ptr_t = std::unique_ptr<int, FileDescriptorDeleter>;
/* Bufferevent托管类
* Bufferevent指针用于构造本Class后,即失去对指针的所有权,指针的释放由本类的析构函数完成 */
class BufferEvent
{
public:
explicit BufferEvent(struct bufferevent *bev)
{ bev_.reset(bev); }
using data_cb_t = std::function<int(struct bufferevent *bev)>;
using event_cb_t = std::function<int(struct bufferevent *bev, short what)>;
using exception_cb_t = std::function<int(std::exception_ptr ex_ptr)>;
void SetReadCallback(data_cb_t cb)
{ read_cb_ = cb; }
void SetWriteCallback(data_cb_t cb)
{ write_cb_ = cb; }
void SetEventCallback(event_cb_t cb)
{ event_cb_ = cb; }
void SetExceptionCallback(exception_cb_t cb)
{ exception_cb_ = cb; }
void Enable(short what)
{ bufferevent_enable(bev_.get(), what); }
void Disable(short what)
{ bufferevent_disable(bev_.get(), what); }
struct bufferevent *Raw()
{ return bev_.get(); }
private:
data_cb_t read_cb_;
data_cb_t write_cb_;
event_cb_t event_cb_;
exception_cb_t exception_cb_;
struct __bufferevent_deleter
{
void operator()(struct bufferevent *bev)
{ bufferevent_free(bev); }
};
std::unique_ptr<struct bufferevent, __bufferevent_deleter> bev_;
private:
static int __wrapper_cb_read(struct bufferevent *bev, void *user)
{
auto *__this = static_cast<BufferEvent *>(user);
std::exception_ptr exp_ptr;
try
{
return __this->read_cb_(bev);
}
catch (const std::exception & e)
{
exp_ptr = std::current_exception();
}
if(__this->exception_cb_ == nullptr)
{
std::rethrow_exception(exp_ptr);
}
if (__this->exception_cb_ != nullptr && __this->exception_cb_(exp_ptr) != 0)
{
std::rethrow_exception(exp_ptr);
}
return 0;
}
static int __wrapper_cb_write(struct bufferevent *bev, void *user)
{
auto *__this = static_cast<BufferEvent *>(user);
std::exception_ptr exp_ptr;
try
{
return __this->write_cb_(bev);
}
catch (const std::exception & e)
{
exp_ptr = std::current_exception();
}
if(__this->exception_cb_ == nullptr)
{
std::rethrow_exception(exp_ptr);
}
if (__this->exception_cb_ != nullptr && __this->exception_cb_(exp_ptr) != 0)
{
std::rethrow_exception(exp_ptr);
}
return 0;
}
static int __wrapper_cb_event(struct bufferevent *bev, short what, void *user)
{
auto *__this = static_cast<BufferEvent *>(user);
std::exception_ptr exp_ptr;
try
{
return __this->event_cb_(bev, what);
}
catch (const std::exception & e)
{
exp_ptr = std::current_exception();
}
if(__this->exception_cb_ == nullptr)
{
std::rethrow_exception(exp_ptr);
}
if (__this->exception_cb_ != nullptr && __this->exception_cb_(exp_ptr) != 0)
{
std::rethrow_exception(exp_ptr);
}
return 0;
}
};
/* Event托管类
* Bufferevent指针用于构造本Class后,即失去对指针的所有权,指针的释放由本类的析构函数完成 */
class Event
{
public:
explicit Event() = default;
virtual ~Event() = default;
using event_cb_t = std::function<void(evutil_socket_t fd, short what)>;
void Init(struct event_base * ev_base, evutil_socket_t fd, short en_event, event_cb_t cb)
{
ev_.reset(event_new(ev_base, fd, en_event, __wrapper_event, this));
if (ev_ == nullptr) throw std::runtime_error("Failed at create event for fd " + fd);
int ret = event_add(ev_.get(), nullptr);
if (ret < 0) throw std::runtime_error("Failed at event_add() for fd " + fd);
event_cb_ = cb;
}
private:
event_cb_t event_cb_;
struct __event_deleter
{
void operator()(struct event *ev) { if (ev != nullptr) event_free(ev); }
};
std::unique_ptr<struct event, __event_deleter> ev_;
private:
static void __wrapper_event(evutil_socket_t fd, short what, void * user)
{
auto *__this = static_cast<Event *>(user);
return __this->event_cb_(fd, what);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Stream.h内的数据结构转换工具
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stream.h>
struct SappIpAddrDeleter
{
void operator()(struct ipaddr *ptr_addr)
{
if (ptr_addr->addrtype == ADDR_TYPE_IPV4) delete ptr_addr->v4;
if (ptr_addr->addrtype == ADDR_TYPE_IPV6) delete ptr_addr->v6;
delete ptr_addr;
}
};
using sapp_ip_addr_ptr_t = std::unique_ptr<struct ipaddr, SappIpAddrDeleter>;
sapp_ip_addr_ptr_t sockaddr_to_sapp_ipaddr(const struct sockaddr *sk_addr_src, const struct sockaddr *sk_addr_dst);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// sslsplit logger to easylogger++
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "easylogging++.h"
#include "util.h"
#define log_dbg_printf(fmt, ...) do { LOG(DEBUG) << string_format(fmt, ##__VA_ARGS__); } while(0)
#define log_err_printf(fmt, ...) do { LOG(ERROR) << string_format(fmt, ##__VA_ARGS__); } while(0)
#define log_dbg_print_free(str) do { LOG(DEBUG) << std::string(str); free(str); } while(0)
|