From e431e4c32bdc0e789c552d266c3891c7ca0c359c Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 30 Mar 2022 17:04:54 -0700 Subject: [PATCH] ... --- Makefile.am | 1 + inc/net.h | 11 ++++--- inc/peer.h | 70 ++++++++++++++++++++++------------------- src/net/expected.c | 45 ++++++++++++++------------- src/net/handler.c | 47 ++++++++++++++-------------- src/net/loop.c | 4 ++- src/net/peek.c | 20 ++++++++++++ src/net/queue.c | 8 ++--- src/net/send.c | 6 ++-- src/net/wait.c | 6 ++-- src/peer/bitfield.c | 40 ++++++++++++++++++++++-- src/peer/cancel.c | 4 +-- src/peer/choke.c | 16 +++++----- src/peer/handshake.c | 14 ++++----- src/peer/hash.c | 8 ++--- src/peer/hashes.c | 4 +-- src/peer/have.c | 8 ++--- src/peer/init.c | 6 +++- src/peer/interest.c | 16 +++++----- src/peer/keepalive.c | 8 ++--- src/peer/piece.c | 4 +-- src/peer/reject.c | 4 +-- src/peer/request.c | 4 +-- test/unit/net.tests.c | 72 ++++++++++++++++++++++++------------------- 24 files changed, 252 insertions(+), 174 deletions(-) create mode 100644 src/net/peek.c diff --git a/Makefile.am b/Makefile.am index 4d513e6..e102240 100644 --- a/Makefile.am +++ b/Makefile.am @@ -38,6 +38,7 @@ seederd_SOURCES = \ src/net/handler.c \ src/net/listen.c \ src/net/loop.c \ + src/net/peek.c \ src/net/queue.c \ src/net/send.c \ src/net/setup.c \ diff --git a/inc/net.h b/inc/net.h index d69828f..e5f21c7 100644 --- a/inc/net.h +++ b/inc/net.h @@ -37,16 +37,17 @@ struct net_info { } int net_cache(void**,size_t*,void*,size_t); -ssize_t net_expected(int,enum peer_message*); -void net_handler(int,int,struct peer*); +ssize_t net_expected(struct peer*,enum peer_message*); +void net_handler(int,struct peer*); int net_listen_sock(); void net_loop(); -int net_queue(int,int,struct peer*); -int net_send(int,struct peer*,void*,size_t); +int net_peek(struct peer*,void*,size_t); +int net_queue(int,struct peer*); +int net_send(struct peer*,void*,size_t); int net_setup(struct net_info*); int net_start(); int net_tcp(); int net_udp(); -int net_wait(int,struct peer*,void*,size_t); +int net_wait(struct peer*,void*,size_t); #endif diff --git a/inc/peer.h b/inc/peer.h index 212e424..5db463f 100644 --- a/inc/peer.h +++ b/inc/peer.h @@ -11,18 +11,24 @@ #include struct peer { + int sock; + unsigned int handshake : 1; unsigned int choked : 1; unsigned int interested: 1; - uint8_t infohash[PEER_INFOHASH_SIZE]; unsigned char peer_id[PEER_PEER_ID_SIZE]; - void *out; - size_t out_size; + uint8_t infohash[PEER_INFOHASH_SIZE]; + + uint8_t *bitfield; + size_t bitfield_size; void *in; size_t in_size; + + void *out; + size_t out_size; }; enum peer_message { @@ -45,37 +51,37 @@ enum peer_message { #include #include -int peer_bitfield(int,struct peer*); -int peer_bitfield_received(int,struct peer*,ssize_t); +int peer_bitfield(struct peer*); +int peer_bitfield_received(struct peer*,ssize_t); int peer_bitfield_toggle(struct peer*,size_t); -int peer_cancel(int,struct peer*); -int peer_cancel_received(int,struct peer*,ssize_t); -int peer_choke(int,struct peer*); -int peer_choke_received(int,struct peer*,ssize_t); +int peer_cancel(struct peer*); +int peer_cancel_received(struct peer*,ssize_t); +int peer_choke(struct peer*); +int peer_choke_received(struct peer*,ssize_t); void peer_free(struct peer*); -int peer_handshake(int,struct peer*); -int peer_hash_reject(int,struct peer*); -int peer_hash_reject_received(int,struct peer*,ssize_t); -int peer_hash_request(int,struct peer*); -int peer_hash_request_received(int,struct peer*,ssize_t); -int peer_hashes(int,struct peer*); -int peer_hashes_received(int,struct peer*,ssize_t); -int peer_have(int,struct peer*,size_t); -int peer_have_received(int,struct peer*,ssize_t); +int peer_handshake(struct peer*); +int peer_hash_reject(struct peer*); +int peer_hash_reject_received(struct peer*,ssize_t); +int peer_hash_request(struct peer*); +int peer_hash_request_received(struct peer*,ssize_t); +int peer_hashes(struct peer*); +int peer_hashes_received(struct peer*,ssize_t); +int peer_have(struct peer*,size_t); +int peer_have_received(struct peer*,ssize_t); int peer_init(struct peer**); -int peer_interested(int,struct peer*); -int peer_interested_received(int,struct peer*,ssize_t); -int peer_keepalive(int,struct peer*); -int peer_keepalive_received(int,struct peer*,ssize_t); -int peer_not_interested(int,struct peer*); -int peer_not_interested_received(int,struct peer*,ssize_t); -int peer_piece(int,struct peer*); -int peer_piece_received(int,struct peer*,ssize_t); -int peer_reject(int,struct peer*); -int peer_reject_received(int,struct peer*,ssize_t); -int peer_request(int,struct peer*); -int peer_request_received(int,struct peer*,ssize_t); -int peer_unchoke(int,struct peer*); -int peer_unchoke_received(int,struct peer*,ssize_t); +int peer_interested(struct peer*); +int peer_interested_received(struct peer*,ssize_t); +int peer_keepalive(struct peer*); +int peer_keepalive_received(struct peer*,ssize_t); +int peer_not_interested(struct peer*); +int peer_not_interested_received(struct peer*,ssize_t); +int peer_piece(struct peer*); +int peer_piece_received(struct peer*,ssize_t); +int peer_reject(struct peer*); +int peer_reject_received(struct peer*,ssize_t); +int peer_request(struct peer*); +int peer_request_received(struct peer*,ssize_t); +int peer_unchoke(struct peer*); +int peer_unchoke_received(struct peer*,ssize_t); #endif diff --git a/src/net/expected.c b/src/net/expected.c index 5796a43..35a13a8 100644 --- a/src/net/expected.c +++ b/src/net/expected.c @@ -1,33 +1,36 @@ #include -ssize_t net_expected(int sock, enum peer_message *type) { - unsigned char buf[4]; +ssize_t net_expected(struct peer *info, enum peer_message *type) { + unsigned char buf[5]; ssize_t i; + if(NULL==info) { return -1; } + (*type) = PEER_MESSAGE_KEEPALIVE; - - i = recv(sock,buf,5,MSG_PEEK); - if(-1==i) { - if(!((errno==EAGAIN)||(errno==EWOULDBLOCK))) { - perror("recv"); - return -1; - } - // returning 0 here won't affect anything, - // will be treated as a keep alive (although - // message is still coming in, but since MSG_PEEK - // flag is used no data will have been consumed) - // and sock will be returned to epoll + /* + * used to figure out whether 4 or 5 bytes + * received; 0xff is an invalid message + * type + */ + buf[4] = 0xff; + + if(net_peek(info,buf,5)<0) { return -1; } + if((info->in!=NULL)&&(info->in_size<4)) { + /* + * Message should at least have 4 bytes (length prefix of + * the following message). + * + * Returning 0 here won't affect anything, + * will be treated as a keep alive (although + * message is still coming in, but since net_peek + * is used no data will have been consumed) + * and sock will be returned to epoll. + */ return 0; } - /* - * message should at least have 4 bytes (length prefix of - * the following message) - */ - if(i<4) { return 0; } - - if(i==5) { (*type) = (uint8_t)buf[4]; } + if(buf[4]!=0xff) { (*type) = (uint8_t)buf[4]; } i = 0; for(size_t j=0;j<4;j++) { diff --git a/src/net/handler.c b/src/net/handler.c index 2442df1..0802c4b 100644 --- a/src/net/handler.c +++ b/src/net/handler.c @@ -1,64 +1,63 @@ #include -static int send_remaining(int,struct peer*); +static int send_remaining(struct peer*); -void net_handler(int epoll_fd, int sock, struct peer *info) { +void net_handler(int epoll_fd, struct peer *info) { enum peer_message type; ssize_t i; if(epoll_fd<0) { return; } - if(sock<0) { return; } if(NULL==info) { goto close; } if(info->out!=NULL) { goto remaining; } if(!info->handshake) { goto handshake; } - if((i = net_expected(sock,&type))<0) { goto close; } + if((i = net_expected(info,&type))<0) { goto close; } switch(type) { /* these are ordered based on enum peer_message */ case PEER_MESSAGE_KEEPALIVE: - if(peer_keepalive_received(sock,info,i)<0) { goto close; } + if(peer_keepalive_received(info,i)<0) { goto close; } break; case PEER_MESSAGE_CHOKE: - if(peer_choke_received(sock,info,i)<0) { goto close; } + if(peer_choke_received(info,i)<0) { goto close; } break; case PEER_MESSAGE_UNCHOKE: - if(peer_unchoke_received(sock,info,i)<0) { goto close; } + if(peer_unchoke_received(info,i)<0) { goto close; } break; case PEER_MESSAGE_INTERESTED: - if(peer_interested_received(sock,info,i)<0) { goto close; } + if(peer_interested_received(info,i)<0) { goto close; } break; case PEER_MESSAGE_NOT_INTERESTED: - if(peer_not_interested_received(sock,info,i)<0) { goto close; } + if(peer_not_interested_received(info,i)<0) { goto close; } break; case PEER_MESSAGE_HAVE: - if(peer_have_received(sock,info,i)<0) { goto close; } + if(peer_have_received(info,i)<0) { goto close; } break; case PEER_MESSAGE_BITFIELD: - if(peer_bitfield_received(sock,info,i)<0) { goto close; } + if(peer_bitfield_received(info,i)<0) { goto close; } break; case PEER_MESSAGE_REQUEST: - if(peer_request_received(sock,info,i)<0) { goto close; } + if(peer_request_received(info,i)<0) { goto close; } break; case PEER_MESSAGE_PIECE: - if(peer_piece_received(sock,info,i)<0) { goto close; } + if(peer_piece_received(info,i)<0) { goto close; } break; case PEER_MESSAGE_CANCEL: - if(peer_cancel_received(sock,info,i)<0) { goto close; } + if(peer_cancel_received(info,i)<0) { goto close; } break; case PEER_MESSAGE_REJECT: - if(peer_reject_received(sock,info,i)<0) { goto close; } + if(peer_reject_received(info,i)<0) { goto close; } break; case PEER_MESSAGE_HASH_REQUEST: - if(peer_hash_request_received(sock,info,i)<0) { goto close; } + if(peer_hash_request_received(info,i)<0) { goto close; } break; case PEER_MESSAGE_HASHES: - if(peer_hashes_received(sock,info,i)<0) { goto close; } + if(peer_hashes_received(info,i)<0) { goto close; } break; case PEER_MESSAGE_HASH_REJECT: - if(peer_hash_reject_received(sock,info,i)<0) { goto close; } + if(peer_hash_reject_received(info,i)<0) { goto close; } break; default: goto close; @@ -66,22 +65,22 @@ void net_handler(int epoll_fd, int sock, struct peer *info) { goto queue; remaining: - if(send_remaining(sock,info)<0) { goto close; } + if(send_remaining(info)<0) { goto close; } goto queue; handshake: - if(peer_handshake(sock,info)<0) { goto close; } + if(peer_handshake(info)<0) { goto close; } goto queue; queue: - if(net_queue(epoll_fd,sock,info)<0) { goto close; } + if(net_queue(epoll_fd,info)<0) { goto close; } return; close: + close(info->sock); peer_free(info); - close(sock); return; } -static int send_remaining(int sock, struct peer *info) { - if(net_send(sock,info,info->out,info->out_size)<0) { return -1; } +static int send_remaining(struct peer *info) { + if(net_send(info,info->out,info->out_size)<0) { return -1; } free(info->out); info->out = NULL; diff --git a/src/net/loop.c b/src/net/loop.c index f230b90..0ee81bf 100644 --- a/src/net/loop.c +++ b/src/net/loop.c @@ -38,13 +38,15 @@ static void net_await_epoll_event(struct net_info *info) { return; } + ((struct peer*)ev.data.ptr)->sock = conn_sock; + if(-1==epoll_ctl(info->epoll_fd,EPOLL_CTL_ADD,conn_sock,&ev)) { peer_free(ev.data.ptr); perror("epoll_ctl"); return; } } else { - net_handler(info->epoll_fd,events[n].data.fd,events[n].data.ptr); + net_handler(info->epoll_fd,events[n].data.ptr); } } } diff --git a/src/net/peek.c b/src/net/peek.c new file mode 100644 index 0000000..b17db79 --- /dev/null +++ b/src/net/peek.c @@ -0,0 +1,20 @@ +#include + +int net_peek(struct peer *info, void *p, size_t size) { + void *next; + next = malloc(size); + if(NULL==next) { return -1; } + + if(net_wait(info,next,size)<0) { return -1; } + if(info->in!=NULL) { + free(next); + return 0; + } + + info->in = next; + info->in_size = size; + + memcpy(p,next,size); + + return 1; +} diff --git a/src/net/queue.c b/src/net/queue.c index f4f08f0..d1f2367 100644 --- a/src/net/queue.c +++ b/src/net/queue.c @@ -1,17 +1,17 @@ #include -int net_queue(int epoll_fd, int sock, struct peer *info) { +int net_queue(int epoll_fd, struct peer *info) { struct epoll_event ev; if(epoll_fd<0) { return -1; } - if(sock<0) { return -1; } if(NULL==info) { return -1; } + if(info->sock<0) { return -1; } ev.events = EPOLLIN | EPOLLOUT | EPOLLET; ev.data.ptr = info; - ev.data.fd = sock; + ev.data.fd = info->sock; - if(-1==epoll_ctl(epoll_fd,EPOLL_CTL_ADD,sock,&ev)) { + if(-1==epoll_ctl(epoll_fd,EPOLL_CTL_ADD,info->sock,&ev)) { perror("epoll_ctl"); return -1; } diff --git a/src/net/send.c b/src/net/send.c index 1a34cfe..ea1152a 100644 --- a/src/net/send.c +++ b/src/net/send.c @@ -1,13 +1,13 @@ #include -int net_send(int sock, struct peer *info, void *p, size_t size) { +int net_send(struct peer *info, void *p, size_t size) { ssize_t i; - if(sock<0) { return -1; } if(NULL==info) { return -1; } + if(info->sock<0) { return -1; } if(NULL==p) { return -1; } - i = send(sock,p,size,0); + i = send(info->sock,p,size,0); if(-1==i) { if(!((errno==EAGAIN)||(errno==EWOULDBLOCK))) { perror("send"); diff --git a/src/net/wait.c b/src/net/wait.c index 8a96276..e400665 100644 --- a/src/net/wait.c +++ b/src/net/wait.c @@ -2,20 +2,20 @@ static void* use_cached(struct peer*,void*,size_t*); -int net_wait(int sock, struct peer *info, void *buf, size_t buf_size) { +int net_wait(struct peer *info, void *buf, size_t buf_size) { void *p; size_t size; ssize_t i; - if(sock<0) { return -1; } if(NULL==info) { return -1; } + if(info->sock<0) { return -1; } if(NULL==buf) { return -1; } if(0==buf_size) { return 1; } size = buf_size; p = use_cached(info,buf,&size); - i = recv(sock,p,size,0); + i = recv(info->sock,p,size,0); if(-1==i) { if(!((errno==EAGAIN)||(errno==EWOULDBLOCK))) { perror("recv"); diff --git a/src/peer/bitfield.c b/src/peer/bitfield.c index 0aed34f..1c3e8eb 100644 --- a/src/peer/bitfield.c +++ b/src/peer/bitfield.c @@ -1,13 +1,47 @@ #include -int peer_bitfield(int sock, struct peer *info) { +int peer_bitfield(struct peer *info) { return -1; } -int peer_bitfield_received(int sock, struct peer *info, ssize_t len) { +int peer_bitfield_init(struct peer *info, size_t pieces) { + info->bitfield_size = pieces/8; + + info->bitfield = calloc(pieces,sizeof(uint8_t)); + if(NULL==info->bitfield) { return -1; } + + return 1; +} + +int peer_bitfield_received(struct peer *info, ssize_t len) { + uint8_t *buf; + + if((len-5)!=info->bitfield_size) { return -1; } + + buf = malloc(len); + if(NULL==buf) { return -1; } + + if(net_wait(info,buf,len)<0) { goto fail; } + if(info->in!=NULL) { goto clean; } + + for(size_t i=5;ibitfield[i] = buf[i]; + } + + return 1; +fail: + free(buf); return -1; +clean: + free(buf); + return 0; } int peer_bitfield_toggle(struct peer *info, size_t index) { - return -1; + size_t byte_index = index/8; + + index %= 8; + info->bitfield[byte_index] = info->bitfield[byte_index] ^ (1< -int peer_cancel(int sock, struct peer *info) { +int peer_cancel(struct peer *info) { return -1; } -int peer_cancel_received(int sock, struct peer *info, ssize_t len) { +int peer_cancel_received(struct peer *info, ssize_t len) { return -1; } diff --git a/src/peer/choke.c b/src/peer/choke.c index 660635f..51e0673 100644 --- a/src/peer/choke.c +++ b/src/peer/choke.c @@ -1,18 +1,18 @@ #include -int peer_choke(int sock, struct peer *info) { +int peer_choke(struct peer *info) { unsigned char buf[5] = "\x00\x00\x00\x01"; buf[4] = PEER_MESSAGE_CHOKE; - return net_send(sock,info,buf,5); + return net_send(info,buf,5); } -int peer_choke_received(int sock, struct peer *info, ssize_t len) { +int peer_choke_received(struct peer *info, ssize_t len) { unsigned char buf[5]; if(5!=len) { return -1; } - if(net_wait(sock,info,buf,5)<0) { return -1; } + if(net_wait(info,buf,5)<0) { return -1; } if(info->in!=NULL) { return 0; } info->choked = 1; @@ -20,19 +20,19 @@ int peer_choke_received(int sock, struct peer *info, ssize_t len) { return 1; } -int peer_unchoke(int sock, struct peer *info) { +int peer_unchoke(struct peer *info) { unsigned char buf[5] = "\x00\x00\x00\x01"; buf[4] = PEER_MESSAGE_UNCHOKE; - return net_send(sock,info,buf,5); + return net_send(info,buf,5); } -int peer_unchoke_received(int sock, struct peer *info, ssize_t len) { +int peer_unchoke_received(struct peer *info, ssize_t len) { unsigned char buf[5]; if(5!=len) { return -1; } - if(net_wait(sock,info,buf,5)<0) { return -1; } + if(net_wait(info,buf,5)<0) { return -1; } if(info->in!=NULL) { return 0; } info->choked = 0; diff --git a/src/peer/handshake.c b/src/peer/handshake.c index 73967bc..59aaff9 100644 --- a/src/peer/handshake.c +++ b/src/peer/handshake.c @@ -5,14 +5,12 @@ #define HEADER_LENGTH HANDSHAKE_STRING_LENGTH+PEER_INFOHASH_SIZE+PEER_PEER_ID_SIZE -static int peer_handshake_receive(int,struct peer*); +static int peer_handshake_receive(struct peer*); -int peer_handshake(int sock, struct peer *info) { +int peer_handshake(struct peer *info) { char header[HEADER_LENGTH] = HANDSHAKE_STRING; - if(sock<0) { return -1; } - - if(peer_handshake_receive(sock,info)<0) { return -1; } + if(peer_handshake_receive(info)<0) { return -1; } if(!info->handshake) { return 0; } // copy infohash to buffer @@ -27,17 +25,17 @@ int peer_handshake(int sock, struct peer *info) { info->peer_id, PEER_PEER_ID_SIZE); - if(net_send(sock,info,header,HEADER_LENGTH)<0) { return -1; } + if(net_send(info,header,HEADER_LENGTH)<0) { return -1; } return 1; } -static int peer_handshake_receive(int sock, struct peer *info) { +static int peer_handshake_receive(struct peer *info) { char header[HEADER_LENGTH]; struct torrent *p; int i; - if((i = net_wait(sock,info,header,HEADER_LENGTH))<=0) { + if((i = net_wait(info,header,HEADER_LENGTH))<=0) { if(i<0) { return -1; } return 0; } diff --git a/src/peer/hash.c b/src/peer/hash.c index efccb0d..88574d3 100644 --- a/src/peer/hash.c +++ b/src/peer/hash.c @@ -1,17 +1,17 @@ #include -int peer_hash_request(int sock, struct peer *info) { +int peer_hash_request(struct peer *info) { return -1; } -int peer_hash_request_received(int sock, struct peer *info, ssize_t len) { +int peer_hash_request_received(struct peer *info, ssize_t len) { return -1; } -int peer_hash_reject(int sock, struct peer *info) { +int peer_hash_reject(struct peer *info) { return -1; } -int peer_hash_reject_received(int sock, struct peer *info, ssize_t len) { +int peer_hash_reject_received(struct peer *info, ssize_t len) { return -1; } diff --git a/src/peer/hashes.c b/src/peer/hashes.c index e4b20cf..750bba5 100644 --- a/src/peer/hashes.c +++ b/src/peer/hashes.c @@ -1,9 +1,9 @@ #include -int peer_hashes(int sock, struct peer *info) { +int peer_hashes(struct peer *info) { return -1; } -int peer_hashes_received(int sock, struct peer *info, ssize_t len) { +int peer_hashes_received(struct peer *info, ssize_t len) { return -1; } diff --git a/src/peer/have.c b/src/peer/have.c index d0a183b..d4bca89 100644 --- a/src/peer/have.c +++ b/src/peer/have.c @@ -1,6 +1,6 @@ #include -int peer_have(int sock, struct peer *info, size_t index) { +int peer_have(struct peer *info, size_t index) { unsigned char buf[9] = "\x00\x00\x00\x05"; buf[4] = PEER_MESSAGE_HAVE; @@ -9,16 +9,16 @@ int peer_have(int sock, struct peer *info, size_t index) { index >>= 8; } - return net_send(sock,info,buf,9); + return net_send(info,buf,9); } -int peer_have_received(int sock, struct peer *info, ssize_t len) { +int peer_have_received(struct peer *info, ssize_t len) { unsigned char buf[9]; size_t index; if(9!=len) { return -1; } - if(net_wait(sock,info,buf,9)<0) { return -1; } + if(net_wait(info,buf,9)<0) { return -1; } if(info->in!=NULL) { return 0; } index = 0; diff --git a/src/peer/init.c b/src/peer/init.c index b51ea38..ef82f9a 100644 --- a/src/peer/init.c +++ b/src/peer/init.c @@ -10,8 +10,12 @@ int peer_init(struct peer **p) { (*p)->choked = 0; (*p)->interested = 0; - memset((*p)->infohash,0,PEER_INFOHASH_SIZE); memset((*p)->peer_id,0,PEER_PEER_ID_SIZE); + + memset((*p)->infohash,0,PEER_INFOHASH_SIZE); + + (*p)->bitfield = NULL; + (*p)->bitfield_size = 0; (*p)->out = NULL; (*p)->out_size = 0; diff --git a/src/peer/interest.c b/src/peer/interest.c index b9b605b..6fe1c8a 100644 --- a/src/peer/interest.c +++ b/src/peer/interest.c @@ -1,18 +1,18 @@ #include -int peer_interested(int sock, struct peer *info) { +int peer_interested(struct peer *info) { unsigned char buf[5] = "\x00\x00\x00\x01"; buf[4] = PEER_MESSAGE_INTERESTED; - return net_send(sock,info,buf,5); + return net_send(info,buf,5); } -int peer_interested_received(int sock, struct peer *info, ssize_t len) { +int peer_interested_received(struct peer *info, ssize_t len) { unsigned char buf[5]; if(5!=len) { return -1; } - if(net_wait(sock,info,buf,5)<0) { return -1; } + if(net_wait(info,buf,5)<0) { return -1; } if(info->in!=NULL) { return 0; } info->interested = 1; @@ -20,19 +20,19 @@ int peer_interested_received(int sock, struct peer *info, ssize_t len) { return 1; } -int peer_not_interested(int sock, struct peer *info) { +int peer_not_interested(struct peer *info) { unsigned char buf[5] = "\x00\x00\x00\x01"; buf[4] = PEER_MESSAGE_NOT_INTERESTED; - return net_send(sock,info,buf,5); + return net_send(info,buf,5); } -int peer_not_interested_received(int sock, struct peer *info, ssize_t len) { +int peer_not_interested_received(struct peer *info, ssize_t len) { unsigned char buf[5]; if(5!=len) { return -1; } - if(net_wait(sock,info,buf,5)<0) { return -1; } + if(net_wait(info,buf,5)<0) { return -1; } if(info->in!=NULL) { return 0; } info->interested = 0; diff --git a/src/peer/keepalive.c b/src/peer/keepalive.c index 0df83fa..e0ac2e9 100644 --- a/src/peer/keepalive.c +++ b/src/peer/keepalive.c @@ -1,17 +1,17 @@ #include -int peer_keepalive(int sock, struct peer *info) { +int peer_keepalive(struct peer *info) { unsigned char buf[4] = "\x00\x00\x00\x00"; - return net_send(sock,info,buf,4); + return net_send(info,buf,4); } -int peer_keepalive_received(int sock, struct peer *info, ssize_t len) { +int peer_keepalive_received(struct peer *info, ssize_t len) { unsigned char buf[4]; if(4!=len) { return -1; } - if(net_wait(sock,info,buf,4)<0) { return -1; } + if(net_wait(info,buf,4)<0) { return -1; } if(info->in!=NULL) { return 0; } return 1; diff --git a/src/peer/piece.c b/src/peer/piece.c index 8715ea3..0e4a1a2 100644 --- a/src/peer/piece.c +++ b/src/peer/piece.c @@ -1,9 +1,9 @@ #include -int peer_piece(int sock, struct peer *info) { +int peer_piece(struct peer *info) { return -1; } -int peer_piece_received(int sock, struct peer *info, ssize_t len) { +int peer_piece_received(struct peer *info, ssize_t len) { return -1; } diff --git a/src/peer/reject.c b/src/peer/reject.c index 8ee1611..a256107 100644 --- a/src/peer/reject.c +++ b/src/peer/reject.c @@ -1,9 +1,9 @@ #include -int peer_reject(int sock, struct peer *info) { +int peer_reject(struct peer *info) { return -1; } -int peer_reject_received(int sock, struct peer *info, ssize_t len) { +int peer_reject_received(struct peer *info, ssize_t len) { return -1; } diff --git a/src/peer/request.c b/src/peer/request.c index 1a71142..82a527b 100644 --- a/src/peer/request.c +++ b/src/peer/request.c @@ -1,9 +1,9 @@ #include -int peer_request(int sock, struct peer *info) { +int peer_request(struct peer *info) { return -1; } -int peer_request_received(int sock, struct peer *info, ssize_t len) { +int peer_request_received(struct peer *info, ssize_t len) { return -1; } diff --git a/test/unit/net.tests.c b/test/unit/net.tests.c index f3557ed..d91570f 100644 --- a/test/unit/net.tests.c +++ b/test/unit/net.tests.c @@ -8,6 +8,7 @@ static void net_cache_basic_test(); static void net_expected_basic_test(); static void net_handler_basic_test(); static void net_handler_invalid_message_test(); +static void net_peek_basic_test(); static void net_queue_basic_test(); static void net_send_basic_test(); static void net_wait_basic_test(); @@ -41,6 +42,7 @@ int main() { net_expected_basic_test(); net_handler_basic_test(); net_handler_invalid_message_test(); + net_peek_basic_test(); net_queue_basic_test(); net_send_basic_test(); net_wait_basic_test(); @@ -98,22 +100,23 @@ static void net_expected_basic_test() { enum peer_message type; unsigned char buf[10]; - assert(1==peer_init(&info)); - setup_sockets(); + assert(1==peer_init(&info)); + info->sock = read_sock; + /* verify messages with <4 bytes returns keepalive */ unsigned char expected1[4] = "\x00\x00\x00\x00"; assert(3==write(write_sock,expected1,3)); - assert(0==net_expected(read_sock,&type)); + assert(0==net_expected(info,&type)); assert(PEER_MESSAGE_KEEPALIVE==type); assert(3==read(read_sock,buf,sizeof(buf))); /* verify keepalive returns as expected */ assert(4==write(write_sock,expected1,4)); - assert(4==net_expected(read_sock,&type)); + assert(4==net_expected(info,&type)); assert(PEER_MESSAGE_KEEPALIVE==type); assert(4==read(read_sock,buf,sizeof(buf))); @@ -153,7 +156,7 @@ static void net_expected_basic_test() { #define NET_HANDLER_HELPER(message,type) { \ assert(sizeof(message)==write(write_sock,message,sizeof(message))); \ \ - net_handler(epoll_fd,read_sock,info); \ + net_handler(epoll_fd,info); \ assert(0==epoll_ctl(epoll_fd,EPOLL_CTL_DEL,read_sock,NULL)); \ assert(sizeof(message)==read(read_sock,buf,sizeof(buf))); \ \ @@ -164,19 +167,20 @@ static void net_handler_invalid_message_test() { struct peer *info; int epoll_fd; + setup_sockets(); + assert(1==peer_init(&info)); info->handshake = 1; + info->sock = read_sock; epoll_fd = epoll_create1(0); assert(epoll_fd!=-1); - setup_sockets(); - memset(message_count, 0, sizeof(message_count)); unsigned char invalid_message_type[5] = "\x00\x00\x00\x01\xff"; assert(5==write(write_sock,invalid_message_type,5)); - net_handler(epoll_fd,read_sock,info); + net_handler(epoll_fd,info); for(size_t i=0;i<25;i++) { assert(0==message_count[i]); @@ -190,16 +194,17 @@ static void net_handler_basic_test() { unsigned char buf[10]; int epoll_fd; + setup_sockets(); + memset(message_count, 0, sizeof(message_count)); assert(1==peer_init(&info)); info->handshake = 1; + info->sock = read_sock; epoll_fd = epoll_create1(0); assert(epoll_fd!=-1); - setup_sockets(); - /* all peer_message enums indexes are +1 in message_count */ unsigned char keepalive_message[4] = "\x00\x00\x00\x00"; NET_HANDLER_HELPER(keepalive_message,PEER_MESSAGE_KEEPALIVE); @@ -248,23 +253,26 @@ static void net_handler_basic_test() { close(write_sock); } +static void net_peek_basic_test() { + struct peer *info; + +} + static void net_queue_basic_test() { int epoll_fd; int sock; struct peer *info; assert(1==peer_init(&info)); + info->sock = 0; epoll_fd = epoll_create1(0); assert(epoll_fd!=-1); - sock = 0; - - assert(-1==net_queue(-1,-1,NULL)); - assert(-1==net_queue(epoll_fd,-1,NULL)); - assert(-1==net_queue(epoll_fd,sock,NULL)); + assert(-1==net_queue(-1,NULL)); + assert(-1==net_queue(epoll_fd,NULL)); - assert(1==net_queue(epoll_fd,sock,info)); + assert(1==net_queue(epoll_fd,info)); peer_free(info); close(epoll_fd); @@ -281,17 +289,18 @@ static void net_send_basic_test() { memset(expected,0,100); assert(1==peer_init(&info)); + info->sock = write_sock; assert(-1==net_send(-1,NULL,NULL,0)); - assert(-1==net_send(write_sock,info,NULL,0)); + assert(-1==net_send(info,NULL,0)); - assert(1==net_send(write_sock,info,buf,0)); + assert(1==net_send(info,buf,0)); assert(-1==read(read_sock,expected,100)); - assert(1==net_send(write_sock,info,buf,50)); + assert(1==net_send(info,buf,50)); assert(50==read(read_sock,expected,100)); - assert(1==net_send(write_sock,info,buf,100)); + assert(1==net_send(info,buf,100)); assert(100==read(read_sock,expected,100)); close(read_sock); @@ -312,46 +321,47 @@ static void net_wait_basic_test() { memset(expected,0,100); assert(1==peer_init(&info)); + info->sock = read_sock; - assert(-1==net_wait(-1,NULL,NULL,0)); - assert(-1==net_wait(read_sock,NULL,NULL,0)); - assert(-1==net_wait(read_sock,info,NULL,0)); - assert(1==net_wait(read_sock,info,expected,0)); + assert(-1==net_wait(NULL,NULL,0)); + assert(-1==net_wait(NULL,NULL,0)); + assert(-1==net_wait(info,NULL,0)); + assert(1==net_wait(info,expected,0)); - assert(0==net_wait(read_sock,info,buf,100)); + assert(0==net_wait(info,buf,100)); assert(50==write(write_sock,expected,50)); - assert(0==net_wait(read_sock,info,buf,100)); + assert(0==net_wait(info,buf,100)); assert(info->in!=NULL); assert(info->in_size==50); assert(memcmp(buf,expected,100)!=0); assert(25==write(write_sock,expected,25)); - assert(0==net_wait(read_sock,info,buf,100)); + assert(0==net_wait(info,buf,100)); assert(info->in!=NULL); assert(info->in_size==75); assert(memcmp(buf,expected,100)!=0); assert(25==write(write_sock,expected,25)); - assert(1==net_wait(read_sock,info,buf,100)); + assert(1==net_wait(info,buf,100)); assert(info->in==NULL); assert(memcmp(buf,expected,100)==0); i = 0; info->in_size = 0; while(i<99) { - assert(0==net_wait(read_sock,info,buf,100)); + assert(0==net_wait(info,buf,100)); increment = rand()%2; i += increment; assert(((increment>0)?1:0)==write(write_sock,expected,increment)); - assert(0==net_wait(read_sock,info,buf,100)); + assert(0==net_wait(info,buf,100)); assert(info->in_size==i); } assert(1==write(write_sock,expected,1)); - assert(1==net_wait(read_sock,info,buf,100)); + assert(1==net_wait(info,buf,100)); assert(info->in==NULL); assert(memcmp(buf,expected,100)==0); -- 2.39.5