-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
563 lines (515 loc) · 17.8 KB
/
main.c
File metadata and controls
563 lines (515 loc) · 17.8 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// Copyright (c) 2024-2025 Lars-Christian Schulz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "console.h"
#include "format.h"
#include "scion/scion.h"
#define OPTPARSE_IMPLEMENTATION
#include "optparse.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if _WIN32
#include <Winsock2.h>
#include <WS2tcpip.h>
#else
#include <arpa/inet.h>
#include <getopt.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#endif
struct arguments {
const char* daemon_addr;
const char* local_addr;
const char* remote_addr;
const char* message;
int count;
bool interactive;
bool stun;
bool quiet;
};
static const char* HELP_MESSAGE = \
"Usage: echo -local LOCAL -remote REMOTE -msg MESSAGE -count COUNT\n"
" LOCAL Local IP address and port (required for servers)\n"
" REMOTE Scion address of the remote server (only for clients)\n"
" MESSAGE The message clients will send to the server\n"
" COUNT Number of messages to send\n"
"Optional Flags:\n"
" -interactive Prompt for path selection (client only)\n"
" -stun Attempt NAT traversal (client only)\n"
" -quiet Don't print addresses and paths";
bool parse_args(int argc, char* argv[], struct arguments* args)
{
static const struct optparse_long longopts[] = {
{ "help", 'h', OPTPARSE_NONE },
{ "sciond", 'd', OPTPARSE_REQUIRED },
{ "local", 'l', OPTPARSE_REQUIRED },
{ "remote", 'r', OPTPARSE_REQUIRED },
{ "msg", 'm', OPTPARSE_REQUIRED },
{ "count", 'c', OPTPARSE_REQUIRED },
{ "interactive", 'i', OPTPARSE_NONE},
{ "stun", 's', OPTPARSE_NONE},
{ "quiet", 'q', OPTPARSE_NONE},
{ 0 }
};
memset(args, 0, sizeof(struct arguments));
args->count = 1;
args->message = "Hello!";
struct optparse options;
optparse_init(&options, argv);
int opt = -1;
while ((opt = optparse_long(&options, longopts, NULL)) != -1) {
switch (opt)
{
case 'd':
args->daemon_addr = options.optarg;
break;
case 'l':
args->local_addr = options.optarg;
break;
case 'r':
args->remote_addr = options.optarg;
break;
case 'm':
args->message = options.optarg;
break;
case 'c':
args->count = atoi(options.optarg);
break;
case 'i':
args->interactive = true;
break;
case 's':
args->stun = true;
break;
case 'q':
args->quiet = true;
break;
case 'h':
default:
puts(HELP_MESSAGE);
return false;
}
};
// Check for mandatory options
if (!args->local_addr && !args->remote_addr) {
puts("At least one of local (for servers) and remote (for clients) is required");
return false;
}
if (!args->daemon_addr) {
args->daemon_addr = getenv("SCION_DAEMON_ADDRESS");
}
return true;
}
////////////
// Server //
////////////
struct Server
{
scion_socket* socket;
scion_raw_path* path;
scion_hdr_cache* headers;
struct sockaddr_scion from;
struct sockaddr_storage next_hop;
struct scion_packet pkt;
char buffer[1024];
bool quiet;
};
void server_received(scion_error status, void* recvd, size_t n, void* user_ptr);
void server_sent(scion_error err, size_t n, void* user_ptr);
int run_server(struct Server* srv,
scion_context* ctx, const struct sockaddr_storage* bind, const struct arguments* args)
{
memset(srv, 0, sizeof(struct Server));
// Create and bind UDP socket
scion_error err = scion_socket_create(ctx, &srv->socket, SOCK_DGRAM);
if (err) {
printf("Allocating socket failed\n");
return EXIT_FAILURE;
}
err = scion_bind(srv->socket, (const struct sockaddr*)bind, sizeof(*bind));
if (err) {
printf("Can't bind to %s (%s:%d)\n", args->local_addr, scion_error_string(err), err);
return EXIT_FAILURE;
}
CON_CURSES(curses_init_server());
{
struct sockaddr_scion bound;
scion_getsockname(srv->socket, &bound);
char buffer[64];
size_t buffer_len = sizeof(buffer);
if (!scion_print_ep(&bound, buffer, &buffer_len))
console_printf("Server listening at %s\nPress q to quit.\n", buffer);
}
// Receive and echo back
srv->path = scion_raw_path_allocate();
if (!srv->path) return EXIT_FAILURE;
srv->headers = scion_hdr_cache_allocate();
if (!srv->headers) return EXIT_FAILURE;
srv->pkt.addr = &srv->from;
srv->pkt.underlay = (struct sockaddr*)&srv->next_hop;
srv->pkt.underlay_len = sizeof(srv->next_hop);
SCION_SET_PATH(srv->pkt, srv->path);
srv->quiet = args->quiet;
struct scion_async_recv_handler handler = {
&server_received, srv
};
scion_recv_async(srv->socket, srv->buffer, sizeof(srv->buffer), &srv->pkt, handler);
return EXIT_SUCCESS;
}
void server_cleanup(struct Server* srv)
{
CON_CURSES(curses_end_server());
scion_hdr_cache_free(srv->headers);
scion_raw_path_free(srv->path);
scion_close(srv->socket);
}
void server_received(scion_error err, void* recvd, size_t n, void* user_ptr)
{
struct Server* srv = user_ptr;
if (!recvd) {
console_printf("Error: %s:%d\n", scion_error_string(err), err);
return;
}
if (!srv->quiet) {
char str[128];
size_t str_len = sizeof(str);
if (!scion_print_ep(&srv->from, str, &str_len))
console_printf("Received %zu bytes from %s\n", n, str);
str_len = sizeof(srv->buffer);
if (!scion_raw_path_print(srv->path, str, &str_len))
console_printf("Path: %s\n", str);
}
if (!scion_raw_path_reverse(srv->path)) {
struct scion_async_send_handler handler = {
&server_sent, srv
};
scion_send_async(srv->socket, srv->headers, recvd, n, &srv->pkt, handler);
}
}
void server_sent(scion_error err, size_t n, void* user_ptr)
{
struct Server* srv = user_ptr;
if (err) {
console_printf("Error: %s:%d\n", scion_error_string(err), err);
return;
}
struct scion_async_recv_handler handler = {
&server_received, srv
};
scion_recv_async(srv->socket, srv->buffer, sizeof(srv->buffer), &srv->pkt, handler);
}
////////////
// Client //
////////////
struct Client
{
scion_timer* timer;
scion_path* path;
scion_socket* socket;
scion_hdr_cache* headers;
struct sockaddr_scion from;
struct sockaddr_storage next_hop;
socklen_t next_hop_len;
struct scion_packet pkt;
char buffer[1024];
const struct arguments* args;
int i;
};
void client_stun_sent(scion_error err, size_t n, void* user_ptr);
void client_stun_received(scion_error err, void* recvd, size_t n, void* user_ptr);
void client_sent(scion_error err, size_t n, void* user_ptr);
void client_received(scion_error err, void* recvd, size_t n, void* user_ptr);
void client_timeout(scion_error err, void* user_ptr);
int client_init(struct Client* cl,
scion_context* ctx, const struct sockaddr_storage* bind, const struct arguments* args)
{
memset(cl, 0, sizeof(struct Client));
cl->timer = scion_timer_allocate(ctx);
if (!cl->timer) return EXIT_FAILURE;
struct sockaddr_scion remote;
size_t len = sizeof(remote);
if (scion_resolve_name(ctx, args->remote_addr, &remote, &len)) {
printf("Can't resolve %s\n", args->remote_addr);
return EXIT_FAILURE;
}
// Get paths to destination AS
scion_path* paths[32];
size_t path_count = sizeof(paths) / sizeof(*paths);
const uint64_t dst_as = scion_ntohll(remote.sscion_addr.sscion_isd_asn);
scion_error err = scion_query_paths(ctx, dst_as, paths, &path_count);
if (err != SCION_OK && err != SCION_BUFFER_TOO_SMALL) {
printf("Error fetching paths (%s:%d)\n", scion_error_string(err), err);
return EXIT_FAILURE;
}
if (err == SCION_BUFFER_TOO_SMALL) {
path_count = sizeof(paths) / sizeof(*paths);
}
if (path_count == 0) {
char buffer[128] = {0};
size_t len = sizeof(buffer);
err = scion_print_ep(&remote, buffer, &len);
if (err == SCION_OK || err == SCION_BUFFER_TOO_SMALL) {
printf("No path to %s\n", buffer);
}
return EXIT_FAILURE;
}
// Select one path
size_t selection = 0;
if (args->interactive) {
char buffer[128] = {0};
for (size_t i = 0; i < path_count; ++i) {
size_t len = sizeof(buffer);
err = scion_path_print(paths[i], buffer, &len);
if (err == SCION_OK || err == SCION_BUFFER_TOO_SMALL) {
printf("[%2zu] %s\n", i, buffer);
}
}
while (true) {
printf("Choose path: ");
if (scanf("%zu", &selection) == 1 && selection < path_count) {
break;
}
}
} else {
selection = (size_t)rand() % path_count;
}
cl->path = paths[selection];
paths[selection] = NULL;
scion_release_paths(paths, path_count); // release paths we didn't choose
cl->next_hop_len = sizeof(cl->next_hop);
err = scion_path_next_hop(cl->path, (struct sockaddr*)&cl->next_hop, &cl->next_hop_len);
if (err == SCION_PATH_IS_EMPTY) {
if (scion_sockaddr_get_host(&remote, (struct sockaddr*)&cl->next_hop, cl->next_hop_len))
return EXIT_FAILURE;
} else if (err) {
return EXIT_FAILURE;
}
// Create and connect UDP socket
err = scion_socket_create(ctx, &cl->socket, SOCK_DGRAM);
if (err) {
printf("Allocating socket failed\n");
return EXIT_FAILURE;
}
err = scion_bind(cl->socket, (const struct sockaddr*)bind, sizeof(*bind));
if (err) {
printf("Can't bind to %s (%s:%d)\n", args->local_addr, scion_error_string(err), err);
return EXIT_FAILURE;
}
err = scion_connect(cl->socket, &remote);
if (err) return EXIT_FAILURE;
return EXIT_SUCCESS;
}
void client_cleanup(struct Client* cl)
{
scion_hdr_cache_free(cl->headers);
scion_close(cl->socket);
scion_release_paths(&cl->path, 1);
scion_timer_free(cl->timer);
}
void client_get_stun_mapping(scion_context* ctx, struct Client* cl)
{
struct sockaddr_storage stun_server;
memcpy(&stun_server, &cl->next_hop, cl->next_hop_len);
if (stun_server.ss_family == AF_INET)
((struct sockaddr_in*)&stun_server)->sin_port = htons(3478);
else if (stun_server.ss_family == AF_INET6)
((struct sockaddr_in6*)&stun_server)->sin6_port = htons(3478);
struct scion_async_send_handler handler = {
&client_stun_sent, cl
};
scion_request_stun_mapping_async(cl->socket,
(struct sockaddr*)&stun_server, cl->next_hop_len, handler);
if (scion_run_for(ctx, 500) < 2) {
// STUN timed out, cancel socket operations and run completion handlers
scion_cancel(cl->socket);
scion_restart(ctx);
scion_run(ctx);
}
}
void client_stun_sent(scion_error err, size_t n, void* user_ptr)
{
struct Client* cl = user_ptr;
struct scion_async_recv_handler handler = {
&client_stun_received, cl
};
scion_recv_stun_response_async(cl->socket, handler);
}
void client_stun_received(scion_error err, void* recvd, size_t n, void* user_ptr)
{
struct Client* cl = user_ptr;
if (err == SCION_STUN_RECEIVED) {
struct sockaddr_scion addr;
scion_getmapped(cl->socket, &addr);
char buffer[128] = {0};
size_t len = sizeof(buffer);
if (!scion_print_ep(&addr, buffer, &len))
printf("SNAT mapped address: %s\n", buffer);
} else {
printf("Can't get SNAT address mapping: %s\n", scion_error_string(err));
}
}
int client_run(struct Client* cl,
scion_context* ctx, const struct sockaddr_storage* bind, const struct arguments* args)
{
// Send message
cl->headers = scion_hdr_cache_allocate();
if (!cl->headers) return EXIT_FAILURE;
SCION_SET_PATH(cl->pkt, cl->path);
cl->pkt.underlay = (struct sockaddr*)&cl->next_hop;
cl->pkt.underlay_len = sizeof(cl->next_hop);
cl->args = args;
struct scion_async_send_handler handler = {
&client_sent, cl
};
scion_send_async(cl->socket, cl->headers, args->message, strlen(args->message), &cl->pkt, handler);
return EXIT_SUCCESS;
}
void client_sent(scion_error err, size_t n, void* user_ptr)
{
struct Client* cl = user_ptr;
if (err) {
printf("Error: %s:%d\n", scion_error_string(err), err);
return;
}
struct scion_async_recv_handler handler = {
&client_received, cl
};
cl->pkt.addr = &cl->from;
SCION_SET_PATH(cl->pkt, NULL);
scion_recv_async(cl->socket, cl->buffer, sizeof(cl->buffer), &cl->pkt, handler);
scion_timer_set_timeout(cl->timer, 1000);
struct scion_wait_handler timer_handler = {
&client_timeout, cl
};
scion_timer_wait_async(cl->timer, timer_handler);
}
void client_received(scion_error err, void* recvd, size_t n, void* user_ptr)
{
struct Client* cl = user_ptr;
// client_timeout might cancel operations if the timer has already expired
scion_timer_cancel(cl->timer);
if (!recvd) {
printf("Error: %s:%d\n", scion_error_string(err), err);
return;
}
if (!cl->args->quiet) {
char addr[128];
size_t addr_len = sizeof(addr);
if (!scion_print_ep(&cl->from, addr, &addr_len))
printf("Received %zu bytes from %s:\n", n, addr);
}
print_buffer(recvd, n);
if (++(cl->i) < cl->args->count) {
struct scion_async_send_handler handler = {
&client_sent, cl
};
scion_send_cached_async(
cl->socket, cl->headers, cl->args->message, strlen(cl->args->message), &cl->pkt, handler);
}
}
void client_timeout(scion_error err, void* user_ptr)
{
struct Client* cl = user_ptr;
if (err == SCION_CANCELLED) return;
printf("No response\n");
scion_cancel(cl->socket);
}
int main(int argc, char* argv[])
{
struct arguments args;
if (!parse_args(argc, argv, &args))
return EXIT_FAILURE;
// Initialize host context
scion_context* ctx = NULL;
struct scion_context_opts opts;
memset(&opts, 0, sizeof(struct scion_context_opts));
opts.daemon_address = args.daemon_addr;
scion_error err = scion_create_host_context(&ctx, &opts);
if (err) {
printf("Failed to created host context (%s:%d)\n", scion_error_string(err), err);
return EXIT_FAILURE;
}
// Parse bind address
struct sockaddr_storage bind_addr = {0};
if (!args.local_addr) {
bind_addr.ss_family = AF_INET;
} else {
const char* phost = NULL;
size_t host_len = 0;
uint16_t port = 0;
if (scion_split_host_port(args.local_addr, &phost, &host_len, &port)) {
printf("Invalid bind address: %s\n", args.local_addr);
scion_delete_host_context(ctx);
return EXIT_FAILURE;
}
char host[128];
if (host_len > sizeof(host) - 1) host_len = sizeof(host) - 1;
memcpy(host, phost, host_len);
host[host_len] = '\0';
struct addrinfo* res = NULL;
if (getaddrinfo(host, NULL, NULL, &res)) {
printf("Invalid bind address: %s\n", args.local_addr);
scion_delete_host_context(ctx);
return EXIT_FAILURE;
}
if (res) {
memcpy(&bind_addr, res->ai_addr, res->ai_addrlen);
if (bind_addr.ss_family == AF_INET)
((struct sockaddr_in*)&bind_addr)->sin_port = port;
else if (bind_addr.ss_family == AF_INET6)
((struct sockaddr_in*)&bind_addr)->sin_port = port;
freeaddrinfo(res);
}
}
int result = 0;
if (args.remote_addr) {
struct Client* client = malloc(sizeof(struct Client));
result = client_init(client, ctx, &bind_addr, &args);
if (result == EXIT_SUCCESS) {
if (args.stun) {
client_get_stun_mapping(ctx, client);
scion_restart(ctx);
}
if (client_run(client, ctx, &bind_addr, &args) == EXIT_SUCCESS) {
scion_run(ctx);
}
}
client_cleanup(client);
free(client);
} else {
struct Server* server = malloc(sizeof(struct Server));
result = run_server(server, ctx, &bind_addr, &args);
if (result == EXIT_SUCCESS) {
CON_WIN32(HANDLE hConsoleInput = GetStdHandle(STD_INPUT_HANDLE));
CON_WIN32(while (!isKeyPressed(hConsoleInput, 'Q')))
CON_CURSES(while (curses_get_char() != 'q'))
{
CON_CURSES(curses_refresh_screen());
scion_run_for(ctx, 100);
}
}
server_cleanup(server);
free(server);
}
scion_delete_host_context(ctx);
return result;
}