-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathserver_chargen.c
274 lines (219 loc) · 6.43 KB
/
server_chargen.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
#include <neat.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "util.h"
#include <errno.h>
/**********************************************************************
chargen server
server_chargen [OPTIONS]
https://tools.ietf.org/html/rfc864
**********************************************************************/
static char *config_property = "{\
\"transport\": {\
\"value\": [\"SCTP\", \"TCP\", \"SCTP/UDP\"],\
\"precedence\": 2}\
}";
static uint16_t config_log_level = 1;
static uint16_t chargen_offset = 0;
#define BUFFERSIZE 32
static neat_error_code on_writable(struct neat_flow_operations *opCB);
static void
print_usage()
{
if (config_log_level >= 2) {
fprintf(stderr, "%s()\n", __func__);
}
printf("server_chargen [OPTIONS]\n");
printf("\t- P <filename> \tneat properties, default properties:\n%s\n", config_property);
printf("\t- v \tlog level 0..2 (%d)\n", config_log_level);
}
/*
Error handler
*/
static neat_error_code
on_error(struct neat_flow_operations *opCB)
{
if (config_log_level >= 2) {
fprintf(stderr, "%s()\n", __func__);
}
exit(EXIT_FAILURE);
}
/*
Read data and discard
*/
static neat_error_code on_readable(struct neat_flow_operations *opCB)
{
// data is available to read
neat_error_code code;
unsigned char buffer[BUFFERSIZE];
uint32_t buffer_filled;
if (config_log_level >= 2) {
fprintf(stderr, "%s()\n", __func__);
}
code = neat_read(opCB->ctx, opCB->flow, buffer, BUFFERSIZE, &buffer_filled, NULL, 0);
if (code != NEAT_OK) {
if (code == NEAT_ERROR_WOULD_BLOCK) {
return NEAT_OK;
} else {
fprintf(stderr, "%s - neat_read error: %d\n", __func__, (int)code);
return on_error(opCB);
}
}
if (config_log_level >= 1) {
printf("data received - %d byte\n", buffer_filled);
}
if (config_log_level >= 2) {
fwrite(buffer, sizeof(char), buffer_filled, stdout);
printf("\n");
fflush(stdout);
}
return NEAT_OK;
}
static neat_error_code
on_all_written(struct neat_flow_operations *opCB)
{
if (config_log_level >= 2) {
fprintf(stderr, "%s()\n", __func__);
}
opCB->on_writable = on_writable;
opCB->on_all_written = NULL;
neat_set_operations(opCB->ctx, opCB->flow, opCB);
return NEAT_OK;
}
static neat_error_code
on_writable(struct neat_flow_operations *opCB)
{
neat_error_code code;
unsigned char buffer[BUFFERSIZE];
int i;
if (config_log_level >= 2) {
fprintf(stderr, "%s()\n", __func__);
}
for (i = 0; i < BUFFERSIZE - 2; i++) {
buffer[i] = 33 + ((chargen_offset + i) % 72);
}
buffer[i++] = '\r';
buffer[i++] = '\n';
chargen_offset = (chargen_offset + 1)%72;
opCB->on_writable = NULL;
opCB->on_all_written = on_all_written;
neat_set_operations(opCB->ctx, opCB->flow, opCB);
code = neat_write(opCB->ctx, opCB->flow, buffer, BUFFERSIZE, NULL, 0);
if (code != NEAT_OK) {
fprintf(stderr, "%s - neat_write error: %d\n", __func__, (int)code);
return on_error(opCB);
}
return NEAT_OK;
}
static neat_error_code
on_close(struct neat_flow_operations *opCB)
{
fprintf(stderr, "%s - flow closed OK!\n", __func__);
return NEAT_OK;
}
static neat_error_code
on_connected(struct neat_flow_operations *opCB)
{
if (config_log_level >= 2) {
fprintf(stderr, "%s()\n", __func__);
}
if (config_log_level >= 1) {
printf("peer connected\n");
}
opCB->on_readable = on_readable;
opCB->on_writable = on_writable;
opCB->on_all_written = NULL;
opCB->on_close = on_close;
neat_set_operations(opCB->ctx, opCB->flow, opCB);
return NEAT_OK;
}
int
main(int argc, char *argv[])
{
int arg, result;
char *arg_property = NULL;
struct neat_ctx *ctx = NULL;
struct neat_flow *flow = NULL;
struct neat_flow_operations ops;
memset(&ops, 0, sizeof(ops));
result = EXIT_SUCCESS;
while ((arg = getopt(argc, argv, "P:v:")) != -1) {
switch(arg) {
case 'P':
if (read_file(optarg, &arg_property) < 0) {
fprintf(stderr, "Unable to read properties from %s: %s",
optarg, strerror(errno));
result = EXIT_FAILURE;
goto cleanup;
}
if (config_log_level >= 1) {
printf("option - properties: %s\n", arg_property);
}
break;
case 'v':
config_log_level = atoi(optarg);
if (config_log_level >= 1) {
printf("option - log level: %d\n", config_log_level);
}
break;
default:
print_usage();
goto cleanup;
break;
}
}
if (optind != argc) {
fprintf(stderr, "%s - argument error\n", __func__);
print_usage();
goto cleanup;
}
if ((ctx = neat_init_ctx()) == NULL) {
fprintf(stderr, "%s - neat_init_ctx failed\n", __func__);
result = EXIT_FAILURE;
goto cleanup;
}
if (config_log_level == 0) {
neat_log_level(ctx, NEAT_LOG_ERROR);
} else if (config_log_level == 1){
neat_log_level(ctx, NEAT_LOG_WARNING);
} else {
neat_log_level(ctx, NEAT_LOG_DEBUG);
}
// new neat flow
if ((flow = neat_new_flow(ctx)) == NULL) {
fprintf(stderr, "%s - neat_new_flow failed\n", __func__);
result = EXIT_FAILURE;
goto cleanup;
}
// set properties
if (neat_set_property(ctx, flow, arg_property ? arg_property : config_property)) {
fprintf(stderr, "%s - neat_set_property failed\n", __func__);
result = EXIT_FAILURE;
goto cleanup;
}
// set callbacks
ops.on_connected = on_connected;
ops.on_error = on_error;
if (neat_set_operations(ctx, flow, &ops)) {
fprintf(stderr, "%s - neat_set_operations failed\n", __func__);
result = EXIT_FAILURE;
goto cleanup;
}
// wait for on_connected or on_error to be invoked
if (neat_accept(ctx, flow, 8080, NULL, 0)) {
fprintf(stderr, "%s - neat_accept failed\n", __func__);
result = EXIT_FAILURE;
goto cleanup;
}
neat_start_event_loop(ctx, NEAT_RUN_DEFAULT);
// cleanup
cleanup:
if (arg_property)
free(arg_property);
if (ctx != NULL) {
neat_free_ctx(ctx);
}
exit(result);
}