-
Notifications
You must be signed in to change notification settings - Fork 5
/
server-main.c
262 lines (238 loc) · 7.2 KB
/
server-main.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
/*
* af_ktls tool
*
* Copyright (C) 2016 Fridolin Pokorny <fpokorny@redhat.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*/
#include <stdio.h>
#include <stdbool.h>
#include <getopt.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "common.h"
#include "server.h"
#define OPT_TLS 't'
#define OPT_DTLS 'd'
#define OPT_PORT 'p'
#define OPT_VERBOSE 'v'
#define OPT_STORE 's'
#define OPT_HELP 'h'
#define OPT_KTLS 'k'
#define OPT_NO_ECHO 'x'
#define OPT_MTU 'm'
#define OPT_RAW_RECV 'r'
#define OPT_TCP 'c'
#define OPT_UDP 'u'
#define OPT_SHORT_OPTS "tdp:vs:hxm:cu"
static struct option long_options[] = {
/* -t */{"tls", no_argument, 0, OPT_TLS},
/* -d */{"dtls", no_argument, 0, OPT_DTLS},
/* -p */{"port", required_argument, 0, OPT_PORT},
/* -v */{"verbose", no_argument, 0, OPT_VERBOSE},
/* -s */{"store", required_argument, 0, OPT_STORE},
/* -h */{"help", no_argument, 0, OPT_HELP},
/* -k */{"ktls", no_argument, 0, OPT_KTLS},
/* -x */{"no-echo", no_argument, 0, OPT_NO_ECHO},
/* -m */{"mtu", required_argument, 0, OPT_MTU},
/* -r */{"raw-recv", no_argument, 0, OPT_RAW_RECV},
/* -c */{"tcp", no_argument, 0, OPT_TCP},
/* -u */{"udp", no_argument, 0, OPT_UDP},
{0, 0, 0, 0}
};
static void print_help(char *progname) {
static const char *help_msg = \
"Usage: %s OPTIONS\n"
"Benchmark Gnu TLS and AL_TLS kernel implementation (echo server)\n"
"\nOptions:\n\n"
"\t--tls|-t benchmark TLS protocol\n"
"\t--dtls|-d benchmark DTLS protocol; the default is TLS\n"
"\t--port|-p benchmark DTLS protocol; the default is TLS\n"
"\t--verbose|-v be verbose like an old lady at marketplace, can be used multiple times\n"
"\t--store FILE|-s FILE store result to file FILE\n"
"\t--ktls|-k use AF_KTLS for communication\n"
"\t--no-echo do not echo data messages\n"
"\t--raw_recv expect unencrypted data\n"
"\t--mtu set MTU\n"
"\t--tcp|-c use TCP (can be used only with --no-tls)\n"
"\t--udp|-u use UDP (can be used only with --no-tls)\n"
"\t--help|-h print this help\n\n";
assert(progname);
printf(help_msg, progname);
}
static int parse_opts(struct server_opts *opts, int argc, char *argv[]) {
int c;
int idx = 0;
char *tmp_ptr = NULL;
bool protocol_seen = false;
bool no_tls_protocol_seen = false;
// assign default values at first
opts->tls = true;
opts->port = 0;
opts->verbose_level = VERBOSE_LEVEL_SILENT;
opts->store_file = 0;
opts->store_file = 0;
opts->ktls = false;
opts->no_echo = false;
opts->raw_recv = false;
opts->no_tls = false;
opts->tcp = true;
opts->mtu = SERVER_MAX_MTU;
// not used when standalone process
opts->port_mem = NULL;
opts->condition_initialized = NULL;
for (;;) {
c = getopt_long (argc, argv, OPT_SHORT_OPTS, long_options, &idx);
if (c == -1) // we are done
break;
switch (c) {
case OPT_TLS:
if (protocol_seen && !opts->tls) {
print_error("option --tls is disjoint with --dtls");
return 1;
}
if (no_tls_protocol_seen) {
print_error("option --dtls/--tls is disjoint with --udp/--tcp");
return 1;
}
protocol_seen = true;
opts->tls = true;
opts->no_tls = false;
break;
case OPT_DTLS:
if (protocol_seen && opts->tls) {
print_error("option --dtls is disjoint with --tls");
return 1;
}
if (no_tls_protocol_seen) {
print_error("option --dtls/--tls is disjoint with --udp/--tcp");
return 1;
}
protocol_seen = true;
opts->tls = false;
opts->no_tls = false;
break;
case OPT_TCP:
if (no_tls_protocol_seen && !opts->tcp) {
print_error("option --tcp is disjoint with --udp");
return 1;
}
if (protocol_seen) {
print_error("option --tcp/--udp is disjoint with --tls/--dtls");
return 1;
}
no_tls_protocol_seen = true;
opts->tcp = true;
opts->no_tls = true;
break;
case OPT_UDP:
if (no_tls_protocol_seen && opts->tcp) {
print_error("option --udp is disjoint with --tcp");
return 1;
}
if (protocol_seen) {
print_error("option --tcp/--udp is disjoint with --tls/--dtls");
return 1;
}
no_tls_protocol_seen = true;
opts->tcp = false;
opts->no_tls = true;
break;
case OPT_PORT:
opts->port = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->port > 65535) {
print_error("unknown port '%s'", optarg);
return 1;
}
break;
case OPT_MTU:
opts->mtu = strtoul(optarg, &tmp_ptr, 10);
if (*tmp_ptr != '\0' || opts->port > SERVER_MAX_MTU) {
print_error("unknown mtu '%s'", optarg);
return 1;
}
break;
case OPT_STORE:
if (opts->store_file) {
print_error("multiple --store supplied");
return 1;
}
opts->store_file = open(optarg, O_WRONLY|O_CREAT|O_TRUNC);
if (opts->store_file < 0) {
perror(optarg);
return 1;
}
break;
case OPT_VERBOSE:
if (opts->verbose_level < VERBOSE_LEVEL_ALL)
opts->verbose_level++;
break;
case OPT_KTLS:
opts->ktls = true;
break;
case OPT_RAW_RECV:
opts->raw_recv = true;
break;
case OPT_NO_ECHO:
opts->no_echo = true;
break;
case OPT_HELP:
print_help(argv[0]);
return 1;
break;
case '?':
print_help(argv[0]);
return 1;
break;
default:
/* should be unreachable */
assert(&long_options);
}
}
/* no additional arguments allowed */
if (optind < argc) {
print_error("unknown argument supplied: %s", argv[optind]);
print_help(argv[0]);
return 1;
}
return 0;
}
static void print_opts(struct server_opts *opts) {
if (opts->no_tls) {
print_debug_server(opts, "protocol: %s", opts->tcp ? "TCP" : "UDP");
} else {
print_debug_server(opts, "protocol: %s", opts->tls ? "TLS" : "DTLS");
}
if (opts->port)
print_debug_server(opts, "port: %u", opts->port);
else
print_debug_server(opts, "port: auto");
if (opts->store_file)
print_debug_server(opts, "store to file: (fd) '%d'", opts->store_file);
if (opts->ktls)
print_debug_server(opts, "using AF_KTLS socket");
if (opts->no_echo)
print_debug_server(opts, "server is not echoing data messages");
}
int main(int argc, char *argv[]) {
int err;
struct server_opts opts;
print_init();
err = parse_opts(&opts, argc, argv);
if (err)
return err;
if (opts.verbose_level >= VERBOSE_LEVEL_SERVER)
print_opts(&opts);
run_server(&opts);
if (opts.store_file)
close(opts.store_file);
print_destruct();
return server_err;
}