-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvty_io.c
291 lines (245 loc) · 8.2 KB
/
vty_io.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "vty_io.h"
//#include <readline/readline.h>
//#include <readline/history.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include "http_server.h"
#include "socket_io.h"
#include <arpa/telnet.h>
#include <termios.h>
#include <sys/ioctl.h>
#include "socket.h"
bool file_write_cb(vty_t* vty);
int file_read_cb(vty_t* vty);
void file_close_cb(vty_io_data_t* vty_data);
bool std_write_cb(vty_t* vty);
int std_read_cb(vty_t* vty);
void std_erase_cb(vty_t* vty);
void std_delete_cb(vty_t* vty);
void std_erase_line_cb(vty_t* vty);
void std_cursor_left_cb(vty_t* vty);
void std_cursor_right_cb(vty_t* vty);
int http_read_cb(vty_t* vty);
bool http_write_cb(vty_t* vty);
bool http_flush_cb(vty_t* vty);
void http_free_priv_cb(vty_t* vty);
void socket_close_cb(vty_io_data_t* vty_data);
vty_t* vty_io_create(vty_type type, vty_io_data_t* data)
{
vty_t* new_vty = vty_create(type, data);
new_vty->stored_vty = new_vty;
vty_io_config(new_vty);
return new_vty;
}
void vty_io_config(vty_t* vty)
{
switch(vty->type)
{
case VTY_FILE:
vty->write_cb = file_write_cb;
vty->read_cb = file_read_cb;
vty->is_interactive = false;
vty->data->close_cb = file_close_cb;
break;
case VTY_STD:
vty->write_cb = std_write_cb;
vty->read_cb = std_read_cb;
vty->erase_char_cb = std_erase_cb;
vty->delete_char_cb = std_delete_cb;
vty->erase_line_cb = std_erase_line_cb;
vty->cursor_left_cb = std_cursor_left_cb;
vty->cursor_right_cb = std_cursor_right_cb;
vty->data->close_cb = NULL;
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
vty->term_height = w.ws_row;
vty->term_width = w.ws_col;
break;
case VTY_HTTP:
vty->write_cb = NULL;
vty->read_cb = http_read_cb;
vty->flush_cb = http_flush_cb;
vty->free_priv_cb = http_free_priv_cb;
http_vty_priv_t* http_priv = calloc(1, sizeof(http_vty_priv_t));
http_priv->request = calloc(1, sizeof(http_request_t));
http_priv->request->buffer = byte_buffer_init(HTTP_REQUEST_BUFSIZE);
http_priv->resp_code = HTTP_RESP_OK;
http_priv->response_header = byte_buffer_init(HTTP_RESPONSE_HEADERS_SIZE);
vty->priv = http_priv;
vty->data->close_cb = socket_close_cb;
//vty->buffer = calloc(BUFSIZE, sizeof(char));
break;
case VTY_SOCKET:
vty->write_cb = socket_write_cb;
vty->read_cb = socket_read_cb;
vty->flush_cb = socket_flush_cb;
vty->erase_char_cb = socket_erase_cb;
vty->delete_char_cb = socket_delete_cb;
vty->erase_line_cb = socket_erase_line_cb;
vty->cursor_left_cb = socket_cursor_left_cb;
vty->cursor_right_cb = socket_cursor_right_cb;
vty->data->close_cb = socket_close_cb;
break;
}
}
bool file_write_cb(vty_t* vty)
{
int n = fwrite(byte_buffer_get_read_ptr(vty->write_buffer), byte_buffer_read_len(vty->write_buffer), 1, vty->data->desc.file);
if(n == 1)
{
byte_buffer_adjust_read_pos(vty->write_buffer, byte_buffer_read_len(vty->write_buffer));
byte_buffer_pack(vty->write_buffer);
}
return n == 1;
}
int file_read_cb(vty_t* vty)
{
char ch = 0;
//*str = malloc(sizeof(char));
ch = fgetc(vty->data->desc.file);
byte_buffer_append(vty->read_buffer, &ch, 1);
if(ch == (char)EOF)
{
return -1;
}
else
{
return 1;
}
}
void file_close_cb(vty_io_data_t* vty_data)
{
fclose(vty_data->desc.file);
}
bool std_write_cb(vty_t* vty)
{
int n = write(fileno(vty->data->desc.io_pair[OUT]), byte_buffer_get_read_ptr(vty->write_buffer), byte_buffer_read_len(vty->write_buffer));
if(n != -1)
{
fflush(vty->data->desc.io_pair[OUT]);
byte_buffer_adjust_read_pos(vty->write_buffer, n);
byte_buffer_pack(vty->write_buffer);
}
return n != -1;
}
int std_read_cb(vty_t* vty)
{
//*str = calloc(1, sizeof(char));
char ch;
int n = read(fileno(vty->data->desc.io_pair[IN]), &ch, 1);
byte_buffer_append(vty->read_buffer, &ch, 1);
return 1;
}
void std_erase_cb(vty_t* vty)
{
vty_write(vty, "\x8");
}
void std_delete_cb(vty_t* vty)
{
vty_write(vty, "\x7f");
}
void std_erase_line_cb(vty_t* vty)
{
vty_write(vty, "\33[2K\r");
}
void std_cursor_left_cb(vty_t* vty)
{
vty_write(vty, "\33[1D");
}
void std_cursor_right_cb(vty_t* vty)
{
vty_write(vty, "\33[1C");
}
int http_read_cb(vty_t* vty)
{
int socket = vty->data->desc.socket;
http_vty_priv_t* http_priv = (http_vty_priv_t*)vty->priv;
int ret = socket_recv(vty_get_pump(vty), socket, http_priv->request->buffer);
if(-1 == ret)
{
event_dispatcher_unregister_handler(vty_get_pump(vty), vty->data->desc.socket, &vty_free, (void*)vty);
}
if(ret > 0)
{
int resp_len = http_server_read_request(http_priv, vty->read_buffer);
if(0 != resp_len && http_priv->resp_code == HTTP_RESP_OK)
{
vty_set_command_received(vty, true);
return resp_len;
}
else if(http_priv->resp_code != HTTP_RESP_OK)
{
vty_set_command_received(vty, true);
}
}
return 0;
}
bool http_flush_cb(vty_t* vty)
{
bool retVal = false;
http_vty_priv_t* http_priv = (http_vty_priv_t*)vty->priv;
int socket = vty->data->desc.socket;
http_server_prepare_response_headers(http_priv, byte_buffer_read_len(vty->write_buffer));
struct iovec iov[2];
iov[0].iov_base = byte_buffer_get_read_ptr(http_priv->response_header);
iov[0].iov_len = byte_buffer_read_len(http_priv->response_header);
iov[1].iov_base = byte_buffer_get_read_ptr(vty->write_buffer);
iov[1].iov_len = byte_buffer_read_len(vty->write_buffer);
int ret = socket_send_v(vty_get_pump(vty), socket, iov, 2, byte_buffer_read_len(http_priv->response_header) + byte_buffer_read_len(vty->write_buffer));
if(-1 == ret)
{
//printf("HTTP send failed, unregestering socket!\n");
event_dispatcher_unregister_handler(vty_get_pump(vty), vty->data->desc.socket, &vty_free, (void*)vty);
}
else if(ret == byte_buffer_read_len(http_priv->response_header) + byte_buffer_read_len(vty->write_buffer))
{
byte_buffer_adjust_read_pos(vty->write_buffer, byte_buffer_read_len(vty->write_buffer));
byte_buffer_pack(vty->write_buffer);
byte_buffer_adjust_read_pos(http_priv->response_header, byte_buffer_read_len(http_priv->response_header));
byte_buffer_pack(http_priv->response_header);
http_set_response(http_priv, HTTP_RESP_NONE);
vty->buf_size = 0;
*vty->buffer = 0;
retVal = true;
//printf("HTTP send complete: %d\n", ret);
}
else
{
//printf("Partial data sent: %d out of %d\n", ret, byte_buffer_read_len(http_priv->response_header) + byte_buffer_read_len(vty->write_buffer));
if(ret >= byte_buffer_read_len(http_priv->response_header))
{
int remaining = ret - byte_buffer_read_len(http_priv->response_header);
if(byte_buffer_read_len(http_priv->response_header) > 0)
{
byte_buffer_adjust_read_pos(http_priv->response_header, byte_buffer_read_len(http_priv->response_header));
}
if(byte_buffer_read_len(vty->write_buffer) > 0)
{
byte_buffer_adjust_read_pos(vty->write_buffer, remaining);
byte_buffer_pack(vty->write_buffer);
}
http_set_response(http_priv, HTTP_RESP_NONE);
}
else
{
byte_buffer_adjust_read_pos(http_priv->response_header, ret);
}
retVal = true;
}
return retVal;
}
void http_free_priv_cb(vty_t* vty)
{
http_vty_priv_t* http_priv = (http_vty_priv_t*)vty->priv;
byte_buffer_free(http_priv->request->buffer);
free(http_priv->request);
//byte_buffer_free(http_priv->response);
free(http_priv->content_type);
free(http_priv->post_data);
free(http_priv->headers);
byte_buffer_free(http_priv->response_header);
free(http_priv);
}