From: alex Date: Fri, 1 Apr 2022 01:02:33 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=11b4536a4e24b627a02910cf33e37724e39dc5a7;p=seeder ... --- diff --git a/inc/net.h b/inc/net.h index e5f21c7..96a85e3 100644 --- a/inc/net.h +++ b/inc/net.h @@ -36,6 +36,13 @@ struct net_info { hints.ai_next = NULL; \ } +#define BYTES_TO_UINT32_T(x) \ + (uint32_t) \ + (x[0]<<24) + \ + (x[1]<<16) + \ + (x[2]<<8) + \ + x[3] + int net_cache(void**,size_t*,void*,size_t); ssize_t net_expected(struct peer*,enum peer_message*); void net_handler(int,struct peer*); diff --git a/src/net/expected.c b/src/net/expected.c index 9a4ba5b..6f2507b 100644 --- a/src/net/expected.c +++ b/src/net/expected.c @@ -33,11 +33,7 @@ ssize_t net_expected(struct peer *info, enum peer_message *type) { if(buf[4]!=0xff) { (*type) = (uint8_t)buf[4]; } - i = 0; - for(size_t j=0;j<4;j++) { - i <<= 8; - i += buf[j]; - } + i = BYTES_TO_UINT32_T(buf); return i+4; } diff --git a/src/peer/bitfield.c b/src/peer/bitfield.c index 1c3e8eb..cb79765 100644 --- a/src/peer/bitfield.c +++ b/src/peer/bitfield.c @@ -1,7 +1,17 @@ #include int peer_bitfield(struct peer *info) { - return -1; + uint8_t *buf; + buf = malloc(info->bitfield_size); + if(NULL==buf) { return -1; } + + memset(buf,0xff,info->bitfield_size); + + if(net_send(info,buf,info->bitfield_size)<0) { return -1; } + + free(buf); + + return 1; } int peer_bitfield_init(struct peer *info, size_t pieces) { diff --git a/src/peer/request.c b/src/peer/request.c index 82a527b..d7ce11d 100644 --- a/src/peer/request.c +++ b/src/peer/request.c @@ -5,5 +5,24 @@ int peer_request(struct peer *info) { } int peer_request_received(struct peer *info, ssize_t len) { + unsigned char buf[17]; +// struct peer_request *req; +// unsigned char *p; +// size_t index, begin, length; + + if(net_wait(info,buf,17)<0) { return -1; } + if(info->in!=NULL) { return 0; } + +/* if(peer_request_init(&req)<0) { return -1; } + + p = &(buf[5]); + req->index = BYTES_TO_UINT32_T(p); + p = &(buf[9]); + req->begin = BYTES_TO_UINT32_T(p); + p = &(buf[13]); + req->length = BYTES_TO_UINT32_T(p); + + if(peer_piece_queue(info,req)<0) { return -1; } +*/ return -1; }