-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbt.c
184 lines (153 loc) · 3.61 KB
/
bt.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
/*
* Copyright (c) 2025 Lucas Gabriel Vuotto <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "bt.h"
#define GROWTH_FACTOR 1024
static void usage(void);
static int do_bcode(int, char *[]);
static int do_tracker(int, char *[]);
static void
usage(void)
{
const char *p;
p = getprogname();
fprintf(stderr, "Usage:\n"
"\t%s -b [torrent-file]\n"
"\t%s -t tracker\n",
p, p);
exit(1);
}
static int
do_bcode(int argc, char *argv[])
{
struct bcode *bcode;
uint8_t *buf, *p;
FILE *fp;
size_t n, bufcap, bufsz;
if (argc > 1)
usage();
if (argc == 1) {
fp = fopen(argv[0], "r");
if (fp == NULL)
err(1, "fopen");
} else
fp = stdin;
buf = malloc(GROWTH_FACTOR);
if (buf == NULL)
err(1, "out of memory");
bufcap = GROWTH_FACTOR;
bufsz = 0;
for (;;) {
if (bufsz > bufcap - GROWTH_FACTOR) {
if (bufcap > SIZE_MAX / 2)
errx(1, "out of memory");
p = reallocarray(buf, bufcap * 2, 1);
if (p == NULL)
err(1, "out of memory");
buf = p;
bufcap *= 2;
}
n = fread(&buf[bufsz], 1, GROWTH_FACTOR, fp);
if (ferror(fp))
err(1, "fread");
bufsz += n;
if (feof(fp))
break;
}
bcode = bcode_parse(buf, bufsz);
if (bcode == NULL)
errx(1, "bcode_parse: parse error");
bcode_print(stdout, bcode);
bcode_free(bcode);
free(buf);
if (fp != stdin)
(void)fclose(fp);
return 0;
}
static int
do_tracker(int argc, char *argv[])
{
struct bttc_ctx *ctx;
const char *errstr;
struct btih *btih;
struct btt_scrape_stats *stats;
int i;
if (argc < 2)
usage();
ctx = bttc_ctx_new(argv[0]);
if (ctx == NULL)
err(1, "out of memory");
argc--;
argv++;
btih = reallocarray(NULL, argc, sizeof(*btih));
stats = reallocarray(NULL, argc, sizeof(*stats));
if (btih == NULL || stats == NULL)
err(1, "out of memory");
for (i = 0; i < argc; i++)
if (!btih_parse(&btih[i], argv[i]))
errx(1, "invalid info hash: %s", argv[i]);
errno = 0;
if (!bttc_scrape(ctx, stats, btih, argc, &errstr)) {
if (errno)
err(1, "announce failed: %s: %s",
bttc_get_tracker(ctx), errstr);
else
errx(1, "announce failed: %s: %s",
bttc_get_tracker(ctx), errstr);
}
for (i = 0; i < argc; i++)
printf("%s\t%" PRIu32 "\t%" PRIu32 "\t%" PRIu32 "\n",
argv[i], stats[i].seeders, stats[i].completed,
stats[i].leechers);
free(btih);
free(stats);
return 0;
}
int
main(int argc, char *argv[])
{
int ch, bflag, tflag;
bflag = tflag = 0;
while ((ch = getopt(argc, argv, "bt")) != -1) {
switch (ch) {
case 'b':
bflag = 1;
break;
case 't':
tflag = 1;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (bflag && tflag)
errx(1, "-b and -t are mutually exclusive");
if (!bflag && !tflag)
usage();
if (bflag)
return do_bcode(argc, argv);
else if (tflag)
return do_tracker(argc, argv);
/* UNREACHABLE */
return 1;
}