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
|
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include "btpd.h"
HTBL_TYPE(numtbl, tlib, unsigned, num, nchain);
HTBL_TYPE(hashtbl, tlib, uint8_t, hash, hchain);
static unsigned m_nextnum;
static unsigned m_ntlibs;
static struct numtbl *m_numtbl;
static struct hashtbl *m_hashtbl;
unsigned
tlib_count(void)
{
return m_ntlibs;
}
struct tlib *
tlib_by_num(unsigned num)
{
return numtbl_find(m_numtbl, &num);
}
struct tlib *
tlib_by_hash(const uint8_t *hash)
{
return hashtbl_find(m_hashtbl, hash);
}
void
tlib_kill(struct tlib *tl)
{
numtbl_remove(m_numtbl, &tl->num);
hashtbl_remove(m_hashtbl, tl->hash);
free(tl);
m_ntlibs--;
}
struct tlib *
tlib_create(const uint8_t *hash)
{
struct tlib *tl = btpd_calloc(1, sizeof(*tl));
char hex[SHAHEXSIZE];
bin2hex(hash, hex, 20);
tl->num = m_nextnum;
bcopy(hash, tl->hash, 20);
m_nextnum++;
m_ntlibs++;
numtbl_insert(m_numtbl, tl);
hashtbl_insert(m_hashtbl, tl);
return tl;
}
int
tlib_del(struct tlib *tl)
{
char relpath[RELPATH_SIZE];
char cmd[PATH_MAX];
assert(tl->tp == NULL);
snprintf(cmd, PATH_MAX, "rm -r torrents/%s",
bin2hex(tl->hash, relpath, 20));
system(cmd);
tlib_kill(tl);
return 0;
}
static void
dct_subst_save(FILE *fp, const char *dct1, const char *dct2)
{
fprintf(fp, "d");
const char *k1 = benc_first(dct1), *k2 = benc_first(dct2);
const char *val, *str, *rest;
size_t len;
while (k1 != NULL && k2 != NULL) {
int test = benc_strcmp(k1, k2);
if (test < 0) {
str = benc_mem(k1, &len, &val);
fprintf(fp, "%d:%.*s", (int)len, (int)len, str);
fwrite(val, 1, benc_length(val), fp);
k1 = benc_next(val);
} else {
str = benc_mem(k2, &len, &val);
fprintf(fp, "%d:%.*s", (int)len, (int)len, str);
fwrite(val, 1, benc_length(val), fp);
k2 = benc_next(val);
if (test == 0)
k1 = benc_next(benc_next(k1));
}
}
rest = k1 != NULL ? k1 : k2;
while (rest != NULL) {
str = benc_mem(rest, &len, &val);
fprintf(fp, "%d:%.*s", (int)len, (int)len, str);
fwrite(val, 1, benc_length(val), fp);
rest = benc_next(val);
}
fprintf(fp, "e");
}
static int
valid_info(char *buf, size_t len)
{
size_t slen;
const char *info;
if (benc_validate(buf, len) != 0)
return 0;
if ((info = benc_dget_dct(buf, "info")) == NULL)
return 0;
if (benc_dget_mem(info, "name", &slen) == NULL || slen == 0)
return 0;
if ((benc_dget_mem(info, "dir", &slen) == NULL ||
(slen == 0 || slen >= PATH_MAX)))
return 0;
return 1;
}
static void
load_info(struct tlib *tl, const char *path)
{
size_t size = 1 << 14;
char buf[size], *p = buf;
const char *info;
if ((errno = read_whole_file((void **)&p, &size, path)) != 0) {
btpd_log(BTPD_L_ERROR, "couldn't load '%s' (%s).\n", path,
strerror(errno));
return;
}
if (!valid_info(buf, size)) {
btpd_log(BTPD_L_ERROR, "bad info file '%s'.\n", path);
return;
}
info = benc_dget_dct(buf, "info");
tl->name = benc_dget_str(info, "name", NULL);
tl->dir = benc_dget_str(info, "dir", NULL);
tl->tot_up = benc_dget_int(info, "total upload");
tl->tot_down = benc_dget_int(info, "total download");
tl->content_size = benc_dget_int(info, "content size");
tl->content_have = benc_dget_int(info, "content have");
if (tl->name == NULL || tl->dir == NULL)
btpd_err("Out of memory.\n");
}
static void
save_info(struct tlib *tl)
{
FILE *fp;
char relpath[SHAHEXSIZE], path[PATH_MAX], wpath[PATH_MAX];
char *old = NULL;
size_t size = 1 << 14;
struct io_buffer iob = buf_init(1 << 10);
buf_print(&iob,
"d4:infod"
"12:content havei%llde12:content sizei%llde"
"3:dir%d:%s4:name%d:%s"
"14:total downloadi%llde12:total uploadi%llde"
"ee",
tl->content_have, tl->content_size,
(int)strlen(tl->dir), tl->dir, (int)strlen(tl->name), tl->name,
tl->tot_down, tl->tot_up);
if (iob.error)
btpd_err("Out of memory.\n");
if ((errno = read_whole_file((void **)&old, &size, path)) != 0
&& errno != ENOENT)
btpd_log(BTPD_L_ERROR, "couldn't load '%s' (%s).\n", path,
strerror(errno));
bin2hex(tl->hash, relpath, 20);
snprintf(path, PATH_MAX, "torrents/%s/info", relpath);
snprintf(wpath, PATH_MAX, "%s.write", path);
if ((fp = fopen(wpath, "w")) == NULL)
btpd_err("failed to open '%s' (%s).\n", wpath, strerror(errno));
if (old != NULL) {
dct_subst_save(fp, old, iob.buf);
free(old);
} else
dct_subst_save(fp, "de", iob.buf);
buf_free(&iob);
if (ferror(fp) || fclose(fp) != 0)
btpd_err("failed to write '%s'.\n", wpath);
if (rename(wpath, path) != 0)
btpd_err("failed to rename: '%s' -> '%s' (%s).\n", wpath, path,
strerror(errno));
}
void
tlib_update_info(struct tlib *tl)
{
assert(tl->tp != NULL);
tl->tot_down += tl->tp->net->downloaded;
tl->tot_up += tl->tp->net->uploaded;
tl->content_have = cm_content(tl->tp);
tl->content_size = tl->tp->total_length;
save_info(tl);
}
static void
write_torrent(const char *mi, size_t mi_size, const char *path)
{
FILE *fp;
if ((fp = fopen(path, "w")) == NULL)
goto err;
if (fwrite(mi, mi_size, 1, fp) != 1) {
errno = EIO;
goto err;
}
if (fclose(fp) != 0)
goto err;
return;
err:
btpd_err("failed to write metainfo '%s' (%s).\n", path, strerror(errno));
}
struct tlib *
tlib_add(const uint8_t *hash, const char *mi, size_t mi_size,
const char *content, char *name)
{
struct tlib *tl = tlib_create(hash);
char relpath[RELPATH_SIZE], file[PATH_MAX];
bin2hex(hash, relpath, 20);
if (name == NULL)
if ((name = mi_name(mi)) == NULL)
btpd_err("out of memory.\n");
tl->content_size = mi_total_length(mi);
tl->name = name;
tl->dir = strdup(content);
if (tl->name == NULL || tl->dir == NULL)
btpd_err("out of memory.\n");
snprintf(file, PATH_MAX, "torrents/%s", relpath);
if (mkdir(file, 0777) != 0)
btpd_err("failed to create dir '%s' (%s).\n", file, strerror(errno));
snprintf(file, PATH_MAX, "torrents/%s/torrent", relpath);
write_torrent(mi, mi_size, file);
save_info(tl);
return tl;
}
static int
num_test(const void *k1, const void *k2)
{
return *(const unsigned *)k1 == *(const unsigned *)k2;
}
static uint32_t
num_hash(const void *k)
{
return *(const unsigned *)k;
}
static int
id_test(const void *k1, const void *k2)
{
return bcmp(k1, k2, 20) == 0;
}
static uint32_t
id_hash(const void *k)
{
return net_read32(k + 16);
}
void
tlib_put_all(struct tlib **v)
{
hashtbl_tov(m_hashtbl, v);
}
void
tlib_init(void)
{
DIR *dirp;
struct dirent *dp;
uint8_t hash[20];
char file[PATH_MAX];
m_numtbl = numtbl_create(num_test, num_hash);
m_hashtbl = hashtbl_create(id_test, id_hash);
if (m_numtbl == NULL || m_hashtbl == NULL)
btpd_err("Out of memory.\n");
if ((dirp = opendir("torrents")) == NULL)
btpd_err("couldn't open the torrents directory.\n");
while ((dp = readdir(dirp)) != NULL) {
if (dp->d_namlen == 40 && ishex(dp->d_name)) {
struct tlib * tl = tlib_create(hex2bin(dp->d_name, hash, 20));
snprintf(file, PATH_MAX, "torrents/%s/info", dp->d_name);
load_info(tl, file);
}
}
closedir(dirp);
}
|