-
Notifications
You must be signed in to change notification settings - Fork 6
/
core.h
127 lines (99 loc) · 2.66 KB
/
core.h
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
////////////////////////////////////////////////////////////////////////////////
// core.h
// author: [email protected]
#ifndef core_h
#define core_h
#ifdef __APPLE__
#include <util.h> // forkpty (POSIX - ain't nobody got time for dat)
#else
#include <pty.h>
#endif
#include <string>
#include <memory>
#include <map>
#include <set>
////////////////////////////////////////////////////////////////////////////////
// types / defines
#define TPT_CLIENT 2
#define TPT_SERVER 1
#define TPT_CLOSE 0
#define TPT_EMPTY -1
#define TPT_ERROR -2
#define LOG_BUFSIZE 8192
#define LOG_FILE (1<<1)
#define LOG_ECHO (1<<2)
#define MSG_INVALID (0)
#define MSG_RVSHELL (1)
#define MSG_WNDSIZE (2)
#define MSG_PROXY_INIT (3)
#define MSG_PROXY_PASS (4)
#define MSG_PROXY_FAIL (5)
#define MSG_PROXY_DATA (6)
#define MSG_PROXY_DEAD (7)
#ifndef MAX
#define MAX(a, b) (a > b ? a : b)
#endif
#ifndef MIN
#define MIN(a, b) (a < b ? a : b)
#endif
#ifndef CLAMP
#define CLAMP(val, min, max) MAX(min, MIN(max, val))
#endif
#ifndef LOG_DISABLE
#define LOG(...) log_print(__VA_ARGS__)
#else
#define LOG(...)
#endif
// type proto
class transport;
class message;
// func proto
void log_init(const char * prefix, int flags);
void log_flags(int flags = 0);
void log_print(const char * fmt, ...);
void hexdump(const char * buf, int len, int cols = 16, bool ascii = true);
////////////////////////////////////////////////////////////////////////////////
// abstract transport interface
class transport {
public:
// ctors / dtors
transport() {}
virtual ~transport() {}
// abstract interface
virtual int init(int type) = 0;
virtual int send(message & msg) = 0;
virtual int recv(message & msg) = 0;
virtual void setopt(int opt, std::string value) = 0;
virtual void close() = 0;
};
////////////////////////////////////////////////////////////////////////////////
// transport message container
class message {
public:
// class
enum { header_len = sizeof(int) * 2 };
enum { body_max_len = 1024 };
// ctors / dtors
message(int type = MSG_INVALID, size_t len = 0);
message(int type, const char * buf, size_t len);
message(const char * buf, size_t len);
// buffer getters
char * data();
char * body();
const char * data() const;
const char * body() const;
size_t data_len() const;
size_t body_len() const;
// misc getters / setters
int type() const;
size_t resize(size_t len);
protected:
// internal data struct
struct {
int type;
int body_len;
char body[body_max_len];
} m_data;
};
////////////////////////////////////////////////////////////////////////////////
#endif // core_h