-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtracker_http_response_reader.c
More file actions
183 lines (154 loc) · 4.86 KB
/
tracker_http_response_reader.c
File metadata and controls
183 lines (154 loc) · 4.86 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
/**
* Copyright (c) 2011, Willem-Hendrik Thiart
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
* @file
* @brief Manage connection with tracker
* @author Willem Thiart himself@willemthiart.com
* @version 0.1
*/
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <setjmp.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "bencode.h"
#include "tracker_client.h"
#include "tracker_client_private.h"
static void __do_peer_list(
trackerclient_t* me,
bencode_t * list
)
{
const char *peerid, *ip;
long int port;
int peerid_len, ip_len;
port = 0;
while (bencode_list_has_next(list))
{
bencode_t dict;
bencode_list_get_next(list, &dict);
while (bencode_dict_has_next(&dict))
{
const char *key;
int klen;
bencode_t benk;
bencode_dict_get_next(&dict, &benk, &key, &klen);
if (!strncmp(key, "peer id", klen))
{
bencode_string_value(&benk, &peerid, &peerid_len);
}
else if (!strncmp(key, "ip", klen))
{
bencode_string_value(&benk, &ip, &ip_len);
}
else if (!strncmp(key, "port", klen))
{
bencode_int_value(&benk, &port);
}
}
if (peerid && ip && port != 0)
{
assert(peerid_len == 20);
// me->funcs.add_peer(me->caller, peerid, peerid_len, ip, ip_len, port);
}
else
{
assert(0);
}
}
}
int trackerclient_read_tracker_response(
trackerclient_t* me,
const char *buf,
int len
)
{
bencode_t ben;
bencode_init(&ben, buf, len);
if (!bencode_is_dict(&ben))
{
return 0;
}
while (bencode_dict_has_next(&ben))
{
int klen;
const char *key;
bencode_t benk;
if (0 == bencode_dict_get_next(&ben, &benk, &key, &klen))
{
// ERROR dict is invalid
return 0;
}
/* This key is OPTIONAL. If present, the dictionary MUST NOT contain
* any other keys. The peer should interpret this as if the attempt to
* join the torrent failed. The value is a human readable string
* containing an error message with the failure reason. */
if (!strncmp(key, "failure reason", klen))
{
int len;
const char *val;
bencode_string_value(&benk, &val, &len);
return 1;
}
/* A peer must send regular HTTP GET requests to the tracker to obtain
* an updated list of peers and update the tracker of its status. The
* value of this key indicated the amount of time that a peer should
* wait between to consecutive regular requests. This key is REQUIRED*/
else if (!strncmp(key, "interval", klen))
{
long int interval;
bencode_int_value(&benk, &interval);
}
/* This is an integer that indicates the number of seeders. This key is
* OPTIONAL. */
else if (!strncmp(key, "complete", klen))
{
long int ncomplete_peers;
bencode_int_value(&benk, &ncomplete_peers);
}
/* This is an integer that indicates the number of peers downloading
* the torrent. This key is OPTIONAL. */
else if (!strncmp(key, "incomplete", klen))
{
long int nincomplete_peers;
bencode_int_value(&benk, &nincomplete_peers);
}
/*
* This is a bencoded list of dictionaries containing a list of peers
* that must be contacted in order to download a file. This key is
* REQUIRED. It has the following structure: */
else if (!strncmp(key, "peers", klen))
{
/* compact=0 */
if (bencode_is_list(&benk))
{
__do_peer_list(me, &benk);
}
/* compact=1 */
/* use the bittorrent compact peer format listing */
else if (bencode_is_string(&benk))
{
int len, ii;
const unsigned char *val;
if (0 == bencode_string_value(&benk, (const char **) &val, &len))
{
// ERROR: string is invalid
return 0;
}
for (ii = 0; ii < len; ii += 6, val += 6)
{
char ip[32];
sprintf(ip, "%d.%d.%d.%d", val[0], val[1], val[2], val[3]);
if (!strcmp(ip,"0.0.0.0")) continue;
me->on_add_peer(me->callee, NULL, 0, ip, strlen(ip),
((int) val[4] << 8) | (int) val[5]);
}
}
}
}
return 1;
}