-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathserver_discard.c
231 lines (194 loc) · 5.71 KB
/
server_discard.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
#include <neat.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "util.h"
#include <errno.h>
#define QUOTE(...) #__VA_ARGS__
/**********************************************************************
discard server
server_discard [OPTIONS]
https://tools.ietf.org/html/rfc863
**********************************************************************/
static uint32_t config_buffer_size = 128;
static uint16_t config_log_level = 1;
static char *config_property = QUOTE({
"transport": {
"value": ["SCTP", "TCP", "SCTP/UDP"],
"precedence": 2}
});
static unsigned char *buffer = NULL;
static uint32_t buffer_filled = 0;
/*
print usage and exit
*/
static void
print_usage()
{
if (config_log_level >= 2) {
fprintf(stderr, "%s()\n", __func__);
}
printf("server_discard [OPTIONS]\n");
printf("\t- P <filename> \tneat properties, default properties:\n%s\n", config_property);
printf("\t- S \tbuffer in byte (%d)\n", config_buffer_size);
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 until buffered_amount == 0 - then stop event loop!
*/
static neat_error_code
on_readable(struct neat_flow_operations *opCB)
{
// data is available to read
neat_error_code code;
if (config_log_level >= 2) {
fprintf(stderr, "%s()\n", __func__);
}
code = neat_read(opCB->ctx, opCB->flow, buffer, config_buffer_size, &buffer_filled, NULL, 0);
if (code != NEAT_OK) {
if (code == NEAT_ERROR_WOULD_BLOCK) {
if (config_log_level >= 1) {
printf("on_readable - NEAT_ERROR_WOULD_BLOCK\n");
}
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("received data - %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_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;
neat_set_operations(opCB->ctx, opCB->flow, opCB);
return NEAT_OK;
}
int
main(int argc, char *argv[])
{
// uint64_t prop;
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:S: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 'S':
config_buffer_size = atoi(optarg);
if (config_log_level >= 1) {
printf("option - buffer size: %d\n", config_buffer_size);
}
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 ((buffer = malloc(config_buffer_size)) == NULL) {
fprintf(stderr, "%s - malloc failed\n", __func__);
result = EXIT_FAILURE;
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);
free(buffer);
if (ctx != NULL) {
neat_free_ctx(ctx);
}
exit(result);
}