forked from emptymonkey/revsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.c
275 lines (224 loc) · 9.7 KB
/
message.c
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "common.h"
/***********************************************************************************************************************
*
* message_push()
*
* Input: Nothing, but we will heavily reference the global io_helper struct.
* Output: 0 on success, -1 on error.
*
* Purpose: This is our message interface for sending data.
*
**********************************************************************************************************************/
int message_push(){
unsigned short header_len;
unsigned short tmp_short;
/* Send the header. */
header_len = sizeof(message->data_type) + sizeof(message->data_len);
if(message->data_type == DT_PROXY || message->data_type == DT_CONNECTION){
header_len += sizeof(message->header_type) + sizeof(message->header_origin) + sizeof(message->header_id);
if(message->header_type == DT_PROXY_HT_CREATE || message->header_type == DT_PROXY_HT_REPORT || message->header_type == DT_CONNECTION_HT_CREATE){
header_len += sizeof(message->header_proxy_type);
}
}
if(header_len > io->message_data_size){
report_error("message_push(): message: local header too long!");
return(-1);
}
tmp_short = htons(header_len);
if(io->remote_write(&tmp_short, sizeof(tmp_short)) == -1){
report_error("message_push(): remote_write(%lx, %d): %s", \
(unsigned long) &tmp_short, (int) sizeof(tmp_short), strerror(errno));
return(-1);
}
if(io->remote_write(&message->data_type, sizeof(message->data_type)) == -1){
report_error("message_push(): remote_write(%lx, %d): %s", \
(unsigned long) &message->data_type, (int) sizeof(message->data_type), strerror(errno));
return(-1);
}
if(message->data_type == DT_NOP){
message->data_len = 0;
}
/* Send the data. */
if(message->data_len > io->message_data_size){
report_error("message_push(): message: local data too long!");
return(-1);
}
tmp_short = htons(message->data_len);
if(io->remote_write(&tmp_short, sizeof(tmp_short)) == -1){
report_error("message_push(): remote_write(%lx, %d): %s", \
(unsigned long) &tmp_short, (int) sizeof(tmp_short), strerror(errno));
return(-1);
}
if(message->data_type == DT_PROXY || message->data_type == DT_CONNECTION){
tmp_short = htons(message->header_type);
if(io->remote_write(&tmp_short, sizeof(tmp_short)) == -1){
report_error("message_push(): remote_write(%lx, %d): %s", \
(unsigned long) &tmp_short, (int) sizeof(tmp_short), strerror(errno));
return(-1);
}
tmp_short = htons(message->header_origin);
if(io->remote_write(&tmp_short, sizeof(tmp_short)) == -1){
report_error("message_push(): remote_write(%lx, %d): %s", \
(unsigned long) &tmp_short, (int) sizeof(tmp_short), strerror(errno));
return(-1);
}
tmp_short = htons(message->header_id);
if(io->remote_write(&tmp_short, sizeof(tmp_short)) == -1){
report_error("message_push(): remote_write(%lx, %d): %s", \
(unsigned long) &tmp_short, (int) sizeof(tmp_short), strerror(errno));
return(-1);
}
if(message->header_type == DT_PROXY_HT_CREATE || message->header_type == DT_PROXY_HT_REPORT || message->header_type == DT_CONNECTION_HT_CREATE){
tmp_short = htons(message->header_proxy_type);
if(io->remote_write(&tmp_short, sizeof(tmp_short)) == -1){
report_error("message_push(): remote_write(%lx, %d): %s", \
(unsigned long) &tmp_short, (int) sizeof(tmp_short), strerror(errno));
return(-1);
}
}
}
if(io->remote_write(message->data, message->data_len) == -1){
report_error("message_push(): remote_write(%lx, %d): %s", \
(unsigned long) message->data, message->data_len, strerror(errno));
return(-1);
}
return(0);
}
/***********************************************************************************************************************
*
* message_pull()
*
* Input: Nothing, but we will heavily reference the global io_helper struct.
* Output: 0 on success, -1 on error.
*
* Purpose: This is our message interface for receiving data.
*
**********************************************************************************************************************/
int message_pull(){
unsigned short header_len;
int retval;
memset(message->data, '\0', io->message_data_size);
/* Grab the header. */
if((retval = io->remote_read(&header_len, sizeof(header_len))) == -1){
/* During a normal disconnect condition, this is where the message_pull should fail, so check for EOF. */
if(!io->eof){
report_error("message_pull(): remote_read(%lx, %d): %s", (unsigned long) &header_len, (int) sizeof(header_len), strerror(errno));
}
return(-1);
}
header_len = ntohs(header_len);
if((retval = io->remote_read(&message->data_type, sizeof(message->data_type))) == -1){
report_error("message_pull(): remote_read(%lx, %d): %s", \
(unsigned long) &message->data_type, (int) sizeof(message->data_type), strerror(errno));
return(-1);
}
header_len -= sizeof(message->data_type);
if((retval = io->remote_read(&message->data_len, sizeof(message->data_len))) == -1){
report_error("message_pull(): remote_read(%lx, %d): %s", (unsigned long) &message->data_len, (int) sizeof(message->data_len), strerror(errno));
return(-1);
}
message->data_len = ntohs(message->data_len);
header_len -= sizeof(message->data_len);
if(header_len > io->message_data_size){
report_error("message_pull(): message: remote header too long!\n");
return(-1);
}
if(message->data_type == DT_PROXY || message->data_type == DT_CONNECTION){
if((retval = io->remote_read(&message->header_type, sizeof(message->header_type))) == -1){
report_error("message_pull(): remote_read(%lx, %d): %s", \
(unsigned long) &message->header_type, (int) sizeof(message->header_type), strerror(errno));
return(-1);
}
message->header_type = ntohs(message->header_type);
header_len -= sizeof(message->header_type);
if((retval = io->remote_read(&message->header_origin, sizeof(message->header_origin))) == -1){
report_error("message_pull(): remote_read(%lx, %d): %s", \
(unsigned long) &message->header_origin, (int) sizeof(message->header_origin), strerror(errno));
return(-1);
}
message->header_origin = ntohs(message->header_origin);
header_len -= sizeof(message->header_origin);
if((retval = io->remote_read(&message->header_id, sizeof(message->header_id))) == -1){
report_error("message_pull(): remote_read(%lx, %d): %s", \
(unsigned long) &message->header_id, (int) sizeof(message->header_id), strerror(errno));
return(-1);
}
message->header_id = ntohs(message->header_id);
header_len -= sizeof(message->header_id);
if(message->header_type == DT_PROXY_HT_CREATE || message->header_type == DT_PROXY_HT_REPORT || message->header_type == DT_CONNECTION_HT_CREATE){
if((retval = io->remote_read(&message->header_proxy_type, sizeof(message->header_proxy_type))) == -1){
report_error("message_pull(): remote_read(%lx, %d): %s", \
(unsigned long) &message->header_proxy_type, (int) sizeof(message->header_proxy_type), strerror(errno));
return(-1);
}
message->header_proxy_type = ntohs(message->header_proxy_type);
header_len -= sizeof(message->header_proxy_type);
}
}
/* Ignore any remaining header data as unknown, and probably from a more modern version of the */
/* protocol than we were compiled with. */
if(header_len){
if(header_len > io->message_data_size){
report_error("message_pull(): headers bigger than buffer!");
return(-1);
}
if((retval = io->remote_read(message->data, header_len)) == -1){
report_error("message_pull(): remote_read(%lx, %d): %s", (unsigned long) message->data, header_len, strerror(errno));
return(-1);
}
}
/* Grab the data. */
if(message->data_len > io->message_data_size){
report_error("message_pull(): message: remote data too long!");
return(-1);
}
if((retval = io->remote_read(message->data, message->data_len)) == -1){
report_error("message_pull(): remote_read(%lx, %d): %s", (unsigned long) message->data, message->data_len, strerror(errno));
return(-1);
}
return(0);
}
/***********************************************************************************************************************
*
* message_helper_create()
*
* Input: A pointer to the data.
* The length of that data.
* The max size that data is allowed to be in this run.
* Output: A pointer to a new message_helper node if successful, NULL if not.
*
* Purpose: Make a new message_helper node and fill it with data. Probably for the write buffering case where a write()
* somewhere is failing non-fataly.
*
**********************************************************************************************************************/
struct message_helper *message_helper_create(char *data, unsigned short data_len, unsigned short message_data_size){
struct message_helper *new_mh;
new_mh = (struct message_helper *) calloc(1, sizeof(struct message_helper));
if(!new_mh){
report_error("message_helper_create(): calloc(1, %d): %s", (int) sizeof(struct message_helper), strerror(errno));
return(NULL);
}
new_mh->data = (char *) calloc(message_data_size, sizeof(char));
if(!new_mh->data){
report_error("message_helper_create(): calloc(1, %d): %s", (int) sizeof(struct message_helper), strerror(errno));
free(new_mh);
return(NULL);
}
memcpy(new_mh->data, data, data_len);
new_mh->data_len = data_len;
return(new_mh);
}
/***********************************************************************************************************************
*
* message_helper_destroy()
*
* Input: The message_helper node that we want to destroy.
* Output: None.
*
* Purpose: Destroy a message_helper node.
*
**********************************************************************************************************************/
void message_helper_destroy(struct message_helper *mh){
free(mh->data);
free(mh);
}