Lines 3-13
Link Here
|
3 |
#include <stdlib.h> |
3 |
#include <stdlib.h> |
4 |
#include <string.h> |
4 |
#include <string.h> |
5 |
|
5 |
|
|
|
6 |
#include "btstream.h" |
6 |
#include "./btcontent.h" |
7 |
#include "./btcontent.h" |
7 |
#include "./msgencode.h" |
8 |
#include "./msgencode.h" |
8 |
#include "./peerlist.h" |
9 |
#include "./peerlist.h" |
9 |
#include "./btconfig.h" |
10 |
#include "./btconfig.h" |
10 |
|
11 |
|
|
|
12 |
size_t get_nl(char *sfrom) |
13 |
{ |
14 |
unsigned char *from = (unsigned char *)sfrom; |
15 |
size_t t; |
16 |
t = (*from++) << 24; |
17 |
t |= (*from++) << 16; |
18 |
t |= (*from++) << 8; |
19 |
t |= *from; |
20 |
return t; |
21 |
} |
22 |
|
23 |
void set_nl(char *sto, size_t from) |
24 |
{ |
25 |
unsigned char *to = (unsigned char *)sto; |
26 |
*to++ = (from >> 24) & 0xff; |
27 |
*to++ = (from >> 16) & 0xff; |
28 |
*to++ = (from >> 8) & 0xff; |
29 |
*to = from & 0xff; |
30 |
} |
31 |
|
11 |
btBasic Self; |
32 |
btBasic Self; |
12 |
|
33 |
|
13 |
void btBasic::SetIp(struct sockaddr_in addr) |
34 |
void btBasic::SetIp(struct sockaddr_in addr) |
Lines 152-158
Link Here
|
152 |
|
173 |
|
153 |
char *msgbuf = stream.in_buffer.BasePointer(); |
174 |
char *msgbuf = stream.in_buffer.BasePointer(); |
154 |
|
175 |
|
155 |
r = ntohl(*(size_t*) msgbuf); |
176 |
r = get_nl(msgbuf); |
|
|
177 |
|
156 |
|
178 |
|
157 |
if( 0 == r ){ |
179 |
if( 0 == r ){ |
158 |
time(&m_last_timestamp); |
180 |
time(&m_last_timestamp); |
Lines 193-199
Link Here
|
193 |
case M_HAVE: |
215 |
case M_HAVE: |
194 |
if(H_HAVE_LEN != r){return -1;} |
216 |
if(H_HAVE_LEN != r){return -1;} |
195 |
|
217 |
|
196 |
idx = ntohl(*(size_t*) (msgbuf + 5)); |
218 |
idx = get_nl(msgbuf + 5); |
197 |
|
219 |
|
198 |
if( idx >= BTCONTENT.GetNPieces() || bitfield.IsSet(idx)) return -1; |
220 |
if( idx >= BTCONTENT.GetNPieces() || bitfield.IsSet(idx)) return -1; |
199 |
|
221 |
|
Lines 208-219
Link Here
|
208 |
case M_REQUEST: |
230 |
case M_REQUEST: |
209 |
if(H_REQUEST_LEN != r || !m_state.remote_interested){ return -1; } |
231 |
if(H_REQUEST_LEN != r || !m_state.remote_interested){ return -1; } |
210 |
|
232 |
|
211 |
idx = ntohl(*(size_t*)(msgbuf + 5)); |
233 |
idx = get_nl(msgbuf + 5); |
212 |
|
234 |
|
213 |
if( !BTCONTENT.pBF->IsSet(idx) ) return -1; |
235 |
if( !BTCONTENT.pBF->IsSet(idx) ) return -1; |
214 |
|
236 |
|
215 |
off = ntohl(*(size_t*)(msgbuf + 9)); |
237 |
off = get_nl(msgbuf + 9); |
216 |
len = ntohl(*(size_t*)(msgbuf + 13)); |
238 |
len = get_nl(msgbuf + 13); |
217 |
|
239 |
|
218 |
if( !reponse_q.IsValidRequest(idx, off, len) ) return -1; |
240 |
if( !reponse_q.IsValidRequest(idx, off, len) ) return -1; |
219 |
|
241 |
|
Lines 235-243
Link Here
|
235 |
case M_CANCEL: |
257 |
case M_CANCEL: |
236 |
if(r != H_CANCEL_LEN || !m_state.remote_interested) return -1; |
258 |
if(r != H_CANCEL_LEN || !m_state.remote_interested) return -1; |
237 |
|
259 |
|
238 |
idx = ntohl(*(size_t*)(msgbuf + 5)); |
260 |
idx = get_nl(msgbuf + 5); |
239 |
off = ntohl(*(size_t*)(msgbuf + 9)); |
261 |
off = get_nl(msgbuf + 9); |
240 |
len = ntohl(*(size_t*)(msgbuf + 13)); |
262 |
len = get_nl(msgbuf + 13); |
241 |
if( reponse_q.Remove(idx,off,len) < 0 ){ |
263 |
if( reponse_q.Remove(idx,off,len) < 0 ){ |
242 |
m_err_count++; |
264 |
m_err_count++; |
243 |
return 0; |
265 |
return 0; |
Lines 312-319
Link Here
|
312 |
size_t idx,off,len; |
334 |
size_t idx,off,len; |
313 |
char *msgbuf = stream.in_buffer.BasePointer(); |
335 |
char *msgbuf = stream.in_buffer.BasePointer(); |
314 |
|
336 |
|
315 |
idx = ntohl(*(size_t*) (msgbuf + 5)); |
337 |
idx = get_nl(msgbuf + 5); |
316 |
off = ntohl(*(size_t*) (msgbuf + 9)); |
338 |
off = get_nl(msgbuf + 9); |
317 |
len = mlen - 9; |
339 |
len = mlen - 9; |
318 |
|
340 |
|
319 |
if( request_q.Remove(idx,off,len) < 0 ){ |
341 |
if( request_q.Remove(idx,off,len) < 0 ){ |