summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: b156e1b9e35d33e012d7cab8da3b717412b589a6 (plain)
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
 * Copyright (C) 2021 Soleta Networks <info@soleta.eu>
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 */

#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <ev.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <string.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <syslog.h>
#include <stddef.h>
#include <stdbool.h>
#include <sys/time.h>
#include <limits.h>
#include <syslog.h>
#include <netinet/tcp.h>
#include <fcntl.h>

#define TIP_TORRENT_PORT	9999

/* number of chunks for files. */
#define MAX_CHUNKS		64

#define container_of(ptr, type, member) ({			\
	typeof( ((type *)0)->member ) *__mptr = (ptr);		\
	(type *)( (char *)__mptr - offsetof(type,member) );})

static const char *filename;
static const char *addr;
struct ev_loop *tip_main_loop;

enum {
	TIP_CLIENT_GET_HEADER,
	TIP_CLIENT_GET_PAYLOAD,
	TIP_CLIENT_POST_REDIRECT,
	TIP_CLIENT_HEAD_HEADER,
	TIP_CLIENT_DONE,
};

struct tip_client {
	ev_io io;
	struct sockaddr_in addr;
	char buf[10240000];
	uint32_t buf_len;
	uint64_t data_len;
	uint64_t content_len;
	uint64_t chunk_offset;
	int state;
	int num_retries;
	int fd;
	bool error;
	bool fatal;
	bool redirected;
	bool server_only;
	struct timeval tv_start, tv_last;
	const char *payload;
};

static struct tip_client _cli = {
	.fd = -1,
};

struct {
	uint32_t	direct_from_server;
	uint32_t	redirects;
} tip_client_stats;

static int tip_client_recv(struct tip_client *cli, int events)
{
	struct ev_io *io = &cli->io;
	int ret;

	ret = recv(io->fd, cli->buf + cli->buf_len,
		   sizeof(cli->buf) - cli->buf_len, 0);
	if (ret < 0) {
		syslog(LOG_ERR, "error reading from server %s:%hu (%s)\n",
		       inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port),
		       strerror(errno));
	}

	return ret;
}

static void tip_client_close(struct tip_client *cli)
{
	ev_io_stop(tip_main_loop, &cli->io);
	shutdown(cli->io.fd, SHUT_RDWR);
	close(cli->io.fd);
	cli->buf_len = 0;
}

static void tip_client_error(struct tip_client *cli)
{
	cli->error = true;
	tip_client_close(cli);
}

/* display progress message every 30 seconds. */
#define TIP_CLIENT_PROGRESS	30

static void tip_client_progress(struct tip_client *cli, bool now)
{
	struct timeval tv_cur, tv;

	gettimeofday(&tv_cur, NULL);
	timersub(&tv_cur, &cli->tv_last, &tv);
	if (now || tv.tv_sec >= TIP_CLIENT_PROGRESS) {
		timersub(&tv_cur, &cli->tv_start, &tv);
		printf("%3lu%% (%lu Mbytes/second) %s from %s:9999\n",
		       cli->content_len > 0 ? 100 * cli->data_len / cli->content_len : 0,
		       tv.tv_sec > 0 ? cli->data_len / 1024000 / tv.tv_sec : cli->data_len / 1024000,
		       filename, inet_ntoa(cli->addr.sin_addr));
		cli->tv_last = tv_cur;
	}
}

static int tip_client_connect(const char *addr);

static int tip_client_get_hdr(struct tip_client *cli)
{
	char *ptr, *trailer, *payload;
	char redirect_addr[32];
	uint32_t payload_len;
	uint32_t header_len;
	int ret;

	ptr = strstr(cli->buf, "\r\n\r\n");
	if (!ptr)
		return 0;

	if (!strncmp(cli->buf, "HTTP/1.1 404 Not Found", strlen("HTTP/1.1 404 Not Found"))) {
		syslog(LOG_ERR, "server says file `%s' not found\n", filename);
		return -1;
	}
	if (!strncmp(cli->buf, "HTTP/1.1 301 Moves Permanently", strlen("HTTP/1.1 301 Moves Permanently"))) {
		ptr = strstr(cli->buf, "Location:");
		if (!ptr)
			return -1;

		ret = sscanf(ptr, "Location: http://%31s[^\r\n]",
			     redirect_addr);
		if (ret != 1)
			return -1;

		ptr = strchr(redirect_addr, ':');
		if (!ptr)
			return -1;

		ptr[0] = '\0';

		syslog(LOG_INFO, "Redirected to %s to fetch file %s\n",
		       redirect_addr, filename);

		cli->redirected = true;
		tip_client_close(cli);
		tip_client_connect(redirect_addr);
		cli->state = TIP_CLIENT_GET_HEADER;

		return 0;
	}

	trailer = ptr + 4;

	ptr = strstr(cli->buf, "Content-Length: ");
	if (!ptr)
		return -1;

	if (sscanf(ptr, "Content-Length: %lu[^\r\n]", &cli->content_len) != 1)
		return -1;
	if (cli->content_len < 0)
		return -1;

	tip_client_progress(cli, true);

	if (cli->content_len == 0) {
		cli->buf_len = 0;
		return 1;
	}

	lseek(cli->fd, cli->chunk_offset, SEEK_SET);
	header_len = trailer - cli->buf;
	payload = cli->buf + header_len;
	payload_len = cli->buf_len - header_len;
	cli->data_len += payload_len;
	cli->buf_len = 0;
	gettimeofday(&cli->tv_start, NULL);
	cli->tv_last = cli->tv_start;

	if (payload_len > 0) {
		ret = write(cli->fd, payload, payload_len);
		if (ret < 0) {
			syslog(LOG_ERR, "failed to write to file %s: %s",
			       filename, strerror(errno));
			return ret;
		}
	}

	return 1;
}

static int tip_client_get_payload(struct tip_client *cli)
{
	int ret;

	cli->data_len += cli->buf_len;

	ret = write(cli->fd, cli->buf, cli->buf_len);
	if (ret < 0) {
		syslog(LOG_ERR, "failed to write to file %s: %s",
		       filename, strerror(errno));
		return ret;
	}

	cli->buf_len = 0;

	tip_client_progress(cli, false);

	if (cli->data_len >= cli->content_len) {
		if (cli->redirected) {
			tip_client_close(cli);
			tip_client_connect(addr);
			cli->state = TIP_CLIENT_POST_REDIRECT;

			return 1;
		}
		cli->state = TIP_CLIENT_DONE;
		return 0;
	}

	return 1;
}

static int delete_file(const char *filename)
{
	if (unlink(filename) < 0) {
		syslog(LOG_ERR, "cannot delete file %s: %s",
		       filename, strerror(errno));
		return -1;
	}

	return 0;
}

static int tip_client_head_hdr(struct tip_client *cli)
{
	char *ptr;

	ptr = strstr(cli->buf, "\r\n\r\n");
	if (!ptr)
		return 0;

	if (!strncmp(cli->buf, "HTTP/1.1 404 Not Found", strlen("HTTP/1.1 404 Not Found"))) {
		syslog(LOG_ERR, "server says file `%s' not found\n", filename);
		return -1;
	}

	ptr = strstr(cli->buf, "Content-Length: ");
	if (!ptr)
		return -1;

	if (sscanf(ptr, "Content-Length: %lu[^\r\n]", &cli->content_len) != 1)
		return -1;
	if (cli->content_len < 0)
		return -1;
	if (cli->content_len == 0) {
		syslog(LOG_ERR, "server reports zero size file %s", filename);
		return -1;
	}

	cli->fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
	if (cli->fd < 0) {
		syslog(LOG_ERR, "failed to open file %s: %s",
		       filename, strerror(errno));
		return -1;
	}

	if (fallocate(cli->fd, 0, 0, cli->content_len) < 0) {
		syslog(LOG_ERR, "failed to allocate room for file %s: %s",
		       filename, strerror(errno));
		delete_file(filename);
		cli->fatal = true;
		return -1;
	}

	cli->state = TIP_CLIENT_DONE;

	return 1;
}

static int tip_client_post_redirect(struct tip_client *cli)
{
	char *ptr;

	ptr = strstr(cli->buf, "\r\n\r\n");
	if (!ptr)
		return 0;

	if (strncmp(cli->buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK")))
		return -1;

	ptr = strstr(cli->buf, "Content-Length: ");
	if (!ptr)
		return -1;

	if (sscanf(ptr, "Content-Length: %lu[^\r\n]", &cli->content_len) != 1)
		return -1;
	if (cli->content_len < 0)
		return -1;

	if (cli->content_len != 0)
		return -1;

	cli->state = TIP_CLIENT_DONE;

	return 0;
}

static void tip_client_read_cb(struct ev_loop *loop, struct ev_io *io, int events)
{
	struct tip_client *cli;
	int ret;

	cli = container_of(io, struct tip_client, io);

	if (events & EV_ERROR) {
		tip_client_error(cli);
		return;
	}

	ret = tip_client_recv(cli, events);
	if (ret <= 0)
		goto error;

	cli->buf_len += ret;

	switch (cli->state) {
	case TIP_CLIENT_GET_HEADER:
		ret = tip_client_get_hdr(cli);
		if (ret < 0)
			goto error;
		if (!ret)
			return;

		cli->state = TIP_CLIENT_GET_PAYLOAD;
		/* Fall through. */
	case TIP_CLIENT_GET_PAYLOAD:
		ret = tip_client_get_payload(cli);
		if (ret < 0)
			goto error;
		if (ret == 0)
			goto close;
		break;
	case TIP_CLIENT_HEAD_HEADER:
		ret = tip_client_head_hdr(cli);
		if (ret < 0)
			goto error;
		if (!ret)
			return;
		goto close;
	case TIP_CLIENT_POST_REDIRECT:
		ret = tip_client_post_redirect(cli);
		if (ret < 0)
			goto error;
		if (ret == 0)
			goto close;
		break;
	}
	return;
error:
	tip_client_error(cli);
	return;
close:
	tip_client_close(cli);
	return;
}

static void tip_client_connect_cb(struct ev_loop *loop, struct ev_io *io, int events)
{
	struct tip_client *cli;
	char buf[PATH_MAX + 1];
	int ret, len;

	cli = container_of(io, struct tip_client, io);

	if (events & EV_ERROR) {
		tip_client_error(cli);
		return;
	}

	len = sizeof(cli->addr);
	ret = connect(cli->io.fd, (struct sockaddr *)&cli->addr, len);
	if (ret < 0) {
		syslog(LOG_ERR, "failed to connect to server to fetch %s", filename);
		tip_client_error(cli);
		return;
	}

	switch (cli->state) {
	case TIP_CLIENT_GET_HEADER:
		syslog(LOG_INFO, "connected to %s to fetch file %s\n",
		       inet_ntoa(cli->addr.sin_addr), filename);
		if (cli->server_only)
			snprintf(buf, sizeof(buf), "GET /%s HTTP/1.1\r\nX-Accept-Redirect: off\r\n\r\n", filename);
		else
			snprintf(buf, sizeof(buf), "GET /%s HTTP/1.1\r\n\r\n", filename);
		break;
	case TIP_CLIENT_HEAD_HEADER:
		syslog(LOG_INFO, "connected to %s to get file size of %s\n",
		       inet_ntoa(cli->addr.sin_addr), filename);
		snprintf(buf, sizeof(buf), "HEAD /%s HTTP/1.1\r\n\r\n", filename);
		break;
	case TIP_CLIENT_POST_REDIRECT:
		syslog(LOG_INFO, "connected to %s to report redirection for %s\n",
		       inet_ntoa(cli->addr.sin_addr), filename);
		snprintf(buf, sizeof(buf), "POST /%s HTTP/1.1\r\n\r\n", filename);
		break;
	}

	ret = send(cli->io.fd, buf, strlen(buf), 0);
	if (ret < 0) {
		syslog(LOG_ERR, "failed to send request for %s", filename);
		tip_client_error(cli);
		return;
	}

	ev_io_stop(tip_main_loop, &cli->io);
	ev_io_init(&cli->io, tip_client_read_cb, cli->io.fd, EV_READ);
	ev_io_start(tip_main_loop, &cli->io);
}

#define TIP_TCP_KEEPALIVE_IDLE	60
#define TIP_TCP_KEEPALIVE_INTL	30
#define TIP_TCP_KEEPALIVE_CNT	4

static int tip_client_connect(const char *addr)
{
	int intl = TIP_TCP_KEEPALIVE_INTL, cnt = TIP_TCP_KEEPALIVE_CNT;
	int on = 1, idle = TIP_TCP_KEEPALIVE_IDLE;
	struct tip_client *cli = &_cli;
	int remote_fd;
	int flags;
	int len;
	int ret;

	remote_fd = socket(AF_INET, SOCK_STREAM, 0);
	if (remote_fd < 0) {
		tip_client_error(cli);
		return -1;
	}

	setsockopt(remote_fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(int));
	setsockopt(remote_fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(int));
	setsockopt(remote_fd, IPPROTO_TCP, TCP_KEEPINTVL, &intl, sizeof(int));
	setsockopt(remote_fd, IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(int));

	flags = fcntl(remote_fd, F_GETFL);
	flags |= O_NONBLOCK;
	ret = fcntl(remote_fd, F_SETFL, flags);
	if (ret < 0) {
		tip_client_error(cli);
		return ret;
	}

	cli->addr.sin_family = AF_INET;
	cli->addr.sin_addr.s_addr = inet_addr(addr);
	cli->addr.sin_port = htons(TIP_TORRENT_PORT);

	len = sizeof(cli->addr);
	ret = connect(remote_fd, (struct sockaddr *)&cli->addr, len);
	if (ret < 0 && errno != EINPROGRESS) {
		syslog(LOG_ERR, "failed to connect to server to fetch %s", filename);
		tip_client_error(cli);
		return ret;
	}

	ev_io_init(&cli->io, tip_client_connect_cb, remote_fd, EV_WRITE);
	ev_io_start(tip_main_loop, &cli->io);

	return 0;
}

#define MAX_RETRIES	5
#define WAIT_RETRY	5	/* wait 5 seconds before retrying. */

static int tip_client_request_file(struct tip_client *cli,
				   const char *server, const char *filename)
{
	tip_client_connect(server);

	while (cli->state != TIP_CLIENT_DONE && !cli->error)
		ev_loop(tip_main_loop, 0);

	if (cli->error) {
		if (cli->fatal) {
			syslog(LOG_ERR, "Fatal error, bailing out!\n");
			return -1;
		}
		syslog(LOG_ERR, "Failed to fetch file %s\n", filename);
		sleep(WAIT_RETRY);
		if (cli->num_retries++ >= MAX_RETRIES) {
			syslog(LOG_ERR, "Maximum number of retries (%d), bailing out!\n",
			       MAX_RETRIES);
			return -1;
		}
		cli->error = false;
		return 1;
	}

	return 0;
}


static uint32_t select_file_chunk(bool *file_chunk)
{
	struct timeval tv;
	uint32_t k;
	int i;

	gettimeofday(&tv, NULL);
	srand(tv.tv_usec);

	k = rand() % MAX_CHUNKS;
	for (i = 0; i < MAX_CHUNKS; i++) {
		if (!file_chunk[k])
			break;

		k++;
		if (k == MAX_CHUNKS)
			k = 0;
	}

	return k;
}

static void tip_client_reset_state(struct tip_client *cli, int fd,
				   uint64_t chunk_offset)
{
	int num_retries = cli->num_retries;

	memset(cli, 0, sizeof(*cli));
	cli->chunk_offset = chunk_offset;
	cli->fd = fd;
	cli->num_retries = num_retries;
}

static char _filename[PATH_MAX + 1];

int main(int argc, char *argv[])
{
	struct timeval tv_start, tv_stop, tv;
	uint64_t data_len = 0, file_size = 0;
	bool file_chunk[MAX_CHUNKS] = {};
	uint64_t chunk_size;
	int i, k, fd, ret;

	if (argc != 3) {
		printf("%s [ip] [file]\n", argv[0]);
		return EXIT_FAILURE;
	}
	addr = argv[1];
	filename = argv[2];

	openlog("tiptorrent-client", LOG_PID, LOG_DAEMON);

	signal(SIGPIPE, SIG_IGN);

	tip_main_loop = ev_default_loop(0);

	gettimeofday(&tv_start, NULL);

	do {
		tip_client_reset_state(&_cli, -1, 0);
		_cli.state = TIP_CLIENT_HEAD_HEADER;
		ret = tip_client_request_file(&_cli, addr, filename);
	} while (ret > 0);

	if (ret < 0)
		goto err_bailout;

	fd = _cli.fd;
	file_size = _cli.content_len;

	for (i = 0; i < MAX_CHUNKS; i++) {
		k = select_file_chunk(file_chunk);
		snprintf(_filename, sizeof(_filename), "%s.%u", argv[2], k);
		filename = _filename;
		chunk_size = file_size / MAX_CHUNKS;

		do {
			tip_client_reset_state(&_cli, fd, chunk_size * k);
			syslog(LOG_INFO, "Requesting file %s to server\n", filename);
			_cli.state = TIP_CLIENT_GET_HEADER;
			ret = tip_client_request_file(&_cli, addr, filename);
		} while (ret > 0);

		if (ret < 0) {
			do {
				tip_client_reset_state(&_cli, fd, chunk_size * k);
				_cli.server_only = true;
				syslog(LOG_INFO, "Requesting file %s to server only\n", filename);
				_cli.state = TIP_CLIENT_GET_HEADER;
				ret = tip_client_request_file(&_cli, addr, filename);
			} while (ret > 0);
		}

		if (ret < 0)
			goto err_bailout;

		if (_cli.redirected)
			tip_client_stats.redirects++;
		else
			tip_client_stats.direct_from_server++;

		tip_client_progress(&_cli, true);
		file_chunk[k] = true;
		data_len += _cli.data_len;
	}
	close(fd);
	gettimeofday(&tv_stop, NULL);
	timersub(&tv_stop, &tv_start, &tv);

	if (data_len != file_size) {
		syslog(LOG_ERR, "Failure, file size is %lu bytes but received %lu bytes!\n",
		       file_size, data_len);
		return EXIT_FAILURE;
	}

	if (_cli.state == TIP_CLIENT_DONE) {
		printf("OK.\n");
		syslog(LOG_INFO, "Done in %lu.%06lu seconds (%lu Mbytes/second). "
		       "Direct from server: %u Redirected: %u\n",
		       tv.tv_sec, tv.tv_usec,
		       tv.tv_sec > 0 ? data_len / 1024000 / tv.tv_sec : data_len / 1024000,
		       tip_client_stats.direct_from_server,
		       tip_client_stats.redirects);
		return EXIT_SUCCESS;
	}

	printf("Failure, see syslog for details.\n");
	syslog(LOG_INFO, "Failure after %lu.%06lu seconds (%lu Mbytes/second). "
	       "Direct from server: %u Redirected: %u\n",
	       tv.tv_sec, tv.tv_usec,
	       tv.tv_sec > 0 ? data_len / 1024000 / tv.tv_sec : data_len / 1024000,
	       tip_client_stats.direct_from_server,
	       tip_client_stats.redirects);
	return EXIT_FAILURE;

err_bailout:
	if (_cli.fatal)
		return EXIT_FAILURE;

	syslog(LOG_INFO, "Failure after maximum number of retries. "
	       "Direct from server: %u Redirected: %u\n",
	       tip_client_stats.direct_from_server,
	       tip_client_stats.redirects);
	return EXIT_FAILURE;
}